-
Notifications
You must be signed in to change notification settings - Fork 2
/
vmdebug.c
92 lines (87 loc) · 2.51 KB
/
vmdebug.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
/*
* SVM Execution Tracing Code
* by Takuo Watanabe
*/
#include <stdio.h>
#include <stdlib.h>
#include "vm.h"
static char *instr_name (Bcode instr) {
switch (instr) {
case i_nop: return "nop";
case i_dup: return "dup";
case i_pop: return "pop";
case i_goto: return "goto";
case i_ifeq: return "ifeq";
case i_ifne: return "ifne";
case i_iflt: return "iflt";
case i_ifle: return "ifle";
case i_ifgt: return "ifgt";
case i_ifge: return "ifge";
case i_giload: return "giload";
case i_gaload: return "gaload";
case i_gistore: return "gistore";
case i_gastore: return "gastore";
case i_iload: return "iload";
case i_aload: return "aload";
case i_istore: return "istore";
case i_astore: return "astore";
case i_bipush: return "bipush";
case i_sipush: return "sipush";
case i_iadd: return "iadd";
case i_isub: return "isub";
case i_imul: return "imul";
case i_idiv: return "idiv";
case i_irem: return "irem";
case i_icall: return "icall";
case i_acall: return "acall";
case i_ticall: return "ticall";
case i_tacall: return "tacall";
case i_sicall: return "sicall";
case i_sacall: return "sacall";
case i_iret: return "iret";
case i_aret: return "aret";
case i_halt: return "halt";
case i_showtop: return "showtop";
default: return "???";
}
}
void dump_vm (long nsteps, Bcode *ip, Bcode code[],
Value *lv, Value *fp, Value *sp, Value *vs_base,
Value gv[], size_t gv_size, vm_mode_t mode) {
long i;
Value *p;
if (mode == vm_normal_mode) return;
/* global variables */
for (i = 0; i < gv_size; i++) {
printf("gv[%ld]=0x%08lx ", i, gv[i]);
if ((i+1) % 4 == 0) printf("\n");
}
if (i % 4 != 0) printf("\n");
/* value stack */
i = 0;
for (p = vs_base - 1; p >= sp; p--) {
printf("vs[%ld]=0x%08lx ", vs_base - 1 - p, *p);
if ((i+1) % 4 == 0) printf("\n");
i++;
}
if (i % 4 != 0) printf("\n");
/* machine state */
printf("lv=%p (%ld), fp=%p (%ld), sp=%p (%ld)\n",
lv, vs_base - lv,
fp, vs_base - fp,
sp, vs_base - sp);
/* next instruction */
printf("nsteps=%ld, ip=%p, pc=%ld, inst=%s\n",
nsteps,
ip,
ip - code,
instr_name(ip[0]));
if (mode == vm_step_mode) {
printf("next? [Y/n]");
switch (getchar()) {
case 'n':
case 'N':
exit(EXIT_SUCCESS);
}
}
}