Skip to content

[GR-62356] Replace ASM with ClassFile API #10758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions compiler/mx.compiler/suite.py
Original file line number Diff line number Diff line change
@@ -294,7 +294,7 @@
"truffle:TRUFFLE_DSL_PROCESSOR"
],
"checkstyle" : "jdk.graal.compiler",
"javaCompliance" : "21+",
"javaCompliance" : "23+",
"jacoco" : "exclude",
"graalCompilerSourceEdition": "ignore",
},
@@ -335,8 +335,8 @@
},
"jacoco" : "exclude",
"checkstyle": "jdk.graal.compiler",
"javaCompliance" : "21+",
"javaPreviewNeeded": "21+",
"javaCompliance" : "23+",
"javaPreviewNeeded": "23+",
"workingSets" : "Graal,HotSpot,Test",
"graalCompilerSourceEdition": "ignore",
},
@@ -372,7 +372,7 @@
"sourceDirs" : ["src"],
"dependencies" : ["mx:JMH_1_21", "jdk.graal.compiler.microbenchmarks"],
"checkstyle" : "jdk.graal.compiler",
"javaCompliance" : "21+",
"javaCompliance" : "23+",
"annotationProcessors" : ["mx:JMH_1_21"],
"spotbugsIgnoresGenerated" : True,
"workingSets" : "Graal,Bench",
@@ -395,7 +395,7 @@
],
},
"checkstyle" : "jdk.graal.compiler",
"javaCompliance" : "21+",
"javaCompliance" : "23+",
"checkPackagePrefix" : "false",
"annotationProcessors" : ["mx:JMH_1_21"],
"spotbugsIgnoresGenerated" : True,
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -24,39 +24,38 @@
*/
package jdk.graal.compiler.core.test;

import static java.lang.classfile.ClassFile.ACC_PUBLIC;
import static java.lang.classfile.ClassFile.ACC_STATIC;

import java.io.File;
import java.io.IOException;
import java.lang.constant.ClassDesc;
import java.nio.file.Files;
import java.util.function.Function;

import org.objectweb.asm.Opcodes;
public interface CustomizedBytecodePattern {

public abstract class CustomizedBytecodePatternTest extends GraalCompilerTest implements Opcodes {
class CachedLoader extends ClassLoader {

protected Class<?> getClass(String className) throws ClassNotFoundException {
return new CachedLoader(CustomizedBytecodePatternTest.class.getClassLoader(), className, this::generateClass).findClass(className);
}
private static final File GENERATED_CLASS_FILE_OUTPUT_DIRECTORY;

private static final File GENERATED_CLASS_FILE_OUTPUT_DIRECTORY;
static {
String prop = System.getProperty("save.generated.classfile.dir");
File file = null;
if (prop != null) {
file = new File(prop);
ensureDirectoryExists(file);
assert file.exists() : file;
static {
String prop = System.getProperty("save.generated.classfile.dir");
File file = null;
if (prop != null) {
file = new File(prop);
ensureDirectoryExists(file);
assert file.exists() : file;
}
GENERATED_CLASS_FILE_OUTPUT_DIRECTORY = file;
}
GENERATED_CLASS_FILE_OUTPUT_DIRECTORY = file;
}

private static File ensureDirectoryExists(File file) {
if (!file.exists()) {
file.mkdirs();
private static File ensureDirectoryExists(File file) {
if (!file.exists()) {
file.mkdirs();
}
return file;
}
return file;
}

public static class CachedLoader extends ClassLoader {

final String className;
Class<?> loaded;
@@ -72,7 +71,7 @@ public CachedLoader(ClassLoader parent, String className, Function<String, byte[
public Class<?> findClass(String name) throws ClassNotFoundException {
if (name.equals(className)) {
if (loaded == null) {
byte[] classfileBytes = classfileSupplier.apply(name.replace('.', '/'));
byte[] classfileBytes = classfileSupplier.apply(name);
if (GENERATED_CLASS_FILE_OUTPUT_DIRECTORY != null) {
try {
File classfile = new File(GENERATED_CLASS_FILE_OUTPUT_DIRECTORY, name.replace('.', File.separatorChar) + ".class");
@@ -90,8 +89,17 @@ public Class<?> findClass(String name) throws ClassNotFoundException {
return super.findClass(name);
}
}
}

int ACC_PUBLIC_STATIC = ACC_PUBLIC | ACC_STATIC;

default ClassDesc cd(Class<?> klass) {
return klass.describeConstable().orElseThrow();
}

default Class<?> getClass(String className) throws ClassNotFoundException {
return new CachedLoader(CustomizedBytecodePattern.class.getClassLoader(), className, this::generateClass).findClass(className);
}

protected abstract byte[] generateClass(String internalClassName);
byte[] generateClass(String className);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -24,6 +24,15 @@
*/
package jdk.graal.compiler.core.test;

import java.lang.classfile.ClassFile;
import java.lang.classfile.CodeBuilder;
import java.lang.classfile.TypeKind;
import java.lang.constant.ClassDesc;
import java.lang.constant.ConstantDescs;
import java.lang.constant.DirectMethodHandleDesc.Kind;
import java.lang.constant.DynamicConstantDesc;
import java.lang.constant.MethodHandleDesc;
import java.lang.constant.MethodTypeDesc;
import java.lang.invoke.MethodHandles;
import java.util.Arrays;
import java.util.LinkedHashMap;
@@ -32,11 +41,6 @@

import org.junit.Assume;
import org.junit.Test;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.ConstantDynamic;
import org.objectweb.asm.Handle;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Type;

import jdk.vm.ci.meta.ConstantPool;
import jdk.vm.ci.meta.ResolvedJavaMethod;
@@ -47,13 +51,7 @@
* @see "https://openjdk.java.net/jeps/309"
* @see "https://bugs.openjdk.java.net/browse/JDK-8177279"
*/
public class DynamicConstantTest extends CustomizedBytecodePatternTest {

private static final int PUBLIC_STATIC = ACC_PUBLIC | ACC_STATIC;

static final String testClassInternalName = DynamicConstantTest.class.getName().replace('.', '/');
static final String constantBootstrapsClassInternalName = "java/lang/invoke/ConstantBootstraps";

public class DynamicConstantTest extends GraalCompilerTest {
/**
* Map of test class generators keyed by internal class name.
*/
@@ -88,11 +86,11 @@ enum CondyType {
* Generates a class with a static {@code run} method that returns a value loaded from
* CONSTANT_Dynamic constant pool entry.
*/
static class TestGenerator {
static class TestGenerator implements CustomizedBytecodePattern {
/**
* Type of value returned by the generated {@code run} method.
*/
final Type type;
final ClassDesc type;

/**
* Type of condy used to produce the returned value.
@@ -112,58 +110,43 @@ static class TestGenerator {

TestGenerator(Class<?> type, CondyType condyType) {
String typeName = type.getSimpleName();
this.type = Type.getType(type);
this.type = cd(type);
this.condyType = condyType;
this.getter = "get" + typeName.substring(0, 1).toUpperCase() + typeName.substring(1);
this.className = DynamicConstantTest.class.getName() + "$" + typeName + '_' + condyType;
}

void generate(ClassWriter cw) {
@Override
public byte[] generateClass(String className) {
return ClassFile.of().build(ClassDesc.of(className), classBuilder -> classBuilder
.withMethodBody("run", MethodTypeDesc.of(type), ACC_PUBLIC_STATIC, this::generate));
}

private void generate(CodeBuilder b) {
// @formatter:off
// Object ConstantBootstraps.invoke(MethodHandles.Lookup lookup, String name, Class<?> type, MethodHandle handle, Object... args)
// @formatter:on
String invokeSig = "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;Ljava/lang/invoke/MethodHandle;[Ljava/lang/Object;)Ljava/lang/Object;";
Handle invokeHandle = new Handle(H_INVOKESTATIC, constantBootstrapsClassInternalName, "invoke", invokeSig, false);
ClassDesc outerClass = cd(DynamicConstantTest.class);
String desc = type.descriptorString();

String desc = type.getDescriptor();
if (condyType == CondyType.CALL_DIRECT_BSM) {
// Example: int DynamicConstantTest.getIntBSM(MethodHandles.Lookup l, String name,
// Class<?> type)
String sig = "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;)" + desc;
Handle handle = new Handle(H_INVOKESTATIC, testClassInternalName, getter + "BSM", sig, false);

ConstantDynamic condy = new ConstantDynamic("const", desc, handle);
MethodVisitor run = cw.visitMethod(PUBLIC_STATIC, "run", "()" + desc, null, null);
run.visitLdcInsn(condy);
run.visitInsn(type.getOpcode(IRETURN));
run.visitMaxs(0, 0);
run.visitEnd();
var condy = DynamicConstantDesc.ofNamed(MethodHandleDesc.of(Kind.STATIC, outerClass, getter + "BSM", sig), "consnt", type);
b.ldc(condy).return_(TypeKind.from(type));
} else if (condyType == CondyType.CALL_INDIRECT_BSM) {
// Example: int DynamicConstantTest.getInt()
Handle handle = new Handle(H_INVOKESTATIC, testClassInternalName, getter, "()" + desc, false);

ConstantDynamic condy = new ConstantDynamic("const", desc, invokeHandle, handle);
MethodVisitor run = cw.visitMethod(PUBLIC_STATIC, "run", "()" + desc, null, null);
run.visitLdcInsn(condy);
run.visitInsn(type.getOpcode(IRETURN));
run.visitMaxs(0, 0);
run.visitEnd();
var condy = DynamicConstantDesc.ofNamed(ConstantDescs.BSM_INVOKE, "consnt", type, MethodHandleDesc.of(Kind.STATIC, outerClass, getter, "()" + desc));
b.ldc(condy).return_(TypeKind.from(type));
} else {
assert condyType == CondyType.CALL_INDIRECT_WITH_ARGS_BSM;
// Example: int DynamicConstantTest.getInt()
Handle handle1 = new Handle(H_INVOKESTATIC, testClassInternalName, getter, "()" + desc, false);

var condy1 = DynamicConstantDesc.ofNamed(ConstantDescs.BSM_INVOKE, "consnt1", type, MethodHandleDesc.of(Kind.STATIC, outerClass, getter, "()" + desc));
// Example: int DynamicConstantTest.getInt(int v1, int v2)
Handle handle2 = new Handle(H_INVOKESTATIC, testClassInternalName, getter, "(" + desc + desc + ")" + desc, false);

ConstantDynamic condy1 = new ConstantDynamic("const1", desc, invokeHandle, handle1);
ConstantDynamic condy2 = new ConstantDynamic("const2", desc, invokeHandle, handle2, condy1, condy1);

MethodVisitor run = cw.visitMethod(PUBLIC_STATIC, "run", "()" + desc, null, null);
run.visitLdcInsn(condy2);
run.visitInsn(type.getOpcode(IRETURN));
run.visitMaxs(0, 0);
run.visitEnd();
var condy2 = DynamicConstantDesc.ofNamed(ConstantDescs.BSM_INVOKE, "consnt2", type, MethodHandleDesc.of(Kind.STATIC, outerClass, getter, "(" + desc + desc + ")" + desc), condy1,
condy1);
b.ldc(condy2).return_(TypeKind.from(type));
}
}
}
@@ -226,21 +209,12 @@ public DynamicConstantTest() {

private static final boolean VERBOSE = Boolean.getBoolean(DynamicConstantTest.class.getSimpleName() + ".verbose");

@Override
protected byte[] generateClass(String internalClassName) {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
cw.visit(55, ACC_SUPER | ACC_PUBLIC, internalClassName, null, "java/lang/Object", null);
generators.get(internalClassName).generate(cw);
cw.visitEnd();
return cw.toByteArray();
}

@SuppressWarnings("try")
@Test
public void test() throws Throwable {
boolean jvmciCompatibilityChecked = false;
for (TestGenerator e : generators.values()) {
Class<?> testClass = getClass(e.className);
Class<?> testClass = e.getClass(e.className);
ResolvedJavaMethod run = getResolvedJavaMethod(testClass, "run");
if (!jvmciCompatibilityChecked) {
checkJVMCICompatibility(run);
Loading
Loading