-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.c
635 lines (577 loc) · 15.8 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#include "chibicc.h"
static FILE *output_file;
static int depth;
static char *argreg[] = {"a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7"};
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(" addi.d $sp, $sp, -8");
println(" st.d $a0, $sp, 0");
depth++;
}
static void pop(char *arg) {
println(" ld.d $%s, $sp, 0", arg);
println(" addi.d $sp, $sp, 8");
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(" li.d $t1, %d", node->var->offset - node->var->ty->size);
println(" add.d $a0, $fp, $t1");
} else {
// Global variable
println(" la.local $a0, %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(" addi.d $a0, $a0, %d", node->member->offset);
return;
}
error_tok(node->tok, "not an lvalue");
}
// Load a value from where a0 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;
}
char *suffix = ty->is_unsigned ? "u" : "";
// When we load a char or a short value to a register, we always
// extend them to the size of int, so we can assume the lower half of
// a register always contains a valid value. The upper half of a
// register for char, short and int may contain garbage. When we load
// a long value to a register, it simply occupies the entire register.
if (ty->size == 1)
println(" ld.b%s $a0, $a0, 0", suffix);
else if (ty->size == 2)
println(" ld.h%s $a0, $a0, 0", suffix);
else if (ty->size == 4)
println(" ld.w%s $a0, $a0, 0", suffix);
else
println(" ld.d $a0, $a0, 0");
}
// Store a0 to an address that the stack top is pointing to.
static void store(Type *ty) {
pop("a1");
if (ty->kind == TY_STRUCT || ty->kind == TY_UNION) {
for (int i = 0; i < ty->size; i++) {
println(" ld.b $a4, $a0, %d\n", i);
println(" st.b $a4, $a1, %d\n", i);
}
return;
}
if (ty->size == 1)
println(" st.b $a0, $a1, 0");
else if (ty->size == 2)
println(" st.h $a0, $a1, 0");
else if (ty->size == 4)
println(" st.w $a0, $a1, 0");
else
println(" st.d $a0, $a1, 0");
}
enum { I8, I16, I32, I64, U8, U16, U32, U64 };
static int getTypeId(Type *ty) {
switch (ty->kind) {
case TY_CHAR:
return ty->is_unsigned ? U8 : I8;
case TY_SHORT:
return ty->is_unsigned ? U16 : I16;
case TY_INT:
return ty->is_unsigned ? U32 : I32;
case TY_LONG:
return ty->is_unsigned ? U64 : I64;
}
return U64;
}
// The table for type casts
static char i32i8[] = " slli.w $a0, $a0, 24\n srai.w $a0, $a0, 24";
static char i32u8[] = " andi $a0, $a0, 0xff";
static char i32i16[] = " slli.w $a0, $a0, 16\n srai.w $a0, $a0, 16";
static char i32u16[] = " slli.d $a0, $a0, 48\n srli.d $a0, $a0, 48";
static char *cast_table[][10] = {
// i8 i16 i32 i64 u8 u16 u32 u64
{NULL, NULL, NULL, NULL, i32u8, i32u16, NULL, NULL}, // i8
{i32i8, NULL, NULL, NULL, i32u8, i32u16, NULL, NULL}, // i16
{i32i8, i32i16, NULL, NULL, i32u8, i32u16, NULL, NULL}, // i32
{i32i8, i32i16, NULL, NULL, i32u8, i32u16, NULL, NULL}, // i64
{i32i8, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, // u8
{i32i8, i32i16, NULL, NULL, i32u8, NULL, NULL, NULL}, // u16
{i32i8, i32i16, NULL, NULL, i32u8, i32u16, NULL, NULL}, // u32
{i32i8, i32i16, NULL, NULL, i32u8, i32u16, NULL, NULL}, // u64
};
static void cast(Type *from, Type *to) {
if (to->kind == TY_VOID)
return;
if (to->kind == TY_BOOL) {
println(" sltu $a0, $r0, $a0");
return;
}
int t1 = getTypeId(from);
int t2 = getTypeId(to);
if (cast_table[t1][t2])
println(cast_table[t1][t2]);
}
// 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_NULL_EXPR:
return;
case ND_NUM: {
union { float f32; double f64; uint32_t u32; uint64_t u64; } u;
switch (node->ty->kind) {
case TY_FLOAT:
u.f32 = node->fval;
println(" li.w $a0, %u # float %f", u.u32, node->fval);
println(" movgr2fr.w $f0, $a0");
return;
case TY_DOUBLE:
u.f64 = node->fval;
println(" li.d $a0, %lu # float %f", u.u64, node->fval);
println(" movgr2fr.d $f0, $a0");
return;
}
println(" li.d $a0, %ld", node->val);
return;
}
case ND_NEG:
gen_expr(node->lhs);
println(" sub.d $a0, $r0, $a0");
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_CAST:
gen_expr(node->lhs);
cast(node->lhs->ty, node->ty);
return;
case ND_MEMZERO: {
int offset = node->var->offset;
for (int i = 0; i < node->var->ty->size; i++) {
offset -= sizeof(char);
println(" li.d $t1, %d", offset);
println(" add.d $t1, $t1, $fp");
println(" st.b $r0, $t1, 0");
}
return;
}
case ND_COND: {
int c = count();
gen_expr(node->cond);
println(" beqz $a0, .L.else.%d", c);
gen_expr(node->then);
println(" b .L.end.%d", c);
println(".L.else.%d:", c);
gen_expr(node->els);
println(".L.end.%d:", c);
return;
}
case ND_NOT:
gen_expr(node->lhs);
println(" sltui $a0, $a0, 1");
return;
case ND_BITNOT:
gen_expr(node->lhs);
println(" li.d $a2, -1");
println(" xor $a0, $a0, $a2");
return;
case ND_LOGAND: {
int c = count();
gen_expr(node->lhs);
println(" beqz $a0, .L.false.%d", c);
gen_expr(node->rhs);
println(" beqz $a0, .L.false.%d", c);
println(" li.d $a0, 1");
println(" b .L.end.%d", c);
println(".L.false.%d:", c);
println(" li.d $a0, 0");
println(".L.end.%d:", c);
return;
}
case ND_LOGOR: {
int c = count();
gen_expr(node->lhs);
println(" bne $a0, $r0, .L.true.%d", c);
gen_expr(node->rhs);
println(" bne $a0, $r0, .L.true.%d", c);
println(" li.d $a0, 0");
println(" b .L.end.%d", c);
println(".L.true.%d:", c);
println(" li.d $a0, 1");
println(".L.end.%d:", c);
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(argreg[i]);
if (depth % 2 == 0) {
println(" bl %s", node->funcname);
} else {
println(" addi.d $fp, $fp,-8");
println(" bl %s", node->funcname);
println(" addi.d $fp, $fp, 8");
}
// It looks like the most significant 48 or 56 bits in a0 may
// contain garbage if a function return type is short or bool/char,
// respectively. We clear the upper bits here.
switch (node->ty->kind) {
case TY_BOOL:
println(" andi $a0, $a0, 0xff");
case TY_CHAR:
if (node->ty->is_unsigned) {
println(" andi $a0, $a0, 0xff");
} else {
println(" slli.w $a0, $a0, 24");
println(" srai.w $a0, $a0, 24");
}
return;
case TY_SHORT:
if (node->ty->is_unsigned) {
println(" slli.d $a0, $a0, 48");
println(" srli.d $a0, $a0, 48");
} else {
println(" slli.w $a0, $a0, 16");
println(" srai.w $a0, $a0, 16");
}
return;
}
return;
}
}
gen_expr(node->rhs);
push();
gen_expr(node->lhs);
pop("a1");
char* suffix = node->lhs->ty->kind == TY_LONG || node->lhs->ty->base
? "d" : "w";
switch (node->kind) {
case ND_ADD:
println(" add.%s $a0, $a0, $a1", suffix);
return;
case ND_SUB:
println(" sub.%s $a0, $a0, $a1", suffix);
return;
case ND_MUL:
println(" mul.%s $a0, $a0, $a1", suffix);
return;
case ND_DIV:
if (node->ty->is_unsigned) {
println(" div.%su $a0, $a0, $a1", suffix);
} else {
println(" div.%s $a0, $a0, $a1", suffix);
}
return;
case ND_MOD:
if (node->ty->is_unsigned) {
println(" mod.%su $a0, $a0, $a1", suffix);
} else {
println(" mod.%s $a0, $a0, $a1", suffix);
}
return;
case ND_BITAND:
println(" and $a0, $a0, $a1");
return;
case ND_BITOR:
println(" or $a0, $a0, $a1");
return;
case ND_BITXOR:
println(" xor $a0, $a0, $a1");
return;
case ND_EQ:
println(" sub.d $a0, $a0, $a1");
println(" sltui $a0, $a0, 1");
return;
case ND_NE:
println(" sub.d $a0, $a0, $a1");
println(" slt $a0, $a0, $r0");
return;
case ND_LT:
if (node->lhs->ty->is_unsigned) {
println(" sltu $a0, $a0, $a1");
} else {
println(" slt $a0, $a0, $a1");
}
return;
case ND_LE:
if (node->lhs->ty->is_unsigned) {
println(" sltu $a0, $a1, $a0");
} else {
println(" slt $a0, $a1, $a0");
}
println(" xori $a0, $a0, 1");
return;
case ND_SHL:
println(" sll.%s $a0, $a0, $a1", suffix);
return;
case ND_SHR:
if (node->lhs->ty->is_unsigned) {
println(" srl.%s $a0, $a0, $a1", suffix);
} else {
println(" sra.%s $a0, $a0, $a1", suffix);
}
return;
default:
break;
}
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(" beqz $a0, .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(" beqz $a0,%s", node->brk_label);
}
gen_stmt(node->then);
println("%s:", node->cont_label);
if (node->inc)
gen_expr(node->inc);
println(" b .L.begin.%d", c);
println("%s:", node->brk_label);
return;
}
case ND_DO: {
int c = count();
println(".L.begin.%d:", c);
gen_stmt(node->then);
println("%s:", node->cont_label);
gen_expr(node->cond);
println(" bne $a0, $r0, .L.begin.%d", c);
println("%s:", node->brk_label);
return;
}
case ND_SWITCH:
gen_expr(node->cond);
for (Node *n = node->case_next; n; n = n->case_next) {
println(" li.d $a4, %ld", n->val);
println(" beq $a0, $a4, %s", n->label);
}
if (node->default_case)
println(" b %s", node->default_case->label);
println(" b %s", node->brk_label);
gen_stmt(node->then);
println("%s:", node->brk_label);
return;
case ND_CASE:
println("%s:", node->label);
gen_stmt(node->lhs);
return;
case ND_BLOCK:
for (Node *n = node->body; n; n = n->next)
gen_stmt(n);
return;
case ND_GOTO:
println(" b %s", node->unique_label);
return;
case ND_LABEL:
println("%s:", node->unique_label);
gen_stmt(node->lhs);
return;
case ND_RETURN:
if (node->lhs)
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 = align_to(offset, var->align);
var->offset = -offset;
offset += var->ty->size;
}
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 || !var->is_definition)
continue;
if (var->is_static)
println(" .local %s", var->name);
else
println(" .globl %s", var->name);
println(" .align %d", (int)log2(var->align));
if (var->init_data) {
println(" .data");
println("%s:", var->name);
Relocation *rel = var->rel;
int pos = 0;
while (pos < var->ty->size) {
if (rel && rel->offset == pos) {
println(" .quad %s%+ld", rel->label, rel->addend);
rel = rel->next;
pos += 8;
} else {
println(" .byte %d", var->init_data[pos++]);
}
}
continue;
}
println(" .bss");
println("%s:", var->name);
println(" .zero %d", var->ty->size);
}
}
static void store_gp(int r, int offset, int sz) {
println(" li.d $t1, %d", offset - sz);
println(" add.d $t1, $t1, $fp");
switch (sz) {
case 1:
println(" st.b $%s, $t1, 0", argreg[r]);
return;
case 2:
println(" st.h $%s, $t1, 0", argreg[r]);
return;
case 4:
println(" st.w $%s, $t1, 0", argreg[r]);
return;
case 8:
println(" st.d $%s, $t1, 0", argreg[r]);
return;
}
unreachable();
}
static void emit_text(Obj *prog) {
for (Obj *fn = prog; fn; fn = fn->next) {
if (!fn->is_function || !fn->is_definition)
continue;
if (fn->is_static)
println(" .local %s", fn->name);
else
println(" .globl %s", fn->name);
println(" .text");
println("%s:", fn->name);
current_fn = fn;
// Prologue
println(" st.d $ra, $sp, -8");
println(" st.d $fp, $sp, -16");
println(" addi.d $fp, $sp, -16");
println(" li.d $t1, -%d", fn->stack_size + 16);
println(" add.d $sp, $sp, $t1");
// println(" addi.d $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) {
// __va_area__
if (var->ty->kind == TY_ARRAY) {
int offset = var->offset - var->ty->size;
while (i < 8) {
offset += 8;
store_gp(i++, offset, 8);
}
} else {
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(" li.d $t1, %d", fn->stack_size + 16);
println(" add.d $sp, $sp, $t1");
println(" ld.d $ra, $sp, -8");
println(" ld.d $fp, $sp, -16");
println(" jr $ra");
}
}
void codegen(Obj *prog, FILE *out) {
output_file = out;
assign_lvar_offsets(prog);
emit_data(prog);
emit_text(prog);
println(".LFE0:");
println(" .size main, .-main");
println(" .section .note.GNU-stack,\"\",@progbits");
}