-
Notifications
You must be signed in to change notification settings - Fork 4
/
glue_port.h
875 lines (875 loc) · 26.8 KB
/
glue_port.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
#ifndef _EEL_GLUE_PORTABLE_H_
#define _EEL_GLUE_PORTABLE_H_
#define DECL_ASMFUNC(x)
#define GLUE_JMP_TYPE int32_t
#define GLUE_JMP_SET_OFFSET(endOfInstruction,offset) (((GLUE_JMP_TYPE *)(endOfInstruction))[-1] = (offset))
#define GLUE_HAS_FXCH
#define GLUE_MAX_FPSTACK_SIZE 64
#define BIF_FPSTACKUSE(x) (0) // fp stack is not used within functions
#define BIF_GETFP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MP(JMP_IF_P1_NZ)
BC_DECL(RET)
BC_DECL(FXCH)
BC_DECL(POP_FPSTACK)
#define GLUE_POP_FPSTACK_SIZE sizeof(EEL_BC_TYPE)
BC_DECL(PUSH_P1)
BC_DECL(PUSH_P1PTR_AS_VALUE)
BC_DECL(POP_FPSTACK_TOSTACK)
BC_DECL(POP_FPSTACK_TO_WTP)
BC_DECL(SET_P1_Z)
BC_DECL(SET_P1_NZ)
BC_DECL_JMP(LOOP_LOADCNT)
BC_DECL_JMP(LOOP_END)
#define GLUE_LOOP_BEGIN_SIZE 0
#define GLUE_LOOP_BEGIN ((void*)"")
#define GLUE_LOOP_CLAMPCNT_SIZE 0
#define GLUE_LOOP_CLAMPCNT ((void*)"")
BC_DECL(WHILE_SETUP)
#define GLUE_WHILE_SETUP_SIZE sizeof(GLUE_WHILE_SETUP)
BC_DECL_JMP(WHILE_END)
BC_DECL(WHILE_BEGIN);
BC_DECL_JMP(WHILE_CHECK_RV)
#define GLUE_MOV_PX_DIRECTVALUE_SIZE (sizeof(EEL_BC_TYPE) + sizeof(INT_PTR))
#define GLUE_MOV_PX_DIRECTVALUE_TOSTACK_SIZE GLUE_MOV_PX_DIRECTVALUE_SIZE
static void GLUE_MOV_PX_DIRECTVALUE_GEN(void *b, INT_PTR v, int32_t wv)
{
static const EEL_BC_TYPE tab[] = {
EEL_BC_MOV_FPTOP_DV,
EEL_BC_MOV_P1_DV,
EEL_BC_MOV_P2_DV,
EEL_BC_MOV_P3_DV,
};
*(EEL_BC_TYPE *)b = tab[wv+1];
*(INT_PTR *) ((char *)b + sizeof(EEL_BC_TYPE)) = v;
}
static int32_t GLUE_RESET_WTP(unsigned char *out, void *ptr)
{
BC_DECL(_RESET_WTP)
if (out) memcpy(out,&GLUE__RESET_WTP,sizeof(GLUE__RESET_WTP));
if (out) *(void **) (out+sizeof(GLUE__RESET_WTP)) = ptr;
return sizeof(GLUE__RESET_WTP) + sizeof(void *);
}
#define GLUE_POP_PX_SIZE sizeof(EEL_BC_TYPE)
static void GLUE_POP_PX(void *b, int32_t wv)
{
static const EEL_BC_TYPE tab[3] ={
EEL_BC_POP_P1,
EEL_BC_POP_P2,
EEL_BC_POP_P3,
};
*(EEL_BC_TYPE *)b = tab[wv];
}
#define GLUE_SET_PX_FROM_P1_SIZE sizeof(EEL_BC_TYPE)
static void GLUE_SET_PX_FROM_P1(void *b, int32_t wv)
{
static const uint32_t tab[3]={
EEL_BC_NOP,
EEL_BC_SET_P2_FROM_P1,
EEL_BC_SET_P3_FROM_P1,
};
*(EEL_BC_TYPE *)b = tab[wv];
}
#define GLUE_MOVE_STACK_SIZE (sizeof(EEL_BC_TYPE) + sizeof(int32_t))
static void GLUE_MOVE_STACK(void *b, int32_t amt)
{
*(EEL_BC_TYPE *)b = EEL_BC_MOVE_STACK;
*(int32_t *)(((EEL_BC_TYPE *)b)+1) = amt;
}
#define GLUE_STORE_P1_TO_STACK_AT_OFFS_SIZE (sizeof(EEL_BC_TYPE) + sizeof(int32_t))
static void GLUE_STORE_P1_TO_STACK_AT_OFFS(void *b, int32_t offs)
{
*(EEL_BC_TYPE *)b = EEL_BC_STORE_P1_TO_STACK_AT_OFFS;
*(int32_t *)(((EEL_BC_TYPE *)b)+1) = offs;
}
#define GLUE_MOVE_PX_STACKPTR_SIZE sizeof(EEL_BC_TYPE)
static void GLUE_MOVE_PX_STACKPTR_GEN(void *b, int32_t wv)
{
static const EEL_BC_TYPE tab[3] = {
EEL_BC_MOVE_STACKPTR_TO_P1,
EEL_BC_MOVE_STACKPTR_TO_P2,
EEL_BC_MOVE_STACKPTR_TO_P3
};
*(EEL_BC_TYPE *)b = tab[wv];
}
static int32_t GLUE_POP_VALUE_TO_ADDR(unsigned char *buf, void *destptr)
{
if (buf)
{
*(EEL_BC_TYPE *)buf = EEL_BC_POP_VALUE_TO_ADDR;
*(void **) (buf+sizeof(EEL_BC_TYPE)) = destptr;
}
return sizeof(EEL_BC_TYPE) + sizeof(void *);
}
static int32_t GLUE_COPY_VALUE_AT_P1_TO_PTR(unsigned char *buf, void *destptr)
{
if (buf)
{
*(EEL_BC_TYPE *)buf = EEL_BC_COPY_VALUE_AT_P1_TO_ADDR;
*(void **) (buf+sizeof(EEL_BC_TYPE)) = destptr;
}
return sizeof(EEL_BC_TYPE) + sizeof(void *);
}
static unsigned char *EEL_GLUE_set_immediate(void *_p, INT_PTR newv)
{
int32_t mv=5;
char *p=(char*)_p;
p+=sizeof(EEL_BC_TYPE);
while (*(INT_PTR*)p && mv-- > 0) p++;
if (!mv) return (unsigned char *)p;
*(INT_PTR *)p = newv;
return (unsigned char *) p + sizeof(INT_PTR) - sizeof(EEL_BC_TYPE);
}
#define GLUE_SET_PX_FROM_WTP_SIZE sizeof(EEL_BC_TYPE)
static void GLUE_SET_PX_FROM_WTP(void *b, int32_t wv)
{
static const EEL_BC_TYPE tab[3]={
EEL_BC_SET_P1_FROM_WTP,
EEL_BC_SET_P2_FROM_WTP,
EEL_BC_SET_P3_FROM_WTP,
};
*(EEL_BC_TYPE *)b = tab[wv];
}
static int32_t GLUE_POP_FPSTACK_TO_PTR(unsigned char *buf, void *destptr)
{
if (buf)
{
*(EEL_BC_TYPE *)buf = EEL_BC_POP_FPSTACK_TO_PTR;
*(void **) (buf+sizeof(EEL_BC_TYPE)) = destptr;
}
return sizeof(EEL_BC_TYPE) + sizeof(void *);
}
#define GLUE_PUSH_VAL_AT_PX_TO_FPSTACK_SIZE sizeof(EEL_BC_TYPE)
static void GLUE_PUSH_VAL_AT_PX_TO_FPSTACK(void *b, int32_t wv)
{
static const EEL_BC_TYPE tab[3] = {
EEL_BC_PUSH_VAL_AT_P1_TO_FPSTACK,
EEL_BC_PUSH_VAL_AT_P2_TO_FPSTACK,
EEL_BC_PUSH_VAL_AT_P3_TO_FPSTACK,
};
*(EEL_BC_TYPE *)b = tab[wv];
}
#define GLUE_POP_FPSTACK_TO_WTP_TO_PX_SIZE (sizeof(GLUE_POP_FPSTACK_TO_WTP) + GLUE_SET_PX_FROM_WTP_SIZE)
static void GLUE_POP_FPSTACK_TO_WTP_TO_PX(unsigned char *buf, int32_t wv)
{
GLUE_SET_PX_FROM_WTP(buf,wv);
memcpy(buf + GLUE_SET_PX_FROM_WTP_SIZE,GLUE_POP_FPSTACK_TO_WTP,sizeof(GLUE_POP_FPSTACK_TO_WTP));
};
static unsigned char GLUE_POP_STACK_TO_FPSTACK[1] = { 0 }; // todo
// end of bytecode glue, now for stubbage
#define EEL_BC_ENDOF(x) (((char*)(x))+sizeof(x))
#define BC_DECLASM(x,y) static EEL_BC_TYPE nseel_asm_##x[1]={EEL_BC_##y};
BC_DECLASM(band,NOP)
BC_DECLASM(bor,NOP)
BC_DECLASM(bnot,BNOT)
BC_DECLASM(bnotnot,BNOTNOT)
BC_DECLASM(equal,EQUAL)
BC_DECLASM(equal_exact,EQUAL_EXACT)
BC_DECLASM(notequal_exact,NOTEQUAL_EXACT)
BC_DECLASM(notequal,NOTEQUAL)
BC_DECLASM(above,ABOVE)
BC_DECLASM(beloweq,BELOWEQ)
BC_DECLASM(add,ADD)
BC_DECLASM(sub,SUB)
BC_DECLASM(mul,MUL)
BC_DECLASM(div,DIV)
BC_DECLASM(and,AND)
BC_DECLASM(or,OR)
BC_DECLASM(or0,OR0)
BC_DECLASM(xor,XOR)
BC_DECLASM(add_op,ADD_OP)
BC_DECLASM(sub_op,SUB_OP)
BC_DECLASM(add_op_fast,ADD_OP_FAST)
BC_DECLASM(sub_op_fast,SUB_OP_FAST)
BC_DECLASM(mul_op,MUL_OP)
BC_DECLASM(div_op,DIV_OP)
BC_DECLASM(mul_op_fast,MUL_OP_FAST)
BC_DECLASM(div_op_fast,DIV_OP_FAST)
BC_DECLASM(and_op,AND_OP)
BC_DECLASM(or_op,OR_OP)
BC_DECLASM(xor_op,XOR_OP)
BC_DECLASM(uminus,UMINUS)
BC_DECLASM(assign,ASSIGN)
BC_DECLASM(assign_fast,ASSIGN_FAST)
BC_DECLASM(assign_fast_fromfp,ASSIGN_FAST_FROMFP)
BC_DECLASM(assign_fromfp,ASSIGN_FROMFP)
BC_DECLASM(mod,MOD)
BC_DECLASM(mod_op,MOD_OP)
BC_DECLASM(shr,SHR)
BC_DECLASM(shl,SHL)
BC_DECLASM(sqr,SQR)
BC_DECLASM(min,MIN)
BC_DECLASM(max,MAX)
BC_DECLASM(min_fp,MIN_FP)
BC_DECLASM(max_fp,MAX_FP)
BC_DECLASM(abs,ABS)
BC_DECLASM(sign,SIGN)
BC_DECLASM(invsqrt,INVSQRT)
BC_DECLASM(booltofp,BOOLTOFP)
BC_DECLASM(fptobool,FPTOBOOL)
BC_DECLASM(fptobool_rev,FPTOBOOL_REV)
#define BC_DECLASM_N(x,y,n) static EEL_BC_TYPE nseel_asm_##x[1 + (n*sizeof(INT_PTR))/sizeof(EEL_BC_TYPE)]={EEL_BC_##y, };
#define BC_DECLASM_N2(x,y,n) static EEL_BC_TYPE _asm_##x[1 + (n*sizeof(INT_PTR))/sizeof(EEL_BC_TYPE)]={EEL_BC_##y, };
#define BC_DECLASM_N_EXPORT(x,y,n) EEL_BC_TYPE _asm_##x[1 + (n*sizeof(INT_PTR))/sizeof(EEL_BC_TYPE)]={EEL_BC_##y, }; const void *const _asm_##x##_end = EEL_BC_ENDOF(_asm_##x);
BC_DECLASM_N(fcall,FCALL,1)
BC_DECLASM_N(1pdd,CFUNC_1PDD,1)
BC_DECLASM_N(2pdd,CFUNC_2PDD,1)
BC_DECLASM_N(2pdds,CFUNC_2PDDS,1)
BC_DECLASM_N2(megabuf,MEGABUF,0)
#define _asm_megabuf_end EEL_BC_ENDOF(_asm_megabuf)
BC_DECLASM_N_EXPORT(generic1parm,GENERIC1PARM,2)
BC_DECLASM_N_EXPORT(generic2parm,GENERIC2PARM,2)
BC_DECLASM_N_EXPORT(generic3parm,GENERIC3PARM,2)
BC_DECLASM_N_EXPORT(generic1parm_retd,GENERIC1PARM_RETD,2)
BC_DECLASM_N_EXPORT(generic2parm_retd,GENERIC2PARM_RETD,2)
BC_DECLASM_N_EXPORT(generic3parm_retd,GENERIC3PARM_RETD,2)
#define _asm_generic1parm_end EEL_BC_ENDOF(_asm_generic1parm)
#define _asm_generic2parm_end EEL_BC_ENDOF(_asm_generic2parm)
#define _asm_generic3parm_end EEL_BC_ENDOF(_asm_generic3parm)
#define _asm_generic1parm_retd_end EEL_BC_ENDOF(_asm_generic1parm_retd)
#define _asm_generic2parm_retd_end EEL_BC_ENDOF(_asm_generic2parm_retd)
#define _asm_generic3parm_retd_end EEL_BC_ENDOF(_asm_generic3parm_retd)
#define nseel_asm_1pdd_end EEL_BC_ENDOF(nseel_asm_1pdd)
#define nseel_asm_2pdd_end EEL_BC_ENDOF(nseel_asm_2pdd)
#define nseel_asm_2pdds_end EEL_BC_ENDOF(nseel_asm_2pdds)
#define nseel_asm_fcall_end EEL_BC_ENDOF(nseel_asm_fcall)
#define nseel_asm_band_end EEL_BC_ENDOF(nseel_asm_band)
#define nseel_asm_bor_end EEL_BC_ENDOF(nseel_asm_bor)
#define nseel_asm_bnot_end EEL_BC_ENDOF(nseel_asm_bnot)
#define nseel_asm_bnotnot_end EEL_BC_ENDOF(nseel_asm_bnotnot)
#define nseel_asm_equal_end EEL_BC_ENDOF(nseel_asm_equal)
#define nseel_asm_equal_exact_end EEL_BC_ENDOF(nseel_asm_equal_exact)
#define nseel_asm_notequal_end EEL_BC_ENDOF(nseel_asm_notequal)
#define nseel_asm_notequal_exact_end EEL_BC_ENDOF(nseel_asm_notequal_exact)
#define nseel_asm_above_end EEL_BC_ENDOF(nseel_asm_above)
#define nseel_asm_beloweq_end EEL_BC_ENDOF(nseel_asm_beloweq)
#define nseel_asm_min_end EEL_BC_ENDOF(nseel_asm_min)
#define nseel_asm_max_end EEL_BC_ENDOF(nseel_asm_max)
#define nseel_asm_abs_end EEL_BC_ENDOF(nseel_asm_abs)
#define nseel_asm_min_fp_end EEL_BC_ENDOF(nseel_asm_min_fp)
#define nseel_asm_max_fp_end EEL_BC_ENDOF(nseel_asm_max_fp)
#define nseel_asm_sign_end EEL_BC_ENDOF(nseel_asm_sign)
#define nseel_asm_invsqrt_end EEL_BC_ENDOF(nseel_asm_invsqrt)
#define nseel_asm_dbg_getstackptr_end EEL_BC_ENDOF(nseel_asm_dbg_getstackptr)
#define nseel_asm_add_end EEL_BC_ENDOF(nseel_asm_add)
#define nseel_asm_sub_end EEL_BC_ENDOF(nseel_asm_sub)
#define nseel_asm_mul_end EEL_BC_ENDOF(nseel_asm_mul)
#define nseel_asm_div_end EEL_BC_ENDOF(nseel_asm_div)
#define nseel_asm_and_end EEL_BC_ENDOF(nseel_asm_and)
#define nseel_asm_or_end EEL_BC_ENDOF(nseel_asm_or)
#define nseel_asm_or0_end EEL_BC_ENDOF(nseel_asm_or0)
#define nseel_asm_xor_end EEL_BC_ENDOF(nseel_asm_xor)
#define nseel_asm_add_op_end EEL_BC_ENDOF(nseel_asm_add_op)
#define nseel_asm_sub_op_end EEL_BC_ENDOF(nseel_asm_sub_op)
#define nseel_asm_add_op_fast_end EEL_BC_ENDOF(nseel_asm_add_op_fast)
#define nseel_asm_sub_op_fast_end EEL_BC_ENDOF(nseel_asm_sub_op_fast)
#define nseel_asm_mul_op_end EEL_BC_ENDOF(nseel_asm_mul_op)
#define nseel_asm_mul_op_fast_end EEL_BC_ENDOF(nseel_asm_mul_op_fast)
#define nseel_asm_div_op_end EEL_BC_ENDOF(nseel_asm_div_op)
#define nseel_asm_div_op_fast_end EEL_BC_ENDOF(nseel_asm_div_op_fast)
#define nseel_asm_and_op_end EEL_BC_ENDOF(nseel_asm_and_op)
#define nseel_asm_or_op_end EEL_BC_ENDOF(nseel_asm_or_op)
#define nseel_asm_xor_op_end EEL_BC_ENDOF(nseel_asm_xor_op)
#define nseel_asm_uminus_end EEL_BC_ENDOF(nseel_asm_uminus)
#define nseel_asm_assign_end EEL_BC_ENDOF(nseel_asm_assign)
#define nseel_asm_assign_fast_end EEL_BC_ENDOF(nseel_asm_assign_fast)
#define nseel_asm_assign_fast_fromfp_end EEL_BC_ENDOF(nseel_asm_assign_fast_fromfp)
#define nseel_asm_assign_fromfp_end EEL_BC_ENDOF(nseel_asm_assign_fromfp)
#define nseel_asm_mod_end EEL_BC_ENDOF(nseel_asm_mod)
#define nseel_asm_mod_op_end EEL_BC_ENDOF(nseel_asm_mod_op)
#define nseel_asm_shr_end EEL_BC_ENDOF(nseel_asm_shr)
#define nseel_asm_shl_end EEL_BC_ENDOF(nseel_asm_shl)
#define nseel_asm_sqr_end EEL_BC_ENDOF(nseel_asm_sqr)
#define nseel_asm_booltofp_end EEL_BC_ENDOF(nseel_asm_booltofp)
#define nseel_asm_fptobool_end EEL_BC_ENDOF(nseel_asm_fptobool)
#define nseel_asm_fptobool_rev_end EEL_BC_ENDOF(nseel_asm_fptobool_rev)
#define nseel_asm_stack_push_end EEL_BC_ENDOF(nseel_asm_stack_push)
#define nseel_asm_stack_pop_end EEL_BC_ENDOF(nseel_asm_stack_pop)
#define nseel_asm_stack_pop_fast_end EEL_BC_ENDOF(nseel_asm_stack_pop_fast)
#define nseel_asm_stack_peek_end EEL_BC_ENDOF(nseel_asm_stack_peek)
#define nseel_asm_stack_peek_int_end EEL_BC_ENDOF(nseel_asm_stack_peek_int)
#define nseel_asm_stack_peek_top_end EEL_BC_ENDOF(nseel_asm_stack_peek_top)
#define nseel_asm_stack_exch_end EEL_BC_ENDOF(nseel_asm_stack_exch)
static void *GLUE_realAddress(void *fn, void *fn_e, int32_t *size)
{
*size = (char *)fn_e - (char *)fn;
return fn;
}
#define EEL_BC_STACKSIZE (32768)
// todo: check for stack overflows! we could determine if this is possible at compile time.
#define EEL_BC_STACK_POP_SIZE 8
#define EEL_BC_STACK_PUSH(type, val) (*(type *)(stackptr -= EEL_BC_STACK_POP_SIZE)) = (val)
#define EEL_BC_STACK_POP() (stackptr += EEL_BC_STACK_POP_SIZE)
#define EEL_BC_TRUE ((float*)(INT_PTR)1)
static void GLUE_CALL_CODE(INT_PTR bp, INT_PTR cp, INT_PTR rt)
{
char __stack[EEL_BC_STACKSIZE];
char *iptr = (char*)cp;
char *stackptr=__stack + EEL_BC_STACKSIZE;
float *p1 = NULL, *p2 = NULL, *p3 = NULL, *wtp = (float*)bp;
#define fp_top (_fpstacktop[0])
#define fp_top2 (_fpstacktop[-1])
#define fp_push(x) *++_fpstacktop=(x)
#define fp_pop() (*_fpstacktop--)
#define fp_rewind(x) (_fpstacktop -= (x))
float fpstack[GLUE_MAX_FPSTACK_SIZE];
float *_fpstacktop=fpstack-1;
for (;;)
{
EEL_BC_TYPE inst = *(EEL_BC_TYPE *)iptr;
iptr += sizeof(EEL_BC_TYPE);
switch (inst)
{
case EEL_BC_FXCH:
{
float a = fp_top;
fp_top=fp_top2;
fp_top2=a;
}
break;
case EEL_BC_POP_FPSTACK: fp_rewind(1); break;
case EEL_BC_NOP: break;
case EEL_BC_RET:
if (EEL_BC_STACK_POP() > __stack+EEL_BC_STACKSIZE)
{
return;
}
iptr = *(void **)(stackptr - EEL_BC_STACK_POP_SIZE);
break;
case EEL_BC_JMP_NC:
iptr += sizeof(GLUE_JMP_TYPE)+*(GLUE_JMP_TYPE *)iptr;
break;
case EEL_BC_JMP_IF_P1_Z:
iptr += p1 ? sizeof(GLUE_JMP_TYPE) : sizeof(GLUE_JMP_TYPE)+*(GLUE_JMP_TYPE *)iptr;
break;
case EEL_BC_JMP_IF_P1_NZ:
iptr += p1 ? sizeof(GLUE_JMP_TYPE)+*(GLUE_JMP_TYPE *)iptr : sizeof(GLUE_JMP_TYPE);
break;
case EEL_BC_MOV_FPTOP_DV:
fp_push(**(float **)iptr);
iptr += sizeof(void*);
break;
case EEL_BC_MOV_P1_DV:
p1 = *(void **)iptr;
iptr += sizeof(void*);
break;
case EEL_BC_MOV_P2_DV:
p2 = *(void **)iptr;
iptr += sizeof(void*);
break;
case EEL_BC_MOV_P3_DV:
p3 = *(void **)iptr;
iptr += sizeof(void*);
break;
case EEL_BC__RESET_WTP:
wtp = *(void **)iptr;
iptr += sizeof(void*);
break;
case EEL_BC_PUSH_P1:
EEL_BC_STACK_PUSH(void *, p1);
break;
case EEL_BC_PUSH_P1PTR_AS_VALUE:
EEL_BC_STACK_PUSH(float, *p1);
break;
case EEL_BC_POP_P1:
p1 = *(float **) stackptr;
EEL_BC_STACK_POP();
break;
case EEL_BC_POP_P2:
p2 = *(float **) stackptr;
EEL_BC_STACK_POP();
break;
case EEL_BC_POP_P3:
p3 = *(float **) stackptr;
EEL_BC_STACK_POP();
break;
case EEL_BC_POP_VALUE_TO_ADDR:
**(float**)iptr = *(float *)stackptr;
EEL_BC_STACK_POP();
iptr += sizeof(void*);
break;
case EEL_BC_MOVE_STACK:
stackptr += *(int32_t *)iptr;
iptr += sizeof(int32_t);
break;
case EEL_BC_STORE_P1_TO_STACK_AT_OFFS:
*(void **) (stackptr + *(int32_t *)iptr) = p1;
iptr += sizeof(int32_t);
break;
case EEL_BC_MOVE_STACKPTR_TO_P1:
p1 = (float *)stackptr;
break;
case EEL_BC_MOVE_STACKPTR_TO_P2:
p2 = (float *)stackptr;
break;
case EEL_BC_MOVE_STACKPTR_TO_P3:
p3 = (float *)stackptr;
break;
case EEL_BC_SET_P2_FROM_P1:
p2=p1;
break;
case EEL_BC_SET_P3_FROM_P1:
p3=p1;
break;
case EEL_BC_COPY_VALUE_AT_P1_TO_ADDR:
**(float **)iptr = *p1;
iptr += sizeof(void*);
break;
case EEL_BC_SET_P1_FROM_WTP:
p1 = wtp;
break;
case EEL_BC_SET_P2_FROM_WTP:
p2 = wtp;
break;
case EEL_BC_SET_P3_FROM_WTP:
p3 = wtp;
break;
case EEL_BC_POP_FPSTACK_TO_PTR:
**((float **)iptr) = fp_pop();
iptr += sizeof(void *);
break;
case EEL_BC_POP_FPSTACK_TOSTACK:
EEL_BC_STACK_PUSH(float, fp_pop());
break;
case EEL_BC_PUSH_VAL_AT_P1_TO_FPSTACK:
fp_push(*p1);
break;
case EEL_BC_PUSH_VAL_AT_P2_TO_FPSTACK:
fp_push(*p2);
break;
case EEL_BC_PUSH_VAL_AT_P3_TO_FPSTACK:
fp_push(*p3);
break;
case EEL_BC_POP_FPSTACK_TO_WTP:
*wtp++ = fp_pop();
break;
case EEL_BC_SET_P1_Z:
p1=NULL;
break;
case EEL_BC_SET_P1_NZ:
p1 = EEL_BC_TRUE;
break;
case EEL_BC_LOOP_LOADCNT:
if ((EEL_BC_STACK_PUSH(int32_t, (int32_t)fp_pop())) < 1)
{
EEL_BC_STACK_POP();
iptr+= sizeof(GLUE_JMP_TYPE)+*(GLUE_JMP_TYPE *)iptr;
}
else
{
iptr += sizeof(GLUE_JMP_TYPE);
EEL_BC_STACK_PUSH(void *, wtp);
}
break;
case EEL_BC_LOOP_END:
wtp = *(void **) (stackptr);
if (--(*(int32_t *)(stackptr+EEL_BC_STACK_POP_SIZE)) <= 0)
{
stackptr += EEL_BC_STACK_POP_SIZE*2;
iptr += sizeof(GLUE_JMP_TYPE);
}
else
{
iptr += sizeof(GLUE_JMP_TYPE)+*(GLUE_JMP_TYPE *)iptr; // back to the start!
}
break;
case EEL_BC_WHILE_SETUP:
EEL_BC_STACK_PUSH(int32_t, NSEEL_NATIVE_FLT_MAX_INT);
break;
case EEL_BC_WHILE_BEGIN:
EEL_BC_STACK_PUSH(void *, wtp);
break;
case EEL_BC_WHILE_END:
wtp = *(float **) stackptr;
EEL_BC_STACK_POP();
if (--(*(int32_t *)stackptr) <= 0)
{
EEL_BC_STACK_POP();
iptr += sizeof(GLUE_JMP_TYPE) + *(GLUE_JMP_TYPE *)iptr; // endpt
}
else
iptr += sizeof(GLUE_JMP_TYPE);
break;
case EEL_BC_WHILE_CHECK_RV:
if (p1)
iptr += sizeof(GLUE_JMP_TYPE)+*(GLUE_JMP_TYPE *)iptr; // loop
else
{
// done
EEL_BC_STACK_POP();
iptr += sizeof(GLUE_JMP_TYPE);
}
break;
case EEL_BC_BNOT:
p1 = p1 ? NULL : EEL_BC_TRUE;
break;
case EEL_BC_BNOTNOT:
p1 = p1 ? EEL_BC_TRUE : NULL;
break;
case EEL_BC_EQUAL:
p1 = fabsf(fp_top - fp_top2) < NSEEL_CLOSEFACTOR ? EEL_BC_TRUE : NULL;
fp_rewind(2);
break;
case EEL_BC_EQUAL_EXACT:
p1 = fp_top == fp_top2 ? EEL_BC_TRUE : NULL;
fp_rewind(2);
break;
case EEL_BC_NOTEQUAL:
p1 = fabsf(fp_top - fp_top2) >= NSEEL_CLOSEFACTOR ? EEL_BC_TRUE : NULL;
fp_rewind(2);
break;
case EEL_BC_NOTEQUAL_EXACT:
p1 = fp_top != fp_top2 ? EEL_BC_TRUE : NULL;
fp_rewind(2);
break;
case EEL_BC_ABOVE:
p1 = fp_top < fp_top2 ? EEL_BC_TRUE : NULL;
fp_rewind(2);
break;
case EEL_BC_BELOWEQ:
p1 = fp_top >= fp_top2 ? EEL_BC_TRUE : NULL;
fp_rewind(2);
break;
case EEL_BC_ADD:
fp_top2 += fp_top;
fp_rewind(1);
break;
case EEL_BC_SUB:
fp_top2 -= fp_top;
fp_rewind(1);
break;
case EEL_BC_MUL:
fp_top2 *= fp_top;
fp_rewind(1);
break;
case EEL_BC_DIV:
fp_top2 /= fp_top;
fp_rewind(1);
break;
case EEL_BC_AND:
fp_top2 = (float) (((int32_t)fp_top) & (int32_t)(fp_top2));
fp_rewind(1);
break;
case EEL_BC_OR:
fp_top2 = (float) (((int32_t)fp_top) | (int32_t)(fp_top2));
fp_rewind(1);
break;
case EEL_BC_OR0:
fp_top = (float) ((int32_t)(fp_top));
break;
case EEL_BC_XOR:
fp_top2 = (float) (((int32_t)fp_top) ^ (int32_t)(fp_top2));
fp_rewind(1);
break;
case EEL_BC_ADD_OP:
*(p1 = p2) = *p2 + fp_pop();
break;
case EEL_BC_SUB_OP:
*(p1 = p2) = *p2 - fp_pop();
break;
case EEL_BC_ADD_OP_FAST:
*(p1 = p2) += fp_pop();
break;
case EEL_BC_SUB_OP_FAST:
*(p1 = p2) -= fp_pop();
break;
case EEL_BC_MUL_OP:
*(p1 = p2) = *p2 * fp_pop();
break;
case EEL_BC_DIV_OP:
*(p1 = p2) = *p2 / fp_pop();
break;
case EEL_BC_MUL_OP_FAST:
*(p1 = p2) *= fp_pop();
break;
case EEL_BC_DIV_OP_FAST:
*(p1 = p2) /= fp_pop();
break;
case EEL_BC_AND_OP:
p1 = p2;
*p2 = (float) (((int32_t)*p2) & (int32_t)fp_pop());
break;
case EEL_BC_OR_OP:
p1 = p2;
*p2 = (float) (((int32_t)*p2) | (int32_t)fp_pop());
break;
case EEL_BC_XOR_OP:
p1 = p2;
*p2 = (float) (((int32_t)*p2) ^ (int32_t)fp_pop());
break;
case EEL_BC_UMINUS:
fp_top = -fp_top;
break;
case EEL_BC_ASSIGN:
*p2 = *p1;
p1 = p2;
break;
case EEL_BC_ASSIGN_FAST:
*p2 = *p1;
p1 = p2;
break;
case EEL_BC_ASSIGN_FAST_FROMFP:
*p2 = fp_pop();
p1 = p2;
break;
case EEL_BC_ASSIGN_FROMFP:
*p2 = fp_pop();
p1 = p2;
break;
case EEL_BC_MOD:
{
int32_t a = (int32_t) (fp_pop());
fp_top = a ? (float) ((int32_t)fp_top % a) : 0.0f;
}
break;
case EEL_BC_MOD_OP:
{
int32_t a = (int32_t) (fp_pop());
*p2 = a ? (float) ((int32_t)*p2 % a) : 0.0f;
p1=p2;
}
break;
case EEL_BC_SHR:
fp_top2 = (float) (((int32_t)fp_top2) >> (int32_t)fp_top);
fp_rewind(1);
break;
case EEL_BC_SHL:
fp_top2 = (float) (((int32_t)fp_top2) << (int32_t)fp_top);
fp_rewind(1);
break;
case EEL_BC_SQR:
fp_top *= fp_top;
break;
case EEL_BC_MIN:
if (*p1 > *p2) p1 = p2;
break;
case EEL_BC_MAX:
if (*p1 < *p2) p1 = p2;
break;
case EEL_BC_MIN_FP:
{
float a=fp_pop();
if (a<fp_top) fp_top=a;
}
break;
case EEL_BC_MAX_FP:
{
float a=fp_pop();
if (a>fp_top) fp_top=a;
}
break;
case EEL_BC_ABS:
fp_top = fabsf(fp_top);
break;
case EEL_BC_SIGN:
if (fp_top<0.0f) fp_top=-1.0f;
else if (fp_top>0.0f) fp_top=1.0f;
break;
case EEL_BC_INVSQRT:
{
float y = (float)fp_top;
int32_t i = 0x5f3759df - ( (* (int32_t *) &y) >> 1 );
y = *(float *) &i;
fp_top = y * ( 1.5f - ( (fp_top * 0.5f) * y * y ) );
}
break;
case EEL_BC_FCALL:
{
char *newiptr = *(char **)iptr;
EEL_BC_STACK_PUSH(void *, (iptr += sizeof(void *)));
iptr = newiptr;
}
break;
case EEL_BC_BOOLTOFP:
fp_push(p1 ? 1.0f : 0.0f);
break;
case EEL_BC_FPTOBOOL:
p1 = fabsf(fp_pop()) >= NSEEL_CLOSEFACTOR ? EEL_BC_TRUE : NULL;
break;
case EEL_BC_FPTOBOOL_REV:
p1 = fabsf(fp_pop()) < NSEEL_CLOSEFACTOR ? EEL_BC_TRUE : NULL;
break;
case EEL_BC_CFUNC_1PDD:
{
float (*f)(float) = *(float (**)(float)) iptr;
fp_top = f(fp_top);
iptr += sizeof(void *);
}
break;
case EEL_BC_CFUNC_2PDD:
{
float (*f)(float,float) = *(float (**)(float,float))iptr;
fp_top2 = f(fp_top2,fp_top);
fp_rewind(1);
iptr += sizeof(void *);
}
break;
case EEL_BC_CFUNC_2PDDS:
{
float (*f)(float,float) = *(float (**)(float,float))iptr;
*p2 = f(*p2,fp_pop());
p1 = p2;
iptr += sizeof(void *);
}
break;
case EEL_BC_MEGABUF:
{
p1 = ((float*)rt) + (uint32_t)(fp_pop() + NSEEL_CLOSEFACTOR);
}
break;
case EEL_BC_GENERIC1PARM:
{
float *(*f)(void *,float*) = *(float *(**)(void *, float *)) (iptr+sizeof(void *));
p1 = f(*(void **)iptr,p1);
iptr += sizeof(void *)*2;
}
break;
case EEL_BC_GENERIC2PARM:
{
float *(*f)(void *,float*,float*) = *(float *(**)(void *, float *, float *)) (iptr+sizeof(void *));
p1 = f(*(void **)iptr,p2, p1);
iptr += sizeof(void *)*2;
}
break;
case EEL_BC_GENERIC3PARM:
{
float *(*f)(void *,float*,float*,float*) = *(float *(**)(void *, float *, float *, float *)) (iptr+sizeof(void *));
p1 = f(*(void **)iptr,p3, p2, p1);
iptr += sizeof(void *)*2;
}
break;
case EEL_BC_GENERIC1PARM_RETD:
{
float (*f)(void *,float*) = *(float (**)(void *, float *)) (iptr+sizeof(void *));
fp_push(f(*(void **)iptr,p1));
iptr += sizeof(void *)*2;
}
break;
case EEL_BC_GENERIC2PARM_RETD:
{
float (*f)(void *,float*,float*) = *(float (**)(void *, float *, float *)) (iptr+sizeof(void *));
fp_push(f(*(void **)iptr,p2, p1));
iptr += sizeof(void *)*2;
}
break;
case EEL_BC_GENERIC3PARM_RETD:
{
float (*f)(void *,float*,float*,float*) = *(float (**)(void *, float *, float *, float *)) (iptr+sizeof(void *));
fp_push(f(*(void **)iptr,p3, p2, p1));
iptr += sizeof(void *)*2;
}
break;
}
}
#undef fp_top
#undef fp_top2
#undef fp_pop
#undef fp_push
};
#endif