-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.c
299 lines (271 loc) · 8.12 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
#include "9cc.h"
static char *argreg1[] = {"dil", "sil", "dl", "cl", "r8b", "r9b"};
static char *argreg8[] = {"rdi", "rsi", "rdx", "rcx", "r8", "r9"};
static int labelseq = 1;
static char *funcname;
static void gen(Node *node);
static void gen_addr(Node *node) {
switch (node->kind) {
case ND_VAR: {
Var *var = node->var;
if (var->is_local) {
printf(" lea rax, [rbp-%d]\n", node->var->offset);
printf(" push rax\n");
} else {
printf(" push offset %s\n", var->name);
}
return;
}
case ND_DEREF:
gen(node->lhs);
return;
default:
error_tok(node->tok, "代入の左辺値が変数ではありません");
}
}
static void gen_lval(Node *node) {
if (node->ty->kind == TY_ARRAY)
error_tok(node->tok, "左辺値ではありません");
gen_addr(node);
}
static void load(Type *ty) {
printf(" pop rax\n");
if (ty->size == 1)
printf(" movsx rax, byte ptr [rax]\n");
else
printf(" mov rax, [rax]\n");
printf(" push rax\n");
}
static void store(Type *ty) {
printf(" pop rdi\n");
printf(" pop rax\n");
if (ty->size == 1)
printf(" mov [rax], dil\n");
else
printf(" mov [rax], rdi\n");
printf(" push rdi\n");
}
static void gen(Node *node) {
switch(node->kind) {
case ND_NULL:
return;
case ND_NUM:
printf(" push %ld\n", node->val);
return;
case ND_EXPR_STMT:
gen(node->lhs);
printf(" add rsp, 8\n");
return;
case ND_VAR:
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_RETURN:
gen(node->lhs);
printf(" pop rax\n");
printf(" jmp .L.return.%s\n", funcname);
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_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++;
printf(".L.begin.%d:\n", seq);
gen(node->cond);
printf(" pop rax\n");
printf(" cmp rax, 0\n");
printf(" je .L.end.%d\n", seq);
gen(node->then);
printf(" jmp .L.begin.%d\n", seq);
printf(".L.end.%d:\n", seq);
return;
}
case ND_FOR: {
int seq = labelseq++;
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.end.%d\n", seq);
}
gen(node->then);
if (node->inc)
gen(node->inc);
printf(" jmp .L.begin.%d\n", seq);
printf(".L.end.%d:\n", seq);
return;
}
case ND_BLOCK:
for (Node *n = node->body; n; n = n->next)
gen(n);
return;
case ND_FUNCALL: {
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]);
}
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);
printf(" push rax\n");
return;
}
default:
break;
}
gen(node->lhs);
gen(node->rhs);
printf(" pop rdi\n");
printf(" pop rax\n");
switch(node->kind) {
case ND_ADD:
printf(" add rax, rdi\n");
break;
case ND_PTR_ADD:
printf(" imul rdi, %d\n", node->ty->base->size);
printf(" add rax, rdi\n");
break;
case ND_SUB:
printf(" sub rax, rdi\n");
break;
case ND_PTR_SUB:
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:
printf(" imul rax, rdi\n");
break;
case ND_DIV:
printf(" cqo\n");
printf(" idiv rdi\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_LE:
printf(" cmp rax, rdi\n");
printf(" setle 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;
default:
break;
}
printf(" push rax\n");
}
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 {
printf(" mov [rbp-%d], %s\n", var->offset, argreg8[idx]);
}
}
static void emit_data(Program *prog) {
printf(".data\n");
for(VarList *vl = prog->globals; vl; vl = vl->next) {
Var *var = vl->var;
printf("%s:\n", var->name);
if (!var->contents) {
printf(" .zero %d\n", var->ty->size);
continue;
}
for (int i = 0; i < var->cont_len; i++) {
printf(" .byte %d\n", var->contents[i]);
}
}
}
static void emit_text(Program *prog) {
for (Function *fn = prog->fns; fn; fn = fn->next) {
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);
int i = 0;
for (VarList *vl = fn->params; vl; vl = vl->next) {
load_arg(vl->var, i++);
}
for (Node *n = fn->node; n; n = n->next)
gen(n);
// 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);
}