struct9.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 f;
  11. int i;
  12. } test_structure_9;
  13. static test_structure_9 ABI_ATTR struct9 (test_structure_9 ts)
  14. {
  15. ts.f += 1;
  16. ts.i += 1;
  17. return ts;
  18. }
  19. int main (void)
  20. {
  21. ffi_cif cif;
  22. ffi_type *args[MAX_ARGS];
  23. void *values[MAX_ARGS];
  24. ffi_type ts9_type;
  25. ffi_type *ts9_type_elements[3];
  26. test_structure_9 ts9_arg;
  27. /* This is a hack to get a properly aligned result buffer */
  28. test_structure_9 *ts9_result =
  29. (test_structure_9 *) malloc (sizeof(test_structure_9));
  30. ts9_type.size = 0;
  31. ts9_type.alignment = 0;
  32. ts9_type.type = FFI_TYPE_STRUCT;
  33. ts9_type.elements = ts9_type_elements;
  34. ts9_type_elements[0] = &ffi_type_float;
  35. ts9_type_elements[1] = &ffi_type_sint;
  36. ts9_type_elements[2] = NULL;
  37. args[0] = &ts9_type;
  38. values[0] = &ts9_arg;
  39. /* Initialize the cif */
  40. CHECK(ffi_prep_cif(&cif, ABI_NUM, 1, &ts9_type, args) == FFI_OK);
  41. ts9_arg.f = 5.55f;
  42. ts9_arg.i = 5;
  43. printf ("%g\n", ts9_arg.f);
  44. printf ("%d\n", ts9_arg.i);
  45. ffi_call(&cif, FFI_FN(struct9), ts9_result, values);
  46. printf ("%g\n", ts9_result->f);
  47. printf ("%d\n", ts9_result->i);
  48. CHECK(ts9_result->f == 5.55f + 1);
  49. CHECK(ts9_result->i == 5 + 1);
  50. free (ts9_result);
  51. exit(0);
  52. }