1 /* 2 * Copyright (c) 2016, 2020, 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.x64.sysv; 26 27 import jdk.incubator.foreign.CSupport; 28 import jdk.incubator.foreign.ForeignLinker; 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.internal.foreign.abi.SharedUtils; 34 import jdk.internal.foreign.abi.UpcallStubs; 35 36 import java.lang.invoke.MethodHandle; 37 import java.lang.invoke.MethodHandles; 38 import java.lang.invoke.MethodType; 39 import java.util.Optional; 40 import java.util.function.Consumer; 41 42 import static jdk.incubator.foreign.CSupport.*; 43 44 /** 45 * ABI implementation based on System V ABI AMD64 supplement v.0.99.6 46 */ 47 public class SysVx64Linker implements ForeignLinker { 48 public static final int MAX_INTEGER_ARGUMENT_REGISTERS = 6; 49 public static final int MAX_INTEGER_RETURN_REGISTERS = 2; 50 public static final int MAX_VECTOR_ARGUMENT_REGISTERS = 8; 51 public static final int MAX_VECTOR_RETURN_REGISTERS = 2; 52 public static final int MAX_X87_RETURN_REGISTERS = 2; 53 54 private static SysVx64Linker instance; 55 56 static final long ADDRESS_SIZE = 64; // bits 57 58 private static final MethodHandle MH_unboxVaList; 59 private static final MethodHandle MH_boxVaList; 60 61 static { 62 try { 63 MethodHandles.Lookup lookup = MethodHandles.lookup(); 64 MH_unboxVaList = lookup.findVirtual(CSupport.VaList.class, "address", 65 MethodType.methodType(MemoryAddress.class)); 66 MH_boxVaList = lookup.findStatic(SysVx64Linker.class, "newVaListOfAddress", 67 MethodType.methodType(VaList.class, MemoryAddress.class)); 68 } catch (ReflectiveOperationException e) { 69 throw new ExceptionInInitializerError(e); 70 } 71 } 72 73 public static SysVx64Linker getInstance() { 74 if (instance == null) { 75 instance = new SysVx64Linker(); 76 } 77 return instance; 78 } 79 80 public static VaList newVaList(Consumer<VaList.Builder> actions) { 81 SysVVaList.Builder builder = SysVVaList.builder(); 82 actions.accept(builder); 83 return builder.build(); 84 } 85 86 @Override 87 public MethodHandle downcallHandle(MemoryAddress symbol, MethodType type, FunctionDescriptor function) { 88 MethodType llMt = SharedUtils.convertVaListCarriers(type, SysVVaList.CARRIER); 89 MethodHandle handle = CallArranger.arrangeDowncall(symbol, llMt, function); 90 handle = SharedUtils.unboxVaLists(type, handle, MH_unboxVaList); 91 return handle; 92 } 93 94 @Override 95 public MemorySegment upcallStub(MethodHandle target, FunctionDescriptor function) { 96 target = SharedUtils.boxVaLists(target, MH_boxVaList); 97 return UpcallStubs.upcallAddress(CallArranger.arrangeUpcall(target, target.type(), function)); 98 } 99 100 @Override 101 public String name() { 102 return SysV.NAME; 103 } 104 105 static Optional<ArgumentClassImpl> argumentClassFor(MemoryLayout layout) { 106 @SuppressWarnings({"unchecked", "rawtypes"}) 107 Optional<SysV.ArgumentClass> argClassOpt = 108 (Optional<SysV.ArgumentClass>)(Optional)layout.attribute(SysV.CLASS_ATTRIBUTE_NAME); 109 return argClassOpt.map(argClass -> switch (argClass) { 110 case INTEGER -> ArgumentClassImpl.INTEGER; 111 case SSE -> ArgumentClassImpl.SSE; 112 case X87 -> ArgumentClassImpl.X87; 113 case COMPLEX_87 -> ArgumentClassImpl.COMPLEX_X87; 114 case POINTER -> ArgumentClassImpl.POINTER; 115 default -> null; 116 }); 117 } 118 119 public static VaList newVaListOfAddress(MemoryAddress ma) { 120 return SysVVaList.ofAddress(ma); 121 } 122 123 public static VaList emptyVaList() { 124 return SysVVaList.empty(); 125 } 126 }