nested_struct2.c 2.7 KB

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