Notizen und Codeschnipsel GenByteCode

This commit is contained in:
Fikus, Evelyn (WWI2012D) 2015-06-16 11:55:15 +02:00
parent f3a324fb38
commit 1d9f58b196
12 changed files with 186 additions and 110 deletions

View File

@ -3,7 +3,7 @@
<classpathentry kind="src" path="src"/>
<classpathentry excluding=".classpath|.cvsignore|.externalToolBuilders/|.project|.settings/|Papers/|bin/|doc/|examples/|lib/|notizen/|src/|test/|tools/" including="log4j.xml" kind="src" path=""/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="lib" path="lib/junit-4.0.jar" sourcepath="/home/janulrich/.m2/repository/junit/junit/4.0/junit-4.0-sources.jar"/>
<classpathentry kind="lib" path="lib/cloning.jar"/>
<classpathentry kind="lib" path="lib/bcel-5.2.jar"/>

View File

@ -60,8 +60,8 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I
* Log4j - Loggerinstanzen
*/
protected static Logger inferencelog = Logger.getLogger("inference");
//protected static Logger codegenlog = Logger.getLogger("codegen");
//protected static Logger parserlog = Logger.getLogger("parser");
protected static Logger codegenlog = Logger.getLogger("codegen");
protected static Logger parserlog = Logger.getLogger("parser");
protected UsedId pkgName;
protected Modifiers modifiers;
protected String name;
@ -73,8 +73,6 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I
//Method created with BCEL to generate ByteCode
public ByteCodeResult genByteCode() throws IOException {
SectionLogger logger = Logger.getSectionLogger(this.getClass().getName(), Section.CODEGEN);
logger.debug("Test");
@ -90,11 +88,11 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I
ByteCodeResult code = new ByteCodeResult(_cg);
return code;
/*ByteCodeResult bc = new ByteCodeResult();
_cg = new ClassGen(pkgName.get_Name() + "/" + name, superClass.get_Name(), name + ".java", Constants.ACC_PUBLIC , new String[] { });
//ByteCodeResult bc = new ByteCodeResult();
//_cg = new ClassGen(pkgName.get_Name() + "/" + name, superClass.get_Name(), name + ".java", Constants.ACC_PUBLIC , new String[] { });
//_cg zurückgeben
bc.append(BCELByteCodeOutput);
return _cg;*/
//bc.append(BCELByteCodeOutput);
//return _cg;
}
private Menge<Type> superif = new Menge<Type>();

View File

@ -58,19 +58,22 @@ public class Constructor extends Method {
InstructionList il = new InstructionList();
Class parentClass = this.getParentClass();
//TODO: Hier return type und Argument-Typen dynamisch generieren. return type mittels method.getReturnType() ermitteln
MethodGen method = new MethodGen(Constants.ACC_PUBLIC, org.apache.bcel.generic.Type.VOID, org.apache.bcel.generic.Type.NO_ARGS, new String[] { }, "<init>", parentClass.name, il, _cp);
InstructionHandle ih_0 = il.append(_factory.createLoad(org.apache.bcel.generic.Type.OBJECT, 0)); //creates Constructor
il.append(_factory.createInvoke(this.getParentClass().superclassid.toString(), "<init>", org.apache.bcel.generic.Type.VOID, org.apache.bcel.generic.Type.NO_ARGS, Constants.INVOKESPECIAL));
InstructionHandle ih_4 = il.append(_factory.createReturn(org.apache.bcel.generic.Type.VOID)); //creates Constructor
method.setMaxStack();
method.setMaxLocals();
cg.addMethod(method.getMethod());
//Hier müsste drin stehen: Kreiere einen Block, welcher ein Statement enthält, welches ein Super Statement ist.
// MethodGen method = new MethodGen(Constants.ACC_PUBLIC, org.apache.bcel.generic.Type.getReturnType(methode.getType().get_Name()), org.apache.bcel.generic.Type.NO_ARGS , new String[] { }, "<init>", parentClass.name, il, _cp);
il.dispose();
}
// super statement muss drin sein
// stmt genByteCode + im block genByteCode implementieren & dann Hierarchie ausprobieren
// de.dhbw.systanxtree.stmts supercall
// Aufrufhierarchie: Class->Felder->Konstruktor->Methode innerhalb Konstruktor->Block->Statements (in diesem Fall nur super())->hier wird bytecode für superaufruf generiert
/*
@Override
public void parserPostProcessing(SyntaxTreeNode parent){
if(this.parameterlist != null){
@ -82,7 +85,7 @@ public class Constructor extends Method {
gtv.parserPostProcessing(this);
}
}
*/
@Override
public ConstraintsSet TYPE(TypeAssumptions ass) {
//super.setType(this.getParentClass().getType());
@ -95,13 +98,13 @@ public class Constructor extends Method {
//throw new TypeinferenceException("Einem Konstruktor kann kein Typ zugewiesen werden", this);
//this.methode.setType(t);
}
/*
public Constructor(Method methode){
/*public Constructor(Method methode){
super(methode.getOffset());
this.methode = methode;
this.setDeclIdMenge(methode.getDeclIdMenge());
this.methode.setType(this.methode.getParentClass().getType());
}
}*/
@Override
public Menge<GenericTypeVar> getGenericParameter() {
@ -226,11 +229,11 @@ public class Constructor extends Method {
this.methode.setOverloadedID(overloadedID);
}
@Override
/*@Override
public String get_codegen_Param_Type(Menge paralist) {
return this.methode.get_codegen_Param_Type(paralist);
}
}*/
@Override
public String get_Method_Name() {
@ -244,12 +247,12 @@ public class Constructor extends Method {
return this.methode.get_Type_Paralist();
}
@Override
/*@Override
public void codegen(ClassFile classfile, Menge paralist)
throws JVMCodeException {
this.methode.codegen(classfile, paralist);
}
}*/
@Override
public int getLineNumber() {
@ -317,11 +320,11 @@ public class Constructor extends Method {
this.methode.set_Method_Name(string);
}
public ConstraintsSet TYPE(TypeAssumptions ass) {
/*public ConstraintsSet TYPE(TypeAssumptions ass) {
ConstraintsSet ret = new ConstraintsSet();
ret.add(this.methode.get_Block().TYPEStmt(ass));
return ret;
}
}*/
@Override
public String getTypeInformation() {
@ -335,24 +338,24 @@ public class Constructor extends Method {
return this.methode.printJavaCode(resultSet);
}
@Override
/*@Override
public TypeAssumptions createTypeAssumptions(Class classmember) {
Class parentClass = this.getParentClass();
TypeAssumptions ret = new TypeAssumptions();
ret.addAssumption(new ConstructorAssumption(this, parentClass));
return ret;
}
}*/
@Override
public SyntaxTreeNode getParent(){
return this.methode.getParent();
}
@Override
/*@Override
public void parserPostProcessing(SyntaxTreeNode parent) {
this.methode.parserPostProcessing(parent);
}
}*/
@Override
public Menge<SyntaxTreeNode> getChildren() {
@ -360,11 +363,11 @@ public class Constructor extends Method {
return this.methode.getChildren();
}
@Override
/*@Override
public void setType(Type t) {
throw new TypeinferenceException("Einem Konstruktor kann kein Typ zugewiesen werden", this);
//this.methode.setType(t);
}
}*/
@Override
public Type getType() {
@ -418,7 +421,7 @@ public class Constructor extends Method {
public Class getParentClass() {
return this.methode.getParentClass();
}
*/
}

View File

@ -179,3 +179,4 @@ public abstract class Field extends GTVDeclarationContext implements TypeInserta
*/
public abstract void genByteCode(ClassGen cg);
}

View File

@ -164,4 +164,10 @@ public class FieldDeclaration extends Field{
}
/*@Override
public void genByteCode(ClassGen cg) {
// TODO Auto-generated method stub
}*/
}

View File

@ -6,7 +6,13 @@ import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import org.apache.bcel.Constants;
import org.apache.bcel.generic.ClassGen;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.InstructionFactory;
import org.apache.bcel.generic.InstructionHandle;
import org.apache.bcel.generic.InstructionList;
import org.apache.bcel.generic.MethodGen;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.logger.Logger;
@ -20,6 +26,7 @@ import de.dhbwstuttgart.syntaxtree.misc.DeclId;
import de.dhbwstuttgart.syntaxtree.modifier.Modifiers;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.Return;
import de.dhbwstuttgart.syntaxtree.statement.Statement;
import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.Type;
@ -714,8 +721,20 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
@Override
public void genByteCode(ClassGen cg) {
// TODO Auto-generated method stub
ConstantPoolGen _cp = cg.getConstantPool();
InstructionFactory _factory = new InstructionFactory(cg, _cp);
InstructionList il = new InstructionList();
Class parentClass = this.getParentClass();
//TODO: Hier return type und Argument-Typen dynamisch generieren. return type mittels method.getReturnType() ermitteln
MethodGen method = new MethodGen(Constants.ACC_PUBLIC, org.apache.bcel.generic.Type.getReturnType(methode.getType().get_Name()), org.apache.bcel.generic.Type.NO_ARGS , new String[] { }, "<init>", parentClass.name, il, _cp);
//GenByteCode Constructor
method.setMaxStack();
method.setMaxLocals();
cg.addMethod(method.getMethod());
il.dispose();
}

View File

@ -5,8 +5,16 @@ package de.dhbwstuttgart.syntaxtree.statement;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import de.dhbwstuttgart.typeinference.Menge;
import org.apache.bcel.Constants;
import org.apache.bcel.generic.ClassGen;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.InstructionFactory;
import org.apache.bcel.generic.InstructionHandle;
import org.apache.bcel.generic.InstructionList;
import org.apache.bcel.generic.MethodGen;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.logger.Logger;
import de.dhbwstuttgart.logger.Section;
import de.dhbwstuttgart.myexception.CTypeReconstructionException;
@ -14,6 +22,7 @@ import de.dhbwstuttgart.myexception.JVMCodeException;
import de.dhbwstuttgart.myexception.SCExcept;
import de.dhbwstuttgart.myexception.SCStatementException;
import de.dhbwstuttgart.syntaxtree.Class;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.type.Type;
@ -37,6 +46,9 @@ public class Block extends Statement
// ino.end
// ino.class.Block.25037.body
{
private InstructionFactory _factory;
private ConstantPoolGen _cp;
private ClassGen _cg;
// ino.method.Block.25041.definition
public Block()
@ -81,6 +93,7 @@ public class Block extends Statement
// ino.method.set_Statement.25068.definition
public void set_Statement(Statement s)
// ino.end
@ -224,6 +237,7 @@ public class Block extends Statement
ret.attach(stmt.printJavaCode(resultSet));
ret.attach((stmt instanceof ExprStmt ? ";" : "") + "\n");
}
return ret.attach("}");
}
@ -241,5 +255,23 @@ public class Block extends Statement
public String getDescription(){
return "Block";
}
@Override
public void genByteCode(ClassGen cg) {
ConstantPoolGen _cp = cg.getConstantPool();
InstructionFactory _factory = new InstructionFactory(cg, _cp);
InstructionList il = new InstructionList();
Class parentClass = this.getParentClass();
//Instructionhandle dynamisch
//Frage: Wenn Block von Statements erbt, und Block selbst keinen BCEL Code besitzt, ist das hier dann nicht eine Sackgasse?
for(Statement statements : this.statements){
statements.genByteCode(_cg);
}
}
}
// ino.end

View File

@ -4,8 +4,13 @@ package de.dhbwstuttgart.syntaxtree.statement;
// ino.module.Statement.8652.import
import java.util.Enumeration;
import java.util.Hashtable;
import de.dhbwstuttgart.typeinference.Menge;
import org.apache.bcel.generic.ClassGen;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.InstructionFactory;
import org.apache.bcel.generic.InstructionList;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.core.IItemWithOffset;
import de.dhbwstuttgart.myexception.CTypeReconstructionException;
import de.dhbwstuttgart.myexception.JVMCodeException;
@ -122,5 +127,12 @@ public abstract class Statement extends SyntaxTreeNode implements IItemWithOffse
public Type getReturnType(){
return new de.dhbwstuttgart.syntaxtree.type.Void(this,-1);
}
public abstract void genByteCode(ClassGen _cg);
}
// ino.end

View File

@ -1,2 +1,7 @@
class EmptyClass{
public EmptyClass(){}
public void leckMichAmArsch(){}
}

View File

@ -24,7 +24,7 @@ public class EmptyClass {
public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/";
public final static String testFile = "EmptyClass.jav";
public final static String outputFile = "EmptyClass.class";
public final static String outputFile = "EmptyClassTest.class";
@Test
public void test() {
@ -32,7 +32,7 @@ public class EmptyClass {
MyCompilerAPI compiler = MyCompiler.getAPI(logConfig);
try {
compiler.parse(new File(rootDirectory + testFile));
//compiler.typeReconstruction();
compiler.typeReconstruction();
ByteCodeResult bytecode = compiler.generateBytecode();
System.out.println(bytecode);
bytecode.getByteCode().getJavaClass().dump(new File(rootDirectory + outputFile));