forked from kspalaiologos/asm2ws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.c
221 lines (213 loc) · 6.97 KB
/
compile.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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <stdint.h>
#include "vector.h"
#include "common.h"
static void append_code(char ** buf, char * format, ...) {
va_list args, args2;
va_start(args, format);
va_copy(args2, args);
uint32_t buflen = *buf ? strlen(*buf) : 0;
uint32_t length = 1 + buflen + vsnprintf(NULL, 0, format, args);
va_end(args);
if(*buf)
*buf = realloc(*buf, length);
else
*buf = malloc(length);
assert(*buf);
vsnprintf(*buf + buflen, length - buflen, format, args2);
va_end(args2);
}
#define emit(x) append_code(&code, x)
#define emitf(x,...) append_code(&code, x, __VA_ARGS__)
char * compile(struct parse_result_t program) {
unsigned callid = vector_size(program.labels);
char * code = NULL;
emit(
"#include \"vector.h\"\n"
"#ifndef DURING_JIT\n"
"\t#include <stdlib.h>\n"
"\t#include <stdio.h>\n"
"\t#include <stdint.h>\n"
"#else\n"
"\tint putchar(int);\n"
"\tint getchar(void);\n"
"\tint printf(char * fmt, ...);\n"
"\tint scanf(char * fmt, ...);\n"
"\ttypedef int int32_t;\n"
"\t#define NULL ((void *) 0)\n"
"#endif\n"
"\n"
"static vector(int32_t) stack;\n"
"static vector(int32_t) heap;\n"
"static vector(int32_t) callstack;\n"
"static int32_t lhs, rhs, tmp, ip;\n"
"\n"
"#define AT(x, y) vector_end(x)[-y]\n"
"#define BILOAD \\\n"
"\trhs = AT(stack, 1); \\\n"
"\tlhs = AT(stack, 2); \\\n"
"\tvector_pop_back(stack); \\\n"
"\tvector_pop_back(stack)\n"
"\n"
"int main(void) {\n"
"\tbranch: switch(ip) {\n"
"case 0:\n"
);
vector_foreach(struct instruction_t, ins, program.program) {
switch(ins->type) {
case GETC:
emit(
"\trhs = AT(stack, 1);\n"
"\tvector_pop_back(stack);\n"
"\twhile(vector_size(heap) <= (unsigned) rhs)\n"
"\t\tvector_push_back(heap, 0);\n"
"\theap[rhs] = getchar();\n"
"\tif(heap[rhs] < 0) heap[rhs] = -1;\n"
);
break;
case PUTC:
emit(
"\tputchar(AT(stack, 1));\n"
"\tvector_pop_back(stack);\n"
);
break;
case GETN:
emit(
"\trhs = AT(stack, 1);\n"
"\tvector_pop_back(stack);\n"
"\twhile(vector_size(heap) <= (unsigned) rhs)\n"
"\t\tvector_push_back(heap, 0);\n"
"\tscanf(\"%%d\", &tmp);\n"
"\theap[rhs] = tmp;\n"
);
break;
case PUTN:
emit(
"\tprintf(\"%%d\", AT(stack, 1));\n"
"\tvector_pop_back(stack);\n"
);
break;
case PSH:
emitf("\tvector_push_back(stack, %d);\n", ins->data);
break;
case DUP:
emit("\tvector_push_back(stack, AT(stack, 1));\n");
break;
case XCHG:
emit(
"\ttmp = AT(stack, 2);\n"
"\tAT(stack, 2) = AT(stack, 1);\n"
"\tAT(stack, 1) = tmp;\n"
);
break;
case DROP:
emit("\tvector_pop_back(stack);\n");
break;
case ADD:
emit("\tBILOAD; vector_push_back(stack, lhs + rhs);\n");
break;
case SUB:
emit("\tBILOAD; vector_push_back(stack, lhs - rhs);\n");
break;
case DIV:
emit("\tBILOAD; vector_push_back(stack, lhs / rhs);\n");
break;
case MUL:
emit("\tBILOAD; vector_push_back(stack, lhs * rhs);\n");
break;
case MOD:
emit("\tBILOAD; vector_push_back(stack, lhs %% rhs);\n");
break;
case STOP:
emit("\tgoto end;\n");
break;
case STO:
emit(
"\tBILOAD;\n"
"\twhile(vector_size(heap) <= (unsigned) lhs)\n"
"\t\tvector_push_back(heap, 0);\n"
"\theap[lhs] = rhs;\n"
);
break;
case RCL:
emit(
"\tlhs = AT(stack, 1);\n"
"\tvector_pop_back(stack);\n"
"\tif(vector_size(heap) <= (unsigned) lhs)\n"
"\t\tvector_push_back(stack, 0);\n"
"\telse\n"
"\t\tvector_push_back(stack, heap[lhs]);\n"
);
break;
case LBL:
emitf("case %d:;\n", ins->data);
break;
case JMP:
emitf(
"\tip = %d;\n"
"\tgoto branch;\n"
, ins->data);
break;
case BZ:
emitf(
"\tlhs = AT(stack, 1);\n"
"\tvector_pop_back(stack);\n"
"\tif(lhs == 0) {\n"
"\t\tip = %d;\n"
"\t\tgoto branch;\n"
"\t}\n"
, ins->data);
break;
case BLTZ:
emitf(
"\tlhs = AT(stack, 1);\n"
"\tvector_pop_back(stack);\n"
"\tif(lhs < 0) {\n"
"\t\tip = %d;\n"
"\t\tgoto branch;\n"
"\t}\n"
, ins->data);
break;
case CALL:
emitf(
"\tvector_push_back(callstack, %d);\n"
"\tip = %d;\n"
"\tgoto branch;\n"
"case %d:\n"
, callid + 1, ins->data, callid + 1);
callid++;
break;
case RET:
emit(
"\tip = AT(callstack, 1);\n"
"\tvector_pop_back(callstack);\n"
"\tgoto branch;\n"
);
break;
case COPY:
emitf(
"\tvector_push_back(stack, stack[vector_size(stack) - 1 - %d]);\n"
, ins->data);
break;
case SLIDE:
emitf(
"\tfor(rhs = 0; rhs < %d; rhs++)\n"
"\t\tvector_erase(stack, vector_size(stack) - 1);\n"
, ins->data);
break;
}
}
emit(
"\t}\n"
"\tend:\n"
"\tvector_free(stack);\n"
"\tvector_free(heap);\n"
"\tvector_free(callstack);\n"
"}\n"
);
return code;
}