Equal Operator implementiert

This commit is contained in:
Enrico Schrödter 2016-04-13 19:31:35 +02:00
parent 11e149110a
commit dfe79a8030
8 changed files with 135 additions and 24 deletions

View File

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

View File

@ -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;
@ -32,5 +46,38 @@ public class EqualOp extends RelOp
}
// 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

View File

@ -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;
@ -86,6 +90,8 @@ public abstract class Operator extends SyntaxTreeNode
*/
public abstract HashMap<Type,Type> getReturnTypes(TypeAssumptions ass);
public abstract InstructionList genByteCode(ClassGenerator _cg, TypeinferenceResultSet rs, Binary operator);
@Override
public Menge<SyntaxTreeNode> getChildren() {

View File

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

View File

@ -0,0 +1,9 @@
class AddOperator{
Integer method(Integer x){
return x + x;
}
Double method(Double x){
return x + x;
}
}

View File

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

View File

@ -0,0 +1,5 @@
class EqualOperator{
Boolean method(Integer x, Integer y){
return x == y;
}
}

View File

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