diff --git a/src/de/dhbwstuttgart/bytecode/DHBWInstructionFactory.java b/src/de/dhbwstuttgart/bytecode/DHBWInstructionFactory.java index c9a60d89..5cb13991 100644 --- a/src/de/dhbwstuttgart/bytecode/DHBWInstructionFactory.java +++ b/src/de/dhbwstuttgart/bytecode/DHBWInstructionFactory.java @@ -145,7 +145,7 @@ public class DHBWInstructionFactory extends InstructionFactory{ public Integer getStoreIndex(String variableName) { if(!storeIndexes.containsKey(variableName)){ - storeIndexes.put(variableName, storeIndexes.size()); + storeIndexes.put(variableName, storeIndexes.size()+1); } return storeIndexes.get(variableName); diff --git a/src/de/dhbwstuttgart/syntaxtree/operator/EqualOp.java b/src/de/dhbwstuttgart/syntaxtree/operator/EqualOp.java index 184a8501..89285c6b 100755 --- a/src/de/dhbwstuttgart/syntaxtree/operator/EqualOp.java +++ b/src/de/dhbwstuttgart/syntaxtree/operator/EqualOp.java @@ -3,16 +3,30 @@ package de.dhbwstuttgart.syntaxtree.operator; // ino.end // ino.module.EqualOp.8597.import import java.util.Iterator; -import de.dhbwstuttgart.typeinference.Menge; +import org.apache.commons.bcel6.Constants; +import org.apache.commons.bcel6.generic.BranchInstruction; +import org.apache.commons.bcel6.generic.GOTO; +import org.apache.commons.bcel6.generic.IF_ICMPNE; +import org.apache.commons.bcel6.generic.InstructionConstants; +import org.apache.commons.bcel6.generic.InstructionList; +import org.apache.commons.bcel6.generic.NOP; +import org.apache.commons.bcel6.generic.ObjectType; + +import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.bytecode.ClassGenerator; +import de.dhbwstuttgart.bytecode.DHBWInstructionFactory; import de.dhbwstuttgart.myexception.CTypeReconstructionException; import de.dhbwstuttgart.myexception.JVMCodeException; +import de.dhbwstuttgart.parser.JavaClassName; import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; import de.dhbwstuttgart.syntaxtree.statement.Binary; import de.dhbwstuttgart.syntaxtree.statement.Expr; import de.dhbwstuttgart.syntaxtree.statement.Null; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.typeinference.Pair; +import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; +import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException; import de.dhbwstuttgart.typeinference.unify.Unify; @@ -31,6 +45,39 @@ public class EqualOp extends RelOp super(offset, variableLength); } // ino.end + + @Override + public InstructionList genByteCode(ClassGenerator _cg, TypeinferenceResultSet rs, Binary operator) { + DHBWInstructionFactory _factory = _cg.getInstructionFactory(); + + InstructionList linkeSeite = operator.expr1.genByteCode(_cg, rs); + linkeSeite.append(_factory.createInvoke("java.lang.Integer", "intValue", org.apache.commons.bcel6.generic.Type.INT, new org.apache.commons.bcel6.generic.Type[] {}, Constants.INVOKEVIRTUAL)); + + InstructionList rechteSeite = operator.expr2.genByteCode(_cg, rs); + rechteSeite.append(_factory.createInvoke("java.lang.Integer", "intValue", org.apache.commons.bcel6.generic.Type.INT, new org.apache.commons.bcel6.generic.Type[] {}, Constants.INVOKEVIRTUAL)); + + + if(operator.getReturnType().getName().equals(new JavaClassName("String"))){ + throw new TypeinferenceException("Zeichenketten zusammenfügen ist noch nicht unterstützt",this); + } + + linkeSeite.append(rechteSeite); + + //TODO: bytecode (Bis jetzt wird jeder Condition als EQUALS behandelt) + //TODO: bytecode autoboxing + + BranchInstruction if_icmpneInstruction = new IF_ICMPNE(null); + linkeSeite.append(if_icmpneInstruction); + linkeSeite.append(InstructionConstants.ICONST_1); + + BranchInstruction gotoInstruction = new GOTO(null); + + linkeSeite.append(gotoInstruction); + if_icmpneInstruction.setTarget(linkeSeite.append(InstructionConstants.ICONST_0)); + gotoInstruction.setTarget(linkeSeite.append(_factory.createInvoke("java.lang.Boolean", "valueOf", new ObjectType("java.lang.Boolean"), new org.apache.commons.bcel6.generic.Type[] {org.apache.commons.bcel6.generic.Type.BOOLEAN}, Constants.INVOKESTATIC))); + + return linkeSeite; + } } // ino.end diff --git a/src/de/dhbwstuttgart/syntaxtree/operator/Operator.java b/src/de/dhbwstuttgart/syntaxtree/operator/Operator.java index 8639bb8f..4a2573ba 100755 --- a/src/de/dhbwstuttgart/syntaxtree/operator/Operator.java +++ b/src/de/dhbwstuttgart/syntaxtree/operator/Operator.java @@ -5,8 +5,11 @@ package de.dhbwstuttgart.syntaxtree.operator; import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; -import de.dhbwstuttgart.typeinference.Menge; +import org.apache.commons.bcel6.generic.InstructionList; + +import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.bytecode.ClassGenerator; import de.dhbwstuttgart.core.IItemWithOffset; import de.dhbwstuttgart.myexception.CTypeReconstructionException; import de.dhbwstuttgart.myexception.JVMCodeException; @@ -20,6 +23,7 @@ import de.dhbwstuttgart.typeinference.ConstraintsSet; import de.dhbwstuttgart.typeinference.OderConstraint; import de.dhbwstuttgart.typeinference.Pair; import de.dhbwstuttgart.typeinference.SingleConstraint; +import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; import de.dhbwstuttgart.typeinference.UndConstraint; import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions; import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException; @@ -85,6 +89,8 @@ public abstract class Operator extends SyntaxTreeNode * @return */ public abstract HashMap getReturnTypes(TypeAssumptions ass); + + public abstract InstructionList genByteCode(ClassGenerator _cg, TypeinferenceResultSet rs, Binary operator); @Override diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java b/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java index 035d4c0a..f0fd2d9d 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java @@ -229,27 +229,7 @@ public JavaCodeResult printJavaCode(ResultSet resultSet) { @Override public InstructionList genByteCode(ClassGenerator _cg, TypeinferenceResultSet rs) { - InstructionList linkeSeite = this.expr1.genByteCode(_cg, rs); - InstructionList rechteSeite = this.expr2.genByteCode(_cg, rs); - if(this.getReturnType().getName().equals(new JavaClassName("String"))){ - throw new TypeinferenceException("Zeichenketten zusammenfügen ist noch nicht unterstützt",this); - } - - linkeSeite.append(rechteSeite); - - //TODO: bytecode (Bis jetzt wird jeder Condition als EQUALS behandelt) - //TODO: bytecode autoboxing - - BranchInstruction if_icmpneInstruction = new IF_ICMPNE(null); - linkeSeite.append(if_icmpneInstruction); - linkeSeite.append(InstructionConstants.ICONST_1); - - BranchInstruction gotoInstruction = new GOTO(null); - - linkeSeite.append(gotoInstruction); - if_icmpneInstruction.setTarget(linkeSeite.append(InstructionConstants.ICONST_0)); - gotoInstruction.setTarget(linkeSeite.append(new NOP())); - return linkeSeite; + return op.genByteCode(_cg, rs, this); } } diff --git a/test/bytecode/operators/AddOperator.jav b/test/bytecode/operators/AddOperator.jav new file mode 100644 index 00000000..d7c79a23 --- /dev/null +++ b/test/bytecode/operators/AddOperator.jav @@ -0,0 +1,9 @@ +class AddOperator{ + Integer method(Integer x){ + return x + x; + } + + Double method(Double x){ + return x + x; + } +} \ No newline at end of file diff --git a/test/bytecode/operators/AddOperatorTest.java b/test/bytecode/operators/AddOperatorTest.java new file mode 100644 index 00000000..53fcc6be --- /dev/null +++ b/test/bytecode/operators/AddOperatorTest.java @@ -0,0 +1,32 @@ +package bytecode.operators; + +import static org.junit.Assert.*; + +import java.io.File; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Vector; + +import org.junit.Ignore; +import org.junit.Test; + +import bytecode.SourceFileBytecodeTest; + +public class AddOperatorTest extends SourceFileBytecodeTest{ + @Override + protected void init() { + testName = "AddOperator"; + rootDirectory = System.getProperty("user.dir")+"/test/bytecode/operators/"; + } + + @Test + public void testConstruct() throws Exception{ + ClassLoader classLoader = getClassLoader(); + + Class cls = classLoader.loadClass(testName); + + Object obj = cls.newInstance(); + assertTrue(true); + } +} diff --git a/test/bytecode/operators/EqualOperator.jav b/test/bytecode/operators/EqualOperator.jav new file mode 100644 index 00000000..a51b512a --- /dev/null +++ b/test/bytecode/operators/EqualOperator.jav @@ -0,0 +1,5 @@ +class EqualOperator{ + Boolean method(Integer x, Integer y){ + return x == y; + } +} \ No newline at end of file diff --git a/test/bytecode/operators/EqualOperatorTest.java b/test/bytecode/operators/EqualOperatorTest.java new file mode 100644 index 00000000..b605799f --- /dev/null +++ b/test/bytecode/operators/EqualOperatorTest.java @@ -0,0 +1,32 @@ +package bytecode.operators; + +import static org.junit.Assert.*; + +import java.io.File; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Vector; + +import org.junit.Ignore; +import org.junit.Test; + +import bytecode.SourceFileBytecodeTest; + +public class EqualOperatorTest extends SourceFileBytecodeTest{ + @Override + protected void init() { + testName = "EqualOperator"; + rootDirectory = System.getProperty("user.dir")+"/test/bytecode/operators/"; + } + + @Test + public void testConstruct() throws Exception{ + ClassLoader classLoader = getClassLoader(); + + Class cls = classLoader.loadClass(testName); + + Object obj = cls.newInstance(); + assertTrue(true); + } +}