@ -180,11 +180,7 @@ public class TransformExceptionHandlers {
* @param ret the ReturnBlock .
* @param ret the ReturnBlock .
* /
* /
private void removeReturnLocal ( ReturnBlock ret ) {
private void removeReturnLocal ( ReturnBlock ret ) {
if ( ret . outer = = null
StructuredBlock pred = getPredecessor ( ret ) ;
| | ! ( ret . outer instanceof SequentialBlock ) )
return ;
StructuredBlock pred = ret . outer . getSubBlocks ( ) [ 0 ] ;
if ( ! ( pred instanceof InstructionBlock ) )
if ( ! ( pred instanceof InstructionBlock ) )
return ;
return ;
Expression instr = ( ( InstructionBlock ) pred ) . getInstruction ( ) ;
Expression instr = ( ( InstructionBlock ) pred ) . getInstruction ( ) ;
@ -203,15 +199,13 @@ public class TransformExceptionHandlers {
}
}
/ * *
/ * *
* Remove the JSR ' s jumping to the specified subRoutine . It
* Remove the wrongly placed JSRs jumping to the specified
* is checked if the next block is a leaving instruction , and
* subRoutine . The right JSRs are already removed , but we have to
* otherwise the JsrBlock is not removed ( to give the user a
* replace the wrong ones with a warning .
* hint that something went wrong ) . This will also remove the
* local javac generates for returns .
* @param tryFlow the FlowBLock of the try block .
* @param tryFlow the FlowBLock 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 removeBad JSR ( FlowBlock tryFlow , StructuredBlock catchBlock ,
FlowBlock subRoutine ) {
FlowBlock subRoutine ) {
Jump nextJump ;
Jump nextJump ;
for ( Jump jumps = tryFlow . getJumps ( subRoutine ) ;
for ( Jump jumps = tryFlow . getJumps ( subRoutine ) ;
@ -225,64 +219,96 @@ public class TransformExceptionHandlers {
/* This is the mandatory jsr in the catch block */
/* This is the mandatory jsr in the catch block */
continue ;
continue ;
}
}
if ( prev . outer . getNextFlowBlock ( ) ! = null ) {
/ * We have a JSR to the subroutine , which is badly placed .
/* The jsr is directly before a jump, okay. */
* We complain here .
* /
DescriptionBlock msg
= new DescriptionBlock ( "ERROR: JSR FINALLY BLOCK!" ) ;
tryFlow . removeSuccessor ( jumps ) ;
tryFlow . removeSuccessor ( jumps ) ;
prev . removeJump ( ) ;
prev . removeJump ( ) ;
prev . outer . removeBlock ( ) ;
msg . replace ( prev . outer ) ;
continue ;
} else {
}
/ * We have a jump to the subroutine , that is wrong .
if ( prev . outer . outer instanceof SequentialBlock
* We complain here .
& & prev . outer . outer . getSubBlocks ( ) [ 0 ] = = prev . outer ) {
* /
SequentialBlock seq = ( SequentialBlock ) prev . outer . outer ;
DescriptionBlock msg
if ( seq . subBlocks [ 1 ] instanceof JsrBlock
= new DescriptionBlock ( "ERROR: GOTO FINALLY BLOCK!" ) ;
| | ( seq . subBlocks [ 1 ] instanceof SequentialBlock
& & seq . subBlocks [ 1 ] . getSubBlocks ( ) [ 0 ]
instanceof JsrBlock ) ) {
/* The jsr is followed by a jsr, okay. */
tryFlow . removeSuccessor ( jumps ) ;
tryFlow . removeSuccessor ( jumps ) ;
prev . removeJump ( ) ;
prev . removeJump ( ) ;
prev . outer . removeBlock ( ) ;
prev . appendBlock ( msg ) ;
continue ;
}
}
}
}
if ( seq . subBlocks [ 1 ] instanceof ReturnBlock
& & ! ( seq . subBlocks [ 1 ] instanceof ThrowBlock ) ) {
/* The jsr is followed by a return, okay. */
private static StructuredBlock getPredecessor ( StructuredBlock stmt )
tryFlow . removeSuccessor ( jumps ) ;
{
prev . removeJump ( ) ;
if ( stmt . outer instanceof SequentialBlock ) {
ReturnBlock ret = ( ReturnBlock ) seq . subBlocks [ 1 ] ;
SequentialBlock seq = ( SequentialBlock ) stmt . outer ;
prev . outer . removeBlock ( ) ;
if ( seq . subBlocks [ 1 ] = = stmt )
return seq . subBlocks [ 0 ] ;
else if ( seq . outer instanceof SequentialBlock )
return seq . outer . getSubBlocks ( ) [ 0 ] ;
}
return null ;
}
removeReturnLocal ( ret ) ;
/ * *
continue ;
* Gets the slot of the monitorexit instruction instr in the
* stmt , or - 1 if stmt isn ' t a InstructionBlock with a
* monitorexit instruction .
* @param stmt the stmt , may be null .
* /
private static int getMonitorExitSlot ( StructuredBlock stmt ) {
if ( stmt instanceof InstructionBlock ) {
Expression instr = ( ( InstructionBlock ) stmt ) . getInstruction ( ) ;
if ( instr instanceof MonitorExitOperator ) {
MonitorExitOperator monExit = ( MonitorExitOperator ) instr ;
if ( monExit . getFreeOperandCount ( ) = = 0
& & ( monExit . getSubExpressions ( ) [ 0 ]
instanceof LocalLoadOperator ) )
return ( ( LocalLoadOperator ) monExit . getSubExpressions ( ) [ 0 ] )
. getLocalInfo ( ) . getSlot ( ) ;
}
}
}
}
return - 1 ;
}
}
/ * Now we have a jump to the subroutine , that is wrong .
* We complain here .
private boolean isMonitorExitSubRoutine ( FlowBlock subRoutine ,
* /
LocalInfo local ) {
DescriptionBlock msg
if ( transformSubRoutine ( subRoutine . block )
= new DescriptionBlock ( "ERROR: GOTO FINALLY BLOCK!" ) ;
& & getMonitorExitSlot ( subRoutine . block ) = = local . getSlot ( ) )
tryFlow . removeSuccessor ( jumps ) ;
return true ;
prev . removeJump ( ) ;
return false ;
prev . appendBlock ( msg ) ;
}
}
private static StructuredBlock skipFinExitChain ( StructuredBlock block )
{
StructuredBlock pred , result ;
if ( block instanceof ReturnBlock )
pred = getPredecessor ( block ) ;
else
pred = block ;
result = null ;
while ( pred instanceof JsrBlock
| | getMonitorExitSlot ( pred ) > = 0 ) {
result = pred ;
pred = getPredecessor ( pred ) ;
}
}
return result ;
}
public void checkAndRemoveJSR ( FlowBlock tryFlow ,
private void checkAndRemoveJSR ( FlowBlock tryFlow ,
StructuredBlock catchBlock ,
StructuredBlock catchBlock ,
FlowBlock subRoutine ,
FlowBlock subRoutine ,
int startOutExit , int endOutExit ) {
int startOutExit , int endOutExit ) {
boolean foundSub = false ;
Iterator iter = tryFlow . getSuccessors ( ) . iterator ( ) ;
Iterator iter = tryFlow . getSuccessors ( ) . iterator ( ) ;
dest_loop :
dest_loop :
while ( iter . hasNext ( ) ) {
while ( iter . hasNext ( ) ) {
FlowBlock dest = ( FlowBlock ) iter . next ( ) ;
FlowBlock dest = ( FlowBlock ) iter . next ( ) ;
if ( dest = = subRoutine ) {
if ( dest = = subRoutine )
foundSub = true ;
continue dest_loop ;
continue dest_loop ;
}
boolean isFirstJump = true ;
boolean isFirstJump = true ;
for ( Jump jumps = tryFlow . getJumps ( dest ) ;
for ( Jump jumps = tryFlow . getJumps ( dest ) ;
@ -295,41 +321,34 @@ public class TransformExceptionHandlers {
* /
* /
continue ;
continue ;
}
}
if ( prev instanceof JsrBlock ) {
/ * The jump is directly preceeded by a jsr .
* Everything okay .
* /
continue ;
}
if ( prev instanceof EmptyBlock
if ( prev instanceof EmptyBlock
& & prev . outer instanceof JsrBlock ) {
& & prev . outer instanceof JsrBlock ) {
/ * If jump is a jsr check the outer
/ * This jump is really a jsr , since it doesn ' t
* block instead .
* leave the block forever , we can ignore it .
* /
prev = prev . outer ;
}
if ( ( prev instanceof ReturnBlock
| | prev instanceof JsrBlock )
& & prev . outer instanceof SequentialBlock ) {
SequentialBlock seq = ( SequentialBlock ) prev . outer ;
if ( seq . subBlocks [ 1 ] = = prev
& & ( seq . subBlocks [ 0 ] instanceof JsrBlock ) ) {
/ * The jump is preceeded by another jsr , okay .
* /
* /
continue ;
continue ;
}
}
if ( seq . subBlocks [ 0 ] = = prev
StructuredBlock pred = skipFinExitChain ( prev ) ;
& & seq . outer instanceof SequentialBlock
& & ( seq . outer . getSubBlocks ( ) [ 0 ] instanceof JsrBlock ) ) {
if ( pred instanceof JsrBlock ) {
/ * Again the jump is preceeded by another jsr , okay .
StructuredBlock jsrInner = ( ( JsrBlock ) pred ) . innerBlock ;
if ( jsrInner instanceof EmptyBlock
& & jsrInner . jump ! = null
& & jsrInner . jump . destination = = subRoutine ) {
/ * The jump is preceeded by the right jsr . Remove
* the jsr .
* /
* /
tryFlow . removeSuccessor ( jsrInner . jump ) ;
jsrInner . removeJump ( ) ;
pred . removeBlock ( ) ;
if ( prev instanceof ReturnBlock )
removeReturnLocal ( ( ReturnBlock ) prev ) ;
continue ;
continue ;
}
}
}
}
if ( isFirstJump ) {
if ( pred = = null & & isFirstJump ) {
/ * Now we have a jump that is not preceded by the
/ * Now we have a jump that is not preceded by any
* jsr . There ' s a last chance : the jump jumps
* jsr . There ' s a last chance : the jump jumps
* directly to a correct jsr instruction , which
* directly to a correct jsr instruction , which
* lies outside the try / catch block .
* lies outside the try / catch block .
@ -360,42 +379,18 @@ public class TransformExceptionHandlers {
* /
* /
DescriptionBlock msg
DescriptionBlock msg
= new DescriptionBlock ( "ERROR: NO JSR TO FINALLY" ) ;
= new DescriptionBlock ( "ERROR: NO JSR TO FINALLY" ) ;
if ( pred ! = null )
pred . prependBlock ( msg ) ;
else {
prev . appendBlock ( msg ) ;
prev . appendBlock ( msg ) ;
msg . moveJump ( jumps ) ;
msg . moveJump ( prev . jump ) ;
}
}
}
}
if ( foundSub )
removeJSR ( tryFlow , catchBlock , subRoutine ) ;
}
}
removeBadJSR ( tryFlow , catchBlock , subRoutine ) ;
static boolean isMonitorExit ( Expression instr , LocalInfo local ) {
if ( instr instanceof MonitorExitOperator ) {
MonitorExitOperator monExit = ( MonitorExitOperator ) instr ;
if ( monExit . getFreeOperandCount ( ) = = 0
& & monExit . getSubExpressions ( ) [ 0 ] instanceof LocalLoadOperator
& & ( ( ( LocalLoadOperator ) monExit . getSubExpressions ( ) [ 0 ] )
. getLocalInfo ( ) . getSlot ( ) = = local . getSlot ( ) ) ) {
return true ;
}
}
return false ;
}
boolean isMonitorExitSubRoutine ( FlowBlock subRoutine , LocalInfo local ) {
if ( transformSubRoutine ( subRoutine . block ) ) {
if ( subRoutine . block instanceof InstructionBlock ) {
Expression instr =
( ( InstructionBlock ) subRoutine . block )
. getInstruction ( ) ;
if ( isMonitorExit ( instr , local ) ) {
return true ;
}
}
}
return false ;
}
}
public void checkAndRemoveMonitorExit ( FlowBlock tryFlow ,
private void checkAndRemoveMonitorExit ( FlowBlock tryFlow ,
StructuredBlock catchBlock ,
StructuredBlock catchBlock ,
LocalInfo local ,
LocalInfo local ,
int start , int end ) {
int start , int end ) {
@ -409,91 +404,57 @@ public class TransformExceptionHandlers {
jumps ! = null ; jumps = jumps . next , isFirstJump = false ) {
jumps ! = null ; jumps = jumps . next , isFirstJump = false ) {
StructuredBlock prev = jumps . prev ;
StructuredBlock prev = jumps . prev ;
if ( prev instanceof ThrowBlock ) {
if ( prev instanceof ThrowBlock ) {
/ * The jump is a throw . We have a catch all block
/ * The jump is a throw . We have a catch - all block
* that will do the monitorexit .
* that will do the finally .
* /
continue ;
}
if ( prev instanceof JsrBlock ) {
/ * The jump is directly preceeded by a jsr .
* /
* /
continue ;
continue ;
}
}
if ( prev instanceof EmptyBlock
if ( prev instanceof EmptyBlock
& & prev . outer instanceof JsrBlock ) {
& & prev . outer instanceof JsrBlock ) {
/ * If jump is a jsr ch eck the out er
/ * This jump is really a jsr , sin ce i t d oesn ' t
* block instead .
* leave the block forever , we can ignore it .
* /
* /
prev = prev . outer ;
continue ;
}
}
StructuredBlock pred = skipFinExitChain ( prev ) ;
if ( pred instanceof JsrBlock ) {
StructuredBlock jsrInner = ( ( JsrBlock ) pred ) . innerBlock ;
if ( jsrInner instanceof EmptyBlock
& & jsrInner . jump ! = null ) {
FlowBlock dest = jsrInner . jump . destination ;
/ * If the block is a jsr or a return block , check if
if ( subRoutine = = null
* it is preceeded by another jsr .
& & dest . getAddr ( ) > = start
* /
& & dest . getNextAddr ( ) < = end ) {
if ( ( prev instanceof JsrBlock
dest . analyze ( start , end ) ;
| | prev instanceof ReturnBlock )
if ( isMonitorExitSubRoutine ( dest , local ) )
& & prev . outer instanceof SequentialBlock ) {
subRoutine = dest ;
SequentialBlock seq = ( SequentialBlock ) prev . outer ;
}
StructuredBlock pred = null ;
if ( seq . subBlocks [ 1 ] = = prev )
pred = seq . subBlocks [ 0 ] ;
else if ( seq . outer instanceof SequentialBlock )
pred = seq . outer . getSubBlocks ( ) [ 0 ] ;
if ( pred ! = null ) {
if ( dest = = subRoutine ) {
if ( pred instanceof JsrBlock )
/ * The jump is preceeded by the right jsr . Remove
/ * The jump is preceeded by another jsr , okay .
* the jsr .
* /
* /
continue ;
tryFlow . removeSuccessor ( jsrInner . jump ) ;
jsrInner . removeJump ( ) ;
if ( pred instanceof InstructionBlock ) {
Expression instr =
( ( InstructionBlock ) pred ) . getInstruction ( ) ;
if ( isMonitorExit ( instr , local ) ) {
pred . removeBlock ( ) ;
pred . removeBlock ( ) ;
if ( prev instanceof ReturnBlock )
if ( prev instanceof ReturnBlock )
removeReturnLocal ( ( ReturnBlock ) prev ) ;
removeReturnLocal ( ( ReturnBlock ) prev ) ;
continue ;
continue ;
}
}
}
}
}
} else if ( getMonitorExitSlot ( pred ) = = local . getSlot ( ) ) {
}
/ * The jump is preceeded by the right monitor
* exit instruction .
if ( prev instanceof InstructionBlock
& & isMonitorExit ( ( ( InstructionBlock ) prev ) . instr , local ) ) {
/ * This is probably the last expression in the
* synchronized block , and has the right monitor exit
* attached . Remove this block .
* /
* /
prev . removeBlock ( ) ;
pred . removeBlock ( ) ;
if ( prev instanceof ReturnBlock )
removeReturnLocal ( ( ReturnBlock ) prev ) ;
continue ;
continue ;
}
}
if ( isFirstJump ) {
if ( pred = = null & & isFirstJump ) {
/ * This is the first jump to that destination .
* Check if the destination does the monitorExit
* /
/ * The block is a jsr that is not preceeded by
* another jsr . This must be the monitorexit
* subroutine .
* /
if ( prev instanceof JsrBlock ) {
if ( subRoutine = = null
& & successor . getAddr ( ) > = start
& & successor . getNextAddr ( ) < = end ) {
successor . analyze ( start , end ) ;
if ( isMonitorExitSubRoutine ( successor , local ) )
subRoutine = successor ;
}
if ( subRoutine = = successor )
continue dest_loop ;
}
/ * Now we have a jump that is not preceded by a
/ * Now we have a jump that is not preceded by a
* monitorexit . There ' s a last chance : the jump
* monitorexit . There ' s a last chance : the jump
* jumps directly to the correct monitorexit
* jumps directly to the correct monitorexit
@ -521,20 +482,18 @@ public class TransformExceptionHandlers {
}
}
if ( subRoutine = = dest ) {
if ( subRoutine = = dest ) {
successor . removeSuccessor ( jsrInner . jump ) ;
jsrInner . removeJump ( ) ;
sb . removeBlock ( ) ;
sb . removeBlock ( ) ;
continue dest_loop ;
continue dest_loop ;
}
}
}
}
if ( sb instanceof InstructionBlock ) {
if ( getMonitorExitSlot ( sb ) = = local . getSlot ( ) ) {
Expression instr = ( ( InstructionBlock ) sb )
. getInstruction ( ) ;
if ( isMonitorExit ( instr , local ) ) {
sb . removeBlock ( ) ;
sb . removeBlock ( ) ;
continue dest_loop ;
continue dest_loop ;
}
}
}
}
}
}
}
/ * Complain !
/ * Complain !
* /
* /
@ -546,7 +505,7 @@ public class TransformExceptionHandlers {
}
}
if ( subRoutine ! = null ) {
if ( subRoutine ! = null ) {
removeJSR ( tryFlow , catchBlock , subRoutine ) ;
removeBad JSR ( tryFlow , catchBlock , subRoutine ) ;
tryFlow . mergeAddr ( subRoutine ) ;
tryFlow . mergeAddr ( subRoutine ) ;
}
}
}
}