-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCg.java
More file actions
206 lines (195 loc) · 6.03 KB
/
Cg.java
File metadata and controls
206 lines (195 loc) · 6.03 KB
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
// COMS22303: Code generation
import java.util.*;
import java.io.*;
import java.lang.reflect.Array;
import antlr.collections.AST;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
public class Cg
{
// Generate code from a program (in IRTree form)
static String lastLabel = "none";
public static void program(IRTree irt, PrintStream o)
{
emit(o, "XOR R0,R0,R0"); // Initialize R0 to 0
statement(irt, o);
emit(o, "HALT"); // Program must end with HALT
Memory.dumpData(o); // Dump DATA lines: initial memory contents
}
// Generate code from a statement (in IRTree form)
private static void statement(IRTree irt, PrintStream o)
{
if (irt.getOp().equals("SEQ")) {
statement(irt.getSub(0), o);
statement(irt.getSub(1), o);
}
else if (irt.getOp().equals("WRS") && irt.getSub(0).getOp().equals("MEM") &&
irt.getSub(0).getSub(0).getOp().equals("CONST")) {
String a = irt.getSub(0).getSub(0).getSub(0).getOp();
emit(o, "WRS "+a);
}
else if (irt.getOp().equals("WRR")) {
String e = expression(irt.getSub(0), o);
emit(o, "WRR "+e);
Reg.freeReg(e);
}
else if(irt.getOp().equals("MOVE"))
{
//deal with assignment
String memOffset = irt.getSub(0).getSub(0).getSub(0).getOp();
String e = expression(irt.getSub(1), o);
emit(o, "STORE "+e+",R0,"+memOffset);
Reg.freeReg(e);
}
else if(irt.getOp().equals("READIN"))
{
String memOffset = irt.getSub(0).getSub(0).getSub(0).getOp();
String inReg = Reg.newReg();
emit(o, "RDR "+ inReg);
emit(o, "STORE "+inReg+",R0,"+memOffset);
Reg.freeReg(inReg);
}
else if(irt.getOp().equals("IF"))
{
System.out.println("GOT IF");
}
else if(irt.getOp().equals("CJUMP"))
{
String op = irt.getSub(0).getOp();
String leftCond = expression(irt.getSub(1), o);
String rightCond = expression(irt.getSub(2), o);
String jumpLabel = irt.getSub(3).getOp();
String tempReg = Reg.newReg();
if(op.equals("=")){
emit(o, "SUBR " + tempReg + "," + leftCond + "," + rightCond);
emit(o, "BNEZR " + tempReg + "," + jumpLabel);
}
else if(op.equals(">"))
{
emit(o, "SUBR " + tempReg + "," + rightCond + "," + leftCond);
emit(o, "BGEZR " + tempReg + "," + jumpLabel);
}
else if(op.equals("<"))
{
emit(o, "SUBR " + tempReg + "," + leftCond + "," + rightCond);
emit(o, "BGEZR " + tempReg + "," + jumpLabel);
}else if(op.equals(">="))
{
emit(o, "SUBR " + tempReg + "," + leftCond + "," + rightCond);
emit(o, "BLTZR " + tempReg + "," + jumpLabel);
}
else if(op.equals("<="))
{
emit(o, "SUBR " + tempReg + "," + rightCond + "," + leftCond);
emit(o, "BLTZR " + tempReg + "," + jumpLabel);
}
else if(op.equals("!="))
{
emit(o, "SUBR " + tempReg + "," + leftCond + "," + rightCond);
emit(o, "BEQZR " + tempReg + "," + jumpLabel);
}else{
error(op + " in CJUMP");
}
Reg.freeReg(tempReg);
Reg.freeReg(leftCond);
Reg.freeReg(rightCond);
}
else if(irt.getOp().equals("JUMP"))
{
emit(o, "JMP " + irt.getSub(0).getOp());
}
else if(irt.getOp().equals("NOOP"))
{
//do nuthin
}
else if(irt.getOp().equals("REPEAT"))
{
/*
String condLabel = irt.getSub(2).getSub(0).getOp();
String postLabel = irt.getSub(1).getSub(3).getSub(0).getSub(0).getOp();
System.out.println("conl = " + postLabel);
emit(o, condLabel + ":NOP");
expression(irt.getSub(1), o);
statement(irt.getSub(0), o);
emit(o, "JMP "+condLabel);
emit(o, postLabel + ":NOP");*/
}
else if(irt.getOp().equals("LABEL"))
{
//emit(o, irt.getSub(0).getOp() + ":");
emitLabel(o, irt.getSub(0).getOp());
}
else {
error("statement - " + irt.getOp());
}
}
// Generate code from an expression (in IRTree form)
private static String expression(IRTree irt, PrintStream o)
{
String result = "";
if (irt.getOp().equals("CONST"))
{
String t = irt.getSub(0).getOp();
result = Reg.newReg();
emit(o, "MOVIR "+result+","+t);
}
else if (irt.getOp().equals("BINOP"))
{
String e = expression(irt.getSub(1), o);
String f = expression(irt.getSub(2), o);
result = Reg.newReg();
if (irt.getSub(0).getOp().equals("+"))
{
emit(o, "ADDR " + result + "," + e + "," + f);
}
else if(irt.getSub(0).getOp().equals("-"))
{
emit(o, "SUBR " + result + "," + e + "," + f);
}
else if(irt.getSub(0).getOp().equals("*"))
{
emit(o, "MULR " + result + "," + e + "," + f);
}
else if(irt.getSub(0).getOp().equals("/"))
{
emit(o, "DIVR " + result + "," + e + "," + f);
}
Reg.freeReg(e);
Reg.freeReg(f);
}
else if(irt.getOp().equals("MEM"))
{
String memAddr = irt.getSub(0).getSub(0).getOp();
result = Reg.newReg();
emit(o, "LOAD "+result+",R0,"+memAddr);
}
else {
error(" - expression - " + irt.getOp());
}
return result; // Return name of the register holding expression's value
}
// Generate an instruction
private static void emit(PrintStream o, String s)
{
if(lastLabel.equals("none"))
o.println(s);
else
{
o.println(lastLabel + ":" + s);
lastLabel = "none";
}
}
public static void emitLabel(PrintStream o, String labelName)
{
System.out.println("storing " + labelName);
if(!lastLabel.equals("none"))
o.println(lastLabel+":");
lastLabel = labelName;
}
// Error
private static void error(String op)
{
System.out.println("CG error: "+op);
System.exit(1);
}
}