cls_18byte.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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_18byte {
  10. double a;
  11. unsigned char b;
  12. unsigned char c;
  13. double d;
  14. } cls_struct_18byte;
  15. cls_struct_18byte cls_struct_18byte_fn(struct cls_struct_18byte a1,
  16. struct cls_struct_18byte a2)
  17. {
  18. struct cls_struct_18byte result;
  19. result.a = a1.a + a2.a;
  20. result.b = a1.b + a2.b;
  21. result.c = a1.c + a2.c;
  22. result.d = a1.d + a2.d;
  23. printf("%g %d %d %g %g %d %d %g: %g %d %d %g\n", a1.a, a1.b, a1.c, a1.d,
  24. a2.a, a2.b, a2.c, a2.d,
  25. result.a, result.b, result.c, result.d);
  26. return result;
  27. }
  28. static void
  29. cls_struct_18byte_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
  30. void* userdata __UNUSED__)
  31. {
  32. struct cls_struct_18byte a1, a2;
  33. a1 = *(struct cls_struct_18byte*)(args[0]);
  34. a2 = *(struct cls_struct_18byte*)(args[1]);
  35. *(cls_struct_18byte*)resp = cls_struct_18byte_fn(a1, a2);
  36. }
  37. int main (void)
  38. {
  39. ffi_cif cif;
  40. void *code;
  41. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  42. void* args_dbl[3];
  43. ffi_type* cls_struct_fields[5];
  44. ffi_type cls_struct_type;
  45. ffi_type* dbl_arg_types[3];
  46. struct cls_struct_18byte g_dbl = { 1.0, 127, 126, 3.0 };
  47. struct cls_struct_18byte f_dbl = { 4.0, 125, 124, 5.0 };
  48. struct cls_struct_18byte res_dbl;
  49. cls_struct_type.size = 0;
  50. cls_struct_type.alignment = 0;
  51. cls_struct_type.type = FFI_TYPE_STRUCT;
  52. cls_struct_type.elements = cls_struct_fields;
  53. cls_struct_fields[0] = &ffi_type_double;
  54. cls_struct_fields[1] = &ffi_type_uchar;
  55. cls_struct_fields[2] = &ffi_type_uchar;
  56. cls_struct_fields[3] = &ffi_type_double;
  57. cls_struct_fields[4] = NULL;
  58. dbl_arg_types[0] = &cls_struct_type;
  59. dbl_arg_types[1] = &cls_struct_type;
  60. dbl_arg_types[2] = NULL;
  61. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
  62. dbl_arg_types) == FFI_OK);
  63. args_dbl[0] = &g_dbl;
  64. args_dbl[1] = &f_dbl;
  65. args_dbl[2] = NULL;
  66. ffi_call(&cif, FFI_FN(cls_struct_18byte_fn), &res_dbl, args_dbl);
  67. /* { dg-output "1 127 126 3 4 125 124 5: 5 252 250 8" } */
  68. printf("res: %g %d %d %g\n", res_dbl.a, res_dbl.b, res_dbl.c, res_dbl.d);
  69. /* { dg-output "\nres: 5 252 250 8" } */
  70. CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_18byte_gn, NULL, code) == FFI_OK);
  71. res_dbl = ((cls_struct_18byte(*)(cls_struct_18byte, cls_struct_18byte))(code))(g_dbl, f_dbl);
  72. /* { dg-output "\n1 127 126 3 4 125 124 5: 5 252 250 8" } */
  73. printf("res: %g %d %d %g\n", res_dbl.a, res_dbl.b, res_dbl.c, res_dbl.d);
  74. /* { dg-output "\nres: 5 252 250 8" } */
  75. exit(0);
  76. }