@ -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 .
tryFlow . removeSuccessor ( jumps ) ;
* /
prev . removeJump ( ) ;
DescriptionBlock msg
prev . outer . removeBlock ( ) ;
= new DescriptionBlock ( "ERROR: JSR FINALLY BLOCK!" ) ;
continue ;
tryFlow . removeSuccessor ( jumps ) ;
}
prev . removeJump ( ) ;
if ( prev . outer . outer instanceof SequentialBlock
msg . replace ( prev . outer ) ;
& & prev . outer . outer . getSubBlocks ( ) [ 0 ] = = prev . outer ) {
} else {
SequentialBlock seq = ( SequentialBlock ) prev . outer . outer ;
/ * We have a jump to the subroutine , that is wrong .
if ( seq . subBlocks [ 1 ] instanceof JsrBlock
* We complain here .
| | ( seq . subBlocks [ 1 ] instanceof SequentialBlock
* /
& & seq . subBlocks [ 1 ] . getSubBlocks ( ) [ 0 ]
DescriptionBlock msg
instanceof JsrBlock ) ) {
= new DescriptionBlock ( "ERROR: GOTO FINALLY BLOCK!" ) ;
/* The jsr is followed by a jsr, okay. */
tryFlow . removeSuccessor ( jumps ) ;
tryFlow . removeSuccessor ( jumps ) ;
prev . removeJump ( ) ;
prev . removeJump ( ) ;
prev . appendBlock ( msg ) ;
prev . outer . removeBlock ( ) ;
}
continue ;
}
}
}
if ( seq . subBlocks [ 1 ] instanceof ReturnBlock
& & ! ( seq . subBlocks [ 1 ] instanceof ThrowBlock ) ) {
private static StructuredBlock getPredecessor ( StructuredBlock stmt )
{
/* The jsr is followed by a return, okay. */
if ( stmt . outer instanceof SequentialBlock ) {
tryFlow . removeSuccessor ( jumps ) ;
SequentialBlock seq = ( SequentialBlock ) stmt . outer ;
prev . removeJump ( ) ;
if ( seq . subBlocks [ 1 ] = = stmt )
ReturnBlock ret = ( ReturnBlock ) seq . subBlocks [ 1 ] ;
return seq . subBlocks [ 0 ] ;
prev . outer . removeBlock ( ) ;
else if ( seq . outer instanceof SequentialBlock )
return seq . outer . getSubBlocks ( ) [ 0 ] ;
removeReturnLocal ( ret ) ;
}
continue ;
return null ;
}
}
}
/ * *
* 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 ( ) ;
}
}
/ * Now we have a jump to the subroutine , that is wrong .
* We complain here .
* /
DescriptionBlock msg
= new DescriptionBlock ( "ERROR: GOTO FINALLY BLOCK!" ) ;
tryFlow . removeSuccessor ( jumps ) ;
prev . removeJump ( ) ;
prev . appendBlock ( msg ) ;
}
}
return - 1 ;
}
}
public void checkAndRemoveJSR ( FlowBlock tryFlow ,
private boolean isMonitorExitSubRoutine ( FlowBlock subRoutine ,
StructuredBlock catchBlock ,
LocalInfo local ) {
FlowBlock subRoutine ,
if ( transformSubRoutine ( subRoutine . block )
int startOutExit , int endOutExit ) {
& & getMonitorExitSlot ( subRoutine . block ) = = local . getSlot ( ) )
boolean foundSub = false ;
return true ;
return false ;
}
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 ;
}
private void checkAndRemoveJSR ( FlowBlock tryFlow ,
StructuredBlock catchBlock ,
FlowBlock subRoutine ,
int startOutExit , int endOutExit ) {
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 ;
continue ;
}
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 ;
}
if ( seq . subBlocks [ 0 ] = = prev
& & seq . outer instanceof SequentialBlock
& & ( seq . outer . getSubBlocks ( ) [ 0 ] instanceof JsrBlock ) ) {
/ * Again the jump is preceeded by another jsr , okay .
* /
continue ;
}
}
}
StructuredBlock pred = skipFinExitChain ( prev ) ;
if ( pred instanceof JsrBlock ) {
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 ;
}
}
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,45 +379,21 @@ public class TransformExceptionHandlers {
* /
* /
DescriptionBlock msg
DescriptionBlock msg
= new DescriptionBlock ( "ERROR: NO JSR TO FINALLY" ) ;
= new DescriptionBlock ( "ERROR: NO JSR TO FINALLY" ) ;
prev . appendBlock ( msg ) ;
if ( pred ! = null )
msg . moveJump ( jumps ) ;
pred . prependBlock ( msg ) ;
}
else {
}
prev . appendBlock ( msg ) ;
if ( foundSub )
msg . moveJump ( prev . jump ) ;
removeJSR ( 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 ;
removeBadJSR ( tryFlow , catchBlock , subRoutine ) ;
}
}
boolean isMonitorExitSubRoutine ( FlowBlock subRoutine , LocalInfo local ) {
private void checkAndRemoveMonitorExit ( FlowBlock tryFlow ,
if ( transformSubRoutine ( subRoutine . block ) ) {
StructuredBlock catchBlock ,
if ( subRoutine . block instanceof InstructionBlock ) {
LocalInfo local ,
Expression instr =
int start , int end ) {
( ( InstructionBlock ) subRoutine . block )
. getInstruction ( ) ;
if ( isMonitorExit ( instr , local ) ) {
return true ;
}
}
}
return false ;
}
public void checkAndRemoveMonitorExit ( FlowBlock tryFlow ,
StructuredBlock catchBlock ,
LocalInfo local ,
int start , int end ) {
FlowBlock subRoutine = null ;
FlowBlock subRoutine = null ;
Iterator succs = tryFlow . getSuccessors ( ) . iterator ( ) ;
Iterator succs = tryFlow . getSuccessors ( ) . iterator ( ) ;
dest_loop :
dest_loop :
@ -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 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 the block is a jsr or a return block , check if
* it is preceeded by another jsr .
* /
if ( ( prev instanceof JsrBlock
| | prev instanceof ReturnBlock )
& & prev . outer instanceof SequentialBlock ) {
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 ( pred instanceof JsrBlock )
/ * The jump is preceeded by another jsr , okay .
* /
continue ;
if ( pred instanceof InstructionBlock ) {
Expression instr =
( ( InstructionBlock ) pred ) . getInstruction ( ) ;
if ( isMonitorExit ( instr , local ) ) {
pred . removeBlock ( ) ;
if ( prev instanceof ReturnBlock )
removeReturnLocal ( ( ReturnBlock ) prev ) ;
continue ;
}
}
}
}
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 ( ) ;
continue ;
}
if ( isFirstJump ) {
/ * This is the first jump to that destination .
* Check if the destination does the monitorExit
* /
* /
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 ;
/ * 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
if ( subRoutine = = null
& & successor . getAddr ( ) > = start
& & dest . getAddr ( ) > = start
& & successor . getNextAddr ( ) < = end ) {
& & dest . getNextAddr ( ) < = end ) {
successor . analyze ( start , end ) ;
dest . analyze ( start , end ) ;
if ( isMonitorExitSubRoutine ( dest , local ) )
if ( isMonitorExitSubRoutine ( successor , local ) )
subRoutine = dest ;
subRoutine = successor ;
}
}
if ( subRoutine = = successor )
if ( dest = = subRoutine ) {
continue dest_loop ;
/ * 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 ;
}
}
}
} else if ( getMonitorExitSlot ( pred ) = = local . getSlot ( ) ) {
/ * The jump is preceeded by the right monitor
* exit instruction .
* /
pred . removeBlock ( ) ;
if ( prev instanceof ReturnBlock )
removeReturnLocal ( ( ReturnBlock ) prev ) ;
continue ;
}
if ( pred = = null & & isFirstJump ) {
/ * 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,17 +482,15 @@ 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 )
sb . removeBlock ( ) ;
. getInstruction ( ) ;
continue dest_loop ;
if ( isMonitorExit ( instr , local ) ) {
sb . removeBlock ( ) ;
continue dest_loop ;
}
}
}
}
}
}
}
@ -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 ) ;
}
}
}
}