forked from DoctorWkt/acwj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.c
295 lines (271 loc) · 7.95 KB
/
gen.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
#include "defs.h"
#include "data.h"
#include "decl.h"
// Generic code generator
// Copyright (c) 2019 Warren Toomey, GPL3
// Generate and return a new label number
int genlabel(void) {
static int id = 1;
return (id );
}
// Generate the code for an IF statement
// and an optional ELSE clause
static int genIF(struct ASTnode *n) {
int Lfalse, Lend;
// Generate two labels: one for the
// false compound statement, and one
// for the end of the overall IF statement.
// When there is no ELSE clause, Lfalse _is_
// the ending label!
Lfalse = genlabel();
if (n->right)
Lend = genlabel();
// Generate the condition code followed
// by a jump to the false label.
genAST(n->left, Lfalse, n->op);
genfreeregs();
// Generate the true compound statement
genAST(n->mid, NOLABEL, n->op);
genfreeregs();
// If there is an optional ELSE clause,
// generate the jump to skip to the end
if (n->right)
cgjump(Lend);
// Now the false label
cglabel(Lfalse);
// Optional ELSE clause: generate the
// false compound statement and the
// end label
if (n->right) {
genAST(n->right, NOLABEL, n->op);
genfreeregs();
cglabel(Lend);
}
return (NOREG);
}
// Generate the code for a WHILE statement
static int genWHILE(struct ASTnode *n) {
int Lstart, Lend;
// Generate the start and end labels
// and output the start label
Lstart = genlabel();
Lend = genlabel();
cglabel(Lstart);
// Generate the condition code followed
// by a jump to the end label.
genAST(n->left, Lend, n->op);
genfreeregs();
// Generate the compound statement for the body
genAST(n->right, NOLABEL, n->op);
genfreeregs();
// Finally output the jump back to the condition,
// and the end label
cgjump(Lstart);
cglabel(Lend);
return (NOREG);
}
// Generate the code to copy the arguments of a
// function call to its parameters, then call the
// function itself. Return the register that holds
// the function's return value.
static int gen_funccall(struct ASTnode *n) {
struct ASTnode *gluetree = n->left;
int reg;
int numargs = 0;
// If there is a list of arguments, walk this list
// from the last argument (right-hand child) to the
// first
while (gluetree) {
// Calculate the expression's value
reg = genAST(gluetree->right, NOLABEL, gluetree->op);
// Copy this into the n'th function parameter: size is 1, 2, 3, ...
cgcopyarg(reg, gluetree->v.size);
// Keep the first (highest) number of arguments
if (numargs == 0)
numargs = gluetree->v.size;
genfreeregs();
gluetree = gluetree->left;
}
// Call the function, clean up the stack (based on numargs),
// and return its result
return (cgcall(n->v.id, numargs));
}
// Given an AST, an optional label, and the AST op
// of the parent, generate assembly code recursively.
// Return the register id with the tree's final value.
int genAST(struct ASTnode *n, int label, int parentASTop) {
int leftreg, rightreg;
// We have some specific AST node handling at the top
// so that we don't evaluate the child sub-trees immediately
switch (n->op) {
case A_IF:
return (genIF(n));
case A_WHILE:
return (genWHILE(n));
case A_FUNCCALL:
return (gen_funccall(n));
case A_GLUE:
// Do each child statement, and free the
// registers after each child
genAST(n->left, NOLABEL, n->op);
genfreeregs();
genAST(n->right, NOLABEL, n->op);
genfreeregs();
return (NOREG);
case A_FUNCTION:
// Generate the function's preamble before the code
// in the child sub-tree
cgfuncpreamble(n->v.id);
genAST(n->left, NOLABEL, n->op);
cgfuncpostamble(n->v.id);
return (NOREG);
}
// General AST node handling below
// Get the left and right sub-tree values
if (n->left)
leftreg = genAST(n->left, NOLABEL, n->op);
if (n->right)
rightreg = genAST(n->right, NOLABEL, n->op);
switch (n->op) {
case A_ADD:
return (cgadd(leftreg, rightreg));
case A_SUBTRACT:
return (cgsub(leftreg, rightreg));
case A_MULTIPLY:
return (cgmul(leftreg, rightreg));
case A_DIVIDE:
return (cgdiv(leftreg, rightreg));
case A_AND:
return (cgand(leftreg, rightreg));
case A_OR:
return (cgor(leftreg, rightreg));
case A_XOR:
return (cgxor(leftreg, rightreg));
case A_LSHIFT:
return (cgshl(leftreg, rightreg));
case A_RSHIFT:
return (cgshr(leftreg, rightreg));
case A_EQ:
case A_NE:
case A_LT:
case A_GT:
case A_LE:
case A_GE:
// If the parent AST node is an A_IF or A_WHILE, generate
// a compare followed by a jump. Otherwise, compare registers
// and set one to 1 or 0 based on the comparison.
if (parentASTop == A_IF || parentASTop == A_WHILE)
return (cgcompare_and_jump(n->op, leftreg, rightreg, label));
else
return (cgcompare_and_set(n->op, leftreg, rightreg));
case A_INTLIT:
return (cgloadint(n->v.intvalue, n->type));
case A_STRLIT:
return (cgloadglobstr(n->v.id));
case A_IDENT:
// Load our value if we are an rvalue
// or we are being dereferenced
if (n->rvalue || parentASTop == A_DEREF) {
if (Symtable[n->v.id].class == C_GLOBAL) {
return (cgloadglob(n->v.id, n->op));
} else {
return (cgloadlocal(n->v.id, n->op));
}
} else
return (NOREG);
case A_ASSIGN:
// Are we assigning to an identifier or through a pointer?
switch (n->right->op) {
case A_IDENT:
if (Symtable[n->right->v.id].class == C_GLOBAL)
return (cgstorglob(leftreg, n->right->v.id));
else
return (cgstorlocal(leftreg, n->right->v.id));
case A_DEREF:
return (cgstorderef(leftreg, rightreg, n->right->type));
default:
fatald("Can't A_ASSIGN in genAST(), op", n->op);
}
case A_WIDEN:
// Widen the child's type to the parent's type
return (cgwiden(leftreg, n->left->type, n->type));
case A_RETURN:
cgreturn(leftreg, Functionid);
return (NOREG);
case A_ADDR:
return (cgaddress(n->v.id));
case A_DEREF:
// If we are an rvalue, dereference to get the value we point at,
// otherwise leave it for A_ASSIGN to store through the pointer
if (n->rvalue)
return (cgderef(leftreg, n->left->type));
else
return (leftreg);
case A_SCALE:
// Small optimisation: use shift if the
// scale value is a known power of two
switch (n->v.size) {
case 2:
return (cgshlconst(leftreg, 1));
case 4:
return (cgshlconst(leftreg, 2));
case 8:
return (cgshlconst(leftreg, 3));
default:
// Load a register with the size and
// multiply the leftreg by this size
rightreg = cgloadint(n->v.size, P_INT);
return (cgmul(leftreg, rightreg));
}
case A_POSTINC:
case A_POSTDEC:
// Load and decrement the variable's value into a register
// and post increment/decrement it
if (Symtable[n->v.id].class == C_GLOBAL)
return (cgloadglob(n->v.id, n->op));
else
return (cgloadlocal(n->v.id, n->op));
case A_PREINC:
case A_PREDEC:
// Load and decrement the variable's value into a register
// and pre increment/decrement it
if (Symtable[n->left->v.id].class == C_GLOBAL)
return (cgloadglob(n->left->v.id, n->op));
else
return (cgloadlocal(n->left->v.id, n->op));
case A_NEGATE:
return (cgnegate(leftreg));
case A_INVERT:
return (cginvert(leftreg));
case A_LOGNOT:
return (cglognot(leftreg));
case A_TOBOOL:
// If the parent AST node is an A_IF or A_WHILE, generate
// a compare followed by a jump. Otherwise, set the register
// to 0 or 1 based on it's zeroeness or non-zeroeness
return (cgboolean(leftreg, parentASTop, label));
default:
fatald("Unknown AST operator", n->op);
}
return (NOREG); // Keep -Wall happy
}
void genpreamble() {
cgpreamble();
}
void genpostamble() {
cgpostamble();
}
void genfreeregs() {
freeall_registers();
}
void genglobsym(int id) {
cgglobsym(id);
}
int genglobstr(char *strvalue) {
int l = genlabel();
cgglobstr(l, strvalue);
return (l);
}
int genprimsize(int type) {
return (cgprimsize(type));
}