From 02039a5334cc3ffc8c0a55616d1cdcbcbdb9a074 Mon Sep 17 00:00:00 2001 From: 404Simon Date: Mon, 20 May 2024 16:59:30 +0200 Subject: [PATCH 1/7] accurately depicted stack size --- output/ClassCanBeTyped.class | Bin 669 -> 0 bytes src/main/java/de/maishai/Compiler.java | 6 ++---- .../typedast/typedclass/TypedAssignment.java | 8 +++---- .../typedast/typedclass/TypedBinary.java | 2 ++ .../typedclass/TypedFieldVarAccess.java | 1 + .../typedast/typedclass/TypedIfElse.java | 1 + .../maishai/typedast/typedclass/TypedNew.java | 8 +++---- .../JavaTestfiles/ClassCanBeBytecoded.java | 20 ++++++++++++++++++ 8 files changed, 32 insertions(+), 14 deletions(-) delete mode 100644 output/ClassCanBeTyped.class create mode 100644 src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java diff --git a/output/ClassCanBeTyped.class b/output/ClassCanBeTyped.class deleted file mode 100644 index 404c65be6356b71ee67e9b2b980dbbcbe06124f8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 669 zcmah^O;5s55S*ojYRg9v@LSKt3m0#K2YVth@nVSaR-i#h1rr)0|H*@%Og#7l{87f) zS^^@8hkf(j&dlz-&#(6n0H-(>l>8`|PW@nf5nj(8!oCF&xTD}PXhgwy(75W3!d`5_ z5Xcj&&6=^gg7W2x_l*6XAlDp@hw-_<^xE}~!0_swJRB5km?#ReUOVaMh`tIcs$ZEU zsM5V=gQ;RcO^2+b2Vs1ptNl8ontb_O;H{G~7bW(67~f6$eh@`T)LXjzNk4=j!@OWI zGIG2LtQ0q}nY`@;9WoB`JdfBd6Eno@$s03`gX)V-4qcXYUGH(fLr zD|GWbfhPZzU<;L$R@4NRYq#8L>xEa1SVb)r6!=zw%&BeYULCEJeomfwsB0H{OO$AD V;o4uN=B@)Arc{xru8i@+;TwfvSwR2* diff --git a/src/main/java/de/maishai/Compiler.java b/src/main/java/de/maishai/Compiler.java index 08a70a1..9a06d14 100644 --- a/src/main/java/de/maishai/Compiler.java +++ b/src/main/java/de/maishai/Compiler.java @@ -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")); } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java index 7985d3f..922664c 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java @@ -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) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java b/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java index 5ebe4fd..e9a1c83 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java @@ -206,5 +206,7 @@ public class TypedBinary implements TypedExpression { } } ctx.popStack(); + ctx.popStack(); + ctx.pushAnonToStack(); } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java index 74ab48a..ceb0f44 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java @@ -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(); } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java b/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java index 69a5240..985d464 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java @@ -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); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedNew.java b/src/main/java/de/maishai/typedast/typedclass/TypedNew.java index 9c8497e..3ee9c4a 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedNew.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedNew.java @@ -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(), "", CodeGenUtils.generateDescriptor(args.stream().map(TypedExpression::getType).toList(), Type.VOID), false); + for (TypedExpression arg : args) { + ctx.popStack(); + } System.out.println("INVOKESPECIAL " + type.getReference() + " " + CodeGenUtils.generateDescriptor(args.stream().map(TypedExpression::getType).toList(), Type.VOID)); } } diff --git a/src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java b/src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java new file mode 100644 index 0000000..a4f48ad --- /dev/null +++ b/src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java @@ -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; + } +} \ No newline at end of file From acf7b4eff8be110a65aaba7bdebe9f7625270b31 Mon Sep 17 00:00:00 2001 From: 404Simon Date: Mon, 20 May 2024 17:53:15 +0200 Subject: [PATCH 2/7] accurately depicted stack size --- .../java/de/maishai/typedast/typedclass/TypedAssignment.java | 5 ++++- .../java/de/maishai/typedast/typedclass/TypedReturn.java | 4 +++- src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java index 922664c..fd2a733 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java @@ -98,7 +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()); - + ctx.popStack(); } else { if(value.getType().getKind() == Type.Kind.REFERENCE) { System.out.println("ASTORE " + ctx.getLocalVar(location.getName()).get().index()); @@ -115,5 +115,8 @@ public class TypedAssignment implements TypedStatement { location.getRecursiveOwnerChain().codeGen(ctx); ctx.pushAnonToStack(); } + if (location.getRecursiveOwnerChain() == null && location.getField()) { + ctx.pushStack("this"); + } } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java index 06a854e..b7599c2 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java @@ -47,13 +47,15 @@ public class TypedReturn implements TypedStatement { public void codeGen(MethodContext ctx) { if (ret == null) { ctx.getMv().visitInsn(Opcodes.RETURN); + System.out.println("RETURN"); } else { - System.out.println("return: " + ret); ret.codeGen(ctx); if (ret.getType().getKind() != Type.Kind.REFERENCE) { ctx.getMv().visitInsn(Opcodes.IRETURN); + System.out.println("IRETURN"); } else { ctx.getMv().visitInsn(Opcodes.ARETURN); + System.out.println("ARETURN"); } } diff --git a/src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java b/src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java index a4f48ad..d479b86 100644 --- a/src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java +++ b/src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java @@ -1,4 +1,5 @@ public class ClassCanBeBytecoded { + public ClassCanBeBytecoded c; public ClassCanBeBytecoded() { } @@ -15,6 +16,7 @@ public class ClassCanBeBytecoded { ClassCanBeBytecoded c; c = new ClassCanBeBytecoded(result); result = var1 + var1; + this.c = c; return c; } } \ No newline at end of file From 99ed3a322b4ff07bfbb1e4b2e1ab84656e4717d3 Mon Sep 17 00:00:00 2001 From: 404Simon Date: Mon, 20 May 2024 18:44:34 +0200 Subject: [PATCH 3/7] add methodcall --- .../typedast/typedclass/TypedAssignment.java | 4 +-- .../typedclass/TypedFieldVarAccess.java | 10 ++++++- .../typedast/typedclass/TypedMethodCall.java | 26 +++++++++++++++++++ .../JavaTestfiles/ClassCanBeBytecoded.java | 20 +++++--------- 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java index fd2a733..89c4eaf 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java @@ -81,7 +81,7 @@ public class TypedAssignment implements TypedStatement { @Override public void codeGen(MethodContext ctx) { - if(value instanceof TypedNew) { + if(value instanceof TypedNew || value instanceof TypedMethodCall) { value.codeGen(ctx); getOwnerChain(ctx); } else { @@ -104,6 +104,7 @@ public class TypedAssignment implements TypedStatement { System.out.println("ASTORE " + ctx.getLocalVar(location.getName()).get().index()); ctx.getMv().visitVarInsn(Opcodes.ASTORE, ctx.getLocalVar(location.getName()).get().index()); } else { + System.out.println("ISTORE " + ctx.getLocalVar(location.getName()).get().index()); ctx.getMv().visitVarInsn(Opcodes.ISTORE, ctx.getLocalVar(location.getName()).get().index()); } } @@ -113,7 +114,6 @@ public class TypedAssignment implements TypedStatement { private void getOwnerChain(MethodContext ctx) { if (location.getRecursiveOwnerChain() != null) { location.getRecursiveOwnerChain().codeGen(ctx); - ctx.pushAnonToStack(); } if (location.getRecursiveOwnerChain() == null && location.getField()) { ctx.pushStack("this"); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java index ceb0f44..2396544 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java @@ -108,12 +108,20 @@ public class TypedFieldVarAccess implements TypedExpression { public void codeGen(MethodContext ctx) { if (recursiveOwnerChain != null) { recursiveOwnerChain.codeGen(ctx); + ctx.popStack(); } if (field && recursiveOwnerChain == null) { ctx.pushStack("this"); + ctx.popStack(); } if (field) { - ctx.getMv().visitFieldInsn(Opcodes.GETFIELD, recursiveOwnerChain.getType().getReference(), name, type.getDescriptor()); + String ownerChainName; + if (recursiveOwnerChain != null) { + ownerChainName = recursiveOwnerChain.getType().getReference(); + } else { + ownerChainName = type.getReference(); + } + ctx.getMv().visitFieldInsn(Opcodes.GETFIELD, ownerChainName, name, type.getDescriptor()); } else { int loadOpcode = type.getKind() == Type.Kind.REFERENCE ? Opcodes.ALOAD : Opcodes.ILOAD; ctx.getMv().visitVarInsn(loadOpcode, ctx.getLocalVar(name).get().index()); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java index 21fe9d7..922021f 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java @@ -5,6 +5,7 @@ import de.maishai.ast.records.MethodCall; import de.maishai.typedast.*; import lombok.Data; import lombok.NoArgsConstructor; +import org.objectweb.asm.Opcodes; import java.util.ArrayList; import java.util.List; @@ -83,6 +84,31 @@ public class TypedMethodCall implements TypedExpression, TypedStatement { @Override public void codeGen(MethodContext ctx) { + getOwnerChain(ctx); + for (TypedExpression arg : args) { + arg.codeGen(ctx); + } + String descriptor = CodeGenUtils.generateDescriptor(args.stream().map(TypedExpression::getType).toList(), type); + String methodOwnerClass; + if (recipient.getRecursiveOwnerChain() == null) { + methodOwnerClass = ctx.getClassContext().getName(); + } else { + methodOwnerClass = recipient.getRecursiveOwnerChain().getType().getReference(); + } + ctx.getMv().visitMethodInsn(Opcodes.INVOKEVIRTUAL, methodOwnerClass, recipient.getName(), descriptor, false); + System.out.println("INVOKEVIRTUAL " + methodOwnerClass + " " + recipient.getName() + " " + descriptor); + ctx.popStack(); // pop the owner + for (TypedExpression arg : args) { + ctx.popStack(); + } + ctx.pushAnonToStack(); + } + private void getOwnerChain(MethodContext ctx) { + if (recipient.getRecursiveOwnerChain() != null) { + recipient.getRecursiveOwnerChain().codeGen(ctx); + } else { + ctx.pushStack("this"); + } } } diff --git a/src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java b/src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java index d479b86..05b5dcf 100644 --- a/src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java +++ b/src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java @@ -1,22 +1,16 @@ public class ClassCanBeBytecoded { public ClassCanBeBytecoded c; + public int x; public ClassCanBeBytecoded() { } - public ClassCanBeBytecoded(int var1) { + public int callable() { + return 1; } - 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; - this.c = c; - return c; + public int test(int var1) { + int i; + i = this.c.c.callable(); + return i; } } \ No newline at end of file From de4082eff38f5e291bbc1eaa9d64a93cf8736bf3 Mon Sep 17 00:00:00 2001 From: 404Simon Date: Mon, 20 May 2024 20:18:23 +0200 Subject: [PATCH 4/7] add while and do while --- .../maishai/typedast/typedclass/TypedDoWhile.java | 14 ++++++++++++++ .../de/maishai/typedast/typedclass/TypedWhile.java | 13 +++++++++++++ .../JavaTestfiles/ClassCanBeBytecoded.java | 7 ++++--- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java b/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java index 7a831d7..c95617f 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java @@ -3,6 +3,8 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.*; import de.maishai.typedast.*; import lombok.Data; +import org.objectweb.asm.Label; +import org.objectweb.asm.Opcodes; import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression; @@ -33,6 +35,18 @@ public class TypedDoWhile implements TypedStatement { @Override public void codeGen(MethodContext ctx) { + Label loopStart = new Label(); + Label loopEnd = new Label(); + ctx.getMv().visitLabel(loopStart); + + typedBlock.codeGen(ctx); + + cond.codeGen(ctx); + + ctx.getMv().visitJumpInsn(Opcodes.IFNE, loopStart); + ctx.popStack(); + + ctx.getMv().visitLabel(loopEnd); } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java b/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java index 6591278..8625a07 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java @@ -3,6 +3,9 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.*; import de.maishai.typedast.*; import lombok.Data; +import org.objectweb.asm.Label; +import org.objectweb.asm.Opcodes; + import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression; @@ -33,6 +36,16 @@ public class TypedWhile implements TypedStatement { @Override public void codeGen(MethodContext ctx) { + Label loopStart = new Label(); + Label loopEnd = new Label(); + ctx.getMv().visitLabel(loopStart); + cond.codeGen(ctx); + + ctx.getMv().visitJumpInsn(Opcodes.IFEQ, loopEnd); + ctx.popStack(); + typedBlock.codeGen(ctx); + ctx.getMv().visitJumpInsn(Opcodes.GOTO, loopStart); + ctx.getMv().visitLabel(loopEnd); } } diff --git a/src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java b/src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java index 05b5dcf..09d873d 100644 --- a/src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java +++ b/src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java @@ -9,8 +9,9 @@ public class ClassCanBeBytecoded { } public int test(int var1) { - int i; - i = this.c.c.callable(); - return i; + do { + var1 = var1 + 1; + } while (var1 < 10); + return var1; } } \ No newline at end of file From cc68035406ad2fc4144ffc1f28cf6bacb27ae2a4 Mon Sep 17 00:00:00 2001 From: 404Simon Date: Mon, 20 May 2024 20:24:00 +0200 Subject: [PATCH 5/7] add for --- .../de/maishai/typedast/typedclass/TypedFor.java | 16 ++++++++++++++++ .../JavaTestfiles/ClassCanBeBytecoded.java | 5 +++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFor.java b/src/main/java/de/maishai/typedast/typedclass/TypedFor.java index bb18699..e72e630 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedFor.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFor.java @@ -5,6 +5,8 @@ import de.maishai.typedast.*; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; +import org.objectweb.asm.Label; +import org.objectweb.asm.Opcodes; import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression; @@ -42,6 +44,20 @@ public class TypedFor implements TypedStatement { @Override public void codeGen(MethodContext ctx) { + Label loopStart = new Label(); + Label loopUpdate = new Label(); + Label loopEnd = new Label(); + assign.codeGen(ctx); + ctx.getMv().visitLabel(loopStart); + cond.codeGen(ctx); + ctx.getMv().visitJumpInsn(Opcodes.IFEQ, loopEnd); + ctx.popStack(); + + typedBlock.codeGen(ctx); + ctx.getMv().visitLabel(loopUpdate); + inc.codeGen(ctx); + ctx.getMv().visitJumpInsn(Opcodes.GOTO, loopStart); + ctx.getMv().visitLabel(loopEnd); } } diff --git a/src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java b/src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java index 09d873d..2e19958 100644 --- a/src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java +++ b/src/main/resources/JavaTestfiles/ClassCanBeBytecoded.java @@ -9,9 +9,10 @@ public class ClassCanBeBytecoded { } public int test(int var1) { - do { + int i; + for (i = 0; i < 12; i = i + 1) { var1 = var1 + 1; - } while (var1 < 10); + } return var1; } } \ No newline at end of file From 050dedd4dbc32936c045f9d312d84369458c510f Mon Sep 17 00:00:00 2001 From: 404Simon Date: Mon, 20 May 2024 20:45:17 +0200 Subject: [PATCH 6/7] remove unused labels and make break work --- src/main/java/de/maishai/typedast/MethodContext.java | 10 +++------- .../de/maishai/typedast/typedclass/TypedBreak.java | 6 +++++- .../de/maishai/typedast/typedclass/TypedDoWhile.java | 1 + .../java/de/maishai/typedast/typedclass/TypedFor.java | 1 + .../de/maishai/typedast/typedclass/TypedWhile.java | 1 + .../resources/JavaTestfiles/ClassCanBeBytecoded.java | 10 ++++++---- 6 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/maishai/typedast/MethodContext.java b/src/main/java/de/maishai/typedast/MethodContext.java index f252656..2c12164 100644 --- a/src/main/java/de/maishai/typedast/MethodContext.java +++ b/src/main/java/de/maishai/typedast/MethodContext.java @@ -15,23 +15,20 @@ public class MethodContext { public record LocalVariable(String name, int index, Type type) { } - private Label startLabel; - private Label endLabel; private MethodVisitor mv; - private ClassContext classContext; + private final ClassContext classContext; private int localVarIndex = 0; private final Map variableIndex = new HashMap<>(); private Stack stack = new Stack<>(); private int maxStack = 0; + //used to jump out of loops with break + private final Stack