Skip to content

Commit 0f03554

Browse files
committed
8342469: Improve API documentation for java.lang.classfile.instruction
Reviewed-by: asotona, darcy
1 parent 9bd70ec commit 0f03554

39 files changed

+2407
-441
lines changed

src/java.base/share/classes/java/lang/classfile/ClassFile.java

+23-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.lang.classfile.constantpool.ClassEntry;
3333
import java.lang.classfile.constantpool.ConstantPoolBuilder;
3434
import java.lang.classfile.constantpool.Utf8Entry;
35+
import java.lang.classfile.instruction.BranchInstruction;
36+
import java.lang.classfile.instruction.DiscontinuedInstruction;
3537
import java.lang.classfile.instruction.ExceptionCatch;
3638
import java.lang.constant.ClassDesc;
3739
import java.lang.reflect.AccessFlag;
@@ -230,17 +232,35 @@ enum LineNumbersOption implements Option {
230232
/**
231233
* Option describing whether to automatically rewrite short jumps to
232234
* long when necessary.
233-
* Default is {@code FIX_SHORT_JUMPS} to automatically rewrite jump
235+
* Default is {@link #FIX_SHORT_JUMPS} to automatically rewrite jump
234236
* instructions.
237+
* <p>
238+
* Due to physical restrictions, some types of instructions cannot encode
239+
* certain jump targets with bci offsets less than -32768 or greater than
240+
* 32767, as they use a {@code s2} to encode such an offset. (The maximum
241+
* length of the {@code code} array is 65535.) These types of instructions
242+
* are called "short jumps".
235243
*
244+
* @see BranchInstruction
245+
* @see DiscontinuedInstruction.JsrInstruction
236246
* @since 24
237247
*/
238248
enum ShortJumpsOption implements Option {
239249

240-
/** Automatically convert short jumps to long when necessary */
250+
/**
251+
* Automatically convert short jumps to long when necessary.
252+
* <p>
253+
* For an invalid instruction model, a {@link CodeBuilder} may generate
254+
* another or a few other instructions to accomplish the same effect.
255+
*/
241256
FIX_SHORT_JUMPS,
242257

243-
/** Fail if short jump overflows */
258+
/**
259+
* Fail with an {@link IllegalArgumentException} if short jump overflows.
260+
* <p>
261+
* This is useful to ensure the physical accuracy of a generated {@code
262+
* class} file.
263+
*/
244264
FAIL_ON_SHORT_JUMPS
245265
}
246266

src/java.base/share/classes/java/lang/classfile/CodeBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
* #with(ClassFileElement)} or concretely by calling the various {@code withXxx}
5252
* methods.
5353
*
54-
* <h2>Instruction Factories</h2>
54+
* <h2 id="instruction-factories">Instruction Factories</h2>
5555
* {@code CodeBuilder} provides convenience methods to create instructions (See
5656
* JVMS {@jvms 6.5} Instructions) by their mnemonic, taking necessary operands.
5757
* <ul>

src/java.base/share/classes/java/lang/classfile/Instruction.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,20 @@
2525

2626
package java.lang.classfile;
2727

28+
import java.lang.classfile.attribute.CodeAttribute;
2829
import java.lang.classfile.instruction.*;
2930

3031
import jdk.internal.classfile.impl.AbstractInstruction;
3132

3233
/**
33-
* Models an executable instruction in a method body.
34+
* Models an executable instruction in the {@code code} array of the {@link
35+
* CodeAttribute Code} attribute of a method.
36+
* <p>
37+
* The {@link #opcode() opcode} identifies the operation of an instruction.
38+
* Each {@linkplain Opcode#kind() kind} of opcode has its own modeling interface
39+
* for instructions.
3440
*
41+
* @sealedGraph
3542
* @since 24
3643
*/
3744
public sealed interface Instruction extends CodeElement
@@ -46,12 +53,14 @@ public sealed interface Instruction extends CodeElement
4653
ThrowInstruction, TypeCheckInstruction, AbstractInstruction {
4754

4855
/**
49-
* {@return the opcode of this instruction}
56+
* {@return the operation of this instruction}
5057
*/
5158
Opcode opcode();
5259

5360
/**
5461
* {@return the size in bytes of this instruction}
62+
* This value is equal to {@link Opcode#sizeIfFixed()
63+
* opcode().sizeIfFixed()} if it is not {@code -1}.
5564
*/
5665
int sizeInBytes();
5766
}

src/java.base/share/classes/java/lang/classfile/Label.java

+49-10
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,60 @@
2424
*/
2525
package java.lang.classfile;
2626

27+
import java.lang.classfile.attribute.CodeAttribute;
28+
import java.lang.classfile.instruction.LabelTarget;
29+
import java.util.ListIterator;
30+
2731
import jdk.internal.classfile.impl.LabelImpl;
2832

2933
/**
3034
* A marker for a position within the instructions of a method body. The
31-
* association between a label's identity and the position it represents is
32-
* managed by the entity managing the method body (a {@link CodeModel} or {@link
33-
* CodeBuilder}), not the label itself; this allows the same label to have a
34-
* meaning both in an existing method (as managed by a {@linkplain CodeModel})
35-
* and in the transformation of that method (as managed by a {@linkplain
36-
* CodeBuilder}), while corresponding to different positions in each. When
37-
* traversing the elements of a {@linkplain CodeModel}, {@linkplain Label}
38-
* markers will be delivered at the position to which they correspond. A label
39-
* can be bound to the current position within a {@linkplain CodeBuilder} via
40-
* {@link CodeBuilder#labelBinding(Label)} or {@link CodeBuilder#with(ClassFileElement)}.
35+
* position is a cursor position in the list of instructions, similar to that
36+
* of a {@link ListIterator}.
37+
*
38+
* <h2 id="reading">Reading Labels</h2>
39+
* Labels read from {@code class} files represent positions in the {@code code}
40+
* array of a {@link CodeAttribute Code} attribute. It is associated with a
41+
* <dfn>{@index bci}</dfn> (bytecode index), also known as <dfn>{@index pc}</dfn>
42+
* (program counter), the index into the {@code code} array; the actual cursor
43+
* position is immediately before the given index, so a label at the beginning
44+
* of the instructions has bci {@code 0}, and a label at the end of the
45+
* instructions has bci {@link CodeAttribute#codeLength codeLength() + 1}. The
46+
* bci can be inspected through {@link CodeAttribute#labelToBci
47+
* CodeAttribute::labelToBci}.
48+
* <p>
49+
* In generic {@link CodeModel}s, a label may not have a bci value; the position
50+
* of a label can be found by searching for the corresponding {@link LabelTarget}
51+
* within that model.
52+
*
53+
* <h2 id="writing">Writing Labels</h2>
54+
* Many models in {@link java.lang.classfile} refer to labels. To write a
55+
* label, a label must be obtained, it must be bound to a {@link CodeBuilder}.
56+
* <p>
57+
* To obtain a label:
58+
* <ul>
59+
* <li>Use a label read from other models.
60+
* <li>Use pre-defined labels from a {@link CodeBuilder}, such as {@link
61+
* CodeBuilder#startLabel() CodeBuilder::startLabel}, {@link CodeBuilder#endLabel
62+
* CodeBuilder::endLabel}, or {@link CodeBuilder.BlockCodeBuilder#breakLabel
63+
* BlockCodeBuilder::breakLabel}. They are already bound.
64+
* <li>Create labels with {@link CodeBuilder#newLabel CodeBuilder::newLabel} or
65+
* {@link CodeBuilder#newBoundLabel CodeBuilder::newBoundLabel}.
66+
* </ul>
67+
* <p>
68+
* A label must be bound exactly once in the {@code CodeBuilder} where it is
69+
* used; otherwise, writing fails. To bind an unbound label:
70+
* <ul>
71+
* <li>Send a read {@link LabelTarget} to a {@code CodeBuilder}.
72+
* <li>Use {@link CodeBuilder#labelBinding CodeBuilder::labelBinding}.
73+
* </ul>
74+
* Note that a label read from another model is not automatically bound in a
75+
* {@code CodeBuilder}; they are separate entities and the label is bound to
76+
* different positions in them.
4177
*
78+
* @see CodeAttribute#labelToBci CodeAttribute::labelToBci
79+
* @see CodeBuilder#newLabel CodeBuilder::newLabel
80+
* @see CodeBuilder#labelBinding CodeBuilder::labelBinding
4281
* @since 24
4382
*/
4483
public sealed interface Label

0 commit comments

Comments
 (0)