package org.graalvm.compiler.hotspot.meta;
import static jdk.vm.ci.hotspot.HotSpotCallingConventionType.JavaCall;
import static jdk.vm.ci.hotspot.HotSpotCallingConventionType.JavaCallee;
import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.RegisterEffect.DESTROYS_ALL_CALLER_SAVE_REGISTERS;
import static org.graalvm.compiler.hotspot.meta.HotSpotForeignCallDescriptor.Reexecutability.NOT_REEXECUTABLE;
import static org.graalvm.compiler.hotspot.meta.HotSpotForeignCallDescriptor.Reexecutability.REEXECUTABLE;
import static org.graalvm.compiler.hotspot.meta.HotSpotForeignCallDescriptor.Transition.LEAF_NO_VZERO;
import static org.graalvm.compiler.hotspot.meta.HotSpotForeignCallDescriptor.Transition.SAFEPOINT;
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.MARK_WORD_LOCATION;
import static jdk.internal.vm.compiler.word.LocationIdentity.any;
import java.util.ArrayList;
import java.util.List;
import jdk.internal.vm.compiler.collections.EconomicMap;
import org.graalvm.compiler.core.common.LIRKind;
import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
import org.graalvm.compiler.core.common.spi.ForeignCallSignature;
import org.graalvm.compiler.debug.GraalError;
import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage;
import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.RegisterEffect;
import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkageImpl;
import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
import org.graalvm.compiler.hotspot.meta.HotSpotForeignCallDescriptor.Reexecutability;
import org.graalvm.compiler.hotspot.meta.HotSpotForeignCallDescriptor.Transition;
import org.graalvm.compiler.hotspot.stubs.ForeignCallStub;
import org.graalvm.compiler.hotspot.stubs.Stub;
import org.graalvm.compiler.options.OptionValues;
import org.graalvm.compiler.word.Word;
import org.graalvm.compiler.word.WordTypes;
import jdk.internal.vm.compiler.word.LocationIdentity;
import jdk.vm.ci.code.CallingConvention;
import jdk.vm.ci.code.CodeCacheProvider;
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.MetaAccessProvider;
public abstract class HotSpotForeignCallsProviderImpl implements HotSpotForeignCallsProvider {
public static final LocationIdentity[] NO_LOCATIONS = {};
public static final HotSpotForeignCallDescriptor OSR_MIGRATION_END = new HotSpotForeignCallDescriptor(LEAF_NO_VZERO, NOT_REEXECUTABLE, NO_LOCATIONS, "OSR_migration_end", void.class, long.class);
public static final HotSpotForeignCallDescriptor IDENTITY_HASHCODE = new HotSpotForeignCallDescriptor(SAFEPOINT, NOT_REEXECUTABLE, MARK_WORD_LOCATION, "identity_hashcode", int.class,
Object.class);
public static final HotSpotForeignCallDescriptor VERIFY_OOP = new HotSpotForeignCallDescriptor(LEAF_NO_VZERO, REEXECUTABLE, NO_LOCATIONS, "verify_oop", Object.class,
Object.class);
public static final HotSpotForeignCallDescriptor LOAD_AND_CLEAR_EXCEPTION = new HotSpotForeignCallDescriptor(LEAF_NO_VZERO, NOT_REEXECUTABLE, any(), "load_and_clear_exception", Object.class,
Word.class);
public static final HotSpotForeignCallDescriptor TEST_DEOPTIMIZE_CALL_INT = new HotSpotForeignCallDescriptor(SAFEPOINT, REEXECUTABLE, any(), "test_deoptimize_call_int", int.class, int.class);
protected final HotSpotJVMCIRuntime jvmciRuntime;
protected final HotSpotGraalRuntimeProvider runtime;
protected final EconomicMap<ForeignCallSignature, HotSpotForeignCallLinkage> foreignCalls = EconomicMap.create();
protected final EconomicMap<ForeignCallSignature, HotSpotForeignCallDescriptor> signatureMap = EconomicMap.create();
protected final MetaAccessProvider metaAccess;
protected final CodeCacheProvider codeCache;
protected final WordTypes wordTypes;
public HotSpotForeignCallsProviderImpl(HotSpotJVMCIRuntime jvmciRuntime, HotSpotGraalRuntimeProvider runtime, MetaAccessProvider metaAccess, CodeCacheProvider codeCache,
WordTypes wordTypes) {
this.jvmciRuntime = jvmciRuntime;
this.runtime = runtime;
this.metaAccess = metaAccess;
this.codeCache = codeCache;
this.wordTypes = wordTypes;
}
public HotSpotForeignCallLinkage register(HotSpotForeignCallLinkage linkage) {
assert !foreignCalls.containsKey(linkage.getDescriptor().getSignature()) : "already registered linkage for " + linkage.getDescriptor();
foreignCalls.put(linkage.getDescriptor().getSignature(), linkage);
return linkage;
}
public HotSpotForeignCallLinkage registerStubCall(
HotSpotForeignCallDescriptor descriptor,
RegisterEffect effect) {
return register(HotSpotForeignCallLinkageImpl.create(metaAccess,
codeCache,
wordTypes,
this,
descriptor,
0L, effect,
JavaCall,
JavaCallee));
}
public HotSpotForeignCallLinkage registerStubCall(
ForeignCallSignature signature,
Transition transition,
Reexecutability reexecutability,
RegisterEffect effect,
LocationIdentity... killedLocations) {
HotSpotForeignCallDescriptor descriptor = new HotSpotForeignCallDescriptor(signature, transition, reexecutability, killedLocations);
signatureMap.put(signature, descriptor);
return registerStubCall(descriptor, effect);
}
public HotSpotForeignCallLinkage registerForeignCall(
HotSpotForeignCallDescriptor descriptor,
long address,
CallingConvention.Type outgoingCcType) {
if (address == 0) {
throw new IllegalArgumentException("address must be non-zero");
}
Class<?> resultType = descriptor.getResultType();
assert descriptor.getTransition() != SAFEPOINT || resultType.isPrimitive() || Word.class.isAssignableFrom(resultType) : "non-leaf foreign calls must return objects in thread local storage: " +
descriptor;
return register(HotSpotForeignCallLinkageImpl.create(metaAccess,
codeCache,
wordTypes,
this,
descriptor,
address,
DESTROYS_ALL_CALLER_SAVE_REGISTERS,
outgoingCcType,
null
));
}
public void linkForeignCall(OptionValues options,
HotSpotProviders providers,
HotSpotForeignCallDescriptor descriptor,
long address,
boolean prependThread) {
if (address == 0) {
throw new IllegalArgumentException("Can't link foreign call with zero address");
}
ForeignCallStub stub = new ForeignCallStub(options, jvmciRuntime, providers, address, descriptor, prependThread);
HotSpotForeignCallLinkage linkage = stub.getLinkage();
HotSpotForeignCallLinkage targetLinkage = stub.getTargetLinkage();
linkage.setCompiledStub(stub);
register(linkage);
register(targetLinkage);
}
public static final boolean PREPEND_THREAD = true;
public static final boolean DONT_PREPEND_THREAD = !PREPEND_THREAD;
@Override
public HotSpotForeignCallLinkage lookupForeignCall(ForeignCallDescriptor descriptor) {
assert foreignCalls != null : descriptor;
HotSpotForeignCallLinkage callTarget = foreignCalls.get(descriptor.getSignature());
if (callTarget == null) {
throw GraalError.shouldNotReachHere("missing implementation for runtime call: " + descriptor);
}
callTarget.finalizeAddress(runtime.getHostBackend());
return callTarget;
}
@Override
public HotSpotForeignCallDescriptor getDescriptor(ForeignCallSignature signature) {
HotSpotForeignCallDescriptor descriptor = signatureMap.get(signature);
assert descriptor != null : signature;
return descriptor;
}
HotSpotForeignCallDescriptor createDescriptor(ForeignCallSignature signature, Transition transition, Reexecutability reexecutability, LocationIdentity... killLocations) {
assert signatureMap.get(signature) == null;
HotSpotForeignCallDescriptor descriptor = new HotSpotForeignCallDescriptor(signature, transition, reexecutability, killLocations);
signatureMap.put(signature, descriptor);
return descriptor;
}
@Override
public LIRKind getValueKind(JavaKind javaKind) {
return LIRKind.fromJavaKind(codeCache.getTarget().arch, javaKind);
}
@Override
public List<Stub> getStubs() {
List<Stub> stubs = new ArrayList<>();
for (HotSpotForeignCallLinkage linkage : foreignCalls.getValues()) {
if (linkage.isCompiledStub()) {
Stub stub = linkage.getStub();
assert stub != null;
stubs.add(stub);
}
}
return stubs;
}
}