|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.google.dexmaker.Code
public final class Code
Builds a sequence of instructions.
getParameter()
. Non-static methods and constructors also have a this
parameter; it's available as getThis()
. Allocate a new local
variable using newLocal()
, and assign a default value to it
with loadConstant()
. Copy a value from one local to
another with move()
.
Every local variable has a fixed type. This is either a primitive type (of any size) or a reference type. This class emits instructions appropriate to the types they operate on. Not all operations are local on all types; attempting to emit such an operation will fail with an unchecked exception.
op(UnaryOp, Local, Local)
. Transform two values
into a third value using op(BinaryOp,
Local, Local, Local)
. In either overload the first Local
parameter
is where the result will be sent; the other Local
parameters are the
inputs.
compareLongs()
compares two locals each
containing a long
primitive. This is the only operation that
can compare longs. The result of the comparison is written to another
int
local.compareFloatingPoint()
compares two
locals; both float
primitives or both double
primitives. This is the only operation that can compare floating
point values. This comparison takes an extra parameter that sets
the desired result if either parameter is NaN
. The result of
the comparison is wrtten to another int
local.
compare()
compares two locals. The Comparison.EQ
and Comparison.NE
options compare either
int
primitives or references. The other options compare only
int
primitives. This comparison takes a Label
that
will be jumped to if the comparison is true. If the comparison is
false the next instruction in sequence will be executed.
new Label()
, and mark it using mark(Label)
. All jumps to a label will execute instructions starting from
that label. You can jump to a label that hasn't yet been marked (jumping
forward) or to a label that has already been marked (jumping backward). Jump
unconditionally with jump(Label)
or conditionally based on a
comparison using compare()
.
Most methods should contain either a return instruction. Void methods
should use returnVoid()
; non-void methods should use returnValue()
with a local whose return type matches the
method's return type. Constructors are considered void methods and should
call returnVoid()
. Methods may make multiple returns. Methods
containing no return statements must either loop infinitely or throw
unconditionally; it is not legal to end a sequence of instructions without a
jump, return or throw.
addCatchClause()
to register the target label and
throwable class. All statements that follow will jump to that catch clause if
they throw a Throwable
assignable to that type. Use removeCatchClause()
to unregister the throwable class.
Throw an throwable by first assigning it to a local and then calling
throwValue()
. Control flow will jump to the nearest label
assigned to a type assignable to the thrown type. In this context, "nearest"
means the label requiring the fewest stack frames to be popped.
TypeId.getMethod()
. This is more onerous than Java language invokes, which
can infer the target method using the target object and parameters. There are
four invoke kinds:
invokeStatic()
is used for static methods.invokeDirect()
is used for private instance
methods and for constructors to call their superclass's
constructor.invokeInterface()
is used to invoke a method
whose declaring type is an interface.invokeVirtual()
is used to invoke any other
method. The target must not be static, private, a constructor, or an
interface method.invokeSuper()
is used to invoke the closest
superclass's virtual method. The target must not be static, private,
a constructor method, or an interface method.newInstance()
is used to invoke a
constructor.sget()
; write them using sput()
. For instance values you'll need to specify the declaring
instance; use getThis()
in an instance method to use this
. Read instance values using iget()
and write them with
iput()
.
newArray()
. Read an array's length
with arrayLength()
and its elements with aget()
. Write an array's elements with aput()
.
cast()
to perform either a numeric cast or
a type cast. Interrogate the type of a value in a local
using instanceOfType()
.
monitorEnter()
; release it with
monitorExit()
. It is the caller's responsibility to
guarantee that enter and exit calls are balanced, even in the presence of
exceptions thrown.
Warning: Even if a method has the synchronized
flag,
dex requires instructions to acquire and release monitors manually. A method
declared with SYNCHRONIZED
but without manual calls to monitorEnter()
and monitorExit()
will not be synchronized when executed.
Method Summary | ||
---|---|---|
void |
addCatchClause(TypeId<? extends Throwable> toCatch,
Label catchClause)
Registers catchClause as a branch target for all instructions
in this frame that throw a class assignable to toCatch . |
|
void |
aget(Local<?> target,
Local<?> array,
Local<Integer> index)
Assigns target to the element of array at index index . |
|
void |
aput(Local<?> array,
Local<Integer> index,
Local<?> source)
Sets the element at index in array the value in source . |
|
|
arrayLength(Local<Integer> target,
Local<T> array)
Sets target to the length of the array in array . |
|
void |
cast(Local<?> target,
Local<?> source)
Performs either a numeric cast or a type cast. |
|
|
compare(Comparison comparison,
Label trueLabel,
Local<T> a,
Local<T> b)
Compare ints or references. |
|
|
compareFloatingPoint(Local<Integer> target,
Local<T> a,
Local<T> b,
int nanValue)
Compare floats or doubles. |
|
void |
compareLongs(Local<Integer> target,
Local<Long> a,
Local<Long> b)
Compare longs. |
|
|
getParameter(int index,
TypeId<T> type)
Returns the local for the parameter at index index and of type
type . |
|
|
getThis(TypeId<T> type)
Returns the local for this of type type . |
|
|
iget(FieldId<D,V> fieldId,
Local<V> target,
Local<D> instance)
Copies the value in instance field fieldId of instance to
target . |
|
void |
instanceOfType(Local<?> target,
Local<?> source,
TypeId<?> type)
Tests if the value in source is assignable to type . |
|
|
invokeDirect(MethodId<D,R> method,
Local<? super R> target,
Local<? extends D> instance,
Local<?>... args)
Calls method of instance using args and assigns
the result to target . |
|
|
invokeInterface(MethodId<D,R> method,
Local<? super R> target,
Local<? extends D> instance,
Local<?>... args)
Calls the interface method method of instance using
args and assigns the result to target . |
|
|
invokeStatic(MethodId<?,R> method,
Local<? super R> target,
Local<?>... args)
Calls the static method method using args and assigns the
result to target . |
|
|
invokeSuper(MethodId<D,R> method,
Local<? super R> target,
Local<? extends D> instance,
Local<?>... args)
Calls the closest superclass's virtual method method of instance using args and assigns the result to target . |
|
|
invokeVirtual(MethodId<D,R> method,
Local<? super R> target,
Local<? extends D> instance,
Local<?>... args)
Calls the non-private instance method method of instance
using args and assigns the result to target . |
|
|
iput(FieldId<D,V> fieldId,
Local<D> instance,
Local<V> source)
Copies the value in source to the instance field fieldId
of instance . |
|
void |
jump(Label target)
Transfers flow control to the instructions at target . |
|
|
loadConstant(Local<T> target,
T value)
Copies the constant value value to target . |
|
void |
mark(Label label)
Start defining instructions for the named label. |
|
void |
monitorEnter(Local<?> monitor)
Awaits the lock on monitor , and acquires it. |
|
void |
monitorExit(Local<?> monitor)
Releases the held lock on monitor . |
|
|
move(Local<T> target,
Local<T> source)
Copies the value in source to target . |
|
|
newArray(Local<T> target,
Local<Integer> length)
Assigns target to a newly allocated array of length length . |
|
|
newInstance(Local<T> target,
MethodId<T,Void> constructor,
Local<?>... args)
Calls the constructor constructor using args and assigns
the new instance to target . |
|
|
newLocal(TypeId<T> type)
Allocates a new local variable of type type . |
|
|
op(BinaryOp op,
Local<T> target,
Local<T> a,
Local<T> b)
Executes op and sets target to the result. |
|
|
op(UnaryOp op,
Local<T> target,
Local<T> source)
Executes op and sets target to the result. |
|
Label |
removeCatchClause(TypeId<? extends Throwable> toCatch)
Deregisters the catch clause label for toCatch and returns it. |
|
void |
returnValue(Local<?> result)
Returns the value in result to the calling method. |
|
void |
returnVoid()
Returns from a void method. |
|
|
sget(FieldId<?,V> fieldId,
Local<V> target)
Copies the value in target to the static field fieldId . |
|
|
sput(FieldId<?,V> fieldId,
Local<V> source)
Copies the value in source to the static field fieldId . |
|
void |
throwValue(Local<? extends Throwable> toThrow)
Throws the throwable in toThrow . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public <T> Local<T> newLocal(TypeId<T> type)
type
. It is an error to
allocate a local after instructions have been emitted.
public <T> Local<T> getParameter(int index, TypeId<T> type)
index
and of type
type
.
public <T> Local<T> getThis(TypeId<T> type)
this
of type type
. It is an error
to call getThis()
if this is a static method.
public void mark(Label label)
public void jump(Label target)
target
. It is an
error to jump to a label not marked on this Code
.
public void addCatchClause(TypeId<? extends Throwable> toCatch, Label catchClause)
catchClause
as a branch target for all instructions
in this frame that throw a class assignable to toCatch
. This
includes methods invoked from this frame. Deregister the clause using
removeCatchClause()
. It is an error to
register a catch clause without also marking it
in the same
Code
instance.
public Label removeCatchClause(TypeId<? extends Throwable> toCatch)
toCatch
and returns it.
public void throwValue(Local<? extends Throwable> toThrow)
toThrow
.
public <T> void loadConstant(Local<T> target, T value)
value
to target
. The constant
must be a primitive, String, Class, TypeId, or null.
public <T> void move(Local<T> target, Local<T> source)
source
to target
.
public <T> void op(UnaryOp op, Local<T> target, Local<T> source)
op
and sets target
to the result.
public <T> void op(BinaryOp op, Local<T> target, Local<T> a, Local<T> b)
op
and sets target
to the result.
public <T> void compare(Comparison comparison, Label trueLabel, Local<T> a, Local<T> b)
trueLabel
. If it is false, execution continues to the next
instruction.
public <T extends Number> void compareFloatingPoint(Local<Integer> target, Local<T> a, Local<T> b, int nanValue)
target
if a < b
, 0 in target
if a == b
and 1 in target if a > b
. This stores nanValue
in target
if either value
is NaN
.
public void compareLongs(Local<Integer> target, Local<Long> a, Local<Long> b)
target
if a < b
, 0 in target
if a == b
and 1 in target if a > b
.
public <D,V> void iget(FieldId<D,V> fieldId, Local<V> target, Local<D> instance)
fieldId
of instance
to
target
.
public <D,V> void iput(FieldId<D,V> fieldId, Local<D> instance, Local<V> source)
source
to the instance field fieldId
of instance
.
public <V> void sget(FieldId<?,V> fieldId, Local<V> target)
target
to the static field fieldId
.
public <V> void sput(FieldId<?,V> fieldId, Local<V> source)
source
to the static field fieldId
.
public <T> void newInstance(Local<T> target, MethodId<T,Void> constructor, Local<?>... args)
constructor
using args
and assigns
the new instance to target
.
public <R> void invokeStatic(MethodId<?,R> method, Local<? super R> target, Local<?>... args)
method
using args
and assigns the
result to target
.
target
- the local to receive the method's return value, or null
if the return type is void
or if its value not needed.public <D,R> void invokeVirtual(MethodId<D,R> method, Local<? super R> target, Local<? extends D> instance, Local<?>... args)
method
of instance
using args
and assigns the result to target
.
method
- a non-private, non-static, method declared on a class. May
not be an interface method or a constructor.target
- the local to receive the method's return value, or null
if the return type is void
or if its value not needed.public <D,R> void invokeDirect(MethodId<D,R> method, Local<? super R> target, Local<? extends D> instance, Local<?>... args)
method
of instance
using args
and assigns
the result to target
.
method
- either a private method or the superclass's constructor in
a constructor's call to super()
.target
- the local to receive the method's return value, or null
if the return type is void
or if its value not needed.public <D,R> void invokeSuper(MethodId<D,R> method, Local<? super R> target, Local<? extends D> instance, Local<?>... args)
method
of instance
using args
and assigns the result to target
.
target
- the local to receive the method's return value, or null
if the return type is void
or if its value not needed.public <D,R> void invokeInterface(MethodId<D,R> method, Local<? super R> target, Local<? extends D> instance, Local<?>... args)
method
of instance
using
args
and assigns the result to target
.
method
- a method declared on an interface.target
- the local to receive the method's return value, or null
if the return type is void
or if its value not needed.public void instanceOfType(Local<?> target, Local<?> source, TypeId<?> type)
source
is assignable to type
. If it
is, target
is assigned to 1; otherwise target
is assigned
to 0.
public void cast(Local<?> target, Local<?> source)
1.8d
to an integer
yields 1
, losing the fractional part. Converting the integer
0x12345678
to a short yields 0x5678
, losing the high
bytes. The following numeric casts are supported:
From | To |
---|---|
int | byte, char, short, long, float, double |
long | int, float, double |
float | int, long, double |
double | int, long, float |
For some primitive conversions it will be necessary to chain multiple cast operations. For example, to go from float to short one would first cast float to int and then int to short.
Numeric casts never throw ClassCastException
.
ClassCastException
is thrown.
public <T> void arrayLength(Local<Integer> target, Local<T> array)
target
to the length of the array in array
.
public <T> void newArray(Local<T> target, Local<Integer> length)
target
to a newly allocated array of length length
. The array's type is the same as target
's type.
public void aget(Local<?> target, Local<?> array, Local<Integer> index)
target
to the element of array
at index index
.
public void aput(Local<?> array, Local<Integer> index, Local<?> source)
index
in array
the value in source
.
public void returnVoid()
void
method. After a return it is an error to
define further instructions after a return without first marking
an existing unmarked label.
public void returnValue(Local<?> result)
result
to the calling method. After a return
it is an error to define further instructions after a return without
first marking
an existing unmarked label.
public void monitorEnter(Local<?> monitor)
monitor
, and acquires it.
public void monitorExit(Local<?> monitor)
monitor
.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |