NewStatement bytecode

This commit is contained in:
JanUlrich 2015-11-06 18:27:47 +01:00
parent a221adb28c
commit 0048d5f305
7 changed files with 118 additions and 46 deletions

View File

@ -32,6 +32,7 @@ public class JavaToBCEL {
new BCELifier(new ClassParser(rootDirectory+"LocalVarAccess.class").parse(), new FileOutputStream(new File(rootDirectory+"LocalVarAccessCreator.java"))).start();
new BCELifier(new ClassParser(rootDirectory+"Wildcard.class").parse(), new FileOutputStream(new File(rootDirectory+"WildcardCreator.java"))).start();
new BCELifier(new ClassParser(rootDirectory+"BooleanValue.class").parse(), new FileOutputStream(new File(rootDirectory+"BooleanValueCreator.java"))).start();
new BCELifier(new ClassParser(rootDirectory+"NewClass.class").parse(), new FileOutputStream(new File(rootDirectory+"NewClassCreator.java"))).start();
} catch (ClassFormatException | IOException e) {
e.printStackTrace();

View File

@ -1,39 +1,26 @@
// ino.module.ArgumentList.8621.package
package de.dhbwstuttgart.syntaxtree.statement;
// ino.end
// ino.module.ArgumentList.8621.import
import java.util.Iterator;
import de.dhbwstuttgart.typeinference.Menge;
import java.util.Iterator;
import org.apache.commons.bcel6.generic.InstructionList;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.bytecode.ClassGenerator;
import de.dhbwstuttgart.myexception.JVMCodeException;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.typeinference.JavaCodeResult;
import de.dhbwstuttgart.typeinference.ResultSet;
// ino.class.ArgumentList.24911.declaration
public class ArgumentList extends SyntaxTreeNode
// ino.end
// ino.class.ArgumentList.24911.body
{
// ino.attribute.expr.24914.declaration
public Menge<Expr> expr = new Menge<Expr>();
// ino.end
// ino.method.get_firstElement.24923.definition
public Object get_firstElement()
// ino.end
// ino.method.get_firstElement.24923.body
{
return expr.firstElement();
}
// ino.end
/**
* @author Andreas Stadelmeier, a10023
@ -93,6 +80,22 @@ public class ArgumentList extends SyntaxTreeNode
public Menge<? extends SyntaxTreeNode> getChildren() {
return expr;
}
public InstructionList generateBytecode(ClassGenerator cg){
InstructionList ret = new InstructionList();
for(Expr e : expr){
ret.append(e.genByteCode(cg));
}
return ret;
}
public org.apache.commons.bcel6.generic.Type[] getBytecodeTypeList(ClassGenerator cg){
org.apache.commons.bcel6.generic.Type[] ret = new org.apache.commons.bcel6.generic.Type[expr.size()];
int i = 0;
for(Expr e : expr){
ret[i] = e.getType().getBytecodeType(cg);
i++;
}
return ret;
}
}
// ino.end

View File

@ -6,8 +6,11 @@ import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import org.apache.commons.bcel6.Constants;
import org.apache.commons.bcel6.generic.ClassGen;
import org.apache.commons.bcel6.generic.InstructionConstants;
import org.apache.commons.bcel6.generic.InstructionList;
import org.apache.commons.bcel6.generic.ObjectType;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.bytecode.ClassGenerator;
@ -55,6 +58,7 @@ public class NewClass extends Expr
// ino.attribute.arglist.25837.declaration
private ArgumentList arglist;
private boolean isStatement = false;
// ino.end
// ino.attribute.parserlog.25840.declaration
protected static Logger parserlog = Logger.getLogger("parser");
@ -129,15 +133,7 @@ public class NewClass extends Expr
//TODO: Das hier noch vervollständigen
ConstraintsSet ret = new ConstraintsSet();
UndConstraint callConstraints = new UndConstraint();
//Die Auskommentierten Zeilen gehÃren zu MethodRefNew
//Menge<Type> argumentTypeList = new Menge<Type>();
//for(Expr expr : this.arglist.expr){
// argumentTypeList.add(expr.getTypeVariable());
//}
//FunN funN= new FunN(null,argumentTypeList);
//Constraint newClassTypeConstraint = new Constraint(null,null);
//ret.add(newClassTypeConstraint);
int numArgs = 0;
if(this.arglist != null)numArgs = this.arglist.size();
ConstructorAssumption cA = assumptions.getConstructorAssumption(this.get_Name(), numArgs);
@ -147,22 +143,9 @@ public class NewClass extends Expr
ret.add(this.arglist.expr.elementAt(i).TYPEExpr(assumptions));
callConstraints.addConstraint( this.arglist.expr.elementAt(i).getType().TYPE(assumptions, this), cA.getParameterType(i).TYPE(assumptions, this));
}
//if(this.arglist != null && this.arglist.expr != null)for(Expr arg : this.arglist.expr){
// ret.add(arg.TYPEExpr(assumptions));
//}
Type thisT = assumptions.checkType(new RefType(this.get_Name(),this,0), (SyntaxTreeNode)this);
this.setType(thisT);
/*
//Ein new-Aufruf ist nichts anderes als ein MethodCall der Methode <init>
MethodCall newAufruf = new MethodCall(0,0);
this.setType(assumptions.getTypeFor(new RefType(this.get_Name(),0)));
newAufruf.type = this.getType();
newAufruf.set_Name("<init>");
newAufruf.set_Receiver(null);
ret.add(new Overloading(assumptions, newAufruf, this.getType()).generateConsstraints());
*/
return ret;
}
@ -176,6 +159,7 @@ public class NewClass extends Expr
public ConstraintsSet TYPEStmt(TypeAssumptions assumptions){
ConstraintsSet ret = this.TYPEExpr(assumptions); //TypeExpr aufrufen
this.setType(new Void(this,0)); //Typ des Statments auf Void setzen.
this.isStatement = true;
return ret;
}
@ -208,8 +192,21 @@ public class NewClass extends Expr
@Override
public InstructionList genByteCode(ClassGenerator _cg) {
// TODO Bytecode
throw new NotImplementedException();
InstructionList il = new InstructionList();
if(arglist!=null){
il.append(arglist.generateBytecode(_cg));
il.append(_cg.getInstructionFactory().createInvoke(this.get_Name(), "<init>",
org.apache.commons.bcel6.generic.Type.VOID,
this.arglist.getBytecodeTypeList(_cg), Constants.INVOKESPECIAL));
}else{
il.append(_cg.getInstructionFactory().createInvoke(this.get_Name(), "<init>",
org.apache.commons.bcel6.generic.Type.VOID,
new org.apache.commons.bcel6.generic.Type[]{}, Constants.INVOKESPECIAL));
}
if(this.isStatement){
il.append(InstructionConstants.POP);
}
return il;
}

View File

@ -857,8 +857,8 @@ public class RefType extends ObjectType implements IMatchable
cg.addExtraClass(generatedClass.genByteCode(new TypeinferenceResultSet(generatedClass, new Menge<>(), new ResultSet())).getByteCode());
}
return "L"+combinedType+";";
String ret = new org.apache.commons.bcel6.generic.ObjectType(combinedType).getSignature();
return ret;
}
public String getCombinedType(ClassGenerator cg){

View File

@ -0,0 +1,10 @@
class NewStatement{
public NewStatement(Integer i){}
public NewStatement(String i){}
method() {
NewStatement n;
n = new NewStatement("hallo");
n = new NewStatement(1);
}
}

View File

@ -0,0 +1,55 @@
package bytecode;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
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 NewStatementTest {
public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/";
public final static String testFile = "NewStatement.jav";
public final static String outputFile = "NewStatement.class";
@Test
public void test() {
SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory);
}
@Test
public void testUntypedVectorDeclaredMethods() {
try{
File file = new File(System.getProperty("user.dir")+"/test/bytecode/types/");
URL url = file.toURL();
URL[] urls = new URL[]{url};
ClassLoader classLoader = new URLClassLoader(urls);
Class untypedVectorTest = classLoader.loadClass("UntypedVector");
for(Method method: untypedVectorTest.getDeclaredMethods()){
System.out.println(method.toGenericString());
}
}catch(Exception e){
throw new RuntimeException(e);
}
}
}

6
test/bytecode/Test2.java Normal file
View File

@ -0,0 +1,6 @@
class Test2{
public static void main(java.lang.String[] args){
new NewStatement(1).method();
}
}