nested_struct1.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. 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_16byte1 {
  10. double a;
  11. float b;
  12. int c;
  13. } cls_struct_16byte1;
  14. typedef struct cls_struct_16byte2 {
  15. int ii;
  16. double dd;
  17. float ff;
  18. } cls_struct_16byte2;
  19. typedef struct cls_struct_combined {
  20. cls_struct_16byte1 d;
  21. cls_struct_16byte2 e;
  22. } cls_struct_combined;
  23. cls_struct_combined cls_struct_combined_fn(struct cls_struct_16byte1 b0,
  24. struct cls_struct_16byte2 b1,
  25. struct cls_struct_combined b2,
  26. struct cls_struct_16byte1 b3)
  27. {
  28. struct cls_struct_combined result;
  29. result.d.a = b0.a + b1.dd + b2.d.a;
  30. result.d.b = b0.b + b1.ff + b2.d.b;
  31. result.d.c = b0.c + b1.ii + b2.d.c;
  32. result.e.ii = b0.c + b1.ii + b2.e.ii;
  33. result.e.dd = b0.a + b1.dd + b2.e.dd;
  34. result.e.ff = b0.b + b1.ff + b2.e.ff;
  35. printf("%g %g %d %d %g %g %g %g %d %d %g %g %g %g %d: %g %g %d %d %g %g\n",
  36. b0.a, b0.b, b0.c,
  37. b1.ii, b1.dd, b1.ff,
  38. b2.d.a, b2.d.b, b2.d.c,
  39. b2.e.ii, b2.e.dd, b2.e.ff,
  40. b3.a, b3.b, b3.c,
  41. result.d.a, result.d.b, result.d.c,
  42. result.e.ii, result.e.dd, result.e.ff);
  43. return result;
  44. }
  45. static void
  46. cls_struct_combined_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
  47. void* userdata __UNUSED__)
  48. {
  49. struct cls_struct_16byte1 b0;
  50. struct cls_struct_16byte2 b1;
  51. struct cls_struct_combined b2;
  52. struct cls_struct_16byte1 b3;
  53. b0 = *(struct cls_struct_16byte1*)(args[0]);
  54. b1 = *(struct cls_struct_16byte2*)(args[1]);
  55. b2 = *(struct cls_struct_combined*)(args[2]);
  56. b3 = *(struct cls_struct_16byte1*)(args[3]);
  57. *(cls_struct_combined*)resp = cls_struct_combined_fn(b0, b1, b2, b3);
  58. }
  59. int main (void)
  60. {
  61. ffi_cif cif;
  62. void *code;
  63. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  64. void* args_dbl[5];
  65. ffi_type* cls_struct_fields[5];
  66. ffi_type* cls_struct_fields1[5];
  67. ffi_type* cls_struct_fields2[5];
  68. ffi_type cls_struct_type, cls_struct_type1, cls_struct_type2;
  69. ffi_type* dbl_arg_types[5];
  70. struct cls_struct_16byte1 e_dbl = { 9.0, 2.0, 6};
  71. struct cls_struct_16byte2 f_dbl = { 1, 2.0, 3.0};
  72. struct cls_struct_combined g_dbl = {{4.0, 5.0, 6},
  73. {3, 1.0, 8.0}};
  74. struct cls_struct_16byte1 h_dbl = { 3.0, 2.0, 4};
  75. struct cls_struct_combined res_dbl;
  76. cls_struct_type.size = 0;
  77. cls_struct_type.alignment = 0;
  78. cls_struct_type.type = FFI_TYPE_STRUCT;
  79. cls_struct_type.elements = cls_struct_fields;
  80. cls_struct_type1.size = 0;
  81. cls_struct_type1.alignment = 0;
  82. cls_struct_type1.type = FFI_TYPE_STRUCT;
  83. cls_struct_type1.elements = cls_struct_fields1;
  84. cls_struct_type2.size = 0;
  85. cls_struct_type2.alignment = 0;
  86. cls_struct_type2.type = FFI_TYPE_STRUCT;
  87. cls_struct_type2.elements = cls_struct_fields2;
  88. cls_struct_fields[0] = &ffi_type_double;
  89. cls_struct_fields[1] = &ffi_type_float;
  90. cls_struct_fields[2] = &ffi_type_sint;
  91. cls_struct_fields[3] = NULL;
  92. cls_struct_fields1[0] = &ffi_type_sint;
  93. cls_struct_fields1[1] = &ffi_type_double;
  94. cls_struct_fields1[2] = &ffi_type_float;
  95. cls_struct_fields1[3] = NULL;
  96. cls_struct_fields2[0] = &cls_struct_type;
  97. cls_struct_fields2[1] = &cls_struct_type1;
  98. cls_struct_fields2[2] = NULL;
  99. dbl_arg_types[0] = &cls_struct_type;
  100. dbl_arg_types[1] = &cls_struct_type1;
  101. dbl_arg_types[2] = &cls_struct_type2;
  102. dbl_arg_types[3] = &cls_struct_type;
  103. dbl_arg_types[4] = NULL;
  104. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 4, &cls_struct_type2,
  105. dbl_arg_types) == FFI_OK);
  106. args_dbl[0] = &e_dbl;
  107. args_dbl[1] = &f_dbl;
  108. args_dbl[2] = &g_dbl;
  109. args_dbl[3] = &h_dbl;
  110. args_dbl[4] = NULL;
  111. ffi_call(&cif, FFI_FN(cls_struct_combined_fn), &res_dbl, args_dbl);
  112. /* { dg-output "9 2 6 1 2 3 4 5 6 3 1 8 3 2 4: 15 10 13 10 12 13" } */
  113. CHECK( res_dbl.d.a == (e_dbl.a + f_dbl.dd + g_dbl.d.a));
  114. CHECK( res_dbl.d.b == (e_dbl.b + f_dbl.ff + g_dbl.d.b));
  115. CHECK( res_dbl.d.c == (e_dbl.c + f_dbl.ii + g_dbl.d.c));
  116. CHECK( res_dbl.e.ii == (e_dbl.c + f_dbl.ii + g_dbl.e.ii));
  117. CHECK( res_dbl.e.dd == (e_dbl.a + f_dbl.dd + g_dbl.e.dd));
  118. CHECK( res_dbl.e.ff == (e_dbl.b + f_dbl.ff + g_dbl.e.ff));
  119. CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_combined_gn, NULL, code) == FFI_OK);
  120. res_dbl = ((cls_struct_combined(*)(cls_struct_16byte1,
  121. cls_struct_16byte2,
  122. cls_struct_combined,
  123. cls_struct_16byte1))
  124. (code))(e_dbl, f_dbl, g_dbl, h_dbl);
  125. /* { dg-output "\n9 2 6 1 2 3 4 5 6 3 1 8 3 2 4: 15 10 13 10 12 13" } */
  126. CHECK( res_dbl.d.a == (e_dbl.a + f_dbl.dd + g_dbl.d.a));
  127. CHECK( res_dbl.d.b == (e_dbl.b + f_dbl.ff + g_dbl.d.b));
  128. CHECK( res_dbl.d.c == (e_dbl.c + f_dbl.ii + g_dbl.d.c));
  129. CHECK( res_dbl.e.ii == (e_dbl.c + f_dbl.ii + g_dbl.e.ii));
  130. CHECK( res_dbl.e.dd == (e_dbl.a + f_dbl.dd + g_dbl.e.dd));
  131. CHECK( res_dbl.e.ff == (e_dbl.b + f_dbl.ff + g_dbl.e.ff));
  132. /* CHECK( 1 == 0); */
  133. exit(0);
  134. }