Skip to content

Commit a412922

Browse files
committed
Added new static field to track current stack size and methods to increase/decrease
1 parent 0c069c9 commit a412922

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ public class JVMInstructionUtils {
1212

1313
public static int numLocals = 0;
1414
public static int stackSize = 0;
15+
public static int currStackSize = 0;
16+
17+
public static void increaseStackSize(int n) {
18+
currStackSize += n;
19+
if (currStackSize > stackSize)
20+
stackSize = currStackSize;
21+
}
22+
23+
public static void decreaseStackSize(int n) {
24+
currStackSize -= n;
25+
if (currStackSize > stackSize)
26+
stackSize = currStackSize;
27+
}
1528

1629
public static String getLoadInstruction(Element element, HashMap<String, Descriptor> varTable) {
1730
if (element.isLiteral()) {
@@ -33,7 +46,7 @@ public static String getLoadInstruction(Element element, HashMap<String, Descrip
3346
else
3447
elementType = element.getType().getTypeOfElement();
3548
int virtualReg = varTable.get(((Operand)element).getName()).getVirtualReg();
36-
if (virtualReg > JVMInstructionUtils.numLocals)
49+
if (virtualReg > numLocals)
3750
numLocals = virtualReg;
3851

3952
switch (elementType) {
@@ -62,7 +75,7 @@ public static String getArrayLoadInstruction(ArrayOperand array, HashMap<String,
6275

6376
public static String getStoreInstruction(Element element, HashMap<String, Descriptor> varTable) {
6477
int virtualReg = varTable.get(((Operand)element).getName()).getVirtualReg();
65-
if (virtualReg > JVMInstructionUtils.numLocals)
78+
if (virtualReg > numLocals)
6679
numLocals = virtualReg;
6780

6881
if (element.isLiteral()) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public static String createConstructMethod(String superClassName) {
197197

198198
public static String createMethodDirective(Method method) {
199199
JVMInstructionUtils.numLocals = 0;
200-
JVMInstructionUtils.stackSize = 99;
200+
JVMInstructionUtils.stackSize = 0;
201201
String instructions = handleMethodStatements(method);
202202
if (method.isStaticMethod() && method.getParams().size() > 0)
203203
JVMInstructionUtils.numLocals++;

0 commit comments

Comments
 (0)