1 /*
  2  * Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2019, 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.linux;
 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 import jdk.internal.foreign.abi.aarch64.CallArranger;
 37 
 38 import java.lang.invoke.MethodHandle;
 39 import java.lang.invoke.MethodHandles;
 40 import java.lang.invoke.MethodType;
 41 import java.util.Objects;
 42 import java.util.function.Consumer;
 43 
 44 /**
 45  * ABI implementation based on ARM document "Procedure Call Standard for
 46  * the ARM 64-bit Architecture".
 47  */
 48 public final class LinuxAArch64Linker extends AbstractCLinker {
 49     private static LinuxAArch64Linker instance;
 50 
 51     static final long ADDRESS_SIZE = 64; // bits
 52 
 53     private static final MethodHandle MH_unboxVaList;
 54     private static final MethodHandle MH_boxVaList;
 55 
 56     static {
 57         try {
 58             MethodHandles.Lookup lookup = MethodHandles.lookup();
 59             MH_unboxVaList = lookup.findVirtual(VaList.class, "address",
 60                 MethodType.methodType(MemoryAddress.class));
 61             MH_boxVaList = MethodHandles.insertArguments(lookup.findStatic(LinuxAArch64Linker.class, "newVaListOfAddress",
 62                 MethodType.methodType(VaList.class, MemoryAddress.class, ResourceScope.class)), 1, ResourceScope.globalScope());
 63         } catch (ReflectiveOperationException e) {
 64             throw new ExceptionInInitializerError(e);
 65         }
 66     }
 67 
 68     public static LinuxAArch64Linker getInstance() {
 69         if (instance == null) {
 70             instance = new LinuxAArch64Linker();
 71         }
 72         return instance;
 73     }
 74 
 75     @Override
 76     public final MethodHandle downcallHandle(MethodType type, FunctionDescriptor function) {
 77         Objects.requireNonNull(type);
 78         Objects.requireNonNull(function);
 79         MethodType llMt = SharedUtils.convertVaListCarriers(type, LinuxAArch64VaList.CARRIER);
 80         MethodHandle handle = CallArranger.arrangeDowncall(llMt, function);
 81         if (!type.returnType().equals(MemorySegment.class)) {
 82             // not returning segment, just insert a throwing allocator
 83             handle = MethodHandles.insertArguments(handle, 1, SharedUtils.THROWING_ALLOCATOR);
 84         }
 85         handle = SharedUtils.unboxVaLists(type, handle, MH_unboxVaList);
 86         return handle;
 87     }
 88 
 89     @Override
 90     public final MemoryAddress upcallStub(MethodHandle target, FunctionDescriptor function, ResourceScope scope) {
 91         Objects.requireNonNull(scope);
 92         Objects.requireNonNull(target);
 93         Objects.requireNonNull(function);
 94         target = SharedUtils.boxVaLists(target, MH_boxVaList);
 95         return UpcallStubs.upcallAddress(CallArranger.arrangeUpcall(target, target.type(), function), (ResourceScopeImpl) scope);
 96     }
 97 
 98     public static VaList newVaList(Consumer<VaList.Builder> actions, ResourceScope scope) {
 99         LinuxAArch64VaList.Builder builder = LinuxAArch64VaList.builder(scope);
100         actions.accept(builder);
101         return builder.build();
102     }
103 
104     public static VaList newVaListOfAddress(MemoryAddress ma, ResourceScope scope) {
105         return LinuxAArch64VaList.ofAddress(ma, scope);
106     }
107 
108     public static VaList emptyVaList() {
109         return LinuxAArch64VaList.empty();
110     }
111 
112 }