gnu.bytecode
Class CodeAttr

java.lang.Object
  |
  +--gnu.bytecode.Attribute
        |
        +--gnu.bytecode.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
 LocalVarsAttr locals
           
 
Constructor Summary
CodeAttr(Method meth)
           
 
Method Summary
 void addHandler(int start_pc, int end_pc, int handler_pc, ClassType catch_type, ConstantPool constants)
          Add an exception handler.
 void addHandler(int start_pc, int end_pc, int handler_pc, int catch_type)
          Add an exception handler.
 Variable addLocal(Type type)
          Add a new local variable (in the current scope).
 Variable addLocal(Type type, java.lang.String name)
          Add a new local variable (in the current scope).
 void assignConstants(ClassType cl)
          Add any needed constant pool entries for this Attribute.
 void beginFragment(boolean isHandler)
           
 void disAssemble(ClassTypeWriter dst, int offset, int length)
           
 void emitAdd()
          Deprecated.  
 void emitAdd(char sig)
           
 void emitAdd(PrimType type)
           
 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(int size)
          Compile code to duplicate the top 1 or 2 words.
 void emitDup(int size, int offset)
          Compile code to duplicate with offset.
 void emitDup(Type type)
           
 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 emitGoto(Label label, int opcode)
          Compile an unconditional branch (goto) or a jsr.
 void emitGotoIfCompare1(Label label, int opcode)
           
 void emitGotoIfCompare2(Label label, int logop)
           
 void emitGotoIfEq(Label label)
          Compile a conditional transfer if 2 top stack elements are equal.
 void emitGotoIfEq(Label label, boolean invert)
           
 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 (!(x OPCODE 0)) ...
 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 (!(x OPCODE y)) ...
 void emitIfIntLEqZero()
          Compile start of conditional: if (x <= 0)
 void emitIfIntLt()
           
 void emitIfIntNotZero()
          Compile start of conditional: if (x != 0)
 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 != y) ...
 void emitIfNotNull()
          Compile start of conditional: if (x != null)
 void emitIfNull()
          Compile start of conditional: if (x == null)
 void emitIfRefCompare1(int opcode)
          Compile start of a conditional: if (!(x OPCODE null)) ...
 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 emitLoad(Variable var)
          Comple 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)
           
 void emitNewArray(Type element_type, int dims)
          Compile code to allocate a new array.
 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 emitPushConstant(CpoolEntry cnst)
           
 void emitPushConstant(int val, Type type)
           
 void emitPushDouble(double x)
           
 void emitPushFloat(float x)
           
 void emitPushInt(int i)
           
 void emitPushLong(long i)
           
 void emitPushNull()
           
 void emitPushString(java.lang.String str)
           
 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()
          Deprecated.  
 void emitSub(char sig)
           
 void emitSub(PrimType type)
           
 void emitSwap()
           
 void emitTailCall(boolean pop_args, Scope scope)
          Compile a tail-call to position 0 of the current procewure.
 void emitThrow()
           
 void emitTryCatchEnd()
           
 void emitTryEnd()
           
 void emitTryStart(boolean has_finally, Type result_type)
           
 void emitUshr()
           
 void emitXOr()
           
 void endFragment()
           
 void enterScope(Scope scope)
           
 void finalize_labels()
           
 Variable getArg(int index)
          Get the index'th parameter.
 Attribute getAttributes()
          Get the (first) Attribute of this container.
 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()
           
 int getLength()
          Return the length of the attribute in bytes.
 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()
           
 boolean isInTry()
           
 Variable lookup(java.lang.String name)
          Search by name for a Variable
 Scope popScope()
           
 Type popType()
           
 void print(ClassTypeWriter dst)
           
 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(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)
          Set the (list of) Attributes of this container.
 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(java.io.DataOutputStream dstr)
          Write out the contents of the Attribute.
 
Methods inherited from class gnu.bytecode.Attribute
addToFrontOf, assignConstants, count, get, getContainer, getLengthAll, getName, getNameIndex, getNext, isSkipped, setContainer, setName, setNameIndex, setNext, setSkipped, setSkipped, writeAll
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

locals

public LocalVarsAttr locals
Constructor Detail

CodeAttr

public CodeAttr(Method meth)
Method Detail

getAttributes

public final Attribute getAttributes()
Description copied from interface: AttrContainer
Get the (first) Attribute of this container.
Specified by:
getAttributes in interface AttrContainer

setAttributes

public final void setAttributes(Attribute attributes)
Description copied from interface: AttrContainer
Set the (list of) Attributes of this container.
Specified by:
setAttributes in interface AttrContainer

getMethod

public final Method getMethod()

getPC

public final int getPC()

getConstants

public final ConstantPool getConstants()

reachableHere

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

setReachable

public final void setReachable(boolean val)

setUnreachable

public final void setUnreachable()

getMaxStack

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

getMaxLocals

public int getMaxLocals()
Get 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.

setMaxLocals

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

getCode

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

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

getCodeLength

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

reserve

public final void reserve(int bytes)

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(int linenumber)

pushType

public final void pushType(Type type)

popType

public final Type popType()

topType

public final Type topType()

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

emitSwap

public void emitSwap()

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)

enterScope

public void enterScope(Scope scope)

pushScope

public Scope pushScope()

popScope

public Scope popScope()

getArg

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

lookup

public Variable lookup(java.lang.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).

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,
                         java.lang.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.

emitPushConstant

public final void emitPushConstant(int val,
                                   Type type)

emitPushConstant

public final void emitPushConstant(CpoolEntry cnst)

emitPushInt

public final void emitPushInt(int i)

emitPushLong

public void emitPushLong(long i)

emitPushFloat

public void emitPushFloat(float x)

emitPushDouble

public void emitPushDouble(double x)

emitPushString

public final void emitPushString(java.lang.String str)

emitPushNull

public void emitPushNull()

emitPushThis

public final void emitPushThis()

emitArrayLength

public final void emitArrayLength()

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

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

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:
type - type of the array elements

emitNewArray

public void emitNewArray(Type element_type)

emitAdd

public final void emitAdd(char sig)

emitAdd

public final void emitAdd(PrimType type)

emitAdd

public final void emitAdd()
Deprecated.  


emitSub

public final void emitSub(char sig)

emitSub

public final void emitSub(PrimType type)

emitSub

public final void emitSub()
Deprecated.  


emitMul

public final void emitMul()

emitDiv

public final void emitDiv()

emitRem

public final void emitRem()

emitShl

public final void emitShl()

emitShr

public final void emitShr()

emitUshr

public final void emitUshr()

emitAnd

public final void emitAnd()

emitIOr

public final void emitIOr()

emitXOr

public final void emitXOr()

emitNot

public final void emitNot(Type type)

emitPrimop

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

emitLoad

public final void emitLoad(Variable var)
Comple code to push the contents of a local variable onto the statck.
Parameters:
var - The variable whose contents we want to push.

emitStore

public void emitStore(Variable var)

emitInc

public void emitInc(Variable var,
                    short inc)

emitGetStatic

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

emitGetField

public final void emitGetField(Field field)
Compile code to get 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 => ...

emitPutField

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

emitInvokeMethod

public void emitInvokeMethod(Method method,
                             int opcode)

emitInvoke

public void emitInvoke(Method method)

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

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

emitInvokeInterface

public void emitInvokeInterface(Method method)

emitGoto

public final void emitGoto(Label label,
                           int opcode)
Compile an unconditional branch (goto) or a jsr.
Parameters:
label - target of the branch (must be in this method).

emitGoto

public final void emitGoto(Label label)
Compile an unconditional branch (goto).
Parameters:
label - target of the branch (must be in this method).

emitGotoIfEq

public final void emitGotoIfEq(Label label,
                               boolean invert)

emitGotoIfEq

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

emitGotoIfNE

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

emitGotoIfCompare1

public final void emitGotoIfCompare1(Label label,
                                     int opcode)

emitGotoIfIntEqZero

public final void emitGotoIfIntEqZero(Label label)

emitGotoIfIntNeZero

public final void emitGotoIfIntNeZero(Label label)

emitGotoIfIntLtZero

public final void emitGotoIfIntLtZero(Label label)

emitGotoIfIntGeZero

public final void emitGotoIfIntGeZero(Label label)

emitGotoIfIntGtZero

public final void emitGotoIfIntGtZero(Label label)

emitGotoIfIntLeZero

public final void emitGotoIfIntLeZero(Label label)

emitGotoIfCompare2

public final void emitGotoIfCompare2(Label label,
                                     int logop)

emitGotoIfLt

public final void emitGotoIfLt(Label label)

emitGotoIfGe

public final void emitGotoIfGe(Label label)

emitGotoIfGt

public final void emitGotoIfGt(Label label)

emitGotoIfLe

public final void emitGotoIfLe(Label label)

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.

emitIfIntNotZero

public final void emitIfIntNotZero()
Compile start of conditional: if (x != 0)

emitIfIntLEqZero

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

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.

emitIfNotNull

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

emitIfNull

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

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.

emitIfIntLt

public final void emitIfIntLt()

emitIfNEq

public final void emitIfNEq()
Compile start of a conditional: if (x != y) ... The values of x and y 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.

emitIfLt

public final void emitIfLt()
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.

emitIfLe

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

emitRet

public void emitRet(Variable var)
Emit a 'ret' instruction.
Parameters:
var - the variable containing the return address

emitIfThen

public final void emitIfThen()

emitElse

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

emitFi

public final void emitFi()
Compile end of conditional.

emitConvert

public final void emitConvert(Type from,
                              Type to)

emitCheckcast

public void emitCheckcast(Type type)

emitInstanceof

public void emitInstanceof(Type type)

emitThrow

public final void emitThrow()

emitMonitorEnter

public final void emitMonitorEnter()

emitMonitorExit

public final void emitMonitorExit()

emitReturn

public final void emitReturn()
Compile a method return.

addHandler

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

addHandler

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

emitTryStart

public void emitTryStart(boolean has_finally,
                         Type result_type)

emitTryEnd

public void emitTryEnd()

emitCatchStart

public void emitCatchStart(Variable var)

emitCatchEnd

public void emitCatchEnd()

emitFinallyStart

public void emitFinallyStart()

emitFinallyEnd

public void emitFinallyEnd()

emitTryCatchEnd

public void emitTryCatchEnd()

isInTry

public final boolean isInTry()

emitTailCall

public void emitTailCall(boolean pop_args,
                         Scope scope)
Compile a tail-call to position 0 of the current procewure.
Parameters:
pop_args - if true, copy argument registers (except this) from stack.
scope - Scope whose start we jump back to.

finalize_labels

public void finalize_labels()

assignConstants

public void assignConstants(ClassType cl)
Description copied from class: Attribute
Add any needed constant pool entries for this Attribute. Overridden by sub-classes. Do any other cleanup needed before writing out a .class file.
Overrides:
assignConstants in class Attribute

getLength

public final int getLength()
Description copied from class: Attribute
Return the length of the attribute in bytes. Does not include the 6-byte header (for the name_index and the length).
Overrides:
getLength in class Attribute

write

public void write(java.io.DataOutputStream dstr)
           throws java.io.IOException
Description copied from class: Attribute
Write out the contents of the Attribute. Does not write the 6-byte attribute header.
Overrides:
write in class Attribute

print

public void print(ClassTypeWriter dst)
Overrides:
print in class Attribute

disAssemble

public void disAssemble(ClassTypeWriter dst,
                        int offset,
                        int length)

saveStackTypeState

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

restoreStackTypeState

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

beginFragment

public void beginFragment(boolean isHandler)

endFragment

public void endFragment()