public interface CodeContext
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.
Modifier and Type | Method and Description |
---|---|
void |
addExceptionTableEntry(Label startHandled,
Label endHandled,
CodeClass eClass,
Label handler)
Add an exception table entry.
|
void |
close()
Close the context for writing.
|
CodeClass |
getCodeClass()
Get the class for which a method is being generated.
|
CodeMethod |
getCodeMethod()
Get the method which is being generated.
|
ConstantPool |
getConstants()
Get the constants pool for this context.
|
void |
markLabel(Label lab)
Mark a label at the current point in the stream.
|
void |
open()
Open the context for writing.
|
void |
registerParametricType(ParametricType type,
CodeClass concreteType)
Register a concrete type for a parametric type.
|
int |
resolveLocal(LocalVariable lv)
Resolve a local variable to the local variable slot assigned to it.
|
CodeClass |
resolveParametricType(ParametricType type)
Resolve a parametric type to a concrete class.
|
CodeContext |
subContext()
Open a sub context.
|
void |
writeByte(byte b)
Write a single byte to the context.
|
void |
writeLabel(Label lab)
Write the offset of a Label to the context.
|
void |
writeShort(int i)
Write a short (2 bytes) to the context.
|
CodeClass getCodeClass()
CodeMethod getCodeMethod()
ConstantPool getConstants()
void writeByte(byte b) throws CodeException
This can be used both to write opcodes and to write byte data to the context.
b
- the byte to writeCodeException
void writeShort(int i) throws CodeException
i
- the short to writeCodeException
void writeLabel(Label lab) throws CodeException
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.
lab
- the Label to writeCodeException
int resolveLocal(LocalVariable lv) throws CodeException
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.
lv
- the LocalVariable to resolveCodeException
void markLabel(Label lab) throws CodeException
This can be used as the target for branching instructions, such as GOTO and IF.
lab
- the Label to markCodeException
- if the label has previously been markedvoid registerParametricType(ParametricType type, CodeClass concreteType) throws CodeException
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.
type
- ParametricType the parametric type to registerconcreteType
- the CodeClass that it resolves toCodeException
- if the type has already been registered or if the
guarantees about type made in the parametric type are violatedCodeClass resolveParametricType(ParametricType type) throws CodeException
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.
type
- the ParametricType to resolveCodeException
- if the type has not been registeredCodeContext subContext()
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.
void open() throws CodeException
This must be called before any code writing methods are called. It can not be called more than once.
CodeException
void close() throws CodeException
This must be called after all code writing methods have been called. It can not be called more than once.
CodeException
void addExceptionTableEntry(Label startHandled, Label endHandled, CodeClass eClass, Label handler) throws CodeException
startHandled
- the beginning of the try blockendHandled
- the end of the try blockeClass
- the exception classhandler
- the beginning of the exception handlerCodeException
Copyright © 2014 BioJava. All rights reserved.