-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.c
369 lines (333 loc) · 10.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
#include "9cc.h"
static int label_seq = 0;
static char *arg_reg64[] = {"rdi", "rsi", "rdx", "rcx", "r8", "r9"};
static char *arg_reg32[] = {"edi", "esi", "edx", "ecx", "r8d", "r9d"};
static char *arg_reg8[] = {"dil", "sil", "dl", "cl", "r8b", "r9b"};
char *nl2space(char *str) {
char *first = str;
while (*str) {
if (isspace(*str))
*str = ' ';
str++;
}
return first;
}
Function *current_fn;
void gen_expr(Node *node);
void gen_stmt(Node *node);
void gen_addr(Node *node) {
if (node->code) printf("# code addr: %s \n", nl2space(node->code));
if (node->kind == ND_DEREF) {
gen_expr(node->lhs);
return;
}
if (node->var->is_local) {
printf(" lea rax, [rbp-%d]\n", node->var->offset);
} else {
printf(" mov rax, offset %s\n", node->var->name);
}
printf(" push rax\n");
}
/**
* スタックの先頭にあるアドレスからtyのサイズに応じてデータを読む。tyが配列の場合は何も読まない。
* @param ty
*/
void load(Type *ty) {
// 変数が配列の時はレジスタに読み込めないので何もしない。結果的にアドレスが返る。
if (ty->kind == TY_ARRAY) return;
printf(" pop rax\n");
switch (ty->size) {
case 1:
// charの場合は正負不要だと思うのでmovzxを使う
// レジスタがeaxでいいのかは要検討
printf(" movzx eax, BYTE PTR [rax]\n");
break;
case 4:
printf(" mov eax, [rax]\n");
break;
case 8:
printf(" mov rax, [rax]\n");
break;
}
printf(" push rax\n");
}
void gen_expr(Node *node) {
if (node->code) {
printf("# code expr: %s \n", nl2space(node->code));
}
switch (node->kind) {
case ND_NUM:
printf(" push %d\n", node->val);
return;
case ND_VAR:
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:
if (node->ty->kind == TY_ARRAY) error("配列への値の保存");
gen_addr(node->lhs);
gen_expr(node->rhs);
printf(" pop rdi\n");
printf(" pop rax\n");
switch (node->ty->size) {
case 1:
printf(" mov [rax], dil\n");
break;
case 4:
printf(" mov [rax], edi\n");
break;
case 8:
printf(" mov [rax], rdi\n");
break;
}
printf(" push rdi\n");
return;
case ND_FUNC_CALL: {
// 引数の数だけスタックに値を積む
int arg_count = 0;
for (Node *arg = node->args; arg; arg = arg->next) {
arg_count += 1;
gen_expr(arg);
}
for (int i = 0; i < arg_count; i++) {
printf(" pop %s\n", arg_reg64[arg_count - i - 1]);
}
// rspを16の倍数にするための処理 popやpushが8バイト事のアドレスの移動なので8で割り切れないことは無い想定
int seq = label_seq++;
// 分岐
printf("# call %s\n", node->func_name);
printf(" mov rax, rsp\n");
printf(" and rax, 15\n");
printf(" jnz .L.call.%d\n", seq);
// 16で割り切れるとき
printf(" mov rax, 0\n");
printf(" call %s\n", node->func_name);
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->func_name);
printf(" add rsp, 8\n"); // 元に戻す
printf(".L.end.%d:\n", seq);
printf(" push rax\n"); // raxに入ってる返り値をスタックに積む
printf("# end call %s\n", node->func_name);
return;
}
case ND_STMT_EXPR: {
for (Node *n = node->body; n; n = n->next) {
gen_stmt(n);
}
printf(" push rax\n");
return;
}
default:;
}
gen_expr(node->lhs);
gen_expr(node->rhs);
printf(" pop rdi\n");
printf(" pop rax\n");
switch (node->kind) {
case ND_ADD: {
if (node->ty->base)
printf(" imul rdi, %d\n", node->ty->base->size);
printf(" add rax, rdi\n");
break;
}
case ND_SUB:
if (node->ty->base)
printf(" imul rdi, %d\n", node->ty->base->size);
printf(" sub rax, 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_MOD:
printf(" cqo\n");
printf(" idiv rdi\n");
printf(" mov rax, rdx\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:;
}
printf(" push rax\n");
}
void gen_stmt(Node *node) {
if (node->kind != ND_LVAR_DEF && node->code) {
printf("# code stmt: %s \n", nl2space(node->code));
}
switch (node->kind) {
case ND_RETURN: {
gen_expr(node->lhs);
printf(" pop rax\n");
printf(" jmp .L.return.%s\n", current_fn->name);
return;
}
case ND_IF: {
int seq = label_seq++;
printf("# if %d \n", seq);
gen_expr(node->cond);
printf(" pop rax\n");
printf(" cmp rax, 0\n");
if (node->els) {
printf(" je .L.else.if.%d\n", seq);
gen_stmt(node->then);
printf(" jmp .L.end.if.%d\n", seq);
printf(".L.else.if.%d:\n", seq);
gen_stmt(node->els);
} else {
printf(" je .L.end.if.%d\n", seq);
gen_stmt(node->then);
}
printf(".L.end.if.%d:\n", seq);
printf("# end if %d \n", seq);
return;
}
case ND_WHILE: {
int seq = label_seq++;
printf("# while %d \n", seq);
printf(".L.begin.while.%d:\n", seq);
gen_expr(node->cond);
printf(" pop rax\n");
printf(" cmp rax, 0\n");
printf(" je .L.end.while.%d\n", seq);
gen_stmt(node->then);
printf(" jmp .L.begin.while.%d\n", seq);
printf(".L.end.while.%d:\n", seq);
printf("# end while %d \n", seq);
return;
}
case ND_FOR: {
int seq = label_seq++;
printf("# for %d \n", seq);
if (node->init) {
gen_expr(node->init);
printf(" pop rax\n"); // スタック消費
}
printf(".L.begin.for.%d:\n", seq);
if (node->cond) {
gen_expr(node->cond);
printf(" pop rax\n");
printf(" cmp rax, 0\n");
printf(" je .L.end.for.%d\n", seq);
}
gen_stmt(node->then);
if (node->inc) {
gen_expr(node->inc);
printf(" pop rax\n"); // スタック消費
}
printf(" jmp .L.begin.for.%d\n", seq);
printf(".L.end.for.%d:\n", seq);
printf("# end for %d \n", seq);
return;
}
case ND_BLOCK: {
for (Node *n = node->body; n; n = n->next)
gen_stmt(n);
return;
}
case ND_LVAR_DEF:
return;
case ND_EXPR_STMT:
gen_expr(node->lhs);
printf(" pop rax\n"); // スタック溢れ防止のポップ
return;
default:
error("文では無いノード: %s", node->code);
}
}
int args_count(Function *func) {
int count = 0;
for (Var *var = func->args; var; var = var->next) {
count += 1;
}
return count;
}
int local_stack_size(Function *func) {
int size = 0;
for (Var *var = func->locals; var; var = var->next) {
size += var->ty->size;
}
return size;
}
void codegen(Program *pg) {
printf(".intel_syntax noprefix\n");
printf(".data\n");
for (Var *var = pg->globals; var; var = var->next) {
printf("%s:\n", var->name);
if (!var->init_data) {
printf(" .zero %d\n", var->ty->size);
continue;
}
for (int i = 0; i < var->ty->size; i++)
printf(" .byte %d\n", var->init_data[i]);
}
printf(".text\n");
for (Function *func = pg->functions; func; func = func->next) {
printf(".global %s\n", func->name);
printf("%s:\n", func->name);
current_fn = func;
// プロローグ
printf(" push rbp\n");
printf(" mov rbp, rsp\n");
// ローカル変数の領域確保
printf(" sub rsp, %d\n", local_stack_size(func));
int last = args_count(func);
Var *arg = func->args;
for (int i = 0; i < last; i += 1) {
int reg_index = last - i - 1;
switch (arg->ty->size) {
case 1:
printf(" mov [rbp-%d], %s\n", arg->offset, arg_reg8[reg_index]);
break;
case 4:
printf(" mov [rbp-%d], %s\n", arg->offset, arg_reg32[reg_index]);
break;
case 8:
printf(" mov [rbp-%d], %s\n", arg->offset, arg_reg64[reg_index]);
break;
}
arg = arg->next;
}
// 先頭の式から順にコード生成
for (Node *st = func->block->body; st; st = st->next) {
gen_stmt(st);
}
// エピローグ
// 最後の式の結果がRAXに残っているのでそれが返り値になる
printf(".L.return.%s:\n", func->name);
printf(" mov rsp, rbp\n");
printf(" pop rbp\n");
printf(" ret\n");
}
}