nested_struct9.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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: none.
  7. Originator: <andreast@gcc.gnu.org> 20051010 */
  8. /* { dg-do run } */
  9. #include "ffitest.h"
  10. typedef struct A {
  11. unsigned char a;
  12. unsigned long long b;
  13. } A;
  14. typedef struct B {
  15. struct A x;
  16. unsigned char y;
  17. } B;
  18. typedef struct C {
  19. unsigned long d;
  20. unsigned char e;
  21. } C;
  22. static B B_fn(struct A b2, struct B b3, struct C b4)
  23. {
  24. struct B result;
  25. result.x.a = b2.a + b3.x.a + b4.d;
  26. result.x.b = b2.b + b3.x.b + b3.y + b4.e;
  27. result.y = b2.b + b3.x.b + b4.e;
  28. printf("%d %d %d %d %d %d %d: %d %d %d\n", b2.a, (int)b2.b,
  29. b3.x.a, (int)b3.x.b, b3.y, (int)b4.d, b4.e,
  30. result.x.a, (int)result.x.b, result.y);
  31. return result;
  32. }
  33. static void
  34. B_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
  35. void* userdata __UNUSED__)
  36. {
  37. struct A b0;
  38. struct B b1;
  39. struct C b2;
  40. b0 = *(struct A*)(args[0]);
  41. b1 = *(struct B*)(args[1]);
  42. b2 = *(struct C*)(args[2]);
  43. *(B*)resp = B_fn(b0, b1, b2);
  44. }
  45. int main (void)
  46. {
  47. ffi_cif cif;
  48. void *code;
  49. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  50. void* args_dbl[4];
  51. ffi_type* cls_struct_fields[3];
  52. ffi_type* cls_struct_fields1[3];
  53. ffi_type* cls_struct_fields2[3];
  54. ffi_type cls_struct_type, cls_struct_type1, cls_struct_type2;
  55. ffi_type* dbl_arg_types[4];
  56. struct A e_dbl = { 1, 7LL};
  57. struct B f_dbl = {{12.0 , 127}, 99};
  58. struct C g_dbl = { 2, 9};
  59. struct B res_dbl;
  60. cls_struct_type.size = 0;
  61. cls_struct_type.alignment = 0;
  62. cls_struct_type.type = FFI_TYPE_STRUCT;
  63. cls_struct_type.elements = cls_struct_fields;
  64. cls_struct_type1.size = 0;
  65. cls_struct_type1.alignment = 0;
  66. cls_struct_type1.type = FFI_TYPE_STRUCT;
  67. cls_struct_type1.elements = cls_struct_fields1;
  68. cls_struct_type2.size = 0;
  69. cls_struct_type2.alignment = 0;
  70. cls_struct_type2.type = FFI_TYPE_STRUCT;
  71. cls_struct_type2.elements = cls_struct_fields2;
  72. cls_struct_fields[0] = &ffi_type_uchar;
  73. cls_struct_fields[1] = &ffi_type_uint64;
  74. cls_struct_fields[2] = NULL;
  75. cls_struct_fields1[0] = &cls_struct_type;
  76. cls_struct_fields1[1] = &ffi_type_uchar;
  77. cls_struct_fields1[2] = NULL;
  78. cls_struct_fields2[0] = &ffi_type_ulong;
  79. cls_struct_fields2[1] = &ffi_type_uchar;
  80. cls_struct_fields2[2] = NULL;
  81. dbl_arg_types[0] = &cls_struct_type;
  82. dbl_arg_types[1] = &cls_struct_type1;
  83. dbl_arg_types[2] = &cls_struct_type2;
  84. dbl_arg_types[3] = NULL;
  85. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 3, &cls_struct_type1,
  86. dbl_arg_types) == FFI_OK);
  87. args_dbl[0] = &e_dbl;
  88. args_dbl[1] = &f_dbl;
  89. args_dbl[2] = &g_dbl;
  90. args_dbl[3] = NULL;
  91. ffi_call(&cif, FFI_FN(B_fn), &res_dbl, args_dbl);
  92. /* { dg-output "1 7 12 127 99 2 9: 15 242 143" } */
  93. CHECK( res_dbl.x.a == (e_dbl.a + f_dbl.x.a + g_dbl.d));
  94. CHECK( res_dbl.x.b == (e_dbl.b + f_dbl.x.b + f_dbl.y + g_dbl.e));
  95. CHECK( res_dbl.y == (e_dbl.b + f_dbl.x.b + g_dbl.e));
  96. CHECK(ffi_prep_closure_loc(pcl, &cif, B_gn, NULL, code) == FFI_OK);
  97. res_dbl = ((B(*)(A, B, C))(code))(e_dbl, f_dbl, g_dbl);
  98. /* { dg-output "\n1 7 12 127 99 2 9: 15 242 143" } */
  99. CHECK( res_dbl.x.a == (e_dbl.a + f_dbl.x.a + g_dbl.d));
  100. CHECK( res_dbl.x.b == (e_dbl.b + f_dbl.x.b + f_dbl.y + g_dbl.e));
  101. CHECK( res_dbl.y == (e_dbl.b + f_dbl.x.b + g_dbl.e));
  102. exit(0);
  103. }