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