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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "glk/glulxe/glulxe.h"
namespace Glk {
namespace Glulxe {
void Glulxe::execute_loop() {
bool done_executing = false;
int ix;
uint opcode;
const operandlist_t *oplist;
oparg_t inst[MAX_OPERANDS];
uint value, addr, val0, val1;
int vals0, vals1;
uint *arglist;
uint arglistfix[3];
#ifdef FLOAT_SUPPORT
gfloat32 valf, valf1, valf2;
#endif /* FLOAT_SUPPORT */
while (!done_executing && !g_vm->shouldQuit()) {
profile_tick();
debugger_tick();
/* Do OS-specific processing, if appropriate. */
glk_tick();
/* Stash the current opcode's address, in case the interpreter needs to serialize the VM state out-of-band. */
prevpc = pc;
/* Fetch the opcode number. */
opcode = Mem1(pc);
pc++;
if (opcode & 0x80) {
/* More than one-byte opcode. */
if (opcode & 0x40) {
/* Four-byte opcode */
opcode &= 0x3F;
opcode = (opcode << 8) | Mem1(pc);
pc++;
opcode = (opcode << 8) | Mem1(pc);
pc++;
opcode = (opcode << 8) | Mem1(pc);
pc++;
} else {
/* Two-byte opcode */
opcode &= 0x7F;
opcode = (opcode << 8) | Mem1(pc);
pc++;
}
}
/* Now we have an opcode number. */
/* Fetch the structure that describes how the operands for this
opcode are arranged. This is a pointer to an immutable,
static object. */
if (opcode < 0x80)
oplist = fast_operandlist[opcode];
else
oplist = lookup_operandlist(opcode);
if (!oplist)
fatal_error_i("Encountered unknown opcode.", opcode);
/* Based on the oplist structure, load the actual operand values
into inst. This moves the PC up to the end of the instruction. */
parse_operands(inst, oplist);
/* Perform the opcode. This switch statement is split in two, based
on some paranoid suspicions about the ability of compilers to
optimize large-range switches. Ignore that. */
if (opcode < 0x80) {
switch (opcode) {
case op_nop:
break;
case op_add:
value = inst[0].value + inst[1].value;
store_operand(inst[2].desttype, inst[2].value, value);
break;
case op_sub:
value = inst[0].value - inst[1].value;
store_operand(inst[2].desttype, inst[2].value, value);
break;
case op_mul:
value = inst[0].value * inst[1].value;
store_operand(inst[2].desttype, inst[2].value, value);
break;
case op_div:
vals0 = inst[0].value;
vals1 = inst[1].value;
if (vals1 == 0)
fatal_error("Division by zero.");
/* Since C doesn't guarantee the results of division of negative
numbers, we carefully convert everything to positive values
first. They have to be unsigned values, too, otherwise the
0x80000000 case goes wonky. */
if (vals0 < 0) {
val0 = (-vals0);
if (vals1 < 0) {
val1 = (-vals1);
value = val0 / val1;
} else {
val1 = vals1;
value = -(int)(val0 / val1);
}
} else {
val0 = vals0;
if (vals1 < 0) {
val1 = (-vals1);
value = -(int)(val0 / val1);
} else {
val1 = vals1;
value = val0 / val1;
}
}
store_operand(inst[2].desttype, inst[2].value, value);
break;
case op_mod:
vals0 = inst[0].value;
vals1 = inst[1].value;
if (vals1 == 0)
fatal_error("Division by zero doing remainder.");
if (vals1 < 0) {
val1 = -vals1;
} else {
val1 = vals1;
}
if (vals0 < 0) {
val0 = (-vals0);
value = -(int)(val0 % val1);
} else {
val0 = vals0;
value = val0 % val1;
}
store_operand(inst[2].desttype, inst[2].value, value);
break;
case op_neg:
vals0 = inst[0].value;
value = (-vals0);
store_operand(inst[1].desttype, inst[1].value, value);
break;
case op_bitand:
value = (inst[0].value & inst[1].value);
store_operand(inst[2].desttype, inst[2].value, value);
break;
case op_bitor:
value = (inst[0].value | inst[1].value);
store_operand(inst[2].desttype, inst[2].value, value);
break;
case op_bitxor:
value = (inst[0].value ^ inst[1].value);
store_operand(inst[2].desttype, inst[2].value, value);
break;
case op_bitnot:
value = ~(inst[0].value);
store_operand(inst[1].desttype, inst[1].value, value);
break;
case op_shiftl:
vals0 = inst[1].value;
if (vals0 < 0 || vals0 >= 32)
value = 0;
else
value = ((uint)(inst[0].value) << (uint)vals0);
store_operand(inst[2].desttype, inst[2].value, value);
break;
case op_ushiftr:
vals0 = inst[1].value;
if (vals0 < 0 || vals0 >= 32)
value = 0;
else
value = ((uint)(inst[0].value) >> (uint)vals0);
store_operand(inst[2].desttype, inst[2].value, value);
break;
case op_sshiftr:
vals0 = inst[1].value;
if (vals0 < 0 || vals0 >= 32) {
if (inst[0].value & 0x80000000)
value = 0xFFFFFFFF;
else
value = 0;
} else {
/* This is somewhat foolhardy -- C doesn't guarantee that
right-shifting a signed value replicates the sign bit.
We'll assume it for now. */
value = ((int)(inst[0].value) >> (int)vals0);
}
store_operand(inst[2].desttype, inst[2].value, value);
break;
case op_jump:
value = inst[0].value;
/* fall through to PerformJump label. */
PerformJump: /* goto label for successful jumping... ironic, no? */
if (value == 0 || value == 1) {
/* Return from function. This is exactly what happens in
return_op, but it's only a few lines of code, so I won't
bother with a "goto". */
leave_function();
if (stackptr == 0) {
done_executing = true;
break;
}
pop_callstub(value); /* zero or one */
} else {
/* Branch to a new PC value. */
pc = (pc + value - 2);
}
break;
case op_jz:
if (inst[0].value == 0) {
value = inst[1].value;
goto PerformJump;
}
break;
case op_jnz:
if (inst[0].value != 0) {
value = inst[1].value;
goto PerformJump;
}
break;
case op_jeq:
if (inst[0].value == inst[1].value) {
value = inst[2].value;
goto PerformJump;
}
break;
case op_jne:
if (inst[0].value != inst[1].value) {
value = inst[2].value;
goto PerformJump;
}
break;
case op_jlt:
vals0 = inst[0].value;
vals1 = inst[1].value;
if (vals0 < vals1) {
value = inst[2].value;
goto PerformJump;
}
break;
case op_jgt:
vals0 = inst[0].value;
vals1 = inst[1].value;
if (vals0 > vals1) {
value = inst[2].value;
goto PerformJump;
}
break;
case op_jle:
vals0 = inst[0].value;
vals1 = inst[1].value;
if (vals0 <= vals1) {
value = inst[2].value;
goto PerformJump;
}
break;
case op_jge:
vals0 = inst[0].value;
vals1 = inst[1].value;
if (vals0 >= vals1) {
value = inst[2].value;
goto PerformJump;
}
break;
case op_jltu:
val0 = inst[0].value;
val1 = inst[1].value;
if (val0 < val1) {
value = inst[2].value;
goto PerformJump;
}
break;
case op_jgtu:
val0 = inst[0].value;
val1 = inst[1].value;
if (val0 > val1) {
value = inst[2].value;
goto PerformJump;
}
break;
case op_jleu:
val0 = inst[0].value;
val1 = inst[1].value;
if (val0 <= val1) {
value = inst[2].value;
goto PerformJump;
}
break;
case op_jgeu:
val0 = inst[0].value;
val1 = inst[1].value;
if (val0 >= val1) {
value = inst[2].value;
goto PerformJump;
}
break;
case op_call:
value = inst[1].value;
arglist = pop_arguments(value, 0);
push_callstub(inst[2].desttype, inst[2].value);
enter_function(inst[0].value, value, arglist);
break;
case op_return:
leave_function();
if (stackptr == 0) {
done_executing = true;
break;
}
pop_callstub(inst[0].value);
break;
case op_tailcall:
value = inst[1].value;
arglist = pop_arguments(value, 0);
leave_function();
enter_function(inst[0].value, value, arglist);
break;
case op_catch:
push_callstub(inst[0].desttype, inst[0].value);
value = inst[1].value;
val0 = stackptr;
store_operand(inst[0].desttype, inst[0].value, val0);
goto PerformJump;
break;
case op_throw:
profile_fail("throw");
value = inst[0].value;
stackptr = inst[1].value;
pop_callstub(value);
break;
case op_copy:
value = inst[0].value;
#ifdef TOLERATE_SUPERGLUS_BUG
if (inst[1].desttype == 1 && inst[1].value == 0)
inst[1].desttype = 0;
#endif /* TOLERATE_SUPERGLUS_BUG */
store_operand(inst[1].desttype, inst[1].value, value);
break;
case op_copys:
value = inst[0].value;
store_operand_s(inst[1].desttype, inst[1].value, value);
break;
case op_copyb:
value = inst[0].value;
store_operand_b(inst[1].desttype, inst[1].value, value);
break;
case op_sexs:
val0 = inst[0].value;
if (val0 & 0x8000)
val0 |= 0xFFFF0000;
else
val0 &= 0x0000FFFF;
store_operand(inst[1].desttype, inst[1].value, val0);
break;
case op_sexb:
val0 = inst[0].value;
if (val0 & 0x80)
val0 |= 0xFFFFFF00;
else
val0 &= 0x000000FF;
store_operand(inst[1].desttype, inst[1].value, val0);
break;
case op_aload:
value = inst[0].value;
value += 4 * inst[1].value;
val0 = Mem4(value);
store_operand(inst[2].desttype, inst[2].value, val0);
break;
case op_aloads:
value = inst[0].value;
value += 2 * inst[1].value;
val0 = Mem2(value);
store_operand(inst[2].desttype, inst[2].value, val0);
break;
case op_aloadb:
value = inst[0].value;
value += inst[1].value;
val0 = Mem1(value);
store_operand(inst[2].desttype, inst[2].value, val0);
break;
case op_aloadbit:
value = inst[0].value;
vals0 = inst[1].value;
val1 = (vals0 & 7);
if (vals0 >= 0)
value += (vals0 >> 3);
else
value -= (1 + ((-1 - vals0) >> 3));
if (Mem1(value) & (1 << val1))
val0 = 1;
else
val0 = 0;
store_operand(inst[2].desttype, inst[2].value, val0);
break;
case op_astore:
value = inst[0].value;
value += 4 * inst[1].value;
val0 = inst[2].value;
MemW4(value, val0);
break;
case op_astores:
value = inst[0].value;
value += 2 * inst[1].value;
val0 = inst[2].value;
MemW2(value, val0);
break;
case op_astoreb:
value = inst[0].value;
value += inst[1].value;
val0 = inst[2].value;
MemW1(value, val0);
break;
case op_astorebit:
value = inst[0].value;
vals0 = inst[1].value;
val1 = (vals0 & 7);
if (vals0 >= 0)
value += (vals0 >> 3);
else
value -= (1 + ((-1 - vals0) >> 3));
val0 = Mem1(value);
if (inst[2].value)
val0 |= (1 << val1);
else
val0 &= ~((uint)(1 << val1));
MemW1(value, val0);
break;
case op_stkcount:
value = (stackptr - valstackbase) / 4;
store_operand(inst[0].desttype, inst[0].value, value);
break;
case op_stkpeek:
vals0 = inst[0].value * 4;
if (vals0 < 0 || vals0 >= (int)(stackptr - valstackbase))
fatal_error("Stkpeek outside current stack range.");
value = Stk4(stackptr - (vals0 + 4));
store_operand(inst[1].desttype, inst[1].value, value);
break;
case op_stkswap:
if (stackptr < valstackbase + 8) {
fatal_error("Stack underflow in stkswap.");
}
val0 = Stk4(stackptr - 4);
val1 = Stk4(stackptr - 8);
StkW4(stackptr - 4, val1);
StkW4(stackptr - 8, val0);
break;
case op_stkcopy:
vals0 = inst[0].value;
if (vals0 < 0)
fatal_error("Negative operand in stkcopy.");
if (vals0 == 0)
break;
if (stackptr < valstackbase + vals0 * 4)
fatal_error("Stack underflow in stkcopy.");
if (stackptr + vals0 * 4 > stacksize)
fatal_error("Stack overflow in stkcopy.");
addr = stackptr - vals0 * 4;
for (ix = 0; ix < vals0; ix++) {
value = Stk4(addr + ix * 4);
StkW4(stackptr + ix * 4, value);
}
stackptr += vals0 * 4;
break;
case op_stkroll:
vals0 = inst[0].value;
vals1 = inst[1].value;
if (vals0 < 0)
fatal_error("Negative operand in stkroll.");
if (stackptr < valstackbase + vals0 * 4)
fatal_error("Stack underflow in stkroll.");
if (vals0 == 0)
break;
/* The following is a bit ugly. We want to do vals1 = vals0-vals1,
because rolling down is sort of easier than rolling up. But
we also want to take the result mod vals0. The % operator is
annoying for negative numbers, so we need to do this in two
cases. */
if (vals1 > 0) {
vals1 = vals1 % vals0;
vals1 = (vals0) - vals1;
} else {
vals1 = (-vals1) % vals0;
}
if (vals1 == 0)
break;
addr = stackptr - vals0 * 4;
for (ix = 0; ix < vals1; ix++) {
value = Stk4(addr + ix * 4);
StkW4(stackptr + ix * 4, value);
}
for (ix = 0; ix < vals0; ix++) {
value = Stk4(addr + (vals1 + ix) * 4);
StkW4(addr + ix * 4, value);
}
break;
case op_streamchar:
profile_in(0xE0000001, stackptr, false);
value = inst[0].value & 0xFF;
(this->*stream_char_handler)(value);
profile_out(stackptr);
break;
case op_streamunichar:
profile_in(0xE0000002, stackptr, false);
value = inst[0].value;
(this->*stream_unichar_handler)(value);
profile_out(stackptr);
break;
case op_streamnum:
profile_in(0xE0000003, stackptr, false);
vals0 = inst[0].value;
stream_num(vals0, false, 0);
profile_out(stackptr);
break;
case op_streamstr:
profile_in(0xE0000004, stackptr, false);
stream_string(inst[0].value, 0, 0);
profile_out(stackptr);
break;
default:
fatal_error_i("Executed unknown opcode.", opcode);
}
} else {
switch (opcode) {
case op_gestalt:
value = do_gestalt(inst[0].value, inst[1].value);
store_operand(inst[2].desttype, inst[2].value, value);
break;
case op_debugtrap:
#if VM_DEBUGGER
/* We block and handle debug commands, but only if the
library has invoked debug features. (Meaning, has
the cycle handler ever been called.) */
if (debugger_ever_invoked()) {
debugger_block_and_debug("user debugtrap, pausing...");
break;
}
#endif /* VM_DEBUGGER */
fatal_error_i("user debugtrap encountered.", inst[0].value);
case op_jumpabs:
pc = inst[0].value;
break;
case op_callf:
push_callstub(inst[1].desttype, inst[1].value);
enter_function(inst[0].value, 0, arglistfix);
break;
case op_callfi:
arglistfix[0] = inst[1].value;
push_callstub(inst[2].desttype, inst[2].value);
enter_function(inst[0].value, 1, arglistfix);
break;
case op_callfii:
arglistfix[0] = inst[1].value;
arglistfix[1] = inst[2].value;
push_callstub(inst[3].desttype, inst[3].value);
enter_function(inst[0].value, 2, arglistfix);
break;
case op_callfiii:
arglistfix[0] = inst[1].value;
arglistfix[1] = inst[2].value;
arglistfix[2] = inst[3].value;
push_callstub(inst[4].desttype, inst[4].value);
enter_function(inst[0].value, 3, arglistfix);
break;
case op_getmemsize:
store_operand(inst[0].desttype, inst[0].value, endmem);
break;
case op_setmemsize:
value = change_memsize(inst[0].value, false);
store_operand(inst[1].desttype, inst[1].value, value);
break;
case op_getstringtbl:
value = stream_get_table();
store_operand(inst[0].desttype, inst[0].value, value);
break;
case op_setstringtbl:
stream_set_table(inst[0].value);
break;
case op_getiosys:
stream_get_iosys(&val0, &val1);
store_operand(inst[0].desttype, inst[0].value, val0);
store_operand(inst[1].desttype, inst[1].value, val1);
break;
case op_setiosys:
stream_set_iosys(inst[0].value, inst[1].value);
break;
case op_glk:
profile_in(0xF0000000 + inst[0].value, stackptr, false);
value = inst[1].value;
arglist = pop_arguments(value, 0);
val0 = perform_glk(inst[0].value, value, arglist);
#ifdef TOLERATE_SUPERGLUS_BUG
if (inst[2].desttype == 1 && inst[2].value == 0)
inst[2].desttype = 0;
#endif /* TOLERATE_SUPERGLUS_BUG */
store_operand(inst[2].desttype, inst[2].value, val0);
profile_out(stackptr);
break;
case op_random:
vals0 = inst[0].value;
if (vals0 == 0)
value = glulx_random();
else if (vals0 >= 1)
value = glulx_random() % (uint)(vals0);
else
value = -(int)(glulx_random() % (uint)(-vals0));
store_operand(inst[1].desttype, inst[1].value, value);
break;
case op_setrandom:
glulx_setrandom(inst[0].value);
break;
case op_verify:
value = perform_verify();
store_operand(inst[0].desttype, inst[0].value, value);
break;
case op_restart:
profile_fail("restart");
vm_restart();
break;
case op_protect:
val0 = inst[0].value;
val1 = val0 + inst[1].value;
if (val0 == val1) {
val0 = 0;
val1 = 0;
}
protectstart = val0;
protectend = val1;
break;
case op_save:
push_callstub(inst[1].desttype, inst[1].value);
value = saveGameData(find_stream_by_id(inst[0].value), "Savegame").getCode() == Common::kNoError ? 0 : 1;
pop_callstub(value);
break;
case op_restore:
value = loadGameData(find_stream_by_id(inst[0].value)).getCode() == Common::kNoError ? 0 : 1;
if (value == 0) {
/* We've succeeded, and the stack now contains the callstub
saved during saveundo. Ignore this opcode's operand. */
value = (uint) - 1;
pop_callstub(value);
} else {
/* We've failed, so we must store the failure in this opcode's
operand. */
store_operand(inst[1].desttype, inst[1].value, value);
}
break;
case op_saveundo:
push_callstub(inst[0].desttype, inst[0].value);
value = perform_saveundo();
pop_callstub(value);
break;
case op_restoreundo:
value = perform_restoreundo();
if (value == 0) {
/* We've succeeded, and the stack now contains the callstub
saved during saveundo. Ignore this opcode's operand. */
value = (uint) - 1;
pop_callstub(value);
} else {
/* We've failed, so we must store the failure in this opcode's
operand. */
store_operand(inst[0].desttype, inst[0].value, value);
}
break;
case op_quit:
done_executing = true;
break;
case op_linearsearch:
value = linear_search(inst[0].value, inst[1].value, inst[2].value,
inst[3].value, inst[4].value, inst[5].value, inst[6].value);
store_operand(inst[7].desttype, inst[7].value, value);
break;
case op_binarysearch:
value = binary_search(inst[0].value, inst[1].value, inst[2].value,
inst[3].value, inst[4].value, inst[5].value, inst[6].value);
store_operand(inst[7].desttype, inst[7].value, value);
break;
case op_linkedsearch:
value = linked_search(inst[0].value, inst[1].value, inst[2].value,
inst[3].value, inst[4].value, inst[5].value);
store_operand(inst[6].desttype, inst[6].value, value);
break;
case op_mzero: {
uint lx;
uint count = inst[0].value;
addr = inst[1].value;
for (lx = 0; lx < count; lx++, addr++) {
MemW1(addr, 0);
}
}
break;
case op_mcopy: {
uint lx;
uint count = inst[0].value;
uint addrsrc = inst[1].value;
uint addrdest = inst[2].value;
if (addrdest < addrsrc) {
for (lx = 0; lx < count; lx++, addrsrc++, addrdest++) {
value = Mem1(addrsrc);
MemW1(addrdest, value);
}
} else {
addrsrc += (count - 1);
addrdest += (count - 1);
for (lx = 0; lx < count; lx++, addrsrc--, addrdest--) {
value = Mem1(addrsrc);
MemW1(addrdest, value);
}
}
}
break;
case op_malloc:
value = heap_alloc(inst[0].value);
store_operand(inst[1].desttype, inst[1].value, value);
break;
case op_mfree:
heap_free(inst[0].value);
break;
case op_accelfunc:
accel_set_func(inst[0].value, inst[1].value);
break;
case op_accelparam:
accel_set_param(inst[0].value, inst[1].value);
break;
#ifdef FLOAT_SUPPORT
case op_numtof:
vals0 = inst[0].value;
value = encode_float((gfloat32)vals0);
store_operand(inst[1].desttype, inst[1].value, value);
break;
case op_ftonumz:
valf = decode_float(inst[0].value);
if (!signbit(valf)) {
if (isnan(valf) || isinf(valf) || (valf > 2147483647.0))
vals0 = 0x7FFFFFFF;
else
vals0 = (int)(truncf(valf));
} else {
if (isnan(valf) || isinf(valf) || (valf < -2147483647.0))
vals0 = 0x80000000;
else
vals0 = (int)(truncf(valf));
}
store_operand(inst[1].desttype, inst[1].value, vals0);
break;
case op_ftonumn:
valf = decode_float(inst[0].value);
if (!signbit(valf)) {
if (isnan(valf) || isinf(valf) || (valf > 2147483647.0))
vals0 = 0x7FFFFFFF;
else
vals0 = (int)(roundf(valf));
} else {
if (isnan(valf) || isinf(valf) || (valf < -2147483647.0))
vals0 = 0x80000000;
else
vals0 = (int)(roundf(valf));
}
store_operand(inst[1].desttype, inst[1].value, vals0);
break;
case op_fadd:
valf1 = decode_float(inst[0].value);
valf2 = decode_float(inst[1].value);
value = encode_float(valf1 + valf2);
store_operand(inst[2].desttype, inst[2].value, value);
break;
case op_fsub:
valf1 = decode_float(inst[0].value);
valf2 = decode_float(inst[1].value);
value = encode_float(valf1 - valf2);
store_operand(inst[2].desttype, inst[2].value, value);
break;
case op_fmul:
valf1 = decode_float(inst[0].value);
valf2 = decode_float(inst[1].value);
value = encode_float(valf1 * valf2);
store_operand(inst[2].desttype, inst[2].value, value);
break;
case op_fdiv:
valf1 = decode_float(inst[0].value);
valf2 = decode_float(inst[1].value);
value = encode_float(valf1 / valf2);
store_operand(inst[2].desttype, inst[2].value, value);
break;
case op_fmod:
valf1 = decode_float(inst[0].value);
valf2 = decode_float(inst[1].value);
valf = fmodf(valf1, valf2);
val0 = encode_float(valf);
val1 = encode_float((valf1 - valf) / valf2);
if (val1 == 0x0 || val1 == 0x80000000) {
/* When the quotient is zero, the sign has been lost in the
shuffle. We'll set that by hand, based on the original
arguments. */
val1 = (inst[0].value ^ inst[1].value) & 0x80000000;
}
store_operand(inst[2].desttype, inst[2].value, val0);
store_operand(inst[3].desttype, inst[3].value, val1);
break;
case op_floor:
valf = decode_float(inst[0].value);
value = encode_float(floorf(valf));
store_operand(inst[1].desttype, inst[1].value, value);
break;
case op_ceil:
valf = decode_float(inst[0].value);
value = encode_float(ceilf(valf));
if (value == 0x0 || value == 0x80000000) {
/* When the result is zero, the sign may have been lost in the
shuffle. (This is a bug in some C libraries.) We'll set the
sign by hand, based on the original argument. */
value = inst[0].value & 0x80000000;
}
store_operand(inst[1].desttype, inst[1].value, value);
break;
case op_sqrt:
valf = decode_float(inst[0].value);
value = encode_float(sqrtf(valf));
store_operand(inst[1].desttype, inst[1].value, value);
break;
case op_log:
valf = decode_float(inst[0].value);
value = encode_float(logf(valf));
store_operand(inst[1].desttype, inst[1].value, value);
break;
case op_exp:
valf = decode_float(inst[0].value);
value = encode_float(expf(valf));
store_operand(inst[1].desttype, inst[1].value, value);
break;
case op_pow:
valf1 = decode_float(inst[0].value);
valf2 = decode_float(inst[1].value);
value = encode_float(glulx_powf(valf1, valf2));
store_operand(inst[2].desttype, inst[2].value, value);
break;
case op_sin:
valf = decode_float(inst[0].value);
value = encode_float(sinf(valf));
store_operand(inst[1].desttype, inst[1].value, value);
break;
case op_cos:
valf = decode_float(inst[0].value);
value = encode_float(cosf(valf));
store_operand(inst[1].desttype, inst[1].value, value);
break;
case op_tan:
valf = decode_float(inst[0].value);
value = encode_float(tanf(valf));
store_operand(inst[1].desttype, inst[1].value, value);
break;
case op_asin:
valf = decode_float(inst[0].value);
value = encode_float(asinf(valf));
store_operand(inst[1].desttype, inst[1].value, value);
break;
case op_acos:
valf = decode_float(inst[0].value);
value = encode_float(acosf(valf));
store_operand(inst[1].desttype, inst[1].value, value);
break;
case op_atan:
valf = decode_float(inst[0].value);
value = encode_float(atanf(valf));
store_operand(inst[1].desttype, inst[1].value, value);
break;
case op_atan2:
valf1 = decode_float(inst[0].value);
valf2 = decode_float(inst[1].value);
value = encode_float(atan2f(valf1, valf2));
store_operand(inst[2].desttype, inst[2].value, value);
break;
case op_jisinf:
/* Infinity is well-defined, so we don't bother to convert to
float. */
val0 = inst[0].value;
if (val0 == 0x7F800000 || val0 == 0xFF800000) {
value = inst[1].value;
goto PerformJump;
}
break;
case op_jisnan:
/* NaN is well-defined, so we don't bother to convert to
float. */
val0 = inst[0].value;
if ((val0 & 0x7F800000) == 0x7F800000 && (val0 & 0x007FFFFF) != 0) {
value = inst[1].value;
goto PerformJump;
}
break;
case op_jfeq:
if ((inst[2].value & 0x7F800000) == 0x7F800000 && (inst[2].value & 0x007FFFFF) != 0) {
/* The delta is NaN, which can never match. */
val0 = 0;
} else if ((inst[0].value == 0x7F800000 || inst[0].value == 0xFF800000)
&& (inst[1].value == 0x7F800000 || inst[1].value == 0xFF800000)) {
/* Both are infinite. Opposite infinities are never equal,
even if the difference is infinite, so this is easy. */
val0 = (inst[0].value == inst[1].value);
} else {
valf1 = decode_float(inst[1].value) - decode_float(inst[0].value);
valf2 = fabs(decode_float(inst[2].value));
val0 = (valf1 <= valf2 && valf1 >= -valf2);
}
if (val0) {
value = inst[3].value;
goto PerformJump;
}
break;
case op_jfne:
if ((inst[2].value & 0x7F800000) == 0x7F800000 && (inst[2].value & 0x007FFFFF) != 0) {
/* The delta is NaN, which can never match. */
val0 = 0;
} else if ((inst[0].value == 0x7F800000 || inst[0].value == 0xFF800000)
&& (inst[1].value == 0x7F800000 || inst[1].value == 0xFF800000)) {
/* Both are infinite. Opposite infinities are never equal,
even if the difference is infinite, so this is easy. */
val0 = (inst[0].value == inst[1].value);
} else {
valf1 = decode_float(inst[1].value) - decode_float(inst[0].value);
valf2 = fabs(decode_float(inst[2].value));
val0 = (valf1 <= valf2 && valf1 >= -valf2);
}
if (!val0) {
value = inst[3].value;
goto PerformJump;
}
break;
case op_jflt:
valf1 = decode_float(inst[0].value);
valf2 = decode_float(inst[1].value);
if (valf1 < valf2) {
value = inst[2].value;
goto PerformJump;
}
break;
case op_jfgt:
valf1 = decode_float(inst[0].value);
valf2 = decode_float(inst[1].value);
if (valf1 > valf2) {
value = inst[2].value;
goto PerformJump;
}
break;
case op_jfle:
valf1 = decode_float(inst[0].value);
valf2 = decode_float(inst[1].value);
if (valf1 <= valf2) {
value = inst[2].value;
goto PerformJump;
}
break;
case op_jfge:
valf1 = decode_float(inst[0].value);
valf2 = decode_float(inst[1].value);
if (valf1 >= valf2) {
value = inst[2].value;
goto PerformJump;
}
break;
#endif /* FLOAT_SUPPORT */
#ifdef GLULX_EXTEND_OPCODES
GLULX_EXTEND_OPCODES
#endif /* GLULX_EXTEND_OPCODES */
default:
fatal_error_i("Executed unknown opcode.", opcode);
}
}
}
/* done executing */
#if VM_DEBUGGER
debugger_handle_quit();
#endif /* VM_DEBUGGER */
}
} // End of namespace Glulxe
} // End of namespace Glk
|