-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.c
188 lines (174 loc) · 4.43 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
#include "chibicc.h"
int labelseq = 0;
char *funcname;
// Push the given node's address to the stack.
void gen_addr(Node *node) {
if (node->kind != ND_VAR)
error("not an lvalue");
printf("\tsub x0, x29, #%d\n", node->var->offset);
printf("\tstr x0, [sp, #-16]!\n");
return;
}
void load() {
printf("\tldr x0, [sp], #16\n");
printf("\tldr x0, [x0]\n");
printf("\tstr x0, [sp, #-16]!\n");
}
void store() {
printf("\tldr x1, [sp], #16\n");
printf("\tldr x0, [sp], #0\n");
printf("\tstr x1, [x0]\n");
printf("\tstr x1, [sp, #-16]!\n");
}
// Generate code for a given node.
void gen(Node *node) {
if (!node)
return;
switch (node->kind) {
case ND_NUM:
printf("\tmov w0, #%d\n", node->val);
printf("\tstr w0, [sp, #-16]!\n");
return;
case ND_STMT:
gen(node->lhs);
return;
case ND_VAR:
gen_addr(node);
load();
return;
case ND_ASSIGN:
gen_addr(node->lhs);
gen(node->rhs);
store();
return;
case ND_IF:
labelseq++;
if (node->els) {
gen(node->cond);
printf("\tldr w0, [sp], #16\n");
printf("\tcbz w0, Lelse%d\n", labelseq);
gen(node->then);
printf("\tb\tLend%d:\n", labelseq);
printf("Lelse%d:\n", labelseq);
gen(node->els);
printf("Lend%d:\n", labelseq);
} else {
gen(node->cond);
printf("\tldr w0, [sp], #16\n");
printf("\tcbz w0, Lend%d\n", labelseq);
gen(node->then);
printf("Lend%d:\n", labelseq);
}
return;
case ND_WHILE:
labelseq++;
printf("Lbegin%d:\n", labelseq);
gen(node->cond);
printf("\tldr w0, [sp], #16\n");
printf("\tcbz w0, Lend%d\n", labelseq);
gen(node->then);
printf("\tb\tLbegin%d\n", labelseq);
printf("Lend%d:\n", labelseq);
return;
case ND_FOR:
labelseq++;
gen(node->init);
printf("Lbegin%d:\n", labelseq);
if (node->cond) {
gen(node->cond);
printf("\tldr w0, [sp], #16\n");
printf("\tcbz w0, Lend%d\n", labelseq);
}
gen(node->then);
gen(node->inc);
printf("\tb\tLbegin%d\n", labelseq);
printf("Lend%d:\n", labelseq);
return;
case ND_BLOCK:
for (Node *n = node->body; n; n = n->next)
gen(n);
return;
case ND_CALL: {
char *argreg[] = {"w0", "w1", "w2", "w3", "w4", "w5"};
int nargs = 0;
for (Node *arg = node->args; arg; arg = arg->next) {
gen(arg);
nargs++;
}
for (int i = 0; i < nargs; i++)
printf("\tldr\t%s, [sp, #%d]\n", argreg[i], 16 * (nargs - 1 - i));
printf("\tadd\tsp, sp, #%d\n", 16 * nargs);
printf("\tbl\t_%s\n", node->funcname);
return;
}
case ND_RT:
gen(node->lhs);
printf("\tb\tLreturn_%s\n", funcname);
return;
default:;
}
gen(node->lhs);
gen(node->rhs);
printf("\tldr w1, [sp], #16\n");
printf("\tldr w0, [sp], #16\n");
switch (node->kind) {
case ND_ADD:
printf("\tadd w0, w0, w1\n");
break;
case ND_SUB:
printf("\tsub w0, w0, w1\n");
break;
case ND_MUL:
printf("\tmul w0, w0, w1\n");
break;
case ND_DIV:
printf("\tudiv w0, w0, w1\n");
break;
case ND_EQ:
printf("\tsubs\tw0, w0, w1\n");
printf("\tcset\tw0, eq\n");
printf("\tand w0, w0, #0x1\n");
break;
case ND_NE:
printf("\tsubs\tw0, w0, w1\n");
printf("\tcset\tw0, ne\n");
printf("\tand w0, w0, #0x1\n");
break;
case ND_LT:
printf("\tsubs\tw0, w0, w1\n");
printf("\tcset\tw0, lt\n");
printf("\tand w0, w0, #0x1\n");
break;
case ND_LE:
printf("\tsubs\tw0, w0, w1\n");
printf("\tcset\tw0, le\n");
printf("\tand w0, w0, #0x1\n");
break;
default:
error("unexpected node kind: %d", node->kind);
}
printf("\tstr w0, [sp, #-16]!\n");
}
void codegen(Function *prog) {
for (Function *fn = prog; fn; fn = fn->next) {
funcname = fn->name;
// Function header
printf("\t.global _%s\n", funcname);
printf("\t.p2align 2\n");
printf("_%s:\n", funcname);
int stack_size = fn->stack_size + 16;
// Prologue
printf("\tsub\tsp, sp, #%d\n", stack_size); // Reserve
printf("\tstp\tx29, x30, [sp, #%d]\n", fn->stack_size);
printf("\tadd\tx29, sp, #%d\n", fn->stack_size);
// Emit code
for (Node *n = fn->node; n; n = n->next)
gen(n);
// Epilogue
printf("Lreturn_%s:\n", funcname);
printf("\tsub\tsp, x29, #%d\n", fn->stack_size);
printf("\tldp\tx29, x30, [sp, #%d]\n", fn->stack_size);
printf("\tadd\tsp, sp, #%d\n", stack_size);
printf("\tret\n"); // default to X30
}
}