cls_19byte.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Area: ffi_call, closure_call
  2. Purpose: Check structure passing with different structure size.
  3. Depending on the ABI. Double alignment check on darwin.
  4. Limitations: none.
  5. PR: none.
  6. Originator: <andreast@gcc.gnu.org> 20030915 */
  7. /* { dg-do run } */
  8. #include "ffitest.h"
  9. typedef struct cls_struct_19byte {
  10. double a;
  11. unsigned char b;
  12. unsigned char c;
  13. double d;
  14. unsigned char e;
  15. } cls_struct_19byte;
  16. cls_struct_19byte cls_struct_19byte_fn(struct cls_struct_19byte a1,
  17. struct cls_struct_19byte a2)
  18. {
  19. struct cls_struct_19byte result;
  20. result.a = a1.a + a2.a;
  21. result.b = a1.b + a2.b;
  22. result.c = a1.c + a2.c;
  23. result.d = a1.d + a2.d;
  24. result.e = a1.e + a2.e;
  25. printf("%g %d %d %g %d %g %d %d %g %d: %g %d %d %g %d\n",
  26. a1.a, a1.b, a1.c, a1.d, a1.e,
  27. a2.a, a2.b, a2.c, a2.d, a2.e,
  28. result.a, result.b, result.c, result.d, result.e);
  29. return result;
  30. }
  31. static void
  32. cls_struct_19byte_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
  33. void* userdata __UNUSED__)
  34. {
  35. struct cls_struct_19byte a1, a2;
  36. a1 = *(struct cls_struct_19byte*)(args[0]);
  37. a2 = *(struct cls_struct_19byte*)(args[1]);
  38. *(cls_struct_19byte*)resp = cls_struct_19byte_fn(a1, a2);
  39. }
  40. int main (void)
  41. {
  42. ffi_cif cif;
  43. void *code;
  44. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  45. void* args_dbl[3];
  46. ffi_type* cls_struct_fields[6];
  47. ffi_type cls_struct_type;
  48. ffi_type* dbl_arg_types[3];
  49. struct cls_struct_19byte g_dbl = { 1.0, 127, 126, 3.0, 120 };
  50. struct cls_struct_19byte f_dbl = { 4.0, 125, 124, 5.0, 119 };
  51. struct cls_struct_19byte res_dbl;
  52. cls_struct_type.size = 0;
  53. cls_struct_type.alignment = 0;
  54. cls_struct_type.type = FFI_TYPE_STRUCT;
  55. cls_struct_type.elements = cls_struct_fields;
  56. cls_struct_fields[0] = &ffi_type_double;
  57. cls_struct_fields[1] = &ffi_type_uchar;
  58. cls_struct_fields[2] = &ffi_type_uchar;
  59. cls_struct_fields[3] = &ffi_type_double;
  60. cls_struct_fields[4] = &ffi_type_uchar;
  61. cls_struct_fields[5] = NULL;
  62. dbl_arg_types[0] = &cls_struct_type;
  63. dbl_arg_types[1] = &cls_struct_type;
  64. dbl_arg_types[2] = NULL;
  65. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
  66. dbl_arg_types) == FFI_OK);
  67. args_dbl[0] = &g_dbl;
  68. args_dbl[1] = &f_dbl;
  69. args_dbl[2] = NULL;
  70. ffi_call(&cif, FFI_FN(cls_struct_19byte_fn), &res_dbl, args_dbl);
  71. /* { dg-output "1 127 126 3 120 4 125 124 5 119: 5 252 250 8 239" } */
  72. printf("res: %g %d %d %g %d\n", res_dbl.a, res_dbl.b, res_dbl.c,
  73. res_dbl.d, res_dbl.e);
  74. /* { dg-output "\nres: 5 252 250 8 239" } */
  75. CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_19byte_gn, NULL, code) == FFI_OK);
  76. res_dbl = ((cls_struct_19byte(*)(cls_struct_19byte, cls_struct_19byte))(code))(g_dbl, f_dbl);
  77. /* { dg-output "\n1 127 126 3 120 4 125 124 5 119: 5 252 250 8 239" } */
  78. printf("res: %g %d %d %g %d\n", res_dbl.a, res_dbl.b, res_dbl.c,
  79. res_dbl.d, res_dbl.e);
  80. /* { dg-output "\nres: 5 252 250 8 239" } */
  81. exit(0);
  82. }