|
|
@ -23,108 +23,147 @@ import jode.*; |
|
|
|
public class CreateAssignExpression implements Transformation{ |
|
|
|
public class CreateAssignExpression implements Transformation{ |
|
|
|
|
|
|
|
|
|
|
|
public boolean transform(FlowBlock flow) { |
|
|
|
public boolean transform(FlowBlock flow) { |
|
|
|
|
|
|
|
if (!(flow.lastModified instanceof InstructionContainer) |
|
|
|
|
|
|
|
|| !(flow.lastModified.outer instanceof SequentialBlock) |
|
|
|
|
|
|
|
|| !(((InstructionContainer)flow.lastModified).getInstruction() |
|
|
|
|
|
|
|
instanceof StoreInstruction) |
|
|
|
|
|
|
|
|| !(((InstructionContainer)flow.lastModified).getInstruction() |
|
|
|
|
|
|
|
.isVoid())) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
return (createAssignOp(flow) || createAssignExpression(flow)); |
|
|
|
return (createAssignOp(flow) || createAssignExpression(flow)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public boolean createAssignOp(FlowBlock flow) { |
|
|
|
public boolean createAssignOp(FlowBlock flow) { |
|
|
|
Expression rightHandSide; |
|
|
|
|
|
|
|
StoreInstruction store; |
|
|
|
/* Situation: |
|
|
|
BinaryOperator binop; |
|
|
|
* |
|
|
|
InstructionContainer lastBlock; |
|
|
|
* (push loadstoreOps) <- not checked |
|
|
|
SequentialBlock opBlock; |
|
|
|
* sequBlock: |
|
|
|
SequentialBlock sequBlock; |
|
|
|
* dup |
|
|
|
boolean isExpression = false; |
|
|
|
* opBlock: |
|
|
|
try { |
|
|
|
* load(stack) * rightHandSide |
|
|
|
InstructionBlock ib; |
|
|
|
* (optional dup_x) |
|
|
|
lastBlock = (InstructionContainer) flow.lastModified; |
|
|
|
* store(stack) |
|
|
|
store = (StoreInstruction) lastBlock.getInstruction(); |
|
|
|
* |
|
|
|
|
|
|
|
* We transform it to: |
|
|
|
opBlock = (SequentialBlock) lastBlock.outer; |
|
|
|
* (push loadstoreOps) |
|
|
|
if (opBlock.subBlocks[1] != lastBlock) |
|
|
|
* rightHandSide |
|
|
|
|
|
|
|
* store(stack) *= (stack) |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* If the optional dup is present the store*= becomes non void. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
InstructionContainer lastBlock |
|
|
|
|
|
|
|
= (InstructionContainer) flow.lastModified; |
|
|
|
|
|
|
|
SequentialBlock opBlock = (SequentialBlock) lastBlock.outer; |
|
|
|
|
|
|
|
StoreInstruction store |
|
|
|
|
|
|
|
= (StoreInstruction) lastBlock.getInstruction(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boolean isAssignOp = false; |
|
|
|
|
|
|
|
if (opBlock.subBlocks[0] instanceof SpecialBlock) { |
|
|
|
|
|
|
|
SpecialBlock dup = (SpecialBlock) opBlock.subBlocks[0]; |
|
|
|
|
|
|
|
if (dup.type != SpecialBlock.DUP |
|
|
|
|
|
|
|
|| dup.depth != store.getLValueOperandCount() |
|
|
|
|
|
|
|
|| dup.count != store.getLValueType().stackSize() |
|
|
|
|
|
|
|
|| !(opBlock.outer instanceof SequentialBlock)) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
|
|
|
|
opBlock = (SequentialBlock) opBlock.outer; |
|
|
|
|
|
|
|
isAssignOp = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (opBlock.subBlocks[0] instanceof SpecialBlock) { |
|
|
|
if (!(opBlock.subBlocks[0] instanceof InstructionBlock)) |
|
|
|
SpecialBlock dup = (SpecialBlock) opBlock.subBlocks[0]; |
|
|
|
return false; |
|
|
|
if (dup.type != SpecialBlock.DUP |
|
|
|
|
|
|
|
|| dup.depth != store.getLValueOperandCount() |
|
|
|
|
|
|
|
|| dup.count != store.getLValueType().stackSize()) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
opBlock = (SequentialBlock) lastBlock.outer; |
|
|
|
|
|
|
|
isExpression = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ib = (InstructionBlock) opBlock.subBlocks[0]; |
|
|
|
InstructionBlock ib = (InstructionBlock) opBlock.subBlocks[0]; |
|
|
|
ComplexExpression binopExpr = |
|
|
|
|
|
|
|
(ComplexExpression) ib.getInstruction(); |
|
|
|
if (!(opBlock.outer instanceof SequentialBlock) |
|
|
|
binop = (BinaryOperator) binopExpr.getOperator(); |
|
|
|
|| !(opBlock.outer.getSubBlocks()[0] instanceof SpecialBlock)) |
|
|
|
if (binop.getOperatorIndex() < binop.ADD_OP || |
|
|
|
return false; |
|
|
|
binop.getOperatorIndex() >= binop.ASSIGN_OP) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rightHandSide = binopExpr.getSubExpressions()[1]; |
|
|
|
SequentialBlock sequBlock = (SequentialBlock) opBlock.outer; |
|
|
|
if (rightHandSide.isVoid()) |
|
|
|
SpecialBlock dup = (SpecialBlock) sequBlock.subBlocks[0]; |
|
|
|
return false; /* XXX */ |
|
|
|
if (dup.type != SpecialBlock.DUP |
|
|
|
|
|
|
|
|| dup.depth != 0 |
|
|
|
|
|
|
|
|| dup.count != store.getLValueOperandCount()) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
Operator load = (Operator) binopExpr.getSubExpressions()[0]; |
|
|
|
if (!(ib.getInstruction() instanceof ComplexExpression)) |
|
|
|
if (!store.matches(load)) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
ComplexExpression expr = (ComplexExpression) ib.getInstruction(); |
|
|
|
|
|
|
|
|
|
|
|
sequBlock = (SequentialBlock) opBlock.outer; |
|
|
|
int opIndex; |
|
|
|
|
|
|
|
Expression rightHandSide; |
|
|
|
|
|
|
|
|
|
|
|
SpecialBlock dup = (SpecialBlock) sequBlock.subBlocks[0]; |
|
|
|
if (expr.getOperator() instanceof BinaryOperator) { |
|
|
|
if (dup.type != SpecialBlock.DUP |
|
|
|
BinaryOperator binop = (BinaryOperator) expr.getOperator(); |
|
|
|
|| dup.depth != 0 |
|
|
|
|
|
|
|
|| dup.count != store.getLValueOperandCount()) |
|
|
|
opIndex = binop.getOperatorIndex(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (opIndex < binop.ADD_OP || opIndex >= binop.ASSIGN_OP |
|
|
|
|
|
|
|
|| !(expr.getSubExpressions()[0] instanceof Operator) |
|
|
|
|
|
|
|
|| !store.matches((Operator) expr.getSubExpressions()[0])) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} catch (ClassCastException ex) { |
|
|
|
|
|
|
|
return false; |
|
|
|
rightHandSide = expr.getSubExpressions()[1]; |
|
|
|
} catch (NullPointerException ex) { |
|
|
|
} else { |
|
|
|
return false; |
|
|
|
Expression simple = expr.simplifyString(); |
|
|
|
|
|
|
|
rightHandSide = simple; |
|
|
|
|
|
|
|
/* Now search for the leftmost operand ... */ |
|
|
|
|
|
|
|
ComplexExpression last = null; |
|
|
|
|
|
|
|
while (simple instanceof ComplexExpression |
|
|
|
|
|
|
|
&& simple.getOperator() instanceof StringAddOperator) { |
|
|
|
|
|
|
|
last = (ComplexExpression) simple; |
|
|
|
|
|
|
|
simple = last.getSubExpressions()[0]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ... check it ... */ |
|
|
|
|
|
|
|
if (last == null || !(simple instanceof Operator) |
|
|
|
|
|
|
|
|| !store.matches((Operator) simple)) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ... and remove it. */ |
|
|
|
|
|
|
|
if (last.getParent() != null) { |
|
|
|
|
|
|
|
((ComplexExpression)last.getParent()).getSubExpressions()[0] |
|
|
|
|
|
|
|
= last.getSubExpressions()[1]; |
|
|
|
|
|
|
|
} else |
|
|
|
|
|
|
|
rightHandSide = last.getSubExpressions()[1]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
opIndex = Operator.ADD_OP; |
|
|
|
} |
|
|
|
} |
|
|
|
((InstructionBlock)opBlock.subBlocks[0]) |
|
|
|
dup.removeBlock(); |
|
|
|
.setInstruction(rightHandSide); |
|
|
|
ib.setInstruction(rightHandSide); |
|
|
|
opBlock.moveDefinitions(sequBlock, opBlock); |
|
|
|
|
|
|
|
opBlock.replace(sequBlock); |
|
|
|
store.setOperatorIndex(store.OPASSIGN_OP+opIndex); |
|
|
|
|
|
|
|
|
|
|
|
store.setOperatorIndex(store.OPASSIGN_OP+binop.getOperatorIndex()); |
|
|
|
if (isAssignOp) |
|
|
|
store.setLValueType(binop.getType() |
|
|
|
store.makeNonVoid(); |
|
|
|
.intersection(store.getLValueType())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isExpression) |
|
|
|
|
|
|
|
lastBlock.setInstruction |
|
|
|
|
|
|
|
(new AssignOperator(store.getOperatorIndex(), store)); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
lastBlock.setInstruction(store); |
|
|
|
|
|
|
|
lastBlock.moveDefinitions(opBlock.subBlocks[1], lastBlock); |
|
|
|
|
|
|
|
lastBlock.replace(opBlock.subBlocks[1]); |
|
|
|
lastBlock.replace(opBlock.subBlocks[1]); |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public boolean createAssignExpression(FlowBlock flow) { |
|
|
|
public boolean createAssignExpression(FlowBlock flow) { |
|
|
|
StoreInstruction store; |
|
|
|
/* Situation: |
|
|
|
InstructionContainer lastBlock; |
|
|
|
* sequBlock: |
|
|
|
SequentialBlock sequBlock; |
|
|
|
* dup_X(lvalue_count) |
|
|
|
try { |
|
|
|
* store instruction |
|
|
|
lastBlock = (InstructionContainer) flow.lastModified; |
|
|
|
*/ |
|
|
|
store = (StoreInstruction) lastBlock.getInstruction(); |
|
|
|
InstructionContainer lastBlock |
|
|
|
|
|
|
|
= (InstructionContainer) flow.lastModified; |
|
|
|
sequBlock = (SequentialBlock) lastBlock.outer; |
|
|
|
SequentialBlock sequBlock = (SequentialBlock) lastBlock.outer; |
|
|
|
|
|
|
|
StoreInstruction store = (StoreInstruction) lastBlock.getInstruction(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (sequBlock.subBlocks[0] instanceof SpecialBlock) { |
|
|
|
|
|
|
|
|
|
|
|
SpecialBlock dup = (SpecialBlock) sequBlock.subBlocks[0]; |
|
|
|
SpecialBlock dup = (SpecialBlock) sequBlock.subBlocks[0]; |
|
|
|
if (dup.type != SpecialBlock.DUP |
|
|
|
if (dup.type != SpecialBlock.DUP |
|
|
|
|| dup.depth != store.getLValueOperandCount() |
|
|
|
|| dup.depth != store.getLValueOperandCount() |
|
|
|
|| dup.count != store.getLValueType().stackSize()) |
|
|
|
|| dup.count != store.getLValueType().stackSize()) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} catch (NullPointerException ex) { |
|
|
|
|
|
|
|
return false; |
|
|
|
dup.removeBlock(); |
|
|
|
} catch (ClassCastException ex) { |
|
|
|
store.makeNonVoid(); |
|
|
|
return false; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
lastBlock.setInstruction |
|
|
|
return false; |
|
|
|
(new AssignOperator(store.getOperatorIndex(), store)); |
|
|
|
|
|
|
|
lastBlock.moveDefinitions(sequBlock, lastBlock); |
|
|
|
|
|
|
|
lastBlock.replace(sequBlock); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|