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:
Fayez Abu Alia 2018-12-04 15:01:50 +01:00
parent 964b73e8c4
commit af6537841c
9 changed files with 74 additions and 23 deletions

View File

@ -6,6 +6,7 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.statement.*;
@ -55,6 +56,8 @@ public class BytecodeGen implements ASTVisitor {
private SourceFile sf;
private String path;
private Optional<Constructor> fieldInitializations;
private int indexOfFirstParam = 0;
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;
fieldInitializations = classOrInterface.getfieldInitializations();
// resultSet = listOfResultSets.get(0);
boolean isConsWithNoParamsVisited = false;
@ -156,10 +160,12 @@ public class BytecodeGen implements ASTVisitor {
}
for(Constructor c : classOrInterface.getConstructors()) {
if(!isConsWithNoParamsVisited)
// if(!isConsWithNoParamsVisited) {
c.accept(this);
if(!c.getParameterList().iterator().hasNext())
isConsWithNoParamsVisited = true;
// }
// if(!c.getParameterList().iterator().hasNext())
// isConsWithNoParamsVisited = true;
}
for(Method m : classOrInterface.getMethods()) {
@ -203,6 +209,7 @@ public class BytecodeGen implements ASTVisitor {
}
if(methodNameAndParamsT.contains(methParamTypes)) {
System.out.println("ignore - Method: "+field.name +" , paramsType: "+methParamTypes);
return;
}
methodNameAndParamsT.add(methParamTypes);
@ -225,13 +232,18 @@ public class BytecodeGen implements ASTVisitor {
Signature signature = new Signature(field, genericsAndBounds,methodParamsAndTypes,resultSet,constraints);
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);
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);
mv.visitCode();
Block block = fieldInitializations.get().block;
BytecodeGenMethod gen = new BytecodeGenMethod(className,superClass,resultSet,field, mv,paramsAndLocals,cw,
genericsAndBoundsMethod,genericsAndBounds,isInterface,classFiles, sf,path);
if(!field.getParameterList().iterator().hasNext()) {
genericsAndBoundsMethod,genericsAndBounds,isInterface,classFiles, sf,path, block);
if(!field.getParameterList().iterator().hasNext() && !(field.block.statements.get(field.block.statements.size()-1) instanceof ReturnVoid)) {
mv.visitInsn(Opcodes.RETURN);
}
mv.visitMaxs(0, 0);

View File

@ -75,6 +75,8 @@ public class BytecodeGenMethod implements StatementVisitor {
private boolean needDUP = false;
private Block blockFieldInit = null;
private boolean isBlockFieldInitVisited = false;
// for tests **
private String fieldName;
private String fieldDesc;
@ -85,6 +87,31 @@ public class BytecodeGenMethod implements StatementVisitor {
private HashMap<String, byte[]> classFiles;
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,
HashMap<String, Integer> paramsAndLocals, ClassWriter cw, HashMap<String, String> genericsAndBoundsMethod,
@ -161,6 +188,17 @@ public class BytecodeGenMethod implements StatementVisitor {
superCall.arglist.accept(this);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, this.superClass, superCall.name, "()V",
isInterface);
if(blockFieldInit!=null && !isBlockFieldInitVisited) {
isBlockFieldInitVisited =true;
//blockFieldInit.accept(this);
for(Statement stmt : blockFieldInit.statements) {
if(stmt instanceof SuperCall)
continue;
stmt.accept(this);
}
}
}
// ??

View File

@ -19,6 +19,7 @@ import de.dhbwstuttgart.syntaxtree.Constructor;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
@ -331,15 +332,17 @@ public class Signature {
case "EWC":
System.out.println("EWC---Signature");
SuperWildcardType ewc = (SuperWildcardType) t;
ExtendsWildcardType ewc = (ExtendsWildcardType) t;
String esigInner = ewc.getInnerType().acceptTV(new TypeToSignature());
System.out.println(esigInner);
if(ewc.getInnerType() instanceof TypePlaceholder) {
sv.visitTypeArgument('+').visitTypeVariable(esigInner.substring(1, esigInner.length()));
} else if(ewc.getInnerType() instanceof RefType) {
if(esigInner.contains("$$")) {
sv.visitTypeArgument('+').visitInterface().visitClassType(esigInner.substring(1,esigInner.length()));
}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 {
sv.visitTypeArgument('+').visitTypeVariable(esigInner.substring(1));

View File

@ -20,6 +20,7 @@ public class KindOfLambda implements StatementVisitor{
private List<RefTypeOrTPHOrWildcardOrGeneric> argumentList = new ArrayList<>();
private ArrayList<String> usedVars = new ArrayList<>();
private boolean hasThis = false;
private ArrayList<String> definedLocals = new ArrayList<>();
public KindOfLambda(LambdaExpression lambdaExpression) {
this.params = lambdaExpression.params;
@ -110,7 +111,7 @@ public class KindOfLambda implements StatementVisitor{
@Override
public void visit(LocalVar localVar) {
if(!contain(params, localVar.name)) {
if(!contain(params, localVar.name) && !definedLocals.contains(localVar.name)) {
argumentList.add(localVar.getType());
if(hasThis) {
usedVars.add(1, localVar.name);
@ -134,8 +135,7 @@ public class KindOfLambda implements StatementVisitor{
@Override
public void visit(LocalVarDecl localVarDecl) {
// TODO Auto-generated method stub
definedLocals.add(localVarDecl.getName());
}
@Override

View File

@ -136,9 +136,8 @@ public class Simplify {
// put the generated constraint and its equal set into result set
result.put(constraint, eq);
constraints.clear();
allTypes.clear();
}
allTypes.clear();
}
}
// build an equal set that contains all types

View File

@ -16,7 +16,7 @@ public class Constructor extends Method {
//TODO: Constructor braucht ein super-Statement
public Constructor(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList parameterList, Block codeInsideConstructor,
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);
}

View File

@ -33,8 +33,8 @@ public class MatrixOpTest {
compiler = new JavaTXCompiler(fileToTest);
pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/";
compiler.generateBytecode(pathToClassFile);
// loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
// classToTest = loader.loadClass("MatrixOP");
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
classToTest = loader.loadClass("MatrixOP");
/*
Vector<Vector<Integer>> vv = new Vector<Vector<Integer>>();
Vector<Integer> v1 = new Vector<Integer> ();

View File

@ -25,11 +25,11 @@ public class YTest {
public void generateBC() throws Exception {
path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Y.jav";
fileToTest = new File(path);
compiler = new JavaTXCompiler(fileToTest);
compiler.generateBytecode(System.getProperty("user.dir")+"/testBytecode/generatedBC/");
pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/";
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
classToTest = loader.loadClass("Y");
// compiler = new JavaTXCompiler(fileToTest);
// compiler.generateBytecode(System.getProperty("user.dir")+"/testBytecode/generatedBC/");
// pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/";
// loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
// classToTest = loader.loadClass("Y");
/*
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();

View File

@ -20,4 +20,3 @@ public class OLMain {
return ol.m(x);
}
}