-
Notifications
You must be signed in to change notification settings - Fork 0
/
SBGenerator.java
199 lines (167 loc) · 4.47 KB
/
SBGenerator.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
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
import java.io.PrintWriter;
class SBGenerator extends IRVisitor {
PrintWriter _writer;
int _lcount;
int _ocount;
SymbolTable _symTab;
SBGenerator(PrintWriter writer, SymbolTable symTab) {
_writer = writer;
_lcount = 0;
_ocount = -1; // init value
_symTab = symTab;
}
SBGenerator p(Address ir) {
ir.accept(this);
return this;
}
SBGenerator p(String s) {
return this;
}
SBGenerator p(int i) {
return this;
}
SBGenerator pln() {
return this;
}
String getNewLabel() {
return "S_" + _lcount++;
}
// ---------------- top-level ----------------
Object visit(Program ir) {
for (int i=0, n=ir._functions.size(); i<n; i++) {
FunDef fun = ir._functions.get(i);
fun.accept(this);
if (i != n - 1)
pln();
}
return null;
}
Object visit(FunDef ir) {
_symTab.push(ir._heldScope);
ir._stack = new CCStack();
p(ir._name._id).p(" = fun ").p(ir._type.toString()).pln();
// firstly push defined local on formal parameters
for (FieldType s :ir._type._formals._fields) {
ir._stack.push(s._field._id);
}
for (Instruction instr : ir._sym._instructions) {
for (Label label : instr._labels)
p(" ").p(label).p(":");
if (!instr._labels.isEmpty())
pln();
p(" ");
instr.accept(this);
p(";").pln();
}
_symTab.pop(ir._heldScope);
return null;
}
Object visit(Type ir) {
assert false : "use 'p(Type t)' instead";
return null;
}
Object visit(Label ir) {
p(ir._name);
return null;
}
// ---------------- addresses ----------------
Object visit(NameAddr ir) {
assert _symTab._current._owner instanceof FunDef;
FunDef owner = (FunDef)_symTab._current._owner;
owner._stack.push(ir);
p(ir._name);
return null;
}
Object visit(ConstantAddr ir) {
if (ir._literal instanceof StringLit) {
assert _symTab._current._owner instanceof FunDef;
FunDef owner = (FunDef)_symTab._current._owner;
ir._label = owner._name._id + "." + getNewLabel();
_symTab._current._rodatas.add(ir);
}
p(ir._literal.toString());
return ir._literal.toString();
}
Object visit(TempAddr ir) {
assert _symTab._current._owner instanceof FunDef;
FunDef owner = (FunDef)_symTab._current._owner;
owner._stack.push(ir);
p(ir._name);
return null;
}
Object visit(SizeofAddr ir) {
p("sizeof(").p(ir._ofType.toString()).p(")");
return null;
}
// ---------------- instructions for computing values ----------------
Object visit(CopyInstr ir) {
p(ir._out).p(" = ").p(ir._in);
return null;
}
Object visit(InfixInstr ir) {
p(ir._out).p(" = ").p(ir._lhs).p(" ").p(ir._op).p(" ").p(ir._rhs);
return null;
}
Object visit(PrefixInstr ir) {
p(ir._out).p(" = ").p(ir._op).p(" ").p(ir._in);
return null;
}
Object visit(CastInstr ir) {
p(ir._out).p(" = ").p(ir._in).p(" : ").p(ir._type.toString());
return null;
}
// ---------------- instructions for jumping ----------------
Object visit(UncondJumpInstr ir) {
p("goto ").p(ir._tgt);
return null;
}
Object visit(TrueJumpInstr ir) {
p("if ").p(ir._cond).p(" goto ").p(ir._tgt);
return null;
}
Object visit(FalseJumpInstr ir) {
p("ifFalse ").p(ir._cond).p(" goto ").p(ir._tgt);
return null;
}
Object visit(RelopJumpInstr ir) {
p("if ").p(ir._lhs).p(" ").p(ir._op).p(" ").p(ir._rhs)
.p(" goto ").p(ir._tgt);
return null;
}
// ---------------- instructions for functions ----------------
Object visit(ParamInstr ir) {
p("param[").p(ir._index).p(" : ").p(ir._arity).p("] = ").p(ir._in);
return null;
}
Object visit(CallInstr ir) {
if (null != ir._out)
p(ir._out).p(" = ");
p("call ").p(ir._fun.name()).p(" : ").p(ir._arity);
return null;
}
Object visit(ReturnInstr ir) {
p("return");
if (null != ir._val) {
p(" ").p(ir._val);
}
return null;
}
// ---------------- instructions for memory access ----------------
Object visit(ArrReadInstr ir) {
p(ir._out).p(" = ").p(ir._base).p("[").p(ir._subscript).p("]");
return null;
}
Object visit(ArrWriteInstr ir) {
p(ir._base).p("[").p(ir._subscript).p("] = ").p(ir._in);
return null;
}
Object visit(RecReadInstr ir) {
p(ir._out).p(" = ").p(ir._base).p(".").p(ir._field.name());
return null;
}
Object visit(RecWriteInstr ir) {
p(ir._base).p(".").p(ir._field.name());
p(" = ").p(ir._in);
return null;
}
}