Merge branch 'bytecode' of ssh://gohorb.ba-horb.de/bahome/projekt/git/JavaCompilerCore into bytecode

This commit is contained in:
Martin Plümicke 2016-04-12 09:57:09 +02:00
commit 0438ef9af4
34 changed files with 329 additions and 252 deletions

View File

@ -1,8 +1,12 @@
package de.dhbwstuttgart.bytecode; package de.dhbwstuttgart.bytecode;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Vector;
import org.apache.commons.bcel6.classfile.BootstrapMethod; import org.apache.commons.bcel6.classfile.BootstrapMethod;
import org.apache.commons.bcel6.classfile.BootstrapMethods; import org.apache.commons.bcel6.classfile.BootstrapMethods;
@ -10,6 +14,7 @@ import org.apache.commons.bcel6.classfile.ConstantPool;
import org.apache.commons.bcel6.classfile.InnerClass; import org.apache.commons.bcel6.classfile.InnerClass;
import org.apache.commons.bcel6.classfile.InnerClasses; import org.apache.commons.bcel6.classfile.InnerClasses;
import org.apache.commons.bcel6.classfile.JavaClass; import org.apache.commons.bcel6.classfile.JavaClass;
import org.apache.commons.bcel6.classfile.Method;
import org.apache.commons.bcel6.classfile.Signature; import org.apache.commons.bcel6.classfile.Signature;
import org.apache.commons.bcel6.generic.ClassGen; import org.apache.commons.bcel6.generic.ClassGen;
import org.apache.commons.bcel6.generic.ConstantPoolGen; import org.apache.commons.bcel6.generic.ConstantPoolGen;
@ -34,6 +39,7 @@ public class ClassGenerator extends ClassGen{
private Menge<TypePlaceholder> usedTPHs = new Menge<>(); private Menge<TypePlaceholder> usedTPHs = new Menge<>();
private Map<String, ClassGenerator> extraClasses = new HashMap<>(); private Map<String, ClassGenerator> extraClasses = new HashMap<>();
private List<String> methodsNamesAndTypes = new LinkedList<>();
public ClassGenerator(String name, Type superClass, String string, short accessflags, String[] strings, TypeinferenceResults typeinferenceResults) { public ClassGenerator(String name, Type superClass, String string, short accessflags, String[] strings, TypeinferenceResults typeinferenceResults) {
super(name,superClass.get_Name(),string,accessflags,strings, new DHBWConstantPoolGen()); super(name,superClass.get_Name(),string,accessflags,strings, new DHBWConstantPoolGen());
@ -47,7 +53,7 @@ public class ClassGenerator extends ClassGen{
} }
public DHBWInstructionFactory getInstructionFactory() { public DHBWInstructionFactory getInstructionFactory() {
return factory ; return factory;
} }
/** /**
@ -162,6 +168,20 @@ public class ClassGenerator extends ClassGen{
public TypeinferenceResults getTypeinferenceResults() { public TypeinferenceResults getTypeinferenceResults() {
return tiResult; return tiResult;
} }
@Override
public void addMethod(Method m) {
String methodNameAndTypes = m.getName()+Arrays.toString(m.getArgumentTypes());
if(methodsNamesAndTypes.contains(methodNameAndTypes)){
return;
}
methodsNamesAndTypes.add(methodNameAndTypes);
super.addMethod(m);
}

View File

@ -2,6 +2,8 @@ package de.dhbwstuttgart.bytecode;
import java.awt.List; import java.awt.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.bcel6.Constants; import org.apache.commons.bcel6.Constants;
import org.apache.commons.bcel6.classfile.Attribute; import org.apache.commons.bcel6.classfile.Attribute;
@ -32,6 +34,7 @@ public class DHBWInstructionFactory extends InstructionFactory{
private DHBWConstantPoolGen cp; private DHBWConstantPoolGen cp;
private ClassGenerator cg; private ClassGenerator cg;
private static Map<String, Integer> storeIndexes = new HashMap<>();
public DHBWInstructionFactory(ClassGenerator cg, DHBWConstantPoolGen cp) { public DHBWInstructionFactory(ClassGenerator cg, DHBWConstantPoolGen cp) {
super(cg, cp); super(cg, cp);
@ -137,8 +140,15 @@ public class DHBWInstructionFactory extends InstructionFactory{
} }
public LocalVariableInstruction createLoad(org.apache.commons.bcel6.generic.Type bytecodeType, String variableName) { public LocalVariableInstruction createLoad(org.apache.commons.bcel6.generic.Type bytecodeType, String variableName) {
int index = 1; //TODO: Hier muss ein Logger her, welcher aufzeichnet, an welcher Position welche Variable liegt. return InstructionFactory.createLoad(bytecodeType, storeIndexes.get(variableName));
return InstructionFactory.createLoad(bytecodeType, index); }
public Integer getStoreIndex(String variableName) {
if(!storeIndexes.containsKey(variableName)){
storeIndexes.put(variableName, storeIndexes.size()+1);
}
return storeIndexes.get(variableName);
} }
public static Type createObjectType() { public static Type createObjectType() {
@ -148,4 +158,8 @@ public class DHBWInstructionFactory extends InstructionFactory{
public Attribute createSignatureAttribute(String signature) { public Attribute createSignatureAttribute(String signature) {
return new Signature(cp.addUtf8("Signature"),2,cp.addUtf8(signature),cp.getConstantPool()); return new Signature(cp.addUtf8("Signature"),2,cp.addUtf8(signature),cp.getConstantPool());
} }
public void resetStoreIndexes() {
storeIndexes = new HashMap<>();
}
} }

View File

@ -7,6 +7,8 @@ import java.util.Collection;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.lang.model.element.Modifier; import javax.lang.model.element.Modifier;
@ -106,7 +108,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I
} }
//Zuerst die Methoden und Felder abarbeiten: //Zuerst die Methoden und Felder abarbeiten:
for(Method m : methods){ for(Method m : methods){
m.genByteCode(_cg); m.genByteCode(_cg, this);
} }
InstructionList fieldInitializations = new InstructionList(); InstructionList fieldInitializations = new InstructionList();
for(FieldDeclaration f : fieldDeclarations){ for(FieldDeclaration f : fieldDeclarations){

View File

@ -98,7 +98,7 @@ public class Constructor extends Method {
} }
@Override @Override
public void genByteCode(ClassGenerator cg) { public void genByteCode(ClassGenerator cg, Class classObj) {
this.genByteCode(cg, new InstructionList()); this.genByteCode(cg, new InstructionList());
} }
// super statement muss drin sein // super statement muss drin sein

View File

@ -16,6 +16,7 @@ import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.ResultSet; import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.TypeInsertable; import de.dhbwstuttgart.typeinference.TypeInsertable;
import de.dhbwstuttgart.typeinference.Typeable; import de.dhbwstuttgart.typeinference.Typeable;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions; import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertPoint; import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertPoint;

View File

@ -180,14 +180,18 @@ public class FieldDeclaration extends Field{
//Die Felddekleration an den Konstruktor anhängen: //Die Felddekleration an den Konstruktor anhängen:
InstructionList il = new InstructionList(); InstructionList il = new InstructionList();
il.append(new This(this).genByteCode(cg, rs)); il.append(new This(this).genByteCode(cg, rs));
il.append(this.wert.genByteCode(cg, rs));
if(wert != null){
il.append(this.wert.genByteCode(cg, rs));
}
FieldInstruction putFieldInstruction = FieldInstruction putFieldInstruction =
cg.getInstructionFactory().createFieldAccess(this.getParentClass().getName().toString(), cg.getInstructionFactory().createFieldAccess(this.getParentClass().getName().toString(),
this.getDescription(), this.getType().getBytecodeType(cg, rs), Constants.PUTFIELD); this.getDescription(), this.getType().getBytecodeType(cg, rs), Constants.PUTFIELD);
il.append(putFieldInstruction ); il.append(putFieldInstruction );
return il; return il;
} }
/*@Override /*@Override
public void genByteCode(ClassGen cg) { public void genByteCode(ClassGen cg) {
// TODO Auto-generated method stub // TODO Auto-generated method stub

View File

@ -4,7 +4,7 @@ package de.dhbwstuttgart.syntaxtree;
// ino.module.FormalParameter.8561.import // ino.module.FormalParameter.8561.import
import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.bytecode.ClassGenerator;
import de.dhbwstuttgart.logger.Logger; import de.dhbwstuttgart.logger.Logger;
import de.dhbwstuttgart.syntaxtree.misc.DeclId; import de.dhbwstuttgart.syntaxtree.misc.DeclId;
@ -14,6 +14,7 @@ import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.ResultSet; import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.TypeInsertable; import de.dhbwstuttgart.typeinference.TypeInsertable;
import de.dhbwstuttgart.typeinference.Typeable; import de.dhbwstuttgart.typeinference.Typeable;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException; import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException;
import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException; import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException;
import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertPoint; import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertPoint;
@ -233,7 +234,7 @@ public class FormalParameter extends SyntaxTreeNode implements Typeable, TypeIns
} }
@Override @Override
public String getDescription() { public String getDescription(){
String ret = ""; String ret = "";
if(this.getType() != null && !(this.getType() instanceof TypePlaceholder)){ if(this.getType() != null && !(this.getType() instanceof TypePlaceholder)){
ret += this.getType().getBytecodeSignature(null, null); ret += this.getType().getBytecodeSignature(null, null);

View File

@ -1,6 +1,7 @@
// ino.module.Method.8564.package // ino.module.Method.8564.package
package de.dhbwstuttgart.syntaxtree; package de.dhbwstuttgart.syntaxtree;
import java.util.Arrays;
// ino.end // ino.end
// ino.module.Method.8564.import // ino.module.Method.8564.import
import java.util.Enumeration; import java.util.Enumeration;
@ -465,39 +466,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
} }
@Override @Override
public void wandleRefTypeAttributes2GenericAttributes( public void wandleRefTypeAttributes2GenericAttributes(Menge<Type> classParalist) {
Menge<Type> classParalist) {
/*
* Menge<Type> paralist = new Menge<Type>();//Mit den Generischen Typen
* der Methode paralist.addAll(classParalist);
* paralist.addAll(this.genericMethodParameters);
*
* // Zuerst Returntype untersuchen Type returnType=getType(); Type
* pendantReturnType = null; if(returnType instanceof
* RefType)pendantReturnType =
* ((RefType)returnType).findGenericType(paralist, new
* Menge<GenericTypeVar>()); //GenericTypeVar
* pendantReturnType=ClassHelper.findGenericType(returnType,
* paralist,genericMethodParameters); if(pendantReturnType!=null){
* //Wenn generisch, dann modifizieren setReturnType(pendantReturnType);
* }
*
* // Dann parameterlist untersuchen for(int
* par=0;par<getParameterCount();par++){ FormalParameter
* fp=parameterlist.formalparameter.get(par); Type fpType=fp.getType();
* // Nur wenn es sich um ein RefType-Field handelt Type pendantPara =
* null; if(fpType instanceof RefType)pendantPara =
* ((RefType)fpType).findGenericType(paralist, new
* Menge<GenericTypeVar>()); //GenericTypeVar
* pendantPara=ClassHelper.findGenericType
* (fpType,paralist,genericMethodParameters); if(pendantPara!=null){
* //Wenn generisch, dann modifizieren fp.setType(pendantPara); } }
*
* // Zuletzt alle Lokalen Variablendeklarationen durchgehen
* if(block!=null){
* block.wandleRefTypeAttributes2GenericAttributes(paralist
* ,genericMethodParameters); }
*/
} }
public void set_Method_Name(String string) { public void set_Method_Name(String string) {
@ -520,30 +489,11 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
// TypeCheck, falls es sich um einen RefType handelt: // TypeCheck, falls es sich um einen RefType handelt:
this.returntype = this.returntype.checkTYPE(localAss, this); this.returntype = this.returntype.checkTYPE(localAss, this);
/*
* if(this.returntype!=null && (this.returntype instanceof RefType)&&
* !(this.returntype instanceof mycompiler.mytype.Void)){//Sonderfall
* der Methode: Ihr Typ darf Void definiert werden. Type replaceType =
* null; replaceType = ass.getTypeFor((RefType)this.returntype);
* if(replaceType == null)throw new
* TypeinferenceException("Der Typ "+this
* .getType().getName()+" ist nicht korrekt",this); this.returntype =
* replaceType; }
*/
// Die Parameter zu den Assumptions hinzufügen: // Die Parameter zu den Assumptions hinzufügen:
if (this.parameterlist != null) if (this.parameterlist != null)
for (FormalParameter param : this.parameterlist) { for (FormalParameter param : this.parameterlist) {
param.setType(param.getType().checkTYPE(localAss, this)); param.setType(param.getType().checkTYPE(localAss, this));
/*
* if(param.getType() instanceof RefType) { Type replaceType =
* null; replaceType = ass.getTypeFor((RefType)param.getType());
* if(replaceType == null) throw new
* TypeinferenceException("Der Typ "
* +param.getType().getName()+" ist nicht korrekt",param);
* param.setType(replaceType); }
*/
localAss.addAssumption(new ParameterAssumption(param)); localAss.addAssumption(new ParameterAssumption(param));
} }
ret.add(this.block.TYPEStmt(localAss)); ret.add(this.block.TYPEStmt(localAss));
@ -587,107 +537,15 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
TypeAssumptions ret = new TypeAssumptions(); TypeAssumptions ret = new TypeAssumptions();
ret.addAssumption(new MethodAssumption(this, parentClass)); ret.addAssumption(new MethodAssumption(this, parentClass));
return ret; return ret;
/*
* TypeAssumptions assumptions = new TypeAssumptions(); this.assumedType
* = null; //if((this.get_Method_Name().equals(classmember.getName()) ||
* this.get_Method_Name().equals("<init>")) &&
* ((this.getType().equals(new mycompiler.mytype.Void(0))) ||
* this.getType() instanceof TypePlaceholder)){
* if((this.get_Method_Name().equals(classmember.getName()) ||
* this.get_Method_Name().equals("<init>"))) {
* this.set_Method_Name("<init>"); this.assumedType = new
* RefType(classmember.getName(),0);
* this.setReturnType(this.assumedType); this.assumedType = new
* RefType("void",0); //Return constructorReturnStatement = new
* Return(0,0); //constructorReturnStatement.retexpr =
* //this.block.statements.add(constructorReturnStatement); } //hoth:
* 06.04.2006 //durchlaufe Block und suche nach Objektvariablen fuer
* Offset-Markierung Iterator<CTypeAssumption> fieldVarIterator =
* assumptions.iterator(); while (fieldVarIterator.hasNext()) { //Wenn
* ObjektVariable CTypeAssumption dieAssum = fieldVarIterator.next();
* if(dieAssum instanceof CInstVarTypeAssumption) {
* Class.isFirstLocalVarDecl=false; if(this.get_Block() != null)
* this.get_Block
* ().addOffsetsToAssumption(dieAssum,dieAssum.getIdentifier(),true); }
* }
*
* //methodList.addElement(method);
*
* //¯Â¿Â½r V_fields_methods: CMethodTypeAssumption methodAssum
* = new CMethodTypeAssumption(classmember.getType(),
* this.get_Method_Name(), this.getType(),
* this.getParameterCount(),this.getLineNumber(),this.getOffset(),new
* Menge<Integer>(),this.getGenericMethodParameters()); // Typannahme
* bauen...
*
*
* //Methode in V_Fields_methods ablegen //Dabei wird die
* OverloadedMethodID ermittelt !! //=> Method setzenuct
*
*
* assumptions.add(methodAssum);
* this.setOverloadedID(methodAssum.getHashSetKey
* ().getOverloadedMethodID());
*
*
* //¯Â¿Â½r die V_i: CTypeAssumptionSet localAssum = new
* CTypeAssumptionSet();
*
* //Bauen... ParameterList parameterList = this.getParameterList();
* if(parameterList!=null){ for(int i=0;
* i<parameterList.sc_get_Formalparalist().size(); i++){ FormalParameter
* para = parameterList.sc_get_Formalparalist().elementAt(i); //
* ¯Â¿Â½r V_fields_methods: CParaTypeAssumption paraAssum = new
* CParaTypeAssumption(classmember.getName(), this.get_Method_Name(),
* this.getParameterCount(), this.getOverloadedID(),para.get_Name(),
* para.getType(), para.getLineNumber(),para.getOffset(),new
* Menge<Integer>()); //fuege Offsets fuer Parameter hinzu, hoth:
* 06.04.2006 Class.isFirstLocalVarDecl=false;
*
* if(this.get_Block() != null)
* this.get_Block().addOffsetsToAssumption(paraAssum
* ,paraAssum.getIdentifier(),true);
*
* methodAssum.addParaAssumption(paraAssum);
*
* // ¯Â¿Â½r die V_i: CLocalVarTypeAssumption varAssum = new
* CLocalVarTypeAssumption(classmember.getName(),
* this.get_Method_Name(), this.getParameterCount(),
* this.getOverloadedID(),"1", para.get_Name(),para.getType(),
* para.getLineNumber(),para.getOffset(),new Menge<Integer>());
* localAssum.addElement(varAssum);
* //rememberLocals.addElement(varAssum); } } //...und
* hinzuf�gen:
*
* assumptions.add(localAssum);//Assumptions ¼r lokale Variablen den
* Assumptions hinzufügen
*
* //Hier wird der Typ der als Assumption eingetragen wird in die
* Variable assumedType dieser Klasse geschrieben: if(this.assumedType
* == null) // Falls der Typ nicht schon gesetzt ist. Das ist der Fall,
* falls die Methode ein Konstruktor ist this.assumedType =
* methodAssum.getAssumedType();
*
* return assumptions;
*/
} }
@Override @Override
public void parserPostProcessing(SyntaxTreeNode parent) { public void parserPostProcessing(SyntaxTreeNode parent) {
if (this.getType() == null) if (this.getType() == null)
this.setType(TypePlaceholder.fresh(this)); this.setType(TypePlaceholder.fresh(this));
// Bei dem Elterntyp der Methode darf es sich nur um eine Klasse
// handeln, daher Cast ohne Prüfung:
// Class parentClass = (Class)parent;
if (this.returntype == null) if (this.returntype == null)
this.returntype = TypePlaceholder.fresh(this); this.returntype = TypePlaceholder.fresh(this);
super.parserPostProcessing(parent); super.parserPostProcessing(parent);
/*
* this.returntype.parserPostProcessing(this); if(this.parameterlist !=
* null){ for(FormalParameter fp : this.parameterlist){
* fp.parserPostProcessing(this); } } for(GenericTypeVar gtv :
* this.getGenericParameter()){ gtv.parserPostProcessing(this); }
*/
} }
@Override @Override
@ -728,7 +586,6 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
DImethod.set_Name(withSignature); DImethod.set_Name(withSignature);
ret.set_DeclId(DImethod); ret.set_DeclId(DImethod);
Block tempBlock = new Block(); Block tempBlock = new Block();
// tempBlock.setType(new RefType(parent.getName(),0));
ret.set_Block(tempBlock); ret.set_Block(tempBlock);
ret.parserPostProcessing(parent); ret.parserPostProcessing(parent);
return ret; return ret;
@ -751,7 +608,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
return super.equals(obj); return super.equals(obj);
} }
public void genByteCode(ClassGenerator cg) { public void genByteCode(ClassGenerator cg, Class classObj) {
List<TypeinferenceResultSet> typeInterferenceResults = cg.getTypeinferenceResults().getTypeReconstructions(this, cg); List<TypeinferenceResultSet> typeInterferenceResults = cg.getTypeinferenceResults().getTypeReconstructions(this, cg);
for(TypeinferenceResultSet t: typeInterferenceResults){ for(TypeinferenceResultSet t: typeInterferenceResults){
@ -771,11 +628,12 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
argumentNames[i] = parameter.getIdentifier(); argumentNames[i] = parameter.getIdentifier();
i++; i++;
} }
if(!createdMethods.contains(argumentTypes, new ArgumentTypeEquals())){
createdMethods.add(argumentTypes);
}
} }
String nameAndSignature = get_Method_Name()+Arrays.toString(argumentTypes);
Logger.getLogger("nameAndSignature").error(nameAndSignature, Section.CODEGEN);
short constants = Constants.ACC_PUBLIC; //Per Definition ist jede Methode public short constants = Constants.ACC_PUBLIC; //Per Definition ist jede Methode public
if(this.modifiers != null && this.modifiers.includesModifier(new Static())) constants += Constants.ACC_STATIC; if(this.modifiers != null && this.modifiers.includesModifier(new Static())) constants += Constants.ACC_STATIC;
@ -788,44 +646,8 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
//Methode generieren und anfügen: //Methode generieren und anfügen:
cg.addMethod(method.createMethod(cg, getParameterList(), returnType, get_Block(), t)); cg.addMethod(method.createMethod(cg, getParameterList(), returnType, get_Block(), t));
/* Logger.getLogger("createMethod").debug(this.toString(), Section.CODEGEN);
short constants = Constants.ACC_PUBLIC; //Per Definition ist jede Methode public
if(this.modifiers != null && this.modifiers.includesModifier(new Static())) constants += Constants.ACC_STATIC;
Type returnType = this.getType();
//Methode generieren:
MethodGenerator method = new MethodGenerator(constants, returnType.getBytecodeType(cg), argumentTypes , argumentNames, this.get_Method_Name(), parentClass.name, il, _cp);
//Methode generieren und anfügen:
cg.addMethod(method.createMethod(cg, getParameterList(), returnType, get_Block()));
*/
} }
} }
} }
class ArgumentTypeEquals implements Equal<org.apache.commons.bcel6.generic.Type[]>{
@Override
public boolean equal(org.apache.commons.bcel6.generic.Type[] a, org.apache.commons.bcel6.generic.Type[] b) {
if(a.length != b.length){
Logger.getLogger("ArgumentTypeEquals").error("false(length)", Section.CODEGEN);
return false;
}
for(int i = 0; i < a.length; i++){
if(!a[i].equals(b[i])){
String as = a[i].toString();
String bs = b[i].toString();
Logger.getLogger("ArgumentTypeEquals").error("false "+as+" != "+bs, Section.CODEGEN);
return false;
}
}
Logger.getLogger("ArgumentTypeEquals").error("true", Section.CODEGEN);
return true;
}
}
// ino.end // ino.end

View File

@ -3,6 +3,7 @@ package de.dhbwstuttgart.syntaxtree;
import org.apache.commons.bcel6.generic.ClassGen; import org.apache.commons.bcel6.generic.ClassGen;
import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.bytecode.ClassGenerator;
import de.dhbwstuttgart.core.IItemWithOffset; import de.dhbwstuttgart.core.IItemWithOffset;
import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.type.Type; import de.dhbwstuttgart.syntaxtree.type.Type;
@ -10,6 +11,7 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.Pair; import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.ResultSet; import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.TypeInsertable; import de.dhbwstuttgart.typeinference.TypeInsertable;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.exceptions.DebugException; import de.dhbwstuttgart.typeinference.exceptions.DebugException;
import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException; import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException;
import de.dhbwstuttgart.typeinference.typedeployment.GenericTypeInsertPoint; import de.dhbwstuttgart.typeinference.typedeployment.GenericTypeInsertPoint;
@ -53,6 +55,8 @@ public abstract class SyntaxTreeNode implements IItemWithOffset{
/** /**
* Eine Beschreibung/Name des SyntaxTree-Nodes * Eine Beschreibung/Name des SyntaxTree-Nodes
* Hat nichts mit der Description im Bytecode zu tun,
* wird für die Anzeige des AST im Plugin verwendet
* @return * @return
*/ */
public String getDescription(){ public String getDescription(){

View File

@ -17,6 +17,7 @@ import org.apache.commons.bcel6.generic.LSTORE;
import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.bytecode.ClassGenerator; import de.dhbwstuttgart.bytecode.ClassGenerator;
import de.dhbwstuttgart.bytecode.DHBWInstructionFactory;
import de.dhbwstuttgart.logger.Logger; import de.dhbwstuttgart.logger.Logger;
import de.dhbwstuttgart.myexception.CTypeReconstructionException; import de.dhbwstuttgart.myexception.CTypeReconstructionException;
import de.dhbwstuttgart.myexception.JVMCodeException; import de.dhbwstuttgart.myexception.JVMCodeException;
@ -176,18 +177,12 @@ public class Assign extends Expr
return ret; return ret;
} }
public static int counterAssign = 0; //Zaehlvariable f<EFBFBD>r ISTORE
@Override @Override
public InstructionList genByteCode(ClassGenerator cg, TypeinferenceResultSet rs) { public InstructionList genByteCode(ClassGenerator cg, TypeinferenceResultSet rs) {
// TODO Auto-generated method stub DHBWInstructionFactory _factory = new DHBWInstructionFactory(cg, cg.getConstantPool());
InstructionFactory _factory = new InstructionFactory(cg, cg.getConstantPool());
InstructionList il = expr2.genByteCode(cg, rs);//expr2 rechte expr InstructionList il = expr2.genByteCode(cg, rs);//expr2 rechte expr
/* /*
String expr2Type = expr2.getType().get_Name().toString(); String expr2Type = expr2.getType().get_Name().toString();
switch(expr2Type){ switch(expr2Type){
@ -213,10 +208,7 @@ public class Assign extends Expr
} }
*/ */
//Es wird momentan immer von RefType ausgegangen: //Es wird momentan immer von RefType ausgegangen:
il.append(new ASTORE(counterAssign)); il.append(new ASTORE(_factory.getStoreIndex(expr1.get_Name())));
//TODO: ^^
counterAssign++; //macht STORE f<EFBFBD>r meherere Variable nutzbar (nicht nur ISTORE_1, ISTORE_2, etc.)
return il; return il;
} }

View File

@ -379,7 +379,7 @@ public class LocalVarDecl extends Statement implements TypeInsertable
} }
@Override @Override
public String getDescription() { public String getDescription(){
if(this.getType() == null)return "no type " + declid.toString(); if(this.getType() == null)return "no type " + declid.toString();
if(this.getType() instanceof TypePlaceholder)return declid.toString(); if(this.getType() instanceof TypePlaceholder)return declid.toString();
return this.getType().toString() + " " + declid.toString(); return this.getType().toString() + " " + declid.toString();

View File

@ -12,6 +12,7 @@ import org.apache.commons.bcel6.generic.InstructionList;
import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.bytecode.ClassGenerator; import de.dhbwstuttgart.bytecode.ClassGenerator;
import de.dhbwstuttgart.bytecode.DHBWInstructionFactory;
import de.dhbwstuttgart.logger.Logger; import de.dhbwstuttgart.logger.Logger;
import de.dhbwstuttgart.myexception.JVMCodeException; import de.dhbwstuttgart.myexception.JVMCodeException;
import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.Method;
@ -324,7 +325,8 @@ public class MethodCall extends Expr
@Override @Override
public InstructionList genByteCode(ClassGenerator cg, TypeinferenceResultSet rs) { public InstructionList genByteCode(ClassGenerator cg, TypeinferenceResultSet rs) {
InstructionList il = new InstructionList(); InstructionList il = new InstructionList();
InstructionFactory _factory = new InstructionFactory(cg, cg.getConstantPool()); DHBWInstructionFactory _factory = cg.getInstructionFactory();
_factory.resetStoreIndexes();
il.append(receiver.get_Expr().genByteCode(cg, rs)); il.append(receiver.get_Expr().genByteCode(cg, rs));
@ -343,6 +345,8 @@ public class MethodCall extends Expr
argumentTypen = new org.apache.commons.bcel6.generic.Type[this.getArgumentList().size()]; argumentTypen = new org.apache.commons.bcel6.generic.Type[this.getArgumentList().size()];
int i = 0; int i = 0;
for(Expr argument : this.arglist.expr){ for(Expr argument : this.arglist.expr){
_factory.getStoreIndex(argument.get_Name());
argumentTypen[i] = argument.getType().getBytecodeType(cg, rs); argumentTypen[i] = argument.getType().getBytecodeType(cg, rs);
//Das Argument auf den Stack legen: //Das Argument auf den Stack legen:
il.append(argument.genByteCode(cg, rs)); il.append(argument.genByteCode(cg, rs));

View File

@ -200,13 +200,16 @@ public class NewClass extends Expr
il.append(_cg.getInstructionFactory().createNew(this.getType().getBytecodeType(_cg, rs).toString())); il.append(_cg.getInstructionFactory().createNew(this.getType().getBytecodeType(_cg, rs).toString()));
il.append(InstructionConstants.DUP); il.append(InstructionConstants.DUP);
String signature = getType().getBytecodeSignature(_cg, rs);
String description = signature.substring(1, signature.length()-1);
if(arglist!=null){ if(arglist!=null){
il.append(arglist.generateBytecode(_cg, rs)); il.append(arglist.generateBytecode(_cg, rs));
il.append(_cg.getInstructionFactory().createInvoke(this.getType().getDescription(), "<init>", il.append(_cg.getInstructionFactory().createInvoke(description, "<init>",
org.apache.commons.bcel6.generic.Type.VOID, org.apache.commons.bcel6.generic.Type.VOID,
this.arglist.getBytecodeTypeList(_cg, rs), Constants.INVOKESPECIAL)); this.arglist.getBytecodeTypeList(_cg, rs), Constants.INVOKESPECIAL));
}else{ }else{
il.append(_cg.getInstructionFactory().createInvoke(this.getType().getDescription(), "<init>", il.append(_cg.getInstructionFactory().createInvoke(description, "<init>",
org.apache.commons.bcel6.generic.Type.VOID, org.apache.commons.bcel6.generic.Type.VOID,
new org.apache.commons.bcel6.generic.Type[]{}, Constants.INVOKESPECIAL)); new org.apache.commons.bcel6.generic.Type[]{}, Constants.INVOKESPECIAL));
} }

View File

@ -235,7 +235,7 @@ public class GenericTypeVar extends ObjectType
@Override @Override
public org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg, TypeinferenceResultSet rs) { public org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg, TypeinferenceResultSet rs) {
// TODO Bytecode // TODO Bytecode
return null; return org.apache.commons.bcel6.generic.Type.getType(getSignatureType(null));// new org.apache.commons.bcel6.generic.ObjectType("Object");
} }

View File

@ -874,7 +874,6 @@ public class RefType extends ObjectType implements IMatchable
public GenericClassType getGenericClassType(){ public GenericClassType getGenericClassType(){
return new GenericClassType(getName().toString(), getParaList(), parent, getOffset()); return new GenericClassType(getName().toString(), getParaList(), parent, getOffset());
} }
} }
// ino.end // ino.end

View File

@ -16,35 +16,6 @@ public class TypeinferenceResults {
} }
public TypeinferenceResults(Menge<TypeinferenceResultSet> typeReconstructions) { public TypeinferenceResults(Menge<TypeinferenceResultSet> typeReconstructions) {
//TODO: filter
int limit = typeReconstructions.size();
Logger.getLogger("TypeinferenceResults").error(new Integer(limit).toString(), Section.CODEGEN);
for(int i = 0; i < limit; i++){
Logger.getLogger("TypeinferenceResults").error(new Integer(i).toString(), Section.CODEGEN);
for(Pair p: typeReconstructions.get(i).getUnifiedConstraints()){
boolean flag = false;
Logger.getLogger("TypeinferenceResults").error(p.toString(), Section.CODEGEN);
if( p.TA1 instanceof SuperWildcardType ||
p.TA1 instanceof ExtendsWildcardType ||
p.TA2 instanceof SuperWildcardType ||
p.TA2 instanceof ExtendsWildcardType ||
flag){
Logger.getLogger("TypeinferenceResults").error("remove", Section.CODEGEN);
typeReconstructions.remove(i);
i--;
limit--;
flag = true;
}
}
}
this.typeReconstructions = typeReconstructions; this.typeReconstructions = typeReconstructions;
} }

View File

@ -0,0 +1,3 @@
class Id {
<GPM, FNF extends GPM> Fun1<? extends GPM, ? super FNF> op = (x) -> x;
}

View File

@ -0,0 +1,14 @@
package bytecode;
import org.junit.Test;
public class IdentityFieldTest {
public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/";
public final static String testFile = "IdentityField.jav";
public final static String outputFile = "IdentityField.class";
@Test
public void test() {
SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile);
}
}

View File

@ -0,0 +1,3 @@
class Parameter{
public String param;
}

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 ParameterTest {
public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/";
public final static String testFile = "Parameter.jav";
public final static String outputFile = "Parameter.class";
@Test
public void test() {
SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile);
}
}

View File

@ -0,0 +1,7 @@
import java.lang.System;
class SystemOutPrintln{
void method() {
System.out.println("Hello World");
}
}

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 SystemOutPrintlnTest {
public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/";
public final static String testFile = "SystemOutPrintln.jav";
public final static String outputFile = "SystemOutPrintln.class";
@Test
public void test() {
SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile);
}
}

View File

@ -0,0 +1,8 @@
import java.util.Vector;
class AutoOverloadingMultiResults{
void method(Integer a) {
b;
b = 1;
}
}

View File

@ -0,0 +1,35 @@
package bytecode.types;
import static org.junit.Assert.*;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Vector;
import org.junit.Test;
import org.junit.Ignore;
import bytecode.SourceFileBytecodeTest;
public class AutoOverloadingMultiResultsTest extends SourceFileBytecodeTest{
@Override
protected void init() {
testName = "AutoOverloadingMultiResults";
rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/";
}
@Test
public void testConstruct() throws Exception{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Object obj = cls.newInstance();
assertTrue(true);
}
}

View File

@ -1,6 +1,6 @@
import java.util.Vector; import java.util.Vector;
class AutoOverloadingVector{ class AutoOverloadingVector{
method2(p){ method2(p){
method(p); method(p);
} }

View File

@ -43,7 +43,7 @@ public class AutoOverloadingVectorTest extends SourceFileBytecodeTest{
URL url = file.toURL(); URL url = file.toURL();
URL[] urls = new URL[]{url}; URL[] urls = new URL[]{url};
Class string = classLoader.loadClass("java.lang.String"); Class string = classLoader.loadClass("java%util%Vector%%java%lang%String%");
Class[] params = new Class[1]; Class[] params = new Class[1];
params[0] = string; params[0] = string;
@ -69,13 +69,13 @@ public class AutoOverloadingVectorTest extends SourceFileBytecodeTest{
URL url = file.toURL(); URL url = file.toURL();
URL[] urls = new URL[]{url}; URL[] urls = new URL[]{url};
Integer integer = new Integer(123); Class integer = classLoader.loadClass("java%util%Vector%%java%lang%Integer%");
Class[] params = new Class[1]; Class[] params = new Class[1];
params[0] = integer.getClass(); params[0] = integer;
Method method = cls.getDeclaredMethod("method2", params); Method method = cls.getDeclaredMethod("method2", params);
method.invoke(obj, integer); method.invoke(obj, integer.newInstance());
assertTrue(true); assertTrue(true);
}catch(Exception e){ }catch(Exception e){
throw new RuntimeException(e); throw new RuntimeException(e);

View File

@ -1,6 +1,6 @@
import java.util.Vector; import java.util.Vector;
class SuperType{ class ExtendsType{
Vector<Integer> integerVector; Vector<Integer> integerVector;
void method() { void method() {

View File

@ -16,7 +16,7 @@ import bytecode.SourceFileBytecodeTest;
public class ExtendsTypeTest extends SourceFileBytecodeTest{ public class ExtendsTypeTest extends SourceFileBytecodeTest{
@Override @Override
protected void init() { protected void init() {
testName = "SuperType"; testName = "ExtendsType";
rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/";
} }

View File

@ -4,6 +4,7 @@ import static org.junit.Assert.*;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Vector;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -60,13 +61,13 @@ public class ExtendsVectorStringTest extends ASTBytecodeTest{
Object obj = cls.newInstance(); Object obj = cls.newInstance();
Class stringClass = classLoader.loadClass("java.lang.Object"); Class objectClass = classLoader.loadClass("java.lang.Object");
Class[] params = new Class[1]; Class[] params = new Class[1];
params[0] = stringClass; params[0] = objectClass;
Method method = cls.getDeclaredMethod("add", params); Method method = cls.getDeclaredMethod("add", params);
method.invoke(obj, stringClass.newInstance()); method.invoke(obj, objectClass.newInstance());
}catch(Exception e){ }catch(Exception e){
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -83,5 +84,9 @@ public class ExtendsVectorStringTest extends ASTBytecodeTest{
public String getTestName() { public String getTestName() {
return "ExtendsVectorString"; return "ExtendsVectorString";
} }
class StringVector extends Vector<String>{
}
} }

View File

@ -0,0 +1,11 @@
import java.util.Vector;
class MultiExtends{
Integer method(Vector<Integer> a){
return method2(a);
}
Integer method2(Vector<Number> b){
return 1;
}
}

View File

@ -0,0 +1,35 @@
package bytecode.types;
import static org.junit.Assert.*;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Vector;
import org.junit.Test;
import org.junit.Ignore;
import bytecode.SourceFileBytecodeTest;
public class MultiExtendsTest extends SourceFileBytecodeTest{
@Override
protected void init() {
testName = "MultiExtends";
rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/";
}
@Test
public void testConstruct() throws Exception{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Object obj = cls.newInstance();
assertTrue(true);
}
}

View File

@ -9,4 +9,14 @@ class Overloading{
void method(Vector<Integer> v) { void method(Vector<Integer> v) {
} }
main(String[] args) {
ol;
ol = new Overloading();
v;
v = new Vector<String> ();
ol.method(v);
}
} }

View File

@ -0,0 +1,13 @@
import java.util.Vector;
class SuperType{
Vector<Number> numberVector;
void method() {
method(numberVector);
}
void method(Vector<? super Integer> v) {
}
}

View File

@ -0,0 +1,28 @@
package bytecode.types;
import static org.junit.Assert.*;
import org.junit.Test;
import bytecode.SourceFileBytecodeTest;
public class WildcardTest extends SourceFileBytecodeTest{
@Override
protected void init() {
testName = "Wildcard";
rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/";
}
@Test
public void testConstruct() throws Exception{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Object obj = cls.newInstance();
assertTrue(true);
}
}