- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodegen.java
More file actions
Latest commit
254 lines (219 loc) · 9.16 KB
/
Copy pathCodegen.java
File metadata and controls
254 lines (219 loc) · 9.16 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
importjava.io.*;
// **********************************************************************
// The Codegen class provides constants and operations useful for code
// generation.
//
// The constants are:
// Registers: FP, SP, RA, V0, V1, A0, T0, T1
// Values: TRUE, FALSE
//
// The operations are include various "generate" methods to print nicely
// formatted assembly code:
// generateWithComment
// generate
// generateIndexed
// generateLabeled
// genPush
// genPop
// genLabel
// and a method nextLabel to create and return a new label.
//
// **********************************************************************
publicclassCodegen {
// file into which generated code is written
publicstaticPrintWriterp = null;
// values of true and false
publicstaticfinalStringTRUE = "1";
publicstaticfinalStringFALSE = "0";
// registers
publicstaticfinalStringFP = "$fp";
publicstaticfinalStringSP = "$sp";
publicstaticfinalStringRA = "$ra";
publicstaticfinalStringV0 = "$v0";
publicstaticfinalStringV1 = "$v1";
publicstaticfinalStringA0 = "$a0";
publicstaticfinalStringT0 = "$t0";
publicstaticfinalStringT1 = "$t1";
// for pretty printing generated code
privatestaticfinalintMAXLEN = 4;
// for generating labels
privatestaticintcurrLabel = 0;
// **********************************************************************
// **********************************************************************
// GENERATE OPERATIONS
// **********************************************************************
// **********************************************************************
// **********************************************************************
// generateWithComment
// given: op code, comment, and 0 to 3 string args
// do: write nicely formatted code (ending with new line)
// **********************************************************************
publicstaticvoidgenerateWithComment(Stringopcode, Stringcomment,
Stringarg1, Stringarg2, Stringarg3) {
intspace = MAXLEN - opcode.length() + 2;
p.print("\t" + opcode);
if (arg1 != "") {
for (intk = 1; k <= space; k++)
p.print(" ");
p.print(arg1);
if (arg2 != "") {
p.print(", " + arg2);
if (arg3 != "")
p.print(", " + arg3);
}
}
if (comment != "")
p.print("\t\t#" + comment);
p.println();
}
publicstaticvoidgenerateWithComment(Stringopcode, Stringcomment,
Stringarg1, Stringarg2) {
generateWithComment(opcode, comment, arg1, arg2, "");
}
publicstaticvoidgenerateWithComment(Stringopcode, Stringcomment,
Stringarg1) {
generateWithComment(opcode, comment, arg1, "", "");
}
publicstaticvoidgenerateWithComment(Stringopcode, Stringcomment) {
generateWithComment(opcode, comment, "", "", "");
}
// **********************************************************************
// generate
// given: op code, and 0 to 3 string args
// do: write nicely formatted code (ending with new line)
// **********************************************************************
publicstaticvoidgenerate(Stringopcode, Stringarg1, Stringarg2,
Stringarg3) {
intspace = MAXLEN - opcode.length() + 2;
p.print("\t" + opcode);
if (arg1 != "") {
for (intk = 1; k <= space; k++)
p.print(" ");
p.print(arg1);
if (arg2 != "") {
p.print(", " + arg2);
if (arg3 != "")
p.print(", " + arg3);
}
}
p.println();
}
publicstaticvoidgenerate(Stringopcode, Stringarg1, Stringarg2) {
generate(opcode, arg1, arg2, "");
}
publicstaticvoidgenerate(Stringopcode, Stringarg1) {
generate(opcode, arg1, "", "");
}
publicstaticvoidgenerate(Stringopcode) {
generate(opcode, "", "", "");
}
// **********************************************************************
// generate (two string args, one int)
// given: op code and args
// do: write nicely formatted code (ending with new line)
// **********************************************************************
publicstaticvoidgenerate(Stringopcode, Stringarg1, Stringarg2,
intarg3) {
intspace = MAXLEN - opcode.length() + 2;
p.print("\t" + opcode);
for (intk = 1; k <= space; k++)
p.print(" ");
p.println(arg1 + ", " + arg2 + ", " + arg3);
}
// **********************************************************************
// generate (one string arg, one int)
// given: op code and args
// do: write nicely formatted code (ending with new line)
// **********************************************************************
publicstaticvoidgenerate(Stringopcode, Stringarg1, intarg2) {
intspace = MAXLEN - opcode.length() + 2;
p.print("\t" + opcode);
for (intk = 1; k <= space; k++)
p.print(" ");
p.println(arg1 + ", " + arg2);
}
// **********************************************************************
// generateIndexed
// given: op code, target register T1 (as string), indexed register T2
// (as string), - offset xx (int), and optional comment
// do: write nicely formatted code (ending with new line):
// op T1, xx(T2) #comment
// **********************************************************************
publicstaticvoidgenerateIndexed(Stringopcode, Stringarg1, Stringarg2,
intarg3, Stringcomment) {
intspace = MAXLEN - opcode.length() + 2;
p.print("\t" + opcode);
for (intk = 1; k <= space; k++)
p.print(" ");
p.print(arg1 + ", " + arg3 + "(" + arg2 + ")");
if (comment != "")
p.print("\t#" + comment);
p.println();
}
publicstaticvoidgenerateIndexed(Stringopcode, Stringarg1, Stringarg2,
intarg3) {
generateIndexed(opcode, arg1, arg2, arg3, "");
}
// **********************************************************************
// generateLabeled (string args -- perhaps empty)
// given: label, op code, comment, and arg
// do: write nicely formatted code (ending with new line)
// **********************************************************************
publicstaticvoidgenerateLabeled(Stringlabel, Stringopcode,
Stringcomment, Stringarg1) {
intspace = MAXLEN - opcode.length() + 2;
p.print(label + ":");
p.print("\t" + opcode);
if (arg1 != "") {
for (intk = 1; k <= space; k++)
p.print(" ");
p.print(arg1);
}
if (comment != "")
p.print("\t# " + comment);
p.println();
}
publicstaticvoidgenerateLabeled(Stringlabel, Stringopcode,
Stringcomment) {
generateLabeled(label, opcode, comment, "");
}
// **********************************************************************
// genPush
// generate code to push the given value onto the stack
// **********************************************************************
publicstaticvoidgenPush(Strings) {
generateIndexed("sw", s, SP, 0, "PUSH");
generate("subu", SP, SP, 4);
}
// **********************************************************************
// genPop
// generate code to pop into the given register
// **********************************************************************
publicstaticvoidgenPop(Strings) {
generateIndexed("lw", s, SP, 4, "POP");
generate("addu", SP, SP, 4);
}
// **********************************************************************
// genLabel
// given: label L and comment (comment may be empty)
// generate: L: # comment
// **********************************************************************
publicstaticvoidgenLabel(Stringlabel, Stringcomment) {
p.print(label + ":");
if (comment != "")
p.print("\t\t" + "# " + comment);
p.println();
}
publicstaticvoidgenLabel(Stringlabel) {
genLabel(label, "");
}
// **********************************************************************
// Return a different label each time:
// L0 L1 L2, etc.
// **********************************************************************
publicstaticStringnextLabel() {
Integerk = newInteger(currLabel++);
Stringtmp = ".L" + k;
return(tmp);
}
}