-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm_engine.c
672 lines (650 loc) · 22.5 KB
/
vm_engine.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
#include "mcp_forth.h"
#include <stdarg.h>
#define RASSERT(expr, ecode) do { if(!(expr)) return (ecode); } while(0)
#define POP(var) do { int ret; if((ret = pop((var), &c->stack))) return ret; } while(0)
#define PUSH(var) do { int ret; if((ret = push((var), &c->stack))) return ret; } while(0)
#define RETURN_POP(var) do { int ret; if((ret = pop((int *) (var), &c->return_stack))) return ret; } while(0)
#define RETURN_PUSH(var) do { int ret; if((ret = push((int) (var), &c->return_stack))) return ret; } while(0)
#define MAX_CALLBACKS 8
#define CALLBACK_TARGET_NAME(number) callback_target_ ## number
#define CALLBACK_TARGET_DEFINE(number) static int CALLBACK_TARGET_NAME(number)(int arg1, ...) { \
va_list ap; \
va_start(ap, arg1); \
int ret = callback_handle(number, arg1, ap); \
va_end(ap); \
return ret; \
}
union iu {int i; unsigned u;};
typedef struct {
m4_stack_t stack;
m4_stack_t return_stack;
int return_stack_len_base;
uint8_t * memory_base;
uint8_t * memory;
int memory_len;
const m4_runtime_cb_pair_t ** runtime_cbs;
const uint8_t * data_start;
union {int * ip; int i;} * variables_and_constants;
uint8_t * callback_info;
const uint8_t ** callback_word_locations;
const uint8_t * pc;
} ctx_t;
#ifdef M4_NO_TLS
static ctx_t * g_ctx;
#else
static __thread ctx_t * g_ctx;
#endif
static int run_inner(ctx_t * c);
static int pc_step(const uint8_t ** pc_p) {
int ret = m4_num_decode(*pc_p);
*pc_p += m4_num_encoded_size_from_encoded(*pc_p);
return ret;
}
static int pop(int * dst, m4_stack_t * stack) {
if(stack->len <= 0) return M4_STACK_UNDERFLOW_ERROR;
stack->data -= 1;
stack->len -= 1;
*dst = stack->data[0];
return 0;
}
static int push(int src, m4_stack_t * stack) {
if(stack->len >= stack->max) return M4_STACK_OVERFLOW_ERROR;
stack->data[0] = src;
stack->data += 1;
stack->len += 1;
return 0;
}
static int callback_handle(int cb_i, int arg1, va_list ap)
{
ctx_t * c = g_ctx;
uint8_t cb_info = c->callback_info[cb_i];
int n_params = cb_info >> 1;
assert(0 == push(arg1, &c->stack));
for(int i = 1; i < n_params; i++) {
assert(0 == push(va_arg(ap, int), &c->stack));
}
assert(0 == push(c->return_stack_len_base, &c->return_stack));
assert(0 == push((int) NULL, &c->return_stack));
c->return_stack_len_base = c->return_stack.len;
const uint8_t * pc_save = c->pc;
c->pc = c->callback_word_locations[cb_i];
assert(0 == run_inner(c));
c->pc = pc_save;
bool has_return_val = cb_info & 1;
int ret;
if(has_return_val) assert(0 == pop(&ret, &c->stack));
return ret; /* uninitialized is ok */
}
CALLBACK_TARGET_DEFINE(0)
CALLBACK_TARGET_DEFINE(1)
CALLBACK_TARGET_DEFINE(2)
CALLBACK_TARGET_DEFINE(3)
CALLBACK_TARGET_DEFINE(4)
CALLBACK_TARGET_DEFINE(5)
CALLBACK_TARGET_DEFINE(6)
CALLBACK_TARGET_DEFINE(7)
static int (*const callback_targets[MAX_CALLBACKS])(int arg1, ...) = {
CALLBACK_TARGET_NAME(0),
CALLBACK_TARGET_NAME(1),
CALLBACK_TARGET_NAME(2),
CALLBACK_TARGET_NAME(3),
CALLBACK_TARGET_NAME(4),
CALLBACK_TARGET_NAME(5),
CALLBACK_TARGET_NAME(6),
CALLBACK_TARGET_NAME(7),
};
static int run_inner(ctx_t * c) {
while(1) {
const uint8_t * pc_initial = c->pc;
int op = pc_step(&c->pc);
int builtin_num = -1;
if(op >= M4_OPCODE_LAST_ && op <= M4_NUM_MAX_ONE_BYTE) {
builtin_num = op - M4_OPCODE_LAST_;
op = M4_OPCODE_BUILTIN_WORD;
}
switch(op) {
case M4_OPCODE_BUILTIN_WORD:
if(builtin_num == -1) {
builtin_num = pc_step(&c->pc);
}
switch (builtin_num) {
case 0: { /* dup */
int x;
POP(&x);
PUSH(x);
PUSH(x);
break;
}
case 1: { /* - */
int l, r;
POP(&r);
POP(&l);
PUSH(l - r);
break;
}
case 2: { /* > */
int l, r;
POP(&r);
POP(&l);
PUSH(l > r ? -1 : 0);
break;
}
case 3: { /* + */
int l, r;
POP(&r);
POP(&l);
PUSH(l + r);
break;
}
case 4: { /* = */
int l, r;
POP(&r);
POP(&l);
PUSH(l == r ? -1 : 0);
break;
}
case 5: { /* over */
int b, a;
POP(&a);
POP(&b);
PUSH(b);
PUSH(a);
PUSH(b);
break;
}
case 6: { /* mod */
int l, r;
POP(&r);
POP(&l);
PUSH(l % r);
break;
}
case 7: { /* drop */
int x;
POP(&x);
break;
}
case 8: { /* swap */
int b, a;
POP(&a);
POP(&b);
PUSH(a);
PUSH(b);
break;
}
case 9: { /* * */
int l, r;
POP(&r);
POP(&l);
PUSH(l * r);
break;
}
case 10: { /* allot */
int x;
POP(&x);
c->memory += x;
int bytes_remaining = m4_bytes_remaining(c->memory_base, c->memory, c->memory_len);
if(bytes_remaining < 0) return M4_OUT_OF_MEMORY_ERROR;
if(bytes_remaining > c->memory_len) return M4_DATA_SPACE_POINTER_OUT_OF_BOUNDS_ERROR;
break;
}
case 11: { /* 1+ */
int x;
POP(&x);
PUSH(x + 1);
break;
}
case 12: { /* invert */
int x;
POP(&x);
PUSH(~x);
break;
}
case 13: { /* 0= */
int x;
POP(&x);
PUSH(x == 0 ? -1 : 0);
break;
}
case 14: { /* i */
PUSH(c->return_stack.data[-1]);
break;
}
case 15: { /* j */
PUSH(c->return_stack.data[-3]);
break;
}
case 16: { /* 1- */
int x;
POP(&x);
PUSH(x - 1);
break;
}
case 17: { /* rot */
int x1, x2, x3;
POP(&x3);
POP(&x2);
POP(&x1);
PUSH(x2);
PUSH(x3);
PUSH(x1);
break;
}
case 18: { /* pick */
int n;
POP(&n);
if(c->stack.len <= n) {
return M4_STACK_UNDERFLOW_ERROR;
}
PUSH(c->stack.data[-n - 1]);
break;
}
case 19: { /* xor */
int l, r;
POP(&r);
POP(&l);
PUSH(l ^ r);
break;
}
case 20: { /* lshift */
union iu l, r;
POP(&r.i);
POP(&l.i);
PUSH(l.u << r.u);
break;
}
case 21: { /* rshift */
union iu l, r;
POP(&r.i);
POP(&l.i);
PUSH(l.u >> r.u);
break;
}
case 22: { /* < */
int l, r;
POP(&r);
POP(&l);
PUSH(l < r ? -1 : 0);
break;
}
case 23: { /* >= */
int l, r;
POP(&r);
POP(&l);
PUSH(l >= r ? -1 : 0);
break;
}
case 24: { /* <= */
int l, r;
POP(&r);
POP(&l);
PUSH(l <= r ? -1 : 0);
break;
}
case 25: { /* or */
union iu l, r;
POP(&r.i);
POP(&l.i);
PUSH(l.u | r.u);
break;
}
case 26: { /* and */
union iu l, r;
POP(&r.i);
POP(&l.i);
PUSH(l.u & r.u);
break;
}
case 27: { /* depth */
PUSH(c->stack.len);
break;
}
case 28: { /* c! */
int chr, addr;
POP(&addr);
POP(&chr);
*(uint8_t *)addr = chr;
break;
}
case 29: { /* c@ */
int addr;
POP(&addr);
PUSH(*(uint8_t *)addr);
break;
}
case 30: { /* 0< */
int x;
POP(&x);
PUSH(x < 0 ? -1 : 0);
break;
}
case 31: { /* ! */
int x, addr;
POP(&addr);
POP(&x);
*(int *)addr = x;
break;
}
case 32: { /* @ */
int addr;
POP(&addr);
PUSH(*(int *)addr);
break;
}
case 33: { /* c, */
if(m4_bytes_remaining(c->memory_base, c->memory, c->memory_len) < 1) {
return M4_OUT_OF_MEMORY_ERROR;
}
int chr;
POP(&chr);
*(c->memory++) = chr;
break;
}
case 34: { /* here */
PUSH((int) c->memory);
break;
}
case 35: { /* max */
int a, b;
POP(&b);
POP(&a);
PUSH(a > b ? a : b);
break;
}
case 36: { /* fill */
int addr, len, chr;
POP(&chr);
POP(&len);
POP(&addr);
memset((void *) addr, chr, len);
break;
}
case 37: { /* 2drop */
int x;
POP(&x);
POP(&x);
break;
}
case 38: { /* move */
int src, dst, len;
POP(&len);
POP(&dst);
POP(&src);
memmove((void *) dst, (const void *) src, len);
break;
}
case 39: { /* execute */
RETURN_PUSH(c->return_stack_len_base);
RETURN_PUSH(c->pc);
c->return_stack_len_base = c->return_stack.len;
int fn;
POP(&fn);
c->pc = (const uint8_t *) fn;
break;
}
case 40: /* align */
c->memory = m4_align(c->memory);
if(m4_bytes_remaining(c->memory_base, c->memory, c->memory_len) < 0) {
return M4_OUT_OF_MEMORY_ERROR;
}
break;
case 41: { /* , */
if(m4_bytes_remaining(c->memory_base, c->memory, c->memory_len) < 4) {
return M4_OUT_OF_MEMORY_ERROR;
}
int x;
POP(&x);
*(int *) c->memory = x;
c->memory += 4;
break;
}
case 42: { /* compare */
int a1, u1, a2, u2;
POP(&u2);
POP(&a2);
POP(&u1);
POP(&a1);
int result = memcmp((void *) a1, (void *) a2, u1 < u2 ? u1 : u2);
if(result == 0) {
PUSH(u1 < u2 ? -1 : u1 > u2 ? 1 : 0);
} else {
PUSH(result < 0 ? -1 : 1);
}
break;
}
case 43: { /* <> */
int l, r;
POP(&r);
POP(&l);
PUSH(l != r ? -1 : 0);
break;
}
case 44: { /* >r */
int x;
POP(&x);
RETURN_PUSH(x);
break;
}
case 45: { /* r> */
int x;
RETURN_POP(&x);
PUSH(x);
break;
}
case 46: { /* 2>r */
int l, r;
POP(&r);
POP(&l);
RETURN_PUSH(l);
RETURN_PUSH(r);
break;
}
case 47: { /* 2r> */
int l, r;
RETURN_POP(&r);
RETURN_POP(&l);
PUSH(l);
PUSH(r);
break;
}
case 48: { /* 2/ */
int x;
POP(&x);
PUSH(x / 2);
break;
}
case 49: { /* 2dup */
int l, r;
POP(&r);
POP(&l);
PUSH(l);
PUSH(r);
PUSH(l);
PUSH(r);
break;
}
case 50: { /* 0<> */
int x;
POP(&x);
PUSH(x != 0 ? -1 : 0);
break;
}
case 51: { /* 0> */
int x;
POP(&x);
PUSH(x > 0 ? -1 : 0);
break;
}
case 52: { /* w@ */
int addr;
POP(&addr);
PUSH(*(uint16_t *)addr);
break;
}
case 53: { /* w! */
int chr, addr;
POP(&addr);
POP(&chr);
*(uint16_t *)addr = chr;
break;
}
case 54: { /* +! */
int n, addr;
POP(&addr);
POP(&n);
*(int *) addr += n;
break;
}
case 55: { /* / */
int l, r;
POP(&r);
POP(&l);
PUSH(l / r);
break;
}
case 56: { /* cells */
int x;
POP(&x);
PUSH(x * 4);
break;
}
default:
assert(0);
}
break;
case M4_OPCODE_DEFINED_WORD: {
RETURN_PUSH(c->return_stack_len_base);
int target_offset = pc_step(&c->pc);
RETURN_PUSH(c->pc);
c->return_stack_len_base = c->return_stack.len;
c->pc = pc_initial + target_offset;
break;
}
case M4_OPCODE_RUNTIME_WORD: {
int runtime_word_i = pc_step(&c->pc);
const m4_runtime_cb_pair_t * cb_pair = c->runtime_cbs[runtime_word_i];
int ret = cb_pair->cb(cb_pair->param, &c->stack);
if(ret) return ret;
break;
}
case M4_OPCODE_PUSH_LITERAL:
PUSH(pc_step(&c->pc));
break;
case M4_OPCODE_EXIT_WORD:
c->return_stack.data -= c->return_stack.len - c->return_stack_len_base;
c->return_stack.len = c->return_stack_len_base;
RETURN_POP(&c->pc);
RETURN_POP(&c->return_stack_len_base);
if(c->pc == NULL) return 0;
break;
case M4_OPCODE_PUSH_DATA_ADDRESS:
PUSH((int) (c->data_start + pc_step(&c->pc)));
break;
case M4_OPCODE_DECLARE_VARIABLE:
c->memory = m4_align(c->memory);
c->variables_and_constants[pc_step(&c->pc)].ip = (int *) c->memory;
c->memory += 4;
if(m4_bytes_remaining(c->memory_base, c->memory, c->memory_len) < 0) {
return M4_OUT_OF_MEMORY_ERROR;
}
break;
case M4_OPCODE_USE_VARIABLE_OR_CONSTANT:
PUSH(c->variables_and_constants[pc_step(&c->pc)].i);
break;
case M4_OPCODE_HALT:
return 0;
case M4_OPCODE_BRANCH_IF_ZERO: {
int cond;
POP(&cond);
int target_offset = pc_step(&c->pc);
if(cond == 0) c->pc = pc_initial + target_offset;
break;
}
case M4_OPCODE_BRANCH:
c->pc = pc_initial + pc_step(&c->pc);
break;
case M4_OPCODE_DO: {
int end, start;
POP(&start);
POP(&end);
RETURN_PUSH(end);
RETURN_PUSH(start);
break;
}
case M4_OPCODE_LOOP: {
int target_offset = pc_step(&c->pc);
int end, i;
RETURN_POP(&i);
RETURN_POP(&end);
i++;
if(i < end) {
c->pc = pc_initial + target_offset;
RETURN_PUSH(end);
RETURN_PUSH(i);
}
break;
}
case M4_OPCODE_PUSH_OFFSET_ADDRESS: {
int offset = pc_step(&c->pc);
PUSH((int) pc_initial + offset);
break;
}
case M4_OPCODE_PUSH_CALLBACK:
PUSH((int) callback_targets[pc_step(&c->pc)]);
break;
case M4_OPCODE_DECLARE_CONSTANT: {
int x;
POP(&x);
c->variables_and_constants[pc_step(&c->pc)].i = x;
break;
}
default:
assert(0);
}
}
}
int m4_vm_engine_run(
const uint8_t * bin,
int bin_len,
uint8_t * memory_start,
int memory_len,
const m4_runtime_cb_array_t ** cb_arrays,
const char ** missing_runtime_word_dst
) {
uint8_t * memory_p = m4_align(memory_start);
ctx_t * c = (ctx_t *) memory_p;
memory_p += sizeof(ctx_t);
if(m4_bytes_remaining(memory_start, memory_p, memory_len) < 0) return M4_OUT_OF_MEMORY_ERROR;
g_ctx = c;
c->stack.data = (int *) memory_p;
memory_p += 100 * 4;
if(m4_bytes_remaining(memory_start, memory_p, memory_len) < 0) return M4_OUT_OF_MEMORY_ERROR;
c->stack.max = 100;
c->stack.len = 0;
c->return_stack.data = (int *) memory_p;
memory_p += 100 * 4;
if(m4_bytes_remaining(memory_start, memory_p, memory_len) < 0) return M4_OUT_OF_MEMORY_ERROR;
c->return_stack.max = 100;
c->return_stack.len = 0;
c->return_stack_len_base = 0;
c->memory_base = memory_start;
c->memory_len = memory_len;
int res = m4_unpack_binary_header(
bin,
bin_len,
memory_p,
m4_bytes_remaining(memory_start, memory_p, memory_len),
cb_arrays,
missing_runtime_word_dst,
MAX_CALLBACKS,
(int ***) &c->variables_and_constants,
&c->runtime_cbs,
&c->data_start,
&c->callback_info,
&c->callback_word_locations,
&c->pc,
&c->memory
);
if(res) return res;
return run_inner(c);
}