From e5fc8c8288f20281c6bef4eb37a13c84bfc5d297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Sat, 30 Apr 2016 11:14:24 +0200 Subject: [PATCH] =?UTF-8?q?-=20Div=20und=20MulOperatoren=20f=C3=BCr=20alle?= =?UTF-8?q?=20numerischen=20Datentypen=20implementiert=20(Nur=20wenn=20bei?= =?UTF-8?q?de=20Operanten=20den=20gleichen=20Typ=20haben)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../syntaxtree/operator/AddOp.java | 1 - .../syntaxtree/operator/DivideOp.java | 23 +++++++++++++--- .../syntaxtree/operator/MulOp.java | 27 ++++++++++++++++--- .../syntaxtree/operator/PlusOp.java | 1 - .../syntaxtree/operator/TimesOp.java | 19 ++++++++++--- test/bytecode/operators/DivOperator.jav | 2 +- test/bytecode/operators/MulOperator.jav | 2 +- 7 files changed, 62 insertions(+), 13 deletions(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/operator/AddOp.java b/src/de/dhbwstuttgart/syntaxtree/operator/AddOp.java index 8a8324c6..c2811dec 100755 --- a/src/de/dhbwstuttgart/syntaxtree/operator/AddOp.java +++ b/src/de/dhbwstuttgart/syntaxtree/operator/AddOp.java @@ -142,7 +142,6 @@ public abstract class AddOp extends Operator }else{ return type.get_Name(); } - } abstract ArithmeticInstruction getOperator(String returnType); diff --git a/src/de/dhbwstuttgart/syntaxtree/operator/DivideOp.java b/src/de/dhbwstuttgart/syntaxtree/operator/DivideOp.java index 850d2ba0..e776617c 100755 --- a/src/de/dhbwstuttgart/syntaxtree/operator/DivideOp.java +++ b/src/de/dhbwstuttgart/syntaxtree/operator/DivideOp.java @@ -3,9 +3,17 @@ package de.dhbwstuttgart.syntaxtree.operator; // ino.end // ino.module.DivideOp.8596.import import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException; import org.apache.commons.bcel6.generic.ArithmeticInstruction; +import org.apache.commons.bcel6.generic.DADD; +import org.apache.commons.bcel6.generic.DDIV; +import org.apache.commons.bcel6.generic.FADD; +import org.apache.commons.bcel6.generic.FDIV; +import org.apache.commons.bcel6.generic.IADD; import org.apache.commons.bcel6.generic.IDIV; +import org.apache.commons.bcel6.generic.LADD; +import org.apache.commons.bcel6.generic.LDIV; import de.dhbwstuttgart.myexception.JVMCodeException; import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; @@ -28,10 +36,19 @@ public class DivideOp extends MulOp super(offset,variableLength); } // ino.end - - @Override + ArithmeticInstruction getOperator(String returnType) { - return new IDIV(); + if(returnType.equals("java.lang.Integer")){ + return new IDIV(); + }else if(returnType.equals("java.lang.Double")){ + return new DDIV(); + }else if(returnType.equals("java.lang.Float")){ + return new FDIV(); + }else if(returnType.equals("java.lang.Long")){ + return new LDIV(); + }else{ + throw new NotImplementedException(); + } } } // ino.end diff --git a/src/de/dhbwstuttgart/syntaxtree/operator/MulOp.java b/src/de/dhbwstuttgart/syntaxtree/operator/MulOp.java index d0b33559..d84690d8 100755 --- a/src/de/dhbwstuttgart/syntaxtree/operator/MulOp.java +++ b/src/de/dhbwstuttgart/syntaxtree/operator/MulOp.java @@ -19,8 +19,11 @@ import de.dhbwstuttgart.bytecode.DHBWInstructionFactory; import de.dhbwstuttgart.myexception.CTypeReconstructionException; import de.dhbwstuttgart.syntaxtree.statement.Binary; import de.dhbwstuttgart.syntaxtree.statement.Expr; +import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; import de.dhbwstuttgart.syntaxtree.type.Type; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.Pair; import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions; @@ -75,7 +78,7 @@ public abstract class MulOp extends Operator */ DHBWInstructionFactory _factory = _cg.getInstructionFactory(); - String returnType = getReturnType(operator.get_Expr1(), operator.get_Expr2()); + String returnType = getReturnType(operator.get_Expr1(), operator.get_Expr2(), rs); InstructionList il = getInstructionListForOperand(_cg, rs, operator.get_Expr1(), returnType); @@ -87,8 +90,20 @@ public abstract class MulOp extends Operator return il; } - private String getReturnType(Expr expr1, Expr expr2) { - return "java.lang.Integer"; + private String getReturnType(Expr expr1, Expr expr2, TypeinferenceResultSet rs) { + Type type = expr1.getType(); + if(type instanceof TypePlaceholder){ + Type tphType = rs.getTypeOfPlaceholder((TypePlaceholder) expr1.getType()); + if(tphType instanceof ExtendsWildcardType){ + return ((ExtendsWildcardType) tphType).get_ExtendsType().get_Name(); + }else if(tphType instanceof SuperWildcardType){ + return ((SuperWildcardType) tphType).get_SuperType().get_Name(); + }else{ + return tphType.getName().toString(); + } + }else{ + return type.get_Name(); + } } abstract ArithmeticInstruction getOperator(String returnType); @@ -96,6 +111,12 @@ public abstract class MulOp extends Operator private InvokeInstruction convertValueToObject(DHBWInstructionFactory _factory, String returnType) { if(returnType.equals("java.lang.Integer")){ return _factory.createInvoke("java.lang.Integer", "valueOf", new ObjectType("java.lang.Integer"), new org.apache.commons.bcel6.generic.Type[] {org.apache.commons.bcel6.generic.Type.INT}, Constants.INVOKESTATIC); + }else if(returnType.equals("java.lang.Double")){ + return _factory.createInvoke("java.lang.Double", "valueOf", new ObjectType("java.lang.Double"), new org.apache.commons.bcel6.generic.Type[] {org.apache.commons.bcel6.generic.Type.DOUBLE}, Constants.INVOKESTATIC); + }else if(returnType.equals("java.lang.Float")){ + return _factory.createInvoke("java.lang.Float", "valueOf", new ObjectType("java.lang.Float"), new org.apache.commons.bcel6.generic.Type[] {org.apache.commons.bcel6.generic.Type.FLOAT}, Constants.INVOKESTATIC); + }else if(returnType.equals("java.lang.Long")){ + return _factory.createInvoke("java.lang.Long", "valueOf", new ObjectType("java.lang.Long"), new org.apache.commons.bcel6.generic.Type[] {org.apache.commons.bcel6.generic.Type.LONG}, Constants.INVOKESTATIC); }else{ throw new NotImplementedException(); } diff --git a/src/de/dhbwstuttgart/syntaxtree/operator/PlusOp.java b/src/de/dhbwstuttgart/syntaxtree/operator/PlusOp.java index 73acd1b9..53fbae1c 100755 --- a/src/de/dhbwstuttgart/syntaxtree/operator/PlusOp.java +++ b/src/de/dhbwstuttgart/syntaxtree/operator/PlusOp.java @@ -57,7 +57,6 @@ public class PlusOp extends AddOp }else{ throw new NotImplementedException(); } - } } // ino.end diff --git a/src/de/dhbwstuttgart/syntaxtree/operator/TimesOp.java b/src/de/dhbwstuttgart/syntaxtree/operator/TimesOp.java index c43ff29c..e54693cb 100755 --- a/src/de/dhbwstuttgart/syntaxtree/operator/TimesOp.java +++ b/src/de/dhbwstuttgart/syntaxtree/operator/TimesOp.java @@ -4,9 +4,13 @@ package de.dhbwstuttgart.syntaxtree.operator; // ino.module.TimesOp.8611.import import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException; import org.apache.commons.bcel6.generic.ArithmeticInstruction; +import org.apache.commons.bcel6.generic.DMUL; +import org.apache.commons.bcel6.generic.FMUL; import org.apache.commons.bcel6.generic.IMUL; +import org.apache.commons.bcel6.generic.LMUL; import de.dhbwstuttgart.myexception.JVMCodeException; import de.dhbwstuttgart.syntaxtree.statement.Binary; @@ -25,10 +29,19 @@ public class TimesOp extends MulOp super(offset,variableLength); } // ino.end - - @Override + ArithmeticInstruction getOperator(String returnType) { - return new IMUL(); + if(returnType.equals("java.lang.Integer")){ + return new IMUL(); + }else if(returnType.equals("java.lang.Double")){ + return new DMUL(); + }else if(returnType.equals("java.lang.Float")){ + return new FMUL(); + }else if(returnType.equals("java.lang.Long")){ + return new LMUL(); + }else{ + throw new NotImplementedException(); + } } } diff --git a/test/bytecode/operators/DivOperator.jav b/test/bytecode/operators/DivOperator.jav index 6ba3d4fe..d23c04ba 100644 --- a/test/bytecode/operators/DivOperator.jav +++ b/test/bytecode/operators/DivOperator.jav @@ -1,5 +1,5 @@ class DivOperator{ - Integer method(Integer x, Integer y){ + method(x, y){ return x / y; } } \ No newline at end of file diff --git a/test/bytecode/operators/MulOperator.jav b/test/bytecode/operators/MulOperator.jav index dd08e82e..8efd6349 100644 --- a/test/bytecode/operators/MulOperator.jav +++ b/test/bytecode/operators/MulOperator.jav @@ -1,5 +1,5 @@ class MulOperator{ - Integer method(Integer x, Integer y){ + method(x, y){ return x * y; } } \ No newline at end of file