@@ -27,6 +27,7 @@ public static void decreaseStackSize(int n) {
27
27
}
28
28
29
29
public static String getLoadInstruction (Element element , HashMap <String , Descriptor > varTable ) {
30
+ increaseStackSize (1 );
30
31
if (element .isLiteral ()) {
31
32
int literal = parseInt (((LiteralElement )element ).getLiteral ());
32
33
if (literal >= 0 && literal <= 5 )
@@ -74,6 +75,7 @@ public static String getArrayLoadInstruction(ArrayOperand array, HashMap<String,
74
75
}
75
76
76
77
public static String getStoreInstruction (Element element , HashMap <String , Descriptor > varTable ) {
78
+ decreaseStackSize (1 );
77
79
int virtualReg = varTable .get (((Operand )element ).getName ()).getVirtualReg ();
78
80
if (virtualReg > numLocals )
79
81
numLocals = virtualReg ;
@@ -128,13 +130,15 @@ public static String getInvokeVirtualInstruction(CallInstruction instruction, Ha
128
130
statementList += getLoadInstruction (instruction .getFirstArg (), varTable );
129
131
statementList += loadInvokeArguments (instruction .getListOfOperands (), varTable );
130
132
statementList += "\t invokevirtual " + createInvokeInstructionArgument (instruction , false );
133
+ decreaseStackSize (instruction .getListOfOperands ().size () + 1 );
131
134
return statementList ;
132
135
}
133
136
134
137
public static String getInvokeStaticInstruction (CallInstruction instruction , HashMap <String , Descriptor > varTable ) {
135
138
String statementList = "" ;
136
139
statementList += loadInvokeArguments (instruction .getListOfOperands (), varTable );
137
140
statementList += "\t invokestatic " + createInvokeInstructionArgument (instruction , true );
141
+ decreaseStackSize (instruction .getListOfOperands ().size ());
138
142
return statementList ;
139
143
}
140
144
@@ -147,6 +151,7 @@ public static String getNewInstruction(CallInstruction instruction, HashMap<Stri
147
151
statementList += loadInvokeArguments (instruction .getListOfOperands (), varTable );
148
152
statementList += "\t new " + ((Operand )instruction .getFirstArg ()).getName () + '\n' ;
149
153
statementList += "\t dup\n " ;
154
+ increaseStackSize (2 );
150
155
return statementList ;
151
156
}
152
157
@@ -172,6 +177,7 @@ public static String createUnaryOpStatement(UnaryOpInstruction instruction, Hash
172
177
case NOT : case NOTB :
173
178
statementList += "\t ifeq " ;
174
179
statementList += createAuxBranchStatement ();
180
+ decreaseStackSize (1 );
175
181
break ;
176
182
}
177
183
return statementList ;
@@ -185,41 +191,51 @@ public static String createBinaryOpInstruction(BinaryOpInstruction instruction,
185
191
switch (instruction .getOperation ().getOpType ()) {
186
192
case ADD :
187
193
statementList += "\t iadd\n " ;
194
+ decreaseStackSize (1 );
188
195
break ;
189
196
case SUB :
190
197
statementList += "\t isub\n " ;
198
+ decreaseStackSize (1 );
191
199
break ;
192
200
case MUL :
193
201
statementList += "\t imul\n " ;
202
+ decreaseStackSize (1 );
194
203
break ;
195
204
case DIV :
196
205
statementList += "\t idiv\n " ;
206
+ decreaseStackSize (1 );
197
207
break ;
198
208
case AND : case ANDB :
199
209
statementList += "\t iand\n " ;
210
+ decreaseStackSize (1 );
200
211
break ;
201
212
case OR : case ORB :
202
213
statementList += "\t ior\n " ;
214
+ decreaseStackSize (1 );
203
215
break ;
204
216
case LTH :
205
217
statementList += "\t if_icmplt " ;
206
218
if (!isBranchCond )
207
219
statementList += createAuxBranchStatement ();
220
+ decreaseStackSize (2 );
208
221
break ;
209
222
case LTE :
210
223
statementList += "\t if_icmple " ;
211
224
if (!isBranchCond )
212
225
statementList += createAuxBranchStatement ();
226
+ decreaseStackSize (2 );
213
227
break ;
214
228
case GTH :
215
229
statementList += "\t if_icmpgt " ;
216
230
if (!isBranchCond )
217
231
statementList += createAuxBranchStatement ();
232
+ decreaseStackSize (2 );
218
233
break ;
219
234
case GTE :
220
235
statementList += "\t if_icmpge " ;
221
236
if (!isBranchCond )
222
237
statementList += createAuxBranchStatement ();
238
+ decreaseStackSize (2 );
223
239
break ;
224
240
}
225
241
return statementList ;
@@ -228,8 +244,11 @@ public static String createBinaryOpInstruction(BinaryOpInstruction instruction,
228
244
public static String createNoperInstruction (SingleOpInstruction instruction , HashMap <String , Descriptor > varTable ) {
229
245
Element operand = instruction .getSingleOperand ();
230
246
if (operand instanceof ArrayOperand ) {
231
- return getArrayLoadInstruction ((ArrayOperand )operand , varTable )
232
- + "\t iaload\n " ;
247
+ String statementList = "" ;
248
+ statementList += getArrayLoadInstruction ((ArrayOperand )operand , varTable );
249
+ statementList += "\t iaload\n " ;
250
+ decreaseStackSize (1 );
251
+ return statementList ;
233
252
}
234
253
return getLoadInstruction (operand , varTable );
235
254
}
@@ -241,8 +260,10 @@ public static String createAssignStatement(AssignInstruction instruction, HashMa
241
260
if (assignElement instanceof ArrayOperand )
242
261
statementList += getArrayLoadInstruction ((ArrayOperand )assignElement , varTable );
243
262
statementList += JasminUtils .handleInstruction (instruction .getRhs (), varTable , true );
244
- if (assignElement instanceof ArrayOperand )
263
+ if (assignElement instanceof ArrayOperand ) {
245
264
statementList += "\t iastore\n " ;
265
+ decreaseStackSize (3 );
266
+ }
246
267
else
247
268
statementList += getStoreInstruction (assignElement , varTable );
248
269
return statementList ;
@@ -272,6 +293,7 @@ public static String createCallStatement(CallInstruction instruction, HashMap<St
272
293
break ;
273
294
case ldc :
274
295
statementList += "\t ldc " + ((LiteralElement )instruction .getFirstArg ()).getLiteral () + '\n' ;
296
+ increaseStackSize (1 );
275
297
break ;
276
298
}
277
299
return statementList ;
@@ -288,23 +310,25 @@ public static String createGetfieldStatement(GetFieldInstruction instruction, Ha
288
310
}
289
311
290
312
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 ());
293
315
294
316
String statementList = "" ;
295
317
statementList += getLoadInstruction (instruction .getFirstOperand (), varTable );
296
- statementList += loadInvokeArguments (aux , varTable );
318
+ statementList += loadInvokeArguments (arguments , varTable );
297
319
statementList += "\t putfield "
298
320
+ JasminUtils .getTypeDescriptor (instruction .getFirstOperand ().getType (), false )
299
321
+ '/' + ((Operand )instruction .getSecondOperand ()).getName () + " "
300
322
+ JasminUtils .getTypeDescriptor (instruction .getThirdOperand ().getType (), true ) + '\n' ;
323
+ decreaseStackSize (arguments .size () + 1 );
301
324
return statementList ;
302
325
}
303
326
304
327
public static String createSingleOpConditionStatement (SingleOpCondInstruction instruction , HashMap <String , Descriptor > varTable ) {
305
328
String statementList = "" ;
306
329
statementList += createNoperInstruction (instruction .getCondition (), varTable );
307
330
statementList += "\t ifne " + instruction .getLabel () + "\n " ;
331
+ decreaseStackSize (1 );
308
332
return statementList ;
309
333
}
310
334
@@ -337,13 +361,15 @@ public static String createAuxBranchStatement() {
337
361
JasminUtils .customLabelCounter ++;
338
362
// if condition is false
339
363
statementList += "\t iconst_0\n " ;
364
+ increaseStackSize (1 );
340
365
// skip true section
341
366
statementList += "\t goto false_" + JasminUtils .customLabelCounter + "\n " ;
342
367
JasminUtils .customLabelCounter ++;
343
368
// true section
344
369
statementList += "\t true_" + (JasminUtils .customLabelCounter - 2 ) + ":\n " ;
345
370
// if condition is true
346
371
statementList += "\t iconst_1\n " ;
372
+ increaseStackSize (1 );
347
373
// false section (for skipping true section)
348
374
statementList += "\t false_" + (JasminUtils .customLabelCounter - 1 ) + ":\n " ;
349
375
return statementList ;
@@ -360,10 +386,12 @@ public static String createReturnStatement(ReturnInstruction instruction, HashMa
360
386
case INT32 : case BOOLEAN :
361
387
statementList += getLoadInstruction (returnElement , varTable );
362
388
statementList += "\t ireturn\n " ;
389
+ decreaseStackSize (1 );
363
390
break ;
364
391
case STRING : case OBJECTREF : case ARRAYREF : case THIS :
365
392
statementList += getLoadInstruction (returnElement , varTable );
366
393
statementList += "\t areturn\n " ;
394
+ decreaseStackSize (1 );
367
395
}
368
396
return statementList ;
369
397
}
0 commit comments