Skip to content

Commit 2023588

Browse files
committed
For each isntruction increase or decrease stack size accordingly
1 parent 9d9e49f commit 2023588

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

src/main/pt/up/fe/comp2023/jasmin/JVMInstructionUtils.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public static void decreaseStackSize(int n) {
2727
}
2828

2929
public static String getLoadInstruction(Element element, HashMap<String, Descriptor> varTable) {
30+
increaseStackSize(1);
3031
if (element.isLiteral()) {
3132
int literal = parseInt(((LiteralElement)element).getLiteral());
3233
if (literal >= 0 && literal <= 5)
@@ -74,6 +75,7 @@ public static String getArrayLoadInstruction(ArrayOperand array, HashMap<String,
7475
}
7576

7677
public static String getStoreInstruction(Element element, HashMap<String, Descriptor> varTable) {
78+
decreaseStackSize(1);
7779
int virtualReg = varTable.get(((Operand)element).getName()).getVirtualReg();
7880
if (virtualReg > numLocals)
7981
numLocals = virtualReg;
@@ -128,13 +130,15 @@ public static String getInvokeVirtualInstruction(CallInstruction instruction, Ha
128130
statementList += getLoadInstruction(instruction.getFirstArg(), varTable);
129131
statementList += loadInvokeArguments(instruction.getListOfOperands(), varTable);
130132
statementList += "\tinvokevirtual " + createInvokeInstructionArgument(instruction, false);
133+
decreaseStackSize(instruction.getListOfOperands().size() + 1);
131134
return statementList;
132135
}
133136

134137
public static String getInvokeStaticInstruction(CallInstruction instruction, HashMap<String, Descriptor> varTable) {
135138
String statementList = "";
136139
statementList += loadInvokeArguments(instruction.getListOfOperands(), varTable);
137140
statementList += "\tinvokestatic " + createInvokeInstructionArgument(instruction, true);
141+
decreaseStackSize(instruction.getListOfOperands().size());
138142
return statementList;
139143
}
140144

@@ -147,6 +151,7 @@ public static String getNewInstruction(CallInstruction instruction, HashMap<Stri
147151
statementList += loadInvokeArguments(instruction.getListOfOperands(), varTable);
148152
statementList += "\tnew " + ((Operand)instruction.getFirstArg()).getName() + '\n';
149153
statementList += "\tdup\n";
154+
increaseStackSize(2);
150155
return statementList;
151156
}
152157

@@ -172,6 +177,7 @@ public static String createUnaryOpStatement(UnaryOpInstruction instruction, Hash
172177
case NOT: case NOTB:
173178
statementList += "\tifeq ";
174179
statementList += createAuxBranchStatement();
180+
decreaseStackSize(1);
175181
break;
176182
}
177183
return statementList;
@@ -185,41 +191,51 @@ public static String createBinaryOpInstruction(BinaryOpInstruction instruction,
185191
switch (instruction.getOperation().getOpType()) {
186192
case ADD:
187193
statementList += "\tiadd\n";
194+
decreaseStackSize(1);
188195
break;
189196
case SUB:
190197
statementList += "\tisub\n";
198+
decreaseStackSize(1);
191199
break;
192200
case MUL:
193201
statementList += "\timul\n";
202+
decreaseStackSize(1);
194203
break;
195204
case DIV:
196205
statementList += "\tidiv\n";
206+
decreaseStackSize(1);
197207
break;
198208
case AND: case ANDB:
199209
statementList += "\tiand\n";
210+
decreaseStackSize(1);
200211
break;
201212
case OR: case ORB:
202213
statementList += "\tior\n";
214+
decreaseStackSize(1);
203215
break;
204216
case LTH:
205217
statementList += "\tif_icmplt ";
206218
if (!isBranchCond)
207219
statementList += createAuxBranchStatement();
220+
decreaseStackSize(2);
208221
break;
209222
case LTE:
210223
statementList += "\tif_icmple ";
211224
if (!isBranchCond)
212225
statementList += createAuxBranchStatement();
226+
decreaseStackSize(2);
213227
break;
214228
case GTH:
215229
statementList += "\tif_icmpgt ";
216230
if (!isBranchCond)
217231
statementList += createAuxBranchStatement();
232+
decreaseStackSize(2);
218233
break;
219234
case GTE:
220235
statementList += "\tif_icmpge ";
221236
if (!isBranchCond)
222237
statementList += createAuxBranchStatement();
238+
decreaseStackSize(2);
223239
break;
224240
}
225241
return statementList;
@@ -228,8 +244,11 @@ public static String createBinaryOpInstruction(BinaryOpInstruction instruction,
228244
public static String createNoperInstruction(SingleOpInstruction instruction, HashMap<String, Descriptor> varTable) {
229245
Element operand = instruction.getSingleOperand();
230246
if (operand instanceof ArrayOperand) {
231-
return getArrayLoadInstruction((ArrayOperand)operand, varTable)
232-
+ "\tiaload\n";
247+
String statementList = "";
248+
statementList += getArrayLoadInstruction((ArrayOperand)operand, varTable);
249+
statementList += "\tiaload\n";
250+
decreaseStackSize(1);
251+
return statementList;
233252
}
234253
return getLoadInstruction(operand, varTable);
235254
}
@@ -241,8 +260,10 @@ public static String createAssignStatement(AssignInstruction instruction, HashMa
241260
if (assignElement instanceof ArrayOperand)
242261
statementList += getArrayLoadInstruction((ArrayOperand)assignElement, varTable);
243262
statementList += JasminUtils.handleInstruction(instruction.getRhs(), varTable, true);
244-
if (assignElement instanceof ArrayOperand)
263+
if (assignElement instanceof ArrayOperand) {
245264
statementList += "\tiastore\n";
265+
decreaseStackSize(3);
266+
}
246267
else
247268
statementList += getStoreInstruction(assignElement, varTable);
248269
return statementList;
@@ -272,6 +293,7 @@ public static String createCallStatement(CallInstruction instruction, HashMap<St
272293
break;
273294
case ldc:
274295
statementList += "\tldc " + ((LiteralElement)instruction.getFirstArg()).getLiteral() + '\n';
296+
increaseStackSize(1);
275297
break;
276298
}
277299
return statementList;
@@ -288,23 +310,25 @@ public static String createGetfieldStatement(GetFieldInstruction instruction, Ha
288310
}
289311

290312
public static String createPutfieldStatement(PutFieldInstruction instruction, HashMap<String, Descriptor> varTable) {
291-
ArrayList<Element> aux = new ArrayList<>();
292-
aux.add(instruction.getThirdOperand());
313+
ArrayList<Element> arguments = new ArrayList<>();
314+
arguments.add(instruction.getThirdOperand());
293315

294316
String statementList = "";
295317
statementList += getLoadInstruction(instruction.getFirstOperand(), varTable);
296-
statementList += loadInvokeArguments(aux, varTable);
318+
statementList += loadInvokeArguments(arguments, varTable);
297319
statementList += "\tputfield "
298320
+ JasminUtils.getTypeDescriptor(instruction.getFirstOperand().getType(), false)
299321
+ '/' + ((Operand)instruction.getSecondOperand()).getName() + " "
300322
+ JasminUtils.getTypeDescriptor(instruction.getThirdOperand().getType(), true) + '\n';
323+
decreaseStackSize(arguments.size() + 1);
301324
return statementList;
302325
}
303326

304327
public static String createSingleOpConditionStatement(SingleOpCondInstruction instruction, HashMap<String, Descriptor> varTable) {
305328
String statementList = "";
306329
statementList += createNoperInstruction(instruction.getCondition(), varTable);
307330
statementList += "\tifne " + instruction.getLabel() + "\n";
331+
decreaseStackSize(1);
308332
return statementList;
309333
}
310334

@@ -337,13 +361,15 @@ public static String createAuxBranchStatement() {
337361
JasminUtils.customLabelCounter++;
338362
// if condition is false
339363
statementList += "\ticonst_0\n";
364+
increaseStackSize(1);
340365
// skip true section
341366
statementList += "\tgoto false_" + JasminUtils.customLabelCounter + "\n";
342367
JasminUtils.customLabelCounter++;
343368
// true section
344369
statementList += "\ttrue_" + (JasminUtils.customLabelCounter - 2) + ":\n";
345370
// if condition is true
346371
statementList += "\ticonst_1\n";
372+
increaseStackSize(1);
347373
// false section (for skipping true section)
348374
statementList += "\tfalse_" + (JasminUtils.customLabelCounter - 1) + ":\n";
349375
return statementList;
@@ -360,10 +386,12 @@ public static String createReturnStatement(ReturnInstruction instruction, HashMa
360386
case INT32: case BOOLEAN:
361387
statementList += getLoadInstruction(returnElement, varTable);
362388
statementList += "\tireturn\n";
389+
decreaseStackSize(1);
363390
break;
364391
case STRING: case OBJECTREF: case ARRAYREF: case THIS:
365392
statementList += getLoadInstruction(returnElement, varTable);
366393
statementList += "\tareturn\n";
394+
decreaseStackSize(1);
367395
}
368396
return statementList;
369397
}

src/main/pt/up/fe/comp2023/jasmin/JasminUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ public static String handleInstruction(Instruction instruction, HashMap<String,
109109
(CallInstruction)instruction,
110110
varTable
111111
);
112-
if (!isRhs && ((CallInstruction)instruction).getReturnType().getTypeOfElement() != ElementType.VOID)
112+
if (!isRhs && ((CallInstruction)instruction).getReturnType().getTypeOfElement() != ElementType.VOID) {
113113
statementList += "\tpop\n";
114+
JVMInstructionUtils.decreaseStackSize(1);
115+
}
114116
break;
115117
case GOTO:
116118
statementList += JVMInstructionUtils.createGotoStatement(

0 commit comments

Comments
 (0)