-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.c
346 lines (306 loc) · 7.79 KB
/
codegen.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "chibicc.h"
static FILE *output_file;
static int depth;
static char *argreg8[] = {"w0", "w1", "w2", "w3", "w4", "w5"};
static char *argreg64[] = {"x0", "x1", "x2", "x3", "x4", "x5"};
static Obj *current_fn;
static void gen_expr(Node *node);
static void gen_stmt(Node *node);
static void println(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(output_file, fmt, ap);
va_end(ap);
fprintf(output_file, "\n");
}
static int count(void) {
static int i = 1;
return i++;
}
static void push(void) {
println(" str x0, [sp, #-8]!");
depth++;
}
static void pop(char *arg) {
println(" ldr %s, [sp], #8", arg);
depth--;
}
// Round up `n` to the nearest multiple of `align`. For instance,
// align_to(5, 8) returns 8 and align_to(11, 8) returns 16.
int align_to(int n, int align) {
return (n + align - 1) / align * align;
}
// Compute the absolute address of a given node.
// It's an error if a given node does not reside in memory.
static void gen_addr(Node *node) {
switch (node->kind) {
case ND_VAR:
if (node->var->is_local) {
// Local variable
println(" add x0, x29, %d", node->var->offset);
} else {
// Global variable
println(" adrp x0, %s", node->var->name);
println(" add x0, x0, :lo12:%s", node->var->name);
}
return;
case ND_DEREF:
gen_expr(node->lhs);
return;
case ND_COMMA:
gen_expr(node->lhs);
gen_addr(node->rhs);
return;
case ND_MEMBER:
gen_addr(node->lhs);
println(" add x0, x0, #%d", node->member->offset);
return;
}
error_tok(node->tok, "not an lvalue");
}
// Load a value from where %rax is pointing to.
static void load(Type *ty) {
if (ty->kind == TY_ARRAY || ty->kind == TY_STRUCT || ty->kind == TY_UNION) {
// If it is an array, do not attempt to load a value to the
// register because in general we can't load an entire array to a
// register. As a result, the result of an evaluation of an array
// becomes not the array itself but the address of the array.
// This is where "array is automatically converted to a pointer to
// the first element of the array in C" occurs.
return;
}
if (ty->size == 1)
println(" ldrb w0, [x0]");
else if (ty->size == 2)
println(" ldrh w0, [x0]");
else if (ty->size == 4)
println(" ldr w0, [x0]");
else
println(" ldr x0, [x0]");
}
// Store %rax to an address that the stack top is pointing to.
static void store(Type *ty) {
pop("x1");
if (ty->kind == TY_STRUCT || ty->kind == TY_UNION) {
for (int i = 0; i < ty->size; i++) {
println(" ldrb w2, [x0, #%d]", i);
println(" strb w2, [x1, #%d]", i);
}
return;
}
if (ty->size == 1)
println(" strb w0, [x1]");
else if (ty->size == 2)
println(" strh w0, [x1]");
else if (ty->size == 4)
println(" str w0, [x1]");
else
println(" str x0, [x1]");
}
// Generate code for a given node.
static void gen_expr(Node *node) {
println(" .loc 1 %d", node->tok->line_no);
switch (node->kind) {
case ND_NUM:
println(" ldr x0, =%ld", node->val);
return;
case ND_NEG:
gen_expr(node->lhs);
println(" neg x0, x0");
return;
case ND_VAR:
case ND_MEMBER:
gen_addr(node);
load(node->ty);
return;
case ND_DEREF:
gen_expr(node->lhs);
load(node->ty);
return;
case ND_ADDR:
gen_addr(node->lhs);
return;
case ND_ASSIGN:
gen_addr(node->lhs);
push();
gen_expr(node->rhs);
store(node->ty);
return;
case ND_STMT_EXPR:
for (Node *n = node->body; n; n = n->next)
gen_stmt(n);
return;
case ND_COMMA:
gen_expr(node->lhs);
gen_expr(node->rhs);
return;
case ND_FUNCALL: {
int nargs = 0;
for (Node *arg = node->args; arg; arg = arg->next) {
gen_expr(arg);
push();
nargs++;
}
for (int i = nargs - 1; i >= 0; i--)
pop(argreg64[i]);
println(" bl %s", node->funcname);
return;
}
}
gen_expr(node->rhs);
push();
gen_expr(node->lhs);
pop("x1");
switch (node->kind) {
case ND_ADD:
println(" add x0, x0, x1");
return;
case ND_SUB:
println(" sub x0, x0, x1");
return;
case ND_MUL:
println(" mul x0, x0, x1");
return;
case ND_DIV:
println(" sdiv x0, x0, x1");
return;
case ND_EQ:
case ND_NE:
case ND_LT:
case ND_LE:
println(" cmp x0, x1");
if (node->kind == ND_EQ)
println(" cset x0, eq");
else if (node->kind == ND_NE)
println(" cset x0, ne");
else if (node->kind == ND_LT)
println(" cset x0, lt");
else if (node->kind == ND_LE)
println(" cset x0, le");
return;
}
error_tok(node->tok, "invalid expression");
}
static void gen_stmt(Node *node) {
println(" .loc 1 %d", node->tok->line_no);
switch (node->kind) {
case ND_IF: {
int c = count();
gen_expr(node->cond);
println(" cbz x0, .L.else.%d", c);
gen_stmt(node->then);
println(" b .L.end.%d", c);
println(".L.else.%d:", c);
if (node->els)
gen_stmt(node->els);
println(".L.end.%d:", c);
return;
}
case ND_FOR: {
int c = count();
if (node->init)
gen_stmt(node->init);
println(".L.begin.%d:", c);
if (node->cond) {
gen_expr(node->cond);
println(" cbz x0, .L.end.%d", c);
}
gen_stmt(node->then);
if (node->inc)
gen_expr(node->inc);
println(" b .L.begin.%d", c);
println(".L.end.%d:", c);
return;
}
case ND_BLOCK:
for (Node *n = node->body; n; n = n->next)
gen_stmt(n);
return;
case ND_RETURN:
gen_expr(node->lhs);
println(" b .L.return.%s", current_fn->name);
return;
case ND_EXPR_STMT:
gen_expr(node->lhs);
return;
}
error_tok(node->tok, "invalid statement");
}
// Assign offsets to local variables.
static void assign_lvar_offsets(Obj *prog) {
for (Obj *fn = prog; fn; fn = fn->next) {
if (!fn->is_function)
continue;
int offset = 0;
for (Obj *var = fn->locals; var; var = var->next) {
offset += var->ty->size;
offset = align_to(offset, var->ty->align);
var->offset = -offset;
}
fn->stack_size = align_to(offset, 16);
}
}
static void emit_data(Obj *prog) {
for (Obj *var = prog; var; var = var->next) {
if (var->is_function)
continue;
println(" .data");
println(" .globl %s", var->name);
println("%s:", var->name);
if (var->init_data) {
for (int i = 0; i < var->ty->size; i++)
println(" .byte %d", var->init_data[i]);
} else {
println(" .zero %d", var->ty->size);
}
}
}
static void store_gp(int r, int offset, int sz) {
switch (sz) {
case 1:
println(" strb %s, [x29, #%d]", argreg8[r], offset);
return;
case 2:
println(" strh %s, [x29, #%d]", argreg8[r], offset);
return;
case 4:
println(" str %s, [x29, #%d]", argreg8[r], offset);
return;
case 8:
println(" str %s, [x29, #%d]", argreg64[r], offset);
return;
}
unreachable();
}
static void emit_text(Obj *prog) {
for (Obj *fn = prog; fn; fn = fn->next) {
if (!fn->is_function || !fn->is_definition)
continue;
println(" .globl %s", fn->name);
println(" .text");
println("%s:", fn->name);
current_fn = fn;
// Prologue
println(" stp x29, x30, [sp, #-16]!");
println(" mov x29, sp");
println(" sub sp, sp, #%d", fn->stack_size);
// Save passed-by-register arguments to the stack
int i = 0;
for (Obj *var = fn->params; var; var = var->next)
store_gp(i++, var->offset, var->ty->size);
// Emit code
gen_stmt(fn->body);
assert(depth == 0);
// Epilogue
println(".L.return.%s:", fn->name);
println(" add sp, sp, #%d", fn->stack_size);
println(" ldp x29, x30, [sp], #16");
println(" ret");
}
}
void codegen(Obj *prog, FILE *out) {
output_file = out;
assign_lvar_offsets(prog);
emit_data(prog);
emit_text(prog);
}