forked from DoctorWkt/acwj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.c
43 lines (37 loc) · 912 Bytes
/
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
#include "defs.h"
#include "data.h"
#include "decl.h"
// Generic code generator
// Copyright (c) 2019 Warren Toomey, GPL3
// Given an AST, generate
// assembly code recursively
static int genAST(struct ASTnode *n) {
int leftreg, rightreg;
// Get the left and right sub-tree values
if (n->left)
leftreg = genAST(n->left);
if (n->right)
rightreg = genAST(n->right);
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_INTLIT:
return (cgload(n->intvalue));
default:
fprintf(stderr, "Unknown AST operator %d\n", n->op);
exit(1);
}
}
void generatecode(struct ASTnode *n) {
int reg;
cgpreamble();
reg= genAST(n);
cgprintint(reg);
cgpostamble();
}