-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.c
468 lines (414 loc) · 13.4 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
/**
* @file codegen.c
* @brief Implementation of the code generation phase for the compiler.
*
* This file contains functions for generating assembly code from an Abstract
* Syntax Tree (AST). It processes various types of statements and expressions,
* as specified in the BNF grammar file, to produce the final output assembly code.
*
* @author Mohamed Gamal
* @project ToyCComp
* @date 2024-12-24
*
* @note This file is part of the ToyCComp project, which is a hobby compiler
* project designed for learning and experimenting with compiler design.
*/
#include "asm.h"
#include "ast.h"
#include "codegen.h"
#include "debug.h"
#include "symtab.h"
#include <math.h>
#include <stdlib.h>
//////////////////////////////
// All functions protptypes //
//////////////////////////////
static ASTNode_t *get_loop_context(ASTNode_t *root);
static void generate_statements(CodeGenerator_t *gen, ASTNode_t *root);
static void generate_statement(CodeGenerator_t *gen, ASTNode_t *root);
static void generate_stmt_if(CodeGenerator_t *gen, ASTNode_t *root);
static void generate_stmt_while(CodeGenerator_t *gen, ASTNode_t *root);
static void generate_stmt_do_while(CodeGenerator_t *gen, ASTNode_t *root);
static void generate_stmt_for(CodeGenerator_t *gen, ASTNode_t *root);
static void generate_stmt_break(CodeGenerator_t *gen, ASTNode_t *root);
static void generate_stmt_assign(CodeGenerator_t *gen, ASTNode_t *root);
static void generate_stmt_return(CodeGenerator_t *gen, ASTNode_t *root);
static void generate_stmt_fcall(CodeGenerator_t *gen, ASTNode_t *root);
static Register generate_expr(CodeGenerator_t *gen, ASTNode_t *root);
static Register generate_expr_comparison(CodeGenerator_t *gen, ASTNode_t *root);
static Register generate_expr_arithmetic(CodeGenerator_t *gen, ASTNode_t *root);
static Register generate_expr_fcall(CodeGenerator_t *gen, ASTNode_t *root);
static Register generate_expr_addressof(CodeGenerator_t *gen, ASTNode_t *root);
static Register generate_expr_ptrdref(CodeGenerator_t *gen, ASTNode_t *root);
static Register generate_expr_arr_index(CodeGenerator_t *gen, ASTNode_t *root);
static void generate_declerations(CodeGenerator_t *gen, ASTNode_t *root);
static void generate_decleration(CodeGenerator_t *gen, ASTNode_t *root);
static void generate_decl_func(CodeGenerator_t *gen, ASTNode_t *root);
static void generate_decl_var(CodeGenerator_t *gen, ASTNode_t *root);
//////////////////////////////
//////////////////////////////
static bool return_called_flag = false;
static ASTNode_t *get_loop_context(ASTNode_t *node)
{
if (node == NULL)
return NULL;
node = node->parent;
while (node != NULL)
{
if (ASTCheckLoopContext(node->type))
return node;
node = node->parent;
}
return NULL;
}
static Register generate_expr(CodeGenerator_t *gen, ASTNode_t *root)
{
switch (root->type)
{
case AST_FUNC_CALL:
return generate_expr_fcall(gen, root);
case AST_ADDRESSOF:
return generate_expr_addressof(gen, root);
default:
return generate_expr_comparison(gen, root);
}
}
static Register generate_expr_comparison(CodeGenerator_t *gen, ASTNode_t *root)
{
Register left, right;
if (
root->type != AST_COMP_EQ &&
root->type != AST_COMP_NE &&
root->type != AST_COMP_GT &&
root->type != AST_COMP_GE &&
root->type != AST_COMP_LT &&
root->type != AST_COMP_LE)
{
return generate_expr_arithmetic(gen, root);
}
left = generate_expr(gen, root->left);
right = generate_expr(gen, root->right);
switch (root->type)
{
case AST_COMP_EQ:
return asm_comp_eq(gen, left, right);
case AST_COMP_NE:
return asm_comp_ne(gen, left, right);
case AST_COMP_GT:
return asm_comp_gt(gen, left, right);
case AST_COMP_GE:
return asm_comp_ge(gen, left, right);
case AST_COMP_LT:
return asm_comp_lt(gen, left, right);
case AST_COMP_LE:
return asm_comp_le(gen, left, right);
default:
debug_print(
SEV_ERROR,
"[CG] Unexpected type: %s in generate_expr_comparison\n",
NodeToString(*root));
exit(1);
}
}
static Register generate_expr_arithmetic(CodeGenerator_t *gen, ASTNode_t *root)
{
Register left, right;
if (
root->left &&
root->type != AST_FUNC_CALL &&
root->type != AST_PTRDREF &&
root->type != AST_ARRAY_INDEX)
left = generate_expr(gen, root->left);
if (root->right && root->type != AST_ARRAY_INDEX)
right = generate_expr(gen, root->right);
switch (root->type)
{
case AST_ADD:
return asm_add(gen, left, right);
case AST_SUBTRACT:
return asm_sub(gen, left, right);
case AST_MULT:
return asm_mul(gen, left, right);
case AST_DIV:
return asm_div(gen, left, right);
case AST_INT_LIT:
return asm_init_register(gen, root->value);
case AST_VAR:
return asm_get_global_var(gen, symtab_get_symbol(root->value)->sym_name);
case AST_OFFSET_SCALE:
Register offset = asm_init_register(gen, root->value);
return asm_mul(gen, left, offset);
case AST_PTRDREF:
if (root->expr_type->pointer_level > 0)
return generate_expr_ptrdref(gen, root);
else
return asm_load_mem(
gen,
generate_expr_ptrdref(gen, root),
root->expr_type->size);
case AST_ARRAY_INDEX:
return asm_load_mem(
gen,
generate_expr_arr_index(gen, root),
root->expr_type->size);
default:
debug_print(
SEV_ERROR,
"[CG] Unexpected type: %s in generate_expr_arithmetic\n",
NodeToString(*root));
exit(1);
}
}
static Register generate_expr_fcall(CodeGenerator_t *gen, ASTNode_t *root)
{
Register expr = asm_NoReg;
if (root->left)
expr = generate_expr(gen, root->left);
return asm_generate_func_call(
gen,
symtab_get_symbol(root->value)->sym_name,
expr,
true);
}
static Register generate_expr_addressof(CodeGenerator_t *gen, ASTNode_t *root)
{
return asm_address_of(gen, symtab_get_symbol(root->left->value)->sym_name);
}
static Register generate_expr_ptrdref(CodeGenerator_t *gen, ASTNode_t *root)
{
Register expr = generate_expr(gen, root->left);
if (root->expr_type->pointer_level == 0)
return expr;
else
return asm_load_mem(gen, expr, root->expr_type->size);
}
static Register generate_expr_arr_index(CodeGenerator_t *gen, ASTNode_t *root)
{
Register index, base_address;
index = generate_expr(gen, root->right);
asm_sll(gen, index, (int)log2(root->value / 8));
base_address = asm_address_of(gen, symtab_get_symbol(root->left->value)->sym_name);
return asm_add(gen, base_address, index);
}
static void generate_statements(CodeGenerator_t *gen, ASTNode_t *root)
{
while (root)
{
generate_statement(gen, root);
root = root->next;
}
}
static void generate_statement(CodeGenerator_t *gen, ASTNode_t *root)
{
switch (root->type)
{
case AST_VAR_DECL:
generate_decl_var(gen, root);
break;
case AST_ASSIGN:
generate_stmt_assign(gen, root);
break;
case AST_IF:
generate_stmt_if(gen, root);
break;
case AST_WHILE:
generate_stmt_while(gen, root);
break;
case AST_DO_WHILE:
generate_stmt_do_while(gen, root);
break;
case AST_FOR:
generate_stmt_for(gen, root);
break;
case AST_BREAK:
generate_stmt_break(gen, root);
break;
case AST_EMPTY:
break;
case AST_RETURN:
generate_stmt_return(gen, root);
break;
case AST_FUNC_CALL:
generate_stmt_fcall(gen, root);
break;
default:
debug_print(SEV_ERROR, "[CG] Unexpected node: %s", NodeToString(*root));
exit(0);
}
}
static void generate_decl_var(CodeGenerator_t *gen, ASTNode_t *root)
{
// TODO check if the variable has initial value
Symbol_t *symbol = symtab_get_symbol(root->value);
asm_add_global_var(
gen,
symbol->sym_name,
(RegSize_e)root->expr_type->size,
symbol->data_type->array_size);
if (root->left)
{
Register value = generate_expr(gen, root->left);
asm_set_global_var(gen, symtab_get_symbol(root->value)->sym_name, value);
}
}
static void generate_stmt_if(CodeGenerator_t *gen, ASTNode_t *root)
{
Register comp;
LabelId false_label = asm_generate_label();
LabelId end_label = asm_generate_label();
comp = generate_expr(gen, root->left);
asm_jmp_ne(gen, comp, 1, false_label);
generate_statements(gen, root->right->left);
asm_jmp(gen, end_label);
asm_lbl(gen, false_label);
if (root->right->right)
{
generate_statements(gen, root->right->right);
asm_jmp(gen, end_label);
}
asm_lbl(gen, end_label);
}
static void generate_stmt_while(CodeGenerator_t *gen, ASTNode_t *root)
{
Register comp;
LabelId start_label = asm_generate_label();
LabelId end_label = asm_generate_label();
// Save the end label as the value of the node. This will be used
// by the break statement
root->value = end_label;
asm_lbl(gen, start_label);
comp = generate_expr(gen, root->left);
asm_jmp_ne(gen, comp, 1, end_label);
generate_statements(gen, root->right);
asm_jmp(gen, start_label);
asm_lbl(gen, end_label);
}
static void generate_stmt_do_while(CodeGenerator_t *gen, ASTNode_t *root)
{
Register comp;
LabelId start_label = asm_generate_label();
LabelId end_label = asm_generate_label();
root->value = end_label;
asm_lbl(gen, start_label);
generate_statements(gen, root->right);
comp = generate_expr(gen, root->left);
asm_jmp_eq(gen, comp, 1, start_label);
asm_lbl(gen, end_label);
}
static void generate_stmt_for(CodeGenerator_t *gen, ASTNode_t *root)
{
Register comp;
ASTNode_t *init = root->left;
ASTNode_t *cond = root->left->next;
ASTNode_t *update = root->left->next->next;
LabelId start_label = asm_generate_label();
LabelId end_label = asm_generate_label();
root->value = end_label;
generate_statement(gen, init);
asm_lbl(gen, start_label);
comp = generate_expr(gen, cond);
asm_jmp_ne(gen, comp, 1, end_label);
generate_statements(gen, root->right);
generate_statement(gen, update);
asm_jmp(gen, start_label);
asm_lbl(gen, end_label);
}
static void generate_stmt_break(CodeGenerator_t *gen, ASTNode_t *root)
{
// TODO: Move this check early beforce codegen stage
ASTNode_t *loop_node = get_loop_context(root);
if (loop_node == NULL)
{
debug_print(SEV_ERROR, "[CG] break statement was called outside a loop context");
exit(1);
}
asm_jmp(gen, (LabelId)loop_node->value);
}
static void generate_stmt_assign(CodeGenerator_t *gen, ASTNode_t *root)
{
Symbol_t *symbol;
Register i = generate_expr(gen, root->right);
switch (root->left->type)
{
case AST_VAR:
symbol = symtab_get_symbol(root->left->value);
asm_set_global_var(gen, symbol->sym_name, i);
break;
case AST_PTRDREF:
Register expr1 = generate_expr_ptrdref(gen, root->left);
asm_store_mem(gen, expr1, i, root->left->expr_type->size);
break;
case AST_ARRAY_INDEX:
Register expr2 = generate_expr_arr_index(gen, root->left);
asm_store_mem(gen, expr2, i, root->left->expr_type->size);
break;
default:
debug_print(SEV_ERROR, "[CG] Unsupported lvalue type %s", NodeToString(*(root->left)));
exit(1);
break;
}
}
static void generate_stmt_return(CodeGenerator_t *gen, ASTNode_t *root)
{
Register i = generate_expr(gen, root->left);
asm_generate_func_return(gen, i, symtab_get_symbol(root->value)->data_type->size);
return_called_flag = true;
}
static void generate_stmt_fcall(CodeGenerator_t *gen, ASTNode_t *root)
{
Register expr = asm_NoReg;
if (root->left)
expr = generate_expr(gen, root->left);
asm_generate_func_call(
gen,
symtab_get_symbol(root->value)->sym_name,
expr,
false);
}
static void generate_declerations(CodeGenerator_t *gen, ASTNode_t *root)
{
while (root)
{
generate_decleration(gen, root);
root = root->next;
}
}
static void generate_decleration(CodeGenerator_t *gen, ASTNode_t *root)
{
switch (root->type)
{
case AST_FUNC_DECL:
generate_decl_func(gen, root);
break;
case AST_VAR_DECL:
generate_decl_var(gen, root);
break;
default:
debug_print(SEV_ERROR, "[CG] Unexpected declaration type found");
exit(1);
}
}
static void generate_decl_func(CodeGenerator_t *gen, ASTNode_t *root)
{
return_called_flag = false;
asm_generate_function_prologue(gen, symtab_get_symbol(root->value)->sym_name);
generate_statements(gen, root->left);
if (!return_called_flag)
{
Register i = asm_init_register(gen, 0);
asm_generate_func_return(gen, i, SIZE_8bit);
}
asm_generate_function_epilogue(gen);
}
CodeGenerator_t *codegen_init(char *path)
{
CodeGenerator_t *gen = (CodeGenerator_t *)malloc(sizeof(CodeGenerator_t));
gen->file = fopen(path, "w");
// gen->file = stdout;
return gen;
}
void codegen_start(CodeGenerator_t *gen, ASTNode_t *root)
{
generate_declerations(gen, root);
asm_wrapup(gen);
}