-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.c
342 lines (295 loc) · 9.88 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
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
#include "defs.h"
#include "data.h"
#include "decl.h"
static int labelid = 1;
int genlabel(void) {
return (labelid++);
}
static int genIF(struct ASTnode *n, int looptoplabel, int loopendlabel) {
int Lfalse, Lend;
Lfalse = genlabel();
if (n->right)
Lend = genlabel();
genAST(n->left, Lfalse, NOLABEL, NOLABEL, n->op);
genfreeregs(-1);
genAST(n->mid, NOLABEL, looptoplabel, loopendlabel, n->op);
genfreeregs(-1);
if (n->right)
cgjump(Lend);
cglabel(Lfalse);
if (n->right) {
genAST(n->right, NOLABEL, looptoplabel, loopendlabel, n->op);
genfreeregs(-1);
cglabel(Lend);
}
return (NOREG);
}
static int genWHILE(struct ASTnode *n) {
int Lstart, Lend;
Lstart = genlabel();
Lend = genlabel();
cglabel(Lstart);
genAST(n->left, Lend, NOLABEL, NOLABEL, n->op);
genfreeregs(-1);
genAST(n->right, NOLABEL, Lstart, Lend, n->op);
genfreeregs(-1);
cgjump(Lstart);
cglabel(Lend);
return (NOREG);
}
static int genFunccall(struct ASTnode *n) {
struct ASTnode *gluetree = n->left;
int reg;
int numargs = 0;
while (gluetree) {
reg = genAST(gluetree->right, NOLABEL, NOLABEL, NOLABEL, gluetree->op);
cgcopyarg(reg, gluetree->size);
if (numargs == 0)
numargs = gluetree->size;
genfreeregs(-1);
gluetree = gluetree->left;
}
return (cgcall(n->sym, numargs));
}
static int genSWITCH(struct ASTnode *n) {
int *caseval, *caselabel;
int Ljumptop, Lend;
int i, reg, defaultlabel;
struct ASTnode *c;
caseval = (int *)malloc((n->intvalue + 1) * sizeof(int));
caselabel = (int *)malloc((n->intvalue + 1) * sizeof(int));
Ljumptop = genlabel();
Lend = genlabel();
defaultlabel = Lend;
reg = genAST(n->left, NOLABEL, NOLABEL, NOLABEL, 0);
cgjump(Ljumptop);
genfreeregs(-1);
for (i = 0, c = n->right; c != NULL; i++, c = c->right) {
caselabel[i] = genlabel();
caseval[i] = c->intvalue;
cglabel(caselabel[i]);
if (c->op == A_DEFAULT)
defaultlabel = caselabel[i];
if (c->left != NULL) {
genAST(c->left, NOLABEL, NOLABEL, Lend, 0);
genfreeregs(-1);
}
}
cgjump(Lend);
cgswitch(reg, n->intvalue, Ljumptop, caselabel, caseval, defaultlabel);
cglabel(Lend);
return (NOREG);
}
static int genTernary(struct ASTnode *n) {
int Lfasle, Lend;
int expreg, reg;
Lfasle = genlabel();
Lend = genlabel();
genAST(n->left, Lfasle, NOLABEL, NOLABEL, n->op);
genfreeregs(-1);
reg = alloc_register();
expreg = genAST(n->mid, NOLABEL, NOLABEL, NOLABEL, 0);
cgmove(expreg, reg);
genfreeregs(reg);
cgjump(Lend);
cglabel(Lfasle);
expreg = genAST(n->right, NOLABEL, NOLABEL, NOLABEL, 0);
cgmove(expreg, reg);
genfreeregs(reg);
cglabel(Lend);
return (reg);
}
int genAST(struct ASTnode *n, int iflabel, int looptoplabel,
int loopendlabel, int parentASTop) {
int leftreg, rightreg, asignreg;
switch (n->op) {
case A_IF:
return (genIF(n, looptoplabel, loopendlabel));
case A_WHILE:
return (genWHILE(n));
case A_SWITCH:
return (genSWITCH(n));
case A_FUNCCALL:
return (genFunccall(n));
case A_TERNARY:
return (genTernary(n));
case A_GLUE:
if (n->left != NULL) {
genAST(n->left, NOLABEL, looptoplabel, loopendlabel,n->op);
genfreeregs(-1);
}
if (n->right != NULL) {
genAST(n->right, NOLABEL, looptoplabel, loopendlabel, n->op);
genfreeregs(-1);
}
return (NOREG);
case A_FUNCTION:
cgfuncpreamble(n->sym);
if (n->left != NULL) genAST(n->left, NOLABEL, NOLABEL, NOLABEL, n->op);
cgfuncpostamble(n->sym);
return (NOREG);
}
if (n->left)
leftreg = genAST(n->left, NOLABEL, NOLABEL, NOLABEL, n->op);
if (n->right)
rightreg = genAST(n->right, NOLABEL, NOLABEL, NOLABEL, n->op);
switch (n->op) {
case A_ASSIGN:
case A_ASPLUS:
case A_ASMINUS:
case A_ASSTAR:
case A_ASSLASH:
if (n->right->op == A_DEREF)
asignreg = cgderef(rightreg, n->right->type);
switch (n->op) {
case A_ASPLUS:
if (n->right->op == A_DEREF)
leftreg = cgadd(asignreg, leftreg);
else
leftreg = cgadd(rightreg, leftreg);
break;
case A_ASMINUS:
if (n->right->op == A_DEREF)
leftreg = cgsub(asignreg, leftreg);
else
leftreg = cgsub(rightreg, leftreg);
break;
case A_ASSTAR:
if (n->right->op == A_DEREF)
leftreg = cgmul(asignreg, leftreg);
else
leftreg = cgmul(rightreg, leftreg);
break;
case A_ASSLASH:
if (n->right->op == A_DEREF)
leftreg = cgdiv(asignreg, leftreg);
else
leftreg = cgdiv(rightreg, leftreg);
break;
}
switch (n->right->op) {
case A_IDENT:
if (n->right->sym->class == C_GLOBAL || n->right->sym->class == C_STATIC)
return (cgstorglob(leftreg, n->right->sym));
else
return (cgstorlocal(leftreg, n->right->sym));
case A_DEREF:
return (cgstorderef(leftreg, rightreg, n->right->type));
default:
fatald("can't assign in genAST(), op", n->op);
}
break;
case A_OR:
return (cgor(leftreg, rightreg));
case A_XOR:
return (cgxor(leftreg, rightreg));
case A_AND:
return (cgand(leftreg, rightreg));
case A_EQ:
case A_NE:
case A_LT:
case A_GT:
case A_LE:
case A_GE:
if (parentASTop == A_IF || parentASTop == A_WHILE ||
parentASTop == A_TERNARY)
return (cgcompare_and_jump(n->op, leftreg, rightreg, iflabel));
else
return (cgcompare_and_set(n->op, leftreg, rightreg));
case A_LSHIFT:
return (cgshl(leftreg, rightreg));
case A_RSHIFT:
return (cgshr(leftreg, rightreg));
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_INTLIT:
return (cgloadint(n->intvalue, n->type));
case A_STRLIT:
return (cgloadglobstr(n->intvalue));
case A_IDENT:
if (n->rvalue || parentASTop == A_DEREF
|| (parentASTop >= A_ASPLUS && parentASTop <= A_ASSLASH)) {
if (n->sym->class == C_GLOBAL || n->sym->class == C_STATIC)
return (cgloadglob(n->sym, n->op));
else
return (cgloadlocal(n->sym, n->op));
} else
return (NOREG);
case A_PREINC:
case A_PREDEC:
case A_POSTINC:
case A_POSTDEC:
if (n->sym->class == C_GLOBAL || n->sym->class == C_STATIC)
return (cgloadglob(n->sym, n->op));
else
return (cgloadlocal(n->sym, n->op));
case A_NEGATE:
return (cgnegate(leftreg));
case A_INVERT:
return (cginvert(leftreg));
case A_LOGNOT:
return (cglognot(leftreg));
case A_TOBOOL:
return (cgboolean(leftreg, parentASTop, iflabel));
case A_RETURN:
cgreturn(leftreg, Functionid);
return (NOREG);
case A_ADDR:
return (cgaddress(n->sym));
case A_DEREF:
if (n->rvalue || parentASTop == A_DEREF)
return (cgderef(leftreg, n->type));
else
return (leftreg);
case A_WIDEN:
return (cgwiden(leftreg, n->left->type, n->type));
case A_SCALE:
switch (n->size) {
case 2: return (cgshlconst(leftreg, 1));
case 4: return (cgshlconst(leftreg, 2));
case 8: return (cgshlconst(leftreg, 3));
default:
rightreg = cgloadint(n->size, P_INT);
return (cgmul(leftreg, rightreg));
}
case A_CAST:
return (leftreg);
case A_BREAK:
cgjump(loopendlabel);
return (NOREG);
case A_CONTINUE:
cgjump(looptoplabel);
return (NOREG);
default:
fatald("unknown AST operator", n->op);
}
}
void genpreamble(void) {
cgpreamble();
}
void genpostamble(void) {
cgpostamble();
}
void genfreeregs(int keepreg) {
freeall_registers(keepreg);
}
void genglobsym(struct symtable *sym) {
cgglobsym(sym);
}
int genglobstr(char *strvalue) {
int l = genlabel();
cgglobstr(l, strvalue);
return (l);
}
int genprimsize(int type) {
return (cgprimsize(type));
}
int genalign(int type, int offset, int direction) {
return (cglign(type, offset, direction));
}