From 1d9f58b196dd254e4578014283a8a3138f502ceb Mon Sep 17 00:00:00 2001 From: "Fikus, Evelyn (WWI2012D)" Date: Tue, 16 Jun 2015 11:55:15 +0200 Subject: [PATCH] Notizen und Codeschnipsel GenByteCode --- .classpath | 22 +++--- bin/log4j.xml | 70 +++++++++---------- bin/log4jTesting.xml | 48 ++++++------- src/de/dhbwstuttgart/syntaxtree/Class.java | 16 ++--- .../dhbwstuttgart/syntaxtree/Constructor.java | 53 +++++++------- src/de/dhbwstuttgart/syntaxtree/Field.java | 1 + .../syntaxtree/FieldDeclaration.java | 6 ++ src/de/dhbwstuttgart/syntaxtree/Method.java | 21 +++++- .../syntaxtree/statement/Block.java | 34 ++++++++- .../syntaxtree/statement/Statement.java | 16 ++++- test/bytecode/EmptyClass.jav | 5 ++ test/bytecode/EmptyClass.java | 4 +- 12 files changed, 186 insertions(+), 110 deletions(-) diff --git a/.classpath b/.classpath index f142a181c..fd7ce2652 100755 --- a/.classpath +++ b/.classpath @@ -1,11 +1,11 @@ - - - - - - - - - - - + + + + + + + + + + + diff --git a/bin/log4j.xml b/bin/log4j.xml index 64e7c5dbd..f36fb342a 100755 --- a/bin/log4j.xml +++ b/bin/log4j.xml @@ -1,35 +1,35 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/bin/log4jTesting.xml b/bin/log4jTesting.xml index dc30c2453..ef8492188 100755 --- a/bin/log4jTesting.xml +++ b/bin/log4jTesting.xml @@ -1,24 +1,24 @@ - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/de/dhbwstuttgart/syntaxtree/Class.java b/src/de/dhbwstuttgart/syntaxtree/Class.java index fecde5472..6cb761afc 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Class.java +++ b/src/de/dhbwstuttgart/syntaxtree/Class.java @@ -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; @@ -72,9 +72,7 @@ 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 superif = new Menge(); diff --git a/src/de/dhbwstuttgart/syntaxtree/Constructor.java b/src/de/dhbwstuttgart/syntaxtree/Constructor.java index 958a0f30b..f09ccd30c 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Constructor.java +++ b/src/de/dhbwstuttgart/syntaxtree/Constructor.java @@ -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[] { }, "", parentClass.name, il, _cp); + + //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[] { }, "", 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(), "", 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()); + 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 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 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(); } - */ + } diff --git a/src/de/dhbwstuttgart/syntaxtree/Field.java b/src/de/dhbwstuttgart/syntaxtree/Field.java index cfce0dadc..55c553ac3 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Field.java +++ b/src/de/dhbwstuttgart/syntaxtree/Field.java @@ -179,3 +179,4 @@ public abstract class Field extends GTVDeclarationContext implements TypeInserta */ public abstract void genByteCode(ClassGen cg); } + diff --git a/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java b/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java index eefd74824..8d321a045 100644 --- a/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java +++ b/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java @@ -164,4 +164,10 @@ public class FieldDeclaration extends Field{ } + /*@Override + public void genByteCode(ClassGen cg) { + // TODO Auto-generated method stub + + }*/ + } diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index 88c3478cb..8d7a8e900 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -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[] { }, "", parentClass.name, il, _cp); + + //GenByteCode Constructor + + method.setMaxStack(); + method.setMaxLocals(); + cg.addMethod(method.getMethod()); + il.dispose(); } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Block.java b/src/de/dhbwstuttgart/syntaxtree/statement/Block.java index 639a98c2c..4ab830110 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Block.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Block.java @@ -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() @@ -79,6 +91,7 @@ public class Block extends Statement } // ino.end + // ino.method.set_Statement.25068.definition @@ -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 + + diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Statement.java b/src/de/dhbwstuttgart/syntaxtree/statement/Statement.java index b844a30fc..70d3e540a 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Statement.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Statement.java @@ -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 diff --git a/test/bytecode/EmptyClass.jav b/test/bytecode/EmptyClass.jav index 29d3c6260..44dfecf21 100644 --- a/test/bytecode/EmptyClass.jav +++ b/test/bytecode/EmptyClass.jav @@ -1,2 +1,7 @@ class EmptyClass{ + +public EmptyClass(){} + +public void leckMichAmArsch(){} + } \ No newline at end of file diff --git a/test/bytecode/EmptyClass.java b/test/bytecode/EmptyClass.java index affdf31d1..5ce92d79b 100644 --- a/test/bytecode/EmptyClass.java +++ b/test/bytecode/EmptyClass.java @@ -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));