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

http://unladen-swallow.googlecode.com/ · C · 580 lines · 413 code · 84 blank · 83 comment · 72 complexity · 7ca86e7025e65dd69d1ed227e9fe964e MD5 · raw file

  1. /* -----------------------------------------------------------------------
  2. ffi.c - Copyright (c) 1998, 2007, 2008 Red Hat, Inc.
  3. Copyright (c) 2000 Hewlett Packard Company
  4. IA64 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. #include <stdbool.h>
  27. #include <float.h>
  28. #include "ia64_flags.h"
  29. /* A 64-bit pointer value. In LP64 mode, this is effectively a plain
  30. pointer. In ILP32 mode, it's a pointer that's been extended to
  31. 64 bits by "addp4". */
  32. typedef void *PTR64 __attribute__((mode(DI)));
  33. /* Memory image of fp register contents. This is the implementation
  34. specific format used by ldf.fill/stf.spill. All we care about is
  35. that it wants a 16 byte aligned slot. */
  36. typedef struct
  37. {
  38. UINT64 x[2] __attribute__((aligned(16)));
  39. } fpreg;
  40. /* The stack layout given to ffi_call_unix and ffi_closure_unix_inner. */
  41. struct ia64_args
  42. {
  43. fpreg fp_regs[8]; /* Contents of 8 fp arg registers. */
  44. UINT64 gp_regs[8]; /* Contents of 8 gp arg registers. */
  45. UINT64 other_args[]; /* Arguments passed on stack, variable size. */
  46. };
  47. /* Adjust ADDR, a pointer to an 8 byte slot, to point to the low LEN bytes. */
  48. static inline void *
  49. endian_adjust (void *addr, size_t len)
  50. {
  51. #ifdef __BIG_ENDIAN__
  52. return addr + (8 - len);
  53. #else
  54. return addr;
  55. #endif
  56. }
  57. /* Store VALUE to ADDR in the current cpu implementation's fp spill format.
  58. This is a macro instead of a function, so that it works for all 3 floating
  59. point types without type conversions. Type conversion to long double breaks
  60. the denorm support. */
  61. #define stf_spill(addr, value) \
  62. asm ("stf.spill %0 = %1%P0" : "=m" (*addr) : "f"(value));
  63. /* Load a value from ADDR, which is in the current cpu implementation's
  64. fp spill format. As above, this must also be a macro. */
  65. #define ldf_fill(result, addr) \
  66. asm ("ldf.fill %0 = %1%P1" : "=f"(result) : "m"(*addr));
  67. /* Return the size of the C type associated with with TYPE. Which will
  68. be one of the FFI_IA64_TYPE_HFA_* values. */
  69. static size_t
  70. hfa_type_size (int type)
  71. {
  72. switch (type)
  73. {
  74. case FFI_IA64_TYPE_HFA_FLOAT:
  75. return sizeof(float);
  76. case FFI_IA64_TYPE_HFA_DOUBLE:
  77. return sizeof(double);
  78. case FFI_IA64_TYPE_HFA_LDOUBLE:
  79. return sizeof(__float80);
  80. default:
  81. abort ();
  82. }
  83. }
  84. /* Load from ADDR a value indicated by TYPE. Which will be one of
  85. the FFI_IA64_TYPE_HFA_* values. */
  86. static void
  87. hfa_type_load (fpreg *fpaddr, int type, void *addr)
  88. {
  89. switch (type)
  90. {
  91. case FFI_IA64_TYPE_HFA_FLOAT:
  92. stf_spill (fpaddr, *(float *) addr);
  93. return;
  94. case FFI_IA64_TYPE_HFA_DOUBLE:
  95. stf_spill (fpaddr, *(double *) addr);
  96. return;
  97. case FFI_IA64_TYPE_HFA_LDOUBLE:
  98. stf_spill (fpaddr, *(__float80 *) addr);
  99. return;
  100. default:
  101. abort ();
  102. }
  103. }
  104. /* Load VALUE into ADDR as indicated by TYPE. Which will be one of
  105. the FFI_IA64_TYPE_HFA_* values. */
  106. static void
  107. hfa_type_store (int type, void *addr, fpreg *fpaddr)
  108. {
  109. switch (type)
  110. {
  111. case FFI_IA64_TYPE_HFA_FLOAT:
  112. {
  113. float result;
  114. ldf_fill (result, fpaddr);
  115. *(float *) addr = result;
  116. break;
  117. }
  118. case FFI_IA64_TYPE_HFA_DOUBLE:
  119. {
  120. double result;
  121. ldf_fill (result, fpaddr);
  122. *(double *) addr = result;
  123. break;
  124. }
  125. case FFI_IA64_TYPE_HFA_LDOUBLE:
  126. {
  127. __float80 result;
  128. ldf_fill (result, fpaddr);
  129. *(__float80 *) addr = result;
  130. break;
  131. }
  132. default:
  133. abort ();
  134. }
  135. }
  136. /* Is TYPE a struct containing floats, doubles, or extended doubles,
  137. all of the same fp type? If so, return the element type. Return
  138. FFI_TYPE_VOID if not. */
  139. static int
  140. hfa_element_type (ffi_type *type, int nested)
  141. {
  142. int element = FFI_TYPE_VOID;
  143. switch (type->type)
  144. {
  145. case FFI_TYPE_FLOAT:
  146. /* We want to return VOID for raw floating-point types, but the
  147. synthetic HFA type if we're nested within an aggregate. */
  148. if (nested)
  149. element = FFI_IA64_TYPE_HFA_FLOAT;
  150. break;
  151. case FFI_TYPE_DOUBLE:
  152. /* Similarly. */
  153. if (nested)
  154. element = FFI_IA64_TYPE_HFA_DOUBLE;
  155. break;
  156. case FFI_TYPE_LONGDOUBLE:
  157. /* Similarly, except that that HFA is true for double extended,
  158. but not quad precision. Both have sizeof == 16, so tell the
  159. difference based on the precision. */
  160. if (LDBL_MANT_DIG == 64 && nested)
  161. element = FFI_IA64_TYPE_HFA_LDOUBLE;
  162. break;
  163. case FFI_TYPE_STRUCT:
  164. {
  165. ffi_type **ptr = &type->elements[0];
  166. for (ptr = &type->elements[0]; *ptr ; ptr++)
  167. {
  168. int sub_element = hfa_element_type (*ptr, 1);
  169. if (sub_element == FFI_TYPE_VOID)
  170. return FFI_TYPE_VOID;
  171. if (element == FFI_TYPE_VOID)
  172. element = sub_element;
  173. else if (element != sub_element)
  174. return FFI_TYPE_VOID;
  175. }
  176. }
  177. break;
  178. default:
  179. return FFI_TYPE_VOID;
  180. }
  181. return element;
  182. }
  183. /* Perform machine dependent cif processing. */
  184. ffi_status
  185. ffi_prep_cif_machdep(ffi_cif *cif)
  186. {
  187. int flags;
  188. /* Adjust cif->bytes to include space for the bits of the ia64_args frame
  189. that preceeds the integer register portion. The estimate that the
  190. generic bits did for the argument space required is good enough for the
  191. integer component. */
  192. cif->bytes += offsetof(struct ia64_args, gp_regs[0]);
  193. if (cif->bytes < sizeof(struct ia64_args))
  194. cif->bytes = sizeof(struct ia64_args);
  195. /* Set the return type flag. */
  196. flags = cif->rtype->type;
  197. switch (cif->rtype->type)
  198. {
  199. case FFI_TYPE_LONGDOUBLE:
  200. /* Leave FFI_TYPE_LONGDOUBLE as meaning double extended precision,
  201. and encode quad precision as a two-word integer structure. */
  202. if (LDBL_MANT_DIG != 64)
  203. flags = FFI_IA64_TYPE_SMALL_STRUCT | (16 << 8);
  204. break;
  205. case FFI_TYPE_STRUCT:
  206. {
  207. size_t size = cif->rtype->size;
  208. int hfa_type = hfa_element_type (cif->rtype, 0);
  209. if (hfa_type != FFI_TYPE_VOID)
  210. {
  211. size_t nelts = size / hfa_type_size (hfa_type);
  212. if (nelts <= 8)
  213. flags = hfa_type | (size << 8);
  214. }
  215. else
  216. {
  217. if (size <= 32)
  218. flags = FFI_IA64_TYPE_SMALL_STRUCT | (size << 8);
  219. }
  220. }
  221. break;
  222. default:
  223. break;
  224. }
  225. cif->flags = flags;
  226. return FFI_OK;
  227. }
  228. extern int ffi_call_unix (struct ia64_args *, PTR64, void (*)(void), UINT64);
  229. void
  230. ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
  231. {
  232. struct ia64_args *stack;
  233. long i, avn, gpcount, fpcount;
  234. ffi_type **p_arg;
  235. FFI_ASSERT (cif->abi == FFI_UNIX);
  236. /* If we have no spot for a return value, make one. */
  237. if (rvalue == NULL && cif->rtype->type != FFI_TYPE_VOID)
  238. rvalue = alloca (cif->rtype->size);
  239. /* Allocate the stack frame. */
  240. stack = alloca (cif->bytes);
  241. gpcount = fpcount = 0;
  242. avn = cif->nargs;
  243. for (i = 0, p_arg = cif->arg_types; i < avn; i++, p_arg++)
  244. {
  245. switch ((*p_arg)->type)
  246. {
  247. case FFI_TYPE_SINT8:
  248. stack->gp_regs[gpcount++] = *(SINT8 *)avalue[i];
  249. break;
  250. case FFI_TYPE_UINT8:
  251. stack->gp_regs[gpcount++] = *(UINT8 *)avalue[i];
  252. break;
  253. case FFI_TYPE_SINT16:
  254. stack->gp_regs[gpcount++] = *(SINT16 *)avalue[i];
  255. break;
  256. case FFI_TYPE_UINT16:
  257. stack->gp_regs[gpcount++] = *(UINT16 *)avalue[i];
  258. break;
  259. case FFI_TYPE_SINT32:
  260. stack->gp_regs[gpcount++] = *(SINT32 *)avalue[i];
  261. break;
  262. case FFI_TYPE_UINT32:
  263. stack->gp_regs[gpcount++] = *(UINT32 *)avalue[i];
  264. break;
  265. case FFI_TYPE_SINT64:
  266. case FFI_TYPE_UINT64:
  267. stack->gp_regs[gpcount++] = *(UINT64 *)avalue[i];
  268. break;
  269. case FFI_TYPE_POINTER:
  270. stack->gp_regs[gpcount++] = (UINT64)(PTR64) *(void **)avalue[i];
  271. break;
  272. case FFI_TYPE_FLOAT:
  273. if (gpcount < 8 && fpcount < 8)
  274. stf_spill (&stack->fp_regs[fpcount++], *(float *)avalue[i]);
  275. stack->gp_regs[gpcount++] = *(UINT32 *)avalue[i];
  276. break;
  277. case FFI_TYPE_DOUBLE:
  278. if (gpcount < 8 && fpcount < 8)
  279. stf_spill (&stack->fp_regs[fpcount++], *(double *)avalue[i]);
  280. stack->gp_regs[gpcount++] = *(UINT64 *)avalue[i];
  281. break;
  282. case FFI_TYPE_LONGDOUBLE:
  283. if (gpcount & 1)
  284. gpcount++;
  285. if (LDBL_MANT_DIG == 64 && gpcount < 8 && fpcount < 8)
  286. stf_spill (&stack->fp_regs[fpcount++], *(__float80 *)avalue[i]);
  287. memcpy (&stack->gp_regs[gpcount], avalue[i], 16);
  288. gpcount += 2;
  289. break;
  290. case FFI_TYPE_STRUCT:
  291. {
  292. size_t size = (*p_arg)->size;
  293. size_t align = (*p_arg)->alignment;
  294. int hfa_type = hfa_element_type (*p_arg, 0);
  295. FFI_ASSERT (align <= 16);
  296. if (align == 16 && (gpcount & 1))
  297. gpcount++;
  298. if (hfa_type != FFI_TYPE_VOID)
  299. {
  300. size_t hfa_size = hfa_type_size (hfa_type);
  301. size_t offset = 0;
  302. size_t gp_offset = gpcount * 8;
  303. while (fpcount < 8
  304. && offset < size
  305. && gp_offset < 8 * 8)
  306. {
  307. hfa_type_load (&stack->fp_regs[fpcount], hfa_type,
  308. avalue[i] + offset);
  309. offset += hfa_size;
  310. gp_offset += hfa_size;
  311. fpcount += 1;
  312. }
  313. }
  314. memcpy (&stack->gp_regs[gpcount], avalue[i], size);
  315. gpcount += (size + 7) / 8;
  316. }
  317. break;
  318. default:
  319. abort ();
  320. }
  321. }
  322. ffi_call_unix (stack, rvalue, fn, cif->flags);
  323. }
  324. /* Closures represent a pair consisting of a function pointer, and
  325. some user data. A closure is invoked by reinterpreting the closure
  326. as a function pointer, and branching to it. Thus we can make an
  327. interpreted function callable as a C function: We turn the
  328. interpreter itself, together with a pointer specifying the
  329. interpreted procedure, into a closure.
  330. For IA64, function pointer are already pairs consisting of a code
  331. pointer, and a gp pointer. The latter is needed to access global
  332. variables. Here we set up such a pair as the first two words of
  333. the closure (in the "trampoline" area), but we replace the gp
  334. pointer with a pointer to the closure itself. We also add the real
  335. gp pointer to the closure. This allows the function entry code to
  336. both retrieve the user data, and to restire the correct gp pointer. */
  337. extern void ffi_closure_unix ();
  338. ffi_status
  339. ffi_prep_closure_loc (ffi_closure* closure,
  340. ffi_cif* cif,
  341. void (*fun)(ffi_cif*,void*,void**,void*),
  342. void *user_data,
  343. void *codeloc)
  344. {
  345. /* The layout of a function descriptor. A C function pointer really
  346. points to one of these. */
  347. struct ia64_fd
  348. {
  349. UINT64 code_pointer;
  350. UINT64 gp;
  351. };
  352. struct ffi_ia64_trampoline_struct
  353. {
  354. UINT64 code_pointer; /* Pointer to ffi_closure_unix. */
  355. UINT64 fake_gp; /* Pointer to closure, installed as gp. */
  356. UINT64 real_gp; /* Real gp value. */
  357. };
  358. struct ffi_ia64_trampoline_struct *tramp;
  359. struct ia64_fd *fd;
  360. FFI_ASSERT (cif->abi == FFI_UNIX);
  361. tramp = (struct ffi_ia64_trampoline_struct *)closure->tramp;
  362. fd = (struct ia64_fd *)(void *)ffi_closure_unix;
  363. tramp->code_pointer = fd->code_pointer;
  364. tramp->real_gp = fd->gp;
  365. tramp->fake_gp = (UINT64)(PTR64)codeloc;
  366. closure->cif = cif;
  367. closure->user_data = user_data;
  368. closure->fun = fun;
  369. return FFI_OK;
  370. }
  371. UINT64
  372. ffi_closure_unix_inner (ffi_closure *closure, struct ia64_args *stack,
  373. void *rvalue, void *r8)
  374. {
  375. ffi_cif *cif;
  376. void **avalue;
  377. ffi_type **p_arg;
  378. long i, avn, gpcount, fpcount;
  379. cif = closure->cif;
  380. avn = cif->nargs;
  381. avalue = alloca (avn * sizeof (void *));
  382. /* If the structure return value is passed in memory get that location
  383. from r8 so as to pass the value directly back to the caller. */
  384. if (cif->flags == FFI_TYPE_STRUCT)
  385. rvalue = r8;
  386. gpcount = fpcount = 0;
  387. for (i = 0, p_arg = cif->arg_types; i < avn; i++, p_arg++)
  388. {
  389. switch ((*p_arg)->type)
  390. {
  391. case FFI_TYPE_SINT8:
  392. case FFI_TYPE_UINT8:
  393. avalue[i] = endian_adjust(&stack->gp_regs[gpcount++], 1);
  394. break;
  395. case FFI_TYPE_SINT16:
  396. case FFI_TYPE_UINT16:
  397. avalue[i] = endian_adjust(&stack->gp_regs[gpcount++], 2);
  398. break;
  399. case FFI_TYPE_SINT32:
  400. case FFI_TYPE_UINT32:
  401. avalue[i] = endian_adjust(&stack->gp_regs[gpcount++], 4);
  402. break;
  403. case FFI_TYPE_SINT64:
  404. case FFI_TYPE_UINT64:
  405. avalue[i] = &stack->gp_regs[gpcount++];
  406. break;
  407. case FFI_TYPE_POINTER:
  408. avalue[i] = endian_adjust(&stack->gp_regs[gpcount++], sizeof(void*));
  409. break;
  410. case FFI_TYPE_FLOAT:
  411. if (gpcount < 8 && fpcount < 8)
  412. {
  413. fpreg *addr = &stack->fp_regs[fpcount++];
  414. float result;
  415. avalue[i] = addr;
  416. ldf_fill (result, addr);
  417. *(float *)addr = result;
  418. }
  419. else
  420. avalue[i] = endian_adjust(&stack->gp_regs[gpcount], 4);
  421. gpcount++;
  422. break;
  423. case FFI_TYPE_DOUBLE:
  424. if (gpcount < 8 && fpcount < 8)
  425. {
  426. fpreg *addr = &stack->fp_regs[fpcount++];
  427. double result;
  428. avalue[i] = addr;
  429. ldf_fill (result, addr);
  430. *(double *)addr = result;
  431. }
  432. else
  433. avalue[i] = &stack->gp_regs[gpcount];
  434. gpcount++;
  435. break;
  436. case FFI_TYPE_LONGDOUBLE:
  437. if (gpcount & 1)
  438. gpcount++;
  439. if (LDBL_MANT_DIG == 64 && gpcount < 8 && fpcount < 8)
  440. {
  441. fpreg *addr = &stack->fp_regs[fpcount++];
  442. __float80 result;
  443. avalue[i] = addr;
  444. ldf_fill (result, addr);
  445. *(__float80 *)addr = result;
  446. }
  447. else
  448. avalue[i] = &stack->gp_regs[gpcount];
  449. gpcount += 2;
  450. break;
  451. case FFI_TYPE_STRUCT:
  452. {
  453. size_t size = (*p_arg)->size;
  454. size_t align = (*p_arg)->alignment;
  455. int hfa_type = hfa_element_type (*p_arg, 0);
  456. FFI_ASSERT (align <= 16);
  457. if (align == 16 && (gpcount & 1))
  458. gpcount++;
  459. if (hfa_type != FFI_TYPE_VOID)
  460. {
  461. size_t hfa_size = hfa_type_size (hfa_type);
  462. size_t offset = 0;
  463. size_t gp_offset = gpcount * 8;
  464. void *addr = alloca (size);
  465. avalue[i] = addr;
  466. while (fpcount < 8
  467. && offset < size
  468. && gp_offset < 8 * 8)
  469. {
  470. hfa_type_store (hfa_type, addr + offset,
  471. &stack->fp_regs[fpcount]);
  472. offset += hfa_size;
  473. gp_offset += hfa_size;
  474. fpcount += 1;
  475. }
  476. if (offset < size)
  477. memcpy (addr + offset, (char *)stack->gp_regs + gp_offset,
  478. size - offset);
  479. }
  480. else
  481. avalue[i] = &stack->gp_regs[gpcount];
  482. gpcount += (size + 7) / 8;
  483. }
  484. break;
  485. default:
  486. abort ();
  487. }
  488. }
  489. closure->fun (cif, rvalue, avalue, closure->user_data);
  490. return cif->flags;
  491. }