Class InstructionFactory

  • All Implemented Interfaces:
    InstructionConstants

    public class InstructionFactory
    extends java.lang.Object
    implements InstructionConstants
    Instances of this class may be used, for example, to generate typed versions of instructions. Its main purpose is to be used as the byte code generating backend of a compiler. You can subclass it to add your own create methods.

    Note: The static createXXX methods return singleton instances from the InstructionConst class.

    See Also:
    Const, InstructionConst
    • Field Detail

      • cg

        @Deprecated
        protected ClassGen cg
        Deprecated.
        (since 6.0) will be made private; do not access directly, use getter/setter
      • cp

        @Deprecated
        protected ConstantPoolGen cp
        Deprecated.
        (since 6.0) will be made private; do not access directly, use getter/setter
    • Method Detail

      • createArrayLoad

        public static ArrayInstruction createArrayLoad​(Type type)
        Creates an array load instruction for the given type.
        Parameters:
        type - type of elements of array, that is, array.getElementType().
        Returns:
        The array load instruction.
      • createArrayStore

        public static ArrayInstruction createArrayStore​(Type type)
        Creates an array store instruction for the given type.
        Parameters:
        type - type of elements of array, that is, array.getElementType().
        Returns:
        The array store instruction.
      • createBinaryOperation

        public static ArithmeticInstruction createBinaryOperation​(java.lang.String op,
                                                                  Type type)
        Create binary operation for simple basic types, such as int and float.
        Parameters:
        op - operation, such as "+", "*", "<<", etc.
        type - The operand type.
        Returns:
        The binary arithmetic instruction.
      • createBranchInstruction

        public static BranchInstruction createBranchInstruction​(short opcode,
                                                                InstructionHandle target)
        Create branch instruction by given opcode, except LOOKUPSWITCH and TABLESWITCH. For those you should use the SWITCH compound instruction.
        Parameters:
        opcode - The branch opcode.
        target - The target instruction handle.
        Returns:
        The branch instruction.
      • createDup

        public static StackInstruction createDup​(int size)
        Creates a DUP instruction.
        Parameters:
        size - size of operand, either 1 (int, for example) or 2 (double).
        Returns:
        The DUP instruction.
      • createDup_1

        public static StackInstruction createDup_1​(int size)
        Creates a DUP_X1 instruction.
        Parameters:
        size - size of operand, either 1 (int, for example) or 2 (double).
        Returns:
        The DUP instruction.
      • createDup_2

        public static StackInstruction createDup_2​(int size)
        Creates a DUP_X2 instruction.
        Parameters:
        size - size of operand, either 1 (int, for example) or 2 (double).
        Returns:
        The DUP instruction.
      • createLoad

        public static LocalVariableInstruction createLoad​(Type type,
                                                          int index)
        Creates a load instruction for the given type.
        Parameters:
        type - The variable type.
        index - index of local variable.
        Returns:
        The load instruction.
      • createNull

        public static Instruction createNull​(Type type)
        Create "null" value for reference types, 0 for basic types like int.
        Parameters:
        type - The type.
        Returns:
        The null or zero instruction.
      • createPop

        public static StackInstruction createPop​(int size)
        Creates a pop instruction.
        Parameters:
        size - size of operand, either 1 (int, for example) or 2 (double).
        Returns:
        The pop instruction.
      • createReturn

        public static ReturnInstruction createReturn​(Type type)
        Create typed return.
        Parameters:
        type - The return type.
        Returns:
        The return instruction.
      • createStore

        public static LocalVariableInstruction createStore​(Type type,
                                                           int index)
        Creates a store instruction.
        Parameters:
        type - The variable type.
        index - index of local variable.
        Returns:
        The store instruction.
      • createThis

        public static Instruction createThis()
        Create reference to 'this'.
        Returns:
        reference to 'this'.
      • createAppend

        public Instruction createAppend​(Type type)
        Creates an append instruction for the given type.
        Parameters:
        type - The type to append.
        Returns:
        The append instruction.
      • createCast

        public Instruction createCast​(Type srcType,
                                      Type destType)
        Create conversion operation for two stack operands, this may be an I2C, instruction, for example, if the operands are basic types and CHECKCAST if they are reference types.
        Parameters:
        srcType - The source type.
        destType - The destination type.
        Returns:
        The conversion instruction.
      • createConstant

        public Instruction createConstant​(java.lang.Object value)
        Uses PUSH to push a constant value onto the stack.
        Parameters:
        value - must be of type Number, Boolean, Character or String.
        Returns:
        The instruction.
      • createFieldAccess

        public FieldInstruction createFieldAccess​(java.lang.String className,
                                                  java.lang.String name,
                                                  Type type,
                                                  short kind)
        Create a field instruction.
        Parameters:
        className - class name containing the field.
        name - name of the referenced field.
        type - type of field.
        kind - how to access, that is, GETFIELD, PUTFIELD, GETSTATIC, PUTSTATIC.
        Returns:
        A field instruction.
        See Also:
        Const
      • createGetField

        public GETFIELD createGetField​(java.lang.String className,
                                       java.lang.String name,
                                       Type t)
        Creates a GETFIELD instruction.
        Parameters:
        className - The class name.
        name - The field name.
        t - The field type.
        Returns:
        The GETFIELD instruction.
      • createGetStatic

        public GETSTATIC createGetStatic​(java.lang.String className,
                                         java.lang.String name,
                                         Type t)
        Creates a GETSTATIC instruction.
        Parameters:
        className - The class name.
        name - The field name.
        t - The field type.
        Returns:
        The GETSTATIC instruction.
      • createInvoke

        public InvokeInstruction createInvoke​(java.lang.String className,
                                              java.lang.String name,
                                              Type retType,
                                              Type[] argTypes,
                                              short kind)
        Create an invoke instruction. (Except for invokedynamic.)
        Parameters:
        className - name of the called class.
        name - name of the called method.
        retType - return type of method.
        argTypes - argument types of method.
        kind - how to invoke, that is, INVOKEINTERFACE, INVOKESTATIC, INVOKEVIRTUAL, or INVOKESPECIAL.
        Returns:
        The invoke instruction.
        See Also:
        Const
      • createInvoke

        public InvokeInstruction createInvoke​(java.lang.String className,
                                              java.lang.String name,
                                              Type retType,
                                              Type[] argTypes,
                                              short kind,
                                              boolean useInterface)
        Create an invoke instruction. (Except for invokedynamic.)
        Parameters:
        className - name of the called class.
        name - name of the called method.
        retType - return type of method.
        argTypes - argument types of method.
        kind - how to invoke: INVOKEINTERFACE, INVOKESTATIC, INVOKEVIRTUAL, or INVOKESPECIAL.
        useInterface - force use of InterfaceMethodref.
        Returns:
        A new InvokeInstruction.
        Since:
        6.5.0
      • createNew

        public NEW createNew​(ObjectType t)
        Creates a NEW instruction.
        Parameters:
        t - The object type.
        Returns:
        The NEW instruction.
      • createNew

        public NEW createNew​(java.lang.String s)
        Creates a NEW instruction.
        Parameters:
        s - The class name.
        Returns:
        The NEW instruction.
      • createNewArray

        public Instruction createNewArray​(Type t,
                                          short dim)
        Create new array of given size and type.
        Parameters:
        t - The array element type.
        dim - The array dimensions.
        Returns:
        An instruction that creates the corresponding array at runtime, for example is an AllocationInstruction.
      • createPrintln

        public InstructionList createPrintln​(java.lang.String s)
        Create a call to the most popular System.out.println() method.
        Parameters:
        s - The string to print.
        Returns:
        The instruction list.
      • createPutField

        public PUTFIELD createPutField​(java.lang.String className,
                                       java.lang.String name,
                                       Type t)
        Creates a PUTFIELD instruction.
        Parameters:
        className - The class name.
        name - The field name.
        t - The field type.
        Returns:
        The PUTFIELD instruction.
      • createPutStatic

        public PUTSTATIC createPutStatic​(java.lang.String className,
                                         java.lang.String name,
                                         Type t)
        Creates a PUTSTATIC instruction.
        Parameters:
        className - The class name.
        name - The field name.
        t - The field type.
        Returns:
        The PUTSTATIC instruction.
      • getClassGen

        public ClassGen getClassGen()
        Gets the class generator.
        Returns:
        The class generator.
      • setClassGen

        public void setClassGen​(ClassGen c)
        Sets the class generator.
        Parameters:
        c - The class generator.