cls_24byte.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Area: ffi_call, closure_call
  2. Purpose: Check structure passing with different structure size.
  3. Depending on the ABI. Check overlapping.
  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_24byte {
  10. double a;
  11. double b;
  12. int c;
  13. float d;
  14. } cls_struct_24byte;
  15. cls_struct_24byte cls_struct_24byte_fn(struct cls_struct_24byte b0,
  16. struct cls_struct_24byte b1,
  17. struct cls_struct_24byte b2,
  18. struct cls_struct_24byte b3)
  19. {
  20. struct cls_struct_24byte result;
  21. result.a = b0.a + b1.a + b2.a + b3.a;
  22. result.b = b0.b + b1.b + b2.b + b3.b;
  23. result.c = b0.c + b1.c + b2.c + b3.c;
  24. result.d = b0.d + b1.d + b2.d + b3.d;
  25. printf("%g %g %d %g %g %g %d %g %g %g %d %g %g %g %d %g: %g %g %d %g\n",
  26. b0.a, b0.b, b0.c, b0.d,
  27. b1.a, b1.b, b1.c, b1.d,
  28. b2.a, b2.b, b2.c, b2.d,
  29. b3.a, b3.b, b3.c, b2.d,
  30. result.a, result.b, result.c, result.d);
  31. return result;
  32. }
  33. static void
  34. cls_struct_24byte_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
  35. void* userdata __UNUSED__)
  36. {
  37. struct cls_struct_24byte b0, b1, b2, b3;
  38. b0 = *(struct cls_struct_24byte*)(args[0]);
  39. b1 = *(struct cls_struct_24byte*)(args[1]);
  40. b2 = *(struct cls_struct_24byte*)(args[2]);
  41. b3 = *(struct cls_struct_24byte*)(args[3]);
  42. *(cls_struct_24byte*)resp = cls_struct_24byte_fn(b0, b1, b2, b3);
  43. }
  44. int main (void)
  45. {
  46. ffi_cif cif;
  47. void *code;
  48. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  49. void* args_dbl[5];
  50. ffi_type* cls_struct_fields[5];
  51. ffi_type cls_struct_type;
  52. ffi_type* dbl_arg_types[5];
  53. struct cls_struct_24byte e_dbl = { 9.0, 2.0, 6, 5.0 };
  54. struct cls_struct_24byte f_dbl = { 1.0, 2.0, 3, 7.0 };
  55. struct cls_struct_24byte g_dbl = { 4.0, 5.0, 7, 9.0 };
  56. struct cls_struct_24byte h_dbl = { 8.0, 6.0, 1, 4.0 };
  57. struct cls_struct_24byte res_dbl;
  58. cls_struct_type.size = 0;
  59. cls_struct_type.alignment = 0;
  60. cls_struct_type.type = FFI_TYPE_STRUCT;
  61. cls_struct_type.elements = cls_struct_fields;
  62. cls_struct_fields[0] = &ffi_type_double;
  63. cls_struct_fields[1] = &ffi_type_double;
  64. cls_struct_fields[2] = &ffi_type_sint;
  65. cls_struct_fields[3] = &ffi_type_float;
  66. cls_struct_fields[4] = NULL;
  67. dbl_arg_types[0] = &cls_struct_type;
  68. dbl_arg_types[1] = &cls_struct_type;
  69. dbl_arg_types[2] = &cls_struct_type;
  70. dbl_arg_types[3] = &cls_struct_type;
  71. dbl_arg_types[4] = NULL;
  72. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 4, &cls_struct_type,
  73. dbl_arg_types) == FFI_OK);
  74. args_dbl[0] = &e_dbl;
  75. args_dbl[1] = &f_dbl;
  76. args_dbl[2] = &g_dbl;
  77. args_dbl[3] = &h_dbl;
  78. args_dbl[4] = NULL;
  79. ffi_call(&cif, FFI_FN(cls_struct_24byte_fn), &res_dbl, args_dbl);
  80. /* { dg-output "9 2 6 5 1 2 3 7 4 5 7 9 8 6 1 9: 22 15 17 25" } */
  81. printf("res: %g %g %d %g\n", res_dbl.a, res_dbl.b, res_dbl.c, res_dbl.d);
  82. /* { dg-output "\nres: 22 15 17 25" } */
  83. CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_24byte_gn, NULL, code) == FFI_OK);
  84. res_dbl = ((cls_struct_24byte(*)(cls_struct_24byte,
  85. cls_struct_24byte,
  86. cls_struct_24byte,
  87. cls_struct_24byte))
  88. (code))(e_dbl, f_dbl, g_dbl, h_dbl);
  89. /* { dg-output "\n9 2 6 5 1 2 3 7 4 5 7 9 8 6 1 9: 22 15 17 25" } */
  90. printf("res: %g %g %d %g\n", res_dbl.a, res_dbl.b, res_dbl.c, res_dbl.d);
  91. /* { dg-output "\nres: 22 15 17 25" } */
  92. exit(0);
  93. }