-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodegen.java
254 lines (219 loc) · 9.16 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
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.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.
//
// **********************************************************************
public class Codegen {
// file into which generated code is written
public static PrintWriter p = null;
// values of true and false
public static final String TRUE = "1";
public static final String FALSE = "0";
// registers
public static final String FP = "$fp";
public static final String SP = "$sp";
public static final String RA = "$ra";
public static final String V0 = "$v0";
public static final String V1 = "$v1";
public static final String A0 = "$a0";
public static final String T0 = "$t0";
public static final String T1 = "$t1";
// for pretty printing generated code
private static final int MAXLEN = 4;
// for generating labels
private static int currLabel = 0;
// **********************************************************************
// **********************************************************************
// GENERATE OPERATIONS
// **********************************************************************
// **********************************************************************
// **********************************************************************
// generateWithComment
// given: op code, comment, and 0 to 3 string args
// do: write nicely formatted code (ending with new line)
// **********************************************************************
public static void generateWithComment(String opcode, String comment,
String arg1, String arg2, String arg3) {
int space = MAXLEN - opcode.length() + 2;
p.print("\t" + opcode);
if (arg1 != "") {
for (int k = 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();
}
public static void generateWithComment(String opcode, String comment,
String arg1, String arg2) {
generateWithComment(opcode, comment, arg1, arg2, "");
}
public static void generateWithComment(String opcode, String comment,
String arg1) {
generateWithComment(opcode, comment, arg1, "", "");
}
public static void generateWithComment(String opcode, String comment) {
generateWithComment(opcode, comment, "", "", "");
}
// **********************************************************************
// generate
// given: op code, and 0 to 3 string args
// do: write nicely formatted code (ending with new line)
// **********************************************************************
public static void generate(String opcode, String arg1, String arg2,
String arg3) {
int space = MAXLEN - opcode.length() + 2;
p.print("\t" + opcode);
if (arg1 != "") {
for (int k = 1; k <= space; k++)
p.print(" ");
p.print(arg1);
if (arg2 != "") {
p.print(", " + arg2);
if (arg3 != "")
p.print(", " + arg3);
}
}
p.println();
}
public static void generate(String opcode, String arg1, String arg2) {
generate(opcode, arg1, arg2, "");
}
public static void generate(String opcode, String arg1) {
generate(opcode, arg1, "", "");
}
public static void generate(String opcode) {
generate(opcode, "", "", "");
}
// **********************************************************************
// generate (two string args, one int)
// given: op code and args
// do: write nicely formatted code (ending with new line)
// **********************************************************************
public static void generate(String opcode, String arg1, String arg2,
int arg3) {
int space = MAXLEN - opcode.length() + 2;
p.print("\t" + opcode);
for (int k = 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)
// **********************************************************************
public static void generate(String opcode, String arg1, int arg2) {
int space = MAXLEN - opcode.length() + 2;
p.print("\t" + opcode);
for (int k = 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
// **********************************************************************
public static void generateIndexed(String opcode, String arg1, String arg2,
int arg3, String comment) {
int space = MAXLEN - opcode.length() + 2;
p.print("\t" + opcode);
for (int k = 1; k <= space; k++)
p.print(" ");
p.print(arg1 + ", " + arg3 + "(" + arg2 + ")");
if (comment != "")
p.print("\t# " + comment);
p.println();
}
public static void generateIndexed(String opcode, String arg1, String arg2,
int arg3) {
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)
// **********************************************************************
public static void generateLabeled(String label, String opcode,
String comment, String arg1) {
int space = MAXLEN - opcode.length() + 2;
p.print(label + ":");
p.print("\t" + opcode);
if (arg1 != "") {
for (int k = 1; k <= space; k++)
p.print(" ");
p.print(arg1);
}
if (comment != "")
p.print("\t# " + comment);
p.println();
}
public static void generateLabeled(String label, String opcode,
String comment) {
generateLabeled(label, opcode, comment, "");
}
// **********************************************************************
// genPush
// generate code to push the given value onto the stack
// **********************************************************************
public static void genPush(String s) {
generateIndexed("sw", s, SP, 0, "PUSH");
generate("subu", SP, SP, 4);
}
// **********************************************************************
// genPop
// generate code to pop into the given register
// **********************************************************************
public static void genPop(String s) {
generateIndexed("lw", s, SP, 4, "POP");
generate("addu", SP, SP, 4);
}
// **********************************************************************
// genLabel
// given: label L and comment (comment may be empty)
// generate: L: # comment
// **********************************************************************
public static void genLabel(String label, String comment) {
p.print(label + ":");
if (comment != "")
p.print("\t\t" + "# " + comment);
p.println();
}
public static void genLabel(String label) {
genLabel(label, "");
}
// **********************************************************************
// Return a different label each time:
// L0 L1 L2, etc.
// **********************************************************************
public static String nextLabel() {
Integer k = new Integer(currLabel++);
String tmp = ".L" + k;
return(tmp);
}
}