cls_16byte.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_16byte {
  10. int a;
  11. double b;
  12. int c;
  13. } cls_struct_16byte;
  14. cls_struct_16byte cls_struct_16byte_fn(struct cls_struct_16byte b1,
  15. struct cls_struct_16byte b2)
  16. {
  17. struct cls_struct_16byte result;
  18. result.a = b1.a + b2.a;
  19. result.b = b1.b + b2.b;
  20. result.c = b1.c + b2.c;
  21. printf("%d %g %d %d %g %d: %d %g %d\n", b1.a, b1.b, b1.c, b2.a, b2.b, b2.c,
  22. result.a, result.b, result.c);
  23. return result;
  24. }
  25. static void cls_struct_16byte_gn(ffi_cif* cif __UNUSED__, void* resp,
  26. void** args, void* userdata __UNUSED__)
  27. {
  28. struct cls_struct_16byte b1, b2;
  29. b1 = *(struct cls_struct_16byte*)(args[0]);
  30. b2 = *(struct cls_struct_16byte*)(args[1]);
  31. *(cls_struct_16byte*)resp = cls_struct_16byte_fn(b1, b2);
  32. }
  33. int main (void)
  34. {
  35. ffi_cif cif;
  36. void *code;
  37. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  38. void* args_dbl[5];
  39. ffi_type* cls_struct_fields[4];
  40. ffi_type cls_struct_type;
  41. ffi_type* dbl_arg_types[5];
  42. struct cls_struct_16byte h_dbl = { 7, 8.0, 9 };
  43. struct cls_struct_16byte j_dbl = { 1, 9.0, 3 };
  44. struct cls_struct_16byte res_dbl;
  45. cls_struct_type.size = 0;
  46. cls_struct_type.alignment = 0;
  47. cls_struct_type.type = FFI_TYPE_STRUCT;
  48. cls_struct_type.elements = cls_struct_fields;
  49. cls_struct_fields[0] = &ffi_type_sint;
  50. cls_struct_fields[1] = &ffi_type_double;
  51. cls_struct_fields[2] = &ffi_type_sint;
  52. cls_struct_fields[3] = NULL;
  53. dbl_arg_types[0] = &cls_struct_type;
  54. dbl_arg_types[1] = &cls_struct_type;
  55. dbl_arg_types[2] = NULL;
  56. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
  57. dbl_arg_types) == FFI_OK);
  58. args_dbl[0] = &h_dbl;
  59. args_dbl[1] = &j_dbl;
  60. args_dbl[2] = NULL;
  61. ffi_call(&cif, FFI_FN(cls_struct_16byte_fn), &res_dbl, args_dbl);
  62. /* { dg-output "7 8 9 1 9 3: 8 17 12" } */
  63. printf("res: %d %g %d\n", res_dbl.a, res_dbl.b, res_dbl.c);
  64. /* { dg-output "\nres: 8 17 12" } */
  65. res_dbl.a = 0;
  66. res_dbl.b = 0.0;
  67. res_dbl.c = 0;
  68. CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_16byte_gn, NULL, code) == FFI_OK);
  69. res_dbl = ((cls_struct_16byte(*)(cls_struct_16byte, cls_struct_16byte))(code))(h_dbl, j_dbl);
  70. /* { dg-output "\n7 8 9 1 9 3: 8 17 12" } */
  71. printf("res: %d %g %d\n", res_dbl.a, res_dbl.b, res_dbl.c);
  72. /* { dg-output "\nres: 8 17 12" } */
  73. exit(0);
  74. }