ConditionTest angelegt

ForTest gefixed
This commit is contained in:
Enrico Schrödter 2015-10-15 17:46:13 +02:00
parent f23a341d72
commit d6fb9ff2c7
10 changed files with 121 additions and 17 deletions

View File

@ -541,8 +541,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
ret.add(this.block.TYPEStmt(localAss)); ret.add(this.block.TYPEStmt(localAss));
// eine Verknüpfung mit der Type Assumption aus dem Assumption Set // eine Verknüpfung mit der Type Assumption aus dem Assumption Set
// und dem ermittelten Typ der Methode: // und dem ermittelten Typ der Methode:
ret.add(new SingleConstraint(this.block.getType().TYPE(localAss, this), ret.add(new SingleConstraint(this.block.getType().TYPE(localAss, this), this.returntype.TYPE(localAss, this)));
this.returntype.TYPE(localAss, this)));
return ret; return ret;
} }

View File

@ -6,8 +6,13 @@ import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Hashtable; import java.util.Hashtable;
import org.apache.commons.bcel6.generic.BranchInstruction;
import org.apache.commons.bcel6.generic.ClassGen; 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.InstructionList;
import org.apache.commons.bcel6.generic.NOP;
import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.bytecode.ClassGenerator; import de.dhbwstuttgart.bytecode.ClassGenerator;
@ -228,8 +233,22 @@ public JavaCodeResult printJavaCode(ResultSet resultSet) {
if(this.getReturnType().getName().equals(new JavaClassName("String"))){ if(this.getReturnType().getName().equals(new JavaClassName("String"))){
throw new TypeinferenceException("Zeichenketten zusammenfügen ist noch nicht unterstützt",this); 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;
} }
} }

View File

@ -4,8 +4,13 @@ import java.util.Enumeration;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Iterator; import java.util.Iterator;
import org.apache.commons.bcel6.generic.BranchInstruction;
import org.apache.commons.bcel6.generic.ClassGen; 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.InstructionList;
import org.apache.commons.bcel6.generic.NOP;
import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.bytecode.ClassGenerator; 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.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.Type; 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.ConstraintsSet;
import de.dhbwstuttgart.typeinference.JavaCodeResult; import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.Pair; import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.ResultSet; import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions; import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException; import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException;
import de.dhbwstuttgart.typeinference.unify.Unify; import de.dhbwstuttgart.typeinference.unify.Unify;
@ -91,8 +99,13 @@ public class ForStmt extends Statement
@Override @Override
public ConstraintsSet TYPEStmt(TypeAssumptions assumptions) { public ConstraintsSet TYPEStmt(TypeAssumptions assumptions) {
// TODO Auto-generated method stub //TODO: TYPEStmt
return null; ConstraintsSet ret = new ConstraintsSet();
this.setType(TypePlaceholder.fresh(this));
ret.add(body_Loop_block.TYPEStmt(assumptions));
return ret;
} }
public int getTypeLineNumber() { public int getTypeLineNumber() {
@ -100,9 +113,9 @@ public class ForStmt extends Statement
} }
@Override @Override
public JavaCodeResult printJavaCode(ResultSet resultSet) { public JavaCodeResult printJavaCode(ResultSet resultSet) {
// TODO Auto-generated method stub // TODO printJavaCode
throw new NotImplementedException(); return new JavaCodeResult();
} }
@Override @Override
@ -121,7 +134,34 @@ public JavaCodeResult printJavaCode(ResultSet resultSet) {
@Override @Override
public InstructionList genByteCode(ClassGenerator _cg) { 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;
} }
} }

View File

@ -179,8 +179,6 @@ public class IfStmt extends Statement
@Override @Override
public InstructionList genByteCode(ClassGenerator _cg) { public InstructionList genByteCode(ClassGenerator _cg) {
InstructionFactory _factory = new InstructionFactory(_cg, _cg.getConstantPool());
InstructionList il = new InstructionList(); InstructionList il = new InstructionList();
IfInstruction ifInstruction = new IFEQ(null); IfInstruction ifInstruction = new IFEQ(null);

View File

@ -13,6 +13,7 @@ import org.apache.commons.bcel6.generic.ObjectType;
import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.bytecode.ClassGenerator; import de.dhbwstuttgart.bytecode.ClassGenerator;
import de.dhbwstuttgart.logger.Logger; import de.dhbwstuttgart.logger.Logger;
import de.dhbwstuttgart.logger.Section;
import de.dhbwstuttgart.myexception.CTypeReconstructionException; import de.dhbwstuttgart.myexception.CTypeReconstructionException;
import de.dhbwstuttgart.myexception.JVMCodeException; import de.dhbwstuttgart.myexception.JVMCodeException;
import de.dhbwstuttgart.myexception.SCExcept; import de.dhbwstuttgart.myexception.SCExcept;
@ -152,7 +153,13 @@ public class LocalOrFieldVar extends Expr
if(this.isFieldAccess){ 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().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; return il;
} }

View File

@ -0,0 +1,7 @@
class Condition{
Boolean method() {
return 0 == 1;
}
}

View File

@ -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);
}
}

View File

@ -1,6 +1,6 @@
class IfElse{ class IfElse{
method(){ method(){
while(true){ for( i = 0; i < 10 ; i = i++){
} }
} }

View File

@ -1,7 +1,7 @@
class IfStatement{ class IfStatement{
method(flag){ method(flag){
if(flag){ if(flag){
} }
} }
} }

View File

@ -20,7 +20,7 @@ public class SingleClassTester {
try { try {
compiler.parse(new File(inputFile)); compiler.parse(new File(inputFile));
Menge<ByteCodeResult> bytecode = compiler.generateBytecode(compiler.typeReconstruction().firstElement()); Menge<ByteCodeResult> bytecode = compiler.generateBytecode(compiler.typeReconstruction().firstElement());
System.out.println(bytecode); //System.out.println(bytecode);
bytecode.firstElement().getByteCode().getJavaClass().dump(new File(outputFile)); bytecode.firstElement().getByteCode().getJavaClass().dump(new File(outputFile));
} catch (IOException | yyException e) { } catch (IOException | yyException e) {
e.printStackTrace(); e.printStackTrace();