cls_20byte1.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. int a;
  11. double b;
  12. double 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("%d %g %g %d %g %g: %d %g %g\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[3];
  40. ffi_type* cls_struct_fields[4];
  41. ffi_type cls_struct_type;
  42. ffi_type* dbl_arg_types[3];
  43. struct cls_struct_20byte g_dbl = { 1, 2.0, 3.0 };
  44. struct cls_struct_20byte f_dbl = { 4, 5.0, 7.0 };
  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_sint;
  51. cls_struct_fields[1] = &ffi_type_double;
  52. cls_struct_fields[2] = &ffi_type_double;
  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: %d %g %g\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: %d %g %g\n", res_dbl.a, res_dbl.b, res_dbl.c);
  70. /* { dg-output "\nres: 5 7 10" } */
  71. exit(0);
  72. }