diff --git a/src/de/dhbwstuttgart/bytecode/AStatement.java b/src/de/dhbwstuttgart/bytecode/AStatement.java new file mode 100644 index 00000000..5c28bf43 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/AStatement.java @@ -0,0 +1,30 @@ +package de.dhbwstuttgart.bytecode; + +import org.objectweb.asm.Label; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; + +import de.dhbwstuttgart.syntaxtree.statement.BinaryExpr; +import de.dhbwstuttgart.syntaxtree.statement.Expression; + +public abstract class AStatement implements IStatement { + protected Expression expr; + + public AStatement(Expression expr) { + this.expr = expr; + } + + @Override + public boolean isExprBinary() { + return (expr instanceof BinaryExpr); + } + + @Override + public void genBCForRelOp(MethodVisitor mv,Label branchLabel, Label endLabel, BytecodeGenMethod bytecodeGenMethod) { + mv.visitInsn(Opcodes.ICONST_1); + mv.visitJumpInsn(Opcodes.GOTO, endLabel); + mv.visitLabel(branchLabel); + mv.visitInsn(Opcodes.ICONST_0); + mv.visitLabel(endLabel); + } +} diff --git a/src/de/dhbwstuttgart/bytecode/ArgumentExpr.java b/src/de/dhbwstuttgart/bytecode/ArgumentExpr.java new file mode 100644 index 00000000..b396e8d1 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/ArgumentExpr.java @@ -0,0 +1,11 @@ +package de.dhbwstuttgart.bytecode; + +import de.dhbwstuttgart.syntaxtree.statement.Expression; + +public class ArgumentExpr extends AStatement { + + public ArgumentExpr(Expression expr) { + super(expr); + } + +} diff --git a/src/de/dhbwstuttgart/bytecode/AssignStmt.java b/src/de/dhbwstuttgart/bytecode/AssignStmt.java new file mode 100644 index 00000000..366c450a --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/AssignStmt.java @@ -0,0 +1,11 @@ +package de.dhbwstuttgart.bytecode; + +import de.dhbwstuttgart.syntaxtree.statement.Expression; + +public class AssignStmt extends AStatement { + + public AssignStmt(Expression rightSide) { + super(rightSide); + } + +} diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index c00c152a..8b928a31 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -55,6 +55,8 @@ public class BytecodeGenMethod implements StatementVisitor { HashMap genericsAndBoundsMethod; private HashMap genericsAndBounds; private boolean isBinaryExp = false; + + private IStatement statement; // for tests ** private String fieldName; @@ -148,6 +150,7 @@ public class BytecodeGenMethod implements StatementVisitor { @Override public void visit(Assign assign) { + statement = new AssignStmt(assign.rightSide); isAssignStmt = true; // if the right side is a lambda => the left side must be a functional interface @@ -157,8 +160,7 @@ public class BytecodeGenMethod implements StatementVisitor { isRightSideALambda = false; } - if (assign.rightSide instanceof BinaryExpr) - isBinaryExp = true; + isBinaryExp = statement.isExprBinary(); if (assign.lefSide instanceof AssignToField) { @@ -198,7 +200,7 @@ public class BytecodeGenMethod implements StatementVisitor { Label endLabel = new Label(); // this case for while loops - if(!isAssignStmt) + if(statement instanceof LoopStmt) mv.visitLabel(endLabel); binary.lexpr.accept(this); @@ -341,18 +343,9 @@ public class BytecodeGenMethod implements StatementVisitor { default: break; } - if(isAssignStmt) { - mv.visitInsn(Opcodes.ICONST_1); - mv.visitJumpInsn(Opcodes.GOTO, endLabel); - mv.visitLabel(branchLabel); - mv.visitInsn(Opcodes.ICONST_0); - mv.visitLabel(endLabel); - } else { - loopBlock.accept(this); - mv.visitJumpInsn(Opcodes.GOTO, endLabel); - - mv.visitLabel(branchLabel); - } + + statement.genBCForRelOp(mv, branchLabel, endLabel,this); + break; } } @@ -375,18 +368,7 @@ public class BytecodeGenMethod implements StatementVisitor { default: break; } - if(isAssignStmt) { - mv.visitInsn(Opcodes.ICONST_1); - mv.visitJumpInsn(Opcodes.GOTO, endLabel); - mv.visitLabel(branchLabel); - mv.visitInsn(Opcodes.ICONST_0); - mv.visitLabel(endLabel); - } else { - loopBlock.accept(this); - mv.visitJumpInsn(Opcodes.GOTO, endLabel); - - mv.visitLabel(branchLabel); - } + statement.genBCForRelOp(mv, branchLabel, endLabel, this); } @@ -644,6 +626,7 @@ public class BytecodeGenMethod implements StatementVisitor { @Override public void visit(MethodCall methodCall) { + methodCall.receiver.accept(this); methodCall.arglist.accept(this); @@ -667,7 +650,7 @@ public class BytecodeGenMethod implements StatementVisitor { @Override public void visit(NewClass methodCall) { - + mv.visitTypeInsn(Opcodes.NEW, methodCall.name.replace(".", "/")); mv.visitInsn(Opcodes.DUP); // creates Descriptor @@ -776,8 +759,10 @@ public class BytecodeGenMethod implements StatementVisitor { @Override public void visit(Return aReturn) { - if(aReturn.retexpr instanceof BinaryExpr) - isBinaryExp = true; + + statement = new ReturnStmt(aReturn.retexpr); + + isBinaryExp = statement.isExprBinary(); aReturn.retexpr.accept(this); @@ -818,10 +803,12 @@ public class BytecodeGenMethod implements StatementVisitor { @Override public void visit(WhileStmt whileStmt) { + + statement = new LoopStmt(whileStmt.expr, whileStmt.loopBlock); this.loopBlock = whileStmt.loopBlock; - if(whileStmt.expr instanceof BinaryExpr) - isBinaryExp = true; + + isBinaryExp = statement.isExprBinary(); whileStmt.expr.accept(this); @@ -1030,6 +1017,7 @@ public class BytecodeGenMethod implements StatementVisitor { @Override public void visit(ArgumentList argumentList) { for (Expression al : argumentList.getArguments()) { + statement = new ArgumentExpr(al); al.accept(this); } } diff --git a/src/de/dhbwstuttgart/bytecode/IStatement.java b/src/de/dhbwstuttgart/bytecode/IStatement.java new file mode 100644 index 00000000..f8d61097 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/IStatement.java @@ -0,0 +1,9 @@ +package de.dhbwstuttgart.bytecode; + +import org.objectweb.asm.Label; +import org.objectweb.asm.MethodVisitor; + +public interface IStatement { + public boolean isExprBinary(); + public void genBCForRelOp(MethodVisitor mv, Label branchLabel, Label endLabel, BytecodeGenMethod bytecodeGenMethod); +} diff --git a/src/de/dhbwstuttgart/bytecode/LoopStmt.java b/src/de/dhbwstuttgart/bytecode/LoopStmt.java new file mode 100644 index 00000000..109efd34 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/LoopStmt.java @@ -0,0 +1,25 @@ +package de.dhbwstuttgart.bytecode; + +import org.objectweb.asm.Label; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; + +import de.dhbwstuttgart.syntaxtree.statement.Expression; +import de.dhbwstuttgart.syntaxtree.statement.Statement; + +public class LoopStmt extends AStatement { + + private Statement loopBlock; + + public LoopStmt(Expression expr, Statement loopBlock) { + super(expr); + this.loopBlock = loopBlock; + } + + @Override + public void genBCForRelOp(MethodVisitor mv,Label branchLabel, Label endLabel, BytecodeGenMethod bytecodeGenMethod) { + this.loopBlock.accept(bytecodeGenMethod); + mv.visitJumpInsn(Opcodes.GOTO, endLabel); + mv.visitLabel(branchLabel); + } +} diff --git a/src/de/dhbwstuttgart/bytecode/ReturnStmt.java b/src/de/dhbwstuttgart/bytecode/ReturnStmt.java new file mode 100644 index 00000000..bd747720 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/ReturnStmt.java @@ -0,0 +1,14 @@ +package de.dhbwstuttgart.bytecode; + +import org.objectweb.asm.MethodVisitor; + +import de.dhbwstuttgart.syntaxtree.statement.BinaryExpr; +import de.dhbwstuttgart.syntaxtree.statement.Expression; + +public class ReturnStmt extends AStatement { + + public ReturnStmt(Expression retexpr) { + super(retexpr); + } + +}