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

http://unladen-swallow.googlecode.com/ · C · 926 lines · 661 code · 126 blank · 139 comment · 117 complexity · c4b338c1c56fbe690ffd27d93d206de9 MD5 · raw file

  1. /* -----------------------------------------------------------------------
  2. ffi.c - Copyright (c) 1996, 2007, 2008 Red Hat, Inc.
  3. Copyright (c) 2008 David Daney
  4. MIPS Foreign Function Interface
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files (the
  7. ``Software''), to deal in the Software without restriction, including
  8. without limitation the rights to use, copy, modify, merge, publish,
  9. distribute, sublicense, and/or sell copies of the Software, and to
  10. permit persons to whom the Software is furnished to do so, subject to
  11. the following conditions:
  12. The above copyright notice and this permission notice shall be included
  13. in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. DEALINGS IN THE SOFTWARE.
  22. ----------------------------------------------------------------------- */
  23. #include <ffi.h>
  24. #include <ffi_common.h>
  25. #include <stdlib.h>
  26. #ifdef __GNUC__
  27. # if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3))
  28. # define USE__BUILTIN___CLEAR_CACHE 1
  29. # endif
  30. #endif
  31. #ifndef USE__BUILTIN___CLEAR_CACHE
  32. #include <sys/cachectl.h>
  33. #endif
  34. #ifdef FFI_DEBUG
  35. # define FFI_MIPS_STOP_HERE() ffi_stop_here()
  36. #else
  37. # define FFI_MIPS_STOP_HERE() do {} while(0)
  38. #endif
  39. #ifdef FFI_MIPS_N32
  40. #define FIX_ARGP \
  41. FFI_ASSERT(argp <= &stack[bytes]); \
  42. if (argp == &stack[bytes]) \
  43. { \
  44. argp = stack; \
  45. FFI_MIPS_STOP_HERE(); \
  46. }
  47. #else
  48. #define FIX_ARGP
  49. #endif
  50. /* ffi_prep_args is called by the assembly routine once stack space
  51. has been allocated for the function's arguments */
  52. static void ffi_prep_args(char *stack,
  53. extended_cif *ecif,
  54. int bytes,
  55. int flags)
  56. {
  57. int i;
  58. void **p_argv;
  59. char *argp;
  60. ffi_type **p_arg;
  61. #ifdef FFI_MIPS_N32
  62. /* If more than 8 double words are used, the remainder go
  63. on the stack. We reorder stuff on the stack here to
  64. support this easily. */
  65. if (bytes > 8 * sizeof(ffi_arg))
  66. argp = &stack[bytes - (8 * sizeof(ffi_arg))];
  67. else
  68. argp = stack;
  69. #else
  70. argp = stack;
  71. #endif
  72. memset(stack, 0, bytes);
  73. #ifdef FFI_MIPS_N32
  74. if ( ecif->cif->rstruct_flag != 0 )
  75. #else
  76. if ( ecif->cif->rtype->type == FFI_TYPE_STRUCT )
  77. #endif
  78. {
  79. *(ffi_arg *) argp = (ffi_arg) ecif->rvalue;
  80. argp += sizeof(ffi_arg);
  81. FIX_ARGP;
  82. }
  83. p_argv = ecif->avalue;
  84. for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types; i; i--, p_arg++)
  85. {
  86. size_t z;
  87. unsigned int a;
  88. /* Align if necessary. */
  89. a = (*p_arg)->alignment;
  90. if (a < sizeof(ffi_arg))
  91. a = sizeof(ffi_arg);
  92. if ((a - 1) & (unsigned long) argp)
  93. {
  94. argp = (char *) ALIGN(argp, a);
  95. FIX_ARGP;
  96. }
  97. z = (*p_arg)->size;
  98. if (z <= sizeof(ffi_arg))
  99. {
  100. int type = (*p_arg)->type;
  101. z = sizeof(ffi_arg);
  102. /* The size of a pointer depends on the ABI */
  103. if (type == FFI_TYPE_POINTER)
  104. type =
  105. (ecif->cif->abi == FFI_N64) ? FFI_TYPE_SINT64 : FFI_TYPE_SINT32;
  106. switch (type)
  107. {
  108. case FFI_TYPE_SINT8:
  109. *(ffi_arg *)argp = *(SINT8 *)(* p_argv);
  110. break;
  111. case FFI_TYPE_UINT8:
  112. *(ffi_arg *)argp = *(UINT8 *)(* p_argv);
  113. break;
  114. case FFI_TYPE_SINT16:
  115. *(ffi_arg *)argp = *(SINT16 *)(* p_argv);
  116. break;
  117. case FFI_TYPE_UINT16:
  118. *(ffi_arg *)argp = *(UINT16 *)(* p_argv);
  119. break;
  120. case FFI_TYPE_SINT32:
  121. *(ffi_arg *)argp = *(SINT32 *)(* p_argv);
  122. break;
  123. case FFI_TYPE_UINT32:
  124. *(ffi_arg *)argp = *(UINT32 *)(* p_argv);
  125. break;
  126. /* This can only happen with 64bit slots. */
  127. case FFI_TYPE_FLOAT:
  128. *(float *) argp = *(float *)(* p_argv);
  129. break;
  130. /* Handle structures. */
  131. default:
  132. memcpy(argp, *p_argv, (*p_arg)->size);
  133. break;
  134. }
  135. }
  136. else
  137. {
  138. #ifdef FFI_MIPS_O32
  139. memcpy(argp, *p_argv, z);
  140. #else
  141. {
  142. unsigned long end = (unsigned long) argp + z;
  143. unsigned long cap = (unsigned long) stack + bytes;
  144. /* Check if the data will fit within the register space.
  145. Handle it if it doesn't. */
  146. if (end <= cap)
  147. memcpy(argp, *p_argv, z);
  148. else
  149. {
  150. unsigned long portion = cap - (unsigned long)argp;
  151. memcpy(argp, *p_argv, portion);
  152. argp = stack;
  153. z -= portion;
  154. memcpy(argp, (void*)((unsigned long)(*p_argv) + portion),
  155. z);
  156. }
  157. }
  158. #endif
  159. }
  160. p_argv++;
  161. argp += z;
  162. FIX_ARGP;
  163. }
  164. }
  165. #ifdef FFI_MIPS_N32
  166. /* The n32 spec says that if "a chunk consists solely of a double
  167. float field (but not a double, which is part of a union), it
  168. is passed in a floating point register. Any other chunk is
  169. passed in an integer register". This code traverses structure
  170. definitions and generates the appropriate flags. */
  171. static unsigned
  172. calc_n32_struct_flags(ffi_type *arg, unsigned *loc, unsigned *arg_reg)
  173. {
  174. unsigned flags = 0;
  175. unsigned index = 0;
  176. ffi_type *e;
  177. while ((e = arg->elements[index]))
  178. {
  179. /* Align this object. */
  180. *loc = ALIGN(*loc, e->alignment);
  181. if (e->type == FFI_TYPE_DOUBLE)
  182. {
  183. /* Already aligned to FFI_SIZEOF_ARG. */
  184. *arg_reg = *loc / FFI_SIZEOF_ARG;
  185. if (*arg_reg > 7)
  186. break;
  187. flags += (FFI_TYPE_DOUBLE << (*arg_reg * FFI_FLAG_BITS));
  188. *loc += e->size;
  189. }
  190. else
  191. *loc += e->size;
  192. index++;
  193. }
  194. /* Next Argument register at alignment of FFI_SIZEOF_ARG. */
  195. *arg_reg = ALIGN(*loc, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
  196. return flags;
  197. }
  198. static unsigned
  199. calc_n32_return_struct_flags(ffi_type *arg)
  200. {
  201. unsigned flags = 0;
  202. unsigned small = FFI_TYPE_SMALLSTRUCT;
  203. ffi_type *e;
  204. /* Returning structures under n32 is a tricky thing.
  205. A struct with only one or two floating point fields
  206. is returned in $f0 (and $f2 if necessary). Any other
  207. struct results at most 128 bits are returned in $2
  208. (the first 64 bits) and $3 (remainder, if necessary).
  209. Larger structs are handled normally. */
  210. if (arg->size > 16)
  211. return 0;
  212. if (arg->size > 8)
  213. small = FFI_TYPE_SMALLSTRUCT2;
  214. e = arg->elements[0];
  215. if (e->type == FFI_TYPE_DOUBLE)
  216. flags = FFI_TYPE_DOUBLE;
  217. else if (e->type == FFI_TYPE_FLOAT)
  218. flags = FFI_TYPE_FLOAT;
  219. if (flags && (e = arg->elements[1]))
  220. {
  221. if (e->type == FFI_TYPE_DOUBLE)
  222. flags += FFI_TYPE_DOUBLE << FFI_FLAG_BITS;
  223. else if (e->type == FFI_TYPE_FLOAT)
  224. flags += FFI_TYPE_FLOAT << FFI_FLAG_BITS;
  225. else
  226. return small;
  227. if (flags && (arg->elements[2]))
  228. {
  229. /* There are three arguments and the first two are
  230. floats! This must be passed the old way. */
  231. return small;
  232. }
  233. }
  234. else
  235. if (!flags)
  236. return small;
  237. return flags;
  238. }
  239. #endif
  240. /* Perform machine dependent cif processing */
  241. ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
  242. {
  243. cif->flags = 0;
  244. #ifdef FFI_MIPS_O32
  245. /* Set the flags necessary for O32 processing. FFI_O32_SOFT_FLOAT
  246. * does not have special handling for floating point args.
  247. */
  248. if (cif->rtype->type != FFI_TYPE_STRUCT && cif->abi == FFI_O32)
  249. {
  250. if (cif->nargs > 0)
  251. {
  252. switch ((cif->arg_types)[0]->type)
  253. {
  254. case FFI_TYPE_FLOAT:
  255. case FFI_TYPE_DOUBLE:
  256. cif->flags += (cif->arg_types)[0]->type;
  257. break;
  258. default:
  259. break;
  260. }
  261. if (cif->nargs > 1)
  262. {
  263. /* Only handle the second argument if the first
  264. is a float or double. */
  265. if (cif->flags)
  266. {
  267. switch ((cif->arg_types)[1]->type)
  268. {
  269. case FFI_TYPE_FLOAT:
  270. case FFI_TYPE_DOUBLE:
  271. cif->flags += (cif->arg_types)[1]->type << FFI_FLAG_BITS;
  272. break;
  273. default:
  274. break;
  275. }
  276. }
  277. }
  278. }
  279. }
  280. /* Set the return type flag */
  281. if (cif->abi == FFI_O32_SOFT_FLOAT)
  282. {
  283. switch (cif->rtype->type)
  284. {
  285. case FFI_TYPE_VOID:
  286. case FFI_TYPE_STRUCT:
  287. cif->flags += cif->rtype->type << (FFI_FLAG_BITS * 2);
  288. break;
  289. case FFI_TYPE_SINT64:
  290. case FFI_TYPE_UINT64:
  291. case FFI_TYPE_DOUBLE:
  292. cif->flags += FFI_TYPE_UINT64 << (FFI_FLAG_BITS * 2);
  293. break;
  294. case FFI_TYPE_FLOAT:
  295. default:
  296. cif->flags += FFI_TYPE_INT << (FFI_FLAG_BITS * 2);
  297. break;
  298. }
  299. }
  300. else
  301. {
  302. /* FFI_O32 */
  303. switch (cif->rtype->type)
  304. {
  305. case FFI_TYPE_VOID:
  306. case FFI_TYPE_STRUCT:
  307. case FFI_TYPE_FLOAT:
  308. case FFI_TYPE_DOUBLE:
  309. cif->flags += cif->rtype->type << (FFI_FLAG_BITS * 2);
  310. break;
  311. case FFI_TYPE_SINT64:
  312. case FFI_TYPE_UINT64:
  313. cif->flags += FFI_TYPE_UINT64 << (FFI_FLAG_BITS * 2);
  314. break;
  315. default:
  316. cif->flags += FFI_TYPE_INT << (FFI_FLAG_BITS * 2);
  317. break;
  318. }
  319. }
  320. #endif
  321. #ifdef FFI_MIPS_N32
  322. /* Set the flags necessary for N32 processing */
  323. {
  324. unsigned arg_reg = 0;
  325. unsigned loc = 0;
  326. unsigned count = (cif->nargs < 8) ? cif->nargs : 8;
  327. unsigned index = 0;
  328. unsigned struct_flags = 0;
  329. if (cif->rtype->type == FFI_TYPE_STRUCT)
  330. {
  331. struct_flags = calc_n32_return_struct_flags(cif->rtype);
  332. if (struct_flags == 0)
  333. {
  334. /* This means that the structure is being passed as
  335. a hidden argument */
  336. arg_reg = 1;
  337. count = (cif->nargs < 7) ? cif->nargs : 7;
  338. cif->rstruct_flag = !0;
  339. }
  340. else
  341. cif->rstruct_flag = 0;
  342. }
  343. else
  344. cif->rstruct_flag = 0;
  345. while (count-- > 0 && arg_reg < 8)
  346. {
  347. switch ((cif->arg_types)[index]->type)
  348. {
  349. case FFI_TYPE_FLOAT:
  350. case FFI_TYPE_DOUBLE:
  351. cif->flags +=
  352. ((cif->arg_types)[index]->type << (arg_reg * FFI_FLAG_BITS));
  353. arg_reg++;
  354. break;
  355. case FFI_TYPE_LONGDOUBLE:
  356. /* Align it. */
  357. arg_reg = ALIGN(arg_reg, 2);
  358. /* Treat it as two adjacent doubles. */
  359. cif->flags +=
  360. (FFI_TYPE_DOUBLE << (arg_reg * FFI_FLAG_BITS));
  361. arg_reg++;
  362. cif->flags +=
  363. (FFI_TYPE_DOUBLE << (arg_reg * FFI_FLAG_BITS));
  364. arg_reg++;
  365. break;
  366. case FFI_TYPE_STRUCT:
  367. loc = arg_reg * FFI_SIZEOF_ARG;
  368. cif->flags += calc_n32_struct_flags((cif->arg_types)[index],
  369. &loc, &arg_reg);
  370. break;
  371. default:
  372. arg_reg++;
  373. break;
  374. }
  375. index++;
  376. }
  377. /* Set the return type flag */
  378. switch (cif->rtype->type)
  379. {
  380. case FFI_TYPE_STRUCT:
  381. {
  382. if (struct_flags == 0)
  383. {
  384. /* The structure is returned through a hidden
  385. first argument. Do nothing, 'cause FFI_TYPE_VOID
  386. is 0 */
  387. }
  388. else
  389. {
  390. /* The structure is returned via some tricky
  391. mechanism */
  392. cif->flags += FFI_TYPE_STRUCT << (FFI_FLAG_BITS * 8);
  393. cif->flags += struct_flags << (4 + (FFI_FLAG_BITS * 8));
  394. }
  395. break;
  396. }
  397. case FFI_TYPE_VOID:
  398. /* Do nothing, 'cause FFI_TYPE_VOID is 0 */
  399. break;
  400. case FFI_TYPE_FLOAT:
  401. case FFI_TYPE_DOUBLE:
  402. cif->flags += cif->rtype->type << (FFI_FLAG_BITS * 8);
  403. break;
  404. case FFI_TYPE_LONGDOUBLE:
  405. /* Long double is returned as if it were a struct containing
  406. two doubles. */
  407. cif->flags += FFI_TYPE_STRUCT << (FFI_FLAG_BITS * 8);
  408. cif->flags += (FFI_TYPE_DOUBLE + (FFI_TYPE_DOUBLE << FFI_FLAG_BITS))
  409. << (4 + (FFI_FLAG_BITS * 8));
  410. break;
  411. default:
  412. cif->flags += FFI_TYPE_INT << (FFI_FLAG_BITS * 8);
  413. break;
  414. }
  415. }
  416. #endif
  417. return FFI_OK;
  418. }
  419. /* Low level routine for calling O32 functions */
  420. extern int ffi_call_O32(void (*)(char *, extended_cif *, int, int),
  421. extended_cif *, unsigned,
  422. unsigned, unsigned *, void (*)(void));
  423. /* Low level routine for calling N32 functions */
  424. extern int ffi_call_N32(void (*)(char *, extended_cif *, int, int),
  425. extended_cif *, unsigned,
  426. unsigned, unsigned *, void (*)(void));
  427. void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
  428. {
  429. extended_cif ecif;
  430. ecif.cif = cif;
  431. ecif.avalue = avalue;
  432. /* If the return value is a struct and we don't have a return */
  433. /* value address then we need to make one */
  434. if ((rvalue == NULL) &&
  435. (cif->rtype->type == FFI_TYPE_STRUCT))
  436. ecif.rvalue = alloca(cif->rtype->size);
  437. else
  438. ecif.rvalue = rvalue;
  439. switch (cif->abi)
  440. {
  441. #ifdef FFI_MIPS_O32
  442. case FFI_O32:
  443. case FFI_O32_SOFT_FLOAT:
  444. ffi_call_O32(ffi_prep_args, &ecif, cif->bytes,
  445. cif->flags, ecif.rvalue, fn);
  446. break;
  447. #endif
  448. #ifdef FFI_MIPS_N32
  449. case FFI_N32:
  450. case FFI_N64:
  451. {
  452. int copy_rvalue = 0;
  453. void *rvalue_copy = ecif.rvalue;
  454. if (cif->rtype->type == FFI_TYPE_STRUCT && cif->rtype->size < 16)
  455. {
  456. /* For structures smaller than 16 bytes we clobber memory
  457. in 8 byte increments. Make a copy so we don't clobber
  458. the callers memory outside of the struct bounds. */
  459. rvalue_copy = alloca(16);
  460. copy_rvalue = 1;
  461. }
  462. ffi_call_N32(ffi_prep_args, &ecif, cif->bytes,
  463. cif->flags, rvalue_copy, fn);
  464. if (copy_rvalue)
  465. memcpy(ecif.rvalue, rvalue_copy, cif->rtype->size);
  466. }
  467. break;
  468. #endif
  469. default:
  470. FFI_ASSERT(0);
  471. break;
  472. }
  473. }
  474. #if FFI_CLOSURES
  475. #if defined(FFI_MIPS_O32)
  476. extern void ffi_closure_O32(void);
  477. #else
  478. extern void ffi_closure_N32(void);
  479. #endif /* FFI_MIPS_O32 */
  480. ffi_status
  481. ffi_prep_closure_loc (ffi_closure *closure,
  482. ffi_cif *cif,
  483. void (*fun)(ffi_cif*,void*,void**,void*),
  484. void *user_data,
  485. void *codeloc)
  486. {
  487. unsigned int *tramp = (unsigned int *) &closure->tramp[0];
  488. void * fn;
  489. char *clear_location = (char *) codeloc;
  490. #if defined(FFI_MIPS_O32)
  491. FFI_ASSERT(cif->abi == FFI_O32 || cif->abi == FFI_O32_SOFT_FLOAT);
  492. fn = ffi_closure_O32;
  493. #else /* FFI_MIPS_N32 */
  494. FFI_ASSERT(cif->abi == FFI_N32 || cif->abi == FFI_N64);
  495. fn = ffi_closure_N32;
  496. #endif /* FFI_MIPS_O32 */
  497. #if defined(FFI_MIPS_O32) || (_MIPS_SIM ==_ABIN32)
  498. /* lui $25,high(fn) */
  499. tramp[0] = 0x3c190000 | ((unsigned)fn >> 16);
  500. /* ori $25,low(fn) */
  501. tramp[1] = 0x37390000 | ((unsigned)fn & 0xffff);
  502. /* lui $12,high(codeloc) */
  503. tramp[2] = 0x3c0c0000 | ((unsigned)codeloc >> 16);
  504. /* jr $25 */
  505. tramp[3] = 0x03200008;
  506. /* ori $12,low(codeloc) */
  507. tramp[4] = 0x358c0000 | ((unsigned)codeloc & 0xffff);
  508. #else
  509. /* N64 has a somewhat larger trampoline. */
  510. /* lui $25,high(fn) */
  511. tramp[0] = 0x3c190000 | ((unsigned long)fn >> 48);
  512. /* lui $12,high(codeloc) */
  513. tramp[1] = 0x3c0c0000 | ((unsigned long)codeloc >> 48);
  514. /* ori $25,mid-high(fn) */
  515. tramp[2] = 0x37390000 | (((unsigned long)fn >> 32 ) & 0xffff);
  516. /* ori $12,mid-high(codeloc) */
  517. tramp[3] = 0x358c0000 | (((unsigned long)codeloc >> 32) & 0xffff);
  518. /* dsll $25,$25,16 */
  519. tramp[4] = 0x0019cc38;
  520. /* dsll $12,$12,16 */
  521. tramp[5] = 0x000c6438;
  522. /* ori $25,mid-low(fn) */
  523. tramp[6] = 0x37390000 | (((unsigned long)fn >> 16 ) & 0xffff);
  524. /* ori $12,mid-low(codeloc) */
  525. tramp[7] = 0x358c0000 | (((unsigned long)codeloc >> 16) & 0xffff);
  526. /* dsll $25,$25,16 */
  527. tramp[8] = 0x0019cc38;
  528. /* dsll $12,$12,16 */
  529. tramp[9] = 0x000c6438;
  530. /* ori $25,low(fn) */
  531. tramp[10] = 0x37390000 | ((unsigned long)fn & 0xffff);
  532. /* jr $25 */
  533. tramp[11] = 0x03200008;
  534. /* ori $12,low(codeloc) */
  535. tramp[12] = 0x358c0000 | ((unsigned long)codeloc & 0xffff);
  536. #endif
  537. closure->cif = cif;
  538. closure->fun = fun;
  539. closure->user_data = user_data;
  540. #ifdef USE__BUILTIN___CLEAR_CACHE
  541. __builtin___clear_cache(clear_location, clear_location + FFI_TRAMPOLINE_SIZE);
  542. #else
  543. cacheflush (clear_location, FFI_TRAMPOLINE_SIZE, ICACHE);
  544. #endif
  545. return FFI_OK;
  546. }
  547. /*
  548. * Decodes the arguments to a function, which will be stored on the
  549. * stack. AR is the pointer to the beginning of the integer arguments
  550. * (and, depending upon the arguments, some floating-point arguments
  551. * as well). FPR is a pointer to the area where floating point
  552. * registers have been saved, if any.
  553. *
  554. * RVALUE is the location where the function return value will be
  555. * stored. CLOSURE is the prepared closure to invoke.
  556. *
  557. * This function should only be called from assembly, which is in
  558. * turn called from a trampoline.
  559. *
  560. * Returns the function return type.
  561. *
  562. * Based on the similar routine for sparc.
  563. */
  564. int
  565. ffi_closure_mips_inner_O32 (ffi_closure *closure,
  566. void *rvalue, ffi_arg *ar,
  567. double *fpr)
  568. {
  569. ffi_cif *cif;
  570. void **avaluep;
  571. ffi_arg *avalue;
  572. ffi_type **arg_types;
  573. int i, avn, argn, seen_int;
  574. cif = closure->cif;
  575. avalue = alloca (cif->nargs * sizeof (ffi_arg));
  576. avaluep = alloca (cif->nargs * sizeof (ffi_arg));
  577. seen_int = (cif->abi == FFI_O32_SOFT_FLOAT);
  578. argn = 0;
  579. if ((cif->flags >> (FFI_FLAG_BITS * 2)) == FFI_TYPE_STRUCT)
  580. {
  581. rvalue = (void *)(UINT32)ar[0];
  582. argn = 1;
  583. }
  584. i = 0;
  585. avn = cif->nargs;
  586. arg_types = cif->arg_types;
  587. while (i < avn)
  588. {
  589. if (i < 2 && !seen_int &&
  590. (arg_types[i]->type == FFI_TYPE_FLOAT ||
  591. arg_types[i]->type == FFI_TYPE_DOUBLE))
  592. {
  593. #ifdef __MIPSEB__
  594. if (arg_types[i]->type == FFI_TYPE_FLOAT)
  595. avaluep[i] = ((char *) &fpr[i]) + sizeof (float);
  596. else
  597. #endif
  598. avaluep[i] = (char *) &fpr[i];
  599. }
  600. else
  601. {
  602. if (arg_types[i]->alignment == 8 && (argn & 0x1))
  603. argn++;
  604. switch (arg_types[i]->type)
  605. {
  606. case FFI_TYPE_SINT8:
  607. avaluep[i] = &avalue[i];
  608. *(SINT8 *) &avalue[i] = (SINT8) ar[argn];
  609. break;
  610. case FFI_TYPE_UINT8:
  611. avaluep[i] = &avalue[i];
  612. *(UINT8 *) &avalue[i] = (UINT8) ar[argn];
  613. break;
  614. case FFI_TYPE_SINT16:
  615. avaluep[i] = &avalue[i];
  616. *(SINT16 *) &avalue[i] = (SINT16) ar[argn];
  617. break;
  618. case FFI_TYPE_UINT16:
  619. avaluep[i] = &avalue[i];
  620. *(UINT16 *) &avalue[i] = (UINT16) ar[argn];
  621. break;
  622. default:
  623. avaluep[i] = (char *) &ar[argn];
  624. break;
  625. }
  626. seen_int = 1;
  627. }
  628. argn += ALIGN(arg_types[i]->size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
  629. i++;
  630. }
  631. /* Invoke the closure. */
  632. (closure->fun) (cif, rvalue, avaluep, closure->user_data);
  633. if (cif->abi == FFI_O32_SOFT_FLOAT)
  634. {
  635. switch (cif->rtype->type)
  636. {
  637. case FFI_TYPE_FLOAT:
  638. return FFI_TYPE_INT;
  639. case FFI_TYPE_DOUBLE:
  640. return FFI_TYPE_UINT64;
  641. default:
  642. return cif->rtype->type;
  643. }
  644. }
  645. else
  646. {
  647. return cif->rtype->type;
  648. }
  649. }
  650. #if defined(FFI_MIPS_N32)
  651. static void
  652. copy_struct_N32(char *target, unsigned offset, ffi_abi abi, ffi_type *type,
  653. int argn, unsigned arg_offset, ffi_arg *ar,
  654. ffi_arg *fpr)
  655. {
  656. ffi_type **elt_typep = type->elements;
  657. while(*elt_typep)
  658. {
  659. ffi_type *elt_type = *elt_typep;
  660. unsigned o;
  661. char *tp;
  662. char *argp;
  663. char *fpp;
  664. o = ALIGN(offset, elt_type->alignment);
  665. arg_offset += o - offset;
  666. offset = o;
  667. argn += arg_offset / sizeof(ffi_arg);
  668. arg_offset = arg_offset % sizeof(ffi_arg);
  669. argp = (char *)(ar + argn);
  670. fpp = (char *)(argn >= 8 ? ar + argn : fpr + argn);
  671. tp = target + offset;
  672. if (elt_type->type == FFI_TYPE_DOUBLE)
  673. *(double *)tp = *(double *)fpp;
  674. else
  675. memcpy(tp, argp + arg_offset, elt_type->size);
  676. offset += elt_type->size;
  677. arg_offset += elt_type->size;
  678. elt_typep++;
  679. argn += arg_offset / sizeof(ffi_arg);
  680. arg_offset = arg_offset % sizeof(ffi_arg);
  681. }
  682. }
  683. /*
  684. * Decodes the arguments to a function, which will be stored on the
  685. * stack. AR is the pointer to the beginning of the integer
  686. * arguments. FPR is a pointer to the area where floating point
  687. * registers have been saved.
  688. *
  689. * RVALUE is the location where the function return value will be
  690. * stored. CLOSURE is the prepared closure to invoke.
  691. *
  692. * This function should only be called from assembly, which is in
  693. * turn called from a trampoline.
  694. *
  695. * Returns the function return flags.
  696. *
  697. */
  698. int
  699. ffi_closure_mips_inner_N32 (ffi_closure *closure,
  700. void *rvalue, ffi_arg *ar,
  701. ffi_arg *fpr)
  702. {
  703. ffi_cif *cif;
  704. void **avaluep;
  705. ffi_arg *avalue;
  706. ffi_type **arg_types;
  707. int i, avn, argn;
  708. cif = closure->cif;
  709. avalue = alloca (cif->nargs * sizeof (ffi_arg));
  710. avaluep = alloca (cif->nargs * sizeof (ffi_arg));
  711. argn = 0;
  712. if (cif->rstruct_flag)
  713. {
  714. #if _MIPS_SIM==_ABIN32
  715. rvalue = (void *)(UINT32)ar[0];
  716. #else /* N64 */
  717. rvalue = (void *)ar[0];
  718. #endif
  719. argn = 1;
  720. }
  721. i = 0;
  722. avn = cif->nargs;
  723. arg_types = cif->arg_types;
  724. while (i < avn)
  725. {
  726. if (arg_types[i]->type == FFI_TYPE_FLOAT
  727. || arg_types[i]->type == FFI_TYPE_DOUBLE)
  728. {
  729. ffi_arg *argp = argn >= 8 ? ar + argn : fpr + argn;
  730. #ifdef __MIPSEB__
  731. if (arg_types[i]->type == FFI_TYPE_FLOAT && argn < 8)
  732. avaluep[i] = ((char *) argp) + sizeof (float);
  733. else
  734. #endif
  735. avaluep[i] = (char *) argp;
  736. }
  737. else
  738. {
  739. unsigned type = arg_types[i]->type;
  740. if (arg_types[i]->alignment > sizeof(ffi_arg))
  741. argn = ALIGN(argn, arg_types[i]->alignment / sizeof(ffi_arg));
  742. ffi_arg *argp = ar + argn;
  743. /* The size of a pointer depends on the ABI */
  744. if (type == FFI_TYPE_POINTER)
  745. type = (cif->abi == FFI_N64) ? FFI_TYPE_SINT64 : FFI_TYPE_SINT32;
  746. switch (type)
  747. {
  748. case FFI_TYPE_SINT8:
  749. avaluep[i] = &avalue[i];
  750. *(SINT8 *) &avalue[i] = (SINT8) *argp;
  751. break;
  752. case FFI_TYPE_UINT8:
  753. avaluep[i] = &avalue[i];
  754. *(UINT8 *) &avalue[i] = (UINT8) *argp;
  755. break;
  756. case FFI_TYPE_SINT16:
  757. avaluep[i] = &avalue[i];
  758. *(SINT16 *) &avalue[i] = (SINT16) *argp;
  759. break;
  760. case FFI_TYPE_UINT16:
  761. avaluep[i] = &avalue[i];
  762. *(UINT16 *) &avalue[i] = (UINT16) *argp;
  763. break;
  764. case FFI_TYPE_SINT32:
  765. avaluep[i] = &avalue[i];
  766. *(SINT32 *) &avalue[i] = (SINT32) *argp;
  767. break;
  768. case FFI_TYPE_UINT32:
  769. avaluep[i] = &avalue[i];
  770. *(UINT32 *) &avalue[i] = (UINT32) *argp;
  771. break;
  772. case FFI_TYPE_STRUCT:
  773. if (argn < 8)
  774. {
  775. /* Allocate space for the struct as at least part of
  776. it was passed in registers. */
  777. avaluep[i] = alloca(arg_types[i]->size);
  778. copy_struct_N32(avaluep[i], 0, cif->abi, arg_types[i],
  779. argn, 0, ar, fpr);
  780. break;
  781. }
  782. /* Else fall through. */
  783. default:
  784. avaluep[i] = (char *) argp;
  785. break;
  786. }
  787. }
  788. argn += ALIGN(arg_types[i]->size, sizeof(ffi_arg)) / sizeof(ffi_arg);
  789. i++;
  790. }
  791. /* Invoke the closure. */
  792. (closure->fun) (cif, rvalue, avaluep, closure->user_data);
  793. return cif->flags >> (FFI_FLAG_BITS * 8);
  794. }
  795. #endif /* FFI_MIPS_N32 */
  796. #endif /* FFI_CLOSURES */