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

This commit is contained in:
Pluemicke Martin 2015-06-18 11:19:00 +02:00
commit 7530e01ea9
8 changed files with 136 additions and 13 deletions

View File

@ -65,8 +65,8 @@ public class Constructor extends Method {
this.method = new MethodGen(Constants.ACC_PUBLIC, this.getType().getBytecodeType(), org.apache.bcel.generic.Type.NO_ARGS , new String[] { }, "<init>", parentClass.name, il, _cp);
//Iteration <EFBFBD>ber Block starten
Block instructions = this.get_Block();
InstructionList blockInstructions = instructions.genByteCode(cg);
Block block = this.get_Block();
InstructionList blockInstructions = block.genByteCode(cg);
il.append(blockInstructions);//Die vom Block generierten Instructions an die InstructionList der Methode anfügen

View File

@ -741,19 +741,27 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable
@Override
public void genByteCode(ClassGen cg) {
/*ConstantPoolGen _cp = cg.getConstantPool();
* InstructionFactory _factory = new InstructionFactory(cg, _cp);
* InstructionList il = new InstructionList();
* Class parentClass = this.getParentClass();
*/
ConstantPoolGen _cp = cg.getConstantPool();
InstructionFactory _factory = new InstructionFactory(cg, _cp);
InstructionList il = new InstructionList();
Class parentClass = this.getParentClass();
MethodGen method = new MethodGen(Constants.ACC_PUBLIC, this.getType().getBytecodeType(), org.apache.bcel.generic.Type.NO_ARGS , new String[] { }, this.get_Method_Name(), parentClass.name, il, _cp);
//oben steht MethodGen method als Variable (Z. 71)
Block block = this.get_Block();
InstructionList blockInstructions = block.genByteCode(cg);
// <EFBFBD>ber Statements iterieren um Block abzurufen
for (Statement statements : block.get_Statement()) {
statements.genByteCode(cg);
il.append(blockInstructions);//Die vom Block generierten Instructions an die InstructionList der Methode anfügen
if (block.get_Statement().size() == 0) { il.append(_factory.createReturn(org.apache.bcel.generic.Type.VOID)); }
else {
if (!(block.get_Statement().lastElement() instanceof Return)) { il.append(_factory.createReturn(org.apache.bcel.generic.Type.VOID)); }
}
method.setMaxStack(); //Die Stack Größe automatisch berechnen lassen (erst nach dem alle Instructions angehängt wurden)
cg.addMethod(method.getMethod());
}
}

View File

@ -4,6 +4,9 @@ package de.dhbwstuttgart.syntaxtree.statement;
// ino.module.Return.8651.import
import java.util.Hashtable;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.InstructionFactory;
import org.apache.bcel.generic.InstructionList;
import org.apache.bcel.generic.ClassGen;
import de.dhbwstuttgart.typeinference.Menge;
@ -130,9 +133,13 @@ public class Return extends Statement
}
@Override
public void genByteCode(ClassGen _cg) {
// TODO Auto-generated method stub
public InstructionList genByteCode(ClassGen cg) {
InstructionFactory _factory = new InstructionFactory(cg, cg.getConstantPool());
InstructionList il = new InstructionList();
il.append(_factory.createReturn(org.apache.bcel.generic.Type.VOID));
//TO BE DONE
return il;
}
}
// ino.end

View File

@ -810,6 +810,10 @@ public class RefType extends ObjectType implements IMatchable
param.parserPostProcessing(this);
}
}
public org.apache.bcel.generic.Type getBytecodeType() {
return new org.apache.bcel.generic.ObjectType(this.getTypeName());
}
}
// ino.end

View File

@ -0,0 +1,7 @@
class MethodEmpty{
void method() { }
}

View File

@ -0,0 +1,45 @@
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 MethodEmpty {
public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/";
public final static String testFile = "MethodEmpty.jav";
public final static String outputFile = "MethodEmpty.class";
@Test
public void test() {
LoggerConfiguration logConfig = new LoggerConfiguration().setOutput(Section.PARSER, System.out);
MyCompilerAPI compiler = MyCompiler.getAPI(logConfig);
try {
compiler.parse(new File(rootDirectory + testFile));
compiler.typeReconstruction();
ByteCodeResult bytecode = compiler.generateBytecode();
System.out.println(bytecode);
bytecode.getByteCode().getJavaClass().dump(new File(rootDirectory + outputFile));
} catch (IOException | yyException e) {
e.printStackTrace();
TestCase.fail();
}
}
}

View File

@ -0,0 +1,7 @@
class MethodEmptyRetType{
Object method() { return null; }
}

View File

@ -0,0 +1,45 @@
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 MethodEmptyRetType {
public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/";
public final static String testFile = "MethodEmptyRetType.jav";
public final static String outputFile = "MethodEmptyRetType.class";
@Test
public void test() {
LoggerConfiguration logConfig = new LoggerConfiguration().setOutput(Section.PARSER, System.out);
MyCompilerAPI compiler = MyCompiler.getAPI(logConfig);
try {
compiler.parse(new File(rootDirectory + testFile));
compiler.typeReconstruction();
ByteCodeResult bytecode = compiler.generateBytecode();
System.out.println(bytecode);
bytecode.getByteCode().getJavaClass().dump(new File(rootDirectory + outputFile));
} catch (IOException | yyException e) {
e.printStackTrace();
TestCase.fail();
}
}
}