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.sysv;
 26 
 27 
 28 import jdk.incubator.foreign.CLinker;
 29 import jdk.incubator.foreign.FunctionDescriptor;
 30 import jdk.incubator.foreign.MemoryAddress;
 31 import jdk.incubator.foreign.MemorySegment;
 32 import jdk.incubator.foreign.NativeSymbol;
 33 import jdk.incubator.foreign.ResourceScope;
 34 import jdk.incubator.foreign.VaList;
 35 import jdk.internal.foreign.abi.SharedUtils;
 36 
 37 import java.lang.invoke.MethodHandle;
 38 import java.lang.invoke.MethodHandles;
 39 import java.lang.invoke.MethodType;
 40 import java.util.Objects;
 41 import java.util.function.Consumer;
 42 
 43 /**
 44  * ABI implementation based on System V ABI AMD64 supplement v.0.99.6
 45  */
 46 public final class SysVx64Linker implements CLinker {
 47     public static final int MAX_INTEGER_ARGUMENT_REGISTERS = 6;
 48     public static final int MAX_INTEGER_RETURN_REGISTERS = 2;
 49     public static final int MAX_VECTOR_ARGUMENT_REGISTERS = 8;
 50     public static final int MAX_VECTOR_RETURN_REGISTERS = 2;
 51     public static final int MAX_X87_RETURN_REGISTERS = 2;
 52 
 53     private static SysVx64Linker instance;
 54 
 55     static final long ADDRESS_SIZE = 64; // bits
 56 
 57     public static SysVx64Linker getInstance() {
 58         if (instance == null) {
 59             instance = new SysVx64Linker();
 60         }
 61         return instance;
 62     }
 63 
 64     public static VaList newVaList(Consumer<VaList.Builder> actions, ResourceScope scope) {
 65         SysVVaList.Builder builder = SysVVaList.builder(scope);
 66         actions.accept(builder);
 67         return builder.build();
 68     }
 69 
 70     @Override
 71     public final MethodHandle downcallHandle(FunctionDescriptor function) {
 72         Objects.requireNonNull(function);
 73         MethodType type = SharedUtils.inferMethodType(function, false);
 74         MethodHandle handle = CallArranger.arrangeDowncall(type, function);
 75         if (!type.returnType().equals(MemorySegment.class)) {
 76             // not returning segment, just insert a throwing allocator
 77             handle = MethodHandles.insertArguments(handle, 1, SharedUtils.THROWING_ALLOCATOR);
 78         }
 79         return SharedUtils.wrapDowncall(handle, function);
 80     }
 81 
 82     @Override
 83     public final NativeSymbol upcallStub(MethodHandle target, FunctionDescriptor function, ResourceScope scope) {
 84         Objects.requireNonNull(scope);
 85         Objects.requireNonNull(target);
 86         Objects.requireNonNull(function);
 87         SharedUtils.checkExceptions(target);
 88         MethodType type = SharedUtils.inferMethodType(function, true);
 89         if (!type.equals(target.type())) {
 90             throw new IllegalArgumentException("Wrong method handle type: " + target.type());
 91         }
 92         return CallArranger.arrangeUpcall(target, target.type(), function, scope);
 93     }
 94 
 95     public static VaList newVaListOfAddress(MemoryAddress ma, ResourceScope scope) {
 96         return SysVVaList.ofAddress(ma, scope);
 97     }
 98 
 99     public static VaList emptyVaList() {
100         return SysVVaList.empty();
101     }
102 }