de.fub.bytecode.generic
Class InstructionList

java.lang.Object
  |
  +--de.fub.bytecode.generic.InstructionList

public class InstructionList
extends java.lang.Object
implements Constants, java.io.Serializable

This class is a container for a list of `Instruction's, instructions can be appended, inserted, deleted, etc.. Instructions are being wrapped into InstructionHandle objects that are returned upon append/insert operations. They give the user (read only) access to the list structure, it can be traversed and manipulated in a controlled way. A list is finally dumped to a byte code array with `getByteCode'.

Version:
$Id: InstructionList.java,v 1.18 1999/09/22 13:00:10 dahm Exp $
Author:
M. Dahm
See Also:
Instruction, InstructionHandle, BranchHandle, Serialized Form

Constructor Summary
InstructionList()
          Create (empty) instruction list.
InstructionList(BranchInstruction i)
          Create instruction list containing one instruction.
InstructionList(byte[] code)
          Initialize instruction list from byte array.
InstructionList(CompoundInstruction c)
          Initialize list with (nonnull) compound instruction.
InstructionList(Instruction i)
          Create instruction list containing one instruction.
 
Method Summary
 BranchHandle append(BranchInstruction i)
          Append a branch instruction to the end of this list.
 InstructionHandle append(CompoundInstruction c)
          Append a compound instruction.
 InstructionHandle append(Instruction i)
          Append an instruction to the end of this list.
 InstructionHandle append(Instruction i, CompoundInstruction c)
          Append a compound instruction, after instruction i.
 BranchHandle append(InstructionHandle ih, BranchInstruction i)
          Append an instruction after instruction (handle) ih contained in this list.
 InstructionHandle append(InstructionHandle ih, Instruction i)
          Append an instruction after instruction (handle) ih contained in this list.
 InstructionHandle append(InstructionHandle ih, InstructionList il)
          Append another list after instruction (handle) ih contained in this list.
 InstructionHandle append(Instruction i, Instruction j)
          Append a single instruction j after another instruction i, which must be in this list of course!
 InstructionHandle append(Instruction i, InstructionList il)
          Append another list after instruction i contained in this list.
 InstructionHandle append(InstructionList il)
          Append another list to this one.
 boolean contains(Instruction i)
           
 boolean contains(InstructionHandle i)
           
 InstructionList copy()
           
 void delete(Instruction i)
          Remove instruction from this list.
 void delete(InstructionHandle ih)
          Remove instruction from this list.
 void delete(InstructionHandle from, InstructionHandle to)
          Remove instructions from instruction `from' to instruction `to' contained in this list.
 void delete(Instruction from, Instruction to)
          Remove instructions from instruction `from' to instruction `to' contained in this list.
 void dispose()
          Delete contents of list.
 java.util.Enumeration elements()
           
static InstructionHandle findHandle(InstructionHandle[] ihs, int[] pos, int count, int target)
          Find the target instruction (handle) that corresponds to the given target position (byte code offset).
 InstructionHandle findHandle(int pos)
          Get instruction handle for instruction at byte code position pos.
 byte[] getByteCode()
           
 InstructionHandle getEnd()
           
 InstructionHandle[] getInstructionHandles()
           
 int[] getInstructionPositions()
          Get positions (offsets) of all instructions in the list.
 int getLength()
           
 InstructionHandle getStart()
           
 BranchHandle insert(BranchInstruction i)
          Insert a branch instruction at start of this list.
 InstructionHandle insert(CompoundInstruction c)
          Insert a compound instruction.
 InstructionHandle insert(Instruction i)
          Insert an instruction at start of this list.
 InstructionHandle insert(Instruction i, CompoundInstruction c)
          Insert a compound instruction before instruction i.
 BranchHandle insert(InstructionHandle ih, BranchInstruction i)
          Insert an instruction before instruction (handle) ih contained in this list.
 InstructionHandle insert(InstructionHandle ih, Instruction i)
          Insert an instruction before instruction (handle) ih contained in this list.
 InstructionHandle insert(InstructionHandle ih, InstructionList il)
          Insert another list before Instruction handle ih contained in this list.
 InstructionHandle insert(Instruction i, Instruction j)
          Insert a single instruction j before another instruction i, which must be in this list of course!
 InstructionHandle insert(Instruction i, InstructionList il)
          Insert another list before Instruction i contained in this list.
 InstructionHandle insert(InstructionList il)
          Insert another list.
 boolean isEmpty()
          Test for empty list.
 void redirectBranches(InstructionHandle old_target, InstructionHandle new_target)
          Redirect all references from old_target to new_target, i.e.
 void redirectExceptionHandlers(CodeExceptionGen[] exceptions, InstructionHandle old_target, InstructionHandle new_target)
          Redirect all references of exception handlers from old_target to new_target.
 void redirectLocalVariables(LocalVariableGen[] lg, InstructionHandle old_target, InstructionHandle new_target)
          Redirect all references of local variables from old_target to new_target.
 void setPositions()
           
 void setPositions(boolean check)
          Give all instructions their position number (offset in byte stream), i.e.
 int size()
           
 java.lang.String toString()
           
 java.lang.String toString(boolean verbose)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

InstructionList

public InstructionList()
Create (empty) instruction list.

InstructionList

public InstructionList(Instruction i)
Create instruction list containing one instruction.
Parameters:
i - initial instruction

InstructionList

public InstructionList(BranchInstruction i)
Create instruction list containing one instruction.
Parameters:
i - initial instruction

InstructionList

public InstructionList(CompoundInstruction c)
Initialize list with (nonnull) compound instruction. Consumes argument list, i.e. it becomes empty.
Parameters:
c - compound instruction (list)

InstructionList

public InstructionList(byte[] code)
Initialize instruction list from byte array.
Parameters:
code - byte array containing the instructions
Method Detail

isEmpty

public boolean isEmpty()
Test for empty list.

findHandle

public static InstructionHandle findHandle(InstructionHandle[] ihs,
                                           int[] pos,
                                           int count,
                                           int target)
Find the target instruction (handle) that corresponds to the given target position (byte code offset).
Parameters:
ihs - array of instruction handles
pos - array of positions corresponding to ihs
count - length of arrays
target - target position to search for
Returns:
target position's instruction handle if available

findHandle

public InstructionHandle findHandle(int pos)
Get instruction handle for instruction at byte code position pos. This only works, if the list is freshly initialized from a byte array or setPositions() has been called before this method.
Parameters:
pos - byte code position to search for
Returns:
target position's instruction handle if available

append

public InstructionHandle append(InstructionHandle ih,
                                InstructionList il)
Append another list after instruction (handle) ih contained in this list. Consumes argument list, i.e. it becomes empty.
Parameters:
ih - where to append the instruction list
il - Instruction list to append to this one
Returns:
instruction handle pointing to the first appended instruction

append

public InstructionHandle append(Instruction i,
                                InstructionList il)
Append another list after instruction i contained in this list. Consumes argument list, i.e. it becomes empty.
Parameters:
i - where to append the instruction list
il - Instruction list to append to this one
Returns:
instruction handle pointing to the first appended instruction

append

public InstructionHandle append(InstructionList il)
Append another list to this one.
Parameters:
il - list to append to end of this list
Returns:
instruction handle of the first appended instruction

append

public InstructionHandle append(Instruction i)
Append an instruction to the end of this list.
Parameters:
i - instruction to append
Returns:
instruction handle of the appended instruction

append

public BranchHandle append(BranchInstruction i)
Append a branch instruction to the end of this list.
Parameters:
i - branch instruction to append
Returns:
branch instruction handle of the appended instruction

append

public InstructionHandle append(Instruction i,
                                Instruction j)
Append a single instruction j after another instruction i, which must be in this list of course!
Parameters:
i - Instruction in list
j - Instruction to append after i in list
Returns:
instruction handle of the first appended instruction

append

public InstructionHandle append(Instruction i,
                                CompoundInstruction c)
Append a compound instruction, after instruction i.
Parameters:
i - Instruction in list
c - The composite instruction (containing an InstructionList)
Returns:
instruction handle of the first appended instruction

append

public InstructionHandle append(CompoundInstruction c)
Append a compound instruction.
Parameters:
c - The composite instruction (containing an InstructionList)
Returns:
instruction handle of the first appended instruction

append

public InstructionHandle append(InstructionHandle ih,
                                Instruction i)
Append an instruction after instruction (handle) ih contained in this list.
Parameters:
ih - where to append the instruction list
i - Instruction to append
Returns:
instruction handle pointing to the first appended instruction

append

public BranchHandle append(InstructionHandle ih,
                           BranchInstruction i)
Append an instruction after instruction (handle) ih contained in this list.
Parameters:
ih - where to append the instruction list
i - Instruction to append
Returns:
instruction handle pointing to the first appended instruction

insert

public InstructionHandle insert(InstructionHandle ih,
                                InstructionList il)
Insert another list before Instruction handle ih contained in this list. Consumes argument list, i.e. it becomes empty.
Parameters:
i - where to append the instruction list
il - Instruction list to insert
Returns:
instruction handle of the first inserted instruction

insert

public InstructionHandle insert(InstructionList il)
Insert another list.
Parameters:
il - list to insert before start of this list
Returns:
instruction handle of the first inserted instruction

insert

public InstructionHandle insert(Instruction i,
                                InstructionList il)
Insert another list before Instruction i contained in this list. Consumes argument list, i.e. it becomes empty.
Parameters:
i - where to append the instruction list
il - Instruction list to insert
Returns:
instruction handle pointing to the first inserted instruction, i.e. il.getStart()

insert

public InstructionHandle insert(Instruction i)
Insert an instruction at start of this list.
Parameters:
i - instruction to insert
Returns:
instruction handle of the inserted instruction

insert

public BranchHandle insert(BranchInstruction i)
Insert a branch instruction at start of this list.
Parameters:
i - branch instruction to insert
Returns:
branch instruction handle of the appended instruction

insert

public InstructionHandle insert(Instruction i,
                                Instruction j)
Insert a single instruction j before another instruction i, which must be in this list of course!
Parameters:
i - Instruction in list
j - Instruction to insert before i in list
Returns:
instruction handle of the first inserted instruction

insert

public InstructionHandle insert(Instruction i,
                                CompoundInstruction c)
Insert a compound instruction before instruction i.
Parameters:
i - Instruction in list
c - The composite instruction (containing an InstructionList)
Returns:
instruction handle of the first inserted instruction

insert

public InstructionHandle insert(CompoundInstruction c)
Insert a compound instruction.
Parameters:
c - The composite instruction (containing an InstructionList)
Returns:
instruction handle of the first inserted instruction

insert

public InstructionHandle insert(InstructionHandle ih,
                                Instruction i)
Insert an instruction before instruction (handle) ih contained in this list.
Parameters:
ih - where to insert to the instruction list
i - Instruction to insert
Returns:
instruction handle of the first inserted instruction

insert

public BranchHandle insert(InstructionHandle ih,
                           BranchInstruction i)
Insert an instruction before instruction (handle) ih contained in this list.
Parameters:
ih - where to insert to the instruction list
i - Instruction to insert
Returns:
instruction handle of the first inserted instruction

delete

public void delete(InstructionHandle ih)
            throws TargetLostException
Remove instruction from this list. The coresponding Instruction handles must not be reused!
Parameters:
ih - instruction (handle) to remove

delete

public void delete(Instruction i)
            throws TargetLostException
Remove instruction from this list. The coresponding Instruction handles must not be reused!
Parameters:
i - instruction to remove

delete

public void delete(InstructionHandle from,
                   InstructionHandle to)
            throws TargetLostException
Remove instructions from instruction `from' to instruction `to' contained in this list. The user must ensure that `from' is an instruction before `to', or risk havoc. The coresponding Instruction handles must not be reused!
Parameters:
from - where to start deleting (inclusive)
to - where to end deleting (inclusive)

delete

public void delete(Instruction from,
                   Instruction to)
            throws TargetLostException
Remove instructions from instruction `from' to instruction `to' contained in this list. The user must ensure that `from' is an instruction before `to', or risk havoc. The coresponding Instruction handles must not be reused!
Parameters:
from - where to start deleting (inclusive)
to - where to end deleting (inclusive)

contains

public boolean contains(InstructionHandle i)

contains

public boolean contains(Instruction i)

setPositions

public void setPositions()

setPositions

public void setPositions(boolean check)
Give all instructions their position number (offset in byte stream), i.e. make the list ready to be dumped.
Parameters:
check - Perform sanity checks, e.g. if all targeted instructions really belong to this list

getByteCode

public byte[] getByteCode()
Returns:
The byte code!

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

toString

public java.lang.String toString(boolean verbose)
Parameters:
verbose - toggle output format
Returns:
String containing all instructions in this list.

elements

public java.util.Enumeration elements()
Returns:
Enumeration that lists all instructions (handles)

getInstructionHandles

public InstructionHandle[] getInstructionHandles()
Returns:
array containing all instructions (handles)

getInstructionPositions

public int[] getInstructionPositions()
Get positions (offsets) of all instructions in the list. This relies on that the list has been freshly created from an byte code array, or that setPositions() has been called. Otherwise this may be inaccurate.
Returns:
array containing all instruction's offset in byte code

copy

public InstructionList copy()
Returns:
complete, i.e. deep copy of this list

dispose

public void dispose()
Delete contents of list. Provides besser memory utilization, because the system then reuses the instruction handles. This method is typically called right after MethodGen.getMethod().

getStart

public InstructionHandle getStart()
Returns:
start of list

getEnd

public InstructionHandle getEnd()
Returns:
end of list

getLength

public int getLength()
Returns:
length of list (Number of instructions, not bytes)

size

public int size()
Returns:
length of list (Number of instructions, not bytes)

redirectBranches

public void redirectBranches(InstructionHandle old_target,
                             InstructionHandle new_target)
Redirect all references from old_target to new_target, i.e. update targets of branch instructions.

redirectLocalVariables

public void redirectLocalVariables(LocalVariableGen[] lg,
                                   InstructionHandle old_target,
                                   InstructionHandle new_target)
Redirect all references of local variables from old_target to new_target.

redirectExceptionHandlers

public void redirectExceptionHandlers(CodeExceptionGen[] exceptions,
                                      InstructionHandle old_target,
                                      InstructionHandle new_target)
Redirect all references of exception handlers from old_target to new_target.