Class CodeContext
- java.lang.Object
-
- org.codehaus.janino.CodeContext
-
public class CodeContext extends java.lang.ObjectThe context of the compilation of a function (constructor or method). Manages generation of byte code, the exception table, generation of line number tables, allocation of local variables, determining of stack size and local variable table size and flow analysis.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classCodeContext.BasicBlockThisCodeContext.Offsetmarks the first byte of a "basic block" in the sense of JLS 17 "4.10.1 Verification by Type Checking": The intent is that a stack map frame must appear at the beginning of each basic block in a method.private classCodeContext.Branchprivate static classCodeContext.ExceptionTableEntryRepresentation of an entry in the "exception_table" of a "Code" attribute (see JVMS 4.7.3).static interfaceCodeContext.FixUpA throw-in interface that marksCodeContext.Offsets as "fix-ups": During the execution offixUp(), all "fix-ups" are invoked and can do last touches to the code attribute.private classCodeContext.FourByteOffsetclassCodeContext.InserterA class that implements an insertion point into a "Code" attribute.classCodeContext.LineNumberOffsetAnCodeContext.Offsetwho's sole purpose is to later create a 'LineNumberTable' attribute.(package private) static classCodeContext.LocalScopeclassCodeContext.OffsetA class that represents an offset within a "Code" attribute.private classCodeContext.OffsetBranchprivate classCodeContext.Relocatable
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<Java.LocalVariableSlot>allLocalVarsAll the local variables that are allocated in any block in thisCodeContext.private CodeContext.Offsetbeginningprivate static java.util.Map<java.lang.Integer,java.lang.Integer>BRANCH_OPCODE_INVERSIONprivate ClassFileclassFileprivate byte[]codeprivate CodeContext.InsertercurrentInserterprivate CodeContext.LocalScopecurrentLocalScopeEach List of Java.LocalVariableSlot is the local variables allocated for a block.private CodeContext.Inserterendprivate java.util.List<CodeContext.ExceptionTableEntry>exceptionTableEntriesprivate static intINITIAL_SIZEprivate shortmaxLocalsprivate intmaxStackprivate shortnextLocalVariableSlotprivate java.util.List<CodeContext.Relocatable>relocatablesprivate static booleanSUPPRESS_STACK_MAP_TABLE
-
Constructor Summary
Constructors Constructor Description CodeContext(ClassFile classFile)Creates an empty "Code" attribute.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddExceptionTableEntry(CodeContext.Offset startPc, CodeContext.Offset endPc, CodeContext.Offset handlerPc, java.lang.String catchTypeFd)Adds another entry to the "exception_table" of this code attribute (see JVMS 4.7.3).voidaddLineNumberOffset(int lineNumber)shortallocateLocalVariable(short size)Allocates space for a local variable of the given size (1 or 2) on the local variable array.Java.LocalVariableSlotallocateLocalVariable(short size, java.lang.String name, IType type)Allocates space for a local variable of the given size (1 or 2) on the local variable array.private static java.util.Map<java.lang.Integer,java.lang.Integer>createBranchOpcodeInversion()CodeContext.InsertercurrentInserter()private voidfixUp()Fixes up all offsets.voidfixUpAndRelocate()Fixes up all of the offsets and relocate() all relocatables.java.util.List<Java.LocalVariableSlot>getAllLocalVars()ClassFilegetClassFile()TheClassFilethis context is related to.private static intinvertBranchOpcode(int branchOpcode)E.g.intmakeSpace(int size)Inserts size NUL bytes at the current inserter's offset, advances the current inserter's offset by size, createsCodeContext.LineNumberOffsets as necessary, and returns the current inserter's original offset (the offset of the first NUL byte that was inserted).private voidmaybeGrow()Grow the code if relocatables are required to.private static StackMapmergeStackMaps(StackMap sm1, StackMap sm2)CodeContext.OffsetnewBasicBlock()ClassFile.CodeAttributenewCodeAttribute(int initialLocalsCount, boolean debugLines, boolean debugVars)CodeContext.InserternewInserter()Allocates anCodeContext.Inserter, set it to the current offset, and inserts it before the current offset.private ClassFile.LineNumberTableAttributenewLineNumberTableAttribute()protected ClassFile.LocalVariableTableAttributenewLocalVariableTableAttribute()CodeContext.OffsetnewOffset()Creates and inserts anCodeContext.Offsetat the current inserter's current position.private ClassFile.StackMapTableAttributenewStackMapTableAttribute(int initialLocalsCount)booleanpeekNullOperand()booleanpeekObjectOperand()ClassFile.StackMapTableAttribute.VerificationTypeInfopeekOperand()voidpopInserter()Replaces the currentCodeContext.Inserterwith the remembered one (seepushInserter(CodeContext.Inserter)).voidpopIntOperand()Asserts that the top operand is aninteger_variable_infoand pops it.ClassFile.StackMapTableAttribute.VerificationTypeInfopopIntOrLongOperand()Asserts that the top operand is anint_variable_infoorlong_variable_info, then pops and returns it.voidpopLongOperand()Asserts that the top operand is along_variable_infoand pops it.voidpopNullOperand()java.lang.StringpopObjectOperand()Asserts that the top operand is anobject_variable_info, and pops it.ClassFile.StackMapTableAttribute.VerificationTypeInfopopObjectOrUninitializedOrUninitializedThisOperand()Asserts that the top operand is anobject_variable_info,uninitialized_variable_infooruninitializedThis_variable_info, and pops it.ClassFile.StackMapTableAttribute.VerificationTypeInfopopOperand()Pops one entry from the current inserter's operand stack.voidpopOperand(java.lang.String expectedFd)Pops the top operand, asserts that it is aninteger_variable_info,long_variable_info,float_variable_info,double_variable_infoorvariable_object_info, and asserts that it matches the given field descriptor.voidpopOperand(ClassFile.StackMapTableAttribute.VerificationTypeInfo expected)Pops the top entry from the operand stack and assert that it equals expected.voidpopOperandAssignableTo(java.lang.String declaredFd)voidpopReferenceOperand()Asserts that the top operand is anobject_variable_infoor anull_variable_infoand pops it.voidpopUninitializedThisOperand()Asserts that the top operand is anuninitializedThis_variable_infoand pops it.voidpopUninitializedVariableOperand()Asserts that the top operand is anuninitialized_variable_infoand pops it.voidpushDoubleOperand()voidpushFloatOperand()voidpushInserter(CodeContext.Inserter ins)Remembers the currentCodeContext.Inserter, then replaces it with the new one.voidpushIntOperand()voidpushLongOperand()voidpushNullOperand()voidpushObjectOperand(java.lang.String fieldDescriptor)voidpushOperand(java.lang.String fieldDescriptor)Pushes oneobject_variable_info,integer_variable_info,double_variable_info,float_variable_infoorlong_variable_infoentry onto the current inserter's operand stack.voidpushOperand(ClassFile.StackMapTableAttribute.VerificationTypeInfo topOperand)voidpushTopOperand()voidpushUninitializedOperand()voidpushUninitializedThisOperand()private static IClassrawTypeOf(IType iType)private voidrelocate()Relocates all relocatables.voidremoveCode(CodeContext.Offset from, CodeContext.Offset to)Removes all code between from and to.voidrestoreLocalVariables()Restores the previous size of the local variables array.java.util.List<Java.LocalVariableSlot>saveLocalVariables()Remembers the current size of the local variables array.java.lang.StringtoString()voidwrite(byte b1)Inserts a byte at the current insertion position.voidwrite(byte[] b)Inserts a sequence of bytes at the current insertion position.voidwrite(byte b1, byte b2)Inserts bytes at the current insertion position.voidwrite(byte b1, byte b2, byte b3)Inserts bytes at the current insertion position.voidwrite(byte b1, byte b2, byte b3, byte b4)Inserts bytes at the current insertion position.voidwrite(byte b1, byte b2, byte b3, byte b4, byte b5)Inserts bytes at the current insertion position.voidwriteBranch(int opcode, CodeContext.Offset dst)Generates a "branch" instruction.voidwriteOffset(CodeContext.Offset src, CodeContext.Offset dst)Writes a four-byte offset (as it is used in TABLESWITCH and LOOKUPSWITCH) into this code context.voidwriteShort(int v)
-
-
-
Field Detail
-
SUPPRESS_STACK_MAP_TABLE
private static final boolean SUPPRESS_STACK_MAP_TABLE
-
INITIAL_SIZE
private static final int INITIAL_SIZE
- See Also:
- Constant Field Values
-
classFile
private final ClassFile classFile
-
maxStack
private int maxStack
-
maxLocals
private short maxLocals
-
code
private byte[] code
-
beginning
private final CodeContext.Offset beginning
-
end
private final CodeContext.Inserter end
-
currentInserter
private CodeContext.Inserter currentInserter
-
exceptionTableEntries
private final java.util.List<CodeContext.ExceptionTableEntry> exceptionTableEntries
-
allLocalVars
private final java.util.List<Java.LocalVariableSlot> allLocalVars
All the local variables that are allocated in any block in thisCodeContext.
-
currentLocalScope
@Nullable private CodeContext.LocalScope currentLocalScope
Each List of Java.LocalVariableSlot is the local variables allocated for a block. They are pushed and popped onto the list together to make allocation of the next local variable slot easy.
-
nextLocalVariableSlot
private short nextLocalVariableSlot
-
relocatables
private final java.util.List<CodeContext.Relocatable> relocatables
-
BRANCH_OPCODE_INVERSION
private static final java.util.Map<java.lang.Integer,java.lang.Integer> BRANCH_OPCODE_INVERSION
-
-
Constructor Detail
-
CodeContext
public CodeContext(ClassFile classFile)
Creates an empty "Code" attribute.
-
-
Method Detail
-
allocateLocalVariable
public short allocateLocalVariable(short size)
Allocates space for a local variable of the given size (1 or 2) on the local variable array.As a side effect, the "max_locals" field of the "Code" attribute is updated.
The only way to deallocate local variables is to
saveLocalVariables()and laterrestoreLocalVariables().- Parameters:
size- The number of slots to allocate (1 or 2)- Returns:
- The slot index of the allocated variable
-
allocateLocalVariable
public Java.LocalVariableSlot allocateLocalVariable(short size, @Nullable java.lang.String name, @Nullable IType type)
Allocates space for a local variable of the given size (1 or 2) on the local variable array. As a side effect, the "max_locals" field of the "Code" attribute is updated.The only way to deallocate local variables is to
saveLocalVariables()and laterrestoreLocalVariables().- Parameters:
size- Number of slots to use (1 or 2)name- The variable name; ifnull, then the variable won't be written to the LocalVariableTabletype- The variable type; if the name is not null, then the type is needed to write to the LocalVariableTable
-
saveLocalVariables
public java.util.List<Java.LocalVariableSlot> saveLocalVariables()
Remembers the current size of the local variables array.
-
restoreLocalVariables
public void restoreLocalVariables()
Restores the previous size of the local variables array. This MUST to be called for every call to saveLocalVariables as it closes the variable extent for all the active local variables in the current block.
-
newCodeAttribute
public ClassFile.CodeAttribute newCodeAttribute(int initialLocalsCount, boolean debugLines, boolean debugVars)
- Parameters:
initialLocalsCount- The number of parameters, plus one iff there is a "this" parameter
-
newLineNumberTableAttribute
private ClassFile.LineNumberTableAttribute newLineNumberTableAttribute()
-
newStackMapTableAttribute
@Nullable private ClassFile.StackMapTableAttribute newStackMapTableAttribute(int initialLocalsCount)
- Returns:
nulliff the stack map is empty
-
newLocalVariableTableAttribute
@Nullable protected ClassFile.LocalVariableTableAttribute newLocalVariableTableAttribute()
- Returns:
- A
ClassFile.LocalVariableTableAttributefor thisCodeContext
-
fixUpAndRelocate
public void fixUpAndRelocate()
Fixes up all of the offsets and relocate() all relocatables.
-
maybeGrow
private void maybeGrow()
Grow the code if relocatables are required to.
-
fixUp
private void fixUp()
Fixes up all offsets.
-
relocate
private void relocate()
Relocates all relocatables.
-
write
public void write(byte[] b)
Inserts a sequence of bytes at the current insertion position. CreatesCodeContext.LineNumberOffsets as necessary.
-
write
public void write(byte b1)
Inserts a byte at the current insertion position. CreatesCodeContext.LineNumberOffsets as necessary.This method is an optimization to avoid allocating small byte[] and ease GC load.
-
write
public void write(byte b1, byte b2)Inserts bytes at the current insertion position. CreatesCodeContext.LineNumberOffsets as necessary.This method is an optimization to avoid allocating small byte[] and ease GC load.
-
write
public void write(byte b1, byte b2, byte b3)Inserts bytes at the current insertion position. CreatesCodeContext.LineNumberOffsets as necessary.This method is an optimization to avoid allocating small byte[] and ease GC load.
-
write
public void write(byte b1, byte b2, byte b3, byte b4)Inserts bytes at the current insertion position. CreatesCodeContext.LineNumberOffsets as necessary.This method is an optimization to avoid allocating small byte[] and ease GC load.
-
write
public void write(byte b1, byte b2, byte b3, byte b4, byte b5)Inserts bytes at the current insertion position. CreatesCodeContext.LineNumberOffsets as necessary.This method is an optimization to avoid allocating small byte[] and ease GC load.
-
addLineNumberOffset
public void addLineNumberOffset(int lineNumber)
-
makeSpace
public int makeSpace(int size)
Inserts size NUL bytes at the current inserter's offset, advances the current inserter's offset by size, createsCodeContext.LineNumberOffsets as necessary, and returns the current inserter's original offset (the offset of the first NUL byte that was inserted).Because the class file format does not support line numbers greater than 65535, these are treated as 65535.
- Parameters:
size- The number of NUL bytes to inject- Returns:
- The offset of the first inserted byte
-
writeShort
public void writeShort(int v)
-
writeBranch
public void writeBranch(int opcode, CodeContext.Offset dst)Generates a "branch" instruction.- Parameters:
opcode- One ofOpcode.GOTO,Opcode.JSRandOpcode.IF*dst- Where to branch
-
invertBranchOpcode
private static int invertBranchOpcode(int branchOpcode)
E.g.Opcode.IFLT("less than") inverts toOpcode.IFGE("greater than or equal to").
-
createBranchOpcodeInversion
private static java.util.Map<java.lang.Integer,java.lang.Integer> createBranchOpcodeInversion()
-
writeOffset
public void writeOffset(CodeContext.Offset src, CodeContext.Offset dst)
Writes a four-byte offset (as it is used in TABLESWITCH and LOOKUPSWITCH) into this code context.
-
newOffset
public CodeContext.Offset newOffset()
Creates and inserts anCodeContext.Offsetat the current inserter's current position.
-
newBasicBlock
public CodeContext.Offset newBasicBlock()
-
newInserter
public CodeContext.Inserter newInserter()
Allocates anCodeContext.Inserter, set it to the current offset, and inserts it before the current offset.In clear text, this means that you can continue writing to the "Code" attribute, then
pushInserter(CodeContext.Inserter)theCodeContext.Inserter, then write again (which inserts bytes into the "Code" attribute at the previously remembered position), and thenpopInserter().
-
currentInserter
public CodeContext.Inserter currentInserter()
- Returns:
- The current inserter
-
pushInserter
public void pushInserter(CodeContext.Inserter ins)
Remembers the currentCodeContext.Inserter, then replaces it with the new one.
-
popInserter
public void popInserter()
Replaces the currentCodeContext.Inserterwith the remembered one (seepushInserter(CodeContext.Inserter)).
-
mergeStackMaps
@Nullable private static final StackMap mergeStackMaps(@Nullable StackMap sm1, @Nullable StackMap sm2)
-
addExceptionTableEntry
public void addExceptionTableEntry(CodeContext.Offset startPc, CodeContext.Offset endPc, CodeContext.Offset handlerPc, @Nullable java.lang.String catchTypeFd)
Adds another entry to the "exception_table" of this code attribute (see JVMS 4.7.3).- Parameters:
catchTypeFd-nullmeansfinallyclause
-
getAllLocalVars
public java.util.List<Java.LocalVariableSlot> getAllLocalVars()
- Returns:
- All the local variables that are allocated in any block in this
CodeContext
-
removeCode
public void removeCode(CodeContext.Offset from, CodeContext.Offset to)
Removes all code between from and to. Also removes anyCodeContext.Relocatables existing in that range.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
pushOperand
public void pushOperand(java.lang.String fieldDescriptor)
Pushes oneobject_variable_info,integer_variable_info,double_variable_info,float_variable_infoorlong_variable_infoentry onto the current inserter's operand stack.
-
pushTopOperand
public void pushTopOperand()
-
pushIntOperand
public void pushIntOperand()
-
pushLongOperand
public void pushLongOperand()
-
pushFloatOperand
public void pushFloatOperand()
-
pushDoubleOperand
public void pushDoubleOperand()
-
pushNullOperand
public void pushNullOperand()
-
pushUninitializedThisOperand
public void pushUninitializedThisOperand()
-
pushUninitializedOperand
public void pushUninitializedOperand()
-
pushObjectOperand
public void pushObjectOperand(java.lang.String fieldDescriptor)
-
pushOperand
public void pushOperand(ClassFile.StackMapTableAttribute.VerificationTypeInfo topOperand)
-
peekNullOperand
public boolean peekNullOperand()
- Returns:
- Whether the top operand is a
null_variable_info
-
peekObjectOperand
public boolean peekObjectOperand()
- Returns:
- Whether the top operand is a
object_variable_info
-
peekOperand
public ClassFile.StackMapTableAttribute.VerificationTypeInfo peekOperand()
- Returns:
- The verification type of the top operand
-
popOperand
public ClassFile.StackMapTableAttribute.VerificationTypeInfo popOperand()
Pops one entry from the current inserter's operand stack.
-
popOperand
public void popOperand(ClassFile.StackMapTableAttribute.VerificationTypeInfo expected)
Pops the top entry from the operand stack and assert that it equals expected.
-
popOperand
public void popOperand(java.lang.String expectedFd)
Pops the top operand, asserts that it is aninteger_variable_info,long_variable_info,float_variable_info,double_variable_infoorvariable_object_info, and asserts that it matches the given field descriptor.
-
popOperandAssignableTo
public void popOperandAssignableTo(java.lang.String declaredFd)
-
popIntOperand
public void popIntOperand()
Asserts that the top operand is aninteger_variable_infoand pops it.
-
popLongOperand
public void popLongOperand()
Asserts that the top operand is along_variable_infoand pops it.
-
popUninitializedThisOperand
public void popUninitializedThisOperand()
Asserts that the top operand is anuninitializedThis_variable_infoand pops it.
-
popUninitializedVariableOperand
public void popUninitializedVariableOperand()
Asserts that the top operand is anuninitialized_variable_infoand pops it.
-
popReferenceOperand
public void popReferenceOperand()
Asserts that the top operand is anobject_variable_infoor anull_variable_infoand pops it.
-
popNullOperand
public void popNullOperand()
-
popObjectOperand
public java.lang.String popObjectOperand()
Asserts that the top operand is anobject_variable_info, and pops it.- Returns:
- The field descriptor of the popped object operand
-
popObjectOrUninitializedOrUninitializedThisOperand
public ClassFile.StackMapTableAttribute.VerificationTypeInfo popObjectOrUninitializedOrUninitializedThisOperand()
Asserts that the top operand is anobject_variable_info,uninitialized_variable_infooruninitializedThis_variable_info, and pops it.
-
popIntOrLongOperand
public ClassFile.StackMapTableAttribute.VerificationTypeInfo popIntOrLongOperand()
Asserts that the top operand is anint_variable_infoorlong_variable_info, then pops and returns it.
-
-