diff --git a/src/main/java/de/maishai/typedast/MethodContext.java b/src/main/java/de/maishai/typedast/MethodContext.java index c8aba5c..db66b0b 100644 --- a/src/main/java/de/maishai/typedast/MethodContext.java +++ b/src/main/java/de/maishai/typedast/MethodContext.java @@ -1,6 +1,5 @@ package de.maishai.typedast; -import de.maishai.VariableGenerator; import lombok.*; import org.objectweb.asm.Label; import org.objectweb.asm.MethodVisitor; diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java b/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java index 3a2d459..2aa1270 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java @@ -7,6 +7,7 @@ import de.maishai.typedast.TypedExpression; import de.maishai.typedast.Type; import lombok.Data; import lombok.NoArgsConstructor; +import org.objectweb.asm.Label; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; @@ -69,14 +70,63 @@ public class TypedBinary implements TypedExpression { @Override public void codeGen(MethodContext ctx) { - left.codeGen(ctx); - right.codeGen(ctx); - if (op == Operator.ADD) { - ctx.getMv().visitInsn(Opcodes.IADD); - } else if (op == Operator.SUB) { - ctx.getMv().visitInsn(Opcodes.ISUB); - } else if (op == Operator.MUL) { - ctx.getMv().visitInsn(Opcodes.IMUL); + if (op == Operator.AND) { + Label end = new Label(); + Label returnTrue = new Label(); + Label returnFalse = new Label(); + left.codeGen(ctx); + ctx.getMv().visitJumpInsn(Opcodes.IFNE, returnFalse); + right.codeGen(ctx); + ctx.getMv().visitJumpInsn(Opcodes.IFNE, returnFalse); + ctx.getMv().visitJumpInsn(Opcodes.GOTO, returnTrue); + + ctx.getMv().visitLabel(returnFalse); + ctx.getMv().visitInsn(Opcodes.ICONST_0); + ctx.getMv().visitJumpInsn(Opcodes.GOTO, end); + ctx.getMv().visitLabel(returnTrue); + ctx.getMv().visitInsn(Opcodes.ICONST_1); + ctx.getMv().visitLabel(end); + } else if (op == Operator.OR) { + Label end = new Label(); + Label returnTrue = new Label(); + Label returnFalse = new Label(); + left.codeGen(ctx); + ctx.getMv().visitJumpInsn(Opcodes.IFNE, returnTrue); + right.codeGen(ctx); + ctx.getMv().visitJumpInsn(Opcodes.IFNE, returnTrue); + ctx.getMv().visitJumpInsn(Opcodes.GOTO, returnFalse); + + ctx.getMv().visitLabel(returnFalse); + ctx.getMv().visitInsn(Opcodes.ICONST_0); + ctx.getMv().visitJumpInsn(Opcodes.GOTO, end); + ctx.getMv().visitLabel(returnTrue); + ctx.getMv().visitInsn(Opcodes.ICONST_1); + ctx.getMv().visitLabel(end); + } else if (op == Operator.EQ) { + Label end = new Label(); + Label returnTrue = new Label(); + Label returnFalse = new Label(); + left.codeGen(ctx); + right.codeGen(ctx); + ctx.getMv().visitJumpInsn(Opcodes.IF_ICMPNE, returnFalse); + ctx.getMv().visitJumpInsn(Opcodes.GOTO, returnTrue); + + ctx.getMv().visitLabel(returnFalse); + ctx.getMv().visitInsn(Opcodes.ICONST_0); + ctx.getMv().visitJumpInsn(Opcodes.GOTO, end); + ctx.getMv().visitLabel(returnTrue); + ctx.getMv().visitInsn(Opcodes.ICONST_1); + ctx.getMv().visitLabel(end); + } else { + left.codeGen(ctx); + right.codeGen(ctx); + if (op == Operator.ADD) { + ctx.getMv().visitInsn(Opcodes.IADD); + } else if (op == Operator.SUB) { + ctx.getMv().visitInsn(Opcodes.ISUB); + } else if (op == Operator.MUL) { + ctx.getMv().visitInsn(Opcodes.IMUL); + } } //TODO: implement other operators ctx.popStack(); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java index 7da3e43..d354b7b 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java @@ -51,6 +51,7 @@ public class TypedReturn implements TypedStatement { if (ret == null) { ctx.getMv().visitInsn(Opcodes.RETURN); } else { + System.out.println("return: " + ret); ret.codeGen(ctx); if (ret.getType().getKind() != Type.Kind.REFERENCE) { ctx.getMv().visitInsn(Opcodes.IRETURN); diff --git a/src/main/resources/JavaTestfiles/ClassCanBeTyped.java b/src/main/resources/JavaTestfiles/ClassCanBeTyped.java index b5d0b60..d02fcc5 100644 --- a/src/main/resources/JavaTestfiles/ClassCanBeTyped.java +++ b/src/main/resources/JavaTestfiles/ClassCanBeTyped.java @@ -3,9 +3,9 @@ public class ClassCanBeTyped { public int x; public boolean y; - public ClassCanBeTyped(int x, boolean y) { - this.c.x = 2 * x; - this.y = !y; + + public boolean test(boolean b, boolean c) { + return b == c; } }