package jdk.internal.foreign.abi.x64.windows;
import jdk.incubator.foreign.Addressable;
import jdk.incubator.foreign.FunctionDescriptor;
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemoryLayout;
import jdk.incubator.foreign.MemorySegment;
import jdk.incubator.foreign.CLinker;
import jdk.internal.foreign.abi.SharedUtils;
import jdk.internal.foreign.abi.UpcallStubs;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Objects;
import java.util.function.Consumer;
import static jdk.internal.foreign.PlatformLayouts.*;
public class Windowsx64Linker implements CLinker {
public static final int MAX_INTEGER_ARGUMENT_REGISTERS = 4;
public static final int MAX_INTEGER_RETURN_REGISTERS = 1;
public static final int MAX_VECTOR_ARGUMENT_REGISTERS = 4;
public static final int MAX_VECTOR_RETURN_REGISTERS = 1;
public static final int MAX_REGISTER_ARGUMENTS = 4;
public static final int MAX_REGISTER_RETURNS = 1;
private static Windowsx64Linker instance;
static final long ADDRESS_SIZE = 64;
private static final MethodHandle MH_unboxVaList;
private static final MethodHandle MH_boxVaList;
static {
try {
MethodHandles.Lookup lookup = MethodHandles.lookup();
MH_unboxVaList = lookup.findVirtual(VaList.class, "address",
MethodType.methodType(MemoryAddress.class));
MH_boxVaList = lookup.findStatic(Windowsx64Linker.class, "newVaListOfAddress",
MethodType.methodType(VaList.class, MemoryAddress.class));
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
}
public static Windowsx64Linker getInstance() {
if (instance == null) {
instance = new Windowsx64Linker();
}
return instance;
}
public static VaList newVaList(Consumer<VaList.Builder> actions, SharedUtils.Allocator allocator) {
WinVaList.Builder builder = WinVaList.builder(allocator);
actions.accept(builder);
return builder.build();
}
@Override
public MethodHandle downcallHandle(Addressable symbol, MethodType type, FunctionDescriptor function) {
Objects.requireNonNull(symbol);
Objects.requireNonNull(type);
Objects.requireNonNull(function);
MethodType llMt = SharedUtils.convertVaListCarriers(type, WinVaList.CARRIER);
MethodHandle handle = CallArranger.arrangeDowncall(symbol, llMt, function);
handle = SharedUtils.unboxVaLists(type, handle, MH_unboxVaList);
return handle;
}
@Override
public MemorySegment upcallStub(MethodHandle target, FunctionDescriptor function) {
Objects.requireNonNull(target);
Objects.requireNonNull(function);
target = SharedUtils.boxVaLists(target, MH_boxVaList);
return UpcallStubs.upcallAddress(CallArranger.arrangeUpcall(target, target.type(), function));
}
public static VaList newVaListOfAddress(MemoryAddress ma) {
return WinVaList.ofAddress(ma);
}
public static VaList emptyVaList() {
return WinVaList.empty();
}
}