nested_struct10.c 3.5 KB

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