-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstackvm.c
1374 lines (1191 loc) · 31.1 KB
/
stackvm.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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* stackvm.c - small demonstration language */
/* PUBLIC DOMAIN - Jon Mayo
* original: June 23, 2011
* updated: November 10, 2019 */
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <math.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stackvm.h"
#if 0 /* useful for debugging */
#include "hexdump.c"
#endif
int stackvm_verbose = 0;
/* logging macros */
#define error(format, ...) fprintf(stderr, "ERROR:%s():%d:" format, __func__, __LINE__, ## __VA_ARGS__)
#define warn(format, ...) fprintf(stderr, "WARN:" format, ## __VA_ARGS__)
#define info(format, ...) do { if (stackvm_verbose > 0) fprintf(stderr, "INFO:" format, ## __VA_ARGS__); } while(0)
#ifdef DEBUG_ENABLED
#define debug(format, ...) fprintf(stderr, "DEBUG:%s():%d:" format, __func__, __LINE__, ## __VA_ARGS__)
#define trace(format, ...) fprintf(stderr, "TRACE:%s():%d:" format, __func__, __LINE__, ## __VA_ARGS__)
#else
#define debug(format, ...) /* disabled debug messages */
#define trace(format, ...) /* disabled trace messages */
#endif
#define vm_error_set(vm, flag) do { \
trace("set error:%#x\n", (flag)); \
(vm)->status |= (flag); \
} while(0)
/* #define strcasecmp stricmp */
#define ARRAY_SIZE(a) (sizeof(a) / sizeof *(a))
#define VM_STACK_SIZE 1024
#define PROGRAM_STACK_SIZE 0x10000
#define MAX_VMMAIN_ARGS 13
/* expanded instruction */
struct vm_op {
int op;
int param;
};
struct vm;
/* environment holds infomation common to multiple VMs */
struct vm_env {
unsigned nr_syscalls;
void (**syscalls)(struct vm *vm);
};
struct vm {
const struct vm_env *env;
void *extra;
int yield; /* set if we run_slice should yield after system calls */
/* code is a read-only area for instructions */
struct vm_op *code;
size_t code_len;
size_t code_mask;
/* heap is the RAM area for data */
union {
vmword_t *words;
uint16_t *halfs;
uint8_t *bytes;
} heap;
size_t heap_len;
size_t heap_mask;
int status; /* stop loop when non-zero */
vmword_t pc; /* program counter */
vmword_t psp; /* program stack pointer */
vmword_t stack_bottom; /* end of the program stack */ // TODO: rename this
vmword_t stack[VM_STACK_SIZE] __attribute__ ((aligned (__BIGGEST_ALIGNMENT__))); /* op stack */
unsigned op_stack;
/* bootstrap and input parameters */
char *vm_filename;
};
static char *opcode_to_name[] = {
"UNDEF", "IGNORE", "BREAK", "ENTER",
"LEAVE", "CALL", "PUSH", "POP",
"CONST", "LOCAL", "JUMP", "EQ",
"NE", "LTI", "LEI", "GTI",
"GEI", "LTU", "LEU", "GTU",
"GEU", "EQF", "NEF", "LTF",
"LEF", "GTF", "GEF", "LOAD1",
"LOAD2", "LOAD4", "STORE1", "STORE2",
"STORE4", "ARG", "BLOCK_COPY", "SEX8",
"SEX16", "NEGI", "ADD", "SUB",
"DIVI", "DIVU", "MODI", "MODU",
"MULI", "MULU", "BAND", "BOR",
"BXOR", "BCOM", "LSH", "RSHI",
"RSHU", "NEGF", "ADDF", "SUBF",
"DIVF", "MULF", "CVIF", "CVFI",
};
struct vm_env *vm_env_new(unsigned nr_syscalls)
{
struct vm_env *env = calloc(1, sizeof(*env));
assert(env != NULL);
env->nr_syscalls = nr_syscalls;
env->syscalls = calloc(sizeof(*env->syscalls), nr_syscalls);
assert(env->syscalls != NULL);
return env;
}
int vm_env_register(struct vm_env *env, int syscall_num, void (*sc)(struct vm *vm))
{
assert(syscall_num < 0);
unsigned ofs = -1 - syscall_num;
if (ofs >= env->nr_syscalls)
return -1;
env->syscalls[ofs] = sc;
return 0;
}
static inline vmword_t dread4(struct vm *vm, vmword_t ofs);
static void vm_enter(struct vm *vm, unsigned local_size)
{
vm->psp -= local_size;
// TODO: check bounds
}
/* return 0 if returning normally, or 1 if from a syscall */
static int vm_leave(struct vm *vm, unsigned local_size)
{
vm->psp += local_size; /* remove the frame added by ENTER */
vmword_t psp = vm->psp;
vmword_t pc = dread4(vm, psp);
trace("LEAVE to %d (%#x) from %d (%#x)\n", pc, pc, vm->pc, vm->pc);
trace("LEAVE stack=%d\n", vm->op_stack);
assert(pc != 0xdeadbeef);
vm->pc = pc;
if ((int)pc == -1) {
info("%s:finished or returning to call\n", vm->vm_filename);
return 1;
}
return 0;
}
static void opush(struct vm *vm, vmword_t val);
static int vm_env_call(const struct vm_env *env, int syscall_num, struct vm *vm)
{
fprintf(stderr, "======== VM Call #%d : Start ========\n",
-1 - syscall_num);
debug("pc=%d (%#x) psp=%d (%#x)\n", vm->pc, vm->pc, vm->psp, vm->psp);
assert(syscall_num < 0);
unsigned ofs = -1 - syscall_num;
if (ofs >= env->nr_syscalls)
goto out_error;
void (*sc)(struct vm *vm) = env->syscalls[ofs];
if (!sc) // TODO: catch this as an error
goto out_error;
unsigned old_stack = vm->op_stack;
unsigned local_size = 0;
vm_enter(vm, local_size);
sc(vm); // TODO: check for errors
if (vm->status)
goto out_error;
if (vm->op_stack != old_stack + 1) {
trace("syscall did not leave correct amount of data on stack. old=%d new=%d\n",
old_stack, vm->op_stack);
vm->op_stack = old_stack;
opush(vm, 0);
// TODO: set an error flag instead of trying to fix it.
}
#if 0
vm_leave(vm, local_size); /* I think it should be this */
// TODO: maybe we could do the equivalent of ENTER and use a local variable to old the real PC
// then restore that PC here.
#else
vm_leave(vm, 0); // broken
#endif
fprintf(stderr, "======== VM Call #%d : Success ========\n", ofs);
debug("pc=%d (%#x) psp=%d (%#x)\n", vm->pc, vm->pc, vm->psp, vm->psp);
return 0;
out_error:
fprintf(stderr, "======== VM Call #%d : Error ========\n", ofs);
debug("pc=%d (%#x) psp=%d (%#x)\n", vm->pc, vm->pc, vm->psp, vm->psp);
return -1;
}
/* returns number of bytes an opcode will consume.
* 0 if an unknown or illegal opcode. */
static unsigned opcode_length(unsigned char op)
{
unsigned len = 1;
switch (op) {
case 0x00: /* UNDEF*/
case 0x01: /* IGNORE */
case 0x02: /* BREAK */
break;
case 0x03: /* ENTER */
case 0x04: /* LEAVE */
len = 5;
break;
case 0x05: /* CALL */
case 0x06: /* PUSH */
case 0x07: /* POP */
break;
case 0x08: /* CONST x */
case 0x09: /* LOCAL x */
len = 5;
break;
case 0x0a: /* JUMP */
break;
case 0x0b: /* EQ x */
case 0x0c: /* NE x */
case 0x0d: /* LTI x */
case 0x0e: /* LEI x */
case 0x0f: /* GTI x */
case 0x10: /* GEI x */
case 0x11: /* LTU x */
case 0x12: /* LEU x */
case 0x13: /* GTU x */
case 0x14: /* GEU x */
case 0x15: /* EQF x */
case 0x16: /* NEF x */
case 0x17: /* LTF x */
case 0x18: /* LEF x */
case 0x19: /* GTF x */
case 0x1a: /* GEF x */
len = 5;
break;
case 0x1b: /* LOAD1 */
case 0x1c: /* LOAD2 */
case 0x1d: /* LOAD4 */
case 0x1e: /* STORE1 */
case 0x1f: /* STORE2 */
case 0x20: /* STORE4 */
break;
case 0x21: /* ARG x */
len = 2;
break;
case 0x22: /* BLOCK_COPY x */
case 0x23: /* SEX8 */
case 0x24: /* SEX16 */
case 0x25: /* NEGI */
case 0x26: /* ADD */
case 0x27: /* SUB */
case 0x28: /* DIVI */
case 0x29: /* DIVU */
case 0x2a: /* MODI */
case 0x2b: /* MODU */
case 0x2c: /* MULI */
case 0x2d: /* MULU */
case 0x2e: /* BAND */
case 0x2f: /* BOR */
case 0x30: /* BXOR */
case 0x31: /* BCOM */
case 0x32: /* LSH */
case 0x33: /* RSHI */
case 0x34: /* RSHU */
case 0x35: /* NEGF */
case 0x36: /* ADDF */
case 0x37: /* SUBF */
case 0x38: /* DIVF */
case 0x39: /* MULF */
case 0x3a: /* CVIF */
case 0x3b: /* CVFI */
break;
default:
return 0; /* failure */
}
return len;
}
/* returns the count of instructions.
* returns 0 on error or if there are no complete instructions. */
static unsigned count_instructions(const unsigned char *opbytes, size_t oplen)
{
unsigned total = 0;
while (oplen > 0) {
// trace("opbytes=%p oplen=%zd total=%d\n", opbytes, oplen, total);
unsigned result = opcode_length(*opbytes);
if (result > oplen)
return 0; /* truncated instruction */
if (!result)
return 0; /* illegal instruction */
assert(result <= 5);
opbytes += result;
oplen -= result;
total++;
}
trace("total=%d\n", total);
return total;
}
static const char *disassemble_opcode(const struct vm_op *op)
{
static char buf[256];
unsigned b = op->op;
if (b < ARRAY_SIZE(opcode_to_name)) {
if (opcode_length(b) > 1) {
unsigned p = op->param;
snprintf(buf, sizeof(buf), "%s %d [0x%02x %#x]",
opcode_to_name[b], p, b, p);
} else {
snprintf(buf, sizeof(buf), "%s [0x%02x]",
opcode_to_name[b], b);
}
} else {
snprintf(buf, sizeof(buf), "0x%02hhx", b & 255);
}
return buf;
}
static void disassemble(FILE *out, const struct vm_op *ops, size_t ops_len, vmword_t baseaddr)
{
unsigned i;
fprintf(out, "---8<--- start of disassembly (len=%zd) ---8<---\n", ops_len);
for (i = 0; i < ops_len; i++)
fprintf(out, "%06x: %s\n",
baseaddr + i, disassemble_opcode(ops + i));
fprintf(out, "---8<--- end of disassembly ---8<---\n");
}
/* round up len to next power of two minus one */
static size_t make_mask(size_t len)
{
size_t ret = 0;
while (ret + 1 < len)
ret = (ret << 1) | 1;
return ret;
}
static inline int _check_code_bounds(const char *func, unsigned line, struct vm *vm, vmword_t ofs)
{
if (ofs & ~vm->code_mask) {
error("%s():%d:ofs=%d (%#x)\n", func, line, ofs, ofs);
vm_error_set(vm, VM_ERROR_OUT_OF_BOUNDS);
return -1;
}
return 0;
}
#define check_code_bounds(vm, ofs) do { \
if (ofs & ~vm->code_mask) { \
vm_error_set(vm, VM_ERROR_OUT_OF_BOUNDS); \
} } while (0)
static inline int check_data_bounds(struct vm *vm, vmword_t ofs)
{
if (ofs & ~vm->heap_mask) {
vm_error_set(vm, VM_ERROR_OUT_OF_BOUNDS);
return -1;
}
return 0;
}
static inline int check_stack_bounds(struct vm *vm)
{
if (vm->psp < vm->stack_bottom)
return -1;
return check_data_bounds(vm, vm->psp);
}
/* push a value onto the op stack */
static void opush(struct vm *vm, vmword_t val)
{
trace("%s:PUSH %d\n", vm->vm_filename, val);
if (vm->op_stack < ARRAY_SIZE(vm->stack))
vm->stack[vm->op_stack++] = val;
else
vm_error_set(vm, VM_ERROR_STACK_OVERFLOW);
}
#if 0
/* peek a value from the op stack
* negative numbers start from most recent entry. (-1 = newest)
* positive numbers start from oldest entry on stack. (0 = oldest) */
static vmword_t opeek(struct vm *vm, int index)
{
unsigned ofs = index < 0 ? vm->op_stack + index : (unsigned)index;
if (ofs < vm->op_stack)
return vm->stack[ofs];
vm_error_set(vm, VM_ERROR_STACK_UNDERFLOW);
return 0xdeadbeef;
}
#endif
/* pop a value from the op stack */
static vmword_t opop(struct vm *vm)
{
if (vm->op_stack) {
vmword_t val = vm->stack[--vm->op_stack];
trace("%s:POP %d\n", vm->vm_filename, val);
return val;
}
vm_error_set(vm, VM_ERROR_STACK_UNDERFLOW);
return 0xdeadbeef;
}
/* push a value onto the op stack */
static void opushf(struct vm *vm, vmsingle_t val)
{
static_assert(sizeof(val) == sizeof(vmword_t), "vmword_t and vmsingle_t must be of equal size");
if (vm->op_stack < ARRAY_SIZE(vm->stack))
memcpy(&vm->stack[vm->op_stack++], &val, sizeof(val));
else
vm_error_set(vm, VM_ERROR_STACK_OVERFLOW);
}
/* pop a value from the op stack */
static vmsingle_t opopf(struct vm *vm)
{
if (vm->op_stack)
return *(vmsingle_t*)(vm->stack + --vm->op_stack);
vm_error_set(vm, VM_ERROR_STACK_UNDERFLOW);
return NAN;
}
static inline void dwrite4(struct vm *vm, vmword_t ofs, vmword_t val)
{
trace("%s:write %d (%#x) : %d\n", vm->vm_filename, ofs, ofs, val);
if (check_data_bounds(vm, ofs) || check_data_bounds(vm, ofs + 3))
return;
if (ofs & 3) {
vm_error_set(vm, VM_ERROR_UNALIGNED);
return;
}
vm->heap.words[ofs >> 2] = val;
}
static inline void dwrite2(struct vm *vm, vmword_t ofs, uint16_t val)
{
trace("%s:write %d (%#x) : %d\n", vm->vm_filename, ofs, ofs, val);
if (check_data_bounds(vm, ofs) || check_data_bounds(vm, ofs + 1))
return;
if (ofs & 1) {
vm_error_set(vm, VM_ERROR_UNALIGNED);
return;
}
vm->heap.halfs[ofs >> 1] = val;
}
static inline void dwrite1(struct vm *vm, vmword_t ofs, uint8_t val)
{
check_data_bounds(vm, ofs);
vm->heap.bytes[ofs] = val;
}
static inline vmword_t dread4(struct vm *vm, vmword_t ofs)
{
// TODO: use one function to check range
if (check_data_bounds(vm, ofs) || check_data_bounds(vm, ofs + 3)) {
vm_error_set(vm, VM_ERROR_OUT_OF_BOUNDS);
return 0xdeadbeef;
}
if (ofs & 3) {
vm_error_set(vm, VM_ERROR_UNALIGNED);
return 0xdeadbeef;
}
vmword_t val = vm->heap.words[ofs >> 2];
// trace("%s:read %d (%#x) : %d\n", vm->vm_filename, ofs, ofs, val);
return val;
}
static inline uint16_t dread2(struct vm *vm, vmword_t ofs)
{
// TODO: use one function to check range
if (check_data_bounds(vm, ofs) || check_data_bounds(vm, ofs + 1)) {
vm_error_set(vm, VM_ERROR_OUT_OF_BOUNDS);
return 0xdead;
}
if (ofs & 1) {
vm_error_set(vm, VM_ERROR_UNALIGNED);
return 0xbeef;
}
return vm->heap.halfs[ofs >> 1];
}
static inline uint8_t *dmemptr(struct vm *vm, vmword_t ofs, size_t len)
{
if (check_data_bounds(vm, ofs) ||
(len > 0 && check_data_bounds(vm, ofs + len - 1))) {
vm_error_set(vm, VM_ERROR_OUT_OF_BOUNDS);
error("%s:ofs=%#x len=%d\n", vm->vm_filename, ofs, (int)len);
return NULL;
}
return &vm->heap.bytes[ofs];
}
static inline uint8_t dread1(struct vm *vm, vmword_t ofs)
{
if (check_data_bounds(vm, ofs)) {
vm_error_set(vm, VM_ERROR_OUT_OF_BOUNDS);
return 0xde;
}
return vm->heap.bytes[ofs];
}
#if 0
/* write value to program stack */
static void ppoke(struct vm *vm, int index, vmword_t val)
{
// unsigned ofs = vm->psp - index - 4; // TODO: is this correct?
unsigned ofs = vm->psp - index; // TODO: is this correct?
if (check_data_bounds(vm, ofs))
return;
trace("%s:write %d (%#x) : %d\n", vm->vm_filename, ofs, ofs, val);
dwrite4(vm, ofs, val);
}
#endif
void vm_disassemble(const struct vm *vm)
{
disassemble(stdout, vm->code, vm->code_len, 0);
}
#if 0
static void vm_stacktrace(const struct vm *vm)
{
abort(); // TODO: implement this
}
#endif
/* return 1 if finished (re-entrant), 0 if not finished, and -1 on error. */
int vm_run_slice(struct vm *vm)
{
vmword_t a; /* scratch area */
vmword_t b; /* scratch area */
vmsingle_t af; /* scratch area */
vmsingle_t bf; /* scratch area */
int e;
debug("code_mask=0x%08zx code_len=0x%08zx\n",
vm->code_mask, vm->code_len);
debug("heap_mask=0x%08zx heap_len=0x%08zx\n",
vm->heap_mask, vm->heap_len);
/* the interpreter should only use _mask, not _len for checks */
assert(vm->code_mask == make_mask(vm->code_len));
assert(vm->heap_mask == make_mask(vm->heap_len));
while (!vm->status && !_check_code_bounds(__func__, __LINE__, vm, vm->pc)) {
struct vm_op *op = &vm->code[vm->pc++];
#ifdef DEBUG_ENABLED
{ /* debug only */
vmword_t top = ~0;
if (vm->op_stack)
top = vm->stack[vm->op_stack - 1];
debug("PC:pc=%d (%#x) op=%s top=%d (%#x) psp=%d (%#x)\n",
vm->pc - 1, vm->pc - 1, disassemble_opcode(op),
top, top, vm->psp, vm->psp);
}
#endif
switch (op->op) {
;
case 0x00: /* UNDEF*/
break;
case 0x01: /* IGNORE */
break;
case 0x02: /* BREAK */
abort(); // TODO: implement a debugging callback
break;
case 0x03: /* ENTER - increase program stack by amount */
vm->psp -= op->param;
break;
case 0x04: /* LEAVE - shrink program stack by amount */
if (vm_leave(vm, op->param)) {
e = 1; /* finished - results on stack */
goto out;
}
break;
case 0x05: /* CALL */
dwrite4(vm, vm->psp + 4, vm->psp); /* save the old SP */
dwrite4(vm, vm->psp, vm->pc); /* save the old PC */
a = opop(vm);
if ((int32_t)a < 0) {
vmword_t old_pc = vm->pc;
// TODO: the original would store as byte offset, not instruction index
// dwrite4(vm, vm->psp + 4, -1 - vm->pc); /* save the old PC */
// TODO: do system call if program counter is negative
vm->pc = 0xdeadbeef; /* system call better clean this up */
if (vm->env) {
vm->yield = 0;
if (vm_env_call(vm->env, a, vm))
vm_error_set(vm, VM_ERROR_BAD_SYSCALL);
if (vm->yield) {
e = 0; /* not finished - yielding */
goto out;
}
} else {
// TODO: catch this as an error
error("%s:environment not set during system call (pc=%#x call=%d)\n",
vm->vm_filename, old_pc, (int)a);
vm_error_set(vm, VM_ERROR_BAD_ENVIRONMENT);
}
info("**** %s:restoring PC(%#x) to %#x\n",
vm->vm_filename, vm->pc, old_pc);
vm->pc = old_pc;
} else {
vm->pc = a;
check_code_bounds(vm, vm->pc);
}
break;
case 0x06: /* PUSH - push a 0 onto data stack */
opush(vm, 0);
break;
case 0x07: /* POP - remove an item from data stack */
opop(vm);
break;
case 0x08: /* CONST x */
opush(vm, op->param);
break;
case 0x09: /* LOCAL x - get address of local */
a = vm->psp + op->param;
opush(vm, a);
break;
case 0x0a: /* JUMP */
vm->pc = opop(vm);
check_code_bounds(vm, vm->pc);
break;
case 0x0b: /* EQ x */
a = opop(vm);
b = opop(vm);
if (b == a)
vm->pc = op->param;
break;
case 0x0c: /* NE x */
a = opop(vm);
b = opop(vm);
if (b != a)
vm->pc = op->param;
break;
case 0x0d: /* LTI x */
a = opop(vm);
b = opop(vm);
if ((int)b < (int)a)
vm->pc = op->param;
break;
case 0x0e: /* LEI x */
a = opop(vm);
b = opop(vm);
if ((int)b <= (int)a)
vm->pc = op->param;
break;
case 0x0f: /* GTI x */
a = opop(vm);
b = opop(vm);
if ((int)b > (int)a)
vm->pc = op->param;
break;
case 0x10: /* GEI x */
a = opop(vm);
b = opop(vm);
if ((int)b >= (int)a)
vm->pc = op->param;
break;
case 0x11: /* LTU x */
a = opop(vm);
b = opop(vm);
if (b < a)
vm->pc = op->param;
break;
case 0x12: /* LEU x */
a = opop(vm);
b = opop(vm);
if (b <= a)
vm->pc = op->param;
break;
case 0x13: /* GTU x */
a = opop(vm);
b = opop(vm);
if (b > a)
vm->pc = op->param;
break;
case 0x14: /* GEU x */
a = opop(vm);
b = opop(vm);
if (b >= a)
vm->pc = op->param;
break;
case 0x15: /* EQF x */
af = opopf(vm);
bf = opopf(vm);
if (bf == af)
vm->pc = op->param;
break;
case 0x16: /* NEF x */
af = opopf(vm);
bf = opopf(vm);
if (bf != af)
vm->pc = op->param;
break;
case 0x17: /* LTF x */
af = opopf(vm);
bf = opopf(vm);
if (bf < af)
vm->pc = op->param;
break;
case 0x18: /* LEF x */
af = opopf(vm);
bf = opopf(vm);
if (bf <= af)
vm->pc = op->param;
break;
case 0x19: /* GTF x */
af = opopf(vm);
bf = opopf(vm);
if (bf > af)
vm->pc = op->param;
break;
case 0x1a: /* GEF x */
af = opopf(vm);
bf = opopf(vm);
if (bf >= af)
vm->pc = op->param;
break;
case 0x1b: /* LOAD1 */
a = opop(vm);
b = dread1(vm, a);
opush(vm, b);
break;
case 0x1c: /* LOAD2 */
a = opop(vm);
b = dread2(vm, a);
opush(vm, b);
break;
case 0x1d: /* LOAD4 */
a = opop(vm);
b = dread4(vm, a);
trace("%s:LOAD4 (@%d) : %d\n", vm->vm_filename, a, b);
opush(vm, b);
break;
case 0x1e: /* STORE1 */
a = opop(vm);
b = opop(vm);
dwrite1(vm, b, a);
break;
case 0x1f: /* STORE2 */
a = opop(vm);
b = opop(vm);
dwrite2(vm, b, a);
break;
case 0x20: /* STORE4 */
a = opop(vm);
b = opop(vm);
trace("%s:STORE4 %d (@%d) : %d\n", vm->vm_filename, a, a, b);
dwrite4(vm, b, a);
break;
case 0x21: /* ARG x - write value to program stack */
a = opop(vm);
b = vm->psp + op->param;
trace("%s:ARG %d (@%d) : %d\n", vm->vm_filename, op->param, b, a);
dwrite4(vm, b, a);
break;
case 0x22: /* BLOCK_COPY x - copy x bytes */
a = opop(vm); /* src */
b = opop(vm); /* dest */
#if 0 // TODO: implement this
block_copy(vm, b, a, op->param);
#endif
abort(); // TODO: implement this
break;
case 0x23: /* SEX8 */
a = (int32_t)(int8_t)opop(vm);
opush(vm, a);
break;
case 0x24: /* SEX16 */
a = (int32_t)(int16_t)opop(vm);
opush(vm, a);
break;
case 0x25: /* NEGI */
a = opop(vm);
opush(vm, -a);
break;
case 0x26: /* ADD */
a = opop(vm);
b = opop(vm);
opush(vm, b + a);
break;
case 0x27: /* SUB */
a = opop(vm);
b = opop(vm);
opush(vm, b - a);
break;
case 0x28: /* DIVI */
a = opop(vm);
b = opop(vm);
if (a) // TODO: check for INT_MIN / -1
opush(vm, (int)b / (int)a);
else
vm_error_set(vm, VM_ERROR_MATH_ERROR);
break;
case 0x29: /* DIVU */
a = opop(vm);
b = opop(vm);
if (a)
opush(vm, b / a);
else
vm_error_set(vm, VM_ERROR_MATH_ERROR);
break;
case 0x2a: /* MODI */
a = opop(vm);
b = opop(vm);
if (a) // TODO: check for INT_MIN / -1
opush(vm, (int)b % (int)a);
else
vm_error_set(vm, VM_ERROR_MATH_ERROR);
break;
case 0x2b: /* MODU */
a = opop(vm);
b = opop(vm);
if (a)
opush(vm, b % a);
else
vm_error_set(vm, VM_ERROR_MATH_ERROR);
break;
case 0x2c: /* MULI */
a = opop(vm);
b = opop(vm);
// TODO: check for INT_MIN * -1 errors
opush(vm, b * a);
break;
case 0x2d: /* MULU */
a = opop(vm);
b = opop(vm);
opush(vm, b * a);
break;
case 0x2e: /* BAND */
a = opop(vm);
b = opop(vm);
opush(vm, b & a);
break;
case 0x2f: /* BOR */
a = opop(vm);
b = opop(vm);
opush(vm, b | a);
break;
case 0x30: /* BXOR */
a = opop(vm);
b = opop(vm);
opush(vm, b ^ a);
break;
case 0x31: /* BCOM */
a = opop(vm);
opush(vm, ~a);
break;
case 0x32: /* LSH */
a = opop(vm);
b = opop(vm);
opush(vm, b << a);
break;
case 0x33: /* RSHI */
a = opop(vm);
b = opop(vm);
opush(vm, (int)b >> a);
break;
case 0x34: /* RSHU */
a = opop(vm);
b = opop(vm);
opush(vm, (unsigned)b >> a);
break;
case 0x35: /* NEGF */
af = opopf(vm);
opushf(vm, -af);
break;
case 0x36: /* ADDF */
af = opopf(vm);
bf = opopf(vm);
opushf(vm, bf + af);
break;
case 0x37: /* SUBF */
af = opopf(vm);
bf = opopf(vm);
opushf(vm, bf - af);
break;
case 0x38: /* DIVF */
af = opopf(vm);
bf = opopf(vm);
opushf(vm, bf / af);
break;
case 0x39: /* MULF */
af = opopf(vm);
bf = opopf(vm);
opushf(vm, bf * af);
break;
case 0x3a: /* CVIF */
af = (vmsingle_t)opop(vm);
opushf(vm, af);
break;
case 0x3b: /* CVFI */
a = (vmword_t)opopf(vm);
opush(vm, a);
break;
default:
vm_error_set(vm, VM_ERROR_INVALID_OPCODE);
}
}
// finished: // TODO: LEAVE -1 could jump here instead of using flags
if (vm->status)
error("%s:error 0x%x (pc=0x%x)\n", vm->vm_filename, vm->status, vm->pc);
else
info("%s:not finished!\n", vm->vm_filename);
e = vm->status ? -1 : 0;
out:
trace("%s:run slice e=%d status=%#x\n", vm->vm_filename, e, vm->status);
/* dump some of the stack on error */
if (e < 0) {
error("%s:pstack trace:\n", vm->vm_filename);
debug("pc=%d (%#x) psp=%d (%#x) bottom=%#x op_stk=%d\n",
vm->pc, vm->pc, vm->psp, vm->psp, vm->stack_bottom, vm->op_stack);
if (vm->pc >= vm->stack_bottom) {
warn("PC is in stack region!\n");
}
int i;