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
|
#include "ccc.h"
#include "codegen.h"
#include "scope.h"
#include "register.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define CGEN_PANIC(format, ...) {\
fprintf(\
stderr,\
"ccc: code gen error: " format "\n" __VA_OPT__(,)\
__VA_ARGS__);\
exit(1);\
}
struct lval_def {
struct storage_location loc;
unsigned long long sz;
};
static const struct storage_location RV_LOC = {
.type = STO_REG,
.reg = &RAX,
};
#define RETURN_LABEL_FMT "%s@coda"
#define FULL_REG_SZ 8
static struct scope* scope;
static const struct fn_decl_node* active_fn;
static void emit_storage_loc(
FILE* outfile,
const struct storage_location* loc,
unsigned long long sz
) {
switch (loc->type) {
case STO_LABEL:
fprintf(outfile, "%s", loc->label);
break;
case STO_FN:
fprintf(outfile, "%s", loc->decl->name);
break;
case STO_REG:
if (sz > 4) fprintf(outfile, "%s", loc->reg->qword);
else if (sz > 2) fprintf(outfile, "%s", loc->reg->dword);
else if (sz > 1) fprintf(outfile, "%s", loc->reg->word);
else fprintf(outfile, "%s", loc->reg->byte);
break;
case STO_STACK:
if (loc->bp_offset < 0)
fprintf(outfile, "[rbp + %lld]", -loc->bp_offset);
else if (loc->bp_offset > 0)
fprintf(outfile, "[rbp - %lld]", loc->bp_offset);
else
fprintf(outfile, "[rbp]");
break;
case STO_IMM:
fprintf(outfile, "%llu", loc->value);
break;
}
}
static bool locs_equal(
const struct storage_location* a,
const struct storage_location* b
) {
if (a->type != b->type) return false;
switch (a->type) {
case STO_IMM:
return a->value == b->value;
case STO_REG:
return strcmp(a->reg->qword, b->reg->qword) == 0;
case STO_STACK:
return a->bp_offset == b->bp_offset;
case STO_LABEL:
return strcmp(a->label, b->label) == 0;
case STO_FN:
return a->decl == b->decl;
}
CGEN_PANIC("unhandled storage type case");
}
static void emit_mov(
FILE* outfile,
const struct lval_def* dst,
const struct storage_location* src
) {
/* first optimization: if dst == src, emit nothing */
if (locs_equal(&dst->loc, src)) return;
switch (dst->loc.type) {
case STO_REG:
if (src->type == STO_REG && dst->sz < 4) {
fprintf(outfile, "\tmovzx ");
emit_storage_loc(outfile, &dst->loc, FULL_REG_SZ);
} else {
fprintf(outfile, "\tmov ");
emit_storage_loc(outfile, &dst->loc, dst->sz);
}
fprintf(outfile, ", ");
emit_storage_loc(outfile, src, dst->sz);
break;
case STO_STACK:
if (src->type == STO_STACK) {
/* `mov mem, mem` is illegal in x86_64 */
emit_mov(
outfile,
&(struct lval_def) {
.loc = RV_LOC,
.sz = dst->sz,
},
src);
emit_mov(outfile, dst, &RV_LOC);
return;
}
fprintf(outfile, "\tmov ");
if (src->type == STO_IMM) {
/* must specify size to move immediates into memory*/
if (dst->sz > 4) fprintf(outfile, "qword ");
else if (dst->sz > 2) fprintf(outfile, "dword ");
else if (dst->sz > 1) fprintf(outfile, "word ");
else fprintf(outfile, "byte ");
}
emit_storage_loc(outfile, &dst->loc, dst->sz);
fprintf(outfile, ", ");
emit_storage_loc(outfile, src, dst->sz);
break;
case STO_LABEL:
CGEN_PANIC("can't move value into label %s", dst->loc.label);
case STO_FN:
CGEN_PANIC(
"can't move value into function %s", dst->loc.decl->name);
case STO_IMM:
CGEN_PANIC(
"can't move value into immediate value %lld", dst->loc.value);
}
fprintf(outfile, "\n");
}
static unsigned long long get_type_size(const struct type_node* type) {
if (type->ptr_level > 0) return PTR_SIZE;
return type->def.sz;
}
static struct lval_def make_stack_lval(
FILE* outfile,
const struct type_node* type
) {
unsigned long long type_sz = get_type_size(type);
fprintf(outfile, "\tsub rsp, %llu\n", type_sz);
scope->bp_offset += type_sz;
return (struct lval_def) {
.loc = {
.type = STO_STACK,
.bp_offset = scope->bp_offset,
},
.sz = type_sz,
};
}
static void emit_expr(
FILE* outfile,
const struct expr_node* node,
const struct lval_def* dst);
static void emit_int_lit(
FILE* outfile,
const struct int_lit_node* node,
const struct lval_def* dst
) {
if (dst != NULL)
emit_mov(
outfile,
dst,
&(struct storage_location) {
.type = STO_IMM,
.value = node->val,
});
}
static void emit_float_lit(
FILE* outfile,
const struct float_lit_node* node,
const struct lval_def* dst
) {
if (dst != NULL) {
CGEN_PANIC("float literals are not implemented");
}
}
static void emit_char_lit(
FILE* outfile,
const struct char_lit_node* node,
const struct lval_def* dst
) {
if (dst != NULL) {
emit_mov(
outfile,
dst,
&(struct storage_location) {
.type = STO_IMM,
.value = (unsigned long long) node->val
});
}
}
static void emit_str_lit(
FILE* outfile,
const struct str_lit_node* node,
const struct lval_def* dst
) {
if (dst != NULL) {
CGEN_PANIC("string literals are not implemented");
}
}
static struct var_def get_var(const char* name) {
struct var_def var_def;
if (!scope_get_var(scope, &var_def, name))
CGEN_PANIC("reference to undefined variable %s", name);
return var_def;
}
static void emit_var_ref(
FILE* outfile,
const struct var_ref_node* node,
const struct lval_def* dst
) {
if (dst != NULL) {
struct var_def var_def = get_var(node->ident);
emit_mov(outfile, dst, &var_def.loc);
}
}
static void emit_stmt(FILE* outfile, const struct stmt_node* node);
static struct var_def emit_var_decl(
FILE* outfile,
const struct var_decl_node* node
) {
struct lval_def var_dst = make_stack_lval(outfile, &node->type);
struct var_def var_def = {
.name = node->ident,
.loc = var_dst.loc,
.sz = var_dst.sz,
};
scope_define_var(scope, var_def);
return var_def;
}
static struct lval_def emit_lval(
FILE* outfile,
const struct lval_node* node
) {
struct var_def var_def;
switch (node->type) {
case LVAL_VAR_DECL:
var_def = emit_var_decl(outfile, &node->as._var_decl);
return (struct lval_def) {.loc = var_def.loc, .sz = var_def.sz};
case LVAL_VAR_REF:
var_def = get_var(node->as._var_ref.ident);
return (struct lval_def) {.loc = var_def.loc, .sz = var_def.sz};
}
CGEN_PANIC("unknown lval type: %d", node->type);
}
static void emit_assignment(
FILE* outfile,
const struct assign_node* node,
const struct lval_def* dst
) {
const struct lval_def lval_def = emit_lval(outfile, &node->lval);
emit_expr(outfile, node->rval, &lval_def);
if (dst != NULL) emit_mov(outfile, dst, &lval_def.loc);
}
static void emit_call(
FILE* outfile,
const struct call_node* node,
const struct lval_def* dst
) {
unsigned long long orig_bp_offset = scope->bp_offset;
unsigned long long arg_bp_offset = orig_bp_offset;
struct var_decl_node* arg_decl = node->called_fn->args_head;
struct expr_node* arg_eval = node->args_head;
while (arg_decl != NULL && arg_eval != NULL) {
struct lval_def arg_dst = make_stack_lval(outfile, &arg_decl->type);
emit_expr(outfile, arg_eval, &arg_dst);
arg_decl = arg_decl->next;
arg_eval = arg_eval->next;
}
if (arg_decl != NULL)
CGEN_PANIC("too many arguments to function %s", node->called_fn->name);
if (arg_eval != NULL)
CGEN_PANIC("missing arguments to function %s", node->called_fn->name);
unsigned char arg_regnum = 0;
arg_decl = node->called_fn->args_head;
while (arg_decl != NULL) {
unsigned long long type_sz = get_type_size(&arg_decl->type);
arg_bp_offset += type_sz;
struct lval_def arg_dst;
if (arg_regnum < CC_N_REGS)
arg_dst = (struct lval_def) {
.loc = (struct storage_location) {
.type = STO_REG,
.reg = CALLING_CONV[arg_regnum++],
},
.sz = type_sz,
};
else
arg_dst = make_stack_lval(outfile, &arg_decl->type);
emit_mov(outfile, &arg_dst, &(struct storage_location) {
.type = STO_STACK,
.bp_offset = arg_bp_offset,
});
arg_decl = arg_decl->next;
}
fprintf(outfile, "\tcall %s\n", node->called_fn->name);
if (dst != NULL) {
if (node->called_fn->return_type.def.sz == 0)
CGEN_PANIC("can't assign the result of a void function");
emit_mov(outfile, dst, &RV_LOC);
}
/* pop our argument temporaries off the stack */
scope->bp_offset = orig_bp_offset;
if (orig_bp_offset > 0)
fprintf(outfile, "\tlea rsp, [rbp - %llu]\n", orig_bp_offset);
else
fprintf(outfile, "\tmov rsp, rbp\n");
}
static void emit_expr(
FILE* outfile,
const struct expr_node* node,
const struct lval_def* dst
) {
switch (node->type) {
case EXPR_INT_LIT:
emit_int_lit(outfile, &node->as._int_lit, dst);
break;
case EXPR_FLOAT_LIT:
emit_float_lit(outfile, &node->as._float_lit, dst);
break;
case EXPR_CHAR_LIT:
emit_char_lit(outfile, &node->as._char_lit, dst);
break;
case EXPR_STR_LIT:
emit_str_lit(outfile, &node->as._str_lit, dst);
break;
case EXPR_VAR_REF:
emit_var_ref(outfile, &node->as._var_ref, dst);
break;
case EXPR_ASSIGN:
emit_assignment(outfile, &node->as._assign, dst);
break;
case EXPR_CALL:
emit_call(outfile, &node->as._call, dst);
break;
}
}
static void emit_return(FILE* outfile, const struct return_node* node) {
if (active_fn == NULL) CGEN_PANIC("must be inside a function to return");
if (node->ret_val != NULL) {
if (active_fn->return_type.def.sz == 0)
CGEN_PANIC(
"returning a value from void function %s", active_fn->name);
emit_expr(
outfile,
node->ret_val,
&(struct lval_def) {
.loc = RV_LOC,
.sz = active_fn->return_type.def.sz,
});
} else if (active_fn->return_type.def.sz > 0) {
CGEN_PANIC(
"non-void function %s should return a value", active_fn->name);
}
fprintf(outfile, "\tjmp " RETURN_LABEL_FMT "\n", active_fn->name);
}
static void emit_group(FILE* outfile, const struct group_node* node) {
const struct stmt_node* body_node = node->body_head;
while (body_node != NULL) {
emit_stmt(outfile, body_node);
body_node = body_node->next;
}
}
static void emit_stmt_group(FILE* outfile, const struct group_node* node) {
scope_push(&scope);
scope->bp_offset = scope->next_out->bp_offset; /* don't reset bp */
emit_group(outfile, node);
/* don't reset sp because alloca needs to work */
scope->next_out->bp_offset = scope->bp_offset;
scope_pop(&scope);
}
static void emit_stmt(FILE* outfile, const struct stmt_node* node) {
switch (node->type) {
case STMT_EMPTY:
break;
case STMT_VAR_DECL:
emit_var_decl(outfile, &node->as._var_decl);
break;
case STMT_RETURN:
emit_return(outfile, &node->as._return);
break;
case STMT_EXPR:
emit_expr(outfile, &node->as._expr, NULL);
break;
case STMT_GROUP:
emit_stmt_group(outfile, &node->as._group);
break;
}
}
static void emit_fn_decl(FILE* outfile, const struct fn_decl_node* node) {
if (active_fn != NULL)
CGEN_PANIC(
"can't define function %s inside function %s",
node->name,
active_fn->name);
active_fn = node;
fprintf(outfile, "%s:\n", node->name);
fprintf(outfile, "\tpush rbp\n");
fprintf(outfile, "\tmov rbp, rsp\n");
scope_push(&scope);
scope->bp_offset = 0;
long long spilled_bp_ofs = -16; // return address + old bp
unsigned char arg_regnum = 0;
struct var_decl_node* args_node = node->args_head;
while (args_node != NULL) {
struct lval_def arg_dst = make_stack_lval(outfile, &args_node->type);
scope_define_var(
scope,
(struct var_def) {
.name = args_node->ident,
.loc = arg_dst.loc,
.sz = arg_dst.sz,
});
struct storage_location arg_src;
if (arg_regnum < CC_N_REGS) {
arg_src = (struct storage_location) {
.type = STO_REG,
.reg = CALLING_CONV[arg_regnum++]
};
} else {
arg_src = (struct storage_location) {
.type = STO_STACK,
.bp_offset = spilled_bp_ofs,
};
spilled_bp_ofs -= arg_dst.sz;
}
emit_mov(outfile, &arg_dst, &arg_src);
args_node = args_node->next;
}
emit_group(outfile, &node->body);
scope_pop(&scope);
fprintf(outfile, RETURN_LABEL_FMT ":\n", node->name);
fprintf(outfile, "\tmov rsp, rbp\n");
fprintf(outfile, "\tpop rbp\n");
fprintf(outfile, "\tret\n");
active_fn = NULL;
}
static void emit_root_node(FILE* outfile, const struct root_node* node) {
switch (node->type) {
case ROOT_FN_DECL:
emit_fn_decl(outfile, &node->as._fn_decl);
break;
}
}
void emit_code(const struct root_node* ast, const char* path) {
FILE* outfile = fopen(path, "w");
if (outfile == NULL) CCC_PANIC;
fprintf(outfile, "section .text\n");
scope_push(&scope);
scope_install_default_types(scope);
/* output all non-static function declarations as globals */
const struct root_node* node = ast;
while (node != NULL) {
if (node->type == ROOT_FN_DECL) {
const char* fn_name = node->as._fn_decl.name;
scope_define_var(scope, (struct var_def) {
.name = fn_name,
.loc = {
.type = STO_LABEL,
.label = fn_name,
},
/* sz ignored, not relevant to functions */
});
fprintf(outfile, "global %s\n", fn_name);
}
node = node->next;
}
fprintf(outfile, "\n");
/* actual code body */
node = ast;
while (node != NULL) {
emit_root_node(outfile, node);
if (node->next != NULL) fprintf(outfile, "\n");
node = node->next;
}
scope_pop(&scope);
fclose(outfile);
}
|