forked from michellegao715/SimpleJava-Compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAATBuildTree.java
More file actions
262 lines (213 loc) · 7.21 KB
/
AATBuildTree.java
File metadata and controls
262 lines (213 loc) · 7.21 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import java.util.Vector;
public class AATBuildTree {
public AATStatement functionDefinition(AATStatement body, int framesize, Label start,
Label end) {
// <Label for start of function>
// <Code to set up activation record>
// <function body>
// <label for end of function>
// <code to clean up activation record>
// <return>
AATStatement storeSP = new AATMove(
new AATMemory(
new AATOperator(
new AATRegister(Register.SP()),
new AATConstant(framesize),
AATOperator.MINUS)),
new AATRegister(Register.SP()));
AATStatement storeFP = new AATMove(
new AATMemory(
new AATOperator(
new AATRegister(Register.SP()),
new AATOperator(
new AATConstant(framesize),
new AATOperator(
new AATConstant(1),
new AATConstant(MachineDependent.WORDSIZE),
AATOperator.MULTIPLY),
AATOperator.PLUS),
AATOperator.MINUS)),
new AATRegister(Register.FP()));
AATStatement storeRA = new AATMove(
new AATMemory(
new AATOperator(
new AATRegister(Register.SP()),
new AATOperator(
new AATConstant(framesize),
new AATOperator(
new AATConstant(2),
new AATConstant(MachineDependent.WORDSIZE),
AATOperator.MULTIPLY),
AATOperator.PLUS),
AATOperator.MINUS)),
new AATRegister(Register.ReturnAddr()));
AATStatement updateFP = new AATMove(
new AATRegister(Register.FP()),
new AATRegister(Register.SP()));
AATStatement updateSP = new AATMove(
new AATRegister(Register.SP()),
new AATOperator(
new AATRegister(Register.FP()),
new AATOperator(
new AATConstant(framesize),
new AATOperator(
new AATConstant(3),
new AATConstant(MachineDependent.WORDSIZE),
AATOperator.MULTIPLY),
AATOperator.PLUS),
AATOperator.MINUS));
AATStatement restoreRA = new AATMove(
new AATRegister(Register.ReturnAddr()),
new AATMemory(
new AATOperator(
new AATRegister(Register.SP()),
new AATOperator(
new AATConstant(1),
new AATConstant(MachineDependent.WORDSIZE),
AATOperator.MULTIPLY),
AATOperator.PLUS)));
AATStatement restoreFP = new AATMove(
new AATRegister(Register.FP()),
new AATMemory(
new AATOperator(
new AATRegister(Register.SP()),
new AATOperator(
new AATConstant(2),
new AATConstant(MachineDependent.WORDSIZE),
AATOperator.MULTIPLY),
AATOperator.PLUS)));
AATStatement restoreSP = new AATMove(
new AATRegister(Register.SP()),
new AATMemory(
new AATOperator(
new AATRegister(Register.SP()),
new AATOperator(
new AATConstant(3),
new AATConstant(MachineDependent.WORDSIZE),
AATOperator.MULTIPLY),
AATOperator.PLUS)));
AATStatement setup =
new AATSequential(storeSP,
new AATSequential(storeFP,
new AATSequential(storeRA,
new AATSequential(updateFP, updateSP))));
AATStatement cleanup =
new AATSequential(restoreRA,
new AATSequential(restoreFP, restoreSP));
AATStatement tree =
new AATSequential(new AATLabel(start),
new AATSequential(setup,
new AATSequential(body,
new AATSequential(new AATLabel(end),
new AATSequential(cleanup, new AATReturn())))));
return tree;
}
public AATStatement ifStatement(AATExpression test, AATStatement ifbody, AATStatement elsebody) {
Label iftrue = new Label("iftrue");
Label ifend = new Label("ifend");
AATStatement tree =
new AATSequential(new AATConditionalJump(test, iftrue),
new AATSequential(elsebody,
new AATSequential(new AATJump(iftrue),
new AATSequential(new AATLabel(iftrue),
new AATSequential(ifbody, new AATLabel(ifend))))));
return tree;
}
public AATExpression allocate(AATExpression size) {
//TODO AATCallExpression(allocateLabel,)
//for NewArrayExpression, NewClassExpression
Vector actuals = new Vector(1);
actuals.addElement(size);
return new AATCallExpression(Label.AbsLabel("allocate"), actuals);
}
public AATStatement whileStatement(AATExpression test, AATStatement whilebody) {
Label whiletest = new Label("whiletest");
Label whilestart = new Label("whilestart");
AATStatement tree =
new AATSequential(new AATJump(whiletest),
new AATSequential(new AATLabel(whilestart),
new AATSequential(whilebody,
new AATSequential(new AATLabel(whiletest), new AATConditionalJump(test, whilestart)))));
return tree;
}
public AATStatement dowhileStatement(AATExpression test, AATStatement dowhilebody) {
Label dowhilestart = new Label("dowwhilestart");
AATStatement tree =
new AATSequential(new AATLabel(dowhilestart),
new AATSequential(dowhilebody, new AATConditionalJump(test, dowhilestart)));
return tree;
}
public AATStatement forStatement(AATStatement init, AATExpression test,
AATStatement increment, AATStatement body) {
Label fortest = new Label("fortest");
Label forstart = new Label("forstart");
AATSequential tree =
new AATSequential(init,
new AATSequential(new AATJump(fortest),
new AATSequential(new AATLabel(forstart),
new AATSequential(body,
new AATSequential(increment,
new AATSequential(new AATLabel(fortest), new AATConditionalJump(test, forstart)))))));
return tree;
}
public AATStatement emptyStatement() {
return new AATEmpty();
}
public AATStatement callStatement(Vector actuals, Label name) {
return new AATCallStatement(name, actuals);
}
public AATStatement assignmentStatement(AATExpression lhs,
AATExpression rhs) {
return new AATMove(lhs,rhs);
}
public AATStatement sequentialStatement(AATStatement first,
AATStatement second) {
return new AATSequential(first, second);
}
public AATExpression baseVariable(int offset) {
return new AATMemory(
new AATOperator(
new AATRegister(Register.FP()),
new AATConstant(offset),
AATOperator.MINUS));
}
public AATExpression arrayVariable(AATExpression base, AATExpression index, int elementSize) {
return new AATMemory(
new AATOperator(
base,
new AATOperator(
index,
new AATConstant(elementSize),
AATOperator.MULTIPLY),
AATOperator.MINUS));
}
//TODO how to deal with S.A[3] in slides 07-69
public AATExpression classVariable(AATExpression base, int offset) {
return new AATMemory(
new AATOperator(
base,
new AATConstant(offset),
AATOperator.MINUS));
}
public AATExpression constantExpression(int value) {
return new AATConstant(value);
}
public AATExpression operatorExpression(AATExpression left,
AATExpression right,
int operator) {
return new AATOperator(
left,
right,
operator);
}
public AATExpression callExpression(Vector actuals, Label name) {
return new AATCallExpression(name, actuals);
}
public AATStatement returnStatement(AATExpression value, Label functionend) {
//first use AATMove to copy the value to register. then AATJump to functionend.
// copy the value to be returned to the result register, and then jump to the end of the function.
return new AATSequential(
new AATMove(new AATRegister(Register.Result()), value),
new AATJump(functionend));
}
}