tccgen: Allow struct init from struct
[tinycc.git] / tests / libtcc_test_mt.c
blob6892bfef2e7981cc6ce8244bd23992481a0e4790
1 /*
2 * Multi-thread Test for libtcc
3 */
5 #ifndef FIB
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include "libtcc.h"
11 #define M 20 /* number of states */
12 #define F(n) (n % 20 + 2) /* fib argument */
14 #ifdef _WIN32
15 #include <windows.h>
16 #define TF_TYPE(func, param) DWORD WINAPI func(void *param)
17 typedef TF_TYPE(ThreadFunc, x);
18 HANDLE hh[M];
19 void create_thread(ThreadFunc f, int n)
21 DWORD tid;
22 hh[n] = CreateThread(NULL, 0, f, (void*)(size_t)n, 0, &tid);
24 void wait_threads(int n)
26 WaitForMultipleObjects(n, hh, TRUE, INFINITE);
27 while (n)
28 CloseHandle(hh[--n]);
30 void sleep_ms(unsigned n)
32 Sleep(n);
34 #else
35 #include <sys/time.h>
36 #include <unistd.h>
37 #include <pthread.h>
38 #define TF_TYPE(func, param) void* func(void *param)
39 typedef TF_TYPE(ThreadFunc, x);
40 pthread_t hh[M];
41 void create_thread(ThreadFunc f, int n)
43 pthread_create(&hh[n], NULL, f, (void*)(size_t)n);
45 void wait_threads(int n)
47 while (n)
48 pthread_join(hh[--n], NULL);
51 void sleep_ms(unsigned n)
53 usleep(n * 1000);
55 #endif
57 void handle_error(void *opaque, const char *msg)
59 fprintf(opaque, "%s\n", msg);
62 /* this function is called by the generated code */
63 int add(int a, int b)
65 return a + b;
68 #define _str(s) #s
69 #define str(s) _str(s)
70 /* as a trick, prepend #line directive for better error/warning messages */
71 #define PROG(lbl) \
72 char lbl[] = "#line " str(__LINE__) " " str(__FILE__) "\n\n"
74 PROG(my_program)
75 "#include <tcclib.h>\n" /* include the "Simple libc header for TCC" */
76 "int add(int a, int b);\n"
77 "int fib(int n)\n"
78 "{\n"
79 " if (n <= 2)\n"
80 " return 1;\n"
81 " else\n"
82 " return add(fib(n-1),fib(n-2));\n"
83 "}\n"
84 "\n"
85 "int foo(int n)\n"
86 "{\n"
87 " printf(\" %d\", fib(n));\n"
88 " return 0;\n"
89 "# warning is this the correct file:line...\n"
90 "}\n";
92 int g_argc; char **g_argv;
94 void parse_args(TCCState *s)
96 int i;
97 /* if tcclib.h and libtcc1.a are not installed, where can we find them */
98 for (i = 1; i < g_argc; ++i) {
99 char *a = g_argv[i];
100 if (a[0] == '-') {
101 if (a[1] == 'B')
102 tcc_set_lib_path(s, a+2);
103 else if (a[1] == 'I')
104 tcc_add_include_path(s, a+2);
105 else if (a[1] == 'L')
106 tcc_add_library_path(s, a+2);
107 else if (a[1] == 'D')
108 tcc_define_symbol(s, a+2, NULL);
113 TCCState *new_state(int w)
115 TCCState *s = tcc_new();
116 if (!s) {
117 fprintf(stderr, __FILE__ ": could not create tcc state\n");
118 exit(1);
120 tcc_set_error_func(s, stdout, handle_error);
121 parse_args(s);
122 if (!w) tcc_set_options(s, "-w");
123 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
124 return s;
127 void *reloc_state(TCCState *s, const char *entry)
129 void *func;
130 tcc_add_symbol(s, "add", add);
131 if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0) {
132 fprintf(stderr, __FILE__ ": could not relocate tcc state.\n");
133 return NULL;
135 func = tcc_get_symbol(s, entry);
136 if (!func)
137 fprintf(stderr, __FILE__ ": could not get entry symbol.\n");
138 return func;
141 /* work with several states at the same time */
142 int state_test(void)
144 TCCState *s[M];
145 int (*func[M])(int);
146 int n;
148 for (n = 0; n < M + 4; ++n) {
149 unsigned a = n, b = n - 1, c = n - 2, d = n - 3, e = n - 4;
150 if (a < M)
151 s[a] = new_state(0);
152 if (b < M)
153 if (tcc_compile_string(s[b], my_program) == -1)
154 break;
155 if (c < M)
156 func[c] = reloc_state(s[c], "foo");
157 if (d < M && func[d])
158 func[d](F(d));
159 if (e < M)
160 tcc_delete(s[e]);
162 return 0;
165 /* simple compilation in threads */
166 TF_TYPE(thread_test_simple, vn)
168 TCCState *s;
169 int (*func)(int);
170 int ret;
171 int n = (size_t)vn;
173 s = new_state(0);
174 sleep_ms(1);
175 ret = tcc_compile_string(s, my_program);
176 sleep_ms(1);
177 if (ret >= 0) {
178 func = reloc_state(s, "foo");
179 if (func)
180 func(F(n));
182 tcc_delete(s);
183 return 0;
186 /* more complex compilation in threads */
187 TF_TYPE(thread_test_complex, vn)
189 TCCState *s;
190 int ret;
191 int n = (size_t)vn;
192 char *argv[30], b[10];
193 int argc = 0, i;
195 sprintf(b, "%d", F(n));
197 for (i = 1; i < g_argc; ++i) argv[argc++] = g_argv[i];
198 #if 0
199 argv[argc++] = "-run";
200 for (i = 1; i < g_argc; ++i) argv[argc++] = g_argv[i];
201 #endif
202 argv[argc++] = "-DFIB";
203 argv[argc++] = "-run";
204 argv[argc++] = __FILE__;
205 argv[argc++] = b;
206 argv[argc] = NULL;
208 s = new_state(1);
209 sleep_ms(2);
210 ret = tcc_add_file(s, argv[0]);
211 sleep_ms(3);
212 if (ret < 0)
213 exit(1);
214 tcc_run(s, argc, argv);
215 tcc_delete(s);
216 fflush(stdout);
217 return 0;
220 void time_tcc(int n, const char *src)
222 TCCState *s;
223 int ret, i = 0;
224 while (i++ < n) {
225 s = new_state(1);
226 printf(" %d", i), fflush(stdout);
227 ret = tcc_add_file(s, src);
228 tcc_delete(s);
229 if (ret < 0)
230 exit(1);
234 static unsigned getclock_ms(void)
236 #ifdef _WIN32
237 return GetTickCount();
238 #else
239 struct timeval tv;
240 gettimeofday(&tv, NULL);
241 return tv.tv_sec*1000 + (tv.tv_usec+500)/1000;
242 #endif
245 int main(int argc, char **argv)
247 int n;
248 unsigned t;
250 g_argc = argc;
251 g_argv = argv;
253 if (argc < 2) {
254 fprintf(stderr, "usage: libtcc_test_mt tcc.c <options>\n");
255 return 1;
258 #if 1
259 printf("running fib with mixed calls\n "), fflush(stdout);
260 t = getclock_ms();
261 state_test();
262 printf("\n (%u ms)\n", getclock_ms() - t);
263 #endif
264 #if 1
265 printf("running fib in threads\n "), fflush(stdout);
266 t = getclock_ms();
267 for (n = 0; n < M; ++n)
268 create_thread(thread_test_simple, n);
269 wait_threads(n);
270 printf("\n (%u ms)\n", getclock_ms() - t);
271 #endif
272 #if 1
273 printf("running tcc.c in threads to run fib\n "), fflush(stdout);
274 t = getclock_ms();
275 for (n = 0; n < M; ++n)
276 create_thread(thread_test_complex, n);
277 wait_threads(n);
278 printf("\n (%u ms)\n", getclock_ms() - t);
279 #endif
280 #if 1
281 printf("compiling tcc.c 10 times\n "), fflush(stdout);
282 t = getclock_ms();
283 time_tcc(10, argv[1]);
284 printf("\n (%u ms)\n", getclock_ms() - t), fflush(stdout);
285 #endif
286 return 0;
289 #else
290 #include <tcclib.h>
291 int fib(n)
293 return (n <= 2) ? 1 : fib(n-1) + fib(n-2);
296 int main(int argc, char **argv)
298 printf(" %d", fib(atoi(argv[1]), 2));
299 return 0;
301 #endif