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
|
#include "codegen.h"
#include "common.h"
#include "preprocess.h"
enum GenMode {
GenMode_lval,
GenMode_rval,
};
typedef enum GenMode GenMode;
struct CodeGen {
FILE* out;
int next_label;
int* loop_labels;
AstNode* current_func;
};
typedef struct CodeGen CodeGen;
CodeGen* codegen_new(FILE* out) {
CodeGen* g = calloc(1, sizeof(CodeGen));
g->out = out;
g->next_label = 1;
g->loop_labels = calloc(1024, sizeof(int));
return g;
}
int codegen_new_label(CodeGen* g) {
int new_label = g->next_label;
++g->next_label;
return new_label;
}
void codegen_expr(CodeGen* g, AstNode* ast, GenMode gen_mode);
void codegen_stmt(CodeGen* g, AstNode* ast);
const char* param_reg(int n) {
if (n == 0) {
return "rdi";
} else if (n == 1) {
return "rsi";
} else if (n == 2) {
return "rdx";
} else if (n == 3) {
return "rcx";
} else if (n == 4) {
return "r8";
} else if (n == 5) {
return "r9";
} else {
unreachable();
}
}
void codegen_func_prologue(CodeGen* g, AstNode* ast) {
fprintf(g->out, " push rbp\n");
fprintf(g->out, " mov rbp, rsp\n");
for (int i = 0; i < ast->node_params->node_len; ++i) {
fprintf(g->out, " push %s\n", param_reg(i));
}
fprintf(g->out, " sub rsp, %d\n", ast->node_stack_size);
}
void codegen_func_epilogue(CodeGen* g, AstNode* ast) {
fprintf(g->out, " mov rsp, rbp\n");
fprintf(g->out, " pop rbp\n");
fprintf(g->out, " ret\n");
}
void codegen_int_expr(CodeGen* g, AstNode* ast) {
fprintf(g->out, " push %d\n", ast->node_int_value);
}
void codegen_str_expr(CodeGen* g, AstNode* ast) {
fprintf(g->out, " mov rax, OFFSET FLAG:.Lstr__%d\n", ast->node_idx);
fprintf(g->out, " push rax\n");
}
void codegen_unary_expr(CodeGen* g, AstNode* ast) {
codegen_expr(g, ast->node_operand, GenMode_rval);
if (ast->node_op == TokenKind_not) {
fprintf(g->out, " pop rax\n");
fprintf(g->out, " mov rdi, 0\n");
fprintf(g->out, " cmp rax, rdi\n");
fprintf(g->out, " sete al\n");
fprintf(g->out, " movzb rax, al\n");
fprintf(g->out, " push rax\n");
} else {
unreachable();
}
}
void codegen_ref_expr(CodeGen* g, AstNode* ast, GenMode gen_mode) {
codegen_expr(g, ast->node_operand, GenMode_lval);
}
// "reg" stores the address of the expression to be pushed.
void codegen_push_expr(CodeGen* g, const char* reg, int size) {
if (size == 1) {
fprintf(g->out, " movsx %s, BYTE PTR [%s]\n", reg, reg);
fprintf(g->out, " push %s\n", reg);
} else if (size == 2) {
fprintf(g->out, " movsx %s, WORD PTR [%s]\n", reg, reg);
fprintf(g->out, " push %s\n", reg);
} else if (size == 4) {
fprintf(g->out, " movsxd %s, DWORD PTR [%s]\n", reg, reg);
fprintf(g->out, " push %s\n", reg);
} else if (size == 8) {
fprintf(g->out, " mov %s, [%s]\n", reg, reg);
fprintf(g->out, " push %s\n", reg);
} else {
// Perform bitwise copy. Use r10 register as temporary space.
// Note: rsp must be aligned to 8.
fprintf(g->out, " sub rsp, %d\n", to_aligned(size, 8));
for (int i = 0; i < size; ++i) {
// Copy a sinle byte from the address that "reg" points to to the stack via r10 register.
fprintf(g->out, " mov r10b, [%s+%d]\n", reg, i);
fprintf(g->out, " mov [rsp+%d], r10b\n", i);
}
}
}
void codegen_lval2rval(CodeGen* g, Type* ty) {
if (ty->kind == TypeKind_array) {
return;
}
fprintf(g->out, " pop rax\n");
codegen_push_expr(g, "rax", type_sizeof(ty));
}
void codegen_deref_expr(CodeGen* g, AstNode* ast, GenMode gen_mode) {
codegen_expr(g, ast->node_operand, GenMode_rval);
if (gen_mode == GenMode_rval) {
codegen_lval2rval(g, ast->node_operand->ty->base);
}
}
void codegen_logical_expr(CodeGen* g, AstNode* ast) {
int label = codegen_new_label(g);
if (ast->node_op == TokenKind_andand) {
codegen_expr(g, ast->node_lhs, GenMode_rval);
fprintf(g->out, " pop rax\n");
fprintf(g->out, " cmp rax, 0\n");
fprintf(g->out, " je .Lelse%d\n", label);
codegen_expr(g, ast->node_rhs, GenMode_rval);
fprintf(g->out, " jmp .Lend%d\n", label);
fprintf(g->out, ".Lelse%d:\n", label);
fprintf(g->out, " push 0\n");
fprintf(g->out, ".Lend%d:\n", label);
} else {
codegen_expr(g, ast->node_lhs, GenMode_rval);
fprintf(g->out, " pop rax\n");
fprintf(g->out, " cmp rax, 0\n");
fprintf(g->out, " je .Lelse%d\n", label);
fprintf(g->out, " push 1\n");
fprintf(g->out, " jmp .Lend%d\n", label);
fprintf(g->out, ".Lelse%d:\n", label);
codegen_expr(g, ast->node_rhs, GenMode_rval);
fprintf(g->out, ".Lend%d:\n", label);
}
}
void codegen_binary_expr(CodeGen* g, AstNode* ast, GenMode gen_mode) {
codegen_expr(g, ast->node_lhs, gen_mode);
codegen_expr(g, ast->node_rhs, gen_mode);
fprintf(g->out, " pop rdi\n");
fprintf(g->out, " pop rax\n");
if (ast->node_op == TokenKind_plus) {
fprintf(g->out, " add rax, rdi\n");
} else if (ast->node_op == TokenKind_minus) {
fprintf(g->out, " sub rax, rdi\n");
} else if (ast->node_op == TokenKind_star) {
fprintf(g->out, " imul rax, rdi\n");
} else if (ast->node_op == TokenKind_slash) {
fprintf(g->out, " cqo\n");
fprintf(g->out, " idiv rdi\n");
} else if (ast->node_op == TokenKind_percent) {
fprintf(g->out, " cqo\n");
fprintf(g->out, " idiv rdi\n");
fprintf(g->out, " mov rax, rdx\n");
} else if (ast->node_op == TokenKind_or) {
fprintf(g->out, " or rax, rdi\n");
} else if (ast->node_op == TokenKind_lshift) {
fprintf(g->out, " mov rcx, rdi\n");
fprintf(g->out, " shl rax, cl\n");
} else if (ast->node_op == TokenKind_rshift) {
// TODO: check if the operand is signed or unsigned
fprintf(g->out, " mov rcx, rdi\n");
fprintf(g->out, " sar rax, cl\n");
} else if (ast->node_op == TokenKind_eq) {
fprintf(g->out, " cmp rax, rdi\n");
fprintf(g->out, " sete al\n");
fprintf(g->out, " movzb rax, al\n");
} else if (ast->node_op == TokenKind_ne) {
fprintf(g->out, " cmp rax, rdi\n");
fprintf(g->out, " setne al\n");
fprintf(g->out, " movzb rax, al\n");
} else if (ast->node_op == TokenKind_lt) {
fprintf(g->out, " cmp rax, rdi\n");
fprintf(g->out, " setl al\n");
fprintf(g->out, " movzb rax, al\n");
} else if (ast->node_op == TokenKind_le) {
fprintf(g->out, " cmp rax, rdi\n");
fprintf(g->out, " setle al\n");
fprintf(g->out, " movzb rax, al\n");
} else {
unreachable();
}
fprintf(g->out, " push rax\n");
}
void codegen_cond_expr(CodeGen* g, AstNode* ast, GenMode gen_mode) {
int label = codegen_new_label(g);
codegen_expr(g, ast->node_cond, GenMode_rval);
fprintf(g->out, " pop rax\n");
fprintf(g->out, " cmp rax, 0\n");
fprintf(g->out, " je .Lelse%d\n", label);
codegen_expr(g, ast->node_then, gen_mode);
fprintf(g->out, " jmp .Lend%d\n", label);
fprintf(g->out, ".Lelse%d:\n", label);
codegen_expr(g, ast->node_else, gen_mode);
fprintf(g->out, ".Lend%d:\n", label);
}
void codegen_assign_expr_helper(CodeGen* g, AstNode* ast) {
if (ast->node_op == TokenKind_assign) {
return;
}
fprintf(g->out, " pop rdi\n");
fprintf(g->out, " push [rsp]\n");
codegen_lval2rval(g, ast->node_lhs->ty);
fprintf(g->out, " pop rax\n");
if (ast->node_op == TokenKind_assign_add) {
fprintf(g->out, " add rax, rdi\n");
} else if (ast->node_op == TokenKind_assign_sub) {
fprintf(g->out, " sub rax, rdi\n");
} else if (ast->node_op == TokenKind_assign_mul) {
fprintf(g->out, " imul rax, rdi\n");
} else if (ast->node_op == TokenKind_assign_div) {
fprintf(g->out, " cqo\n");
fprintf(g->out, " idiv rdi\n");
} else if (ast->node_op == TokenKind_assign_mod) {
fprintf(g->out, " cqo\n");
fprintf(g->out, " idiv rdi\n");
fprintf(g->out, " mov rax, rdx\n");
} else {
unreachable();
}
fprintf(g->out, " push rax\n");
}
void codegen_assign_expr(CodeGen* g, AstNode* ast) {
int sizeof_lhs = type_sizeof(ast->node_lhs->ty);
int sizeof_rhs = type_sizeof(ast->node_rhs->ty);
codegen_expr(g, ast->node_lhs, GenMode_lval);
codegen_expr(g, ast->node_rhs, GenMode_rval);
codegen_assign_expr_helper(g, ast);
if (sizeof_lhs == 1) {
fprintf(g->out, " pop rdi\n");
fprintf(g->out, " pop rax\n");
fprintf(g->out, " mov BYTE PTR [rax], dil\n");
fprintf(g->out, " push rdi\n");
} else if (sizeof_lhs == 2) {
fprintf(g->out, " pop rdi\n");
fprintf(g->out, " pop rax\n");
fprintf(g->out, " mov WORD PTR [rax], di\n");
fprintf(g->out, " push rdi\n");
} else if (sizeof_lhs == 4) {
fprintf(g->out, " pop rdi\n");
fprintf(g->out, " pop rax\n");
fprintf(g->out, " mov DWORD PTR [rax], edi\n");
fprintf(g->out, " push rdi\n");
} else if (sizeof_lhs == 8) {
fprintf(g->out, " pop rdi\n");
fprintf(g->out, " pop rax\n");
fprintf(g->out, " mov [rax], rdi\n");
fprintf(g->out, " push rdi\n");
} else {
if (ast->node_op != TokenKind_assign) {
unimplemented();
}
// Note: rsp must be aligned to 8.
int aligned_size = to_aligned(sizeof_lhs, 8);
fprintf(g->out, " mov rax, [rsp+%d]\n", aligned_size);
// Perform bitwise copy. Use r10 register as temporary space.
for (int i = 0; i < aligned_size; ++i) {
// Copy a sinle byte from the stack to the address that rax points to via r10 register.
fprintf(g->out, " mov r10b, [rsp+%d]\n", i);
fprintf(g->out, " mov [rax+%d], r10b\n", i);
}
// Pop the RHS value and the LHS address.
fprintf(g->out, " add rsp, %d\n", aligned_size + 8);
// TODO: dummy
fprintf(g->out, " push 0\n");
}
}
void codegen_func_call(CodeGen* g, AstNode* ast) {
const char* func_name = ast->name;
if (strcmp(func_name, "__ducc_va_start") == 0) {
int stack_size = g->current_func->node_stack_size;
fprintf(g->out, " # __ducc_va_start BEGIN\n");
for (int i = 0; i < 6; ++i) {
fprintf(g->out, " mov rax, %s\n", param_reg(i));
fprintf(g->out, " mov [rbp-%d], rax\n", 8 + (stack_size - 4 - i) * 8);
}
AstNode* va_list_args = ast->node_args->node_items;
codegen_expr(g, va_list_args, GenMode_lval);
fprintf(g->out, " pop rdi\n");
fprintf(g->out, " mov rax, rbp\n");
fprintf(g->out, " sub rax, %d\n", 8 + (stack_size - 1) * 8);
fprintf(g->out, " mov [rdi], rax\n");
fprintf(g->out, " mov DWORD PTR [rax], 8\n");
fprintf(g->out, " mov DWORD PTR [rax+4], 0\n");
fprintf(g->out, " mov QWORD PTR [rax+8], 0\n");
fprintf(g->out, " mov rdi, rbp\n");
fprintf(g->out, " sub rdi, %d\n", 8 + (stack_size - 4) * 8);
fprintf(g->out, " mov QWORD PTR [rax+16], rdi\n");
fprintf(g->out, " # __ducc_va_start END\n");
return;
}
AstNode* args = ast->node_args;
for (int i = 0; i < args->node_len; ++i) {
AstNode* arg = args->node_items + i;
codegen_expr(g, arg, GenMode_rval);
}
for (int i = args->node_len - 1; i >= 0; --i) {
fprintf(g->out, " pop %s\n", param_reg(i));
}
int label = codegen_new_label(g);
fprintf(g->out, " mov rax, rsp\n");
fprintf(g->out, " and rax, 15\n");
fprintf(g->out, " cmp rax, 0\n");
fprintf(g->out, " je .Laligned%d\n", label);
fprintf(g->out, " mov rax, 0\n");
fprintf(g->out, " sub rsp, 8\n");
fprintf(g->out, " call %s\n", func_name);
fprintf(g->out, " add rsp, 8\n");
fprintf(g->out, " push rax\n");
fprintf(g->out, " jmp .Lend%d\n", label);
fprintf(g->out, ".Laligned%d:\n", label);
fprintf(g->out, " mov rax, 0\n");
fprintf(g->out, " call %s\n", func_name);
fprintf(g->out, " push rax\n");
fprintf(g->out, ".Lend%d:\n", label);
}
void codegen_lvar(CodeGen* g, AstNode* ast, GenMode gen_mode) {
fprintf(g->out, " mov rax, rbp\n");
fprintf(g->out, " sub rax, %d\n", ast->node_stack_offset);
fprintf(g->out, " push rax\n");
if (gen_mode == GenMode_rval) {
codegen_lval2rval(g, ast->ty);
}
}
void codegen_gvar(CodeGen* g, AstNode* ast, GenMode gen_mode) {
fprintf(g->out, " lea rax, %s[rip]\n", ast->name);
fprintf(g->out, " push rax\n");
if (gen_mode == GenMode_rval) {
codegen_lval2rval(g, ast->ty);
}
}
void codegen_composite_expr(CodeGen* g, AstNode* ast) {
// Standard C does not have composite expression, but ducc internally has.
for (int i = 0; i < ast->node_len; ++i) {
AstNode* expr = ast->node_items + i;
codegen_expr(g, expr, GenMode_rval);
if (i != ast->node_len - 1) {
// TODO: the expression on the stack can be more than 8 bytes.
fprintf(g->out, " pop rax\n");
}
}
}
void codegen_expr(CodeGen* g, AstNode* ast, GenMode gen_mode) {
if (ast->kind == AstNodeKind_int_expr) {
codegen_int_expr(g, ast);
} else if (ast->kind == AstNodeKind_str_expr) {
codegen_str_expr(g, ast);
} else if (ast->kind == AstNodeKind_unary_expr) {
codegen_unary_expr(g, ast);
} else if (ast->kind == AstNodeKind_ref_expr) {
codegen_ref_expr(g, ast, gen_mode);
} else if (ast->kind == AstNodeKind_deref_expr) {
codegen_deref_expr(g, ast, gen_mode);
} else if (ast->kind == AstNodeKind_binary_expr) {
codegen_binary_expr(g, ast, gen_mode);
} else if (ast->kind == AstNodeKind_cond_expr) {
codegen_cond_expr(g, ast, gen_mode);
} else if (ast->kind == AstNodeKind_logical_expr) {
codegen_logical_expr(g, ast);
} else if (ast->kind == AstNodeKind_assign_expr) {
codegen_assign_expr(g, ast);
} else if (ast->kind == AstNodeKind_func_call) {
codegen_func_call(g, ast);
} else if (ast->kind == AstNodeKind_lvar) {
codegen_lvar(g, ast, gen_mode);
} else if (ast->kind == AstNodeKind_gvar) {
codegen_gvar(g, ast, gen_mode);
} else if (ast->kind == AstNodeKind_list) {
codegen_composite_expr(g, ast);
} else {
unreachable();
}
}
void codegen_return_stmt(CodeGen* g, AstNode* ast) {
if (ast->node_expr) {
codegen_expr(g, ast->node_expr, GenMode_rval);
fprintf(g->out, " pop rax\n");
}
codegen_func_epilogue(g, ast);
}
void codegen_if_stmt(CodeGen* g, AstNode* ast) {
int label = codegen_new_label(g);
codegen_expr(g, ast->node_cond, GenMode_rval);
fprintf(g->out, " pop rax\n");
fprintf(g->out, " cmp rax, 0\n");
fprintf(g->out, " je .Lelse%d\n", label);
codegen_stmt(g, ast->node_then);
fprintf(g->out, " jmp .Lend%d\n", label);
fprintf(g->out, ".Lelse%d:\n", label);
if (ast->node_else) {
codegen_stmt(g, ast->node_else);
}
fprintf(g->out, ".Lend%d:\n", label);
}
void codegen_for_stmt(CodeGen* g, AstNode* ast) {
int label = codegen_new_label(g);
++g->loop_labels;
*g->loop_labels = label;
if (ast->node_init) {
codegen_expr(g, ast->node_init, GenMode_rval);
fprintf(g->out, " pop rax\n");
}
fprintf(g->out, ".Lbegin%d:\n", label);
codegen_expr(g, ast->node_cond, GenMode_rval);
fprintf(g->out, " pop rax\n");
fprintf(g->out, " cmp rax, 0\n");
fprintf(g->out, " je .Lend%d\n", label);
codegen_stmt(g, ast->node_body);
fprintf(g->out, ".Lcontinue%d:\n", label);
if (ast->node_update) {
codegen_expr(g, ast->node_update, GenMode_rval);
fprintf(g->out, " pop rax\n");
}
fprintf(g->out, " jmp .Lbegin%d\n", label);
fprintf(g->out, ".Lend%d:\n", label);
--g->loop_labels;
}
void codegen_do_while_stmt(CodeGen* g, AstNode* ast) {
int label = codegen_new_label(g);
++g->loop_labels;
*g->loop_labels = label;
fprintf(g->out, ".Lbegin%d:\n", label);
codegen_stmt(g, ast->node_body);
fprintf(g->out, ".Lcontinue%d:\n", label);
codegen_expr(g, ast->node_cond, GenMode_rval);
fprintf(g->out, " pop rax\n");
fprintf(g->out, " cmp rax, 0\n");
fprintf(g->out, " je .Lend%d\n", label);
fprintf(g->out, " jmp .Lbegin%d\n", label);
fprintf(g->out, ".Lend%d:\n", label);
--g->loop_labels;
}
void codegen_break_stmt(CodeGen* g, AstNode* ast) {
int label = *g->loop_labels;
fprintf(g->out, " jmp .Lend%d\n", label);
}
void codegen_continue_stmt(CodeGen* g, AstNode* ast) {
int label = *g->loop_labels;
fprintf(g->out, " jmp .Lcontinue%d\n", label);
}
void codegen_expr_stmt(CodeGen* g, AstNode* ast) {
codegen_expr(g, ast->node_expr, GenMode_rval);
// TODO: the expression on the stack can be more than 8 bytes.
fprintf(g->out, " pop rax\n");
}
void codegen_var_decl(CodeGen* g, AstNode* ast) {
}
void codegen_nop(CodeGen* g, AstNode* ast) {
}
void codegen_block_stmt(CodeGen* g, AstNode* ast) {
for (int i = 0; i < ast->node_len; ++i) {
AstNode* stmt = ast->node_items + i;
codegen_stmt(g, stmt);
}
}
void codegen_stmt(CodeGen* g, AstNode* ast) {
if (ast->kind == AstNodeKind_list) {
codegen_block_stmt(g, ast);
} else if (ast->kind == AstNodeKind_return_stmt) {
codegen_return_stmt(g, ast);
} else if (ast->kind == AstNodeKind_if_stmt) {
codegen_if_stmt(g, ast);
} else if (ast->kind == AstNodeKind_for_stmt) {
codegen_for_stmt(g, ast);
} else if (ast->kind == AstNodeKind_do_while_stmt) {
codegen_do_while_stmt(g, ast);
} else if (ast->kind == AstNodeKind_break_stmt) {
codegen_break_stmt(g, ast);
} else if (ast->kind == AstNodeKind_continue_stmt) {
codegen_continue_stmt(g, ast);
} else if (ast->kind == AstNodeKind_expr_stmt) {
codegen_expr_stmt(g, ast);
} else if (ast->kind == AstNodeKind_lvar_decl) {
codegen_var_decl(g, ast);
} else if (ast->kind == AstNodeKind_nop) {
codegen_nop(g, ast);
} else {
unreachable();
}
}
void codegen_func(CodeGen* g, AstNode* ast) {
g->current_func = ast;
fprintf(g->out, ".globl %s\n", ast->name);
fprintf(g->out, "%s:\n", ast->name);
codegen_func_prologue(g, ast);
codegen_stmt(g, ast->node_body);
if (strcmp(ast->name, "main") == 0) {
// C99: 5.1.2.2.3
fprintf(g->out, " mov rax, 0\n");
}
codegen_func_epilogue(g, ast);
fprintf(g->out, "\n");
g->current_func = NULL;
}
void codegen(Program* prog, FILE* out) {
CodeGen* g = codegen_new(out);
fprintf(g->out, ".intel_syntax noprefix\n\n");
// For GNU ld:
// https://sourceware.org/binutils/docs/ld/Options.html
fprintf(g->out, ".section .note.GNU-stack,\"\",@progbits\n\n");
fprintf(g->out, ".section .rodata\n\n");
for (int i = 0; prog->str_literals[i]; ++i) {
fprintf(g->out, ".Lstr__%d:\n", i + 1);
fprintf(g->out, " .string \"%s\"\n\n", prog->str_literals[i]);
}
fprintf(g->out, ".data\n\n");
for (int i = 0; i < prog->vars->node_len; ++i) {
AstNode* var = prog->vars->node_items + i;
if (var->node_expr) {
if (var->ty->kind == TypeKind_char)
fprintf(g->out, " %s: .byte %d\n", var->name, var->node_expr->node_int_value);
else if (var->ty->kind == TypeKind_short)
fprintf(g->out, " %s: .word %d\n", var->name, var->node_expr->node_int_value);
else if (var->ty->kind == TypeKind_int)
fprintf(g->out, " %s: .int %d\n", var->name, var->node_expr->node_int_value);
else
unimplemented();
} else {
fprintf(g->out, " %s: .zero %d\n", var->name, type_sizeof(var->ty));
}
}
fprintf(g->out, ".text\n\n");
for (int i = 0; i < prog->funcs->node_len; ++i) {
AstNode* func = prog->funcs->node_items + i;
codegen_func(g, func);
}
}
|