/js/src/ctypes/libffi/src/sh/ffi.c

http://github.com/zpao/v8monkey · C · 716 lines · 584 code · 83 blank · 49 comment · 119 complexity · 2d1bfc8959e6b3ba92e32cbad5ea94b6 MD5 · raw file

  1. /* -----------------------------------------------------------------------
  2. ffi.c - Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Kaz Kojima
  3. Copyright (c) 2008 Red Hat, Inc.
  4. SuperH 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. #define NGREGARG 4
  27. #if defined(__SH4__)
  28. #define NFREGARG 8
  29. #endif
  30. #if defined(__HITACHI__)
  31. #define STRUCT_VALUE_ADDRESS_WITH_ARG 1
  32. #else
  33. #define STRUCT_VALUE_ADDRESS_WITH_ARG 0
  34. #endif
  35. /* If the structure has essentialy an unique element, return its type. */
  36. static int
  37. simple_type (ffi_type *arg)
  38. {
  39. if (arg->type != FFI_TYPE_STRUCT)
  40. return arg->type;
  41. else if (arg->elements[1])
  42. return FFI_TYPE_STRUCT;
  43. return simple_type (arg->elements[0]);
  44. }
  45. static int
  46. return_type (ffi_type *arg)
  47. {
  48. unsigned short type;
  49. if (arg->type != FFI_TYPE_STRUCT)
  50. return arg->type;
  51. type = simple_type (arg->elements[0]);
  52. if (! arg->elements[1])
  53. {
  54. switch (type)
  55. {
  56. case FFI_TYPE_SINT8:
  57. case FFI_TYPE_UINT8:
  58. case FFI_TYPE_SINT16:
  59. case FFI_TYPE_UINT16:
  60. case FFI_TYPE_SINT32:
  61. case FFI_TYPE_UINT32:
  62. return FFI_TYPE_INT;
  63. default:
  64. return type;
  65. }
  66. }
  67. /* gcc uses r0/r1 pair for some kind of structures. */
  68. if (arg->size <= 2 * sizeof (int))
  69. {
  70. int i = 0;
  71. ffi_type *e;
  72. while ((e = arg->elements[i++]))
  73. {
  74. type = simple_type (e);
  75. switch (type)
  76. {
  77. case FFI_TYPE_SINT32:
  78. case FFI_TYPE_UINT32:
  79. case FFI_TYPE_INT:
  80. case FFI_TYPE_FLOAT:
  81. return FFI_TYPE_UINT64;
  82. default:
  83. break;
  84. }
  85. }
  86. }
  87. return FFI_TYPE_STRUCT;
  88. }
  89. /* ffi_prep_args is called by the assembly routine once stack space
  90. has been allocated for the function's arguments */
  91. void ffi_prep_args(char *stack, extended_cif *ecif)
  92. {
  93. register unsigned int i;
  94. register int tmp;
  95. register unsigned int avn;
  96. register void **p_argv;
  97. register char *argp;
  98. register ffi_type **p_arg;
  99. int greg, ireg;
  100. #if defined(__SH4__)
  101. int freg = 0;
  102. #endif
  103. tmp = 0;
  104. argp = stack;
  105. if (return_type (ecif->cif->rtype) == FFI_TYPE_STRUCT)
  106. {
  107. *(void **) argp = ecif->rvalue;
  108. argp += 4;
  109. ireg = STRUCT_VALUE_ADDRESS_WITH_ARG ? 1 : 0;
  110. }
  111. else
  112. ireg = 0;
  113. /* Set arguments for registers. */
  114. greg = ireg;
  115. avn = ecif->cif->nargs;
  116. p_argv = ecif->avalue;
  117. for (i = 0, p_arg = ecif->cif->arg_types; i < avn; i++, p_arg++, p_argv++)
  118. {
  119. size_t z;
  120. z = (*p_arg)->size;
  121. if (z < sizeof(int))
  122. {
  123. if (greg++ >= NGREGARG)
  124. continue;
  125. z = sizeof(int);
  126. switch ((*p_arg)->type)
  127. {
  128. case FFI_TYPE_SINT8:
  129. *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
  130. break;
  131. case FFI_TYPE_UINT8:
  132. *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
  133. break;
  134. case FFI_TYPE_SINT16:
  135. *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
  136. break;
  137. case FFI_TYPE_UINT16:
  138. *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
  139. break;
  140. case FFI_TYPE_STRUCT:
  141. *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
  142. break;
  143. default:
  144. FFI_ASSERT(0);
  145. }
  146. argp += z;
  147. }
  148. else if (z == sizeof(int))
  149. {
  150. #if defined(__SH4__)
  151. if ((*p_arg)->type == FFI_TYPE_FLOAT)
  152. {
  153. if (freg++ >= NFREGARG)
  154. continue;
  155. }
  156. else
  157. #endif
  158. {
  159. if (greg++ >= NGREGARG)
  160. continue;
  161. }
  162. *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
  163. argp += z;
  164. }
  165. #if defined(__SH4__)
  166. else if ((*p_arg)->type == FFI_TYPE_DOUBLE)
  167. {
  168. if (freg + 1 >= NFREGARG)
  169. continue;
  170. freg = (freg + 1) & ~1;
  171. freg += 2;
  172. memcpy (argp, *p_argv, z);
  173. argp += z;
  174. }
  175. #endif
  176. else
  177. {
  178. int n = (z + sizeof (int) - 1) / sizeof (int);
  179. #if defined(__SH4__)
  180. if (greg + n - 1 >= NGREGARG)
  181. continue;
  182. #else
  183. if (greg >= NGREGARG)
  184. continue;
  185. #endif
  186. greg += n;
  187. memcpy (argp, *p_argv, z);
  188. argp += n * sizeof (int);
  189. }
  190. }
  191. /* Set arguments on stack. */
  192. greg = ireg;
  193. #if defined(__SH4__)
  194. freg = 0;
  195. #endif
  196. p_argv = ecif->avalue;
  197. for (i = 0, p_arg = ecif->cif->arg_types; i < avn; i++, p_arg++, p_argv++)
  198. {
  199. size_t z;
  200. z = (*p_arg)->size;
  201. if (z < sizeof(int))
  202. {
  203. if (greg++ < NGREGARG)
  204. continue;
  205. z = sizeof(int);
  206. switch ((*p_arg)->type)
  207. {
  208. case FFI_TYPE_SINT8:
  209. *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
  210. break;
  211. case FFI_TYPE_UINT8:
  212. *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
  213. break;
  214. case FFI_TYPE_SINT16:
  215. *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
  216. break;
  217. case FFI_TYPE_UINT16:
  218. *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
  219. break;
  220. case FFI_TYPE_STRUCT:
  221. *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
  222. break;
  223. default:
  224. FFI_ASSERT(0);
  225. }
  226. argp += z;
  227. }
  228. else if (z == sizeof(int))
  229. {
  230. #if defined(__SH4__)
  231. if ((*p_arg)->type == FFI_TYPE_FLOAT)
  232. {
  233. if (freg++ < NFREGARG)
  234. continue;
  235. }
  236. else
  237. #endif
  238. {
  239. if (greg++ < NGREGARG)
  240. continue;
  241. }
  242. *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
  243. argp += z;
  244. }
  245. #if defined(__SH4__)
  246. else if ((*p_arg)->type == FFI_TYPE_DOUBLE)
  247. {
  248. if (freg + 1 < NFREGARG)
  249. {
  250. freg = (freg + 1) & ~1;
  251. freg += 2;
  252. continue;
  253. }
  254. memcpy (argp, *p_argv, z);
  255. argp += z;
  256. }
  257. #endif
  258. else
  259. {
  260. int n = (z + sizeof (int) - 1) / sizeof (int);
  261. if (greg + n - 1 < NGREGARG)
  262. {
  263. greg += n;
  264. continue;
  265. }
  266. #if (! defined(__SH4__))
  267. else if (greg < NGREGARG)
  268. {
  269. greg = NGREGARG;
  270. continue;
  271. }
  272. #endif
  273. memcpy (argp, *p_argv, z);
  274. argp += n * sizeof (int);
  275. }
  276. }
  277. return;
  278. }
  279. /* Perform machine dependent cif processing */
  280. ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
  281. {
  282. int i, j;
  283. int size, type;
  284. int n, m;
  285. int greg;
  286. #if defined(__SH4__)
  287. int freg = 0;
  288. #endif
  289. cif->flags = 0;
  290. greg = ((return_type (cif->rtype) == FFI_TYPE_STRUCT) &&
  291. STRUCT_VALUE_ADDRESS_WITH_ARG) ? 1 : 0;
  292. #if defined(__SH4__)
  293. for (i = j = 0; i < cif->nargs && j < 12; i++)
  294. {
  295. type = (cif->arg_types)[i]->type;
  296. switch (type)
  297. {
  298. case FFI_TYPE_FLOAT:
  299. if (freg >= NFREGARG)
  300. continue;
  301. freg++;
  302. cif->flags += ((cif->arg_types)[i]->type) << (2 * j);
  303. j++;
  304. break;
  305. case FFI_TYPE_DOUBLE:
  306. if ((freg + 1) >= NFREGARG)
  307. continue;
  308. freg = (freg + 1) & ~1;
  309. freg += 2;
  310. cif->flags += ((cif->arg_types)[i]->type) << (2 * j);
  311. j++;
  312. break;
  313. default:
  314. size = (cif->arg_types)[i]->size;
  315. n = (size + sizeof (int) - 1) / sizeof (int);
  316. if (greg + n - 1 >= NGREGARG)
  317. continue;
  318. greg += n;
  319. for (m = 0; m < n; m++)
  320. cif->flags += FFI_TYPE_INT << (2 * j++);
  321. break;
  322. }
  323. }
  324. #else
  325. for (i = j = 0; i < cif->nargs && j < 4; i++)
  326. {
  327. size = (cif->arg_types)[i]->size;
  328. n = (size + sizeof (int) - 1) / sizeof (int);
  329. if (greg >= NGREGARG)
  330. continue;
  331. else if (greg + n - 1 >= NGREGARG)
  332. n = NGREGARG - greg;
  333. greg += n;
  334. for (m = 0; m < n; m++)
  335. cif->flags += FFI_TYPE_INT << (2 * j++);
  336. }
  337. #endif
  338. /* Set the return type flag */
  339. switch (cif->rtype->type)
  340. {
  341. case FFI_TYPE_STRUCT:
  342. cif->flags += (unsigned) (return_type (cif->rtype)) << 24;
  343. break;
  344. case FFI_TYPE_VOID:
  345. case FFI_TYPE_FLOAT:
  346. case FFI_TYPE_DOUBLE:
  347. case FFI_TYPE_SINT64:
  348. case FFI_TYPE_UINT64:
  349. cif->flags += (unsigned) cif->rtype->type << 24;
  350. break;
  351. default:
  352. cif->flags += FFI_TYPE_INT << 24;
  353. break;
  354. }
  355. return FFI_OK;
  356. }
  357. extern void ffi_call_SYSV(void (*)(char *, extended_cif *), extended_cif *,
  358. unsigned, unsigned, unsigned *, void (*fn)(void));
  359. void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
  360. {
  361. extended_cif ecif;
  362. UINT64 trvalue;
  363. ecif.cif = cif;
  364. ecif.avalue = avalue;
  365. /* If the return value is a struct and we don't have a return */
  366. /* value address then we need to make one */
  367. if (cif->rtype->type == FFI_TYPE_STRUCT
  368. && return_type (cif->rtype) != FFI_TYPE_STRUCT)
  369. ecif.rvalue = &trvalue;
  370. else if ((rvalue == NULL) &&
  371. (cif->rtype->type == FFI_TYPE_STRUCT))
  372. {
  373. ecif.rvalue = alloca(cif->rtype->size);
  374. }
  375. else
  376. ecif.rvalue = rvalue;
  377. switch (cif->abi)
  378. {
  379. case FFI_SYSV:
  380. ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes, cif->flags, ecif.rvalue,
  381. fn);
  382. break;
  383. default:
  384. FFI_ASSERT(0);
  385. break;
  386. }
  387. if (rvalue
  388. && cif->rtype->type == FFI_TYPE_STRUCT
  389. && return_type (cif->rtype) != FFI_TYPE_STRUCT)
  390. memcpy (rvalue, &trvalue, cif->rtype->size);
  391. }
  392. extern void ffi_closure_SYSV (void);
  393. #if defined(__SH4__)
  394. extern void __ic_invalidate (void *line);
  395. #endif
  396. ffi_status
  397. ffi_prep_closure_loc (ffi_closure* closure,
  398. ffi_cif* cif,
  399. void (*fun)(ffi_cif*, void*, void**, void*),
  400. void *user_data,
  401. void *codeloc)
  402. {
  403. unsigned int *tramp;
  404. unsigned int insn;
  405. FFI_ASSERT (cif->abi == FFI_GCC_SYSV);
  406. tramp = (unsigned int *) &closure->tramp[0];
  407. /* Set T bit if the function returns a struct pointed with R2. */
  408. insn = (return_type (cif->rtype) == FFI_TYPE_STRUCT
  409. ? 0x0018 /* sett */
  410. : 0x0008 /* clrt */);
  411. #ifdef __LITTLE_ENDIAN__
  412. tramp[0] = 0xd301d102;
  413. tramp[1] = 0x0000412b | (insn << 16);
  414. #else
  415. tramp[0] = 0xd102d301;
  416. tramp[1] = 0x412b0000 | insn;
  417. #endif
  418. *(void **) &tramp[2] = (void *)codeloc; /* ctx */
  419. *(void **) &tramp[3] = (void *)ffi_closure_SYSV; /* funaddr */
  420. closure->cif = cif;
  421. closure->fun = fun;
  422. closure->user_data = user_data;
  423. #if defined(__SH4__)
  424. /* Flush the icache. */
  425. __ic_invalidate(codeloc);
  426. #endif
  427. return FFI_OK;
  428. }
  429. /* Basically the trampoline invokes ffi_closure_SYSV, and on
  430. * entry, r3 holds the address of the closure.
  431. * After storing the registers that could possibly contain
  432. * parameters to be passed into the stack frame and setting
  433. * up space for a return value, ffi_closure_SYSV invokes the
  434. * following helper function to do most of the work.
  435. */
  436. #ifdef __LITTLE_ENDIAN__
  437. #define OFS_INT8 0
  438. #define OFS_INT16 0
  439. #else
  440. #define OFS_INT8 3
  441. #define OFS_INT16 2
  442. #endif
  443. int
  444. ffi_closure_helper_SYSV (ffi_closure *closure, void *rvalue,
  445. unsigned long *pgr, unsigned long *pfr,
  446. unsigned long *pst)
  447. {
  448. void **avalue;
  449. ffi_type **p_arg;
  450. int i, avn;
  451. int ireg, greg = 0;
  452. #if defined(__SH4__)
  453. int freg = 0;
  454. #endif
  455. ffi_cif *cif;
  456. cif = closure->cif;
  457. avalue = alloca(cif->nargs * sizeof(void *));
  458. /* Copy the caller's structure return value address so that the closure
  459. returns the data directly to the caller. */
  460. if (cif->rtype->type == FFI_TYPE_STRUCT && STRUCT_VALUE_ADDRESS_WITH_ARG)
  461. {
  462. rvalue = (void *) *pgr++;
  463. ireg = 1;
  464. }
  465. else
  466. ireg = 0;
  467. cif = closure->cif;
  468. greg = ireg;
  469. avn = cif->nargs;
  470. /* Grab the addresses of the arguments from the stack frame. */
  471. for (i = 0, p_arg = cif->arg_types; i < avn; i++, p_arg++)
  472. {
  473. size_t z;
  474. z = (*p_arg)->size;
  475. if (z < sizeof(int))
  476. {
  477. if (greg++ >= NGREGARG)
  478. continue;
  479. z = sizeof(int);
  480. switch ((*p_arg)->type)
  481. {
  482. case FFI_TYPE_SINT8:
  483. case FFI_TYPE_UINT8:
  484. avalue[i] = (((char *)pgr) + OFS_INT8);
  485. break;
  486. case FFI_TYPE_SINT16:
  487. case FFI_TYPE_UINT16:
  488. avalue[i] = (((char *)pgr) + OFS_INT16);
  489. break;
  490. case FFI_TYPE_STRUCT:
  491. avalue[i] = pgr;
  492. break;
  493. default:
  494. FFI_ASSERT(0);
  495. }
  496. pgr++;
  497. }
  498. else if (z == sizeof(int))
  499. {
  500. #if defined(__SH4__)
  501. if ((*p_arg)->type == FFI_TYPE_FLOAT)
  502. {
  503. if (freg++ >= NFREGARG)
  504. continue;
  505. avalue[i] = pfr;
  506. pfr++;
  507. }
  508. else
  509. #endif
  510. {
  511. if (greg++ >= NGREGARG)
  512. continue;
  513. avalue[i] = pgr;
  514. pgr++;
  515. }
  516. }
  517. #if defined(__SH4__)
  518. else if ((*p_arg)->type == FFI_TYPE_DOUBLE)
  519. {
  520. if (freg + 1 >= NFREGARG)
  521. continue;
  522. if (freg & 1)
  523. pfr++;
  524. freg = (freg + 1) & ~1;
  525. freg += 2;
  526. avalue[i] = pfr;
  527. pfr += 2;
  528. }
  529. #endif
  530. else
  531. {
  532. int n = (z + sizeof (int) - 1) / sizeof (int);
  533. #if defined(__SH4__)
  534. if (greg + n - 1 >= NGREGARG)
  535. continue;
  536. #else
  537. if (greg >= NGREGARG)
  538. continue;
  539. #endif
  540. greg += n;
  541. avalue[i] = pgr;
  542. pgr += n;
  543. }
  544. }
  545. greg = ireg;
  546. #if defined(__SH4__)
  547. freg = 0;
  548. #endif
  549. for (i = 0, p_arg = cif->arg_types; i < avn; i++, p_arg++)
  550. {
  551. size_t z;
  552. z = (*p_arg)->size;
  553. if (z < sizeof(int))
  554. {
  555. if (greg++ < NGREGARG)
  556. continue;
  557. z = sizeof(int);
  558. switch ((*p_arg)->type)
  559. {
  560. case FFI_TYPE_SINT8:
  561. case FFI_TYPE_UINT8:
  562. avalue[i] = (((char *)pst) + OFS_INT8);
  563. break;
  564. case FFI_TYPE_SINT16:
  565. case FFI_TYPE_UINT16:
  566. avalue[i] = (((char *)pst) + OFS_INT16);
  567. break;
  568. case FFI_TYPE_STRUCT:
  569. avalue[i] = pst;
  570. break;
  571. default:
  572. FFI_ASSERT(0);
  573. }
  574. pst++;
  575. }
  576. else if (z == sizeof(int))
  577. {
  578. #if defined(__SH4__)
  579. if ((*p_arg)->type == FFI_TYPE_FLOAT)
  580. {
  581. if (freg++ < NFREGARG)
  582. continue;
  583. }
  584. else
  585. #endif
  586. {
  587. if (greg++ < NGREGARG)
  588. continue;
  589. }
  590. avalue[i] = pst;
  591. pst++;
  592. }
  593. #if defined(__SH4__)
  594. else if ((*p_arg)->type == FFI_TYPE_DOUBLE)
  595. {
  596. if (freg + 1 < NFREGARG)
  597. {
  598. freg = (freg + 1) & ~1;
  599. freg += 2;
  600. continue;
  601. }
  602. avalue[i] = pst;
  603. pst += 2;
  604. }
  605. #endif
  606. else
  607. {
  608. int n = (z + sizeof (int) - 1) / sizeof (int);
  609. if (greg + n - 1 < NGREGARG)
  610. {
  611. greg += n;
  612. continue;
  613. }
  614. #if (! defined(__SH4__))
  615. else if (greg < NGREGARG)
  616. {
  617. greg += n;
  618. pst += greg - NGREGARG;
  619. continue;
  620. }
  621. #endif
  622. avalue[i] = pst;
  623. pst += n;
  624. }
  625. }
  626. (closure->fun) (cif, rvalue, avalue, closure->user_data);
  627. /* Tell ffi_closure_SYSV how to perform return type promotions. */
  628. return return_type (cif->rtype);
  629. }