1 /*
  2  * Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package jdk.internal.foreign.abi.x64.windows;
 26 
 27 import jdk.incubator.foreign.FunctionDescriptor;
 28 import jdk.incubator.foreign.MemoryAddress;
 29 import jdk.incubator.foreign.MemorySegment;
 30 import jdk.incubator.foreign.ResourceScope;
 31 import jdk.internal.foreign.AbstractCLinker;
 32 import jdk.internal.foreign.ResourceScopeImpl;
 33 import jdk.internal.foreign.abi.SharedUtils;
 34 import jdk.internal.foreign.abi.UpcallStubs;
 35 
 36 import java.lang.invoke.MethodHandle;
 37 import java.lang.invoke.MethodHandles;
 38 import java.lang.invoke.MethodType;
 39 import java.util.Objects;
 40 import java.util.function.Consumer;
 41 
 42 /**
 43  * ABI implementation based on Windows ABI AMD64 supplement v.0.99.6
 44  */
 45 public final class Windowsx64Linker extends AbstractCLinker {
 46 
 47     public static final int MAX_INTEGER_ARGUMENT_REGISTERS = 4;
 48     public static final int MAX_INTEGER_RETURN_REGISTERS = 1;
 49     public static final int MAX_VECTOR_ARGUMENT_REGISTERS = 4;
 50     public static final int MAX_VECTOR_RETURN_REGISTERS = 1;
 51     public static final int MAX_REGISTER_ARGUMENTS = 4;
 52     public static final int MAX_REGISTER_RETURNS = 1;
 53 
 54     private static Windowsx64Linker instance;
 55 
 56     static final long ADDRESS_SIZE = 64; // bits
 57 
 58     private static final MethodHandle MH_unboxVaList;
 59     private static final MethodHandle MH_boxVaList;
 60 
 61     static {
 62         try {
 63             MethodHandles.Lookup lookup = MethodHandles.lookup();
 64             MH_unboxVaList = lookup.findVirtual(VaList.class, "address",
 65                 MethodType.methodType(MemoryAddress.class));
 66             MH_boxVaList = MethodHandles.insertArguments(lookup.findStatic(Windowsx64Linker.class, "newVaListOfAddress",
 67                 MethodType.methodType(VaList.class, MemoryAddress.class, ResourceScope.class)), 1, ResourceScope.globalScope());
 68         } catch (ReflectiveOperationException e) {
 69             throw new ExceptionInInitializerError(e);
 70         }
 71     }
 72 
 73     public static Windowsx64Linker getInstance() {
 74         if (instance == null) {
 75             instance = new Windowsx64Linker();
 76         }
 77         return instance;
 78     }
 79 
 80     public static VaList newVaList(Consumer<VaList.Builder> actions, ResourceScope scope) {
 81         WinVaList.Builder builder = WinVaList.builder(scope);
 82         actions.accept(builder);
 83         return builder.build();
 84     }
 85 
 86     @Override
 87     public final MethodHandle downcallHandle(MethodType type, FunctionDescriptor function) {
 88         Objects.requireNonNull(type);
 89         Objects.requireNonNull(function);
 90         MethodType llMt = SharedUtils.convertVaListCarriers(type, WinVaList.CARRIER);
 91         MethodHandle handle = CallArranger.arrangeDowncall(llMt, function);
 92         if (!type.returnType().equals(MemorySegment.class)) {
 93             // not returning segment, just insert a throwing allocator
 94             handle = MethodHandles.insertArguments(handle, 1, SharedUtils.THROWING_ALLOCATOR);
 95         }
 96         handle = SharedUtils.unboxVaLists(type, handle, MH_unboxVaList);
 97         return handle;
 98     }
 99 
100     @Override
101     public final MemoryAddress upcallStub(MethodHandle target, FunctionDescriptor function, ResourceScope scope) {
102         Objects.requireNonNull(scope);
103         Objects.requireNonNull(target);
104         Objects.requireNonNull(function);
105         target = SharedUtils.boxVaLists(target, MH_boxVaList);
106         return UpcallStubs.upcallAddress(CallArranger.arrangeUpcall(target, target.type(), function), (ResourceScopeImpl) scope);
107     }
108 
109     public static VaList newVaListOfAddress(MemoryAddress ma, ResourceScope scope) {
110         return WinVaList.ofAddress(ma, scope);
111     }
112 
113     public static VaList emptyVaList() {
114         return WinVaList.empty();
115     }
116 }