nested_struct4.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Area: ffi_call, closure_call
  2. Purpose: Check structure passing with different structure size.
  3. Contains structs as parameter of the struct itself.
  4. Sample taken from Alan Modras patch to src/prep_cif.c.
  5. Limitations: none.
  6. PR: PR 25630.
  7. Originator: <andreast@gcc.gnu.org> 20051010 */
  8. /* { dg-do run } */
  9. #include "ffitest.h"
  10. typedef struct A {
  11. double a;
  12. unsigned char b;
  13. } A;
  14. typedef struct B {
  15. struct A x;
  16. unsigned char y;
  17. } B;
  18. static B B_fn(struct A b2, struct B b3)
  19. {
  20. struct B result;
  21. result.x.a = b2.a + b3.x.a;
  22. result.x.b = b2.b + b3.x.b + b3.y;
  23. result.y = b2.b + b3.x.b;
  24. printf("%d %d %d %d %d: %d %d %d\n", (int)b2.a, b2.b,
  25. (int)b3.x.a, b3.x.b, b3.y,
  26. (int)result.x.a, result.x.b, result.y);
  27. return result;
  28. }
  29. static void
  30. B_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
  31. void* userdata __UNUSED__)
  32. {
  33. struct A b0;
  34. struct B b1;
  35. b0 = *(struct A*)(args[0]);
  36. b1 = *(struct B*)(args[1]);
  37. *(B*)resp = B_fn(b0, b1);
  38. }
  39. int main (void)
  40. {
  41. ffi_cif cif;
  42. void *code;
  43. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  44. void* args_dbl[3];
  45. ffi_type* cls_struct_fields[3];
  46. ffi_type* cls_struct_fields1[3];
  47. ffi_type cls_struct_type, cls_struct_type1;
  48. ffi_type* dbl_arg_types[3];
  49. struct A e_dbl = { 1.0, 7};
  50. struct B f_dbl = {{12.0 , 127}, 99};
  51. struct B res_dbl;
  52. cls_struct_type.size = 0;
  53. cls_struct_type.alignment = 0;
  54. cls_struct_type.type = FFI_TYPE_STRUCT;
  55. cls_struct_type.elements = cls_struct_fields;
  56. cls_struct_type1.size = 0;
  57. cls_struct_type1.alignment = 0;
  58. cls_struct_type1.type = FFI_TYPE_STRUCT;
  59. cls_struct_type1.elements = cls_struct_fields1;
  60. cls_struct_fields[0] = &ffi_type_double;
  61. cls_struct_fields[1] = &ffi_type_uchar;
  62. cls_struct_fields[2] = NULL;
  63. cls_struct_fields1[0] = &cls_struct_type;
  64. cls_struct_fields1[1] = &ffi_type_uchar;
  65. cls_struct_fields1[2] = NULL;
  66. dbl_arg_types[0] = &cls_struct_type;
  67. dbl_arg_types[1] = &cls_struct_type1;
  68. dbl_arg_types[2] = NULL;
  69. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type1,
  70. dbl_arg_types) == FFI_OK);
  71. args_dbl[0] = &e_dbl;
  72. args_dbl[1] = &f_dbl;
  73. args_dbl[2] = NULL;
  74. ffi_call(&cif, FFI_FN(B_fn), &res_dbl, args_dbl);
  75. /* { dg-output "1 7 12 127 99: 13 233 134" } */
  76. CHECK( res_dbl.x.a == (e_dbl.a + f_dbl.x.a));
  77. CHECK( res_dbl.x.b == (e_dbl.b + f_dbl.x.b + f_dbl.y));
  78. CHECK( res_dbl.y == (e_dbl.b + f_dbl.x.b));
  79. CHECK(ffi_prep_closure_loc(pcl, &cif, B_gn, NULL, code) == FFI_OK);
  80. res_dbl = ((B(*)(A, B))(code))(e_dbl, f_dbl);
  81. /* { dg-output "\n1 7 12 127 99: 13 233 134" } */
  82. CHECK( res_dbl.x.a == (e_dbl.a + f_dbl.x.a));
  83. CHECK( res_dbl.x.b == (e_dbl.b + f_dbl.x.b + f_dbl.y));
  84. CHECK( res_dbl.y == (e_dbl.b + f_dbl.x.b));
  85. exit(0);
  86. }