Class InstructionFactory

  • All Implemented Interfaces:
    InstructionConstants

    public class InstructionFactory
    extends java.lang.Object
    implements InstructionConstants
    Instances of this class may be used, e.g., 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
    • Constructor Detail

      • InstructionFactory

        public InstructionFactory​(ClassGen cg)
        Initialize with ClassGen object
      • InstructionFactory

        public InstructionFactory​(ConstantPoolGen cp)
        Initialize just with ConstantPoolGen object
    • Method Detail

      • createInvoke

        public InvokeInstruction createInvoke​(java.lang.String class_name,
                                              java.lang.String name,
                                              Type ret_type,
                                              Type[] arg_types,
                                              short kind)
        Create an invoke instruction. (Except for invokedynamic.)
        Parameters:
        class_name - name of the called class
        name - name of the called method
        ret_type - return type of method
        arg_types - argument types of method
        kind - how to invoke, i.e., INVOKEINTERFACE, INVOKESTATIC, INVOKEVIRTUAL, or INVOKESPECIAL
        See Also:
        Const
      • createInvoke

        public InvokeInstruction createInvoke​(java.lang.String class_name,
                                              java.lang.String name,
                                              Type ret_type,
                                              Type[] arg_types,
                                              short kind,
                                              boolean use_interface)
        Create an invoke instruction. (Except for invokedynamic.)
        Parameters:
        class_name - name of the called class
        name - name of the called method
        ret_type - return type of method
        arg_types - argument types of method
        kind - how to invoke: INVOKEINTERFACE, INVOKESTATIC, INVOKEVIRTUAL, or INVOKESPECIAL
        use_interface - force use of InterfaceMethodref
        Returns:
        A new InvokeInstruction.
        Since:
        6.5.0
      • 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
      • 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
      • createFieldAccess

        public FieldInstruction createFieldAccess​(java.lang.String class_name,
                                                  java.lang.String name,
                                                  Type type,
                                                  short kind)
        Create a field instruction.
        Parameters:
        class_name - name of the accessed class
        name - name of the referenced field
        type - type of field
        kind - how to access, i.e., GETFIELD, PUTFIELD, GETSTATIC, PUTSTATIC
        See Also:
        Const
      • createThis

        public static Instruction createThis()
        Create reference to `this'
      • 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.
      • createPop

        public static StackInstruction createPop​(int size)
        Parameters:
        size - size of operand, either 1 (int, e.g.) or 2 (double)
      • createDup

        public static StackInstruction createDup​(int size)
        Parameters:
        size - size of operand, either 1 (int, e.g.) or 2 (double)
      • createDup_2

        public static StackInstruction createDup_2​(int size)
        Parameters:
        size - size of operand, either 1 (int, e.g.) or 2 (double)
      • createDup_1

        public static StackInstruction createDup_1​(int size)
        Parameters:
        size - size of operand, either 1 (int, e.g.) or 2 (double)
      • createArrayLoad

        public static ArrayInstruction createArrayLoad​(Type type)
        Parameters:
        type - type of elements of array, i.e., array.getElementType()
      • createArrayStore

        public static ArrayInstruction createArrayStore​(Type type)
        Parameters:
        type - type of elements of array, i.e., array.getElementType()
      • createCast

        public Instruction createCast​(Type src_type,
                                      Type dest_type)
        Create conversion operation for two stack operands, this may be an I2C, instruction, e.g., if the operands are basic types and CHECKCAST if they are reference types.
      • createGetField

        public GETFIELD createGetField​(java.lang.String class_name,
                                       java.lang.String name,
                                       Type t)
      • createGetStatic

        public GETSTATIC createGetStatic​(java.lang.String class_name,
                                         java.lang.String name,
                                         Type t)
      • createPutField

        public PUTFIELD createPutField​(java.lang.String class_name,
                                       java.lang.String name,
                                       Type t)
      • createPutStatic

        public PUTSTATIC createPutStatic​(java.lang.String class_name,
                                         java.lang.String name,
                                         Type t)
      • createNew

        public NEW createNew​(java.lang.String s)
      • createNewArray

        public Instruction createNewArray​(Type t,
                                          short dim)
        Create new array of given size and type.
        Returns:
        an instruction that creates the corresponding array at runtime, i.e. is an AllocationInstruction
      • createNull

        public static Instruction createNull​(Type type)
        Create "null" value for reference types, 0 for basic types like int
      • 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.
      • setClassGen

        public void setClassGen​(ClassGen c)
      • getClassGen

        public ClassGen getClassGen()