finally blocks reworked.

allow jsr to occur outside of try block.
handle finally blocks, whose subroutine can't be merged


git-svn-id: https://svn.code.sf.net/p/jode/code/trunk@1101 379699f6-c40d-0410-875b-85095c16579e
branch_1_1
jochen 26 years ago
parent 7c1859a4df
commit 67e5bf4656
  1. 160
      jode/jode/flow/TransformExceptionHandlers.java.in

@ -26,6 +26,7 @@ import jode.expr.*;
import @COLLECTIONS@.TreeSet; import @COLLECTIONS@.TreeSet;
import @COLLECTIONS@.SortedSet; import @COLLECTIONS@.SortedSet;
import @COLLECTIONS@.Set;
import @COLLECTIONS@.Map; import @COLLECTIONS@.Map;
import @COLLECTIONS@.Iterator; import @COLLECTIONS@.Iterator;
import @COLLECTIONEXTRA@.Comparable; import @COLLECTIONEXTRA@.Comparable;
@ -233,23 +234,31 @@ public class TransformExceptionHandlers {
} }
} }
} }
/* Now we have a dangling JSR at the wrong place. /* Now we have a jump to the subroutine, that is wrong.
* We don't do anything, so that JSR will show up in * We complain here.
* the output.
*/ */
DescriptionBlock msg
= new DescriptionBlock("ERROR: GOTO FINALLY BLOCK!");
prev.appendBlock(msg);
} }
} }
public void checkAndRemoveJSR(FlowBlock tryFlow, FlowBlock subRoutine) { public void checkAndRemoveJSR(FlowBlock tryFlow, FlowBlock subRoutine,
int startOutExit, int endOutExit) {
boolean foundSub = false;
FlowBlock exitBlock = null;
Iterator iter = tryFlow.getSuccessors().iterator(); Iterator iter = tryFlow.getSuccessors().iterator();
dest_loop:
while (iter.hasNext()) { while (iter.hasNext()) {
FlowBlock dest = (FlowBlock) iter.next(); FlowBlock dest = (FlowBlock) iter.next();
if (dest == subRoutine) if (dest == subRoutine) {
continue; foundSub = true;
continue dest_loop;
Jump jumps = tryFlow.getJumps(dest); }
for ( ; jumps != null; jumps = jumps.next) { boolean isFirstJump = true;
for (Jump jumps = tryFlow.getJumps(dest);
jumps != null; jumps = jumps.next, isFirstJump = false) {
StructuredBlock prev = jumps.prev; StructuredBlock prev = jumps.prev;
if (prev instanceof ThrowBlock) { if (prev instanceof ThrowBlock) {
@ -290,6 +299,45 @@ public class TransformExceptionHandlers {
continue; continue;
} }
} }
if (isFirstJump) {
/* Now we have a jump that is not preceded by the
* jsr. There's a last chance: the jump jumps
* directly to a correct jsr instruction, which
* lies outside the try/catch block.
*/
System.err.println("tick0"
+exitBlock
+" ["+startOutExit+","+endOutExit
+"]->"+subRoutine.getAddr()
+" dest "+jumps.destination
+" dest preds "+jumps.destination.predecessors);
if (exitBlock == null
&& jumps.destination.predecessors.size() == 1
&& jumps.destination.getAddr() >= startOutExit
&& jumps.destination.getNextAddr() <= endOutExit) {
System.err.println("tick1");
jumps.destination.analyze(startOutExit, endOutExit);
StructuredBlock sb = jumps.destination.block;
if (sb instanceof SequentialBlock)
sb = sb.getSubBlocks()[0];
System.err.println("tick2");
if (sb instanceof JsrBlock
&& sb.getSubBlocks()[0] instanceof EmptyBlock
&& (sb.getSubBlocks()[0].jump.destination
== subRoutine)) {
System.err.println("tick3");
StructuredBlock jsrInner = sb.getSubBlocks()[0];
exitBlock = jumps.destination;
jumps.destination.removeSuccessor(jsrInner.jump);
jsrInner.removeJump();
sb.removeBlock();
continue dest_loop;
}
}
}
/* Now we have a jump with a wrong destination. /* Now we have a jump with a wrong destination.
* Complain! * Complain!
*/ */
@ -299,6 +347,7 @@ public class TransformExceptionHandlers {
msg.moveJump(jumps); msg.moveJump(jumps);
} }
} }
if (foundSub)
removeJSR(tryFlow, subRoutine); removeJSR(tryFlow, subRoutine);
} }
@ -419,6 +468,7 @@ public class TransformExceptionHandlers {
* block. * block.
*/ */
if (exitBlock == null if (exitBlock == null
&& jumps.destination.predecessors.size() != 1
&& jumps.destination.getAddr() >= startOutExit && jumps.destination.getAddr() >= startOutExit
&& jumps.destination.getNextAddr() <= endOutExit) { && jumps.destination.getNextAddr() <= endOutExit) {
jumps.destination.analyze(startOutExit, endOutExit); jumps.destination.analyze(startOutExit, endOutExit);
@ -579,15 +629,21 @@ public class TransformExceptionHandlers {
} }
} }
if (catchBlock instanceof SequentialBlock if (!(catchBlock instanceof SequentialBlock
&& catchBlock.getSubBlocks()[0] instanceof JsrBlock && catchBlock.getSubBlocks()[0] instanceof JsrBlock
&& instr instanceof StoreInstruction && instr instanceof StoreInstruction
&& catchBlock.getSubBlocks()[1] instanceof ThrowBlock && catchBlock.getSubBlocks()[1] instanceof ThrowBlock))
&& (((ThrowBlock)catchBlock.getSubBlocks()[1]).instr
instanceof LocalLoadOperator) return false;
&& (((StoreInstruction) instr).lvalueMatches
((LocalLoadOperator) JsrBlock jsrBlock = (JsrBlock)catchBlock.getSubBlocks()[0];
((ThrowBlock)catchBlock.getSubBlocks()[1]).instr))) { ThrowBlock throwBlock = (ThrowBlock) catchBlock.getSubBlocks()[1];
if (!(throwBlock.instr instanceof LocalLoadOperator)
|| !(((StoreInstruction) instr).lvalueMatches
((LocalLoadOperator) throwBlock.instr)))
return false;
/* Wow that was complicated :-) /* Wow that was complicated :-)
* But now we know that the catch block looks * But now we know that the catch block looks
@ -604,27 +660,28 @@ public class TransformExceptionHandlers {
* need to check if it breaks to the right block, because * need to check if it breaks to the right block, because
* we know that there is only one Block around the jsr. * we know that there is only one Block around the jsr.
*/ */
if (!(((JsrBlock)catchBlock.getSubBlocks()[0]).innerBlock if (!(jsrBlock.innerBlock instanceof BreakBlock))
instanceof BreakBlock))
return false; return false;
/* Check if the try block has no exit (except throws) /* Check if the try block has no exit (except throws)
*/ */
Jump throwJumps = tryFlow.getJumps(FlowBlock.END_OF_METHOD); Set succs = tryFlow.getSuccessors();
if (tryFlow.getSuccessors().size() > 1 if (succs.size() > 0) {
|| (tryFlow.getSuccessors().size() > 0 if (!succs.contains(FlowBlock.END_OF_METHOD))
&& throwJumps == null))
return false; return false;
Jump throwJumps
for (/**/; throwJumps != null; throwJumps = throwJumps.next) { = tryFlow.getJumps(FlowBlock.END_OF_METHOD);
for (/**/; throwJumps != null;
throwJumps = throwJumps.next) {
if (!(throwJumps.prev instanceof ThrowBlock)) if (!(throwJumps.prev instanceof ThrowBlock))
/* There is a return exit in the try block */ /* There is a return exit in the try block */
return false; return false;
} }
}
/* Remove the jump of the throw instruction. /* Remove the jump of the throw instruction.
*/ */
catchFlow.removeSuccessor(catchBlock.getSubBlocks()[1].jump); catchFlow.removeSuccessor(throwBlock.jump);
catchBlock.getSubBlocks()[1].removeJump(); throwBlock.removeJump();
/* Replace the catchBlock with the finallyBlock. /* Replace the catchBlock with the finallyBlock.
*/ */
@ -637,30 +694,44 @@ public class TransformExceptionHandlers {
tryFlow.mergeSuccessors(catchFlow); tryFlow.mergeSuccessors(catchFlow);
} else { } else {
FlowBlock subRoutine = FlowBlock subRoutine = jsrBlock.innerBlock.jump.destination;
((JsrBlock)catchBlock.getSubBlocks()[0]) checkAndRemoveJSR(tryFlow, subRoutine,
.innerBlock.jump.destination; tryFlow.getNextAddr(), catchFlow.getAddr());
subRoutine.analyze(catchFlow.getNextAddr(), end); while (subRoutine.analyze(catchFlow.getNextAddr(), end));
if (!transformSubRoutine(subRoutine.block))
return false;
tryFlow.mergeAddr(catchFlow); /* Now check if the subroutine is correct and has only the
* catchFlow as predecessor.
*/
if (subRoutine.predecessors.size() == 1
&& transformSubRoutine(subRoutine.block)) {
subRoutine.mergeAddr(catchFlow);
checkAndRemoveJSR(tryFlow, subRoutine); /* Now remove the jump to the JSR from the catch block
* and the jump of the throw instruction.
*/
catchFlow.removeSuccessor(jsrBlock.innerBlock.jump);
jsrBlock.innerBlock.removeJump();
catchFlow.removeSuccessor(throwBlock.jump);
throwBlock.removeJump();
tryFlow.updateInOutCatch(subRoutine); tryFlow.updateInOutCatch(subRoutine);
tryFlow.mergeAddr(subRoutine); tryFlow.mergeAddr(subRoutine);
tryFlow.mergeSuccessors(subRoutine); tryFlow.mergeSuccessors(subRoutine);
finallyBlock = subRoutine.block; finallyBlock = subRoutine.block;
} else {
catchFlow.removeSuccessor(throwBlock.jump);
throwBlock.removeJump();
finallyBlock = jsrBlock;
/* Now remove the jump to the JSR from the catch block finallyBlock.replace(catchFlow.block);
* and the jump of the throw instruction. transformSubRoutine(finallyBlock);
*/
catchBlock.getSubBlocks()[0].getSubBlocks()[0] tryFlow.updateInOutCatch(catchFlow);
.jump.destination.predecessors.remove(catchFlow); tryFlow.mergeAddr(catchFlow);
catchBlock.getSubBlocks()[1] finallyBlock = catchFlow.block;
.jump.destination.predecessors.remove(catchFlow); tryFlow.mergeSuccessors(catchFlow);
}
} }
TryBlock tryBlock = (TryBlock)tryFlow.block; TryBlock tryBlock = (TryBlock)tryFlow.block;
@ -678,8 +749,6 @@ public class TransformExceptionHandlers {
tryBlock.addCatchBlock(newBlock); tryBlock.addCatchBlock(newBlock);
return true; return true;
} }
return false;
}
private boolean analyzeSpecialFinally(FlowBlock tryFlow, private boolean analyzeSpecialFinally(FlowBlock tryFlow,
FlowBlock catchFlow, int end) { FlowBlock catchFlow, int end) {
@ -724,7 +793,6 @@ public class TransformExceptionHandlers {
if (succ != null) { if (succ != null) {
Jump jumps = tryFlow.removeJumps(succ); Jump jumps = tryFlow.removeJumps(succ);
succ.predecessors.remove(tryFlow);
/* Handle the jumps in the tryFlow. /* Handle the jumps in the tryFlow.
*/ */
jumps = tryFlow.resolveSomeJumps(jumps, succ); jumps = tryFlow.resolveSomeJumps(jumps, succ);
@ -751,7 +819,7 @@ public class TransformExceptionHandlers {
newBlock.setCatchBlock(succ.block); newBlock.setCatchBlock(succ.block);
tryFlow.mergeSuccessors(succ); tryFlow.mergeSuccessors(succ);
} else { } else {
/* Put the catchBlock in instaed. /* Put the catchBlock in instead.
*/ */
newBlock.setCatchBlock(catchFlow.block); newBlock.setCatchBlock(catchFlow.block);
tryFlow.mergeSuccessors(catchFlow); tryFlow.mergeSuccessors(catchFlow);
@ -855,7 +923,9 @@ public class TransformExceptionHandlers {
catchFlow.getAddr()); catchFlow.getAddr());
newFlow.appendBlock(jump, 0); newFlow.appendBlock(jump, 0);
catchFlow.prevByAddr.nextByAddr = newFlow; catchFlow.prevByAddr.nextByAddr = newFlow;
newFlow.prevByAddr = catchFlow.prevByAddr;
newFlow.nextByAddr = catchFlow; newFlow.nextByAddr = catchFlow;
catchFlow.prevByAddr = newFlow;
catchFlow = newFlow; catchFlow = newFlow;
} else { } else {
if ((GlobalOptions.debuggingFlags if ((GlobalOptions.debuggingFlags

Loading…
Cancel
Save