1 /* 2 * Copyright (c) 2016, 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; 26 27 import jdk.internal.foreign.abi.x64.windows.Windowsx64ABI; 28 import jdk.internal.foreign.abi.x64.sysv.SysVx64ABI; 29 30 import java.foreign.Library; 31 import java.foreign.NativeMethodType; 32 import java.foreign.layout.Layout; 33 import java.lang.invoke.MethodHandle; 34 import java.util.Collection; 35 36 /** 37 * This class models a system application binary interface (ABI). 38 */ 39 public interface SystemABI { 40 /** 41 * This enum represents the set of basic (C) types that should be supported 42 * by the system ABI. 43 */ 44 enum CType { 45 Bool, 46 Char, 47 SignedChar, 48 Short, 49 Int, 50 Long, 51 LongLong, 52 UnsignedChar, 53 UnsignedShort, 54 UnsignedInt, 55 UnsignedLong, 56 UnsignedLongLong, 57 Pointer, 58 Float, 59 Double, 60 LongDouble 61 } 62 63 /** 64 * Query the ABI-specific layout of given basic type. Information such as size 65 * and alignment can be obtained by inspecting the result layout (including its layout annotations). 66 */ 67 Layout layoutOf(CType type); 68 69 /** 70 * Obtain a method handle which can be used to call a given native function, 71 * given default calling covention. 72 * 73 * This is equivalent to: 74 * downcallHandle(defaultCallingConvention(), addr, nmt, name) 75 */ 76 default MethodHandle downcallHandle(Library.Symbol symbol, java.foreign.NativeMethodType nmt) { 77 return downcallHandle(defaultCallingConvention(), symbol, nmt); 78 } 79 80 /** 81 * Obtain a method handle which can be used to call a given native function, 82 * given selected calling convention. 83 */ 84 MethodHandle downcallHandle(CallingConvention cc, Library.Symbol symbol, java.foreign.NativeMethodType nmt); 85 86 /** 87 * Obtain the pointer to a native stub (using default calling convention) which 88 * can be used to upcall into a given method handle. 89 * 90 * This is equivalent to: 91 * upcallStub(defaultCallingConvention(), receiver, ret, args) 92 */ 93 default Library.Symbol upcallStub(MethodHandle target, java.foreign.NativeMethodType nmt) { 94 return upcallStub(defaultCallingConvention(), target, nmt); 95 } 96 97 /** 98 * Obtain the pointer to a native stub (using selected calling convention) which 99 * can be used to upcall into a given method handle. 100 */ 101 Library.Symbol upcallStub(CallingConvention cc, MethodHandle target, NativeMethodType nmt); 102 103 /** 104 * Query standard calling convention used by this platform ABI. 105 */ 106 CallingConvention defaultCallingConvention(); 107 108 /** 109 * Obtain given calling convention by name (if available). 110 */ 111 CallingConvention namedCallingConvention(String name) throws IllegalArgumentException; 112 113 /** 114 * Query list of supported calling conventions. 115 */ 116 Collection<CallingConvention> callingConventions(); 117 118 /** 119 * A calling convention specifies how arguments and return types are communicated 120 * from caller to callee. 121 */ 122 interface CallingConvention { 123 String name(); 124 } 125 126 static SystemABI getInstance() { 127 String arch = System.getProperty("os.arch"); 128 String os = System.getProperty("os.name"); 129 if (arch.equals("amd64") || arch.equals("x86_64")) { 130 if (os.startsWith("Windows")) { 131 return Windowsx64ABI.getInstance(); 132 } else { 133 return SysVx64ABI.getInstance(); 134 } 135 } 136 throw new UnsupportedOperationException("Unsupported os or arch: " + os + ", " + arch); 137 } 138 }