Debian Codegraph @

terps/glulxe/exec.c

raw symbols SCIP
1 /* exec.c: Glulxe code for program execution. The main interpreter loop.
2 Designed by Andrew Plotkin <[email protected]>
3 http://eblong.com/zarf/glulx/index.html
4 */
5
6 #include "glk.h"
7 #include "glulxe.h"
8 #include "opcodes.h"
9
10 #ifdef FLOAT_SUPPORT
11 #include <math.h>
12
13 #ifdef DOUBLE_SUPPORT /* Inside FLOAT_SUPPORT! */
14 /* A couple of macros which test a pair of glui32 words as a double */
15 #define DOUBLE_PAIR_ISINF(vhi, vlo) (((vhi) == 0x7FF00000 || (vhi) == 0xFFF00000) && (vlo) == 0)
16 #define DOUBLE_PAIR_ISNAN(vhi, vlo) (((vhi) & 0x7FF00000) == 0x7FF00000 && (((vhi) & 0xFFFFF) != 0 || (vlo) != 0))
17 #endif /* DOUBLE_SUPPORT */
18
19 #endif /* FLOAT_SUPPORT */
20
21 /* execute_loop():
22 The main interpreter loop. This repeats until the program is done.
23 */
24 void execute_loop()
25 {
26 int done_executing = FALSE;
27 int ix;
28 glui32 opcode;
29 const operandlist_t *oplist;
30 oparg_t inst[MAX_OPERANDS];
31 glui32 value, addr, val0, val1;
32 glsi32 vals0, vals1;
33 glui32 *arglist;
34 glui32 arglistfix[3];
35
36 #ifdef FLOAT_SUPPORT
37 gfloat32 valf, valf1, valf2;
38 #ifdef DOUBLE_SUPPORT /* Inside FLOAT_SUPPORT! */
39 glui32 val0hi, val0lo, val1hi, val1lo;
40 gfloat64 vald, vald1, vald2;
41 #endif /* DOUBLE_SUPPORT */
42 #endif /* FLOAT_SUPPORT */
43
44 while (!done_executing) {
45
46 profile_tick();
47 debugger_tick();
48 /* Do OS-specific processing, if appropriate. */
49 glk_tick();
50
51 /* Stash the current opcode's address, in case the interpreter needs to serialize the VM state out-of-band. */
52 prevpc = pc;
53
54 /* Fetch the opcode number. */
55 opcode = Mem1(pc);
56 pc++;
57 if (opcode & 0x80) {
58 /* More than one-byte opcode. */
59 if (opcode & 0x40) {
60 /* Four-byte opcode */
61 opcode &= 0x3F;
62 opcode = (opcode << 8) | Mem1(pc);
63 pc++;
64 opcode = (opcode << 8) | Mem1(pc);
65 pc++;
66 opcode = (opcode << 8) | Mem1(pc);
67 pc++;
68 }
69 else {
70 /* Two-byte opcode */
71 opcode &= 0x7F;
72 opcode = (opcode << 8) | Mem1(pc);
73 pc++;
74 }
75 }
76
77 /* Now we have an opcode number. */
78
79 /* Fetch the structure that describes how the operands for this
80 opcode are arranged. This is a pointer to an immutable,
81 static object. */
82 if (opcode < 0x80)
83 oplist = fast_operandlist[opcode];
84 else
85 oplist = lookup_operandlist(opcode);
86
87 if (!oplist)
88 fatal_error_i("Encountered unknown opcode.", opcode);
89
90 /* Based on the oplist structure, load the actual operand values
91 into inst. This moves the PC up to the end of the instruction. */
92 parse_operands(inst, oplist);
93
94 /* Perform the opcode. This switch statement is split in two, based
95 on some paranoid suspicions about the ability of compilers to
96 optimize large-range switches. Ignore that. */
97
98 if (opcode < 0x80) {
99
100 switch (opcode) {
101
102 case op_nop:
103 break;
104
105 case op_add:
106 value = inst[0].value + inst[1].value;
107 store_operand(inst[2].desttype, inst[2].value, value);
108 break;
109 case op_sub:
110 value = inst[0].value - inst[1].value;
111 store_operand(inst[2].desttype, inst[2].value, value);
112 break;
113 case op_mul:
114 value = inst[0].value * inst[1].value;
115 store_operand(inst[2].desttype, inst[2].value, value);
116 break;
117 case op_div:
118 vals0 = inst[0].value;
119 vals1 = inst[1].value;
120 if (vals1 == 0)
121 fatal_error("Division by zero.");
122 /* Since C doesn't guarantee the results of division of negative
123 numbers, we carefully convert everything to positive values
124 first. They have to be unsigned values, too, otherwise the
125 0x80000000 case goes wonky. */
126 if (vals0 < 0) {
127 val0 = (-(glui32)vals0);
128 if (vals1 < 0) {
129 val1 = (-(glui32)vals1);
130 value = val0 / val1;
131 }
132 else {
133 val1 = vals1;
134 value = -(val0 / val1);
135 }
136 }
137 else {
138 val0 = vals0;
139 if (vals1 < 0) {
140 val1 = (-(glui32)vals1);
141 value = -(val0 / val1);
142 }
143 else {
144 val1 = vals1;
145 value = val0 / val1;
146 }
147 }
148 store_operand(inst[2].desttype, inst[2].value, value);
149 break;
150 case op_mod:
151 vals0 = inst[0].value;
152 vals1 = inst[1].value;
153 if (vals1 == 0)
154 fatal_error("Division by zero doing remainder.");
155 if (vals1 < 0) {
156 val1 = -(glui32)vals1;
157 }
158 else {
159 val1 = vals1;
160 }
161 if (vals0 < 0) {
162 val0 = (-(glui32)vals0);
163 value = -(val0 % val1);
164 }
165 else {
166 val0 = vals0;
167 value = val0 % val1;
168 }
169 store_operand(inst[2].desttype, inst[2].value, value);
170 break;
171 case op_neg:
172 vals0 = inst[0].value;
173 value = (-(glui32)vals0);
174 store_operand(inst[1].desttype, inst[1].value, value);
175 break;
176
177 case op_bitand:
178 value = (inst[0].value & inst[1].value);
179 store_operand(inst[2].desttype, inst[2].value, value);
180 break;
181 case op_bitor:
182 value = (inst[0].value | inst[1].value);
183 store_operand(inst[2].desttype, inst[2].value, value);
184 break;
185 case op_bitxor:
186 value = (inst[0].value ^ inst[1].value);
187 store_operand(inst[2].desttype, inst[2].value, value);
188 break;
189 case op_bitnot:
190 value = ~(inst[0].value);
191 store_operand(inst[1].desttype, inst[1].value, value);
192 break;
193
194 case op_shiftl:
195 vals0 = inst[1].value;
196 if (vals0 < 0 || vals0 >= 32)
197 value = 0;
198 else
199 value = ((glui32)(inst[0].value) << (glui32)vals0);
200 store_operand(inst[2].desttype, inst[2].value, value);
201 break;
202 case op_ushiftr:
203 vals0 = inst[1].value;
204 if (vals0 < 0 || vals0 >= 32)
205 value = 0;
206 else
207 value = ((glui32)(inst[0].value) >> (glui32)vals0);
208 store_operand(inst[2].desttype, inst[2].value, value);
209 break;
210 case op_sshiftr:
211 vals0 = inst[1].value;
212 if (vals0 < 0 || vals0 >= 32) {
213 if (inst[0].value & 0x80000000)
214 value = 0xFFFFFFFF;
215 else
216 value = 0;
217 }
218 else {
219 /* This is somewhat foolhardy -- C doesn't guarantee that
220 right-shifting a signed value replicates the sign bit.
221 We'll assume it for now. */
222 value = ((glsi32)(inst[0].value) >> (glsi32)vals0);
223 }
224 store_operand(inst[2].desttype, inst[2].value, value);
225 break;
226
227 case op_jump:
228 value = inst[0].value;
229 /* fall through to PerformJump label. */
230
231 PerformJump: /* goto label for successful jumping... ironic, no? */
232 if (value == 0 || value == 1) {
233 /* Return from function. This is exactly what happens in
234 return_op, but it's only a few lines of code, so I won't
235 bother with a "goto". */
236 leave_function();
237 if (stackptr == 0) {
238 done_executing = TRUE;
239 break;
240 }
241 pop_callstub(value); /* zero or one */
242 }
243 else {
244 /* Branch to a new PC value. */
245 pc = (pc + value - 2);
246 }
247 break;
248
249 case op_jz:
250 if (inst[0].value == 0) {
251 value = inst[1].value;
252 goto PerformJump;
253 }
254 break;
255 case op_jnz:
256 if (inst[0].value != 0) {
257 value = inst[1].value;
258 goto PerformJump;
259 }
260 break;
261 case op_jeq:
262 if (inst[0].value == inst[1].value) {
263 value = inst[2].value;
264 goto PerformJump;
265 }
266 break;
267 case op_jne:
268 if (inst[0].value != inst[1].value) {
269 value = inst[2].value;
270 goto PerformJump;
271 }
272 break;
273 case op_jlt:
274 vals0 = inst[0].value;
275 vals1 = inst[1].value;
276 if (vals0 < vals1) {
277 value = inst[2].value;
278 goto PerformJump;
279 }
280 break;
281 case op_jgt:
282 vals0 = inst[0].value;
283 vals1 = inst[1].value;
284 if (vals0 > vals1) {
285 value = inst[2].value;
286 goto PerformJump;
287 }
288 break;
289 case op_jle:
290 vals0 = inst[0].value;
291 vals1 = inst[1].value;
292 if (vals0 <= vals1) {
293 value = inst[2].value;
294 goto PerformJump;
295 }
296 break;
297 case op_jge:
298 vals0 = inst[0].value;
299 vals1 = inst[1].value;
300 if (vals0 >= vals1) {
301 value = inst[2].value;
302 goto PerformJump;
303 }
304 break;
305 case op_jltu:
306 val0 = inst[0].value;
307 val1 = inst[1].value;
308 if (val0 < val1) {
309 value = inst[2].value;
310 goto PerformJump;
311 }
312 break;
313 case op_jgtu:
314 val0 = inst[0].value;
315 val1 = inst[1].value;
316 if (val0 > val1) {
317 value = inst[2].value;
318 goto PerformJump;
319 }
320 break;
321 case op_jleu:
322 val0 = inst[0].value;
323 val1 = inst[1].value;
324 if (val0 <= val1) {
325 value = inst[2].value;
326 goto PerformJump;
327 }
328 break;
329 case op_jgeu:
330 val0 = inst[0].value;
331 val1 = inst[1].value;
332 if (val0 >= val1) {
333 value = inst[2].value;
334 goto PerformJump;
335 }
336 break;
337
338 case op_call:
339 value = inst[1].value;
340 arglist = pop_arguments(value, 0);
341 push_callstub(inst[2].desttype, inst[2].value);
342 enter_function(inst[0].value, value, arglist);
343 break;
344 case op_return:
345 leave_function();
346 if (stackptr == 0) {
347 done_executing = TRUE;
348 break;
349 }
350 pop_callstub(inst[0].value);
351 break;
352 case op_tailcall:
353 value = inst[1].value;
354 arglist = pop_arguments(value, 0);
355 leave_function();
356 enter_function(inst[0].value, value, arglist);
357 break;
358
359 case op_catch:
360 push_callstub(inst[0].desttype, inst[0].value);
361 value = inst[1].value;
362 val0 = stackptr;
363 store_operand(inst[0].desttype, inst[0].value, val0);
364 goto PerformJump;
365 break;
366 case op_throw:
367 profile_fail("throw");
368 value = inst[0].value;
369 stackptr = inst[1].value;
370 pop_callstub(value);
371 break;
372
373 case op_copy:
374 value = inst[0].value;
375 #ifdef TOLERATE_SUPERGLUS_BUG
376 if (inst[1].desttype == 1 && inst[1].value == 0)
377 inst[1].desttype = 0;
378 #endif /* TOLERATE_SUPERGLUS_BUG */
379 store_operand(inst[1].desttype, inst[1].value, value);
380 break;
381 case op_copys:
382 value = inst[0].value;
383 store_operand_s(inst[1].desttype, inst[1].value, value);
384 break;
385 case op_copyb:
386 value = inst[0].value;
387 store_operand_b(inst[1].desttype, inst[1].value, value);
388 break;
389
390 case op_sexs:
391 val0 = inst[0].value;
392 if (val0 & 0x8000)
393 val0 |= 0xFFFF0000;
394 else
395 val0 &= 0x0000FFFF;
396 store_operand(inst[1].desttype, inst[1].value, val0);
397 break;
398 case op_sexb:
399 val0 = inst[0].value;
400 if (val0 & 0x80)
401 val0 |= 0xFFFFFF00;
402 else
403 val0 &= 0x000000FF;
404 store_operand(inst[1].desttype, inst[1].value, val0);
405 break;
406
407 case op_aload:
408 value = inst[0].value;
409 value += 4 * inst[1].value;
410 val0 = Mem4(value);
411 store_operand(inst[2].desttype, inst[2].value, val0);
412 break;
413 case op_aloads:
414 value = inst[0].value;
415 value += 2 * inst[1].value;
416 val0 = Mem2(value);
417 store_operand(inst[2].desttype, inst[2].value, val0);
418 break;
419 case op_aloadb:
420 value = inst[0].value;
421 value += inst[1].value;
422 val0 = Mem1(value);
423 store_operand(inst[2].desttype, inst[2].value, val0);
424 break;
425 case op_aloadbit:
426 value = inst[0].value;
427 vals0 = inst[1].value;
428 val1 = (vals0 & 7);
429 if (vals0 >= 0)
430 value += (vals0 >> 3);
431 else
432 value -= (1 + ((-1 - vals0) >> 3));
433 if (Mem1(value) & (1 << val1))
434 val0 = 1;
435 else
436 val0 = 0;
437 store_operand(inst[2].desttype, inst[2].value, val0);
438 break;
439
440 case op_astore:
441 value = inst[0].value;
442 value += 4 * inst[1].value;
443 val0 = inst[2].value;
444 MemW4(value, val0);
445 break;
446 case op_astores:
447 value = inst[0].value;
448 value += 2 * inst[1].value;
449 val0 = inst[2].value;
450 MemW2(value, val0);
451 break;
452 case op_astoreb:
453 value = inst[0].value;
454 value += inst[1].value;
455 val0 = inst[2].value;
456 MemW1(value, val0);
457 break;
458 case op_astorebit:
459 value = inst[0].value;
460 vals0 = inst[1].value;
461 val1 = (vals0 & 7);
462 if (vals0 >= 0)
463 value += (vals0 >> 3);
464 else
465 value -= (1 + ((-1 - vals0) >> 3));
466 val0 = Mem1(value);
467 if (inst[2].value)
468 val0 |= (1 << val1);
469 else
470 val0 &= ~((glui32)(1 << val1));
471 MemW1(value, val0);
472 break;
473
474 case op_stkcount:
475 value = (stackptr - valstackbase) / 4;
476 store_operand(inst[0].desttype, inst[0].value, value);
477 break;
478 case op_stkpeek:
479 vals0 = inst[0].value * 4;
480 if (vals0 < 0 || vals0 >= (stackptr - valstackbase))
481 fatal_error("Stkpeek outside current stack range.");
482 value = Stk4(stackptr - (vals0+4));
483 store_operand(inst[1].desttype, inst[1].value, value);
484 break;
485 case op_stkswap:
486 if (stackptr < valstackbase+8) {
487 fatal_error("Stack underflow in stkswap.");
488 }
489 val0 = Stk4(stackptr-4);
490 val1 = Stk4(stackptr-8);
491 StkW4(stackptr-4, val1);
492 StkW4(stackptr-8, val0);
493 break;
494 case op_stkcopy:
495 vals0 = inst[0].value;
496 if (vals0 < 0)
497 fatal_error("Negative operand in stkcopy.");
498 if (vals0 == 0)
499 break;
500 if (stackptr < valstackbase+vals0*4)
501 fatal_error("Stack underflow in stkcopy.");
502 if (stackptr + vals0*4 > stacksize)
503 fatal_error("Stack overflow in stkcopy.");
504 addr = stackptr - vals0*4;
505 for (ix=0; ix<vals0; ix++) {
506 value = Stk4(addr + ix*4);
507 StkW4(stackptr + ix*4, value);
508 }
509 stackptr += vals0*4;
510 break;
511 case op_stkroll:
512 vals0 = inst[0].value;
513 vals1 = inst[1].value;
514 if (vals0 < 0)
515 fatal_error("Negative operand in stkroll.");
516 if (stackptr < valstackbase+vals0*4)
517 fatal_error("Stack underflow in stkroll.");
518 if (vals0 == 0)
519 break;
520 /* The following is a bit ugly. We want to do vals1 = vals0-vals1,
521 because rolling down is sort of easier than rolling up. But
522 we also want to take the result mod vals0. The % operator is
523 annoying for negative numbers, so we need to do this in two
524 cases. */
525 if (vals1 > 0) {
526 vals1 = vals1 % vals0;
527 vals1 = (vals0) - vals1;
528 }
529 else {
530 vals1 = (-(glui32)vals1) % vals0;
531 }
532 if (vals1 == 0)
533 break;
534 addr = stackptr - vals0*4;
535 for (ix=0; ix<vals1; ix++) {
536 value = Stk4(addr + ix*4);
537 StkW4(stackptr + ix*4, value);
538 }
539 for (ix=0; ix<vals0; ix++) {
540 value = Stk4(addr + (vals1+ix)*4);
541 StkW4(addr + ix*4, value);
542 }
543 break;
544
545 case op_streamchar:
546 profile_in(0xE0000001, stackptr, FALSE);
547 value = inst[0].value & 0xFF;
548 (*stream_char_handler)(value);
549 profile_out(stackptr);
550 break;
551 case op_streamunichar:
552 profile_in(0xE0000002, stackptr, FALSE);
553 value = inst[0].value;
554 (*stream_unichar_handler)(value);
555 profile_out(stackptr);
556 break;
557 case op_streamnum:
558 profile_in(0xE0000003, stackptr, FALSE);
559 vals0 = inst[0].value;
560 stream_num(vals0, FALSE, 0);
561 profile_out(stackptr);
562 break;
563 case op_streamstr:
564 profile_in(0xE0000004, stackptr, FALSE);
565 stream_string(inst[0].value, 0, 0);
566 profile_out(stackptr);
567 break;
568
569 default:
570 fatal_error_i("Executed unknown opcode.", opcode);
571 }
572 }
573 else {
574
575 switch (opcode) {
576
577 case op_gestalt:
578 value = do_gestalt(inst[0].value, inst[1].value);
579 store_operand(inst[2].desttype, inst[2].value, value);
580 break;
581
582 case op_debugtrap:
583 #if VM_DEBUGGER
584 /* We block and handle debug commands, but only if the
585 library has invoked debug features. (Meaning, has
586 the cycle handler ever been called.) */
587 if (debugger_ever_invoked()) {
588 debugger_block_and_debug("user debugtrap, pausing...");
589 break;
590 }
591 #endif /* VM_DEBUGGER */
592 fatal_error_i("user debugtrap encountered.", inst[0].value);
593
594 case op_jumpabs:
595 pc = inst[0].value;
596 break;
597
598 case op_callf:
599 push_callstub(inst[1].desttype, inst[1].value);
600 enter_function(inst[0].value, 0, arglistfix);
601 break;
602 case op_callfi:
603 arglistfix[0] = inst[1].value;
604 push_callstub(inst[2].desttype, inst[2].value);
605 enter_function(inst[0].value, 1, arglistfix);
606 break;
607 case op_callfii:
608 arglistfix[0] = inst[1].value;
609 arglistfix[1] = inst[2].value;
610 push_callstub(inst[3].desttype, inst[3].value);
611 enter_function(inst[0].value, 2, arglistfix);
612 break;
613 case op_callfiii:
614 arglistfix[0] = inst[1].value;
615 arglistfix[1] = inst[2].value;
616 arglistfix[2] = inst[3].value;
617 push_callstub(inst[4].desttype, inst[4].value);
618 enter_function(inst[0].value, 3, arglistfix);
619 break;
620
621 case op_getmemsize:
622 store_operand(inst[0].desttype, inst[0].value, endmem);
623 break;
624 case op_setmemsize:
625 value = change_memsize(inst[0].value, FALSE);
626 store_operand(inst[1].desttype, inst[1].value, value);
627 break;
628
629 case op_getstringtbl:
630 value = stream_get_table();
631 store_operand(inst[0].desttype, inst[0].value, value);
632 break;
633 case op_setstringtbl:
634 stream_set_table(inst[0].value);
635 break;
636
637 case op_getiosys:
638 stream_get_iosys(&val0, &val1);
639 store_operand(inst[0].desttype, inst[0].value, val0);
640 store_operand(inst[1].desttype, inst[1].value, val1);
641 break;
642 case op_setiosys:
643 stream_set_iosys(inst[0].value, inst[1].value);
644 break;
645
646 case op_glk:
647 profile_in(0xF0000000+inst[0].value, stackptr, FALSE);
648 value = inst[1].value;
649 arglist = pop_arguments(value, 0);
650 val0 = perform_glk(inst[0].value, value, arglist);
651 #ifdef TOLERATE_SUPERGLUS_BUG
652 if (inst[2].desttype == 1 && inst[2].value == 0)
653 inst[2].desttype = 0;
654 #endif /* TOLERATE_SUPERGLUS_BUG */
655 store_operand(inst[2].desttype, inst[2].value, val0);
656 profile_out(stackptr);
657 break;
658
659 case op_random:
660 vals0 = inst[0].value;
661 if (vals0 == 0)
662 value = glulx_random();
663 else if (vals0 >= 1)
664 value = glulx_random() % (glui32)(vals0);
665 else
666 value = -(glulx_random() % (glui32)(-(glui32)vals0));
667 store_operand(inst[1].desttype, inst[1].value, value);
668 break;
669 case op_setrandom:
670 glulx_setrandom(inst[0].value);
671 break;
672
673 case op_verify:
674 value = perform_verify();
675 store_operand(inst[0].desttype, inst[0].value, value);
676 break;
677
678 case op_restart:
679 profile_fail("restart");
680 vm_restart();
681 break;
682
683 case op_protect:
684 val0 = inst[0].value;
685 val1 = val0 + inst[1].value;
686 if (val0 == val1) {
687 val0 = 0;
688 val1 = 0;
689 }
690 protectstart = val0;
691 protectend = val1;
692 break;
693
694 case op_save:
695 push_callstub(inst[1].desttype, inst[1].value);
696 value = perform_save(find_stream_by_id(inst[0].value));
697 pop_callstub(value);
698 break;
699
700 case op_restore:
701 value = perform_restore(find_stream_by_id(inst[0].value), FALSE);
702 if (value == 0) {
703 /* We've succeeded, and the stack now contains the callstub
704 saved during saveundo. Ignore this opcode's operand. */
705 value = -1;
706 pop_callstub(value);
707 }
708 else {
709 /* We've failed, so we must store the failure in this opcode's
710 operand. */
711 store_operand(inst[1].desttype, inst[1].value, value);
712 }
713 break;
714
715 case op_saveundo:
716 push_callstub(inst[0].desttype, inst[0].value);
717 value = perform_saveundo();
718 pop_callstub(value);
719 break;
720
721 case op_restoreundo:
722 value = perform_restoreundo();
723 if (value == 0) {
724 /* We've succeeded, and the stack now contains the callstub
725 saved during saveundo. Ignore this opcode's operand. */
726 value = -1;
727 pop_callstub(value);
728 }
729 else {
730 /* We've failed, so we must store the failure in this opcode's
731 operand. */
732 store_operand(inst[0].desttype, inst[0].value, value);
733 }
734 break;
735
736 case op_hasundo:
737 value = has_undo();
738 store_operand(inst[0].desttype, inst[0].value, value);
739 break;
740
741 case op_discardundo:
742 discard_undo();
743 break;
744
745 case op_quit:
746 done_executing = TRUE;
747 break;
748
749 case op_linearsearch:
750 value = linear_search(inst[0].value, inst[1].value, inst[2].value,
751 inst[3].value, inst[4].value, inst[5].value, inst[6].value);
752 store_operand(inst[7].desttype, inst[7].value, value);
753 break;
754 case op_binarysearch:
755 value = binary_search(inst[0].value, inst[1].value, inst[2].value,
756 inst[3].value, inst[4].value, inst[5].value, inst[6].value);
757 store_operand(inst[7].desttype, inst[7].value, value);
758 break;
759 case op_linkedsearch:
760 value = linked_search(inst[0].value, inst[1].value, inst[2].value,
761 inst[3].value, inst[4].value, inst[5].value);
762 store_operand(inst[6].desttype, inst[6].value, value);
763 break;
764
765 case op_mzero: {
766 glui32 lx;
767 glui32 count = inst[0].value;
768 addr = inst[1].value;
769 for (lx=0; lx<count; lx++, addr++) {
770 MemW1(addr, 0);
771 }
772 }
773 break;
774 case op_mcopy: {
775 glui32 lx;
776 glui32 count = inst[0].value;
777 glui32 addrsrc = inst[1].value;
778 glui32 addrdest = inst[2].value;
779 if (addrdest < addrsrc) {
780 for (lx=0; lx<count; lx++, addrsrc++, addrdest++) {
781 value = Mem1(addrsrc);
782 MemW1(addrdest, value);
783 }
784 }
785 else {
786 addrsrc += (count-1);
787 addrdest += (count-1);
788 for (lx=0; lx<count; lx++, addrsrc--, addrdest--) {
789 value = Mem1(addrsrc);
790 MemW1(addrdest, value);
791 }
792 }
793 }
794 break;
795 case op_malloc:
796 value = heap_alloc(inst[0].value);
797 store_operand(inst[1].desttype, inst[1].value, value);
798 break;
799 case op_mfree:
800 heap_free(inst[0].value);
801 break;
802
803 case op_accelfunc:
804 accel_set_func(inst[0].value, inst[1].value);
805 break;
806 case op_accelparam:
807 accel_set_param(inst[0].value, inst[1].value);
808 break;
809
810 #ifdef FLOAT_SUPPORT
811
812 case op_numtof:
813 vals0 = inst[0].value;
814 value = encode_float((gfloat32)vals0);
815 store_operand(inst[1].desttype, inst[1].value, value);
816 break;
817 case op_ftonumz:
818 valf = decode_float(inst[0].value);
819 if (!signbit(valf)) {
820 if (isnan(valf) || isinf(valf) || (valf > 2147483647.0))
821 vals0 = 0x7FFFFFFF;
822 else
823 vals0 = (glsi32)(truncf(valf));
824 }
825 else {
826 if (isnan(valf) || isinf(valf) || (valf < -2147483647.0))
827 vals0 = 0x80000000;
828 else
829 vals0 = (glsi32)(truncf(valf));
830 }
831 store_operand(inst[1].desttype, inst[1].value, vals0);
832 break;
833 case op_ftonumn:
834 valf = decode_float(inst[0].value);
835 if (!signbit(valf)) {
836 if (isnan(valf) || isinf(valf) || (valf > 2147483647.0))
837 vals0 = 0x7FFFFFFF;
838 else
839 vals0 = (glsi32)(roundf(valf));
840 }
841 else {
842 if (isnan(valf) || isinf(valf) || (valf < -2147483647.0))
843 vals0 = 0x80000000;
844 else
845 vals0 = (glsi32)(roundf(valf));
846 }
847 store_operand(inst[1].desttype, inst[1].value, vals0);
848 break;
849
850 case op_fadd:
851 valf1 = decode_float(inst[0].value);
852 valf2 = decode_float(inst[1].value);
853 value = encode_float(valf1 + valf2);
854 store_operand(inst[2].desttype, inst[2].value, value);
855 break;
856 case op_fsub:
857 valf1 = decode_float(inst[0].value);
858 valf2 = decode_float(inst[1].value);
859 value = encode_float(valf1 - valf2);
860 store_operand(inst[2].desttype, inst[2].value, value);
861 break;
862 case op_fmul:
863 valf1 = decode_float(inst[0].value);
864 valf2 = decode_float(inst[1].value);
865 value = encode_float(valf1 * valf2);
866 store_operand(inst[2].desttype, inst[2].value, value);
867 break;
868 case op_fdiv:
869 valf1 = decode_float(inst[0].value);
870 valf2 = decode_float(inst[1].value);
871 value = encode_float(valf1 / valf2);
872 store_operand(inst[2].desttype, inst[2].value, value);
873 break;
874
875 case op_fmod:
876 valf1 = decode_float(inst[0].value);
877 valf2 = decode_float(inst[1].value);
878 valf = fmodf(valf1, valf2);
879 val0 = encode_float(valf);
880 val1 = encode_float((valf1-valf) / valf2);
881 if (val1 == 0x0 || val1 == 0x80000000) {
882 /* When the quotient is zero, the sign has been lost in the
883 shuffle. We'll set that by hand, based on the original
884 arguments. */
885 val1 = (inst[0].value ^ inst[1].value) & 0x80000000;
886 }
887 store_operand(inst[2].desttype, inst[2].value, val0);
888 store_operand(inst[3].desttype, inst[3].value, val1);
889 break;
890
891 case op_floor:
892 valf = decode_float(inst[0].value);
893 value = encode_float(floorf(valf));
894 store_operand(inst[1].desttype, inst[1].value, value);
895 break;
896 case op_ceil:
897 valf = decode_float(inst[0].value);
898 value = encode_float(ceilf(valf));
899 if (value == 0x0 || value == 0x80000000) {
900 /* When the result is zero, the sign may have been lost in the
901 shuffle. (This is a bug in some C libraries.) We'll set the
902 sign by hand, based on the original argument. */
903 value = inst[0].value & 0x80000000;
904 }
905 store_operand(inst[1].desttype, inst[1].value, value);
906 break;
907
908 case op_sqrt:
909 valf = decode_float(inst[0].value);
910 value = encode_float(sqrtf(valf));
911 store_operand(inst[1].desttype, inst[1].value, value);
912 break;
913 case op_log:
914 valf = decode_float(inst[0].value);
915 value = encode_float(logf(valf));
916 store_operand(inst[1].desttype, inst[1].value, value);
917 break;
918 case op_exp:
919 valf = decode_float(inst[0].value);
920 value = encode_float(expf(valf));
921 store_operand(inst[1].desttype, inst[1].value, value);
922 break;
923 case op_pow:
924 valf1 = decode_float(inst[0].value);
925 valf2 = decode_float(inst[1].value);
926 value = encode_float(glulx_powf(valf1, valf2));
927 store_operand(inst[2].desttype, inst[2].value, value);
928 break;
929
930 case op_sin:
931 valf = decode_float(inst[0].value);
932 value = encode_float(sinf(valf));
933 store_operand(inst[1].desttype, inst[1].value, value);
934 break;
935 case op_cos:
936 valf = decode_float(inst[0].value);
937 value = encode_float(cosf(valf));
938 store_operand(inst[1].desttype, inst[1].value, value);
939 break;
940 case op_tan:
941 valf = decode_float(inst[0].value);
942 value = encode_float(tanf(valf));
943 store_operand(inst[1].desttype, inst[1].value, value);
944 break;
945 case op_asin:
946 valf = decode_float(inst[0].value);
947 value = encode_float(asinf(valf));
948 store_operand(inst[1].desttype, inst[1].value, value);
949 break;
950 case op_acos:
951 valf = decode_float(inst[0].value);
952 value = encode_float(acosf(valf));
953 store_operand(inst[1].desttype, inst[1].value, value);
954 break;
955 case op_atan:
956 valf = decode_float(inst[0].value);
957 value = encode_float(atanf(valf));
958 store_operand(inst[1].desttype, inst[1].value, value);
959 break;
960 case op_atan2:
961 valf1 = decode_float(inst[0].value);
962 valf2 = decode_float(inst[1].value);
963 value = encode_float(atan2f(valf1, valf2));
964 store_operand(inst[2].desttype, inst[2].value, value);
965 break;
966
967 case op_jisinf:
968 /* Infinity is well-defined, so we don't bother to convert to
969 float. */
970 val0 = inst[0].value;
971 if (val0 == 0x7F800000 || val0 == 0xFF800000) {
972 value = inst[1].value;
973 goto PerformJump;
974 }
975 break;
976 case op_jisnan:
977 /* NaN is well-defined, so we don't bother to convert to
978 float. */
979 val0 = inst[0].value;
980 if ((val0 & 0x7F800000) == 0x7F800000 && (val0 & 0x007FFFFF) != 0) {
981 value = inst[1].value;
982 goto PerformJump;
983 }
984 break;
985
986 case op_jfeq:
987 if ((inst[2].value & 0x7F800000) == 0x7F800000 && (inst[2].value & 0x007FFFFF) != 0) {
988 /* The delta is NaN, which can never match. */
989 val0 = 0;
990 }
991 else if ((inst[0].value == 0x7F800000 || inst[0].value == 0xFF800000)
992 && (inst[1].value == 0x7F800000 || inst[1].value == 0xFF800000)) {
993 /* Both are infinite. Opposite infinities are never equal,
994 even if the difference is infinite, so this is easy. */
995 val0 = (inst[0].value == inst[1].value);
996 }
997 else {
998 valf1 = decode_float(inst[1].value) - decode_float(inst[0].value);
999 valf2 = fabsf(decode_float(inst[2].value));
1000 val0 = (valf1 <= valf2 && valf1 >= -valf2);
1001 }
1002 if (val0) {
1003 value = inst[3].value;
1004 goto PerformJump;
1005 }
1006 break;
1007 case op_jfne:
1008 if ((inst[2].value & 0x7F800000) == 0x7F800000 && (inst[2].value & 0x007FFFFF) != 0) {
1009 /* The delta is NaN, which can never match. */
1010 val0 = 0;
1011 }
1012 else if ((inst[0].value == 0x7F800000 || inst[0].value == 0xFF800000)
1013 && (inst[1].value == 0x7F800000 || inst[1].value == 0xFF800000)) {
1014 /* Both are infinite. Opposite infinities are never equal,
1015 even if the difference is infinite, so this is easy. */
1016 val0 = (inst[0].value == inst[1].value);
1017 }
1018 else {
1019 valf1 = decode_float(inst[1].value) - decode_float(inst[0].value);
1020 valf2 = fabsf(decode_float(inst[2].value));
1021 val0 = (valf1 <= valf2 && valf1 >= -valf2);
1022 }
1023 if (!val0) {
1024 value = inst[3].value;
1025 goto PerformJump;
1026 }
1027 break;
1028
1029 case op_jflt:
1030 valf1 = decode_float(inst[0].value);
1031 valf2 = decode_float(inst[1].value);
1032 if (valf1 < valf2) {
1033 value = inst[2].value;
1034 goto PerformJump;
1035 }
1036 break;
1037 case op_jfgt:
1038 valf1 = decode_float(inst[0].value);
1039 valf2 = decode_float(inst[1].value);
1040 if (valf1 > valf2) {
1041 value = inst[2].value;
1042 goto PerformJump;
1043 }
1044 break;
1045 case op_jfle:
1046 valf1 = decode_float(inst[0].value);
1047 valf2 = decode_float(inst[1].value);
1048 if (valf1 <= valf2) {
1049 value = inst[2].value;
1050 goto PerformJump;
1051 }
1052 break;
1053 case op_jfge:
1054 valf1 = decode_float(inst[0].value);
1055 valf2 = decode_float(inst[1].value);
1056 if (valf1 >= valf2) {
1057 value = inst[2].value;
1058 goto PerformJump;
1059 }
1060 break;
1061
1062 #ifdef DOUBLE_SUPPORT /* Inside FLOAT_SUPPORT! */
1063
1064 case op_numtod:
1065 vals0 = inst[0].value;
1066 encode_double((gfloat64)vals0, &val0hi, &val0lo);
1067 store_operand(inst[1].desttype, inst[1].value, val0lo);
1068 store_operand(inst[2].desttype, inst[2].value, val0hi);
1069 break;
1070 case op_dtonumz:
1071 vald = decode_double(inst[0].value, inst[1].value);
1072 if (!signbit(vald)) {
1073 if (isnan(vald) || isinf(vald) || (vald > 2147483647.0))
1074 vals0 = 0x7FFFFFFF;
1075 else
1076 vals0 = (glsi32)(trunc(vald));
1077 }
1078 else {
1079 if (isnan(vald) || isinf(vald) || (vald < -2147483647.0))
1080 vals0 = 0x80000000;
1081 else
1082 vals0 = (glsi32)(trunc(vald));
1083 }
1084 store_operand(inst[2].desttype, inst[2].value, vals0);
1085 break;
1086 case op_dtonumn:
1087 vald = decode_double(inst[0].value, inst[1].value);
1088 if (!signbit(vald)) {
1089 if (isnan(vald) || isinf(vald) || (vald > 2147483647.0))
1090 vals0 = 0x7FFFFFFF;
1091 else
1092 vals0 = (glsi32)(round(vald));
1093 }
1094 else {
1095 if (isnan(vald) || isinf(vald) || (vald < -2147483647.0))
1096 vals0 = 0x80000000;
1097 else
1098 vals0 = (glsi32)(round(vald));
1099 }
1100 store_operand(inst[2].desttype, inst[2].value, vals0);
1101 break;
1102 case op_ftod:
1103 valf = decode_float(inst[0].value);
1104 encode_double((gfloat64)valf, &val0hi, &val0lo);
1105 store_operand(inst[1].desttype, inst[1].value, val0lo);
1106 store_operand(inst[2].desttype, inst[2].value, val0hi);
1107 break;
1108 case op_dtof:
1109 vald = decode_double(inst[0].value, inst[1].value);
1110 value = encode_float((gfloat32)vald);
1111 store_operand(inst[2].desttype, inst[2].value, value);
1112 break;
1113
1114 case op_dadd:
1115 vald1 = decode_double(inst[0].value, inst[1].value);
1116 vald2 = decode_double(inst[2].value, inst[3].value);
1117 encode_double(vald1 + vald2, &val0hi, &val0lo);
1118 store_operand(inst[4].desttype, inst[4].value, val0lo);
1119 store_operand(inst[5].desttype, inst[5].value, val0hi);
1120 break;
1121 case op_dsub:
1122 vald1 = decode_double(inst[0].value, inst[1].value);
1123 vald2 = decode_double(inst[2].value, inst[3].value);
1124 encode_double(vald1 - vald2, &val0hi, &val0lo);
1125 store_operand(inst[4].desttype, inst[4].value, val0lo);
1126 store_operand(inst[5].desttype, inst[5].value, val0hi);
1127 break;
1128 case op_dmul:
1129 vald1 = decode_double(inst[0].value, inst[1].value);
1130 vald2 = decode_double(inst[2].value, inst[3].value);
1131 encode_double(vald1 * vald2, &val0hi, &val0lo);
1132 store_operand(inst[4].desttype, inst[4].value, val0lo);
1133 store_operand(inst[5].desttype, inst[5].value, val0hi);
1134 break;
1135 case op_ddiv:
1136 vald1 = decode_double(inst[0].value, inst[1].value);
1137 vald2 = decode_double(inst[2].value, inst[3].value);
1138 encode_double(vald1 / vald2, &val0hi, &val0lo);
1139 store_operand(inst[4].desttype, inst[4].value, val0lo);
1140 store_operand(inst[5].desttype, inst[5].value, val0hi);
1141 break;
1142
1143 case op_dmodr:
1144 vald1 = decode_double(inst[0].value, inst[1].value);
1145 vald2 = decode_double(inst[2].value, inst[3].value);
1146 vald = fmod(vald1, vald2);
1147 encode_double(vald, &val0hi, &val0lo);
1148 store_operand(inst[4].desttype, inst[4].value, val0lo);
1149 store_operand(inst[5].desttype, inst[5].value, val0hi);
1150 break;
1151 case op_dmodq:
1152 vald1 = decode_double(inst[0].value, inst[1].value);
1153 vald2 = decode_double(inst[2].value, inst[3].value);
1154 vald = fmod(vald1, vald2);
1155 vald = (vald1-vald) / vald2;
1156 encode_double(vald, &val0hi, &val0lo);
1157 if ((val0hi == 0x0 || val0hi == 0x80000000) && val0lo == 0x0) {
1158 /* When the quotient is zero, the sign has been lost in the
1159 shuffle. We'll set that by hand, based on the original
1160 arguments. */
1161 val0hi = (inst[0].value ^ inst[2].value) & 0x80000000;
1162 }
1163 store_operand(inst[4].desttype, inst[4].value, val0lo);
1164 store_operand(inst[5].desttype, inst[5].value, val0hi);
1165 break;
1166
1167 case op_dfloor:
1168 vald = decode_double(inst[0].value, inst[1].value);
1169 encode_double(floor(vald), &val0hi, &val0lo);
1170 store_operand(inst[2].desttype, inst[2].value, val0lo);
1171 store_operand(inst[3].desttype, inst[3].value, val0hi);
1172 break;
1173 case op_dceil:
1174 vald = decode_double(inst[0].value, inst[1].value);
1175 encode_double(ceil(vald), &val0hi, &val0lo);
1176 store_operand(inst[2].desttype, inst[2].value, val0lo);
1177 store_operand(inst[3].desttype, inst[3].value, val0hi);
1178 break;
1179
1180 case op_dsqrt:
1181 vald = decode_double(inst[0].value, inst[1].value);
1182 encode_double(sqrt(vald), &val0hi, &val0lo);
1183 store_operand(inst[2].desttype, inst[2].value, val0lo);
1184 store_operand(inst[3].desttype, inst[3].value, val0hi);
1185 break;
1186 case op_dlog:
1187 vald = decode_double(inst[0].value, inst[1].value);
1188 encode_double(log(vald), &val0hi, &val0lo);
1189 store_operand(inst[2].desttype, inst[2].value, val0lo);
1190 store_operand(inst[3].desttype, inst[3].value, val0hi);
1191 break;
1192 case op_dexp:
1193 vald = decode_double(inst[0].value, inst[1].value);
1194 encode_double(exp(vald), &val0hi, &val0lo);
1195 store_operand(inst[2].desttype, inst[2].value, val0lo);
1196 store_operand(inst[3].desttype, inst[3].value, val0hi);
1197 break;
1198 case op_dpow:
1199 vald1 = decode_double(inst[0].value, inst[1].value);
1200 vald2 = decode_double(inst[2].value, inst[3].value);
1201 encode_double(glulx_pow(vald1, vald2), &val0hi, &val0lo);
1202 store_operand(inst[4].desttype, inst[4].value, val0lo);
1203 store_operand(inst[5].desttype, inst[5].value, val0hi);
1204 break;
1205
1206 case op_dsin:
1207 vald = decode_double(inst[0].value, inst[1].value);
1208 encode_double(sin(vald), &val0hi, &val0lo);
1209 store_operand(inst[2].desttype, inst[2].value, val0lo);
1210 store_operand(inst[3].desttype, inst[3].value, val0hi);
1211 break;
1212 case op_dcos:
1213 vald = decode_double(inst[0].value, inst[1].value);
1214 encode_double(cos(vald), &val0hi, &val0lo);
1215 store_operand(inst[2].desttype, inst[2].value, val0lo);
1216 store_operand(inst[3].desttype, inst[3].value, val0hi);
1217 break;
1218 case op_dtan:
1219 vald = decode_double(inst[0].value, inst[1].value);
1220 encode_double(tan(vald), &val0hi, &val0lo);
1221 store_operand(inst[2].desttype, inst[2].value, val0lo);
1222 store_operand(inst[3].desttype, inst[3].value, val0hi);
1223 break;
1224 case op_dasin:
1225 vald = decode_double(inst[0].value, inst[1].value);
1226 encode_double(asin(vald), &val0hi, &val0lo);
1227 store_operand(inst[2].desttype, inst[2].value, val0lo);
1228 store_operand(inst[3].desttype, inst[3].value, val0hi);
1229 break;
1230 case op_dacos:
1231 vald = decode_double(inst[0].value, inst[1].value);
1232 encode_double(acos(vald), &val0hi, &val0lo);
1233 store_operand(inst[2].desttype, inst[2].value, val0lo);
1234 store_operand(inst[3].desttype, inst[3].value, val0hi);
1235 break;
1236 case op_datan:
1237 vald = decode_double(inst[0].value, inst[1].value);
1238 encode_double(atan(vald), &val0hi, &val0lo);
1239 store_operand(inst[2].desttype, inst[2].value, val0lo);
1240 store_operand(inst[3].desttype, inst[3].value, val0hi);
1241 break;
1242 case op_datan2:
1243 vald1 = decode_double(inst[0].value, inst[1].value);
1244 vald2 = decode_double(inst[2].value, inst[3].value);
1245 vald = atan2(vald1, vald2);
1246 encode_double(vald, &val0hi, &val0lo);
1247 store_operand(inst[4].desttype, inst[4].value, val0lo);
1248 store_operand(inst[5].desttype, inst[5].value, val0hi);
1249 break;
1250
1251 case op_jdisinf:
1252 /* Infinity is well-defined, so we don't bother to convert to
1253 float. */
1254 val0 = inst[0].value;
1255 val1 = inst[1].value;
1256 if (DOUBLE_PAIR_ISINF(val0, val1)) {
1257 value = inst[2].value;
1258 goto PerformJump;
1259 }
1260 break;
1261 case op_jdisnan:
1262 /* NaN is well-defined, so we don't bother to convert to
1263 float. */
1264 val0 = inst[0].value;
1265 val1 = inst[1].value;
1266 if (DOUBLE_PAIR_ISNAN(val0, val1)) {
1267 value = inst[2].value;
1268 goto PerformJump;
1269 }
1270 break;
1271
1272 case op_jdeq:
1273 if (DOUBLE_PAIR_ISNAN(inst[4].value, inst[5].value)) {
1274 /* The delta is NaN, which can never match. */
1275 val0 = 0;
1276 }
1277 else if (DOUBLE_PAIR_ISINF(inst[0].value, inst[1].value)
1278 && DOUBLE_PAIR_ISINF(inst[2].value, inst[3].value)) {
1279 /* Both are infinite. Opposite infinities are never equal,
1280 even if the difference is infinite, so this is easy.
1281 (We only need to compare the high words, because the low
1282 word of both INF and -INF is zero.) */
1283 val0 = (inst[0].value == inst[2].value);
1284 }
1285 else {
1286 vald1 = decode_double(inst[2].value, inst[3].value) - decode_double(inst[0].value, inst[1].value);
1287 vald2 = fabs(decode_double(inst[4].value, inst[5].value));
1288 val0 = (vald1 <= vald2 && vald1 >= -vald2);
1289 }
1290 if (val0) {
1291 value = inst[6].value;
1292 goto PerformJump;
1293 }
1294 break;
1295 case op_jdne:
1296 if (DOUBLE_PAIR_ISNAN(inst[4].value, inst[5].value)) {
1297 /* The delta is NaN, which can never match. */
1298 val0 = 0;
1299 }
1300 else if (DOUBLE_PAIR_ISINF(inst[0].value, inst[1].value)
1301 && DOUBLE_PAIR_ISINF(inst[2].value, inst[3].value)) {
1302 /* Both are infinite. Opposite infinities are never equal,
1303 even if the difference is infinite, so this is easy.
1304 (We only need to compare the high words, because the low
1305 word of both INF and -INF is zero.) */
1306 val0 = (inst[0].value == inst[2].value);
1307 }
1308 else {
1309 vald1 = decode_double(inst[2].value, inst[3].value) - decode_double(inst[0].value, inst[1].value);
1310 vald2 = fabs(decode_double(inst[4].value, inst[5].value));
1311 val0 = (vald1 <= vald2 && vald1 >= -vald2);
1312 }
1313 if (!val0) {
1314 value = inst[6].value;
1315 goto PerformJump;
1316 }
1317 break;
1318
1319 case op_jdlt:
1320 vald1 = decode_double(inst[0].value, inst[1].value);
1321 vald2 = decode_double(inst[2].value, inst[3].value);
1322 if (vald1 < vald2) {
1323 value = inst[4].value;
1324 goto PerformJump;
1325 }
1326 break;
1327 case op_jdgt:
1328 vald1 = decode_double(inst[0].value, inst[1].value);
1329 vald2 = decode_double(inst[2].value, inst[3].value);
1330 if (vald1 > vald2) {
1331 value = inst[4].value;
1332 goto PerformJump;
1333 }
1334 break;
1335 case op_jdle:
1336 vald1 = decode_double(inst[0].value, inst[1].value);
1337 vald2 = decode_double(inst[2].value, inst[3].value);
1338 if (vald1 <= vald2) {
1339 value = inst[4].value;
1340 goto PerformJump;
1341 }
1342 break;
1343 case op_jdge:
1344 vald1 = decode_double(inst[0].value, inst[1].value);
1345 vald2 = decode_double(inst[2].value, inst[3].value);
1346 if (vald1 >= vald2) {
1347 value = inst[4].value;
1348 goto PerformJump;
1349 }
1350 break;
1351
1352 #endif /* DOUBLE_SUPPORT */
1353
1354 #endif /* FLOAT_SUPPORT */
1355
1356 #ifdef GLULX_EXTEND_OPCODES
1357 GLULX_EXTEND_OPCODES
1358 #endif /* GLULX_EXTEND_OPCODES */
1359
1360 default:
1361 fatal_error_i("Executed unknown opcode.", opcode);
1362 }
1363 }
1364 }
1365 /* done executing */
1366 #if VM_DEBUGGER
1367 debugger_handle_quit();
1368 #endif /* VM_DEBUGGER */
1369 }