cls_9byte1.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Area: ffi_call, closure_call
  2. Purpose: Check structure passing with different structure size.
  3. Depending on the ABI. Darwin/AIX do double-word
  4. alignment of the struct if the first element is a double.
  5. Check that it does not here.
  6. Limitations: none.
  7. PR: none.
  8. Originator: <andreast@gcc.gnu.org> 20030914 */
  9. /* { dg-do run } */
  10. #include "ffitest.h"
  11. typedef struct cls_struct_9byte {
  12. int a;
  13. double b;
  14. } cls_struct_9byte;
  15. cls_struct_9byte cls_struct_9byte_fn(struct cls_struct_9byte b1,
  16. struct cls_struct_9byte b2)
  17. {
  18. struct cls_struct_9byte result;
  19. result.a = b1.a + b2.a;
  20. result.b = b1.b + b2.b;
  21. printf("%d %g %d %g: %d %g\n", b1.a, b1.b, b2.a, b2.b,
  22. result.a, result.b);
  23. return result;
  24. }
  25. static void cls_struct_9byte_gn(ffi_cif* cif __UNUSED__, void* resp,
  26. void** args, void* userdata __UNUSED__)
  27. {
  28. struct cls_struct_9byte b1, b2;
  29. b1 = *(struct cls_struct_9byte*)(args[0]);
  30. b2 = *(struct cls_struct_9byte*)(args[1]);
  31. *(cls_struct_9byte*)resp = cls_struct_9byte_fn(b1, b2);
  32. }
  33. int main (void)
  34. {
  35. ffi_cif cif;
  36. void *code;
  37. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  38. void* args_dbl[3];
  39. ffi_type* cls_struct_fields[3];
  40. ffi_type cls_struct_type;
  41. ffi_type* dbl_arg_types[3];
  42. struct cls_struct_9byte h_dbl = { 7, 8.0};
  43. struct cls_struct_9byte j_dbl = { 1, 9.0};
  44. struct cls_struct_9byte res_dbl;
  45. cls_struct_type.size = 0;
  46. cls_struct_type.alignment = 0;
  47. cls_struct_type.type = FFI_TYPE_STRUCT;
  48. cls_struct_type.elements = cls_struct_fields;
  49. cls_struct_fields[0] = &ffi_type_sint;
  50. cls_struct_fields[1] = &ffi_type_double;
  51. cls_struct_fields[2] = NULL;
  52. dbl_arg_types[0] = &cls_struct_type;
  53. dbl_arg_types[1] = &cls_struct_type;
  54. dbl_arg_types[2] = NULL;
  55. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
  56. dbl_arg_types) == FFI_OK);
  57. args_dbl[0] = &h_dbl;
  58. args_dbl[1] = &j_dbl;
  59. args_dbl[2] = NULL;
  60. ffi_call(&cif, FFI_FN(cls_struct_9byte_fn), &res_dbl, args_dbl);
  61. /* { dg-output "7 8 1 9: 8 17" } */
  62. printf("res: %d %g\n", res_dbl.a, res_dbl.b);
  63. /* { dg-output "\nres: 8 17" } */
  64. CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_9byte_gn, NULL, code) == FFI_OK);
  65. res_dbl = ((cls_struct_9byte(*)(cls_struct_9byte, cls_struct_9byte))(code))(h_dbl, j_dbl);
  66. /* { dg-output "\n7 8 1 9: 8 17" } */
  67. printf("res: %d %g\n", res_dbl.a, res_dbl.b);
  68. /* { dg-output "\nres: 8 17" } */
  69. exit(0);
  70. }