-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathterp.c
1766 lines (1501 loc) · 50.7 KB
/
terp.c
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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// $Id: terp.c,v 1.42 2004/12/22 14:33:40 iain Exp $
// Interpreter engine.
#include "git.h"
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
// -------------------------------------------------------------
// Global variables
git_sint32* gStackPointer;
#ifdef USE_DIRECT_THREADING
Opcode* gOpcodeTable;
#endif
enum IOMode gIoMode = IO_NULL;
// -------------------------------------------------------------
// Useful macros for manipulating the stack
#define LOCAL(n) (locals[(n)])
#define PUSH(n) *sp++ = (n)
#define POP (*--sp)
#define READ_PC ((git_uint32)(*pc++))
#define CHECK_FREE(n) if ((top - sp) < (n)) goto stack_overflow
#define CHECK_USED(n) if ((sp - values) < (n)) goto stack_underflow
// -------------------------------------------------------------
// Random number generator, from Glulxe
static glui32 xo_random(void);
static void xo_seed_random(glui32 seed);
static int rand_use_native = 1;
/* Set the random-number seed, and also select which RNG to use.
*/
void git_seed_random(glui32 seed)
{
if (seed == 0)
{
rand_use_native = 1;
xo_seed_random(time(NULL));
}
else
{
rand_use_native = 0;
xo_seed_random(seed);
}
}
/* Return a random number in the range 0 to 2^32-1. */
glui32 git_random()
{
#ifdef USE_NATIVE_RANDOM
if (rand_use_native)
return native_random();
#endif
return xo_random();
}
/* This is the "xoshiro128**" random-number generator and seed function.
Adapted from: https://prng.di.unimi.it/xoshiro128starstar.c
About this algorithm: https://prng.di.unimi.it/
*/
static uint32_t xo_table[4];
static void xo_seed_random(glui32 seed)
{
int ix;
/* Set up the 128-bit state from a single 32-bit integer. We rely
on a different RNG, SplitMix32. This isn't high-quality, but we
just need to get a bunch of bits into xo_table. */
for (ix=0; ix<4; ix++) {
seed += 0x9E3779B9;
glui32 s = seed;
s ^= s >> 15;
s *= 0x85EBCA6B;
s ^= s >> 13;
s *= 0xC2B2AE35;
s ^= s >> 16;
xo_table[ix] = s;
}
}
static glui32 xo_random(void)
{
/* I've inlined the utility function:
rotl(x, k) => (x << k) | (x >> (32 - k))
*/
const uint32_t t1x5 = xo_table[1] * 5;
const uint32_t result = ((t1x5 << 7) | (t1x5 >> (32-7))) * 9;
const uint32_t t1s9 = xo_table[1] << 9;
xo_table[2] ^= xo_table[0];
xo_table[3] ^= xo_table[1];
xo_table[1] ^= xo_table[2];
xo_table[0] ^= xo_table[3];
xo_table[2] ^= t1s9;
const uint32_t t3 = xo_table[3];
xo_table[3] = ((t3 << 11) | (t3 >> (32-11)));
return result;
}
// -------------------------------------------------------------
// Floating point support
GIT_INLINE git_uint32 ENCODE_FLOAT(git_float f)
{
git_uint32 n;
memcpy(&n, &f, 4);
return n;
}
GIT_INLINE git_float DECODE_FLOAT(git_uint32 n) {
git_float f;
memcpy(&f, &n, 4);
return f;
}
GIT_INLINE void ENCODE_DOUBLE(git_double d, git_sint32 *hi, git_sint32 *lo)
{
#if defined(USE_BIG_ENDIAN) || defined(USE_BIG_ENDIAN_UNALIGNED)
memcpy(hi, &d, 4);
memcpy(lo, ((char *)&d)+4, 4);
#else
memcpy(lo, &d, 4);
memcpy(hi, ((char *)&d)+4, 4);
#endif
}
GIT_INLINE git_double DECODE_DOUBLE(git_uint32 hi, git_uint32 lo)
{
git_double d;
#if defined(USE_BIG_ENDIAN) || defined(USE_BIG_ENDIAN_UNALIGNED)
memcpy(&d, &hi, 4);
memcpy(((char *)&d)+4, &lo, 4);
#else
memcpy(&d, &lo, 4);
memcpy(((char *)&d)+4, &hi, 4);
#endif
return d;
}
static int floatCompare(git_sint32 L1, git_sint32 L2, git_sint32 L3)
{
git_float F1, F2;
if (((L3 & 0x7F800000) == 0x7F800000) && ((L3 & 0x007FFFFF) != 0))
return 0;
if ((L1 == 0x7F800000 || L1 == 0xFF800000) && (L2 == 0x7F800000 || L2 == 0xFF800000))
return (L1 == L2);
F1 = DECODE_FLOAT(L2) - DECODE_FLOAT(L1);
F2 = fabs(DECODE_FLOAT(L3));
return ((F1 <= F2) && (F1 >= -F2));
}
static int doubleCompare(git_sint32 L1, git_sint32 L2, git_sint32 L3, git_sint32 L4, git_sint32 L5, git_sint32 L6, git_sint32 L7)
{
git_double D1, D2;
if (((L5 & 0x7FF00000) == 0x7FF00000) && (((L5 & 0xFFFFF) != 0x0) || (L6 != 0x0)))
return 0;
if ((L1 == 0x7FF00000 || L1 == 0xFFF00000) && L2 == 0x0 && (L3 == 0x7FF00000 || L3 == 0xFFF00000) && L4 == 0x0)
return (L1 == L3);
D1 = DECODE_DOUBLE(L3, L4) - DECODE_DOUBLE(L1, L2);
D2 = fabs(DECODE_DOUBLE(L5, L6));
return ((D1 <= D2) && (D1 >= -D2));
}
static void testDouble()
{
glui32 PI_hi = 0x400921FB;
glui32 PI_lo = 0x54442D18;
git_double pi = DECODE_DOUBLE(PI_hi, PI_lo);
if (!(pi > 3.1415 && pi < 3.1416)) {
// If this fails, on a big-endian system, check that USE_BIG_ENDIAN
// or USE_BIG_ENDIAN_UNALIGNED have been defined in config.h
fatalError("Test decode of double precision value failed");
}
}
// -------------------------------------------------------------
// Functions
GIT_INLINE void checkDivideArgs(git_sint32 L1, git_sint32 L2)
{
if (L2 == 0) fatalError ("Divide by zero");
if (((git_uint32) L1 == 0x80000000) && (L2 == -1)) fatalError ("Divide overflow");
}
void startProgram (size_t cacheSize)
{
Block pc; // Program counter (pointer into dynamically generated code)
git_sint32 L1=0, L2=0, L3=0, L4=0, L5=0, L6=0, L7=0;
#define S1 L1
#define S2 L2
git_float F1=0.0f, F2=0.0f, F3=0.0f, F4=0.0f;
git_double D1=0.0f, D2=0.0f, D3=0.0f;
git_sint32* base; // Bottom of the stack.
git_sint32* frame; // Bottom of the current stack frame.
git_sint32* locals; // Start of the locals section of the current frame.
git_sint32* values; // Start of the values section of the current frame.
git_sint32* sp; // Next free stack slot.
git_sint32* top; // The top of the stack -- that is, the first unusable slot.
git_sint32 args [64]; // Array of arguments. Count is stored in L2.
git_uint32 ioRock = 0;
git_uint32 stringTable = memRead32(28);
git_uint32 startPos = memRead32(24);
git_uint32 stackSize = memRead32(20);
git_uint32 protectPos = 0;
git_uint32 protectSize = 0;
git_uint32 maybe_unused glulxPC = 0;
git_uint32 maybe_unused glulxOpcode = 0;
acceleration_func accelfunc;
// Initialise the code cache.
#ifdef USE_DIRECT_THREADING
static Opcode opcodeTable [] = {
#define LABEL(label) &&do_ ## label,
#include "labels.inc"
NULL};
gOpcodeTable = opcodeTable;
# if (UINTPTR_MAX > 0xffffffffULL)
const uintptr_t opcodeHi = (uintptr_t)opcodeTable[0] & ~0xffffffffULL;
for (L1 = 1; opcodeTable[L1] != NULL; ++L1)
assert (opcodeHi == ((uintptr_t)opcodeTable[L1] & ~0xffffffffULL));
# endif
#endif
testDouble ();
initCompiler (cacheSize);
// Initialise the random number generator.
git_seed_random (0);
// Set up the stack.
base = malloc (stackSize);
if (base == NULL)
fatalError ("Couldn't allocate stack");
top = base + (stackSize / 4);
frame = locals = values = sp = base;
// Call the first function.
L1 = startPos; // Initial PC.
L2 = 0; // No arguments.
goto do_enter_function_L1;
#if defined(USE_DIRECT_THREADING) && (UINTPTR_MAX > 0xffffffffULL)
#define NEXT do { goto *(Opcode)(*(pc++) | opcodeHi); } while(0)
#elif defined(USE_DIRECT_THREADING)
#define NEXT do { goto *(Opcode)(*(pc++)); } while(0)
#else
#define NEXT goto next
//#define NEXT do { CHECK_USED(0); CHECK_FREE(0); goto next; } while (0)
next:
switch (*pc++)
{
#define LABEL(foo) case label_ ## foo: goto do_ ## foo;
#include "labels.inc"
default: fatalError("exec: bad opcode");
}
#endif
do_debug_step:
// This opcode lets us keep track of how the compiled
// code relates to the original glulx code.
glulxPC = READ_PC; // Glulx program counter.
glulxOpcode = READ_PC; // Glulx opcode number.
// fprintf (stdout, "\nPC: 0x%08x\nOpcode: 0x%04x\n", glulxPC, glulxOpcode);
// fprintf (stdout, "Stack:");
// for (L7 = 0 ; L7 < (sp - base) ; ++L7)
// fprintf (stdout," 0x%x", base[L7]);
// fprintf (stdout, "\n");
NEXT;
#define LOAD_INSTRUCTIONS(reg) \
do_ ## reg ## _const: reg = READ_PC; NEXT; \
do_ ## reg ## _stack: CHECK_USED(1); reg = POP; NEXT; \
do_ ## reg ## _addr: reg = memRead32 (READ_PC); NEXT; \
do_ ## reg ## _local: reg = LOCAL (READ_PC); NEXT
LOAD_INSTRUCTIONS(L1);
LOAD_INSTRUCTIONS(L2);
LOAD_INSTRUCTIONS(L3);
LOAD_INSTRUCTIONS(L4);
LOAD_INSTRUCTIONS(L5);
LOAD_INSTRUCTIONS(L6);
LOAD_INSTRUCTIONS(L7);
#define STORE_INSTRUCTIONS(reg) \
do_ ## reg ## _stack: CHECK_FREE(1); PUSH(reg); NEXT; \
do_ ## reg ## _addr: memWrite32 (READ_PC, reg); NEXT; \
do_ ## reg ## _local: LOCAL (READ_PC) = reg; NEXT
STORE_INSTRUCTIONS(S1);
STORE_INSTRUCTIONS(S2);
#define DOUBLE_LOAD(mode2) \
do_L1_const_L2_ ## mode2: L1 = READ_PC; goto do_L2_ ## mode2; \
do_L1_stack_L2_ ## mode2: CHECK_USED(1); L1 = POP; goto do_L2_ ## mode2; \
do_L1_local_L2_ ## mode2: L1 = LOCAL (READ_PC); goto do_L2_ ## mode2; \
do_L1_addr_L2_ ## mode2: L1 = memRead32 (READ_PC); goto do_L2_ ## mode2
DOUBLE_LOAD(const);
DOUBLE_LOAD(stack);
DOUBLE_LOAD(local);
DOUBLE_LOAD(addr);
#undef LOAD_INSTRUCTIONS
#undef STORE_INSTRUCTIONS
#undef DOUBLE_LOAD
do_L1_addr16: L1 = memRead16 (READ_PC); NEXT;
do_L1_addr8: L1 = memRead8 (READ_PC); NEXT;
do_S1_addr16: memWrite16 (READ_PC, S1); NEXT;
do_S1_addr8: memWrite8 (READ_PC, S1); NEXT;
#define UL7 ((git_uint32)L7)
do_recompile:
pc = compile (*pc);
NEXT;
do_jump_abs_L7:
pc = getCode (UL7);
NEXT;
do_enter_function_L1: // Arg count is in L2.
// Check for an accelerated function
accelfunc = accel_get_func(L1);
if (accelfunc) {
S1 = accelfunc(L2, (glui32 *) args);
goto do_pop_call_stub;
}
frame = sp;
// Read the function type.
L7 = memRead8(L1++);
// Parse the local variables descriptor.
L6 = L5 = L4 = 0;
do
{
L6 = memRead8(L1++); // LocalType
L5 = memRead8(L1++); // LocalCount
if (L6 != 4 && L6 != 0) // We only support 4-byte locals.
{
if (L6 == 1 || L6 == 2)
fatalError("Short local variables are not supported, use Glulxe");
else
fatalError("Local variable wasn't 4 bytes wide");
}
L4 += L5; // Cumulative local count.
}
while (L5 != 0);
// Write out the stack frame.
// Recall that the number of locals is stored in L4.
CHECK_FREE(3 + L4);
PUSH (L4*4 + 12); // FrameLen
PUSH (12); // LocalsPos
if (L4 == 0)
L6 = 0;
else
L6 = (4 << 24) | (L4 << 16);
PUSH (L6); // format of locals
// This is where the local variables start, so:
locals = sp;
// Read the arguments, based on the function type.
switch (L7)
{
case 0xC0: // arguments should be placed on the stack.
// argc is in L2; we'll randomly use L5 as scratch.
CHECK_FREE(L5 + 1);
// Initialise the local variables.
for ( ; L4 > 0 ; --L4)
PUSH (0);
// This is where the temporary values start, so:
values = sp;
// Push the args onto the stack.
for (L5 = 0 ; L5 < L2 ; ++L5)
PUSH (args [L5]);
// Push the arg count.
PUSH (L2);
break;
case 0xC1: // arguments should be written into locals.
// argc is in L2, num locals is in L4.
// Stuff as many locals as possible with arguments.
for (L5 = 1 ; L5 <= L2 && L4 > 0 ; ++L5, --L4)
PUSH (args [L2 - L5]);
// Initialise any remaining locals.
for ( ; L4 > 0 ; --L4)
PUSH (0);
// This is where the temporary values start, so:
values = sp;
break;
default:
// This isn't a function!
fatalError("Not a function");
break;
}
// Start executing the function.
L7 = L1;
goto do_jump_abs_L7;
do_nop: NEXT;
#define PEEPHOLE_STORE(tag, code) \
do_ ## tag ## _discard: code; NEXT; \
do_ ## tag ## _S1_stack: code; goto do_S1_stack; \
do_ ## tag ## _S1_local: code; goto do_S1_local; \
do_ ## tag ## _S1_addr: code; goto do_S1_addr
PEEPHOLE_STORE(add, S1 = L1 + L2);
PEEPHOLE_STORE(sub, S1 = L1 - L2);
PEEPHOLE_STORE(mul, S1 = L1 * L2);
PEEPHOLE_STORE(div, checkDivideArgs(L1, L2); S1 = L1 / L2);
PEEPHOLE_STORE(mod, checkDivideArgs(L1, L2); S1 = L1 % L2);
PEEPHOLE_STORE(neg, S1 = -L1);
PEEPHOLE_STORE(bitnot, S1 = ~L1);
PEEPHOLE_STORE(bitand, S1 = L1 & L2);
PEEPHOLE_STORE(bitor, S1 = L1 | L2);
PEEPHOLE_STORE(bitxor, S1 = L1 ^ L2);
PEEPHOLE_STORE(shiftl, if (L2 > 31 || L2 < 0) S1 = 0; else S1 = L1 << ((git_uint32) L2));
PEEPHOLE_STORE(sshiftr, if (L2 > 31 || L2 < 0) L2 = 31; S1 = ((git_sint32) L1) >> ((git_uint32) L2));
PEEPHOLE_STORE(ushiftr, if (L2 > 31 || L2 < 0) S1 = 0; else S1 = ((git_uint32) L1) >> ((git_uint32) L2));
PEEPHOLE_STORE(aload, S1 = memRead32 (L1 + (L2<<2)));
PEEPHOLE_STORE(aloads, S1 = memRead16 (L1 + (L2<<1)));
PEEPHOLE_STORE(aloadb, S1 = memRead8 (L1 + L2));
PEEPHOLE_STORE(aloadbit,S1 = (memRead8 (L1 + (L2>>3)) >> (L2 & 7)) & 1);
PEEPHOLE_STORE(copys, S1 = L1 & 0xFFFF);
PEEPHOLE_STORE(copyb, S1 = L1 & 0x00FF);
PEEPHOLE_STORE(sexs, S1 = (git_sint32)((signed short)(L1 & 0xFFFF)));
PEEPHOLE_STORE(sexb, S1 = (git_sint32)((signed char)(L1 & 0x00FF)));
PEEPHOLE_STORE(fadd, F1 = DECODE_FLOAT(L1) + DECODE_FLOAT(L2); S1 = ENCODE_FLOAT(F1));
PEEPHOLE_STORE(fsub, F1 = DECODE_FLOAT(L1) - DECODE_FLOAT(L2); S1 = ENCODE_FLOAT(F1));
PEEPHOLE_STORE(fmul, F1 = DECODE_FLOAT(L1) * DECODE_FLOAT(L2); S1 = ENCODE_FLOAT(F1));
PEEPHOLE_STORE(fdiv, F1 = DECODE_FLOAT(L1) / DECODE_FLOAT(L2); S1 = ENCODE_FLOAT(F1));
#define PEEPHOLE_LOAD(tag,reg) \
do_ ## tag ## _ ## reg ## _const: reg = READ_PC; goto do_ ## tag; \
do_ ## tag ## _ ## reg ## _stack: CHECK_USED(1); reg = POP; goto do_ ## tag; \
do_ ## tag ## _ ## reg ## _local: reg = LOCAL(READ_PC); goto do_ ## tag; \
do_ ## tag ## _ ## reg ## _addr: reg = memRead32(READ_PC); goto do_ ## tag
PEEPHOLE_LOAD (return, L1);
PEEPHOLE_LOAD (astore, L3);
PEEPHOLE_LOAD (astores, L3);
PEEPHOLE_LOAD (astoreb, L3);
PEEPHOLE_LOAD (astorebit, L3);
#undef PEEPHOLE_STORE
do_astore: memWrite32 (L1 + (L2<<2), L3); NEXT;
do_astores: memWrite16 (L1 + (L2<<1), L3); NEXT;
do_astoreb: memWrite8 (L1 + L2, L3); NEXT;
do_astorebit:
L4 = memRead8(L1 + (L2>>3));
if (L3 == 0)
L4 &= ~(1 << (L2 & 7));
else
L4 |= (1 << (L2 & 7));
memWrite8(L1 + (L2>>3), L4);
NEXT;
#define DO_JUMP(tag, reg, cond) \
do_ ## tag ## _var: L7 = READ_PC; if (cond) goto do_goto_ ## reg ## _from_L7; NEXT; \
do_ ## tag ## _const: L7 = READ_PC; if (cond) goto do_jump_abs_L7; NEXT; \
do_ ## tag ## _by: L7 = READ_PC; if (cond) pc += L7; NEXT; \
do_ ## tag ## _return0: if (cond) { L1 = 0; goto do_return; } NEXT; \
do_ ## tag ## _return1: if (cond) { L1 = 1; goto do_return; } NEXT
DO_JUMP(jump, L1, 1 == 1);
DO_JUMP(jz, L2, L1 == 0);
DO_JUMP(jnz, L2, L1 != 0);
DO_JUMP(jeq, L3, L1 == L2);
DO_JUMP(jne, L3, L1 != L2);
DO_JUMP(jlt, L3, L1 < L2);
DO_JUMP(jge, L3, L1 >= L2);
DO_JUMP(jgt, L3, L1 > L2);
DO_JUMP(jle, L3, L1 <= L2);
DO_JUMP(jltu, L3, ((git_uint32)L1 < (git_uint32)L2));
DO_JUMP(jgeu, L3, ((git_uint32)L1 >= (git_uint32)L2));
DO_JUMP(jgtu, L3, ((git_uint32)L1 > (git_uint32)L2));
DO_JUMP(jleu, L3, ((git_uint32)L1 <= (git_uint32)L2));
DO_JUMP(jisnan, L2, (((L1 & 0x7F800000) == 0x7F800000) && ((L1 & 0x007FFFFF) != 0)));
DO_JUMP(jisinf, L2, ((L1 == 0x7F800000) || (L1 == 0xFF800000)));
DO_JUMP(jflt, L3, DECODE_FLOAT(L1) < DECODE_FLOAT(L2));
DO_JUMP(jfge, L3, DECODE_FLOAT(L1) >= DECODE_FLOAT(L2));
DO_JUMP(jfgt, L3, DECODE_FLOAT(L1) > DECODE_FLOAT(L2));
DO_JUMP(jfle, L3, DECODE_FLOAT(L1) <= DECODE_FLOAT(L2));
DO_JUMP(jfeq, L4, floatCompare(L1, L2, L3) != 0);
DO_JUMP(jfne, L4, floatCompare(L1, L2, L3) == 0);
DO_JUMP(jdlt, L5, DECODE_DOUBLE(L1, L2) < DECODE_DOUBLE(L3, L4));
DO_JUMP(jdge, L5, DECODE_DOUBLE(L1, L2) >= DECODE_DOUBLE(L3, L4));
DO_JUMP(jdgt, L5, DECODE_DOUBLE(L1, L2) > DECODE_DOUBLE(L3, L4));
DO_JUMP(jdle, L5, DECODE_DOUBLE(L1, L2) <= DECODE_DOUBLE(L3, L4));
DO_JUMP(jdeq, L7, doubleCompare(L1, L2, L3, L4, L5, L6, L7) != 0);
DO_JUMP(jdne, L7, doubleCompare(L1, L2, L3, L4, L5, L6, L7) == 0);
DO_JUMP(jdisinf, L3, (((L1 == 0x7FF00000) || (L1 == 0xFFF00000)) && L2 == 0x0));
DO_JUMP(jdisnan, L3, (((L1 & 0x7FF00000) == 0x7FF00000) && (((L1 & 0xFFFFF) != 0) || (L2 != 0x0))));
#undef DO_JUMP
do_jumpabs: L7 = L1; goto do_jump_abs_L7; NEXT;
do_goto_L7_from_L7: L1 = L7; goto do_goto_L1_from_L7;
do_goto_L5_from_L7: L1 = L5; goto do_goto_L1_from_L7;
do_goto_L4_from_L7: L1 = L4; goto do_goto_L1_from_L7;
do_goto_L3_from_L7: L1 = L3; goto do_goto_L1_from_L7;
do_goto_L2_from_L7: L1 = L2; goto do_goto_L1_from_L7;
do_goto_L1_from_L7:
if (L1 == 0 || L1 == 1) goto do_return;
L7 = L7 + L1 - 2; goto do_jump_abs_L7;
do_args_stack:
// The first argument is topmost in the stack; the count is in L2.
CHECK_USED(L2);
// We want to store the arguments in 'args' in the same order.
for (L3 = L2 - 1 ; L3 >= 0 ; --L3)
args [L3] = POP;
NEXT;
// Specialised versions of above:
do_args_stack_call_stub_discard:
CHECK_USED(L2);
for (L3 = L2 - 1 ; L3 >= 0 ; --L3)
args [L3] = POP;
goto do_call_stub_discard;
do_args_stack_call_stub_addr:
CHECK_USED(L2);
for (L3 = L2 - 1 ; L3 >= 0 ; --L3)
args [L3] = POP;
goto do_call_stub_addr;
do_args_stack_call_stub_local:
CHECK_USED(L2);
for (L3 = L2 - 1 ; L3 >= 0 ; --L3)
args [L3] = POP;
goto do_call_stub_local;
do_args_stack_call_stub_stack:
CHECK_USED(L2);
for (L3 = L2 - 1 ; L3 >= 0 ; --L3)
args [L3] = POP;
goto do_call_stub_stack;
do_args_3:
args [0] = L4;
args [1] = L3;
args [2] = L2;
L2 = 3;
NEXT;
do_args_2:
args [0] = L3;
args [1] = L2;
L2 = 2;
NEXT;
do_args_1:
args [0] = L2;
L2 = 1;
NEXT;
do_args_0:
L2 = 0;
NEXT;
do_undo_stub_discard:
CHECK_FREE(4);
PUSH (0); // DestType
PUSH (0); // DestAddr
goto finish_undo_stub;
do_undo_stub_addr:
CHECK_FREE(4);
PUSH (1); // DestType
PUSH (READ_PC); // DestAddr
goto finish_undo_stub;
do_undo_stub_local:
CHECK_FREE(4);
PUSH (2); // DestType
PUSH (READ_PC); // DestAddr
goto finish_undo_stub;
do_undo_stub_stack:
CHECK_FREE(4);
PUSH (3); // DestType
PUSH (0); // DestAddr
goto finish_undo_stub;
finish_undo_stub:
PUSH (READ_PC); // PC
PUSH ((frame - base) * 4); // FramePtr
saveUndo (base, sp);
S1 = 0;
goto do_pop_call_stub;
do_restoreundo:
if (restoreUndo (base, protectPos, protectSize) == 0)
{
sp = gStackPointer;
S1 = -1;
goto do_pop_call_stub;
}
S1 = 1;
NEXT;
do_save_stub_discard:
CHECK_FREE(4);
PUSH (0); // DestType
PUSH (0); // DestAddr
goto finish_save_stub;
do_save_stub_addr:
CHECK_FREE(4);
PUSH (1); // DestType
PUSH (READ_PC); // DestAddr
goto finish_save_stub;
do_save_stub_local:
CHECK_FREE(4);
PUSH (2); // DestType
PUSH (READ_PC); // DestAddr
goto finish_save_stub;
do_save_stub_stack:
CHECK_FREE(4);
PUSH (3); // DestType
PUSH (0); // DestAddr
goto finish_save_stub;
finish_save_stub:
PUSH (READ_PC); // PC
PUSH ((frame - base) * 4); // FramePtr
if (gIoMode == IO_GLK)
S1 = saveToFile (base, sp, L1);
else
S1 = 1;
goto do_pop_call_stub;
do_restore:
if (gIoMode == IO_GLK
&& restoreFromFile (base, L1, protectPos, protectSize) == 0)
{
sp = gStackPointer;
S1 = -1;
goto do_pop_call_stub;
}
S1 = 1;
NEXT;
do_catch_stub_discard:
CHECK_FREE(4);
L7 = 0;
PUSH (0); // DestType
goto finish_catch_stub_addr_L7;
do_catch_stub_addr:
CHECK_FREE(4);
L7 = READ_PC;
memWrite32(L7, (sp-base+4)*4);
PUSH (1); // DestType
goto finish_catch_stub_addr_L7;
do_catch_stub_local:
CHECK_FREE(4);
L7 = READ_PC;
LOCAL(L7 / 4) = (sp-base+4)*4;
PUSH (2); // DestType
goto finish_catch_stub_addr_L7;
do_catch_stub_stack:
CHECK_FREE(5);
PUSH (3); // DestType
PUSH (0); // DestAddr
PUSH (READ_PC); // PC
PUSH ((frame - base) * 4); // FramePtr
L7 = (sp - base)*4; // Catch token.
PUSH (L7);
NEXT;
finish_catch_stub_addr_L7:
PUSH (L7); // DestAddr
PUSH (READ_PC); // PC
PUSH ((frame - base) * 4); // FramePtr
NEXT;
do_throw:
if (L2 < 16 || L2 > ((sp-base)*4))
fatalError ("Invalid catch token in throw");
sp = base + L2 / 4;
goto do_pop_call_stub;
do_call_stub_discard:
CHECK_FREE(4);
PUSH (0); // DestType
PUSH (0); // DestAddr
goto finish_call_stub;
do_call_stub_addr:
CHECK_FREE(4);
PUSH (1); // DestType
PUSH (READ_PC); // DestAddr
goto finish_call_stub;
do_call_stub_local:
CHECK_FREE(4);
PUSH (2); // DestType
PUSH (READ_PC); // DestAddr
goto finish_call_stub;
do_call_stub_stack:
CHECK_FREE(4);
PUSH (3); // DestType
PUSH (0); // DestAddr
goto finish_call_stub;
finish_call_stub:
PUSH (READ_PC); // PC
PUSH ((frame - base) * 4); // FramePtr
goto do_enter_function_L1;
do_tailcall:
// Zap the current stack frame, down to its call stub.
sp = frame;
// Call the function!
goto do_enter_function_L1;
do_return:
sp = frame;
// ...
// fall through
// ...
do_pop_call_stub:// L1 holds the return value.
if (sp - base < 4)
{
if (sp == base)
// We just exited the top-level function.
goto finished;
else
// Something nasty happened.
goto stack_underflow;
}
L2 = POP; // FramePtr
L7 = POP; // PC
L6 = POP; // DestAddr
switch (POP) // DestType
{
case 0: // Do not store.
frame = base + L2 / 4;
locals = frame + frame[1]/4;
values = frame + frame[0]/4;
break;
case 1: // Store in main memory.
frame = base + L2 / 4;
locals = frame + frame[1]/4;
values = frame + frame[0]/4;
memWrite32 (L6, L1);
break;
case 2: // Store in local variable.
frame = base + L2 / 4;
locals = frame + frame[1]/4;
values = frame + frame[0]/4;
LOCAL(L6/4) = L1;
break;
case 3: // Push on stack.
frame = base + L2 / 4;
locals = frame + frame[1]/4;
values = frame + frame[0]/4;
PUSH (L1);
break;
case 10: // Resume printing a compressed (E1) string.
frame = base + L2 / 4;
locals = frame + frame[1]/4;
values = frame + frame[0]/4;
goto resume_compressed_string_L7_bit_L6;
case 11: // Resume executing function code after a string completes.
// Don't restore the frame pointer.
break;
case 12: // Resume printing a signed decimal integer.
frame = base + L2 / 4;
locals = frame + frame[1]/4;
values = frame + frame[0]/4;
goto resume_number_L7_digit_L6;
case 13: // Resume printing a C-style (E0) string.
frame = base + L2 / 4;
locals = frame + frame[1]/4;
values = frame + frame[0]/4;
goto resume_c_string_L7;
case 14: // Resume printing a Unicode (E2) string.
frame = base + L2 / 4;
locals = frame + frame[1]/4;
values = frame + frame[0]/4;
goto resume_uni_string_L7;
default:
fatalError("Bad call stub");
}
// Restore the PC.
goto do_jump_abs_L7;
do_stkcount:
S1 = sp - values; NEXT;
do_stkpeek:
if (L1 < 0 || L1 > (sp - values))
fatalError("Out of bounds in stkpeek");
S1 = sp[-1 - L1]; NEXT;
do_stkswap:
CHECK_USED(2);
L1 = POP; L2 = POP; PUSH(L1); PUSH(L2); NEXT;
do_stkcopy:
CHECK_USED(L1);
for (L2 = L1 ; L2 > 0 ; --L2)
{
L3 = sp[-L1];
PUSH (L3);
}
NEXT;
resume_number_L7_digit_L6:
{
char buffer [16];
git_uint32 absn;
// If the IO mode is 'null', do nothing.
if (gIoMode == IO_NULL)
goto do_pop_call_stub;
// Write the number into the buffer.
absn = (L7 < 0) ? -L7 : L7; // Absolute value of number.
L2 = 0; // Current buffer position.
do
{
buffer [L2++] = '0' + (absn % 10);
absn /= 10;
}
while (absn > 0);
if (L7 < 0)
buffer [L2++] = '-';
if (L6 >= L2)
goto do_pop_call_stub; // We printed the whole number already.
// If we're in filter mode, push a call stub
// and filter the next character.
if (gIoMode == IO_FILTER)
{
// Store the next character in the args array.
args[0] = buffer [L2 - L6 - 1];
++L6;
// Push a call stub to print the next character.
CHECK_FREE(4);
PUSH(12); // DestType
PUSH(L6); // DestAddr (next digit)
PUSH(L7); // PC (number to print)
PUSH ((frame - base) * 4); // FramePtr
// Call the filter function.
L1 = ioRock;
L2 = 1;
goto do_enter_function_L1;
}
else
{
// We're in Glk mode. Just print all the characters.
for ( ; L6 < L2 ; ++L6)
glk_put_char (buffer [L2 - L6 - 1]);
}
}
goto do_pop_call_stub;
resume_c_string_L7:
// If the IO mode is 'null', or if we've reached the
// end of the string, do nothing.
L2 = memRead8(L7++);
if (L2 == 0 || gIoMode == IO_NULL)
goto do_pop_call_stub;
// Otherwise we're going to have to print something,
// If the IO mode is 'filter', filter the next char.
if (gIoMode == IO_FILTER)
{
// Store this character in the args array.
args [0] = L2;
// Push a call stub.
CHECK_FREE(4);
PUSH(13); // DestType (resume C string)
PUSH(L6); // DestAddr (ignored)
PUSH(L7); // PC (next char to print)
PUSH ((frame - base) * 4); // FramePtr
// Call the filter function.
L1 = ioRock;
L2 = 1;
goto do_enter_function_L1;
}
// We're in Glk mode. Just print all the characters.
while (L2 != 0)
{
glk_put_char ((unsigned char) L2);
L2 = memRead8(L7++);
}
goto do_pop_call_stub;
resume_uni_string_L7:
// If the IO mode is 'null', or if we've reached the
// end of the string, do nothing.
L2 = memRead32(L7);
L7 += 4;
if (L2 == 0 || gIoMode == IO_NULL)
goto do_pop_call_stub;
// Otherwise we're going to have to print something,
// If the IO mode is 'filter', filter the next char.
if (gIoMode == IO_FILTER)
{
// Store this character in the args array.
args [0] = L2;
// Push a call stub.
CHECK_FREE(4);
PUSH(14); // DestType (resume Unicode string)
PUSH(L6); // DestAddr (ignored)
PUSH(L7); // PC (next char to print)
PUSH ((frame - base) * 4); // FramePtr
// Call the filter function.
L1 = ioRock;
L2 = 1;
goto do_enter_function_L1;
}
// We're in Glk mode. Just print all the characters.
while (L2 != 0)
{
#ifdef GLK_MODULE_UNICODE
glk_put_char_uni ((glui32) L2);
#else
unsigned char c = (L2 > 0 && L2 < 256) ? L2 : '?';
glk_put_char (c);
#endif // GLK_MODULE_UNICODE
L2 = memRead32(L7);
L7 += 4;
}
goto do_pop_call_stub;
resume_compressed_string_L7_bit_L6:
// Load the first string table node into L1.
// Its address is stored at stringTable + 8.