use of de.mirkosertic.bytecoder.ssa.Expression in project Bytecoder by mirkosertic.
the class RedundantAssignmentOptimizer method visit.
@Override
protected void visit(ControlFlowGraph aGraph, ExpressionList aList, Expression aExpression, BytecodeLinkerContext aLinkerContext) {
// Check if a variable assignment is before the current expression
Expression theBefore = aList.predecessorOf(aExpression);
if (theBefore instanceof VariableAssignmentExpression) {
VariableAssignmentExpression theAssignment = (VariableAssignmentExpression) theBefore;
Variable theVariable = theAssignment.getVariable();
Value theValue = theAssignment.getValue();
// Check if there is only one data flow
List<Edge> theDataEdges = theVariable.outgoingEdges(DataFlowEdgeType.filter()).collect(Collectors.toList());
if (theDataEdges.size() == 1) {
List<Value> theIncomingData = aExpression.incomingDataFlows();
if (theIncomingData.contains(theVariable)) {
aExpression.replaceIncomingDataEdge(theVariable, theValue);
aList.remove(theAssignment);
aGraph.getProgram().deleteVariable(theVariable);
}
}
}
}
use of de.mirkosertic.bytecoder.ssa.Expression 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!");
}
}
}
use of de.mirkosertic.bytecoder.ssa.Expression in project Bytecoder by mirkosertic.
the class Relooper method replaceGotosIn.
private void replaceGotosIn(Stack<Block> aTraversalStack, Block aBlock) {
if (aBlock == null) {
return;
}
if (aBlock instanceof SimpleBlock) {
SimpleBlock theSimple = (SimpleBlock) aBlock;
aTraversalStack.push(theSimple);
RegionNode theInternalLabel = theSimple.internalLabel();
replaceGotosIn(aTraversalStack, theSimple, theInternalLabel, theInternalLabel.getExpressions());
replaceGotosIn(aTraversalStack, theSimple.next());
aTraversalStack.pop();
RegionNode theNode = theSimple.internalLabel;
Expression theLastExpression = theNode.getExpressions().lastExpression();
// can be silent, they only need to set the __label__ variable
if (theLastExpression instanceof BreakExpression) {
BreakExpression theBreak = (BreakExpression) theLastExpression;
if (Objects.equals(theBreak.blockToBreak().name(), theSimple.label().name())) {
theBreak.silent();
}
}
return;
}
if (aBlock instanceof LoopBlock) {
LoopBlock theLoop = (LoopBlock) aBlock;
aTraversalStack.push(theLoop);
replaceGotosIn(aTraversalStack, theLoop.inner());
replaceGotosIn(aTraversalStack, theLoop.next());
aTraversalStack.pop();
return;
}
if (aBlock instanceof MultipleBlock) {
MultipleBlock theMultiple = (MultipleBlock) aBlock;
aTraversalStack.push(theMultiple);
for (Block theHandler : theMultiple.handlers()) {
replaceGotosIn(aTraversalStack, theHandler);
}
replaceGotosIn(aTraversalStack, theMultiple.next());
aTraversalStack.pop();
return;
}
throw new IllegalStateException("Don't know how to handle " + aBlock);
}
use of de.mirkosertic.bytecoder.ssa.Expression in project Bytecoder by mirkosertic.
the class Relooper method replaceGotosIn.
private void replaceGotosIn(Stack<Block> aTraversalStack, SimpleBlock aCurrent, RegionNode aLabel, ExpressionList aList) {
for (Expression theExpression : aList.toList()) {
if (theExpression instanceof ExpressionListContainer) {
ExpressionListContainer theContainer = (ExpressionListContainer) theExpression;
for (ExpressionList theList : theContainer.getExpressionLists()) {
replaceGotosIn(aTraversalStack, aCurrent, aLabel, theList);
}
}
if (theExpression instanceof GotoExpression) {
GotoExpression theGoto = (GotoExpression) theExpression;
boolean theGotoFound = false;
// We search the successor edge
for (Map.Entry<RegionNode.Edge, RegionNode> theSuc : aLabel.getSuccessors().entrySet()) {
if (Objects.equals(theSuc.getValue().getStartAddress(), theGoto.getJumpTarget())) {
theGotoFound = true;
RegionNode theTarget = theSuc.getValue();
// We found the matching edge
if (theSuc.getKey().getType() == RegionNode.EdgeType.NORMAL) {
// We can only branch to the next block
// We search the whole hiararchy to find the right block to break out
boolean theSomethingFound = false;
for (int i = aTraversalStack.size() - 1; i >= 0; i--) {
Block theNestingBlock = aTraversalStack.get(i);
if (theNestingBlock.next() != null && theNestingBlock.next().entries().contains(theTarget)) {
theNestingBlock.requireLabel();
BreakExpression theBreak = new BreakExpression(theNestingBlock.label(), theTarget.getStartAddress());
aList.replace(theGoto, theBreak);
if (theNestingBlock.next() instanceof SimpleBlock && theNestingBlock.next().entries().size() == 1) {
theBreak.noSetRequired();
}
theSomethingFound = true;
break;
} else if (theNestingBlock.entries().contains(theTarget)) {
theNestingBlock.requireLabel();
ContinueExpression theContinue = new ContinueExpression(theNestingBlock.label(), theTarget.getStartAddress());
aList.replace(theGoto, theContinue);
theSomethingFound = true;
break;
}
}
if (!theSomethingFound) {
throw new IllegalStateException("Failed to jump to " + theTarget.getStartAddress().getAddress() + " from " + aCurrent.label().name() + " : no matching entry found!");
}
} else {
// We can only branch back into the known stack of nested blocks
boolean theSomethingFound = false;
for (Block theNestingBlock : aTraversalStack) {
if (theNestingBlock.entries().contains(theTarget)) {
theSomethingFound = true;
// We can return to the target in the hierarchy
theNestingBlock.requireLabel();
ContinueExpression theContinue = new ContinueExpression(theNestingBlock.label(), theTarget.getStartAddress());
aList.replace(theGoto, theContinue);
break;
}
}
if (!theSomethingFound) {
throw new IllegalStateException("No back edge target found for " + theTarget.getStartAddress().getAddress());
}
}
}
}
if (!theGotoFound) {
throw new IllegalStateException("No GOTO possible for " + theGoto.getJumpTarget().getAddress() + " in label " + aCurrent.label().name());
}
}
}
}
use of de.mirkosertic.bytecoder.ssa.Expression 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);
}
}
}
Aggregations