Debian Codegraph @

terps/git/terp.c

raw symbols SCIP
1 // $Id: terp.c,v 1.42 2004/12/22 14:33:40 iain Exp $
2 // Interpreter engine.
3
4 #include "git.h"
5 #include <assert.h>
6 #include <math.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <time.h>
11
12 // -------------------------------------------------------------
13 // Global variables
14
15 git_sint32* gStackPointer;
16
17 #ifdef USE_DIRECT_THREADING
18 Opcode* gOpcodeTable;
19 #endif
20
21 enum IOMode gIoMode = IO_NULL;
22
23 // -------------------------------------------------------------
24 // Useful macros for manipulating the stack
25
26 #define LOCAL(n) (locals[(n)])
27
28 #define PUSH(n) *sp++ = (n)
29 #define POP (*--sp)
30 #define READ_PC ((git_uint32)(*pc++))
31
32 #define CHECK_FREE(n) if ((top - sp) < (n)) goto stack_overflow
33 #define CHECK_USED(n) if ((sp - values) < (n)) goto stack_underflow
34
35 // -------------------------------------------------------------
36 // Random number generator, from Glulxe
37
38 static glui32 xo_random(void);
39 static void xo_seed_random(glui32 seed);
40
41 static int rand_use_native = 1;
42
43 /* Set the random-number seed, and also select which RNG to use.
44 */
45 void git_seed_random(glui32 seed)
46 {
47 if (seed == 0)
48 {
49 rand_use_native = 1;
50 xo_seed_random(time(NULL));
51 }
52 else
53 {
54 rand_use_native = 0;
55 xo_seed_random(seed);
56 }
57 }
58
59 /* Return a random number in the range 0 to 2^32-1. */
60 glui32 git_random()
61 {
62 #ifdef USE_NATIVE_RANDOM
63 if (rand_use_native)
64 return native_random();
65 #endif
66 return xo_random();
67 }
68
69 /* This is the "xoshiro128**" random-number generator and seed function.
70 Adapted from: https://prng.di.unimi.it/xoshiro128starstar.c
71 About this algorithm: https://prng.di.unimi.it/
72 */
73 static uint32_t xo_table[4];
74
75 static void xo_seed_random(glui32 seed)
76 {
77 int ix;
78 /* Set up the 128-bit state from a single 32-bit integer. We rely
79 on a different RNG, SplitMix32. This isn't high-quality, but we
80 just need to get a bunch of bits into xo_table. */
81 for (ix=0; ix<4; ix++) {
82 seed += 0x9E3779B9;
83 glui32 s = seed;
84 s ^= s >> 15;
85 s *= 0x85EBCA6B;
86 s ^= s >> 13;
87 s *= 0xC2B2AE35;
88 s ^= s >> 16;
89 xo_table[ix] = s;
90 }
91 }
92
93 static glui32 xo_random(void)
94 {
95 /* I've inlined the utility function:
96 rotl(x, k) => (x << k) | (x >> (32 - k))
97 */
98
99 const uint32_t t1x5 = xo_table[1] * 5;
100 const uint32_t result = ((t1x5 << 7) | (t1x5 >> (32-7))) * 9;
101
102 const uint32_t t1s9 = xo_table[1] << 9;
103
104 xo_table[2] ^= xo_table[0];
105 xo_table[3] ^= xo_table[1];
106 xo_table[1] ^= xo_table[2];
107 xo_table[0] ^= xo_table[3];
108
109 xo_table[2] ^= t1s9;
110
111 const uint32_t t3 = xo_table[3];
112 xo_table[3] = ((t3 << 11) | (t3 >> (32-11)));
113
114 return result;
115 }
116
117 // -------------------------------------------------------------
118 // Floating point support
119
120 GIT_INLINE git_uint32 ENCODE_FLOAT(git_float f)
121 {
122 git_uint32 n;
123 memcpy(&n, &f, 4);
124 return n;
125 }
126
127 GIT_INLINE git_float DECODE_FLOAT(git_uint32 n) {
128 git_float f;
129 memcpy(&f, &n, 4);
130 return f;
131 }
132
133 GIT_INLINE void ENCODE_DOUBLE(git_double d, git_sint32 *hi, git_sint32 *lo)
134 {
135 #if defined(USE_BIG_ENDIAN) || defined(USE_BIG_ENDIAN_UNALIGNED)
136 memcpy(hi, &d, 4);
137 memcpy(lo, ((char *)&d)+4, 4);
138 #else
139 memcpy(lo, &d, 4);
140 memcpy(hi, ((char *)&d)+4, 4);
141 #endif
142 }
143
144 GIT_INLINE git_double DECODE_DOUBLE(git_uint32 hi, git_uint32 lo)
145 {
146 git_double d;
147 #if defined(USE_BIG_ENDIAN) || defined(USE_BIG_ENDIAN_UNALIGNED)
148 memcpy(&d, &hi, 4);
149 memcpy(((char *)&d)+4, &lo, 4);
150 #else
151 memcpy(&d, &lo, 4);
152 memcpy(((char *)&d)+4, &hi, 4);
153 #endif
154 return d;
155 }
156
157 static int floatCompare(git_sint32 L1, git_sint32 L2, git_sint32 L3)
158 {
159 git_float F1, F2;
160
161 if (((L3 & 0x7F800000) == 0x7F800000) && ((L3 & 0x007FFFFF) != 0))
162 return 0;
163 if ((L1 == 0x7F800000 || L1 == 0xFF800000) && (L2 == 0x7F800000 || L2 == 0xFF800000))
164 return (L1 == L2);
165
166 F1 = DECODE_FLOAT(L2) - DECODE_FLOAT(L1);
167 F2 = fabs(DECODE_FLOAT(L3));
168 return ((F1 <= F2) && (F1 >= -F2));
169 }
170
171 static int doubleCompare(git_sint32 L1, git_sint32 L2, git_sint32 L3, git_sint32 L4, git_sint32 L5, git_sint32 L6, git_sint32 L7)
172 {
173 git_double D1, D2;
174
175 if (((L5 & 0x7FF00000) == 0x7FF00000) && (((L5 & 0xFFFFF) != 0x0) || (L6 != 0x0)))
176 return 0;
177 if ((L1 == 0x7FF00000 || L1 == 0xFFF00000) && L2 == 0x0 && (L3 == 0x7FF00000 || L3 == 0xFFF00000) && L4 == 0x0)
178 return (L1 == L3);
179
180 D1 = DECODE_DOUBLE(L3, L4) - DECODE_DOUBLE(L1, L2);
181 D2 = fabs(DECODE_DOUBLE(L5, L6));
182 return ((D1 <= D2) && (D1 >= -D2));
183 }
184
185 static void testDouble()
186 {
187 glui32 PI_hi = 0x400921FB;
188 glui32 PI_lo = 0x54442D18;
189 git_double pi = DECODE_DOUBLE(PI_hi, PI_lo);
190 if (!(pi > 3.1415 && pi < 3.1416)) {
191 // If this fails, on a big-endian system, check that USE_BIG_ENDIAN
192 // or USE_BIG_ENDIAN_UNALIGNED have been defined in config.h
193 fatalError("Test decode of double precision value failed");
194 }
195 }
196
197 // -------------------------------------------------------------
198 // Functions
199
200 void startProgram (size_t cacheSize)
201 {
202 Block pc; // Program counter (pointer into dynamically generated code)
203
204 git_sint32 L1=0, L2=0, L3=0, L4=0, L5=0, L6=0, L7=0;
205 #define S1 L1
206 #define S2 L2
207 git_float F1=0.0f, F2=0.0f, F3=0.0f, F4=0.0f;
208 git_double D1=0.0f, D2=0.0f, D3=0.0f;
209
210 git_sint32* base; // Bottom of the stack.
211 git_sint32* frame; // Bottom of the current stack frame.
212 git_sint32* locals; // Start of the locals section of the current frame.
213 git_sint32* values; // Start of the values section of the current frame.
214 git_sint32* sp; // Next free stack slot.
215 git_sint32* top; // The top of the stack -- that is, the first unusable slot.
216
217 git_sint32 args [64]; // Array of arguments. Count is stored in L2.
218
219 git_uint32 ioRock = 0;
220
221 git_uint32 stringTable = memRead32(28);
222 git_uint32 startPos = memRead32(24);
223 git_uint32 stackSize = memRead32(20);
224
225 git_uint32 protectPos = 0;
226 git_uint32 protectSize = 0;
227
228 git_uint32 maybe_unused glulxPC = 0;
229 git_uint32 maybe_unused glulxOpcode = 0;
230
231 acceleration_func accelfunc;
232
233 // Initialise the code cache.
234
235 #ifdef USE_DIRECT_THREADING
236 static Opcode opcodeTable [] = {
237 #define LABEL(label) &&do_ ## label,
238 #include "labels.inc"
239 NULL};
240 gOpcodeTable = opcodeTable;
241 # if (UINTPTR_MAX > 0xffffffffULL)
242 const uintptr_t opcodeHi = (uintptr_t)opcodeTable[0] & ~0xffffffffULL;
243 for (L1 = 1; opcodeTable[L1] != NULL; ++L1)
244 assert (opcodeHi == ((uintptr_t)opcodeTable[L1] & ~0xffffffffULL));
245 # endif
246 #endif
247
248 testDouble ();
249 initCompiler (cacheSize);
250
251 // Initialise the random number generator.
252 git_seed_random (0);
253
254 // Set up the stack.
255
256 base = malloc (stackSize);
257 if (base == NULL)
258 fatalError ("Couldn't allocate stack");
259
260 top = base + (stackSize / 4);
261 frame = locals = values = sp = base;
262
263 // Call the first function.
264
265 L1 = startPos; // Initial PC.
266 L2 = 0; // No arguments.
267 goto do_enter_function_L1;
268
269 #if defined(USE_DIRECT_THREADING) && (UINTPTR_MAX > 0xffffffffULL)
270 #define NEXT do { goto *(Opcode)(*(pc++) | opcodeHi); } while(0)
271 #elif defined(USE_DIRECT_THREADING)
272 #define NEXT do { goto *(Opcode)(*(pc++)); } while(0)
273 #else
274 #define NEXT goto next
275 //#define NEXT do { CHECK_USED(0); CHECK_FREE(0); goto next; } while (0)
276 next:
277 #ifdef GIT_NEED_TICK
278 glk_tick();
279 #endif
280
281 switch (*pc++)
282 {
283 #define LABEL(foo) case label_ ## foo: goto do_ ## foo;
284 #include "labels.inc"
285 default: fatalError("exec: bad opcode");
286 }
287 #endif
288
289 do_debug_step:
290 // This opcode lets us keep track of how the compiled
291 // code relates to the original glulx code.
292 glulxPC = READ_PC; // Glulx program counter.
293 glulxOpcode = READ_PC; // Glulx opcode number.
294 // fprintf (stdout, "\nPC: 0x%08x\nOpcode: 0x%04x\n", glulxPC, glulxOpcode);
295 // fprintf (stdout, "Stack:");
296 // for (L7 = 0 ; L7 < (sp - base) ; ++L7)
297 // fprintf (stdout," 0x%x", base[L7]);
298 // fprintf (stdout, "\n");
299 NEXT;
300
301 #define LOAD_INSTRUCTIONS(reg) \
302 do_ ## reg ## _const: reg = READ_PC; NEXT; \
303 do_ ## reg ## _stack: CHECK_USED(1); reg = POP; NEXT; \
304 do_ ## reg ## _addr: reg = memRead32 (READ_PC); NEXT; \
305 do_ ## reg ## _local: reg = LOCAL (READ_PC); NEXT
306
307 LOAD_INSTRUCTIONS(L1);
308 LOAD_INSTRUCTIONS(L2);
309 LOAD_INSTRUCTIONS(L3);
310 LOAD_INSTRUCTIONS(L4);
311 LOAD_INSTRUCTIONS(L5);
312 LOAD_INSTRUCTIONS(L6);
313 LOAD_INSTRUCTIONS(L7);
314
315 #define STORE_INSTRUCTIONS(reg) \
316 do_ ## reg ## _stack: CHECK_FREE(1); PUSH(reg); NEXT; \
317 do_ ## reg ## _addr: memWrite32 (READ_PC, reg); NEXT; \
318 do_ ## reg ## _local: LOCAL (READ_PC) = reg; NEXT
319
320 STORE_INSTRUCTIONS(S1);
321 STORE_INSTRUCTIONS(S2);
322
323 #define DOUBLE_LOAD(mode2) \
324 do_L1_const_L2_ ## mode2: L1 = READ_PC; goto do_L2_ ## mode2; \
325 do_L1_stack_L2_ ## mode2: CHECK_USED(1); L1 = POP; goto do_L2_ ## mode2; \
326 do_L1_local_L2_ ## mode2: L1 = LOCAL (READ_PC); goto do_L2_ ## mode2; \
327 do_L1_addr_L2_ ## mode2: L1 = memRead32 (READ_PC); goto do_L2_ ## mode2
328
329 DOUBLE_LOAD(const);
330 DOUBLE_LOAD(stack);
331 DOUBLE_LOAD(local);
332 DOUBLE_LOAD(addr);
333
334 #undef LOAD_INSTRUCTIONS
335 #undef STORE_INSTRUCTIONS
336 #undef DOUBLE_LOAD
337
338 do_L1_addr16: L1 = memRead16 (READ_PC); NEXT;
339 do_L1_addr8: L1 = memRead8 (READ_PC); NEXT;
340 do_S1_addr16: memWrite16 (READ_PC, S1); NEXT;
341 do_S1_addr8: memWrite8 (READ_PC, S1); NEXT;
342
343 #define UL7 ((git_uint32)L7)
344
345 do_recompile:
346 pc = compile (*pc);
347 NEXT;
348
349 do_jump_abs_L7:
350 pc = getCode (UL7);
351 NEXT;
352
353 do_enter_function_L1: // Arg count is in L2.
354
355 // Check for an accelerated function
356 accelfunc = accel_get_func(L1);
357 if (accelfunc) {
358 S1 = accelfunc(L2, (glui32 *) args);
359 goto do_pop_call_stub;
360 }
361
362 frame = sp;
363 // Read the function type.
364 L7 = memRead8(L1++);
365 // Parse the local variables descriptor.
366 L6 = L5 = L4 = 0;
367 do
368 {
369 L6 = memRead8(L1++); // LocalType
370 L5 = memRead8(L1++); // LocalCount
371 if (L6 != 4 && L6 != 0) // We only support 4-byte locals.
372 {
373 if (L6 == 1 || L6 == 2)
374 fatalError("Short local variables are not supported, use Glulxe");
375 else
376 fatalError("Local variable wasn't 4 bytes wide");
377 }
378 L4 += L5; // Cumulative local count.
379 }
380 while (L5 != 0);
381
382 // Write out the stack frame.
383 // Recall that the number of locals is stored in L4.
384
385 CHECK_FREE(3 + L4);
386
387 PUSH (L4*4 + 12); // FrameLen
388 PUSH (12); // LocalsPos
389 if (L4 == 0)
390 L6 = 0;
391 else
392 L6 = (4 << 24) | (L4 << 16);
393 PUSH (L6); // format of locals
394
395 // This is where the local variables start, so:
396 locals = sp;
397
398 // Read the arguments, based on the function type.
399 switch (L7)
400 {
401 case 0xC0: // arguments should be placed on the stack.
402 // argc is in L2; we'll randomly use L5 as scratch.
403 CHECK_FREE(L5 + 1);
404 // Initialise the local variables.
405 for ( ; L4 > 0 ; --L4)
406 PUSH (0);
407 // This is where the temporary values start, so:
408 values = sp;
409 // Push the args onto the stack.
410 for (L5 = 0 ; L5 < L2 ; ++L5)
411 PUSH (args [L5]);
412 // Push the arg count.
413 PUSH (L2);
414 break;
415
416 case 0xC1: // arguments should be written into locals.
417 // argc is in L2, num locals is in L4.
418 // Stuff as many locals as possible with arguments.
419 for (L5 = 1 ; L5 <= L2 && L4 > 0 ; ++L5, --L4)
420 PUSH (args [L2 - L5]);
421 // Initialise any remaining locals.
422 for ( ; L4 > 0 ; --L4)
423 PUSH (0);
424 // This is where the temporary values start, so:
425 values = sp;
426 break;
427
428 default:
429 // This isn't a function!
430 fatalError("Not a function");
431 break;
432 }
433
434 // Start executing the function.
435 L7 = L1;
436 goto do_jump_abs_L7;
437
438 do_nop: NEXT;
439
440 #define PEEPHOLE_STORE(tag, code) \
441 do_ ## tag ## _discard: code; NEXT; \
442 do_ ## tag ## _S1_stack: code; goto do_S1_stack; \
443 do_ ## tag ## _S1_local: code; goto do_S1_local; \
444 do_ ## tag ## _S1_addr: code; goto do_S1_addr
445
446 PEEPHOLE_STORE(add, S1 = L1 + L2);
447 PEEPHOLE_STORE(sub, S1 = L1 - L2);
448 PEEPHOLE_STORE(mul, S1 = L1 * L2);
449 PEEPHOLE_STORE(div, if (L2 == 0) fatalError ("Divide by zero"); S1 = L1 / L2);
450 PEEPHOLE_STORE(mod, if (L2 == 0) fatalError ("Divide by zero"); S1 = L1 % L2);
451
452 PEEPHOLE_STORE(neg, S1 = -L1);
453 PEEPHOLE_STORE(bitnot, S1 = ~L1);
454
455 PEEPHOLE_STORE(bitand, S1 = L1 & L2);
456 PEEPHOLE_STORE(bitor, S1 = L1 | L2);
457 PEEPHOLE_STORE(bitxor, S1 = L1 ^ L2);
458
459 PEEPHOLE_STORE(shiftl, if (L2 > 31 || L2 < 0) S1 = 0; else S1 = L1 << ((git_uint32) L2));
460 PEEPHOLE_STORE(sshiftr, if (L2 > 31 || L2 < 0) L2 = 31; S1 = ((git_sint32) L1) >> ((git_uint32) L2));
461 PEEPHOLE_STORE(ushiftr, if (L2 > 31 || L2 < 0) S1 = 0; else S1 = ((git_uint32) L1) >> ((git_uint32) L2));
462
463 PEEPHOLE_STORE(aload, S1 = memRead32 (L1 + (L2<<2)));
464 PEEPHOLE_STORE(aloads, S1 = memRead16 (L1 + (L2<<1)));
465 PEEPHOLE_STORE(aloadb, S1 = memRead8 (L1 + L2));
466 PEEPHOLE_STORE(aloadbit,S1 = (memRead8 (L1 + (L2>>3)) >> (L2 & 7)) & 1);
467
468 PEEPHOLE_STORE(copys, S1 = L1 & 0xFFFF);
469 PEEPHOLE_STORE(copyb, S1 = L1 & 0x00FF);
470 PEEPHOLE_STORE(sexs, S1 = (git_sint32)((signed short)(L1 & 0xFFFF)));
471 PEEPHOLE_STORE(sexb, S1 = (git_sint32)((signed char)(L1 & 0x00FF)));
472
473 PEEPHOLE_STORE(fadd, F1 = DECODE_FLOAT(L1) + DECODE_FLOAT(L2); S1 = ENCODE_FLOAT(F1));
474 PEEPHOLE_STORE(fsub, F1 = DECODE_FLOAT(L1) - DECODE_FLOAT(L2); S1 = ENCODE_FLOAT(F1));
475 PEEPHOLE_STORE(fmul, F1 = DECODE_FLOAT(L1) * DECODE_FLOAT(L2); S1 = ENCODE_FLOAT(F1));
476 PEEPHOLE_STORE(fdiv, F1 = DECODE_FLOAT(L1) / DECODE_FLOAT(L2); S1 = ENCODE_FLOAT(F1));
477
478 #define PEEPHOLE_LOAD(tag,reg) \
479 do_ ## tag ## _ ## reg ## _const: reg = READ_PC; goto do_ ## tag; \
480 do_ ## tag ## _ ## reg ## _stack: CHECK_USED(1); reg = POP; goto do_ ## tag; \
481 do_ ## tag ## _ ## reg ## _local: reg = LOCAL(READ_PC); goto do_ ## tag; \
482 do_ ## tag ## _ ## reg ## _addr: reg = memRead32(READ_PC); goto do_ ## tag
483
484 PEEPHOLE_LOAD (return, L1);
485 PEEPHOLE_LOAD (astore, L3);
486 PEEPHOLE_LOAD (astores, L3);
487 PEEPHOLE_LOAD (astoreb, L3);
488 PEEPHOLE_LOAD (astorebit, L3);
489
490 #undef PEEPHOLE_STORE
491
492 do_astore: memWrite32 (L1 + (L2<<2), L3); NEXT;
493 do_astores: memWrite16 (L1 + (L2<<1), L3); NEXT;
494 do_astoreb: memWrite8 (L1 + L2, L3); NEXT;
495 do_astorebit:
496 L4 = memRead8(L1 + (L2>>3));
497 if (L3 == 0)
498 L4 &= ~(1 << (L2 & 7));
499 else
500 L4 |= (1 << (L2 & 7));
501 memWrite8(L1 + (L2>>3), L4);
502 NEXT;
503
504 #define DO_JUMP(tag, reg, cond) \
505 do_ ## tag ## _var: L7 = READ_PC; if (cond) goto do_goto_ ## reg ## _from_L7; NEXT; \
506 do_ ## tag ## _const: L7 = READ_PC; if (cond) goto do_jump_abs_L7; NEXT; \
507 do_ ## tag ## _by: L7 = READ_PC; if (cond) pc += L7; NEXT; \
508 do_ ## tag ## _return0: if (cond) { L1 = 0; goto do_return; } NEXT; \
509 do_ ## tag ## _return1: if (cond) { L1 = 1; goto do_return; } NEXT
510
511 DO_JUMP(jump, L1, 1 == 1);
512 DO_JUMP(jz, L2, L1 == 0);
513 DO_JUMP(jnz, L2, L1 != 0);
514 DO_JUMP(jeq, L3, L1 == L2);
515 DO_JUMP(jne, L3, L1 != L2);
516 DO_JUMP(jlt, L3, L1 < L2);
517 DO_JUMP(jge, L3, L1 >= L2);
518 DO_JUMP(jgt, L3, L1 > L2);
519 DO_JUMP(jle, L3, L1 <= L2);
520 DO_JUMP(jltu, L3, ((git_uint32)L1 < (git_uint32)L2));
521 DO_JUMP(jgeu, L3, ((git_uint32)L1 >= (git_uint32)L2));
522 DO_JUMP(jgtu, L3, ((git_uint32)L1 > (git_uint32)L2));
523 DO_JUMP(jleu, L3, ((git_uint32)L1 <= (git_uint32)L2));
524 DO_JUMP(jisnan, L2, (((L1 & 0x7F800000) == 0x7F800000) && ((L1 & 0x007FFFFF) != 0)));
525 DO_JUMP(jisinf, L2, ((L1 == 0x7F800000) || (L1 == 0xFF800000)));
526 DO_JUMP(jflt, L3, DECODE_FLOAT(L1) < DECODE_FLOAT(L2));
527 DO_JUMP(jfge, L3, DECODE_FLOAT(L1) >= DECODE_FLOAT(L2));
528 DO_JUMP(jfgt, L3, DECODE_FLOAT(L1) > DECODE_FLOAT(L2));
529 DO_JUMP(jfle, L3, DECODE_FLOAT(L1) <= DECODE_FLOAT(L2));
530 DO_JUMP(jfeq, L4, floatCompare(L1, L2, L3) != 0);
531 DO_JUMP(jfne, L4, floatCompare(L1, L2, L3) == 0);
532 DO_JUMP(jdlt, L5, DECODE_DOUBLE(L1, L2) < DECODE_DOUBLE(L3, L4));
533 DO_JUMP(jdge, L5, DECODE_DOUBLE(L1, L2) >= DECODE_DOUBLE(L3, L4));
534 DO_JUMP(jdgt, L5, DECODE_DOUBLE(L1, L2) > DECODE_DOUBLE(L3, L4));
535 DO_JUMP(jdle, L5, DECODE_DOUBLE(L1, L2) <= DECODE_DOUBLE(L3, L4));
536 DO_JUMP(jdeq, L7, doubleCompare(L1, L2, L3, L4, L5, L6, L7) != 0);
537 DO_JUMP(jdne, L7, doubleCompare(L1, L2, L3, L4, L5, L6, L7) == 0);
538 DO_JUMP(jdisinf, L3, (((L1 == 0x7FF00000) || (L1 == 0xFFF00000)) && L2 == 0x0));
539 DO_JUMP(jdisnan, L3, (((L1 & 0x7FF00000) == 0x7FF00000) && (((L1 & 0xFFFFF) != 0) || (L2 != 0x0))));
540
541 #undef DO_JUMP
542
543 do_jumpabs: L7 = L1; goto do_jump_abs_L7; NEXT;
544
545 do_goto_L7_from_L7: L1 = L7; goto do_goto_L1_from_L7;
546 do_goto_L5_from_L7: L1 = L5; goto do_goto_L1_from_L7;
547 do_goto_L4_from_L7: L1 = L4; goto do_goto_L1_from_L7;
548 do_goto_L3_from_L7: L1 = L3; goto do_goto_L1_from_L7;
549 do_goto_L2_from_L7: L1 = L2; goto do_goto_L1_from_L7;
550 do_goto_L1_from_L7:
551 if (L1 == 0 || L1 == 1) goto do_return;
552 L7 = L7 + L1 - 2; goto do_jump_abs_L7;
553
554 do_args_stack:
555 // The first argument is topmost in the stack; the count is in L2.
556 CHECK_USED(L2);
557 // We want to store the arguments in 'args' in the same order.
558 for (L3 = L2 - 1 ; L3 >= 0 ; --L3)
559 args [L3] = POP;
560 NEXT;
561
562 // Specialised versions of above:
563 do_args_stack_call_stub_discard:
564 CHECK_USED(L2);
565 for (L3 = L2 - 1 ; L3 >= 0 ; --L3)
566 args [L3] = POP;
567 goto do_call_stub_discard;
568
569 do_args_stack_call_stub_addr:
570 CHECK_USED(L2);
571 for (L3 = L2 - 1 ; L3 >= 0 ; --L3)
572 args [L3] = POP;
573 goto do_call_stub_addr;
574
575 do_args_stack_call_stub_local:
576 CHECK_USED(L2);
577 for (L3 = L2 - 1 ; L3 >= 0 ; --L3)
578 args [L3] = POP;
579 goto do_call_stub_local;
580
581 do_args_stack_call_stub_stack:
582 CHECK_USED(L2);
583 for (L3 = L2 - 1 ; L3 >= 0 ; --L3)
584 args [L3] = POP;
585 goto do_call_stub_stack;
586
587 do_args_3:
588 args [0] = L4;
589 args [1] = L3;
590 args [2] = L2;
591 L2 = 3;
592 NEXT;
593
594 do_args_2:
595 args [0] = L3;
596 args [1] = L2;
597 L2 = 2;
598 NEXT;
599
600 do_args_1:
601 args [0] = L2;
602 L2 = 1;
603 NEXT;
604
605 do_args_0:
606 L2 = 0;
607 NEXT;
608
609 do_undo_stub_discard:
610 CHECK_FREE(4);
611 PUSH (0); // DestType
612 PUSH (0); // DestAddr
613 goto finish_undo_stub;
614
615 do_undo_stub_addr:
616 CHECK_FREE(4);
617 PUSH (1); // DestType
618 PUSH (READ_PC); // DestAddr
619 goto finish_undo_stub;
620
621 do_undo_stub_local:
622 CHECK_FREE(4);
623 PUSH (2); // DestType
624 PUSH (READ_PC); // DestAddr
625 goto finish_undo_stub;
626
627 do_undo_stub_stack:
628 CHECK_FREE(4);
629 PUSH (3); // DestType
630 PUSH (0); // DestAddr
631 goto finish_undo_stub;
632
633 finish_undo_stub:
634 PUSH (READ_PC); // PC
635 PUSH ((frame - base) * 4); // FramePtr
636 saveUndo (base, sp);
637 S1 = 0;
638 goto do_pop_call_stub;
639
640 do_restoreundo:
641 if (restoreUndo (base, protectPos, protectSize) == 0)
642 {
643 sp = gStackPointer;
644 S1 = -1;
645 goto do_pop_call_stub;
646 }
647 S1 = 1;
648 NEXT;
649
650 do_save_stub_discard:
651 CHECK_FREE(4);
652 PUSH (0); // DestType
653 PUSH (0); // DestAddr
654 goto finish_save_stub;
655
656 do_save_stub_addr:
657 CHECK_FREE(4);
658 PUSH (1); // DestType
659 PUSH (READ_PC); // DestAddr
660 goto finish_save_stub;
661
662 do_save_stub_local:
663 CHECK_FREE(4);
664 PUSH (2); // DestType
665 PUSH (READ_PC); // DestAddr
666 goto finish_save_stub;
667
668 do_save_stub_stack:
669 CHECK_FREE(4);
670 PUSH (3); // DestType
671 PUSH (0); // DestAddr
672 goto finish_save_stub;
673
674 finish_save_stub:
675 PUSH (READ_PC); // PC
676 PUSH ((frame - base) * 4); // FramePtr
677 if (gIoMode == IO_GLK)
678 S1 = saveToFile (base, sp, L1);
679 else
680 S1 = 1;
681 goto do_pop_call_stub;
682
683 do_restore:
684 if (gIoMode == IO_GLK
685 && restoreFromFile (base, L1, protectPos, protectSize) == 0)
686 {
687 sp = gStackPointer;
688 S1 = -1;
689 goto do_pop_call_stub;
690 }
691 S1 = 1;
692 NEXT;
693
694 do_catch_stub_discard:
695 CHECK_FREE(4);
696 L7 = 0;
697 PUSH (0); // DestType
698 goto finish_catch_stub_addr_L7;
699
700 do_catch_stub_addr:
701 CHECK_FREE(4);
702 L7 = READ_PC;
703 memWrite32(L7, (sp-base+4)*4);
704 PUSH (1); // DestType
705 goto finish_catch_stub_addr_L7;
706
707 do_catch_stub_local:
708 CHECK_FREE(4);
709 L7 = READ_PC;
710 LOCAL(L7 / 4) = (sp-base+4)*4;
711 PUSH (2); // DestType
712 goto finish_catch_stub_addr_L7;
713
714 do_catch_stub_stack:
715 CHECK_FREE(5);
716 PUSH (3); // DestType
717 PUSH (0); // DestAddr
718 PUSH (READ_PC); // PC
719 PUSH ((frame - base) * 4); // FramePtr
720 L7 = (sp - base)*4; // Catch token.
721 PUSH (L7);
722 NEXT;
723
724 finish_catch_stub_addr_L7:
725 PUSH (L7); // DestAddr
726 PUSH (READ_PC); // PC
727 PUSH ((frame - base) * 4); // FramePtr
728 NEXT;
729
730 do_throw:
731 if (L2 < 16 || L2 > ((sp-base)*4))
732 fatalError ("Invalid catch token in throw");
733 sp = base + L2 / 4;
734 goto do_pop_call_stub;
735
736 do_call_stub_discard:
737 CHECK_FREE(4);
738 PUSH (0); // DestType
739 PUSH (0); // DestAddr
740 goto finish_call_stub;
741
742 do_call_stub_addr:
743 CHECK_FREE(4);
744 PUSH (1); // DestType
745 PUSH (READ_PC); // DestAddr
746 goto finish_call_stub;
747
748 do_call_stub_local:
749 CHECK_FREE(4);
750 PUSH (2); // DestType
751 PUSH (READ_PC); // DestAddr
752 goto finish_call_stub;
753
754 do_call_stub_stack:
755 CHECK_FREE(4);
756 PUSH (3); // DestType
757 PUSH (0); // DestAddr
758 goto finish_call_stub;
759
760 finish_call_stub:
761 PUSH (READ_PC); // PC
762 PUSH ((frame - base) * 4); // FramePtr
763 goto do_enter_function_L1;
764
765 do_tailcall:
766 // Zap the current stack frame, down to its call stub.
767 sp = frame;
768 // Call the function!
769 goto do_enter_function_L1;
770
771 do_return:
772 sp = frame;
773 // ...
774 // fall through
775 // ...
776 do_pop_call_stub:// L1 holds the return value.
777 if (sp - base < 4)
778 {
779 if (sp == base)
780 // We just exited the top-level function.
781 goto finished;
782 else
783 // Something nasty happened.
784 goto stack_underflow;
785 }
786 L2 = POP; // FramePtr
787 L7 = POP; // PC
788 L6 = POP; // DestAddr
789 switch (POP) // DestType
790 {
791 case 0: // Do not store.
792 frame = base + L2 / 4;
793 locals = frame + frame[1]/4;
794 values = frame + frame[0]/4;
795 break;
796
797 case 1: // Store in main memory.
798 frame = base + L2 / 4;
799 locals = frame + frame[1]/4;
800 values = frame + frame[0]/4;
801 memWrite32 (L6, L1);
802 break;
803
804 case 2: // Store in local variable.
805 frame = base + L2 / 4;
806 locals = frame + frame[1]/4;
807 values = frame + frame[0]/4;
808 LOCAL(L6/4) = L1;
809 break;
810
811 case 3: // Push on stack.
812 frame = base + L2 / 4;
813 locals = frame + frame[1]/4;
814 values = frame + frame[0]/4;
815 PUSH (L1);
816 break;
817
818 case 10: // Resume printing a compressed (E1) string.
819 frame = base + L2 / 4;
820 locals = frame + frame[1]/4;
821 values = frame + frame[0]/4;
822 goto resume_compressed_string_L7_bit_L6;
823
824 case 11: // Resume executing function code after a string completes.
825 // Don't restore the frame pointer.
826 break;
827
828 case 12: // Resume printing a signed decimal integer.
829 frame = base + L2 / 4;
830 locals = frame + frame[1]/4;
831 values = frame + frame[0]/4;
832 goto resume_number_L7_digit_L6;
833
834 case 13: // Resume printing a C-style (E0) string.
835 frame = base + L2 / 4;
836 locals = frame + frame[1]/4;
837 values = frame + frame[0]/4;
838 goto resume_c_string_L7;
839
840 case 14: // Resume printing a Unicode (E2) string.
841 frame = base + L2 / 4;
842 locals = frame + frame[1]/4;
843 values = frame + frame[0]/4;
844 goto resume_uni_string_L7;
845
846 default:
847 fatalError("Bad call stub");
848 }
849 // Restore the PC.
850 goto do_jump_abs_L7;
851
852 do_stkcount:
853 S1 = sp - values; NEXT;
854
855 do_stkpeek:
856 if (L1 < 0 || L1 > (sp - values))
857 fatalError("Out of bounds in stkpeek");
858 S1 = sp[-1 - L1]; NEXT;
859
860 do_stkswap:
861 CHECK_USED(2);
862 L1 = POP; L2 = POP; PUSH(L1); PUSH(L2); NEXT;
863
864 do_stkcopy:
865 CHECK_USED(L1);
866 for (L2 = L1 ; L2 > 0 ; --L2)
867 {
868 L3 = sp[-L1];
869 PUSH (L3);
870 }
871 NEXT;
872
873 resume_number_L7_digit_L6:
874 {
875 char buffer [16];
876 git_uint32 absn;
877
878 // If the IO mode is 'null', do nothing.
879 if (gIoMode == IO_NULL)
880 goto do_pop_call_stub;
881
882 // Write the number into the buffer.
883 absn = (L7 < 0) ? -L7 : L7; // Absolute value of number.
884 L2 = 0; // Current buffer position.
885 do
886 {
887 buffer [L2++] = '0' + (absn % 10);
888 absn /= 10;
889 }
890 while (absn > 0);
891
892 if (L7 < 0)
893 buffer [L2++] = '-';
894
895 if (L6 >= L2)
896 goto do_pop_call_stub; // We printed the whole number already.
897
898 // If we're in filter mode, push a call stub
899 // and filter the next character.
900 if (gIoMode == IO_FILTER)
901 {
902 // Store the next character in the args array.
903 args[0] = buffer [L2 - L6 - 1];
904 ++L6;
905
906 // Push a call stub to print the next character.
907 CHECK_FREE(4);
908 PUSH(12); // DestType
909 PUSH(L6); // DestAddr (next digit)
910 PUSH(L7); // PC (number to print)
911 PUSH ((frame - base) * 4); // FramePtr
912
913 // Call the filter function.
914 L1 = ioRock;
915 L2 = 1;
916 goto do_enter_function_L1;
917 }
918 else
919 {
920 // We're in Glk mode. Just print all the characters.
921 for ( ; L6 < L2 ; ++L6)
922 glk_put_char (buffer [L2 - L6 - 1]);
923 }
924 }
925 goto do_pop_call_stub;
926
927 resume_c_string_L7:
928 // If the IO mode is 'null', or if we've reached the
929 // end of the string, do nothing.
930 L2 = memRead8(L7++);
931 if (L2 == 0 || gIoMode == IO_NULL)
932 goto do_pop_call_stub;
933 // Otherwise we're going to have to print something,
934 // If the IO mode is 'filter', filter the next char.
935 if (gIoMode == IO_FILTER)
936 {
937 // Store this character in the args array.
938 args [0] = L2;
939 // Push a call stub.
940 CHECK_FREE(4);
941 PUSH(13); // DestType (resume C string)
942 PUSH(L6); // DestAddr (ignored)
943 PUSH(L7); // PC (next char to print)
944 PUSH ((frame - base) * 4); // FramePtr
945 // Call the filter function.
946 L1 = ioRock;
947 L2 = 1;
948 goto do_enter_function_L1;
949 }
950 // We're in Glk mode. Just print all the characters.
951 while (L2 != 0)
952 {
953 glk_put_char ((unsigned char) L2);
954 L2 = memRead8(L7++);
955 }
956 goto do_pop_call_stub;
957
958 resume_uni_string_L7:
959 // If the IO mode is 'null', or if we've reached the
960 // end of the string, do nothing.
961 L2 = memRead32(L7);
962 L7 += 4;
963 if (L2 == 0 || gIoMode == IO_NULL)
964 goto do_pop_call_stub;
965 // Otherwise we're going to have to print something,
966 // If the IO mode is 'filter', filter the next char.
967 if (gIoMode == IO_FILTER)
968 {
969 // Store this character in the args array.
970 args [0] = L2;
971 // Push a call stub.
972 CHECK_FREE(4);
973 PUSH(14); // DestType (resume Unicode string)
974 PUSH(L6); // DestAddr (ignored)
975 PUSH(L7); // PC (next char to print)
976 PUSH ((frame - base) * 4); // FramePtr
977 // Call the filter function.
978 L1 = ioRock;
979 L2 = 1;
980 goto do_enter_function_L1;
981 }
982 // We're in Glk mode. Just print all the characters.
983 while (L2 != 0)
984 {
985 #ifdef GLK_MODULE_UNICODE
986 glk_put_char_uni ((glui32) L2);
987 #else
988 unsigned char c = (L2 > 0 && L2 < 256) ? L2 : '?';
989 glk_put_char (c);
990 #endif // GLK_MODULE_UNICODE
991 L2 = memRead32(L7);
992 L7 += 4;
993 }
994 goto do_pop_call_stub;
995
996 resume_compressed_string_L7_bit_L6:
997 // Load the first string table node into L1.
998 // Its address is stored at stringTable + 8.
999 L1 = memRead32 (stringTable + 8);
1000 // Load the node's type byte.
1001 L2 = memRead8 (L1++);
1002 // Is the root node a branch?
1003 if (L2 == 0)
1004 {
1005 // We'll keep a reservoir of input bits in L5.
1006 L5 = memRead8(L7);
1007 // Keep following branch nodes until we hit a leaf node.
1008 while (L2 == 0)
1009 {
1010 // Read the next bit.
1011 L4 = (L5 >> L6) & 1;
1012 // If we're finished reading this byte,
1013 // move on to the next one.
1014 if (++L6 > 7)
1015 {
1016 L6 -= 8;
1017 L5 = memRead8(++L7);
1018 }
1019 // Follow the branch.
1020 L1 = memRead32(L1 + 4 * L4);
1021 L2 = memRead8 (L1++);
1022 }
1023 }
1024 else if (L2 == 2 || L2 == 3)
1025 {
1026 // The root node prints a single character or a string.
1027 // This will produce infinite output in the Null or Glk
1028 // I/O modes, so we'll catch that here.
1029
1030 if (gIoMode != IO_FILTER)
1031 fatalError ("String table prints infinite strings!");
1032
1033 // In Filter mode, the output will be sent to the current
1034 // filter function, which can change the string table
1035 // before returning, so we'll continue and see what happens.
1036 }
1037 // We're at a leaf node.
1038 switch (L2)
1039 {
1040 case 1: // Terminator.
1041 goto do_pop_call_stub;
1042
1043 case 2: // Single char.
1044 if (gIoMode == IO_NULL)
1045 { /* Do nothing */ }
1046 else if (gIoMode == IO_GLK)
1047 glk_put_char ((unsigned char) memRead8(L1));
1048 else
1049 {
1050 // Store this character in the args array.
1051 args [0] = memRead8(L1);
1052 // Push a call stub.
1053 CHECK_FREE(4);
1054 PUSH(10); // DestType
1055 PUSH(L6); // DestAddr (bit number in string)
1056 PUSH(L7); // PC (byte address in string)
1057 PUSH ((frame - base) * 4); // FramePtr
1058 // Call the filter function.
1059 L1 = ioRock;
1060 L2 = 1;
1061 goto do_enter_function_L1;
1062 }
1063 break;
1064
1065 case 3: // C string.
1066 // Push a 'resume compressed string' call stub.
1067 CHECK_FREE(4);
1068 PUSH (10); // DestType
1069 PUSH (L6); // DestAddr (bit number in string)
1070 PUSH (L7); // PC (byte address in string)
1071 PUSH ((frame - base) * 4); // FramePtr
1072 // Print the C string.
1073 L7 = L1;
1074 goto resume_c_string_L7;
1075
1076 case 4: // Unicode char
1077 if (gIoMode == IO_NULL)
1078 { /* Do nothing */ }
1079 else if (gIoMode == IO_GLK)
1080 {
1081 #ifdef GLK_MODULE_UNICODE
1082 glk_put_char_uni (memRead32(L1));
1083 #else
1084 git_uint32 c = memRead32(L1);
1085 if (c > 255) c = '?';
1086 glk_put_char ((unsigned char) c);
1087 #endif // GLK_MODULE_UNICODE
1088 }
1089 else
1090 {
1091 // Store this character in the args array.
1092 args [0] = memRead32(L1);
1093 // Push a call stub.
1094 CHECK_FREE(4);
1095 PUSH(10); // DestType
1096 PUSH(L6); // DestAddr (bit number in string)
1097 PUSH(L7); // PC (byte address in string)
1098 PUSH ((frame - base) * 4); // FramePtr
1099 // Call the filter function.
1100 L1 = ioRock;
1101 L2 = 1;
1102 goto do_enter_function_L1;
1103 }
1104 break;
1105
1106 case 5: // Unicode string.
1107 // Push a 'resume compressed string' call stub.
1108 CHECK_FREE(4);
1109 PUSH (10); // DestType
1110 PUSH (L6); // DestAddr (bit number in string)
1111 PUSH (L7); // PC (byte address in string)
1112 PUSH ((frame - base) * 4); // FramePtr
1113 // Print the Unicode string.
1114 L7 = L1;
1115 goto resume_uni_string_L7;
1116
1117 case 8: // Indirect reference.
1118 L3 = memRead32(L1);
1119 L2 = 0; goto indirect_L3_args_L2;
1120
1121 case 9: // Double-indirect reference.
1122 L3 = memRead32(L1); L3 = memRead32(L3);
1123 L2 = 0; goto indirect_L3_args_L2;
1124
1125 case 10: // Indirect reference with args.
1126 L3 = memRead32(L1);
1127 L2 = memRead32(L1 + 4); goto indirect_L3_args_L2;
1128
1129 case 11: // Double-indirect reference with args.
1130 L3 = memRead32(L1); L3 = memRead32(L3);
1131 L2 = memRead32(L1 + 4); goto indirect_L3_args_L2;
1132
1133 indirect_L3_args_L2:
1134 // Push a 'resume compressed string' call stub.
1135 CHECK_FREE(4);
1136 PUSH (10); // DestType
1137 PUSH (L6); // DestAddr (bit number in string)
1138 PUSH (L7); // PC (byte address in string)
1139 PUSH ((frame - base) * 4); // FramePtr
1140 // Check the type of the embedded object.
1141 switch (memRead8(L3))
1142 {
1143 case 0xE0: // C string.
1144 L7 = L3 + 1;
1145 goto resume_c_string_L7;
1146
1147 case 0xE1: // Compressed string.
1148 L7 = L3 + 1;
1149 L6 = 0;
1150 goto resume_compressed_string_L7_bit_L6;
1151
1152 case 0xE2: // Unicode string.
1153 L7 = L3 + 4; // Skip extra three padding bytes.
1154 goto resume_uni_string_L7;
1155
1156 case 0xC0: case 0xC1: // Function.
1157 // Retrieve arguments.
1158 for (L1 += 8, L4 = L2; L4 > 0 ; --L4, L1+=4)
1159 args[L4-1] = memRead32(L1);
1160 // Enter function.
1161 L1 = L3;
1162 goto do_enter_function_L1;
1163
1164 default: fatalError ("Embedded object in string has unknown type");
1165 }
1166 break;
1167
1168 default: fatalError ("Unknown string table node type");
1169 }
1170 // Start back at the root node again.
1171 goto resume_compressed_string_L7_bit_L6;
1172
1173 do_streamstr:
1174 // Push a 'resume function' call stub.
1175 CHECK_FREE(4);
1176 PUSH (11); // DestType
1177 PUSH (0); // Addr
1178 PUSH (READ_PC); // PC
1179 PUSH ((frame - base) * 4); // FramePtr
1180
1181 // Load the string's type byte.
1182 L2 = memRead8(L1++);
1183 if (L2 == 0xE0)
1184 {
1185 // Uncompressed string.
1186 L7 = L1;
1187 goto resume_c_string_L7;
1188 }
1189 else if (L2 == 0xE1)
1190 {
1191 // Compressed string.
1192 L7 = L1;
1193 L6 = 0;
1194 goto resume_compressed_string_L7_bit_L6;
1195 }
1196 else if (L2 == 0xE2)
1197 {
1198 // Uncompressed Unicode string.
1199 L7 = L1 + 3; // Skip three padding bytes.
1200 goto resume_uni_string_L7;
1201 }
1202 else
1203 {
1204 fatalError ("Value used in streamstr was not a string");
1205 goto finished;
1206 }
1207
1208 do_streamchar:
1209 L7 = READ_PC;
1210 if (gIoMode == IO_NULL)
1211 { /* Do nothing */ }
1212 else if (gIoMode == IO_GLK)
1213 {
1214 unsigned char c = (L1 & 0xff);
1215 glk_put_char (c);
1216 }
1217 else
1218 {
1219 // Store this character in the args array.
1220 args [0] = (L1 & 0xff);
1221 // Push a 'resume function' call stub.
1222 CHECK_FREE(4);
1223 PUSH (0); // DestType
1224 PUSH (0); // Addr
1225 PUSH (L7); // PC
1226 PUSH ((frame - base) * 4); // FramePtr
1227 // Call the filter function.
1228 L1 = ioRock;
1229 L2 = 1;
1230 goto do_enter_function_L1;
1231 }
1232 NEXT;
1233
1234 do_streamunichar:
1235 L7 = READ_PC;
1236 if (gIoMode == IO_NULL)
1237 { /* Do nothing */ }
1238 else if (gIoMode == IO_GLK)
1239 {
1240 #ifdef GLK_MODULE_UNICODE
1241 glk_put_char_uni ((glui32) L1);
1242 #else
1243 unsigned char c = (L1 > 0 && L1 < 256) ? L1 : '?';
1244 glk_put_char (c);
1245 #endif // GLK_MODULE_UNICODE
1246 }
1247 else
1248 {
1249 // Store this character in the args array.
1250 args [0] = L1;
1251 // Push a 'resume function' call stub.
1252 CHECK_FREE(4);
1253 PUSH (0); // DestType
1254 PUSH (0); // Addr
1255 PUSH (L7); // PC
1256 PUSH ((frame - base) * 4); // FramePtr
1257 // Call the filter function.
1258 L1 = ioRock;
1259 L2 = 1;
1260 goto do_enter_function_L1;
1261 }
1262 NEXT;
1263
1264 do_streamnum:
1265 // Push a 'resume function' call stub.
1266 CHECK_FREE(4);
1267 PUSH (11); // DestType
1268 PUSH (0); // Addr
1269 PUSH (READ_PC); // PC
1270 PUSH ((frame - base) * 4); // FramePtr
1271
1272 // Print the number.
1273 L7 = L1;
1274 L6 = 0;
1275 goto resume_number_L7_digit_L6;
1276
1277 // Stub opcodes:
1278
1279 do_getmemsize:
1280 S1 = gEndMem;
1281 NEXT;
1282
1283 do_getiosys:
1284 S1 = gIoMode;
1285 S2 = ioRock;
1286 NEXT;
1287
1288 do_setiosys:
1289 switch (L1)
1290 {
1291 case IO_NULL:
1292 case IO_FILTER:
1293 case IO_GLK:
1294 gIoMode = (enum IOMode) L1;
1295 ioRock = L2;
1296 break;
1297
1298 default:
1299 fatalError ("Illegal I/O mode");
1300 break;
1301 }
1302 NEXT;
1303
1304 do_quit:
1305 goto finished;
1306
1307 do_restart:
1308 // Reset game memory to its initial state.
1309 resetMemory(protectPos, protectSize);
1310
1311 // Reset all the stack pointers.
1312 frame = locals = values = sp = base;
1313
1314 // Call the first function.
1315 L1 = startPos; // Initial PC.
1316 L2 = 0; // No arguments.
1317 goto do_enter_function_L1;
1318
1319 do_verify:
1320 S1 = verifyMemory();
1321 NEXT;
1322
1323 do_random:
1324 if (L1 > 0)
1325 S1 = git_random () % L1;
1326 else if (L1 < 0)
1327 S1 = -(git_random () % -L1);
1328 else
1329 {
1330 S1 = git_random ();
1331 }
1332 NEXT;
1333
1334 do_setrandom:
1335 git_seed_random (L1);
1336 NEXT;
1337
1338 do_glk:
1339 // The first argument is topmost in the stack; count is in L2.
1340 CHECK_USED(L2);
1341 // We want to store the arguments in 'args' in the same order.
1342 for (L3 = 0 ; L3 < L2 ; ++L3)
1343 args [L3] = POP;
1344 gStackPointer = sp;
1345 S1 = git_perform_glk (L1, L2, (glui32*) args);
1346 sp = gStackPointer;
1347 NEXT;
1348
1349 do_binarysearch:
1350 S1 = git_binary_search (L1, L2, L3, L4, L5, L6, L7);
1351 NEXT;
1352
1353 do_linearsearch:
1354 S1 = git_linear_search (L1, L2, L3, L4, L5, L6, L7);
1355 NEXT;
1356
1357 do_linkedsearch:
1358 S1 = git_linked_search (L1, L2, L3, L4, L5, L6);
1359 NEXT;
1360
1361 do_gestalt:
1362 S1 = gestalt (L1, L2);
1363 NEXT;
1364
1365 do_getstringtbl: S1 = stringTable; NEXT;
1366 do_setstringtbl: stringTable = L1; NEXT;
1367
1368 do_debugtrap:
1369 // TODO: do something useful here.
1370 NEXT;
1371
1372 do_stkroll:
1373 // We need to rotate the top L1 elements by L2 places.
1374 if (L1 < 0)
1375 fatalError ("Negative number of elements to rotate in stkroll");
1376 if (L1 > (sp - values))
1377 fatalError ("Tried to rotate too many elements in stkroll");
1378 if (L1 == 0)
1379 NEXT;
1380 // Now, let's normalise L2 into the range [0..L1).
1381 if (L2 >= 0)
1382 L2 = L2 % L1;
1383 else
1384 L2 = L1 - (-L2 % L1);
1385 // Avoid trivial cases.
1386 if (L2 == 0 || L2 == L1)
1387 NEXT;
1388 L2 = L1 - L2;
1389 // The problem is reduced to swapping elements [0..L2) with
1390 // elements [L2..L1). Let's call these two sequences A and B,
1391 // so we need to transform AB into BA. We do this sneakily
1392 // with reversals, as follows: AB -> A'B -> A'B' -> (A'B')',
1393 // where X' is the reverse of the sequence X.
1394 #define SWAP(x,y) \
1395 do { L4 = sp[(x)-L1];sp[(x)-L1]=sp[(y)-L1];sp[(y)-L1]=L4; } while (0)
1396
1397 // Reverse [0..L2).
1398 for (L3 = 0 ; L3 < L2/2 ; ++L3)
1399 SWAP (L3, L2-1-L3);
1400 // Reverse [L2..L1).
1401 for (L3 = L2 ; L3 < (L2 + (L1-L2)/2) ; ++L3)
1402 SWAP (L3, L1-1-(L3-L2));
1403 // Reverse [0..L1).
1404 for (L3 = 0 ; L3 < L1/2 ; ++L3)
1405 SWAP (L3, L1-1-L3);
1406
1407 #undef SWAP
1408 // And we're done!
1409 NEXT;
1410
1411 do_setmemsize:
1412 S1 = resizeMemory (L1, 0);
1413 NEXT;
1414
1415 do_protect:
1416 protectPos = L1;
1417 protectSize = L2;
1418 NEXT;
1419
1420 // Memory management (new with glulx spec 3.1)
1421
1422 do_mzero:
1423 if (L1 > 0) {
1424 if (L2 < gRamStart || (L2 + L1) > gEndMem)
1425 memWriteError(L2);
1426 memset(gMem + L2, 0, L1);
1427 }
1428 NEXT;
1429
1430 do_mcopy:
1431 if (L1 > 0) {
1432 if (L2 < 0 || (L2 + L1) > gEndMem)
1433 memReadError(L2);
1434 if (L3 < gRamStart || (L3 + L1) > gEndMem)
1435 memWriteError(L3);
1436 memmove(gMem + L3, gMem + L2, L1);
1437 }
1438 NEXT;
1439
1440 do_malloc:
1441 S1 = heap_alloc(L1);
1442 NEXT;
1443
1444 do_mfree:
1445 heap_free(L1);
1446 NEXT;
1447
1448 // Function acceleration (new with glulx spec 3.1.1)
1449
1450 do_accelfunc:
1451 accel_set_func(L1, L2);
1452 NEXT;
1453
1454 do_accelparam:
1455 accel_set_param(L1, L2);
1456 NEXT;
1457
1458 // Floating point (new with glulx spec 3.1.2)
1459
1460 do_numtof:
1461 F1 = (git_float) L1;
1462 S1 = ENCODE_FLOAT(F1);
1463 NEXT;
1464
1465 do_ftonumz:
1466 F1 = DECODE_FLOAT(L1);
1467 if (!signbit(F1)) {
1468 if (isnan(F1) || isinf(F1) || (F1 > 2147483647.0))
1469 S1 = 0x7FFFFFFF;
1470 else
1471 S1 = (git_sint32) truncf(F1);
1472 } else {
1473 if (isnan(F1) || isinf(F1) || (F1 < -2147483647.0))
1474 S1 = 0x80000000;
1475 else
1476 S1 = (git_sint32) truncf(F1);
1477 }
1478 NEXT;
1479
1480 do_ftonumn:
1481 F1 = DECODE_FLOAT(L1);
1482 if (!signbit(F1)) {
1483 if (isnan(F1) || isinf(F1) || (F1 > 2147483647.0))
1484 S1 = 0x7FFFFFFF;
1485 else
1486 S1 = (git_sint32) roundf(F1);
1487 } else {
1488 if (isnan(F1) || isinf(F1) || (F1 < -2147483647.0))
1489 S1 = 0x80000000;
1490 else
1491 S1 = (git_sint32) roundf(F1);
1492 }
1493 NEXT;
1494
1495 do_ceil:
1496 F1 = ceilf(DECODE_FLOAT(L1));
1497 L2 = ENCODE_FLOAT(F1);
1498 if ((L2 == 0x0) || (L2 == 0x80000000))
1499 L2 = L1 & 0x80000000;
1500 S1 = L2;
1501 NEXT;
1502
1503 do_floor:
1504 F1 = floorf(DECODE_FLOAT(L1));
1505 S1 = ENCODE_FLOAT(F1);
1506 NEXT;
1507
1508 do_sqrt:
1509 F1 = sqrtf(DECODE_FLOAT(L1));
1510 S1 = ENCODE_FLOAT(F1);
1511 NEXT;
1512
1513 do_exp:
1514 F1 = expf(DECODE_FLOAT(L1));
1515 S1 = ENCODE_FLOAT(F1);
1516 NEXT;
1517
1518 do_log:
1519 F1 = logf(DECODE_FLOAT(L1));
1520 S1 = ENCODE_FLOAT(F1);
1521 NEXT;
1522
1523 do_pow:
1524 F1 = powf(DECODE_FLOAT(L1), DECODE_FLOAT(L2));
1525 S1 = ENCODE_FLOAT(F1);
1526 NEXT;
1527
1528 do_atan2:
1529 F1 = atan2f(DECODE_FLOAT(L1), DECODE_FLOAT(L2));
1530 S1 = ENCODE_FLOAT(F1);
1531 NEXT;
1532
1533 do_fmod:
1534 F1 = DECODE_FLOAT(L1);
1535 F2 = DECODE_FLOAT(L2);
1536 F3 = fmodf(F1, F2);
1537 F4 = (F1 - F3) / F2;
1538 L4 = ENCODE_FLOAT(F4);
1539 if ((L4 == 0) || (L4 == 0x80000000))
1540 L4 = (L1 ^ L2) & 0x80000000;
1541 S1 = ENCODE_FLOAT(F3);
1542 S2 = L4;
1543 NEXT;
1544
1545 do_sin:
1546 F1 = sinf(DECODE_FLOAT(L1));
1547 S1 = ENCODE_FLOAT(F1);
1548 NEXT;
1549
1550 do_cos:
1551 F1 = cosf(DECODE_FLOAT(L1));
1552 S1 = ENCODE_FLOAT(F1);
1553 NEXT;
1554
1555 do_tan:
1556 F1 = tanf(DECODE_FLOAT(L1));
1557 S1 = ENCODE_FLOAT(F1);
1558 NEXT;
1559
1560 do_asin:
1561 F1 = asinf(DECODE_FLOAT(L1));
1562 S1 = ENCODE_FLOAT(F1);
1563 NEXT;
1564
1565 do_acos:
1566 F1 = acosf(DECODE_FLOAT(L1));
1567 S1 = ENCODE_FLOAT(F1);
1568 NEXT;
1569
1570 do_atan:
1571 F1 = atanf(DECODE_FLOAT(L1));
1572 S1 = ENCODE_FLOAT(F1);
1573 NEXT;
1574
1575 // Double-precision (new with glulx spec 3.1.3)
1576
1577 do_numtod:
1578 D1 = (git_double) L1;
1579 ENCODE_DOUBLE(D1, &S2, &S1);
1580 NEXT;
1581
1582 do_dtonumz:
1583 D1 = DECODE_DOUBLE(L1, L2);
1584 if (!signbit(D1)) {
1585 if (isnan(D1) || isinf(D1) || (D1 > 2147483647.0))
1586 S1 = 0x7FFFFFFF;
1587 else
1588 S1 = (git_sint32) trunc(D1);
1589 } else {
1590 if (isnan(D1) || isinf(D1) || (D1 < -2147483647.0))
1591 S1 = 0x80000000;
1592 else
1593 S1 = (git_sint32) trunc(D1);
1594 }
1595 NEXT;
1596
1597 do_dtonumn:
1598 D1 = DECODE_DOUBLE(L1, L2);
1599 if (!signbit(D1)) {
1600 if (isnan(D1) || isinf(D1) || (D1 > 2147483647.0))
1601 S1 = 0x7FFFFFFF;
1602 else
1603 S1 = (git_sint32) round(D1);
1604 } else {
1605 if (isnan(D1) || isinf(D1) || (D1 < -2147483647.0))
1606 S1 = 0x80000000;
1607 else
1608 S1 = (git_sint32) round(D1);
1609 }
1610 NEXT;
1611
1612 do_ftod:
1613 D1 = (git_double) DECODE_FLOAT(L1);
1614 ENCODE_DOUBLE(D1, &S2, &S1);
1615 NEXT;
1616
1617 do_dtof:
1618 F1 = (git_float) DECODE_DOUBLE(L1, L2);
1619 S1 = ENCODE_FLOAT(F1);
1620 NEXT;
1621
1622 do_dadd:
1623 D1 = (DECODE_DOUBLE(L1, L2) + DECODE_DOUBLE(L3, L4));
1624 ENCODE_DOUBLE(D1, &S2, &S1);
1625 NEXT;
1626
1627 do_dsub:
1628 D1 = (DECODE_DOUBLE(L1, L2) - DECODE_DOUBLE(L3, L4));
1629 ENCODE_DOUBLE(D1, &S2, &S1);
1630 NEXT;
1631
1632 do_dmul:
1633 D1 = (DECODE_DOUBLE(L1, L2) * DECODE_DOUBLE(L3, L4));
1634 ENCODE_DOUBLE(D1, &S2, &S1);
1635 NEXT;
1636
1637 do_ddiv:
1638 D1 = (DECODE_DOUBLE(L1, L2) / DECODE_DOUBLE(L3, L4));
1639 ENCODE_DOUBLE(D1, &S2, &S1);
1640 NEXT;
1641
1642 do_dmodr:
1643 D1 = fmod(DECODE_DOUBLE(L1, L2), DECODE_DOUBLE(L3, L4));
1644 ENCODE_DOUBLE(D1, &S2, &S1);
1645 NEXT;
1646
1647 do_dmodq:
1648 D1 = DECODE_DOUBLE(L1, L2);
1649 D2 = DECODE_DOUBLE(L3, L4);
1650 D3 = fmod(D1, D2);
1651 D3 = (D1-D3) / D2;
1652 ENCODE_DOUBLE(D3, &L5, &L6);
1653 if ((L5 == 0) || (L5 == 0x80000000))
1654 L5 = (L1 ^ L3) & 0x80000000;
1655 S2 = L5;
1656 S1 = L6;
1657 NEXT;
1658
1659 do_dceil:
1660 D1 = ceil(DECODE_DOUBLE(L1, L2));
1661 ENCODE_DOUBLE(D1, &S2, &S1);
1662 NEXT;
1663
1664 do_dfloor:
1665 D1 = floor(DECODE_DOUBLE(L1, L2));
1666 ENCODE_DOUBLE(D1, &S2, &S1);
1667 NEXT;
1668
1669 do_dsqrt:
1670 D1 = sqrt(DECODE_DOUBLE(L1, L2));
1671 ENCODE_DOUBLE(D1, &S2, &S1);
1672 NEXT;
1673
1674 do_dexp:
1675 D1 = exp(DECODE_DOUBLE(L1, L2));
1676 ENCODE_DOUBLE(D1, &S2, &S1);
1677 NEXT;
1678
1679 do_dlog:
1680 D1 = log(DECODE_DOUBLE(L1, L2));
1681 ENCODE_DOUBLE(D1, &S2, &S1);
1682 NEXT;
1683
1684 do_dpow:
1685 D1 = pow(DECODE_DOUBLE(L1, L2), DECODE_DOUBLE(L3, L4));
1686 ENCODE_DOUBLE(D1, &S2, &S1);
1687 NEXT;
1688
1689 do_dsin:
1690 D1 = sin(DECODE_DOUBLE(L1, L2));
1691 ENCODE_DOUBLE(D1, &S2, &S1);
1692 NEXT;
1693
1694 do_dcos:
1695 D1 = cos(DECODE_DOUBLE(L1, L2));
1696 ENCODE_DOUBLE(D1, &S2, &S1);
1697 NEXT;
1698
1699 do_dtan:
1700 D1 = tan(DECODE_DOUBLE(L1, L2));
1701 ENCODE_DOUBLE(D1, &S2, &S1);
1702 NEXT;
1703
1704 do_dasin:
1705 D1 = asin(DECODE_DOUBLE(L1, L2));
1706 ENCODE_DOUBLE(D1, &S2, &S1);
1707 NEXT;
1708
1709 do_dacos:
1710 D1 = acos(DECODE_DOUBLE(L1, L2));
1711 ENCODE_DOUBLE(D1, &S2, &S1);
1712 NEXT;
1713
1714 do_datan:
1715 D1 = atan(DECODE_DOUBLE(L1, L2));
1716 ENCODE_DOUBLE(D1, &S2, &S1);
1717 NEXT;
1718
1719 do_datan2:
1720 D1 = atan2(DECODE_DOUBLE(L1, L2), DECODE_DOUBLE(L3, L4));
1721 ENCODE_DOUBLE(D1, &S2, &S1);
1722 NEXT;
1723
1724 // Extended undo (new with glulx spec 3.1.3)
1725
1726 do_hasundo:
1727 S1 = hasUndo();
1728 NEXT;
1729
1730 do_discardundo:
1731 discardUndo();
1732 NEXT;
1733
1734 // Special Git opcodes
1735
1736 do_git_setcacheram:
1737 gCacheRAM = (L1 == 0) ? 0 : 1;
1738 NEXT;
1739
1740 do_git_prunecache:
1741 pruneCodeCache (L1, L2);
1742 NEXT;
1743
1744 // Error conditions:
1745
1746 do_error_bad_opcode:
1747 fatalError ("Illegal instruction");
1748 goto finished;
1749
1750 stack_overflow:
1751 fatalError ("Stack overflow");
1752 goto finished;
1753
1754 stack_underflow:
1755 fatalError ("Stack underflow");
1756 goto finished;
1757
1758 // ---------------------------------
1759
1760 finished:
1761
1762 free (base);
1763 shutdownCompiler();
1764 }