package org.graalvm.compiler.hotspot;
import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime;
import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.RegisterEffect.DESTROYS_ALL_CALLER_SAVE_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.HotSpotForeignCallDescriptor;
import org.graalvm.compiler.hotspot.meta.HotSpotForeignCallsProvider;
import org.graalvm.compiler.hotspot.stubs.Stub;
import org.graalvm.compiler.word.WordTypes;
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 HotSpotForeignCallDescriptor descriptor;
private Stub stub;
private final CallingConvention outgoingCallingConvention;
private final CallingConvention incomingCallingConvention;
private final RegisterEffect effect;
private Value[] temporaries = AllocatableValue.NONE;
public static HotSpotForeignCallLinkage create(MetaAccessProvider metaAccess, CodeCacheProvider codeCache, WordTypes wordTypes, HotSpotForeignCallsProvider foreignCalls,
HotSpotForeignCallDescriptor descriptor, long address, RegisterEffect effect, Type outgoingCcType, Type incomingCcType) {
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, outgoingCc, incomingCc);
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(HotSpotForeignCallDescriptor descriptor, long address, RegisterEffect effect,
CallingConvention outgoingCallingConvention, CallingConvention incomingCallingConvention) {
super(address);
this.descriptor = descriptor;
this.address = address;
this.effect = effect;
assert outgoingCallingConvention != null : "only incomingCallingConvention can be null";
this.outgoingCallingConvention = outgoingCallingConvention;
this.incomingCallingConvention = incomingCallingConvention != null ? incomingCallingConvention : outgoingCallingConvention;
}
@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 RegisterEffect getEffect() {
return effect;
}
@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 HotSpotForeignCallDescriptor 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_ALL_CALLER_SAVE_REGISTERS;
}
@Override
public boolean needsDebugInfo() {
return descriptor.canDeoptimize();
}
@Override
public boolean mayContainFP() {
return descriptor.getTransition() != HotSpotForeignCallDescriptor.Transition.LEAF_NO_VZERO;
}
@Override
public boolean needsJavaFrameAnchor() {
if (descriptor.getTransition() == HotSpotForeignCallDescriptor.Transition.SAFEPOINT || descriptor.getTransition() == HotSpotForeignCallDescriptor.Transition.STACK_INSPECTABLE_LEAF) {
if (stub != null) {
return false;
} else {
return true;
}
}
return false;
}
@Override
public String getSymbol() {
return stub == null ? null : stub.toString();
}
@Override
public boolean needsClearUpperVectorRegisters() {
return isCompiledStub() && mayContainFP();
}
}