-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm.c
240 lines (197 loc) · 4.93 KB
/
vm.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
#include "vm.h"
#include "value.h"
#include "values/values.h"
#include "values/process.h"
#include <stdio.h>
#include <stdlib.h>
#define MAX_PROCESSES 64
void debug_print_value(value_t const *val);
static value_t const *nil = (value_t const *)0;
vm_t *vm_create() {
// JWT: This should get moved to a vlisp init at some point
if (nil == 0) {
nil = value_create_nil();
}
vm_t *vm = (vm_t *)malloc(sizeof(vm_t));
vm->processes = (value_t **)malloc(sizeof(value_t *) * MAX_PROCESSES);
for (int i = 0; i < MAX_PROCESSES; i++) {
vm->processes[i] = (value_t *)nil;
}
return vm;
}
int vm_attach_process(vm_t *vm, value_t *proc) {
for (int i = 0; i < MAX_PROCESSES; i++) {
if (vm->processes[i] == nil) {
vm->processes[i] = proc;
return i;
}
}
return -1;
}
void debug_print_elts(value_t const *val) {
if(val == nil) {
return;
}
// Print head of cons
debug_print_value(val->cons[0]);
if(val->cons[1] == nil) {
return;
}
printf(" ");
if(val->cons[1]->type != VT_CONS) {
printf(". ");
debug_print_value(val->cons[1]);
return;
}
debug_print_elts(val->cons[1]);
}
void debug_print_value(value_t const *val) {
switch(val->type) {
case VT_NUMBER:
printf("%d", *(int *)val->data);
break;
case VT_CONS:
printf("(");
debug_print_elts(val);
printf(")");
break;
case VT_STRING:
printf("\"%s\"", (char const *)val->data);
break;
case VT_SYMBOL:
printf("%s", (char const *)val->cons[0]->data);
break;
default:
printf("unknown");
break;
}
}
void debug_print_bytecode(bytecode_t *code) {
switch(code->opcode) {
case OP_PUSH: printf("OP_PUSH: "); break;
case OP_POP: printf("OP_POP: "); break;
case OP_LOAD: printf("OP_LOAD: "); break;
case OP_JMP: printf("OP_JMP: "); break;
case OP_CALL: printf("OP_CALL: "); break;
case OP_RET: printf("OP_RET: "); break;
case OP_NOP: printf("OP_NOP: "); break;
case OP_CONS: printf("OP_CONS: "); break;
case OP_CAR: printf("OP_CAR: "); break;
case OP_CDR: printf("OP_CDR: "); break;
case OP_DUMP: printf("OP_DUMP: "); break;
case OP_EQ: printf("OP_EQ: "); break;
case OP_BIND: printf("OP_BIND: "); break;
case OP_DUP: printf("OP_DUP: "); break;
case OP_ISNULL: printf("OP_ISNULL: "); break;
case OP_ISATOM: printf("OP_ISATOM: "); break;
default: printf("UNKNOWN OPCODE: "); break;
}
debug_print_value(code->value);
printf("\n");
}
void debug_print_stack(process_t *proc) {
printf("sp: %d\n", proc->sp);
int sp = proc->sp;
while(sp--) {
printf("%d] ", sp);
debug_print_value(proc->stack[sp]);
printf("\n");
}
}
value_t *get_cur_proc(vm_t *vm) {
return vm->processes[0];
}
void vm_exec(vm_t *vm) {
value_t const *cdr;
value_t const *car;
value_t const *cons;
value_t const *val1;
value_t const *val2;
while(1) {
value_t *cur_proc = get_cur_proc(vm);
process_t *proc = (process_t *)cur_proc->data;
bytecode_t const *bcp = (bytecode_t const *)proc->bc->data;
bytecode_t bc = bcp[proc->ip++];
printf("ip: %d ] ", proc->ip);
debug_print_bytecode(&bc);
value_t const **stack = proc->stack;
switch(bc.opcode) {
case OP_PUSH:
stack[proc->sp++] = bc.value;
break;
case OP_POP:
proc->sp--;
break;
case OP_JMP:
break;
case OP_CALL:
break;
case OP_RET:
if (proc->bp == -1) {
return;
}
break;
case OP_DUMP:
debug_print_stack(proc);
break;
case OP_CONS:
proc->sp--;
cdr = stack[proc->sp];
proc->sp--;
car = stack[proc->sp];
stack[proc->sp++] = value_create_cons(car, cdr);
break;
case OP_CAR:
proc->sp--;
cons = stack[proc->sp];
stack[proc->sp++] = cons->cons[0];
break;
case OP_CDR:
proc->sp--;
cons = stack[proc->sp];
stack[proc->sp++] = cons->cons[1];
break;
case OP_BIND:
val1 = stack[proc->sp - 1];
val2 = stack[proc->sp - 2];
proc->sp -= 2;
// Currently only supporting dynamic binding
{
value_t *sym = (value_t *)val2;
sym->cons[1] = value_create_cons(val1, sym->cons[1]);
}
break;
case OP_LOAD:
// Dynamic value is stored at the head of the cons[1] list
stack[proc->sp - 1] = stack[proc->sp - 1]->cons[1]->cons[0];
break;
case OP_DUP:
val1 = stack[proc->sp - 1];
stack[proc->sp++] = val1;
break;
case OP_EQ:
val1 = stack[proc->sp - 1];
val2 = stack[proc->sp - 2];
proc->sp -= 2;
stack[proc->sp++] = val1 == val2 ? value_create_number(1) : value_create_number(0);
break;
case OP_ISNULL:
val1 = stack[proc->sp - 1];
if (val1 == nil) {
stack[proc->sp - 1] = process_create_symbol(cur_proc, "#t");
} else {
stack[proc->sp - 1] = nil;
}
break;
case OP_ISATOM:
if (stack[proc->sp - 1]->type == VT_CONS) {
stack[proc->sp - 1] = nil;
} else {
stack[proc->sp - 1] = process_create_symbol(cur_proc, "#t");
}
break;
case OP_NOP:
break;
}
}
}