From d6fb9ff2c7b5bd6a947e4a8e6462f674dd7ada13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Thu, 15 Oct 2015 17:46:13 +0200 Subject: [PATCH] ConditionTest angelegt ForTest gefixed --- src/de/dhbwstuttgart/syntaxtree/Method.java | 3 +- .../syntaxtree/statement/Binary.java | 23 +++++++- .../syntaxtree/statement/ForStmt.java | 54 ++++++++++++++++--- .../syntaxtree/statement/IfStmt.java | 2 - .../syntaxtree/statement/LocalOrFieldVar.java | 9 +++- test/bytecode/Condition.jav | 7 +++ test/bytecode/ConditionTest.java | 34 ++++++++++++ test/bytecode/ForTest.jav | 2 +- test/bytecode/IfStatement.jav | 2 +- test/bytecode/SingleClassTester.java | 2 +- 10 files changed, 121 insertions(+), 17 deletions(-) create mode 100644 test/bytecode/Condition.jav create mode 100644 test/bytecode/ConditionTest.java diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index ba8cba98..3d4a60ce 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -541,8 +541,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable ret.add(this.block.TYPEStmt(localAss)); // eine Verknüpfung mit der Type Assumption aus dem Assumption Set // und dem ermittelten Typ der Methode: - ret.add(new SingleConstraint(this.block.getType().TYPE(localAss, this), - this.returntype.TYPE(localAss, this))); + ret.add(new SingleConstraint(this.block.getType().TYPE(localAss, this), this.returntype.TYPE(localAss, this))); return ret; } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java b/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java index 60b2657b..73467924 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java @@ -6,8 +6,13 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.Hashtable; +import org.apache.commons.bcel6.generic.BranchInstruction; import org.apache.commons.bcel6.generic.ClassGen; +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 de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.bytecode.ClassGenerator; @@ -228,8 +233,22 @@ public JavaCodeResult printJavaCode(ResultSet resultSet) { if(this.getReturnType().getName().equals(new JavaClassName("String"))){ throw new TypeinferenceException("Zeichenketten zusammenfügen ist noch nicht unterstützt",this); } - //TODO: Je nachdem welches der Rückgabetyp ist, müssen die linke und rechte Seite unboxt und addiert werden. - return null; + + 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; } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java index 6f5ab521..dc8526ae 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java @@ -4,8 +4,13 @@ import java.util.Enumeration; import java.util.Hashtable; import java.util.Iterator; +import org.apache.commons.bcel6.generic.BranchInstruction; import org.apache.commons.bcel6.generic.ClassGen; +import org.apache.commons.bcel6.generic.GOTO; +import org.apache.commons.bcel6.generic.IF_ICMPGE; +import org.apache.commons.bcel6.generic.InstructionHandle; import org.apache.commons.bcel6.generic.InstructionList; +import org.apache.commons.bcel6.generic.NOP; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.bytecode.ClassGenerator; @@ -22,10 +27,13 @@ import de.dhbwstuttgart.syntaxtree.operator.RelOp; import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.Type; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.syntaxtree.type.Void; import de.dhbwstuttgart.typeinference.ConstraintsSet; import de.dhbwstuttgart.typeinference.JavaCodeResult; import de.dhbwstuttgart.typeinference.Pair; import de.dhbwstuttgart.typeinference.ResultSet; +import de.dhbwstuttgart.typeinference.SingleConstraint; import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions; import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException; import de.dhbwstuttgart.typeinference.unify.Unify; @@ -91,8 +99,13 @@ public class ForStmt extends Statement @Override public ConstraintsSet TYPEStmt(TypeAssumptions assumptions) { - // TODO Auto-generated method stub - return null; + //TODO: TYPEStmt + ConstraintsSet ret = new ConstraintsSet(); + this.setType(TypePlaceholder.fresh(this)); + + ret.add(body_Loop_block.TYPEStmt(assumptions)); + + return ret; } public int getTypeLineNumber() { @@ -100,9 +113,9 @@ public class ForStmt extends Statement } @Override -public JavaCodeResult printJavaCode(ResultSet resultSet) { - // TODO Auto-generated method stub - throw new NotImplementedException(); + public JavaCodeResult printJavaCode(ResultSet resultSet) { + // TODO printJavaCode + return new JavaCodeResult(); } @Override @@ -121,7 +134,34 @@ public JavaCodeResult printJavaCode(ResultSet resultSet) { @Override public InstructionList genByteCode(ClassGenerator _cg) { - // TODO Bytecode - throw new NotImplementedException(); + /* + 0: iconst_0 + 1: istore_1 + 2: iload_1 + 3: bipush 10 + 5: if_icmpge 14 + 8: iinc 1, 1 + 11: goto 2 + 14: return + */ + + InstructionList il = new InstructionList(); + + il.append(head_Initializer.genByteCode(_cg)); + + InstructionHandle ih = il.append(head_Condition.genByteCode(_cg)); + + BranchInstruction branch = new IF_ICMPGE(null); + il.append(branch); + + il.append(body_Loop_block.genByteCode(_cg)); + + il.append(head_Loop_expr.genByteCode(_cg)); + + il.append(new GOTO(ih)); + + branch.setTarget(il.append(new NOP())); + + return il; } } \ No newline at end of file diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java index a8eac123..91cb548e 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java @@ -179,8 +179,6 @@ public class IfStmt extends Statement @Override public InstructionList genByteCode(ClassGenerator _cg) { - InstructionFactory _factory = new InstructionFactory(_cg, _cg.getConstantPool()); - InstructionList il = new InstructionList(); IfInstruction ifInstruction = new IFEQ(null); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.java b/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.java index 8a39cbd0..50c2bdf8 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.java @@ -13,6 +13,7 @@ import org.apache.commons.bcel6.generic.ObjectType; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.bytecode.ClassGenerator; import de.dhbwstuttgart.logger.Logger; +import de.dhbwstuttgart.logger.Section; import de.dhbwstuttgart.myexception.CTypeReconstructionException; import de.dhbwstuttgart.myexception.JVMCodeException; import de.dhbwstuttgart.myexception.SCExcept; @@ -152,7 +153,13 @@ public class LocalOrFieldVar extends Expr if(this.isFieldAccess){ il.append(cg.getInstructionFactory().createFieldAccess(this.getParentClass().getName().toString(), this.get_Name(), this.getType().getBytecodeType(cg), Constants.GETFIELD)); } - il.append(cg.getInstructionFactory().createLoad(this.getType().getBytecodeType(cg), this.get_Name())); + + Type type = this.getType(); + org.apache.commons.bcel6.generic.Type byteCodeType = type.getBytecodeType(cg); + + String name = this.get_Name(); + + il.append(cg.getInstructionFactory().createLoad(byteCodeType, name)); return il; } diff --git a/test/bytecode/Condition.jav b/test/bytecode/Condition.jav new file mode 100644 index 00000000..4f68e8ab --- /dev/null +++ b/test/bytecode/Condition.jav @@ -0,0 +1,7 @@ +class Condition{ + + Boolean method() { + return 0 == 1; + } + +} \ No newline at end of file diff --git a/test/bytecode/ConditionTest.java b/test/bytecode/ConditionTest.java new file mode 100644 index 00000000..851d6426 --- /dev/null +++ b/test/bytecode/ConditionTest.java @@ -0,0 +1,34 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; + +import junit.framework.TestCase; + +import org.junit.Test; + +import plugindevelopment.TypeInsertTester; +import de.dhbwstuttgart.core.MyCompiler; +import de.dhbwstuttgart.core.MyCompilerAPI; +import de.dhbwstuttgart.logger.LoggerConfiguration; +import de.dhbwstuttgart.logger.Section; +import de.dhbwstuttgart.parser.JavaParser.yyException; +import de.dhbwstuttgart.typeinference.ByteCodeResult; +import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; +import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertSet; + +public class ConditionTest { + + public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/"; + public final static String testFile = "Condition.jav"; + public final static String outputFile = "Condition.class"; + + @Test + public void test() { + SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile); + } + +} diff --git a/test/bytecode/ForTest.jav b/test/bytecode/ForTest.jav index 7343a1b8..649b71b0 100644 --- a/test/bytecode/ForTest.jav +++ b/test/bytecode/ForTest.jav @@ -1,6 +1,6 @@ class IfElse{ method(){ - while(true){ + for( i = 0; i < 10 ; i = i++){ } } diff --git a/test/bytecode/IfStatement.jav b/test/bytecode/IfStatement.jav index 476c9bc0..d9fdb16c 100644 --- a/test/bytecode/IfStatement.jav +++ b/test/bytecode/IfStatement.jav @@ -1,7 +1,7 @@ class IfStatement{ method(flag){ if(flag){ - + } } } \ No newline at end of file diff --git a/test/bytecode/SingleClassTester.java b/test/bytecode/SingleClassTester.java index d769fe98..58dbae58 100644 --- a/test/bytecode/SingleClassTester.java +++ b/test/bytecode/SingleClassTester.java @@ -20,7 +20,7 @@ public class SingleClassTester { try { compiler.parse(new File(inputFile)); Menge bytecode = compiler.generateBytecode(compiler.typeReconstruction().firstElement()); - System.out.println(bytecode); + //System.out.println(bytecode); bytecode.firstElement().getByteCode().getJavaClass().dump(new File(outputFile)); } catch (IOException | yyException e) { e.printStackTrace();