Interface CodeContext


  • public interface CodeContext
    Interface which encapsulates the stream to which Java bytecode can be written.

    The context takes care of all the book-keeping tasks associated with emitting well-formed byte code. For example, the context manages jumps and local variables.

    Most of the funcionality here is very low level. You will almost certainly want to use CodeGenerator instances to manipulate a CodeContext, rather than writing to it yourself.

    Author:
    Thomas Down, Matthew Pocock
    • Method Detail

      • getCodeClass

        CodeClass getCodeClass()
        Get the class for which a method is being generated.
        Returns:
        the current class
      • getCodeMethod

        CodeMethod getCodeMethod()
        Get the method which is being generated.
        Returns:
        the current method
      • getConstants

        ConstantPool getConstants()
        Get the constants pool for this context.
        Returns:
        the contant pool
      • writeByte

        void writeByte​(byte b)
                throws CodeException
        Write a single byte to the context.

        This can be used both to write opcodes and to write byte data to the context.

        Parameters:
        b - the byte to write
        Throws:
        CodeException
      • writeShort

        void writeShort​(int i)
                 throws CodeException
        Write a short (2 bytes) to the context.
        Parameters:
        i - the short to write
        Throws:
        CodeException
      • writeLabel

        void writeLabel​(Label lab)
                 throws CodeException
        Write the offset of a Label to the context.

        This can be called before or after markLabel is invoked for the corresponding label. The context will ensure that the offset is correctly written before the method is fully emitted.

        Parameters:
        lab - the Label to write
        Throws:
        CodeException
      • resolveLocal

        int resolveLocal​(LocalVariable lv)
                  throws CodeException
        Resolve a local variable to the local variable slot assigned to it.

        The context will ensure that local variables are stored in their own bits of the local variable area. It may chose to re-use portions of this area as local variables go out of scope.

        Parameters:
        lv - the LocalVariable to resolve
        Returns:
        the index of the local variable slot
        Throws:
        CodeException
      • markLabel

        void markLabel​(Label lab)
                throws CodeException
        Mark a label at the current point in the stream.

        This can be used as the target for branching instructions, such as GOTO and IF.

        Parameters:
        lab - the Label to mark
        Throws:
        CodeException - if the label has previously been marked
      • registerParametricType

        void registerParametricType​(ParametricType type,
                                    CodeClass concreteType)
                             throws CodeException
        Register a concrete type for a parametric type.

        This is the mechanism where-by real CodeClass types are associated with the virtual ParametricType types. If type pubishes that it is a primative, an object or an array, then the concreteType must be compattible. It's an error to bind the VOID type.

        Parameters:
        type - ParametricType the parametric type to register
        concreteType - the CodeClass that it resolves to
        Throws:
        CodeException - if the type has already been registered or if the guarantees about type made in the parametric type are violated
      • resolveParametricType

        CodeClass resolveParametricType​(ParametricType type)
                                 throws CodeException
        Resolve a parametric type to a concrete class.

        The type will be resolved by first searching through all those registered with this context. If it found there, this value is returned. If it is not, then the emediate parent context is searched. This parent is responsible for searching its parent and so on. If a context has no parent and the type is not registered with this context, it should raise a CodeException.

        Parameters:
        type - the ParametricType to resolve
        Returns:
        the ColdeClass associated with that parametric type
        Throws:
        CodeException - if the type has not been registered
      • subContext

        CodeContext subContext()
        Open a sub context.

        The sub context should inherit all the state of the parent context. Modifications to the state of the child (for example, local variable management or registered labels) should not be propogated to the parent. Modifications to the parent should not be propogated to the child. However, contexts should be used serialy, and only one context should be accessible to a code generator at a time, so in practice, there should be no way for a code generator using a child context to alter the state of or discover the state of the parent context.

      • open

        void open()
           throws CodeException
        Open the context for writing.

        This must be called before any code writing methods are called. It can not be called more than once.

        Throws:
        CodeException
      • close

        void close()
            throws CodeException
        Close the context for writing. It is at this point that any process necisary for comitting the bytecode will be executed.

        This must be called after all code writing methods have been called. It can not be called more than once.

        Throws:
        CodeException
      • addExceptionTableEntry

        void addExceptionTableEntry​(Label startHandled,
                                    Label endHandled,
                                    CodeClass eClass,
                                    Label handler)
                             throws CodeException
        Add an exception table entry.
        Parameters:
        startHandled - the beginning of the try block
        endHandled - the end of the try block
        eClass - the exception class
        handler - the beginning of the exception handler
        Throws:
        CodeException