|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 2022, 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. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +/* |
| 26 | + * @test |
| 27 | + * @enablePreview |
| 28 | + * @modules java.base/jdk.internal.foreign |
| 29 | + * java.base/jdk.internal.foreign.abi |
| 30 | + * java.base/jdk.internal.foreign.abi.aarch64 |
| 31 | + * @build CallArrangerTestBase |
| 32 | + * @run testng TestMacOsAArch64CallArranger |
| 33 | + */ |
| 34 | + |
| 35 | +import java.lang.foreign.FunctionDescriptor; |
| 36 | +import java.lang.foreign.MemoryLayout; |
| 37 | +import java.lang.foreign.StructLayout; |
| 38 | +import java.lang.foreign.MemorySegment; |
| 39 | +import jdk.internal.foreign.abi.Binding; |
| 40 | +import jdk.internal.foreign.abi.CallingSequence; |
| 41 | +import jdk.internal.foreign.abi.LinkerOptions; |
| 42 | +import jdk.internal.foreign.abi.StubLocations; |
| 43 | +import jdk.internal.foreign.abi.VMStorage; |
| 44 | +import jdk.internal.foreign.abi.aarch64.CallArranger; |
| 45 | +import org.testng.annotations.DataProvider; |
| 46 | +import org.testng.annotations.Test; |
| 47 | + |
| 48 | +import java.lang.invoke.MethodType; |
| 49 | + |
| 50 | +import static java.lang.foreign.Linker.Option.firstVariadicArg; |
| 51 | +import static java.lang.foreign.ValueLayout.ADDRESS; |
| 52 | +import static jdk.internal.foreign.PlatformLayouts.AArch64.*; |
| 53 | +import static jdk.internal.foreign.abi.Binding.*; |
| 54 | +import static jdk.internal.foreign.abi.aarch64.AArch64Architecture.*; |
| 55 | +import static jdk.internal.foreign.abi.aarch64.AArch64Architecture.Regs.*; |
| 56 | +import static org.testng.Assert.assertEquals; |
| 57 | +import static org.testng.Assert.assertFalse; |
| 58 | +import static org.testng.Assert.assertTrue; |
| 59 | + |
| 60 | +public class TestMacOsAArch64CallArranger extends CallArrangerTestBase { |
| 61 | + |
| 62 | + private static final VMStorage TARGET_ADDRESS_STORAGE = StubLocations.TARGET_ADDRESS.storage(StorageType.PLACEHOLDER); |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testVarArgsOnStack() { |
| 66 | + MethodType mt = MethodType.methodType(void.class, int.class, int.class, float.class); |
| 67 | + FunctionDescriptor fd = FunctionDescriptor.ofVoid(C_INT, C_INT, C_FLOAT); |
| 68 | + FunctionDescriptor fdExpected = FunctionDescriptor.ofVoid(ADDRESS, C_INT, C_INT, C_FLOAT); |
| 69 | + CallArranger.Bindings bindings = CallArranger.MACOS.getBindings(mt, fd, false, LinkerOptions.forDowncall(fd, firstVariadicArg(1))); |
| 70 | + |
| 71 | + assertFalse(bindings.isInMemoryReturn()); |
| 72 | + CallingSequence callingSequence = bindings.callingSequence(); |
| 73 | + assertEquals(callingSequence.callerMethodType(), mt.insertParameterTypes(0, MemorySegment.class)); |
| 74 | + assertEquals(callingSequence.functionDesc(), fdExpected); |
| 75 | + |
| 76 | + // The two variadic arguments should be allocated on the stack |
| 77 | + checkArgumentBindings(callingSequence, new Binding[][]{ |
| 78 | + { unboxAddress(), vmStore(TARGET_ADDRESS_STORAGE, long.class) }, |
| 79 | + { vmStore(r0, int.class) }, |
| 80 | + { vmStore(stackStorage((short) 4, 0), int.class) }, |
| 81 | + { vmStore(stackStorage((short) 4, 8), float.class) }, |
| 82 | + }); |
| 83 | + |
| 84 | + checkReturnBindings(callingSequence, new Binding[]{}); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testMacArgsOnStack() { |
| 89 | + MethodType mt = MethodType.methodType(void.class, |
| 90 | + int.class, int.class, int.class, int.class, |
| 91 | + int.class, int.class, int.class, int.class, |
| 92 | + int.class, int.class, short.class, byte.class); |
| 93 | + FunctionDescriptor fd = FunctionDescriptor.ofVoid( |
| 94 | + C_INT, C_INT, C_INT, C_INT, |
| 95 | + C_INT, C_INT, C_INT, C_INT, |
| 96 | + C_INT, C_INT, C_SHORT, C_CHAR); |
| 97 | + CallArranger.Bindings bindings = CallArranger.MACOS.getBindings(mt, fd, false); |
| 98 | + |
| 99 | + assertFalse(bindings.isInMemoryReturn()); |
| 100 | + CallingSequence callingSequence = bindings.callingSequence(); |
| 101 | + assertEquals(callingSequence.callerMethodType(), mt.insertParameterTypes(0, MemorySegment.class)); |
| 102 | + assertEquals(callingSequence.functionDesc(), fd.insertArgumentLayouts(0, ADDRESS)); |
| 103 | + |
| 104 | + checkArgumentBindings(callingSequence, new Binding[][]{ |
| 105 | + { unboxAddress(), vmStore(TARGET_ADDRESS_STORAGE, long.class) }, |
| 106 | + { vmStore(r0, int.class) }, |
| 107 | + { vmStore(r1, int.class) }, |
| 108 | + { vmStore(r2, int.class) }, |
| 109 | + { vmStore(r3, int.class) }, |
| 110 | + { vmStore(r4, int.class) }, |
| 111 | + { vmStore(r5, int.class) }, |
| 112 | + { vmStore(r6, int.class) }, |
| 113 | + { vmStore(r7, int.class) }, |
| 114 | + { vmStore(stackStorage((short) 4, 0), int.class) }, |
| 115 | + { vmStore(stackStorage((short) 4, 4), int.class) }, |
| 116 | + { cast(short.class, int.class), vmStore(stackStorage((short) 2, 8), int.class) }, |
| 117 | + { cast(byte.class, int.class), vmStore(stackStorage((short) 1, 10), int.class) }, |
| 118 | + }); |
| 119 | + |
| 120 | + checkReturnBindings(callingSequence, new Binding[]{}); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testMacArgsOnStack2() { |
| 125 | + StructLayout struct = MemoryLayout.structLayout( |
| 126 | + C_FLOAT, |
| 127 | + C_FLOAT |
| 128 | + ); |
| 129 | + MethodType mt = MethodType.methodType(void.class, |
| 130 | + long.class, long.class, long.class, long.class, |
| 131 | + long.class, long.class, long.class, long.class, |
| 132 | + double.class, double.class, double.class, double.class, |
| 133 | + double.class, double.class, double.class, double.class, |
| 134 | + int.class, MemorySegment.class); |
| 135 | + FunctionDescriptor fd = FunctionDescriptor.ofVoid( |
| 136 | + C_LONG_LONG, C_LONG_LONG, C_LONG_LONG, C_LONG_LONG, |
| 137 | + C_LONG_LONG, C_LONG_LONG, C_LONG_LONG, C_LONG_LONG, |
| 138 | + C_DOUBLE, C_DOUBLE, C_DOUBLE, C_DOUBLE, |
| 139 | + C_DOUBLE, C_DOUBLE, C_DOUBLE, C_DOUBLE, |
| 140 | + C_INT, struct); |
| 141 | + CallArranger.Bindings bindings = CallArranger.MACOS.getBindings(mt, fd, false); |
| 142 | + |
| 143 | + assertFalse(bindings.isInMemoryReturn()); |
| 144 | + CallingSequence callingSequence = bindings.callingSequence(); |
| 145 | + assertEquals(callingSequence.callerMethodType(), mt.insertParameterTypes(0, MemorySegment.class)); |
| 146 | + assertEquals(callingSequence.functionDesc(), fd.insertArgumentLayouts(0, ADDRESS)); |
| 147 | + |
| 148 | + checkArgumentBindings(callingSequence, new Binding[][]{ |
| 149 | + { unboxAddress(), vmStore(TARGET_ADDRESS_STORAGE, long.class) }, |
| 150 | + { vmStore(r0, long.class) }, |
| 151 | + { vmStore(r1, long.class) }, |
| 152 | + { vmStore(r2, long.class) }, |
| 153 | + { vmStore(r3, long.class) }, |
| 154 | + { vmStore(r4, long.class) }, |
| 155 | + { vmStore(r5, long.class) }, |
| 156 | + { vmStore(r6, long.class) }, |
| 157 | + { vmStore(r7, long.class) }, |
| 158 | + { vmStore(v0, double.class) }, |
| 159 | + { vmStore(v1, double.class) }, |
| 160 | + { vmStore(v2, double.class) }, |
| 161 | + { vmStore(v3, double.class) }, |
| 162 | + { vmStore(v4, double.class) }, |
| 163 | + { vmStore(v5, double.class) }, |
| 164 | + { vmStore(v6, double.class) }, |
| 165 | + { vmStore(v7, double.class) }, |
| 166 | + { vmStore(stackStorage((short) 4, 0), int.class) }, |
| 167 | + { |
| 168 | + dup(), |
| 169 | + bufferLoad(0, int.class), |
| 170 | + vmStore(stackStorage((short) 4, 4), int.class), |
| 171 | + bufferLoad(4, int.class), |
| 172 | + vmStore(stackStorage((short) 4, 8), int.class), |
| 173 | + } |
| 174 | + }); |
| 175 | + |
| 176 | + checkReturnBindings(callingSequence, new Binding[]{}); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + public void testMacArgsOnStack3() { |
| 181 | + StructLayout struct = MemoryLayout.structLayout( |
| 182 | + C_POINTER, |
| 183 | + C_POINTER |
| 184 | + ); |
| 185 | + MethodType mt = MethodType.methodType(void.class, |
| 186 | + long.class, long.class, long.class, long.class, |
| 187 | + long.class, long.class, long.class, long.class, |
| 188 | + double.class, double.class, double.class, double.class, |
| 189 | + double.class, double.class, double.class, double.class, |
| 190 | + MemorySegment.class, float.class); |
| 191 | + FunctionDescriptor fd = FunctionDescriptor.ofVoid( |
| 192 | + C_LONG_LONG, C_LONG_LONG, C_LONG_LONG, C_LONG_LONG, |
| 193 | + C_LONG_LONG, C_LONG_LONG, C_LONG_LONG, C_LONG_LONG, |
| 194 | + C_DOUBLE, C_DOUBLE, C_DOUBLE, C_DOUBLE, |
| 195 | + C_DOUBLE, C_DOUBLE, C_DOUBLE, C_DOUBLE, |
| 196 | + struct, C_FLOAT); |
| 197 | + CallArranger.Bindings bindings = CallArranger.MACOS.getBindings(mt, fd, false); |
| 198 | + |
| 199 | + assertFalse(bindings.isInMemoryReturn()); |
| 200 | + CallingSequence callingSequence = bindings.callingSequence(); |
| 201 | + assertEquals(callingSequence.callerMethodType(), mt.insertParameterTypes(0, MemorySegment.class)); |
| 202 | + assertEquals(callingSequence.functionDesc(), fd.insertArgumentLayouts(0, ADDRESS)); |
| 203 | + |
| 204 | + checkArgumentBindings(callingSequence, new Binding[][]{ |
| 205 | + { unboxAddress(), vmStore(TARGET_ADDRESS_STORAGE, long.class) }, |
| 206 | + { vmStore(r0, long.class) }, |
| 207 | + { vmStore(r1, long.class) }, |
| 208 | + { vmStore(r2, long.class) }, |
| 209 | + { vmStore(r3, long.class) }, |
| 210 | + { vmStore(r4, long.class) }, |
| 211 | + { vmStore(r5, long.class) }, |
| 212 | + { vmStore(r6, long.class) }, |
| 213 | + { vmStore(r7, long.class) }, |
| 214 | + { vmStore(v0, double.class) }, |
| 215 | + { vmStore(v1, double.class) }, |
| 216 | + { vmStore(v2, double.class) }, |
| 217 | + { vmStore(v3, double.class) }, |
| 218 | + { vmStore(v4, double.class) }, |
| 219 | + { vmStore(v5, double.class) }, |
| 220 | + { vmStore(v6, double.class) }, |
| 221 | + { vmStore(v7, double.class) }, |
| 222 | + { dup(), |
| 223 | + bufferLoad(0, long.class), vmStore(stackStorage((short) 8, 0), long.class), |
| 224 | + bufferLoad(8, long.class), vmStore(stackStorage((short) 8, 8), long.class) }, |
| 225 | + { vmStore(stackStorage((short) 4, 16), float.class) }, |
| 226 | + }); |
| 227 | + |
| 228 | + checkReturnBindings(callingSequence, new Binding[]{}); |
| 229 | + } |
| 230 | + |
| 231 | + @Test |
| 232 | + public void testMacArgsOnStack4() { |
| 233 | + StructLayout struct = MemoryLayout.structLayout( |
| 234 | + C_INT, |
| 235 | + C_INT, |
| 236 | + C_POINTER |
| 237 | + ); |
| 238 | + MethodType mt = MethodType.methodType(void.class, |
| 239 | + long.class, long.class, long.class, long.class, |
| 240 | + long.class, long.class, long.class, long.class, |
| 241 | + double.class, double.class, double.class, double.class, |
| 242 | + double.class, double.class, double.class, double.class, |
| 243 | + float.class, MemorySegment.class); |
| 244 | + FunctionDescriptor fd = FunctionDescriptor.ofVoid( |
| 245 | + C_LONG_LONG, C_LONG_LONG, C_LONG_LONG, C_LONG_LONG, |
| 246 | + C_LONG_LONG, C_LONG_LONG, C_LONG_LONG, C_LONG_LONG, |
| 247 | + C_DOUBLE, C_DOUBLE, C_DOUBLE, C_DOUBLE, |
| 248 | + C_DOUBLE, C_DOUBLE, C_DOUBLE, C_DOUBLE, |
| 249 | + C_FLOAT, struct); |
| 250 | + CallArranger.Bindings bindings = CallArranger.MACOS.getBindings(mt, fd, false); |
| 251 | + |
| 252 | + assertFalse(bindings.isInMemoryReturn()); |
| 253 | + CallingSequence callingSequence = bindings.callingSequence(); |
| 254 | + assertEquals(callingSequence.callerMethodType(), mt.insertParameterTypes(0, MemorySegment.class)); |
| 255 | + assertEquals(callingSequence.functionDesc(), fd.insertArgumentLayouts(0, ADDRESS)); |
| 256 | + |
| 257 | + checkArgumentBindings(callingSequence, new Binding[][]{ |
| 258 | + { unboxAddress(), vmStore(TARGET_ADDRESS_STORAGE, long.class) }, |
| 259 | + { vmStore(r0, long.class) }, |
| 260 | + { vmStore(r1, long.class) }, |
| 261 | + { vmStore(r2, long.class) }, |
| 262 | + { vmStore(r3, long.class) }, |
| 263 | + { vmStore(r4, long.class) }, |
| 264 | + { vmStore(r5, long.class) }, |
| 265 | + { vmStore(r6, long.class) }, |
| 266 | + { vmStore(r7, long.class) }, |
| 267 | + { vmStore(v0, double.class) }, |
| 268 | + { vmStore(v1, double.class) }, |
| 269 | + { vmStore(v2, double.class) }, |
| 270 | + { vmStore(v3, double.class) }, |
| 271 | + { vmStore(v4, double.class) }, |
| 272 | + { vmStore(v5, double.class) }, |
| 273 | + { vmStore(v6, double.class) }, |
| 274 | + { vmStore(v7, double.class) }, |
| 275 | + { vmStore(stackStorage((short) 4, 0), float.class) }, |
| 276 | + { dup(), |
| 277 | + bufferLoad(0, long.class), vmStore(stackStorage((short) 8, 8), long.class), |
| 278 | + bufferLoad(8, long.class), vmStore(stackStorage((short) 8, 16), long.class) }, |
| 279 | + }); |
| 280 | + |
| 281 | + checkReturnBindings(callingSequence, new Binding[]{}); |
| 282 | + } |
| 283 | +} |
0 commit comments