Class InsnFormat

java.lang.Object
com.android.dx.dex.code.InsnFormat
Direct Known Subclasses:
Form10t, Form10x, Form11n, Form11x, Form12x, Form20t, Form21c, Form21h, Form21s, Form21t, Form22b, Form22c, Form22s, Form22t, Form22x, Form23x, Form30t, Form31c, Form31i, Form31t, Form32x, Form35c, Form3rc, Form45cc, Form4rcc, Form51l, SpecialFormat

public abstract class InsnFormat extends Object
Base class for all instruction format handlers. Instruction format handlers know how to translate DalvInsn instances into streams of code units, as well as human-oriented listing strings representing such translations.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final boolean
    flag to enable/disable the new extended opcode formats; meant as a temporary measure until VM support for the salient opcodes is added.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected static int
    Helper method to extract the callout-argument index from an appropriate instruction.
    protected static String
    Helper method to return the comment for a branch.
    boolean
    Returns whether or not the given instruction's branch offset will fit in this instance's format.
    protected static String
    Helper method to return a branch address string.
    abstract int
    Gets the code size of instructions that use this format.
    protected static short
    codeUnit(int low, int high)
    Helper method to combine two bytes into a code unit.
    protected static short
    codeUnit(int n0, int n1, int n2, int n3)
    Helper method to combine four nibbles into a code unit.
    Returns which of a given instruction's registers will fit in this instance's format.
    abstract String
    Returns the string form of the arguments to the given instruction.
    abstract String
    insnCommentString(DalvInsn insn, boolean noteIndices)
    Returns the associated comment for the given instruction, if any.
    abstract boolean
    Returns whether or not the given instruction's arguments will fit in this instance's format.
    protected static boolean
    Helper method to determine if a list of registers are sequential, including degenerate cases for empty or single-element lists.
    final String
    listingString(DalvInsn insn, boolean noteIndices)
    Returns the string form, suitable for inclusion in a listing dump, of the given instruction.
    protected static String
    Helper method to return a literal bits comment string.
    protected static String
    Helper method to return a literal bits argument string.
    protected static int
    makeByte(int low, int high)
    Helper method to combine two nibbles into a byte.
    protected static short
    Helper method to get an extended (16-bit) opcode out of an instruction, returning it as a code unit.
    protected static short
    opcodeUnit(DalvInsn insn, int arg)
    Helper method to combine an opcode and a second byte of data into the appropriate form for emitting into a code buffer.
    protected static String
    Helper method to return a register list string.
    protected static String
    Helper method to return a register range string.
    protected static boolean
    signedFitsInByte(int value)
    Helper method to determine if a signed int value fits in a byte.
    protected static boolean
    signedFitsInNibble(int value)
    Helper method to determine if a signed int value fits in a nibble.
    protected static boolean
    signedFitsInShort(int value)
    Helper method to determine if a signed int value fits in a short.
    protected static boolean
    unsignedFitsInByte(int value)
    Helper method to determine if an unsigned int value fits in a byte.
    protected static boolean
    Helper method to determine if an unsigned int value fits in a nibble.
    protected static boolean
    Helper method to determine if an unsigned int value fits in a short.
    protected static void
    write(AnnotatedOutput out, short c0)
    Writes one code unit to the given output destination.
    protected static void
    write(AnnotatedOutput out, short c0, int c1c2)
    Writes three code units to the given output destination, where the second and third are represented as single int and emitted in little-endian order.
    protected static void
    write(AnnotatedOutput out, short c0, int c1c2, short c3)
    Writes four code units to the given output destination, where the second and third are represented as single int and emitted in little-endian order.
    protected static void
    write(AnnotatedOutput out, short c0, int c1c2, short c3, short c4)
    Writes five code units to the given output destination, where the second and third are represented as single int and emitted in little-endian order.
    protected static void
    write(AnnotatedOutput out, short c0, long c1c2c3c4)
    Writes five code units to the given output destination, where the second through fifth are represented as single long and emitted in little-endian order.
    protected static void
    write(AnnotatedOutput out, short c0, short c1)
    Writes two code units to the given output destination.
    protected static void
    write(AnnotatedOutput out, short c0, short c1, short c2)
    Writes three code units to the given output destination.
    protected static void
    write(AnnotatedOutput out, short c0, short c1, short c2, short c3)
    Writes four code units to the given output destination.
    protected static void
    write(AnnotatedOutput out, short c0, short c1, short c2, short c3, short c4)
    Writes five code units to the given output destination.
    abstract void
    Writes the code units for the given instruction to the given output destination.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • ALLOW_EXTENDED_OPCODES

      public static final boolean ALLOW_EXTENDED_OPCODES
      flag to enable/disable the new extended opcode formats; meant as a temporary measure until VM support for the salient opcodes is added. TODO: Remove this declaration when the VM can deal.
      See Also:
  • Constructor Details

    • InsnFormat

      public InsnFormat()
  • Method Details

    • listingString

      public final String listingString(DalvInsn insn, boolean noteIndices)
      Returns the string form, suitable for inclusion in a listing dump, of the given instruction. The instruction must be of this instance's format for proper operation.
      Parameters:
      insn - non-null; the instruction
      noteIndices - whether to include an explicit notation of constant pool indices
      Returns:
      non-null; the string form
    • insnArgString

      public abstract String insnArgString(DalvInsn insn)
      Returns the string form of the arguments to the given instruction. The instruction must be of this instance's format. If the instruction has no arguments, then the result should be "", not null.

      Subclasses must override this method.

      Parameters:
      insn - non-null; the instruction
      Returns:
      non-null; the string form
    • insnCommentString

      public abstract String insnCommentString(DalvInsn insn, boolean noteIndices)
      Returns the associated comment for the given instruction, if any. The instruction must be of this instance's format. If the instruction has no comment, then the result should be "", not null.

      Subclasses must override this method.

      Parameters:
      insn - non-null; the instruction
      noteIndices - whether to include an explicit notation of constant pool indices
      Returns:
      non-null; the string form
    • codeSize

      public abstract int codeSize()
      Gets the code size of instructions that use this format. The size is a number of 16-bit code units, not bytes. This should throw an exception if this format is of variable size.
      Returns:
      >= 0; the instruction length in 16-bit code units
    • isCompatible

      public abstract boolean isCompatible(DalvInsn insn)
      Returns whether or not the given instruction's arguments will fit in this instance's format. This includes such things as counting register arguments, checking register ranges, and making sure that additional arguments are of appropriate types and are in-range. If this format has a branch target but the instruction's branch offset is unknown, this method will simply not check the offset.

      Subclasses must override this method.

      Parameters:
      insn - non-null; the instruction to check
      Returns:
      true iff the instruction's arguments are appropriate for this instance, or false if not
    • compatibleRegs

      public BitSet compatibleRegs(DalvInsn insn)
      Returns which of a given instruction's registers will fit in this instance's format.

      The default implementation of this method always returns an empty BitSet. Subclasses must override this method if they have registers.

      Parameters:
      insn - non-null; the instruction to check
      Returns:
      non-null; a BitSet flagging registers in the register list that are compatible to this format
    • branchFits

      public boolean branchFits(TargetInsn insn)
      Returns whether or not the given instruction's branch offset will fit in this instance's format. This always returns false for formats that don't include a branch offset.

      The default implementation of this method always returns false. Subclasses must override this method if they include branch offsets.

      Parameters:
      insn - non-null; the instruction to check
      Returns:
      true iff the instruction's branch offset is appropriate for this instance, or false if not
    • writeTo

      public abstract void writeTo(AnnotatedOutput out, DalvInsn insn)
      Writes the code units for the given instruction to the given output destination. The instruction must be of this instance's format.

      Subclasses must override this method.

      Parameters:
      out - non-null; the output destination to write to
      insn - non-null; the instruction to write
    • regListString

      protected static String regListString(RegisterSpecList list)
      Helper method to return a register list string.
      Parameters:
      list - non-null; the list of registers
      Returns:
      non-null; the string form
    • regRangeString

      protected static String regRangeString(RegisterSpecList list)
      Helper method to return a register range string.
      Parameters:
      list - non-null; the list of registers (which must be sequential)
      Returns:
      non-null; the string form
    • literalBitsString

      protected static String literalBitsString(CstLiteralBits value)
      Helper method to return a literal bits argument string.
      Parameters:
      value - the value
      Returns:
      non-null; the string form
    • literalBitsComment

      protected static String literalBitsComment(CstLiteralBits value, int width)
      Helper method to return a literal bits comment string.
      Parameters:
      value - the value
      width - the width of the constant, in bits (used for displaying the uninterpreted bits; one of: 4 8 16 32 64
      Returns:
      non-null; the comment
    • branchString

      protected static String branchString(DalvInsn insn)
      Helper method to return a branch address string.
      Parameters:
      insn - non-null; the instruction in question
      Returns:
      non-null; the string form of the instruction's branch target
    • branchComment

      protected static String branchComment(DalvInsn insn)
      Helper method to return the comment for a branch.
      Parameters:
      insn - non-null; the instruction in question
      Returns:
      non-null; the comment
    • signedFitsInNibble

      protected static boolean signedFitsInNibble(int value)
      Helper method to determine if a signed int value fits in a nibble.
      Parameters:
      value - the value in question
      Returns:
      true iff it's in the range -8..+7
    • unsignedFitsInNibble

      protected static boolean unsignedFitsInNibble(int value)
      Helper method to determine if an unsigned int value fits in a nibble.
      Parameters:
      value - the value in question
      Returns:
      true iff it's in the range 0..0xf
    • signedFitsInByte

      protected static boolean signedFitsInByte(int value)
      Helper method to determine if a signed int value fits in a byte.
      Parameters:
      value - the value in question
      Returns:
      true iff it's in the range -0x80..+0x7f
    • unsignedFitsInByte

      protected static boolean unsignedFitsInByte(int value)
      Helper method to determine if an unsigned int value fits in a byte.
      Parameters:
      value - the value in question
      Returns:
      true iff it's in the range 0..0xff
    • signedFitsInShort

      protected static boolean signedFitsInShort(int value)
      Helper method to determine if a signed int value fits in a short.
      Parameters:
      value - the value in question
      Returns:
      true iff it's in the range -0x8000..+0x7fff
    • unsignedFitsInShort

      protected static boolean unsignedFitsInShort(int value)
      Helper method to determine if an unsigned int value fits in a short.
      Parameters:
      value - the value in question
      Returns:
      true iff it's in the range 0..0xffff
    • isRegListSequential

      protected static boolean isRegListSequential(RegisterSpecList list)
      Helper method to determine if a list of registers are sequential, including degenerate cases for empty or single-element lists.
      Parameters:
      list - non-null; the list of registers
      Returns:
      true iff the list is sequentially ordered
    • argIndex

      protected static int argIndex(DalvInsn insn)
      Helper method to extract the callout-argument index from an appropriate instruction.
      Parameters:
      insn - non-null; the instruction
      Returns:
      >= 0; the callout argument index
    • opcodeUnit

      protected static short opcodeUnit(DalvInsn insn, int arg)
      Helper method to combine an opcode and a second byte of data into the appropriate form for emitting into a code buffer.
      Parameters:
      insn - non-null; the instruction containing the opcode
      arg - 0..255; arbitrary other byte value
      Returns:
      combined value
    • opcodeUnit

      protected static short opcodeUnit(DalvInsn insn)
      Helper method to get an extended (16-bit) opcode out of an instruction, returning it as a code unit. The opcode must be an extended opcode.
      Parameters:
      insn - non-null; the instruction containing the extended opcode
      Returns:
      the opcode as a code unit
    • codeUnit

      protected static short codeUnit(int low, int high)
      Helper method to combine two bytes into a code unit.
      Parameters:
      low - 0..255; low byte
      high - 0..255; high byte
      Returns:
      combined value
    • codeUnit

      protected static short codeUnit(int n0, int n1, int n2, int n3)
      Helper method to combine four nibbles into a code unit.
      Parameters:
      n0 - 0..15; low nibble
      n1 - 0..15; medium-low nibble
      n2 - 0..15; medium-high nibble
      n3 - 0..15; high nibble
      Returns:
      combined value
    • makeByte

      protected static int makeByte(int low, int high)
      Helper method to combine two nibbles into a byte.
      Parameters:
      low - 0..15; low nibble
      high - 0..15; high nibble
      Returns:
      0..255; combined value
    • write

      protected static void write(AnnotatedOutput out, short c0)
      Writes one code unit to the given output destination.
      Parameters:
      out - non-null; where to write to
      c0 - code unit to write
    • write

      protected static void write(AnnotatedOutput out, short c0, short c1)
      Writes two code units to the given output destination.
      Parameters:
      out - non-null; where to write to
      c0 - code unit to write
      c1 - code unit to write
    • write

      protected static void write(AnnotatedOutput out, short c0, short c1, short c2)
      Writes three code units to the given output destination.
      Parameters:
      out - non-null; where to write to
      c0 - code unit to write
      c1 - code unit to write
      c2 - code unit to write
    • write

      protected static void write(AnnotatedOutput out, short c0, short c1, short c2, short c3)
      Writes four code units to the given output destination.
      Parameters:
      out - non-null; where to write to
      c0 - code unit to write
      c1 - code unit to write
      c2 - code unit to write
      c3 - code unit to write
    • write

      protected static void write(AnnotatedOutput out, short c0, short c1, short c2, short c3, short c4)
      Writes five code units to the given output destination.
      Parameters:
      out - non-null; where to write to
      c0 - code unit to write
      c1 - code unit to write
      c2 - code unit to write
      c3 - code unit to write
      c4 - code unit to write
    • write

      protected static void write(AnnotatedOutput out, short c0, int c1c2)
      Writes three code units to the given output destination, where the second and third are represented as single int and emitted in little-endian order.
      Parameters:
      out - non-null; where to write to
      c0 - code unit to write
      c1c2 - code unit pair to write
    • write

      protected static void write(AnnotatedOutput out, short c0, int c1c2, short c3)
      Writes four code units to the given output destination, where the second and third are represented as single int and emitted in little-endian order.
      Parameters:
      out - non-null; where to write to
      c0 - code unit to write
      c1c2 - code unit pair to write
      c3 - code unit to write
    • write

      protected static void write(AnnotatedOutput out, short c0, int c1c2, short c3, short c4)
      Writes five code units to the given output destination, where the second and third are represented as single int and emitted in little-endian order.
      Parameters:
      out - non-null; where to write to
      c0 - code unit to write
      c1c2 - code unit pair to write
      c3 - code unit to write
      c4 - code unit to write
    • write

      protected static void write(AnnotatedOutput out, short c0, long c1c2c3c4)
      Writes five code units to the given output destination, where the second through fifth are represented as single long and emitted in little-endian order.
      Parameters:
      out - non-null; where to write to
      c0 - code unit to write
      c1c2c3c4 - code unit quad to write