From f99d4dae5a3bf074794d80308f5145223747193a Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Mon, 6 Aug 2018 16:14:09 +0200 Subject: [PATCH] DUP Befehl fuer UnaryExpr --- .../bytecode/BytecodeGenMethod.java | 37 ++++++++++--------- test/bytecode/BinaryTest.java | 7 ++++ test/bytecode/MergeTest.java | 6 +-- test/bytecode/javFiles/BinaryInMeth.jav | 4 ++ 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index 1719d64d..643e98d5 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -69,11 +69,7 @@ public class BytecodeGenMethod implements StatementVisitor { private SourceFile sf; private IStatement statement = null; - private boolean returnParentOfUnary = false; - // speichert, ob der Argument RefType ist. - private List argList = new LinkedList<>(); - -// private int numMethodCalls = 0; + private boolean needDUP = false; // for tests ** private String fieldName; @@ -163,7 +159,7 @@ public class BytecodeGenMethod implements StatementVisitor { // wird die lokale Var geladen. Sonst wird zuerst die lokale Var geladen. System.out.println(localVar.name); mv.visitVarInsn(Opcodes.ALOAD, paramsAndLocals.get(localVar.name)); - + if (isBinaryExp) { doUnboxing(getResolvedType(localVar.getType())); } @@ -199,6 +195,7 @@ public class BytecodeGenMethod implements StatementVisitor { doBoxing(binaryType); isBinaryExp = false; } + System.out.println("ASSIGN TYPE R: " + getResolvedType(assign.rightSide.getType())); String typeOfRightSide = getResolvedType(assign.rightSide.getType()); if(typeOfRightSide.contains("<")) { @@ -229,12 +226,18 @@ public class BytecodeGenMethod implements StatementVisitor { // this case for while loops if (statement instanceof LoopStmt) mv.visitLabel(endLabel); - + + if(binary.lexpr instanceof UnaryExpr) + needDUP = true; + binary.lexpr.accept(this); if (!lexpType.equals(rexpType) && !lexpType.equals(largerType)) doCast(lexpType, largerType); - + + if(binary.rexpr instanceof UnaryExpr) + needDUP = true; + binary.rexpr.accept(this); if (!lexpType.equals(rexpType) && !rexpType.equals(largerType)) @@ -713,10 +716,6 @@ public class BytecodeGenMethod implements StatementVisitor { String mDesc = ""; List argListMethCall = new LinkedList<>(); if(methodRefl == null) { - // Alle Argumente sind nicht Primitiv - for(Expression arg : methodCall.arglist.getArguments()) { - argList.add(true); - } MethodFromMethodCall method = new MethodFromMethodCall(methodCall.arglist, methodCall.getType(), receiverName, genericsAndBoundsMethod, genericsAndBounds); mDesc = method.accept(new DescriptorToString(resultSet)); @@ -832,7 +831,6 @@ public class BytecodeGenMethod implements StatementVisitor { @Override public void visit(UnaryExpr unaryExpr) { - unaryExpr.expr.accept(this); Operation op = unaryExpr.operation; @@ -868,13 +866,14 @@ public class BytecodeGenMethod implements StatementVisitor { // Für Byte und Short muss noch einen Cast geben i2b, i2s // das wird später gemacht, da bytecode für cast noch nicht erzeugt wird - if (isIncOrDec && (unaryExpr.expr instanceof LocalVar) && !returnParentOfUnary) { + if (isIncOrDec && (unaryExpr.expr instanceof LocalVar)) { + if(needDUP) { + mv.visitInsn(Opcodes.DUP); + needDUP = false; + } LocalVar local = (LocalVar) unaryExpr.expr; mv.visitVarInsn(Opcodes.ASTORE, paramsAndLocals.get(local.name)); } - - if(returnParentOfUnary) - returnParentOfUnary = false; } private void doVisitNegIns(String typeOfUnary) { @@ -922,7 +921,7 @@ public class BytecodeGenMethod implements StatementVisitor { isBinaryExp = statement.isExprBinary(); if(aReturn.retexpr instanceof UnaryExpr) - returnParentOfUnary = true; + needDUP = true; aReturn.retexpr.accept(this); @@ -1184,6 +1183,8 @@ public class BytecodeGenMethod implements StatementVisitor { for (Expression al : argumentList.getArguments()) { statement = new ArgumentExpr(al); isBinaryExp = statement.isExprBinary(); + if(al instanceof UnaryExpr) + needDUP = true; al.accept(this); //TODO: teste, ob man das für unary braucht if (isBinaryExp) { diff --git a/test/bytecode/BinaryTest.java b/test/bytecode/BinaryTest.java index 543786ad..84f102b7 100644 --- a/test/bytecode/BinaryTest.java +++ b/test/bytecode/BinaryTest.java @@ -39,5 +39,12 @@ public class BinaryTest { Integer res = (Integer) m2.invoke(instanceOfClass, 2,3); assertEquals(6, res); } + + @Test + public void testM3() throws Exception { + Method m3 = classToTest.getDeclaredMethod("m3", Integer.class); + Integer res = (Integer) m3.invoke(instanceOfClass, 2); + assertEquals(4, res); + } } diff --git a/test/bytecode/MergeTest.java b/test/bytecode/MergeTest.java index 6a0044ce..9f3943ca 100644 --- a/test/bytecode/MergeTest.java +++ b/test/bytecode/MergeTest.java @@ -28,9 +28,9 @@ public class MergeTest { fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); compiler.generateBytecode(System.getProperty("user.dir")+"/testBytecode/generatedBC/"); - pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; - loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); - classToTest = loader.loadClass("Merge"); +// pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; +// loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); +// classToTest = loader.loadClass("Merge"); //instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); //Method m = classToTest.getDeclaredMethod("m"); diff --git a/test/bytecode/javFiles/BinaryInMeth.jav b/test/bytecode/javFiles/BinaryInMeth.jav index c77d7767..3b5fa77b 100644 --- a/test/bytecode/javFiles/BinaryInMeth.jav +++ b/test/bytecode/javFiles/BinaryInMeth.jav @@ -10,4 +10,8 @@ public class BinaryInMeth { m2(a,b){ return m(a+b); } + + m3(a) { + return m(++a); + } } \ No newline at end of file