-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.c
59 lines (55 loc) · 1.33 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
#include "defs.h"
#include "data.h"
#include "decl.h"
// Generic code generator
// Copyright (c) 2019 Warren Toomey, GPL3
// Modification Copyright (c) 2022 Emin, GPL3
// Given an AST, generate
// assembly code recursively
int gen_AST(struct ASTnode* n, int reg) {
int leftreg, rightreg;
if (n->left)
leftreg = gen_AST(n->left, -1);
if (n->right)
rightreg = gen_AST(n->right, leftreg);
switch (n->op) {
// Binary operations
case A_ADD:
case A_SUBTRACT:
case A_MULTIPLY:
case A_DIVIDE:
case A_EQ:
case A_NE:
case A_LT:
case A_GT:
case A_LE:
case A_GE:
return cg_binop(leftreg, rightreg, n->op);
case A_INTLIT:
return (cg_load_int(n->v.intvalue));
case A_IDENT:
return (cg_load_glob(Gsym[n->v.id].name));
case A_LVIDENT:
return (cg_store_glob(reg, Gsym[n->v.id].name));
case A_ASSIGN:
return rightreg;
default:
fatald("Unknown AST operator", n->op);
exit(1);
}
}
void gen_preamble() {
cg_preamble();
}
void gen_postamble() {
cg_postamble();
}
void gen_free_regs() {
free_all_registers();
}
void gen_print_int(int reg) {
cg_print_int(reg);
}
void gen_glob_sym(char* s) {
cg_glob_sym(s);
}