< prev index next >

src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/abi/x64/sysv/SysVx64Linker.java

Print this page

  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 }

  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.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 
 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 implements CLinker {
 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     public static SysVx64Linker getInstance() {
 58         if (instance == null) {
 59             instance = new SysVx64Linker();
 60         }
 61         return instance;
 62     }
 63 
 64     public static VaList newVaList(Consumer<VaList.Builder> actions, ResourceScope scope) {
 65         SysVVaList.Builder builder = SysVVaList.builder(scope);
 66         actions.accept(builder);
 67         return builder.build();
 68     }
 69 
 70     @Override
 71     public final MethodHandle downcallHandle(FunctionDescriptor function) {

 72         Objects.requireNonNull(function);
 73         MethodType type = SharedUtils.inferMethodType(function, false);
 74         MethodHandle handle = CallArranger.arrangeDowncall(type, function);
 75         if (!type.returnType().equals(MemorySegment.class)) {
 76             // not returning segment, just insert a throwing allocator
 77             handle = MethodHandles.insertArguments(handle, 1, SharedUtils.THROWING_ALLOCATOR);
 78         }
 79         return SharedUtils.wrapDowncall(handle, function);

 80     }
 81 
 82     @Override
 83     public final NativeSymbol upcallStub(MethodHandle target, FunctionDescriptor function, ResourceScope scope) {
 84         Objects.requireNonNull(scope);
 85         Objects.requireNonNull(target);
 86         Objects.requireNonNull(function);
 87         SharedUtils.checkExceptions(target);
 88         MethodType type = SharedUtils.inferMethodType(function, true);
 89         if (!type.equals(target.type())) {
 90             throw new IllegalArgumentException("Wrong method handle type: " + target.type());
 91         }
 92         return CallArranger.arrangeUpcall(target, target.type(), function, scope);
 93     }
 94 
 95     public static VaList newVaListOfAddress(MemoryAddress ma, ResourceScope scope) {
 96         return SysVVaList.ofAddress(ma, scope);
 97     }
 98 
 99     public static VaList emptyVaList() {
100         return SysVVaList.empty();
101     }
102 }
< prev index next >