Stack wird pro Methode behandelt

This commit is contained in:
Enrico Schrödter 2016-05-26 11:02:02 +02:00
parent 47c2b78713
commit b6df854cc6
8 changed files with 51 additions and 40 deletions

View File

@ -40,6 +40,7 @@ public class ClassGenerator extends ClassGen{
private Map<String, ClassGenerator> extraClasses = new HashMap<>(); private Map<String, ClassGenerator> extraClasses = new HashMap<>();
private List<String> methodsNamesAndTypes = new LinkedList<>(); private List<String> methodsNamesAndTypes = new LinkedList<>();
private MethodGenerator methodGenerator;
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());
@ -171,7 +172,7 @@ public class ClassGenerator extends ClassGen{
@Override @Override
public void addMethod(Method m) { public void addMethod(Method m) {
String methodNameAndTypes = m.getName()+Arrays.toString(m.getArgumentTypes()); String methodNameAndTypes = m.getReturnType().toString()+m.getName()+Arrays.toString(m.getArgumentTypes());
if(methodsNamesAndTypes.contains(methodNameAndTypes)){ if(methodsNamesAndTypes.contains(methodNameAndTypes)){
return; return;
@ -181,8 +182,13 @@ public class ClassGenerator extends ClassGen{
super.addMethod(m); super.addMethod(m);
} }
public void setMethodeGenerator(MethodGenerator methodGenerator) {
this.methodGenerator = methodGenerator;
}
public MethodGenerator getMethodGenerator() {
return methodGenerator;
}
} }

View File

@ -139,23 +139,6 @@ public class DHBWInstructionFactory extends InstructionFactory{
return new INVOKEDYNAMIC(index); return new INVOKEDYNAMIC(index);
} }
public static LocalVariableInstruction createLoad(org.apache.commons.bcel6.generic.Type bytecodeType, String variableName) {
return InstructionFactory.createLoad(bytecodeType, getStoreIndex(variableName));
}
public LocalVariableInstruction createStore(org.apache.commons.bcel6.generic.Type bytecodeType, String variableName) {
return InstructionFactory.createStore(bytecodeType, getStoreIndex(variableName));
}
public static Integer getStoreIndex(String variableName) {
if(storeIndexes.get(variableName) == null){
Integer index = storeIndexes.size()+1;
storeIndexes.put(variableName, index);
}
return storeIndexes.get(variableName);
}
public static Type createObjectType() { public static Type createObjectType() {
return new org.apache.commons.bcel6.generic.ObjectType("java.lang.Object"); return new org.apache.commons.bcel6.generic.ObjectType("java.lang.Object");
} }
@ -163,8 +146,4 @@ 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.clear();
}
} }

View File

@ -4,6 +4,9 @@ import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import org.apache.commons.bcel6.classfile.Attribute; import org.apache.commons.bcel6.classfile.Attribute;
import org.apache.commons.bcel6.classfile.ConstantPool; import org.apache.commons.bcel6.classfile.ConstantPool;
@ -16,7 +19,9 @@ import org.apache.commons.bcel6.classfile.Visitor;
import org.apache.commons.bcel6.generic.BranchInstruction; import org.apache.commons.bcel6.generic.BranchInstruction;
import org.apache.commons.bcel6.generic.ConstantPoolGen; import org.apache.commons.bcel6.generic.ConstantPoolGen;
import org.apache.commons.bcel6.generic.Instruction; import org.apache.commons.bcel6.generic.Instruction;
import org.apache.commons.bcel6.generic.InstructionFactory;
import org.apache.commons.bcel6.generic.InstructionList; import org.apache.commons.bcel6.generic.InstructionList;
import org.apache.commons.bcel6.generic.LocalVariableInstruction;
import org.apache.commons.bcel6.generic.MethodGen; import org.apache.commons.bcel6.generic.MethodGen;
import org.apache.commons.bcel6.generic.StackMapTableGen; import org.apache.commons.bcel6.generic.StackMapTableGen;
import org.apache.commons.bcel6.generic.Type; import org.apache.commons.bcel6.generic.Type;
@ -30,15 +35,22 @@ import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.ParameterList; import de.dhbwstuttgart.syntaxtree.ParameterList;
import de.dhbwstuttgart.syntaxtree.statement.Block; import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.Return; import de.dhbwstuttgart.syntaxtree.statement.Return;
import de.dhbwstuttgart.syntaxtree.statement.Statement;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
public class MethodGenerator extends MethodGen{ public class MethodGenerator extends MethodGen{
private Map<String, Integer> storeIndexes = new HashMap<>();
public MethodGenerator(int access_flags, Type return_type, Type[] arg_types, String[] arg_names, String method_name, public MethodGenerator(int access_flags, Type return_type, Type[] arg_types, String[] arg_names, String method_name,
String class_name, InstructionList il, ConstantPoolGen cp) { String class_name, InstructionList il, ConstantPoolGen cp) {
super(access_flags, return_type, arg_types, arg_names, method_name, class_name, il, cp); super(access_flags, return_type, arg_types, arg_names, method_name, class_name, il, cp);
for(String name: arg_names){
getStoreIndex(name);
}
} }
public Method createMethod(ClassGenerator cg, ParameterList parameter, de.dhbwstuttgart.syntaxtree.type.Type retType, Block block, TypeinferenceResultSet rs){ public Method createMethod(ClassGenerator cg, ParameterList parameter, de.dhbwstuttgart.syntaxtree.type.Type retType, Block block, TypeinferenceResultSet rs){
@ -80,6 +92,23 @@ public class MethodGenerator extends MethodGen{
return method.getMethod(); return method.getMethod();
} }
public LocalVariableInstruction createLoad(org.apache.commons.bcel6.generic.Type bytecodeType, String variableName) {
return InstructionFactory.createLoad(bytecodeType, getStoreIndex(variableName));
}
public LocalVariableInstruction createStore(org.apache.commons.bcel6.generic.Type bytecodeType, String variableName) {
return InstructionFactory.createStore(bytecodeType, getStoreIndex(variableName));
}
public Integer getStoreIndex(String variableName) {
if(storeIndexes.get(variableName) == null){
Integer index = storeIndexes.size()+1;
storeIndexes.put(variableName, index);
}
return storeIndexes.get(variableName);
}
} }

View File

@ -416,7 +416,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
ArrayList<String> argumentNames = new ArrayList<String>(); ArrayList<String> argumentNames = new ArrayList<String>();
if(this.parameterlist != null && this.parameterlist.size() > 0){ if(this.parameterlist != null && this.parameterlist.size() > 0){
generateArgumentList(argumentTypes, argumentNames, cg, _factory, t); generateArgumentList(argumentTypes, argumentNames, cg, t);
} }
short constants = Constants.ACC_PUBLIC; short constants = Constants.ACC_PUBLIC;
@ -426,15 +426,15 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
MethodGenerator method = new MethodGenerator(constants, returnType.getBytecodeType(cg, t), argumentTypes.toArray(new org.apache.commons.bcel6.generic.Type[parameterlist.size()]) , argumentNames.toArray(new String[parameterlist.size()]), this.get_Method_Name(), getParentClass().name, il, _cp); MethodGenerator method = new MethodGenerator(constants, returnType.getBytecodeType(cg, t), argumentTypes.toArray(new org.apache.commons.bcel6.generic.Type[parameterlist.size()]) , argumentNames.toArray(new String[parameterlist.size()]), this.get_Method_Name(), getParentClass().name, il, _cp);
cg.setMethodeGenerator(method);
cg.addMethod(method.createMethod(cg, getParameterList(), returnType, get_Block(), t)); cg.addMethod(method.createMethod(cg, getParameterList(), returnType, get_Block(), t));
} }
private void generateArgumentList(ArrayList<org.apache.commons.bcel6.generic.Type> argumentTypes, ArrayList<String> argumentNames, ClassGenerator cg, DHBWInstructionFactory _factory, TypeinferenceResultSet t) { private void generateArgumentList(ArrayList<org.apache.commons.bcel6.generic.Type> argumentTypes, ArrayList<String> argumentNames, ClassGenerator cg, TypeinferenceResultSet t) {
for(FormalParameter parameter : this.parameterlist){ for(FormalParameter parameter : this.parameterlist){
argumentTypes.add(parameter.getType().getBytecodeType(cg, t)); argumentTypes.add(parameter.getType().getBytecodeType(cg, t));
argumentNames.add(parameter.getIdentifier()); argumentNames.add(parameter.getIdentifier());
_factory.getStoreIndex(parameter.getIdentifier());
} }
} }
} }

View File

@ -181,7 +181,6 @@ public class Assign extends Expr
DHBWInstructionFactory _factory = new DHBWInstructionFactory(cg, cg.getConstantPool()); DHBWInstructionFactory _factory = new DHBWInstructionFactory(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){
@ -207,7 +206,7 @@ public class Assign extends Expr
} }
*/ */
//Es wird momentan immer von RefType ausgegangen: //Es wird momentan immer von RefType ausgegangen:
il.append(_factory.createStore(expr2.getType().getBytecodeType(cg, rs), expr1.get_Name())); il.append(cg.getMethodGenerator().createStore(expr2.getType().getBytecodeType(cg, rs), expr1.get_Name()));
return il; return il;
} }

View File

@ -169,7 +169,7 @@ public class LocalOrFieldVarOrClassname extends Expr
String name = this.get_Name(); String name = this.get_Name();
il.append(cg.getInstructionFactory().createLoad(byteCodeType, name)); il.append(cg.getMethodGenerator().createLoad(byteCodeType, name));
return il; return il;
} }

View File

@ -376,7 +376,7 @@ public class LocalVarDecl extends Statement implements TypeInsertable
@Override @Override
public InstructionList genByteCode(ClassGenerator _cg, TypeinferenceResultSet rs) { public InstructionList genByteCode(ClassGenerator _cg, TypeinferenceResultSet rs) {
_cg.getInstructionFactory().getStoreIndex(get_Name()); _cg.getMethodGenerator().getStoreIndex(get_Name());
return new InstructionList(); return new InstructionList();
} }
} }

View File

@ -326,8 +326,6 @@ public class MethodCall extends Expr
public InstructionList genByteCode(ClassGenerator cg, TypeinferenceResultSet rs) { public InstructionList genByteCode(ClassGenerator cg, TypeinferenceResultSet rs) {
InstructionList il = new InstructionList(); InstructionList il = new InstructionList();
DHBWInstructionFactory _factory = cg.getInstructionFactory(); DHBWInstructionFactory _factory = cg.getInstructionFactory();
//TODO: später wiederherstelln?
_factory.resetStoreIndexes();
il.append(receiver.get_Expr().genByteCode(cg, rs)); il.append(receiver.get_Expr().genByteCode(cg, rs));
@ -346,7 +344,7 @@ 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()); cg.getMethodGenerator().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: