/external/libffi/testsuite/libffi.call/nested_struct1.c

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk · C · 169 lines · 131 code · 28 blank · 10 comment · 14 complexity · 7b376f33987a1ba4ac43431c4e829115 MD5 · raw file

  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. #ifndef USING_MMAP
  63. static ffi_closure cl;
  64. #endif
  65. ffi_closure *pcl;
  66. void* args_dbl[5];
  67. ffi_type* cls_struct_fields[5];
  68. ffi_type* cls_struct_fields1[5];
  69. ffi_type* cls_struct_fields2[5];
  70. ffi_type cls_struct_type, cls_struct_type1, cls_struct_type2;
  71. ffi_type* dbl_arg_types[5];
  72. #ifdef USING_MMAP
  73. pcl = allocate_mmap (sizeof(ffi_closure));
  74. #else
  75. pcl = &cl;
  76. #endif
  77. cls_struct_type.size = 0;
  78. cls_struct_type.alignment = 0;
  79. cls_struct_type.type = FFI_TYPE_STRUCT;
  80. cls_struct_type.elements = cls_struct_fields;
  81. cls_struct_type1.size = 0;
  82. cls_struct_type1.alignment = 0;
  83. cls_struct_type1.type = FFI_TYPE_STRUCT;
  84. cls_struct_type1.elements = cls_struct_fields1;
  85. cls_struct_type2.size = 0;
  86. cls_struct_type2.alignment = 0;
  87. cls_struct_type2.type = FFI_TYPE_STRUCT;
  88. cls_struct_type2.elements = cls_struct_fields2;
  89. struct cls_struct_16byte1 e_dbl = { 9.0, 2.0, 6};
  90. struct cls_struct_16byte2 f_dbl = { 1, 2.0, 3.0};
  91. struct cls_struct_combined g_dbl = {{4.0, 5.0, 6},
  92. {3, 1.0, 8.0}};
  93. struct cls_struct_16byte1 h_dbl = { 3.0, 2.0, 4};
  94. struct cls_struct_combined res_dbl;
  95. cls_struct_fields[0] = &ffi_type_double;
  96. cls_struct_fields[1] = &ffi_type_float;
  97. cls_struct_fields[2] = &ffi_type_sint;
  98. cls_struct_fields[3] = NULL;
  99. cls_struct_fields1[0] = &ffi_type_sint;
  100. cls_struct_fields1[1] = &ffi_type_double;
  101. cls_struct_fields1[2] = &ffi_type_float;
  102. cls_struct_fields1[3] = NULL;
  103. cls_struct_fields2[0] = &cls_struct_type;
  104. cls_struct_fields2[1] = &cls_struct_type1;
  105. cls_struct_fields2[2] = NULL;
  106. dbl_arg_types[0] = &cls_struct_type;
  107. dbl_arg_types[1] = &cls_struct_type1;
  108. dbl_arg_types[2] = &cls_struct_type2;
  109. dbl_arg_types[3] = &cls_struct_type;
  110. dbl_arg_types[4] = NULL;
  111. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 4, &cls_struct_type2,
  112. dbl_arg_types) == FFI_OK);
  113. args_dbl[0] = &e_dbl;
  114. args_dbl[1] = &f_dbl;
  115. args_dbl[2] = &g_dbl;
  116. args_dbl[3] = &h_dbl;
  117. args_dbl[4] = NULL;
  118. ffi_call(&cif, FFI_FN(cls_struct_combined_fn), &res_dbl, args_dbl);
  119. /* { dg-output "9 2 6 1 2 3 4 5 6 3 1 8 3 2 4: 15 10 13 10 12 13" } */
  120. CHECK( res_dbl.d.a == (e_dbl.a + f_dbl.dd + g_dbl.d.a));
  121. CHECK( res_dbl.d.b == (e_dbl.b + f_dbl.ff + g_dbl.d.b));
  122. CHECK( res_dbl.d.c == (e_dbl.c + f_dbl.ii + g_dbl.d.c));
  123. CHECK( res_dbl.e.ii == (e_dbl.c + f_dbl.ii + g_dbl.e.ii));
  124. CHECK( res_dbl.e.dd == (e_dbl.a + f_dbl.dd + g_dbl.e.dd));
  125. CHECK( res_dbl.e.ff == (e_dbl.b + f_dbl.ff + g_dbl.e.ff));
  126. CHECK(ffi_prep_closure(pcl, &cif, cls_struct_combined_gn, NULL) == FFI_OK);
  127. res_dbl = ((cls_struct_combined(*)(cls_struct_16byte1,
  128. cls_struct_16byte2,
  129. cls_struct_combined,
  130. cls_struct_16byte1))
  131. (pcl))(e_dbl, f_dbl, g_dbl, h_dbl);
  132. /* { dg-output "\n9 2 6 1 2 3 4 5 6 3 1 8 3 2 4: 15 10 13 10 12 13" } */
  133. CHECK( res_dbl.d.a == (e_dbl.a + f_dbl.dd + g_dbl.d.a));
  134. CHECK( res_dbl.d.b == (e_dbl.b + f_dbl.ff + g_dbl.d.b));
  135. CHECK( res_dbl.d.c == (e_dbl.c + f_dbl.ii + g_dbl.d.c));
  136. CHECK( res_dbl.e.ii == (e_dbl.c + f_dbl.ii + g_dbl.e.ii));
  137. CHECK( res_dbl.e.dd == (e_dbl.a + f_dbl.dd + g_dbl.e.dd));
  138. CHECK( res_dbl.e.ff == (e_dbl.b + f_dbl.ff + g_dbl.e.ff));
  139. // CHECK( 1 == 0);
  140. exit(0);
  141. }