From 99ed3a322b4ff07bfbb1e4b2e1ab84656e4717d3 Mon Sep 17 00:00:00 2001 From: 404Simon Date: Mon, 20 May 2024 18:44:34 +0200 Subject: [PATCH] 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