Class Instruction

  • All Implemented Interfaces:
    Opcode

    public class Instruction
    extends java.lang.Object
    implements Opcode
    Instruction represents a single instruction in the JVM. All Instructions known their opcode. Some instructions have an operand. Operands are integers, floats, or one of the special classes that represent multiple operands such as IncOperand and Switch.
    See Also:
    IncOperand, Switch, MultiArrayOperand
    • Constructor Detail

      • Instruction

        public Instruction​(int opcode)
        Constructor.
        Parameters:
        opcode - The opcode class of the instruction.
      • Instruction

        public Instruction​(int opcode,
                           java.lang.Object operand)
        Constructor.
        Parameters:
        opcode - The opcode class of the instruction.
        operand - The operand of the instruction, or null.
      • Instruction

        public Instruction​(byte[] code,
                           int index,
                           int[] targets,
                           int[] lookups,
                           LocalVariable[] locals,
                           ConstantPool constants)
        Constructor.
        Parameters:
        code - The raw byte code array of the method.
        index - This index into the byte array of the instruction.
        targets - Indices of the branch targets of the instruction.
        lookups - Values of the switch lookups of the instruction.
        locals - The local variables defined at this index.
        constants - The constant pool for the class.
    • Method Detail

      • origOpcode

        public int origOpcode()
        Returns the original (non-mapped) opcode used to create this Instruction.
      • setUseSlow

        public void setUseSlow​(boolean useSlow)
        Sets a flag that determines whether or not the "slow" version of the instruction should be generated. For example, if useSlow is true, "iload 2" is generated instead of "iload_2".
      • useSlow

        public boolean useSlow()
        Returns whether or not the "slow" version of this instruction is generated.
      • isLoad

        public boolean isLoad()
        Check if the instruction is a load.
        Returns:
        true if the instruction is a load, false if not.
      • isStore

        public boolean isStore()
        Check if the instruction is a store.
        Returns:
        true if the instruction is a store, false if not.
      • isInc

        public boolean isInc()
        Check if the instruction is an increment.
        Returns:
        true if the instruction is an increment, false if not.
      • isThrow

        public boolean isThrow()
        Check if the instruction is an exception throw instruction.
        Returns:
        true if the instruction is a throw, false if not.
      • isInvoke

        public boolean isInvoke()
        Returns true if this instruction invokes a method.
      • isRet

        public boolean isRet()
        Check if the instruction is a subroutine return instruction.
        Returns:
        true if the instruction is a ret, false if not.
      • isReturn

        public boolean isReturn()
        Returns true if the instruction returns from a method.
      • isSwitch

        public boolean isSwitch()
        Check if the instruction is a switch.
        Returns:
        true if the instruction is a switch, false if not.
      • isJump

        public boolean isJump()
        Check if the instruction is a jump.
        Returns:
        true if the instruction is a jump, false if not.
      • isJsr

        public boolean isJsr()
        Check if the instruction is a jsr.
        Returns:
        true if the instruction is a jsr, false if not.
      • isGoto

        public boolean isGoto()
        Check if the instruction is a goto.
        Returns:
        true if the instruction is a goto, false if not.
      • isConditionalJump

        public boolean isConditionalJump()
        Check if the instruction is a conditional jump.
        Returns:
        true if the instruction is a conditional jump, false if not.
      • opcodeClass

        public int opcodeClass()
        Get the opcode class of the instruction.
        Returns:
        The opcode class of the instruction.
      • setOpcodeClass

        public void setOpcodeClass​(int opcode)
        Set the opcode class of the instruction.
        Parameters:
        opcode - The opcode class of the instruction.
      • setOperand

        public void setOperand​(java.lang.Object operand)
        Set the operand of the instruction.
        Parameters:
        operand - The operand of the instruction.
      • operand

        public java.lang.Object operand()
        Get the operand of the instruction.
        Returns:
        The operand of the instruction.
      • toString

        public java.lang.String toString()
        Convert the instruction to a string.
        Overrides:
        toString in class java.lang.Object
        Returns:
        A string representation of the instruction.
      • toUShort

        protected static int toUShort​(byte b1,
                                      byte b2)
        Utility function to join 2 bytes into an unsigned short.
        Parameters:
        b1 - The upper byte.
        b2 - The lower byte.
        Returns:
        The unsigned short.
      • toShort

        protected static short toShort​(byte b1,
                                       byte b2)
        Utility function to join 2 bytes into an signed short.
        Parameters:
        b1 - The upper byte.
        b2 - The lower byte.
        Returns:
        The signed short.
      • toInt

        protected static int toInt​(byte b1,
                                   byte b2,
                                   byte b3,
                                   byte b4)
        Utility function to join 4 bytes into an signed int.
        Parameters:
        b1 - The upper byte.
        b2 - The next-to-upper byte.
        b3 - The next-to-lower byte.
        b4 - The lower byte.
        Returns:
        The signed int.
      • toUByte

        protected static int toUByte​(byte b)
        Utility function to convert a byte into an unsigned byte.
        Parameters:
        b - The byte.
        Returns:
        The unsigned byte.
      • category

        public int category()
        Returns the category of this instruction. An instruction's category is basically the width of the value the instruction places on the stack. Types long and double are Category 2. All other types are Category 1.
      • visit

        public void visit​(InstructionVisitor visitor)
        Big switch statement to call the appropriate method of an instruction visitor.
        Parameters:
        visitor - The instruction visitor.