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 Attributes, such as a LineNumbersAttr.

Field Summary
static booleaninstructionLineMode
If true we get a line number entry for each instruction.
LocalVarsAttrlocals
Constructor Summary
CodeAttr(Method meth)
Method Summary
voidaddHandler(int start_pc, int end_pc, int handler_pc, int catch_type)
Add an exception handler.
voidaddHandler(Label start_try, Label end_try, ClassType catch_type)
Add an exception handler.
VariableaddLocal(Type type)
Add a new local variable (in the current scope).
VariableaddLocal(Type type, String name)
Add a new local variable (in the current scope).
voidaddParamLocals()
Call addLocal for parameters (as implied by method type).
voidassignConstants(ClassType cl)
intbeginFragment(Label start, Label after)
static StringcalculateSplit(String str)
Calculate how many CONSTANT_String constants we need for a string.
static booleancastNeeded(Type top, Type required)
voiddisAssemble(ClassTypeWriter dst, int start, int limit)
voiddoPendingFinalizers(TryState limit)
Call pending finalizer functions.
voidemitAdd(char sig)
voidemitAdd(PrimType type)
voidemitAdd()
voidemitAnd()
voidemitArrayLength()
voidemitArrayLoad(Type element_type)
Load an element from an array.
voidemitArrayStore(Type element_type)
Store into an element of an array.
voidemitCatchEnd()
voidemitCatchStart(Variable var)
voidemitCheckcast(Type type)
voidemitConvert(Type from, Type to)
voidemitDiv()
voidemitDup()
Emit code to duplicate the top element of the stack.
voidemitDup(int size, int offset)
Compile code to duplicate with offset.
voidemitDup(int size)
Compile code to duplicate the top 1 or 2 words.
voidemitDup(Type type)
voidemitDupX()
Emit code to duplicate the top element of the stack and place the copy before the previous element.
voidemitElse()
Compile start of else clause.
voidemitFi()
Compile end of conditional.
voidemitFinallyEnd()
voidemitFinallyStart()
voidemitGetField(Field field)
Compile code to get a non-static field value.
voidemitGetStatic(Field field)
Compile code to get a static field value.
voidemitGoto(Label label)
Compile an unconditional branch (goto).
voidemitGotoIfCompare1(Label label, int opcode)
voidemitGotoIfCompare2(Label label, int logop)
voidemitGotoIfEq(Label label, boolean invert)
voidemitGotoIfEq(Label label)
Compile a conditional transfer if 2 top stack elements are equal.
voidemitGotoIfGe(Label label)
voidemitGotoIfGt(Label label)
voidemitGotoIfIntEqZero(Label label)
voidemitGotoIfIntGeZero(Label label)
voidemitGotoIfIntGtZero(Label label)
voidemitGotoIfIntLeZero(Label label)
voidemitGotoIfIntLtZero(Label label)
voidemitGotoIfIntNeZero(Label label)
voidemitGotoIfLe(Label label)
voidemitGotoIfLt(Label label)
voidemitGotoIfNE(Label label)
Compile conditional transfer if 2 top stack elements are not equal.
voidemitIfCompare1(int opcode)
Compile start of a conditional: if (!
voidemitIfEq()
Compile start of a conditional: if (x == y) ...
voidemitIfGe()
Compile start of a conditional: if (x >= y) ...
voidemitIfGt()
Compile start of a conditional: if (x > y) ...
voidemitIfIntCompare(int opcode)
Compile start of a conditional: if (!
voidemitIfIntEqZero()
Compile start of conditional: if (x == 0) ....
voidemitIfIntLEqZero()
Compile start of conditional: if (x <= 0).
voidemitIfIntLt()
voidemitIfIntNotZero()
Compile start of conditional: if (x !
voidemitIfLe()
Compile start of a conditional: if (x <= y) ...
voidemitIfLt()
Compile start of a conditional: if (x < y) ...
voidemitIfNEq()
Compile start of a conditional: if (x !
voidemitIfNotNull()
Compile start of conditional: if (x !
voidemitIfNull()
Compile start of conditional: if (x == null)
voidemitIfRefCompare1(int opcode)
Compile start of a conditional: if (!
voidemitIfThen()
voidemitInc(Variable var, short inc)
voidemitInstanceof(Type type)
voidemitInvoke(Method method)
voidemitInvokeInterface(Method method)
voidemitInvokeMethod(Method method, int opcode)
voidemitInvokeSpecial(Method method)
voidemitInvokeStatic(Method method)
Compile a static method call.
voidemitInvokeVirtual(Method method)
Compile a virtual method call.
voidemitIOr()
voidemitJsr(Label label)
voidemitLoad(Variable var)
Compile code to push the contents of a local variable onto the statck.
voidemitMonitorEnter()
voidemitMonitorExit()
voidemitMul()
voidemitNew(ClassType type)
Invoke new on a class type.
voidemitNewArray(Type element_type, int dims)
Compile code to allocate a new array.
voidemitNewArray(Type element_type)
voidemitNot(Type type)
voidemitPop(int nvalues)
Compile code to pop values off the stack (and ignore them).
voidemitPrimop(int opcode, int arg_count, Type retType)
voidemitPushClass(String name)
Push a class constant pool entry.
voidemitPushConstant(int val, Type type)
voidemitPushConstant(CpoolEntry cnst)
voidemitPushDouble(double x)
voidemitPushFloat(float x)
voidemitPushInt(int i)
voidemitPushLong(long i)
voidemitPushNull()
voidemitPushPrimArray(Object value, ArrayType arrayType)
Emit code to push a constant primitive array.
voidemitPushString(String str)
Emit code to push the value of a constant String.
voidemitPushThis()
voidemitPutField(Field field)
Compile code to put a non-static field value.
voidemitPutStatic(Field field)
Compile code to put a static field value.
voidemitRem()
voidemitRet(Variable var)
Emit a 'ret' instruction.
voidemitReturn()
Compile a method return.
voidemitShl()
voidemitShr()
voidemitStore(Variable var)
voidemitSub(char sig)
voidemitSub(PrimType type)
voidemitSub()
voidemitSwap()
voidemitTailCall(boolean pop_args, Scope scope)
Compile a tail-call to position 0 of the current procedure.
voidemitThen()
voidemitThrow()
voidemitTryCatchEnd()
voidemitTryEnd()
voidemitTryStart(boolean has_finally, Type result_type)
voidemitUshr()
voidemitWithCleanupCatch(Variable catchVar)
Called after a body that has a cleanup clause.
voidemitWithCleanupDone()
Called after generating a cleanup handler.
voidemitWithCleanupStart()
Beginning of code that has a cleanup handler.
voidemitXOr()
voidendFragment(int cookie)
End a fragment.
voidenterScope(Scope scope)
voidfixupAdd(int kind, Label label)
Add a fixup at this location.
voidfixupChain(Label here, Label target)
This causes a later processFixup to rearrange the code.
VariablegetArg(int index)
Get the index'th parameter.
AttributegetAttributes()
byte[]getCode()
Get the code (instruction bytes) of this method.
intgetCodeLength()
Set the current lengthof the code (instruction bytes) of this method.
ConstantPoolgetConstants()
ScopegetCurrentScope()
TryStategetCurrentTry()
LabelgetLabel()
Get a new Label for the current location.
intgetLength()
intgetMaxLocals()
Get the maximum number of local variable words in this method.
intgetMaxStack()
Get the maximum number of words on the operand stack in this method.
MethodgetMethod()
intgetPC()
intgetSP()
booleanisInTry()
Variablelookup(String name)
Search by name for a Variable
ScopepopScope()
TypepopType()
voidprint(ClassTypeWriter dst)
voidprocessFixups()
ScopepushScope()
voidpushType(Type type)
voidput1(int i)
Write an 8-bit byte to the current code-stream.
voidput2(int i)
Write a 16-bit short to the current code-stream
voidput4(int i)
Write a 32-bit int to the current code-stream
voidputIndex2(CpoolEntry cnst)
voidputLineNumber(String filename, int linenumber)
voidputLineNumber(int linenumber)
booleanreachableHere()
True if control could reach here.
voidreserve(int bytes)
voidrestoreStackTypeState(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.
voidsetAttributes(Attribute attributes)
voidsetCode(byte[] code)
Set the code (instruction bytes) of this method.
voidsetCodeLength(int len)
Set the length the the code (instruction bytes) of this method.
voidsetMaxLocals(int n)
Set the maximum number of local variable words in this method.
voidsetMaxStack(int n)
Set the maximum number of words on the operand stack in this method.
voidsetReachable(boolean val)
voidsetUnreachable()
TypetopType()
voidwrite(DataOutputStream dstr)

Field Detail

instructionLineMode

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}.

locals

public LocalVarsAttr locals

Constructor Detail

CodeAttr

public CodeAttr(Method meth)

Method Detail

addHandler

public void addHandler(int start_pc, int end_pc, int handler_pc, int catch_type)
Add an exception handler.

addHandler

public void addHandler(Label start_try, Label end_try, ClassType catch_type)
Add an exception handler.

addLocal

public Variable addLocal(Type type)
Add a new local variable (in the current scope).

Parameters: type type of the new Variable.

Returns: the new Variable.

addLocal

public Variable addLocal(Type type, String name)
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.

addParamLocals

public void addParamLocals()
Call addLocal for parameters (as implied by method type).

assignConstants

public void assignConstants(ClassType cl)

beginFragment

public int beginFragment(Label start, Label after)

calculateSplit

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.

castNeeded

public static boolean castNeeded(Type top, Type required)

disAssemble

public void disAssemble(ClassTypeWriter dst, int start, int limit)

doPendingFinalizers

public void doPendingFinalizers(TryState limit)
Call pending finalizer functions.

Parameters: limit Only call finalizers more recent than this.

emitAdd

public final void emitAdd(char sig)

emitAdd

public final void emitAdd(PrimType type)

emitAdd

public final void emitAdd()

Deprecated:

emitAnd

public final void emitAnd()

emitArrayLength

public final void emitArrayLength()

emitArrayLoad

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

emitArrayStore

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 => ...

emitCatchEnd

public void emitCatchEnd()

emitCatchStart

public void emitCatchStart(Variable var)

emitCheckcast

public void emitCheckcast(Type type)

emitConvert

public final void emitConvert(Type from, Type to)

emitDiv

public final void emitDiv()

emitDup

public void emitDup()
Emit code to duplicate the top element of the stack.

emitDup

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]

emitDup

public void emitDup(int size)
Compile code to duplicate the top 1 or 2 words.

Parameters: size number of words to duplicate

emitDup

public void emitDup(Type type)

emitDupX

public void emitDupX()
Emit code to duplicate the top element of the stack and place the copy before the previous element.

emitElse

public final void emitElse()
Compile start of else clause.

emitFi

public final void emitFi()
Compile end of conditional.

emitFinallyEnd

public void emitFinallyEnd()

emitFinallyStart

public void emitFinallyStart()

emitGetField

public final void emitGetField(Field field)
Compile code to get a non-static field value. Stack: ..., objectref => ..., value

emitGetStatic

public final void emitGetStatic(Field field)
Compile code to get a static field value. Stack: ... => ..., value

emitGoto

public final void emitGoto(Label label)
Compile an unconditional branch (goto).

Parameters: label target of the branch (must be in this method).

emitGotoIfCompare1

public final void emitGotoIfCompare1(Label label, int opcode)

emitGotoIfCompare2

public final void emitGotoIfCompare2(Label label, int logop)

emitGotoIfEq

public final void emitGotoIfEq(Label label, boolean invert)

Deprecated:

emitGotoIfEq

public final void emitGotoIfEq(Label label)
Compile a conditional transfer if 2 top stack elements are equal.

emitGotoIfGe

public final void emitGotoIfGe(Label label)

emitGotoIfGt

public final void emitGotoIfGt(Label label)

emitGotoIfIntEqZero

public final void emitGotoIfIntEqZero(Label label)

emitGotoIfIntGeZero

public final void emitGotoIfIntGeZero(Label label)

emitGotoIfIntGtZero

public final void emitGotoIfIntGtZero(Label label)

emitGotoIfIntLeZero

public final void emitGotoIfIntLeZero(Label label)

emitGotoIfIntLtZero

public final void emitGotoIfIntLtZero(Label label)

emitGotoIfIntNeZero

public final void emitGotoIfIntNeZero(Label label)

emitGotoIfLe

public final void emitGotoIfLe(Label label)

emitGotoIfLt

public final void emitGotoIfLt(Label label)

emitGotoIfNE

public final void emitGotoIfNE(Label label)
Compile conditional transfer if 2 top stack elements are not equal.

emitIfCompare1

public final void emitIfCompare1(int opcode)
Compile start of a conditional: if (!(x opcode 0)) .... The value of x must already have been pushed.

emitIfEq

public final void emitIfEq()
Compile start of a conditional: if (x == y) ... The values of x and y must already have been pushed.

emitIfGe

public final void emitIfGe()
Compile start of a conditional: if (x >= y) ... The values of x and y must already have been pushed.

emitIfGt

public final void emitIfGt()
Compile start of a conditional: if (x > y) ... The values of x and y must already have been pushed.

emitIfIntCompare

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.

emitIfIntEqZero

public final void emitIfIntEqZero()
Compile start of conditional: if (x == 0) .... Also use this if you have pushed a boolean value: if (!b) ...

emitIfIntLEqZero

public final void emitIfIntLEqZero()
Compile start of conditional: if (x <= 0).

emitIfIntLt

public final void emitIfIntLt()

emitIfIntNotZero

public final void emitIfIntNotZero()
Compile start of conditional: if (x != 0) .... Also use this if you have pushed a boolean value: if (b) ...

emitIfLe

public final void emitIfLe()
Compile start of a conditional: if (x <= y) ... The values of x and y must already have been pushed.

emitIfLt

public final void emitIfLt()
Compile start of a conditional: if (x < y) ... The values of x and y must already have been pushed.

emitIfNEq

public final void emitIfNEq()
Compile start of a conditional: if (x != y) ... The values of x and y must already have been pushed.

emitIfNotNull

public final void emitIfNotNull()
Compile start of conditional: if (x != null)

emitIfNull

public final void emitIfNull()
Compile start of conditional: if (x == null)

emitIfRefCompare1

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.

emitIfThen

public final void emitIfThen()

emitInc

public void emitInc(Variable var, short inc)

emitInstanceof

public void emitInstanceof(Type type)

emitInvoke

public void emitInvoke(Method method)

emitInvokeInterface

public void emitInvokeInterface(Method method)

emitInvokeMethod

public void emitInvokeMethod(Method method, int opcode)

emitInvokeSpecial

public void emitInvokeSpecial(Method method)

emitInvokeStatic

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

emitInvokeVirtual

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

emitIOr

public final void emitIOr()

emitJsr

public final void emitJsr(Label label)

emitLoad

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.

emitMonitorEnter

public final void emitMonitorEnter()

emitMonitorExit

public final void emitMonitorExit()

emitMul

public final void emitMul()

emitNew

public void emitNew(ClassType type)
Invoke new on a class type. Does not call the constructor!

Parameters: type the desired new object type

emitNewArray

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

emitNewArray

public void emitNewArray(Type element_type)

emitNot

public final void emitNot(Type type)

emitPop

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

emitPrimop

public void emitPrimop(int opcode, int arg_count, Type retType)

emitPushClass

public final void emitPushClass(String name)
Push a class constant pool entry. This is only supported by JDK 1.5 and later.

emitPushConstant

public final void emitPushConstant(int val, Type type)

emitPushConstant

public final void emitPushConstant(CpoolEntry cnst)

emitPushDouble

public void emitPushDouble(double x)

emitPushFloat

public void emitPushFloat(float x)

emitPushInt

public final void emitPushInt(int i)

emitPushLong

public void emitPushLong(long i)

emitPushNull

public void emitPushNull()

emitPushPrimArray

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.

emitPushString

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.

emitPushThis

public final void emitPushThis()

emitPutField

public final void emitPutField(Field field)
Compile code to put a non-static field value. Stack: ..., objectref, value => ...

emitPutStatic

public final void emitPutStatic(Field field)
Compile code to put a static field value. Stack: ..., value => ...

emitRem

public final void emitRem()

emitRet

public void emitRet(Variable var)
Emit a 'ret' instruction.

Parameters: var the variable containing the return address

emitReturn

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.

emitShl

public final void emitShl()

emitShr

public final void emitShr()

emitStore

public void emitStore(Variable var)

emitSub

public final void emitSub(char sig)

emitSub

public final void emitSub(PrimType type)

emitSub

public final void emitSub()

Deprecated:

emitSwap

public void emitSwap()

emitTailCall

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.

emitThen

public final void emitThen()

emitThrow

public final void emitThrow()

emitTryCatchEnd

public void emitTryCatchEnd()

emitTryEnd

public void emitTryEnd()

emitTryStart

public void emitTryStart(boolean has_finally, Type result_type)

emitUshr

public final void emitUshr()

emitWithCleanupCatch

public void emitWithCleanupCatch(Variable catchVar)
Called after a body that has a cleanup clause. Followed by the cleanup code.

emitWithCleanupDone

public void emitWithCleanupDone()
Called after generating a cleanup handler.

emitWithCleanupStart

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.

emitXOr

public final void emitXOr()

endFragment

public void endFragment(int cookie)
End a fragment.

Parameters: cookie the return value from the previous beginFragment.

enterScope

public void enterScope(Scope scope)

fixupAdd

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.

fixupChain

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:.

getArg

public Variable getArg(int index)
Get the index'th parameter.

getAttributes

public final Attribute getAttributes()

getCode

public byte[] getCode()
Get the code (instruction bytes) of this method. Does not make a copy.

getCodeLength

public int getCodeLength()
Set the current lengthof the code (instruction bytes) of this method.

getConstants

public final ConstantPool getConstants()

getCurrentScope

public Scope getCurrentScope()

getCurrentTry

public final TryState getCurrentTry()

getLabel

public Label getLabel()
Get a new Label for the current location. Unlike Label.define, Does not change reachableHere().

getLength

public final int getLength()

getMaxLocals

public int getMaxLocals()
Get the maximum number of local variable words in this method.

getMaxStack

public int getMaxStack()
Get the maximum number of words on the operand stack in this method.

getMethod

public final Method getMethod()

getPC

public final int getPC()

getSP

public final int getSP()

isInTry

public final boolean isInTry()

lookup

public Variable lookup(String name)
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).

popScope

public Scope popScope()

popType

public final Type popType()

print

public void print(ClassTypeWriter dst)

processFixups

public void processFixups()

pushScope

public Scope pushScope()

pushType

public final void pushType(Type type)

put1

public final void put1(int i)
Write an 8-bit byte to the current code-stream.

Parameters: i the byte to write

put2

public final void put2(int i)
Write a 16-bit short to the current code-stream

Parameters: i the value to write

put4

public final void put4(int i)
Write a 32-bit int to the current code-stream

Parameters: i the value to write

putIndex2

public final void putIndex2(CpoolEntry cnst)

putLineNumber

public final void putLineNumber(String filename, int linenumber)

putLineNumber

public final void putLineNumber(int linenumber)

reachableHere

public final boolean reachableHere()
True if control could reach here.

reserve

public final void reserve(int bytes)

restoreStackTypeState

public void restoreStackTypeState(Type[] save)
Restore a type state as saved by saveStackTypeState.

saveStackTypeState

public Type[] saveStackTypeState(boolean clear)
Return an object encapsulating the type state of the JVM stack.

setAttributes

public final void setAttributes(Attribute attributes)

setCode

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).

setCodeLength

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.)

setMaxLocals

public void setMaxLocals(int n)
Set the maximum number of local variable words in this method.

setMaxStack

public void setMaxStack(int n)
Set the maximum number of words on the operand stack in this method.

setReachable

public final void setReachable(boolean val)

setUnreachable

public final void setUnreachable()

topType

public final Type topType()

write

public void write(DataOutputStream dstr)