-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.c
More file actions
281 lines (256 loc) · 6.56 KB
/
codegen.c
File metadata and controls
281 lines (256 loc) · 6.56 KB
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
#include "quackcc.h"
static int depth;
static char *argreg[] = {"x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7"};
static Fun *current_function;
static void gen_expr(Node *node);
static char *gen_simple_label_name() {
static int i = 1;
char *label = malloc(16);
snprintf(label, 16, ".L%d.%s", i++, current_function->name);
return label;
}
static char *gen_return_label_name() {
char *label = malloc(16);
snprintf(label, 16, ".L.return.%s", current_function->name);
return label;
}
static void push(char* reg) {
printf(" str %s, [sp, #-16]!\n", reg);
depth++;
}
static void pop(char* reg) {
printf(" ldr %s, [sp], #16\n", reg);
depth--;
}
static void gen_addr(Node *node) {
switch (node->kind) {
case NK_VAR:
printf(" add x0, fp, #%d\n", node->var->offset);
return;
case NK_DEREF:
gen_expr(node->lhs);
return;
default:
error_at(node->token->loc, "not an lvalue");
}
}
static void load(Type *type) {
if (type->kind == TYK_ARRAY) {
// do not attempt to load a value to the register, because in general we
// can't load an entire array to a register.
return;
}
printf(" ldr x0, [x0]\n");
}
static void store(void) {
// this is assuming that x1 is unused; we'll also store into x0
pop("x1");
printf(" str x1, [x0]\n");
}
static void gen_expr(Node *node) {
switch(node->kind) {
case NK_SIZEOF:
printf(" mov x0, #%d\n", node->lhs->type->size);
return;
case NK_NUM:
printf(" mov x0, #%d\n", node->val);
return;
case NK_NEG:
gen_expr(node->lhs);
printf(" neg x0, x0\n");
return;
case NK_VAR:
gen_addr(node);
load(node->type);
return;
case NK_ASSIGN:
gen_expr(node->rhs);
push("x0");
gen_addr(node->lhs);
store();
return;
case NK_DEREF:
gen_expr(node->lhs);
load(node->type);
return;
case NK_ADDR:
gen_addr(node->lhs);
return;
case NK_FUNC_CALL: {
int i = -1;
// push onto stack, so that we can free up x0 for gen_expr
for (Node *arg = node->args; arg; arg = arg->next) {
gen_expr(arg);
push("x0");
i++;
}
// now assign arguments into registers x0 - x7
for (; i >= 0; i--) {
pop(argreg[i]);
}
// call func
printf(" bl _%s\n", node->func_name);
return;
}
default:
break;
}
// evaluate rhs, then push the value in x0 on stack
// later on, we pop this value from the stack into x1 (since x0 is used by lhs)
gen_expr(node->rhs);
push("x0");
gen_expr(node->lhs);
pop("x1");
switch (node->kind) {
case NK_ADD:
printf(" add x0, x0, x1\n");
return;
case NK_SUB:
printf(" sub x0, x0, x1\n");
return;
case NK_MUL:
printf(" mul x0, x0, x1\n");
return;
case NK_DIV:
printf(" sdiv x0, x0, x1\n");
return;
case NK_EQ:
printf(" cmp x0, x1\n");
printf(" mov x0, #0\n");
printf(" cset x0, eq\n");
return;
case NK_NE:
printf(" cmp x0, x1\n");
printf(" mov x0, #0\n");
printf(" cset x0, ne\n");
return;
case NK_LT:
printf(" cmp x0, x1\n");
printf(" mov x0, #0\n");
printf(" cset x0, lt\n");
return;
case NK_LE:
printf(" cmp x0, x1\n");
printf(" mov x0, #0\n");
printf(" cset x0, le\n");
return;
case NK_GT:
printf(" cmp x0, x1\n");
printf(" mov x0, #0\n");
printf(" cset x0, gt\n");
return;
case NK_GE:
printf(" cmp x0, x1\n");
printf(" mov x0, #0\n");
printf(" cset x0, ge\n");
return;
default:
error_at(node->token->loc, "invalid expression");
}
}
static void gen_stmt(Node *node) {
switch (node->kind) {
case NK_EXPR_STMT:
gen_expr(node->lhs);
return;
case NK_RETURN_STMT:
gen_expr(node->lhs);
printf(" b %s\n", gen_return_label_name());
return;
case NK_COMPOUND_STMT:
for (Node *stmt = node->body; stmt; stmt = stmt->next) {
gen_stmt(stmt);
assert(depth == 0);
}
return;
case NK_NULL_STMT:
// do nothing
return;
case NK_IF_STMT: {
if (node->rhs == NULL) {
char *l = gen_simple_label_name();
gen_expr(node->cond);
printf(" cmp x0, #0\n");
printf(" beq %s\n", l);
gen_stmt(node->lhs);
printf("%s:\n", l);
return;
}
char *l1 = gen_simple_label_name();
char *l2 = gen_simple_label_name();
gen_expr(node->cond);
printf(" cmp x0, #0\n");
printf(" beq %s\n", l1);
gen_stmt(node->lhs);
printf(" b %s\n", l2);
printf("%s:\n", l1);
gen_stmt(node->rhs);
printf("%s:\n", l2);
return;
}
case NK_WHILE_STMT: {
char *l1 = gen_simple_label_name();
char *l2 = gen_simple_label_name();
printf("%s:\n", l1);
gen_expr(node->cond);
printf(" cmp x0, #0\n");
printf(" beq %s\n", l2);
gen_stmt(node->body);
printf(" b %s\n", l1);
printf("%s:\n", l2);
return;
}
case NK_FOR_STMT: {
char *l1 = gen_simple_label_name();
char *l2 = gen_simple_label_name();
if (node->lhs != NULL) gen_expr(node->lhs);
printf("%s:\n", l1);
if (node->cond != NULL) {
gen_expr(node->cond);
printf(" cmp x0, #0\n");
printf(" beq %s\n", l2);
}
gen_stmt(node->body);
if (node->rhs != NULL) gen_expr(node->rhs);
printf(" b %s\n", l1);
printf("%s:\n", l2);
return;
}
default:
error_at(node->token->loc, "invalid statement");
}
}
static int align_to(int n, int align) {
return (n + align - 1) / align * align;
}
static void assign_lvar_offsets(Fun *fun) {
int offset = 0;
for (Obj *var = fun->locals; var; var = var->next) {
offset += var->type->size;
var->offset = -offset;
}
fun->stack_size = align_to(offset, 16);
}
static void gen_func(Fun *fun) {
assign_lvar_offsets(fun);
current_function = fun;
printf(".global _%s\n\n", fun->name);
printf("_%s:\n", fun->name);
// prologue
printf(" stp fp, lr, [sp, #-16]!\n");
printf(" mov fp, sp\n");
printf(" sub sp, sp, #%d\n", fun->stack_size);
// move params in registers into their allocated space in the stack
int i = 0;
for (Obj *var = fun->params; var; var = var->next)
printf(" str %s, [fp, %d] \n", argreg[i++], var->offset);
gen_stmt(fun->body);
// epilogue
printf("%s:\n", gen_return_label_name());
printf(" mov sp, fp\n");
printf(" ldp fp, lr, [sp], #16\n");
printf(" ret\n\n");
}
void codegen(Fun *prog) {
for (Fun *fun = prog; fun; fun = fun->next) gen_func(fun);
}