public class CodeContext extends Object
| Modifier and Type | Class and Description |
|---|---|
static interface |
CodeContext.FixUp
A throw-in interface that marks
CodeContext.Offsets as "fix-ups": During the execution of fixUp(), all "fix-ups" are invoked and can do last touches to the code attribute. |
class |
CodeContext.Inserter
A class that implements an insertion point into a "Code" attribute.
|
class |
CodeContext.LineNumberOffset
An
CodeContext.Offset who's sole purpose is to later create a 'LineNumberTable' attribute. |
class |
CodeContext.Offset
A class that represents an offset within a "Code" attribute.
|
| Constructor and Description |
|---|
CodeContext(ClassFile classFile,
String functionName)
Creates an empty "Code" attribute.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addExceptionTableEntry(CodeContext.Offset startPc,
CodeContext.Offset endPc,
CodeContext.Offset handlerPc,
String catchTypeFd)
Adds another entry to the "exception_table" of this code attribute (see JVMS 4.7.3).
|
short |
allocateLocalVariable(short size)
Allocates space for a local variable of the given size (1 or 2) on the local variable array.
|
Java.LocalVariableSlot |
allocateLocalVariable(short size,
String name,
IClass type)
Allocates space for a local variable of the given size (1 or 2) on the local variable array.
|
CodeContext.Inserter |
currentInserter() |
void |
fixUpAndRelocate()
Fixes up all of the offsets and relocate() all relocatables.
|
void |
flowAnalysis(String functionName)
Checks the code for consistency; updates the "maxStack" member.
|
List<Java.LocalVariableSlot> |
getAllLocalVars() |
ClassFile |
getClassFile()
The
ClassFile this context is related to. |
int |
makeSpace(int lineNumber,
int size)
Inserts size NUL bytes at the current inserter's offset, advances the current inserter's offset by
size, creates
CodeContext.LineNumberOffsets as necessary, and returns the current inserter's original
offset (the offset of the first NUL byte that was inserted). |
CodeContext.Inserter |
newInserter()
Allocates an
CodeContext.Inserter, set it to the current offset, and inserts it before the current offset. |
CodeContext.Offset |
newOffset()
Creates and inserts an
CodeContext.Offset at the current inserter's current position. |
void |
popInserter()
Replaces the current
CodeContext.Inserter with the remembered one (see pushInserter(CodeContext.Inserter)). |
void |
pushInserter(CodeContext.Inserter ins)
Remembers the current
CodeContext.Inserter, then replaces it with the new one. |
void |
removeCode(CodeContext.Offset from,
CodeContext.Offset to)
Removes all code between from and to.
|
void |
restoreLocalVariables()
Restores the previous size of the local variables array.
|
List<Java.LocalVariableSlot> |
saveLocalVariables()
Remembers the current size of the local variables array.
|
protected void |
storeCodeAttributeBody(DataOutputStream dos,
short lineNumberTableAttributeNameIndex,
short localVariableTableAttributeNameIndex,
short stackMapTableAttributeNameIndex) |
protected ClassFile.AttributeInfo |
storeLocalVariableTable(DataOutputStream dos,
short localVariableTableAttributeNameIndex) |
void |
write(int lineNumber,
byte b1)
Inserts a byte at the current insertion position.
|
void |
write(int lineNumber,
byte[] b)
Inserts a sequence of bytes at the current insertion position.
|
void |
write(int lineNumber,
byte b1,
byte b2)
Inserts bytes at the current insertion position.
|
void |
write(int lineNumber,
byte b1,
byte b2,
byte b3)
Inserts bytes at the current insertion position.
|
void |
write(int lineNumber,
byte b1,
byte b2,
byte b3,
byte b4)
Inserts bytes at the current insertion position.
|
void |
writeBranch(int lineNumber,
int opcode,
CodeContext.Offset dst)
Generates a "branch" instruction.
|
void |
writeOffset(int lineNumber,
CodeContext.Offset src,
CodeContext.Offset dst)
Writes a four-byte offset (as it is used in TABLESWITCH and LOOKUPSWITCH) into this code context.
|
void |
writeShort(int v) |
public short allocateLocalVariable(short size)
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 later restoreLocalVariables().
size - The number of slots to allocate (1 or 2)public Java.LocalVariableSlot allocateLocalVariable(short size, @Nullable String name, @Nullable IClass type)
The only way to deallocate local variables is to saveLocalVariables() and later restoreLocalVariables().
size - Number of slots to use (1 or 2)name - The variable name; if null, 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
LocalVariableTablepublic List<Java.LocalVariableSlot> saveLocalVariables()
public void restoreLocalVariables()
protected void storeCodeAttributeBody(DataOutputStream dos, short lineNumberTableAttributeNameIndex, short localVariableTableAttributeNameIndex, short stackMapTableAttributeNameIndex) throws IOException
lineNumberTableAttributeNameIndex - 0 == don't generate a "LineNumberTable" attributeIOException@Nullable protected ClassFile.AttributeInfo storeLocalVariableTable(DataOutputStream dos, short localVariableTableAttributeNameIndex)
ClassFile.LocalVariableTableAttribute for this CodeContextpublic void flowAnalysis(String functionName)
InternalCompilerException - The bytecode is inconsistent wrt/ the operand stackpublic void fixUpAndRelocate()
public void write(int lineNumber,
byte[] b)
CodeContext.LineNumberOffsets as necessary.lineNumber - The line number that corresponds to the byte code, or -1b - public void write(int lineNumber,
byte b1)
CodeContext.LineNumberOffsets as necessary.
This method is an optimization to avoid allocating small byte[] and ease GC load.
lineNumber - The line number that corresponds to the byte code, or -1b1 - public void write(int lineNumber,
byte b1,
byte b2)
CodeContext.LineNumberOffsets as necessary.
This method is an optimization to avoid allocating small byte[] and ease GC load.
lineNumber - The line number that corresponds to the byte code, or -1public void write(int lineNumber,
byte b1,
byte b2,
byte b3)
CodeContext.LineNumberOffsets as necessary.
This method is an optimization to avoid allocating small byte[] and ease GC load.
lineNumber - The line number that corresponds to the byte code, or -1public void write(int lineNumber,
byte b1,
byte b2,
byte b3,
byte b4)
CodeContext.LineNumberOffsets as necessary.
This method is an optimization to avoid allocating small byte[] and ease GC load.
lineNumber - The line number that corresponds to the byte code, or -1public int makeSpace(int lineNumber,
int size)
CodeContext.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.
lineNumber - -1 indicates that no particular line in the source code corresponds to this offsetsize - The number of NUL bytes to injectpublic void writeShort(int v)
public void writeBranch(int lineNumber,
int opcode,
CodeContext.Offset dst)
lineNumber - The line number that corresponds to the byte code, or -1opcode - One of Opcode.GOTO, Opcode.JSR and Opcode.IF*dst - Where to branchpublic void writeOffset(int lineNumber,
CodeContext.Offset src,
CodeContext.Offset dst)
public CodeContext.Offset newOffset()
CodeContext.Offset at the current inserter's current position.public CodeContext.Inserter newInserter()
CodeContext.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) the CodeContext.Inserter, then write again (which inserts bytes into the
"Code" attribute at the previously remembered position), and then popInserter().
public CodeContext.Inserter currentInserter()
public void pushInserter(CodeContext.Inserter ins)
CodeContext.Inserter, then replaces it with the new one.public void popInserter()
CodeContext.Inserter with the remembered one (see pushInserter(CodeContext.Inserter)).public void addExceptionTableEntry(CodeContext.Offset startPc, CodeContext.Offset endPc, CodeContext.Offset handlerPc, @Nullable String catchTypeFd)
catchTypeFd - null means finally clausepublic List<Java.LocalVariableSlot> getAllLocalVars()
CodeContextpublic void removeCode(CodeContext.Offset from, CodeContext.Offset to)
CodeContext.Relocatables
existing in that range.Copyright © 2019. All rights reserved.