package org.graalvm.compiler.hotspot.meta;
import java.util.function.Supplier;
import org.graalvm.compiler.core.common.type.ObjectStamp;
import org.graalvm.compiler.core.common.type.Stamp;
import org.graalvm.compiler.core.common.type.StampFactory;
import org.graalvm.compiler.hotspot.nodes.aot.InitializeKlassNode;
import org.graalvm.compiler.hotspot.nodes.aot.ResolveConstantNode;
import org.graalvm.compiler.nodes.ConstantNode;
import org.graalvm.compiler.nodes.DeoptimizingFixedWithNextNode;
import org.graalvm.compiler.nodes.FrameState;
import org.graalvm.compiler.nodes.ValueNode;
import org.graalvm.compiler.nodes.graphbuilderconf.ClassInitializationPlugin;
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
import jdk.vm.ci.hotspot.HotSpotConstantPool;
import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
import jdk.vm.ci.meta.ConstantPool;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;
public final class HotSpotAOTClassInitializationPlugin implements ClassInitializationPlugin {
private static boolean shouldApply(GraphBuilderContext builder, ResolvedJavaType type) {
if (!builder.parsingIntrinsic()) {
if (!type.isArray()) {
ResolvedJavaMethod method = builder.getGraph().method();
ResolvedJavaType methodHolder = method.getDeclaringClass();
return !type.isAssignableFrom(methodHolder) || type.isInterface();
} else if (!type.getComponentType().isPrimitive()) {
return true;
}
}
return false;
}
@Override
public boolean apply(GraphBuilderContext builder, ResolvedJavaType type, Supplier<FrameState> frameState, ValueNode[] classInit) {
if (shouldApply(builder, type)) {
Stamp hubStamp = builder.getStampProvider().createHubStamp((ObjectStamp) StampFactory.objectNonNull());
ConstantNode hub = builder.append(ConstantNode.forConstant(hubStamp, ((HotSpotResolvedObjectType) type).klass(), builder.getMetaAccess(), builder.getGraph()));
DeoptimizingFixedWithNextNode result = builder.append(type.isArray() ? new ResolveConstantNode(hub) : new InitializeKlassNode(hub));
result.setStateBefore(frameState.get());
if (classInit != null) {
classInit[0] = result;
}
return true;
}
return false;
}
@Override
public boolean supportsLazyInitialization(ConstantPool cp) {
return true;
}
@Override
public void loadReferencedType(GraphBuilderContext builder, ConstantPool cp, int cpi, int opcode) {
((HotSpotConstantPool) cp).loadReferencedType(cpi, opcode, false);
}
}