Class MethodCompiler

  • All Implemented Interfaces:
    OpCode

    public abstract class MethodCompiler
    extends Assembler
    • Field Detail

      • SUBROUTINERETURNADDRESSNAME

        public static final java.lang.String SUBROUTINERETURNADDRESSNAME
        See Also:
        Constant Field Values
      • localVariables

        private final java.util.List<javax.lang.model.element.VariableElement> localVariables
      • debugMethod

        private javax.lang.model.element.ExecutableElement debugMethod
      • compiled

        private boolean compiled
      • subroutine

        private java.lang.String subroutine
      • optimize

        private boolean optimize
      • dump

        private boolean dump
      • exceptionTableList

        private final java.util.List<ExceptionTable> exceptionTableList
      • executableElement

        protected javax.lang.model.element.ExecutableElement executableElement
      • executableType

        protected javax.lang.model.type.ExecutableType executableType
    • Constructor Detail

      • MethodCompiler

        protected MethodCompiler()
    • Method Detail

      • startImplement

        void startImplement​(SubClass subClass,
                            MethodInfo methodInfo)
                     throws java.io.IOException
        Throws:
        java.io.IOException
      • getExecutableElement

        public javax.lang.model.element.ExecutableElement getExecutableElement()
      • getSubClass

        public SubClass getSubClass()
      • getModifiers

        public java.lang.String getModifiers()
      • setOptimize

        public void setOptimize​(boolean optimize)
        Set the compiler optimize flag. Affects switches
        Parameters:
        optimize -
      • getMethodDescription

        public java.lang.String getMethodDescription()
      • getParameters

        public java.util.List<? extends javax.lang.model.type.TypeMirror> getParameters()
      • getReturnType

        public javax.lang.model.type.TypeMirror getReturnType()
      • localSize

        public int localSize()
      • localSize

        private int localSize​(int offset,
                              int length)
      • nameArgument

        public final void nameArgument​(java.lang.String name,
                                       int index)
        Names an argument
        Parameters:
        name - Argument name
        index - First argument is 1, this = 0
      • getLocalVariable

        public javax.lang.model.element.VariableElement getLocalVariable​(int index)
        Returns local variable at index. Note! long and double take two positions.
        Parameters:
        index -
        Returns:
      • getLocalVariable

        public javax.lang.model.element.VariableElement getLocalVariable​(java.lang.String name)
      • getLocalVariableIndex

        public int getLocalVariableIndex​(java.lang.String name)
      • nameArguments

        public void nameArguments​(java.lang.String[] args)
      • getLocalName

        public java.lang.String getLocalName​(int index)
        Returns the name of local variable at index
        Parameters:
        index -
        Returns:
      • getLocalType

        public javax.lang.model.type.TypeMirror getLocalType​(int index)
        Returns the type of local variable at index
        Parameters:
        index -
        Returns:
      • getLocalType

        public javax.lang.model.type.TypeMirror getLocalType​(java.lang.String name)
        returns the type of local variable named name.
        Parameters:
        name -
        Returns:
      • hasLocalVariable

        public boolean hasLocalVariable​(java.lang.String name)
        Return true if named variable is added.
        Parameters:
        name -
        Returns:
      • getLocalDescription

        public java.lang.String getLocalDescription​(int index)
        return a descriptive text about local variable named name.
        Parameters:
        index -
        Returns:
      • addVariable

        public void addVariable​(java.lang.String name,
                                java.lang.Class<?> type)
        Add new local variable
        Parameters:
        name -
        type -
      • addVariable

        public void addVariable​(java.lang.String name,
                                javax.lang.model.type.TypeMirror type)
        Add new local variable
        Parameters:
        name -
        type -
      • assignDefault

        public void assignDefault​(java.lang.String name)
                           throws java.io.IOException
        assign default type for local variable depending on type. (false, 0, null)
        Parameters:
        name -
        Throws:
        java.io.IOException
      • loadDefault

        public void loadDefault​(java.lang.Class<?> type)
                         throws java.io.IOException
        Load default value to stack depending on type
        Parameters:
        type -
        Throws:
        java.io.IOException
      • loadDefault

        public void loadDefault​(javax.lang.model.type.TypeMirror type)
                         throws java.io.IOException
        Load default value to stack depending on type
        Parameters:
        type -
        Throws:
        java.io.IOException
      • startSubroutine

        public void startSubroutine​(java.lang.String target)
                             throws java.io.IOException
        Compiles the subroutine start. Use endSubroutine to end that subroutine.
        Parameters:
        target -
        Throws:
        java.io.IOException
      • endSubroutine

        public void endSubroutine()
                           throws java.io.IOException
        Throws:
        java.io.IOException
      • resetSubroutine

        public void resetSubroutine()
        Reset a started subroutine
      • newarray

        public void newarray​(java.lang.Class<?> type,
                             int count)
                      throws java.io.IOException
        Create new array

        Stack: ..., count => ..., arrayref

        Parameters:
        type - array class eg. String[].class
        count - array count
        Throws:
        java.io.IOException
      • newarray

        public void newarray​(javax.lang.model.type.TypeMirror type,
                             int count)
                      throws java.io.IOException
        Create new array

        Stack: ..., => ..., arrayref

        Parameters:
        type - array class eg. String[].class
        count - array count
        Throws:
        java.io.IOException
      • typeForCount

        public javax.lang.model.type.TypeMirror typeForCount​(int count)
        Return a integral class able to support count values
        Parameters:
        count -
        Returns:
      • setDebug

        public void setDebug​(javax.lang.model.element.ExecutableElement debug)
      • fixAddress

        public void fixAddress​(java.lang.String name)
                        throws java.io.IOException
        Labels a current position
        Overrides:
        fixAddress in class Assembler
        Parameters:
        name -
        Throws:
        java.io.IOException
      • anew

        public void anew​(java.lang.Class<?> clazz)
                  throws java.io.IOException
        Create new object

        Stack: ... => ..., objectref

        Parameters:
        clazz -
        Throws:
        java.io.IOException
      • anew

        public void anew​(javax.lang.model.element.TypeElement clazz)
                  throws java.io.IOException
        Create new object

        Stack: ... => ..., objectref

        Parameters:
        clazz -
        Throws:
        java.io.IOException
      • tconst

        public void tconst​(java.lang.Object value)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • tconst

        public void tconst​(int value)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • tconst

        public void tconst​(boolean value)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • tconst

        public void tconst​(char value)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • tconst

        public void tconst​(byte value)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • tconst

        public void tconst​(long value)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • tconst

        public void tconst​(float value)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • tconst

        public void tconst​(double value)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • tconst

        public void tconst​(java.lang.String value)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • ldc

        public void ldc​(java.lang.Class constant)
                 throws java.io.IOException
        Throws:
        java.io.IOException
      • getField

        public void getField​(javax.lang.model.element.VariableElement field)
                      throws java.io.IOException
        Fetch field from object

        Stack: ..., objectref => ..., value

        Parameters:
        field -
        Throws:
        java.io.IOException
      • getStaticField

        public void getStaticField​(javax.lang.model.element.VariableElement field)
                            throws java.io.IOException
        Fetch field from object

        Stack: ..., objectref => ..., value

        Parameters:
        field -
        Throws:
        java.io.IOException
      • getField

        public void getField​(java.lang.Class<?> cls,
                             java.lang.String name)
                      throws java.io.IOException
        Get field from class

        Stack: ..., => ..., value

        Parameters:
        cls -
        name -
        Throws:
        java.io.IOException
      • getField

        public void getField​(java.lang.String name)
                      throws java.io.IOException
        Get field from this class

        Stack: ..., => ..., value

        Parameters:
        name -
        Throws:
        java.io.IOException
      • getStaticField

        public void getStaticField​(java.lang.Class<?> cls,
                                   java.lang.String name)
                            throws java.io.IOException
        Get field from class

        Stack: ..., => ..., value

        Parameters:
        cls -
        name -
        Throws:
        java.io.IOException
      • getStaticField

        public void getStaticField​(java.lang.String name)
                            throws java.io.IOException
        Get field from this class

        Stack: ..., => ..., value

        Parameters:
        name -
        Throws:
        java.io.IOException
      • putField

        public void putField​(javax.lang.model.element.VariableElement field)
                      throws java.io.IOException
        Set field in object

        Stack: ..., objectref, value => ...

        Parameters:
        field -
        Throws:
        java.io.IOException
      • putStaticField

        public void putStaticField​(javax.lang.model.element.VariableElement field)
                            throws java.io.IOException
        Set static field in object

        Stack: ..., objectref, value => ...

        Parameters:
        field -
        Throws:
        java.io.IOException
      • putField

        public void putField​(java.lang.Class<?> cls,
                             java.lang.String name)
                      throws java.io.IOException
        Set field in class

        Stack: ..., value => ...

        Parameters:
        cls -
        name -
        Throws:
        java.io.IOException
      • putField

        public void putField​(java.lang.String name)
                      throws java.io.IOException
        Set field in this class

        Stack: ..., value => ...

        Parameters:
        name -
        Throws:
        java.io.IOException
      • putStaticField

        public void putStaticField​(java.lang.Class<?> cls,
                                   java.lang.String name)
                            throws java.io.IOException
        Set field in class

        Stack: ..., value => ...

        Parameters:
        cls -
        name -
        Throws:
        java.io.IOException
      • putStaticField

        public void putStaticField​(java.lang.String name)
                            throws java.io.IOException
        Set field in this class

        Stack: ..., value => ...

        Parameters:
        name -
        Throws:
        java.io.IOException
      • invokeConstructor

        public void invokeConstructor​(java.lang.Class<?> cls,
                                      java.lang.Class<?>... parameters)
                               throws java.io.IOException
        Parameters:
        cls -
        parameters -
        Throws:
        java.io.IOException
      • invokeMethod

        public void invokeMethod​(java.lang.Class<?> cls,
                                 java.lang.String name,
                                 java.lang.Class<?>... parameters)
                          throws java.io.IOException
        Parameters:
        cls -
        name -
        parameters -
        Throws:
        java.io.IOException
      • invoke

        public void invoke​(javax.lang.model.element.ExecutableElement method)
                    throws java.io.IOException
        Parameters:
        method -
        Throws:
        java.io.IOException
      • invokespecial

        public void invokespecial​(java.lang.Class<?> cls,
                                  java.lang.String name,
                                  java.lang.Class<?>... parameters)
                           throws java.io.IOException
        Invoke instance method; special handling for superclass, private, and instance initialization method invocations

        Stack: ..., objectref, [arg1, [arg2 ...]] => ...

        Parameters:
        cls -
        name -
        parameters -
        Throws:
        java.io.IOException
      • invokespecial

        public void invokespecial​(javax.lang.model.element.ExecutableElement method)
                           throws java.io.IOException
        Invoke instance method; special handling for superclass, private, and instance initialization method invocations

        Stack: ..., objectref, [arg1, [arg2 ...]] => ...

        Parameters:
        method -
        Throws:
        java.io.IOException
      • invokevirtual

        public void invokevirtual​(java.lang.Class<?> cls,
                                  java.lang.String name,
                                  java.lang.Class<?>... parameters)
                           throws java.io.IOException
        Invoke instance method; dispatch based on classIf method is interface calls invokeinterface otherwise calls invokevirtual.

        Stack: ..., objectref, [arg1, [arg2 ...]] => ...

        Parameters:
        cls -
        name -
        parameters -
        Throws:
        java.io.IOException
      • invokevirtual

        public void invokevirtual​(javax.lang.model.element.ExecutableElement method)
                           throws java.io.IOException
        Invoke instance method; dispatch based on classIf method is interface calls invokeinterface otherwise calls invokevirtual.

        Stack: ..., objectref, [arg1, [arg2 ...]] => ...

        Parameters:
        method -
        Throws:
        java.io.IOException
      • invokestatic

        public void invokestatic​(java.lang.Class<?> cls,
                                 java.lang.String name,
                                 java.lang.Class<?>... parameters)
                          throws java.io.IOException
        Invoke a class (static) method

        Stack: ..., [arg1, [arg2 ...]] => ...

        Parameters:
        cls -
        name -
        parameters -
        Throws:
        java.io.IOException
      • invokestatic

        public void invokestatic​(javax.lang.model.element.ExecutableElement method)
                          throws java.io.IOException
        Invoke a class (static) method

        Stack: ..., [arg1, [arg2 ...]] => ...

        Parameters:
        method -
        Throws:
        java.io.IOException
      • argumentCount

        private int argumentCount​(java.util.List<? extends javax.lang.model.element.VariableElement> parameters)
      • end

        public void end()
                 throws java.io.IOException
        Deprecated.
        Current implementation does nothing
        Throws:
        java.io.IOException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • iconst

        public void iconst​(boolean value)
                    throws java.io.IOException
        Push int constant

        Stack: ..., => ..., i

        Parameters:
        value -
        Throws:
        java.io.IOException
      • iconst

        public void iconst​(int value)
                    throws java.io.IOException
        Push int constant. Uses either iconst or ldc.

        Stack: ..., => ..., i

        Overrides:
        iconst in class Assembler
        Parameters:
        value -
        Throws:
        java.io.IOException
      • fconst

        public void fconst​(float value)
                    throws java.io.IOException
        Push float constant. Uses either fconst or ldc.

        Stack: ..., => ..., f

        Parameters:
        value -
        Throws:
        java.io.IOException
      • lconst

        public void lconst​(long value)
                    throws java.io.IOException
        Push long constant. Uses either lconst or ldc.

        Stack: ..., => ..., l

        Parameters:
        value -
        Throws:
        java.io.IOException
      • dconst

        public void dconst​(double value)
                    throws java.io.IOException
        Push double constant. Use either dconst or ldc.

        Stack: ..., => ..., d

        Parameters:
        value -
        Throws:
        java.io.IOException
      • optimizedSwitch

        public void optimizedSwitch​(LookupList list)
                             throws java.io.IOException
        Compiles a tableswitch, lookupswitch or if/goto depending on contents of LookupList.

        Stack: ..., index => ...,

        throws SwitchException if target illegal.

        Parameters:
        list -
        Throws:
        java.io.IOException
      • optimizedSwitch

        public void optimizedSwitch​(java.lang.String def,
                                    LookupList list)
                             throws java.io.IOException
        Compiles a tableswitch, lookupswitch or if/goto depending on contents of LookupList.

        Stack: ..., index => ...,

        Parameters:
        def -
        list -
        Throws:
        java.io.IOException
      • lookupswitch

        public void lookupswitch​(LookupList list)
                          throws java.io.IOException
        Compiles lookupswitch

        Stack: ..., index => ...,

        Parameters:
        list -
        Throws:
        java.io.IOException
      • tableswitch

        public void tableswitch​(int low,
                                int high,
                                java.util.List<java.lang.String> symbols)
                         throws java.io.IOException
        Compiles tableswitch

        Stack: ..., index => ...,

        Parameters:
        low -
        high -
        symbols -
        Throws:
        java.io.IOException
      • tableswitch

        public void tableswitch​(int low,
                                int high,
                                java.lang.String... symbols)
                         throws java.io.IOException
        Compiles tableswitch

        Stack: ..., index => ...,

        Parameters:
        low -
        high -
        symbols -
        Throws:
        java.io.IOException
      • addNewArray

        public void addNewArray​(java.lang.String name,
                                java.lang.Class<?> aClass,
                                int size)
                         throws java.io.IOException
        Create new array

        Stack: ..., count => ..., arrayref

        Parameters:
        name -
        aClass -
        size -
        Throws:
        java.io.IOException
      • addNewArray

        public void addNewArray​(java.lang.String name,
                                javax.lang.model.type.TypeMirror type,
                                int size)
                         throws java.io.IOException
        Create new array

        Stack: ..., count => ..., arrayref

        Parameters:
        name -
        type -
        size -
        Throws:
        java.io.IOException
      • checkcast

        public void checkcast​(java.lang.Class<?> objectref)
                       throws java.io.IOException
        Check whether object is of given type

        Stack: ..., objectref => ..., objectref

        Parameters:
        objectref -
        Throws:
        java.io.IOException
      • checkcast

        public void checkcast​(javax.lang.model.element.TypeElement objectref)
                       throws java.io.IOException
        Check whether object is of given type

        Stack: ..., objectref => ..., objectref

        Parameters:
        objectref -
        Throws:
        java.io.IOException
      • checkcast

        public void checkcast​(javax.lang.model.type.TypeMirror type)
                       throws java.io.IOException
        Check whether object is of given type

        Stack: ..., objectref => ..., objectref

        Parameters:
        type -
        Throws:
        java.io.IOException
      • convert

        public void convert​(java.lang.String fromVariable,
                            javax.lang.model.type.TypeMirror to)
                     throws java.io.IOException,
                            IllegalConversionException
        Convert a local variable to given type. (if it is possible)

        Stack: ..., => ..., objectref

        Parameters:
        fromVariable -
        to -
        Throws:
        java.io.IOException
        IllegalConversionException
      • addExceptionHandler

        public void addExceptionHandler​(Block block,
                                        java.lang.String handler,
                                        java.lang.Class<? extends java.lang.Throwable>... catchTypes)
        Adds an exception handler. If one of catchTypes is thrown inside block the execution continues at handler address. Thrown object is pushed in stack.
        Parameters:
        block -
        handler -
        catchTypes - Throwable objects which are caught. If none is present then all throwables are caught. Note! This is not the same as finally!
      • addExceptionHandler

        public void addExceptionHandler​(Block block,
                                        java.lang.String handler,
                                        java.util.List<? extends javax.lang.model.type.TypeMirror> thrownTypes)
        Adds an exception handler. If one of catchTypes is thrown inside block the execution continues at handler address. Thrown object is pushed in stack.
        Parameters:
        block -
        handler -
        thrownTypes - Throwable objects which are caught. If none is present then all throwables are caught. Note! This is not the same as finally!
      • implement

        protected abstract void implement()
                                   throws java.io.IOException
        Throws:
        java.io.IOException