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

http://unladen-swallow.googlecode.com/ · C · 610 lines · 448 code · 83 blank · 79 comment · 87 complexity · 0e696d546b342a37d9bdcb56bc332515 MD5 · raw file

  1. /* -----------------------------------------------------------------------
  2. ffi.c - Copyright (c) 1996, 2003, 2004, 2007, 2008 Red Hat, Inc.
  3. SPARC Foreign Function Interface
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. ``Software''), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be included
  12. in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. DEALINGS IN THE SOFTWARE.
  21. ----------------------------------------------------------------------- */
  22. #include <ffi.h>
  23. #include <ffi_common.h>
  24. #include <stdlib.h>
  25. /* ffi_prep_args is called by the assembly routine once stack space
  26. has been allocated for the function's arguments */
  27. void ffi_prep_args_v8(char *stack, extended_cif *ecif)
  28. {
  29. int i;
  30. void **p_argv;
  31. char *argp;
  32. ffi_type **p_arg;
  33. /* Skip 16 words for the window save area */
  34. argp = stack + 16*sizeof(int);
  35. /* This should only really be done when we are returning a structure,
  36. however, it's faster just to do it all the time...
  37. if ( ecif->cif->rtype->type == FFI_TYPE_STRUCT ) */
  38. *(int *) argp = (long)ecif->rvalue;
  39. /* And 1 word for the structure return value. */
  40. argp += sizeof(int);
  41. #ifdef USING_PURIFY
  42. /* Purify will probably complain in our assembly routine, unless we
  43. zero out this memory. */
  44. ((int*)argp)[0] = 0;
  45. ((int*)argp)[1] = 0;
  46. ((int*)argp)[2] = 0;
  47. ((int*)argp)[3] = 0;
  48. ((int*)argp)[4] = 0;
  49. ((int*)argp)[5] = 0;
  50. #endif
  51. p_argv = ecif->avalue;
  52. for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types; i; i--, p_arg++)
  53. {
  54. size_t z;
  55. if ((*p_arg)->type == FFI_TYPE_STRUCT
  56. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  57. || (*p_arg)->type == FFI_TYPE_LONGDOUBLE
  58. #endif
  59. )
  60. {
  61. *(unsigned int *) argp = (unsigned long)(* p_argv);
  62. z = sizeof(int);
  63. }
  64. else
  65. {
  66. z = (*p_arg)->size;
  67. if (z < sizeof(int))
  68. {
  69. z = sizeof(int);
  70. switch ((*p_arg)->type)
  71. {
  72. case FFI_TYPE_SINT8:
  73. *(signed int *) argp = *(SINT8 *)(* p_argv);
  74. break;
  75. case FFI_TYPE_UINT8:
  76. *(unsigned int *) argp = *(UINT8 *)(* p_argv);
  77. break;
  78. case FFI_TYPE_SINT16:
  79. *(signed int *) argp = *(SINT16 *)(* p_argv);
  80. break;
  81. case FFI_TYPE_UINT16:
  82. *(unsigned int *) argp = *(UINT16 *)(* p_argv);
  83. break;
  84. default:
  85. FFI_ASSERT(0);
  86. }
  87. }
  88. else
  89. {
  90. memcpy(argp, *p_argv, z);
  91. }
  92. }
  93. p_argv++;
  94. argp += z;
  95. }
  96. return;
  97. }
  98. int ffi_prep_args_v9(char *stack, extended_cif *ecif)
  99. {
  100. int i, ret = 0;
  101. int tmp;
  102. void **p_argv;
  103. char *argp;
  104. ffi_type **p_arg;
  105. tmp = 0;
  106. /* Skip 16 words for the window save area */
  107. argp = stack + 16*sizeof(long long);
  108. #ifdef USING_PURIFY
  109. /* Purify will probably complain in our assembly routine, unless we
  110. zero out this memory. */
  111. ((long long*)argp)[0] = 0;
  112. ((long long*)argp)[1] = 0;
  113. ((long long*)argp)[2] = 0;
  114. ((long long*)argp)[3] = 0;
  115. ((long long*)argp)[4] = 0;
  116. ((long long*)argp)[5] = 0;
  117. #endif
  118. p_argv = ecif->avalue;
  119. if (ecif->cif->rtype->type == FFI_TYPE_STRUCT &&
  120. ecif->cif->rtype->size > 32)
  121. {
  122. *(unsigned long long *) argp = (unsigned long)ecif->rvalue;
  123. argp += sizeof(long long);
  124. tmp = 1;
  125. }
  126. for (i = 0, p_arg = ecif->cif->arg_types; i < ecif->cif->nargs;
  127. i++, p_arg++)
  128. {
  129. size_t z;
  130. z = (*p_arg)->size;
  131. switch ((*p_arg)->type)
  132. {
  133. case FFI_TYPE_STRUCT:
  134. if (z > 16)
  135. {
  136. /* For structures larger than 16 bytes we pass reference. */
  137. *(unsigned long long *) argp = (unsigned long)* p_argv;
  138. argp += sizeof(long long);
  139. tmp++;
  140. p_argv++;
  141. continue;
  142. }
  143. /* FALLTHROUGH */
  144. case FFI_TYPE_FLOAT:
  145. case FFI_TYPE_DOUBLE:
  146. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  147. case FFI_TYPE_LONGDOUBLE:
  148. #endif
  149. ret = 1; /* We should promote into FP regs as well as integer. */
  150. break;
  151. }
  152. if (z < sizeof(long long))
  153. {
  154. switch ((*p_arg)->type)
  155. {
  156. case FFI_TYPE_SINT8:
  157. *(signed long long *) argp = *(SINT8 *)(* p_argv);
  158. break;
  159. case FFI_TYPE_UINT8:
  160. *(unsigned long long *) argp = *(UINT8 *)(* p_argv);
  161. break;
  162. case FFI_TYPE_SINT16:
  163. *(signed long long *) argp = *(SINT16 *)(* p_argv);
  164. break;
  165. case FFI_TYPE_UINT16:
  166. *(unsigned long long *) argp = *(UINT16 *)(* p_argv);
  167. break;
  168. case FFI_TYPE_SINT32:
  169. *(signed long long *) argp = *(SINT32 *)(* p_argv);
  170. break;
  171. case FFI_TYPE_UINT32:
  172. *(unsigned long long *) argp = *(UINT32 *)(* p_argv);
  173. break;
  174. case FFI_TYPE_FLOAT:
  175. *(float *) (argp + 4) = *(FLOAT32 *)(* p_argv); /* Right justify */
  176. break;
  177. case FFI_TYPE_STRUCT:
  178. memcpy(argp, *p_argv, z);
  179. break;
  180. default:
  181. FFI_ASSERT(0);
  182. }
  183. z = sizeof(long long);
  184. tmp++;
  185. }
  186. else if (z == sizeof(long long))
  187. {
  188. memcpy(argp, *p_argv, z);
  189. z = sizeof(long long);
  190. tmp++;
  191. }
  192. else
  193. {
  194. if ((tmp & 1) && (*p_arg)->alignment > 8)
  195. {
  196. tmp++;
  197. argp += sizeof(long long);
  198. }
  199. memcpy(argp, *p_argv, z);
  200. z = 2 * sizeof(long long);
  201. tmp += 2;
  202. }
  203. p_argv++;
  204. argp += z;
  205. }
  206. return ret;
  207. }
  208. /* Perform machine dependent cif processing */
  209. ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
  210. {
  211. int wordsize;
  212. if (cif->abi != FFI_V9)
  213. {
  214. wordsize = 4;
  215. /* If we are returning a struct, this will already have been added.
  216. Otherwise we need to add it because it's always got to be there! */
  217. if (cif->rtype->type != FFI_TYPE_STRUCT)
  218. cif->bytes += wordsize;
  219. /* sparc call frames require that space is allocated for 6 args,
  220. even if they aren't used. Make that space if necessary. */
  221. if (cif->bytes < 4*6+4)
  222. cif->bytes = 4*6+4;
  223. }
  224. else
  225. {
  226. wordsize = 8;
  227. /* sparc call frames require that space is allocated for 6 args,
  228. even if they aren't used. Make that space if necessary. */
  229. if (cif->bytes < 8*6)
  230. cif->bytes = 8*6;
  231. }
  232. /* Adjust cif->bytes. to include 16 words for the window save area,
  233. and maybe the struct/union return pointer area, */
  234. cif->bytes += 16 * wordsize;
  235. /* The stack must be 2 word aligned, so round bytes up
  236. appropriately. */
  237. cif->bytes = ALIGN(cif->bytes, 2 * wordsize);
  238. /* Set the return type flag */
  239. switch (cif->rtype->type)
  240. {
  241. case FFI_TYPE_VOID:
  242. case FFI_TYPE_FLOAT:
  243. case FFI_TYPE_DOUBLE:
  244. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  245. case FFI_TYPE_LONGDOUBLE:
  246. #endif
  247. cif->flags = cif->rtype->type;
  248. break;
  249. case FFI_TYPE_STRUCT:
  250. if (cif->abi == FFI_V9 && cif->rtype->size > 32)
  251. cif->flags = FFI_TYPE_VOID;
  252. else
  253. cif->flags = FFI_TYPE_STRUCT;
  254. break;
  255. case FFI_TYPE_SINT64:
  256. case FFI_TYPE_UINT64:
  257. if (cif->abi != FFI_V9)
  258. {
  259. cif->flags = FFI_TYPE_SINT64;
  260. break;
  261. }
  262. /* FALLTHROUGH */
  263. default:
  264. cif->flags = FFI_TYPE_INT;
  265. break;
  266. }
  267. return FFI_OK;
  268. }
  269. int ffi_v9_layout_struct(ffi_type *arg, int off, char *ret, char *intg, char *flt)
  270. {
  271. ffi_type **ptr = &arg->elements[0];
  272. while (*ptr != NULL)
  273. {
  274. if (off & ((*ptr)->alignment - 1))
  275. off = ALIGN(off, (*ptr)->alignment);
  276. switch ((*ptr)->type)
  277. {
  278. case FFI_TYPE_STRUCT:
  279. off = ffi_v9_layout_struct(*ptr, off, ret, intg, flt);
  280. off = ALIGN(off, FFI_SIZEOF_ARG);
  281. break;
  282. case FFI_TYPE_FLOAT:
  283. case FFI_TYPE_DOUBLE:
  284. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  285. case FFI_TYPE_LONGDOUBLE:
  286. #endif
  287. memmove(ret + off, flt + off, (*ptr)->size);
  288. off += (*ptr)->size;
  289. break;
  290. default:
  291. memmove(ret + off, intg + off, (*ptr)->size);
  292. off += (*ptr)->size;
  293. break;
  294. }
  295. ptr++;
  296. }
  297. return off;
  298. }
  299. #ifdef SPARC64
  300. extern int ffi_call_v9(void *, extended_cif *, unsigned,
  301. unsigned, unsigned *, void (*fn)(void));
  302. #else
  303. extern int ffi_call_v8(void *, extended_cif *, unsigned,
  304. unsigned, unsigned *, void (*fn)(void));
  305. #endif
  306. void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
  307. {
  308. extended_cif ecif;
  309. void *rval = rvalue;
  310. ecif.cif = cif;
  311. ecif.avalue = avalue;
  312. /* If the return value is a struct and we don't have a return */
  313. /* value address then we need to make one */
  314. ecif.rvalue = rvalue;
  315. if (cif->rtype->type == FFI_TYPE_STRUCT)
  316. {
  317. if (cif->rtype->size <= 32)
  318. rval = alloca(64);
  319. else
  320. {
  321. rval = NULL;
  322. if (rvalue == NULL)
  323. ecif.rvalue = alloca(cif->rtype->size);
  324. }
  325. }
  326. switch (cif->abi)
  327. {
  328. case FFI_V8:
  329. #ifdef SPARC64
  330. /* We don't yet support calling 32bit code from 64bit */
  331. FFI_ASSERT(0);
  332. #else
  333. ffi_call_v8(ffi_prep_args_v8, &ecif, cif->bytes,
  334. cif->flags, rvalue, fn);
  335. #endif
  336. break;
  337. case FFI_V9:
  338. #ifdef SPARC64
  339. ffi_call_v9(ffi_prep_args_v9, &ecif, cif->bytes,
  340. cif->flags, rval, fn);
  341. if (rvalue && rval && cif->rtype->type == FFI_TYPE_STRUCT)
  342. ffi_v9_layout_struct(cif->rtype, 0, (char *)rvalue, (char *)rval, ((char *)rval)+32);
  343. #else
  344. /* And vice versa */
  345. FFI_ASSERT(0);
  346. #endif
  347. break;
  348. default:
  349. FFI_ASSERT(0);
  350. break;
  351. }
  352. }
  353. #ifdef SPARC64
  354. extern void ffi_closure_v9(void);
  355. #else
  356. extern void ffi_closure_v8(void);
  357. #endif
  358. ffi_status
  359. ffi_prep_closure_loc (ffi_closure* closure,
  360. ffi_cif* cif,
  361. void (*fun)(ffi_cif*, void*, void**, void*),
  362. void *user_data,
  363. void *codeloc)
  364. {
  365. unsigned int *tramp = (unsigned int *) &closure->tramp[0];
  366. unsigned long fn;
  367. #ifdef SPARC64
  368. /* Trampoline address is equal to the closure address. We take advantage
  369. of that to reduce the trampoline size by 8 bytes. */
  370. FFI_ASSERT (cif->abi == FFI_V9);
  371. fn = (unsigned long) ffi_closure_v9;
  372. tramp[0] = 0x83414000; /* rd %pc, %g1 */
  373. tramp[1] = 0xca586010; /* ldx [%g1+16], %g5 */
  374. tramp[2] = 0x81c14000; /* jmp %g5 */
  375. tramp[3] = 0x01000000; /* nop */
  376. *((unsigned long *) &tramp[4]) = fn;
  377. #else
  378. unsigned long ctx = (unsigned long) codeloc;
  379. FFI_ASSERT (cif->abi == FFI_V8);
  380. fn = (unsigned long) ffi_closure_v8;
  381. tramp[0] = 0x03000000 | fn >> 10; /* sethi %hi(fn), %g1 */
  382. tramp[1] = 0x05000000 | ctx >> 10; /* sethi %hi(ctx), %g2 */
  383. tramp[2] = 0x81c06000 | (fn & 0x3ff); /* jmp %g1+%lo(fn) */
  384. tramp[3] = 0x8410a000 | (ctx & 0x3ff);/* or %g2, %lo(ctx) */
  385. #endif
  386. closure->cif = cif;
  387. closure->fun = fun;
  388. closure->user_data = user_data;
  389. /* Flush the Icache. FIXME: alignment isn't certain, assume 8 bytes */
  390. #ifdef SPARC64
  391. asm volatile ("flush %0" : : "r" (closure) : "memory");
  392. asm volatile ("flush %0" : : "r" (((char *) closure) + 8) : "memory");
  393. #else
  394. asm volatile ("iflush %0" : : "r" (closure) : "memory");
  395. asm volatile ("iflush %0" : : "r" (((char *) closure) + 8) : "memory");
  396. #endif
  397. return FFI_OK;
  398. }
  399. int
  400. ffi_closure_sparc_inner_v8(ffi_closure *closure,
  401. void *rvalue, unsigned long *gpr, unsigned long *scratch)
  402. {
  403. ffi_cif *cif;
  404. ffi_type **arg_types;
  405. void **avalue;
  406. int i, argn;
  407. cif = closure->cif;
  408. arg_types = cif->arg_types;
  409. avalue = alloca(cif->nargs * sizeof(void *));
  410. /* Copy the caller's structure return address so that the closure
  411. returns the data directly to the caller. */
  412. if (cif->flags == FFI_TYPE_STRUCT
  413. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  414. || cif->flags == FFI_TYPE_LONGDOUBLE
  415. #endif
  416. )
  417. rvalue = (void *) gpr[0];
  418. /* Always skip the structure return address. */
  419. argn = 1;
  420. /* Grab the addresses of the arguments from the stack frame. */
  421. for (i = 0; i < cif->nargs; i++)
  422. {
  423. if (arg_types[i]->type == FFI_TYPE_STRUCT
  424. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  425. || arg_types[i]->type == FFI_TYPE_LONGDOUBLE
  426. #endif
  427. )
  428. {
  429. /* Straight copy of invisible reference. */
  430. avalue[i] = (void *)gpr[argn++];
  431. }
  432. else if ((arg_types[i]->type == FFI_TYPE_DOUBLE
  433. || arg_types[i]->type == FFI_TYPE_SINT64
  434. || arg_types[i]->type == FFI_TYPE_UINT64)
  435. /* gpr is 8-byte aligned. */
  436. && (argn % 2) != 0)
  437. {
  438. /* Align on a 8-byte boundary. */
  439. scratch[0] = gpr[argn];
  440. scratch[1] = gpr[argn+1];
  441. avalue[i] = scratch;
  442. scratch -= 2;
  443. argn += 2;
  444. }
  445. else
  446. {
  447. /* Always right-justify. */
  448. argn += ALIGN(arg_types[i]->size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
  449. avalue[i] = ((char *) &gpr[argn]) - arg_types[i]->size;
  450. }
  451. }
  452. /* Invoke the closure. */
  453. (closure->fun) (cif, rvalue, avalue, closure->user_data);
  454. /* Tell ffi_closure_sparc how to perform return type promotions. */
  455. return cif->rtype->type;
  456. }
  457. int
  458. ffi_closure_sparc_inner_v9(ffi_closure *closure,
  459. void *rvalue, unsigned long *gpr, double *fpr)
  460. {
  461. ffi_cif *cif;
  462. ffi_type **arg_types;
  463. void **avalue;
  464. int i, argn, fp_slot_max;
  465. cif = closure->cif;
  466. arg_types = cif->arg_types;
  467. avalue = alloca(cif->nargs * sizeof(void *));
  468. /* Copy the caller's structure return address so that the closure
  469. returns the data directly to the caller. */
  470. if (cif->flags == FFI_TYPE_VOID
  471. && cif->rtype->type == FFI_TYPE_STRUCT)
  472. {
  473. rvalue = (void *) gpr[0];
  474. /* Skip the structure return address. */
  475. argn = 1;
  476. }
  477. else
  478. argn = 0;
  479. fp_slot_max = 16 - argn;
  480. /* Grab the addresses of the arguments from the stack frame. */
  481. for (i = 0; i < cif->nargs; i++)
  482. {
  483. if (arg_types[i]->type == FFI_TYPE_STRUCT)
  484. {
  485. if (arg_types[i]->size > 16)
  486. {
  487. /* Straight copy of invisible reference. */
  488. avalue[i] = (void *)gpr[argn++];
  489. }
  490. else
  491. {
  492. /* Left-justify. */
  493. ffi_v9_layout_struct(arg_types[i],
  494. 0,
  495. (char *) &gpr[argn],
  496. (char *) &gpr[argn],
  497. (char *) &fpr[argn]);
  498. avalue[i] = &gpr[argn];
  499. argn += ALIGN(arg_types[i]->size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
  500. }
  501. }
  502. else
  503. {
  504. /* Right-justify. */
  505. argn += ALIGN(arg_types[i]->size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
  506. if (i < fp_slot_max
  507. && (arg_types[i]->type == FFI_TYPE_FLOAT
  508. || arg_types[i]->type == FFI_TYPE_DOUBLE
  509. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  510. || arg_types[i]->type == FFI_TYPE_LONGDOUBLE
  511. #endif
  512. ))
  513. avalue[i] = ((char *) &fpr[argn]) - arg_types[i]->size;
  514. else
  515. avalue[i] = ((char *) &gpr[argn]) - arg_types[i]->size;
  516. }
  517. }
  518. /* Invoke the closure. */
  519. (closure->fun) (cif, rvalue, avalue, closure->user_data);
  520. /* Tell ffi_closure_sparc how to perform return type promotions. */
  521. return cif->rtype->type;
  522. }