forked from rui314/chibicc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.c
688 lines (631 loc) · 15.9 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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#include "chibi.h"
static char *argreg1[] = {"dil", "sil", "dl", "cl", "r8b", "r9b"};
static char *argreg2[] = {"di", "si", "dx", "cx", "r8w", "r9w"};
static char *argreg4[] = {"edi", "esi", "edx", "ecx", "r8d", "r9d"};
static char *argreg8[] = {"rdi", "rsi", "rdx", "rcx", "r8", "r9"};
static int labelseq = 1;
static int brkseq;
static int contseq;
static char *funcname;
static void gen(Node *node);
// Pushes the given node's address to the stack.
static void gen_addr(Node *node) {
switch (node->kind) {
case ND_VAR: {
if (node->init)
gen(node->init);
Var *var = node->var;
if (var->is_local) {
printf(" lea rax, [rbp-%d]\n", var->offset);
printf(" push rax\n");
} else {
printf(" push offset %s\n", var->name);
}
return;
}
case ND_DEREF:
gen(node->lhs);
return;
case ND_MEMBER:
gen_addr(node->lhs);
printf(" pop rax\n");
printf(" add rax, %d\n", node->member->offset);
printf(" push rax\n");
return;
}
error_tok(node->tok, "not an lvalue");
}
static void gen_lval(Node *node) {
if (node->ty->kind == TY_ARRAY)
error_tok(node->tok, "not an lvalue");
gen_addr(node);
}
static void load(Type *ty) {
printf(" pop rax\n");
if (ty->size == 1) {
if (ty->is_signed)
printf(" movsx rax, byte ptr [rax]\n");
else
printf(" movzx rax, byte ptr [rax]\n");
} else if (ty->size == 2) {
if (ty->is_signed)
printf(" movsx rax, word ptr [rax]\n");
else
printf(" movzx rax, word ptr [rax]\n");
} else if (ty->size == 4) {
if (ty->is_signed)
printf(" movsxd rax, dword ptr [rax]\n");
else
printf(" mov eax, dword ptr [rax]\n");
} else {
assert(ty->size == 8);
printf(" mov rax, [rax]\n");
}
printf(" push rax\n");
}
static void store(Type *ty) {
printf(" pop rdi\n");
printf(" pop rax\n");
if (ty->kind == TY_BOOL) {
printf(" cmp rdi, 0\n");
printf(" setne dil\n");
printf(" movzb rdi, dil\n");
}
if (ty->size == 1) {
printf(" mov [rax], dil\n");
} else if (ty->size == 2) {
printf(" mov [rax], di\n");
} else if (ty->size == 4) {
printf(" mov [rax], edi\n");
} else {
assert(ty->size == 8);
printf(" mov [rax], rdi\n");
}
printf(" push rdi\n");
}
static void truncate(Type *ty) {
printf(" pop rax\n");
if (ty->kind == TY_BOOL) {
printf(" cmp rax, 0\n");
printf(" setne al\n");
}
if (ty->size == 1) {
printf(" movsx rax, al\n");
} else if (ty->size == 2) {
printf(" movsx rax, ax\n");
} else if (ty->size == 4) {
printf(" movsxd rax, eax\n");
}
printf(" push rax\n");
}
static void inc(Type *ty) {
printf(" pop rax\n");
printf(" add rax, %d\n", ty->base ? ty->base->size : 1);
printf(" push rax\n");
}
static void dec(Type *ty) {
printf(" pop rax\n");
printf(" sub rax, %d\n", ty->base ? ty->base->size : 1);
printf(" push rax\n");
}
static void gen_binary(Node *node) {
printf(" pop rdi\n");
printf(" pop rax\n");
switch (node->kind) {
case ND_ADD:
case ND_ADD_EQ:
printf(" add rax, rdi\n");
break;
case ND_PTR_ADD:
case ND_PTR_ADD_EQ:
printf(" imul rdi, %d\n", node->ty->base->size);
printf(" add rax, rdi\n");
break;
case ND_SUB:
case ND_SUB_EQ:
printf(" sub rax, rdi\n");
break;
case ND_PTR_SUB:
case ND_PTR_SUB_EQ:
printf(" imul rdi, %d\n", node->ty->base->size);
printf(" sub rax, rdi\n");
break;
case ND_PTR_DIFF:
printf(" sub rax, rdi\n");
printf(" cqo\n");
printf(" mov rdi, %d\n", node->lhs->ty->base->size);
printf(" idiv rdi\n");
break;
case ND_MUL:
case ND_MUL_EQ:
if (node->ty->is_signed)
printf(" imul rax, rdi\n");
else
printf(" mul rdi\n");
break;
case ND_DIV:
case ND_DIV_EQ:
if (node->ty->is_signed) {
printf(" cqo\n");
printf(" idiv rdi\n");
} else {
printf(" mov rdx, 0\n");
printf(" div rdi\n");
}
break;
case ND_BITAND:
case ND_BITAND_EQ:
printf(" and rax, rdi\n");
break;
case ND_BITOR:
case ND_BITOR_EQ:
printf(" or rax, rdi\n");
break;
case ND_BITXOR:
case ND_BITXOR_EQ:
printf(" xor rax, rdi\n");
break;
case ND_SHL:
case ND_SHL_EQ:
printf(" mov cl, dil\n");
printf(" shl rax, cl\n");
break;
case ND_SHR:
case ND_SHR_EQ:
printf(" mov cl, dil\n");
if (node->ty->is_signed)
printf(" sar rax, cl\n");
else
printf(" shr rax, cl\n");
break;
case ND_EQ:
printf(" cmp rax, rdi\n");
printf(" sete al\n");
printf(" movzb rax, al\n");
break;
case ND_NE:
printf(" cmp rax, rdi\n");
printf(" setne al\n");
printf(" movzb rax, al\n");
break;
case ND_LT:
printf(" cmp rax, rdi\n");
printf(" setl al\n");
printf(" movzb rax, al\n");
break;
case ND_LE:
printf(" cmp rax, rdi\n");
printf(" setle al\n");
printf(" movzb rax, al\n");
break;
}
printf(" push rax\n");
}
// Generate code for a given node.
static void gen(Node *node) {
switch (node->kind) {
case ND_NULL:
return;
case ND_NUM:
if (node->val == (int)node->val) {
printf(" push %ld\n", node->val);
} else {
printf(" movabs rax, %ld\n", node->val);
printf(" push rax\n");
}
return;
case ND_EXPR_STMT:
gen(node->lhs);
printf(" add rsp, 8\n");
return;
case ND_VAR:
if (node->init)
gen(node->init);
gen_addr(node);
if (node->ty->kind != TY_ARRAY)
load(node->ty);
return;
case ND_MEMBER:
gen_addr(node);
if (node->ty->kind != TY_ARRAY)
load(node->ty);
return;
case ND_ASSIGN:
gen_lval(node->lhs);
gen(node->rhs);
store(node->ty);
return;
case ND_TERNARY: {
int seq = labelseq++;
gen(node->cond);
printf(" pop rax\n");
printf(" cmp rax, 0\n");
printf(" je .L.else.%d\n", seq);
gen(node->then);
printf(" jmp .L.end.%d\n", seq);
printf(".L.else.%d:\n", seq);
gen(node->els);
printf(".L.end.%d:\n", seq);
return;
}
case ND_PRE_INC:
gen_lval(node->lhs);
printf(" push [rsp]\n");
load(node->ty);
inc(node->ty);
store(node->ty);
return;
case ND_PRE_DEC:
gen_lval(node->lhs);
printf(" push [rsp]\n");
load(node->ty);
dec(node->ty);
store(node->ty);
return;
case ND_POST_INC:
gen_lval(node->lhs);
printf(" push [rsp]\n");
load(node->ty);
inc(node->ty);
store(node->ty);
dec(node->ty);
return;
case ND_POST_DEC:
gen_lval(node->lhs);
printf(" push [rsp]\n");
load(node->ty);
dec(node->ty);
store(node->ty);
inc(node->ty);
return;
case ND_ADD_EQ:
case ND_PTR_ADD_EQ:
case ND_SUB_EQ:
case ND_PTR_SUB_EQ:
case ND_MUL_EQ:
case ND_DIV_EQ:
case ND_SHL_EQ:
case ND_SHR_EQ:
case ND_BITAND_EQ:
case ND_BITOR_EQ:
case ND_BITXOR_EQ:
gen_lval(node->lhs);
printf(" push [rsp]\n");
load(node->lhs->ty);
gen(node->rhs);
gen_binary(node);
store(node->ty);
return;
case ND_COMMA:
gen(node->lhs);
gen(node->rhs);
return;
case ND_ADDR:
gen_addr(node->lhs);
return;
case ND_DEREF:
gen(node->lhs);
if (node->ty->kind != TY_ARRAY)
load(node->ty);
return;
case ND_NOT:
gen(node->lhs);
printf(" pop rax\n");
printf(" cmp rax, 0\n");
printf(" sete al\n");
printf(" movzb rax, al\n");
printf(" push rax\n");
return;
case ND_BITNOT:
gen(node->lhs);
printf(" pop rax\n");
printf(" not rax\n");
printf(" push rax\n");
return;
case ND_LOGAND: {
int seq = labelseq++;
gen(node->lhs);
printf(" pop rax\n");
printf(" cmp rax, 0\n");
printf(" je .L.false.%d\n", seq);
gen(node->rhs);
printf(" pop rax\n");
printf(" cmp rax, 0\n");
printf(" je .L.false.%d\n", seq);
printf(" push 1\n");
printf(" jmp .L.end.%d\n", seq);
printf(".L.false.%d:\n", seq);
printf(" push 0\n");
printf(".L.end.%d:\n", seq);
return;
}
case ND_LOGOR: {
int seq = labelseq++;
gen(node->lhs);
printf(" pop rax\n");
printf(" cmp rax, 0\n");
printf(" jne .L.true.%d\n", seq);
gen(node->rhs);
printf(" pop rax\n");
printf(" cmp rax, 0\n");
printf(" jne .L.true.%d\n", seq);
printf(" push 0\n");
printf(" jmp .L.end.%d\n", seq);
printf(".L.true.%d:\n", seq);
printf(" push 1\n");
printf(".L.end.%d:\n", seq);
return;
}
case ND_IF: {
int seq = labelseq++;
if (node->els) {
gen(node->cond);
printf(" pop rax\n");
printf(" cmp rax, 0\n");
printf(" je .L.else.%d\n", seq);
gen(node->then);
printf(" jmp .L.end.%d\n", seq);
printf(".L.else.%d:\n", seq);
gen(node->els);
printf(".L.end.%d:\n", seq);
} else {
gen(node->cond);
printf(" pop rax\n");
printf(" cmp rax, 0\n");
printf(" je .L.end.%d\n", seq);
gen(node->then);
printf(".L.end.%d:\n", seq);
}
return;
}
case ND_WHILE: {
int seq = labelseq++;
int brk = brkseq;
int cont = contseq;
brkseq = contseq = seq;
printf(".L.continue.%d:\n", seq);
gen(node->cond);
printf(" pop rax\n");
printf(" cmp rax, 0\n");
printf(" je .L.break.%d\n", seq);
gen(node->then);
printf(" jmp .L.continue.%d\n", seq);
printf(".L.break.%d:\n", seq);
brkseq = brk;
contseq = cont;
return;
}
case ND_FOR: {
int seq = labelseq++;
int brk = brkseq;
int cont = contseq;
brkseq = contseq = seq;
if (node->init)
gen(node->init);
printf(".L.begin.%d:\n", seq);
if (node->cond) {
gen(node->cond);
printf(" pop rax\n");
printf(" cmp rax, 0\n");
printf(" je .L.break.%d\n", seq);
}
gen(node->then);
printf(".L.continue.%d:\n", seq);
if (node->inc)
gen(node->inc);
printf(" jmp .L.begin.%d\n", seq);
printf(".L.break.%d:\n", seq);
brkseq = brk;
contseq = cont;
return;
}
case ND_DO: {
int seq = labelseq++;
int brk = brkseq;
int cont = contseq;
brkseq = contseq = seq;
printf(".L.begin.%d:\n", seq);
gen(node->then);
printf(".L.continue.%d:\n", seq);
gen(node->cond);
printf(" pop rax\n");
printf(" cmp rax, 0\n");
printf(" jne .L.begin.%d\n", seq);
printf(".L.break.%d:\n", seq);
brkseq = brk;
contseq = cont;
return;
}
case ND_SWITCH: {
int seq = labelseq++;
int brk = brkseq;
brkseq = seq;
node->case_label = seq;
gen(node->cond);
printf(" pop rax\n");
for (Node *n = node->case_next; n; n = n->case_next) {
n->case_label = labelseq++;
n->case_end_label = seq;
printf(" cmp rax, %ld\n", n->val);
printf(" je .L.case.%d\n", n->case_label);
}
if (node->default_case) {
int i = labelseq++;
node->default_case->case_end_label = seq;
node->default_case->case_label = i;
printf(" jmp .L.case.%d\n", i);
}
printf(" jmp .L.break.%d\n", seq);
gen(node->then);
printf(".L.break.%d:\n", seq);
brkseq = brk;
return;
}
case ND_CASE:
printf(".L.case.%d:\n", node->case_label);
gen(node->lhs);
return;
case ND_BLOCK:
case ND_STMT_EXPR:
for (Node *n = node->body; n; n = n->next)
gen(n);
return;
case ND_BREAK:
if (brkseq == 0)
error_tok(node->tok, "stray break");
printf(" jmp .L.break.%d\n", brkseq);
return;
case ND_CONTINUE:
if (contseq == 0)
error_tok(node->tok, "stray continue");
printf(" jmp .L.continue.%d\n", contseq);
return;
case ND_GOTO:
printf(" jmp .L.label.%s.%s\n", funcname, node->label_name);
return;
case ND_LABEL:
printf(".L.label.%s.%s:\n", funcname, node->label_name);
gen(node->lhs);
return;
case ND_FUNCALL: {
if (!strcmp(node->funcname, "__builtin_va_start")) {
printf(" pop rax\n");
printf(" mov edi, dword ptr [rbp-8]\n");
printf(" mov dword ptr [rax], 0\n");
printf(" mov dword ptr [rax+4], 0\n");
printf(" mov qword ptr [rax+8], rdi\n");
printf(" mov qword ptr [rax+16], 0\n");
return;
}
int nargs = 0;
for (Node *arg = node->args; arg; arg = arg->next) {
gen(arg);
nargs++;
}
for (int i = nargs - 1; i >= 0; i--)
printf(" pop %s\n", argreg8[i]);
// We need to align RSP to a 16 byte boundary before
// calling a function because it is an ABI requirement.
// RAX is set to 0 for variadic function.
int seq = labelseq++;
printf(" mov rax, rsp\n");
printf(" and rax, 15\n");
printf(" jnz .L.call.%d\n", seq);
printf(" mov rax, 0\n");
printf(" call %s\n", node->funcname);
printf(" jmp .L.end.%d\n", seq);
printf(".L.call.%d:\n", seq);
printf(" sub rsp, 8\n");
printf(" mov rax, 0\n");
printf(" call %s\n", node->funcname);
printf(" add rsp, 8\n");
printf(".L.end.%d:\n", seq);
if (node->ty->kind == TY_BOOL)
printf(" movzb rax, al\n");
printf(" push rax\n");
if (node->ty->kind != TY_VOID)
truncate(node->ty);
return;
}
case ND_RETURN:
if (node->lhs) {
gen(node->lhs);
printf(" pop rax\n");
}
printf(" jmp .L.return.%s\n", funcname);
return;
case ND_CAST:
gen(node->lhs);
truncate(node->ty);
return;
}
gen(node->lhs);
gen(node->rhs);
gen_binary(node);
}
static void emit_data(Program *prog) {
for (VarList *vl = prog->globals; vl; vl = vl->next)
if (!vl->var->is_static)
printf(".global %s\n", vl->var->name);
printf(".bss\n");
for (VarList *vl = prog->globals; vl; vl = vl->next) {
Var *var = vl->var;
if (var->initializer)
continue;
printf(".align %d\n", var->ty->align);
printf("%s:\n", var->name);
printf(" .zero %d\n", var->ty->size);
}
printf(".data\n");
for (VarList *vl = prog->globals; vl; vl = vl->next) {
Var *var = vl->var;
if (!var->initializer)
continue;
printf(".align %d\n", var->ty->align);
printf("%s:\n", var->name);
for (Initializer *init = var->initializer; init; init = init->next) {
if (init->label)
printf(" .quad %s%+ld\n", init->label, init->addend);
else if (init->sz == 1)
printf(" .byte %ld\n", init->val);
else
printf(" .%dbyte %ld\n", init->sz, init->val);
}
}
}
static void load_arg(Var *var, int idx) {
int sz = var->ty->size;
if (sz == 1) {
printf(" mov [rbp-%d], %s\n", var->offset, argreg1[idx]);
} else if (sz == 2) {
printf(" mov [rbp-%d], %s\n", var->offset, argreg2[idx]);
} else if (sz == 4) {
printf(" mov [rbp-%d], %s\n", var->offset, argreg4[idx]);
} else {
assert(sz == 8);
printf(" mov [rbp-%d], %s\n", var->offset, argreg8[idx]);
}
}
static void emit_text(Program *prog) {
printf(".text\n");
for (Function *fn = prog->fns; fn; fn = fn->next) {
if (!fn->is_static)
printf(".global %s\n", fn->name);
printf("%s:\n", fn->name);
funcname = fn->name;
// Prologue
printf(" push rbp\n");
printf(" mov rbp, rsp\n");
printf(" sub rsp, %d\n", fn->stack_size);
// Save arg registers if function is variadic
if (fn->has_varargs) {
int n = 0;
for (VarList *vl = fn->params; vl; vl = vl->next)
n++;
printf("mov dword ptr [rbp-8], %d\n", n * 8);
printf("mov [rbp-16], r9\n");
printf("mov [rbp-24], r8\n");
printf("mov [rbp-32], rcx\n");
printf("mov [rbp-40], rdx\n");
printf("mov [rbp-48], rsi\n");
printf("mov [rbp-56], rdi\n");
}
// Push arguments to the stack
int i = 0;
for (VarList *vl = fn->params; vl; vl = vl->next)
load_arg(vl->var, i++);
// Emit code
for (Node *node = fn->node; node; node = node->next)
gen(node);
// Epilogue
printf(".L.return.%s:\n", funcname);
printf(" mov rsp, rbp\n");
printf(" pop rbp\n");
printf(" ret\n");
}
}
void codegen(Program *prog) {
printf(".intel_syntax noprefix\n");
emit_data(prog);
emit_text(prog);
}