-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstr.c
610 lines (484 loc) · 11.2 KB
/
instr.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
#include "emu.h"
#include "instr.h"
static uint16_t
getinput(uint16_t instr, uint16_t literal)
{
if (literal <= INT16_MAX)
return (literal);
if (literal <= 32775)
return (regs[literal - 32768]);
illins(instr);
}
static void
setreg(uint16_t instr, uint16_t dst, uint16_t src)
{
if (dst < 32768 || dst > 32775)
illins(instr);
dst -= 32768;
regs[dst] = src;
}
static inline uint16_t
modmath(uint16_t val)
{
return (val & 0x7fff);
}
static uint16_t
popval(uint16_t instr)
{
if (stack_depth == 0)
illins(instr);
return (stack[--stack_depth]);
}
static void
pushval(uint16_t val)
{
if (stack_depth == stack_alloc) {
if (stack_alloc == 0)
stack_alloc = 4096 / sizeof(*stack);
else
stack_alloc = stack_alloc * 2;
stack = realloc(stack, stack_alloc * sizeof(*stack));
ASSERT(stack != NULL, "realloc");
}
stack[stack_depth++] = val;
}
void
instr_add(struct instr_decode_common *idc)
{
uint16_t src1, src2, dst;
dst = idc->args[0];
src1 = getinput(idc->instr, idc->args[1]);
src2 = getinput(idc->instr, idc->args[2]);
setreg(idc->instr, dst, modmath(src1 + src2));
}
void
instr_and(struct instr_decode_common *idc)
{
uint16_t src1, src2, dst;
dst = idc->args[0];
src1 = getinput(idc->instr, idc->args[1]);
src2 = getinput(idc->instr, idc->args[2]);
setreg(idc->instr, dst, src1 & src2);
}
void
instr_call(struct instr_decode_common *idc)
{
uint16_t dst;
dst = getinput(idc->instr, idc->args[0]);
pushval(pc + 2);
/* Decrement by size of jmp <a> instruction */
pc = dst - 2;
}
void
instr_eq(struct instr_decode_common *idc)
{
uint16_t src1, src2, dst;
dst = idc->args[0];
src1 = getinput(idc->instr, idc->args[1]);
src2 = getinput(idc->instr, idc->args[2]);
if (src1 == src2)
setreg(idc->instr, dst, 1);
else
setreg(idc->instr, dst, 0);
}
void
instr_gt(struct instr_decode_common *idc)
{
uint16_t src1, src2, dst;
dst = idc->args[0];
src1 = getinput(idc->instr, idc->args[1]);
src2 = getinput(idc->instr, idc->args[2]);
if (src1 > src2)
setreg(idc->instr, dst, 1);
else
setreg(idc->instr, dst, 0);
}
void
instr_halt(struct instr_decode_common *idc __unused)
{
halted = true;
}
void
instr_in(struct instr_decode_common *idc)
{
int rc;
rc = fgetc(infile);
if (rc == EOF) {
fprintf(stderr, "Cannot proceed without input.\n");
halted = true;
}
setreg(idc->instr, idc->args[0], (char)rc);
}
void
instr_jmp(struct instr_decode_common *idc)
{
uint16_t dst;
dst = getinput(idc->instr, idc->args[0]);
/* Decrement by size of jmp <a> instruction */
pc = dst - 2;
}
void
instr_jf(struct instr_decode_common *idc)
{
uint16_t cnd, dst;
cnd = getinput(idc->instr, idc->args[0]);
dst = getinput(idc->instr, idc->args[1]);
/* Decrement by size of jf <a> <b> instruction */
if (cnd == 0)
pc = dst - 3;
}
void
instr_jt(struct instr_decode_common *idc)
{
uint16_t cnd, dst;
cnd = getinput(idc->instr, idc->args[0]);
dst = getinput(idc->instr, idc->args[1]);
/* Decrement by size of jt <a> <b> instruction */
if (cnd != 0)
pc = dst - 3;
}
void
instr_ld(struct instr_decode_common *idc)
{
uint16_t src, dst;
dst = idc->args[0];
src = getinput(idc->instr, idc->args[1]);
setreg(idc->instr, dst, src);
}
void
instr_mod(struct instr_decode_common *idc)
{
uint16_t src1, src2, dst;
dst = idc->args[0];
src1 = getinput(idc->instr, idc->args[1]);
src2 = getinput(idc->instr, idc->args[2]);
setreg(idc->instr, dst, src1 % src2);
}
void
instr_mult(struct instr_decode_common *idc)
{
uint16_t src1, src2, dst;
dst = idc->args[0];
src1 = getinput(idc->instr, idc->args[1]);
src2 = getinput(idc->instr, idc->args[2]);
setreg(idc->instr, dst, modmath(src1 * src2));
}
void
instr_nop(struct instr_decode_common *idc __unused)
{
}
void
instr_not(struct instr_decode_common *idc)
{
uint16_t src, dst;
dst = idc->args[0];
src = getinput(idc->instr, idc->args[1]);
setreg(idc->instr, dst, modmath(~src));
}
void
instr_or(struct instr_decode_common *idc)
{
uint16_t src1, src2, dst;
dst = idc->args[0];
src1 = getinput(idc->instr, idc->args[1]);
src2 = getinput(idc->instr, idc->args[2]);
setreg(idc->instr, dst, src1 | src2);
}
void
instr_out(struct instr_decode_common *idc)
{
char c;
c = getinput(idc->instr, idc->args[0]);
fputc(c, outfile);
}
void
instr_pop(struct instr_decode_common *idc)
{
uint16_t dst;
dst = idc->args[0];
setreg(idc->instr, dst, popval(idc->instr));
}
void
instr_push(struct instr_decode_common *idc)
{
uint16_t src;
src = getinput(idc->instr, idc->args[0]);
pushval(src);
}
void
instr_ret(struct instr_decode_common *idc)
{
uint16_t dst;
if (stack_depth != 0) {
dst = popval(idc->instr);
/* Decrement by size of ret instruction */
pc = dst - 1;
} else
halted = true;
}
void
instr_rmem(struct instr_decode_common *idc)
{
uint16_t src, dst;
dst = idc->args[0];
src = getinput(idc->instr, idc->args[1]);
ASSERT(src < ARRAYLEN(memory), "overflow");
setreg(idc->instr, dst, memory[src]);
}
void
instr_wmem(struct instr_decode_common *idc)
{
uint16_t src, dst;
dst = getinput(idc->instr, idc->args[0]);
src = getinput(idc->instr, idc->args[1]);
ASSERT(dst < ARRAYLEN(memory), "overflow");
memory[dst] = src;
}
void
instr_unimp(struct instr_decode_common *idc __unused)
{
unhandled(idc->instr);
}
static const char *
fmt_dst(char *out, unsigned literal)
{
if (literal <= 32775 && literal > INT16_MAX) {
sprintf(out, "regs[%u]", literal - 32768);
return (out);
}
return ("bogus");
}
static const char *
fmt_src(char *out, unsigned literal)
{
if (literal <= INT16_MAX) {
sprintf(out, "%u", literal);
return (out);
} else if (literal <= 32775) {
sprintf(out, "regs[%u]", literal - 32768);
return (out);
}
return ("bogus");
}
void
trans_add(struct instr_decode_common *idc)
{
char buf1[16], buf2[16], buf3[16];
fprintf(coutfile, "\t%s = MOD(%s + %s);\n",
fmt_dst(buf1, idc->args[0]),
fmt_src(buf2, idc->args[1]),
fmt_src(buf3, idc->args[2]));
}
void
trans_and(struct instr_decode_common *idc)
{
char buf1[16], buf2[16], buf3[16];
fprintf(coutfile, "\t%s = %s & %s;\n",
fmt_dst(buf1, idc->args[0]),
fmt_src(buf2, idc->args[1]),
fmt_src(buf3, idc->args[2]));
}
void
trans_call(struct instr_decode_common *idc)
{
uint16_t literal = idc->args[0];
fprintf(coutfile, "\tpush(%u);\n", pc + 2);
if (literal <= INT16_MAX)
fprintf(coutfile, "\tgoto l%u;\n", literal);
else if (literal <= 32775) {
#if 0
fprintf(coutfile, "\tprintf(\"CALL goto r%%u=%%u=%%p\\n\", %u, regs[%u], jmptable[regs[%u]]);\n",
literal - 32768, literal - 32768, literal - 32768);
#endif
fprintf(coutfile, "\tgoto *jmptable[ regs[%u] ];\n",
literal - 32768);
} else
abort();
}
void
trans_eq(struct instr_decode_common *idc)
{
char buf1[16], buf2[16], buf3[16];
fprintf(coutfile, "\t%s = (%s == %s);\n",
fmt_dst(buf1, idc->args[0]),
fmt_src(buf2, idc->args[1]),
fmt_src(buf3, idc->args[2]));
}
void
trans_gt(struct instr_decode_common *idc)
{
char buf1[16], buf2[16], buf3[16];
fprintf(coutfile, "\t%s = (%s > %s);\n",
fmt_dst(buf1, idc->args[0]),
fmt_src(buf2, idc->args[1]),
fmt_src(buf3, idc->args[2]));
}
void
trans_halt(struct instr_decode_common *idc)
{
char buf1[16], buf2[16], buf3[16];
(void)idc;
fprintf(coutfile, "\thalted = true;\n"
"\texit(2);\n");
}
void
trans_in(struct instr_decode_common *idc)
{
char buf1[16];
fprintf(coutfile,
"\ttmp = fgetc(stdin);\n"
"\tif (tmp == EOF) {\n"
"\t\tprintf(\"EOF\\n\");\n"
"\t\tabort();\n"
"\t}\n");
fprintf(coutfile, "\t%s = tmp;\n", fmt_dst(buf1, idc->args[0]));
}
void
trans_jmp(struct instr_decode_common *idc)
{
uint16_t literal = idc->args[0];
if (literal <= INT16_MAX)
fprintf(coutfile, "\tgoto l%u;\n", literal);
else if (literal <= 32775) {
#if 0
fprintf(coutfile, "\tprintf(\"JMP goto r%%u=%%u=%%p\\n\", %u, regs[%u], jmptable[regs[%u]]);\n",
literal - 32768, literal - 32768, literal - 32768);
#endif
fprintf(coutfile, "\tgoto *jmptable[ regs[%u] ];\n",
literal - 32768);
} else
abort();
}
void
trans_jf(struct instr_decode_common *idc)
{
uint16_t literal;
char buf1[16];
fprintf(coutfile, "\tif (%s == 0)\n", fmt_src(buf1, idc->args[0]));
literal = idc->args[1];
if (literal <= INT16_MAX)
fprintf(coutfile, "\t\tgoto l%u;\n", literal);
else if (literal <= 32775) {
#if 0
fprintf(coutfile, "\tprintf(\"JF goto r%%u=%%u=%%p\\n\", %u, regs[%u], jmptable[regs[%u]]);\n",
literal - 32768, literal - 32768, literal - 32768);
#endif
fprintf(coutfile, "\t\tgoto *jmptable[ regs[%u] ];\n",
literal - 32768);
} else
abort();
}
void
trans_jt(struct instr_decode_common *idc)
{
uint16_t literal;
char buf1[16];
fprintf(coutfile, "\tif (%s != 0)\n", fmt_src(buf1, idc->args[0]));
literal = idc->args[1];
if (literal <= INT16_MAX)
fprintf(coutfile, "\t\tgoto l%u;\n", literal);
else if (literal <= 32775) {
#if 0
fprintf(coutfile, "\tprintf(\"JT goto r%%u=%%u=%%p\\n\", %u, regs[%u], jmptable[regs[%u]]);\n",
literal - 32768, literal - 32768, literal - 32768);
#endif
fprintf(coutfile, "\t\tgoto *jmptable[ regs[%u] ];\n",
literal - 32768);
} else
abort();
}
void
trans_ld(struct instr_decode_common *idc)
{
char buf1[16], buf2[16];
fprintf(coutfile, "\t%s = %s;\n",
fmt_dst(buf1, idc->args[0]),
fmt_src(buf2, idc->args[1]));
}
void
trans_mod(struct instr_decode_common *idc)
{
char buf1[16], buf2[16], buf3[16];
fprintf(coutfile, "\t%s = %s %% %s;\n",
fmt_dst(buf1, idc->args[0]),
fmt_src(buf2, idc->args[1]),
fmt_src(buf3, idc->args[2]));
}
void
trans_mult(struct instr_decode_common *idc)
{
char buf1[16], buf2[16], buf3[16];
fprintf(coutfile, "\t%s = MOD(%s * %s);\n",
fmt_dst(buf1, idc->args[0]),
fmt_src(buf2, idc->args[1]),
fmt_src(buf3, idc->args[2]));
}
void
trans_nop(struct instr_decode_common *idc)
{
(void)idc;
fprintf(coutfile, "\t/* nop */do { } while (0);\n");
}
void
trans_not(struct instr_decode_common *idc)
{
char buf1[16], buf2[16];
fprintf(coutfile, "\t%s = MOD(~%s);\n",
fmt_dst(buf1, idc->args[0]),
fmt_src(buf2, idc->args[1]));
}
void
trans_or(struct instr_decode_common *idc)
{
char buf1[16], buf2[16], buf3[16];
fprintf(coutfile, "\t%s = %s | %s;\n",
fmt_dst(buf1, idc->args[0]),
fmt_src(buf2, idc->args[1]),
fmt_src(buf3, idc->args[2]));
}
void
trans_out(struct instr_decode_common *idc)
{
char buf1[16];
fprintf(coutfile, "\tfputc((char)%s, stdout);\n",
fmt_src(buf1, idc->args[0]));
}
void
trans_pop(struct instr_decode_common *idc)
{
char buf1[16];
fprintf(coutfile, "\t%s = pop();\n", fmt_dst(buf1, idc->args[0]));
}
void
trans_push(struct instr_decode_common *idc)
{
char buf1[16];
fprintf(coutfile, "\tpush(%s);\n", fmt_src(buf1, idc->args[0]));
}
void
trans_ret(struct instr_decode_common *idc)
{
char buf1[16];
(void)idc;
fprintf(coutfile, "\ttmp = pop();\n");
#if 0
fprintf(coutfile, "\tprintf(\"RET goto %%u=%%p\\n\", tmp, jmptable[tmp]);\n");
#endif
fprintf(coutfile, "\tgoto *jmptable[ tmp ];\n");
}
void
trans_rmem(struct instr_decode_common *idc)
{
char buf1[16], buf2[16];
fprintf(coutfile, "\t%s = memory[%s];\n",
fmt_dst(buf1, idc->args[0]),
fmt_src(buf2, idc->args[1]));
}
void
trans_wmem(struct instr_decode_common *idc)
{
char buf1[16], buf2[16];
fprintf(coutfile, "\tmemory[%s] = %s;\n",
fmt_src(buf1, idc->args[0]),
fmt_src(buf2, idc->args[1]));
}