aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/glulxe/operand.cpp
blob: a880b74b5153c76e0588a6ff521dd48d32bda56c (plain)
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
/* 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 {

/**
 * The actual immutable structures which lookup_operandlist() returns.
 */
static const operandlist_t list_none = { 0, 4, nullptr };

static const int array_S[1] = { modeform_Store };
static const operandlist_t list_S = { 1, 4, &array_S[0] };
static const int array_LS[2] = { modeform_Load, modeform_Store };
static const operandlist_t list_LS = { 2, 4, &array_LS[0] };
static const int array_LLS[3] = { modeform_Load, modeform_Load, modeform_Store };
static const operandlist_t list_LLS = { 3, 4, &array_LLS[0] };
static const int array_LLLS[4] = { modeform_Load, modeform_Load, modeform_Load, modeform_Store };
static const operandlist_t list_LLLS = { 4, 4, &array_LLLS[0] };
static const int array_LLLLS[5] = { modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Store };
static const operandlist_t list_LLLLS = { 5, 4, &array_LLLLS[0] };
/* static const int array_LLLLLS[6] = { modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Store };
static const operandlist_t list_LLLLLS = { 6, 4, &array_LLLLLS }; */ /* not currently used */
static const int array_LLLLLLS[7] = { modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Store };
static const operandlist_t list_LLLLLLS = { 7, 4, &array_LLLLLLS[0] };
static const int array_LLLLLLLS[8] = { modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Load, modeform_Store };
static const operandlist_t list_LLLLLLLS = { 8, 4, &array_LLLLLLLS[0] };

static const int array_L[1] = { modeform_Load };
static const operandlist_t list_L = { 1, 4, &array_L[0] };
static const int array_LL[2] = { modeform_Load, modeform_Load };
static const operandlist_t list_LL = { 2, 4, &array_LL[0] };
static const int array_LLL[3] = { modeform_Load, modeform_Load, modeform_Load };
static const operandlist_t list_LLL = { 3, 4, &array_LLL[0] };
static const operandlist_t list_2LS = { 2, 2, &array_LS[0] };
static const operandlist_t list_1LS = { 2, 1, &array_LS[0] };
static const int array_LLLL[4] = { modeform_Load, modeform_Load, modeform_Load, modeform_Load };
static const operandlist_t list_LLLL = { 4, 4, &array_LLLL[0] };
static const int array_SL[2] = { modeform_Store, modeform_Load };
static const operandlist_t list_SL = { 2, 4, &array_SL[0] };
static const int array_SS[2] = { modeform_Store, modeform_Store };
static const operandlist_t list_SS = { 2, 4, &array_SS[0] };
static const int array_LLSS[4] = { modeform_Load, modeform_Load, modeform_Store, modeform_Store };
static const operandlist_t list_LLSS = { 4, 4, &array_LLSS[0] };

void Glulxe::init_operands() {
	for (int ix = 0; ix < 0x80; ix++)
		fast_operandlist[ix] = lookup_operandlist(ix);
}

const operandlist_t *Glulxe::lookup_operandlist(uint 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:
		return &list_S;
	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 GLULX_EXTEND_OPERANDS
		GLULX_EXTEND_OPERANDS
#endif /* GLULX_EXTEND_OPERANDS */

	default:
		return nullptr;
	}
}

void Glulxe::parse_operands(oparg_t *args, const operandlist_t *oplist) {
	int ix;
	oparg_t *curarg;
	int numops = oplist->num_ops;
	int argsize = oplist->arg_size;
	uint modeaddr = pc;
	int modeval = 0;

	pc += (numops + 1) / 2;

	for (ix = 0, curarg = args; ix < numops; ix++, curarg++) {
		int mode;
		uint value;
		uint 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 = (int)(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 = (int)(signed char)(Mem1(pc));
				pc++;
				value = (value << 8) | (uint)(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 = (uint)Mem2(pc);
				addr += ramstart;
				pc += 2;
				goto MainMemAddr;

			case 13: /* main memory RAM, one-byte address */
				addr = (uint)(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 = (uint)Mem2(pc);
				pc += 2;
				goto MainMemAddr;

			case 5: /* main memory, one-byte address */
				addr = (uint)(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 = (uint)Mem2(pc);
				pc += 2;
				goto LocalsAddr;

			case 9: /* locals, one-byte address */
				addr = (uint)(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 = (uint)Mem2(pc);
				addr += ramstart;
				pc += 2;
				goto WrMainMemAddr;

			case 13: /* main memory RAM, one-byte address */
				addr = (uint)(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 = (uint)Mem2(pc);
				pc += 2;
				goto WrMainMemAddr;

			case 5: /* main memory, one-byte address */
				addr = (uint)(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 = (uint)Mem2(pc);
				pc += 2;
				goto WrLocalsAddr;

			case 9: /* locals, one-byte address */
				addr = (uint)(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.");
				break;

			default:
				fatal_error("Unknown addressing mode in store operand.");
			}
		}
	}
}

void Glulxe::store_operand(uint desttype, uint destaddr, uint 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 Glulxe::store_operand_s(uint desttype, uint destaddr, uint 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 Glulxe::store_operand_b(uint desttype, uint destaddr, uint 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.");

	}
}

} // End of namespace Glulxe
} // End of namespace Glk