cls_8byte.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Area: ffi_call, closure_call
  2. Purpose: Check structure passing with different structure size.
  3. Depending on the ABI. Check overlapping.
  4. Limitations: none.
  5. PR: none.
  6. Originator: <andreast@gcc.gnu.org> 20030828 */
  7. /* { dg-do run } */
  8. #include "ffitest.h"
  9. typedef struct cls_struct_8byte {
  10. int a;
  11. float b;
  12. } cls_struct_8byte;
  13. cls_struct_8byte cls_struct_8byte_fn(struct cls_struct_8byte a1,
  14. struct cls_struct_8byte a2)
  15. {
  16. struct cls_struct_8byte result;
  17. result.a = a1.a + a2.a;
  18. result.b = a1.b + a2.b;
  19. printf("%d %g %d %g: %d %g\n", a1.a, a1.b, a2.a, a2.b, result.a, result.b);
  20. return result;
  21. }
  22. static void
  23. cls_struct_8byte_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
  24. void* userdata __UNUSED__)
  25. {
  26. struct cls_struct_8byte a1, a2;
  27. a1 = *(struct cls_struct_8byte*)(args[0]);
  28. a2 = *(struct cls_struct_8byte*)(args[1]);
  29. *(cls_struct_8byte*)resp = cls_struct_8byte_fn(a1, a2);
  30. }
  31. int main (void)
  32. {
  33. ffi_cif cif;
  34. void *code;
  35. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  36. void* args_dbl[5];
  37. ffi_type* cls_struct_fields[4];
  38. ffi_type cls_struct_type;
  39. ffi_type* dbl_arg_types[5];
  40. struct cls_struct_8byte g_dbl = { 1, 2.0 };
  41. struct cls_struct_8byte f_dbl = { 4, 5.0 };
  42. struct cls_struct_8byte res_dbl;
  43. cls_struct_type.size = 0;
  44. cls_struct_type.alignment = 0;
  45. cls_struct_type.type = FFI_TYPE_STRUCT;
  46. cls_struct_type.elements = cls_struct_fields;
  47. cls_struct_fields[0] = &ffi_type_sint;
  48. cls_struct_fields[1] = &ffi_type_float;
  49. cls_struct_fields[2] = NULL;
  50. dbl_arg_types[0] = &cls_struct_type;
  51. dbl_arg_types[1] = &cls_struct_type;
  52. dbl_arg_types[2] = NULL;
  53. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
  54. dbl_arg_types) == FFI_OK);
  55. args_dbl[0] = &g_dbl;
  56. args_dbl[1] = &f_dbl;
  57. args_dbl[2] = NULL;
  58. ffi_call(&cif, FFI_FN(cls_struct_8byte_fn), &res_dbl, args_dbl);
  59. /* { dg-output "1 2 4 5: 5 7" } */
  60. printf("res: %d %g\n", res_dbl.a, res_dbl.b);
  61. /* { dg-output "\nres: 5 7" } */
  62. CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_8byte_gn, NULL, code) == FFI_OK);
  63. res_dbl = ((cls_struct_8byte(*)(cls_struct_8byte, cls_struct_8byte))(code))(g_dbl, f_dbl);
  64. /* { dg-output "\n1 2 4 5: 5 7" } */
  65. printf("res: %d %g\n", res_dbl.a, res_dbl.b);
  66. /* { dg-output "\nres: 5 7" } */
  67. exit(0);
  68. }