forked from DoctorWkt/acwj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.c
488 lines (440 loc) · 13.8 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
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
#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
static int labelid = 1;
int genlabel(void) {
return (labelid++);
}
// Generate the code for an IF statement
// and an optional ELSE clause.
static int genIF(struct ASTnode *n, int looptoplabel, int loopendlabel) {
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, NOLABEL, NOLABEL, n->op);
genfreeregs(NOREG);
// Generate the true compound statement
genAST(n->mid, NOLABEL, looptoplabel, loopendlabel, n->op);
genfreeregs(NOREG);
// 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, NOLABEL, NOLABEL, n->op);
genfreeregs(NOREG);
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, Lstart, Lend, n->op);
genfreeregs(NOREG);
// Generate the compound statement for the body
genAST(n->right, NOLABEL, Lstart, Lend, n->op);
genfreeregs(NOREG);
// Finally output the jump back to the condition,
// and the end label
cgjump(Lstart);
cglabel(Lend);
return (NOREG);
}
// Generate the code for a SWITCH statement
static int genSWITCH(struct ASTnode *n) {
int *caseval, *caselabel;
int Ljumptop, Lend;
int i, reg, defaultlabel = 0, casecount = 0;
struct ASTnode *c;
// Create arrays for the case values and associated labels.
// Ensure that we have at least one position in each array.
caseval = (int *) malloc((n->a_intvalue + 1) * sizeof(int));
caselabel = (int *) malloc((n->a_intvalue + 1) * sizeof(int));
// Generate labels for the top of the jump table, and the
// end of the switch statement. Set a default label for
// the end of the switch, in case we don't have a default.
Ljumptop = genlabel();
Lend = genlabel();
defaultlabel = Lend;
// Output the code to calculate the switch condition
reg = genAST(n->left, NOLABEL, NOLABEL, NOLABEL, 0);
cgjump(Ljumptop);
genfreeregs(reg);
// Walk the right-child linked list to
// generate the code for each case
for (i = 0, c = n->right; c != NULL; i++, c = c->right) {
// Get a label for this case. Store it
// and the case value in the arrays.
// Record if it is the default case.
caselabel[i] = genlabel();
caseval[i] = c->a_intvalue;
cglabel(caselabel[i]);
if (c->op == A_DEFAULT)
defaultlabel = caselabel[i];
else
casecount++;
// Generate the case code. Pass in the end label for the breaks.
// If case has no body, we will fall into the following body.
if (c->left)
genAST(c->left, NOLABEL, NOLABEL, Lend, 0);
genfreeregs(NOREG);
}
// Ensure the last case jumps past the switch table
cgjump(Lend);
// Now output the switch table and the end label.
cgswitch(reg, casecount, Ljumptop, caselabel, caseval, defaultlabel);
cglabel(Lend);
return (NOREG);
}
// Generate the code for an
// A_LOGAND or A_LOGOR operation
static int gen_logandor(struct ASTnode *n) {
// Generate two labels
int Lfalse = genlabel();
int Lend = genlabel();
int reg;
// Generate the code for the left expression
// followed by the jump to the false label
reg= genAST(n->left, NOLABEL, NOLABEL, NOLABEL, 0);
cgboolean(reg, n->op, Lfalse);
genfreeregs(NOREG);
// Generate the code for the right expression
// followed by the jump to the false label
reg= genAST(n->right, NOLABEL, NOLABEL, NOLABEL, 0);
cgboolean(reg, n->op, Lfalse);
genfreeregs(reg);
// We didn't jump so set the right boolean value
if (n->op== A_LOGAND) {
cgloadboolean(reg, 1);
cgjump(Lend);
cglabel(Lfalse);
cgloadboolean(reg, 0);
} else {
cgloadboolean(reg, 0);
cgjump(Lend);
cglabel(Lfalse);
cgloadboolean(reg, 1);
}
cglabel(Lend);
return(reg);
}
// 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;
// Save the registers before we copy the arguments
spill_all_regs();
// 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, NOLABEL, NOLABEL, gluetree->op);
// Copy this into the n'th function parameter: size is 1, 2, 3, ...
cgcopyarg(reg, gluetree->a_size);
// Keep the first (highest) number of arguments
if (numargs == 0)
numargs = gluetree->a_size;
gluetree = gluetree->left;
}
// Call the function, clean up the stack (based on numargs),
// and return its result
return (cgcall(n->sym, numargs));
}
// Generate code for a ternary expression
static int gen_ternary(struct ASTnode *n) {
int Lfalse, Lend;
int reg, expreg;
// Generate two labels: one for the
// false expression, and one for the
// end of the overall expression
Lfalse = genlabel();
Lend = genlabel();
// Generate the condition code followed
// by a jump to the false label.
genAST(n->left, Lfalse, NOLABEL, NOLABEL, n->op);
genfreeregs(NOREG);
// Get a register to hold the result of the two expressions
reg = alloc_register();
// Generate the true expression and the false label.
// Move the expression result into the known register.
expreg = genAST(n->mid, NOLABEL, NOLABEL, NOLABEL, n->op);
cgmove(expreg, reg);
// Don't free the register holding the result, though!
genfreeregs(reg);
cgjump(Lend);
cglabel(Lfalse);
// Generate the false expression and the end label.
// Move the expression result into the known register.
expreg = genAST(n->right, NOLABEL, NOLABEL, NOLABEL, n->op);
cgmove(expreg, reg);
// Don't free the register holding the result, though!
genfreeregs(reg);
cglabel(Lend);
return (reg);
}
// 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 iflabel, int looptoplabel,
int loopendlabel, int parentASTop) {
int leftreg= NOREG, rightreg= NOREG;
// Empty tree, do nothing
if (n==NULL) return(NOREG);
// 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, looptoplabel, loopendlabel));
case A_WHILE:
return (genWHILE(n));
case A_SWITCH:
return (genSWITCH(n));
case A_FUNCCALL:
return (gen_funccall(n));
case A_TERNARY:
return (gen_ternary(n));
case A_LOGOR:
return (gen_logandor(n));
case A_LOGAND:
return (gen_logandor(n));
case A_GLUE:
// Do each child statement, and free the
// registers after each child
if (n->left != NULL)
genAST(n->left, iflabel, looptoplabel, loopendlabel, n->op);
genfreeregs(NOREG);
if (n->right != NULL)
genAST(n->right, iflabel, looptoplabel, loopendlabel, n->op);
genfreeregs(NOREG);
return (NOREG);
case A_FUNCTION:
// Generate the function's preamble before the code
// in the child sub-tree
cgfuncpreamble(n->sym);
genAST(n->left, NOLABEL, NOLABEL, NOLABEL, n->op);
cgfuncpostamble(n->sym);
return (NOREG);
}
// General AST node handling below
// Get the left and right sub-tree values
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_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, A_WHILE or A_TERNARY,
// 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 ||
parentASTop == A_TERNARY)
return (cgcompare_and_jump(n->op, leftreg, rightreg, iflabel));
else
return (cgcompare_and_set(n->op, leftreg, rightreg));
case A_INTLIT:
return (cgloadint(n->a_intvalue, n->type));
case A_STRLIT:
return (cgloadglobstr(n->a_intvalue));
case A_IDENT:
// Load our value if we are an rvalue
// or we are being dereferenced
if (n->rvalue || parentASTop == A_DEREF) {
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_ASPLUS:
case A_ASMINUS:
case A_ASSTAR:
case A_ASSLASH:
case A_ASSIGN:
// For the '+=' and friends operators, generate suitable code
// and get the register with the result. Then take the left child,
// make it the right child so that we can fall into the assignment code.
switch (n->op) {
case A_ASPLUS:
leftreg = cgadd(leftreg, rightreg);
n->right = n->left;
break;
case A_ASMINUS:
leftreg = cgsub(leftreg, rightreg);
n->right = n->left;
break;
case A_ASSTAR:
leftreg = cgmul(leftreg, rightreg);
n->right = n->left;
break;
case A_ASSLASH:
leftreg = cgdiv(leftreg, rightreg);
n->right = n->left;
break;
}
// Now into the assignment code
// Are we assigning to an identifier or through a pointer?
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 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->sym));
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->a_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->a_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 (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_PREINC:
case A_PREDEC:
// Load and decrement the variable's value into a register
// and pre increment/decrement it
if (n->left->sym->class == C_GLOBAL || n->left->sym->class == C_STATIC)
return (cgloadglob(n->left->sym, n->op));
else
return (cgloadlocal(n->left->sym, 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, iflabel));
case A_BREAK:
cgjump(loopendlabel);
return (NOREG);
case A_CONTINUE:
cgjump(looptoplabel);
return (NOREG);
case A_CAST:
return (leftreg); // Not much to do
default:
fatald("Unknown AST operator", n->op);
}
return (NOREG); // Keep -Wall happy
}
void genpreamble() {
cgpreamble();
}
void genpostamble() {
cgpostamble();
}
void genfreeregs(int keepreg) {
freeall_registers(keepreg);
}
void genglobsym(struct symtable *node) {
cgglobsym(node);
}
// Generate a global string.
// If append is true, append to
// previous genglobstr() call.
int genglobstr(char *strvalue, int append) {
int l = genlabel();
cgglobstr(l, strvalue, append);
return (l);
}
void genglobstrend(void) {
cgglobstrend();
}
int genprimsize(int type) {
return (cgprimsize(type));
}
int genalign(int type, int offset, int direction) {
return (cgalign(type, offset, direction));
}