problem1.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Area: ffi_call, closure_call
  2. Purpose: Check structure passing with different structure size.
  3. Limitations: none.
  4. PR: none.
  5. Originator: <andreast@gcc.gnu.org> 20030828 */
  6. /* { dg-do run } */
  7. #include "ffitest.h"
  8. typedef struct my_ffi_struct {
  9. double a;
  10. double b;
  11. double c;
  12. } my_ffi_struct;
  13. my_ffi_struct callee(struct my_ffi_struct a1, struct my_ffi_struct a2)
  14. {
  15. struct my_ffi_struct result;
  16. result.a = a1.a + a2.a;
  17. result.b = a1.b + a2.b;
  18. result.c = a1.c + a2.c;
  19. printf("%g %g %g %g %g %g: %g %g %g\n", a1.a, a1.b, a1.c,
  20. a2.a, a2.b, a2.c, result.a, result.b, result.c);
  21. return result;
  22. }
  23. void stub(ffi_cif* cif __UNUSED__, void* resp, void** args,
  24. void* userdata __UNUSED__)
  25. {
  26. struct my_ffi_struct a1;
  27. struct my_ffi_struct a2;
  28. a1 = *(struct my_ffi_struct*)(args[0]);
  29. a2 = *(struct my_ffi_struct*)(args[1]);
  30. *(my_ffi_struct *)resp = callee(a1, a2);
  31. }
  32. int main(void)
  33. {
  34. ffi_type* my_ffi_struct_fields[4];
  35. ffi_type my_ffi_struct_type;
  36. ffi_cif cif;
  37. void *code;
  38. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  39. void* args[4];
  40. ffi_type* arg_types[3];
  41. struct my_ffi_struct g = { 1.0, 2.0, 3.0 };
  42. struct my_ffi_struct f = { 1.0, 2.0, 3.0 };
  43. struct my_ffi_struct res;
  44. my_ffi_struct_type.size = 0;
  45. my_ffi_struct_type.alignment = 0;
  46. my_ffi_struct_type.type = FFI_TYPE_STRUCT;
  47. my_ffi_struct_type.elements = my_ffi_struct_fields;
  48. my_ffi_struct_fields[0] = &ffi_type_double;
  49. my_ffi_struct_fields[1] = &ffi_type_double;
  50. my_ffi_struct_fields[2] = &ffi_type_double;
  51. my_ffi_struct_fields[3] = NULL;
  52. arg_types[0] = &my_ffi_struct_type;
  53. arg_types[1] = &my_ffi_struct_type;
  54. arg_types[2] = NULL;
  55. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &my_ffi_struct_type,
  56. arg_types) == FFI_OK);
  57. args[0] = &g;
  58. args[1] = &f;
  59. args[2] = NULL;
  60. ffi_call(&cif, FFI_FN(callee), &res, args);
  61. /* { dg-output "1 2 3 1 2 3: 2 4 6" } */
  62. printf("res: %g %g %g\n", res.a, res.b, res.c);
  63. /* { dg-output "\nres: 2 4 6" } */
  64. CHECK(ffi_prep_closure_loc(pcl, &cif, stub, NULL, code) == FFI_OK);
  65. res = ((my_ffi_struct(*)(struct my_ffi_struct, struct my_ffi_struct))(code))(g, f);
  66. /* { dg-output "\n1 2 3 1 2 3: 2 4 6" } */
  67. printf("res: %g %g %g\n", res.a, res.b, res.c);
  68. /* { dg-output "\nres: 2 4 6" } */
  69. exit(0);;
  70. }