com.sun.tools.javac.jvm
Class Code

java.lang.Object
  extended by com.sun.tools.javac.jvm.Code

public class Code
extends java.lang.Object

An internal structure that corresponds to the code attribute of methods in a classfile. The class also provides some utility operations to generate bytecode instructions.

This is NOT part of any API supported by Sun Microsystems. If you write code that depends on this, you do so at your own risk. This code and its internal interfaces are subject to change or deletion without notice.


Nested Class Summary
static class Code.Chain
          A chain represents a list of unresolved jumps.
(package private) static class Code.LocalVar
          A live range of a local variable.
private static class Code.Mneumonics
           
static class Code.StackMapFormat
           
(package private) static class Code.StackMapFrame
          An entry in the stack map.
(package private)  class Code.State
           
 
Field Summary
private  boolean alive
          Code generation enabled?
(package private)  ListBuffer<char[]> catchInfo
          A buffer for expression catch data.
 byte[] code
          The code buffer.
 int cp
          the current code pointer.
 CRTable crt
          The CharacterRangeTable
 boolean debugCode
           
 boolean fatcode
          Are we generating code with jumps >= 32K?
private  boolean fixedPc
          Is it forbidden to compactify code, because something is pointing to current location?
(package private)  Code.StackMapFrame frameBeforeLast
          The stack map frame before the last one.
(package private) static Type jsrReturnValue
           
(package private)  Code.StackMapFrame lastFrame
          The last stack map frame in StackMapTable.
(package private)  int lastStackMapPC
          The last PC at which we generated a stack map.
(package private)  boolean lineDebugInfo
          Switch: emit line number info.
(package private)  List<char[]> lineInfo
          A buffer for line number information.
(package private)  Position.LineMap lineMap
          Emit line number info if map supplied
(package private)  Code.LocalVar[] lvar
          Local variables, indexed by register.
 int max_locals
          The maximum number of local variable slots.
 int max_stack
          The maximum stack size.
(package private)  Symbol.MethodSymbol meth
           
 boolean needStackMap
           
 int nextreg
          The next available register.
(package private)  Code.Chain pendingJumps
          A chain for jumps to be resolved before the next opcode is emitted.
(package private)  boolean pendingStackMap
          Set true when a stackMap is needed at the current PC.
(package private)  int pendingStatPos
          The position of the currently statement, if we are at the start of this statement, NOPOS otherwise.
(package private)  Pool pool
          The constant pool of the current class.
(package private)  Code.StackMapFormat stackMap
          The stack map format to be generated.
(package private)  Code.StackMapFrame[] stackMapBuffer
          A buffer of cldc stack map entries.
(package private)  int stackMapBufferSize
           
(package private)  ClassWriter.StackMapTableFrame[] stackMapTableBuffer
          A buffer of compressed StackMapTable entries.
(package private)  Code.State state
          The current machine state (registers and stack).
(package private)  Symtab syms
           
(package private)  Types types
           
(package private)  Code.LocalVar[] varBuffer
          Previously live local variables, to be put into the variable table.
(package private)  int varBufferSize
           
(package private)  boolean varDebugInfo
          Switch: emit variable debug info.
 
Constructor Summary
Code(Symbol.MethodSymbol meth, boolean fatcode, Position.LineMap lineMap, boolean varDebugInfo, Code.StackMapFormat stackMap, boolean debugCode, CRTable crt, Symtab syms, Types types, Pool pool)
          Construct a code object, given the settings of the fatcode, debugging info switches and the CharacterRangeTable.
 
Method Summary
 void addCatch(char startPc, char endPc, char handlerPc, char catchType)
          Add a catch clause to code.
 void addLineNumber(char startPc, char lineNumber)
          Add a line number entry.
private  void addLocalVar(Symbol.VarSymbol v)
          Add a new local variable.
 void align(int incr)
          Align code pointer to next `incr' boundary.
static int arraycode(Type type)
          Given a type, return its code for allocating arrays of that type.
 Code.Chain branch(int opcode)
          Emit a branch with given opcode; return its chain.
 boolean checkLimits(JCDiagnostic.DiagnosticPosition pos, Log log)
          Check the code against VM spec limits; if problems report them and return true.
 int curPc()
          The current output code pointer.
private  void emit1(int od)
          Emit a byte of code.
private  void emit2(int od)
          Emit two bytes of code.
 void emit4(int od)
          Emit four bytes of code.
 void emitAnewarray(int od, Type arrayType)
          Emit anewarray.
(package private)  void emitCLDCStackMap(int pc, int localsSize)
          Emit a CLDC stack map frame.
 void emitInvokeinterface(int meth, Type mtype)
          Emit an invokeinterface instruction.
 void emitInvokespecial(int meth, Type mtype)
          Emit an invokespecial instruction.
 void emitInvokestatic(int meth, Type mtype)
          Emit an invokestatic instruction.
 void emitInvokevirtual(int meth, Type mtype)
          Emit an invokevirtual instruction.
 int emitJump(int opcode)
          Emit a jump instruction.
 void emitMultianewarray(int ndims, int type, Type arrayType)
          Emit a multinewarray instruction.
 void emitNewarray(int elemcode, Type arrayType)
          Emit newarray.
private  void emitop(int op)
          Emit an opcode.
 void emitop0(int op)
          Emit an opcode with no operand field.
 void emitop1(int op, int od)
          Emit an opcode with a one-byte operand field.
 void emitop1w(int op, int od)
          Emit an opcode with a one-byte operand field; widen if field does not fit in a byte.
 void emitop1w(int op, int od1, int od2)
          Emit an opcode with two one-byte operand fields; widen if either field does not fit in a byte.
 void emitop2(int op, int od)
          Emit an opcode with a two-byte operand field.
 void emitop4(int op, int od)
          Emit an opcode with a four-byte operand field.
 void emitStackMap()
          Emit a stack map entry.
(package private)  void emitStackMapFrame(int pc, int localsSize)
           
private  void endScope(int adr)
          End the scope of a variable.
 void endScopes(int first)
          End scopes of all variables with registers >= first.
 int entryPoint()
          Declare an entry point; return current code pointer
 int entryPoint(Code.State state)
          Declare an entry point with initial state; return current code pointer
 int entryPoint(Code.State state, Type pushed)
          Declare an entry point with initial state plus a pushed value; return current code pointer
private  int get1(int pc)
          Return code byte at position pc as an unsigned int.
private  int get2(int pc)
          Return two code bytes at position pc as an unsigned int.
 int get4(int pc)
          Return four code bytes at position pc as an int.
(package private)  Code.StackMapFrame getInitialFrame()
           
private  int getLocalsSize()
           
 boolean isAlive()
          Is code generation currently enabled?
 void markDead()
          Switch code generation on/off.
 void markStatBegin()
          Force stat begin eagerly
static Code.Chain mergeChains(Code.Chain chain1, Code.Chain chain2)
          Merge the jumps in of two chains into one.
static java.lang.String mnem(int opcode)
          static tables
static int negate(int opcode)
          Negate a branch opcode.
private  int newLocal(int typecode)
          Create a new local variable address and return it.
 int newLocal(Symbol.VarSymbol v)
           
private  int newLocal(Type type)
           
 void newRegSegment()
          Start a set of fresh registers.
(package private)  void postop()
           
private  void put1(int pc, int op)
          Place a byte into code at address pc.
private  void put2(int pc, int od)
          Place two bytes into code at address pc.
 void put4(int pc, int od)
          Place four bytes into code at address pc.
(package private)  void putVar(Code.LocalVar var)
          Put a live variable range into the buffer to be output to the class file.
 void resolve(Code.Chain chain)
          Resolve chain to point to current code pointer.
 void resolve(Code.Chain chain, int target)
          Resolve chain to point to given target.
 void resolvePending()
          Resolve any pending jumps.
 void setDefined(Bits newDefined)
          Set the current variable defined state.
 void setDefined(int adr)
          Mark a register as being (possibly) defined.
 void setUndefined(int adr)
          Mark a register as being undefined.
 void statBegin(int pos)
          Mark beginning of statement.
static int truncate(int tc)
          Collapse type code for subtypes of int to INTcode.
static int typecode(Type type)
          Given a type, return its type code (used implicitly in the JVM architecture).
private  Type typeForPool(java.lang.Object o)
          The type of a constant pool entry.
static int width(int typecode)
          The width in bytes of objects of the type.
static int width(List<Type> types)
          The total width taken up by a vector of objects.
static int width(Type type)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

debugCode

public final boolean debugCode

needStackMap

public final boolean needStackMap

types

final Types types

syms

final Symtab syms

max_stack

public int max_stack
The maximum stack size.


max_locals

public int max_locals
The maximum number of local variable slots.


code

public byte[] code
The code buffer.


cp

public int cp
the current code pointer.


catchInfo

ListBuffer<char[]> catchInfo
A buffer for expression catch data. Each enter is a vector of four unsigned shorts.


lineInfo

List<char[]> lineInfo
A buffer for line number information. Each entry is a vector of two unsigned shorts.


crt

public CRTable crt
The CharacterRangeTable


fatcode

public boolean fatcode
Are we generating code with jumps >= 32K?


alive

private boolean alive
Code generation enabled?


state

Code.State state
The current machine state (registers and stack).


fixedPc

private boolean fixedPc
Is it forbidden to compactify code, because something is pointing to current location?


nextreg

public int nextreg
The next available register.


pendingJumps

Code.Chain pendingJumps
A chain for jumps to be resolved before the next opcode is emitted. We do this lazily to avoid jumps to jumps.


pendingStatPos

int pendingStatPos
The position of the currently statement, if we are at the start of this statement, NOPOS otherwise. We need this to emit line numbers lazily, which we need to do because of jump-to-jump optimization.


pendingStackMap

boolean pendingStackMap
Set true when a stackMap is needed at the current PC.


stackMap

Code.StackMapFormat stackMap
The stack map format to be generated.


varDebugInfo

boolean varDebugInfo
Switch: emit variable debug info.


lineDebugInfo

boolean lineDebugInfo
Switch: emit line number info.


lineMap

Position.LineMap lineMap
Emit line number info if map supplied


pool

final Pool pool
The constant pool of the current class.


meth

final Symbol.MethodSymbol meth

stackMapBuffer

Code.StackMapFrame[] stackMapBuffer
A buffer of cldc stack map entries.


stackMapTableBuffer

ClassWriter.StackMapTableFrame[] stackMapTableBuffer
A buffer of compressed StackMapTable entries.


stackMapBufferSize

int stackMapBufferSize

lastStackMapPC

int lastStackMapPC
The last PC at which we generated a stack map.


lastFrame

Code.StackMapFrame lastFrame
The last stack map frame in StackMapTable.


frameBeforeLast

Code.StackMapFrame frameBeforeLast
The stack map frame before the last one.


jsrReturnValue

static Type jsrReturnValue

lvar

Code.LocalVar[] lvar
Local variables, indexed by register.


varBuffer

Code.LocalVar[] varBuffer
Previously live local variables, to be put into the variable table.


varBufferSize

int varBufferSize
Constructor Detail

Code

public Code(Symbol.MethodSymbol meth,
            boolean fatcode,
            Position.LineMap lineMap,
            boolean varDebugInfo,
            Code.StackMapFormat stackMap,
            boolean debugCode,
            CRTable crt,
            Symtab syms,
            Types types,
            Pool pool)
Construct a code object, given the settings of the fatcode, debugging info switches and the CharacterRangeTable.

Method Detail

checkLimits

public boolean checkLimits(JCDiagnostic.DiagnosticPosition pos,
                           Log log)
Check the code against VM spec limits; if problems report them and return true.


typecode

public static int typecode(Type type)
Given a type, return its type code (used implicitly in the JVM architecture).


truncate

public static int truncate(int tc)
Collapse type code for subtypes of int to INTcode.


width

public static int width(int typecode)
The width in bytes of objects of the type.


width

public static int width(Type type)

width

public static int width(List<Type> types)
The total width taken up by a vector of objects.


arraycode

public static int arraycode(Type type)
Given a type, return its code for allocating arrays of that type.


curPc

public int curPc()
The current output code pointer.


emit1

private void emit1(int od)
Emit a byte of code.


emit2

private void emit2(int od)
Emit two bytes of code.


emit4

public void emit4(int od)
Emit four bytes of code.


emitop

private void emitop(int op)
Emit an opcode.


postop

void postop()

emitMultianewarray

public void emitMultianewarray(int ndims,
                               int type,
                               Type arrayType)
Emit a multinewarray instruction.


emitNewarray

public void emitNewarray(int elemcode,
                         Type arrayType)
Emit newarray.


emitAnewarray

public void emitAnewarray(int od,
                          Type arrayType)
Emit anewarray.


emitInvokeinterface

public void emitInvokeinterface(int meth,
                                Type mtype)
Emit an invokeinterface instruction.


emitInvokespecial

public void emitInvokespecial(int meth,
                              Type mtype)
Emit an invokespecial instruction.


emitInvokestatic

public void emitInvokestatic(int meth,
                             Type mtype)
Emit an invokestatic instruction.


emitInvokevirtual

public void emitInvokevirtual(int meth,
                              Type mtype)
Emit an invokevirtual instruction.


emitop0

public void emitop0(int op)
Emit an opcode with no operand field.


emitop1

public void emitop1(int op,
                    int od)
Emit an opcode with a one-byte operand field.


typeForPool

private Type typeForPool(java.lang.Object o)
The type of a constant pool entry.


emitop1w

public void emitop1w(int op,
                     int od)
Emit an opcode with a one-byte operand field; widen if field does not fit in a byte.


emitop1w

public void emitop1w(int op,
                     int od1,
                     int od2)
Emit an opcode with two one-byte operand fields; widen if either field does not fit in a byte.


emitop2

public void emitop2(int op,
                    int od)
Emit an opcode with a two-byte operand field.


emitop4

public void emitop4(int op,
                    int od)
Emit an opcode with a four-byte operand field.


align

public void align(int incr)
Align code pointer to next `incr' boundary.


put1

private void put1(int pc,
                  int op)
Place a byte into code at address pc. Pre: pc + 1 <= cp.


put2

private void put2(int pc,
                  int od)
Place two bytes into code at address pc. Pre: pc + 2 <= cp.


put4

public void put4(int pc,
                 int od)
Place four bytes into code at address pc. Pre: pc + 4 <= cp.


get1

private int get1(int pc)
Return code byte at position pc as an unsigned int.


get2

private int get2(int pc)
Return two code bytes at position pc as an unsigned int.


get4

public int get4(int pc)
Return four code bytes at position pc as an int.


isAlive

public boolean isAlive()
Is code generation currently enabled?


markDead

public void markDead()
Switch code generation on/off.


entryPoint

public int entryPoint()
Declare an entry point; return current code pointer


entryPoint

public int entryPoint(Code.State state)
Declare an entry point with initial state; return current code pointer


entryPoint

public int entryPoint(Code.State state,
                      Type pushed)
Declare an entry point with initial state plus a pushed value; return current code pointer


emitStackMap

public void emitStackMap()
Emit a stack map entry.


getLocalsSize

private int getLocalsSize()

emitCLDCStackMap

void emitCLDCStackMap(int pc,
                      int localsSize)
Emit a CLDC stack map frame.


emitStackMapFrame

void emitStackMapFrame(int pc,
                       int localsSize)

getInitialFrame

Code.StackMapFrame getInitialFrame()

negate

public static int negate(int opcode)
Negate a branch opcode.


emitJump

public int emitJump(int opcode)
Emit a jump instruction. Return code pointer of instruction to be patched.


branch

public Code.Chain branch(int opcode)
Emit a branch with given opcode; return its chain. branch differs from jump in that jsr is treated as no-op.


resolve

public void resolve(Code.Chain chain,
                    int target)
Resolve chain to point to given target.


resolve

public void resolve(Code.Chain chain)
Resolve chain to point to current code pointer.


resolvePending

public void resolvePending()
Resolve any pending jumps.


mergeChains

public static Code.Chain mergeChains(Code.Chain chain1,
                                     Code.Chain chain2)
Merge the jumps in of two chains into one.


addCatch

public void addCatch(char startPc,
                     char endPc,
                     char handlerPc,
                     char catchType)
Add a catch clause to code.


addLineNumber

public void addLineNumber(char startPc,
                          char lineNumber)
Add a line number entry.


statBegin

public void statBegin(int pos)
Mark beginning of statement.


markStatBegin

public void markStatBegin()
Force stat begin eagerly


addLocalVar

private void addLocalVar(Symbol.VarSymbol v)
Add a new local variable.


setDefined

public void setDefined(Bits newDefined)
Set the current variable defined state.


setDefined

public void setDefined(int adr)
Mark a register as being (possibly) defined.


setUndefined

public void setUndefined(int adr)
Mark a register as being undefined.


endScope

private void endScope(int adr)
End the scope of a variable.


putVar

void putVar(Code.LocalVar var)
Put a live variable range into the buffer to be output to the class file.


newLocal

private int newLocal(int typecode)
Create a new local variable address and return it.


newLocal

private int newLocal(Type type)

newLocal

public int newLocal(Symbol.VarSymbol v)

newRegSegment

public void newRegSegment()
Start a set of fresh registers.


endScopes

public void endScopes(int first)
End scopes of all variables with registers >= first.


mnem

public static java.lang.String mnem(int opcode)
static tables