-
Notifications
You must be signed in to change notification settings - Fork 36
/
test.cpp
383 lines (346 loc) · 13.2 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include <iostream>
#include "stdlib.h"
#include "stdio.h"
using namespace std;
#include "call_in_stack.h"
#ifdef __x86_64__
typedef long double doublewordtype;
#else
typedef long double doublewordtype;
#endif
void xxz(int a, float b, short c, long d, double e, doublewordtype f,int& g, const int& h,int* i, const int* j){
/*cout << a << b << c << d << e << f << g << h << *i << *j << endl;*/
}
int z = 3;
//#define void int
void bb(){
++z;
cout << z << endl;
cout << endl;
}
void bb1(int& c){
++z;
cout << z << endl;
cout << c << endl;
}
void bb2(int &c, int d){
++z;
cout << z << endl;
cout << c << endl;
cout << d << endl;
}
#ifdef ENABLE_RIGHT_VALUE_REFERENCE
#include <utility>
int&& b3(int &&c, const int&& d){
cout << c << d << endl;
cout << "right_value_test" << endl;
return std::move(c);
}
struct global_struct1{
global_struct1(){
int x = 3, y = 6;
cout << call_in_stack(b3, 3, 6);
//C++ 0x support left value reference binding to right value reference
//But C++ 11 does not.
// cout << call_in_stack(b3, 3, y);
cout << call_in_stack(b3, std::move(x), std::move(y)) << endl;
}
}_test1;
#endif
__attribute__ ((noinline)) void tb(int a, int b, int c, int d, int e, int f,int g, short h,int i, short j, int k = 0, char l = 0){
cout << &a << endl;
cout << &g << endl;
cout << &h << endl;
cout << &i << endl;
cout << &j << endl;
cout << &k << endl;
cout<< &l << endl;
}
short t0(int a, int b, double c, const float d, const volatile double e, const double f,int &g, const int& h,int* i, const int& j){
doublewordtype xx = f;
cout << xx << a << b << c << d << e << f << g << h << *i << j << f << endl;
cout <<"typical0" << endl;
cout << endl;
return 0;
// about 4kB stack cost
}
double t8(int a, float b, double c, const float d, const volatile double e, const double f,int &g, const int& h,int* i, const int& j){
doublewordtype xx = f;
cout << xx << a << b << c << d << e << f << g << h << *i << j << f << endl;
cout <<"typical8" << endl;
cout << endl;
return -8;
// about 4kB stack cost
}
void t1(doublewordtype a, float b, double c, const float d, const volatile double e, const double f,int &g, const int& h,int* i, const int& j){
doublewordtype xx = f;
cout <<"typical1 begin" << endl;
cout << a << endl;
cout << b << c << d << xx << endl;
cout << e << endl;
cout<< f << g << h << *i << j << f << endl;
cout <<"typical1 end" << endl;
#ifdef __x86_64__
printf("%d%Lf\n",*i,a);
#else
printf("%d%Lf\n",*i,a);
//printf("%d%lld\n",*i,a);
#endif
cout << endl;
// about 4kB stack cost
}
typedef int(*new_type)(const char*);
new_type t2(int a, float b, doublewordtype c, long d, volatile int e, const double f,int &g, const int& h,int* i, const int& j){
doublewordtype xx = f;
//cout << &xx << &a << &b << &c << &d << &e << &f << &g << &h << &i << &j << &xx << endl;
cout << xx << a << b << c << d << e << f << g << h << *i << j << xx << endl;
cout <<"typical2" << endl;
#ifdef __x86_64__
printf("%d%d%Lf\n",*i,j,c);
#else
printf("%d%d%Lf\n",*i,j,c);
//printf("%d%d%lld\n",*i,j,c);
#endif
cout << endl;
return &puts;
// about 4kB stack cost
}
int t3(int a, float b, short c, long d, int e, const double f,int &g, const int& h,int* i, doublewordtype j){
doublewordtype xx = f;
cout << xx << a << b << c << d << e << f << g << h << *i << endl;
cout <<"typical3" << endl;
cout << j << xx << endl;
#ifdef __x86_64__
printf("%d%Lf\n",*i,j);
#else
printf("%d%Lf\n",*i,j);
//printf("%d%lld\n",*i,j);
#endif
cout << g << endl;
return g;
// about 4kB stack cost
}
char t4(int a, long long b, short c, long d, volatile int e, long long f,int &g, const int& h,int* i, doublewordtype j){
doublewordtype xx = f;
cout << &i << endl;
cout << &j << endl;
cout << xx << a << b << c << d << e << f << g << h << *i << endl;
cout <<"typical4" << endl;
cout << j << xx << endl;
#ifdef __x86_64__
printf("%d%Lf\n",*i,j);
#else
printf("%d%Lf\n",*i,j);
//printf("%d%lld\n",*i,j);
#endif
cout << endl;
return 'a';
// about 4kB stack cost
}
void long_double_test(){
//cout << (long double)123456789.1234567 << endl;
printf("%Lf\n",(long double)123456789.1234567);
}
char buf[24*1024];
int ibuf[2048];
void double_jump(int a, float b, doublewordtype c, long d, double e, const double f,int &g, const int& h,int* i, const int& j){
call_in_stack_safe(buf, sizeof(buf), t1, 1,2,3,4,5,6,++z, z, &z, z);
cout << "OK" << endl;
call_in_stack_safe(buf, sizeof(buf), t3, a,b,c,d,e,f,g,h,i,j);
cout << "OK" << endl;
call_in_stack_safe(ibuf,t2, a,b,c,d,e,f,g,h,i,j );
cout << "OK" << endl;
call_in_stack_safe(ibuf, t1, 1,2,3,4,5,6,++z, z, &z, z );
cout << "OK" << endl;
call_in_stack_safe(buf, bb );
cout<< "End" << endl;
}
void double_jump1(int a, float b, doublewordtype c, long d, double e, const double f,int &g, const int& h,int* i, const int& j){
call_in_stack_safe(buf, sizeof(buf), t1, 1,2,3,4,5,6,++z, z, &z, z);
}
struct ag{ag(int){};};
void ff(ag);
#include "cmath"
#include <string>
#include <stdarg.h>
struct class_test_v{
int p_src_v;
class_test_v():p_src_v(1234567890){}
virtual int vdummy(){
if(p_src_v == 1234567890){
cout << "functor_test: " << p_src_v << endl;
}
else{
throw p_src_v;
}
return 4444;
}
int dummy(){
if(p_src_v == 1234567890){
cout << "functor_test: " << p_src_v << endl;
}
else{
throw p_src_v;
}
return 4321;
}
};
struct class_test_a : public virtual class_test_v{
int p_src_a;
class_test_a():p_src_a(123456789){}
int printa(const char * format, const char* str, int d){
printf(format, str, d);
if(p_src_a == 123456789){
cout << "functor_test: " << p_src_a << endl;
}
else{
throw p_src_a;
}
return 8844;
}
virtual int vdummya(){
if(p_src_a == 123456789){
cout << "functor_test: " << p_src_a << endl;
}
else{
throw p_src_a;
}
return 8844;
}
virtual int vprinta(const char * format, const char* str, int d){
printf(format, str, d);
if(p_src_a == 123456789){
cout << "functor_test: " << p_src_a << endl;
}
else{
throw p_src_a;
}
return 8844;
}
};
struct class_test_b: public virtual class_test_v{
int p_src_b;
class_test_b():p_src_b(987654321){}
int printb(const char * format, const char* str, int d){
printf(format, str, d);
if(p_src_b == 987654321){
cout << "functor_test: " << p_src_b << endl;
}
else{
throw p_src_b;
}
return 4488;
}
virtual int vprintb(const char * format, const char* str, int d){
printf(format, str, d);
if(p_src_b == 987654321){
cout << "functor_test: " << p_src_b << endl;
}
else{
throw p_src_b;
}
return 4488;
}
};
struct empty_test{
char test[13];
};
struct class_test: public empty_test, public class_test_b, public class_test_a{
int src;
int operator()(const char * format, const char* str, int d){
printf(format, str, d);
cout << "functor_test: " << src << endl;
return 8866;
}
virtual int vprinta(const char * format, const char* str, int d){
printf(format, str, d);
cout << "functor_test: " << p_src_a << endl;
return 1234;
}
}functor_test;
FORCE_NOINLINE void float_output(double testnum){
//double x = testnum1;
printf("%f%f%f%f%f%f%f%f%f%f\n"
,testnum,testnum,testnum,testnum,testnum,testnum,testnum,testnum,testnum,testnum);
}
int main(){
float_output(1.2);
cout << "You should see \"TEST PASSED\" in the end if your compiling environment test passed! " << endl;
/* cout <<change_ref_to_pointer<doublewordtype>::size<< _COUNT_OF_SIZE(change_ref_to_pointer<doublewordtype>::size, WORDSIZE)<<endl;
cout << _ALIGNED_COST(_COUNT_OF_SIZE(change_ref_to_pointer<doublewordtype>::size, WORDSIZE), 2)<<endl;
args_list <int , float , doublewordtype , long , volatile int , const double ,int &, const int& ,int* , const int& >::out_stackword_cost(cout);
args_list <int , float , doublewordtype , long , volatile int , const double ,int &, const int& ,int* , const int& >::out_stack_padding_reporter(cout); */
char *d = (char*)malloc(sizeof(buf));
call_in_stack_safe(d, sizeof(buf), bb );
call_in_stack(buf, sizeof(buf) , bb );
call_in_stack(buf, bb );
call_in_stack( buf, &bb1, z );
call_in_stack(buf, bb2, z, 134);
//call_with_stack_exp_class<void>::call_with_stack_exp(z, 134, buf + sizeof(buf) - 8, bb2, bb1);
t1(12345.6789,2,3,4,5,6,++z, z, &z, z);
call_in_stack( buf, sizeof(buf), t1, 12345.6789,2,3,4,5,6,++z, z, &z, z);
//call_in_stack_impl::function_property<typeof(&t1)>::arg_list_type::out_stackword_cost(cout);
call_in_stack(buf ,t0, 1,2,3,4,5,6,++z, z, &z, z);
call_in_stack(buf ,t1 ,9.87654321,2,3,4,5,6,++z, z, &z, z);
call_in_stack(buf ,t8 ,1,2,3,4,5,6,++z, z, &z, z);
call_in_stack(buf ,sizeof(buf), t2, 1,2,3,4,5,6,++z, z, &z, 5 )("This is a jock");
call_in_stack(buf ,t2, 1,2,3,4,5,6,++z, z, &z, 5);
call_in_stack(d, sizeof(buf), bb );
call_in_stack(d, sizeof(buf), t1 , 1,2,3,4,5,6,++z, z, &z, z );
call_in_stack(d, sizeof(buf), bb );
free(d);
call_in_stack_safe(buf, sizeof(buf), double_jump, 1,2,3,4,5,6,++z, z, &z, z);
call_in_stack_safe(buf, double_jump, 1,2,3,4,5,6,++z, z, &z,z);
call_in_stack_safe(buf, double_jump1, 1,2,3,4,5,6,++z, z, &z, z);
// cout << args_list<int , long long , short, long, volatile int, long long ,int &, const int& ,int* >::stackword_cost << endl;
// cout << args_list<int , long long , short, long, volatile int, long long ,int &, const int& ,int* , doublewordtype>::stackword_cost << endl;
t3(1,2,3,4,5,6,z, z, &z, z );
call_in_stack(buf , t3, 1,2,3,4,5,6,++z, z, &z, z) ;
call_in_stack_safe(buf, t4, 1,2,3,4,5,6,++z, z, &z, z);
call_in_stack(8*1024, t1, 1,2,3,4,5,6,++z, z, &z, z );
cout << call_in_stack(buf, t3, 1,2,3,4,5,6,++z, z, &z, z ) << endl;
//cout << function_property<typeof(&printf)>::arguments_count << endl;
printf( "%d%d%p%f%d\n", 1, z, &z, (double)z, z+1);
cout << call_in_stack_impl::args_list<char const*, int, int, int*, double, int>::stackword_cost << endl;
call_in_stack(buf, &printf, "%d%d%p%f%d\n", 1, z, &z, (double)z, z+1);
call_in_stack(buf, &printf, "%d%d%p%f%d%s\n", 1, 2, buf, 3.0, 5, "Hello world!");
call_in_stack(buf, &printf, "%d%d%p%Lf%d%s\n", 1, 2, buf, 3.0L, 5, "Hello world!");
double (*plog)( double ) = &log;
cout << call_in_stack(4*1024, plog, 2.0) << endl;
float (*pflog)( float ) = &log;
cout << call_in_stack(12*1024,pflog, (float)2.0) << endl;
cout << call_in_stack((float (*)( float ))(&log), (float)2.0) << endl;
call_in_stack(4*1024, long_double_test);
string test_string("Hello world");
cout << call_in_stack(from_member_fun(test_string,size)) << endl;
cout << call_in_stack(from_nonvirtual_member_fun(test_string,size)) << endl;
cout << call_in_stack(from_member_fun(test_string,c_str)) << endl;
//sorry I can not support overloaded member function like following "at", because I can not give a convenient interface for you.
//cout << call_in_stack(from_member_fun(test_string,at) , 5) << endl;
call_in_stack(from_member_fun(test_string, reserve , 8));
call_in_stack(from_nonvirtual_member_fun(test_string, reserve , 8));
cout << call_in_stack(from_member_fun(test_string,data)) << endl;
functor_test.src = 1024;
cout << call_in_stack(from_functor(functor_test,"%s%d\n", "Hello boy,", 1024))<< endl;
functor_test.src = 4201;
cout << call_in_stack(from_functor(functor_test,"%s%d\n", "Hello boy,", 1024)) << endl;
cout << call_in_stack((*
(call_in_stack_impl::member_function_fast<__typeof__((&call_in_stack_impl::change_ref_to_pointer<__typeof__(functor_test)>::content_type::vprinta))>
::funtion_ptr_t*)(((char**)(call_in_stack_impl::member_function_fast<
__typeof__((&call_in_stack_impl::change_ref_to_pointer<__typeof__(functor_test)>::content_type::vprinta))>::class_type*)
(&functor_test))[0] +
((call_in_stack_impl::member_function_fast<__typeof__((&call_in_stack_impl::change_ref_to_pointer<__typeof__(functor_test)>::
content_type::vprinta))>)
((&call_in_stack_impl::change_ref_to_pointer<__typeof__(functor_test)>::content_type::vprinta))).un.vst.vtable_offset_1 - 1 ))
, &functor_test, "%s%d\n", "Hello boy,", 1024) << endl;
cout << call_in_stack(from_virtual_member_fun(functor_test,vprinta, "%s%d\n", "Hello boy,", 1024)) << endl;
cout << call_in_stack(from_virtual_member_fun(functor_test,vprintb, "%s%d\n", "Hello boy,", 1024)) << endl;
cout << call_in_stack(from_virtual_member_fun(functor_test,vdummy)) << endl;
cout << call_in_stack(from_nonvirtual_member_fun(functor_test,dummy)) << endl;
cout << call_in_stack(from_nonvirtual_member_fun(functor_test,printa, "%s%d\n", "Hello boy,", 1024)) << endl;
cout << call_in_stack(from_nonvirtual_member_fun(functor_test,printb, "%s%d\n", "Hello boy,", 1024)) << endl;
cout << "Ok !" << endl;
cout << "GCC_VERSION:" << GCC_VERSION << endl;
cout << "\"TEST PASSED\"" << endl;
return 0;
}