@ -103,35 +103,62 @@ public class TransformExceptionHandlers {
handlers . add ( new Handler ( tryBlock , endBlock , catchBlock , type ) ) ;
handlers . add ( new Handler ( tryBlock , endBlock , catchBlock , type ) ) ;
}
}
/ * *
* Merge the try flow block with the catch flow block . This is a kind
* of special T2 transformation , as all jumps to the catch block are
* implicit ( exception can be thrown everywhere ) . < br >
*
* This method doesn ' t actually merge the contents of the blocks . The
* caller should do it right afterwards . < br >
*
* The flow block catchFlow mustn ' t have any predecessors .
* @param tryFlow the flow block containing the try .
* @param catchFlow the flow block containing the catch handler .
* /
static void mergeTryCatch ( FlowBlock tryFlow , FlowBlock catchFlow ) {
if ( ( GlobalOptions . debuggingFlags
& GlobalOptions . DEBUG_ANALYZE ) ! = 0 )
GlobalOptions . err . println
( "mergeTryCatch(" + tryFlow . getBlockNr ( )
+ ", " + catchFlow . getBlockNr ( ) + ")" ) ;
tryFlow . updateInOutCatch ( catchFlow ) ;
tryFlow . mergeSuccessors ( catchFlow ) ;
tryFlow . mergeBlockNr ( catchFlow ) ;
}
/ * simple try catch block :
/ * *
* Analyzes a simple try / catch block . The try and catch part are both
* analyzed , the try block is already created , but the catch block
* isn ' t . < br >
* The catchFlow block mustn ' t have any predecessors .
*
*
* try - header
* @param type The type of the exception which is caught .
* | - first instruction
* @param tryFlow The flow block containing the try . The contained
* | . . .
* block must be a try block .
* | last instruction
* @param catchFlow the flow block containing the catch handler .
* | - optional jump ( last + 1 )
* | . . .
* ` - catch block
* /
* /
static void analyzeCatchBlock ( Type type , FlowBlock tryFlow ,
static void analyzeCatchBlock ( Type type , FlowBlock tryFlow ,
StructuredBlock catchBlock ) {
FlowBlock catchFlow ) {
/* Merge try and catch flow blocks */
mergeTryCatch ( tryFlow , catchFlow ) ;
/* Insert catch block into tryFlow */
CatchBlock newBlock = new CatchBlock ( type ) ;
CatchBlock newBlock = new CatchBlock ( type ) ;
( ( TryBlock ) tryFlow . block ) . addCatchBlock ( newBlock ) ;
( ( TryBlock ) tryFlow . block ) . addCatchBlock ( newBlock ) ;
newBlock . setCatchBlock ( catchBlock ) ;
newBlock . setCatchBlock ( catchFlow . block ) ;
tryFlow . lastModified = tryFlow . block ;
}
}
/* And now the complicated parts. */
/ * *
/ * *
* This transforms a sub routine , that is checks if the beginning
* This transforms a sub routine , i . e . it checks if the beginning
* local assignment matches the final ret and then returns .
* local assignment matches the final ret and removes both . It also
* accepts sub routines that just pop their return address .
* /
* /
boolean transformSubRoutine ( StructuredBlock subRoutine ) {
boolean transformSubRoutine ( StructuredBlock subRoutineBlock ) {
if ( ! ( subRoutine instanceof SequentialBlock ) )
StructuredBlock firstBlock = subRoutineBlock ;
return false ;
if ( firstBlock instanceof SequentialBlock )
SequentialBlock sequBlock = ( SequentialBlock ) subRoutine ;
firstBlock = subRoutineBlock . getSubBlocks ( ) [ 0 ] ;
StructuredBlock firstBlock = sequBlock . getSubBlocks ( ) [ 0 ] ;
LocalInfo local = null ;
LocalInfo local = null ;
if ( firstBlock instanceof SpecialBlock ) {
if ( firstBlock instanceof SpecialBlock ) {
@ -156,24 +183,25 @@ public class TransformExceptionHandlers {
} else
} else
return false ;
return false ;
/ * We are now committed . Remove the first Statement which
/ * We are now committed and can start changing code . Remove
* stores / removes the return address .
* the first Statement which stores / removes the return
* address .
* /
* /
firstBlock . removeBlock ( ) ;
firstBlock . removeBlock ( ) ;
/ * XXX - Replace any RET with a jump to end of this flow block .
/ * We don ' t check if there is a RET in the middle .
*
*
* This is a complicated task which isn ' t needed for javac nor
* This is a complicated task which isn ' t needed for javac nor
* jikes . We just check if the last instruction is a ret and
* jikes . We just check if the last instruction is a ret and
* replac e this . This will never produce code with wrong semantic ,
* remov e this . This will never produce code with wrong semantic ,
* as long as the bytecode was verified correctly .
* as long as the bytecode was verified correctly .
* /
* /
while ( sequBlock . subBlocks [ 1 ] instanceof SequentialBlock )
while ( subRoutineBlock instanceof SequentialBlock )
seq uBlock = ( SequentialBlock ) sequBlock . subBlocks [ 1 ] ;
subRoutine Block = subRoutineBlock . getSubBlocks ( ) [ 1 ] ;
if ( sequBlock . subBlocks [ 1 ] instanceof RetBlock
if ( subRoutineBlock instanceof RetBlock
& & ( ( ( RetBlock ) sequBlock . subBlocks [ 1 ] ) . local . equals ( local ) ) ) {
& & ( ( ( RetBlock ) subRoutineBlock ) . local . equals ( local ) ) ) {
sequBlock . subBlocks [ 1 ] . removeBlock ( ) ;
subRoutineBlock . removeBlock ( ) ;
}
}
return true ;
return true ;
}
}
@ -203,51 +231,32 @@ public class TransformExceptionHandlers {
}
}
/ * *
/ * *
* Remove the wrongly placed JSRs jumping to the specified
* Remove the JSRs jumping to the specified subRoutine . The right
* subRoutine . The right JSRs are already removed , but we have to
* JSRs are marked and we can just remove them . For the other JSR
* replace the wrong ones with a warning .
* instructions we replace them with a warning .
* @param tryFlow the FlowBL ock of the try block .
* @param tryFlow the FlowBl ock of the try block .
* @param subRoutine the FlowBlock of the sub routine .
* @param subRoutine the FlowBlock of the sub routine .
* /
* /
private void removeJSR ( FlowBlock tryFlow , StructuredBlock catchBlock ,
private void removeJSR ( FlowBlock tryFlow , FlowBlock subRoutine ) {
FlowBlock subRoutine ) {
for ( Jump jumps = tryFlow . removeJumps ( subRoutine ) ;
Jump nextJump ;
jumps ! = null ; jumps = jumps . next ) {
for ( Jump jumps = tryFlow . getJumps ( subRoutine ) ;
jumps ! = null ; jumps = nextJump ) {
StructuredBlock prev = jumps . prev ;
StructuredBlock prev = jumps . prev ;
nextJump = jumps . next ;
prev . removeJump ( ) ;
if ( prev instanceof EmptyBlock
& & prev . outer instanceof JsrBlock ) {
JsrBlock jsr = ( JsrBlock ) prev . outer ;
if ( prev . outer = = catchBlock ) {
/* This is the mandatory jsr in the catch block */
continue ;
}
tryFlow . removeSuccessor ( jumps ) ;
if ( prev instanceof EmptyBlock
prev . removeJump ( ) ;
& & prev . outer instanceof JsrBlock
if ( jsr . isGood ( ) ) {
& & ( ( JsrBlock ) prev . outer ) . isGood ( ) ) {
StructuredBlock next = jsr . getNextBlock ( ) ;
StructuredBlock next = prev . outer . getNextBlock ( ) ;
jsr . removeBlock ( ) ;
prev . outer . removeBlock ( ) ;
if ( next instanceof ReturnBlock )
if ( next instanceof ReturnBlock )
removeReturnLocal ( ( ReturnBlock ) next ) ;
removeReturnLocal ( ( ReturnBlock ) next ) ;
} else {
} else {
/ * We have a JSR to the subroutine , which is badly placed .
/ * We have a jump to the subroutine , that is badly placed .
* We complain here .
* /
DescriptionBlock msg
= new DescriptionBlock ( "ERROR: JSR FINALLY BLOCK!" ) ;
msg . replace ( prev . outer ) ;
}
} else {
/ * We have a jump to the subroutine , that is wrong .
* We complain here .
* We complain here .
* /
* /
DescriptionBlock msg
DescriptionBlock msg = new DescriptionBlock
= new DescriptionBlock ( "ERROR: GOTO FINALLY BLOCK!" ) ;
( "ERROR: invalid jump to finally block!" ) ;
tryFlow . removeSuccessor ( jumps ) ;
prev . removeJump ( ) ;
prev . appendBlock ( msg ) ;
prev . appendBlock ( msg ) ;
}
}
}
}
@ -313,7 +322,6 @@ public class TransformExceptionHandlers {
private void checkAndRemoveJSR ( FlowBlock tryFlow ,
private void checkAndRemoveJSR ( FlowBlock tryFlow ,
StructuredBlock catchBlock ,
FlowBlock subRoutine ,
FlowBlock subRoutine ,
int startOutExit , int endOutExit ) {
int startOutExit , int endOutExit ) {
Iterator iter = tryFlow . getSuccessors ( ) . iterator ( ) ;
Iterator iter = tryFlow . getSuccessors ( ) . iterator ( ) ;
@ -330,8 +338,8 @@ public class TransformExceptionHandlers {
StructuredBlock prev = jumps . prev ;
StructuredBlock prev = jumps . prev ;
if ( prev instanceof EmptyBlock
if ( prev instanceof EmptyBlock
& & prev . outer instanceof JsrBlock ) {
& & prev . outer instanceof JsrBlock ) {
/ * This jump is really a jsr , since it doesn ' t
/ * This jump is a jsr , since it doesn ' t leave the
* leave the block forever , we can ignore it .
* block forever , we can ignore it .
* /
* /
continue ;
continue ;
}
}
@ -382,7 +390,7 @@ public class TransformExceptionHandlers {
* Complain !
* Complain !
* /
* /
DescriptionBlock msg
DescriptionBlock msg
= new DescriptionBlock ( "ERROR: NO JSR TO FINALLY " ) ;
= new DescriptionBlock ( "ERROR: no jsr to finally " ) ;
if ( pred ! = null )
if ( pred ! = null )
pred . prependBlock ( msg ) ;
pred . prependBlock ( msg ) ;
else {
else {
@ -392,11 +400,10 @@ public class TransformExceptionHandlers {
}
}
}
}
if ( tryFlow . getSuccessors ( ) . contains ( subRoutine ) )
if ( tryFlow . getSuccessors ( ) . contains ( subRoutine ) )
removeJSR ( tryFlow , catchBlock , subRoutine ) ;
removeJSR ( tryFlow , subRoutine ) ;
}
}
private void checkAndRemoveMonitorExit ( FlowBlock tryFlow ,
private void checkAndRemoveMonitorExit ( FlowBlock tryFlow ,
StructuredBlock catchBlock ,
LocalInfo local ,
LocalInfo local ,
int start , int end ) {
int start , int end ) {
FlowBlock subRoutine = null ;
FlowBlock subRoutine = null ;
@ -494,7 +501,7 @@ public class TransformExceptionHandlers {
/ * Complain !
/ * Complain !
* /
* /
DescriptionBlock msg
DescriptionBlock msg
= new DescriptionBlock ( "ERROR: NO MONITOREXIT " ) ;
= new DescriptionBlock ( "ERROR: no monitorexit " ) ;
prev . appendBlock ( msg ) ;
prev . appendBlock ( msg ) ;
msg . moveJump ( jumps ) ;
msg . moveJump ( jumps ) ;
}
}
@ -502,8 +509,9 @@ public class TransformExceptionHandlers {
if ( subRoutine ! = null ) {
if ( subRoutine ! = null ) {
if ( tryFlow . getSuccessors ( ) . contains ( subRoutine ) )
if ( tryFlow . getSuccessors ( ) . contains ( subRoutine ) )
removeJSR ( tryFlow , catchBlock , subRoutine ) ;
removeJSR ( tryFlow , subRoutine ) ;
tryFlow . mergeBlockNr ( subRoutine ) ;
if ( subRoutine . predecessors . size ( ) = = 0 )
tryFlow . mergeBlockNr ( subRoutine ) ;
}
}
}
}
@ -526,20 +534,25 @@ public class TransformExceptionHandlers {
}
}
private boolean analyzeSynchronized ( FlowBlock tryFlow ,
private boolean analyzeSynchronized ( FlowBlock tryFlow ,
StructuredBlock catchBlock ,
FlowBlock catchFlow ,
int endHandler ) {
int endHandler ) {
/ * Check if this is a synchronized block . We mustn ' t change
* anything until we are sure .
* /
StructuredBlock catchBlock = catchFlow . block ;
/* Check for a optional exception store and skip it */
StoreInstruction excStore = getExceptionStore ( catchBlock ) ;
StoreInstruction excStore = getExceptionStore ( catchBlock ) ;
if ( excStore ! = null )
if ( excStore ! = null )
catchBlock = catchBlock . getSubBlocks ( ) [ 1 ] ;
catchBlock = catchBlock . getSubBlocks ( ) [ 1 ] ;
/* Check for the monitorexit instruction */
if ( ! ( catchBlock instanceof SequentialBlock
if ( ! ( catchBlock instanceof SequentialBlock
& & catchBlock . getSubBlocks ( ) [ 0 ]
& & catchBlock . getSubBlocks ( ) [ 0 ]
instanceof InstructionBlock ) )
instanceof InstructionBlock ) )
return false ;
return false ;
Expression instr =
Expression instr =
( ( InstructionBlock ) catchBlock . getSubBlocks ( ) [ 0 ] ) . getInstruction ( ) ;
( ( InstructionBlock ) catchBlock . getSubBlocks ( ) [ 0 ] ) . getInstruction ( ) ;
if ( ! ( instr instanceof MonitorExitOperator
if ( ! ( instr instanceof MonitorExitOperator
& & instr . getFreeOperandCount ( ) = = 0
& & instr . getFreeOperandCount ( ) = = 0
& & ( ( ( MonitorExitOperator ) instr ) . getSubExpressions ( ) [ 0 ]
& & ( ( ( MonitorExitOperator ) instr ) . getSubExpressions ( ) [ 0 ]
@ -547,9 +560,9 @@ public class TransformExceptionHandlers {
& & catchBlock . getSubBlocks ( ) [ 1 ] instanceof ThrowBlock ) )
& & catchBlock . getSubBlocks ( ) [ 1 ] instanceof ThrowBlock ) )
return false ;
return false ;
/* Check for the throw instruction */
Expression throwInstr =
Expression throwInstr =
( ( ThrowBlock ) catchBlock . getSubBlocks ( ) [ 1 ] ) . getInstruction ( ) ;
( ( ThrowBlock ) catchBlock . getSubBlocks ( ) [ 1 ] ) . getInstruction ( ) ;
if ( excStore ! = null ) {
if ( excStore ! = null ) {
if ( ! ( throwInstr instanceof Operator
if ( ! ( throwInstr instanceof Operator
& & excStore . lvalueMatches ( ( Operator ) throwInstr ) ) )
& & excStore . lvalueMatches ( ( Operator ) throwInstr ) ) )
@ -584,6 +597,12 @@ public class TransformExceptionHandlers {
* return_n
* return_n
* /
* /
/ * Merge try and catch flow blocks . No need to insert the
* catchFlow . block into the try flow though , since all its
* instruction are synthetic .
* /
mergeTryCatch ( tryFlow , catchFlow ) ;
MonitorExitOperator monexit = ( MonitorExitOperator )
MonitorExitOperator monexit = ( MonitorExitOperator )
( ( InstructionBlock ) catchBlock . getSubBlocks ( ) [ 0 ] ) . instr ;
( ( InstructionBlock ) catchBlock . getSubBlocks ( ) [ 0 ] ) . instr ;
LocalInfo local =
LocalInfo local =
@ -597,7 +616,7 @@ public class TransformExceptionHandlers {
+ "," + tryFlow . getNextBlockNr ( ) + "," + endHandler + ")" ) ;
+ "," + tryFlow . getNextBlockNr ( ) + "," + endHandler + ")" ) ;
checkAndRemoveMonitorExit
checkAndRemoveMonitorExit
( tryFlow , catchBlock , local , tryFlow . getNextBlockNr ( ) , endHandler ) ;
( tryFlow , local , tryFlow . getNextBlockNr ( ) , endHandler ) ;
SynchronizedBlock syncBlock = new SynchronizedBlock ( local ) ;
SynchronizedBlock syncBlock = new SynchronizedBlock ( local ) ;
TryBlock tryBlock = ( TryBlock ) tryFlow . block ;
TryBlock tryBlock = ( TryBlock ) tryFlow . block ;
@ -609,8 +628,41 @@ public class TransformExceptionHandlers {
return true ;
return true ;
}
}
/ * *
* Merge try and finally flow blocks .
* @param tryFlow The try flow block . Its contained block must be
* a try block .
* @param catchFlow The catch flow block that contains the finally
* block .
* @param finallyBlock block that either contains the finally block .
* It is part of the catchFlow . The other parts of catchFlow are
* synthetic and can be removed .
* /
private void mergeFinallyBlock ( FlowBlock tryFlow , FlowBlock catchFlow ,
StructuredBlock finallyBlock ) {
TryBlock tryBlock = ( TryBlock ) tryFlow . block ;
if ( tryBlock . getSubBlocks ( ) [ 0 ] instanceof TryBlock ) {
/ * A try { try { } catch { } } finally { } is equivalent
* to a try { } catch { } finally { }
* so 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 ;
}
/* Now merge try and catch flow blocks */
mergeTryCatch ( tryFlow , catchFlow ) ;
FinallyBlock newBlock = new FinallyBlock ( ) ;
newBlock . setCatchBlock ( finallyBlock ) ;
tryBlock . addCatchBlock ( newBlock ) ;
}
private boolean analyzeFinally ( FlowBlock tryFlow ,
private boolean analyzeFinally ( FlowBlock tryFlow ,
StructuredBlock catchBlock , int end ) {
FlowBlock catchFlow , int end ) {
/ * Layout of a try - finally block :
/ * Layout of a try - finally block :
*
*
@ -632,6 +684,7 @@ public class TransformExceptionHandlers {
* return_n
* return_n
* /
* /
StructuredBlock catchBlock = catchFlow . block ;
StoreInstruction excStore = getExceptionStore ( catchBlock ) ;
StoreInstruction excStore = getExceptionStore ( catchBlock ) ;
if ( excStore = = null )
if ( excStore = = null )
return false ;
return false ;
@ -643,9 +696,10 @@ public class TransformExceptionHandlers {
StructuredBlock finallyBlock = null ;
StructuredBlock finallyBlock = null ;
if ( catchBlock . getSubBlocks ( ) [ 0 ] instanceof LoopBlock ) {
if ( catchBlock . getSubBlocks ( ) [ 0 ] instanceof LoopBlock ) {
/ * In case the try block has no exit ( that means , it throws
/ * In case the try block has no exit ( that means , it
* an exception ) , the finallyBlock was already merged with
* throws an exception or loops forever ) , the finallyBlock
* the catchBlock . We have to check for this case separately :
* was already merged with the catchBlock . We have to
* check for this case separately :
*
*
* do {
* do {
* JSR
* JSR
@ -682,7 +736,7 @@ public class TransformExceptionHandlers {
FlowBlock subRoutine ;
FlowBlock subRoutine ;
if ( finallyBlock ! = null ) {
if ( finallyBlock ! = null ) {
/ * Check if the jsr breaks ( see two comments above ) . We don ' t
/ * Check if the jsr breaks ( see comment above ) . We don ' t
* 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 .
* /
* /
@ -690,269 +744,255 @@ public class TransformExceptionHandlers {
return false ;
return false ;
/ * Check if the try block has no exit
/ * Check if the try block has no exit
* XXX - Unfortunately the try block already has the
* successors of catch block .
* /
* /
// if (tryFlow.getSuccessors().size() > 0)
if ( tryFlow . getSuccessors ( ) . size ( ) > 0 )
// return false;
return false ;
catchBlock = finallyBlock ;
catchBlock = finallyBlock ;
subRoutine = null ;
subRoutine = null ;
} else {
} else {
if ( ! ( jsrBlock . innerBlock instanceof EmptyBlock ) )
if ( ! ( jsrBlock . innerBlock instanceof EmptyBlock ) )
return false ;
return false ;
catchBlock = jsrBlock ;
finallyBlock = jsrBlock . innerBlock ;
subRoutine = jsrBlock . innerBlock . jump . destination ;
subRoutine = finallyBlock . jump . destination ;
checkAndRemoveJSR ( tryFlow , catchBlock , subRoutine ,
tryFlow . getNextBlockNr ( ) ,
subRoutine . getBlockNr ( ) ) ;
}
/ * Wow that was complicated : - )
* But now we know that the catch block looks
* exactly like it should :
*
* local_n = POP
* catchBlock :
* JSR
* finally
* throw local_n
* /
TryBlock tryBlock = ( TryBlock ) tryFlow . block ;
/ * We are committed now and can start changing the try
if ( tryBlock . getSubBlocks ( ) [ 0 ] instanceof TryBlock ) {
* block .
/* remove the surrounding tryBlock */
* /
TryBlock innerTry = ( TryBlock ) tryBlock . getSubBlocks ( ) [ 0 ] ;
checkAndRemoveJSR ( tryFlow , subRoutine ,
innerTry . gen = tryBlock . gen ;
tryFlow . getNextBlockNr ( ) , end ) ;
innerTry . replace ( tryBlock ) ;
tryBlock = innerTry ;
tryFlow . lastModified = tryBlock ;
tryFlow . block = tryBlock ;
}
FinallyBlock newBlock = new FinallyBlock ( ) ;
newBlock . setCatchBlock ( catchBlock ) ;
tryBlock . addCatchBlock ( newBlock ) ;
if ( subRoutine ! = null ) {
while ( subRoutine . analyze ( tryFlow . getNextBlockNr ( ) , end ) ) ;
/ * Now check if the subroutine is correct and has only the
/ * Now analyze and transform the subroutine .
* catchFlow as predecessor .
* /
* /
if ( subRoutine . predecessors . size ( ) = = 1
while ( subRoutine . analyze ( tryFlow . getNextBlockNr ( ) , end ) ) ;
& & transformSubRoutine ( subRoutine . block ) ) {
if ( subRoutine . predecessors . size ( ) = = 1 ) {
/ * catchFlow is synthetic , so we can safely remove it
tryFlow . removeSuccessor ( jsrBlock . innerBlock . jump ) ;
* here .
tryFlow . mergeBlockNr ( subRoutine ) ;
* /
tryFlow . mergeSuccessors ( subRoutine ) ;
subRoutine . mergeBlockNr ( catchFlow ) ;
subRoutine . block . replace ( catchBlock ) ;
catchFlow = subRoutine ;
tryFlow . updateInOutCatch ( subRoutine ) ;
if ( ! transformSubRoutine ( subRoutine . block ) ) {
finallyBlock = subRoutine . block ;
DescriptionBlock msg = new DescriptionBlock
( "ERROR: Missing return address handling" ) ;
StructuredBlock subblock = subRoutine . block ;
msg . replace ( finallyBlock ) ;
msg . appendBlock ( finallyBlock ) ;
}
finallyBlock = subRoutine . block ;
}
}
}
}
/ * Now finish it .
* /
mergeFinallyBlock ( tryFlow , catchFlow , finallyBlock ) ;
return true ;
return true ;
}
}
private boolean analyzeSpecialFinally ( FlowBlock tryFlow ,
private boolean analyzeSpecialFinally ( FlowBlock tryFlow ,
StructuredBlock catchBlock ,
FlowBlock catchFlow , int end ) {
int end ) {
StructuredBlock finallyBlock = catchFlow . block ;
StructuredBlock firstInstr =
StructuredBlock firstInstr =
catch Block instanceof SequentialBlock
finally Block instanceof SequentialBlock
? catch Block. getSubBlocks ( ) [ 0 ] : catch Block;
? finally Block. getSubBlocks ( ) [ 0 ] : finally Block;
if ( ! ( firstInstr instanceof SpecialBlock
if ( ! ( firstInstr instanceof SpecialBlock
& & ( ( SpecialBlock ) firstInstr ) . type = = SpecialBlock . POP
& & ( ( SpecialBlock ) firstInstr ) . type = = SpecialBlock . POP
& & ( ( SpecialBlock ) firstInstr ) . count = = 1 ) )
& & ( ( SpecialBlock ) firstInstr ) . count = = 1 ) )
return false ;
return false ;
/ * This may be a special try / finally - block , where
/ * This is a special try / finally - block , where
* the finally block ends with a break , return or
* the finally block ends with a break , return or
* similar .
* similar .
* /
* /
FlowBlock succ = null ;
/* remove the pop now */
/ * Make sure that resolveJump only works on the inside of the try
if ( catchBlock instanceof SequentialBlock )
* /
catchBlock = catchBlock . getSubBlocks ( ) [ 1 ] ;
tryFlow . lastModified = tryFlow . block . getSubBlocks ( ) [ 0 ] ;
if ( finallyBlock instanceof SequentialBlock )
finallyBlock = finallyBlock . getSubBlocks ( ) [ 1 ] ;
else {
else {
catchBlock = new EmptyBlock ( ) ;
finallyBlock = new EmptyBlock ( ) ;
catchBlock . moveJump ( firstInstr . jump ) ;
finallyBlock . moveJump ( firstInstr . jump ) ;
succ = firstInstr . jump . destination ;
}
// Set trySuccs = tryFlow.getSuccessors();
// if (trySuccs.size() > 1
// || (trySuccs.size() == 1
// && trySuccs.iterator().next() != succ))
// return false;
if ( succ ! = null ) {
/ * Handle the jumps in the tryFlow to finallyFlow .
/ * Handle the jumps in the tryFlow .
* /
* /
Jump jumps = tryFlow . removeJumps ( succ ) ;
FlowBlock finallyFlow = finallyBlock . jump . destination ;
jumps = tryFlow . resolveSomeJumps ( jumps , succ ) ;
if ( tryFlow . getSuccessors ( ) . contains ( finallyFlow ) ) {
tryFlow . resolveRemaining ( jumps ) ;
Jump jumps = tryFlow . removeJumps ( finallyFlow ) ;
jumps = tryFlow . resolveSomeJumps ( jumps , finallyFlow ) ;
tryFlow . resolveRemaining ( jumps ) ;
}
}
}
TryBlock tryBlock = ( TryBlock ) tryFlow . block ;
/* Complain about all other jumps in try block */
if ( tryBlock . getSubBlocks ( ) [ 0 ] instanceof TryBlock ) {
Set trySuccs = tryFlow . getSuccessors ( ) ;
/* remove the unnecessary tryBlock */
for ( Iterator i = trySuccs . iterator ( ) ; i . hasNext ( ) ; ) {
TryBlock innerTry = ( TryBlock ) tryBlock . getSubBlocks ( ) [ 0 ] ;
for ( Jump jumps = tryFlow . getJumps ( ( FlowBlock ) i . next ( ) ) ;
innerTry . gen = tryBlock . gen ;
jumps ! = null ; jumps = jumps . next ) {
innerTry . replace ( tryBlock ) ;
DescriptionBlock msg =
tryBlock = innerTry ;
new DescriptionBlock
tryFlow . lastModified = innerTry ;
( "ERROR: doesn't go through finally block!" ) ;
}
if ( jumps . prev instanceof ReturnBlock ) {
FinallyBlock newBlock = new FinallyBlock ( ) ;
msg . replace ( jumps . prev ) ;
tryBlock . addCatchBlock ( newBlock ) ;
msg . appendBlock ( jumps . prev ) ;
newBlock . setCatchBlock ( catchBlock ) ;
} else {
jumps . prev . appendBlock ( msg ) ;
msg . moveJump ( jumps ) ;
}
}
}
mergeFinallyBlock ( tryFlow , catchFlow , finallyBlock ) ;
/* Following code will work be put inside the finallyBlock */
tryFlow . lastModified = finallyBlock ;
return true ;
return true ;
}
}
/ * *
void checkTryCatchOrder ( ) {
* Analyzes all exception handlers to try / catch / finally or
* synchronized blocks .
* /
public void analyze ( ) {
/ * Check if try / catch ranges are okay . The following succeeds
/ * Check if try / catch ranges are okay . The following succeeds
* for all classes generated by the sun java compiler , but hand
* for all classes generated by the sun java compiler , but hand
* optimized classes ( or generated by other compilers ) will fail .
* optimized classes ( or generated by other compilers ) will fail .
* /
* /
{
Handler last = null ;
Handler last = null ;
for ( Iterator i = handlers . iterator ( ) ; i . hasNext ( ) ; ) {
for ( Iterator i = handlers . iterator ( ) ; i . hasNext ( ) ; ) {
Handler exc = ( Handler ) i . next ( ) ;
Handler exc = ( Handler ) i . next ( ) ;
int start = exc . start . getBlockNr ( ) ;
int start = exc . start . getBlockNr ( ) ;
int end = exc . end . getBlockNr ( ) ;
int end = exc . end . getBlockNr ( ) ;
int handler = exc . handler . getBlockNr ( ) ;
int handler = exc . handler . getBlockNr ( ) ;
if ( start > end | | handler < = end )
if ( start > end | | handler < = end )
throw new InternalError
throw new InternalError
( "ExceptionHandler order failed: not "
( "ExceptionHandler order failed: not "
+ start + " < " + end + " <= " + handler ) ;
+ start + " < " + end + " <= " + handler ) ;
if ( last ! = null
if ( last ! = null
& & ( last . start . getBlockNr ( ) ! = start
& & ( last . start . getBlockNr ( ) ! = start
| | last . end . getBlockNr ( ) ! = end ) ) {
| | last . end . getBlockNr ( ) ! = end ) ) {
/ * The last handler does catch another range .
/ * The last handler does catch another range .
* Due to the order :
* Due to the order :
* start < last . start . getBlockNr ( )
* start < last . start . getBlockNr ( )
* | | end > last . end . getBlockNr ( )
* | | end > last . end . getBlockNr ( )
* /
* /
if ( end > = last . start . getBlockNr ( )
if ( end > = last . start . getBlockNr ( )
& & end < last . end . getBlockNr ( ) )
& & end < last . end . getBlockNr ( ) )
throw new InternalError
throw new InternalError
( "Exception handlers ranges are intersecting: ["
( "Exception handlers ranges are intersecting: ["
+ last . start . getBlockNr ( ) + ", "
+ last . start . getBlockNr ( ) + ", "
+ last . end . getBlockNr ( ) + "] and ["
+ last . end . getBlockNr ( ) + "] and ["
+ start + ", " + end + "]." ) ;
+ start + ", " + end + "]." ) ;
}
last = exc ;
}
}
last = exc ;
}
}
}
{
/ * *
Iterator i = handlers . iterator ( ) ;
* Analyzes all exception handlers to try / catch / finally or
Handler exc = null ;
* synchronized blocks .
Handler next = i . hasNext ( ) ? ( Handler ) i . next ( ) : null ;
* /
while ( next ! = null ) {
public void analyze ( ) {
Handler last = exc ;
checkTryCatchOrder ( ) ;
exc = next ;
int startNr = exc . start . getBlockNr ( ) ;
Iterator i = handlers . iterator ( ) ;
int endNr = exc . end . getBlockNr ( ) ;
Handler exc = null ;
next = i . hasNext ( ) ? ( Handler ) i . next ( ) : null ;
Handler next = i . hasNext ( ) ? ( Handler ) i . next ( ) : null ;
int endHandler = Integer . MAX_VALUE ;
while ( next ! = null ) {
/ * If the next exception handler catches a bigger range
Handler last = exc ;
* it must surround the handler completely .
exc = next ;
next = i . hasNext ( ) ? ( Handler ) i . next ( ) : null ;
int startNr = exc . start . getBlockNr ( ) ;
int endNr = exc . end . getBlockNr ( ) ;
int endHandler = Integer . MAX_VALUE ;
/ * If the next exception handler catches a bigger range
* it must surround the handler completely .
* /
if ( next ! = null
& & next . end . getBlockNr ( ) > endNr )
endHandler = next . end . getBlockNr ( ) + 1 ;
FlowBlock tryFlow = exc . start ;
tryFlow . checkConsistent ( ) ;
if ( last = = null | | exc . type = = null
| | last . start . getBlockNr ( ) ! = startNr
| | last . end . getBlockNr ( ) ! = endNr ) {
/ * The last handler does catch another range .
* Create a new try block .
* /
* /
if ( next ! = null
if ( ( GlobalOptions . debuggingFlags
& & next . end . getBlockNr ( ) > endNr )
& GlobalOptions . DEBUG_ANALYZE ) ! = 0 )
endHandler = next . end . getBlockNr ( ) + 1 ;
GlobalOptions . err . println
( "analyzeTry(" + startNr + ", " + endNr + ")" ) ;
FlowBlock tryFlow = exc . start ;
while ( true ) {
tryFlow . checkConsistent ( ) ;
while ( tryFlow . analyze ( startNr , endNr + 1 ) ) ;
int nextNr = tryFlow . getNextBlockNr ( ) ;
if ( last = = null | | exc . type = = null
if ( nextNr > endNr )
| | last . start . getBlockNr ( ) ! = startNr
break ;
| | last . end . getBlockNr ( ) ! = endNr ) {
tryFlow = flowBlocks [ nextNr ] ;
/ * The last handler does catch another range .
* Create a new try block .
* /
if ( ( GlobalOptions . debuggingFlags
& GlobalOptions . DEBUG_ANALYZE ) ! = 0 )
GlobalOptions . err . println
( "analyzeTry(" + startNr + ", " + endNr + ")" ) ;
while ( true ) {
while ( tryFlow . analyze ( startNr ,
endNr + 1 ) ) ;
int nextNr = tryFlow . getNextBlockNr ( ) ;
if ( nextNr > endNr )
break ;
tryFlow = flowBlocks [ nextNr ] ;
}
if ( tryFlow . getBlockNr ( ) ! = startNr )
GlobalOptions . err . println
( "Warning: Can't completely analyze try." ) ;
TryBlock tryBlock = new TryBlock ( tryFlow ) ;
} else if ( ! ( tryFlow . block instanceof TryBlock ) )
throw new InternalError ( "no TryBlock" ) ;
FlowBlock catchFlow = exc . handler ;
boolean isMultiUsed = catchFlow . predecessors . size ( ) ! = 0 ;
if ( ! isMultiUsed & & next ! = null ) {
for ( Iterator j = handlers . tailSet ( next ) . iterator ( ) ;
j . hasNext ( ) ; ) {
Handler h = ( Handler ) j . next ( ) ;
if ( h . handler = = catchFlow ) {
isMultiUsed = true ;
break ;
}
}
}
}
if ( tryFlow . getBlockNr ( ) ! = startNr ) {
if ( isMultiUsed ) {
GlobalOptions . err . println
/ * If this exception is used in other exception handlers ,
( "Warning: Can't completely analyze try." ) ;
* create a new flow block , that jumps to the handler .
* This will be our new exception handler .
* /
FlowBlock newFlow = new FlowBlock
( catchFlow . method , catchFlow . getBlockNr ( ) ,
catchFlow . prevByCodeOrder ) ;
newFlow . setSuccessors ( new FlowBlock [ ] { catchFlow } ) ;
newFlow . nextByCodeOrder = catchFlow ;
catchFlow . prevByCodeOrder = newFlow ;
catchFlow = newFlow ;
} else {
if ( ( GlobalOptions . debuggingFlags
& GlobalOptions . DEBUG_ANALYZE ) ! = 0 )
GlobalOptions . err . println
( "analyzeCatch("
+ catchFlow . getBlockNr ( ) + ", " + endHandler + ")" ) ;
while ( catchFlow . analyze ( catchFlow . getBlockNr ( ) ,
endHandler ) ) ;
}
}
TryBlock tryBlock = new TryBlock ( tryFlow ) ;
} else if ( ! ( tryFlow . block instanceof TryBlock ) )
throw new InternalError ( "no TryBlock" ) ;
FlowBlock catchFlow = exc . handler ;
boolean isMultiUsed = catchFlow . predecessors . size ( ) ! = 0 ;
if ( ! isMultiUsed & & next ! = null ) {
for ( Iterator j = handlers . tailSet ( next ) . iterator ( ) ;
j . hasNext ( ) ; ) {
Handler h = ( Handler ) j . next ( ) ;
if ( h . handler = = catchFlow ) {
isMultiUsed = true ;
break ;
}
}
}
/* Merge the try-block with the catch-block now */
if ( isMultiUsed ) {
tryFlow . updateInOutCatch ( catchFlow ) ;
/ * If this exception is used in other exception handlers ,
tryFlow . mergeSuccessors ( catchFlow ) ;
* create a new flow block , that jumps to the handler .
tryFlow . mergeBlockNr ( catchFlow ) ;
* This will be our new exception handler .
if ( exc . type ! = null )
* /
analyzeCatchBlock ( exc . type , tryFlow , catchFlow . block ) ;
FlowBlock newFlow = new FlowBlock
( catchFlow . method , catchFlow . getBlockNr ( ) ,
else if ( ! analyzeSynchronized ( tryFlow , catchFlow . block ,
catchFlow . prevByCodeOrder ) ;
endHandler )
newFlow . setSuccessors ( new FlowBlock [ ] { catchFlow } ) ;
& & ! analyzeFinally ( tryFlow , catchFlow . block ,
newFlow . nextByCodeOrder = catchFlow ;
endHandler )
catchFlow . prevByCodeOrder = newFlow ;
& & ! analyzeSpecialFinally ( tryFlow , catchFlow . block ,
catchFlow = newFlow ;
endHandler ) )
} else {
analyzeCatchBlock ( Type . tObject , tryFlow , catchFlow . block ) ;
tryFlow . checkConsistent ( ) ;
if ( ( GlobalOptions . debuggingFlags
if ( ( GlobalOptions . debuggingFlags
& GlobalOptions . DEBUG_ANALYZE ) ! = 0 )
& GlobalOptions . DEBUG_ANALYZE ) ! = 0 )
GlobalOptions . err . println
GlobalOptions . err . println
( "analyzeTryCatch(" + tryFlow . getBlockNr ( ) + ", "
( "analyzeCatch("
+ tryFlow . getNextBlockNr ( ) + ") done." ) ;
+ catchFlow . getBlockNr ( ) + ", " + endHandler + ")" ) ;
while ( catchFlow . analyze ( catchFlow . getBlockNr ( ) ,
endHandler ) ) ;
}
}
if ( exc . type ! = null )
analyzeCatchBlock ( exc . type , tryFlow , catchFlow ) ;
else if ( ! analyzeSynchronized ( tryFlow , catchFlow , endHandler )
& & ! analyzeFinally ( tryFlow , catchFlow , endHandler )
& & ! analyzeSpecialFinally ( tryFlow , catchFlow ,
endHandler ) )
/ * As last resort make a catch ( Object ) block . This doesn ' t
* compile , but at least it gives a hint what the code
* does .
* /
analyzeCatchBlock ( Type . tObject , tryFlow , catchFlow ) ;
tryFlow . checkConsistent ( ) ;
if ( ( GlobalOptions . debuggingFlags
& GlobalOptions . DEBUG_ANALYZE ) ! = 0 )
GlobalOptions . err . println
( "analyzeTryCatch(" + tryFlow . getBlockNr ( ) + ", "
+ tryFlow . getNextBlockNr ( ) + ") done." ) ;
}
}
}
}
}
}