Debian Codegraph @

testsuite/libffi.closures/closure_simple.c

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