cls_20byte.c 2.5 KB

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