/Modules/_ctypes/libffi/src/powerpc/ffi_darwin.c

http://unladen-swallow.googlecode.com/ · C · 800 lines · 493 code · 101 blank · 206 comment · 92 complexity · 99c864cc939078110592da3cedda290f MD5 · raw file

  1. /* -----------------------------------------------------------------------
  2. ffi_darwin.c
  3. Copyright (C) 1998 Geoffrey Keating
  4. Copyright (C) 2001 John Hornkvist
  5. Copyright (C) 2002, 2006, 2007 Free Software Foundation, Inc.
  6. FFI support for Darwin and AIX.
  7. Permission is hereby granted, free of charge, to any person obtaining
  8. a copy of this software and associated documentation files (the
  9. ``Software''), to deal in the Software without restriction, including
  10. without limitation the rights to use, copy, modify, merge, publish,
  11. distribute, sublicense, and/or sell copies of the Software, and to
  12. permit persons to whom the Software is furnished to do so, subject to
  13. the following conditions:
  14. The above copyright notice and this permission notice shall be included
  15. in all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. OTHER DEALINGS IN THE SOFTWARE.
  23. ----------------------------------------------------------------------- */
  24. #include <ffi.h>
  25. #include <ffi_common.h>
  26. #include <stdlib.h>
  27. extern void ffi_closure_ASM(void);
  28. enum {
  29. /* The assembly depends on these exact flags. */
  30. FLAG_RETURNS_NOTHING = 1 << (31-30), /* These go in cr7 */
  31. FLAG_RETURNS_FP = 1 << (31-29),
  32. FLAG_RETURNS_64BITS = 1 << (31-28),
  33. FLAG_RETURNS_128BITS = 1 << (31-31),
  34. FLAG_ARG_NEEDS_COPY = 1 << (31- 7),
  35. FLAG_FP_ARGUMENTS = 1 << (31- 6), /* cr1.eq; specified by ABI */
  36. FLAG_4_GPR_ARGUMENTS = 1 << (31- 5),
  37. FLAG_RETVAL_REFERENCE = 1 << (31- 4)
  38. };
  39. /* About the DARWIN ABI. */
  40. enum {
  41. NUM_GPR_ARG_REGISTERS = 8,
  42. NUM_FPR_ARG_REGISTERS = 13
  43. };
  44. enum { ASM_NEEDS_REGISTERS = 4 };
  45. /* ffi_prep_args is called by the assembly routine once stack space
  46. has been allocated for the function's arguments.
  47. The stack layout we want looks like this:
  48. | Return address from ffi_call_DARWIN | higher addresses
  49. |--------------------------------------------|
  50. | Previous backchain pointer 4 | stack pointer here
  51. |--------------------------------------------|<+ <<< on entry to
  52. | Saved r28-r31 4*4 | | ffi_call_DARWIN
  53. |--------------------------------------------| |
  54. | Parameters (at least 8*4=32) | |
  55. |--------------------------------------------| |
  56. | Space for GPR2 4 | |
  57. |--------------------------------------------| | stack |
  58. | Reserved 2*4 | | grows |
  59. |--------------------------------------------| | down V
  60. | Space for callee's LR 4 | |
  61. |--------------------------------------------| | lower addresses
  62. | Saved CR 4 | |
  63. |--------------------------------------------| | stack pointer here
  64. | Current backchain pointer 4 |-/ during
  65. |--------------------------------------------| <<< ffi_call_DARWIN
  66. */
  67. void ffi_prep_args(extended_cif *ecif, unsigned *const stack)
  68. {
  69. const unsigned bytes = ecif->cif->bytes;
  70. const unsigned flags = ecif->cif->flags;
  71. /* 'stacktop' points at the previous backchain pointer. */
  72. unsigned *const stacktop = stack + (bytes / sizeof(unsigned));
  73. /* 'fpr_base' points at the space for fpr1, and grows upwards as
  74. we use FPR registers. */
  75. double *fpr_base = (double*) (stacktop - ASM_NEEDS_REGISTERS) - NUM_FPR_ARG_REGISTERS;
  76. int fparg_count = 0;
  77. /* 'next_arg' grows up as we put parameters in it. */
  78. unsigned *next_arg = stack + 6; /* 6 reserved positions. */
  79. int i = ecif->cif->nargs;
  80. double double_tmp;
  81. void **p_argv = ecif->avalue;
  82. unsigned gprvalue;
  83. ffi_type** ptr = ecif->cif->arg_types;
  84. char *dest_cpy;
  85. unsigned size_al = 0;
  86. /* Check that everything starts aligned properly. */
  87. FFI_ASSERT(((unsigned)(char *)stack & 0xF) == 0);
  88. FFI_ASSERT(((unsigned)(char *)stacktop & 0xF) == 0);
  89. FFI_ASSERT((bytes & 0xF) == 0);
  90. /* Deal with return values that are actually pass-by-reference.
  91. Rule:
  92. Return values are referenced by r3, so r4 is the first parameter. */
  93. if (flags & FLAG_RETVAL_REFERENCE)
  94. *next_arg++ = (unsigned)(char *)ecif->rvalue;
  95. /* Now for the arguments. */
  96. for (;
  97. i > 0;
  98. i--, ptr++, p_argv++)
  99. {
  100. switch ((*ptr)->type)
  101. {
  102. /* If a floating-point parameter appears before all of the general-
  103. purpose registers are filled, the corresponding GPRs that match
  104. the size of the floating-point parameter are skipped. */
  105. case FFI_TYPE_FLOAT:
  106. double_tmp = *(float *)*p_argv;
  107. if (fparg_count >= NUM_FPR_ARG_REGISTERS)
  108. *(double *)next_arg = double_tmp;
  109. else
  110. *fpr_base++ = double_tmp;
  111. next_arg++;
  112. fparg_count++;
  113. FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
  114. break;
  115. case FFI_TYPE_DOUBLE:
  116. double_tmp = *(double *)*p_argv;
  117. if (fparg_count >= NUM_FPR_ARG_REGISTERS)
  118. *(double *)next_arg = double_tmp;
  119. else
  120. *fpr_base++ = double_tmp;
  121. next_arg += 2;
  122. fparg_count++;
  123. FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
  124. break;
  125. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  126. case FFI_TYPE_LONGDOUBLE:
  127. double_tmp = ((double *)*p_argv)[0];
  128. if (fparg_count >= NUM_FPR_ARG_REGISTERS)
  129. *(double *)next_arg = double_tmp;
  130. else
  131. *fpr_base++ = double_tmp;
  132. next_arg += 2;
  133. fparg_count++;
  134. double_tmp = ((double *)*p_argv)[1];
  135. if (fparg_count >= NUM_FPR_ARG_REGISTERS)
  136. *(double *)next_arg = double_tmp;
  137. else
  138. *fpr_base++ = double_tmp;
  139. next_arg += 2;
  140. fparg_count++;
  141. FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
  142. break;
  143. #endif
  144. case FFI_TYPE_UINT64:
  145. case FFI_TYPE_SINT64:
  146. *(long long *)next_arg = *(long long *)*p_argv;
  147. next_arg+=2;
  148. break;
  149. case FFI_TYPE_UINT8:
  150. gprvalue = *(unsigned char *)*p_argv;
  151. goto putgpr;
  152. case FFI_TYPE_SINT8:
  153. gprvalue = *(signed char *)*p_argv;
  154. goto putgpr;
  155. case FFI_TYPE_UINT16:
  156. gprvalue = *(unsigned short *)*p_argv;
  157. goto putgpr;
  158. case FFI_TYPE_SINT16:
  159. gprvalue = *(signed short *)*p_argv;
  160. goto putgpr;
  161. case FFI_TYPE_STRUCT:
  162. dest_cpy = (char *) next_arg;
  163. /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
  164. SI 4 bytes) are aligned as if they were those modes.
  165. Structures with 3 byte in size are padded upwards. */
  166. size_al = (*ptr)->size;
  167. /* If the first member of the struct is a double, then align
  168. the struct to double-word.
  169. Type 3 is defined in include/ffi.h. #define FFI_TYPE_DOUBLE 3. */
  170. if ((*ptr)->elements[0]->type == 3)
  171. size_al = ALIGN((*ptr)->size, 8);
  172. if (size_al < 3 && ecif->cif->abi == FFI_DARWIN)
  173. dest_cpy += 4 - size_al;
  174. memcpy((char *)dest_cpy, (char *)*p_argv, size_al);
  175. next_arg += (size_al + 3) / 4;
  176. break;
  177. case FFI_TYPE_INT:
  178. case FFI_TYPE_UINT32:
  179. case FFI_TYPE_SINT32:
  180. case FFI_TYPE_POINTER:
  181. gprvalue = *(unsigned *)*p_argv;
  182. putgpr:
  183. *next_arg++ = gprvalue;
  184. break;
  185. default:
  186. break;
  187. }
  188. }
  189. /* Check that we didn't overrun the stack... */
  190. //FFI_ASSERT(gpr_base <= stacktop - ASM_NEEDS_REGISTERS);
  191. //FFI_ASSERT((unsigned *)fpr_base
  192. // <= stacktop - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS);
  193. //FFI_ASSERT(flags & FLAG_4_GPR_ARGUMENTS || intarg_count <= 4);
  194. }
  195. /* Adjust the size of S to be correct for Darwin.
  196. On Darwin, the first field of a structure has natural alignment. */
  197. static void
  198. darwin_adjust_aggregate_sizes (ffi_type *s)
  199. {
  200. int i;
  201. if (s->type != FFI_TYPE_STRUCT)
  202. return;
  203. s->size = 0;
  204. for (i = 0; s->elements[i] != NULL; i++)
  205. {
  206. ffi_type *p;
  207. int align;
  208. p = s->elements[i];
  209. darwin_adjust_aggregate_sizes (p);
  210. if (i == 0
  211. && (p->type == FFI_TYPE_UINT64
  212. || p->type == FFI_TYPE_SINT64
  213. || p->type == FFI_TYPE_DOUBLE
  214. || p->alignment == 8))
  215. align = 8;
  216. else if (p->alignment == 16 || p->alignment < 4)
  217. align = p->alignment;
  218. else
  219. align = 4;
  220. s->size = ALIGN(s->size, align) + p->size;
  221. }
  222. s->size = ALIGN(s->size, s->alignment);
  223. if (s->elements[0]->type == FFI_TYPE_UINT64
  224. || s->elements[0]->type == FFI_TYPE_SINT64
  225. || s->elements[0]->type == FFI_TYPE_DOUBLE
  226. || s->elements[0]->alignment == 8)
  227. s->alignment = s->alignment > 8 ? s->alignment : 8;
  228. /* Do not add additional tail padding. */
  229. }
  230. /* Perform machine dependent cif processing. */
  231. ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
  232. {
  233. /* All this is for the DARWIN ABI. */
  234. int i;
  235. ffi_type **ptr;
  236. unsigned bytes;
  237. int fparg_count = 0, intarg_count = 0;
  238. unsigned flags = 0;
  239. unsigned size_al = 0;
  240. /* All the machine-independent calculation of cif->bytes will be wrong.
  241. All the calculation of structure sizes will also be wrong.
  242. Redo the calculation for DARWIN. */
  243. if (cif->abi == FFI_DARWIN)
  244. {
  245. darwin_adjust_aggregate_sizes (cif->rtype);
  246. for (i = 0; i < cif->nargs; i++)
  247. darwin_adjust_aggregate_sizes (cif->arg_types[i]);
  248. }
  249. /* Space for the frame pointer, callee's LR, CR, etc, and for
  250. the asm's temp regs. */
  251. bytes = (6 + ASM_NEEDS_REGISTERS) * sizeof(long);
  252. /* Return value handling. The rules are as follows:
  253. - 32-bit (or less) integer values are returned in gpr3;
  254. - Structures of size <= 4 bytes also returned in gpr3;
  255. - 64-bit integer values and structures between 5 and 8 bytes are returned
  256. in gpr3 and gpr4;
  257. - Single/double FP values are returned in fpr1;
  258. - Long double FP (if not equivalent to double) values are returned in
  259. fpr1 and fpr2;
  260. - Larger structures values are allocated space and a pointer is passed
  261. as the first argument. */
  262. switch (cif->rtype->type)
  263. {
  264. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  265. case FFI_TYPE_LONGDOUBLE:
  266. flags |= FLAG_RETURNS_128BITS;
  267. flags |= FLAG_RETURNS_FP;
  268. break;
  269. #endif
  270. case FFI_TYPE_DOUBLE:
  271. flags |= FLAG_RETURNS_64BITS;
  272. /* Fall through. */
  273. case FFI_TYPE_FLOAT:
  274. flags |= FLAG_RETURNS_FP;
  275. break;
  276. case FFI_TYPE_UINT64:
  277. case FFI_TYPE_SINT64:
  278. flags |= FLAG_RETURNS_64BITS;
  279. break;
  280. case FFI_TYPE_STRUCT:
  281. flags |= FLAG_RETVAL_REFERENCE;
  282. flags |= FLAG_RETURNS_NOTHING;
  283. intarg_count++;
  284. break;
  285. case FFI_TYPE_VOID:
  286. flags |= FLAG_RETURNS_NOTHING;
  287. break;
  288. default:
  289. /* Returns 32-bit integer, or similar. Nothing to do here. */
  290. break;
  291. }
  292. /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the
  293. first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest
  294. goes on the stack. Structures are passed as a pointer to a copy of
  295. the structure. Stuff on the stack needs to keep proper alignment. */
  296. for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
  297. {
  298. switch ((*ptr)->type)
  299. {
  300. case FFI_TYPE_FLOAT:
  301. case FFI_TYPE_DOUBLE:
  302. fparg_count++;
  303. /* If this FP arg is going on the stack, it must be
  304. 8-byte-aligned. */
  305. if (fparg_count > NUM_FPR_ARG_REGISTERS
  306. && intarg_count%2 != 0)
  307. intarg_count++;
  308. break;
  309. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  310. case FFI_TYPE_LONGDOUBLE:
  311. fparg_count += 2;
  312. /* If this FP arg is going on the stack, it must be
  313. 8-byte-aligned. */
  314. if (fparg_count > NUM_FPR_ARG_REGISTERS
  315. && intarg_count%2 != 0)
  316. intarg_count++;
  317. intarg_count +=2;
  318. break;
  319. #endif
  320. case FFI_TYPE_UINT64:
  321. case FFI_TYPE_SINT64:
  322. /* 'long long' arguments are passed as two words, but
  323. either both words must fit in registers or both go
  324. on the stack. If they go on the stack, they must
  325. be 8-byte-aligned. */
  326. if (intarg_count == NUM_GPR_ARG_REGISTERS-1
  327. || (intarg_count >= NUM_GPR_ARG_REGISTERS && intarg_count%2 != 0))
  328. intarg_count++;
  329. intarg_count += 2;
  330. break;
  331. case FFI_TYPE_STRUCT:
  332. size_al = (*ptr)->size;
  333. /* If the first member of the struct is a double, then align
  334. the struct to double-word.
  335. Type 3 is defined in include/ffi.h. #define FFI_TYPE_DOUBLE 3. */
  336. if ((*ptr)->elements[0]->type == 3)
  337. size_al = ALIGN((*ptr)->size, 8);
  338. intarg_count += (size_al + 3) / 4;
  339. break;
  340. default:
  341. /* Everything else is passed as a 4-byte word in a GPR, either
  342. the object itself or a pointer to it. */
  343. intarg_count++;
  344. break;
  345. }
  346. }
  347. if (fparg_count != 0)
  348. flags |= FLAG_FP_ARGUMENTS;
  349. /* Space for the FPR registers, if needed. */
  350. if (fparg_count != 0)
  351. bytes += NUM_FPR_ARG_REGISTERS * sizeof(double);
  352. /* Stack space. */
  353. if ((intarg_count + 2 * fparg_count) > NUM_GPR_ARG_REGISTERS)
  354. bytes += (intarg_count + 2 * fparg_count) * sizeof(long);
  355. else
  356. bytes += NUM_GPR_ARG_REGISTERS * sizeof(long);
  357. /* The stack space allocated needs to be a multiple of 16 bytes. */
  358. bytes = (bytes + 15) & ~0xF;
  359. cif->flags = flags;
  360. cif->bytes = bytes;
  361. return FFI_OK;
  362. }
  363. extern void ffi_call_AIX(extended_cif *, unsigned, unsigned, unsigned *,
  364. void (*fn)(void), void (*fn2)(void));
  365. extern void ffi_call_DARWIN(extended_cif *, unsigned, unsigned, unsigned *,
  366. void (*fn)(void), void (*fn2)(void));
  367. void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
  368. {
  369. extended_cif ecif;
  370. ecif.cif = cif;
  371. ecif.avalue = avalue;
  372. /* If the return value is a struct and we don't have a return
  373. value address then we need to make one. */
  374. if ((rvalue == NULL) &&
  375. (cif->rtype->type == FFI_TYPE_STRUCT))
  376. {
  377. ecif.rvalue = alloca(cif->rtype->size);
  378. }
  379. else
  380. ecif.rvalue = rvalue;
  381. switch (cif->abi)
  382. {
  383. case FFI_AIX:
  384. ffi_call_AIX(&ecif, -cif->bytes, cif->flags, ecif.rvalue, fn,
  385. ffi_prep_args);
  386. break;
  387. case FFI_DARWIN:
  388. ffi_call_DARWIN(&ecif, -cif->bytes, cif->flags, ecif.rvalue, fn,
  389. ffi_prep_args);
  390. break;
  391. default:
  392. FFI_ASSERT(0);
  393. break;
  394. }
  395. }
  396. static void flush_icache(char *);
  397. static void flush_range(char *, int);
  398. /* The layout of a function descriptor. A C function pointer really
  399. points to one of these. */
  400. typedef struct aix_fd_struct {
  401. void *code_pointer;
  402. void *toc;
  403. } aix_fd;
  404. /* here I'd like to add the stack frame layout we use in darwin_closure.S
  405. and aix_clsoure.S
  406. SP previous -> +---------------------------------------+ <--- child frame
  407. | back chain to caller 4 |
  408. +---------------------------------------+ 4
  409. | saved CR 4 |
  410. +---------------------------------------+ 8
  411. | saved LR 4 |
  412. +---------------------------------------+ 12
  413. | reserved for compilers 4 |
  414. +---------------------------------------+ 16
  415. | reserved for binders 4 |
  416. +---------------------------------------+ 20
  417. | saved TOC pointer 4 |
  418. +---------------------------------------+ 24
  419. | always reserved 8*4=32 (previous GPRs)|
  420. | according to the linkage convention |
  421. | from AIX |
  422. +---------------------------------------+ 56
  423. | our FPR area 13*8=104 |
  424. | f1 |
  425. | . |
  426. | f13 |
  427. +---------------------------------------+ 160
  428. | result area 8 |
  429. +---------------------------------------+ 168
  430. | alignement to the next multiple of 16 |
  431. SP current --> +---------------------------------------+ 176 <- parent frame
  432. | back chain to caller 4 |
  433. +---------------------------------------+ 180
  434. | saved CR 4 |
  435. +---------------------------------------+ 184
  436. | saved LR 4 |
  437. +---------------------------------------+ 188
  438. | reserved for compilers 4 |
  439. +---------------------------------------+ 192
  440. | reserved for binders 4 |
  441. +---------------------------------------+ 196
  442. | saved TOC pointer 4 |
  443. +---------------------------------------+ 200
  444. | always reserved 8*4=32 we store our |
  445. | GPRs here |
  446. | r3 |
  447. | . |
  448. | r10 |
  449. +---------------------------------------+ 232
  450. | overflow part |
  451. +---------------------------------------+ xxx
  452. | ???? |
  453. +---------------------------------------+ xxx
  454. */
  455. ffi_status
  456. ffi_prep_closure_loc (ffi_closure* closure,
  457. ffi_cif* cif,
  458. void (*fun)(ffi_cif*, void*, void**, void*),
  459. void *user_data,
  460. void *codeloc)
  461. {
  462. unsigned int *tramp;
  463. struct ffi_aix_trampoline_struct *tramp_aix;
  464. aix_fd *fd;
  465. switch (cif->abi)
  466. {
  467. case FFI_DARWIN:
  468. FFI_ASSERT (cif->abi == FFI_DARWIN);
  469. tramp = (unsigned int *) &closure->tramp[0];
  470. tramp[0] = 0x7c0802a6; /* mflr r0 */
  471. tramp[1] = 0x429f000d; /* bcl- 20,4*cr7+so,0x10 */
  472. tramp[4] = 0x7d6802a6; /* mflr r11 */
  473. tramp[5] = 0x818b0000; /* lwz r12,0(r11) function address */
  474. tramp[6] = 0x7c0803a6; /* mtlr r0 */
  475. tramp[7] = 0x7d8903a6; /* mtctr r12 */
  476. tramp[8] = 0x816b0004; /* lwz r11,4(r11) static chain */
  477. tramp[9] = 0x4e800420; /* bctr */
  478. tramp[2] = (unsigned long) ffi_closure_ASM; /* function */
  479. tramp[3] = (unsigned long) codeloc; /* context */
  480. closure->cif = cif;
  481. closure->fun = fun;
  482. closure->user_data = user_data;
  483. /* Flush the icache. Only necessary on Darwin. */
  484. flush_range(codeloc, FFI_TRAMPOLINE_SIZE);
  485. break;
  486. case FFI_AIX:
  487. tramp_aix = (struct ffi_aix_trampoline_struct *) (closure->tramp);
  488. fd = (aix_fd *)(void *)ffi_closure_ASM;
  489. FFI_ASSERT (cif->abi == FFI_AIX);
  490. tramp_aix->code_pointer = fd->code_pointer;
  491. tramp_aix->toc = fd->toc;
  492. tramp_aix->static_chain = codeloc;
  493. closure->cif = cif;
  494. closure->fun = fun;
  495. closure->user_data = user_data;
  496. default:
  497. FFI_ASSERT(0);
  498. break;
  499. }
  500. return FFI_OK;
  501. }
  502. static void
  503. flush_icache(char *addr)
  504. {
  505. #ifndef _AIX
  506. __asm__ volatile (
  507. "dcbf 0,%0\n"
  508. "\tsync\n"
  509. "\ticbi 0,%0\n"
  510. "\tsync\n"
  511. "\tisync"
  512. : : "r"(addr) : "memory");
  513. #endif
  514. }
  515. static void
  516. flush_range(char * addr1, int size)
  517. {
  518. #define MIN_LINE_SIZE 32
  519. int i;
  520. for (i = 0; i < size; i += MIN_LINE_SIZE)
  521. flush_icache(addr1+i);
  522. flush_icache(addr1+size-1);
  523. }
  524. typedef union
  525. {
  526. float f;
  527. double d;
  528. } ffi_dblfl;
  529. int ffi_closure_helper_DARWIN (ffi_closure*, void*,
  530. unsigned long*, ffi_dblfl*);
  531. /* Basically the trampoline invokes ffi_closure_ASM, and on
  532. entry, r11 holds the address of the closure.
  533. After storing the registers that could possibly contain
  534. parameters to be passed into the stack frame and setting
  535. up space for a return value, ffi_closure_ASM invokes the
  536. following helper function to do most of the work. */
  537. int ffi_closure_helper_DARWIN (ffi_closure* closure, void * rvalue,
  538. unsigned long * pgr, ffi_dblfl * pfr)
  539. {
  540. /* rvalue is the pointer to space for return value in closure assembly
  541. pgr is the pointer to where r3-r10 are stored in ffi_closure_ASM
  542. pfr is the pointer to where f1-f13 are stored in ffi_closure_ASM. */
  543. typedef double ldbits[2];
  544. union ldu
  545. {
  546. ldbits lb;
  547. long double ld;
  548. };
  549. void ** avalue;
  550. ffi_type ** arg_types;
  551. long i, avn;
  552. long nf; /* number of floating registers already used. */
  553. long ng; /* number of general registers already used. */
  554. ffi_cif * cif;
  555. double temp;
  556. unsigned size_al;
  557. union ldu temp_ld;
  558. cif = closure->cif;
  559. avalue = alloca(cif->nargs * sizeof(void *));
  560. nf = 0;
  561. ng = 0;
  562. /* Copy the caller's structure return value address so that the closure
  563. returns the data directly to the caller. */
  564. if (cif->rtype->type == FFI_TYPE_STRUCT)
  565. {
  566. rvalue = (void *) *pgr;
  567. pgr++;
  568. ng++;
  569. }
  570. i = 0;
  571. avn = cif->nargs;
  572. arg_types = cif->arg_types;
  573. /* Grab the addresses of the arguments from the stack frame. */
  574. while (i < avn)
  575. {
  576. switch (arg_types[i]->type)
  577. {
  578. case FFI_TYPE_SINT8:
  579. case FFI_TYPE_UINT8:
  580. avalue[i] = (char *) pgr + 3;
  581. ng++;
  582. pgr++;
  583. break;
  584. case FFI_TYPE_SINT16:
  585. case FFI_TYPE_UINT16:
  586. avalue[i] = (char *) pgr + 2;
  587. ng++;
  588. pgr++;
  589. break;
  590. case FFI_TYPE_SINT32:
  591. case FFI_TYPE_UINT32:
  592. case FFI_TYPE_POINTER:
  593. avalue[i] = pgr;
  594. ng++;
  595. pgr++;
  596. break;
  597. case FFI_TYPE_STRUCT:
  598. /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
  599. SI 4 bytes) are aligned as if they were those modes. */
  600. size_al = arg_types[i]->size;
  601. /* If the first member of the struct is a double, then align
  602. the struct to double-word.
  603. Type 3 is defined in include/ffi.h. #define FFI_TYPE_DOUBLE 3. */
  604. if (arg_types[i]->elements[0]->type == 3)
  605. size_al = ALIGN(arg_types[i]->size, 8);
  606. if (size_al < 3 && cif->abi == FFI_DARWIN)
  607. avalue[i] = (void*) pgr + 4 - size_al;
  608. else
  609. avalue[i] = (void*) pgr;
  610. ng += (size_al + 3) / 4;
  611. pgr += (size_al + 3) / 4;
  612. break;
  613. case FFI_TYPE_SINT64:
  614. case FFI_TYPE_UINT64:
  615. /* Long long ints are passed in two gpr's. */
  616. avalue[i] = pgr;
  617. ng += 2;
  618. pgr += 2;
  619. break;
  620. case FFI_TYPE_FLOAT:
  621. /* A float value consumes a GPR.
  622. There are 13 64bit floating point registers. */
  623. if (nf < NUM_FPR_ARG_REGISTERS)
  624. {
  625. temp = pfr->d;
  626. pfr->f = (float)temp;
  627. avalue[i] = pfr;
  628. pfr++;
  629. }
  630. else
  631. {
  632. avalue[i] = pgr;
  633. }
  634. nf++;
  635. ng++;
  636. pgr++;
  637. break;
  638. case FFI_TYPE_DOUBLE:
  639. /* A double value consumes two GPRs.
  640. There are 13 64bit floating point registers. */
  641. if (nf < NUM_FPR_ARG_REGISTERS)
  642. {
  643. avalue[i] = pfr;
  644. pfr++;
  645. }
  646. else
  647. {
  648. avalue[i] = pgr;
  649. }
  650. nf++;
  651. ng += 2;
  652. pgr += 2;
  653. break;
  654. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  655. case FFI_TYPE_LONGDOUBLE:
  656. /* A long double value consumes four GPRs and two FPRs.
  657. There are 13 64bit floating point registers. */
  658. if (nf < NUM_FPR_ARG_REGISTERS - 1)
  659. {
  660. avalue[i] = pfr;
  661. pfr += 2;
  662. }
  663. /* Here we have the situation where one part of the long double
  664. is stored in fpr13 and the other part is already on the stack.
  665. We use a union to pass the long double to avalue[i]. */
  666. else if (nf == NUM_FPR_ARG_REGISTERS - 1)
  667. {
  668. memcpy (&temp_ld.lb[0], pfr, sizeof(ldbits));
  669. memcpy (&temp_ld.lb[1], pgr + 2, sizeof(ldbits));
  670. avalue[i] = &temp_ld.ld;
  671. }
  672. else
  673. {
  674. avalue[i] = pgr;
  675. }
  676. nf += 2;
  677. ng += 4;
  678. pgr += 4;
  679. break;
  680. #endif
  681. default:
  682. FFI_ASSERT(0);
  683. }
  684. i++;
  685. }
  686. (closure->fun) (cif, rvalue, avalue, closure->user_data);
  687. /* Tell ffi_closure_ASM to perform return type promotions. */
  688. return cif->rtype->type;
  689. }