forked from DoctorWkt/acwj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcg.c
650 lines (574 loc) · 18.1 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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
#include "defs.h"
#include "data.h"
#include "decl.h"
// Code generator for x86-64
// Copyright (c) 2019 Warren Toomey, GPL3
// Flag to say which section were are outputting in to
enum { no_seg, text_seg, data_seg } currSeg = no_seg;
void cgtextseg() {
if (currSeg != text_seg) {
fputs("\t.text\n", Outfile);
currSeg = text_seg;
}
}
void cgdataseg() {
if (currSeg != data_seg) {
fputs("\t.data\n", Outfile);
currSeg = data_seg;
}
}
// Position of next local variable relative to stack base pointer.
// We store the offset as positive to make aligning the stack pointer easier
static int localOffset;
static int stackOffset;
// Create the position of a new local variable.
static int newlocaloffset(int type) {
// Decrement the offset by a minimum of 4 bytes
// and allocate on the stack
localOffset += (cgprimsize(type) > 4) ? cgprimsize(type) : 4;
return (-localOffset);
}
// List of available registers and their names.
// We need a list of byte and doubleword registers, too
// The list also includes the registers used to
// hold function parameters
#define NUMFREEREGS 4
#define FIRSTPARAMREG 9 // Position of first parameter register
static int freereg[NUMFREEREGS];
static char *reglist[] =
{ "%r10", "%r11", "%r12", "%r13", "%r9", "%r8", "%rcx", "%rdx", "%rsi",
"%rdi"
};
static char *breglist[] =
{ "%r10b", "%r11b", "%r12b", "%r13b", "%r9b", "%r8b", "%cl", "%dl", "%sil",
"%dil"
};
static char *dreglist[] =
{ "%r10d", "%r11d", "%r12d", "%r13d", "%r9d", "%r8d", "%ecx", "%edx",
"%esi", "%edi"
};
// Set all registers as available
void freeall_registers(void) {
freereg[0] = freereg[1] = freereg[2] = freereg[3] = 1;
}
// Allocate a free register. Return the number of
// the register. Die if no available registers.
static int alloc_register(void) {
for (int i = 0; i < NUMFREEREGS; i++) {
if (freereg[i]) {
freereg[i] = 0;
return (i);
}
}
fatal("Out of registers");
return (NOREG); // Keep -Wall happy
}
// Return a register to the list of available registers.
// Check to see if it's not already there.
static void free_register(int reg) {
if (freereg[reg] != 0)
fatald("Error trying to free register", reg);
freereg[reg] = 1;
}
// Print out the assembly preamble
void cgpreamble() {
freeall_registers();
}
// Nothing to do
void cgpostamble() {
}
// Print out a function preamble
void cgfuncpreamble(int id) {
char *name = Symtable[id].name;
int i;
int paramOffset = 16; // Any pushed params start at this stack offset
int paramReg = FIRSTPARAMREG; // Index to the first param register in above reg lists
// Output in the text segment, reset local offset
cgtextseg();
localOffset = 0;
// Output the function start, save the %rsp and %rsp
fprintf(Outfile,
"\t.globl\t%s\n"
"\t.type\t%s, @function\n"
"%s:\n" "\tpushq\t%%rbp\n"
"\tmovq\t%%rsp, %%rbp\n", name, name, name);
// Copy any in-register parameters to the stack
// Stop after no more than six parameter registers
for (i = NSYMBOLS - 1; i > Locls; i--) {
if (Symtable[i].class != C_PARAM)
break;
if (i < NSYMBOLS - 6)
break;
Symtable[i].posn = newlocaloffset(Symtable[i].type);
cgstorlocal(paramReg--, i);
}
// For the remainder, if they are a parameter then they are
// already on the stack. If only a local, make a stack position.
for (; i > Locls; i--) {
if (Symtable[i].class == C_PARAM) {
Symtable[i].posn = paramOffset;
paramOffset += 8;
} else {
Symtable[i].posn = newlocaloffset(Symtable[i].type);
}
}
// Align the stack pointer to be a multiple of 16
// less than its previous value
stackOffset = (localOffset + 15) & ~15;
fprintf(Outfile, "\taddq\t$%d,%%rsp\n", -stackOffset);
}
// Print out a function postamble
void cgfuncpostamble(int id) {
cglabel(Symtable[id].endlabel);
fprintf(Outfile, "\taddq\t$%d,%%rsp\n", stackOffset);
fputs("\tpopq %rbp\n" "\tret\n", Outfile);
}
// Load an integer literal value into a register.
// Return the number of the register.
// For x86-64, we don't need to worry about the type.
int cgloadint(int value, int type) {
// Get a new register
int r = alloc_register();
fprintf(Outfile, "\tmovq\t$%d, %s\n", value, reglist[r]);
return (r);
}
// Load a value from a variable into a register.
// Return the number of the register. If the
// operation is pre- or post-increment/decrement,
// also perform this action.
int cgloadglob(int id, int op) {
// Get a new register
int r = alloc_register();
if (cgprimsize(Symtable[id].type) == 8) {
if (op == A_PREINC)
fprintf(Outfile, "\tincq\t%s(%%rip)\n", Symtable[id].name);
if (op == A_PREDEC)
fprintf(Outfile, "\tdecq\t%s(%%rip)\n", Symtable[id].name);
fprintf(Outfile, "\tmovq\t%s(%%rip), %s\n", Symtable[id].name,
reglist[r]);
if (op == A_POSTINC)
fprintf(Outfile, "\tincq\t%s(%%rip)\n", Symtable[id].name);
if (op == A_POSTDEC)
fprintf(Outfile, "\tdecq\t%s(%%rip)\n", Symtable[id].name);
} else
// Print out the code to initialise it
switch (Symtable[id].type) {
case P_CHAR:
if (op == A_PREINC)
fprintf(Outfile, "\tincb\t%s(%%rip)\n", Symtable[id].name);
if (op == A_PREDEC)
fprintf(Outfile, "\tdecb\t%s(%%rip)\n", Symtable[id].name);
fprintf(Outfile, "\tmovzbq\t%s(%%rip), %s\n", Symtable[id].name,
reglist[r]);
if (op == A_POSTINC)
fprintf(Outfile, "\tincb\t%s(%%rip)\n", Symtable[id].name);
if (op == A_POSTDEC)
fprintf(Outfile, "\tdecb\t%s(%%rip)\n", Symtable[id].name);
break;
case P_INT:
if (op == A_PREINC)
fprintf(Outfile, "\tincl\t%s(%%rip)\n", Symtable[id].name);
if (op == A_PREDEC)
fprintf(Outfile, "\tdecl\t%s(%%rip)\n", Symtable[id].name);
fprintf(Outfile, "\tmovslq\t%s(%%rip), %s\n", Symtable[id].name,
reglist[r]);
if (op == A_POSTINC)
fprintf(Outfile, "\tincl\t%s(%%rip)\n", Symtable[id].name);
if (op == A_POSTDEC)
fprintf(Outfile, "\tdecl\t%s(%%rip)\n", Symtable[id].name);
break;
default:
fatald("Bad type in cgloadglob:", Symtable[id].type);
}
return (r);
}
// Load a value from a local variable into a register.
// Return the number of the register. If the
// operation is pre- or post-increment/decrement,
// also perform this action.
int cgloadlocal(int id, int op) {
// Get a new register
int r = alloc_register();
// Print out the code to initialise it
if (cgprimsize(Symtable[id].type) == 8) {
if (op == A_PREINC)
fprintf(Outfile, "\tincq\t%d(%%rbp)\n", Symtable[id].posn);
if (op == A_PREDEC)
fprintf(Outfile, "\tdecq\t%d(%%rbp)\n", Symtable[id].posn);
fprintf(Outfile, "\tmovq\t%d(%%rbp), %s\n", Symtable[id].posn,
reglist[r]);
if (op == A_POSTINC)
fprintf(Outfile, "\tincq\t%d(%%rbp)\n", Symtable[id].posn);
if (op == A_POSTDEC)
fprintf(Outfile, "\tdecq\t%d(%%rbp)\n", Symtable[id].posn);
} else
switch (Symtable[id].type) {
case P_CHAR:
if (op == A_PREINC)
fprintf(Outfile, "\tincb\t%d(%%rbp)\n", Symtable[id].posn);
if (op == A_PREDEC)
fprintf(Outfile, "\tdecb\t%d(%%rbp)\n", Symtable[id].posn);
fprintf(Outfile, "\tmovzbq\t%d(%%rbp), %s\n", Symtable[id].posn,
reglist[r]);
if (op == A_POSTINC)
fprintf(Outfile, "\tincb\t%d(%%rbp)\n", Symtable[id].posn);
if (op == A_POSTDEC)
fprintf(Outfile, "\tdecb\t%d(%%rbp)\n", Symtable[id].posn);
break;
case P_INT:
if (op == A_PREINC)
fprintf(Outfile, "\tincl\t%d(%%rbp)\n", Symtable[id].posn);
if (op == A_PREDEC)
fprintf(Outfile, "\tdecl\t%d(%%rbp)\n", Symtable[id].posn);
fprintf(Outfile, "\tmovslq\t%d(%%rbp), %s\n", Symtable[id].posn,
reglist[r]);
if (op == A_POSTINC)
fprintf(Outfile, "\tincl\t%d(%%rbp)\n", Symtable[id].posn);
if (op == A_POSTDEC)
fprintf(Outfile, "\tdecl\t%d(%%rbp)\n", Symtable[id].posn);
break;
default:
fatald("Bad type in cgloadlocal:", Symtable[id].type);
}
return (r);
}
// Given the label number of a global string,
// load its address into a new register
int cgloadglobstr(int id) {
// Get a new register
int r = alloc_register();
fprintf(Outfile, "\tleaq\tL%d(%%rip), %s\n", id, reglist[r]);
return (r);
}
// Add two registers together and return
// the number of the register with the result
int cgadd(int r1, int r2) {
fprintf(Outfile, "\taddq\t%s, %s\n", reglist[r1], reglist[r2]);
free_register(r1);
return (r2);
}
// Subtract the second register from the first and
// return the number of the register with the result
int cgsub(int r1, int r2) {
fprintf(Outfile, "\tsubq\t%s, %s\n", reglist[r2], reglist[r1]);
free_register(r2);
return (r1);
}
// Multiply two registers together and return
// the number of the register with the result
int cgmul(int r1, int r2) {
fprintf(Outfile, "\timulq\t%s, %s\n", reglist[r1], reglist[r2]);
free_register(r1);
return (r2);
}
// Divide the first register by the second and
// return the number of the register with the result
int cgdiv(int r1, int r2) {
fprintf(Outfile, "\tmovq\t%s,%%rax\n", reglist[r1]);
fprintf(Outfile, "\tcqo\n");
fprintf(Outfile, "\tidivq\t%s\n", reglist[r2]);
fprintf(Outfile, "\tmovq\t%%rax,%s\n", reglist[r1]);
free_register(r2);
return (r1);
}
int cgand(int r1, int r2) {
fprintf(Outfile, "\tandq\t%s, %s\n", reglist[r1], reglist[r2]);
free_register(r1);
return (r2);
}
int cgor(int r1, int r2) {
fprintf(Outfile, "\torq\t%s, %s\n", reglist[r1], reglist[r2]);
free_register(r1);
return (r2);
}
int cgxor(int r1, int r2) {
fprintf(Outfile, "\txorq\t%s, %s\n", reglist[r1], reglist[r2]);
free_register(r1);
return (r2);
}
int cgshl(int r1, int r2) {
fprintf(Outfile, "\tmovb\t%s, %%cl\n", breglist[r2]);
fprintf(Outfile, "\tshlq\t%%cl, %s\n", reglist[r1]);
free_register(r2);
return (r1);
}
int cgshr(int r1, int r2) {
fprintf(Outfile, "\tmovb\t%s, %%cl\n", breglist[r2]);
fprintf(Outfile, "\tshrq\t%%cl, %s\n", reglist[r1]);
free_register(r2);
return (r1);
}
// Negate a register's value
int cgnegate(int r) {
fprintf(Outfile, "\tnegq\t%s\n", reglist[r]);
return (r);
}
// Invert a register's value
int cginvert(int r) {
fprintf(Outfile, "\tnotq\t%s\n", reglist[r]);
return (r);
}
// Logically negate a register's value
int cglognot(int r) {
fprintf(Outfile, "\ttest\t%s, %s\n", reglist[r], reglist[r]);
fprintf(Outfile, "\tsete\t%s\n", breglist[r]);
fprintf(Outfile, "\tmovzbq\t%s, %s\n", breglist[r], reglist[r]);
return (r);
}
// Convert an integer value to a boolean value. Jump if
// it's an IF or WHILE operation
int cgboolean(int r, int op, int label) {
fprintf(Outfile, "\ttest\t%s, %s\n", reglist[r], reglist[r]);
if (op == A_IF || op == A_WHILE)
fprintf(Outfile, "\tje\tL%d\n", label);
else {
fprintf(Outfile, "\tsetnz\t%s\n", breglist[r]);
fprintf(Outfile, "\tmovzbq\t%s, %s\n", breglist[r], reglist[r]);
}
return (r);
}
// Call a function with the given symbol id
// Pop off any arguments pushed on the stack
// Return the register with the result
int cgcall(int id, int numargs) {
// Get a new register
int outr = alloc_register();
// Call the function
fprintf(Outfile, "\tcall\t%s@PLT\n", Symtable[id].name);
// Remove any arguments pushed on the stack
if (numargs > 6)
fprintf(Outfile, "\taddq\t$%d, %%rsp\n", 8 * (numargs - 6));
// and copy the return value into our register
fprintf(Outfile, "\tmovq\t%%rax, %s\n", reglist[outr]);
return (outr);
}
// Given a register with an argument value,
// copy this argument into the argposn'th
// parameter in preparation for a future function
// call. Note that argposn is 1, 2, 3, 4, ..., never zero.
void cgcopyarg(int r, int argposn) {
// If this is above the sixth argument, simply push the
// register on the stack. We rely on being called with
// successive arguments in the correct order for x86-64
if (argposn > 6) {
fprintf(Outfile, "\tpushq\t%s\n", reglist[r]);
} else {
// Otherwise, copy the value into one of the six registers
// used to hold parameter values
fprintf(Outfile, "\tmovq\t%s, %s\n", reglist[r],
reglist[FIRSTPARAMREG - argposn + 1]);
}
}
// Shift a register left by a constant
int cgshlconst(int r, int val) {
fprintf(Outfile, "\tsalq\t$%d, %s\n", val, reglist[r]);
return (r);
}
// Store a register's value into a variable
int cgstorglob(int r, int id) {
if (cgprimsize(Symtable[id].type) == 8) {
fprintf(Outfile, "\tmovq\t%s, %s(%%rip)\n", reglist[r],
Symtable[id].name);
} else
switch (Symtable[id].type) {
case P_CHAR:
fprintf(Outfile, "\tmovb\t%s, %s(%%rip)\n", breglist[r],
Symtable[id].name);
break;
case P_INT:
fprintf(Outfile, "\tmovl\t%s, %s(%%rip)\n", dreglist[r],
Symtable[id].name);
break;
default:
fatald("Bad type in cgstorglob:", Symtable[id].type);
}
return (r);
}
// Store a register's value into a local variable
int cgstorlocal(int r, int id) {
if (cgprimsize(Symtable[id].type) == 8) {
fprintf(Outfile, "\tmovq\t%s, %d(%%rbp)\n", reglist[r],
Symtable[id].posn);
} else
switch (Symtable[id].type) {
case P_CHAR:
fprintf(Outfile, "\tmovb\t%s, %d(%%rbp)\n", breglist[r],
Symtable[id].posn);
break;
case P_INT:
fprintf(Outfile, "\tmovl\t%s, %d(%%rbp)\n", dreglist[r],
Symtable[id].posn);
break;
default:
fatald("Bad type in cgstorlocal:", Symtable[id].type);
}
return (r);
}
// Given a P_XXX type value, return the
// size of a primitive type in bytes.
int cgprimsize(int type) {
if (ptrtype(type))
return (8);
switch (type) {
case P_CHAR:
return (1);
case P_INT:
return (4);
case P_LONG:
return (8);
default:
fatald("Bad type in cgprimsize:", type);
}
return (0); // Keep -Wall happy
}
// Generate a global symbol but not functions
void cgglobsym(int id) {
int typesize;
if (Symtable[id].stype == S_FUNCTION)
return;
// Get the size of the type
typesize = cgprimsize(Symtable[id].type);
// Generate the global identity and the label
cgdataseg();
fprintf(Outfile, "\t.globl\t%s\n", Symtable[id].name);
fprintf(Outfile, "%s:", Symtable[id].name);
// Generate the space
for (int i = 0; i < Symtable[id].size; i++) {
switch (typesize) {
case 1:
fprintf(Outfile, "\t.byte\t0\n");
break;
case 4:
fprintf(Outfile, "\t.long\t0\n");
break;
case 8:
fprintf(Outfile, "\t.quad\t0\n");
break;
default:
fatald("Unknown typesize in cgglobsym: ", typesize);
}
}
}
// Generate a global string and its start label
void cgglobstr(int l, char *strvalue) {
char *cptr;
cglabel(l);
for (cptr = strvalue; *cptr; cptr++) {
fprintf(Outfile, "\t.byte\t%d\n", *cptr);
}
fprintf(Outfile, "\t.byte\t0\n");
}
// List of comparison instructions,
// in AST order: A_EQ, A_NE, A_LT, A_GT, A_LE, A_GE
static char *cmplist[] =
{ "sete", "setne", "setl", "setg", "setle", "setge" };
// Compare two registers and set if true.
int cgcompare_and_set(int ASTop, int r1, int r2) {
// Check the range of the AST operation
if (ASTop < A_EQ || ASTop > A_GE)
fatal("Bad ASTop in cgcompare_and_set()");
fprintf(Outfile, "\tcmpq\t%s, %s\n", reglist[r2], reglist[r1]);
fprintf(Outfile, "\t%s\t%s\n", cmplist[ASTop - A_EQ], breglist[r2]);
fprintf(Outfile, "\tmovzbq\t%s, %s\n", breglist[r2], reglist[r2]);
free_register(r1);
return (r2);
}
// Generate a label
void cglabel(int l) {
fprintf(Outfile, "L%d:\n", l);
}
// Generate a jump to a label
void cgjump(int l) {
fprintf(Outfile, "\tjmp\tL%d\n", l);
}
// List of inverted jump instructions,
// in AST order: A_EQ, A_NE, A_LT, A_GT, A_LE, A_GE
static char *invcmplist[] = { "jne", "je", "jge", "jle", "jg", "jl" };
// Compare two registers and jump if false.
int cgcompare_and_jump(int ASTop, int r1, int r2, int label) {
// Check the range of the AST operation
if (ASTop < A_EQ || ASTop > A_GE)
fatal("Bad ASTop in cgcompare_and_set()");
fprintf(Outfile, "\tcmpq\t%s, %s\n", reglist[r2], reglist[r1]);
fprintf(Outfile, "\t%s\tL%d\n", invcmplist[ASTop - A_EQ], label);
freeall_registers();
return (NOREG);
}
// Widen the value in the register from the old
// to the new type, and return a register with
// this new value
int cgwiden(int r, int oldtype, int newtype) {
// Nothing to do
return (r);
}
// Generate code to return a value from a function
void cgreturn(int reg, int id) {
// Generate code depending on the function's type
switch (Symtable[id].type) {
case P_CHAR:
fprintf(Outfile, "\tmovzbl\t%s, %%eax\n", breglist[reg]);
break;
case P_INT:
fprintf(Outfile, "\tmovl\t%s, %%eax\n", dreglist[reg]);
break;
case P_LONG:
fprintf(Outfile, "\tmovq\t%s, %%rax\n", reglist[reg]);
break;
default:
fatald("Bad function type in cgreturn:", Symtable[id].type);
}
cgjump(Symtable[id].endlabel);
}
// Generate code to load the address of an
// identifier into a variable. Return a new register
int cgaddress(int id) {
int r = alloc_register();
if (Symtable[id].class == C_GLOBAL)
fprintf(Outfile, "\tleaq\t%s(%%rip), %s\n", Symtable[id].name,
reglist[r]);
else
fprintf(Outfile, "\tleaq\t%d(%%rbp), %s\n", Symtable[id].posn,
reglist[r]);
return (r);
}
// Dereference a pointer to get the value it
// pointing at into the same register
int cgderef(int r, int type) {
// Get the type that we are pointing to
int newtype = value_at(type);
// Now get the size of this type
int size = cgprimsize(newtype);
switch (size) {
case 1:
fprintf(Outfile, "\tmovzbq\t(%s), %s\n", reglist[r], reglist[r]);
break;
case 2:
fprintf(Outfile, "\tmovslq\t(%s), %s\n", reglist[r], reglist[r]);
break;
case 4:
case 8:
fprintf(Outfile, "\tmovq\t(%s), %s\n", reglist[r], reglist[r]);
break;
default:
fatald("Can't cgderef on type:", type);
}
return (r);
}
// Store through a dereferenced pointer
int cgstorderef(int r1, int r2, int type) {
// Get the size of the type
int size = cgprimsize(type);
switch (size) {
case 1:
fprintf(Outfile, "\tmovb\t%s, (%s)\n", breglist[r1], reglist[r2]);
break;
case 2:
case 4:
case 8:
fprintf(Outfile, "\tmovq\t%s, (%s)\n", reglist[r1], reglist[r2]);
break;
default:
fatald("Can't cgstoderef on type:", type);
}
return (r1);
}