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.FunctionDescriptor;
 29 import jdk.incubator.foreign.MemoryAddress;
 30 import jdk.incubator.foreign.MemorySegment;
 31 import jdk.incubator.foreign.ResourceScope;
 32 import jdk.internal.foreign.AbstractCLinker;
 33 import jdk.internal.foreign.ResourceScopeImpl;
 34 import jdk.internal.foreign.abi.SharedUtils;
 35 import jdk.internal.foreign.abi.UpcallStubs;
 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 extends AbstractCLinker {
 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     private static final MethodHandle MH_unboxVaList;
 58     private static final MethodHandle MH_boxVaList;
 59 
 60     static {
 61         try {
 62             MethodHandles.Lookup lookup = MethodHandles.lookup();
 63             MH_unboxVaList = lookup.findVirtual(VaList.class, "address",
 64                 MethodType.methodType(MemoryAddress.class));
 65             MH_boxVaList = MethodHandles.insertArguments(lookup.findStatic(SysVx64Linker.class, "newVaListOfAddress",
 66                 MethodType.methodType(VaList.class, MemoryAddress.class, ResourceScope.class)), 1, ResourceScope.globalScope());
 67         } catch (ReflectiveOperationException e) {
 68             throw new ExceptionInInitializerError(e);
 69         }
 70     }
 71 
 72     public static SysVx64Linker getInstance() {
 73         if (instance == null) {
 74             instance = new SysVx64Linker();
 75         }
 76         return instance;
 77     }
 78 
 79     public static VaList newVaList(Consumer<VaList.Builder> actions, ResourceScope scope) {
 80         SysVVaList.Builder builder = SysVVaList.builder(scope);
 81         actions.accept(builder);
 82         return builder.build();
 83     }
 84 
 85     @Override
 86     public final MethodHandle downcallHandle(MethodType type, FunctionDescriptor function) {
 87         Objects.requireNonNull(type);
 88         Objects.requireNonNull(function);
 89         MethodType llMt = SharedUtils.convertVaListCarriers(type, SysVVaList.CARRIER);
 90         MethodHandle handle = CallArranger.arrangeDowncall(llMt, function);
 91         if (!type.returnType().equals(MemorySegment.class)) {
 92             // not returning segment, just insert a throwing allocator
 93             handle = MethodHandles.insertArguments(handle, 1, SharedUtils.THROWING_ALLOCATOR);
 94         }
 95         handle = SharedUtils.unboxVaLists(type, handle, MH_unboxVaList);
 96         return handle;
 97     }
 98 
 99     @Override
100     public final MemoryAddress upcallStub(MethodHandle target, FunctionDescriptor function, ResourceScope scope) {
101         Objects.requireNonNull(scope);
102         Objects.requireNonNull(target);
103         Objects.requireNonNull(function);
104         target = SharedUtils.boxVaLists(target, MH_boxVaList);
105         return UpcallStubs.upcallAddress(CallArranger.arrangeUpcall(target, target.type(), function), (ResourceScopeImpl) scope);
106     }
107 
108     public static VaList newVaListOfAddress(MemoryAddress ma, ResourceScope scope) {
109         return SysVVaList.ofAddress(ma, scope);
110     }
111 
112     public static VaList emptyVaList() {
113         return SysVVaList.empty();
114     }
115 }