Debian Codegraph @

terps/glulxe/operand.c

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