forked from DoctorWkt/acwj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcg.c
272 lines (235 loc) · 7.3 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
#include "defs.h"
#include "data.h"
#include "decl.h"
// Code generator for x86-64
// Copyright (c) 2019 Warren Toomey, GPL3
// List of available registers and their names.
// We need a list of byte and doubleword registers, too
static int freereg[4];
static char *reglist[4] = { "%r8", "%r9", "%r10", "%r11" };
static char *breglist[4] = { "%r8b", "%r9b", "%r10b", "%r11b" };
static char *dreglist[4] = { "%r8d", "%r9d", "%r10d", "%r11d" };
// 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 < 4; 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();
fputs("\t.text\n", Outfile);
}
// Nothing to do
void cgpostamble() {
}
// Print out a function preamble
void cgfuncpreamble(int id) {
char *name = Gsym[id].name;
fprintf(Outfile,
"\t.text\n"
"\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);
}
// Print out a function postamble
void cgfuncpostamble(int id) {
cglabel(Gsym[id].endlabel);
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
int cgloadglob(int id) {
// Get a new register
int r = alloc_register();
// Print out the code to initialise it
switch (Gsym[id].type) {
case P_CHAR:
fprintf(Outfile, "\tmovzbq\t%s(\%%rip), %s\n", Gsym[id].name,
reglist[r]);
break;
case P_INT:
fprintf(Outfile, "\tmovzbl\t%s(\%%rip), %s\n", Gsym[id].name,
reglist[r]);
break;
case P_LONG:
fprintf(Outfile, "\tmovq\t%s(\%%rip), %s\n", Gsym[id].name, reglist[r]);
break;
default:
fatald("Bad type in cgloadglob:", Gsym[id].type);
}
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);
}
// Call printint() with the given register
void cgprintint(int r) {
fprintf(Outfile, "\tmovq\t%s, %%rdi\n", reglist[r]);
fprintf(Outfile, "\tcall\tprintint\n");
free_register(r);
}
// Call a function with one argument from the given register
// Return the register with the result
int cgcall(int r, int id) {
// Get a new register
int outr = alloc_register();
fprintf(Outfile, "\tmovq\t%s, %%rdi\n", reglist[r]);
fprintf(Outfile, "\tcall\t%s\n", Gsym[id].name);
fprintf(Outfile, "\tmovq\t%%rax, %s\n", reglist[outr]);
free_register(r);
return (outr);
}
// Store a register's value into a variable
int cgstorglob(int r, int id) {
switch (Gsym[id].type) {
case P_CHAR:
fprintf(Outfile, "\tmovb\t%s, %s(\%%rip)\n", breglist[r],
Gsym[id].name);
break;
case P_INT:
fprintf(Outfile, "\tmovl\t%s, %s(\%%rip)\n", dreglist[r],
Gsym[id].name);
break;
case P_LONG:
fprintf(Outfile, "\tmovq\t%s, %s(\%%rip)\n", reglist[r], Gsym[id].name);
break;
default:
fatald("Bad type in cgloadglob:", Gsym[id].type);
}
return (r);
}
// Array of type sizes in P_XXX order.
// 0 means no size.
static int psize[] = { 0, 0, 1, 4, 8 };
// Given a P_XXX type value, return the
// size of a primitive type in bytes.
int cgprimsize(int type) {
// Check the type is valid
if (type < P_NONE || type > P_LONG)
fatal("Bad type in cgprimsize()");
return (psize[type]);
}
// Generate a global symbol
void cgglobsym(int id) {
int typesize;
// Get the size of the type
typesize = cgprimsize(Gsym[id].type);
fprintf(Outfile, "\t.comm\t%s,%d,%d\n", Gsym[id].name, typesize, typesize);
}
// 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 (Gsym[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:", Gsym[id].type);
}
cgjump(Gsym[id].endlabel);
}