-
Notifications
You must be signed in to change notification settings - Fork 2
/
cg.c
605 lines (478 loc) · 11.9 KB
/
cg.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
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
602
603
604
605
/********************************************************************/
/* */
/* */
/* CODE GENERATOR */
/* */
/* */
/* */
/********************************************************************/
#include "cg.h"
void cg(TAC *t1){
TAC *tls = init_cg(t1);
printf(" .file \"%s\"\n", input_file_name);
printf(".PRINTF_ARG:\n");
printf(" .string %s\n", M_PRINTF_ARG);
printf(" .text\n");
printf(".globl main\n\n"); //entry point
printf(" .type main, @function\n\n");
printf("main:\n");
tls = tls->next; //always take off the first label for main
for(; tls != NULL; tls = tls->next){
printf("/*");
print_tac(tls);
printf("*/\n");
cg_instruction(tls);
}
}
TAC *init_cg(TAC *t1){
int r;
TAC *c;
TAC *p;
for(r = 0; r < R_MAX; r++)
rdesc[r].name = NULL;
insert_desc(0, mkconst(0), UNMODIFIED);
tos = VAR_OFF;
next_arg = 0;
c = NULL;
p = t1;
//reverse the TAC list
while(p != NULL){
p->next = c;
c = p;
p = p->prev;
}
return c;
}
void cg_instruction(TAC *c){
switch(c->op)
{
case TAC_UNDEF:
error("cannot translate TAC_UNDEF");
return;
case TAC_ADD:
cg_binop(TAC_ADD, c->a.var, c->b.var, c->c.var);
return;
case TAC_SUB:
cg_binop(TAC_SUB, c->a.var, c->b.var, c->c.var);
return;
case TAC_MUL:
cg_binop(TAC_MUL, c->a.var, c->b.var, c->c.var);
return;
case TAC_DIV:
cg_binop(TAC_DIV, c->a.var, c->b.var, c->c.var);
return;
case TAC_NEG:
cg_unop(TAC_NEG, c->a.var, c->c.var);
return;
case TAC_COPY:
cg_copy(c->a.var, c->b.var);
return;
case TAC_GOTO:
cg_cond("GOTO", NULL, c->a.lab->a.var->int_value);
return;
case TAC_IFZ:
cg_cond("IFZ", c->b.var, c->a.lab->a.var->int_value);
return;
case TAC_IFNZ:
cg_cond("IFNZ", c->b.var, c->a.lab->a.var->int_value);
return;
case TAC_ARG:
cg_arg(c->a.var);
return;
case TAC_CALL:
cg_call(c->b.lab->a.var->int_value, c->a.var);
return;
case TAC_RETURN:
cg_return(c->a.var);
return;
case TAC_MOD:
cg_binop(TAC_MOD, c->a.var, c->b.var, c->c.var);
return;
case TAC_AND:
cg_binop(TAC_AND, c->a.var, c->b.var, c->c.var);
return;
case TAC_OR:
cg_binop(TAC_OR, c->a.var, c->b.var, c->c.var);
return;
case TAC_NOT:
cg_unop(TAC_NOT, c->a.var, c->c.var);
return;
case TAC_TDIV:
cg_binop(TAC_TDIV, c->a.var, c->b.var, c->c.var);
return;
case TAC_POS:
cg_unop(TAC_POS, c->a.var, c->c.var);
return;
case TAC_LABEL:
{
flush_all();
printf("L%d:\n", c->a.var->int_value);
return;
}
case TAC_VAR:
{
c->a.var->offset = tos;
tos += c->a.var->size;
return;
}
case TAC_BEGINFUNC:
tos = VAR_OFF;
printf("\n");
return;
case TAC_ENDFUNC:
cg_return(NULL);
return;
case TAC_BEGINPROC:
return;
case TAC_ENDPROC:
cg_return(NULL);
return;
case TAC_BEGINPROG:
{
printf(" pushl %s\n", M_EBP); //push the base pointer
printf(" movl %s, %s\n", M_ESP, M_EBP); //set the stack pointer to the base pointer
printf(" movl $0, -4(%s)\n", M_EBP); //set first static link to zero
printf(" subl $%d, %s\n", c->a.var->size + 4, M_ESP);
return;
}
case TAC_MAINIDLIST:
{
return;
}
case TAC_ENDPROG:
{
printf(" leave\n");
printf(" ret\n");
return;
}
case TAC_OFFSET:
cg_offset(c->a.var, c->b.var, c->c.var);
return;
case TAC_LESSTHAN:
cg_relop("LESSTHAN", c->a.var, c->b.var, c->c.var);
return;
case TAC_GRTRTHAN:
cg_relop("GRTRTHAN", c->a.var, c->b.var, c->c.var);
return;
case TAC_NE:
cg_relop("NE", c->a.var, c->b.var, c->c.var);
return;
case TAC_LE:
cg_relop("LE", c->a.var, c->b.var, c->c.var);
return;
case TAC_GE:
cg_relop("GE", c->a.var, c->b.var, c->c.var);
return;
case TAC_EQUAL:
cg_relop("EQUAL", c->a.var, c->b.var, c->c.var);
return;
case TAC_PRINT:
cg_print(c->a.var);
return;
default:
error("unknown TAC opcode to translate");
return;
}
}
void cg_binop(int op, SYMB *a, SYMB *b, SYMB *c){
int cr = get_rreg(c); // result register
int br = get_areg(b, cr);
switch(op)
{
case TAC_ADD:
printf(" addl %s, %s\n", find_reg(br), find_reg(cr));
break;
case TAC_SUB:
printf(" subl %s, %s\n", find_reg(br), find_reg(cr));
break;
case TAC_MUL:
printf(" imull %s, %s\n", find_reg(br), find_reg(cr));
break;
//this is a logical AND, not bitwise
case TAC_AND:
{
printf(" andl %s, %s\n", find_reg(br), find_reg(cr));
break;
}
//this is a logical OR, not bitwise
case TAC_OR:
{
printf(" orl %s, %s\n", find_reg(br), find_reg(cr));
break;
}
//special cases
case TAC_MOD:
case TAC_TDIV:
case TAC_DIV:
{
int eax = 6;
int edx = 9;
spill_one(eax);
spill_one(edx);
insert_desc(eax, c, MODIFIED);
insert_desc(edx, c, MODIFIED);
int ar = get_rreg(a);
if((ar == eax) || (ar == edx)){ //unlikely case
error("encountered an unhandled exception!\n");
exit(1);
}
load_reg(eax, c);
load_reg(ar, b);
printf(" cltd %s\n", find_reg(eax)); //sign extend c
printf(" idivl %s\n", find_reg(ar)); //ar has result
clear_desc(eax);
clear_desc(edx);
insert_desc(ar, a, MODIFIED);
break;
}
}
if((op != TAC_DIV) && (op != TAC_TDIV) && (op != TAC_MOD)){
//delete c from descriptors and insert a
clear_desc(cr);
insert_desc(cr, a, MODIFIED);
}
}
void cg_unop(int op, SYMB *a, SYMB *b){
int ar = get_rreg(b);
switch(op)
{
case TAC_NEG:
{
if(b->int_value < 0)
break;
else
printf(" negl %s\n", find_reg(ar));
break;
}
case TAC_POS:
{
if(b->int_value > 0)
break;
else
printf(" negl %s\n", find_reg(ar));
break;
}
//this is a logical NOT, not bitwise
case TAC_NOT:
{
//is b > 0?
if(ar == R_COND){ //check and see if ar is %al
error("encountered unhandled exception!\n");
exit(1);
}
else
printf(" cmpl $0, %s\n", find_reg(ar));
printf(" setg %s\n", M_AL);
spill_one(ar);
printf(" movzbl %s, %s\n", M_AL, find_reg(ar));
//now invert what ever is in ar, by comparing to zero
printf(" cmpl $0, %s\n", find_reg(ar));
printf(" sete %s\n", M_AL);
printf(" movzbl %s, %s\n", M_AL, find_reg(ar)); // this is the final result
break;
}
}
//insert descriptor for a
insert_desc(ar, a, MODIFIED);
}
void cg_relop(char *op, SYMB *a, SYMB *b, SYMB *c){
int cr = get_rreg(c); // result register
int br = get_areg(b, cr);
printf(" cmpl %s, %s\n", find_reg(br), find_reg(cr));
if(!strcmp(op, "GRTRTHAN"))
printf(" setg %s\n", M_AL);
if(!strcmp(op, "GE"))
printf(" setge %s\n", M_AL);
if(!strcmp(op, "LESSTHAN"))
printf(" setl %s\n", M_AL);
if(!strcmp(op, "LE"))
printf(" setle %s\n", M_AL);
if(!strcmp(op, "EQUAL"))
printf(" sete %s\n", M_AL);
if(!strcmp(op, "NE"))
printf(" setne %s\n", M_AL);
clear_desc(cr);
printf(" movzbl %s, %s\n", M_AL, find_reg(cr));
insert_desc(cr, a, MODIFIED);
spill_one(cr);
}
void cg_offset(SYMB *a, SYMB *b, SYMB *c){
//not implemented in this version
}
void cg_copy(SYMB *a, SYMB *b){
int br = get_rreg(b); //load b into a register
insert_desc(br, a, MODIFIED); //indicate that a is there
}
void cg_cond(char *op, SYMB *a, int l){
spill_all();
if( a != NULL ){ // it is not a simple GOTO, set flags
int r ;
for( r = R_GEN ; r < R_MAX ; r++ ) //Is it in reg?
if( rdesc[r].name == a )
break ;
//was already in a reg
if( r < R_MAX ){
//use cmpl to set the flag
printf(" cmpl $0, %s\n", find_reg(r));
if(!strcmp(op, "IFZ"))
printf(" je L%d\n", l);
if(!strcmp(op, "IFNZ"))
printf(" jne L%d\n", l);
}
//was not in a reg, load into new reg
else{
r = get_rreg(a);
printf(" cmpl $0, %s\n", find_reg(r));
if(!strcmp(op, "IFZ"))
printf(" je L%d\n", l);
if(!strcmp(op, "IFNZ"))
printf(" jne L%d\n", l);
}
}
if(!strcmp(op, "GOTO"))
printf(" jmp L%d\n", l);
}
void cg_print(SYMB *s){
int r = get_areg(s, &r);
flush_all();
if(s->token_type_1 == T_CONSTANT)
printf(" pushl $%d\n", s->int_value);
if((s->token_type_2 == T_VAR) || (s->token_type_2 == T_TEMP))
printf(" pushl -%d(%s)\n", s->offset, M_EBP);
printf(" pushl $.PRINTF_ARG\n");
printf(" call printf\n");
printf(" addl $8, %s\n", M_ESP); //always passed 2 arguments
}
void cg_arg(SYMB *a){
// not implemented in this version
}
void cg_call(int f, SYMB *res){
// not implemented in this version
}
void cg_return(SYMB *a){
// not implemented in this version
}
int insert_desc(int r, SYMB *n, int mod){
rdesc[r].name = n;
rdesc[r].modified = mod;
}
void clear_desc(int r){
rdesc[r].name = NULL;
}
void load_reg(int r, SYMB *n){
int s;
//check if its already in a register
for(s = 0; s < R_MAX; s++)
if(rdesc[s].name == n){
if(s == R_COND) //we are loading the contents of the cond register
printf(" movzbl %s, %s\n", find_reg(s), find_reg(r));
else
printf(" movl %s, %s\n", find_reg(s), find_reg(r));
insert_desc(r, n, rdesc[s].modified);
return;
}
//not in a register, so load it
if(n->token_type_1 == T_CONSTANT)
printf(" movl $%d, %s\n", n->int_value, find_reg(r));
else
printf(" movl -%d(%s), %s\n", n->offset, M_EBP, find_reg(r));
insert_desc(r, n, UNMODIFIED);
}
int get_rreg(SYMB *c){
int r;
//already in a register
for(r = R_GEN; r < R_MAX; r++)
if(rdesc[r].name == c){
spill_one(r);
return r;
}
//empty register
for(r = R_GEN; r < R_MAX; r++)
if(rdesc[r].name == NULL){
load_reg(r, c);
return r;
}
//unmodified register
for(r = R_GEN; r < R_MAX; r++)
if(!(rdesc[r].modified)){
clear_desc(r);
load_reg(r, c);
return r;
}
//modified register
spill_one(R_GEN);
clear_desc(R_GEN);
load_reg(R_GEN, c);
return R_GEN;
}
int get_areg(SYMB *b, int cr){
int r;
//already in register
for(r = R_ZERO; r < R_MAX; r++)
if(rdesc[r].name == b)
return r;
//empty register
for(r = R_GEN; r < R_MAX; r++)
if(rdesc[r].name == NULL){
load_reg(r, b);
return r;
}
//unmodified register
for(r = R_GEN; r < R_MAX; r++)
if((!rdesc[r].modified) && (r != cr)){
clear_desc(r);
load_reg(r, b);
return r;
}
//modified register
for(r = R_GEN; r < R_MAX; r++)
if(r != cr){
spill_one(r);
clear_desc(r);
load_reg(r, b);
return r;
}
}
void flush_all(){
int r;
spill_all();
for(r = R_GEN; r < R_MAX; r++)
clear_desc(r);
}
void spill_all(){
int r;
for(r = R_GEN; r < R_MAX; r++)
spill_one(r);
}
void spill_one(int r){
if(rdesc[r].modified){
printf(" movl %s, -%d(%s)\n", find_reg(r), rdesc[r].name->offset, M_EBP);
rdesc[r].modified = UNMODIFIED;
}
}
char *find_reg(int enum_val){
switch(enum_val)
{
case 5:
return "%al";
break;
case 6:
return "%eax";
break;
case 7:
return "%ebx";
break;
case 8:
return "%ecx";
break;
case 9:
return "%edx";
break;
case 10:
return "%esi";
break;
case 11:
return "%edi";
break;
}
}