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

http://unladen-swallow.googlecode.com/ · C · 1429 lines · 1004 code · 151 blank · 274 comment · 213 complexity · cf9067966101c83870b90ce82fac84b2 MD5 · raw file

  1. /* -----------------------------------------------------------------------
  2. ffi.c - Copyright (c) 1998 Geoffrey Keating
  3. Copyright (C) 2007 Free Software Foundation, Inc
  4. Copyright (C) 2008 Red Hat, Inc
  5. PowerPC Foreign Function Interface
  6. Permission is hereby granted, free of charge, to any person obtaining
  7. a copy of this software and associated documentation files (the
  8. ``Software''), to deal in the Software without restriction, including
  9. without limitation the rights to use, copy, modify, merge, publish,
  10. distribute, sublicense, and/or sell copies of the Software, and to
  11. permit persons to whom the Software is furnished to do so, subject to
  12. the following conditions:
  13. The above copyright notice and this permission notice shall be included
  14. in all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. ----------------------------------------------------------------------- */
  23. #include <ffi.h>
  24. #include <ffi_common.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. extern void ffi_closure_SYSV (void);
  28. extern void FFI_HIDDEN ffi_closure_LINUX64 (void);
  29. enum {
  30. /* The assembly depends on these exact flags. */
  31. FLAG_RETURNS_SMST = 1 << (31-31), /* Used for FFI_SYSV small structs. */
  32. FLAG_RETURNS_NOTHING = 1 << (31-30), /* These go in cr7 */
  33. FLAG_RETURNS_FP = 1 << (31-29),
  34. FLAG_RETURNS_64BITS = 1 << (31-28),
  35. FLAG_RETURNS_128BITS = 1 << (31-27), /* cr6 */
  36. FLAG_ARG_NEEDS_COPY = 1 << (31- 7),
  37. FLAG_FP_ARGUMENTS = 1 << (31- 6), /* cr1.eq; specified by ABI */
  38. FLAG_4_GPR_ARGUMENTS = 1 << (31- 5),
  39. FLAG_RETVAL_REFERENCE = 1 << (31- 4)
  40. };
  41. /* About the SYSV ABI. */
  42. unsigned int NUM_GPR_ARG_REGISTERS = 8;
  43. #ifndef __NO_FPRS__
  44. unsigned int NUM_FPR_ARG_REGISTERS = 8;
  45. #else
  46. unsigned int NUM_FPR_ARG_REGISTERS = 0;
  47. #endif
  48. enum { ASM_NEEDS_REGISTERS = 4 };
  49. /* ffi_prep_args_SYSV is called by the assembly routine once stack space
  50. has been allocated for the function's arguments.
  51. The stack layout we want looks like this:
  52. | Return address from ffi_call_SYSV 4bytes | higher addresses
  53. |--------------------------------------------|
  54. | Previous backchain pointer 4 | stack pointer here
  55. |--------------------------------------------|<+ <<< on entry to
  56. | Saved r28-r31 4*4 | | ffi_call_SYSV
  57. |--------------------------------------------| |
  58. | GPR registers r3-r10 8*4 | | ffi_call_SYSV
  59. |--------------------------------------------| |
  60. | FPR registers f1-f8 (optional) 8*8 | |
  61. |--------------------------------------------| | stack |
  62. | Space for copied structures | | grows |
  63. |--------------------------------------------| | down V
  64. | Parameters that didn't fit in registers | |
  65. |--------------------------------------------| | lower addresses
  66. | Space for callee's LR 4 | |
  67. |--------------------------------------------| | stack pointer here
  68. | Current backchain pointer 4 |-/ during
  69. |--------------------------------------------| <<< ffi_call_SYSV
  70. */
  71. void
  72. ffi_prep_args_SYSV (extended_cif *ecif, unsigned *const stack)
  73. {
  74. const unsigned bytes = ecif->cif->bytes;
  75. const unsigned flags = ecif->cif->flags;
  76. typedef union {
  77. char *c;
  78. unsigned *u;
  79. long long *ll;
  80. float *f;
  81. double *d;
  82. } valp;
  83. /* 'stacktop' points at the previous backchain pointer. */
  84. valp stacktop;
  85. /* 'gpr_base' points at the space for gpr3, and grows upwards as
  86. we use GPR registers. */
  87. valp gpr_base;
  88. int intarg_count;
  89. /* 'fpr_base' points at the space for fpr1, and grows upwards as
  90. we use FPR registers. */
  91. valp fpr_base;
  92. int fparg_count;
  93. /* 'copy_space' grows down as we put structures in it. It should
  94. stay 16-byte aligned. */
  95. valp copy_space;
  96. /* 'next_arg' grows up as we put parameters in it. */
  97. valp next_arg;
  98. int i, ii MAYBE_UNUSED;
  99. ffi_type **ptr;
  100. double double_tmp;
  101. union {
  102. void **v;
  103. char **c;
  104. signed char **sc;
  105. unsigned char **uc;
  106. signed short **ss;
  107. unsigned short **us;
  108. unsigned int **ui;
  109. long long **ll;
  110. float **f;
  111. double **d;
  112. } p_argv;
  113. size_t struct_copy_size;
  114. unsigned gprvalue;
  115. if (ecif->cif->abi == FFI_LINUX_SOFT_FLOAT)
  116. NUM_FPR_ARG_REGISTERS = 0;
  117. stacktop.c = (char *) stack + bytes;
  118. gpr_base.u = stacktop.u - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS;
  119. intarg_count = 0;
  120. fpr_base.d = gpr_base.d - NUM_FPR_ARG_REGISTERS;
  121. fparg_count = 0;
  122. copy_space.c = ((flags & FLAG_FP_ARGUMENTS) ? fpr_base.c : gpr_base.c);
  123. next_arg.u = stack + 2;
  124. /* Check that everything starts aligned properly. */
  125. FFI_ASSERT (((unsigned) (char *) stack & 0xF) == 0);
  126. FFI_ASSERT (((unsigned) copy_space.c & 0xF) == 0);
  127. FFI_ASSERT (((unsigned) stacktop.c & 0xF) == 0);
  128. FFI_ASSERT ((bytes & 0xF) == 0);
  129. FFI_ASSERT (copy_space.c >= next_arg.c);
  130. /* Deal with return values that are actually pass-by-reference. */
  131. if (flags & FLAG_RETVAL_REFERENCE)
  132. {
  133. *gpr_base.u++ = (unsigned long) (char *) ecif->rvalue;
  134. intarg_count++;
  135. }
  136. /* Now for the arguments. */
  137. p_argv.v = ecif->avalue;
  138. for (ptr = ecif->cif->arg_types, i = ecif->cif->nargs;
  139. i > 0;
  140. i--, ptr++, p_argv.v++)
  141. {
  142. switch ((*ptr)->type)
  143. {
  144. case FFI_TYPE_FLOAT:
  145. /* With FFI_LINUX_SOFT_FLOAT floats are handled like UINT32. */
  146. if (ecif->cif->abi == FFI_LINUX_SOFT_FLOAT)
  147. goto soft_float_prep;
  148. double_tmp = **p_argv.f;
  149. if (fparg_count >= NUM_FPR_ARG_REGISTERS)
  150. {
  151. *next_arg.f = (float) double_tmp;
  152. next_arg.u += 1;
  153. }
  154. else
  155. *fpr_base.d++ = double_tmp;
  156. fparg_count++;
  157. FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
  158. break;
  159. case FFI_TYPE_DOUBLE:
  160. /* With FFI_LINUX_SOFT_FLOAT doubles are handled like UINT64. */
  161. if (ecif->cif->abi == FFI_LINUX_SOFT_FLOAT)
  162. goto soft_double_prep;
  163. double_tmp = **p_argv.d;
  164. if (fparg_count >= NUM_FPR_ARG_REGISTERS)
  165. {
  166. if (intarg_count >= NUM_GPR_ARG_REGISTERS
  167. && intarg_count % 2 != 0)
  168. {
  169. intarg_count++;
  170. next_arg.u++;
  171. }
  172. *next_arg.d = double_tmp;
  173. next_arg.u += 2;
  174. }
  175. else
  176. *fpr_base.d++ = double_tmp;
  177. fparg_count++;
  178. FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
  179. break;
  180. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  181. case FFI_TYPE_LONGDOUBLE:
  182. if ((ecif->cif->abi != FFI_LINUX)
  183. && (ecif->cif->abi != FFI_LINUX_SOFT_FLOAT))
  184. goto do_struct;
  185. /* The soft float ABI for long doubles works like this,
  186. a long double is passed in four consecutive gprs if available.
  187. A maximum of 2 long doubles can be passed in gprs.
  188. If we do not have 4 gprs left, the long double is passed on the
  189. stack, 4-byte aligned. */
  190. if (ecif->cif->abi == FFI_LINUX_SOFT_FLOAT)
  191. {
  192. unsigned int int_tmp = (*p_argv.ui)[0];
  193. if (intarg_count >= NUM_GPR_ARG_REGISTERS - 3)
  194. {
  195. if (intarg_count < NUM_GPR_ARG_REGISTERS)
  196. intarg_count += NUM_GPR_ARG_REGISTERS - intarg_count;
  197. *next_arg.u = int_tmp;
  198. next_arg.u++;
  199. for (ii = 1; ii < 4; ii++)
  200. {
  201. int_tmp = (*p_argv.ui)[ii];
  202. *next_arg.u = int_tmp;
  203. next_arg.u++;
  204. }
  205. }
  206. else
  207. {
  208. *gpr_base.u++ = int_tmp;
  209. for (ii = 1; ii < 4; ii++)
  210. {
  211. int_tmp = (*p_argv.ui)[ii];
  212. *gpr_base.u++ = int_tmp;
  213. }
  214. }
  215. intarg_count +=4;
  216. }
  217. else
  218. {
  219. double_tmp = (*p_argv.d)[0];
  220. if (fparg_count >= NUM_FPR_ARG_REGISTERS - 1)
  221. {
  222. if (intarg_count >= NUM_GPR_ARG_REGISTERS
  223. && intarg_count % 2 != 0)
  224. {
  225. intarg_count++;
  226. next_arg.u++;
  227. }
  228. *next_arg.d = double_tmp;
  229. next_arg.u += 2;
  230. double_tmp = (*p_argv.d)[1];
  231. *next_arg.d = double_tmp;
  232. next_arg.u += 2;
  233. }
  234. else
  235. {
  236. *fpr_base.d++ = double_tmp;
  237. double_tmp = (*p_argv.d)[1];
  238. *fpr_base.d++ = double_tmp;
  239. }
  240. fparg_count += 2;
  241. FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
  242. }
  243. break;
  244. #endif
  245. case FFI_TYPE_UINT64:
  246. case FFI_TYPE_SINT64:
  247. soft_double_prep:
  248. if (intarg_count == NUM_GPR_ARG_REGISTERS-1)
  249. intarg_count++;
  250. if (intarg_count >= NUM_GPR_ARG_REGISTERS)
  251. {
  252. if (intarg_count % 2 != 0)
  253. {
  254. intarg_count++;
  255. next_arg.u++;
  256. }
  257. *next_arg.ll = **p_argv.ll;
  258. next_arg.u += 2;
  259. }
  260. else
  261. {
  262. /* whoops: abi states only certain register pairs
  263. * can be used for passing long long int
  264. * specifically (r3,r4), (r5,r6), (r7,r8),
  265. * (r9,r10) and if next arg is long long but
  266. * not correct starting register of pair then skip
  267. * until the proper starting register
  268. */
  269. if (intarg_count % 2 != 0)
  270. {
  271. intarg_count ++;
  272. gpr_base.u++;
  273. }
  274. *gpr_base.ll++ = **p_argv.ll;
  275. }
  276. intarg_count += 2;
  277. break;
  278. case FFI_TYPE_STRUCT:
  279. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  280. do_struct:
  281. #endif
  282. struct_copy_size = ((*ptr)->size + 15) & ~0xF;
  283. copy_space.c -= struct_copy_size;
  284. memcpy (copy_space.c, *p_argv.c, (*ptr)->size);
  285. gprvalue = (unsigned long) copy_space.c;
  286. FFI_ASSERT (copy_space.c > next_arg.c);
  287. FFI_ASSERT (flags & FLAG_ARG_NEEDS_COPY);
  288. goto putgpr;
  289. case FFI_TYPE_UINT8:
  290. gprvalue = **p_argv.uc;
  291. goto putgpr;
  292. case FFI_TYPE_SINT8:
  293. gprvalue = **p_argv.sc;
  294. goto putgpr;
  295. case FFI_TYPE_UINT16:
  296. gprvalue = **p_argv.us;
  297. goto putgpr;
  298. case FFI_TYPE_SINT16:
  299. gprvalue = **p_argv.ss;
  300. goto putgpr;
  301. case FFI_TYPE_INT:
  302. case FFI_TYPE_UINT32:
  303. case FFI_TYPE_SINT32:
  304. case FFI_TYPE_POINTER:
  305. soft_float_prep:
  306. gprvalue = **p_argv.ui;
  307. putgpr:
  308. if (intarg_count >= NUM_GPR_ARG_REGISTERS)
  309. *next_arg.u++ = gprvalue;
  310. else
  311. *gpr_base.u++ = gprvalue;
  312. intarg_count++;
  313. break;
  314. }
  315. }
  316. /* Check that we didn't overrun the stack... */
  317. FFI_ASSERT (copy_space.c >= next_arg.c);
  318. FFI_ASSERT (gpr_base.u <= stacktop.u - ASM_NEEDS_REGISTERS);
  319. FFI_ASSERT (fpr_base.u
  320. <= stacktop.u - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS);
  321. FFI_ASSERT (flags & FLAG_4_GPR_ARGUMENTS || intarg_count <= 4);
  322. }
  323. /* About the LINUX64 ABI. */
  324. enum {
  325. NUM_GPR_ARG_REGISTERS64 = 8,
  326. NUM_FPR_ARG_REGISTERS64 = 13
  327. };
  328. enum { ASM_NEEDS_REGISTERS64 = 4 };
  329. /* ffi_prep_args64 is called by the assembly routine once stack space
  330. has been allocated for the function's arguments.
  331. The stack layout we want looks like this:
  332. | Ret addr from ffi_call_LINUX64 8bytes | higher addresses
  333. |--------------------------------------------|
  334. | CR save area 8bytes |
  335. |--------------------------------------------|
  336. | Previous backchain pointer 8 | stack pointer here
  337. |--------------------------------------------|<+ <<< on entry to
  338. | Saved r28-r31 4*8 | | ffi_call_LINUX64
  339. |--------------------------------------------| |
  340. | GPR registers r3-r10 8*8 | |
  341. |--------------------------------------------| |
  342. | FPR registers f1-f13 (optional) 13*8 | |
  343. |--------------------------------------------| |
  344. | Parameter save area | |
  345. |--------------------------------------------| |
  346. | TOC save area 8 | |
  347. |--------------------------------------------| | stack |
  348. | Linker doubleword 8 | | grows |
  349. |--------------------------------------------| | down V
  350. | Compiler doubleword 8 | |
  351. |--------------------------------------------| | lower addresses
  352. | Space for callee's LR 8 | |
  353. |--------------------------------------------| |
  354. | CR save area 8 | |
  355. |--------------------------------------------| | stack pointer here
  356. | Current backchain pointer 8 |-/ during
  357. |--------------------------------------------| <<< ffi_call_LINUX64
  358. */
  359. void FFI_HIDDEN
  360. ffi_prep_args64 (extended_cif *ecif, unsigned long *const stack)
  361. {
  362. const unsigned long bytes = ecif->cif->bytes;
  363. const unsigned long flags = ecif->cif->flags;
  364. typedef union {
  365. char *c;
  366. unsigned long *ul;
  367. float *f;
  368. double *d;
  369. } valp;
  370. /* 'stacktop' points at the previous backchain pointer. */
  371. valp stacktop;
  372. /* 'next_arg' points at the space for gpr3, and grows upwards as
  373. we use GPR registers, then continues at rest. */
  374. valp gpr_base;
  375. valp gpr_end;
  376. valp rest;
  377. valp next_arg;
  378. /* 'fpr_base' points at the space for fpr3, and grows upwards as
  379. we use FPR registers. */
  380. valp fpr_base;
  381. int fparg_count;
  382. int i, words;
  383. ffi_type **ptr;
  384. double double_tmp;
  385. union {
  386. void **v;
  387. char **c;
  388. signed char **sc;
  389. unsigned char **uc;
  390. signed short **ss;
  391. unsigned short **us;
  392. signed int **si;
  393. unsigned int **ui;
  394. unsigned long **ul;
  395. float **f;
  396. double **d;
  397. } p_argv;
  398. unsigned long gprvalue;
  399. stacktop.c = (char *) stack + bytes;
  400. gpr_base.ul = stacktop.ul - ASM_NEEDS_REGISTERS64 - NUM_GPR_ARG_REGISTERS64;
  401. gpr_end.ul = gpr_base.ul + NUM_GPR_ARG_REGISTERS64;
  402. rest.ul = stack + 6 + NUM_GPR_ARG_REGISTERS64;
  403. fpr_base.d = gpr_base.d - NUM_FPR_ARG_REGISTERS64;
  404. fparg_count = 0;
  405. next_arg.ul = gpr_base.ul;
  406. /* Check that everything starts aligned properly. */
  407. FFI_ASSERT (((unsigned long) (char *) stack & 0xF) == 0);
  408. FFI_ASSERT (((unsigned long) stacktop.c & 0xF) == 0);
  409. FFI_ASSERT ((bytes & 0xF) == 0);
  410. /* Deal with return values that are actually pass-by-reference. */
  411. if (flags & FLAG_RETVAL_REFERENCE)
  412. *next_arg.ul++ = (unsigned long) (char *) ecif->rvalue;
  413. /* Now for the arguments. */
  414. p_argv.v = ecif->avalue;
  415. for (ptr = ecif->cif->arg_types, i = ecif->cif->nargs;
  416. i > 0;
  417. i--, ptr++, p_argv.v++)
  418. {
  419. switch ((*ptr)->type)
  420. {
  421. case FFI_TYPE_FLOAT:
  422. double_tmp = **p_argv.f;
  423. *next_arg.f = (float) double_tmp;
  424. if (++next_arg.ul == gpr_end.ul)
  425. next_arg.ul = rest.ul;
  426. if (fparg_count < NUM_FPR_ARG_REGISTERS64)
  427. *fpr_base.d++ = double_tmp;
  428. fparg_count++;
  429. FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
  430. break;
  431. case FFI_TYPE_DOUBLE:
  432. double_tmp = **p_argv.d;
  433. *next_arg.d = double_tmp;
  434. if (++next_arg.ul == gpr_end.ul)
  435. next_arg.ul = rest.ul;
  436. if (fparg_count < NUM_FPR_ARG_REGISTERS64)
  437. *fpr_base.d++ = double_tmp;
  438. fparg_count++;
  439. FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
  440. break;
  441. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  442. case FFI_TYPE_LONGDOUBLE:
  443. double_tmp = (*p_argv.d)[0];
  444. *next_arg.d = double_tmp;
  445. if (++next_arg.ul == gpr_end.ul)
  446. next_arg.ul = rest.ul;
  447. if (fparg_count < NUM_FPR_ARG_REGISTERS64)
  448. *fpr_base.d++ = double_tmp;
  449. fparg_count++;
  450. double_tmp = (*p_argv.d)[1];
  451. *next_arg.d = double_tmp;
  452. if (++next_arg.ul == gpr_end.ul)
  453. next_arg.ul = rest.ul;
  454. if (fparg_count < NUM_FPR_ARG_REGISTERS64)
  455. *fpr_base.d++ = double_tmp;
  456. fparg_count++;
  457. FFI_ASSERT (__LDBL_MANT_DIG__ == 106);
  458. FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
  459. break;
  460. #endif
  461. case FFI_TYPE_STRUCT:
  462. words = ((*ptr)->size + 7) / 8;
  463. if (next_arg.ul >= gpr_base.ul && next_arg.ul + words > gpr_end.ul)
  464. {
  465. size_t first = gpr_end.c - next_arg.c;
  466. memcpy (next_arg.c, *p_argv.c, first);
  467. memcpy (rest.c, *p_argv.c + first, (*ptr)->size - first);
  468. next_arg.c = rest.c + words * 8 - first;
  469. }
  470. else
  471. {
  472. char *where = next_arg.c;
  473. /* Structures with size less than eight bytes are passed
  474. left-padded. */
  475. if ((*ptr)->size < 8)
  476. where += 8 - (*ptr)->size;
  477. memcpy (where, *p_argv.c, (*ptr)->size);
  478. next_arg.ul += words;
  479. if (next_arg.ul == gpr_end.ul)
  480. next_arg.ul = rest.ul;
  481. }
  482. break;
  483. case FFI_TYPE_UINT8:
  484. gprvalue = **p_argv.uc;
  485. goto putgpr;
  486. case FFI_TYPE_SINT8:
  487. gprvalue = **p_argv.sc;
  488. goto putgpr;
  489. case FFI_TYPE_UINT16:
  490. gprvalue = **p_argv.us;
  491. goto putgpr;
  492. case FFI_TYPE_SINT16:
  493. gprvalue = **p_argv.ss;
  494. goto putgpr;
  495. case FFI_TYPE_UINT32:
  496. gprvalue = **p_argv.ui;
  497. goto putgpr;
  498. case FFI_TYPE_INT:
  499. case FFI_TYPE_SINT32:
  500. gprvalue = **p_argv.si;
  501. goto putgpr;
  502. case FFI_TYPE_UINT64:
  503. case FFI_TYPE_SINT64:
  504. case FFI_TYPE_POINTER:
  505. gprvalue = **p_argv.ul;
  506. putgpr:
  507. *next_arg.ul++ = gprvalue;
  508. if (next_arg.ul == gpr_end.ul)
  509. next_arg.ul = rest.ul;
  510. break;
  511. }
  512. }
  513. FFI_ASSERT (flags & FLAG_4_GPR_ARGUMENTS
  514. || (next_arg.ul >= gpr_base.ul
  515. && next_arg.ul <= gpr_base.ul + 4));
  516. }
  517. /* Perform machine dependent cif processing */
  518. ffi_status
  519. ffi_prep_cif_machdep (ffi_cif *cif)
  520. {
  521. /* All this is for the SYSV and LINUX64 ABI. */
  522. int i;
  523. ffi_type **ptr;
  524. unsigned bytes;
  525. int fparg_count = 0, intarg_count = 0;
  526. unsigned flags = 0;
  527. unsigned struct_copy_size = 0;
  528. unsigned type = cif->rtype->type;
  529. unsigned size = cif->rtype->size;
  530. if (cif->abi == FFI_LINUX_SOFT_FLOAT)
  531. NUM_FPR_ARG_REGISTERS = 0;
  532. if (cif->abi != FFI_LINUX64)
  533. {
  534. /* All the machine-independent calculation of cif->bytes will be wrong.
  535. Redo the calculation for SYSV. */
  536. /* Space for the frame pointer, callee's LR, and the asm's temp regs. */
  537. bytes = (2 + ASM_NEEDS_REGISTERS) * sizeof (int);
  538. /* Space for the GPR registers. */
  539. bytes += NUM_GPR_ARG_REGISTERS * sizeof (int);
  540. }
  541. else
  542. {
  543. /* 64-bit ABI. */
  544. /* Space for backchain, CR, LR, cc/ld doubleword, TOC and the asm's temp
  545. regs. */
  546. bytes = (6 + ASM_NEEDS_REGISTERS64) * sizeof (long);
  547. /* Space for the mandatory parm save area and general registers. */
  548. bytes += 2 * NUM_GPR_ARG_REGISTERS64 * sizeof (long);
  549. }
  550. /* Return value handling. The rules for SYSV are as follows:
  551. - 32-bit (or less) integer values are returned in gpr3;
  552. - Structures of size <= 4 bytes also returned in gpr3;
  553. - 64-bit integer values and structures between 5 and 8 bytes are returned
  554. in gpr3 and gpr4;
  555. - Single/double FP values are returned in fpr1;
  556. - Larger structures are allocated space and a pointer is passed as
  557. the first argument.
  558. - long doubles (if not equivalent to double) are returned in
  559. fpr1,fpr2 for Linux and as for large structs for SysV.
  560. For LINUX64:
  561. - integer values in gpr3;
  562. - Structures/Unions by reference;
  563. - Single/double FP values in fpr1, long double in fpr1,fpr2.
  564. - soft-float float/doubles are treated as UINT32/UINT64 respectivley.
  565. - soft-float long doubles are returned in gpr3-gpr6. */
  566. switch (type)
  567. {
  568. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  569. case FFI_TYPE_LONGDOUBLE:
  570. if (cif->abi != FFI_LINUX && cif->abi != FFI_LINUX64
  571. && cif->abi != FFI_LINUX_SOFT_FLOAT)
  572. goto byref;
  573. flags |= FLAG_RETURNS_128BITS;
  574. /* Fall through. */
  575. #endif
  576. case FFI_TYPE_DOUBLE:
  577. flags |= FLAG_RETURNS_64BITS;
  578. /* Fall through. */
  579. case FFI_TYPE_FLOAT:
  580. /* With FFI_LINUX_SOFT_FLOAT no fp registers are used. */
  581. if (cif->abi != FFI_LINUX_SOFT_FLOAT)
  582. flags |= FLAG_RETURNS_FP;
  583. break;
  584. case FFI_TYPE_UINT64:
  585. case FFI_TYPE_SINT64:
  586. flags |= FLAG_RETURNS_64BITS;
  587. break;
  588. case FFI_TYPE_STRUCT:
  589. if (cif->abi == FFI_SYSV)
  590. {
  591. /* The final SYSV ABI says that structures smaller or equal 8 bytes
  592. are returned in r3/r4. The FFI_GCC_SYSV ABI instead returns them
  593. in memory. */
  594. /* Treat structs with size <= 8 bytes. */
  595. if (size <= 8)
  596. {
  597. flags |= FLAG_RETURNS_SMST;
  598. /* These structs are returned in r3. We pack the type and the
  599. precalculated shift value (needed in the sysv.S) into flags.
  600. The same applies for the structs returned in r3/r4. */
  601. if (size <= 4)
  602. {
  603. flags |= 1 << (31 - FFI_SYSV_TYPE_SMALL_STRUCT - 1);
  604. flags |= 8 * (4 - size) << 4;
  605. break;
  606. }
  607. /* These structs are returned in r3 and r4. See above. */
  608. if (size <= 8)
  609. {
  610. flags |= 1 << (31 - FFI_SYSV_TYPE_SMALL_STRUCT - 2);
  611. flags |= 8 * (8 - size) << 4;
  612. break;
  613. }
  614. }
  615. }
  616. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  617. byref:
  618. #endif
  619. intarg_count++;
  620. flags |= FLAG_RETVAL_REFERENCE;
  621. /* Fall through. */
  622. case FFI_TYPE_VOID:
  623. flags |= FLAG_RETURNS_NOTHING;
  624. break;
  625. default:
  626. /* Returns 32-bit integer, or similar. Nothing to do here. */
  627. break;
  628. }
  629. if (cif->abi != FFI_LINUX64)
  630. /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the
  631. first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest
  632. goes on the stack. Structures and long doubles (if not equivalent
  633. to double) are passed as a pointer to a copy of the structure.
  634. Stuff on the stack needs to keep proper alignment. */
  635. for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
  636. {
  637. switch ((*ptr)->type)
  638. {
  639. case FFI_TYPE_FLOAT:
  640. /* With FFI_LINUX_SOFT_FLOAT floats are handled like UINT32. */
  641. if (cif->abi == FFI_LINUX_SOFT_FLOAT)
  642. goto soft_float_cif;
  643. fparg_count++;
  644. /* floating singles are not 8-aligned on stack */
  645. break;
  646. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  647. case FFI_TYPE_LONGDOUBLE:
  648. if (cif->abi != FFI_LINUX && cif->abi != FFI_LINUX_SOFT_FLOAT)
  649. goto do_struct;
  650. if (cif->abi == FFI_LINUX_SOFT_FLOAT)
  651. {
  652. if (intarg_count >= NUM_GPR_ARG_REGISTERS - 3
  653. || intarg_count < NUM_GPR_ARG_REGISTERS)
  654. /* A long double in FFI_LINUX_SOFT_FLOAT can use only
  655. a set of four consecutive gprs. If we have not enough,
  656. we have to adjust the intarg_count value. */
  657. intarg_count += NUM_GPR_ARG_REGISTERS - intarg_count;
  658. intarg_count += 4;
  659. break;
  660. }
  661. else
  662. fparg_count++;
  663. /* Fall thru */
  664. #endif
  665. case FFI_TYPE_DOUBLE:
  666. /* With FFI_LINUX_SOFT_FLOAT doubles are handled like UINT64. */
  667. if (cif->abi == FFI_LINUX_SOFT_FLOAT)
  668. goto soft_double_cif;
  669. fparg_count++;
  670. /* If this FP arg is going on the stack, it must be
  671. 8-byte-aligned. */
  672. if (fparg_count > NUM_FPR_ARG_REGISTERS
  673. && intarg_count >= NUM_GPR_ARG_REGISTERS
  674. && intarg_count % 2 != 0)
  675. intarg_count++;
  676. break;
  677. case FFI_TYPE_UINT64:
  678. case FFI_TYPE_SINT64:
  679. soft_double_cif:
  680. /* 'long long' arguments are passed as two words, but
  681. either both words must fit in registers or both go
  682. on the stack. If they go on the stack, they must
  683. be 8-byte-aligned.
  684. Also, only certain register pairs can be used for
  685. passing long long int -- specifically (r3,r4), (r5,r6),
  686. (r7,r8), (r9,r10).
  687. */
  688. if (intarg_count == NUM_GPR_ARG_REGISTERS-1
  689. || intarg_count % 2 != 0)
  690. intarg_count++;
  691. intarg_count += 2;
  692. break;
  693. case FFI_TYPE_STRUCT:
  694. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  695. do_struct:
  696. #endif
  697. /* We must allocate space for a copy of these to enforce
  698. pass-by-value. Pad the space up to a multiple of 16
  699. bytes (the maximum alignment required for anything under
  700. the SYSV ABI). */
  701. struct_copy_size += ((*ptr)->size + 15) & ~0xF;
  702. /* Fall through (allocate space for the pointer). */
  703. default:
  704. soft_float_cif:
  705. /* Everything else is passed as a 4-byte word in a GPR, either
  706. the object itself or a pointer to it. */
  707. intarg_count++;
  708. break;
  709. }
  710. }
  711. else
  712. for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
  713. {
  714. switch ((*ptr)->type)
  715. {
  716. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  717. case FFI_TYPE_LONGDOUBLE:
  718. if (cif->abi == FFI_LINUX_SOFT_FLOAT)
  719. intarg_count += 4;
  720. else
  721. {
  722. fparg_count += 2;
  723. intarg_count += 2;
  724. }
  725. break;
  726. #endif
  727. case FFI_TYPE_FLOAT:
  728. case FFI_TYPE_DOUBLE:
  729. fparg_count++;
  730. intarg_count++;
  731. break;
  732. case FFI_TYPE_STRUCT:
  733. intarg_count += ((*ptr)->size + 7) / 8;
  734. break;
  735. default:
  736. /* Everything else is passed as a 8-byte word in a GPR, either
  737. the object itself or a pointer to it. */
  738. intarg_count++;
  739. break;
  740. }
  741. }
  742. if (fparg_count != 0)
  743. flags |= FLAG_FP_ARGUMENTS;
  744. if (intarg_count > 4)
  745. flags |= FLAG_4_GPR_ARGUMENTS;
  746. if (struct_copy_size != 0)
  747. flags |= FLAG_ARG_NEEDS_COPY;
  748. if (cif->abi != FFI_LINUX64)
  749. {
  750. /* Space for the FPR registers, if needed. */
  751. if (fparg_count != 0)
  752. bytes += NUM_FPR_ARG_REGISTERS * sizeof (double);
  753. /* Stack space. */
  754. if (intarg_count > NUM_GPR_ARG_REGISTERS)
  755. bytes += (intarg_count - NUM_GPR_ARG_REGISTERS) * sizeof (int);
  756. if (fparg_count > NUM_FPR_ARG_REGISTERS)
  757. bytes += (fparg_count - NUM_FPR_ARG_REGISTERS) * sizeof (double);
  758. }
  759. else
  760. {
  761. /* Space for the FPR registers, if needed. */
  762. if (fparg_count != 0)
  763. bytes += NUM_FPR_ARG_REGISTERS64 * sizeof (double);
  764. /* Stack space. */
  765. if (intarg_count > NUM_GPR_ARG_REGISTERS64)
  766. bytes += (intarg_count - NUM_GPR_ARG_REGISTERS64) * sizeof (long);
  767. }
  768. /* The stack space allocated needs to be a multiple of 16 bytes. */
  769. bytes = (bytes + 15) & ~0xF;
  770. /* Add in the space for the copied structures. */
  771. bytes += struct_copy_size;
  772. cif->flags = flags;
  773. cif->bytes = bytes;
  774. return FFI_OK;
  775. }
  776. extern void ffi_call_SYSV(extended_cif *, unsigned, unsigned, unsigned *,
  777. void (*fn)(void));
  778. extern void FFI_HIDDEN ffi_call_LINUX64(extended_cif *, unsigned long,
  779. unsigned long, unsigned long *,
  780. void (*fn)(void));
  781. void
  782. ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
  783. {
  784. extended_cif ecif;
  785. ecif.cif = cif;
  786. ecif.avalue = avalue;
  787. /* If the return value is a struct and we don't have a return */
  788. /* value address then we need to make one */
  789. if ((rvalue == NULL) && (cif->rtype->type == FFI_TYPE_STRUCT))
  790. {
  791. ecif.rvalue = alloca(cif->rtype->size);
  792. }
  793. else
  794. ecif.rvalue = rvalue;
  795. switch (cif->abi)
  796. {
  797. #ifndef POWERPC64
  798. case FFI_SYSV:
  799. case FFI_GCC_SYSV:
  800. case FFI_LINUX:
  801. case FFI_LINUX_SOFT_FLOAT:
  802. ffi_call_SYSV (&ecif, -cif->bytes, cif->flags, ecif.rvalue, fn);
  803. break;
  804. #else
  805. case FFI_LINUX64:
  806. ffi_call_LINUX64 (&ecif, -(long) cif->bytes, cif->flags, ecif.rvalue, fn);
  807. break;
  808. #endif
  809. default:
  810. FFI_ASSERT (0);
  811. break;
  812. }
  813. }
  814. #ifndef POWERPC64
  815. #define MIN_CACHE_LINE_SIZE 8
  816. static void
  817. flush_icache (char *wraddr, char *xaddr, int size)
  818. {
  819. int i;
  820. for (i = 0; i < size; i += MIN_CACHE_LINE_SIZE)
  821. __asm__ volatile ("icbi 0,%0;" "dcbf 0,%1;"
  822. : : "r" (xaddr + i), "r" (wraddr + i) : "memory");
  823. __asm__ volatile ("icbi 0,%0;" "dcbf 0,%1;" "sync;" "isync;"
  824. : : "r"(xaddr + size - 1), "r"(wraddr + size - 1)
  825. : "memory");
  826. }
  827. #endif
  828. ffi_status
  829. ffi_prep_closure_loc (ffi_closure *closure,
  830. ffi_cif *cif,
  831. void (*fun) (ffi_cif *, void *, void **, void *),
  832. void *user_data,
  833. void *codeloc)
  834. {
  835. #ifdef POWERPC64
  836. void **tramp = (void **) &closure->tramp[0];
  837. FFI_ASSERT (cif->abi == FFI_LINUX64);
  838. /* Copy function address and TOC from ffi_closure_LINUX64. */
  839. memcpy (tramp, (char *) ffi_closure_LINUX64, 16);
  840. tramp[2] = codeloc;
  841. #else
  842. unsigned int *tramp;
  843. FFI_ASSERT (cif->abi == FFI_GCC_SYSV || cif->abi == FFI_SYSV);
  844. tramp = (unsigned int *) &closure->tramp[0];
  845. tramp[0] = 0x7c0802a6; /* mflr r0 */
  846. tramp[1] = 0x4800000d; /* bl 10 <trampoline_initial+0x10> */
  847. tramp[4] = 0x7d6802a6; /* mflr r11 */
  848. tramp[5] = 0x7c0803a6; /* mtlr r0 */
  849. tramp[6] = 0x800b0000; /* lwz r0,0(r11) */
  850. tramp[7] = 0x816b0004; /* lwz r11,4(r11) */
  851. tramp[8] = 0x7c0903a6; /* mtctr r0 */
  852. tramp[9] = 0x4e800420; /* bctr */
  853. *(void **) &tramp[2] = (void *) ffi_closure_SYSV; /* function */
  854. *(void **) &tramp[3] = codeloc; /* context */
  855. /* Flush the icache. */
  856. flush_icache ((char *)tramp, (char *)codeloc, FFI_TRAMPOLINE_SIZE);
  857. #endif
  858. closure->cif = cif;
  859. closure->fun = fun;
  860. closure->user_data = user_data;
  861. return FFI_OK;
  862. }
  863. typedef union
  864. {
  865. float f;
  866. double d;
  867. } ffi_dblfl;
  868. int ffi_closure_helper_SYSV (ffi_closure *, void *, unsigned long *,
  869. ffi_dblfl *, unsigned long *);
  870. /* Basically the trampoline invokes ffi_closure_SYSV, and on
  871. * entry, r11 holds the address of the closure.
  872. * After storing the registers that could possibly contain
  873. * parameters to be passed into the stack frame and setting
  874. * up space for a return value, ffi_closure_SYSV invokes the
  875. * following helper function to do most of the work
  876. */
  877. int
  878. ffi_closure_helper_SYSV (ffi_closure *closure, void *rvalue,
  879. unsigned long *pgr, ffi_dblfl *pfr,
  880. unsigned long *pst)
  881. {
  882. /* rvalue is the pointer to space for return value in closure assembly */
  883. /* pgr is the pointer to where r3-r10 are stored in ffi_closure_SYSV */
  884. /* pfr is the pointer to where f1-f8 are stored in ffi_closure_SYSV */
  885. /* pst is the pointer to outgoing parameter stack in original caller */
  886. void ** avalue;
  887. ffi_type ** arg_types;
  888. long i, avn;
  889. long nf; /* number of floating registers already used */
  890. long ng; /* number of general registers already used */
  891. ffi_cif * cif;
  892. double temp;
  893. unsigned size;
  894. cif = closure->cif;
  895. avalue = alloca (cif->nargs * sizeof (void *));
  896. size = cif->rtype->size;
  897. nf = 0;
  898. ng = 0;
  899. /* Copy the caller's structure return value address so that the closure
  900. returns the data directly to the caller.
  901. For FFI_SYSV the result is passed in r3/r4 if the struct size is less
  902. or equal 8 bytes. */
  903. if ((cif->rtype->type == FFI_TYPE_STRUCT
  904. && !((cif->abi == FFI_SYSV) && (size <= 8)))
  905. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  906. || (cif->rtype->type == FFI_TYPE_LONGDOUBLE
  907. && cif->abi != FFI_LINUX && cif->abi != FFI_LINUX_SOFT_FLOAT)
  908. #endif
  909. )
  910. {
  911. rvalue = (void *) *pgr;
  912. ng++;
  913. pgr++;
  914. }
  915. i = 0;
  916. avn = cif->nargs;
  917. arg_types = cif->arg_types;
  918. /* Grab the addresses of the arguments from the stack frame. */
  919. while (i < avn)
  920. {
  921. switch (arg_types[i]->type)
  922. {
  923. case FFI_TYPE_SINT8:
  924. case FFI_TYPE_UINT8:
  925. /* there are 8 gpr registers used to pass values */
  926. if (ng < 8)
  927. {
  928. avalue[i] = (char *) pgr + 3;
  929. ng++;
  930. pgr++;
  931. }
  932. else
  933. {
  934. avalue[i] = (char *) pst + 3;
  935. pst++;
  936. }
  937. break;
  938. case FFI_TYPE_SINT16:
  939. case FFI_TYPE_UINT16:
  940. /* there are 8 gpr registers used to pass values */
  941. if (ng < 8)
  942. {
  943. avalue[i] = (char *) pgr + 2;
  944. ng++;
  945. pgr++;
  946. }
  947. else
  948. {
  949. avalue[i] = (char *) pst + 2;
  950. pst++;
  951. }
  952. break;
  953. case FFI_TYPE_SINT32:
  954. case FFI_TYPE_UINT32:
  955. case FFI_TYPE_POINTER:
  956. soft_float_closure:
  957. /* there are 8 gpr registers used to pass values */
  958. if (ng < 8)
  959. {
  960. avalue[i] = pgr;
  961. ng++;
  962. pgr++;
  963. }
  964. else
  965. {
  966. avalue[i] = pst;
  967. pst++;
  968. }
  969. break;
  970. case FFI_TYPE_STRUCT:
  971. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  972. do_struct:
  973. #endif
  974. /* Structs are passed by reference. The address will appear in a
  975. gpr if it is one of the first 8 arguments. */
  976. if (ng < 8)
  977. {
  978. avalue[i] = (void *) *pgr;
  979. ng++;
  980. pgr++;
  981. }
  982. else
  983. {
  984. avalue[i] = (void *) *pst;
  985. pst++;
  986. }
  987. break;
  988. case FFI_TYPE_SINT64:
  989. case FFI_TYPE_UINT64:
  990. soft_double_closure:
  991. /* passing long long ints are complex, they must
  992. * be passed in suitable register pairs such as
  993. * (r3,r4) or (r5,r6) or (r6,r7), or (r7,r8) or (r9,r10)
  994. * and if the entire pair aren't available then the outgoing
  995. * parameter stack is used for both but an alignment of 8
  996. * must will be kept. So we must either look in pgr
  997. * or pst to find the correct address for this type
  998. * of parameter.
  999. */
  1000. if (ng < 7)
  1001. {
  1002. if (ng & 0x01)
  1003. {
  1004. /* skip r4, r6, r8 as starting points */
  1005. ng++;
  1006. pgr++;
  1007. }
  1008. avalue[i] = pgr;
  1009. ng += 2;
  1010. pgr += 2;
  1011. }
  1012. else
  1013. {
  1014. if (((long) pst) & 4)
  1015. pst++;
  1016. avalue[i] = pst;
  1017. pst += 2;
  1018. }
  1019. break;
  1020. case FFI_TYPE_FLOAT:
  1021. /* With FFI_LINUX_SOFT_FLOAT floats are handled like UINT32. */
  1022. if (cif->abi == FFI_LINUX_SOFT_FLOAT)
  1023. goto soft_float_closure;
  1024. /* unfortunately float values are stored as doubles
  1025. * in the ffi_closure_SYSV code (since we don't check
  1026. * the type in that routine).
  1027. */
  1028. /* there are 8 64bit floating point registers */
  1029. if (nf < 8)
  1030. {
  1031. temp = pfr->d;
  1032. pfr->f = (float) temp;
  1033. avalue[i] = pfr;
  1034. nf++;
  1035. pfr++;
  1036. }
  1037. else
  1038. {
  1039. /* FIXME? here we are really changing the values
  1040. * stored in the original calling routines outgoing
  1041. * parameter stack. This is probably a really
  1042. * naughty thing to do but...
  1043. */
  1044. avalue[i] = pst;
  1045. pst += 1;
  1046. }
  1047. break;
  1048. case FFI_TYPE_DOUBLE:
  1049. /* With FFI_LINUX_SOFT_FLOAT doubles are handled like UINT64. */
  1050. if (cif->abi == FFI_LINUX_SOFT_FLOAT)
  1051. goto soft_double_closure;
  1052. /* On the outgoing stack all values are aligned to 8 */
  1053. /* there are 8 64bit floating point registers */
  1054. if (nf < 8)
  1055. {
  1056. avalue[i] = pfr;
  1057. nf++;
  1058. pfr++;
  1059. }
  1060. else
  1061. {
  1062. if (((long) pst) & 4)
  1063. pst++;
  1064. avalue[i] = pst;
  1065. pst += 2;
  1066. }
  1067. break;
  1068. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  1069. case FFI_TYPE_LONGDOUBLE:
  1070. if (cif->abi != FFI_LINUX && cif->abi != FFI_LINUX_SOFT_FLOAT)
  1071. goto do_struct;
  1072. if (cif->abi == FFI_LINUX_SOFT_FLOAT)
  1073. { /* Test if for the whole long double, 4 gprs are available.
  1074. otherwise the stuff ends up on the stack. */
  1075. if (ng < 5)
  1076. {
  1077. avalue[i] = pgr;
  1078. pgr += 4;
  1079. ng += 4;
  1080. }
  1081. else
  1082. {
  1083. avalue[i] = pst;
  1084. pst += 4;
  1085. }
  1086. break;
  1087. }
  1088. if (nf < 7)
  1089. {
  1090. avalue[i] = pfr;
  1091. pfr += 2;
  1092. nf += 2;
  1093. }
  1094. else
  1095. {
  1096. if (((long) pst) & 4)
  1097. pst++;
  1098. avalue[i] = pst;
  1099. pst += 4;
  1100. nf = 8;
  1101. }
  1102. break;
  1103. #endif
  1104. default:
  1105. FFI_ASSERT (0);
  1106. }
  1107. i++;
  1108. }
  1109. (closure->fun) (cif, rvalue, avalue, closure->user_data);
  1110. /* Tell ffi_closure_SYSV how to perform return type promotions.
  1111. Because the FFI_SYSV ABI returns the structures <= 8 bytes in r3/r4
  1112. we have to tell ffi_closure_SYSV how to treat them. */
  1113. if (cif->abi == FFI_SYSV && cif->rtype->type == FFI_TYPE_STRUCT
  1114. && size <= 8)
  1115. return FFI_SYSV_TYPE_SMALL_STRUCT + size;
  1116. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  1117. else if (cif->rtype->type == FFI_TYPE_LONGDOUBLE
  1118. && cif->abi != FFI_LINUX && cif->abi != FFI_LINUX_SOFT_FLOAT)
  1119. return FFI_TYPE_STRUCT;
  1120. #endif
  1121. /* With FFI_LINUX_SOFT_FLOAT floats and doubles are handled like UINT32
  1122. respectivley UINT64. */
  1123. if (cif->abi == FFI_LINUX_SOFT_FLOAT)
  1124. {
  1125. switch (cif->rtype->type)
  1126. {
  1127. case FFI_TYPE_FLOAT:
  1128. return FFI_TYPE_UINT32;
  1129. break;
  1130. case FFI_TYPE_DOUBLE:
  1131. return FFI_TYPE_UINT64;
  1132. break;
  1133. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  1134. case FFI_TYPE_LONGDOUBLE:
  1135. return FFI_TYPE_UINT128;
  1136. break;
  1137. #endif
  1138. default:
  1139. return cif->rtype->type;
  1140. }
  1141. }
  1142. else
  1143. {
  1144. return cif->rtype->type;
  1145. }
  1146. }
  1147. int FFI_HIDDEN ffi_closure_helper_LINUX64 (ffi_closure *, void *,
  1148. unsigned long *, ffi_dblfl *);
  1149. int FFI_HIDDEN
  1150. ffi_closure_helper_LINUX64 (ffi_closure *closure, void *rvalue,
  1151. unsigned long *pst, ffi_dblfl *pfr)
  1152. {
  1153. /* rvalue is the pointer to space for return value in closure assembly */
  1154. /* pst is the pointer to parameter save area
  1155. (r3-r10 are stored into its first 8 slots by ffi_closure_LINUX64) */
  1156. /* pfr is the pointer to where f1-f13 are stored in ffi_closure_LINUX64 */
  1157. void **avalue;
  1158. ffi_type **arg_types;
  1159. long i, avn;
  1160. ffi_cif *cif;
  1161. ffi_dblfl *end_pfr = pfr + NUM_FPR_ARG_REGISTERS64;
  1162. cif = closure->cif;
  1163. avalue = alloca (cif->nargs * sizeof (void *));
  1164. /* Copy the caller's structure return value address so that the closure
  1165. returns the data directly to the caller. */
  1166. if (cif->rtype->type == FFI_TYPE_STRUCT)
  1167. {
  1168. rvalue = (void *) *pst;
  1169. pst++;
  1170. }
  1171. i = 0;
  1172. avn = cif->nargs;
  1173. arg_types = cif->arg_types;
  1174. /* Grab the addresses of the arguments from the stack frame. */
  1175. while (i < avn)
  1176. {
  1177. switch (arg_types[i]->type)
  1178. {
  1179. case FFI_TYPE_SINT8:
  1180. case FFI_TYPE_UINT8:
  1181. avalue[i] = (char *) pst + 7;
  1182. pst++;
  1183. break;
  1184. case FFI_TYPE_SINT16:
  1185. case FFI_TYPE_UINT16:
  1186. avalue[i] = (char *) pst + 6;
  1187. pst++;
  1188. break;
  1189. case FFI_TYPE_SINT32:
  1190. case FFI_TYPE_UINT32:
  1191. avalue[i] = (char *) pst + 4;
  1192. pst++;
  1193. break;
  1194. case FFI_TYPE_SINT64:
  1195. case FFI_TYPE_UINT64:
  1196. case FFI_TYPE_POINTER:
  1197. avalue[i] = pst;
  1198. pst++;
  1199. break;
  1200. case FFI_TYPE_STRUCT:
  1201. /* Structures with size less than eight bytes are passed
  1202. left-padded. */
  1203. if (arg_types[i]->size < 8)
  1204. avalue[i] = (char *) pst + 8 - arg_types[i]->size;
  1205. else
  1206. avalue[i] = pst;
  1207. pst += (arg_types[i]->size + 7) / 8;
  1208. break;
  1209. case FFI_TYPE_FLOAT:
  1210. /* unfortunately float values are stored as doubles
  1211. * in the ffi_closure_LINUX64 code (since we don't check
  1212. * the type in that routine).
  1213. */
  1214. /* there are 13 64bit floating point registers */
  1215. if (pfr < end_pfr)
  1216. {
  1217. double temp = pfr->d;
  1218. pfr->f = (float) temp;
  1219. avalue[i] = pfr;
  1220. pfr++;
  1221. }
  1222. else
  1223. avalue[i] = pst;
  1224. pst++;
  1225. break;
  1226. case FFI_TYPE_DOUBLE:
  1227. /* On the outgoing stack all values are aligned to 8 */
  1228. /* there are 13 64bit floating point registers */
  1229. if (pfr < end_pfr)
  1230. {
  1231. avalue[i] = pfr;
  1232. pfr++;
  1233. }
  1234. else
  1235. avalue[i] = pst;
  1236. pst++;
  1237. break;
  1238. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  1239. case FFI_TYPE_LONGDOUBLE:
  1240. if (pfr + 1 < end_pfr)
  1241. {
  1242. avalue[i] = pfr;
  1243. pfr += 2;
  1244. }
  1245. else
  1246. {
  1247. if (pfr < end_pfr)
  1248. {
  1249. /* Passed partly in f13 and partly on the stack.
  1250. Move it all to the stack. */
  1251. *pst = *(unsigned long *) pfr;
  1252. pfr++;
  1253. }
  1254. avalue[i] = pst;
  1255. }
  1256. pst += 2;
  1257. break;
  1258. #endif
  1259. default:
  1260. FFI_ASSERT (0);
  1261. }
  1262. i++;
  1263. }
  1264. (closure->fun) (cif, rvalue, avalue, closure->user_data);
  1265. /* Tell ffi_closure_LINUX64 how to perform return type promotions. */
  1266. return cif->rtype->type;
  1267. }