mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8323058: Revisit j.l.classfile.CodeBuilder API surface
Reviewed-by: briangoetz, psandoz
This commit is contained in:
parent
286cbf831c
commit
ae82405ff7
53 changed files with 725 additions and 876 deletions
File diff suppressed because it is too large
Load diff
|
@ -228,7 +228,7 @@
|
|||
* instruction invoking {@code println} could have been generated with {@link
|
||||
* java.lang.classfile.CodeBuilder#invokevirtual(java.lang.constant.ClassDesc,
|
||||
* java.lang.String, java.lang.constant.MethodTypeDesc) CodeBuilder.invokevirtual}, {@link
|
||||
* java.lang.classfile.CodeBuilder#invokeInstruction(java.lang.classfile.Opcode,
|
||||
* java.lang.classfile.CodeBuilder#invoke(java.lang.classfile.Opcode,
|
||||
* java.lang.constant.ClassDesc, java.lang.String, java.lang.constant.MethodTypeDesc,
|
||||
* boolean) CodeBuilder.invokeInstruction}, or {@link
|
||||
* java.lang.classfile.CodeBuilder#with(java.lang.classfile.ClassFileElement)
|
||||
|
|
|
@ -213,7 +213,7 @@ class PackageSnippets {
|
|||
if (e instanceof InvokeInstruction i
|
||||
&& i.owner().asInternalName().equals("Foo")
|
||||
&& i.opcode() == Opcode.INVOKESTATIC)
|
||||
b.invokeInstruction(i.opcode(), CD_Bar, i.name().stringValue(), i.typeSymbol(), i.isInterface());
|
||||
b.invoke(i.opcode(), CD_Bar, i.name().stringValue(), i.typeSymbol(), i.isInterface());
|
||||
else b.with(e);
|
||||
};
|
||||
// @end
|
||||
|
@ -327,7 +327,7 @@ class PackageSnippets {
|
|||
for (CodeElement e : xm) {
|
||||
if (e instanceof InvokeInstruction i && i.owner().asInternalName().equals("Foo")
|
||||
&& i.opcode() == Opcode.INVOKESTATIC)
|
||||
codeBuilder.invokeInstruction(i.opcode(), CD_Bar,
|
||||
codeBuilder.invoke(i.opcode(), CD_Bar,
|
||||
i.name().stringValue(), i.typeSymbol(), i.isInterface());
|
||||
else codeBuilder.with(e);
|
||||
}});
|
||||
|
|
|
@ -382,7 +382,7 @@ public class MethodHandleProxies {
|
|||
|
||||
// <clinit>
|
||||
clb.withMethodBody(CLASS_INIT_NAME, MTD_void, ACC_STATIC, cob -> {
|
||||
cob.constantInstruction(ifaceDesc);
|
||||
cob.loadConstant(ifaceDesc);
|
||||
cob.putstatic(proxyDesc, TYPE_NAME, CD_Class);
|
||||
cob.return_();
|
||||
});
|
||||
|
@ -406,7 +406,7 @@ public class MethodHandleProxies {
|
|||
// this.m<i> = callerBoundTarget.asType(xxType);
|
||||
cob.aload(0);
|
||||
cob.aload(3);
|
||||
cob.constantInstruction(mi.desc);
|
||||
cob.loadConstant(mi.desc);
|
||||
cob.invokevirtual(CD_MethodHandle, "asType", MTD_MethodHandle_MethodType);
|
||||
cob.putfield(proxyDesc, mi.fieldName, CD_MethodHandle);
|
||||
}
|
||||
|
@ -423,12 +423,12 @@ public class MethodHandleProxies {
|
|||
// check lookupClass
|
||||
cob.aload(0);
|
||||
cob.invokevirtual(CD_MethodHandles_Lookup, "lookupClass", MTD_Class);
|
||||
cob.constantInstruction(proxyDesc);
|
||||
cob.loadConstant(proxyDesc);
|
||||
cob.if_acmpne(failLabel);
|
||||
// check original access
|
||||
cob.aload(0);
|
||||
cob.invokevirtual(CD_MethodHandles_Lookup, "lookupModes", MTD_int);
|
||||
cob.constantInstruction(Lookup.ORIGINAL);
|
||||
cob.loadConstant(Lookup.ORIGINAL);
|
||||
cob.iand();
|
||||
cob.ifeq(failLabel);
|
||||
// success
|
||||
|
@ -452,11 +452,11 @@ public class MethodHandleProxies {
|
|||
bcb.aload(0);
|
||||
bcb.getfield(proxyDesc, mi.fieldName, CD_MethodHandle);
|
||||
for (int j = 0; j < mi.desc.parameterCount(); j++) {
|
||||
bcb.loadInstruction(TypeKind.from(mi.desc.parameterType(j)),
|
||||
bcb.loadLocal(TypeKind.from(mi.desc.parameterType(j)),
|
||||
bcb.parameterSlot(j));
|
||||
}
|
||||
bcb.invokevirtual(CD_MethodHandle, "invokeExact", mi.desc);
|
||||
bcb.returnInstruction(TypeKind.from(mi.desc.returnType()));
|
||||
bcb.return_(TypeKind.from(mi.desc.returnType()));
|
||||
}, ctb -> ctb
|
||||
// catch (Error | RuntimeException | Declared ex) { throw ex; }
|
||||
.catchingMulti(mi.thrown, CodeBuilder::athrow)
|
||||
|
|
|
@ -1124,7 +1124,7 @@ public final class StringConcatFactory {
|
|||
}
|
||||
}
|
||||
len += args.parameterCount() * ARGUMENT_SIZE_FACTOR;
|
||||
cb.constantInstruction(len);
|
||||
cb.loadConstant(len);
|
||||
cb.invokespecial(STRING_BUILDER, "<init>", INT_CONSTRUCTOR_TYPE);
|
||||
|
||||
// At this point, we have a blank StringBuilder on stack, fill it in with .append calls.
|
||||
|
@ -1137,7 +1137,7 @@ public final class StringConcatFactory {
|
|||
}
|
||||
Class<?> cl = args.parameterType(c);
|
||||
TypeKind kind = TypeKind.from(cl);
|
||||
cb.loadInstruction(kind, off);
|
||||
cb.loadLocal(kind, off);
|
||||
off += kind.slotSize();
|
||||
MethodTypeDesc desc = getSBAppendDesc(cl);
|
||||
cb.invokevirtual(STRING_BUILDER, "append", desc);
|
||||
|
|
|
@ -689,7 +689,7 @@ final class ProxyGenerator {
|
|||
.block(blockBuilder -> blockBuilder
|
||||
.aload(cob.parameterSlot(0))
|
||||
.invokevirtual(CD_MethodHandles_Lookup, "lookupClass", MTD_Class)
|
||||
.constantInstruction(Opcode.LDC, CD_Proxy)
|
||||
.ldc(CD_Proxy)
|
||||
.if_acmpne(blockBuilder.breakLabel())
|
||||
.aload(cob.parameterSlot(0))
|
||||
.invokevirtual(CD_MethodHandles_Lookup, "hasFullPrivilegeAccess", MTD_boolean)
|
||||
|
@ -763,11 +763,11 @@ final class ProxyGenerator {
|
|||
|
||||
if (parameterTypes.length > 0) {
|
||||
// Create an array and fill with the parameters converting primitives to wrappers
|
||||
cob.constantInstruction(parameterTypes.length)
|
||||
cob.loadConstant(parameterTypes.length)
|
||||
.anewarray(CE_Object);
|
||||
for (int i = 0; i < parameterTypes.length; i++) {
|
||||
cob.dup()
|
||||
.constantInstruction(i);
|
||||
.loadConstant(i);
|
||||
codeWrapArgument(cob, parameterTypes[i], cob.parameterSlot(i));
|
||||
cob.aastore();
|
||||
}
|
||||
|
@ -811,7 +811,7 @@ final class ProxyGenerator {
|
|||
*/
|
||||
private void codeWrapArgument(CodeBuilder cob, Class<?> type, int slot) {
|
||||
if (type.isPrimitive()) {
|
||||
cob.loadInstruction(TypeKind.from(type).asLoadable(), slot);
|
||||
cob.loadLocal(TypeKind.from(type).asLoadable(), slot);
|
||||
PrimitiveTypeInfo prim = PrimitiveTypeInfo.get(type);
|
||||
cob.invokestatic(prim.wrapperMethodRef);
|
||||
} else {
|
||||
|
@ -830,7 +830,7 @@ final class ProxyGenerator {
|
|||
|
||||
cob.checkcast(prim.wrapperClass)
|
||||
.invokevirtual(prim.unwrapMethodRef)
|
||||
.returnInstruction(TypeKind.from(type).asLoadable());
|
||||
.return_(TypeKind.from(type).asLoadable());
|
||||
} else {
|
||||
cob.checkcast(toClassDesc(type))
|
||||
.areturn();
|
||||
|
@ -847,13 +847,13 @@ final class ProxyGenerator {
|
|||
codeClassForName(cob, fromClass);
|
||||
|
||||
cob.ldc(method.getName())
|
||||
.constantInstruction(parameterTypes.length)
|
||||
.loadConstant(parameterTypes.length)
|
||||
.anewarray(CE_Class);
|
||||
|
||||
// Construct an array with the parameter types mapping primitives to Wrapper types
|
||||
for (int i = 0; i < parameterTypes.length; i++) {
|
||||
cob.dup()
|
||||
.constantInstruction(i);
|
||||
.loadConstant(i);
|
||||
if (parameterTypes[i].isPrimitive()) {
|
||||
PrimitiveTypeInfo prim = PrimitiveTypeInfo.get(parameterTypes[i]);
|
||||
cob.getstatic(prim.typeFieldRef);
|
||||
|
|
|
@ -414,7 +414,7 @@ public class SwitchBootstraps {
|
|||
cb.ireturn();
|
||||
cb.labelBinding(nonNullLabel);
|
||||
if (labelConstants.length == 0) {
|
||||
cb.constantInstruction(0)
|
||||
cb.loadConstant(0)
|
||||
.ireturn();
|
||||
return;
|
||||
}
|
||||
|
@ -454,7 +454,7 @@ public class SwitchBootstraps {
|
|||
// Object o = ...
|
||||
// o instanceof Wrapped(float)
|
||||
cb.aload(SELECTOR_OBJ);
|
||||
cb.instanceof_(Wrapper.forBasicType(classLabel)
|
||||
cb.instanceOf(Wrapper.forBasicType(classLabel)
|
||||
.wrapperType()
|
||||
.describeConstable()
|
||||
.orElseThrow());
|
||||
|
@ -464,7 +464,7 @@ public class SwitchBootstraps {
|
|||
// o instanceof float
|
||||
Label notNumber = cb.newLabel();
|
||||
cb.aload(SELECTOR_OBJ);
|
||||
cb.instanceof_(ConstantDescs.CD_Number);
|
||||
cb.instanceOf(ConstantDescs.CD_Number);
|
||||
if (selectorType == long.class || selectorType == float.class || selectorType == double.class ||
|
||||
selectorType == Long.class || selectorType == Float.class || selectorType == Double.class) {
|
||||
cb.ifeq(next);
|
||||
|
@ -493,7 +493,7 @@ public class SwitchBootstraps {
|
|||
cb.goto_(compare);
|
||||
cb.labelBinding(notNumber);
|
||||
cb.aload(SELECTOR_OBJ);
|
||||
cb.instanceof_(ConstantDescs.CD_Character);
|
||||
cb.instanceOf(ConstantDescs.CD_Character);
|
||||
cb.ifeq(next);
|
||||
cb.aload(SELECTOR_OBJ);
|
||||
cb.checkcast(ConstantDescs.CD_Character);
|
||||
|
@ -514,11 +514,11 @@ public class SwitchBootstraps {
|
|||
Optional<ClassDesc> classLabelConstableOpt = classLabel.describeConstable();
|
||||
if (classLabelConstableOpt.isPresent()) {
|
||||
cb.aload(SELECTOR_OBJ);
|
||||
cb.instanceof_(classLabelConstableOpt.orElseThrow());
|
||||
cb.instanceOf(classLabelConstableOpt.orElseThrow());
|
||||
cb.ifeq(next);
|
||||
} else {
|
||||
cb.aload(EXTRA_CLASS_LABELS);
|
||||
cb.constantInstruction(extraClassLabels.size());
|
||||
cb.loadConstant(extraClassLabels.size());
|
||||
cb.invokeinterface(ConstantDescs.CD_List,
|
||||
"get",
|
||||
MethodTypeDesc.of(ConstantDescs.CD_Object,
|
||||
|
@ -537,7 +537,7 @@ public class SwitchBootstraps {
|
|||
int enumIdx = enumDescs.size();
|
||||
enumDescs.add(enumLabel);
|
||||
cb.aload(ENUM_CACHE);
|
||||
cb.constantInstruction(enumIdx);
|
||||
cb.loadConstant(enumIdx);
|
||||
cb.invokestatic(ConstantDescs.CD_Integer,
|
||||
"valueOf",
|
||||
MethodTypeDesc.of(ConstantDescs.CD_Integer,
|
||||
|
@ -561,7 +561,7 @@ public class SwitchBootstraps {
|
|||
Label compare = cb.newLabel();
|
||||
Label notNumber = cb.newLabel();
|
||||
cb.aload(SELECTOR_OBJ);
|
||||
cb.instanceof_(ConstantDescs.CD_Number);
|
||||
cb.instanceOf(ConstantDescs.CD_Number);
|
||||
cb.ifeq(notNumber);
|
||||
cb.aload(SELECTOR_OBJ);
|
||||
cb.checkcast(ConstantDescs.CD_Number);
|
||||
|
@ -571,7 +571,7 @@ public class SwitchBootstraps {
|
|||
cb.goto_(compare);
|
||||
cb.labelBinding(notNumber);
|
||||
cb.aload(SELECTOR_OBJ);
|
||||
cb.instanceof_(ConstantDescs.CD_Character);
|
||||
cb.instanceOf(ConstantDescs.CD_Character);
|
||||
cb.ifeq(next);
|
||||
cb.aload(SELECTOR_OBJ);
|
||||
cb.checkcast(ConstantDescs.CD_Character);
|
||||
|
@ -587,9 +587,9 @@ public class SwitchBootstraps {
|
|||
element.caseLabel() instanceof Double ||
|
||||
element.caseLabel() instanceof Boolean)) {
|
||||
if (element.caseLabel() instanceof Boolean c) {
|
||||
cb.constantInstruction(c ? 1 : 0);
|
||||
cb.loadConstant(c ? 1 : 0);
|
||||
} else {
|
||||
cb.constantInstruction((ConstantDesc) element.caseLabel());
|
||||
cb.loadConstant((ConstantDesc) element.caseLabel());
|
||||
}
|
||||
cb.invokestatic(element.caseLabel().getClass().describeConstable().orElseThrow(),
|
||||
"valueOf",
|
||||
|
@ -605,11 +605,11 @@ public class SwitchBootstraps {
|
|||
throw new InternalError("Unsupported label type: " +
|
||||
element.caseLabel().getClass());
|
||||
}
|
||||
cb.constantInstruction(idx);
|
||||
cb.loadConstant(idx);
|
||||
cb.ireturn();
|
||||
}
|
||||
cb.labelBinding(dflt);
|
||||
cb.constantInstruction(cases.size());
|
||||
cb.loadConstant(cases.size());
|
||||
cb.ireturn();
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue