diff --git a/src/main/java/de/maishai/typedast/ClassContext.java b/src/main/java/de/maishai/typedast/ClassContext.java index 50e100c..23cbaa8 100644 --- a/src/main/java/de/maishai/typedast/ClassContext.java +++ b/src/main/java/de/maishai/typedast/ClassContext.java @@ -6,11 +6,13 @@ import org.objectweb.asm.ClassWriter; @Data public class ClassContext { - private Type name; + private String name; + private Type type; private ClassWriter cw; public ClassContext(String name, ClassWriter cw) { - this.name = Type.REFERENCE(name); + this.name = name; + type = Type.REFERENCE(name); this.cw = cw; } } diff --git a/src/main/java/de/maishai/typedast/MethodContext.java b/src/main/java/de/maishai/typedast/MethodContext.java index b3b0832..3d7d00b 100644 --- a/src/main/java/de/maishai/typedast/MethodContext.java +++ b/src/main/java/de/maishai/typedast/MethodContext.java @@ -28,7 +28,7 @@ public class MethodContext { endLabel = new Label(); this.mv = mv; this.classContext = classContext; - registerVariable("this", classContext.getName()); + registerVariable("this", classContext.getType()); mv.visitCode(); mv.visitLabel(startLabel); } @@ -58,6 +58,7 @@ public class MethodContext { } else { mv.visitVarInsn(Opcodes.ILOAD, localVariable.index); } + System.out.println("Pushed " + variableIndex.get(varName) + " to stack"); } public void popStack() { @@ -66,6 +67,7 @@ public class MethodContext { public void wrapUp() { mv.visitLabel(endLabel); + System.out.println("maxStack: " + maxStack + " localVarIndex: " + localVarIndex); mv.visitMaxs(maxStack, localVarIndex); mv.visitEnd(); } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java index cc8992e..720050b 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java @@ -63,9 +63,19 @@ public class TypedAssignment implements TypedStatement { @Override public void codeGen(MethodContext ctx) { - if (value instanceof TypedIntLiteral) { - ctx.getMv().visitIntInsn(Opcodes.BIPUSH, ((TypedIntLiteral) value).getValue()); - ctx.getMv().visitVarInsn(Opcodes.ISTORE, ctx.getLocalVarIndex()); + ctx.pushStack("this"); + // load location recursively on stack + location.codeGen(ctx); + + // put value on stack (WIP!!) + ctx.pushStack("x"); + + //save value in field + String receiver = ctx.getClassContext().getName(); + if (location.getRecursiveOwnerChain() != null) { + receiver = location.getRecursiveOwnerChain().getType().getReference(); } + ctx.getMv().visitFieldInsn(Opcodes.PUTFIELD, receiver, location.getName(), value.getType().getDescriptor()); + System.out.println("PUTFIELD: " + receiver + " " + location.getName() + " " + value.getType().getDescriptor()); } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java index 7c53585..8867511 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java @@ -77,11 +77,14 @@ public class TypedConstructor implements TypedNode { public void codeGen(ClassContext ctx) { int accessModifier = Opcodes.ACC_PUBLIC; // ist laut Andi ok MethodVisitor mv = ctx.getCw().visitMethod(accessModifier, "", CodeGenUtils.generateDescriptor(typedParameters, Type.VOID), null, null); + System.out.println("Visiting method: " + "" + CodeGenUtils.generateDescriptor(typedParameters, Type.VOID)); MethodContext mctx = new MethodContext(ctx, mv); typedParameters.forEach(param -> mctx.registerVariable(param.getParaName(), param.getType())); //super(); mctx.pushStack("this"); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "", "()V", false); + System.out.println("INVOKESPECIAL: " + "java/lang/Object" + " " + "" + " " + "()V"); + mctx.popStack(); typedBlock.codeGen(mctx); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java index 621ec43..8eb8ede 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java @@ -7,6 +7,7 @@ import de.maishai.typedast.Type; import lombok.Data; import lombok.NoArgsConstructor; import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; import java.util.Map; @@ -76,6 +77,12 @@ public class TypedFieldVarAccess implements TypedExpression { @Override public void codeGen(MethodContext ctx) { - + if (recursiveOwnerChain != null) { + recursiveOwnerChain.codeGen(ctx); + } + if (!field) { + ctx.getMv().visitFieldInsn(Opcodes.GETFIELD, ctx.getClassContext().getName(), name, type.getDescriptor()); + System.out.println("GETFIELD: " + ctx.getClassContext().getName() + " " + name + " " + type.getDescriptor()); + } } } diff --git a/src/main/resources/JavaTestfiles/ClassCanBeTyped.java b/src/main/resources/JavaTestfiles/ClassCanBeTyped.java index b4d9fd9..4c399fe 100644 --- a/src/main/resources/JavaTestfiles/ClassCanBeTyped.java +++ b/src/main/resources/JavaTestfiles/ClassCanBeTyped.java @@ -1,81 +1,10 @@ public class ClassCanBeTyped { + public ClassCanBeTyped c; + public int x; - int x; - int y; - ClassCanBeTyped b; - ClassCanBeTyped c; public ClassCanBeTyped(int x) { - this.x = x; + this.c.x = x; } - public ClassCanBeTyped(int x, int y) { - this.x = x; - this.y = y; - } - public ClassCanBeTyped initClassCanBeTyped(int x) { - int a; - a = 10; - b = new ClassCanBeTyped(x); - b.x = 10 + a; - b.y = 20; - b.c.x = 20 + a; - b.c.b.y = b.x; - - return b; - } - public ClassCanBeTyped init(int x, int y) { - return new ClassCanBeTyped(x, y); - } - public ClassCanBeTyped(int x) { - this.x = x; - int i; - b = b.getX().c.getC(); - i = b.getX().c.getX(); - } - - public ClassCanBeTyped() { - this.x = 10; - int i; - for (i = 0; i < (x + 1); i = i + 1) { - int j; - for (j = 0; j < this.x; j += 1) { - this.x = this.x * this.x; - break; - } - } - do { - this.y = this.y + 1; - } while (this.y < 10); - - int k; - k = 0; - for (k = 0; k < 10; k = k + 1) { - if (k == 5) { - return; - } - } - - } - - public ClassCanBeTyped(int x, int y, char z) { - this.x = x; - this.y = y; - } - - public int getX(char z) { - return this.x; - } - - public ClassCanBeTyped getC() { - return c; - } - - public int getX() { - return x; - } - - public boolean methodCall() { - return false; - } - - } + +