accurately depicted stack size

This commit is contained in:
404Simon 2024-05-20 16:59:30 +02:00
parent f1b366e157
commit 02039a5334
8 changed files with 32 additions and 14 deletions

Binary file not shown.

View File

@ -98,9 +98,7 @@ public class Compiler {
}
public static void main(String[] args) {
generateByteCodeFileFromFile(List.of("src/main/resources/JavaTestfiles/ClassWithConstructor.java",
"src/main/resources/JavaTestfiles/ClassWithConstructorAndMethodCall.java",
"src/main/resources/JavaTestfiles/ComplexClass.java"),
List.of("ClassWithConstructor","ClassWithConstructorAndMethodCall","ComplexClass"));
generateByteCodeFileFromFile(List.of("src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java"),
List.of("ClassCanBeBytecoded"));
}
}

View File

@ -85,13 +85,11 @@ public class TypedAssignment implements TypedStatement {
value.codeGen(ctx);
getOwnerChain(ctx);
} else {
ctx.pushStack("this");
getOwnerChain(ctx);
value.codeGen(ctx);
}
//save value in field
//save value
if (location.getField()) {
String receiver = ctx.getClassContext().getName();
@ -100,6 +98,7 @@ public class TypedAssignment implements TypedStatement {
}
ctx.getMv().visitFieldInsn(Opcodes.PUTFIELD, receiver, location.getName(), value.getType().getDescriptor());
System.out.println("PUTFIELD: " + receiver + " " + location.getName() + " " + value.getType().getDescriptor());
} else {
if(value.getType().getKind() == Type.Kind.REFERENCE) {
System.out.println("ASTORE " + ctx.getLocalVar(location.getName()).get().index());
@ -108,8 +107,7 @@ public class TypedAssignment implements TypedStatement {
ctx.getMv().visitVarInsn(Opcodes.ISTORE, ctx.getLocalVar(location.getName()).get().index());
}
}
//ctx.popStack();
//ctx.popStack();
ctx.popStack();
}
private void getOwnerChain(MethodContext ctx) {

View File

@ -206,5 +206,7 @@ public class TypedBinary implements TypedExpression {
}
}
ctx.popStack();
ctx.popStack();
ctx.pushAnonToStack();
}
}

View File

@ -119,5 +119,6 @@ public class TypedFieldVarAccess implements TypedExpression {
ctx.getMv().visitVarInsn(loadOpcode, ctx.getLocalVar(name).get().index());
System.out.println(loadOpcode == Opcodes.ALOAD ? "ALOAD " + ctx.getLocalVar(name).get().index() : "ILOAD " + ctx.getLocalVar(name).get().index());
}
ctx.pushAnonToStack();
}
}

View File

@ -49,6 +49,7 @@ public class TypedIfElse implements TypedStatement {
Label end = new Label();
typedCon.codeGen(ctx);
ctx.getMv().visitJumpInsn(Opcodes.IFEQ, falseLabel);
ctx.popStack();
ifTypedBlock.codeGen(ctx);
ctx.getMv().visitJumpInsn(Opcodes.GOTO, end);

View File

@ -55,11 +55,6 @@ public class TypedNew implements TypedExpression, TypedStatement {
@Override
public void codeGen(MethodContext ctx) {
//pop the stack
//ctx.popStack();
//ctx.getMv().visitInsn(Opcodes.POP);
//System.out.println("Popped stack");
ctx.getMv().visitTypeInsn(Opcodes.NEW, type.getReference());
System.out.println("NEW " + type.getReference());
ctx.pushAnonToStack();
@ -70,6 +65,9 @@ public class TypedNew implements TypedExpression, TypedStatement {
arg.codeGen(ctx);
}
ctx.getMv().visitMethodInsn(Opcodes.INVOKESPECIAL, type.getReference(), "<init>", CodeGenUtils.generateDescriptor(args.stream().map(TypedExpression::getType).toList(), Type.VOID), false);
for (TypedExpression arg : args) {
ctx.popStack();
}
System.out.println("INVOKESPECIAL " + type.getReference() + "<init> " + CodeGenUtils.generateDescriptor(args.stream().map(TypedExpression::getType).toList(), Type.VOID));
}
}

View File

@ -0,0 +1,20 @@
public class ClassCanBeBytecoded {
public ClassCanBeBytecoded() {
}
public ClassCanBeBytecoded(int var1) {
}
public ClassCanBeBytecoded test(int var1) {
int result;
if (var1 > 10) {
result = var1 - 10;
} else {
result = var1 + 10;
}
ClassCanBeBytecoded c;
c = new ClassCanBeBytecoded(result);
result = var1 + var1;
return c;
}
}