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

http://unladen-swallow.googlecode.com/ · C · 780 lines · 484 code · 125 blank · 171 comment · 84 complexity · b03c7644ee96611457e7eed78b58a9c0 MD5 · raw file

  1. /* -----------------------------------------------------------------------
  2. ffi.c - Copyright (c) 2000, 2007 Software AG
  3. Copyright (c) 2008 Red Hat, Inc
  4. S390 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, EXPRESS
  15. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. ----------------------------------------------------------------------- */
  22. /*====================================================================*/
  23. /* Includes */
  24. /* -------- */
  25. /*====================================================================*/
  26. #include <ffi.h>
  27. #include <ffi_common.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. /*====================== End of Includes =============================*/
  31. /*====================================================================*/
  32. /* Defines */
  33. /* ------- */
  34. /*====================================================================*/
  35. /* Maximum number of GPRs available for argument passing. */
  36. #define MAX_GPRARGS 5
  37. /* Maximum number of FPRs available for argument passing. */
  38. #ifdef __s390x__
  39. #define MAX_FPRARGS 4
  40. #else
  41. #define MAX_FPRARGS 2
  42. #endif
  43. /* Round to multiple of 16. */
  44. #define ROUND_SIZE(size) (((size) + 15) & ~15)
  45. /* If these values change, sysv.S must be adapted! */
  46. #define FFI390_RET_VOID 0
  47. #define FFI390_RET_STRUCT 1
  48. #define FFI390_RET_FLOAT 2
  49. #define FFI390_RET_DOUBLE 3
  50. #define FFI390_RET_INT32 4
  51. #define FFI390_RET_INT64 5
  52. /*===================== End of Defines ===============================*/
  53. /*====================================================================*/
  54. /* Prototypes */
  55. /* ---------- */
  56. /*====================================================================*/
  57. static void ffi_prep_args (unsigned char *, extended_cif *);
  58. void
  59. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
  60. __attribute__ ((visibility ("hidden")))
  61. #endif
  62. ffi_closure_helper_SYSV (ffi_closure *, unsigned long *,
  63. unsigned long long *, unsigned long *);
  64. /*====================== End of Prototypes ===========================*/
  65. /*====================================================================*/
  66. /* Externals */
  67. /* --------- */
  68. /*====================================================================*/
  69. extern void ffi_call_SYSV(unsigned,
  70. extended_cif *,
  71. void (*)(unsigned char *, extended_cif *),
  72. unsigned,
  73. void *,
  74. void (*fn)(void));
  75. extern void ffi_closure_SYSV(void);
  76. /*====================== End of Externals ============================*/
  77. /*====================================================================*/
  78. /* */
  79. /* Name - ffi_check_struct_type. */
  80. /* */
  81. /* Function - Determine if a structure can be passed within a */
  82. /* general purpose or floating point register. */
  83. /* */
  84. /*====================================================================*/
  85. static int
  86. ffi_check_struct_type (ffi_type *arg)
  87. {
  88. size_t size = arg->size;
  89. /* If the struct has just one element, look at that element
  90. to find out whether to consider the struct as floating point. */
  91. while (arg->type == FFI_TYPE_STRUCT
  92. && arg->elements[0] && !arg->elements[1])
  93. arg = arg->elements[0];
  94. /* Structs of size 1, 2, 4, and 8 are passed in registers,
  95. just like the corresponding int/float types. */
  96. switch (size)
  97. {
  98. case 1:
  99. return FFI_TYPE_UINT8;
  100. case 2:
  101. return FFI_TYPE_UINT16;
  102. case 4:
  103. if (arg->type == FFI_TYPE_FLOAT)
  104. return FFI_TYPE_FLOAT;
  105. else
  106. return FFI_TYPE_UINT32;
  107. case 8:
  108. if (arg->type == FFI_TYPE_DOUBLE)
  109. return FFI_TYPE_DOUBLE;
  110. else
  111. return FFI_TYPE_UINT64;
  112. default:
  113. break;
  114. }
  115. /* Other structs are passed via a pointer to the data. */
  116. return FFI_TYPE_POINTER;
  117. }
  118. /*======================== End of Routine ============================*/
  119. /*====================================================================*/
  120. /* */
  121. /* Name - ffi_prep_args. */
  122. /* */
  123. /* Function - Prepare parameters for call to function. */
  124. /* */
  125. /* ffi_prep_args is called by the assembly routine once stack space */
  126. /* has been allocated for the function's arguments. */
  127. /* */
  128. /*====================================================================*/
  129. static void
  130. ffi_prep_args (unsigned char *stack, extended_cif *ecif)
  131. {
  132. /* The stack space will be filled with those areas:
  133. FPR argument register save area (highest addresses)
  134. GPR argument register save area
  135. temporary struct copies
  136. overflow argument area (lowest addresses)
  137. We set up the following pointers:
  138. p_fpr: bottom of the FPR area (growing upwards)
  139. p_gpr: bottom of the GPR area (growing upwards)
  140. p_ov: bottom of the overflow area (growing upwards)
  141. p_struct: top of the struct copy area (growing downwards)
  142. All areas are kept aligned to twice the word size. */
  143. int gpr_off = ecif->cif->bytes;
  144. int fpr_off = gpr_off + ROUND_SIZE (MAX_GPRARGS * sizeof (long));
  145. unsigned long long *p_fpr = (unsigned long long *)(stack + fpr_off);
  146. unsigned long *p_gpr = (unsigned long *)(stack + gpr_off);
  147. unsigned char *p_struct = (unsigned char *)p_gpr;
  148. unsigned long *p_ov = (unsigned long *)stack;
  149. int n_fpr = 0;
  150. int n_gpr = 0;
  151. int n_ov = 0;
  152. ffi_type **ptr;
  153. void **p_argv = ecif->avalue;
  154. int i;
  155. /* If we returning a structure then we set the first parameter register
  156. to the address of where we are returning this structure. */
  157. if (ecif->cif->flags == FFI390_RET_STRUCT)
  158. p_gpr[n_gpr++] = (unsigned long) ecif->rvalue;
  159. /* Now for the arguments. */
  160. for (ptr = ecif->cif->arg_types, i = ecif->cif->nargs;
  161. i > 0;
  162. i--, ptr++, p_argv++)
  163. {
  164. void *arg = *p_argv;
  165. int type = (*ptr)->type;
  166. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  167. /* 16-byte long double is passed like a struct. */
  168. if (type == FFI_TYPE_LONGDOUBLE)
  169. type = FFI_TYPE_STRUCT;
  170. #endif
  171. /* Check how a structure type is passed. */
  172. if (type == FFI_TYPE_STRUCT)
  173. {
  174. type = ffi_check_struct_type (*ptr);
  175. /* If we pass the struct via pointer, copy the data. */
  176. if (type == FFI_TYPE_POINTER)
  177. {
  178. p_struct -= ROUND_SIZE ((*ptr)->size);
  179. memcpy (p_struct, (char *)arg, (*ptr)->size);
  180. arg = &p_struct;
  181. }
  182. }
  183. /* Now handle all primitive int/pointer/float data types. */
  184. switch (type)
  185. {
  186. case FFI_TYPE_DOUBLE:
  187. if (n_fpr < MAX_FPRARGS)
  188. p_fpr[n_fpr++] = *(unsigned long long *) arg;
  189. else
  190. #ifdef __s390x__
  191. p_ov[n_ov++] = *(unsigned long *) arg;
  192. #else
  193. p_ov[n_ov++] = ((unsigned long *) arg)[0],
  194. p_ov[n_ov++] = ((unsigned long *) arg)[1];
  195. #endif
  196. break;
  197. case FFI_TYPE_FLOAT:
  198. if (n_fpr < MAX_FPRARGS)
  199. p_fpr[n_fpr++] = (long long) *(unsigned int *) arg << 32;
  200. else
  201. p_ov[n_ov++] = *(unsigned int *) arg;
  202. break;
  203. case FFI_TYPE_POINTER:
  204. if (n_gpr < MAX_GPRARGS)
  205. p_gpr[n_gpr++] = (unsigned long)*(unsigned char **) arg;
  206. else
  207. p_ov[n_ov++] = (unsigned long)*(unsigned char **) arg;
  208. break;
  209. case FFI_TYPE_UINT64:
  210. case FFI_TYPE_SINT64:
  211. #ifdef __s390x__
  212. if (n_gpr < MAX_GPRARGS)
  213. p_gpr[n_gpr++] = *(unsigned long *) arg;
  214. else
  215. p_ov[n_ov++] = *(unsigned long *) arg;
  216. #else
  217. if (n_gpr == MAX_GPRARGS-1)
  218. n_gpr = MAX_GPRARGS;
  219. if (n_gpr < MAX_GPRARGS)
  220. p_gpr[n_gpr++] = ((unsigned long *) arg)[0],
  221. p_gpr[n_gpr++] = ((unsigned long *) arg)[1];
  222. else
  223. p_ov[n_ov++] = ((unsigned long *) arg)[0],
  224. p_ov[n_ov++] = ((unsigned long *) arg)[1];
  225. #endif
  226. break;
  227. case FFI_TYPE_UINT32:
  228. if (n_gpr < MAX_GPRARGS)
  229. p_gpr[n_gpr++] = *(unsigned int *) arg;
  230. else
  231. p_ov[n_ov++] = *(unsigned int *) arg;
  232. break;
  233. case FFI_TYPE_INT:
  234. case FFI_TYPE_SINT32:
  235. if (n_gpr < MAX_GPRARGS)
  236. p_gpr[n_gpr++] = *(signed int *) arg;
  237. else
  238. p_ov[n_ov++] = *(signed int *) arg;
  239. break;
  240. case FFI_TYPE_UINT16:
  241. if (n_gpr < MAX_GPRARGS)
  242. p_gpr[n_gpr++] = *(unsigned short *) arg;
  243. else
  244. p_ov[n_ov++] = *(unsigned short *) arg;
  245. break;
  246. case FFI_TYPE_SINT16:
  247. if (n_gpr < MAX_GPRARGS)
  248. p_gpr[n_gpr++] = *(signed short *) arg;
  249. else
  250. p_ov[n_ov++] = *(signed short *) arg;
  251. break;
  252. case FFI_TYPE_UINT8:
  253. if (n_gpr < MAX_GPRARGS)
  254. p_gpr[n_gpr++] = *(unsigned char *) arg;
  255. else
  256. p_ov[n_ov++] = *(unsigned char *) arg;
  257. break;
  258. case FFI_TYPE_SINT8:
  259. if (n_gpr < MAX_GPRARGS)
  260. p_gpr[n_gpr++] = *(signed char *) arg;
  261. else
  262. p_ov[n_ov++] = *(signed char *) arg;
  263. break;
  264. default:
  265. FFI_ASSERT (0);
  266. break;
  267. }
  268. }
  269. }
  270. /*======================== End of Routine ============================*/
  271. /*====================================================================*/
  272. /* */
  273. /* Name - ffi_prep_cif_machdep. */
  274. /* */
  275. /* Function - Perform machine dependent CIF processing. */
  276. /* */
  277. /*====================================================================*/
  278. ffi_status
  279. ffi_prep_cif_machdep(ffi_cif *cif)
  280. {
  281. size_t struct_size = 0;
  282. int n_gpr = 0;
  283. int n_fpr = 0;
  284. int n_ov = 0;
  285. ffi_type **ptr;
  286. int i;
  287. /* Determine return value handling. */
  288. switch (cif->rtype->type)
  289. {
  290. /* Void is easy. */
  291. case FFI_TYPE_VOID:
  292. cif->flags = FFI390_RET_VOID;
  293. break;
  294. /* Structures are returned via a hidden pointer. */
  295. case FFI_TYPE_STRUCT:
  296. cif->flags = FFI390_RET_STRUCT;
  297. n_gpr++; /* We need one GPR to pass the pointer. */
  298. break;
  299. /* Floating point values are returned in fpr 0. */
  300. case FFI_TYPE_FLOAT:
  301. cif->flags = FFI390_RET_FLOAT;
  302. break;
  303. case FFI_TYPE_DOUBLE:
  304. cif->flags = FFI390_RET_DOUBLE;
  305. break;
  306. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  307. case FFI_TYPE_LONGDOUBLE:
  308. cif->flags = FFI390_RET_STRUCT;
  309. n_gpr++;
  310. break;
  311. #endif
  312. /* Integer values are returned in gpr 2 (and gpr 3
  313. for 64-bit values on 31-bit machines). */
  314. case FFI_TYPE_UINT64:
  315. case FFI_TYPE_SINT64:
  316. cif->flags = FFI390_RET_INT64;
  317. break;
  318. case FFI_TYPE_POINTER:
  319. case FFI_TYPE_INT:
  320. case FFI_TYPE_UINT32:
  321. case FFI_TYPE_SINT32:
  322. case FFI_TYPE_UINT16:
  323. case FFI_TYPE_SINT16:
  324. case FFI_TYPE_UINT8:
  325. case FFI_TYPE_SINT8:
  326. /* These are to be extended to word size. */
  327. #ifdef __s390x__
  328. cif->flags = FFI390_RET_INT64;
  329. #else
  330. cif->flags = FFI390_RET_INT32;
  331. #endif
  332. break;
  333. default:
  334. FFI_ASSERT (0);
  335. break;
  336. }
  337. /* Now for the arguments. */
  338. for (ptr = cif->arg_types, i = cif->nargs;
  339. i > 0;
  340. i--, ptr++)
  341. {
  342. int type = (*ptr)->type;
  343. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  344. /* 16-byte long double is passed like a struct. */
  345. if (type == FFI_TYPE_LONGDOUBLE)
  346. type = FFI_TYPE_STRUCT;
  347. #endif
  348. /* Check how a structure type is passed. */
  349. if (type == FFI_TYPE_STRUCT)
  350. {
  351. type = ffi_check_struct_type (*ptr);
  352. /* If we pass the struct via pointer, we must reserve space
  353. to copy its data for proper call-by-value semantics. */
  354. if (type == FFI_TYPE_POINTER)
  355. struct_size += ROUND_SIZE ((*ptr)->size);
  356. }
  357. /* Now handle all primitive int/float data types. */
  358. switch (type)
  359. {
  360. /* The first MAX_FPRARGS floating point arguments
  361. go in FPRs, the rest overflow to the stack. */
  362. case FFI_TYPE_DOUBLE:
  363. if (n_fpr < MAX_FPRARGS)
  364. n_fpr++;
  365. else
  366. n_ov += sizeof (double) / sizeof (long);
  367. break;
  368. case FFI_TYPE_FLOAT:
  369. if (n_fpr < MAX_FPRARGS)
  370. n_fpr++;
  371. else
  372. n_ov++;
  373. break;
  374. /* On 31-bit machines, 64-bit integers are passed in GPR pairs,
  375. if one is still available, or else on the stack. If only one
  376. register is free, skip the register (it won't be used for any
  377. subsequent argument either). */
  378. #ifndef __s390x__
  379. case FFI_TYPE_UINT64:
  380. case FFI_TYPE_SINT64:
  381. if (n_gpr == MAX_GPRARGS-1)
  382. n_gpr = MAX_GPRARGS;
  383. if (n_gpr < MAX_GPRARGS)
  384. n_gpr += 2;
  385. else
  386. n_ov += 2;
  387. break;
  388. #endif
  389. /* Everything else is passed in GPRs (until MAX_GPRARGS
  390. have been used) or overflows to the stack. */
  391. default:
  392. if (n_gpr < MAX_GPRARGS)
  393. n_gpr++;
  394. else
  395. n_ov++;
  396. break;
  397. }
  398. }
  399. /* Total stack space as required for overflow arguments
  400. and temporary structure copies. */
  401. cif->bytes = ROUND_SIZE (n_ov * sizeof (long)) + struct_size;
  402. return FFI_OK;
  403. }
  404. /*======================== End of Routine ============================*/
  405. /*====================================================================*/
  406. /* */
  407. /* Name - ffi_call. */
  408. /* */
  409. /* Function - Call the FFI routine. */
  410. /* */
  411. /*====================================================================*/
  412. void
  413. ffi_call(ffi_cif *cif,
  414. void (*fn)(void),
  415. void *rvalue,
  416. void **avalue)
  417. {
  418. int ret_type = cif->flags;
  419. extended_cif ecif;
  420. ecif.cif = cif;
  421. ecif.avalue = avalue;
  422. ecif.rvalue = rvalue;
  423. /* If we don't have a return value, we need to fake one. */
  424. if (rvalue == NULL)
  425. {
  426. if (ret_type == FFI390_RET_STRUCT)
  427. ecif.rvalue = alloca (cif->rtype->size);
  428. else
  429. ret_type = FFI390_RET_VOID;
  430. }
  431. switch (cif->abi)
  432. {
  433. case FFI_SYSV:
  434. ffi_call_SYSV (cif->bytes, &ecif, ffi_prep_args,
  435. ret_type, ecif.rvalue, fn);
  436. break;
  437. default:
  438. FFI_ASSERT (0);
  439. break;
  440. }
  441. }
  442. /*======================== End of Routine ============================*/
  443. /*====================================================================*/
  444. /* */
  445. /* Name - ffi_closure_helper_SYSV. */
  446. /* */
  447. /* Function - Call a FFI closure target function. */
  448. /* */
  449. /*====================================================================*/
  450. void
  451. ffi_closure_helper_SYSV (ffi_closure *closure,
  452. unsigned long *p_gpr,
  453. unsigned long long *p_fpr,
  454. unsigned long *p_ov)
  455. {
  456. unsigned long long ret_buffer;
  457. void *rvalue = &ret_buffer;
  458. void **avalue;
  459. void **p_arg;
  460. int n_gpr = 0;
  461. int n_fpr = 0;
  462. int n_ov = 0;
  463. ffi_type **ptr;
  464. int i;
  465. /* Allocate buffer for argument list pointers. */
  466. p_arg = avalue = alloca (closure->cif->nargs * sizeof (void *));
  467. /* If we returning a structure, pass the structure address
  468. directly to the target function. Otherwise, have the target
  469. function store the return value to the GPR save area. */
  470. if (closure->cif->flags == FFI390_RET_STRUCT)
  471. rvalue = (void *) p_gpr[n_gpr++];
  472. /* Now for the arguments. */
  473. for (ptr = closure->cif->arg_types, i = closure->cif->nargs;
  474. i > 0;
  475. i--, p_arg++, ptr++)
  476. {
  477. int deref_struct_pointer = 0;
  478. int type = (*ptr)->type;
  479. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  480. /* 16-byte long double is passed like a struct. */
  481. if (type == FFI_TYPE_LONGDOUBLE)
  482. type = FFI_TYPE_STRUCT;
  483. #endif
  484. /* Check how a structure type is passed. */
  485. if (type == FFI_TYPE_STRUCT)
  486. {
  487. type = ffi_check_struct_type (*ptr);
  488. /* If we pass the struct via pointer, remember to
  489. retrieve the pointer later. */
  490. if (type == FFI_TYPE_POINTER)
  491. deref_struct_pointer = 1;
  492. }
  493. /* Pointers are passed like UINTs of the same size. */
  494. if (type == FFI_TYPE_POINTER)
  495. #ifdef __s390x__
  496. type = FFI_TYPE_UINT64;
  497. #else
  498. type = FFI_TYPE_UINT32;
  499. #endif
  500. /* Now handle all primitive int/float data types. */
  501. switch (type)
  502. {
  503. case FFI_TYPE_DOUBLE:
  504. if (n_fpr < MAX_FPRARGS)
  505. *p_arg = &p_fpr[n_fpr++];
  506. else
  507. *p_arg = &p_ov[n_ov],
  508. n_ov += sizeof (double) / sizeof (long);
  509. break;
  510. case FFI_TYPE_FLOAT:
  511. if (n_fpr < MAX_FPRARGS)
  512. *p_arg = &p_fpr[n_fpr++];
  513. else
  514. *p_arg = (char *)&p_ov[n_ov++] + sizeof (long) - 4;
  515. break;
  516. case FFI_TYPE_UINT64:
  517. case FFI_TYPE_SINT64:
  518. #ifdef __s390x__
  519. if (n_gpr < MAX_GPRARGS)
  520. *p_arg = &p_gpr[n_gpr++];
  521. else
  522. *p_arg = &p_ov[n_ov++];
  523. #else
  524. if (n_gpr == MAX_GPRARGS-1)
  525. n_gpr = MAX_GPRARGS;
  526. if (n_gpr < MAX_GPRARGS)
  527. *p_arg = &p_gpr[n_gpr], n_gpr += 2;
  528. else
  529. *p_arg = &p_ov[n_ov], n_ov += 2;
  530. #endif
  531. break;
  532. case FFI_TYPE_INT:
  533. case FFI_TYPE_UINT32:
  534. case FFI_TYPE_SINT32:
  535. if (n_gpr < MAX_GPRARGS)
  536. *p_arg = (char *)&p_gpr[n_gpr++] + sizeof (long) - 4;
  537. else
  538. *p_arg = (char *)&p_ov[n_ov++] + sizeof (long) - 4;
  539. break;
  540. case FFI_TYPE_UINT16:
  541. case FFI_TYPE_SINT16:
  542. if (n_gpr < MAX_GPRARGS)
  543. *p_arg = (char *)&p_gpr[n_gpr++] + sizeof (long) - 2;
  544. else
  545. *p_arg = (char *)&p_ov[n_ov++] + sizeof (long) - 2;
  546. break;
  547. case FFI_TYPE_UINT8:
  548. case FFI_TYPE_SINT8:
  549. if (n_gpr < MAX_GPRARGS)
  550. *p_arg = (char *)&p_gpr[n_gpr++] + sizeof (long) - 1;
  551. else
  552. *p_arg = (char *)&p_ov[n_ov++] + sizeof (long) - 1;
  553. break;
  554. default:
  555. FFI_ASSERT (0);
  556. break;
  557. }
  558. /* If this is a struct passed via pointer, we need to
  559. actually retrieve that pointer. */
  560. if (deref_struct_pointer)
  561. *p_arg = *(void **)*p_arg;
  562. }
  563. /* Call the target function. */
  564. (closure->fun) (closure->cif, rvalue, avalue, closure->user_data);
  565. /* Convert the return value. */
  566. switch (closure->cif->rtype->type)
  567. {
  568. /* Void is easy, and so is struct. */
  569. case FFI_TYPE_VOID:
  570. case FFI_TYPE_STRUCT:
  571. #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
  572. case FFI_TYPE_LONGDOUBLE:
  573. #endif
  574. break;
  575. /* Floating point values are returned in fpr 0. */
  576. case FFI_TYPE_FLOAT:
  577. p_fpr[0] = (long long) *(unsigned int *) rvalue << 32;
  578. break;
  579. case FFI_TYPE_DOUBLE:
  580. p_fpr[0] = *(unsigned long long *) rvalue;
  581. break;
  582. /* Integer values are returned in gpr 2 (and gpr 3
  583. for 64-bit values on 31-bit machines). */
  584. case FFI_TYPE_UINT64:
  585. case FFI_TYPE_SINT64:
  586. #ifdef __s390x__
  587. p_gpr[0] = *(unsigned long *) rvalue;
  588. #else
  589. p_gpr[0] = ((unsigned long *) rvalue)[0],
  590. p_gpr[1] = ((unsigned long *) rvalue)[1];
  591. #endif
  592. break;
  593. case FFI_TYPE_POINTER:
  594. case FFI_TYPE_UINT32:
  595. case FFI_TYPE_UINT16:
  596. case FFI_TYPE_UINT8:
  597. p_gpr[0] = *(unsigned long *) rvalue;
  598. break;
  599. case FFI_TYPE_INT:
  600. case FFI_TYPE_SINT32:
  601. case FFI_TYPE_SINT16:
  602. case FFI_TYPE_SINT8:
  603. p_gpr[0] = *(signed long *) rvalue;
  604. break;
  605. default:
  606. FFI_ASSERT (0);
  607. break;
  608. }
  609. }
  610. /*======================== End of Routine ============================*/
  611. /*====================================================================*/
  612. /* */
  613. /* Name - ffi_prep_closure_loc. */
  614. /* */
  615. /* Function - Prepare a FFI closure. */
  616. /* */
  617. /*====================================================================*/
  618. ffi_status
  619. ffi_prep_closure_loc (ffi_closure *closure,
  620. ffi_cif *cif,
  621. void (*fun) (ffi_cif *, void *, void **, void *),
  622. void *user_data,
  623. void *codeloc)
  624. {
  625. FFI_ASSERT (cif->abi == FFI_SYSV);
  626. #ifndef __s390x__
  627. *(short *)&closure->tramp [0] = 0x0d10; /* basr %r1,0 */
  628. *(short *)&closure->tramp [2] = 0x9801; /* lm %r0,%r1,6(%r1) */
  629. *(short *)&closure->tramp [4] = 0x1006;
  630. *(short *)&closure->tramp [6] = 0x07f1; /* br %r1 */
  631. *(long *)&closure->tramp [8] = (long)codeloc;
  632. *(long *)&closure->tramp[12] = (long)&ffi_closure_SYSV;
  633. #else
  634. *(short *)&closure->tramp [0] = 0x0d10; /* basr %r1,0 */
  635. *(short *)&closure->tramp [2] = 0xeb01; /* lmg %r0,%r1,14(%r1) */
  636. *(short *)&closure->tramp [4] = 0x100e;
  637. *(short *)&closure->tramp [6] = 0x0004;
  638. *(short *)&closure->tramp [8] = 0x07f1; /* br %r1 */
  639. *(long *)&closure->tramp[16] = (long)codeloc;
  640. *(long *)&closure->tramp[24] = (long)&ffi_closure_SYSV;
  641. #endif
  642. closure->cif = cif;
  643. closure->user_data = user_data;
  644. closure->fun = fun;
  645. return FFI_OK;
  646. }
  647. /*======================== End of Routine ============================*/