add methodcall

This commit is contained in:
404Simon 2024-05-20 18:44:34 +02:00
parent acf7b4eff8
commit 99ed3a322b
4 changed files with 44 additions and 16 deletions

View File

@ -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");

View File

@ -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());

View File

@ -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");
}
}
}

View File

@ -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;
}
}