package org.graalvm.compiler.hotspot;
import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime;
import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.RegisterEffect.DESTROYS_REGISTERS;
import jdk.internal.vm.compiler.collections.EconomicSet;
import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
import org.graalvm.compiler.core.target.Backend;
import org.graalvm.compiler.hotspot.meta.HotSpotForeignCallsProvider;
import org.graalvm.compiler.hotspot.stubs.Stub;
import org.graalvm.compiler.word.WordTypes;
import jdk.internal.vm.compiler.word.LocationIdentity;
import jdk.vm.ci.code.CallingConvention;
import jdk.vm.ci.code.CallingConvention.Type;
import jdk.vm.ci.code.CodeCacheProvider;
import jdk.vm.ci.code.InstalledCode;
import jdk.vm.ci.code.Register;
import jdk.vm.ci.code.RegisterConfig;
import jdk.vm.ci.code.ValueKindFactory;
import jdk.vm.ci.hotspot.HotSpotCallingConventionType;
import jdk.vm.ci.hotspot.HotSpotForeignCallTarget;
import jdk.vm.ci.meta.AllocatableValue;
import jdk.vm.ci.meta.JavaType;
import jdk.vm.ci.meta.MetaAccessProvider;
import jdk.vm.ci.meta.ResolvedJavaType;
import jdk.vm.ci.meta.Value;
public class HotSpotForeignCallLinkageImpl extends HotSpotForeignCallTarget implements HotSpotForeignCallLinkage {
protected final ForeignCallDescriptor descriptor;
private Stub stub;
private final CallingConvention outgoingCallingConvention;
private final CallingConvention incomingCallingConvention;
private final RegisterEffect effect;
private final Transition transition;
private Value[] temporaries = AllocatableValue.NONE;
private final LocationIdentity[] killedLocations;
private final boolean reexecutable;
public static HotSpotForeignCallLinkage create(MetaAccessProvider metaAccess, CodeCacheProvider codeCache, WordTypes wordTypes, HotSpotForeignCallsProvider foreignCalls,
ForeignCallDescriptor descriptor, long address, RegisterEffect effect, Type outgoingCcType, Type incomingCcType, Transition transition, boolean reexecutable,
LocationIdentity... killedLocations) {
CallingConvention outgoingCc = createCallingConvention(metaAccess, codeCache, wordTypes, foreignCalls, descriptor, outgoingCcType);
CallingConvention incomingCc = incomingCcType == null ? null : createCallingConvention(metaAccess, codeCache, wordTypes, foreignCalls, descriptor, incomingCcType);
HotSpotForeignCallLinkageImpl linkage = new HotSpotForeignCallLinkageImpl(descriptor, address, effect, transition, outgoingCc, incomingCc, reexecutable, killedLocations);
if (outgoingCcType == HotSpotCallingConventionType.NativeCall) {
linkage.temporaries = foreignCalls.getNativeABICallerSaveRegisters();
}
return linkage;
}
public static CallingConvention createCallingConvention(MetaAccessProvider metaAccess, CodeCacheProvider codeCache, WordTypes wordTypes, ValueKindFactory<?> valueKindFactory,
ForeignCallDescriptor descriptor, Type ccType) {
assert ccType != null;
Class<?>[] argumentTypes = descriptor.getArgumentTypes();
JavaType[] parameterTypes = new JavaType[argumentTypes.length];
for (int i = 0; i < parameterTypes.length; ++i) {
parameterTypes[i] = asJavaType(argumentTypes[i], metaAccess, wordTypes);
}
JavaType returnType = asJavaType(descriptor.getResultType(), metaAccess, wordTypes);
RegisterConfig regConfig = codeCache.getRegisterConfig();
return regConfig.getCallingConvention(ccType, returnType, parameterTypes, valueKindFactory);
}
private static JavaType asJavaType(Class<?> type, MetaAccessProvider metaAccess, WordTypes wordTypes) {
ResolvedJavaType javaType = metaAccess.lookupJavaType(type);
if (wordTypes.isWord(javaType)) {
javaType = metaAccess.lookupJavaType(wordTypes.getWordKind().toJavaClass());
}
return javaType;
}
public HotSpotForeignCallLinkageImpl(ForeignCallDescriptor descriptor, long address, RegisterEffect effect, Transition transition, CallingConvention outgoingCallingConvention,
CallingConvention incomingCallingConvention, boolean reexecutable, LocationIdentity... killedLocations) {
super(address);
this.descriptor = descriptor;
this.address = address;
this.effect = effect;
this.transition = transition;
assert outgoingCallingConvention != null : "only incomingCallingConvention can be null";
this.outgoingCallingConvention = outgoingCallingConvention;
this.incomingCallingConvention = incomingCallingConvention != null ? incomingCallingConvention : outgoingCallingConvention;
this.reexecutable = reexecutable;
this.killedLocations = killedLocations;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(stub == null ? descriptor.toString() : stub.toString());
sb.append("@0x").append(Long.toHexString(address)).append(':').append(outgoingCallingConvention).append(":").append(incomingCallingConvention);
if (temporaries != null && temporaries.length != 0) {
sb.append("; temps=");
String sep = "";
for (Value op : temporaries) {
sb.append(sep).append(op);
sep = ",";
}
}
return sb.toString();
}
@Override
public boolean isReexecutable() {
return reexecutable;
}
@Override
public boolean isGuaranteedSafepoint() {
return transition == Transition.SAFEPOINT;
}
@Override
public LocationIdentity[] getKilledLocations() {
return killedLocations;
}
@Override
public CallingConvention getOutgoingCallingConvention() {
return outgoingCallingConvention;
}
@Override
public CallingConvention getIncomingCallingConvention() {
return incomingCallingConvention;
}
@Override
public Value[] getTemporaries() {
if (temporaries.length == 0) {
return temporaries;
}
return temporaries.clone();
}
@Override
public long getMaxCallTargetOffset() {
return runtime().getHostJVMCIBackend().getCodeCache().getMaxCallTargetOffset(address);
}
@Override
public ForeignCallDescriptor getDescriptor() {
return descriptor;
}
@Override
public void setCompiledStub(Stub stub) {
assert address == 0L : "cannot set stub for linkage that already has an address: " + this;
this.stub = stub;
}
@Override
public boolean isCompiledStub() {
return address == 0L || stub != null;
}
@Override
public Stub getStub() {
assert checkStubCondition();
return stub;
}
private boolean checkStubCondition() {
assert stub != null : "linkage without an address must be a stub - forgot to register a Stub associated with " + descriptor + "?";
return true;
}
@Override
public void finalizeAddress(Backend backend) {
if (address == 0) {
assert checkStubCondition();
InstalledCode code = stub.getCode(backend);
EconomicSet<Register> destroyedRegisters = stub.getDestroyedCallerRegisters();
if (!destroyedRegisters.isEmpty()) {
AllocatableValue[] temporaryLocations = new AllocatableValue[destroyedRegisters.size()];
int i = 0;
for (Register reg : destroyedRegisters) {
temporaryLocations[i++] = reg.asValue();
}
temporaries = temporaryLocations;
}
address = code.getStart();
}
}
@Override
public long getAddress() {
assert address != 0L : "address not yet finalized: " + this;
return address;
}
@Override
public boolean destroysRegisters() {
return effect == DESTROYS_REGISTERS;
}
@Override
public boolean needsDebugInfo() {
return transition == Transition.SAFEPOINT;
}
@Override
public boolean mayContainFP() {
return transition != Transition.LEAF_NOFP;
}
@Override
public boolean needsJavaFrameAnchor() {
if (transition == Transition.SAFEPOINT || transition == Transition.STACK_INSPECTABLE_LEAF) {
if (stub != null) {
return false;
} else {
return true;
}
}
return false;
}
@Override
public String getSymbol() {
return stub == null ? null : stub.toString();
}
}