Skip to content

Commit cb17b0f

Browse files
authored
Classfile api label resolver branch (openjdk#33)
* removed impl.LabelResolver * labelToBci moved to CodeAttribute and impl.LabelContext * removed labelToBci from CodeBuilder
1 parent de17a3b commit cb17b0f

18 files changed

+54
-100
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,6 @@ public sealed interface CodeBuilder
119119
* the current block will be the entire method body. */
120120
Label endLabel();
121121

122-
/** {@return the bytecode offset associated with the specified label} */
123-
int labelToBci(Label label);
124-
125122
/**
126123
* {@return the local variable slot associated with the receiver}.
127124
*

src/java.base/share/classes/jdk/classfile/CodeModel.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import jdk.classfile.attribute.CodeAttribute;
3232
import jdk.classfile.impl.BufferedCodeBuilder;
3333
import jdk.classfile.impl.CodeImpl;
34-
import jdk.classfile.impl.LabelResolver;
3534
import jdk.classfile.instruction.ExceptionCatch;
3635

3736
/**
@@ -40,7 +39,7 @@
4039
* #elements()}).
4140
*/
4241
public sealed interface CodeModel
43-
extends CompoundElement<CodeElement>, AttributedElement, MethodElement, LabelResolver
42+
extends CompoundElement<CodeElement>, AttributedElement, MethodElement
4443
permits CodeAttribute, BufferedCodeBuilder.Model, CodeImpl {
4544

4645
/**

src/java.base/share/classes/jdk/classfile/attribute/CodeAttribute.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import jdk.classfile.Attribute;
2929
import jdk.classfile.CodeModel;
30+
import jdk.classfile.Label;
3031
import jdk.classfile.impl.BoundAttribute;
3132

3233
/**
@@ -47,4 +48,6 @@ public sealed interface CodeAttribute extends Attribute<CodeAttribute>, CodeMode
4748
* {@return the bytes (bytecode) of the code array}
4849
*/
4950
byte[] codeArray();
51+
52+
int labelToBci(Label label);
5053
}

src/java.base/share/classes/jdk/classfile/impl/AbstractBoundLocalVariable.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@ public int slot() {
9393
return code.classReader.readU2(offset + 8);
9494
}
9595

96-
public void writeTo(BufWriter b, CodeBuilder builder) {
97-
int startBci = builder.labelToBci(startScope());
98-
int endBci = builder.labelToBci(endScope());
96+
public void writeTo(BufWriter b) {
97+
var lc = ((BufWriterImpl)b).labelContext();
98+
int startBci = lc.labelToBci(startScope());
99+
int endBci = lc.labelToBci(endScope());
99100
int length = endBci - startBci;
100101
b.writeU2(startBci);
101102
b.writeU2(length);

src/java.base/share/classes/jdk/classfile/impl/AbstractInstruction.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,9 +1487,10 @@ public Label endScope() {
14871487
return endScope;
14881488
}
14891489

1490-
public void writeTo(BufWriter b, CodeBuilder builder) {
1491-
int startBci = builder.labelToBci(startScope());
1492-
int endBci = builder.labelToBci(endScope());
1490+
public void writeTo(BufWriter b) {
1491+
var lc = ((BufWriterImpl)b).labelContext();
1492+
int startBci = lc.labelToBci(startScope());
1493+
int endBci = lc.labelToBci(endScope());
14931494
int length = endBci - startBci;
14941495
b.writeU2(startBci);
14951496
b.writeU2(length);

src/java.base/share/classes/jdk/classfile/impl/BufWriterImpl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
public final class BufWriterImpl implements BufWriter {
4343

4444
private final ConstantPoolBuilder constantPool;
45-
private LabelResolver labelResolver;
45+
private LabelContext labelContext;
4646
private ClassEntry thisClass;
4747
byte[] elems;
4848
int offset = 0;
@@ -61,12 +61,12 @@ public ConstantPoolBuilder constantPool() {
6161
return constantPool;
6262
}
6363

64-
public LabelResolver labelResolver() {
65-
return labelResolver;
64+
public LabelContext labelContext() {
65+
return labelContext;
6666
}
6767

68-
public void setLabelResolver(LabelResolver labelResolver) {
69-
this.labelResolver = labelResolver;
68+
public void setLabelContext(LabelContext labelContext) {
69+
this.labelContext = labelContext;
7070
}
7171
@Override
7272
public boolean canWriteDirect(ConstantPool other) {

src/java.base/share/classes/jdk/classfile/impl/BufferedCodeBuilder.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,6 @@ public Optional<MethodModel> parent() {
196196
return Optional.empty();
197197
}
198198

199-
@Override
200-
public int labelToBci(Label label) {
201-
throw new UnsupportedOperationException("nyi");
202-
}
203-
204199
@Override
205200
public void writeTo(DirectMethodBuilder builder) {
206201
builder.withCode(new Consumer<>() {

src/java.base/share/classes/jdk/classfile/impl/ClassPrinterImpl.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@
3636
import java.util.List;
3737
import java.util.Map;
3838
import java.util.Objects;
39-
import java.util.Optional;
4039
import java.util.function.BiConsumer;
4140
import java.util.Set;
4241
import java.util.function.Consumer;
43-
import java.util.stream.Collectors;
4442
import java.util.stream.IntStream;
4543
import java.util.stream.Stream;
4644
import jdk.classfile.Annotation;
@@ -50,8 +48,8 @@
5048
import jdk.classfile.AnnotationValue.*;
5149
import jdk.classfile.Attribute;
5250
import jdk.classfile.ClassModel;
53-
import jdk.classfile.CodeModel;
5451
import jdk.classfile.ClassPrinter.*;
52+
import jdk.classfile.CodeModel;
5553
import jdk.classfile.Instruction;
5654
import jdk.classfile.MethodModel;
5755
import jdk.classfile.TypeAnnotation;
@@ -512,7 +510,7 @@ private static Node elementValuePairsToTree(List<AnnotationElement> evps) {
512510
new MapNodeImpl(FLOW, "value").with(elementValueToTree(evp.value())))));
513511
}
514512

515-
private static Stream<ConstantDesc> convertVTIs(LabelResolver lr, List<StackMapTableAttribute.VerificationTypeInfo> vtis) {
513+
private static Stream<ConstantDesc> convertVTIs(CodeAttribute lr, List<StackMapTableAttribute.VerificationTypeInfo> vtis) {
516514
return vtis.stream().mapMulti((vti, ret) -> {
517515
switch (vti) {
518516
case SimpleVerificationTypeInfo s -> {
@@ -549,7 +547,7 @@ public static MapNode modelToTree(CompoundElement<?> model, Verbosity verbosity)
549547
case ClassModel cm -> classToTree(cm, verbosity);
550548
case FieldModel fm -> fieldToTree(fm, verbosity);
551549
case MethodModel mm -> methodToTree(mm, verbosity);
552-
case CodeModel com -> codeToTree((CodeImpl)com, verbosity);
550+
case CodeModel com -> codeToTree((CodeAttribute)com, verbosity);
553551
};
554552
}
555553

@@ -647,7 +645,7 @@ private static Node[] constantPoolToTree(ConstantPool cp, Verbosity verbosity) {
647645
}
648646
}
649647

650-
private static Node frameToTree(ConstantDesc name, LabelResolver lr, StackMapFrameInfo f) {
648+
private static Node frameToTree(ConstantDesc name, CodeAttribute lr, StackMapFrameInfo f) {
651649
return new MapNodeImpl(FLOW, name).with(
652650
list("locals", "item", convertVTIs(lr, f.locals())),
653651
list("stack", "item", convertVTIs(lr, f.stack())));
@@ -669,14 +667,14 @@ public static MapNode methodToTree(MethodModel m, Verbosity verbosity) {
669667
leaf("method type", m.methodType().stringValue()),
670668
list("attributes", "attribute", m.attributes().stream().map(Attribute::attributeName)))
671669
.with(attributesToTree(m.attributes(), verbosity))
672-
.with(codeToTree((CodeImpl)m.code().orElse(null), verbosity));
670+
.with(codeToTree((CodeAttribute)m.code().orElse(null), verbosity));
673671
}
674672

675-
private static MapNode codeToTree(CodeImpl com, Verbosity verbosity) {
673+
private static MapNode codeToTree(CodeAttribute com, Verbosity verbosity) {
676674
if (verbosity != Verbosity.MEMBERS_ONLY && com != null) {
677675
var codeNode = new MapNodeImpl(BLOCK, "code");
678-
codeNode.with(leaf("max stack", ((CodeAttribute)com).maxStack()));
679-
codeNode.with(leaf("max locals", ((CodeAttribute)com).maxLocals()));
676+
codeNode.with(leaf("max stack", com.maxStack()));
677+
codeNode.with(leaf("max locals", com.maxLocals()));
680678
codeNode.with(list("attributes", "attribute", com.attributes().stream().map(Attribute::attributeName)));
681679
var stackMap = new MapNodeImpl(BLOCK, "stack map frames");
682680
var visibleTypeAnnos = new LinkedHashMap<Integer, List<TypeAnnotation>>();
@@ -994,7 +992,7 @@ private static Node[] localInfoToTree(List<LocalVariableInfo> locals, int slot,
994992
return new Node[0];
995993
}
996994

997-
private static void forEachOffset(TypeAnnotation ta, LabelResolver lr, BiConsumer<Integer, TypeAnnotation> consumer) {
995+
private static void forEachOffset(TypeAnnotation ta, CodeAttribute lr, BiConsumer<Integer, TypeAnnotation> consumer) {
998996
switch (ta.targetInfo()) {
999997
case TypeAnnotation.OffsetTarget ot -> consumer.accept(lr.labelToBci(ot.target()), ta);
1000998
case TypeAnnotation.TypeArgumentTarget tat -> consumer.accept(lr.labelToBci(tat.target()), ta);

src/java.base/share/classes/jdk/classfile/impl/DirectCodeBuilder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public void writeBody(BufWriter b) {
238238
// @@@ Filter out LVs whose boundary labels are not defined?
239239
b.writeU2(localVariables.size());
240240
for (LocalVariable l : localVariables) {
241-
l.writeTo(b, DirectCodeBuilder.this);
241+
l.writeTo(b);
242242
}
243243
// @@@ If we're filtering, then also have to patch count
244244
}
@@ -254,7 +254,7 @@ public void writeBody(BufWriter b) {
254254
// @@@ Filter out LVs whose boundary labels are not defined?
255255
b.writeU2(localVariableTypes.size());
256256
for (LocalVariableType l : localVariableTypes) {
257-
l.writeTo(b, DirectCodeBuilder.this);
257+
l.writeTo(b);
258258
}
259259
// @@@ If we're filtering, then also have to patch count
260260
}
@@ -272,6 +272,7 @@ public void writeBody(BufWriter b) {
272272
@Override
273273
public void writeBody(BufWriter b) {
274274
BufWriterImpl buf = (BufWriterImpl) b;
275+
buf.setLabelContext(DirectCodeBuilder.this);
275276

276277
int codeLength = curPc();
277278
int maxStack, maxLocals;
@@ -304,14 +305,13 @@ else if (canReuseStackmaps) {
304305
}
305306
attributes.withAttribute(stackMapAttr);
306307

307-
buf.setLabelResolver(DirectCodeBuilder.this);
308308
buf.writeU2(maxStack);
309309
buf.writeU2(maxLocals);
310310
buf.writeInt(codeLength);
311311
buf.writeBytes(bytecodesBufWriter);
312312
writeExceptionHandlers(b);
313313
attributes.writeTo(b);
314-
buf.setLabelResolver(null);
314+
buf.setLabelContext(null);
315315
}
316316
};
317317
}

src/java.base/share/classes/jdk/classfile/impl/LabelContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@
2929
/**
3030
* LabelContext
3131
*/
32-
public sealed interface LabelContext extends LabelResolver
32+
public sealed interface LabelContext
3333
permits BufferedCodeBuilder, CodeImpl, DirectCodeBuilder {
3434
Label newLabel();
3535
Label getLabel(int bci);
3636
void setLabelTarget(Label label, int bci);
37+
int labelToBci(Label label);
3738
}

0 commit comments

Comments
 (0)