struct7.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* Area: ffi_call
  2. Purpose: Check structures.
  3. Limitations: none.
  4. PR: none.
  5. Originator: From the original ffitest.c */
  6. /* { dg-do run } */
  7. #include "ffitest.h"
  8. typedef struct
  9. {
  10. float f1;
  11. float f2;
  12. double d;
  13. } test_structure_7;
  14. static test_structure_7 ABI_ATTR struct7 (test_structure_7 ts)
  15. {
  16. ts.f1 += 1;
  17. ts.f2 += 1;
  18. ts.d += 1;
  19. return ts;
  20. }
  21. int main (void)
  22. {
  23. ffi_cif cif;
  24. ffi_type *args[MAX_ARGS];
  25. void *values[MAX_ARGS];
  26. ffi_type ts7_type;
  27. ffi_type *ts7_type_elements[4];
  28. test_structure_7 ts7_arg;
  29. /* This is a hack to get a properly aligned result buffer */
  30. test_structure_7 *ts7_result =
  31. (test_structure_7 *) malloc (sizeof(test_structure_7));
  32. ts7_type.size = 0;
  33. ts7_type.alignment = 0;
  34. ts7_type.type = FFI_TYPE_STRUCT;
  35. ts7_type.elements = ts7_type_elements;
  36. ts7_type_elements[0] = &ffi_type_float;
  37. ts7_type_elements[1] = &ffi_type_float;
  38. ts7_type_elements[2] = &ffi_type_double;
  39. ts7_type_elements[3] = NULL;
  40. args[0] = &ts7_type;
  41. values[0] = &ts7_arg;
  42. /* Initialize the cif */
  43. CHECK(ffi_prep_cif(&cif, ABI_NUM, 1, &ts7_type, args) == FFI_OK);
  44. ts7_arg.f1 = 5.55f;
  45. ts7_arg.f2 = 55.5f;
  46. ts7_arg.d = 6.66;
  47. printf ("%g\n", ts7_arg.f1);
  48. printf ("%g\n", ts7_arg.f2);
  49. printf ("%g\n", ts7_arg.d);
  50. ffi_call(&cif, FFI_FN(struct7), ts7_result, values);
  51. printf ("%g\n", ts7_result->f1);
  52. printf ("%g\n", ts7_result->f2);
  53. printf ("%g\n", ts7_result->d);
  54. CHECK(ts7_result->f1 == 5.55f + 1);
  55. CHECK(ts7_result->f2 == 55.5f + 1);
  56. CHECK(ts7_result->d == 6.66 + 1);
  57. free (ts7_result);
  58. exit(0);
  59. }