Package com.tonic.builder
Class CodeBuilder
- java.lang.Object
-
- com.tonic.builder.CodeBuilder
-
public class CodeBuilder extends Object
A fluent assembler for a method body that records instructions as sized ops and resolves named labels to branch offsets when the body is built or assembled.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description CodeBuilderaaload()Appendsaaload, loading a reference element from an array.CodeBuilderaastore()Appendsaastore, storing a reference element into an array.CodeBuilderaconst_null()Appendsaconst_null.CodeBuilderaload(int index)Appends a reference load from a local slot.CodeBuilderanewarray(String type)Appendsanewarrayfor a reference element type.CodeBuilderareturn()Appendsareturn, returning a reference.CodeBuilderarraylength()Appendsarraylength, replacing an array reference with its length.CodeWriter.ClonedRangeassemble(ClassFile target)Materializes the recorded ops into a detached instruction snapshot resolved againsttarget's constant pool.CodeBuilderastore(int index)Appends a reference store to a local slot.CodeBuilderathrow()Appendsathrow, throwing the reference on top of the stack.CodeBuilderbaload()Appendsbaload, loading a byte or boolean element from an array.CodeBuilderbastore()Appendsbastore, storing a byte or boolean element into an array.CodeBuilderbipush(int value)Appendsbipush, pushing a signed byte widened to int.CodeBuildercaload()Appendscaload, loading a char element from an array.CodeBuildercastore()Appendscastore, storing a char element into an array.CodeBuildercheckcast(String type)Appendscheckcast.CodeBuilderd2f()Appendsd2f, narrowing a double to float.CodeBuilderd2i()Appendsd2i, converting a double to int.CodeBuilderd2l()Appendsd2l, converting a double to long.CodeBuilderdadd()Appendsdadd, adding the top two double values.CodeBuilderdaload()Appendsdaload, loading a double element from an array.CodeBuilderdastore()Appendsdastore, storing a double element into an array.CodeBuilderdcmpg()Appendsdcmpg, comparing two doubles and pushing 1 when either is NaN.CodeBuilderdcmpl()Appendsdcmpl, comparing two doubles and pushing -1 when either is NaN.CodeBuilderdconst(double value)Appends a double constant push, usingdconst_0/dconst_1where possible.CodeBuilderddiv()Appendsddiv, dividing the top two double values.static CodeBuilderdetached()Creates a standalone builder for authoring a detached instruction snippet, independent of theClassBuilder/MethodBuilderchain.CodeBuilderdload(int index)Appends a double load from a local slot.CodeBuilderdmul()Appendsdmul, multiplying the top two double values.CodeBuilderdneg()Appendsdneg, negating the top double value.CodeBuilderdrem()Appendsdrem, taking the remainder of the top two double values.CodeBuilderdreturn()Appendsdreturn, returning a double.CodeBuilderdstore(int index)Appends a double store to a local slot.CodeBuilderdsub()Appendsdsub, subtracting the top two double values.CodeBuilderdup()Appendsdup, duplicating the top one-slot value.CodeBuilderdup_x1()Appendsdup_x1, duplicating the top value one slot down.CodeBuilderdup_x2()Appendsdup_x2, duplicating the top value two slots down.CodeBuilderdup2()Appendsdup2, duplicating the top two slots.CodeBuilderdup2_x1()Appendsdup2_x1, duplicating the top two slots one slot down.CodeBuilderdup2_x2()Appendsdup2_x2, duplicating the top two slots two slots down.MethodBuilderend()Ends the body and hands back to the enclosing method builder.CodeBuilderexternalLabel(String name)Declaresnameas an external branch target - an instruction in the host method the snippet will be spliced into, not one defined in the snippet.CodeBuilderf2d()Appendsf2d, widening a float to double.CodeBuilderf2i()Appendsf2i, converting a float to int.CodeBuilderf2l()Appendsf2l, converting a float to long.CodeBuilderfadd()Appendsfadd, adding the top two float values.CodeBuilderfaload()Appendsfaload, loading a float element from an array.CodeBuilderfastore()Appendsfastore, storing a float element into an array.CodeBuilderfcmpg()Appendsfcmpg, comparing two floats and pushing 1 when either is NaN.CodeBuilderfcmpl()Appendsfcmpl, comparing two floats and pushing -1 when either is NaN.CodeBuilderfconst(float value)Appends a float constant push, usingfconst_0/fconst_1/fconst_2where possible.CodeBuilderfdiv()Appendsfdiv, dividing the top two float values.CodeBuilderfload(int index)Appends a float load from a local slot.CodeBuilderfmul()Appendsfmul, multiplying the top two float values.CodeBuilderfneg()Appendsfneg, negating the top float value.CodeBuilderfrem()Appendsfrem, taking the remainder of the top two float values.CodeBuilderfreturn()Appendsfreturn, returning a float.CodeBuilderfstore(int index)Appends a float store to a local slot.CodeBuilderfsub()Appendsfsub, subtracting the top two float values.CodeBuildergetfield(String owner, String name, String descriptor)Appendsgetfield, adding the field reference to the constant pool.CodeBuildergetstatic(String owner, String name, String descriptor)Appendsgetstatic, adding the field reference to the constant pool.CodeBuildergoto_(String labelName)Appends an unconditionalgoto.CodeBuilderi2b()Appendsi2b, truncating an int to byte.CodeBuilderi2c()Appendsi2c, truncating an int to char.CodeBuilderi2d()Appendsi2d, converting an int to double.CodeBuilderi2f()Appendsi2f, converting an int to float.CodeBuilderi2l()Appendsi2l, widening an int to long.CodeBuilderi2s()Appendsi2s, truncating an int to short.CodeBuilderiadd()Appendsiadd, adding the top two int values.CodeBuilderiaload()Appendsiaload, loading a int element from an array.CodeBuilderiand()Appendsiand, bitwise and of the top two int values.CodeBuilderiastore()Appendsiastore, storing a int element into an array.CodeBuildericonst(int value)Appends the shortest int constant push for the value.CodeBuilderidiv()Appendsidiv, dividing the top two int values.CodeBuilderif_acmpeq(String labelName)Appendsif_acmpeq, branching when the top two references are the same object.CodeBuilderif_acmpne(String labelName)Appendsif_acmpne, branching when the top two references are different objects.CodeBuilderif_icmpeq(String labelName)Appendsif_icmpeq, branching when the top two ints are equal.CodeBuilderif_icmpge(String labelName)Appendsif_icmpge, branching when the first int is at least the second.CodeBuilderif_icmpgt(String labelName)Appendsif_icmpgt, branching when the first int is greater than the second.CodeBuilderif_icmple(String labelName)Appendsif_icmple, branching when the first int is at most the second.CodeBuilderif_icmplt(String labelName)Appendsif_icmplt, branching when the first int is less than the second.CodeBuilderif_icmpne(String labelName)Appendsif_icmpne, branching when the top two ints differ.CodeBuilderifeq(String labelName)Appendsifeq, branching when the top int is zero.CodeBuilderifge(String labelName)Appendsifge, branching when the top int is at least zero.CodeBuilderifgt(String labelName)Appendsifgt, branching when the top int is greater than zero.CodeBuilderifle(String labelName)Appendsifle, branching when the top int is at most zero.CodeBuilderiflt(String labelName)Appendsiflt, branching when the top int is less than zero.CodeBuilderifne(String labelName)Appendsifne, branching when the top int is non-zero.CodeBuilderifnonnull(String labelName)Appendsifnonnull, branching when the top reference is non-null.CodeBuilderifnull(String labelName)Appendsifnull, branching when the top reference is null.CodeBuilderiinc(int varIndex, int increment)Appendsiinc, adding a constant to an int local in place.CodeBuilderiload(int index)Appends a int load from a local slot.CodeBuilderimul()Appendsimul, multiplying the top two int values.CodeBuilderineg()Appendsineg, negating the top int value.CodeBuilderinstanceof_(String type)Appendsinstanceof, replacing the top reference with 1 when it is of the given type.CodeBuilderinvokedynamic(String name, String descriptor, MethodHandle bootstrap, Object... bsmArgs)Appendsinvokedynamic, registering the bootstrap method and its static arguments on the host class - the enclosing class builder, or theassemble(ClassFile)target when detached.CodeBuilderinvokeinterface(String owner, String name, String descriptor)Appendsinvokeinterface, adding the interface method reference and deriving the argument slot count from the descriptor.CodeBuilderinvokespecial(String owner, String name, String descriptor)Appendsinvokespecial, adding the method reference to the constant pool.CodeBuilderinvokestatic(String owner, String name, String descriptor)Appendsinvokestatic, adding the method reference to the constant pool.CodeBuilderinvokevirtual(String owner, String name, String descriptor)Appendsinvokevirtual, adding the method reference to the constant pool.CodeBuilderior()Appendsior, bitwise or of the top two int values.CodeBuilderirem()Appendsirem, taking the remainder of the top two int values.CodeBuilderireturn()Appendsireturn, returning an int.CodeBuilderishl()Appendsishl, shifting a int left.CodeBuilderishr()Appendsishr, arithmetic right shift of a int.CodeBuilderistore(int index)Appends a int store to a local slot.CodeBuilderisub()Appendsisub, subtracting the top two int values.CodeBuilderiushr()Appendsiushr, logical right shift of a int.CodeBuilderixor()Appendsixor, bitwise exclusive or of the top two int values.CodeBuilderl2d()Appendsl2d, converting a long to double.CodeBuilderl2f()Appendsl2f, converting a long to float.CodeBuilderl2i()Appendsl2i, narrowing a long to int.CodeBuilderlabel(String name)Defines a branch target at the current position.CodeBuilderladd()Appendsladd, adding the top two long values.CodeBuilderlaload()Appendslaload, loading a long element from an array.CodeBuilderland()Appendsland, bitwise and of the top two long values.CodeBuilderlastore()Appendslastore, storing a long element into an array.CodeBuilderlcmp()Appendslcmp, pushing -1, 0 or 1 for the top two longs.CodeBuilderlconst(long value)Appends a long constant push, usinglconst_0/lconst_1where possible.CodeBuilderldc(Object value)Appends a constant load, routing numeric boxes to the matching typed constant push.CodeBuilderldiv()Appendsldiv, dividing the top two long values.CodeBuilderlload(int index)Appends a long load from a local slot.CodeBuilderlmul()Appendslmul, multiplying the top two long values.CodeBuilderlneg()Appendslneg, negating the top long value.CodeBuilderlookupswitch(String defaultLabel, Map<Integer,String> cases)Emits alookupswitchmapping each key incasesto its target label, withdefaultLabelas the fallthrough.CodeBuilderlor()Appendslor, bitwise or of the top two long values.CodeBuilderlrem()Appendslrem, taking the remainder of the top two long values.CodeBuilderlreturn()Appendslreturn, returning a long.CodeBuilderlshl()Appendslshl, shifting a long left.CodeBuilderlshr()Appendslshr, arithmetic right shift of a long.CodeBuilderlstore(int index)Appends a long store to a local slot.CodeBuilderlsub()Appendslsub, subtracting the top two long values.CodeBuilderlushr()Appendslushr, logical right shift of a long.CodeBuilderlxor()Appendslxor, bitwise exclusive or of the top two long values.CodeBuildermonitorenter()Appendsmonitorenter, acquiring the monitor of the top reference.CodeBuildermonitorexit()Appendsmonitorexit, releasing the monitor of the top reference.CodeBuildermultianewarray(String descriptor, int dims)Appendsmultianewarray, consuming one length from the stack per dimension.CodeBuildernew_(String type)Appendsnew, adding the class reference to the constant pool.CodeBuildernewarray(int arrayType)Appendsnewarrayfor a primitive element type.CodeBuildernop()Appendsnop.CodeBuilderpop()Appendspop, discarding the top one-slot value.CodeBuilderpop2()Appendspop2, discarding the top two slots.CodeBuilderputfield(String owner, String name, String descriptor)Appendsputfield, adding the field reference to the constant pool.CodeBuilderputstatic(String owner, String name, String descriptor)Appendsputstatic, adding the field reference to the constant pool.CodeBuildersaload()Appendssaload, loading a short element from an array.CodeBuildersastore()Appendssastore, storing a short element into an array.CodeBuildersipush(int value)Appendssipush, pushing a signed short widened to int.CodeBuilderswap()Appendsswap, exchanging the top two one-slot values.CodeBuildertableswitch(int low, int high, String defaultLabel, String... caseLabels)Emits atableswitchover the contiguous keyslow..high.CodeBuildertrycatch(String startLabel, String endLabel, String handlerLabel, String exceptionType)Records an exception-table region, resolved to offsets when the body is built or assembled.CodeBuildervreturn()Appendsreturn, returning from a void method.
-
-
-
Method Detail
-
detached
public static CodeBuilder detached()
Creates a standalone builder for authoring a detached instruction snippet, independent of theClassBuilder/MethodBuilderchain.- Returns:
- a builder with no enclosing method
-
externalLabel
public CodeBuilder externalLabel(String name)
Declaresnameas an external branch target - an instruction in the host method the snippet will be spliced into, not one defined in the snippet.- Parameters:
name- the label name, created on first use- Returns:
- this builder
-
label
public CodeBuilder label(String name)
Defines a branch target at the current position.- Parameters:
name- the label name, created on first use- Returns:
- this builder
-
iconst
public CodeBuilder iconst(int value)
Appends the shortest int constant push for the value.- Parameters:
value- the int to push- Returns:
- this builder
-
lconst
public CodeBuilder lconst(long value)
Appends a long constant push, usinglconst_0/lconst_1where possible.- Parameters:
value- the long to push- Returns:
- this builder
-
fconst
public CodeBuilder fconst(float value)
Appends a float constant push, usingfconst_0/fconst_1/fconst_2where possible.- Parameters:
value- the float to push- Returns:
- this builder
-
dconst
public CodeBuilder dconst(double value)
Appends a double constant push, usingdconst_0/dconst_1where possible.- Parameters:
value- the double to push- Returns:
- this builder
-
aconst_null
public CodeBuilder aconst_null()
Appendsaconst_null.- Returns:
- this builder
-
ldc
public CodeBuilder ldc(Object value)
Appends a constant load, routing numeric boxes to the matching typed constant push.- Parameters:
value- a String, Integer, Long, Float or Double- Returns:
- this builder
- Throws:
IllegalArgumentException- if the value is of any other type
-
bipush
public CodeBuilder bipush(int value)
Appendsbipush, pushing a signed byte widened to int.- Parameters:
value- the byte-ranged value to push- Returns:
- this builder
-
sipush
public CodeBuilder sipush(int value)
Appendssipush, pushing a signed short widened to int.- Parameters:
value- the short-ranged value to push- Returns:
- this builder
-
iload
public CodeBuilder iload(int index)
Appends a int load from a local slot.- Parameters:
index- the local variable slot- Returns:
- this builder
-
lload
public CodeBuilder lload(int index)
Appends a long load from a local slot.- Parameters:
index- the local variable slot- Returns:
- this builder
-
fload
public CodeBuilder fload(int index)
Appends a float load from a local slot.- Parameters:
index- the local variable slot- Returns:
- this builder
-
dload
public CodeBuilder dload(int index)
Appends a double load from a local slot.- Parameters:
index- the local variable slot- Returns:
- this builder
-
aload
public CodeBuilder aload(int index)
Appends a reference load from a local slot.- Parameters:
index- the local variable slot- Returns:
- this builder
-
istore
public CodeBuilder istore(int index)
Appends a int store to a local slot.- Parameters:
index- the local variable slot- Returns:
- this builder
-
lstore
public CodeBuilder lstore(int index)
Appends a long store to a local slot.- Parameters:
index- the local variable slot- Returns:
- this builder
-
fstore
public CodeBuilder fstore(int index)
Appends a float store to a local slot.- Parameters:
index- the local variable slot- Returns:
- this builder
-
dstore
public CodeBuilder dstore(int index)
Appends a double store to a local slot.- Parameters:
index- the local variable slot- Returns:
- this builder
-
astore
public CodeBuilder astore(int index)
Appends a reference store to a local slot.- Parameters:
index- the local variable slot- Returns:
- this builder
-
iaload
public CodeBuilder iaload()
Appendsiaload, loading a int element from an array.- Returns:
- this builder
-
laload
public CodeBuilder laload()
Appendslaload, loading a long element from an array.- Returns:
- this builder
-
faload
public CodeBuilder faload()
Appendsfaload, loading a float element from an array.- Returns:
- this builder
-
daload
public CodeBuilder daload()
Appendsdaload, loading a double element from an array.- Returns:
- this builder
-
aaload
public CodeBuilder aaload()
Appendsaaload, loading a reference element from an array.- Returns:
- this builder
-
baload
public CodeBuilder baload()
Appendsbaload, loading a byte or boolean element from an array.- Returns:
- this builder
-
caload
public CodeBuilder caload()
Appendscaload, loading a char element from an array.- Returns:
- this builder
-
saload
public CodeBuilder saload()
Appendssaload, loading a short element from an array.- Returns:
- this builder
-
iastore
public CodeBuilder iastore()
Appendsiastore, storing a int element into an array.- Returns:
- this builder
-
lastore
public CodeBuilder lastore()
Appendslastore, storing a long element into an array.- Returns:
- this builder
-
fastore
public CodeBuilder fastore()
Appendsfastore, storing a float element into an array.- Returns:
- this builder
-
dastore
public CodeBuilder dastore()
Appendsdastore, storing a double element into an array.- Returns:
- this builder
-
aastore
public CodeBuilder aastore()
Appendsaastore, storing a reference element into an array.- Returns:
- this builder
-
bastore
public CodeBuilder bastore()
Appendsbastore, storing a byte or boolean element into an array.- Returns:
- this builder
-
castore
public CodeBuilder castore()
Appendscastore, storing a char element into an array.- Returns:
- this builder
-
sastore
public CodeBuilder sastore()
Appendssastore, storing a short element into an array.- Returns:
- this builder
-
arraylength
public CodeBuilder arraylength()
Appendsarraylength, replacing an array reference with its length.- Returns:
- this builder
-
pop
public CodeBuilder pop()
Appendspop, discarding the top one-slot value.- Returns:
- this builder
-
pop2
public CodeBuilder pop2()
Appendspop2, discarding the top two slots.- Returns:
- this builder
-
dup
public CodeBuilder dup()
Appendsdup, duplicating the top one-slot value.- Returns:
- this builder
-
dup_x1
public CodeBuilder dup_x1()
Appendsdup_x1, duplicating the top value one slot down.- Returns:
- this builder
-
dup_x2
public CodeBuilder dup_x2()
Appendsdup_x2, duplicating the top value two slots down.- Returns:
- this builder
-
dup2
public CodeBuilder dup2()
Appendsdup2, duplicating the top two slots.- Returns:
- this builder
-
dup2_x1
public CodeBuilder dup2_x1()
Appendsdup2_x1, duplicating the top two slots one slot down.- Returns:
- this builder
-
dup2_x2
public CodeBuilder dup2_x2()
Appendsdup2_x2, duplicating the top two slots two slots down.- Returns:
- this builder
-
swap
public CodeBuilder swap()
Appendsswap, exchanging the top two one-slot values.- Returns:
- this builder
-
iadd
public CodeBuilder iadd()
Appendsiadd, adding the top two int values.- Returns:
- this builder
-
isub
public CodeBuilder isub()
Appendsisub, subtracting the top two int values.- Returns:
- this builder
-
imul
public CodeBuilder imul()
Appendsimul, multiplying the top two int values.- Returns:
- this builder
-
idiv
public CodeBuilder idiv()
Appendsidiv, dividing the top two int values.- Returns:
- this builder
-
irem
public CodeBuilder irem()
Appendsirem, taking the remainder of the top two int values.- Returns:
- this builder
-
ineg
public CodeBuilder ineg()
Appendsineg, negating the top int value.- Returns:
- this builder
-
ladd
public CodeBuilder ladd()
Appendsladd, adding the top two long values.- Returns:
- this builder
-
lsub
public CodeBuilder lsub()
Appendslsub, subtracting the top two long values.- Returns:
- this builder
-
lmul
public CodeBuilder lmul()
Appendslmul, multiplying the top two long values.- Returns:
- this builder
-
ldiv
public CodeBuilder ldiv()
Appendsldiv, dividing the top two long values.- Returns:
- this builder
-
lrem
public CodeBuilder lrem()
Appendslrem, taking the remainder of the top two long values.- Returns:
- this builder
-
lneg
public CodeBuilder lneg()
Appendslneg, negating the top long value.- Returns:
- this builder
-
fadd
public CodeBuilder fadd()
Appendsfadd, adding the top two float values.- Returns:
- this builder
-
fsub
public CodeBuilder fsub()
Appendsfsub, subtracting the top two float values.- Returns:
- this builder
-
fmul
public CodeBuilder fmul()
Appendsfmul, multiplying the top two float values.- Returns:
- this builder
-
fdiv
public CodeBuilder fdiv()
Appendsfdiv, dividing the top two float values.- Returns:
- this builder
-
frem
public CodeBuilder frem()
Appendsfrem, taking the remainder of the top two float values.- Returns:
- this builder
-
fneg
public CodeBuilder fneg()
Appendsfneg, negating the top float value.- Returns:
- this builder
-
dadd
public CodeBuilder dadd()
Appendsdadd, adding the top two double values.- Returns:
- this builder
-
dsub
public CodeBuilder dsub()
Appendsdsub, subtracting the top two double values.- Returns:
- this builder
-
dmul
public CodeBuilder dmul()
Appendsdmul, multiplying the top two double values.- Returns:
- this builder
-
ddiv
public CodeBuilder ddiv()
Appendsddiv, dividing the top two double values.- Returns:
- this builder
-
drem
public CodeBuilder drem()
Appendsdrem, taking the remainder of the top two double values.- Returns:
- this builder
-
dneg
public CodeBuilder dneg()
Appendsdneg, negating the top double value.- Returns:
- this builder
-
iand
public CodeBuilder iand()
Appendsiand, bitwise and of the top two int values.- Returns:
- this builder
-
ior
public CodeBuilder ior()
Appendsior, bitwise or of the top two int values.- Returns:
- this builder
-
ixor
public CodeBuilder ixor()
Appendsixor, bitwise exclusive or of the top two int values.- Returns:
- this builder
-
ishl
public CodeBuilder ishl()
Appendsishl, shifting a int left.- Returns:
- this builder
-
ishr
public CodeBuilder ishr()
Appendsishr, arithmetic right shift of a int.- Returns:
- this builder
-
iushr
public CodeBuilder iushr()
Appendsiushr, logical right shift of a int.- Returns:
- this builder
-
land
public CodeBuilder land()
Appendsland, bitwise and of the top two long values.- Returns:
- this builder
-
lor
public CodeBuilder lor()
Appendslor, bitwise or of the top two long values.- Returns:
- this builder
-
lxor
public CodeBuilder lxor()
Appendslxor, bitwise exclusive or of the top two long values.- Returns:
- this builder
-
lshl
public CodeBuilder lshl()
Appendslshl, shifting a long left.- Returns:
- this builder
-
lshr
public CodeBuilder lshr()
Appendslshr, arithmetic right shift of a long.- Returns:
- this builder
-
lushr
public CodeBuilder lushr()
Appendslushr, logical right shift of a long.- Returns:
- this builder
-
i2l
public CodeBuilder i2l()
Appendsi2l, widening an int to long.- Returns:
- this builder
-
i2f
public CodeBuilder i2f()
Appendsi2f, converting an int to float.- Returns:
- this builder
-
i2d
public CodeBuilder i2d()
Appendsi2d, converting an int to double.- Returns:
- this builder
-
l2i
public CodeBuilder l2i()
Appendsl2i, narrowing a long to int.- Returns:
- this builder
-
l2f
public CodeBuilder l2f()
Appendsl2f, converting a long to float.- Returns:
- this builder
-
l2d
public CodeBuilder l2d()
Appendsl2d, converting a long to double.- Returns:
- this builder
-
f2i
public CodeBuilder f2i()
Appendsf2i, converting a float to int.- Returns:
- this builder
-
f2l
public CodeBuilder f2l()
Appendsf2l, converting a float to long.- Returns:
- this builder
-
f2d
public CodeBuilder f2d()
Appendsf2d, widening a float to double.- Returns:
- this builder
-
d2i
public CodeBuilder d2i()
Appendsd2i, converting a double to int.- Returns:
- this builder
-
d2l
public CodeBuilder d2l()
Appendsd2l, converting a double to long.- Returns:
- this builder
-
d2f
public CodeBuilder d2f()
Appendsd2f, narrowing a double to float.- Returns:
- this builder
-
i2b
public CodeBuilder i2b()
Appendsi2b, truncating an int to byte.- Returns:
- this builder
-
i2c
public CodeBuilder i2c()
Appendsi2c, truncating an int to char.- Returns:
- this builder
-
i2s
public CodeBuilder i2s()
Appendsi2s, truncating an int to short.- Returns:
- this builder
-
lcmp
public CodeBuilder lcmp()
Appendslcmp, pushing -1, 0 or 1 for the top two longs.- Returns:
- this builder
-
fcmpl
public CodeBuilder fcmpl()
Appendsfcmpl, comparing two floats and pushing -1 when either is NaN.- Returns:
- this builder
-
fcmpg
public CodeBuilder fcmpg()
Appendsfcmpg, comparing two floats and pushing 1 when either is NaN.- Returns:
- this builder
-
dcmpl
public CodeBuilder dcmpl()
Appendsdcmpl, comparing two doubles and pushing -1 when either is NaN.- Returns:
- this builder
-
dcmpg
public CodeBuilder dcmpg()
Appendsdcmpg, comparing two doubles and pushing 1 when either is NaN.- Returns:
- this builder
-
ifeq
public CodeBuilder ifeq(String labelName)
Appendsifeq, branching when the top int is zero.- Parameters:
labelName- the target label, created on first use- Returns:
- this builder
-
ifne
public CodeBuilder ifne(String labelName)
Appendsifne, branching when the top int is non-zero.- Parameters:
labelName- the target label, created on first use- Returns:
- this builder
-
iflt
public CodeBuilder iflt(String labelName)
Appendsiflt, branching when the top int is less than zero.- Parameters:
labelName- the target label, created on first use- Returns:
- this builder
-
ifge
public CodeBuilder ifge(String labelName)
Appendsifge, branching when the top int is at least zero.- Parameters:
labelName- the target label, created on first use- Returns:
- this builder
-
ifgt
public CodeBuilder ifgt(String labelName)
Appendsifgt, branching when the top int is greater than zero.- Parameters:
labelName- the target label, created on first use- Returns:
- this builder
-
ifle
public CodeBuilder ifle(String labelName)
Appendsifle, branching when the top int is at most zero.- Parameters:
labelName- the target label, created on first use- Returns:
- this builder
-
if_icmpeq
public CodeBuilder if_icmpeq(String labelName)
Appendsif_icmpeq, branching when the top two ints are equal.- Parameters:
labelName- the target label, created on first use- Returns:
- this builder
-
if_icmpne
public CodeBuilder if_icmpne(String labelName)
Appendsif_icmpne, branching when the top two ints differ.- Parameters:
labelName- the target label, created on first use- Returns:
- this builder
-
if_icmplt
public CodeBuilder if_icmplt(String labelName)
Appendsif_icmplt, branching when the first int is less than the second.- Parameters:
labelName- the target label, created on first use- Returns:
- this builder
-
if_icmpge
public CodeBuilder if_icmpge(String labelName)
Appendsif_icmpge, branching when the first int is at least the second.- Parameters:
labelName- the target label, created on first use- Returns:
- this builder
-
if_icmpgt
public CodeBuilder if_icmpgt(String labelName)
Appendsif_icmpgt, branching when the first int is greater than the second.- Parameters:
labelName- the target label, created on first use- Returns:
- this builder
-
if_icmple
public CodeBuilder if_icmple(String labelName)
Appendsif_icmple, branching when the first int is at most the second.- Parameters:
labelName- the target label, created on first use- Returns:
- this builder
-
if_acmpeq
public CodeBuilder if_acmpeq(String labelName)
Appendsif_acmpeq, branching when the top two references are the same object.- Parameters:
labelName- the target label, created on first use- Returns:
- this builder
-
if_acmpne
public CodeBuilder if_acmpne(String labelName)
Appendsif_acmpne, branching when the top two references are different objects.- Parameters:
labelName- the target label, created on first use- Returns:
- this builder
-
ifnull
public CodeBuilder ifnull(String labelName)
Appendsifnull, branching when the top reference is null.- Parameters:
labelName- the target label, created on first use- Returns:
- this builder
-
ifnonnull
public CodeBuilder ifnonnull(String labelName)
Appendsifnonnull, branching when the top reference is non-null.- Parameters:
labelName- the target label, created on first use- Returns:
- this builder
-
goto_
public CodeBuilder goto_(String labelName)
Appends an unconditionalgoto.- Parameters:
labelName- the target label, created on first use- Returns:
- this builder
-
tableswitch
public CodeBuilder tableswitch(int low, int high, String defaultLabel, String... caseLabels)
Emits atableswitchover the contiguous keyslow..high.- Parameters:
low- the lowest keyhigh- the highest keydefaultLabel- label taken when the value is outside the key rangecaseLabels- target labels for the keys in ascending order- Returns:
- this builder
- Throws:
IllegalArgumentException- if high is below low, or the label count does not match the key range
-
lookupswitch
public CodeBuilder lookupswitch(String defaultLabel, Map<Integer,String> cases)
Emits alookupswitchmapping each key incasesto its target label, withdefaultLabelas the fallthrough.- Parameters:
defaultLabel- label taken when no key matchescases- key to target label mapping- Returns:
- this builder
-
invokevirtual
public CodeBuilder invokevirtual(String owner, String name, String descriptor)
Appendsinvokevirtual, adding the method reference to the constant pool.- Parameters:
owner- internal name of the declaring classname- the method namedescriptor- the method descriptor- Returns:
- this builder
-
invokespecial
public CodeBuilder invokespecial(String owner, String name, String descriptor)
Appendsinvokespecial, adding the method reference to the constant pool.- Parameters:
owner- internal name of the declaring classname- the method namedescriptor- the method descriptor- Returns:
- this builder
-
invokestatic
public CodeBuilder invokestatic(String owner, String name, String descriptor)
Appendsinvokestatic, adding the method reference to the constant pool.- Parameters:
owner- internal name of the declaring classname- the method namedescriptor- the method descriptor- Returns:
- this builder
-
invokeinterface
public CodeBuilder invokeinterface(String owner, String name, String descriptor)
Appendsinvokeinterface, adding the interface method reference and deriving the argument slot count from the descriptor.- Parameters:
owner- internal name of the declaring interfacename- the method namedescriptor- the method descriptor- Returns:
- this builder
-
invokedynamic
public CodeBuilder invokedynamic(String name, String descriptor, MethodHandle bootstrap, Object... bsmArgs)
Appendsinvokedynamic, registering the bootstrap method and its static arguments on the host class - the enclosing class builder, or theassemble(ClassFile)target when detached.- Parameters:
name- the call site namedescriptor- the call site descriptorbootstrap- handle of the bootstrap methodbsmArgs- static bootstrap arguments; String, Integer, Long, Float, Double, TypeDescriptor and MethodHandle are encoded, any other type is skipped- Returns:
- this builder
-
getfield
public CodeBuilder getfield(String owner, String name, String descriptor)
Appendsgetfield, adding the field reference to the constant pool.- Parameters:
owner- internal name of the declaring classname- the field namedescriptor- the field type descriptor- Returns:
- this builder
-
putfield
public CodeBuilder putfield(String owner, String name, String descriptor)
Appendsputfield, adding the field reference to the constant pool.- Parameters:
owner- internal name of the declaring classname- the field namedescriptor- the field type descriptor- Returns:
- this builder
-
getstatic
public CodeBuilder getstatic(String owner, String name, String descriptor)
Appendsgetstatic, adding the field reference to the constant pool.- Parameters:
owner- internal name of the declaring classname- the field namedescriptor- the field type descriptor- Returns:
- this builder
-
putstatic
public CodeBuilder putstatic(String owner, String name, String descriptor)
Appendsputstatic, adding the field reference to the constant pool.- Parameters:
owner- internal name of the declaring classname- the field namedescriptor- the field type descriptor- Returns:
- this builder
-
new_
public CodeBuilder new_(String type)
Appendsnew, adding the class reference to the constant pool.- Parameters:
type- internal name of the class to allocate- Returns:
- this builder
-
newarray
public CodeBuilder newarray(int arrayType)
Appendsnewarrayfor a primitive element type.- Parameters:
arrayType- the JVM array type code for the element- Returns:
- this builder
-
anewarray
public CodeBuilder anewarray(String type)
Appendsanewarrayfor a reference element type.- Parameters:
type- internal name of the element class- Returns:
- this builder
-
multianewarray
public CodeBuilder multianewarray(String descriptor, int dims)
Appendsmultianewarray, consuming one length from the stack per dimension.- Parameters:
descriptor- internal name of the array classdims- number of dimensions to allocate- Returns:
- this builder
-
checkcast
public CodeBuilder checkcast(String type)
Appendscheckcast.- Parameters:
type- internal name to cast the top reference to- Returns:
- this builder
-
instanceof_
public CodeBuilder instanceof_(String type)
Appendsinstanceof, replacing the top reference with 1 when it is of the given type.- Parameters:
type- internal name to test against- Returns:
- this builder
-
ireturn
public CodeBuilder ireturn()
Appendsireturn, returning an int.- Returns:
- this builder
-
lreturn
public CodeBuilder lreturn()
Appendslreturn, returning a long.- Returns:
- this builder
-
freturn
public CodeBuilder freturn()
Appendsfreturn, returning a float.- Returns:
- this builder
-
dreturn
public CodeBuilder dreturn()
Appendsdreturn, returning a double.- Returns:
- this builder
-
areturn
public CodeBuilder areturn()
Appendsareturn, returning a reference.- Returns:
- this builder
-
vreturn
public CodeBuilder vreturn()
Appendsreturn, returning from a void method.- Returns:
- this builder
-
athrow
public CodeBuilder athrow()
Appendsathrow, throwing the reference on top of the stack.- Returns:
- this builder
-
trycatch
public CodeBuilder trycatch(String startLabel, String endLabel, String handlerLabel, String exceptionType)
Records an exception-table region, resolved to offsets when the body is built or assembled.- Parameters:
startLabel- label at the first protected instructionendLabel- label just past the protected rangehandlerLabel- label at the handler entryexceptionType- internal name of the caught class, null to catch any throwable- Returns:
- this builder
-
nop
public CodeBuilder nop()
Appendsnop.- Returns:
- this builder
-
monitorenter
public CodeBuilder monitorenter()
Appendsmonitorenter, acquiring the monitor of the top reference.- Returns:
- this builder
-
monitorexit
public CodeBuilder monitorexit()
Appendsmonitorexit, releasing the monitor of the top reference.- Returns:
- this builder
-
iinc
public CodeBuilder iinc(int varIndex, int increment)
Appendsiinc, adding a constant to an int local in place.- Parameters:
varIndex- the local variable slotincrement- the signed amount to add- Returns:
- this builder
-
end
public MethodBuilder end()
Ends the body and hands back to the enclosing method builder.- Returns:
- the enclosing MethodBuilder, or null when this builder was created by
detached()
-
assemble
public CodeWriter.ClonedRange assemble(ClassFile target)
Materializes the recorded ops into a detached instruction snapshot resolved againsttarget's constant pool.- Parameters:
target- the class whose constant pool backs the snippet and which will host the result- Returns:
- the assembled snippet
-
-