| 1 | n/a | #if defined(__ppc__) || defined(__ppc64__) |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | /* ----------------------------------------------------------------------- |
|---|
| 4 | n/a | ffi.c - Copyright (c) 1998 Geoffrey Keating |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | PowerPC Foreign Function Interface |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | Darwin ABI support (c) 2001 John Hornkvist |
|---|
| 9 | n/a | AIX ABI support (c) 2002 Free Software Foundation, Inc. |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | Permission is hereby granted, free of charge, to any person obtaining |
|---|
| 12 | n/a | a copy of this software and associated documentation files (the |
|---|
| 13 | n/a | ``Software''), to deal in the Software without restriction, including |
|---|
| 14 | n/a | without limitation the rights to use, copy, modify, merge, publish, |
|---|
| 15 | n/a | distribute, sublicense, and/or sell copies of the Software, and to |
|---|
| 16 | n/a | permit persons to whom the Software is furnished to do so, subject to |
|---|
| 17 | n/a | the following conditions: |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | The above copyright notice and this permission notice shall be included |
|---|
| 20 | n/a | in all copies or substantial portions of the Software. |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS |
|---|
| 23 | n/a | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|---|
| 24 | n/a | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|---|
| 25 | n/a | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR |
|---|
| 26 | n/a | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
|---|
| 27 | n/a | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|---|
| 28 | n/a | OTHER DEALINGS IN THE SOFTWARE. |
|---|
| 29 | n/a | ----------------------------------------------------------------------- */ |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | #include <ffi.h> |
|---|
| 32 | n/a | #include <ffi_common.h> |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | #include <stdbool.h> |
|---|
| 35 | n/a | #include <stdio.h> |
|---|
| 36 | n/a | #include <stdlib.h> |
|---|
| 37 | n/a | #include <ppc-darwin.h> |
|---|
| 38 | n/a | #include <architecture/ppc/mode_independent_asm.h> |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | #if 0 |
|---|
| 41 | n/a | #if defined(POWERPC_DARWIN) |
|---|
| 42 | n/a | #include <libkern/OSCacheControl.h> // for sys_icache_invalidate() |
|---|
| 43 | n/a | #endif |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | #else |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | #pragma weak sys_icache_invalidate |
|---|
| 48 | n/a | extern void sys_icache_invalidate(void *start, size_t len); |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | #endif |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | extern void ffi_closure_ASM(void); |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | // The layout of a function descriptor. A C function pointer really |
|---|
| 56 | n/a | // points to one of these. |
|---|
| 57 | n/a | typedef struct aix_fd_struct { |
|---|
| 58 | n/a | void* code_pointer; |
|---|
| 59 | n/a | void* toc; |
|---|
| 60 | n/a | } aix_fd; |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | /* ffi_prep_args is called by the assembly routine once stack space |
|---|
| 63 | n/a | has been allocated for the function's arguments. |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | The stack layout we want looks like this: |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | | Return address from ffi_call_DARWIN | higher addresses |
|---|
| 68 | n/a | |--------------------------------------------| |
|---|
| 69 | n/a | | Previous backchain pointer 4/8 | stack pointer here |
|---|
| 70 | n/a | |--------------------------------------------|-\ <<< on entry to |
|---|
| 71 | n/a | | Saved r28-r31 (4/8)*4 | | ffi_call_DARWIN |
|---|
| 72 | n/a | |--------------------------------------------| | |
|---|
| 73 | n/a | | Parameters (at least 8*(4/8)=32/64) | | (176) +112 - +288 |
|---|
| 74 | n/a | |--------------------------------------------| | |
|---|
| 75 | n/a | | Space for GPR2 4/8 | | |
|---|
| 76 | n/a | |--------------------------------------------| | stack | |
|---|
| 77 | n/a | | Reserved (4/8)*2 | | grows | |
|---|
| 78 | n/a | |--------------------------------------------| | down V |
|---|
| 79 | n/a | | Space for callee's LR 4/8 | | |
|---|
| 80 | n/a | |--------------------------------------------| | lower addresses |
|---|
| 81 | n/a | | Saved CR 4/8 | | |
|---|
| 82 | n/a | |--------------------------------------------| | stack pointer here |
|---|
| 83 | n/a | | Current backchain pointer 4/8 | | during |
|---|
| 84 | n/a | |--------------------------------------------|-/ <<< ffi_call_DARWIN |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | Note: ppc64 CR is saved in the low word of a long on the stack. |
|---|
| 87 | n/a | */ |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | /*@-exportheader@*/ |
|---|
| 90 | n/a | void |
|---|
| 91 | n/a | ffi_prep_args( |
|---|
| 92 | n/a | extended_cif* inEcif, |
|---|
| 93 | n/a | unsigned *const stack) |
|---|
| 94 | n/a | /*@=exportheader@*/ |
|---|
| 95 | n/a | { |
|---|
| 96 | n/a | /* Copy the ecif to a local var so we can trample the arg. |
|---|
| 97 | n/a | BC note: test this with GP later for possible problems... */ |
|---|
| 98 | n/a | volatile extended_cif* ecif = inEcif; |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | const unsigned bytes = ecif->cif->bytes; |
|---|
| 101 | n/a | const unsigned flags = ecif->cif->flags; |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | /* Cast the stack arg from int* to long*. sizeof(long) == 4 in 32-bit mode |
|---|
| 104 | n/a | and 8 in 64-bit mode. */ |
|---|
| 105 | n/a | unsigned long *const longStack = (unsigned long *const)stack; |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | /* 'stacktop' points at the previous backchain pointer. */ |
|---|
| 108 | n/a | #if defined(__ppc64__) |
|---|
| 109 | n/a | // In ppc-darwin.s, an extra 96 bytes is reserved for the linkage area, |
|---|
| 110 | n/a | // saved registers, and an extra FPR. |
|---|
| 111 | n/a | unsigned long *const stacktop = |
|---|
| 112 | n/a | (unsigned long *)(unsigned long)((char*)longStack + bytes + 96); |
|---|
| 113 | n/a | #elif defined(__ppc__) |
|---|
| 114 | n/a | unsigned long *const stacktop = longStack + (bytes / sizeof(long)); |
|---|
| 115 | n/a | #else |
|---|
| 116 | n/a | #error undefined architecture |
|---|
| 117 | n/a | #endif |
|---|
| 118 | n/a | |
|---|
| 119 | n/a | /* 'fpr_base' points at the space for fpr1, and grows upwards as |
|---|
| 120 | n/a | we use FPR registers. */ |
|---|
| 121 | n/a | double* fpr_base = (double*)(stacktop - ASM_NEEDS_REGISTERS) - |
|---|
| 122 | n/a | NUM_FPR_ARG_REGISTERS; |
|---|
| 123 | n/a | |
|---|
| 124 | n/a | #if defined(__ppc64__) |
|---|
| 125 | n/a | // 64-bit saves an extra register, and uses an extra FPR. Knock fpr_base |
|---|
| 126 | n/a | // down a couple pegs. |
|---|
| 127 | n/a | fpr_base -= 2; |
|---|
| 128 | n/a | #endif |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | unsigned int fparg_count = 0; |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | /* 'next_arg' grows up as we put parameters in it. */ |
|---|
| 133 | n/a | unsigned long* next_arg = longStack + 6; /* 6 reserved positions. */ |
|---|
| 134 | n/a | |
|---|
| 135 | n/a | int i; |
|---|
| 136 | n/a | double double_tmp; |
|---|
| 137 | n/a | void** p_argv = ecif->avalue; |
|---|
| 138 | n/a | unsigned long gprvalue; |
|---|
| 139 | n/a | ffi_type** ptr = ecif->cif->arg_types; |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | /* Check that everything starts aligned properly. */ |
|---|
| 142 | n/a | FFI_ASSERT(stack == SF_ROUND(stack)); |
|---|
| 143 | n/a | FFI_ASSERT(stacktop == SF_ROUND(stacktop)); |
|---|
| 144 | n/a | FFI_ASSERT(bytes == SF_ROUND(bytes)); |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | /* Deal with return values that are actually pass-by-reference. |
|---|
| 147 | n/a | Rule: |
|---|
| 148 | n/a | Return values are referenced by r3, so r4 is the first parameter. */ |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | if (flags & FLAG_RETVAL_REFERENCE) |
|---|
| 151 | n/a | *next_arg++ = (unsigned long)(char*)ecif->rvalue; |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | /* Now for the arguments. */ |
|---|
| 154 | n/a | for (i = ecif->cif->nargs; i > 0; i--, ptr++, p_argv++) |
|---|
| 155 | n/a | { |
|---|
| 156 | n/a | switch ((*ptr)->type) |
|---|
| 157 | n/a | { |
|---|
| 158 | n/a | /* If a floating-point parameter appears before all of the general- |
|---|
| 159 | n/a | purpose registers are filled, the corresponding GPRs that match |
|---|
| 160 | n/a | the size of the floating-point parameter are shadowed for the |
|---|
| 161 | n/a | benefit of vararg and pre-ANSI functions. */ |
|---|
| 162 | n/a | case FFI_TYPE_FLOAT: |
|---|
| 163 | n/a | double_tmp = *(float*)*p_argv; |
|---|
| 164 | n/a | |
|---|
| 165 | n/a | if (fparg_count < NUM_FPR_ARG_REGISTERS) |
|---|
| 166 | n/a | *fpr_base++ = double_tmp; |
|---|
| 167 | n/a | |
|---|
| 168 | n/a | *(double*)next_arg = double_tmp; |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | next_arg++; |
|---|
| 171 | n/a | fparg_count++; |
|---|
| 172 | n/a | FFI_ASSERT(flags & FLAG_FP_ARGUMENTS); |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | break; |
|---|
| 175 | n/a | |
|---|
| 176 | n/a | case FFI_TYPE_DOUBLE: |
|---|
| 177 | n/a | double_tmp = *(double*)*p_argv; |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | if (fparg_count < NUM_FPR_ARG_REGISTERS) |
|---|
| 180 | n/a | *fpr_base++ = double_tmp; |
|---|
| 181 | n/a | |
|---|
| 182 | n/a | *(double*)next_arg = double_tmp; |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | next_arg += MODE_CHOICE(2,1); |
|---|
| 185 | n/a | fparg_count++; |
|---|
| 186 | n/a | FFI_ASSERT(flags & FLAG_FP_ARGUMENTS); |
|---|
| 187 | n/a | |
|---|
| 188 | n/a | break; |
|---|
| 189 | n/a | |
|---|
| 190 | n/a | #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE |
|---|
| 191 | n/a | case FFI_TYPE_LONGDOUBLE: |
|---|
| 192 | n/a | #if defined(__ppc64__) |
|---|
| 193 | n/a | if (fparg_count < NUM_FPR_ARG_REGISTERS) |
|---|
| 194 | n/a | *(long double*)fpr_base = *(long double*)*p_argv; |
|---|
| 195 | n/a | #elif defined(__ppc__) |
|---|
| 196 | n/a | if (fparg_count < NUM_FPR_ARG_REGISTERS - 1) |
|---|
| 197 | n/a | *(long double*)fpr_base = *(long double*)*p_argv; |
|---|
| 198 | n/a | else if (fparg_count == NUM_FPR_ARG_REGISTERS - 1) |
|---|
| 199 | n/a | *(double*)fpr_base = *(double*)*p_argv; |
|---|
| 200 | n/a | #else |
|---|
| 201 | n/a | #error undefined architecture |
|---|
| 202 | n/a | #endif |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | *(long double*)next_arg = *(long double*)*p_argv; |
|---|
| 205 | n/a | fparg_count += 2; |
|---|
| 206 | n/a | fpr_base += 2; |
|---|
| 207 | n/a | next_arg += MODE_CHOICE(4,2); |
|---|
| 208 | n/a | FFI_ASSERT(flags & FLAG_FP_ARGUMENTS); |
|---|
| 209 | n/a | |
|---|
| 210 | n/a | break; |
|---|
| 211 | n/a | #endif // FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | case FFI_TYPE_UINT64: |
|---|
| 214 | n/a | case FFI_TYPE_SINT64: |
|---|
| 215 | n/a | #if defined(__ppc64__) |
|---|
| 216 | n/a | gprvalue = *(long long*)*p_argv; |
|---|
| 217 | n/a | goto putgpr; |
|---|
| 218 | n/a | #elif defined(__ppc__) |
|---|
| 219 | n/a | *(long long*)next_arg = *(long long*)*p_argv; |
|---|
| 220 | n/a | next_arg += 2; |
|---|
| 221 | n/a | break; |
|---|
| 222 | n/a | #else |
|---|
| 223 | n/a | #error undefined architecture |
|---|
| 224 | n/a | #endif |
|---|
| 225 | n/a | |
|---|
| 226 | n/a | case FFI_TYPE_POINTER: |
|---|
| 227 | n/a | gprvalue = *(unsigned long*)*p_argv; |
|---|
| 228 | n/a | goto putgpr; |
|---|
| 229 | n/a | |
|---|
| 230 | n/a | case FFI_TYPE_UINT8: |
|---|
| 231 | n/a | gprvalue = *(unsigned char*)*p_argv; |
|---|
| 232 | n/a | goto putgpr; |
|---|
| 233 | n/a | |
|---|
| 234 | n/a | case FFI_TYPE_SINT8: |
|---|
| 235 | n/a | gprvalue = *(signed char*)*p_argv; |
|---|
| 236 | n/a | goto putgpr; |
|---|
| 237 | n/a | |
|---|
| 238 | n/a | case FFI_TYPE_UINT16: |
|---|
| 239 | n/a | gprvalue = *(unsigned short*)*p_argv; |
|---|
| 240 | n/a | goto putgpr; |
|---|
| 241 | n/a | |
|---|
| 242 | n/a | case FFI_TYPE_SINT16: |
|---|
| 243 | n/a | gprvalue = *(signed short*)*p_argv; |
|---|
| 244 | n/a | goto putgpr; |
|---|
| 245 | n/a | |
|---|
| 246 | n/a | case FFI_TYPE_STRUCT: |
|---|
| 247 | n/a | { |
|---|
| 248 | n/a | #if defined(__ppc64__) |
|---|
| 249 | n/a | unsigned int gprSize = 0; |
|---|
| 250 | n/a | unsigned int fprSize = 0; |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | ffi64_struct_to_reg_form(*ptr, (char*)*p_argv, NULL, &fparg_count, |
|---|
| 253 | n/a | (char*)next_arg, &gprSize, (char*)fpr_base, &fprSize); |
|---|
| 254 | n/a | next_arg += gprSize / sizeof(long); |
|---|
| 255 | n/a | fpr_base += fprSize / sizeof(double); |
|---|
| 256 | n/a | |
|---|
| 257 | n/a | #elif defined(__ppc__) |
|---|
| 258 | n/a | char* dest_cpy = (char*)next_arg; |
|---|
| 259 | n/a | |
|---|
| 260 | n/a | /* Structures that match the basic modes (QI 1 byte, HI 2 bytes, |
|---|
| 261 | n/a | SI 4 bytes) are aligned as if they were those modes. |
|---|
| 262 | n/a | Structures with 3 byte in size are padded upwards. */ |
|---|
| 263 | n/a | unsigned size_al = (*ptr)->size; |
|---|
| 264 | n/a | |
|---|
| 265 | n/a | /* If the first member of the struct is a double, then align |
|---|
| 266 | n/a | the struct to double-word. */ |
|---|
| 267 | n/a | if ((*ptr)->elements[0]->type == FFI_TYPE_DOUBLE) |
|---|
| 268 | n/a | size_al = ALIGN((*ptr)->size, 8); |
|---|
| 269 | n/a | |
|---|
| 270 | n/a | if (ecif->cif->abi == FFI_DARWIN) |
|---|
| 271 | n/a | { |
|---|
| 272 | n/a | if (size_al < 3) |
|---|
| 273 | n/a | dest_cpy += 4 - size_al; |
|---|
| 274 | n/a | } |
|---|
| 275 | n/a | |
|---|
| 276 | n/a | memcpy((char*)dest_cpy, (char*)*p_argv, size_al); |
|---|
| 277 | n/a | next_arg += (size_al + 3) / 4; |
|---|
| 278 | n/a | #else |
|---|
| 279 | n/a | #error undefined architecture |
|---|
| 280 | n/a | #endif |
|---|
| 281 | n/a | break; |
|---|
| 282 | n/a | } |
|---|
| 283 | n/a | |
|---|
| 284 | n/a | case FFI_TYPE_INT: |
|---|
| 285 | n/a | case FFI_TYPE_UINT32: |
|---|
| 286 | n/a | case FFI_TYPE_SINT32: |
|---|
| 287 | n/a | gprvalue = *(unsigned*)*p_argv; |
|---|
| 288 | n/a | |
|---|
| 289 | n/a | putgpr: |
|---|
| 290 | n/a | *next_arg++ = gprvalue; |
|---|
| 291 | n/a | break; |
|---|
| 292 | n/a | |
|---|
| 293 | n/a | default: |
|---|
| 294 | n/a | break; |
|---|
| 295 | n/a | } |
|---|
| 296 | n/a | } |
|---|
| 297 | n/a | |
|---|
| 298 | n/a | /* Check that we didn't overrun the stack... */ |
|---|
| 299 | n/a | //FFI_ASSERT(gpr_base <= stacktop - ASM_NEEDS_REGISTERS); |
|---|
| 300 | n/a | //FFI_ASSERT((unsigned *)fpr_base |
|---|
| 301 | n/a | // <= stacktop - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS); |
|---|
| 302 | n/a | //FFI_ASSERT(flags & FLAG_4_GPR_ARGUMENTS || intarg_count <= 4); |
|---|
| 303 | n/a | } |
|---|
| 304 | n/a | |
|---|
| 305 | n/a | #if defined(__ppc64__) |
|---|
| 306 | n/a | |
|---|
| 307 | n/a | bool |
|---|
| 308 | n/a | ffi64_struct_contains_fp( |
|---|
| 309 | n/a | const ffi_type* inType) |
|---|
| 310 | n/a | { |
|---|
| 311 | n/a | bool containsFP = false; |
|---|
| 312 | n/a | unsigned int i; |
|---|
| 313 | n/a | |
|---|
| 314 | n/a | for (i = 0; inType->elements[i] != NULL && !containsFP; i++) |
|---|
| 315 | n/a | { |
|---|
| 316 | n/a | if (inType->elements[i]->type == FFI_TYPE_FLOAT || |
|---|
| 317 | n/a | inType->elements[i]->type == FFI_TYPE_DOUBLE || |
|---|
| 318 | n/a | inType->elements[i]->type == FFI_TYPE_LONGDOUBLE) |
|---|
| 319 | n/a | containsFP = true; |
|---|
| 320 | n/a | else if (inType->elements[i]->type == FFI_TYPE_STRUCT) |
|---|
| 321 | n/a | containsFP = ffi64_struct_contains_fp(inType->elements[i]); |
|---|
| 322 | n/a | } |
|---|
| 323 | n/a | |
|---|
| 324 | n/a | return containsFP; |
|---|
| 325 | n/a | } |
|---|
| 326 | n/a | |
|---|
| 327 | n/a | #endif // defined(__ppc64__) |
|---|
| 328 | n/a | |
|---|
| 329 | n/a | /* Perform machine dependent cif processing. */ |
|---|
| 330 | n/a | ffi_status |
|---|
| 331 | n/a | ffi_prep_cif_machdep( |
|---|
| 332 | n/a | ffi_cif* cif) |
|---|
| 333 | n/a | { |
|---|
| 334 | n/a | /* All this is for the DARWIN ABI. */ |
|---|
| 335 | n/a | int i; |
|---|
| 336 | n/a | ffi_type** ptr; |
|---|
| 337 | n/a | int intarg_count = 0; |
|---|
| 338 | n/a | int fparg_count = 0; |
|---|
| 339 | n/a | unsigned int flags = 0; |
|---|
| 340 | n/a | unsigned int size_al = 0; |
|---|
| 341 | n/a | |
|---|
| 342 | n/a | /* All the machine-independent calculation of cif->bytes will be wrong. |
|---|
| 343 | n/a | Redo the calculation for DARWIN. */ |
|---|
| 344 | n/a | |
|---|
| 345 | n/a | /* Space for the frame pointer, callee's LR, CR, etc, and for |
|---|
| 346 | n/a | the asm's temp regs. */ |
|---|
| 347 | n/a | unsigned int bytes = (6 + ASM_NEEDS_REGISTERS) * sizeof(long); |
|---|
| 348 | n/a | |
|---|
| 349 | n/a | /* Return value handling. The rules are as follows: |
|---|
| 350 | n/a | - 32-bit (or less) integer values are returned in gpr3; |
|---|
| 351 | n/a | - Structures of size <= 4 bytes also returned in gpr3; |
|---|
| 352 | n/a | - 64-bit integer values and structures between 5 and 8 bytes are |
|---|
| 353 | n/a | returned in gpr3 and gpr4; |
|---|
| 354 | n/a | - Single/double FP values are returned in fpr1; |
|---|
| 355 | n/a | - Long double FP (if not equivalent to double) values are returned in |
|---|
| 356 | n/a | fpr1 and fpr2; |
|---|
| 357 | n/a | - Larger structures values are allocated space and a pointer is passed |
|---|
| 358 | n/a | as the first argument. */ |
|---|
| 359 | n/a | switch (cif->rtype->type) |
|---|
| 360 | n/a | { |
|---|
| 361 | n/a | #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE |
|---|
| 362 | n/a | case FFI_TYPE_LONGDOUBLE: |
|---|
| 363 | n/a | flags |= FLAG_RETURNS_128BITS; |
|---|
| 364 | n/a | flags |= FLAG_RETURNS_FP; |
|---|
| 365 | n/a | break; |
|---|
| 366 | n/a | #endif // FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE |
|---|
| 367 | n/a | |
|---|
| 368 | n/a | case FFI_TYPE_DOUBLE: |
|---|
| 369 | n/a | flags |= FLAG_RETURNS_64BITS; |
|---|
| 370 | n/a | /* Fall through. */ |
|---|
| 371 | n/a | case FFI_TYPE_FLOAT: |
|---|
| 372 | n/a | flags |= FLAG_RETURNS_FP; |
|---|
| 373 | n/a | break; |
|---|
| 374 | n/a | |
|---|
| 375 | n/a | #if defined(__ppc64__) |
|---|
| 376 | n/a | case FFI_TYPE_POINTER: |
|---|
| 377 | n/a | #endif |
|---|
| 378 | n/a | case FFI_TYPE_UINT64: |
|---|
| 379 | n/a | case FFI_TYPE_SINT64: |
|---|
| 380 | n/a | flags |= FLAG_RETURNS_64BITS; |
|---|
| 381 | n/a | break; |
|---|
| 382 | n/a | |
|---|
| 383 | n/a | case FFI_TYPE_STRUCT: |
|---|
| 384 | n/a | { |
|---|
| 385 | n/a | #if defined(__ppc64__) |
|---|
| 386 | n/a | |
|---|
| 387 | n/a | if (ffi64_stret_needs_ptr(cif->rtype, NULL, NULL)) |
|---|
| 388 | n/a | { |
|---|
| 389 | n/a | flags |= FLAG_RETVAL_REFERENCE; |
|---|
| 390 | n/a | flags |= FLAG_RETURNS_NOTHING; |
|---|
| 391 | n/a | intarg_count++; |
|---|
| 392 | n/a | } |
|---|
| 393 | n/a | else |
|---|
| 394 | n/a | { |
|---|
| 395 | n/a | flags |= FLAG_RETURNS_STRUCT; |
|---|
| 396 | n/a | |
|---|
| 397 | n/a | if (ffi64_struct_contains_fp(cif->rtype)) |
|---|
| 398 | n/a | flags |= FLAG_STRUCT_CONTAINS_FP; |
|---|
| 399 | n/a | } |
|---|
| 400 | n/a | |
|---|
| 401 | n/a | #elif defined(__ppc__) |
|---|
| 402 | n/a | |
|---|
| 403 | n/a | flags |= FLAG_RETVAL_REFERENCE; |
|---|
| 404 | n/a | flags |= FLAG_RETURNS_NOTHING; |
|---|
| 405 | n/a | intarg_count++; |
|---|
| 406 | n/a | |
|---|
| 407 | n/a | #else |
|---|
| 408 | n/a | #error undefined architecture |
|---|
| 409 | n/a | #endif |
|---|
| 410 | n/a | break; |
|---|
| 411 | n/a | } |
|---|
| 412 | n/a | |
|---|
| 413 | n/a | case FFI_TYPE_VOID: |
|---|
| 414 | n/a | flags |= FLAG_RETURNS_NOTHING; |
|---|
| 415 | n/a | break; |
|---|
| 416 | n/a | |
|---|
| 417 | n/a | default: |
|---|
| 418 | n/a | /* Returns 32-bit integer, or similar. Nothing to do here. */ |
|---|
| 419 | n/a | break; |
|---|
| 420 | n/a | } |
|---|
| 421 | n/a | |
|---|
| 422 | n/a | /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the |
|---|
| 423 | n/a | first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest |
|---|
| 424 | n/a | goes on the stack. Structures are passed as a pointer to a copy of |
|---|
| 425 | n/a | the structure. Stuff on the stack needs to keep proper alignment. */ |
|---|
| 426 | n/a | for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++) |
|---|
| 427 | n/a | { |
|---|
| 428 | n/a | switch ((*ptr)->type) |
|---|
| 429 | n/a | { |
|---|
| 430 | n/a | case FFI_TYPE_FLOAT: |
|---|
| 431 | n/a | case FFI_TYPE_DOUBLE: |
|---|
| 432 | n/a | fparg_count++; |
|---|
| 433 | n/a | /* If this FP arg is going on the stack, it must be |
|---|
| 434 | n/a | 8-byte-aligned. */ |
|---|
| 435 | n/a | if (fparg_count > NUM_FPR_ARG_REGISTERS |
|---|
| 436 | n/a | && intarg_count % 2 != 0) |
|---|
| 437 | n/a | intarg_count++; |
|---|
| 438 | n/a | break; |
|---|
| 439 | n/a | |
|---|
| 440 | n/a | #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE |
|---|
| 441 | n/a | case FFI_TYPE_LONGDOUBLE: |
|---|
| 442 | n/a | fparg_count += 2; |
|---|
| 443 | n/a | /* If this FP arg is going on the stack, it must be |
|---|
| 444 | n/a | 8-byte-aligned. */ |
|---|
| 445 | n/a | |
|---|
| 446 | n/a | if ( |
|---|
| 447 | n/a | #if defined(__ppc64__) |
|---|
| 448 | n/a | fparg_count > NUM_FPR_ARG_REGISTERS + 1 |
|---|
| 449 | n/a | #elif defined(__ppc__) |
|---|
| 450 | n/a | fparg_count > NUM_FPR_ARG_REGISTERS |
|---|
| 451 | n/a | #else |
|---|
| 452 | n/a | #error undefined architecture |
|---|
| 453 | n/a | #endif |
|---|
| 454 | n/a | && intarg_count % 2 != 0) |
|---|
| 455 | n/a | intarg_count++; |
|---|
| 456 | n/a | |
|---|
| 457 | n/a | intarg_count += 2; |
|---|
| 458 | n/a | break; |
|---|
| 459 | n/a | #endif // FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE |
|---|
| 460 | n/a | |
|---|
| 461 | n/a | case FFI_TYPE_UINT64: |
|---|
| 462 | n/a | case FFI_TYPE_SINT64: |
|---|
| 463 | n/a | /* 'long long' arguments are passed as two words, but |
|---|
| 464 | n/a | either both words must fit in registers or both go |
|---|
| 465 | n/a | on the stack. If they go on the stack, they must |
|---|
| 466 | n/a | be 8-byte-aligned. */ |
|---|
| 467 | n/a | if (intarg_count == NUM_GPR_ARG_REGISTERS - 1 |
|---|
| 468 | n/a | || (intarg_count >= NUM_GPR_ARG_REGISTERS |
|---|
| 469 | n/a | && intarg_count % 2 != 0)) |
|---|
| 470 | n/a | intarg_count++; |
|---|
| 471 | n/a | |
|---|
| 472 | n/a | intarg_count += MODE_CHOICE(2,1); |
|---|
| 473 | n/a | |
|---|
| 474 | n/a | break; |
|---|
| 475 | n/a | |
|---|
| 476 | n/a | case FFI_TYPE_STRUCT: |
|---|
| 477 | n/a | size_al = (*ptr)->size; |
|---|
| 478 | n/a | /* If the first member of the struct is a double, then align |
|---|
| 479 | n/a | the struct to double-word. */ |
|---|
| 480 | n/a | if ((*ptr)->elements[0]->type == FFI_TYPE_DOUBLE) |
|---|
| 481 | n/a | size_al = ALIGN((*ptr)->size, 8); |
|---|
| 482 | n/a | |
|---|
| 483 | n/a | #if defined(__ppc64__) |
|---|
| 484 | n/a | // Look for FP struct members. |
|---|
| 485 | n/a | unsigned int j; |
|---|
| 486 | n/a | |
|---|
| 487 | n/a | for (j = 0; (*ptr)->elements[j] != NULL; j++) |
|---|
| 488 | n/a | { |
|---|
| 489 | n/a | if ((*ptr)->elements[j]->type == FFI_TYPE_FLOAT || |
|---|
| 490 | n/a | (*ptr)->elements[j]->type == FFI_TYPE_DOUBLE) |
|---|
| 491 | n/a | { |
|---|
| 492 | n/a | fparg_count++; |
|---|
| 493 | n/a | |
|---|
| 494 | n/a | if (fparg_count > NUM_FPR_ARG_REGISTERS) |
|---|
| 495 | n/a | intarg_count++; |
|---|
| 496 | n/a | } |
|---|
| 497 | n/a | else if ((*ptr)->elements[j]->type == FFI_TYPE_LONGDOUBLE) |
|---|
| 498 | n/a | { |
|---|
| 499 | n/a | fparg_count += 2; |
|---|
| 500 | n/a | |
|---|
| 501 | n/a | if (fparg_count > NUM_FPR_ARG_REGISTERS + 1) |
|---|
| 502 | n/a | intarg_count += 2; |
|---|
| 503 | n/a | } |
|---|
| 504 | n/a | else |
|---|
| 505 | n/a | intarg_count++; |
|---|
| 506 | n/a | } |
|---|
| 507 | n/a | #elif defined(__ppc__) |
|---|
| 508 | n/a | intarg_count += (size_al + 3) / 4; |
|---|
| 509 | n/a | #else |
|---|
| 510 | n/a | #error undefined architecture |
|---|
| 511 | n/a | #endif |
|---|
| 512 | n/a | |
|---|
| 513 | n/a | break; |
|---|
| 514 | n/a | |
|---|
| 515 | n/a | default: |
|---|
| 516 | n/a | /* Everything else is passed as a 4/8-byte word in a GPR, either |
|---|
| 517 | n/a | the object itself or a pointer to it. */ |
|---|
| 518 | n/a | intarg_count++; |
|---|
| 519 | n/a | break; |
|---|
| 520 | n/a | } |
|---|
| 521 | n/a | } |
|---|
| 522 | n/a | |
|---|
| 523 | n/a | /* Space for the FPR registers, if needed. */ |
|---|
| 524 | n/a | if (fparg_count != 0) |
|---|
| 525 | n/a | { |
|---|
| 526 | n/a | flags |= FLAG_FP_ARGUMENTS; |
|---|
| 527 | n/a | #if defined(__ppc64__) |
|---|
| 528 | n/a | bytes += (NUM_FPR_ARG_REGISTERS + 1) * sizeof(double); |
|---|
| 529 | n/a | #elif defined(__ppc__) |
|---|
| 530 | n/a | bytes += NUM_FPR_ARG_REGISTERS * sizeof(double); |
|---|
| 531 | n/a | #else |
|---|
| 532 | n/a | #error undefined architecture |
|---|
| 533 | n/a | #endif |
|---|
| 534 | n/a | } |
|---|
| 535 | n/a | |
|---|
| 536 | n/a | /* Stack space. */ |
|---|
| 537 | n/a | #if defined(__ppc64__) |
|---|
| 538 | n/a | if ((intarg_count + fparg_count) > NUM_GPR_ARG_REGISTERS) |
|---|
| 539 | n/a | bytes += (intarg_count + fparg_count) * sizeof(long); |
|---|
| 540 | n/a | #elif defined(__ppc__) |
|---|
| 541 | n/a | if ((intarg_count + 2 * fparg_count) > NUM_GPR_ARG_REGISTERS) |
|---|
| 542 | n/a | bytes += (intarg_count + 2 * fparg_count) * sizeof(long); |
|---|
| 543 | n/a | #else |
|---|
| 544 | n/a | #error undefined architecture |
|---|
| 545 | n/a | #endif |
|---|
| 546 | n/a | else |
|---|
| 547 | n/a | bytes += NUM_GPR_ARG_REGISTERS * sizeof(long); |
|---|
| 548 | n/a | |
|---|
| 549 | n/a | /* The stack space allocated needs to be a multiple of 16/32 bytes. */ |
|---|
| 550 | n/a | bytes = SF_ROUND(bytes); |
|---|
| 551 | n/a | |
|---|
| 552 | n/a | cif->flags = flags; |
|---|
| 553 | n/a | cif->bytes = bytes; |
|---|
| 554 | n/a | |
|---|
| 555 | n/a | return FFI_OK; |
|---|
| 556 | n/a | } |
|---|
| 557 | n/a | |
|---|
| 558 | n/a | /*@-declundef@*/ |
|---|
| 559 | n/a | /*@-exportheader@*/ |
|---|
| 560 | n/a | extern void |
|---|
| 561 | n/a | ffi_call_AIX( |
|---|
| 562 | n/a | /*@out@*/ extended_cif*, |
|---|
| 563 | n/a | unsigned, |
|---|
| 564 | n/a | unsigned, |
|---|
| 565 | n/a | /*@out@*/ unsigned*, |
|---|
| 566 | n/a | void (*fn)(void), |
|---|
| 567 | n/a | void (*fn2)(extended_cif*, unsigned *const)); |
|---|
| 568 | n/a | |
|---|
| 569 | n/a | extern void |
|---|
| 570 | n/a | ffi_call_DARWIN( |
|---|
| 571 | n/a | /*@out@*/ extended_cif*, |
|---|
| 572 | n/a | unsigned long, |
|---|
| 573 | n/a | unsigned, |
|---|
| 574 | n/a | /*@out@*/ unsigned*, |
|---|
| 575 | n/a | void (*fn)(void), |
|---|
| 576 | n/a | void (*fn2)(extended_cif*, unsigned *const)); |
|---|
| 577 | n/a | /*@=declundef@*/ |
|---|
| 578 | n/a | /*@=exportheader@*/ |
|---|
| 579 | n/a | |
|---|
| 580 | n/a | void |
|---|
| 581 | n/a | ffi_call( |
|---|
| 582 | n/a | /*@dependent@*/ ffi_cif* cif, |
|---|
| 583 | n/a | void (*fn)(void), |
|---|
| 584 | n/a | /*@out@*/ void* rvalue, |
|---|
| 585 | n/a | /*@dependent@*/ void** avalue) |
|---|
| 586 | n/a | { |
|---|
| 587 | n/a | extended_cif ecif; |
|---|
| 588 | n/a | |
|---|
| 589 | n/a | ecif.cif = cif; |
|---|
| 590 | n/a | ecif.avalue = avalue; |
|---|
| 591 | n/a | |
|---|
| 592 | n/a | /* If the return value is a struct and we don't have a return |
|---|
| 593 | n/a | value address then we need to make one. */ |
|---|
| 594 | n/a | if ((rvalue == NULL) && |
|---|
| 595 | n/a | (cif->rtype->type == FFI_TYPE_STRUCT)) |
|---|
| 596 | n/a | { |
|---|
| 597 | n/a | /*@-sysunrecog@*/ |
|---|
| 598 | n/a | ecif.rvalue = alloca(cif->rtype->size); |
|---|
| 599 | n/a | /*@=sysunrecog@*/ |
|---|
| 600 | n/a | } |
|---|
| 601 | n/a | else |
|---|
| 602 | n/a | ecif.rvalue = rvalue; |
|---|
| 603 | n/a | |
|---|
| 604 | n/a | switch (cif->abi) |
|---|
| 605 | n/a | { |
|---|
| 606 | n/a | case FFI_AIX: |
|---|
| 607 | n/a | /*@-usedef@*/ |
|---|
| 608 | n/a | ffi_call_AIX(&ecif, -cif->bytes, |
|---|
| 609 | n/a | cif->flags, ecif.rvalue, fn, ffi_prep_args); |
|---|
| 610 | n/a | /*@=usedef@*/ |
|---|
| 611 | n/a | break; |
|---|
| 612 | n/a | |
|---|
| 613 | n/a | case FFI_DARWIN: |
|---|
| 614 | n/a | /*@-usedef@*/ |
|---|
| 615 | n/a | ffi_call_DARWIN(&ecif, -(long)cif->bytes, |
|---|
| 616 | n/a | cif->flags, ecif.rvalue, fn, ffi_prep_args); |
|---|
| 617 | n/a | /*@=usedef@*/ |
|---|
| 618 | n/a | break; |
|---|
| 619 | n/a | |
|---|
| 620 | n/a | default: |
|---|
| 621 | n/a | FFI_ASSERT(0); |
|---|
| 622 | n/a | break; |
|---|
| 623 | n/a | } |
|---|
| 624 | n/a | } |
|---|
| 625 | n/a | |
|---|
| 626 | n/a | /* here I'd like to add the stack frame layout we use in darwin_closure.S |
|---|
| 627 | n/a | and aix_clsoure.S |
|---|
| 628 | n/a | |
|---|
| 629 | n/a | SP previous -> +---------------------------------------+ <--- child frame |
|---|
| 630 | n/a | | back chain to caller 4 | |
|---|
| 631 | n/a | +---------------------------------------+ 4 |
|---|
| 632 | n/a | | saved CR 4 | |
|---|
| 633 | n/a | +---------------------------------------+ 8 |
|---|
| 634 | n/a | | saved LR 4 | |
|---|
| 635 | n/a | +---------------------------------------+ 12 |
|---|
| 636 | n/a | | reserved for compilers 4 | |
|---|
| 637 | n/a | +---------------------------------------+ 16 |
|---|
| 638 | n/a | | reserved for binders 4 | |
|---|
| 639 | n/a | +---------------------------------------+ 20 |
|---|
| 640 | n/a | | saved TOC pointer 4 | |
|---|
| 641 | n/a | +---------------------------------------+ 24 |
|---|
| 642 | n/a | | always reserved 8*4=32 (previous GPRs)| |
|---|
| 643 | n/a | | according to the linkage convention | |
|---|
| 644 | n/a | | from AIX | |
|---|
| 645 | n/a | +---------------------------------------+ 56 |
|---|
| 646 | n/a | | our FPR area 13*8=104 | |
|---|
| 647 | n/a | | f1 | |
|---|
| 648 | n/a | | . | |
|---|
| 649 | n/a | | f13 | |
|---|
| 650 | n/a | +---------------------------------------+ 160 |
|---|
| 651 | n/a | | result area 8 | |
|---|
| 652 | n/a | +---------------------------------------+ 168 |
|---|
| 653 | n/a | | alignement to the next multiple of 16 | |
|---|
| 654 | n/a | SP current --> +---------------------------------------+ 176 <- parent frame |
|---|
| 655 | n/a | | back chain to caller 4 | |
|---|
| 656 | n/a | +---------------------------------------+ 180 |
|---|
| 657 | n/a | | saved CR 4 | |
|---|
| 658 | n/a | +---------------------------------------+ 184 |
|---|
| 659 | n/a | | saved LR 4 | |
|---|
| 660 | n/a | +---------------------------------------+ 188 |
|---|
| 661 | n/a | | reserved for compilers 4 | |
|---|
| 662 | n/a | +---------------------------------------+ 192 |
|---|
| 663 | n/a | | reserved for binders 4 | |
|---|
| 664 | n/a | +---------------------------------------+ 196 |
|---|
| 665 | n/a | | saved TOC pointer 4 | |
|---|
| 666 | n/a | +---------------------------------------+ 200 |
|---|
| 667 | n/a | | always reserved 8*4=32 we store our | |
|---|
| 668 | n/a | | GPRs here | |
|---|
| 669 | n/a | | r3 | |
|---|
| 670 | n/a | | . | |
|---|
| 671 | n/a | | r10 | |
|---|
| 672 | n/a | +---------------------------------------+ 232 |
|---|
| 673 | n/a | | overflow part | |
|---|
| 674 | n/a | +---------------------------------------+ xxx |
|---|
| 675 | n/a | | ???? | |
|---|
| 676 | n/a | +---------------------------------------+ xxx |
|---|
| 677 | n/a | */ |
|---|
| 678 | n/a | |
|---|
| 679 | n/a | #if !defined(POWERPC_DARWIN) |
|---|
| 680 | n/a | |
|---|
| 681 | n/a | #define MIN_LINE_SIZE 32 |
|---|
| 682 | n/a | |
|---|
| 683 | n/a | static void |
|---|
| 684 | n/a | flush_icache( |
|---|
| 685 | n/a | char* addr) |
|---|
| 686 | n/a | { |
|---|
| 687 | n/a | #ifndef _AIX |
|---|
| 688 | n/a | __asm__ volatile ( |
|---|
| 689 | n/a | "dcbf 0,%0\n" |
|---|
| 690 | n/a | "sync\n" |
|---|
| 691 | n/a | "icbi 0,%0\n" |
|---|
| 692 | n/a | "sync\n" |
|---|
| 693 | n/a | "isync" |
|---|
| 694 | n/a | : : "r" (addr) : "memory"); |
|---|
| 695 | n/a | #endif |
|---|
| 696 | n/a | } |
|---|
| 697 | n/a | |
|---|
| 698 | n/a | static void |
|---|
| 699 | n/a | flush_range( |
|---|
| 700 | n/a | char* addr, |
|---|
| 701 | n/a | int size) |
|---|
| 702 | n/a | { |
|---|
| 703 | n/a | int i; |
|---|
| 704 | n/a | |
|---|
| 705 | n/a | for (i = 0; i < size; i += MIN_LINE_SIZE) |
|---|
| 706 | n/a | flush_icache(addr + i); |
|---|
| 707 | n/a | |
|---|
| 708 | n/a | flush_icache(addr + size - 1); |
|---|
| 709 | n/a | } |
|---|
| 710 | n/a | |
|---|
| 711 | n/a | #endif // !defined(POWERPC_DARWIN) |
|---|
| 712 | n/a | |
|---|
| 713 | n/a | ffi_status |
|---|
| 714 | n/a | ffi_prep_closure( |
|---|
| 715 | n/a | ffi_closure* closure, |
|---|
| 716 | n/a | ffi_cif* cif, |
|---|
| 717 | n/a | void (*fun)(ffi_cif*, void*, void**, void*), |
|---|
| 718 | n/a | void* user_data) |
|---|
| 719 | n/a | { |
|---|
| 720 | n/a | switch (cif->abi) |
|---|
| 721 | n/a | { |
|---|
| 722 | n/a | case FFI_DARWIN: |
|---|
| 723 | n/a | { |
|---|
| 724 | n/a | FFI_ASSERT (cif->abi == FFI_DARWIN); |
|---|
| 725 | n/a | |
|---|
| 726 | n/a | unsigned int* tramp = (unsigned int*)&closure->tramp[0]; |
|---|
| 727 | n/a | |
|---|
| 728 | n/a | #if defined(__ppc64__) |
|---|
| 729 | n/a | tramp[0] = 0x7c0802a6; // mflr r0 |
|---|
| 730 | n/a | tramp[1] = 0x429f0005; // bcl 20,31,+0x8 |
|---|
| 731 | n/a | tramp[2] = 0x7d6802a6; // mflr r11 |
|---|
| 732 | n/a | tramp[3] = 0x7c0803a6; // mtlr r0 |
|---|
| 733 | n/a | tramp[4] = 0xe98b0018; // ld r12,24(r11) |
|---|
| 734 | n/a | tramp[5] = 0x7d8903a6; // mtctr r12 |
|---|
| 735 | n/a | tramp[6] = 0xe96b0020; // ld r11,32(r11) |
|---|
| 736 | n/a | tramp[7] = 0x4e800420; // bctr |
|---|
| 737 | n/a | *(unsigned long*)&tramp[8] = (unsigned long)ffi_closure_ASM; |
|---|
| 738 | n/a | *(unsigned long*)&tramp[10] = (unsigned long)closure; |
|---|
| 739 | n/a | #elif defined(__ppc__) |
|---|
| 740 | n/a | tramp[0] = 0x7c0802a6; // mflr r0 |
|---|
| 741 | n/a | tramp[1] = 0x429f0005; // bcl 20,31,+0x8 |
|---|
| 742 | n/a | tramp[2] = 0x7d6802a6; // mflr r11 |
|---|
| 743 | n/a | tramp[3] = 0x7c0803a6; // mtlr r0 |
|---|
| 744 | n/a | tramp[4] = 0x818b0018; // lwz r12,24(r11) |
|---|
| 745 | n/a | tramp[5] = 0x7d8903a6; // mtctr r12 |
|---|
| 746 | n/a | tramp[6] = 0x816b001c; // lwz r11,28(r11) |
|---|
| 747 | n/a | tramp[7] = 0x4e800420; // bctr |
|---|
| 748 | n/a | tramp[8] = (unsigned long)ffi_closure_ASM; |
|---|
| 749 | n/a | tramp[9] = (unsigned long)closure; |
|---|
| 750 | n/a | #else |
|---|
| 751 | n/a | #error undefined architecture |
|---|
| 752 | n/a | #endif |
|---|
| 753 | n/a | |
|---|
| 754 | n/a | closure->cif = cif; |
|---|
| 755 | n/a | closure->fun = fun; |
|---|
| 756 | n/a | closure->user_data = user_data; |
|---|
| 757 | n/a | |
|---|
| 758 | n/a | // Flush the icache. Only necessary on Darwin. |
|---|
| 759 | n/a | #if defined(POWERPC_DARWIN) |
|---|
| 760 | n/a | sys_icache_invalidate(closure->tramp, FFI_TRAMPOLINE_SIZE); |
|---|
| 761 | n/a | #else |
|---|
| 762 | n/a | flush_range(closure->tramp, FFI_TRAMPOLINE_SIZE); |
|---|
| 763 | n/a | #endif |
|---|
| 764 | n/a | |
|---|
| 765 | n/a | break; |
|---|
| 766 | n/a | } |
|---|
| 767 | n/a | |
|---|
| 768 | n/a | case FFI_AIX: |
|---|
| 769 | n/a | { |
|---|
| 770 | n/a | FFI_ASSERT (cif->abi == FFI_AIX); |
|---|
| 771 | n/a | |
|---|
| 772 | n/a | ffi_aix_trampoline_struct* tramp_aix = |
|---|
| 773 | n/a | (ffi_aix_trampoline_struct*)(closure->tramp); |
|---|
| 774 | n/a | aix_fd* fd = (aix_fd*)(void*)ffi_closure_ASM; |
|---|
| 775 | n/a | |
|---|
| 776 | n/a | tramp_aix->code_pointer = fd->code_pointer; |
|---|
| 777 | n/a | tramp_aix->toc = fd->toc; |
|---|
| 778 | n/a | tramp_aix->static_chain = closure; |
|---|
| 779 | n/a | closure->cif = cif; |
|---|
| 780 | n/a | closure->fun = fun; |
|---|
| 781 | n/a | closure->user_data = user_data; |
|---|
| 782 | n/a | break; |
|---|
| 783 | n/a | } |
|---|
| 784 | n/a | |
|---|
| 785 | n/a | default: |
|---|
| 786 | n/a | return FFI_BAD_ABI; |
|---|
| 787 | n/a | } |
|---|
| 788 | n/a | |
|---|
| 789 | n/a | return FFI_OK; |
|---|
| 790 | n/a | } |
|---|
| 791 | n/a | |
|---|
| 792 | n/a | #if defined(__ppc__) |
|---|
| 793 | n/a | typedef double ldbits[2]; |
|---|
| 794 | n/a | |
|---|
| 795 | n/a | typedef union |
|---|
| 796 | n/a | { |
|---|
| 797 | n/a | ldbits lb; |
|---|
| 798 | n/a | long double ld; |
|---|
| 799 | n/a | } ldu; |
|---|
| 800 | n/a | #endif |
|---|
| 801 | n/a | |
|---|
| 802 | n/a | typedef union |
|---|
| 803 | n/a | { |
|---|
| 804 | n/a | float f; |
|---|
| 805 | n/a | double d; |
|---|
| 806 | n/a | } ffi_dblfl; |
|---|
| 807 | n/a | |
|---|
| 808 | n/a | /* The trampoline invokes ffi_closure_ASM, and on entry, r11 holds the |
|---|
| 809 | n/a | address of the closure. After storing the registers that could possibly |
|---|
| 810 | n/a | contain parameters to be passed into the stack frame and setting up space |
|---|
| 811 | n/a | for a return value, ffi_closure_ASM invokes the following helper function |
|---|
| 812 | n/a | to do most of the work. */ |
|---|
| 813 | n/a | int |
|---|
| 814 | n/a | ffi_closure_helper_DARWIN( |
|---|
| 815 | n/a | ffi_closure* closure, |
|---|
| 816 | n/a | void* rvalue, |
|---|
| 817 | n/a | unsigned long* pgr, |
|---|
| 818 | n/a | ffi_dblfl* pfr) |
|---|
| 819 | n/a | { |
|---|
| 820 | n/a | /* rvalue is the pointer to space for return value in closure assembly |
|---|
| 821 | n/a | pgr is the pointer to where r3-r10 are stored in ffi_closure_ASM |
|---|
| 822 | n/a | pfr is the pointer to where f1-f13 are stored in ffi_closure_ASM. */ |
|---|
| 823 | n/a | |
|---|
| 824 | n/a | #if defined(__ppc__) |
|---|
| 825 | n/a | ldu temp_ld; |
|---|
| 826 | n/a | #endif |
|---|
| 827 | n/a | |
|---|
| 828 | n/a | double temp; |
|---|
| 829 | n/a | unsigned int i; |
|---|
| 830 | n/a | unsigned int nf = 0; /* number of FPRs already used. */ |
|---|
| 831 | n/a | unsigned int ng = 0; /* number of GPRs already used. */ |
|---|
| 832 | n/a | ffi_cif* cif = closure->cif; |
|---|
| 833 | n/a | long avn = cif->nargs; |
|---|
| 834 | n/a | void** avalue = alloca(cif->nargs * sizeof(void*)); |
|---|
| 835 | n/a | ffi_type** arg_types = cif->arg_types; |
|---|
| 836 | n/a | |
|---|
| 837 | n/a | /* Copy the caller's structure return value address so that the closure |
|---|
| 838 | n/a | returns the data directly to the caller. */ |
|---|
| 839 | n/a | #if defined(__ppc64__) |
|---|
| 840 | n/a | if (cif->rtype->type == FFI_TYPE_STRUCT && |
|---|
| 841 | n/a | ffi64_stret_needs_ptr(cif->rtype, NULL, NULL)) |
|---|
| 842 | n/a | #elif defined(__ppc__) |
|---|
| 843 | n/a | if (cif->rtype->type == FFI_TYPE_STRUCT) |
|---|
| 844 | n/a | #else |
|---|
| 845 | n/a | #error undefined architecture |
|---|
| 846 | n/a | #endif |
|---|
| 847 | n/a | { |
|---|
| 848 | n/a | rvalue = (void*)*pgr; |
|---|
| 849 | n/a | pgr++; |
|---|
| 850 | n/a | ng++; |
|---|
| 851 | n/a | } |
|---|
| 852 | n/a | |
|---|
| 853 | n/a | /* Grab the addresses of the arguments from the stack frame. */ |
|---|
| 854 | n/a | for (i = 0; i < avn; i++) |
|---|
| 855 | n/a | { |
|---|
| 856 | n/a | switch (arg_types[i]->type) |
|---|
| 857 | n/a | { |
|---|
| 858 | n/a | case FFI_TYPE_SINT8: |
|---|
| 859 | n/a | case FFI_TYPE_UINT8: |
|---|
| 860 | n/a | avalue[i] = (char*)pgr + MODE_CHOICE(3,7); |
|---|
| 861 | n/a | ng++; |
|---|
| 862 | n/a | pgr++; |
|---|
| 863 | n/a | break; |
|---|
| 864 | n/a | |
|---|
| 865 | n/a | case FFI_TYPE_SINT16: |
|---|
| 866 | n/a | case FFI_TYPE_UINT16: |
|---|
| 867 | n/a | avalue[i] = (char*)pgr + MODE_CHOICE(2,6); |
|---|
| 868 | n/a | ng++; |
|---|
| 869 | n/a | pgr++; |
|---|
| 870 | n/a | break; |
|---|
| 871 | n/a | |
|---|
| 872 | n/a | #if defined(__ppc__) |
|---|
| 873 | n/a | case FFI_TYPE_POINTER: |
|---|
| 874 | n/a | #endif |
|---|
| 875 | n/a | case FFI_TYPE_SINT32: |
|---|
| 876 | n/a | case FFI_TYPE_UINT32: |
|---|
| 877 | n/a | avalue[i] = (char*)pgr + MODE_CHOICE(0,4); |
|---|
| 878 | n/a | ng++; |
|---|
| 879 | n/a | pgr++; |
|---|
| 880 | n/a | |
|---|
| 881 | n/a | break; |
|---|
| 882 | n/a | |
|---|
| 883 | n/a | case FFI_TYPE_STRUCT: |
|---|
| 884 | n/a | if (cif->abi == FFI_DARWIN) |
|---|
| 885 | n/a | { |
|---|
| 886 | n/a | #if defined(__ppc64__) |
|---|
| 887 | n/a | unsigned int gprSize = 0; |
|---|
| 888 | n/a | unsigned int fprSize = 0; |
|---|
| 889 | n/a | unsigned int savedFPRSize = fprSize; |
|---|
| 890 | n/a | |
|---|
| 891 | n/a | avalue[i] = alloca(arg_types[i]->size); |
|---|
| 892 | n/a | ffi64_struct_to_ram_form(arg_types[i], (const char*)pgr, |
|---|
| 893 | n/a | &gprSize, (const char*)pfr, &fprSize, &nf, avalue[i], NULL); |
|---|
| 894 | n/a | |
|---|
| 895 | n/a | ng += gprSize / sizeof(long); |
|---|
| 896 | n/a | pgr += gprSize / sizeof(long); |
|---|
| 897 | n/a | pfr += (fprSize - savedFPRSize) / sizeof(double); |
|---|
| 898 | n/a | |
|---|
| 899 | n/a | #elif defined(__ppc__) |
|---|
| 900 | n/a | /* Structures that match the basic modes (QI 1 byte, HI 2 bytes, |
|---|
| 901 | n/a | SI 4 bytes) are aligned as if they were those modes. */ |
|---|
| 902 | n/a | unsigned int size_al = size_al = arg_types[i]->size; |
|---|
| 903 | n/a | |
|---|
| 904 | n/a | /* If the first member of the struct is a double, then align |
|---|
| 905 | n/a | the struct to double-word. */ |
|---|
| 906 | n/a | if (arg_types[i]->elements[0]->type == FFI_TYPE_DOUBLE) |
|---|
| 907 | n/a | size_al = ALIGN(arg_types[i]->size, 8); |
|---|
| 908 | n/a | |
|---|
| 909 | n/a | if (size_al < 3) |
|---|
| 910 | n/a | avalue[i] = (void*)pgr + MODE_CHOICE(4,8) - size_al; |
|---|
| 911 | n/a | else |
|---|
| 912 | n/a | avalue[i] = (void*)pgr; |
|---|
| 913 | n/a | |
|---|
| 914 | n/a | ng += (size_al + 3) / sizeof(long); |
|---|
| 915 | n/a | pgr += (size_al + 3) / sizeof(long); |
|---|
| 916 | n/a | #else |
|---|
| 917 | n/a | #error undefined architecture |
|---|
| 918 | n/a | #endif |
|---|
| 919 | n/a | } |
|---|
| 920 | n/a | |
|---|
| 921 | n/a | break; |
|---|
| 922 | n/a | |
|---|
| 923 | n/a | #if defined(__ppc64__) |
|---|
| 924 | n/a | case FFI_TYPE_POINTER: |
|---|
| 925 | n/a | #endif |
|---|
| 926 | n/a | case FFI_TYPE_SINT64: |
|---|
| 927 | n/a | case FFI_TYPE_UINT64: |
|---|
| 928 | n/a | /* Long long ints are passed in 1 or 2 GPRs. */ |
|---|
| 929 | n/a | avalue[i] = pgr; |
|---|
| 930 | n/a | ng += MODE_CHOICE(2,1); |
|---|
| 931 | n/a | pgr += MODE_CHOICE(2,1); |
|---|
| 932 | n/a | |
|---|
| 933 | n/a | break; |
|---|
| 934 | n/a | |
|---|
| 935 | n/a | case FFI_TYPE_FLOAT: |
|---|
| 936 | n/a | /* A float value consumes a GPR. |
|---|
| 937 | n/a | There are 13 64-bit floating point registers. */ |
|---|
| 938 | n/a | if (nf < NUM_FPR_ARG_REGISTERS) |
|---|
| 939 | n/a | { |
|---|
| 940 | n/a | temp = pfr->d; |
|---|
| 941 | n/a | pfr->f = (float)temp; |
|---|
| 942 | n/a | avalue[i] = pfr; |
|---|
| 943 | n/a | pfr++; |
|---|
| 944 | n/a | } |
|---|
| 945 | n/a | else |
|---|
| 946 | n/a | avalue[i] = pgr; |
|---|
| 947 | n/a | |
|---|
| 948 | n/a | nf++; |
|---|
| 949 | n/a | ng++; |
|---|
| 950 | n/a | pgr++; |
|---|
| 951 | n/a | break; |
|---|
| 952 | n/a | |
|---|
| 953 | n/a | case FFI_TYPE_DOUBLE: |
|---|
| 954 | n/a | /* A double value consumes one or two GPRs. |
|---|
| 955 | n/a | There are 13 64bit floating point registers. */ |
|---|
| 956 | n/a | if (nf < NUM_FPR_ARG_REGISTERS) |
|---|
| 957 | n/a | { |
|---|
| 958 | n/a | avalue[i] = pfr; |
|---|
| 959 | n/a | pfr++; |
|---|
| 960 | n/a | } |
|---|
| 961 | n/a | else |
|---|
| 962 | n/a | avalue[i] = pgr; |
|---|
| 963 | n/a | |
|---|
| 964 | n/a | nf++; |
|---|
| 965 | n/a | ng += MODE_CHOICE(2,1); |
|---|
| 966 | n/a | pgr += MODE_CHOICE(2,1); |
|---|
| 967 | n/a | |
|---|
| 968 | n/a | break; |
|---|
| 969 | n/a | |
|---|
| 970 | n/a | #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE |
|---|
| 971 | n/a | |
|---|
| 972 | n/a | case FFI_TYPE_LONGDOUBLE: |
|---|
| 973 | n/a | #if defined(__ppc64__) |
|---|
| 974 | n/a | if (nf < NUM_FPR_ARG_REGISTERS) |
|---|
| 975 | n/a | { |
|---|
| 976 | n/a | avalue[i] = pfr; |
|---|
| 977 | n/a | pfr += 2; |
|---|
| 978 | n/a | } |
|---|
| 979 | n/a | #elif defined(__ppc__) |
|---|
| 980 | n/a | /* A long double value consumes 2/4 GPRs and 2 FPRs. |
|---|
| 981 | n/a | There are 13 64bit floating point registers. */ |
|---|
| 982 | n/a | if (nf < NUM_FPR_ARG_REGISTERS - 1) |
|---|
| 983 | n/a | { |
|---|
| 984 | n/a | avalue[i] = pfr; |
|---|
| 985 | n/a | pfr += 2; |
|---|
| 986 | n/a | } |
|---|
| 987 | n/a | /* Here we have the situation where one part of the long double |
|---|
| 988 | n/a | is stored in fpr13 and the other part is already on the stack. |
|---|
| 989 | n/a | We use a union to pass the long double to avalue[i]. */ |
|---|
| 990 | n/a | else if (nf == NUM_FPR_ARG_REGISTERS - 1) |
|---|
| 991 | n/a | { |
|---|
| 992 | n/a | memcpy (&temp_ld.lb[0], pfr, sizeof(temp_ld.lb[0])); |
|---|
| 993 | n/a | memcpy (&temp_ld.lb[1], pgr + 2, sizeof(temp_ld.lb[1])); |
|---|
| 994 | n/a | avalue[i] = &temp_ld.ld; |
|---|
| 995 | n/a | } |
|---|
| 996 | n/a | #else |
|---|
| 997 | n/a | #error undefined architecture |
|---|
| 998 | n/a | #endif |
|---|
| 999 | n/a | else |
|---|
| 1000 | n/a | avalue[i] = pgr; |
|---|
| 1001 | n/a | |
|---|
| 1002 | n/a | nf += 2; |
|---|
| 1003 | n/a | ng += MODE_CHOICE(4,2); |
|---|
| 1004 | n/a | pgr += MODE_CHOICE(4,2); |
|---|
| 1005 | n/a | |
|---|
| 1006 | n/a | break; |
|---|
| 1007 | n/a | |
|---|
| 1008 | n/a | #endif /* FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE */ |
|---|
| 1009 | n/a | |
|---|
| 1010 | n/a | default: |
|---|
| 1011 | n/a | FFI_ASSERT(0); |
|---|
| 1012 | n/a | break; |
|---|
| 1013 | n/a | } |
|---|
| 1014 | n/a | } |
|---|
| 1015 | n/a | |
|---|
| 1016 | n/a | (closure->fun)(cif, rvalue, avalue, closure->user_data); |
|---|
| 1017 | n/a | |
|---|
| 1018 | n/a | /* Tell ffi_closure_ASM to perform return type promotions. */ |
|---|
| 1019 | n/a | return cif->rtype->type; |
|---|
| 1020 | n/a | } |
|---|
| 1021 | n/a | |
|---|
| 1022 | n/a | #if defined(__ppc64__) |
|---|
| 1023 | n/a | |
|---|
| 1024 | n/a | /* ffi64_struct_to_ram_form |
|---|
| 1025 | n/a | |
|---|
| 1026 | n/a | Rebuild a struct's natural layout from buffers of concatenated registers. |
|---|
| 1027 | n/a | Return the number of registers used. |
|---|
| 1028 | n/a | inGPRs[0-7] == r3, inFPRs[0-7] == f1 ... |
|---|
| 1029 | n/a | */ |
|---|
| 1030 | n/a | void |
|---|
| 1031 | n/a | ffi64_struct_to_ram_form( |
|---|
| 1032 | n/a | const ffi_type* inType, |
|---|
| 1033 | n/a | const char* inGPRs, |
|---|
| 1034 | n/a | unsigned int* ioGPRMarker, |
|---|
| 1035 | n/a | const char* inFPRs, |
|---|
| 1036 | n/a | unsigned int* ioFPRMarker, |
|---|
| 1037 | n/a | unsigned int* ioFPRsUsed, |
|---|
| 1038 | n/a | char* outStruct, // caller-allocated |
|---|
| 1039 | n/a | unsigned int* ioStructMarker) |
|---|
| 1040 | n/a | { |
|---|
| 1041 | n/a | unsigned int srcGMarker = 0; |
|---|
| 1042 | n/a | unsigned int srcFMarker = 0; |
|---|
| 1043 | n/a | unsigned int savedFMarker = 0; |
|---|
| 1044 | n/a | unsigned int fprsUsed = 0; |
|---|
| 1045 | n/a | unsigned int savedFPRsUsed = 0; |
|---|
| 1046 | n/a | unsigned int destMarker = 0; |
|---|
| 1047 | n/a | |
|---|
| 1048 | n/a | static unsigned int recurseCount = 0; |
|---|
| 1049 | n/a | |
|---|
| 1050 | n/a | if (ioGPRMarker) |
|---|
| 1051 | n/a | srcGMarker = *ioGPRMarker; |
|---|
| 1052 | n/a | |
|---|
| 1053 | n/a | if (ioFPRMarker) |
|---|
| 1054 | n/a | { |
|---|
| 1055 | n/a | srcFMarker = *ioFPRMarker; |
|---|
| 1056 | n/a | savedFMarker = srcFMarker; |
|---|
| 1057 | n/a | } |
|---|
| 1058 | n/a | |
|---|
| 1059 | n/a | if (ioFPRsUsed) |
|---|
| 1060 | n/a | { |
|---|
| 1061 | n/a | fprsUsed = *ioFPRsUsed; |
|---|
| 1062 | n/a | savedFPRsUsed = fprsUsed; |
|---|
| 1063 | n/a | } |
|---|
| 1064 | n/a | |
|---|
| 1065 | n/a | if (ioStructMarker) |
|---|
| 1066 | n/a | destMarker = *ioStructMarker; |
|---|
| 1067 | n/a | |
|---|
| 1068 | n/a | size_t i; |
|---|
| 1069 | n/a | |
|---|
| 1070 | n/a | switch (inType->size) |
|---|
| 1071 | n/a | { |
|---|
| 1072 | n/a | case 1: case 2: case 4: |
|---|
| 1073 | n/a | srcGMarker += 8 - inType->size; |
|---|
| 1074 | n/a | break; |
|---|
| 1075 | n/a | |
|---|
| 1076 | n/a | default: |
|---|
| 1077 | n/a | break; |
|---|
| 1078 | n/a | } |
|---|
| 1079 | n/a | |
|---|
| 1080 | n/a | for (i = 0; inType->elements[i] != NULL; i++) |
|---|
| 1081 | n/a | { |
|---|
| 1082 | n/a | switch (inType->elements[i]->type) |
|---|
| 1083 | n/a | { |
|---|
| 1084 | n/a | case FFI_TYPE_FLOAT: |
|---|
| 1085 | n/a | srcFMarker = ALIGN(srcFMarker, 4); |
|---|
| 1086 | n/a | srcGMarker = ALIGN(srcGMarker, 4); |
|---|
| 1087 | n/a | destMarker = ALIGN(destMarker, 4); |
|---|
| 1088 | n/a | |
|---|
| 1089 | n/a | if (fprsUsed < NUM_FPR_ARG_REGISTERS) |
|---|
| 1090 | n/a | { |
|---|
| 1091 | n/a | *(float*)&outStruct[destMarker] = |
|---|
| 1092 | n/a | (float)*(double*)&inFPRs[srcFMarker]; |
|---|
| 1093 | n/a | srcFMarker += 8; |
|---|
| 1094 | n/a | fprsUsed++; |
|---|
| 1095 | n/a | } |
|---|
| 1096 | n/a | else |
|---|
| 1097 | n/a | *(float*)&outStruct[destMarker] = |
|---|
| 1098 | n/a | (float)*(double*)&inGPRs[srcGMarker]; |
|---|
| 1099 | n/a | |
|---|
| 1100 | n/a | srcGMarker += 4; |
|---|
| 1101 | n/a | destMarker += 4; |
|---|
| 1102 | n/a | |
|---|
| 1103 | n/a | // Skip to next GPR if next element won't fit and we're |
|---|
| 1104 | n/a | // not already at a register boundary. |
|---|
| 1105 | n/a | if (inType->elements[i + 1] != NULL && (destMarker % 8)) |
|---|
| 1106 | n/a | { |
|---|
| 1107 | n/a | if (!FFI_TYPE_1_BYTE(inType->elements[i + 1]->type) && |
|---|
| 1108 | n/a | (!FFI_TYPE_2_BYTE(inType->elements[i + 1]->type) || |
|---|
| 1109 | n/a | (ALIGN(srcGMarker, 8) - srcGMarker) < 2) && |
|---|
| 1110 | n/a | (!FFI_TYPE_4_BYTE(inType->elements[i + 1]->type) || |
|---|
| 1111 | n/a | (ALIGN(srcGMarker, 8) - srcGMarker) < 4)) |
|---|
| 1112 | n/a | srcGMarker = ALIGN(srcGMarker, 8); |
|---|
| 1113 | n/a | } |
|---|
| 1114 | n/a | |
|---|
| 1115 | n/a | break; |
|---|
| 1116 | n/a | |
|---|
| 1117 | n/a | case FFI_TYPE_DOUBLE: |
|---|
| 1118 | n/a | srcFMarker = ALIGN(srcFMarker, 8); |
|---|
| 1119 | n/a | destMarker = ALIGN(destMarker, 8); |
|---|
| 1120 | n/a | |
|---|
| 1121 | n/a | if (fprsUsed < NUM_FPR_ARG_REGISTERS) |
|---|
| 1122 | n/a | { |
|---|
| 1123 | n/a | *(double*)&outStruct[destMarker] = |
|---|
| 1124 | n/a | *(double*)&inFPRs[srcFMarker]; |
|---|
| 1125 | n/a | srcFMarker += 8; |
|---|
| 1126 | n/a | fprsUsed++; |
|---|
| 1127 | n/a | } |
|---|
| 1128 | n/a | else |
|---|
| 1129 | n/a | *(double*)&outStruct[destMarker] = |
|---|
| 1130 | n/a | *(double*)&inGPRs[srcGMarker]; |
|---|
| 1131 | n/a | |
|---|
| 1132 | n/a | destMarker += 8; |
|---|
| 1133 | n/a | |
|---|
| 1134 | n/a | // Skip next GPR |
|---|
| 1135 | n/a | srcGMarker += 8; |
|---|
| 1136 | n/a | srcGMarker = ALIGN(srcGMarker, 8); |
|---|
| 1137 | n/a | |
|---|
| 1138 | n/a | break; |
|---|
| 1139 | n/a | |
|---|
| 1140 | n/a | case FFI_TYPE_LONGDOUBLE: |
|---|
| 1141 | n/a | destMarker = ALIGN(destMarker, 16); |
|---|
| 1142 | n/a | |
|---|
| 1143 | n/a | if (fprsUsed < NUM_FPR_ARG_REGISTERS) |
|---|
| 1144 | n/a | { |
|---|
| 1145 | n/a | srcFMarker = ALIGN(srcFMarker, 8); |
|---|
| 1146 | n/a | srcGMarker = ALIGN(srcGMarker, 8); |
|---|
| 1147 | n/a | *(long double*)&outStruct[destMarker] = |
|---|
| 1148 | n/a | *(long double*)&inFPRs[srcFMarker]; |
|---|
| 1149 | n/a | srcFMarker += 16; |
|---|
| 1150 | n/a | fprsUsed += 2; |
|---|
| 1151 | n/a | } |
|---|
| 1152 | n/a | else |
|---|
| 1153 | n/a | { |
|---|
| 1154 | n/a | srcFMarker = ALIGN(srcFMarker, 16); |
|---|
| 1155 | n/a | srcGMarker = ALIGN(srcGMarker, 16); |
|---|
| 1156 | n/a | *(long double*)&outStruct[destMarker] = |
|---|
| 1157 | n/a | *(long double*)&inGPRs[srcGMarker]; |
|---|
| 1158 | n/a | } |
|---|
| 1159 | n/a | |
|---|
| 1160 | n/a | destMarker += 16; |
|---|
| 1161 | n/a | |
|---|
| 1162 | n/a | // Skip next 2 GPRs |
|---|
| 1163 | n/a | srcGMarker += 16; |
|---|
| 1164 | n/a | srcGMarker = ALIGN(srcGMarker, 8); |
|---|
| 1165 | n/a | |
|---|
| 1166 | n/a | break; |
|---|
| 1167 | n/a | |
|---|
| 1168 | n/a | case FFI_TYPE_UINT8: |
|---|
| 1169 | n/a | case FFI_TYPE_SINT8: |
|---|
| 1170 | n/a | { |
|---|
| 1171 | n/a | if (inType->alignment == 1) // chars only |
|---|
| 1172 | n/a | { |
|---|
| 1173 | n/a | if (inType->size == 1) |
|---|
| 1174 | n/a | outStruct[destMarker++] = inGPRs[srcGMarker++]; |
|---|
| 1175 | n/a | else if (inType->size == 2) |
|---|
| 1176 | n/a | { |
|---|
| 1177 | n/a | outStruct[destMarker++] = inGPRs[srcGMarker++]; |
|---|
| 1178 | n/a | outStruct[destMarker++] = inGPRs[srcGMarker++]; |
|---|
| 1179 | n/a | i++; |
|---|
| 1180 | n/a | } |
|---|
| 1181 | n/a | else |
|---|
| 1182 | n/a | { |
|---|
| 1183 | n/a | memcpy(&outStruct[destMarker], |
|---|
| 1184 | n/a | &inGPRs[srcGMarker], inType->size); |
|---|
| 1185 | n/a | srcGMarker += inType->size; |
|---|
| 1186 | n/a | destMarker += inType->size; |
|---|
| 1187 | n/a | i += inType->size - 1; |
|---|
| 1188 | n/a | } |
|---|
| 1189 | n/a | } |
|---|
| 1190 | n/a | else // chars and other stuff |
|---|
| 1191 | n/a | { |
|---|
| 1192 | n/a | outStruct[destMarker++] = inGPRs[srcGMarker++]; |
|---|
| 1193 | n/a | |
|---|
| 1194 | n/a | // Skip to next GPR if next element won't fit and we're |
|---|
| 1195 | n/a | // not already at a register boundary. |
|---|
| 1196 | n/a | if (inType->elements[i + 1] != NULL && (srcGMarker % 8)) |
|---|
| 1197 | n/a | { |
|---|
| 1198 | n/a | if (!FFI_TYPE_1_BYTE(inType->elements[i + 1]->type) && |
|---|
| 1199 | n/a | (!FFI_TYPE_2_BYTE(inType->elements[i + 1]->type) || |
|---|
| 1200 | n/a | (ALIGN(srcGMarker, 8) - srcGMarker) < 2) && |
|---|
| 1201 | n/a | (!FFI_TYPE_4_BYTE(inType->elements[i + 1]->type) || |
|---|
| 1202 | n/a | (ALIGN(srcGMarker, 8) - srcGMarker) < 4)) |
|---|
| 1203 | n/a | srcGMarker = ALIGN(srcGMarker, inType->alignment); // was 8 |
|---|
| 1204 | n/a | } |
|---|
| 1205 | n/a | } |
|---|
| 1206 | n/a | |
|---|
| 1207 | n/a | break; |
|---|
| 1208 | n/a | } |
|---|
| 1209 | n/a | |
|---|
| 1210 | n/a | case FFI_TYPE_UINT16: |
|---|
| 1211 | n/a | case FFI_TYPE_SINT16: |
|---|
| 1212 | n/a | srcGMarker = ALIGN(srcGMarker, 2); |
|---|
| 1213 | n/a | destMarker = ALIGN(destMarker, 2); |
|---|
| 1214 | n/a | |
|---|
| 1215 | n/a | *(short*)&outStruct[destMarker] = |
|---|
| 1216 | n/a | *(short*)&inGPRs[srcGMarker]; |
|---|
| 1217 | n/a | srcGMarker += 2; |
|---|
| 1218 | n/a | destMarker += 2; |
|---|
| 1219 | n/a | |
|---|
| 1220 | n/a | break; |
|---|
| 1221 | n/a | |
|---|
| 1222 | n/a | case FFI_TYPE_INT: |
|---|
| 1223 | n/a | case FFI_TYPE_UINT32: |
|---|
| 1224 | n/a | case FFI_TYPE_SINT32: |
|---|
| 1225 | n/a | srcGMarker = ALIGN(srcGMarker, 4); |
|---|
| 1226 | n/a | destMarker = ALIGN(destMarker, 4); |
|---|
| 1227 | n/a | |
|---|
| 1228 | n/a | *(int*)&outStruct[destMarker] = |
|---|
| 1229 | n/a | *(int*)&inGPRs[srcGMarker]; |
|---|
| 1230 | n/a | srcGMarker += 4; |
|---|
| 1231 | n/a | destMarker += 4; |
|---|
| 1232 | n/a | |
|---|
| 1233 | n/a | break; |
|---|
| 1234 | n/a | |
|---|
| 1235 | n/a | case FFI_TYPE_POINTER: |
|---|
| 1236 | n/a | case FFI_TYPE_UINT64: |
|---|
| 1237 | n/a | case FFI_TYPE_SINT64: |
|---|
| 1238 | n/a | srcGMarker = ALIGN(srcGMarker, 8); |
|---|
| 1239 | n/a | destMarker = ALIGN(destMarker, 8); |
|---|
| 1240 | n/a | |
|---|
| 1241 | n/a | *(long long*)&outStruct[destMarker] = |
|---|
| 1242 | n/a | *(long long*)&inGPRs[srcGMarker]; |
|---|
| 1243 | n/a | srcGMarker += 8; |
|---|
| 1244 | n/a | destMarker += 8; |
|---|
| 1245 | n/a | |
|---|
| 1246 | n/a | break; |
|---|
| 1247 | n/a | |
|---|
| 1248 | n/a | case FFI_TYPE_STRUCT: |
|---|
| 1249 | n/a | recurseCount++; |
|---|
| 1250 | n/a | ffi64_struct_to_ram_form(inType->elements[i], inGPRs, |
|---|
| 1251 | n/a | &srcGMarker, inFPRs, &srcFMarker, &fprsUsed, |
|---|
| 1252 | n/a | outStruct, &destMarker); |
|---|
| 1253 | n/a | recurseCount--; |
|---|
| 1254 | n/a | break; |
|---|
| 1255 | n/a | |
|---|
| 1256 | n/a | default: |
|---|
| 1257 | n/a | FFI_ASSERT(0); // unknown element type |
|---|
| 1258 | n/a | break; |
|---|
| 1259 | n/a | } |
|---|
| 1260 | n/a | } |
|---|
| 1261 | n/a | |
|---|
| 1262 | n/a | srcGMarker = ALIGN(srcGMarker, inType->alignment); |
|---|
| 1263 | n/a | |
|---|
| 1264 | n/a | // Take care of the special case for 16-byte structs, but not for |
|---|
| 1265 | n/a | // nested structs. |
|---|
| 1266 | n/a | if (recurseCount == 0 && srcGMarker == 16) |
|---|
| 1267 | n/a | { |
|---|
| 1268 | n/a | *(long double*)&outStruct[0] = *(long double*)&inGPRs[0]; |
|---|
| 1269 | n/a | srcFMarker = savedFMarker; |
|---|
| 1270 | n/a | fprsUsed = savedFPRsUsed; |
|---|
| 1271 | n/a | } |
|---|
| 1272 | n/a | |
|---|
| 1273 | n/a | if (ioGPRMarker) |
|---|
| 1274 | n/a | *ioGPRMarker = ALIGN(srcGMarker, 8); |
|---|
| 1275 | n/a | |
|---|
| 1276 | n/a | if (ioFPRMarker) |
|---|
| 1277 | n/a | *ioFPRMarker = srcFMarker; |
|---|
| 1278 | n/a | |
|---|
| 1279 | n/a | if (ioFPRsUsed) |
|---|
| 1280 | n/a | *ioFPRsUsed = fprsUsed; |
|---|
| 1281 | n/a | |
|---|
| 1282 | n/a | if (ioStructMarker) |
|---|
| 1283 | n/a | *ioStructMarker = ALIGN(destMarker, 8); |
|---|
| 1284 | n/a | } |
|---|
| 1285 | n/a | |
|---|
| 1286 | n/a | /* ffi64_struct_to_reg_form |
|---|
| 1287 | n/a | |
|---|
| 1288 | n/a | Copy a struct's elements into buffers that can be sliced into registers. |
|---|
| 1289 | n/a | Return the sizes of the output buffers in bytes. Pass NULL buffer pointers |
|---|
| 1290 | n/a | to calculate size only. |
|---|
| 1291 | n/a | outGPRs[0-7] == r3, outFPRs[0-7] == f1 ... |
|---|
| 1292 | n/a | */ |
|---|
| 1293 | n/a | void |
|---|
| 1294 | n/a | ffi64_struct_to_reg_form( |
|---|
| 1295 | n/a | const ffi_type* inType, |
|---|
| 1296 | n/a | const char* inStruct, |
|---|
| 1297 | n/a | unsigned int* ioStructMarker, |
|---|
| 1298 | n/a | unsigned int* ioFPRsUsed, |
|---|
| 1299 | n/a | char* outGPRs, // caller-allocated |
|---|
| 1300 | n/a | unsigned int* ioGPRSize, |
|---|
| 1301 | n/a | char* outFPRs, // caller-allocated |
|---|
| 1302 | n/a | unsigned int* ioFPRSize) |
|---|
| 1303 | n/a | { |
|---|
| 1304 | n/a | size_t i; |
|---|
| 1305 | n/a | unsigned int srcMarker = 0; |
|---|
| 1306 | n/a | unsigned int destGMarker = 0; |
|---|
| 1307 | n/a | unsigned int destFMarker = 0; |
|---|
| 1308 | n/a | unsigned int savedFMarker = 0; |
|---|
| 1309 | n/a | unsigned int fprsUsed = 0; |
|---|
| 1310 | n/a | unsigned int savedFPRsUsed = 0; |
|---|
| 1311 | n/a | |
|---|
| 1312 | n/a | static unsigned int recurseCount = 0; |
|---|
| 1313 | n/a | |
|---|
| 1314 | n/a | if (ioStructMarker) |
|---|
| 1315 | n/a | srcMarker = *ioStructMarker; |
|---|
| 1316 | n/a | |
|---|
| 1317 | n/a | if (ioFPRsUsed) |
|---|
| 1318 | n/a | { |
|---|
| 1319 | n/a | fprsUsed = *ioFPRsUsed; |
|---|
| 1320 | n/a | savedFPRsUsed = fprsUsed; |
|---|
| 1321 | n/a | } |
|---|
| 1322 | n/a | |
|---|
| 1323 | n/a | if (ioGPRSize) |
|---|
| 1324 | n/a | destGMarker = *ioGPRSize; |
|---|
| 1325 | n/a | |
|---|
| 1326 | n/a | if (ioFPRSize) |
|---|
| 1327 | n/a | { |
|---|
| 1328 | n/a | destFMarker = *ioFPRSize; |
|---|
| 1329 | n/a | savedFMarker = destFMarker; |
|---|
| 1330 | n/a | } |
|---|
| 1331 | n/a | |
|---|
| 1332 | n/a | switch (inType->size) |
|---|
| 1333 | n/a | { |
|---|
| 1334 | n/a | case 1: case 2: case 4: |
|---|
| 1335 | n/a | destGMarker += 8 - inType->size; |
|---|
| 1336 | n/a | break; |
|---|
| 1337 | n/a | |
|---|
| 1338 | n/a | default: |
|---|
| 1339 | n/a | break; |
|---|
| 1340 | n/a | } |
|---|
| 1341 | n/a | |
|---|
| 1342 | n/a | for (i = 0; inType->elements[i] != NULL; i++) |
|---|
| 1343 | n/a | { |
|---|
| 1344 | n/a | switch (inType->elements[i]->type) |
|---|
| 1345 | n/a | { |
|---|
| 1346 | n/a | // Shadow floating-point types in GPRs for vararg and pre-ANSI |
|---|
| 1347 | n/a | // functions. |
|---|
| 1348 | n/a | case FFI_TYPE_FLOAT: |
|---|
| 1349 | n/a | // Nudge markers to next 4/8-byte boundary |
|---|
| 1350 | n/a | srcMarker = ALIGN(srcMarker, 4); |
|---|
| 1351 | n/a | destGMarker = ALIGN(destGMarker, 4); |
|---|
| 1352 | n/a | destFMarker = ALIGN(destFMarker, 8); |
|---|
| 1353 | n/a | |
|---|
| 1354 | n/a | if (fprsUsed < NUM_FPR_ARG_REGISTERS) |
|---|
| 1355 | n/a | { |
|---|
| 1356 | n/a | if (outFPRs != NULL && inStruct != NULL) |
|---|
| 1357 | n/a | *(double*)&outFPRs[destFMarker] = |
|---|
| 1358 | n/a | (double)*(float*)&inStruct[srcMarker]; |
|---|
| 1359 | n/a | |
|---|
| 1360 | n/a | destFMarker += 8; |
|---|
| 1361 | n/a | fprsUsed++; |
|---|
| 1362 | n/a | } |
|---|
| 1363 | n/a | |
|---|
| 1364 | n/a | if (outGPRs != NULL && inStruct != NULL) |
|---|
| 1365 | n/a | *(double*)&outGPRs[destGMarker] = |
|---|
| 1366 | n/a | (double)*(float*)&inStruct[srcMarker]; |
|---|
| 1367 | n/a | |
|---|
| 1368 | n/a | srcMarker += 4; |
|---|
| 1369 | n/a | destGMarker += 4; |
|---|
| 1370 | n/a | |
|---|
| 1371 | n/a | // Skip to next GPR if next element won't fit and we're |
|---|
| 1372 | n/a | // not already at a register boundary. |
|---|
| 1373 | n/a | if (inType->elements[i + 1] != NULL && (srcMarker % 8)) |
|---|
| 1374 | n/a | { |
|---|
| 1375 | n/a | if (!FFI_TYPE_1_BYTE(inType->elements[i + 1]->type) && |
|---|
| 1376 | n/a | (!FFI_TYPE_2_BYTE(inType->elements[i + 1]->type) || |
|---|
| 1377 | n/a | (ALIGN(destGMarker, 8) - destGMarker) < 2) && |
|---|
| 1378 | n/a | (!FFI_TYPE_4_BYTE(inType->elements[i + 1]->type) || |
|---|
| 1379 | n/a | (ALIGN(destGMarker, 8) - destGMarker) < 4)) |
|---|
| 1380 | n/a | destGMarker = ALIGN(destGMarker, 8); |
|---|
| 1381 | n/a | } |
|---|
| 1382 | n/a | |
|---|
| 1383 | n/a | break; |
|---|
| 1384 | n/a | |
|---|
| 1385 | n/a | case FFI_TYPE_DOUBLE: |
|---|
| 1386 | n/a | srcMarker = ALIGN(srcMarker, 8); |
|---|
| 1387 | n/a | destFMarker = ALIGN(destFMarker, 8); |
|---|
| 1388 | n/a | |
|---|
| 1389 | n/a | if (fprsUsed < NUM_FPR_ARG_REGISTERS) |
|---|
| 1390 | n/a | { |
|---|
| 1391 | n/a | if (outFPRs != NULL && inStruct != NULL) |
|---|
| 1392 | n/a | *(double*)&outFPRs[destFMarker] = |
|---|
| 1393 | n/a | *(double*)&inStruct[srcMarker]; |
|---|
| 1394 | n/a | |
|---|
| 1395 | n/a | destFMarker += 8; |
|---|
| 1396 | n/a | fprsUsed++; |
|---|
| 1397 | n/a | } |
|---|
| 1398 | n/a | |
|---|
| 1399 | n/a | if (outGPRs != NULL && inStruct != NULL) |
|---|
| 1400 | n/a | *(double*)&outGPRs[destGMarker] = |
|---|
| 1401 | n/a | *(double*)&inStruct[srcMarker]; |
|---|
| 1402 | n/a | |
|---|
| 1403 | n/a | srcMarker += 8; |
|---|
| 1404 | n/a | |
|---|
| 1405 | n/a | // Skip next GPR |
|---|
| 1406 | n/a | destGMarker += 8; |
|---|
| 1407 | n/a | destGMarker = ALIGN(destGMarker, 8); |
|---|
| 1408 | n/a | |
|---|
| 1409 | n/a | break; |
|---|
| 1410 | n/a | |
|---|
| 1411 | n/a | case FFI_TYPE_LONGDOUBLE: |
|---|
| 1412 | n/a | srcMarker = ALIGN(srcMarker, 16); |
|---|
| 1413 | n/a | |
|---|
| 1414 | n/a | if (fprsUsed < NUM_FPR_ARG_REGISTERS) |
|---|
| 1415 | n/a | { |
|---|
| 1416 | n/a | destFMarker = ALIGN(destFMarker, 8); |
|---|
| 1417 | n/a | destGMarker = ALIGN(destGMarker, 8); |
|---|
| 1418 | n/a | |
|---|
| 1419 | n/a | if (outFPRs != NULL && inStruct != NULL) |
|---|
| 1420 | n/a | *(long double*)&outFPRs[destFMarker] = |
|---|
| 1421 | n/a | *(long double*)&inStruct[srcMarker]; |
|---|
| 1422 | n/a | |
|---|
| 1423 | n/a | if (outGPRs != NULL && inStruct != NULL) |
|---|
| 1424 | n/a | *(long double*)&outGPRs[destGMarker] = |
|---|
| 1425 | n/a | *(long double*)&inStruct[srcMarker]; |
|---|
| 1426 | n/a | |
|---|
| 1427 | n/a | destFMarker += 16; |
|---|
| 1428 | n/a | fprsUsed += 2; |
|---|
| 1429 | n/a | } |
|---|
| 1430 | n/a | else |
|---|
| 1431 | n/a | { |
|---|
| 1432 | n/a | destGMarker = ALIGN(destGMarker, 16); |
|---|
| 1433 | n/a | |
|---|
| 1434 | n/a | if (outGPRs != NULL && inStruct != NULL) |
|---|
| 1435 | n/a | *(long double*)&outGPRs[destGMarker] = |
|---|
| 1436 | n/a | *(long double*)&inStruct[srcMarker]; |
|---|
| 1437 | n/a | } |
|---|
| 1438 | n/a | |
|---|
| 1439 | n/a | srcMarker += 16; |
|---|
| 1440 | n/a | destGMarker += 16; // Skip next 2 GPRs |
|---|
| 1441 | n/a | destGMarker = ALIGN(destGMarker, 8); // was 16 |
|---|
| 1442 | n/a | |
|---|
| 1443 | n/a | break; |
|---|
| 1444 | n/a | |
|---|
| 1445 | n/a | case FFI_TYPE_UINT8: |
|---|
| 1446 | n/a | case FFI_TYPE_SINT8: |
|---|
| 1447 | n/a | if (inType->alignment == 1) // bytes only |
|---|
| 1448 | n/a | { |
|---|
| 1449 | n/a | if (inType->size == 1) |
|---|
| 1450 | n/a | { |
|---|
| 1451 | n/a | if (outGPRs != NULL && inStruct != NULL) |
|---|
| 1452 | n/a | outGPRs[destGMarker] = inStruct[srcMarker]; |
|---|
| 1453 | n/a | |
|---|
| 1454 | n/a | srcMarker++; |
|---|
| 1455 | n/a | destGMarker++; |
|---|
| 1456 | n/a | } |
|---|
| 1457 | n/a | else if (inType->size == 2) |
|---|
| 1458 | n/a | { |
|---|
| 1459 | n/a | if (outGPRs != NULL && inStruct != NULL) |
|---|
| 1460 | n/a | { |
|---|
| 1461 | n/a | outGPRs[destGMarker] = inStruct[srcMarker]; |
|---|
| 1462 | n/a | outGPRs[destGMarker + 1] = inStruct[srcMarker + 1]; |
|---|
| 1463 | n/a | } |
|---|
| 1464 | n/a | |
|---|
| 1465 | n/a | srcMarker += 2; |
|---|
| 1466 | n/a | destGMarker += 2; |
|---|
| 1467 | n/a | |
|---|
| 1468 | n/a | i++; |
|---|
| 1469 | n/a | } |
|---|
| 1470 | n/a | else |
|---|
| 1471 | n/a | { |
|---|
| 1472 | n/a | if (outGPRs != NULL && inStruct != NULL) |
|---|
| 1473 | n/a | { |
|---|
| 1474 | n/a | // Avoid memcpy for small chunks. |
|---|
| 1475 | n/a | if (inType->size <= sizeof(long)) |
|---|
| 1476 | n/a | *(long*)&outGPRs[destGMarker] = |
|---|
| 1477 | n/a | *(long*)&inStruct[srcMarker]; |
|---|
| 1478 | n/a | else |
|---|
| 1479 | n/a | memcpy(&outGPRs[destGMarker], |
|---|
| 1480 | n/a | &inStruct[srcMarker], inType->size); |
|---|
| 1481 | n/a | } |
|---|
| 1482 | n/a | |
|---|
| 1483 | n/a | srcMarker += inType->size; |
|---|
| 1484 | n/a | destGMarker += inType->size; |
|---|
| 1485 | n/a | i += inType->size - 1; |
|---|
| 1486 | n/a | } |
|---|
| 1487 | n/a | } |
|---|
| 1488 | n/a | else // bytes and other stuff |
|---|
| 1489 | n/a | { |
|---|
| 1490 | n/a | if (outGPRs != NULL && inStruct != NULL) |
|---|
| 1491 | n/a | outGPRs[destGMarker] = inStruct[srcMarker]; |
|---|
| 1492 | n/a | |
|---|
| 1493 | n/a | srcMarker++; |
|---|
| 1494 | n/a | destGMarker++; |
|---|
| 1495 | n/a | |
|---|
| 1496 | n/a | // Skip to next GPR if next element won't fit and we're |
|---|
| 1497 | n/a | // not already at a register boundary. |
|---|
| 1498 | n/a | if (inType->elements[i + 1] != NULL && (destGMarker % 8)) |
|---|
| 1499 | n/a | { |
|---|
| 1500 | n/a | if (!FFI_TYPE_1_BYTE(inType->elements[i + 1]->type) && |
|---|
| 1501 | n/a | (!FFI_TYPE_2_BYTE(inType->elements[i + 1]->type) || |
|---|
| 1502 | n/a | (ALIGN(destGMarker, 8) - destGMarker) < 2) && |
|---|
| 1503 | n/a | (!FFI_TYPE_4_BYTE(inType->elements[i + 1]->type) || |
|---|
| 1504 | n/a | (ALIGN(destGMarker, 8) - destGMarker) < 4)) |
|---|
| 1505 | n/a | destGMarker = ALIGN(destGMarker, inType->alignment); // was 8 |
|---|
| 1506 | n/a | } |
|---|
| 1507 | n/a | } |
|---|
| 1508 | n/a | |
|---|
| 1509 | n/a | break; |
|---|
| 1510 | n/a | |
|---|
| 1511 | n/a | case FFI_TYPE_UINT16: |
|---|
| 1512 | n/a | case FFI_TYPE_SINT16: |
|---|
| 1513 | n/a | srcMarker = ALIGN(srcMarker, 2); |
|---|
| 1514 | n/a | destGMarker = ALIGN(destGMarker, 2); |
|---|
| 1515 | n/a | |
|---|
| 1516 | n/a | if (outGPRs != NULL && inStruct != NULL) |
|---|
| 1517 | n/a | *(short*)&outGPRs[destGMarker] = |
|---|
| 1518 | n/a | *(short*)&inStruct[srcMarker]; |
|---|
| 1519 | n/a | |
|---|
| 1520 | n/a | srcMarker += 2; |
|---|
| 1521 | n/a | destGMarker += 2; |
|---|
| 1522 | n/a | |
|---|
| 1523 | n/a | if (inType->elements[i + 1] == NULL) |
|---|
| 1524 | n/a | destGMarker = ALIGN(destGMarker, inType->alignment); |
|---|
| 1525 | n/a | |
|---|
| 1526 | n/a | break; |
|---|
| 1527 | n/a | |
|---|
| 1528 | n/a | case FFI_TYPE_INT: |
|---|
| 1529 | n/a | case FFI_TYPE_UINT32: |
|---|
| 1530 | n/a | case FFI_TYPE_SINT32: |
|---|
| 1531 | n/a | srcMarker = ALIGN(srcMarker, 4); |
|---|
| 1532 | n/a | destGMarker = ALIGN(destGMarker, 4); |
|---|
| 1533 | n/a | |
|---|
| 1534 | n/a | if (outGPRs != NULL && inStruct != NULL) |
|---|
| 1535 | n/a | *(int*)&outGPRs[destGMarker] = |
|---|
| 1536 | n/a | *(int*)&inStruct[srcMarker]; |
|---|
| 1537 | n/a | |
|---|
| 1538 | n/a | srcMarker += 4; |
|---|
| 1539 | n/a | destGMarker += 4; |
|---|
| 1540 | n/a | |
|---|
| 1541 | n/a | break; |
|---|
| 1542 | n/a | |
|---|
| 1543 | n/a | case FFI_TYPE_POINTER: |
|---|
| 1544 | n/a | case FFI_TYPE_UINT64: |
|---|
| 1545 | n/a | case FFI_TYPE_SINT64: |
|---|
| 1546 | n/a | srcMarker = ALIGN(srcMarker, 8); |
|---|
| 1547 | n/a | destGMarker = ALIGN(destGMarker, 8); |
|---|
| 1548 | n/a | |
|---|
| 1549 | n/a | if (outGPRs != NULL && inStruct != NULL) |
|---|
| 1550 | n/a | *(long long*)&outGPRs[destGMarker] = |
|---|
| 1551 | n/a | *(long long*)&inStruct[srcMarker]; |
|---|
| 1552 | n/a | |
|---|
| 1553 | n/a | srcMarker += 8; |
|---|
| 1554 | n/a | destGMarker += 8; |
|---|
| 1555 | n/a | |
|---|
| 1556 | n/a | if (inType->elements[i + 1] == NULL) |
|---|
| 1557 | n/a | destGMarker = ALIGN(destGMarker, inType->alignment); |
|---|
| 1558 | n/a | |
|---|
| 1559 | n/a | break; |
|---|
| 1560 | n/a | |
|---|
| 1561 | n/a | case FFI_TYPE_STRUCT: |
|---|
| 1562 | n/a | recurseCount++; |
|---|
| 1563 | n/a | ffi64_struct_to_reg_form(inType->elements[i], |
|---|
| 1564 | n/a | inStruct, &srcMarker, &fprsUsed, outGPRs, |
|---|
| 1565 | n/a | &destGMarker, outFPRs, &destFMarker); |
|---|
| 1566 | n/a | recurseCount--; |
|---|
| 1567 | n/a | break; |
|---|
| 1568 | n/a | |
|---|
| 1569 | n/a | default: |
|---|
| 1570 | n/a | FFI_ASSERT(0); |
|---|
| 1571 | n/a | break; |
|---|
| 1572 | n/a | } |
|---|
| 1573 | n/a | } |
|---|
| 1574 | n/a | |
|---|
| 1575 | n/a | destGMarker = ALIGN(destGMarker, inType->alignment); |
|---|
| 1576 | n/a | |
|---|
| 1577 | n/a | // Take care of the special case for 16-byte structs, but not for |
|---|
| 1578 | n/a | // nested structs. |
|---|
| 1579 | n/a | if (recurseCount == 0 && destGMarker == 16) |
|---|
| 1580 | n/a | { |
|---|
| 1581 | n/a | if (outGPRs != NULL && inStruct != NULL) |
|---|
| 1582 | n/a | *(long double*)&outGPRs[0] = *(long double*)&inStruct[0]; |
|---|
| 1583 | n/a | |
|---|
| 1584 | n/a | destFMarker = savedFMarker; |
|---|
| 1585 | n/a | fprsUsed = savedFPRsUsed; |
|---|
| 1586 | n/a | } |
|---|
| 1587 | n/a | |
|---|
| 1588 | n/a | if (ioStructMarker) |
|---|
| 1589 | n/a | *ioStructMarker = ALIGN(srcMarker, 8); |
|---|
| 1590 | n/a | |
|---|
| 1591 | n/a | if (ioFPRsUsed) |
|---|
| 1592 | n/a | *ioFPRsUsed = fprsUsed; |
|---|
| 1593 | n/a | |
|---|
| 1594 | n/a | if (ioGPRSize) |
|---|
| 1595 | n/a | *ioGPRSize = ALIGN(destGMarker, 8); |
|---|
| 1596 | n/a | |
|---|
| 1597 | n/a | if (ioFPRSize) |
|---|
| 1598 | n/a | *ioFPRSize = ALIGN(destFMarker, 8); |
|---|
| 1599 | n/a | } |
|---|
| 1600 | n/a | |
|---|
| 1601 | n/a | /* ffi64_stret_needs_ptr |
|---|
| 1602 | n/a | |
|---|
| 1603 | n/a | Determine whether a returned struct needs a pointer in r3 or can fit |
|---|
| 1604 | n/a | in registers. |
|---|
| 1605 | n/a | */ |
|---|
| 1606 | n/a | |
|---|
| 1607 | n/a | bool |
|---|
| 1608 | n/a | ffi64_stret_needs_ptr( |
|---|
| 1609 | n/a | const ffi_type* inType, |
|---|
| 1610 | n/a | unsigned short* ioGPRCount, |
|---|
| 1611 | n/a | unsigned short* ioFPRCount) |
|---|
| 1612 | n/a | { |
|---|
| 1613 | n/a | // Obvious case first- struct is larger than combined FPR size. |
|---|
| 1614 | n/a | if (inType->size > 14 * 8) |
|---|
| 1615 | n/a | return true; |
|---|
| 1616 | n/a | |
|---|
| 1617 | n/a | // Now the struct can physically fit in registers, determine if it |
|---|
| 1618 | n/a | // also fits logically. |
|---|
| 1619 | n/a | bool needsPtr = false; |
|---|
| 1620 | n/a | unsigned short gprsUsed = 0; |
|---|
| 1621 | n/a | unsigned short fprsUsed = 0; |
|---|
| 1622 | n/a | size_t i; |
|---|
| 1623 | n/a | |
|---|
| 1624 | n/a | if (ioGPRCount) |
|---|
| 1625 | n/a | gprsUsed = *ioGPRCount; |
|---|
| 1626 | n/a | |
|---|
| 1627 | n/a | if (ioFPRCount) |
|---|
| 1628 | n/a | fprsUsed = *ioFPRCount; |
|---|
| 1629 | n/a | |
|---|
| 1630 | n/a | for (i = 0; inType->elements[i] != NULL && !needsPtr; i++) |
|---|
| 1631 | n/a | { |
|---|
| 1632 | n/a | switch (inType->elements[i]->type) |
|---|
| 1633 | n/a | { |
|---|
| 1634 | n/a | case FFI_TYPE_FLOAT: |
|---|
| 1635 | n/a | case FFI_TYPE_DOUBLE: |
|---|
| 1636 | n/a | gprsUsed++; |
|---|
| 1637 | n/a | fprsUsed++; |
|---|
| 1638 | n/a | |
|---|
| 1639 | n/a | if (fprsUsed > 13) |
|---|
| 1640 | n/a | needsPtr = true; |
|---|
| 1641 | n/a | |
|---|
| 1642 | n/a | break; |
|---|
| 1643 | n/a | |
|---|
| 1644 | n/a | case FFI_TYPE_LONGDOUBLE: |
|---|
| 1645 | n/a | gprsUsed += 2; |
|---|
| 1646 | n/a | fprsUsed += 2; |
|---|
| 1647 | n/a | |
|---|
| 1648 | n/a | if (fprsUsed > 14) |
|---|
| 1649 | n/a | needsPtr = true; |
|---|
| 1650 | n/a | |
|---|
| 1651 | n/a | break; |
|---|
| 1652 | n/a | |
|---|
| 1653 | n/a | case FFI_TYPE_UINT8: |
|---|
| 1654 | n/a | case FFI_TYPE_SINT8: |
|---|
| 1655 | n/a | { |
|---|
| 1656 | n/a | gprsUsed++; |
|---|
| 1657 | n/a | |
|---|
| 1658 | n/a | if (gprsUsed > 8) |
|---|
| 1659 | n/a | { |
|---|
| 1660 | n/a | needsPtr = true; |
|---|
| 1661 | n/a | break; |
|---|
| 1662 | n/a | } |
|---|
| 1663 | n/a | |
|---|
| 1664 | n/a | if (inType->elements[i + 1] == NULL) // last byte in the struct |
|---|
| 1665 | n/a | break; |
|---|
| 1666 | n/a | |
|---|
| 1667 | n/a | // Count possible contiguous bytes ahead, up to 8. |
|---|
| 1668 | n/a | unsigned short j; |
|---|
| 1669 | n/a | |
|---|
| 1670 | n/a | for (j = 1; j < 8; j++) |
|---|
| 1671 | n/a | { |
|---|
| 1672 | n/a | if (inType->elements[i + j] == NULL || |
|---|
| 1673 | n/a | !FFI_TYPE_1_BYTE(inType->elements[i + j]->type)) |
|---|
| 1674 | n/a | break; |
|---|
| 1675 | n/a | } |
|---|
| 1676 | n/a | |
|---|
| 1677 | n/a | i += j - 1; // allow for i++ before the test condition |
|---|
| 1678 | n/a | |
|---|
| 1679 | n/a | break; |
|---|
| 1680 | n/a | } |
|---|
| 1681 | n/a | |
|---|
| 1682 | n/a | case FFI_TYPE_UINT16: |
|---|
| 1683 | n/a | case FFI_TYPE_SINT16: |
|---|
| 1684 | n/a | case FFI_TYPE_INT: |
|---|
| 1685 | n/a | case FFI_TYPE_UINT32: |
|---|
| 1686 | n/a | case FFI_TYPE_SINT32: |
|---|
| 1687 | n/a | case FFI_TYPE_POINTER: |
|---|
| 1688 | n/a | case FFI_TYPE_UINT64: |
|---|
| 1689 | n/a | case FFI_TYPE_SINT64: |
|---|
| 1690 | n/a | gprsUsed++; |
|---|
| 1691 | n/a | |
|---|
| 1692 | n/a | if (gprsUsed > 8) |
|---|
| 1693 | n/a | needsPtr = true; |
|---|
| 1694 | n/a | |
|---|
| 1695 | n/a | break; |
|---|
| 1696 | n/a | |
|---|
| 1697 | n/a | case FFI_TYPE_STRUCT: |
|---|
| 1698 | n/a | needsPtr = ffi64_stret_needs_ptr( |
|---|
| 1699 | n/a | inType->elements[i], &gprsUsed, &fprsUsed); |
|---|
| 1700 | n/a | |
|---|
| 1701 | n/a | break; |
|---|
| 1702 | n/a | |
|---|
| 1703 | n/a | default: |
|---|
| 1704 | n/a | FFI_ASSERT(0); |
|---|
| 1705 | n/a | break; |
|---|
| 1706 | n/a | } |
|---|
| 1707 | n/a | } |
|---|
| 1708 | n/a | |
|---|
| 1709 | n/a | if (ioGPRCount) |
|---|
| 1710 | n/a | *ioGPRCount = gprsUsed; |
|---|
| 1711 | n/a | |
|---|
| 1712 | n/a | if (ioFPRCount) |
|---|
| 1713 | n/a | *ioFPRCount = fprsUsed; |
|---|
| 1714 | n/a | |
|---|
| 1715 | n/a | return needsPtr; |
|---|
| 1716 | n/a | } |
|---|
| 1717 | n/a | |
|---|
| 1718 | n/a | /* ffi64_data_size |
|---|
| 1719 | n/a | |
|---|
| 1720 | n/a | Calculate the size in bytes of an ffi type. |
|---|
| 1721 | n/a | */ |
|---|
| 1722 | n/a | |
|---|
| 1723 | n/a | unsigned int |
|---|
| 1724 | n/a | ffi64_data_size( |
|---|
| 1725 | n/a | const ffi_type* inType) |
|---|
| 1726 | n/a | { |
|---|
| 1727 | n/a | unsigned int size = 0; |
|---|
| 1728 | n/a | |
|---|
| 1729 | n/a | switch (inType->type) |
|---|
| 1730 | n/a | { |
|---|
| 1731 | n/a | case FFI_TYPE_UINT8: |
|---|
| 1732 | n/a | case FFI_TYPE_SINT8: |
|---|
| 1733 | n/a | size = 1; |
|---|
| 1734 | n/a | break; |
|---|
| 1735 | n/a | |
|---|
| 1736 | n/a | case FFI_TYPE_UINT16: |
|---|
| 1737 | n/a | case FFI_TYPE_SINT16: |
|---|
| 1738 | n/a | size = 2; |
|---|
| 1739 | n/a | break; |
|---|
| 1740 | n/a | |
|---|
| 1741 | n/a | case FFI_TYPE_INT: |
|---|
| 1742 | n/a | case FFI_TYPE_UINT32: |
|---|
| 1743 | n/a | case FFI_TYPE_SINT32: |
|---|
| 1744 | n/a | case FFI_TYPE_FLOAT: |
|---|
| 1745 | n/a | size = 4; |
|---|
| 1746 | n/a | break; |
|---|
| 1747 | n/a | |
|---|
| 1748 | n/a | case FFI_TYPE_POINTER: |
|---|
| 1749 | n/a | case FFI_TYPE_UINT64: |
|---|
| 1750 | n/a | case FFI_TYPE_SINT64: |
|---|
| 1751 | n/a | case FFI_TYPE_DOUBLE: |
|---|
| 1752 | n/a | size = 8; |
|---|
| 1753 | n/a | break; |
|---|
| 1754 | n/a | |
|---|
| 1755 | n/a | case FFI_TYPE_LONGDOUBLE: |
|---|
| 1756 | n/a | size = 16; |
|---|
| 1757 | n/a | break; |
|---|
| 1758 | n/a | |
|---|
| 1759 | n/a | case FFI_TYPE_STRUCT: |
|---|
| 1760 | n/a | ffi64_struct_to_reg_form( |
|---|
| 1761 | n/a | inType, NULL, NULL, NULL, NULL, &size, NULL, NULL); |
|---|
| 1762 | n/a | break; |
|---|
| 1763 | n/a | |
|---|
| 1764 | n/a | case FFI_TYPE_VOID: |
|---|
| 1765 | n/a | break; |
|---|
| 1766 | n/a | |
|---|
| 1767 | n/a | default: |
|---|
| 1768 | n/a | FFI_ASSERT(0); |
|---|
| 1769 | n/a | break; |
|---|
| 1770 | n/a | } |
|---|
| 1771 | n/a | |
|---|
| 1772 | n/a | return size; |
|---|
| 1773 | n/a | } |
|---|
| 1774 | n/a | |
|---|
| 1775 | n/a | #endif /* defined(__ppc64__) */ |
|---|
| 1776 | n/a | #endif /* __ppc__ || __ppc64__ */ |
|---|