Search in sources :

Example 1 with ReturnExpression

use of de.mirkosertic.bytecoder.ssa.ReturnExpression in project Bytecoder by mirkosertic.

the class InlineGotoOptimizerTest method testSimpleFlow.

@Test
public void testSimpleFlow() {
    Program theProgram = new Program();
    ControlFlowGraph theGraph = theProgram.getControlFlowGraph();
    RegionNode theNode = theGraph.createAt(BytecodeOpcodeAddress.START_AT_ZERO, RegionNode.BlockType.NORMAL);
    RegionNode theNode2 = theGraph.createAt(new BytecodeOpcodeAddress(20), RegionNode.BlockType.NORMAL);
    theNode.getExpressions().add(new GotoExpression(new BytecodeOpcodeAddress(20)));
    theNode.addSuccessor(theNode2);
    theNode2.getExpressions().add(new ReturnExpression());
    theGraph.calculateReachabilityAndMarkBackEdges();
    InlineGotoOptimizer theOptimizer = new InlineGotoOptimizer();
    theOptimizer.optimize(theGraph, null);
    Assert.assertEquals(1, theGraph.getKnownNodes().size(), 0);
    Assert.assertEquals(1, theGraph.getDominatedNodes().size(), 0);
    Assert.assertTrue(theGraph.getKnownNodes().contains(theNode));
    Assert.assertEquals(1, theNode.getExpressions().size(), 0);
    Assert.assertTrue(theNode.getExpressions().toList().get(0) instanceof ReturnExpression);
    Assert.assertEquals(0, theNode.getSuccessors().size(), 0);
    Relooper theRelooper = new Relooper();
    Relooper.Block theRoot = theRelooper.reloop(theGraph);
}
Also used : ReturnExpression(de.mirkosertic.bytecoder.ssa.ReturnExpression) Program(de.mirkosertic.bytecoder.ssa.Program) GotoExpression(de.mirkosertic.bytecoder.ssa.GotoExpression) Relooper(de.mirkosertic.bytecoder.relooper.Relooper) ControlFlowGraph(de.mirkosertic.bytecoder.ssa.ControlFlowGraph) BytecodeOpcodeAddress(de.mirkosertic.bytecoder.core.BytecodeOpcodeAddress) RegionNode(de.mirkosertic.bytecoder.ssa.RegionNode) Test(org.junit.Test)

Example 2 with ReturnExpression

use of de.mirkosertic.bytecoder.ssa.ReturnExpression in project Bytecoder by mirkosertic.

the class InlineGotoOptimizerTest method testIf.

@Test
public void testIf() {
    Program theProgram = new Program();
    ControlFlowGraph theGraph = theProgram.getControlFlowGraph();
    RegionNode theNode = theGraph.createAt(BytecodeOpcodeAddress.START_AT_ZERO, RegionNode.BlockType.NORMAL);
    RegionNode theNode1 = theGraph.createAt(new BytecodeOpcodeAddress(20), RegionNode.BlockType.NORMAL);
    RegionNode theNode2 = theGraph.createAt(new BytecodeOpcodeAddress(40), RegionNode.BlockType.NORMAL);
    RegionNode theNode3 = theGraph.createAt(new BytecodeOpcodeAddress(60), RegionNode.BlockType.NORMAL);
    theNode.addSuccessor(theNode1);
    theNode.addSuccessor(theNode2);
    ExpressionList theTrueClause = new ExpressionList();
    theTrueClause.add(new GotoExpression(new BytecodeOpcodeAddress(20)));
    IFExpression theIF = new IFExpression(new BytecodeOpcodeAddress(0), new BytecodeOpcodeAddress(20), new IntegerValue(10), theTrueClause);
    theNode.getExpressions().add(theIF);
    theNode.getExpressions().add(new GotoExpression(new BytecodeOpcodeAddress(40)));
    theNode1.addSuccessor(theNode3);
    theNode1.getExpressions().add(new GotoExpression(new BytecodeOpcodeAddress(60)));
    theNode2.addSuccessor(theNode3);
    theNode2.getExpressions().add(new GotoExpression(new BytecodeOpcodeAddress(60)));
    theNode3.getExpressions().add(new ReturnExpression());
    theGraph.calculateReachabilityAndMarkBackEdges();
    InlineGotoOptimizer theOptimizer = new InlineGotoOptimizer();
    theOptimizer.optimize(theGraph, null);
    System.out.println(theGraph.toDOT());
}
Also used : ReturnExpression(de.mirkosertic.bytecoder.ssa.ReturnExpression) Program(de.mirkosertic.bytecoder.ssa.Program) GotoExpression(de.mirkosertic.bytecoder.ssa.GotoExpression) ControlFlowGraph(de.mirkosertic.bytecoder.ssa.ControlFlowGraph) IntegerValue(de.mirkosertic.bytecoder.ssa.IntegerValue) BytecodeOpcodeAddress(de.mirkosertic.bytecoder.core.BytecodeOpcodeAddress) IFExpression(de.mirkosertic.bytecoder.ssa.IFExpression) RegionNode(de.mirkosertic.bytecoder.ssa.RegionNode) ExpressionList(de.mirkosertic.bytecoder.ssa.ExpressionList) Test(org.junit.Test)

Example 3 with ReturnExpression

use of de.mirkosertic.bytecoder.ssa.ReturnExpression in project Bytecoder by mirkosertic.

the class Relooper method debugPrint.

private void debugPrint(PrintStream aStream, int aInset, ExpressionList aExpressionList) {
    for (Expression theExpression : aExpressionList.toList()) {
        if (theExpression instanceof BreakExpression) {
            BreakExpression theBreak = (BreakExpression) theExpression;
            printInset(aStream, aInset);
            aStream.println("Break " + theBreak.blockToBreak().name() + " and jump to " + theBreak.jumpTarget().getAddress());
        } else if (theExpression instanceof ContinueExpression) {
            ContinueExpression theContinue = (ContinueExpression) theExpression;
            printInset(aStream, aInset);
            aStream.println("Continue at " + theContinue.labelToReturnTo().name());
        } else if (theExpression instanceof ReturnExpression) {
            printInset(aStream, aInset);
            aStream.println("Return");
        } else if (theExpression instanceof GotoExpression) {
            throw new IllegalStateException("Goto should have been removed!");
        }
    }
}
Also used : ReturnExpression(de.mirkosertic.bytecoder.ssa.ReturnExpression) GotoExpression(de.mirkosertic.bytecoder.ssa.GotoExpression) ContinueExpression(de.mirkosertic.bytecoder.ssa.ContinueExpression) Expression(de.mirkosertic.bytecoder.ssa.Expression) GotoExpression(de.mirkosertic.bytecoder.ssa.GotoExpression) ReturnExpression(de.mirkosertic.bytecoder.ssa.ReturnExpression) BreakExpression(de.mirkosertic.bytecoder.ssa.BreakExpression) ContinueExpression(de.mirkosertic.bytecoder.ssa.ContinueExpression) BreakExpression(de.mirkosertic.bytecoder.ssa.BreakExpression)

Example 4 with ReturnExpression

use of de.mirkosertic.bytecoder.ssa.ReturnExpression in project Bytecoder by mirkosertic.

the class OpenCLWriter method writeExpressions.

private void writeExpressions(ExpressionList aList) {
    for (Expression theExpression : aList.toList()) {
        if (options.isDebugOutput()) {
            String theComment = theExpression.getComment();
            if (theComment != null && !theComment.isEmpty()) {
                print("// ");
                println(theComment);
            }
        }
        if (theExpression instanceof VariableAssignmentExpression) {
            VariableAssignmentExpression theInit = (VariableAssignmentExpression) theExpression;
            Variable theVariable = theInit.getVariable();
            Value theValue = theInit.getValue();
            if (theVariable.resolveType().isObject() && theValue instanceof InvocationExpression) {
                print(toType(theVariable.resolveType(), false));
                print(" ");
                print(theVariable.getName());
                print("_temp = ");
                printValue(theValue);
                println(";");
                print(theVariable.getName());
                print(" = &");
                print(theVariable.getName());
                println("_temp;");
            } else {
                print(theVariable.getName());
                print(" = ");
                printValue(theValue);
                println(";");
            }
        } else if (theExpression instanceof ArrayStoreExpression) {
            ArrayStoreExpression theStore = (ArrayStoreExpression) theExpression;
            List<Value> theIncomingData = theStore.incomingDataFlows();
            Value theArray = theIncomingData.get(0);
            Value theIndex = theIncomingData.get(1);
            Value theValue = theIncomingData.get(2);
            printValue(theArray);
            print("[");
            printValue(theIndex);
            print("] = ");
            printValue(theValue);
            println(";");
        } else if (theExpression instanceof IFExpression) {
            IFExpression theE = (IFExpression) theExpression;
            print("if ");
            printValue(theE.incomingDataFlows().get(0));
            println(" {");
            withDeeperIndent().writeExpressions(theE.getExpressions());
            println("}");
        } else if (theExpression instanceof BreakExpression) {
            BreakExpression theBreak = (BreakExpression) theExpression;
            if (theBreak.isSetLabelRequired()) {
                print("$__label__ = ");
                print(theBreak.jumpTarget().getAddress());
                println(";");
            }
            if (!theBreak.isSilent()) {
                print("goto $");
                print(theBreak.blockToBreak().name());
                println("_next;");
            }
        } else if (theExpression instanceof ContinueExpression) {
            ContinueExpression theContinue = (ContinueExpression) theExpression;
            print("$__label__ = ");
            print(theContinue.jumpTarget().getAddress());
            println(";");
            print("goto $");
            print(theContinue.labelToReturnTo().name());
            println(";");
        } else if (theExpression instanceof ReturnExpression) {
            println("return;");
        } else if (theExpression instanceof PutFieldExpression) {
            PutFieldExpression thePutField = (PutFieldExpression) theExpression;
            List<Value> theIncomingData = thePutField.incomingDataFlows();
            Value theTarget = theIncomingData.get(0);
            BytecodeFieldRefConstant theField = thePutField.getField();
            Value thevalue = theIncomingData.get(1);
            printValue(theTarget);
            printInstanceFieldReference(theField);
            print(" = ");
            printValue(thevalue);
            println(";");
        } else if (theExpression instanceof ReturnValueExpression) {
            ReturnValueExpression theReturn = (ReturnValueExpression) theExpression;
            List<Value> theIncomingData = theReturn.incomingDataFlows();
            print("return ");
            printValue(theIncomingData.get(0));
            println(";");
        } else {
            throw new IllegalArgumentException("Not supported. " + theExpression);
        }
    }
}
Also used : Variable(de.mirkosertic.bytecoder.ssa.Variable) InvocationExpression(de.mirkosertic.bytecoder.ssa.InvocationExpression) IFExpression(de.mirkosertic.bytecoder.ssa.IFExpression) PutFieldExpression(de.mirkosertic.bytecoder.ssa.PutFieldExpression) BreakExpression(de.mirkosertic.bytecoder.ssa.BreakExpression) VariableAssignmentExpression(de.mirkosertic.bytecoder.ssa.VariableAssignmentExpression) ReturnValueExpression(de.mirkosertic.bytecoder.ssa.ReturnValueExpression) ReturnExpression(de.mirkosertic.bytecoder.ssa.ReturnExpression) ArrayStoreExpression(de.mirkosertic.bytecoder.ssa.ArrayStoreExpression) ContinueExpression(de.mirkosertic.bytecoder.ssa.ContinueExpression) InvokeVirtualMethodExpression(de.mirkosertic.bytecoder.ssa.InvokeVirtualMethodExpression) TypeConversionExpression(de.mirkosertic.bytecoder.ssa.TypeConversionExpression) CompareExpression(de.mirkosertic.bytecoder.ssa.CompareExpression) DirectInvokeMethodExpression(de.mirkosertic.bytecoder.ssa.DirectInvokeMethodExpression) InvocationExpression(de.mirkosertic.bytecoder.ssa.InvocationExpression) VariableAssignmentExpression(de.mirkosertic.bytecoder.ssa.VariableAssignmentExpression) InvokeStaticMethodExpression(de.mirkosertic.bytecoder.ssa.InvokeStaticMethodExpression) ReturnExpression(de.mirkosertic.bytecoder.ssa.ReturnExpression) ReturnValueExpression(de.mirkosertic.bytecoder.ssa.ReturnValueExpression) ArrayStoreExpression(de.mirkosertic.bytecoder.ssa.ArrayStoreExpression) BinaryExpression(de.mirkosertic.bytecoder.ssa.BinaryExpression) GetFieldExpression(de.mirkosertic.bytecoder.ssa.GetFieldExpression) ContinueExpression(de.mirkosertic.bytecoder.ssa.ContinueExpression) IFExpression(de.mirkosertic.bytecoder.ssa.IFExpression) ArrayEntryExpression(de.mirkosertic.bytecoder.ssa.ArrayEntryExpression) Expression(de.mirkosertic.bytecoder.ssa.Expression) PutFieldExpression(de.mirkosertic.bytecoder.ssa.PutFieldExpression) BreakExpression(de.mirkosertic.bytecoder.ssa.BreakExpression) DoubleValue(de.mirkosertic.bytecoder.ssa.DoubleValue) Value(de.mirkosertic.bytecoder.ssa.Value) IntegerValue(de.mirkosertic.bytecoder.ssa.IntegerValue) LongValue(de.mirkosertic.bytecoder.ssa.LongValue) FloatValue(de.mirkosertic.bytecoder.ssa.FloatValue) ExpressionList(de.mirkosertic.bytecoder.ssa.ExpressionList) List(java.util.List) BytecodeFieldRefConstant(de.mirkosertic.bytecoder.core.BytecodeFieldRefConstant)

Example 5 with ReturnExpression

use of de.mirkosertic.bytecoder.ssa.ReturnExpression in project Bytecoder by mirkosertic.

the class WASMSSAWriter method writeExpression.

private void writeExpression(Expression aExpression) {
    if (options.isDebugOutput()) {
        String theComment = aExpression.getComment();
        if (theComment != null && !theComment.isEmpty()) {
            print(";; ");
            println(theComment);
        }
    }
    if (aExpression instanceof CheckCastExpression) {
        return;
    }
    if (aExpression instanceof ReturnExpression) {
        writeReturnExpression((ReturnExpression) aExpression);
        return;
    }
    if (aExpression instanceof VariableAssignmentExpression) {
        writeInitVariableExpression((VariableAssignmentExpression) aExpression);
        return;
    }
    if (aExpression instanceof DirectInvokeMethodExpression) {
        writeDirectMethodInvokeExpression((DirectInvokeMethodExpression) aExpression);
        return;
    }
    if (aExpression instanceof IFExpression) {
        writeIFExpression((IFExpression) aExpression);
        return;
    }
    if (aExpression instanceof GotoExpression) {
        writeGotoExpression((GotoExpression) aExpression);
        return;
    }
    if (aExpression instanceof ReturnValueExpression) {
        writeReturnExpression((ReturnValueExpression) aExpression);
        return;
    }
    if (aExpression instanceof PutFieldExpression) {
        writePutFieldExpression((PutFieldExpression) aExpression);
        return;
    }
    if (aExpression instanceof SetMemoryLocationExpression) {
        writeSetMemoryLocationExpression((SetMemoryLocationExpression) aExpression);
        return;
    }
    if (aExpression instanceof PutStaticExpression) {
        writePutStaticExpression((PutStaticExpression) aExpression);
        return;
    }
    if (aExpression instanceof InvokeStaticMethodExpression) {
        writeInvokeStaticExpression((InvokeStaticMethodExpression) aExpression);
        return;
    }
    if (aExpression instanceof ThrowExpression) {
        writeThrowExpression((ThrowExpression) aExpression);
        return;
    }
    if (aExpression instanceof ArrayStoreExpression) {
        writeArrayStoreExpression((ArrayStoreExpression) aExpression);
        return;
    }
    if (aExpression instanceof InvokeVirtualMethodExpression) {
        writeInvokeVirtualExpression((InvokeVirtualMethodExpression) aExpression);
        return;
    }
    if (aExpression instanceof TableSwitchExpression) {
        writeTableSwitchExpression((TableSwitchExpression) aExpression);
        return;
    }
    if (aExpression instanceof LookupSwitchExpression) {
        writeLookupSwitchExpression((LookupSwitchExpression) aExpression);
        return;
    }
    if (aExpression instanceof UnreachableExpression) {
        writeUnreachable((UnreachableExpression) aExpression);
        return;
    }
    if (aExpression instanceof BreakExpression) {
        BreakExpression theBreak = (BreakExpression) aExpression;
        if (theBreak.isSetLabelRequired()) {
            print("(set_local $__label__ (i32.const ");
            print(theBreak.jumpTarget().getAddress());
            println("))");
        }
        if (!theBreak.isSilent()) {
            print("(br $");
            print(theBreak.blockToBreak().name());
            println(")");
        }
        return;
    }
    if (aExpression instanceof ContinueExpression) {
        ContinueExpression theContinue = (ContinueExpression) aExpression;
        print("(set_local $__label__ (i32.const ");
        print(theContinue.jumpTarget().getAddress());
        println("))");
        print("(br $");
        print(theContinue.labelToReturnTo().name());
        println("_inner)");
        return;
    }
    throw new IllegalStateException("Not supported : " + aExpression);
}
Also used : LookupSwitchExpression(de.mirkosertic.bytecoder.ssa.LookupSwitchExpression) ThrowExpression(de.mirkosertic.bytecoder.ssa.ThrowExpression) IFExpression(de.mirkosertic.bytecoder.ssa.IFExpression) PutStaticExpression(de.mirkosertic.bytecoder.ssa.PutStaticExpression) DirectInvokeMethodExpression(de.mirkosertic.bytecoder.ssa.DirectInvokeMethodExpression) PutFieldExpression(de.mirkosertic.bytecoder.ssa.PutFieldExpression) InvokeStaticMethodExpression(de.mirkosertic.bytecoder.ssa.InvokeStaticMethodExpression) BreakExpression(de.mirkosertic.bytecoder.ssa.BreakExpression) VariableAssignmentExpression(de.mirkosertic.bytecoder.ssa.VariableAssignmentExpression) TableSwitchExpression(de.mirkosertic.bytecoder.ssa.TableSwitchExpression) ReturnValueExpression(de.mirkosertic.bytecoder.ssa.ReturnValueExpression) ReturnExpression(de.mirkosertic.bytecoder.ssa.ReturnExpression) ArrayStoreExpression(de.mirkosertic.bytecoder.ssa.ArrayStoreExpression) GotoExpression(de.mirkosertic.bytecoder.ssa.GotoExpression) CheckCastExpression(de.mirkosertic.bytecoder.ssa.CheckCastExpression) InvokeVirtualMethodExpression(de.mirkosertic.bytecoder.ssa.InvokeVirtualMethodExpression) ContinueExpression(de.mirkosertic.bytecoder.ssa.ContinueExpression) SetMemoryLocationExpression(de.mirkosertic.bytecoder.ssa.SetMemoryLocationExpression) UnreachableExpression(de.mirkosertic.bytecoder.ssa.UnreachableExpression)

Aggregations

ReturnExpression (de.mirkosertic.bytecoder.ssa.ReturnExpression)10 GotoExpression (de.mirkosertic.bytecoder.ssa.GotoExpression)8 BytecodeOpcodeAddress (de.mirkosertic.bytecoder.core.BytecodeOpcodeAddress)6 ControlFlowGraph (de.mirkosertic.bytecoder.ssa.ControlFlowGraph)6 Program (de.mirkosertic.bytecoder.ssa.Program)6 RegionNode (de.mirkosertic.bytecoder.ssa.RegionNode)6 Test (org.junit.Test)6 BreakExpression (de.mirkosertic.bytecoder.ssa.BreakExpression)4 ContinueExpression (de.mirkosertic.bytecoder.ssa.ContinueExpression)4 IFExpression (de.mirkosertic.bytecoder.ssa.IFExpression)4 ArrayStoreExpression (de.mirkosertic.bytecoder.ssa.ArrayStoreExpression)3 DirectInvokeMethodExpression (de.mirkosertic.bytecoder.ssa.DirectInvokeMethodExpression)3 Expression (de.mirkosertic.bytecoder.ssa.Expression)3 ExpressionList (de.mirkosertic.bytecoder.ssa.ExpressionList)3 IntegerValue (de.mirkosertic.bytecoder.ssa.IntegerValue)3 InvokeStaticMethodExpression (de.mirkosertic.bytecoder.ssa.InvokeStaticMethodExpression)3 InvokeVirtualMethodExpression (de.mirkosertic.bytecoder.ssa.InvokeVirtualMethodExpression)3 BytecodeFieldRefConstant (de.mirkosertic.bytecoder.core.BytecodeFieldRefConstant)2 Relooper (de.mirkosertic.bytecoder.relooper.Relooper)2 ArrayEntryExpression (de.mirkosertic.bytecoder.ssa.ArrayEntryExpression)2