Skip to content

Commit bb420cc

Browse files
committed
Fix Major Bug of istore and iload operands more than 4
1 parent f91a152 commit bb420cc

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

java_parser.y

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,16 @@
7575
%%
7676
method_body:
7777
%empty
78-
|{generateHeader();}statement_list{generateFooter();}
78+
|{generateHeader();}statement_list{writeBytecode(); generateFooter();}
7979
;
8080

8181
statement_list:
8282
statement {*($$.nextList) = *($1.nextList);}
8383
|statement_list marker_m statement
8484
{
85+
cout<<"Backpacth statement List"<<endl;
8586
backpatch(*($1.nextList) ,$2.nextInstructionIndex);
87+
cout<<"finish backpatch"<<endl;
8688
*($$.nextList) = *($3.nextList);
8789
}
8890
;
@@ -105,6 +107,7 @@ marker_m:
105107

106108
marker_n:
107109
%empty{
110+
cout<<"In marker n ==================== "<< nextInstructionIndex<<" ======"<<endl;
108111
// Save the index of the next instruction index in the marker
109112
$$.nextList = new unordered_set<int>();
110113
*($$.nextList) = makeList(nextInstructionIndex);
@@ -131,9 +134,16 @@ if:
131134
'{' statement_list '}'
132135
marker_n
133136
ELSE '{' marker_m statement_list '}' {
137+
cout<<"backpatching in if else"<<endl;
134138
backpatch(*($3.trueList), $5.nextInstructionIndex);
139+
cout<<"backpatching in if else"<<endl;
135140
backpatch(*($3.falseList), $12.nextInstructionIndex);
141+
cout<<"Finish backpatch if else"<<endl;
136142
std::unordered_set<int> temp = mergeLists( *($7.nextList), *($9.nextList));
143+
144+
cout<<"printing nextlist 9 ====================="<<endl;
145+
for(auto &&item:*($9.nextList)) cout<<outputCode[item]<<endl;
146+
137147
$$.nextList = new unordered_set<int>();
138148
*($$.nextList) = mergeLists(temp, *($13.nextList));
139149
}
@@ -142,8 +152,12 @@ if:
142152
while:
143153
WHILE marker_m '(' boolean_expression ')'
144154
'{' marker_m statement_list '}' {
155+
cout<<"backpatching in while"<<endl;
145156
backpatch(*($8.nextList), $2.nextInstructionIndex);
146157
backpatch(*($4.trueList), $7.nextInstructionIndex);
158+
cout<<"finish backpatch"<<endl;
159+
cout<<"next inst index ==== "<<nextInstructionIndex<<" "<<outputCode.size()<<endl;
160+
for(auto &&item:outputCode) cout<<item<<endl;
147161
$$.nextList = new unordered_set<int>();
148162
*($$.nextList) = *($4.falseList);
149163
appendToCode("goto Label_" + to_string($2.nextInstructionIndex));
@@ -156,9 +170,9 @@ assignment: IDENTIFIER '=' expression ';'{
156170
//Check if the two sides have the same type
157171
if(varToVarIndexAndType[$1].second == $3.varType) {
158172
if(varToVarIndexAndType[$1].second == VarType::INT_TYPE) {
159-
appendToCode("istore_"+to_string(varToVarIndexAndType[$1].first));
173+
appendToCode("istore "+to_string(varToVarIndexAndType[$1].first));
160174
} else {//Only int and float are supported
161-
appendToCode("fstore_"+to_string(varToVarIndexAndType[$1].first));
175+
appendToCode("fstore "+to_string(varToVarIndexAndType[$1].first));
162176
}
163177
} else { // case when the two types aren't the same
164178
//TODO Cast the two variables
@@ -182,12 +196,12 @@ expression:
182196
$$.varType = varToVarIndexAndType[$1].second;
183197
if($$.varType == VarType::INT_TYPE ) {
184198
//write iload + identifier
185-
appendToCode("iload_" + to_string(varToVarIndexAndType[$1].first));
199+
appendToCode("iload " + to_string(varToVarIndexAndType[$1].first));
186200
}
187201
else {
188202
//float
189203
//write fload + identifier
190-
appendToCode("fload_" + to_string(varToVarIndexAndType[$1].first));
204+
appendToCode("fload " + to_string(varToVarIndexAndType[$1].first));
191205
}
192206
}
193207
else {//it's not declared at all
@@ -225,11 +239,13 @@ boolean_expression:
225239
}
226240
|boolean_expression BOOL_OP marker_m boolean_expression {
227241
if(!strcmp($2, "&&")) {
242+
cout<<"backpatching bool"<<endl;
228243
backpatch(*($1.trueList), $3.nextInstructionIndex);
229244
*($$.trueList) = *($4.trueList);
230245
*($$.falseList) = mergeLists(*($1.falseList), *($4.falseList));
231246
}
232247
else if (!strcmp($2,"||")) {
248+
cout<<"backpatching in bool"<<endl;
233249
backpatch(*($1.falseList), $3.nextInstructionIndex);
234250
*($$.trueList) = mergeLists(*($1.trueList), *($4.trueList));
235251
*($$.falseList) = *($4.falseList);
@@ -271,7 +287,6 @@ int main(int argc, char** argv) {
271287
yyparse();
272288
std::cout<<"Yay ! Parser completed working successfully."<<std::endl;
273289

274-
writeBytecode();
275290
}
276291

277292
void yyerror(const char *s) {

semantic_actions_utils.h

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ namespace semantic_actions_util {
6060

6161
void backpatch(unordered_set<int> list, int instruction_index) {
6262
for (auto index : list) {
63+
cout<<"backpatch "<<outputCode[index]<<endl;
6364
outputCode[index] = outputCode[index].substr(0, outputCode[index].size()-1);
6465
outputCode[index] += "Label_" + to_string(instruction_index);
6566
}
@@ -69,31 +70,37 @@ namespace semantic_actions_util {
6970
declareVariable(name, varType);
7071
if (varType == INT_TYPE) {
7172
appendToCode("iconst_0");
72-
appendToCode("istore_" + to_string(currentVariableIndex -1));
73+
appendToCode("istore " + to_string(currentVariableIndex -1));
7374
} else if (varType == FLOAT_TYPE) {
7475
appendToCode("fconst_0");
75-
appendToCode("fstore_" + to_string(currentVariableIndex -1));
76+
appendToCode("fstore " + to_string(currentVariableIndex -1));
7677
}
7778

7879
}
7980

8081
void generateHeader() {
8182
//TO-DO get file name
8283
//appendToCode(".source " + outfileName);
83-
outputCode.push_back(".class public java_class\n.super java/lang/Object\n");
84-
outputCode.push_back(".method public <init>()V");
85-
outputCode.push_back("aload_0");
86-
outputCode.push_back("invokenonvirtual java/lang/Object/<init>()V");
87-
outputCode.push_back("return");
88-
outputCode.push_back(".end method\n");
89-
outputCode.push_back(".method public static main([Ljava/lang/String;)V");
90-
outputCode.push_back(".limit locals 100\n.limit stack 100");
91-
outputCode.push_back(".line 1");
84+
ofstream java_bytecode_file;
85+
java_bytecode_file.open("java_bytecode.j");
86+
java_bytecode_file<<".class public java_class\n.super java/lang/Object\n"<<endl;
87+
java_bytecode_file<<".method public <init>()V"<<endl;
88+
java_bytecode_file<<"aload_0"<<endl;
89+
java_bytecode_file<<"invokenonvirtual java/lang/Object/<init>()V"<<endl;
90+
java_bytecode_file<<"return"<<endl;
91+
java_bytecode_file<<".end method\n"<<endl;
92+
java_bytecode_file<<".method public static main([Ljava/lang/String;)V"<<endl;
93+
java_bytecode_file<<".limit locals 100\n.limit stack 100"<<endl;
94+
java_bytecode_file<<".line 1"<<endl;
95+
java_bytecode_file.close();
9296
}
9397

9498
void generateFooter() {
95-
outputCode.push_back("return");
96-
outputCode.push_back(".end method");
99+
ofstream java_bytecode_file;
100+
java_bytecode_file.open("java_bytecode.j",std::ofstream::app);
101+
java_bytecode_file<<"return"<<endl;
102+
java_bytecode_file<<".end method"<<endl;
103+
java_bytecode_file.close();
97104
}
98105

99106
unordered_set<int> makeList(int instruction_index) {
@@ -114,7 +121,7 @@ namespace semantic_actions_util {
114121

115122
void writeBytecode(){
116123
ofstream java_bytecode_file;
117-
java_bytecode_file.open("java_bytecode.j");
124+
java_bytecode_file.open("java_bytecode.j",std::ofstream::app);
118125
for(auto instruction: outputCode){
119126
// if(instruction.find("goto _") != string::npos) continue;
120127
java_bytecode_file<< instruction<< endl;

0 commit comments

Comments
 (0)