< prev index next >

src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/abi/aarch64/macos/MacOsAArch64Linker.java

Print this page

  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 }

  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.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 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 for macOS on Apple silicon. Based on AAPCS with
 46  * changes to va_list and passing arguments on the stack.
 47  */
 48 public final class MacOsAArch64Linker implements CLinker {
 49     private static MacOsAArch64Linker instance;
 50 
 51     static final long ADDRESS_SIZE = 64; // bits
 52 















 53     public static MacOsAArch64Linker getInstance() {
 54         if (instance == null) {
 55             instance = new MacOsAArch64Linker();
 56         }
 57         return instance;
 58     }
 59 
 60     @Override
 61     public final MethodHandle downcallHandle(FunctionDescriptor function) {

 62         Objects.requireNonNull(function);
 63         MethodType type = SharedUtils.inferMethodType(function, false);
 64         MethodHandle handle = CallArranger.MACOS.arrangeDowncall(type, function);
 65         if (!type.returnType().equals(MemorySegment.class)) {
 66             // not returning segment, just insert a throwing allocator
 67             handle = MethodHandles.insertArguments(handle, 1, SharedUtils.THROWING_ALLOCATOR);
 68         }
 69         return SharedUtils.wrapDowncall(handle, function);

 70     }
 71 
 72     @Override
 73     public final NativeSymbol upcallStub(MethodHandle target, FunctionDescriptor function, ResourceScope scope) {
 74         Objects.requireNonNull(scope);
 75         Objects.requireNonNull(target);
 76         Objects.requireNonNull(function);
 77         MethodType type = SharedUtils.inferMethodType(function, true);
 78         if (!type.equals(target.type())) {
 79             throw new IllegalArgumentException("Wrong method handle type: " + target.type());
 80         }
 81         return CallArranger.MACOS.arrangeUpcall(target, target.type(), function, scope);
 82     }
 83 
 84     public static VaList newVaList(Consumer<VaList.Builder> actions, ResourceScope scope) {
 85         MacOsAArch64VaList.Builder builder = MacOsAArch64VaList.builder(scope);
 86         actions.accept(builder);
 87         return builder.build();
 88     }
 89 
 90     public static VaList newVaListOfAddress(MemoryAddress ma, ResourceScope scope) {
 91         return MacOsAArch64VaList.ofAddress(ma, scope);
 92     }
 93 
 94     public static VaList emptyVaList() {
 95         return MacOsAArch64VaList.empty();
 96     }
 97 
 98 }
< prev index next >