1 | n/a | /* Area: closure_call (thiscall convention) |
---|
2 | n/a | Purpose: Check handling when caller expects thiscall callee |
---|
3 | n/a | Limitations: none. |
---|
4 | n/a | PR: none. |
---|
5 | n/a | Originator: <ktietz@redhat.com> */ |
---|
6 | n/a | |
---|
7 | n/a | /* { dg-do run { target i?86-*-cygwin* i?86-*-mingw* } } */ |
---|
8 | n/a | #include "ffitest.h" |
---|
9 | n/a | |
---|
10 | n/a | static void |
---|
11 | n/a | closure_test_thiscall(ffi_cif* cif __UNUSED__, void* resp, void** args, |
---|
12 | n/a | void* userdata) |
---|
13 | n/a | { |
---|
14 | n/a | *(ffi_arg*)resp = |
---|
15 | n/a | (int)*(int *)args[0] + (int)(*(int *)args[1]) |
---|
16 | n/a | + (int)(*(int *)args[2]) + (int)(*(int *)args[3]) |
---|
17 | n/a | + (int)(intptr_t)userdata; |
---|
18 | n/a | |
---|
19 | n/a | printf("%d %d %d %d: %d\n", |
---|
20 | n/a | (int)*(int *)args[0], (int)(*(int *)args[1]), |
---|
21 | n/a | (int)(*(int *)args[2]), (int)(*(int *)args[3]), |
---|
22 | n/a | (int)*(ffi_arg *)resp); |
---|
23 | n/a | |
---|
24 | n/a | } |
---|
25 | n/a | |
---|
26 | n/a | typedef int (__thiscall *closure_test_type0)(int, int, int, int); |
---|
27 | n/a | |
---|
28 | n/a | int main (void) |
---|
29 | n/a | { |
---|
30 | n/a | ffi_cif cif; |
---|
31 | n/a | void *code; |
---|
32 | n/a | ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code); |
---|
33 | n/a | ffi_type * cl_arg_types[17]; |
---|
34 | n/a | int res; |
---|
35 | n/a | void* sp_pre; |
---|
36 | n/a | void* sp_post; |
---|
37 | n/a | char buf[1024]; |
---|
38 | n/a | |
---|
39 | n/a | cl_arg_types[0] = &ffi_type_uint; |
---|
40 | n/a | cl_arg_types[1] = &ffi_type_uint; |
---|
41 | n/a | cl_arg_types[2] = &ffi_type_uint; |
---|
42 | n/a | cl_arg_types[3] = &ffi_type_uint; |
---|
43 | n/a | cl_arg_types[4] = NULL; |
---|
44 | n/a | |
---|
45 | n/a | /* Initialize the cif */ |
---|
46 | n/a | CHECK(ffi_prep_cif(&cif, FFI_THISCALL, 4, |
---|
47 | n/a | &ffi_type_sint, cl_arg_types) == FFI_OK); |
---|
48 | n/a | |
---|
49 | n/a | CHECK(ffi_prep_closure_loc(pcl, &cif, closure_test_thiscall, |
---|
50 | n/a | (void *) 3 /* userdata */, code) == FFI_OK); |
---|
51 | n/a | |
---|
52 | n/a | #ifdef _MSC_VER |
---|
53 | n/a | __asm { mov sp_pre, esp } |
---|
54 | n/a | #else |
---|
55 | n/a | asm volatile (" movl %%esp,%0" : "=g" (sp_pre)); |
---|
56 | n/a | #endif |
---|
57 | n/a | res = (*(closure_test_type0)code)(0, 1, 2, 3); |
---|
58 | n/a | #ifdef _MSC_VER |
---|
59 | n/a | __asm { mov sp_post, esp } |
---|
60 | n/a | #else |
---|
61 | n/a | asm volatile (" movl %%esp,%0" : "=g" (sp_post)); |
---|
62 | n/a | #endif |
---|
63 | n/a | /* { dg-output "0 1 2 3: 9" } */ |
---|
64 | n/a | |
---|
65 | n/a | printf("res: %d\n",res); |
---|
66 | n/a | /* { dg-output "\nres: 9" } */ |
---|
67 | n/a | |
---|
68 | n/a | sprintf(buf, "mismatch: pre=%p vs post=%p", sp_pre, sp_post); |
---|
69 | n/a | printf("stack pointer %s\n", (sp_pre == sp_post ? "match" : buf)); |
---|
70 | n/a | /* { dg-output "\nstack pointer match" } */ |
---|
71 | n/a | exit(0); |
---|
72 | n/a | } |
---|