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.CLinker;
 28 import jdk.incubator.foreign.FunctionDescriptor;
 29 import jdk.incubator.foreign.MemoryAddress;
 30 import jdk.incubator.foreign.MemorySegment;
 31 import jdk.incubator.foreign.NativeSymbol;
 32 import jdk.incubator.foreign.ResourceScope;
 33 import jdk.incubator.foreign.VaList;
 34 import jdk.internal.foreign.abi.SharedUtils;
 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 implements CLinker {
 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     public static Windowsx64Linker getInstance() {
 59         if (instance == null) {
 60             instance = new Windowsx64Linker();
 61         }
 62         return instance;
 63     }
 64 
 65     public static VaList newVaList(Consumer<VaList.Builder> actions, ResourceScope scope) {
 66         WinVaList.Builder builder = WinVaList.builder(scope);
 67         actions.accept(builder);
 68         return builder.build();
 69     }
 70 
 71     @Override
 72     public final MethodHandle downcallHandle(FunctionDescriptor function) {
 73         Objects.requireNonNull(function);
 74         MethodType type = SharedUtils.inferMethodType(function, false);
 75         MethodHandle handle = CallArranger.arrangeDowncall(type, function);
 76         if (!type.returnType().equals(MemorySegment.class)) {
 77             // not returning segment, just insert a throwing allocator
 78             handle = MethodHandles.insertArguments(handle, 1, SharedUtils.THROWING_ALLOCATOR);
 79         }
 80         return SharedUtils.wrapDowncall(handle, function);
 81     }
 82 
 83     @Override
 84     public final NativeSymbol upcallStub(MethodHandle target, FunctionDescriptor function, ResourceScope scope) {
 85         Objects.requireNonNull(scope);
 86         Objects.requireNonNull(target);
 87         Objects.requireNonNull(function);
 88         SharedUtils.checkExceptions(target);
 89         MethodType type = SharedUtils.inferMethodType(function, true);
 90         if (!type.equals(target.type())) {
 91             throw new IllegalArgumentException("Wrong method handle type: " + target.type());
 92         }
 93         return CallArranger.arrangeUpcall(target, target.type(), function, scope);
 94     }
 95 
 96     public static VaList newVaListOfAddress(MemoryAddress ma, ResourceScope scope) {
 97         return WinVaList.ofAddress(ma, scope);
 98     }
 99 
100     public static VaList emptyVaList() {
101         return WinVaList.empty();
102     }
103 }