nested_struct.c 4.6 KB

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