-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_ir.c
438 lines (378 loc) · 10.3 KB
/
gen_ir.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
#include "9cc.h"
static Function *fn;
static BB *out;
static int nreg = 1;
static BB *new_bb() {
BB *bb = calloc(1, sizeof(BB));
bb->label = nlabel++;
bb->ir = new_vec();
bb->succ = new_vec();
bb->pred = new_vec();
bb->def_regs = new_vec();
bb->in_regs = new_vec();
bb->out_regs = new_vec();
vec_push(fn->bbs, bb);
return bb;
}
static IR *new_ir(int op) {
IR *ir = calloc(1, sizeof(IR));
ir->op = op;
vec_push(out->ir, ir);
return ir;
}
Reg *new_reg() {
Reg *r = calloc(1, sizeof(Reg));
r->vn = nreg++;
r->rn = -1;
return r;
}
static IR *emit(int op, Reg *r0, Reg *r1, Reg *r2) {
IR *ir = new_ir(op);
ir->r0 = r0;
ir->r1 = r1;
ir->r2 = r2;
return ir;
}
static IR *br(Reg *r, BB *then, BB *els) {
IR *ir = new_ir(IR_BR);
ir->r2 = r;
ir->bb1 = then;
ir->bb2 = els;
return ir;
}
static IR *jmp(BB *bb) {
IR *ir = new_ir(IR_JMP);
ir->bb1 = bb;
return ir;
}
static IR *jmp_arg(BB *bb, Reg *r) {
IR *ir = new_ir(IR_JMP);
ir->bb1 = bb;
ir->bbarg = r;
return ir;
}
static Reg *imm(int imm) {
Reg *r = new_reg();
IR *ir = new_ir(IR_IMM);
ir->r0 = r;
ir->imm = imm;
return r;
}
static Reg *gen_expr(Node *node);
static void load(Node *node, Reg *dst, Reg *src) {
IR *ir = emit(IR_LOAD, dst, NULL, src);
ir->size = node->ty->size;
}
static Reg *gen_lval(Node *node) {
if (node->op == ND_DEREF) {
return gen_expr(node->expr);
}
if (node->op == ND_DOT) {
Reg *r1 = new_reg();
Reg *r2 = gen_lval(node->expr);
Reg *r3 = imm(node->ty->offset);
emit(IR_ADD, r1, r2, r3);
return r1;
}
assert(node->op == ND_VARREF);
Var *var = node->var;
IR *ir;
if (var->is_local) {
ir = new_ir(IR_BPREL);
ir->r0 = new_reg();
ir->var = var;
} else {
ir = new_ir(IR_LABEL_ADDR);
ir->r0 = new_reg();
ir->name = var->name;
}
return ir->r0;
}
static Reg *gen_binop(int op, Node *node) {
Reg *r1 = new_reg();
Reg *r2 = gen_expr(node->lhs);
Reg *r3 = gen_expr(node->rhs);
emit(op, r1, r2, r3);
return r1;
}
static void gen_stmt(Node *node);
static Reg *gen_expr(Node *node) {
switch (node->op) {
case ND_NUM:
return imm(node->val);
case ND_EQ:
return gen_binop(IR_EQ, node);
case ND_NE:
return gen_binop(IR_NE, node);
case ND_LOGAND: {
BB *bb = new_bb();
BB *set0 = new_bb();
BB *set1 = new_bb();
BB *last = new_bb();
br(gen_expr(node->lhs), bb, set0);
out = bb;
br(gen_expr(node->rhs), set1, set0);
out = set0;
jmp_arg(last, imm(0));
out = set1;
jmp_arg(last, imm(1));
out = last;
out->param = new_reg();
return out->param;
}
case ND_LOGOR: {
BB *bb = new_bb();
BB *set0 = new_bb();
BB *set1 = new_bb();
BB *last = new_bb();
Reg *r1 = gen_expr(node->lhs);
br(r1, set1, bb);
out = bb;
Reg *r2 = gen_expr(node->rhs);
br(r2, set1, set0);
out = set0;
jmp_arg(last, imm(0));
out = set1;
jmp_arg(last, imm(1));
out = last;
out->param = new_reg();
return out->param;
}
case ND_VARREF:
case ND_DOT: {
Reg *r = new_reg();
load(node, r, gen_lval(node));
return r;
}
case ND_CALL: {
Reg *args[6];
for (int i = 0; i < node->args->len; i++) {
args[i] = gen_expr(node->args->data[i]);
}
IR *ir = new_ir(IR_CALL);
ir->r0 = new_reg();
ir->name = node->name;
ir->nargs = node->args->len;
memcpy(ir->args, args, sizeof(args));
return ir->r0;
}
case ND_ADDR:
return gen_lval(node->expr);
case ND_DEREF: {
Reg *r = new_reg();
load(node, r, gen_expr(node->expr));
return r;
}
case ND_CAST: {
Reg *r1 = gen_expr(node->expr);
if (node->ty->ty != BOOL) {
return r1;
}
Reg *r2 = new_reg();
emit(IR_NE, r2, r1, imm(0));
return r2;
}
case ND_STMT_EXPR:
for (int i = 0; i < node->stmts->len; i++) {
gen_stmt(node->stmts->data[i]);
}
return gen_expr(node->expr);
case '=': {
Reg *r1 = gen_expr(node->rhs);
Reg *r2 = gen_lval(node->lhs);
IR *ir = emit(IR_STORE, NULL, r2, r1);
ir->size = node->ty->size;
return r1;
}
case '+':
return gen_binop(IR_ADD, node);
case '-':
return gen_binop(IR_SUB, node);
case '*':
return gen_binop(IR_MUL, node);
case '/':
return gen_binop(IR_DIV, node);
case '%':
return gen_binop(IR_MOD, node);
case '<':
return gen_binop(IR_LT, node);
case ND_LE:
return gen_binop(IR_LE, node);
case '&':
return gen_binop(IR_AND, node);
case '|':
return gen_binop(IR_OR, node);
case '^':
return gen_binop(IR_XOR, node);
case ND_SHL:
return gen_binop(IR_SHL, node);
case ND_SHR:
return gen_binop(IR_SHR, node);
case '~': {
Reg *r1 = new_reg();
Reg *r2 = gen_expr(node->expr);
emit(IR_XOR, r1, r2, imm(-1));
return r1;
}
case ',':
gen_expr(node->lhs);
return gen_expr(node->rhs);
case '?': {
BB *then = new_bb();
BB *els = new_bb();
BB *last = new_bb();
br(gen_expr(node->cond), then, els);
out = then;
jmp_arg(last, gen_expr(node->then));
out = els;
jmp_arg(last, gen_expr(node->els));
out = last;
out->param = new_reg();
return out->param;
}
case '!': {
Reg *r1 = new_reg();
Reg *r2 = gen_expr(node->expr);
emit(IR_EQ, r1, r2, imm(0));
return r1;
}
default:
assert(0 && "unknown AST type");
}
}
static void gen_stmt(Node *node) {
switch (node->op) {
case ND_NULL:
return;
case ND_IF: {
BB *then = new_bb();
BB *els = new_bb();
BB *last = new_bb();
br(gen_expr(node->cond), then, els);
out = then;
gen_stmt(node->then);
jmp(last);
out = els;
if (node->els) {
gen_stmt(node->els);
}
jmp(last);
out = last;
return;
}
case ND_FOR: {
BB *cond = new_bb();
node->continue_ = new_bb();
BB *body = new_bb();
node->break_ = new_bb();
if (node->init) {
gen_stmt(node->init);
}
jmp(cond);
out = cond;
if (node->cond) {
Reg *r = gen_expr(node->cond);
br(r, body, node->break_);
} else {
jmp(body);
}
out = body;
gen_stmt(node->body);
jmp(node->continue_);
out = node->continue_;
if (node->inc) {
gen_expr(node->inc);
}
jmp(cond);
out = node->break_;
return;
}
case ND_DO_WHILE: {
node->continue_ = new_bb();
BB *body = new_bb();
node->break_ = new_bb();
jmp(body);
out = body;
gen_stmt(node->body);
jmp(node->continue_);
out = node->continue_;
Reg *r = gen_expr(node->cond);
br(r, body, node->break_);
out = node->break_;
return;
}
case ND_SWITCH: {
node->break_ = new_bb();
node->continue_ = new_bb();
Reg *r = gen_expr(node->cond);
for (int i = 0; i < node->cases->len; i++) {
Node *case_ = node->cases->data[i];
case_->bb = new_bb();
BB *next = new_bb();
Reg *r2 = new_reg();
emit(IR_EQ, r2, r, imm(case_->val));
br(r2, case_->bb, next);
out = next;
}
jmp(node->break_);
gen_stmt(node->body);
jmp(node->break_);
out = node->break_;
return;
}
case ND_CASE:
jmp(node->bb);
out = node->bb;
gen_stmt(node->body);
break;
case ND_BREAK:
jmp(node->target->break_);
out = new_bb();
break;
case ND_CONTINUE:
jmp(node->target->continue_);
out = new_bb();
break;
case ND_RETURN: {
Reg *r = gen_expr(node->expr);
IR *ir = new_ir(IR_RETURN);
ir->r2 = r;
out = new_bb();
return;
}
case ND_EXPR_STMT:
gen_expr(node->expr);
return;
case ND_COMP_STMT:
for (int i = 0; i < node->stmts->len; i++) {
gen_stmt(node->stmts->data[i]);
}
return;
default:
error("unknown node: %d", node->op);
}
}
static void gen_param(Var *var, int i) {
IR *ir = new_ir(IR_STORE_ARG);
ir->var = var;
ir->imm = i;
ir->size = var->ty->size;
var->address_taken = true;
}
void gen_ir(Program *prog) {
for (int i = 0; i < prog->funcs->len; i++) {
fn = prog->funcs->data[i];
assert(fn->node->op == ND_FUNC);
out = new_bb();
BB *bb = new_bb();
jmp(bb);
out = bb;
Vector *params = fn->node->params;
for (int i = 0; i < params->len; i++) {
gen_param(params->data[i], i);
}
gen_stmt(fn->node->body);
new_ir(IR_RETURN)->r2 = imm(0);
fn->node = NULL;
}
}