closure_simple.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* Area: closure_call
  2. Purpose: Check simple closure handling with all ABIs
  3. Limitations: none.
  4. PR: none.
  5. Originator: <twalljava@dev.java.net> */
  6. /* { dg-do run } */
  7. #include "ffitest.h"
  8. static void
  9. closure_test(ffi_cif* cif __UNUSED__, void* resp, void** args, void* userdata)
  10. {
  11. *(ffi_arg*)resp =
  12. (int)*(int *)args[0] + (int)(*(int *)args[1])
  13. + (int)(*(int *)args[2]) + (int)(*(int *)args[3])
  14. + (int)(intptr_t)userdata;
  15. printf("%d %d %d %d: %d\n",
  16. (int)*(int *)args[0], (int)(*(int *)args[1]),
  17. (int)(*(int *)args[2]), (int)(*(int *)args[3]),
  18. (int)*(ffi_arg *)resp);
  19. }
  20. typedef int (ABI_ATTR *closure_test_type0)(int, int, int, int);
  21. int main (void)
  22. {
  23. ffi_cif cif;
  24. void *code;
  25. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  26. ffi_type * cl_arg_types[17];
  27. int res;
  28. cl_arg_types[0] = &ffi_type_uint;
  29. cl_arg_types[1] = &ffi_type_uint;
  30. cl_arg_types[2] = &ffi_type_uint;
  31. cl_arg_types[3] = &ffi_type_uint;
  32. cl_arg_types[4] = NULL;
  33. /* Initialize the cif */
  34. CHECK(ffi_prep_cif(&cif, ABI_NUM, 4,
  35. &ffi_type_sint, cl_arg_types) == FFI_OK);
  36. CHECK(ffi_prep_closure_loc(pcl, &cif, closure_test,
  37. (void *) 3 /* userdata */, code) == FFI_OK);
  38. res = (*(closure_test_type0)code)(0, 1, 2, 3);
  39. /* { dg-output "0 1 2 3: 9" } */
  40. printf("res: %d\n",res);
  41. /* { dg-output "\nres: 9" } */
  42. exit(0);
  43. }