-
Notifications
You must be signed in to change notification settings - Fork 22
/
operand.c
693 lines (593 loc) · 16.9 KB
/
operand.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/* operand.c: Glulxe code for instruction operands, reading and writing.
Designed by Andrew Plotkin <[email protected]>
http://eblong.com/zarf/glulx/index.html
*/
#include "glk.h"
#include "glulxe.h"
#include "opcodes.h"
/* ### We could save a few cycles per operand by generating a function for
each operandlist type. */
/* fast_operandlist[]:
This is a handy array in which to look up operandlists quickly.
It stores the operandlists for the first 128 opcodes, which are
the ones used most frequently.
*/
const operandlist_t *fast_operandlist[0x80];
/* The actual immutable structures which lookup_operandlist()
returns. */
static operandlist_t list_none = { 0, 4, NULL };
static int array_S[1] = { modeform_Store };
static operandlist_t list_S = { 1, 4, array_S };
static int array_LS[2] = { modeform_Load, modeform_Store };
static operandlist_t list_LS = { 2, 4, array_LS };
static int array_LLS[3] = { modeform_Load, modeform_Load, modeform_Store };
static operandlist_t list_LLS = { 3, 4, array_LLS };
static int array_LLLS[4] = { modeform_Load, modeform_Load, modeform_Load, modeform_Store };
static operandlist_t list_LLLS = { 4, 4, array_LLLS };
static int array_LLLLS[5] = { modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Store };
static operandlist_t list_LLLLS = { 5, 4, array_LLLLS };
/* static int array_LLLLLS[6] = { modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Store };
static operandlist_t list_LLLLLS = { 6, 4, array_LLLLLS }; */ /* not currently used */
static int array_LLLLLLS[7] = { modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Store };
static operandlist_t list_LLLLLLS = { 7, 4, array_LLLLLLS };
static int array_LLLLLLLS[8] = { modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Store };
static operandlist_t list_LLLLLLLS = { 8, 4, array_LLLLLLLS };
static int array_L[1] = { modeform_Load };
static operandlist_t list_L = { 1, 4, array_L };
static int array_LL[2] = { modeform_Load, modeform_Load };
static operandlist_t list_LL = { 2, 4, array_LL };
static int array_LLL[3] = { modeform_Load, modeform_Load, modeform_Load };
static operandlist_t list_LLL = { 3, 4, array_LLL };
static operandlist_t list_2LS = { 2, 2, array_LS };
static operandlist_t list_1LS = { 2, 1, array_LS };
static int array_LLLL[4] = { modeform_Load, modeform_Load, modeform_Load, modeform_Load };
static operandlist_t list_LLLL = { 4, 4, array_LLLL };
static int array_LLLLL[5] = { modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Load };
static operandlist_t list_LLLLL = { 5, 4, array_LLLLL };
static int array_LLLLLL[6] = { modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Load };
static operandlist_t list_LLLLLL = { 6, 4, array_LLLLLL };
static int array_LLLLLLL[7] = { modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Load };
static operandlist_t list_LLLLLLL = { 7, 4, array_LLLLLLL };
static int array_SL[2] = { modeform_Store, modeform_Load };
static operandlist_t list_SL = { 2, 4, array_SL };
static int array_SS[2] = { modeform_Store, modeform_Store };
static operandlist_t list_SS = { 2, 4, array_SS };
static int array_LSS[3] = { modeform_Load, modeform_Store, modeform_Store };
static operandlist_t list_LSS = { 3, 4, array_LSS };
static int array_LLSS[4] = { modeform_Load, modeform_Load, modeform_Store, modeform_Store };
static operandlist_t list_LLSS = { 4, 4, array_LLSS };
static int array_LLLLSS[6] = { modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Store, modeform_Store };
static operandlist_t list_LLLLSS = { 6, 4, array_LLLLSS };
/* init_operands():
Set up the fast-lookup array of operandlists. This is called just
once, when the terp starts up.
*/
void init_operands()
{
int ix;
for (ix=0; ix<0x80; ix++)
fast_operandlist[ix] = lookup_operandlist(ix);
}
/* lookup_operandlist():
Return the operandlist for a given opcode. For opcodes in the range
00..7F, it's faster to use the array fast_operandlist[].
*/
const operandlist_t *lookup_operandlist(glui32 opcode)
{
switch (opcode) {
case op_nop:
return &list_none;
case op_add:
case op_sub:
case op_mul:
case op_div:
case op_mod:
case op_bitand:
case op_bitor:
case op_bitxor:
case op_shiftl:
case op_sshiftr:
case op_ushiftr:
return &list_LLS;
case op_neg:
case op_bitnot:
return &list_LS;
case op_jump:
case op_jumpabs:
return &list_L;
case op_jz:
case op_jnz:
return &list_LL;
case op_jeq:
case op_jne:
case op_jlt:
case op_jge:
case op_jgt:
case op_jle:
case op_jltu:
case op_jgeu:
case op_jgtu:
case op_jleu:
return &list_LLL;
case op_call:
return &list_LLS;
case op_return:
return &list_L;
case op_catch:
return &list_SL;
case op_throw:
return &list_LL;
case op_tailcall:
return &list_LL;
case op_sexb:
case op_sexs:
return &list_LS;
case op_copy:
return &list_LS;
case op_copys:
return &list_2LS;
case op_copyb:
return &list_1LS;
case op_aload:
case op_aloads:
case op_aloadb:
case op_aloadbit:
return &list_LLS;
case op_astore:
case op_astores:
case op_astoreb:
case op_astorebit:
return &list_LLL;
case op_stkcount:
return &list_S;
case op_stkpeek:
return &list_LS;
case op_stkswap:
return &list_none;
case op_stkroll:
return &list_LL;
case op_stkcopy:
return &list_L;
case op_streamchar:
case op_streamunichar:
case op_streamnum:
case op_streamstr:
return &list_L;
case op_getstringtbl:
return &list_S;
case op_setstringtbl:
return &list_L;
case op_getiosys:
return &list_SS;
case op_setiosys:
return &list_LL;
case op_random:
return &list_LS;
case op_setrandom:
return &list_L;
case op_verify:
return &list_S;
case op_restart:
return &list_none;
case op_save:
case op_restore:
return &list_LS;
case op_saveundo:
case op_restoreundo:
case op_hasundo:
return &list_S;
case op_discardundo:
return &list_none;
case op_protect:
return &list_LL;
case op_quit:
return &list_none;
case op_gestalt:
return &list_LLS;
case op_debugtrap:
return &list_L;
case op_getmemsize:
return &list_S;
case op_setmemsize:
return &list_LS;
case op_linearsearch:
return &list_LLLLLLLS;
case op_binarysearch:
return &list_LLLLLLLS;
case op_linkedsearch:
return &list_LLLLLLS;
case op_glk:
return &list_LLS;
case op_callf:
return &list_LS;
case op_callfi:
return &list_LLS;
case op_callfii:
return &list_LLLS;
case op_callfiii:
return &list_LLLLS;
case op_mzero:
return &list_LL;
case op_mcopy:
return &list_LLL;
case op_malloc:
return &list_LS;
case op_mfree:
return &list_L;
case op_accelfunc:
case op_accelparam:
return &list_LL;
#ifdef FLOAT_SUPPORT
case op_numtof:
case op_ftonumz:
case op_ftonumn:
case op_ceil:
case op_floor:
case op_sqrt:
case op_exp:
case op_log:
return &list_LS;
case op_fadd:
case op_fsub:
case op_fmul:
case op_fdiv:
case op_pow:
case op_atan2:
return &list_LLS;
case op_fmod:
return &list_LLSS;
case op_sin:
case op_cos:
case op_tan:
case op_asin:
case op_acos:
case op_atan:
return &list_LS;
case op_jfeq:
case op_jfne:
return &list_LLLL;
case op_jflt:
case op_jfle:
case op_jfgt:
case op_jfge:
return &list_LLL;
case op_jisnan:
case op_jisinf:
return &list_LL;
#endif /* FLOAT_SUPPORT */
#ifdef DOUBLE_SUPPORT
case op_numtod:
case op_ftod:
return &list_LSS;
case op_dtonumz:
case op_dtonumn:
case op_dtof:
return &list_LLS;
case op_dceil:
case op_dfloor:
case op_dsqrt:
case op_dexp:
case op_dlog:
return &list_LLSS;
case op_dadd:
case op_dsub:
case op_dmul:
case op_ddiv:
case op_dpow:
case op_datan2:
return &list_LLLLSS;
case op_dmodr:
case op_dmodq:
return &list_LLLLSS;
case op_dsin:
case op_dcos:
case op_dtan:
case op_dasin:
case op_dacos:
case op_datan:
return &list_LLSS;
case op_jdeq:
case op_jdne:
return &list_LLLLLLL;
case op_jdlt:
case op_jdle:
case op_jdgt:
case op_jdge:
return &list_LLLLL;
case op_jdisnan:
case op_jdisinf:
return &list_LLL;
#endif /* DOUBLE_SUPPORT */
#ifdef GLULX_EXTEND_OPERANDS
GLULX_EXTEND_OPERANDS
#endif /* GLULX_EXTEND_OPERANDS */
default:
return NULL;
}
}
/* parse_operands():
Read the list of operands of an instruction, and put the values
in args. This assumes that the PC is at the beginning of the
operand mode list (right after an opcode number.) Upon return,
the PC will be at the beginning of the next instruction.
This also assumes that args points at an allocated array of
MAX_OPERANDS oparg_t structures.
*/
void parse_operands(oparg_t *args, const operandlist_t *oplist)
{
int ix;
oparg_t *curarg;
int numops = oplist->num_ops;
int argsize = oplist->arg_size;
glui32 modeaddr = pc;
int modeval = 0;
pc += (numops+1) / 2;
for (ix=0, curarg=args; ix<numops; ix++, curarg++) {
int mode;
glui32 value;
glui32 addr;
curarg->desttype = 0;
if ((ix & 1) == 0) {
modeval = Mem1(modeaddr);
mode = (modeval & 0x0F);
}
else {
mode = ((modeval >> 4) & 0x0F);
modeaddr++;
}
if (oplist->formlist[ix] == modeform_Load) {
switch (mode) {
case 8: /* pop off stack */
if (stackptr < valstackbase+4) {
fatal_error("Stack underflow in operand.");
}
stackptr -= 4;
value = Stk4(stackptr);
break;
case 0: /* constant zero */
value = 0;
break;
case 1: /* one-byte constant */
/* Sign-extend from 8 bits to 32 */
value = (glsi32)(signed char)(Mem1(pc));
pc++;
break;
case 2: /* two-byte constant */
/* Sign-extend the first byte from 8 bits to 32; the subsequent
byte must not be sign-extended. */
value = (glsi32)(signed char)(Mem1(pc));
pc++;
value = (value << 8) | (glui32)(Mem1(pc));
pc++;
break;
case 3: /* four-byte constant */
/* Bytes must not be sign-extended. */
value = Mem4(pc);
pc += 4;
break;
case 15: /* main memory RAM, four-byte address */
addr = Mem4(pc);
addr += ramstart;
pc += 4;
goto MainMemAddr;
case 14: /* main memory RAM, two-byte address */
addr = (glui32)Mem2(pc);
addr += ramstart;
pc += 2;
goto MainMemAddr;
case 13: /* main memory RAM, one-byte address */
addr = (glui32)(Mem1(pc));
addr += ramstart;
pc++;
goto MainMemAddr;
case 7: /* main memory, four-byte address */
addr = Mem4(pc);
pc += 4;
goto MainMemAddr;
case 6: /* main memory, two-byte address */
addr = (glui32)Mem2(pc);
pc += 2;
goto MainMemAddr;
case 5: /* main memory, one-byte address */
addr = (glui32)(Mem1(pc));
pc++;
/* fall through */
MainMemAddr:
/* cases 5, 6, 7, 13, 14, 15 all wind up here. */
if (argsize == 4) {
value = Mem4(addr);
}
else if (argsize == 2) {
value = Mem2(addr);
}
else {
value = Mem1(addr);
}
break;
case 11: /* locals, four-byte address */
addr = Mem4(pc);
pc += 4;
goto LocalsAddr;
case 10: /* locals, two-byte address */
addr = (glui32)Mem2(pc);
pc += 2;
goto LocalsAddr;
case 9: /* locals, one-byte address */
addr = (glui32)(Mem1(pc));
pc++;
/* fall through */
LocalsAddr:
/* cases 9, 10, 11 all wind up here. It's illegal for addr to not
be four-byte aligned, but we don't check this explicitly.
A "strict mode" interpreter probably should. It's also illegal
for addr to be less than zero or greater than the size of
the locals segment. */
addr += localsbase;
if (argsize == 4) {
value = Stk4(addr);
}
else if (argsize == 2) {
value = Stk2(addr);
}
else {
value = Stk1(addr);
}
break;
default:
value = 0;
fatal_error("Unknown addressing mode in load operand.");
}
curarg->value = value;
}
else { /* modeform_Store */
switch (mode) {
case 0: /* discard value */
curarg->desttype = 0;
curarg->value = 0;
break;
case 8: /* push on stack */
curarg->desttype = 3;
curarg->value = 0;
break;
case 15: /* main memory RAM, four-byte address */
addr = Mem4(pc);
addr += ramstart;
pc += 4;
goto WrMainMemAddr;
case 14: /* main memory RAM, two-byte address */
addr = (glui32)Mem2(pc);
addr += ramstart;
pc += 2;
goto WrMainMemAddr;
case 13: /* main memory RAM, one-byte address */
addr = (glui32)(Mem1(pc));
addr += ramstart;
pc++;
goto WrMainMemAddr;
case 7: /* main memory, four-byte address */
addr = Mem4(pc);
pc += 4;
goto WrMainMemAddr;
case 6: /* main memory, two-byte address */
addr = (glui32)Mem2(pc);
pc += 2;
goto WrMainMemAddr;
case 5: /* main memory, one-byte address */
addr = (glui32)(Mem1(pc));
pc++;
/* fall through */
WrMainMemAddr:
/* cases 5, 6, 7 all wind up here. */
curarg->desttype = 1;
curarg->value = addr;
break;
case 11: /* locals, four-byte address */
addr = Mem4(pc);
pc += 4;
goto WrLocalsAddr;
case 10: /* locals, two-byte address */
addr = (glui32)Mem2(pc);
pc += 2;
goto WrLocalsAddr;
case 9: /* locals, one-byte address */
addr = (glui32)(Mem1(pc));
pc++;
/* fall through */
WrLocalsAddr:
/* cases 9, 10, 11 all wind up here. It's illegal for addr to not
be four-byte aligned, but we don't check this explicitly.
A "strict mode" interpreter probably should. It's also illegal
for addr to be less than zero or greater than the size of
the locals segment. */
curarg->desttype = 2;
/* We don't add localsbase here; the store address for desttype 2
is relative to the current locals segment, not an absolute
stack position. */
curarg->value = addr;
break;
case 1:
case 2:
case 3:
fatal_error("Constant addressing mode in store operand.");
default:
fatal_error("Unknown addressing mode in store operand.");
}
}
}
}
/* store_operand():
Store a result value, according to the desttype and destaddress given.
This is usually used to store the result of an opcode, but it's also
used by any code that pulls a call-stub off the stack.
*/
void store_operand(glui32 desttype, glui32 destaddr, glui32 storeval)
{
switch (desttype) {
case 0: /* do nothing; discard the value. */
return;
case 1: /* main memory. */
MemW4(destaddr, storeval);
return;
case 2: /* locals. */
destaddr += localsbase;
StkW4(destaddr, storeval);
return;
case 3: /* push on stack. */
if (stackptr+4 > stacksize) {
fatal_error("Stack overflow in store operand.");
}
StkW4(stackptr, storeval);
stackptr += 4;
return;
default:
fatal_error("Unknown destination type in store operand.");
}
}
void store_operand_s(glui32 desttype, glui32 destaddr, glui32 storeval)
{
storeval &= 0xFFFF;
switch (desttype) {
case 0: /* do nothing; discard the value. */
return;
case 1: /* main memory. */
MemW2(destaddr, storeval);
return;
case 2: /* locals. */
destaddr += localsbase;
StkW2(destaddr, storeval);
return;
case 3: /* push on stack. A four-byte value is actually pushed. */
if (stackptr+4 > stacksize) {
fatal_error("Stack overflow in store operand.");
}
StkW4(stackptr, storeval);
stackptr += 4;
return;
default:
fatal_error("Unknown destination type in store operand.");
}
}
void store_operand_b(glui32 desttype, glui32 destaddr, glui32 storeval)
{
storeval &= 0xFF;
switch (desttype) {
case 0: /* do nothing; discard the value. */
return;
case 1: /* main memory. */
MemW1(destaddr, storeval);
return;
case 2: /* locals. */
destaddr += localsbase;
StkW1(destaddr, storeval);
return;
case 3: /* push on stack. A four-byte value is actually pushed. */
if (stackptr+4 > stacksize) {
fatal_error("Stack overflow in store operand.");
}
StkW4(stackptr, storeval);
stackptr += 4;
return;
default:
fatal_error("Unknown destination type in store operand.");
}
}