com.ibm.wala.util.bytecode
Class BytecodeStream

java.lang.Object
  extended by com.ibm.wala.util.bytecode.BytecodeStream
All Implemented Interfaces:
BytecodeConstants

public class BytecodeStream
extends java.lang.Object
implements BytecodeConstants

Provides minimal abstraction layer to a stream of bytecodes from the code attribute of a method.


Field Summary
 
Fields inherited from interface com.ibm.wala.shrikeBT.BytecodeConstants
JBC_aaload, JBC_aastore, JBC_aconst_null, JBC_aload, JBC_aload_0, JBC_aload_1, JBC_aload_2, JBC_aload_3, JBC_anewarray, JBC_areturn, JBC_arraylength, JBC_astore, JBC_astore_0, JBC_astore_1, JBC_astore_2, JBC_astore_3, JBC_athrow, JBC_baload, JBC_bastore, JBC_bipush, JBC_caload, JBC_castore, JBC_checkcast, JBC_d2f, JBC_d2i, JBC_d2l, JBC_dadd, JBC_daload, JBC_dastore, JBC_dcmpg, JBC_dcmpl, JBC_dconst_0, JBC_dconst_1, JBC_ddiv, JBC_dload, JBC_dload_0, JBC_dload_1, JBC_dload_2, JBC_dload_3, JBC_dmul, JBC_dneg, JBC_drem, JBC_dreturn, JBC_dstore, JBC_dstore_0, JBC_dstore_1, JBC_dstore_2, JBC_dstore_3, JBC_dsub, JBC_dup, JBC_dup_x1, JBC_dup_x2, JBC_dup2, JBC_dup2_x1, JBC_dup2_x2, JBC_f2d, JBC_f2i, JBC_f2l, JBC_fadd, JBC_faload, JBC_fastore, JBC_fcmpg, JBC_fcmpl, JBC_fconst_0, JBC_fconst_1, JBC_fconst_2, JBC_fdiv, JBC_fload, JBC_fload_0, JBC_fload_1, JBC_fload_2, JBC_fload_3, JBC_fmul, JBC_fneg, JBC_frem, JBC_freturn, JBC_fstore, JBC_fstore_0, JBC_fstore_1, JBC_fstore_2, JBC_fstore_3, JBC_fsub, JBC_getfield, JBC_getstatic, JBC_goto, JBC_goto_w, JBC_i2d, JBC_i2f, JBC_i2l, JBC_iadd, JBC_iaload, JBC_iand, JBC_iastore, JBC_iconst_0, JBC_iconst_1, JBC_iconst_2, JBC_iconst_3, JBC_iconst_4, JBC_iconst_5, JBC_iconst_m1, JBC_idiv, JBC_if_acmpeq, JBC_if_acmpne, JBC_if_icmpeq, JBC_if_icmpge, JBC_if_icmpgt, JBC_if_icmple, JBC_if_icmplt, JBC_if_icmpne, JBC_ifeq, JBC_ifge, JBC_ifgt, JBC_ifle, JBC_iflt, JBC_ifne, JBC_ifnonnull, JBC_ifnull, JBC_iinc, JBC_iload, JBC_iload_0, JBC_iload_1, JBC_iload_2, JBC_iload_3, JBC_impdep1, JBC_impdep2, JBC_imul, JBC_ineg, JBC_instanceof, JBC_int2byte, JBC_int2char, JBC_int2short, JBC_invokeinterface, JBC_invokespecial, JBC_invokestatic, JBC_invokevirtual, JBC_ior, JBC_irem, JBC_ireturn, JBC_ishl, JBC_ishr, JBC_istore, JBC_istore_0, JBC_istore_1, JBC_istore_2, JBC_istore_3, JBC_isub, JBC_iushr, JBC_ixor, JBC_jsr, JBC_jsr_w, JBC_l2d, JBC_l2f, JBC_l2i, JBC_ladd, JBC_laload, JBC_land, JBC_lastore, JBC_lcmp, JBC_lconst_0, JBC_lconst_1, JBC_ldc, JBC_ldc_w, JBC_ldc2_w, JBC_ldiv, JBC_length, JBC_lload, JBC_lload_0, JBC_lload_1, JBC_lload_2, JBC_lload_3, JBC_lmul, JBC_lneg, JBC_lookupswitch, JBC_lor, JBC_lrem, JBC_lreturn, JBC_lshl, JBC_lshr, JBC_lstore, JBC_lstore_0, JBC_lstore_1, JBC_lstore_2, JBC_lstore_3, JBC_lsub, JBC_lushr, JBC_lxor, JBC_monitorenter, JBC_monitorexit, JBC_multianewarray, JBC_name, JBC_new, JBC_newarray, JBC_nop, JBC_pop, JBC_pop2, JBC_putfield, JBC_putstatic, JBC_ret, JBC_return, JBC_saload, JBC_sastore, JBC_sipush, JBC_swap, JBC_tableswitch, JBC_wide, JBC_xxxunusedxxx
 
Constructor Summary
BytecodeStream(IMethod m, byte[] bc)
           
 
Method Summary
 void alignInvokeInterface()
          Skips the extra stuff after an invokeinterface instruction Used for invokeinterface
 void alignSwitch()
          Skips the padding of a switch instruction Used for tableswitch, lookupswitch
 int computeLookupSwitchOffset(int value, int num)
          Returns the offset for a given value of the lookupswitch (as a signed int) or 0 if the value is not in the table.
 int computeTableSwitchOffset(int value, int low, int high)
          Returns the offset for a given value of the tableswitch (as a signed int) or 0 if the value is out of range.
 IClass declaringClass()
          Returns the declaring class that this bytecode stream is from
 int getArrayDimension()
          Returns the dimension of the array (as an unsigned byte) Used for multianewarray
 int getArrayElementType()
          Returns the element type (primitive) of the array (as an unsigned byte) Used for newarray
 int getBranchOffset()
          Returns the offset of the branch (as a signed short) Used for if, ificmp, ifacmp, goto, jsr
 int getByteValue()
          Returns a signed byte value Used for bipush
 int getConstantIndex()
          Returns the constant pool index of a constant (as an unsigned byte) Used for ldc
 byte getConstantType(int index)
          Returns the type of a constant at a given constant pool index (as a byte) Used for ldc, ldc_w, ldc2_w
 int getDefaultSwitchOffset()
          Returns the default offset of the switch (as a signed int) Used for tableswitch, lookupswitch
 int getHighSwitchValue()
          Returns the highest value of the tableswitch (as a signed int) Used for tableswitch
 int getIncrement()
          Returns an increment value (as a signed byte) Used for iinc
 int getIntConstant(int index)
          Returns the constant at a given constant pool index (as an int) Used for ldc, ldc_w
 int getLocalNumber()
          Returns the number of the local (as an unsigned byte) Used for iload, lload, fload, dload, aload, istore, lstore, fstore, dstore, astore, iinc, ret
 int getLookupSwitchOffset(int num)
          Returns the numbered offset of the lookupswitch (as a signed int) Used for lookupswitch The "cursor" has to be positioned at the start of the pair table NOTE: Will NOT advance cursor
 int getLookupSwitchValue(int num)
          Returns the numbered value of the lookupswitch (as a signed int) Used for lookupswitch The "cursor" has to be positioned at the start of the pair table NOTE: Will NOT advance cursor
 int getLowSwitchValue()
          Returns the lowest value of the tableswitch (as a signed int) Used for tableswitch
 int getOpcode()
          Returns the opcode of the current instruction in the sequence Note: if skipInstruction has been called, but nextInstruction has not, this method will return the opcode of the skipped instruction!
 int getShortValue()
          Returns a signed short value Used for sipush
 int getSwitchLength()
          Returns the number of match-offset pairs in the lookupswitch (as a signed int) Used for lookupswitch
 int getTableSwitchOffset(int num)
          Returns the numbered offset of the tableswitch (as a signed int) Used for tableswitch The "cursor" has to be positioned at the start of the offset table NOTE: Will NOT advance cursor
 int getWideBranchOffset()
          Returns the wide offset of the branch (as a signed int) Used for goto_w, jsr_w
 int getWideConstantIndex()
          Returns the wide constant pool index of a constant (as an unsigned short) Used for ldc_w, ldc2_w
 int getWideIncrement()
          Returns an increment value (as a signed short) Used for iinc prefixed by wide
 int getWideLocalNumber()
          Returns the wide number of the local (as an unsigned short) Used for iload, lload, fload, dload, aload, istore, lstore, fstore, dstore, astore, iinc prefixed by wide
 int getWideOpcode()
          Returns the opcode of the wide instruction Used for wide Can be one of iload, lload, fload, dload, aload, istore, lstore, fstore, dstore, astore, iinc
 boolean hasMoreBytecodes()
          Does the stream have more bytecodes in it?
 int index()
          Returns the current bytecode index
 boolean isWide()
          Are we currently processing a wide instruction?
 int length()
          Returns the length of the bytecode stream Returns 0 if the method doesn't have any bytecodes (i.e.
 IMethod method()
          Returns the method that this bytecode stream is from
 int nextInstruction()
          Sets up the next instruction in the sequence
 int peekNextOpcode()
          Returns the opcode of the next instruction in the sequence without advancing to it
 void reset()
          Resets the stream to the beginning
 void reset(int index)
          Resets the stream to a given position Use with caution
 void skipInstruction()
          Skips the current instruction
 void skipInstruction(int opcode, boolean wide)
          Skips the current instruction (without using the opcode field) A slightly optimized version of skipInstruction()
 void skipLookupSwitchPairs(int num)
          Skips the match-offset pairs of a lookupswitch instruction Used for lookupswitch
 void skipTableSwitchOffsets(int num)
          Skips the offsets of a tableswitch instruction Used for tableswitch
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BytecodeStream

public BytecodeStream(IMethod m,
                      byte[] bc)
Parameters:
m - the method containing the bytecodes
bc - the array of bytecodes
Throws:
java.lang.IllegalArgumentException - if bc is null
java.lang.IllegalArgumentException - if m is null
Method Detail

method

public final IMethod method()
Returns the method that this bytecode stream is from

Returns:
method

declaringClass

public final IClass declaringClass()
Returns the declaring class that this bytecode stream is from

Returns:
method

length

public final int length()
Returns the length of the bytecode stream Returns 0 if the method doesn't have any bytecodes (i.e. is abstract or native)

Returns:
bytecode stream length

index

public final int index()
Returns the current bytecode index

Returns:
the current bytecode index

reset

public final void reset()
Resets the stream to the beginning

See Also:
reset(int)

reset

public final void reset(int index)
Resets the stream to a given position Use with caution

Parameters:
index - the position to reset the stream to
See Also:
reset()

hasMoreBytecodes

public final boolean hasMoreBytecodes()
Does the stream have more bytecodes in it?

Returns:
whether there are more bytecodes

peekNextOpcode

public final int peekNextOpcode()
Returns the opcode of the next instruction in the sequence without advancing to it

Returns:
the opcode of the next instruction
See Also:
nextInstruction()

nextInstruction

public final int nextInstruction()
Sets up the next instruction in the sequence

Returns:
the opcode of the next instruction
See Also:
peekNextOpcode()

getOpcode

public final int getOpcode()
Returns the opcode of the current instruction in the sequence Note: if skipInstruction has been called, but nextInstruction has not, this method will return the opcode of the skipped instruction!

Returns:
the opcode of the current instruction
See Also:
nextInstruction(), isWide()

isWide

public final boolean isWide()
Are we currently processing a wide instruction?

Returns:
true if current instruction is wide
See Also:
nextInstruction(), getOpcode()

skipInstruction

public final void skipInstruction()
Skips the current instruction

See Also:
skipInstruction(int,boolean)

skipInstruction

public final void skipInstruction(int opcode,
                                  boolean wide)
Skips the current instruction (without using the opcode field) A slightly optimized version of skipInstruction()

Parameters:
opcode - current opcode
wide - whether current instruction follows wide
See Also:
skipInstruction()

getByteValue

public final int getByteValue()
Returns a signed byte value Used for bipush

Returns:
signed byte value

getShortValue

public final int getShortValue()
Returns a signed short value Used for sipush

Returns:
signed short value

getLocalNumber

public final int getLocalNumber()
Returns the number of the local (as an unsigned byte) Used for iload, lload, fload, dload, aload, istore, lstore, fstore, dstore, astore, iinc, ret

Returns:
local number
See Also:
getWideLocalNumber()

getWideLocalNumber

public final int getWideLocalNumber()
Returns the wide number of the local (as an unsigned short) Used for iload, lload, fload, dload, aload, istore, lstore, fstore, dstore, astore, iinc prefixed by wide

Returns:
wide local number
See Also:
getLocalNumber()

getIncrement

public final int getIncrement()
Returns an increment value (as a signed byte) Used for iinc

Returns:
increment
See Also:
getWideIncrement()

getWideIncrement

public final int getWideIncrement()
Returns an increment value (as a signed short) Used for iinc prefixed by wide

Returns:
wide increment
See Also:
getIncrement()

getBranchOffset

public final int getBranchOffset()
Returns the offset of the branch (as a signed short) Used for if, ificmp, ifacmp, goto, jsr

Returns:
branch offset
See Also:
getWideBranchOffset()

getWideBranchOffset

public final int getWideBranchOffset()
Returns the wide offset of the branch (as a signed int) Used for goto_w, jsr_w

Returns:
wide branch offset
See Also:
getBranchOffset()

alignSwitch

public final void alignSwitch()
Skips the padding of a switch instruction Used for tableswitch, lookupswitch


getDefaultSwitchOffset

public final int getDefaultSwitchOffset()
Returns the default offset of the switch (as a signed int) Used for tableswitch, lookupswitch

Returns:
default switch offset

getLowSwitchValue

public final int getLowSwitchValue()
Returns the lowest value of the tableswitch (as a signed int) Used for tableswitch

Returns:
lowest switch value
See Also:
getHighSwitchValue()

getHighSwitchValue

public final int getHighSwitchValue()
Returns the highest value of the tableswitch (as a signed int) Used for tableswitch

Returns:
highest switch value
See Also:
getLowSwitchValue()

skipTableSwitchOffsets

public final void skipTableSwitchOffsets(int num)
Skips the offsets of a tableswitch instruction Used for tableswitch

Parameters:
num - the number of offsets to skip
See Also:
getTableSwitchOffset(int)

getTableSwitchOffset

public final int getTableSwitchOffset(int num)
Returns the numbered offset of the tableswitch (as a signed int) Used for tableswitch The "cursor" has to be positioned at the start of the offset table NOTE: Will NOT advance cursor

Parameters:
num - the number of the offset to retrieve
Returns:
switch offset

computeTableSwitchOffset

public final int computeTableSwitchOffset(int value,
                                          int low,
                                          int high)
Returns the offset for a given value of the tableswitch (as a signed int) or 0 if the value is out of range. Used for tableswitch The "cursor" has to be positioned at the start of the offset table NOTE: Will NOT advance cursor

Parameters:
value - the value to retrieve offset for
low - the lowest value of the tableswitch
high - the highest value of the tableswitch
Returns:
switch offset

getSwitchLength

public final int getSwitchLength()
Returns the number of match-offset pairs in the lookupswitch (as a signed int) Used for lookupswitch

Returns:
number of switch pairs

skipLookupSwitchPairs

public final void skipLookupSwitchPairs(int num)
Skips the match-offset pairs of a lookupswitch instruction Used for lookupswitch

Parameters:
num - the number of match-offset pairs to skip
See Also:
getLookupSwitchValue(int), getLookupSwitchOffset(int)

getLookupSwitchOffset

public final int getLookupSwitchOffset(int num)
Returns the numbered offset of the lookupswitch (as a signed int) Used for lookupswitch The "cursor" has to be positioned at the start of the pair table NOTE: Will NOT advance cursor

Parameters:
num - the number of the offset to retrieve
Returns:
switch offset
See Also:
getLookupSwitchValue(int)

getLookupSwitchValue

public final int getLookupSwitchValue(int num)
Returns the numbered value of the lookupswitch (as a signed int) Used for lookupswitch The "cursor" has to be positioned at the start of the pair table NOTE: Will NOT advance cursor

Parameters:
num - the number of the value to retrieve
Returns:
switch value
See Also:
getLookupSwitchOffset(int)

computeLookupSwitchOffset

public final int computeLookupSwitchOffset(int value,
                                           int num)
Returns the offset for a given value of the lookupswitch (as a signed int) or 0 if the value is not in the table. Used for lookupswitch The "cursor" has to be positioned at the start of the offset table NOTE: Will NOT advance cursor WARNING: Uses LINEAR search. Whoever has time on their hands can re-implement this as a binary search.

Parameters:
value - the value to retrieve offset for
num - the number of match-offset pairs in the lookupswitch
Returns:
switch offset

alignInvokeInterface

public final void alignInvokeInterface()
Skips the extra stuff after an invokeinterface instruction Used for invokeinterface


getArrayElementType

public final int getArrayElementType()
Returns the element type (primitive) of the array (as an unsigned byte) Used for newarray

Returns:
array element type

getArrayDimension

public final int getArrayDimension()
Returns the dimension of the array (as an unsigned byte) Used for multianewarray

Returns:
array dimension

getWideOpcode

public final int getWideOpcode()
Returns the opcode of the wide instruction Used for wide Can be one of iload, lload, fload, dload, aload, istore, lstore, fstore, dstore, astore, iinc

Returns:
the opcode of the wide instruction

getConstantIndex

public final int getConstantIndex()
Returns the constant pool index of a constant (as an unsigned byte) Used for ldc

Returns:
constant index
See Also:
getWideConstantIndex(), getConstantType(int), getIntConstant(int)

getWideConstantIndex

public final int getWideConstantIndex()
Returns the wide constant pool index of a constant (as an unsigned short) Used for ldc_w, ldc2_w

Returns:
wide constant index
See Also:
getConstantIndex(), getConstantType(int), getIntConstant(int)

getConstantType

public final byte getConstantType(int index)
                           throws UnimplementedError
Returns the type of a constant at a given constant pool index (as a byte) Used for ldc, ldc_w, ldc2_w

Returns:
constant type
Throws:
UnimplementedError - unconditionally
See Also:
getConstantIndex(), getWideConstantIndex(), getIntConstant(int)

getIntConstant

public final int getIntConstant(int index)
                         throws UnimplementedError
Returns the constant at a given constant pool index (as an int) Used for ldc, ldc_w

Returns:
int constant
Throws:
UnimplementedError - unconditionally
See Also:
getConstantIndex(), getWideConstantIndex(), getConstantType(int)