gnu.bytecode
Class CodeAttr
public
class
CodeAttr
extends Attribute
implements AttrContainer
Represents the contents of a standard "Code" attribute.
Most of the actual methods that generate bytecode operation
are in this class (typically with names starting with emit
),
though there are also some in Method
.
Note that a CodeAttr
is an Attribute
of a Method
, and can in turn contain other
Attribute
s, such as a LineNumbersAttr
.
Method Summary |
void | addHandler(int start_pc, int end_pc, int handler_pc, int catch_type) Add an exception handler. |
void | addHandler(Label start_try, Label end_try, ClassType catch_type) Add an exception handler. |
Variable | addLocal(Type type) Add a new local variable (in the current scope). |
Variable | addLocal(Type type, String name) Add a new local variable (in the current scope). |
void | addParamLocals() Call addLocal for parameters (as implied by method type). |
void | assignConstants(ClassType cl) |
int | beginFragment(Label start, Label after) |
static String | calculateSplit(String str) Calculate how many CONSTANT_String constants we need for a string.
|
static boolean | castNeeded(Type top, Type required) |
void | disAssemble(ClassTypeWriter dst, int start, int limit) |
void | doPendingFinalizers(TryState limit) Call pending finalizer functions. |
void | emitAdd(char sig) |
void | emitAdd(PrimType type) |
void | emitAdd() |
void | emitAnd() |
void | emitArrayLength() |
void | emitArrayLoad(Type element_type) Load an element from an array.
|
void | emitArrayStore(Type element_type) Store into an element of an array.
|
void | emitCatchEnd() |
void | emitCatchStart(Variable var) |
void | emitCheckcast(Type type) |
void | emitConvert(Type from, Type to) |
void | emitDiv() |
void | emitDup() Emit code to duplicate the top element of the stack. |
void | emitDup(int size, int offset) Compile code to duplicate with offset. |
void | emitDup(int size)
Compile code to duplicate the top 1 or 2 words. |
void | emitDup(Type type) |
void | emitDupX() Emit code to duplicate the top element of the stack
and place the copy before the previous element. |
void | emitElse() Compile start of else clause. |
void | emitFi() Compile end of conditional. |
void | emitFinallyEnd() |
void | emitFinallyStart() |
void | emitGetField(Field field) Compile code to get a non-static field value.
|
void | emitGetStatic(Field field) Compile code to get a static field value.
|
void | emitGoto(Label label) Compile an unconditional branch (goto). |
void | emitGotoIfCompare1(Label label, int opcode) |
void | emitGotoIfCompare2(Label label, int logop) |
void | emitGotoIfEq(Label label, boolean invert) |
void | emitGotoIfEq(Label label) Compile a conditional transfer if 2 top stack elements are equal. |
void | emitGotoIfGe(Label label) |
void | emitGotoIfGt(Label label) |
void | emitGotoIfIntEqZero(Label label) |
void | emitGotoIfIntGeZero(Label label) |
void | emitGotoIfIntGtZero(Label label) |
void | emitGotoIfIntLeZero(Label label) |
void | emitGotoIfIntLtZero(Label label) |
void | emitGotoIfIntNeZero(Label label) |
void | emitGotoIfLe(Label label) |
void | emitGotoIfLt(Label label) |
void | emitGotoIfNE(Label label) Compile conditional transfer if 2 top stack elements are not equal. |
void | emitIfCompare1(int opcode) Compile start of a conditional:
if (! |
void | emitIfEq() Compile start of a conditional: if (x == y) ...
|
void | emitIfGe() Compile start of a conditional: if (x >= y) ...
|
void | emitIfGt() Compile start of a conditional: if (x > y) ...
|
void | emitIfIntCompare(int opcode) Compile start of a conditional: if (! |
void | emitIfIntEqZero() Compile start of conditional: if (x == 0) ....
|
void | emitIfIntLEqZero() Compile start of conditional: if (x <= 0). |
void | emitIfIntLt() |
void | emitIfIntNotZero() Compile start of conditional: if (x ! |
void | emitIfLe() Compile start of a conditional: if (x <= y) ...
|
void | emitIfLt() Compile start of a conditional: if (x < y) ...
|
void | emitIfNEq() Compile start of a conditional: if (x ! |
void | emitIfNotNull() Compile start of conditional: if (x ! |
void | emitIfNull() Compile start of conditional: if (x == null) |
void | emitIfRefCompare1(int opcode) Compile start of a conditional: if (! |
void | emitIfThen() |
void | emitInc(Variable var, short inc) |
void | emitInstanceof(Type type) |
void | emitInvoke(Method method) |
void | emitInvokeInterface(Method method) |
void | emitInvokeMethod(Method method, int opcode) |
void | emitInvokeSpecial(Method method) |
void | emitInvokeStatic(Method method) Compile a static method call.
|
void | emitInvokeVirtual(Method method) Compile a virtual method call.
|
void | emitIOr() |
void | emitJsr(Label label) |
void | emitLoad(Variable var)
Compile code to push the contents of a local variable onto the statck. |
void | emitMonitorEnter() |
void | emitMonitorExit() |
void | emitMul() |
void | emitNew(ClassType type)
Invoke new on a class type.
|
void | emitNewArray(Type element_type, int dims) Compile code to allocate a new array.
|
void | emitNewArray(Type element_type) |
void | emitNot(Type type) |
void | emitPop(int nvalues) Compile code to pop values off the stack (and ignore them). |
void | emitPrimop(int opcode, int arg_count, Type retType) |
void | emitPushClass(String name) Push a class constant pool entry.
|
void | emitPushConstant(int val, Type type) |
void | emitPushConstant(CpoolEntry cnst) |
void | emitPushDouble(double x) |
void | emitPushFloat(float x) |
void | emitPushInt(int i) |
void | emitPushLong(long i) |
void | emitPushNull() |
void | emitPushPrimArray(Object value, ArrayType arrayType) Emit code to push a constant primitive array. |
void | emitPushString(String str) Emit code to push the value of a constant String.
|
void | emitPushThis() |
void | emitPutField(Field field) Compile code to put a non-static field value.
|
void | emitPutStatic(Field field) Compile code to put a static field value.
|
void | emitRem() |
void | emitRet(Variable var) Emit a 'ret' instruction. |
void | emitReturn()
Compile a method return.
|
void | emitShl() |
void | emitShr() |
void | emitStore(Variable var) |
void | emitSub(char sig) |
void | emitSub(PrimType type) |
void | emitSub() |
void | emitSwap() |
void | emitTailCall(boolean pop_args, Scope scope) Compile a tail-call to position 0 of the current procedure. |
void | emitThen() |
void | emitThrow() |
void | emitTryCatchEnd() |
void | emitTryEnd() |
void | emitTryStart(boolean has_finally, Type result_type) |
void | emitUshr() |
void | emitWithCleanupCatch(Variable catchVar) Called after a body that has a cleanup clause.
|
void | emitWithCleanupDone() Called after generating a cleanup handler. |
void | emitWithCleanupStart() Beginning of code that has a cleanup handler.
|
void | emitXOr() |
void | endFragment(int cookie) End a fragment. |
void | enterScope(Scope scope) |
void | fixupAdd(int kind, Label label) Add a fixup at this location. |
void | fixupChain(Label here, Label target) This causes a later processFixup to rearrange the code.
|
Variable | getArg(int index) Get the index'th parameter. |
Attribute | getAttributes() |
byte[] | getCode() Get the code (instruction bytes) of this method.
|
int | getCodeLength() Set the current lengthof the code (instruction bytes) of this method. |
ConstantPool | getConstants() |
Scope | getCurrentScope() |
TryState | getCurrentTry() |
Label | getLabel() Get a new Label for the current location.
|
int | getLength() |
int | getMaxLocals() Get the maximum number of local variable words in this method. |
int | getMaxStack() Get the maximum number of words on the operand stack in this method. |
Method | getMethod() |
int | getPC() |
int | getSP() |
boolean | isInTry() |
Variable | lookup(String name)
Search by name for a Variable |
Scope | popScope() |
Type | popType() |
void | print(ClassTypeWriter dst) |
void | processFixups() |
Scope | pushScope() |
void | pushType(Type type) |
void | put1(int i)
Write an 8-bit byte to the current code-stream. |
void | put2(int i)
Write a 16-bit short to the current code-stream |
void | put4(int i)
Write a 32-bit int to the current code-stream |
void | putIndex2(CpoolEntry cnst) |
void | putLineNumber(String filename, int linenumber) |
void | putLineNumber(int linenumber) |
boolean | reachableHere() True if control could reach here. |
void | reserve(int bytes) |
void | restoreStackTypeState(Type[] save) Restore a type state as saved by saveStackTypeState. |
Type[] | saveStackTypeState(boolean clear) Return an object encapsulating the type state of the JVM stack. |
void | setAttributes(Attribute attributes) |
void | setCode(byte[] code) Set the code (instruction bytes) of this method. |
void | setCodeLength(int len) Set the length the the code (instruction bytes) of this method.
|
void | setMaxLocals(int n) Set the maximum number of local variable words in this method. |
void | setMaxStack(int n) Set the maximum number of words on the operand stack in this method. |
void | setReachable(boolean val) |
void | setUnreachable() |
Type | topType() |
void | write(DataOutputStream dstr) |
public static boolean instructionLineMode
If true we get a line number entry for each instruction.
Normally false, but can be a convenient hack to allow instruction-level
stepping/debugging and stacktraces. In this case {@code LINE==PC}.
public void addHandler(int start_pc, int end_pc, int handler_pc, int catch_type)
Add an exception handler.
Add an exception handler.
Add a new local variable (in the current scope).
Parameters: type type of the new Variable.
Returns: the new Variable.
Add a new local variable (in the current scope).
Parameters: type type of the new Variable. name name of the new Variable.
Returns: the new Variable.
public void addParamLocals()
Call addLocal for parameters (as implied by method type).
public int beginFragment(
Label start,
Label after)
public static final String calculateSplit(String str)
Calculate how many CONSTANT_String constants we need for a string.
Each CONSTANT_String can be at most 0xFFFF bytes (as a UTF8 string).
Returns a String, where each char, coerced to an int, is the length
of a substring of the input that is at most 0xFFFF bytes.
public static boolean castNeeded(
Type top,
Type required)
public void doPendingFinalizers(
TryState limit)
Call pending finalizer functions.
Parameters: limit Only call finalizers more recent than this.
public final void emitAdd(char sig)
public final void emitAdd(
PrimType type)
public final void emitAdd()
public final void emitAnd()
public final void emitArrayLength()
public void emitArrayLoad(
Type element_type)
Load an element from an array.
Must already have pushed the array and the index (in that order):
Stack: ..., array, index => ..., value
public void emitArrayStore(
Type element_type)
Store into an element of an array.
Must already have pushed the array reference, the index,
and the new value (in that order).
Stack: ..., array, index, value => ...
public void emitCatchEnd()
public void emitCatchStart(
Variable var)
public void emitCheckcast(
Type type)
public final void emitConvert(
Type from,
Type to)
public final void emitDiv()
public void emitDup()
Emit code to duplicate the top element of the stack.
public void emitDup(int size, int offset)
Compile code to duplicate with offset.
Parameters: size the size of the stack item to duplicate (1 or 2) offset where to insert the result (must be 0, 1, or 2)
The new words get inserted at stack[SP-size-offset]
public void emitDup(int size)
Compile code to duplicate the top 1 or 2 words.
Parameters: size number of words to duplicate
public void emitDup(
Type type)
public void emitDupX()
Emit code to duplicate the top element of the stack
and place the copy before the previous element.
public final void emitElse()
Compile start of else clause.
public final void emitFi()
Compile end of conditional.
public void emitFinallyEnd()
public void emitFinallyStart()
public final void emitGetField(
Field field)
Compile code to get a non-static field value.
Stack: ..., objectref => ..., value
public final void emitGetStatic(
Field field)
Compile code to get a static field value.
Stack: ... => ..., value
public final void emitGoto(
Label label)
Compile an unconditional branch (goto).
Parameters: label target of the branch (must be in this method).
public final void emitGotoIfCompare1(
Label label, int opcode)
public final void emitGotoIfCompare2(
Label label, int logop)
public final void emitGotoIfEq(
Label label, boolean invert)
public final void emitGotoIfEq(
Label label)
Compile a conditional transfer if 2 top stack elements are equal.
public final void emitGotoIfGe(
Label label)
public final void emitGotoIfGt(
Label label)
public final void emitGotoIfIntEqZero(
Label label)
public final void emitGotoIfIntGeZero(
Label label)
public final void emitGotoIfIntGtZero(
Label label)
public final void emitGotoIfIntLeZero(
Label label)
public final void emitGotoIfIntLtZero(
Label label)
public final void emitGotoIfIntNeZero(
Label label)
public final void emitGotoIfLe(
Label label)
public final void emitGotoIfLt(
Label label)
public final void emitGotoIfNE(
Label label)
Compile conditional transfer if 2 top stack elements are not equal.
public final void emitIfCompare1(int opcode)
Compile start of a conditional:
if (!(x opcode 0)) ....
The value of x must already have been pushed.
public final void emitIfEq()
Compile start of a conditional: if (x == y) ...
The values of x and y must already have been pushed.
public final void emitIfGe()
Compile start of a conditional: if (x >= y) ...
The values of x and y must already have been pushed.
public final void emitIfGt()
Compile start of a conditional: if (x > y) ...
The values of x and y must already have been pushed.
public final void emitIfIntCompare(int opcode)
Compile start of a conditional: if (!(x OPCODE y)) ...
The value of x and y must already have been pushed.
public final void emitIfIntEqZero()
Compile start of conditional: if (x == 0) ....
Also use this if you have pushed a boolean value: if (!b) ...
public final void emitIfIntLEqZero()
Compile start of conditional: if (x <= 0).
public final void emitIfIntLt()
public final void emitIfIntNotZero()
Compile start of conditional: if (x != 0) ....
Also use this if you have pushed a boolean value: if (b) ...
public final void emitIfLe()
Compile start of a conditional: if (x <= y) ...
The values of x and y must already have been pushed.
public final void emitIfLt()
Compile start of a conditional: if (x < y) ...
The values of x and y must already have been pushed.
public final void emitIfNEq()
Compile start of a conditional: if (x != y) ...
The values of x and y must already have been pushed.
public final void emitIfNotNull()
Compile start of conditional: if (x != null)
public final void emitIfNull()
Compile start of conditional: if (x == null)
public final void emitIfRefCompare1(int opcode)
Compile start of a conditional: if (!(x opcode null)) ....
The value of x must already have been pushed and must be of
reference type.
public final void emitIfThen()
public void emitInc(
Variable var, short inc)
public void emitInstanceof(
Type type)
public void emitInvoke(
Method method)
public void emitInvokeInterface(
Method method)
public void emitInvokeMethod(
Method method, int opcode)
public void emitInvokeSpecial(
Method method)
public void emitInvokeStatic(
Method method)
Compile a static method call.
The stack contains the the arguments in order.
Parameters: method the static method to invoke
public void emitInvokeVirtual(
Method method)
Compile a virtual method call.
The stack contains the 'this' object, followed by the arguments in order.
Parameters: method the method to invoke virtually
public final void emitIOr()
public final void emitJsr(
Label label)
public final void emitLoad(
Variable var)
Compile code to push the contents of a local variable onto the statck.
Parameters: var The variable whose contents we want to push.
public final void emitMonitorEnter()
public final void emitMonitorExit()
public final void emitMul()
Invoke new on a class type.
Does not call the constructor!
Parameters: type the desired new object type
public void emitNewArray(
Type element_type, int dims)
Compile code to allocate a new array.
The size should have been already pushed on the stack.
Parameters: element_type type of the array elements
public void emitNewArray(
Type element_type)
public final void emitNot(
Type type)
public void emitPop(int nvalues)
Compile code to pop values off the stack (and ignore them).
Parameters: nvalues the number of values (not words) to pop
public void emitPrimop(int opcode, int arg_count,
Type retType)
public final void emitPushClass(String name)
Push a class constant pool entry.
This is only supported by JDK 1.5 and later.
public final void emitPushConstant(int val,
Type type)
public final void emitPushConstant(
CpoolEntry cnst)
public void emitPushDouble(double x)
public void emitPushFloat(float x)
public final void emitPushInt(int i)
public void emitPushLong(long i)
public void emitPushNull()
public final void emitPushPrimArray(Object value,
ArrayType arrayType)
Emit code to push a constant primitive array.
Parameters: value The array value that we want the emitted code to re-create. arrayType The ArrayType that matches value.
public final void emitPushString(String str)
Emit code to push the value of a constant String.
Uses CONSTANT_String and CONSTANT_Utf8 constant pool entries as needed.
Can handle Strings whose UTF8 length is greates than 0xFFFF bytes
(the limit of a CONSTANT_Utf8) by generating String concatenation.
public final void emitPushThis()
public final void emitPutField(
Field field)
Compile code to put a non-static field value.
Stack: ..., objectref, value => ...
public final void emitPutStatic(
Field field)
Compile code to put a static field value.
Stack: ..., value => ...
public final void emitRem()
Emit a 'ret' instruction.
Parameters: var the variable containing the return address
public final void emitReturn()
Compile a method return.
If inside a 'catch' clause, first call 'finally' clauses.
The return value (unless the return type is void) must be on the stack,
and have the correct type.
public final void emitShl()
public final void emitShr()
public final void emitSub(char sig)
public final void emitSub(
PrimType type)
public final void emitSub()
public void emitSwap()
public void emitTailCall(boolean pop_args,
Scope scope)
Compile a tail-call to position 0 of the current procedure.
Parameters: pop_args if true, copy argument registers (except this) from stack. scope Scope whose start we jump back to.
public final void emitThen()
public final void emitThrow()
public void emitTryCatchEnd()
public void emitTryEnd()
public void emitTryStart(boolean has_finally,
Type result_type)
public final void emitUshr()
public void emitWithCleanupCatch(
Variable catchVar)
Called after a body
that has a cleanup
clause.
Followed by the cleanup
code.
public void emitWithCleanupDone()
Called after generating a cleanup
handler.
public void emitWithCleanupStart()
Beginning of code that has a cleanup handler.
This is similar to a try-finally, but the cleanup is only
done in the case of an exception. Alternatively, the try clause
has to manually do the cleanup with code duplication.
Equivalent to: try body catch (Throwable ex) { cleanup; throw ex; }
Call emitWithCleanupStart
before the body
.
public final void emitXOr()
public void endFragment(int cookie)
End a fragment.
Parameters: cookie the return value from the previous beginFragment.
public void enterScope(
Scope scope)
public final void fixupAdd(int kind,
Label label)
Add a fixup at this location.
Parameters: kind one of the FIXUP_xxx codes. label varies - typically the target of jump.
public final void fixupChain(
Label here,
Label target)
This causes a later processFixup to rearrange the code.
The code at target comes here, instead of the following instructions.
Fuctionally equivalent to: goto target; here:
,
but implemented by code re-arranging. Therefore there should be
at some later point a goto here; target:
.
Get the index'th parameter.
public byte[] getCode()
Get the code (instruction bytes) of this method.
Does not make a copy.
public int getCodeLength()
Set the current lengthof the code (instruction bytes) of this method.
public
Scope getCurrentScope()
Get a new Label for the current location.
Unlike Label.define, Does not change reachableHere().
public final int getLength()
public int getMaxLocals()
Get the maximum number of local variable words in this method.
public int getMaxStack()
Get the maximum number of words on the operand stack in this method.
public final
Method getMethod()
public final int getPC()
public final int getSP()
public final boolean isInTry()
Search by name for a Variable
Parameters: name name to search for
Returns: the Variable, or null if not found (in any scope of this Method).
public final
Type popType()
public void processFixups()
public final void pushType(
Type type)
public final void put1(int i)
Write an 8-bit byte to the current code-stream.
Parameters: i the byte to write
public final void put2(int i)
Write a 16-bit short to the current code-stream
Parameters: i the value to write
public final void put4(int i)
Write a 32-bit int to the current code-stream
Parameters: i the value to write
public final void putLineNumber(String filename, int linenumber)
public final void putLineNumber(int linenumber)
public final boolean reachableHere()
True if control could reach here.
public final void reserve(int bytes)
public void restoreStackTypeState(
Type[] save)
Restore a type state as saved by saveStackTypeState.
public
Type[] saveStackTypeState(boolean clear)
Return an object encapsulating the type state of the JVM stack.
public final void setAttributes(
Attribute attributes)
public void setCode(byte[] code)
Set the code (instruction bytes) of this method.
Parameters: code the code bytes (which are not copied).
Implicitly calls setCodeLength(code.length).
public void setCodeLength(int len)
Set the length the the code (instruction bytes) of this method.
That is the number of current used bytes in getCode().
(Any remaing bytes provide for future growth.)
public void setMaxLocals(int n)
Set the maximum number of local variable words in this method.
public void setMaxStack(int n)
Set the maximum number of words on the operand stack in this method.
public final void setReachable(boolean val)
public final void setUnreachable()
public final
Type topType()
public void write(DataOutputStream dstr)