OSDN Git Service

2009-06-08 Andrew Haley <aph@redhat.com>
[pf3gnuchains/gcc-fork.git] / libffi / testsuite / libffi.call / cls_pointer_stack.c
1 /* Area:                ffi_call, closure_call
2    Purpose:             Check pointer arguments across multiple hideous stack frames.
3    Limitations: none.
4    PR:                  none.
5    Originator:  Blake Chaffin 6/7/2007  */
6
7 /* { dg-do run { xfail mips*-*-* arm*-*-* strongarm*-*-* xscale*-*-* } } */
8 #include "ffitest.h"
9
10 static  long dummyVar;
11
12 long dummy_func(
13         long double a1, char b1,
14         long double a2, char b2,
15         long double a3, char b3,
16         long double a4, char b4)
17 {
18         return a1 + b1 + a2 + b2 + a3 + b3 + a4 + b4;
19 }
20
21 void* cls_pointer_fn2(void* a1, void* a2)
22 {
23         long double     trample1        = (intptr_t)a1 + (intptr_t)a2;
24         char            trample2        = ((char*)&a1)[0] + ((char*)&a2)[0];
25         long double     trample3        = (intptr_t)trample1 + (intptr_t)a1;
26         char            trample4        = trample2 + ((char*)&a1)[1];
27         long double     trample5        = (intptr_t)trample3 + (intptr_t)a2;
28         char            trample6        = trample4 + ((char*)&a2)[1];
29         long double     trample7        = (intptr_t)trample5 + (intptr_t)trample1;
30         char            trample8        = trample6 + trample2;
31
32         dummyVar        = dummy_func(trample1, trample2, trample3, trample4,
33                 trample5, trample6, trample7, trample8);
34
35         void*   result  = (void*)((intptr_t)a1 + (intptr_t)a2);
36
37         printf("0x%08x 0x%08x: 0x%08x\n", 
38                (unsigned int)(uintptr_t) a1,
39                (unsigned int)(uintptr_t) a2,
40                (unsigned int)(uintptr_t) result);
41
42         return result;
43 }
44
45 void* cls_pointer_fn1(void* a1, void* a2)
46 {
47         long double     trample1        = (intptr_t)a1 + (intptr_t)a2;
48         char            trample2        = ((char*)&a1)[0] + ((char*)&a2)[0];
49         long double     trample3        = (intptr_t)trample1 + (intptr_t)a1;
50         char            trample4        = trample2 + ((char*)&a1)[1];
51         long double     trample5        = (intptr_t)trample3 + (intptr_t)a2;
52         char            trample6        = trample4 + ((char*)&a2)[1];
53         long double     trample7        = (intptr_t)trample5 + (intptr_t)trample1;
54         char            trample8        = trample6 + trample2;
55
56         dummyVar        = dummy_func(trample1, trample2, trample3, trample4,
57                 trample5, trample6, trample7, trample8);
58
59         void*   result  = (void*)((intptr_t)a1 + (intptr_t)a2);
60
61         printf("0x%08x 0x%08x: 0x%08x\n",
62                (unsigned int)(intptr_t) a1,
63                (unsigned int)(intptr_t) a2,
64                (unsigned int)(intptr_t) result);
65
66         result  = cls_pointer_fn2(result, a1);
67
68         return result;
69 }
70
71 static void
72 cls_pointer_gn(ffi_cif* cif __UNUSED__, void* resp, 
73                void** args, void* userdata __UNUSED__)
74 {
75         void*   a1      = *(void**)(args[0]);
76         void*   a2      = *(void**)(args[1]);
77
78         long double     trample1        = (intptr_t)a1 + (intptr_t)a2;
79         char            trample2        = ((char*)&a1)[0] + ((char*)&a2)[0];
80         long double     trample3        = (intptr_t)trample1 + (intptr_t)a1;
81         char            trample4        = trample2 + ((char*)&a1)[1];
82         long double     trample5        = (intptr_t)trample3 + (intptr_t)a2;
83         char            trample6        = trample4 + ((char*)&a2)[1];
84         long double     trample7        = (intptr_t)trample5 + (intptr_t)trample1;
85         char            trample8        = trample6 + trample2;
86
87         dummyVar        = dummy_func(trample1, trample2, trample3, trample4,
88                 trample5, trample6, trample7, trample8);
89
90         *(void**)resp = cls_pointer_fn1(a1, a2);
91 }
92
93 int main (void)
94 {
95         ffi_cif cif;
96 #ifndef USING_MMAP
97         static ffi_closure      cl;
98 #endif
99         ffi_closure*    pcl;
100         void*                   args[3];
101 //      ffi_type                cls_pointer_type;
102         ffi_type*               arg_types[3];
103
104 #ifdef USING_MMAP
105         pcl = allocate_mmap(sizeof(ffi_closure));
106 #else
107         pcl = &cl;
108 #endif
109
110 /*      cls_pointer_type.size = sizeof(void*);
111         cls_pointer_type.alignment = 0;
112         cls_pointer_type.type = FFI_TYPE_POINTER;
113         cls_pointer_type.elements = NULL;*/
114
115         void*   arg1    = (void*)0x01234567;
116         void*   arg2    = (void*)0x89abcdef;
117         ffi_arg res             = 0;
118
119         arg_types[0] = &ffi_type_pointer;
120         arg_types[1] = &ffi_type_pointer;
121         arg_types[2] = NULL;
122
123         CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &ffi_type_pointer,
124                 arg_types) == FFI_OK);
125
126         args[0] = &arg1;
127         args[1] = &arg2;
128         args[2] = NULL;
129
130         printf("\n");
131         ffi_call(&cif, FFI_FN(cls_pointer_fn1), &res, args);
132
133         printf("res: 0x%08x\n", (unsigned int) res);
134         // { dg-output "\n0x01234567 0x89abcdef: 0x8acf1356" }
135         // { dg-output "\n0x8acf1356 0x01234567: 0x8bf258bd" }
136         // { dg-output "\nres: 0x8bf258bd" }
137
138         CHECK(ffi_prep_closure(pcl, &cif, cls_pointer_gn, NULL) == FFI_OK);
139
140         res = (ffi_arg)((void*(*)(void*, void*))(pcl))(arg1, arg2);
141
142         printf("res: 0x%08x\n", (unsigned int) res);
143         // { dg-output "\n0x01234567 0x89abcdef: 0x8acf1356" }
144         // { dg-output "\n0x8acf1356 0x01234567: 0x8bf258bd" }
145         // { dg-output "\nres: 0x8bf258bd" }
146
147         exit(0);
148 }