forked from JavaTX/JavaCompilerCore
modified: src/de/dhbwstuttgart/bytecode/BytecodeGen.java
modified: src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java modified: src/de/dhbwstuttgart/bytecode/signature/Signature.java modified: src/de/dhbwstuttgart/bytecode/utilities/KindOfLambda.java modified: src/de/dhbwstuttgart/bytecode/utilities/Simplify.java modified: src/de/dhbwstuttgart/syntaxtree/Constructor.java modified: test/bytecode/MatrixOpTest.java modified: test/bytecode/YTest.java modified: test/bytecode/javFiles/OL.jav
This commit is contained in:
parent
964b73e8c4
commit
af6537841c
@ -6,6 +6,7 @@ import java.util.HashSet;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.*;
|
import de.dhbwstuttgart.syntaxtree.statement.*;
|
||||||
@ -55,6 +56,8 @@ public class BytecodeGen implements ASTVisitor {
|
|||||||
private SourceFile sf;
|
private SourceFile sf;
|
||||||
private String path;
|
private String path;
|
||||||
|
|
||||||
|
private Optional<Constructor> fieldInitializations;
|
||||||
|
|
||||||
private int indexOfFirstParam = 0;
|
private int indexOfFirstParam = 0;
|
||||||
|
|
||||||
private String superClass;
|
private String superClass;
|
||||||
@ -119,6 +122,7 @@ public class BytecodeGen implements ASTVisitor {
|
|||||||
|
|
||||||
int acc = isInterface?classOrInterface.getModifiers()+Opcodes.ACC_ABSTRACT:classOrInterface.getModifiers()+Opcodes.ACC_SUPER;
|
int acc = isInterface?classOrInterface.getModifiers()+Opcodes.ACC_ABSTRACT:classOrInterface.getModifiers()+Opcodes.ACC_SUPER;
|
||||||
|
|
||||||
|
fieldInitializations = classOrInterface.getfieldInitializations();
|
||||||
|
|
||||||
// resultSet = listOfResultSets.get(0);
|
// resultSet = listOfResultSets.get(0);
|
||||||
boolean isConsWithNoParamsVisited = false;
|
boolean isConsWithNoParamsVisited = false;
|
||||||
@ -156,10 +160,12 @@ public class BytecodeGen implements ASTVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for(Constructor c : classOrInterface.getConstructors()) {
|
for(Constructor c : classOrInterface.getConstructors()) {
|
||||||
if(!isConsWithNoParamsVisited)
|
// if(!isConsWithNoParamsVisited) {
|
||||||
c.accept(this);
|
c.accept(this);
|
||||||
if(!c.getParameterList().iterator().hasNext())
|
// }
|
||||||
isConsWithNoParamsVisited = true;
|
|
||||||
|
// if(!c.getParameterList().iterator().hasNext())
|
||||||
|
// isConsWithNoParamsVisited = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(Method m : classOrInterface.getMethods()) {
|
for(Method m : classOrInterface.getMethods()) {
|
||||||
@ -194,7 +200,7 @@ public class BytecodeGen implements ASTVisitor {
|
|||||||
field.getParameterList().accept(this);
|
field.getParameterList().accept(this);
|
||||||
|
|
||||||
String methParamTypes = field.name+"%%";
|
String methParamTypes = field.name+"%%";
|
||||||
|
|
||||||
Iterator<FormalParameter> itr = field.getParameterList().iterator();
|
Iterator<FormalParameter> itr = field.getParameterList().iterator();
|
||||||
while(itr.hasNext()) {
|
while(itr.hasNext()) {
|
||||||
FormalParameter fp = itr.next();
|
FormalParameter fp = itr.next();
|
||||||
@ -203,6 +209,7 @@ public class BytecodeGen implements ASTVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(methodNameAndParamsT.contains(methParamTypes)) {
|
if(methodNameAndParamsT.contains(methParamTypes)) {
|
||||||
|
System.out.println("ignore - Method: "+field.name +" , paramsType: "+methParamTypes);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
methodNameAndParamsT.add(methParamTypes);
|
methodNameAndParamsT.add(methParamTypes);
|
||||||
@ -225,13 +232,18 @@ public class BytecodeGen implements ASTVisitor {
|
|||||||
Signature signature = new Signature(field, genericsAndBounds,methodParamsAndTypes,resultSet,constraints);
|
Signature signature = new Signature(field, genericsAndBounds,methodParamsAndTypes,resultSet,constraints);
|
||||||
sig = signature.toString();
|
sig = signature.toString();
|
||||||
}
|
}
|
||||||
|
if(field.getParameterList().iterator().hasNext())
|
||||||
|
System.out.println(field.getParameterList().iterator().next().getType().acceptTV(new TypeToDescriptor()));
|
||||||
|
|
||||||
NormalConstructor constructor = new NormalConstructor(field,genericsAndBounds,hasGen);
|
NormalConstructor constructor = new NormalConstructor(field,genericsAndBounds,hasGen);
|
||||||
desc = constructor.accept(new DescriptorToString(resultSet));
|
desc = constructor.accept(new DescriptorToString(resultSet));
|
||||||
|
System.out.println("Constructor: " + field.getName() + " Sig: "+ sig + " Desc: " + desc);
|
||||||
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", desc, sig, null);
|
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", desc, sig, null);
|
||||||
mv.visitCode();
|
mv.visitCode();
|
||||||
|
Block block = fieldInitializations.get().block;
|
||||||
BytecodeGenMethod gen = new BytecodeGenMethod(className,superClass,resultSet,field, mv,paramsAndLocals,cw,
|
BytecodeGenMethod gen = new BytecodeGenMethod(className,superClass,resultSet,field, mv,paramsAndLocals,cw,
|
||||||
genericsAndBoundsMethod,genericsAndBounds,isInterface,classFiles, sf,path);
|
genericsAndBoundsMethod,genericsAndBounds,isInterface,classFiles, sf,path, block);
|
||||||
if(!field.getParameterList().iterator().hasNext()) {
|
if(!field.getParameterList().iterator().hasNext() && !(field.block.statements.get(field.block.statements.size()-1) instanceof ReturnVoid)) {
|
||||||
mv.visitInsn(Opcodes.RETURN);
|
mv.visitInsn(Opcodes.RETURN);
|
||||||
}
|
}
|
||||||
mv.visitMaxs(0, 0);
|
mv.visitMaxs(0, 0);
|
||||||
|
@ -75,6 +75,8 @@ public class BytecodeGenMethod implements StatementVisitor {
|
|||||||
|
|
||||||
private boolean needDUP = false;
|
private boolean needDUP = false;
|
||||||
|
|
||||||
|
private Block blockFieldInit = null;
|
||||||
|
private boolean isBlockFieldInitVisited = false;
|
||||||
// for tests **
|
// for tests **
|
||||||
private String fieldName;
|
private String fieldName;
|
||||||
private String fieldDesc;
|
private String fieldDesc;
|
||||||
@ -85,7 +87,32 @@ public class BytecodeGenMethod implements StatementVisitor {
|
|||||||
private HashMap<String, byte[]> classFiles;
|
private HashMap<String, byte[]> classFiles;
|
||||||
|
|
||||||
private ArrayList<RefTypeOrTPHOrWildcardOrGeneric> varsFunInterface = new ArrayList<>();;
|
private ArrayList<RefTypeOrTPHOrWildcardOrGeneric> varsFunInterface = new ArrayList<>();;
|
||||||
|
// generate bytecode for constructor
|
||||||
|
public BytecodeGenMethod(String className, String superClass,ResultSet resultSet, Method m, MethodVisitor mv,
|
||||||
|
HashMap<String, Integer> paramsAndLocals, ClassWriter cw, HashMap<String, String> genericsAndBoundsMethod,
|
||||||
|
HashMap<String, String> genericsAndBounds, boolean isInterface, HashMap<String, byte[]> classFiles,
|
||||||
|
SourceFile sf,String path, Block block) {
|
||||||
|
|
||||||
|
this.className = className;
|
||||||
|
this.superClass = superClass;
|
||||||
|
this.resultSet = resultSet;
|
||||||
|
this.m = m;
|
||||||
|
this.mv = mv;
|
||||||
|
this.paramsAndLocals = paramsAndLocals;
|
||||||
|
this.cw = cw;
|
||||||
|
this.genericsAndBoundsMethod = genericsAndBoundsMethod;
|
||||||
|
this.genericsAndBounds = genericsAndBounds;
|
||||||
|
this.isInterface = isInterface;
|
||||||
|
this.classFiles = classFiles;
|
||||||
|
this.sf = sf;
|
||||||
|
this.path = path;
|
||||||
|
if(block != null)
|
||||||
|
this.blockFieldInit = block;
|
||||||
|
this.m.block.accept(this);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public BytecodeGenMethod(String className, String superClass,ResultSet resultSet, Method m, MethodVisitor mv,
|
public BytecodeGenMethod(String className, String superClass,ResultSet resultSet, Method m, MethodVisitor mv,
|
||||||
HashMap<String, Integer> paramsAndLocals, ClassWriter cw, HashMap<String, String> genericsAndBoundsMethod,
|
HashMap<String, Integer> paramsAndLocals, ClassWriter cw, HashMap<String, String> genericsAndBoundsMethod,
|
||||||
HashMap<String, String> genericsAndBounds, boolean isInterface, HashMap<String, byte[]> classFiles, SourceFile sf,String path) {
|
HashMap<String, String> genericsAndBounds, boolean isInterface, HashMap<String, byte[]> classFiles, SourceFile sf,String path) {
|
||||||
@ -161,6 +188,17 @@ public class BytecodeGenMethod implements StatementVisitor {
|
|||||||
superCall.arglist.accept(this);
|
superCall.arglist.accept(this);
|
||||||
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, this.superClass, superCall.name, "()V",
|
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, this.superClass, superCall.name, "()V",
|
||||||
isInterface);
|
isInterface);
|
||||||
|
|
||||||
|
if(blockFieldInit!=null && !isBlockFieldInitVisited) {
|
||||||
|
isBlockFieldInitVisited =true;
|
||||||
|
//blockFieldInit.accept(this);
|
||||||
|
for(Statement stmt : blockFieldInit.statements) {
|
||||||
|
if(stmt instanceof SuperCall)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
stmt.accept(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ??
|
// ??
|
||||||
|
@ -19,6 +19,7 @@ import de.dhbwstuttgart.syntaxtree.Constructor;
|
|||||||
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
|
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
|
||||||
import de.dhbwstuttgart.syntaxtree.Method;
|
import de.dhbwstuttgart.syntaxtree.Method;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
|
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
|
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||||
@ -331,15 +332,17 @@ public class Signature {
|
|||||||
|
|
||||||
case "EWC":
|
case "EWC":
|
||||||
System.out.println("EWC---Signature");
|
System.out.println("EWC---Signature");
|
||||||
SuperWildcardType ewc = (SuperWildcardType) t;
|
ExtendsWildcardType ewc = (ExtendsWildcardType) t;
|
||||||
String esigInner = ewc.getInnerType().acceptTV(new TypeToSignature());
|
String esigInner = ewc.getInnerType().acceptTV(new TypeToSignature());
|
||||||
|
System.out.println(esigInner);
|
||||||
if(ewc.getInnerType() instanceof TypePlaceholder) {
|
if(ewc.getInnerType() instanceof TypePlaceholder) {
|
||||||
sv.visitTypeArgument('+').visitTypeVariable(esigInner.substring(1, esigInner.length()));
|
sv.visitTypeArgument('+').visitTypeVariable(esigInner.substring(1, esigInner.length()));
|
||||||
} else if(ewc.getInnerType() instanceof RefType) {
|
} else if(ewc.getInnerType() instanceof RefType) {
|
||||||
if(esigInner.contains("$$")) {
|
if(esigInner.contains("$$")) {
|
||||||
sv.visitTypeArgument('+').visitInterface().visitClassType(esigInner.substring(1,esigInner.length()));
|
sv.visitTypeArgument('+').visitInterface().visitClassType(esigInner.substring(1,esigInner.length()));
|
||||||
}else {
|
}else {
|
||||||
sv.visitTypeArgument('+').visitClassType(esigInner.substring(1,esigInner.length()));
|
sv.visitClassType(esigInner.substring(1,esigInner.length()));
|
||||||
|
// sv.visitTypeArgument('+').visitClassType(esigInner.substring(1,esigInner.length()));
|
||||||
}
|
}
|
||||||
}else {
|
}else {
|
||||||
sv.visitTypeArgument('+').visitTypeVariable(esigInner.substring(1));
|
sv.visitTypeArgument('+').visitTypeVariable(esigInner.substring(1));
|
||||||
|
@ -20,6 +20,7 @@ public class KindOfLambda implements StatementVisitor{
|
|||||||
private List<RefTypeOrTPHOrWildcardOrGeneric> argumentList = new ArrayList<>();
|
private List<RefTypeOrTPHOrWildcardOrGeneric> argumentList = new ArrayList<>();
|
||||||
private ArrayList<String> usedVars = new ArrayList<>();
|
private ArrayList<String> usedVars = new ArrayList<>();
|
||||||
private boolean hasThis = false;
|
private boolean hasThis = false;
|
||||||
|
private ArrayList<String> definedLocals = new ArrayList<>();
|
||||||
|
|
||||||
public KindOfLambda(LambdaExpression lambdaExpression) {
|
public KindOfLambda(LambdaExpression lambdaExpression) {
|
||||||
this.params = lambdaExpression.params;
|
this.params = lambdaExpression.params;
|
||||||
@ -110,7 +111,7 @@ public class KindOfLambda implements StatementVisitor{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(LocalVar localVar) {
|
public void visit(LocalVar localVar) {
|
||||||
if(!contain(params, localVar.name)) {
|
if(!contain(params, localVar.name) && !definedLocals.contains(localVar.name)) {
|
||||||
argumentList.add(localVar.getType());
|
argumentList.add(localVar.getType());
|
||||||
if(hasThis) {
|
if(hasThis) {
|
||||||
usedVars.add(1, localVar.name);
|
usedVars.add(1, localVar.name);
|
||||||
@ -134,8 +135,7 @@ public class KindOfLambda implements StatementVisitor{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(LocalVarDecl localVarDecl) {
|
public void visit(LocalVarDecl localVarDecl) {
|
||||||
// TODO Auto-generated method stub
|
definedLocals.add(localVarDecl.getName());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -136,9 +136,8 @@ public class Simplify {
|
|||||||
// put the generated constraint and its equal set into result set
|
// put the generated constraint and its equal set into result set
|
||||||
result.put(constraint, eq);
|
result.put(constraint, eq);
|
||||||
constraints.clear();
|
constraints.clear();
|
||||||
allTypes.clear();
|
|
||||||
}
|
}
|
||||||
|
allTypes.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// build an equal set that contains all types
|
// build an equal set that contains all types
|
||||||
|
@ -16,7 +16,7 @@ public class Constructor extends Method {
|
|||||||
//TODO: Constructor braucht ein super-Statement
|
//TODO: Constructor braucht ein super-Statement
|
||||||
public Constructor(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList parameterList, Block codeInsideConstructor,
|
public Constructor(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList parameterList, Block codeInsideConstructor,
|
||||||
GenericDeclarationList gtvDeclarations, Token offset /*, List<Statement> fieldInitializations geloescht PL 2018-11-24 */) {
|
GenericDeclarationList gtvDeclarations, Token offset /*, List<Statement> fieldInitializations geloescht PL 2018-11-24 */) {
|
||||||
super(modifier, name, returnType, parameterList, codeInsideConstructor /* prepareBlock(codeInsideConstructor ,fieldInitializations geloescht PL 2018-11-24 )*/, gtvDeclarations, offset);
|
super(modifier, name, returnType, parameterList, /*codeInsideConstructor,*/ prepareBlock(codeInsideConstructor ) /*,fieldInitializations )geloescht PL 2018-11-24 )*/, gtvDeclarations, offset);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,8 +33,8 @@ public class MatrixOpTest {
|
|||||||
compiler = new JavaTXCompiler(fileToTest);
|
compiler = new JavaTXCompiler(fileToTest);
|
||||||
pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/";
|
pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/";
|
||||||
compiler.generateBytecode(pathToClassFile);
|
compiler.generateBytecode(pathToClassFile);
|
||||||
// loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
|
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
|
||||||
// classToTest = loader.loadClass("MatrixOP");
|
classToTest = loader.loadClass("MatrixOP");
|
||||||
/*
|
/*
|
||||||
Vector<Vector<Integer>> vv = new Vector<Vector<Integer>>();
|
Vector<Vector<Integer>> vv = new Vector<Vector<Integer>>();
|
||||||
Vector<Integer> v1 = new Vector<Integer> ();
|
Vector<Integer> v1 = new Vector<Integer> ();
|
||||||
|
@ -25,11 +25,11 @@ public class YTest {
|
|||||||
public void generateBC() throws Exception {
|
public void generateBC() throws Exception {
|
||||||
path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Y.jav";
|
path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Y.jav";
|
||||||
fileToTest = new File(path);
|
fileToTest = new File(path);
|
||||||
compiler = new JavaTXCompiler(fileToTest);
|
// compiler = new JavaTXCompiler(fileToTest);
|
||||||
compiler.generateBytecode(System.getProperty("user.dir")+"/testBytecode/generatedBC/");
|
// compiler.generateBytecode(System.getProperty("user.dir")+"/testBytecode/generatedBC/");
|
||||||
pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/";
|
// pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/";
|
||||||
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
|
// loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
|
||||||
classToTest = loader.loadClass("Y");
|
// classToTest = loader.loadClass("Y");
|
||||||
/*
|
/*
|
||||||
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
|
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ public class OL {
|
|||||||
m(x) { return x + x; }
|
m(x) { return x + x; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class OLMain {
|
public class OLMain {
|
||||||
@ -20,4 +20,3 @@ public class OLMain {
|
|||||||
return ol.m(x);
|
return ol.m(x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user