-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenCode.cpp
More file actions
225 lines (201 loc) · 6.56 KB
/
genCode.cpp
File metadata and controls
225 lines (201 loc) · 6.56 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
#include "genCode.h"
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
#include <vector>
extern std::string fileName;
char typeCode[5] = {'I', 'F', 'Z', 'S', 'V'};
char arithCode[3] = {'i', 'f', 'i'};
std::map<std::string, std::string> operatorCode;
std::vector<int> labelStack;
int nextLabelNo = 0;
FILE *yyoutput = stdout;
void genCode(int indent, const char *fmt, ...) {
va_list args;
va_start (args, fmt);
for (int i = 0; i < indent; ++i)
fprintf(yyoutput, "\t");
vfprintf (yyoutput, fmt, args);
fprintf (yyoutput, "\n");
va_end (args);
}
void genProgBegin() {
genCode(0, "; %s.j ", fileName.c_str());
genCode(0, ".class public %s ", fileName.c_str());
genCode(0, ".super java/lang/Object ");
genCode(0, ".field public static _sc Ljava/util/Scanner; ");
}
void genMainBegin() {
genCode(0, "");
genCode(0, ".method public static main([Ljava/lang/String;)V ");
genCode(1, ".limit stack 100 ");
genCode(1, ".limit locals 100 ");
genCode(1, "new java/util/Scanner ");
genCode(1, "dup ");
genCode(1, "getstatic java/lang/System/in Ljava/io/InputStream; ");
genCode(1, "invokespecial java/util/Scanner/<init>(Ljava/io/InputStream;)V ");
genCode(1, "putstatic %s/_sc Ljava/util/Scanner; ", fileName.c_str());
}
void genMainEnd() {
genCode(1, "return ");
genCode(0, ".end method ");
}
void genStoreVar(SymbolTableEntry ste) {
if (ste.attr.varNo == -1) { // global variable
genCode(1, "putstatic %s/%s %c ", fileName.c_str(), ste.name, typeCode[ste.type.typeID]);
} else { // local variable
genCode(1, "%cstore %d ", arithCode[ste.type.typeID], ste.attr.varNo);
}
}
void genLoadVar(SymbolTableEntry ste) {
if (ste.attr.varNo == -1) { // global variable
genCode(1, "getstatic %s/%s %c ", fileName.c_str(), ste.name, typeCode[ste.type.typeID]);
} else { // local variable
genCode(1, "%cload %d ", arithCode[ste.type.typeID], ste.attr.varNo);
}
}
void genLoadConst(Variant val) {
if (val.typeID == T_INTEGER) {
genCode(1, "ldc %d ", val.integer);
} else if (val.typeID == T_REAL) {
genCode(1, "ldc %f ", val.real);
} else if (val.typeID == T_BOOLEAN) {
genCode(1, val.bl? "iconst_1 ": "iconst_0 ");
} else if (val.typeID == T_STRING) {
genCode(1, "ldc \"%s\" ", val.str);
}
}
void genI2F() {
genCode(1, "i2f ");
}
void genStoreAndI2F() {
genCode(1, "fstore %d ", nextVarNo);
genI2F();
genCode(1, "fload %d ", nextVarNo);
}
void genPrint(TypeID t) {
switch(t){
case T_STRING:
genCode(1, "invokevirtual java/io/PrintStream/print(Ljava/lang/String;)V ");
break;
case T_INTEGER:
genCode(1, "invokevirtual java/io/PrintStream/print(I)V ");
break;
case T_REAL:
genCode(1, "invokevirtual java/io/PrintStream/print(F)V ");
break;
case T_BOOLEAN:
genCode(1, "invokevirtual java/io/PrintStream/print(Z)V ");
break;
}
}
void genRead(SymbolTableEntry ste) {
genCode(1, "getstatic %s/_sc Ljava/util/Scanner; ", fileName.c_str());
switch(ste.type.typeID){
case T_INTEGER:
genCode(1, "invokevirtual java/util/Scanner/nextInt()I ");
break;
case T_REAL:
genCode(1, "invokevirtual java/util/Scanner/nextFloat()F ");
break;
case T_BOOLEAN:
genCode(1, "invokevirtual java/util/Scanner/nextBoolean()Z ");
break;
}
genStoreVar(ste);
}
void genRelOp(Type a, std::string op, Type b)
{
labelStack.push_back(nextLabelNo++);
if(a.typeID == T_INTEGER)
genCode(1, "isub ");
else if(a.typeID == T_REAL)
genCode(1, "fcmpl ");
if (op == "<")
genCode(1, "iflt Ltrue_%d ", labelStack.back());
else if (op == "<=")
genCode(1, "ifle Ltrue_%d ", labelStack.back());
else if (op == "<>")
genCode(1, "ifne Ltrue_%d ", labelStack.back());
else if (op == ">=")
genCode(1, "ifge Ltrue_%d ", labelStack.back());
else if (op == ">")
genCode(1, "ifgt Ltrue_%d ", labelStack.back());
else if (op == "=")
genCode(1, "ifeq Ltrue_%d ", labelStack.back());
genCode(1, "iconst_0 "); //false
genCode(1, "goto Lfalse_%d ", labelStack.back());
genCode(0, "Ltrue_%d: ", labelStack.back());
genCode(1, "iconst_1 ");//true
genCode(0, "Lfalse_%d: ", labelStack.back());
labelStack.pop_back();
}
void genFuncInvoke(SymbolTableEntry ste)
{
std::string out;
char buf[300];
sprintf(buf, "invokestatic %s/%s(", fileName.c_str(), ste.name);
out = buf;
for (int i=0; i<ste.attr.paramLst.size(); i++)
out += typeCode[ste.attr.paramLst[i].typeID];
out = out + ')' + typeCode[ste.type.typeID] + ' ';
genCode(1, out.c_str());
}
void genFuncBegin(SymbolTableEntry ste)
{
std::string out;
char buf[300];
sprintf(buf, ".method public static %s(", ste.name);
out = buf;
for (int i=0; i<ste.attr.paramLst.size(); i++)
out += typeCode[ste.attr.paramLst[i].typeID];
out = out + ')' + typeCode[ste.type.typeID] + ' ';
genCode(0, "");
genCode(0, out.c_str());
genCode(0, ".limit stack 100 ");
genCode(0, ".limit locals 100 ");
}
void genFuncEnd(Type t)
{
if (t.typeID != T_NONE)
genCode(1, "%creturn ", arithCode[t.typeID]);
else
genCode(1, "return ");
genCode(0, ".end method ");
}
void genReturn(Type t)
{
if (t.typeID != T_NONE)
genCode(1, "%creturn ", arithCode[t.typeID]);
else
genCode(1, "return ");
}
void genForLoop(SymbolTableEntry ste, int start, int end)
{
labelStack.push_back(nextLabelNo++);
genCode(1, "ldc %d ", start);
genCode(1, "istore %d ", ste.attr.varNo);
genCode(0, "Lbegin_%d: ", labelStack.back());
genCode(1, "iload %d ", ste.attr.varNo);
labelStack.push_back(nextLabelNo++);
genCode(1, "ldc %d ", end);
genCode(1, "isub ");
genCode(1, "ifle Ltrue_%d ", labelStack.back());
genCode(1, "iconst_0 ");
genCode(1, "goto Lfalse_%d ", labelStack.back());
genCode(0, "Ltrue_%d: ", labelStack.back());
genCode(1, "iconst_1 ");
genCode(0, "Lfalse_%d: ", labelStack.back());
labelStack.pop_back();
genCode(1, "ifeq Lexit_%d ", labelStack.back());
}
void genForLoopEnd(SymbolTableEntry ste)
{
genCode(1, "iload %d ", ste.attr.varNo);
genCode(1, "sipush 1 ");
genCode(1, "iadd ");
genCode(1, "istore %d ", ste.attr.varNo);
genCode(1, "goto Lbegin_%d ", labelStack.back());
genCode(0, "Lexit_%d: ", labelStack.back());
labelStack.pop_back();
}