-
Notifications
You must be signed in to change notification settings - Fork 36
/
2
280 lines (207 loc) · 6.39 KB
/
2
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <object.h>
#include <assert.h>
#define COMO_COMPILER 1
#include "como_ast.h"
#include "como_stack.h"
#include "como_debug.h"
#include "como_opcode.h"
#include "como_globals.h"
#include "como_parser.h"
#include "como_lexer.h"
#include "como_compiler.h"
#include "como_io.h"
#include "como_object.h"
static ComoFrame *global_frame = NULL;
static ComoOpCode *create_op(unsigned char op, Object *oper)
{
ComoOpCode *ret = malloc(sizeof(ComoOpCode));
ret->op_code = op;
ret->flags = 0;
if(oper != NULL) {
ret->flags |= COMO_OP_CODE_OPERAND_USED;
}
// TODO, line numbers
ret->operand = oper;
return ret;
}
static ComoFrame *create_frame(Object *code, const char *name)
{
size_t i;
ComoFrame *frame = malloc(sizeof(ComoFrame));
frame->cf_sp = 0;
frame->cf_stack_size = 0;
for(i = 0; i < (size_t)COMO_DEFAULT_FRAME_STACKSIZE; i++) {
frame->cf_stack[i] = NULL;
}
frame->cf_symtab = newMap(4);
frame->code = code;
frame->pc = 0;
frame->next = NULL;
frame->namedparameters = newArray(2);
frame->filename = NULL;
frame->call_stack = NULL;
frame->name = newString(name);
frame->caller = NULL;
return frame;
}
static int exception = 0;
static char exmessage[1024];
#define Como_SetError(fmt, ...) do { \
exception = 1; \
memset(exmessage, 0, sizeof(exmessage)); \
sprintf(exmessage, fmt, __VA_ARGS__); \
} while(0)
/* VM main loop */
Object *Como_EvalFrame(ComoFrame *frame)
{
#define O_INCRREF(O) \
O_REFCNT((O))++
#define O_DECREF(O) do { \
if(--O_REFCNT((O)) == 0) { \
objectDestroy((O)); \
} \
} while(0)
#define OPR_DECREF(Opcode) do { \
(Opcode)->flags |= COMO_OP_CODE_OPERAND_FREE; \
objectDestroy((Opcode)->operand); \
(Opcode)->operand = NULL; \
} while(0)
#define TARGET(Name) \
case Name:
#define VM_CONTINUE() \
goto next_opcode
#define VM_DISPATCH() \
break
#define NEXT_OPCODE() \
(ComoOpCode *)O_PTVAL(O_AVAL((frame)->code)->table[(frame)->pc++]);
#define POP() \
frame->cf_stack[--frame->cf_sp]
#define PUSH(Obj) \
frame->cf_stack[frame->cf_sp++] = Obj
#define OP1() \
opcode->operand
Object *retval = NULL;
for(;;)
{
ComoOpCode *opcode;
next_opcode:
opcode = NEXT_OPCODE();
#ifdef COMO_DEBUG
fprintf(stderr, "%s\n", instrstr(opcode));
#endif
switch(opcode->op_code)
{
TARGET(HALT)
{
return retval;
}
TARGET(LOAD_NAME)
{
Object *arg = OP1();
Object *value =
mapSearchEx(frame->cf_symtab, O_SVAL(arg)->value);
if(value == NULL) {
Como_SetError(
"Undefined symbol, %s", O_SVAL(arg)->value
);
} else {
O_INCRREF(value);
PUSH(value);
OPR_DECREF(opcode);
}
VM_DISPATCH();
}
TARGET(LOAD_CONST)
{
Object *arg = OP1();
PUSH(arg);
O_INCRREF(arg);
VM_DISPATCH();
}
TARGET(STORE_NAME)
{
Object *value = POP();
Object *name = OP1();
mapInsertEx(frame->cf_symtab, O_SVAL(name)->value, value);
O_INCRREF(value);
OPR_DECREF(opcode);
VM_DISPATCH();
}
TARGET(IADD)
{
Object *right = POP();
Object *left = POP();
if(Obj_CheckExact(right, IS_LONG) &&
Obj_CheckExact(left, IS_LONG))
{
long result = O_LVAL(left) + O_LVAL(right);
O_DECREF(left);
O_DECREF(right);
PUSH(newLong(result));
}
else
{
/* Temp char buffers */
char *_s1 = objectToString(left);
char *_s2 = objectToString(right);
Object *s1 = newString(_s1);
Object *s2 = newString(_s2);
Object *result = stringCat(s1, s2);
free(_s1);
free(_s2);
O_DECREF(s1);
O_DECREF(s2);
PUSH(result);
}
VM_DISPATCH();
}
}
if(exception) {
como_error_noreturn(frame, exmessage);
}
}
return retval;
}
static void destroy_frame(ComoFrame *frame)
{
uint32_t i;
objectDestroy(frame->lineno);
objectDestroy(frame->filename);
objectDestroy(frame->cf_symtab);
objectDestroy(frame->namedparameters);
objectDestroy(frame->name);
for(i = 0; i < O_AVAL(frame->code)->size; i++)
{
ComoOpCode *op = (ComoOpCode *)O_PTVAL((O_AVAL(frame->code))->table[i]);
if((op->flags & COMO_OP_CODE_OPERAND_USED) &&
!(op->flags & COMO_OP_CODE_OPERAND_FREE))
{
objectDestroy(op->operand);
}
free(op);
}
objectDestroy(frame->code);
fprintf(stdout, " --- Stack Pointer: %d\n", (int)frame->cf_sp);
fprintf(stdout, " --- Program Counter: %d\n", (int)frame->pc);
free(frame);
}
static void _como_compile_ast(ast_node *p, const char *filename, int dump_asm) {
Object *main_code = newArray(4);
global_frame = create_frame(main_code, "__main__");
global_frame->lineno = newLong(0L);
global_frame->filename = newString(filename);
como_compile(p, global_frame);
arrayPushEx(main_code, newPointer((void *)create_op(HALT, NULL)));
if(!dump_asm)
(void)Como_EvalFrame(global_frame);
destroy_frame(global_frame);
}
void como_compile_ast(ast_node *p, const char *filename) {
_como_compile_ast(p, filename, 0);
}
char *get_active_file_name(void) {
return "-";
}