DUP Befehl fuer UnaryExpr

This commit is contained in:
Fayez Abu Alia 2018-08-06 16:14:09 +02:00
parent dc535ad9b7
commit f99d4dae5a
4 changed files with 33 additions and 21 deletions

View File

@ -69,11 +69,7 @@ public class BytecodeGenMethod implements StatementVisitor {
private SourceFile sf; private SourceFile sf;
private IStatement statement = null; private IStatement statement = null;
private boolean returnParentOfUnary = false; private boolean needDUP = false;
// speichert, ob der Argument RefType ist.
private List<Boolean> argList = new LinkedList<>();
// private int numMethodCalls = 0;
// for tests ** // for tests **
private String fieldName; private String fieldName;
@ -163,7 +159,7 @@ public class BytecodeGenMethod implements StatementVisitor {
// wird die lokale Var geladen. Sonst wird zuerst die lokale Var geladen. // wird die lokale Var geladen. Sonst wird zuerst die lokale Var geladen.
System.out.println(localVar.name); System.out.println(localVar.name);
mv.visitVarInsn(Opcodes.ALOAD, paramsAndLocals.get(localVar.name)); mv.visitVarInsn(Opcodes.ALOAD, paramsAndLocals.get(localVar.name));
if (isBinaryExp) { if (isBinaryExp) {
doUnboxing(getResolvedType(localVar.getType())); doUnboxing(getResolvedType(localVar.getType()));
} }
@ -199,6 +195,7 @@ public class BytecodeGenMethod implements StatementVisitor {
doBoxing(binaryType); doBoxing(binaryType);
isBinaryExp = false; isBinaryExp = false;
} }
System.out.println("ASSIGN TYPE R: " + getResolvedType(assign.rightSide.getType())); System.out.println("ASSIGN TYPE R: " + getResolvedType(assign.rightSide.getType()));
String typeOfRightSide = getResolvedType(assign.rightSide.getType()); String typeOfRightSide = getResolvedType(assign.rightSide.getType());
if(typeOfRightSide.contains("<")) { if(typeOfRightSide.contains("<")) {
@ -229,12 +226,18 @@ public class BytecodeGenMethod implements StatementVisitor {
// this case for while loops // this case for while loops
if (statement instanceof LoopStmt) if (statement instanceof LoopStmt)
mv.visitLabel(endLabel); mv.visitLabel(endLabel);
if(binary.lexpr instanceof UnaryExpr)
needDUP = true;
binary.lexpr.accept(this); binary.lexpr.accept(this);
if (!lexpType.equals(rexpType) && !lexpType.equals(largerType)) if (!lexpType.equals(rexpType) && !lexpType.equals(largerType))
doCast(lexpType, largerType); doCast(lexpType, largerType);
if(binary.rexpr instanceof UnaryExpr)
needDUP = true;
binary.rexpr.accept(this); binary.rexpr.accept(this);
if (!lexpType.equals(rexpType) && !rexpType.equals(largerType)) if (!lexpType.equals(rexpType) && !rexpType.equals(largerType))
@ -713,10 +716,6 @@ public class BytecodeGenMethod implements StatementVisitor {
String mDesc = ""; String mDesc = "";
List<Boolean> argListMethCall = new LinkedList<>(); List<Boolean> argListMethCall = new LinkedList<>();
if(methodRefl == null) { if(methodRefl == null) {
// Alle Argumente sind nicht Primitiv
for(Expression arg : methodCall.arglist.getArguments()) {
argList.add(true);
}
MethodFromMethodCall method = new MethodFromMethodCall(methodCall.arglist, methodCall.getType(), MethodFromMethodCall method = new MethodFromMethodCall(methodCall.arglist, methodCall.getType(),
receiverName, genericsAndBoundsMethod, genericsAndBounds); receiverName, genericsAndBoundsMethod, genericsAndBounds);
mDesc = method.accept(new DescriptorToString(resultSet)); mDesc = method.accept(new DescriptorToString(resultSet));
@ -832,7 +831,6 @@ public class BytecodeGenMethod implements StatementVisitor {
@Override @Override
public void visit(UnaryExpr unaryExpr) { public void visit(UnaryExpr unaryExpr) {
unaryExpr.expr.accept(this); unaryExpr.expr.accept(this);
Operation op = unaryExpr.operation; Operation op = unaryExpr.operation;
@ -868,13 +866,14 @@ public class BytecodeGenMethod implements StatementVisitor {
// Für Byte und Short muss noch einen Cast geben i2b, i2s // Für Byte und Short muss noch einen Cast geben i2b, i2s
// das wird später gemacht, da bytecode für cast noch nicht erzeugt wird // das wird später gemacht, da bytecode für cast noch nicht erzeugt wird
if (isIncOrDec && (unaryExpr.expr instanceof LocalVar) && !returnParentOfUnary) { if (isIncOrDec && (unaryExpr.expr instanceof LocalVar)) {
if(needDUP) {
mv.visitInsn(Opcodes.DUP);
needDUP = false;
}
LocalVar local = (LocalVar) unaryExpr.expr; LocalVar local = (LocalVar) unaryExpr.expr;
mv.visitVarInsn(Opcodes.ASTORE, paramsAndLocals.get(local.name)); mv.visitVarInsn(Opcodes.ASTORE, paramsAndLocals.get(local.name));
} }
if(returnParentOfUnary)
returnParentOfUnary = false;
} }
private void doVisitNegIns(String typeOfUnary) { private void doVisitNegIns(String typeOfUnary) {
@ -922,7 +921,7 @@ public class BytecodeGenMethod implements StatementVisitor {
isBinaryExp = statement.isExprBinary(); isBinaryExp = statement.isExprBinary();
if(aReturn.retexpr instanceof UnaryExpr) if(aReturn.retexpr instanceof UnaryExpr)
returnParentOfUnary = true; needDUP = true;
aReturn.retexpr.accept(this); aReturn.retexpr.accept(this);
@ -1184,6 +1183,8 @@ public class BytecodeGenMethod implements StatementVisitor {
for (Expression al : argumentList.getArguments()) { for (Expression al : argumentList.getArguments()) {
statement = new ArgumentExpr(al); statement = new ArgumentExpr(al);
isBinaryExp = statement.isExprBinary(); isBinaryExp = statement.isExprBinary();
if(al instanceof UnaryExpr)
needDUP = true;
al.accept(this); al.accept(this);
//TODO: teste, ob man das für unary braucht //TODO: teste, ob man das für unary braucht
if (isBinaryExp) { if (isBinaryExp) {

View File

@ -39,5 +39,12 @@ public class BinaryTest {
Integer res = (Integer) m2.invoke(instanceOfClass, 2,3); Integer res = (Integer) m2.invoke(instanceOfClass, 2,3);
assertEquals(6, res); assertEquals(6, res);
} }
@Test
public void testM3() throws Exception {
Method m3 = classToTest.getDeclaredMethod("m3", Integer.class);
Integer res = (Integer) m3.invoke(instanceOfClass, 2);
assertEquals(4, res);
}
} }

View File

@ -28,9 +28,9 @@ public class MergeTest {
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("Merge"); // classToTest = loader.loadClass("Merge");
//instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); //instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
//Method m = classToTest.getDeclaredMethod("m"); //Method m = classToTest.getDeclaredMethod("m");

View File

@ -10,4 +10,8 @@ public class BinaryInMeth {
m2(a,b){ m2(a,b){
return m(a+b); return m(a+b);
} }
m3(a) {
return m(++a);
}
} }