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 25 years ago
parent 7c1859a4df
commit 67e5bf4656
  1. 288
      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,7 +347,8 @@ public class TransformExceptionHandlers {
msg.moveJump(jumps); msg.moveJump(jumps);
} }
} }
removeJSR(tryFlow, subRoutine); if (foundSub)
removeJSR(tryFlow, subRoutine);
} }
static boolean isMonitorExit(Expression instr, LocalInfo local) { static boolean isMonitorExit(Expression instr, LocalInfo local) {
@ -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,106 +629,125 @@ 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];
/* Wow that was complicated :-)
* But now we know that the catch block looks if (!(throwBlock.instr instanceof LocalLoadOperator)
* exactly like it should: || !(((StoreInstruction) instr).lvalueMatches
* ((LocalLoadOperator) throwBlock.instr)))
* catchBlock: return false;
* JSR
* finally /* Wow that was complicated :-)
* throw local_n <- matches the local in instr. * But now we know that the catch block looks
*/ * exactly like it should:
*
if (finallyBlock != null) { * catchBlock:
/* Check if the jsr breaks (see two comments above). We don't * JSR
* need to check if it breaks to the right block, because * finally
* we know that there is only one Block around the jsr. * throw local_n <- matches the local in instr.
*/ */
if (!(((JsrBlock)catchBlock.getSubBlocks()[0]).innerBlock
instanceof BreakBlock)) if (finallyBlock != null) {
return false; /* Check if the jsr breaks (see two comments above). We don't
* need to check if it breaks to the right block, because
/* Check if the try block has no exit (except throws) * we know that there is only one Block around the jsr.
*/ */
Jump throwJumps = tryFlow.getJumps(FlowBlock.END_OF_METHOD); if (!(jsrBlock.innerBlock instanceof BreakBlock))
if (tryFlow.getSuccessors().size() > 1 return false;
|| (tryFlow.getSuccessors().size() > 0
&& throwJumps == null)) /* Check if the try block has no exit (except throws)
return false; */
Set succs = tryFlow.getSuccessors();
for (/**/; throwJumps != null; throwJumps = throwJumps.next) { if (succs.size() > 0) {
if (!(throwJumps.prev instanceof ThrowBlock)) if (!succs.contains(FlowBlock.END_OF_METHOD))
/* There is a return exit in the try block */ return false;
return false; Jump throwJumps
} = tryFlow.getJumps(FlowBlock.END_OF_METHOD);
/* Remove the jump of the throw instruction. for (/**/; throwJumps != null;
*/ throwJumps = throwJumps.next) {
catchFlow.removeSuccessor(catchBlock.getSubBlocks()[1].jump); if (!(throwJumps.prev instanceof ThrowBlock))
catchBlock.getSubBlocks()[1].removeJump(); /* There is a return exit in the try block */
return false;
/* Replace the catchBlock with the finallyBlock. }
*/ }
finallyBlock.replace(catchFlow.block); /* Remove the jump of the throw instruction.
transformSubRoutine(finallyBlock); */
catchFlow.removeSuccessor(throwBlock.jump);
tryFlow.updateInOutCatch(catchFlow); throwBlock.removeJump();
tryFlow.mergeAddr(catchFlow);
finallyBlock = catchFlow.block; /* Replace the catchBlock with the finallyBlock.
tryFlow.mergeSuccessors(catchFlow); */
finallyBlock.replace(catchFlow.block);
} else { transformSubRoutine(finallyBlock);
FlowBlock subRoutine =
((JsrBlock)catchBlock.getSubBlocks()[0]) tryFlow.updateInOutCatch(catchFlow);
.innerBlock.jump.destination; tryFlow.mergeAddr(catchFlow);
finallyBlock = catchFlow.block;
subRoutine.analyze(catchFlow.getNextAddr(), end); tryFlow.mergeSuccessors(catchFlow);
if (!transformSubRoutine(subRoutine.block))
return false; } else {
FlowBlock subRoutine = jsrBlock.innerBlock.jump.destination;
tryFlow.mergeAddr(catchFlow); checkAndRemoveJSR(tryFlow, subRoutine,
tryFlow.getNextAddr(), catchFlow.getAddr());
checkAndRemoveJSR(tryFlow, subRoutine); while (subRoutine.analyze(catchFlow.getNextAddr(), end));
tryFlow.updateInOutCatch(subRoutine); /* Now check if the subroutine is correct and has only the
tryFlow.mergeAddr(subRoutine); * catchFlow as predecessor.
tryFlow.mergeSuccessors(subRoutine); */
finallyBlock = subRoutine.block; if (subRoutine.predecessors.size() == 1
&& transformSubRoutine(subRoutine.block)) {
subRoutine.mergeAddr(catchFlow);
/* Now remove the jump to the JSR from the catch block /* Now remove the jump to the JSR from the catch block
* and the jump of the throw instruction. * and the jump of the throw instruction.
*/ */
catchBlock.getSubBlocks()[0].getSubBlocks()[0] catchFlow.removeSuccessor(jsrBlock.innerBlock.jump);
.jump.destination.predecessors.remove(catchFlow); jsrBlock.innerBlock.removeJump();
catchBlock.getSubBlocks()[1] catchFlow.removeSuccessor(throwBlock.jump);
.jump.destination.predecessors.remove(catchFlow); throwBlock.removeJump();
}
tryFlow.updateInOutCatch(subRoutine);
TryBlock tryBlock = (TryBlock)tryFlow.block; tryFlow.mergeAddr(subRoutine);
if (tryBlock.getSubBlocks()[0] instanceof TryBlock) { tryFlow.mergeSuccessors(subRoutine);
/* remove the surrounding tryBlock */ finallyBlock = subRoutine.block;
TryBlock innerTry = (TryBlock)tryBlock.getSubBlocks()[0]; } else {
innerTry.gen = tryBlock.gen; catchFlow.removeSuccessor(throwBlock.jump);
innerTry.replace(tryBlock); throwBlock.removeJump();
tryBlock = innerTry; finallyBlock = jsrBlock;
tryFlow.lastModified = tryBlock;
tryFlow.block = tryBlock; finallyBlock.replace(catchFlow.block);
} transformSubRoutine(finallyBlock);
FinallyBlock newBlock = new FinallyBlock();
newBlock.setCatchBlock(finallyBlock); tryFlow.updateInOutCatch(catchFlow);
tryBlock.addCatchBlock(newBlock); tryFlow.mergeAddr(catchFlow);
return true; finallyBlock = catchFlow.block;
} tryFlow.mergeSuccessors(catchFlow);
return false; }
}
TryBlock tryBlock = (TryBlock)tryFlow.block;
if (tryBlock.getSubBlocks()[0] instanceof TryBlock) {
/* remove the surrounding tryBlock */
TryBlock innerTry = (TryBlock)tryBlock.getSubBlocks()[0];
innerTry.gen = tryBlock.gen;
innerTry.replace(tryBlock);
tryBlock = innerTry;
tryFlow.lastModified = tryBlock;
tryFlow.block = tryBlock;
}
FinallyBlock newBlock = new FinallyBlock();
newBlock.setCatchBlock(finallyBlock);
tryBlock.addCatchBlock(newBlock);
return true;
} }
private boolean analyzeSpecialFinally(FlowBlock tryFlow, private boolean analyzeSpecialFinally(FlowBlock tryFlow,
@ -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