-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeGen.java
70 lines (58 loc) · 1.62 KB
/
CodeGen.java
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
//
// code generation classes
// written by Tim Budd, spring 2000
//
import java.util.Vector;
class CodeGen {
static void genProlog (String name, int size) {
System.out.println("Begin function " + name);
System.out.println("local space " + size);
// create space for constant pool
endLabel = new Label();
constantTable = new Vector();
stringLabel = new Vector();
}
static void addConstant(Label l, Object s) {
stringLabel.addElement(l);
constantTable.addElement(s);
}
static void genEpilog (String name) {
System.out.println("End function " + name);
// now dump out constant pool
for (int i = 0; i < constantTable.size(); i++) {
gen(".align","4");
Label l = (Label) stringLabel.elementAt(i);
l.genCode();
Object v = constantTable.elementAt(i);
if (v instanceof String)
gen(".string", "\"" + v + "\"");
else if (v instanceof Double)
gen(".float", "" + v);
}
}
static void genGlobal (String name, int size) {
System.out.println("Global " + name + " size " + size);
}
static void genAssign (Ast left, Ast right) {
left.genCode();
right.genCode();
System.out.println("do assignment");
}
static void genReturn (Ast e) {
if (e != null)
e.genCode();
System.out.println("return from function");
}
static void gen (String op) {
System.out.println("\t" + op);
}
static void gen (String op, String a) {
System.out.println("\t" + op + "\t" + a);
}
static void gen (String op, String a, String b) {
System.out.println("\t" + op + "\t" + a + "," + b);
}
static private Label endLabel;
static private Vector stringLabel;
static private Vector constantTable;
}