x86-asm: Implement clflush opcode
[tinycc.git] / tests / libtcc_test.c
blobbe5db613e0b6bd39ee050a7fe9557c3bcc763641
1 /*
2 * Simple Test program for libtcc
4 * libtcc can be useful to use tcc as a "backend" for a code generator.
5 */
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
10 #include "libtcc.h"
12 /* this function is called by the generated code */
13 int add(int a, int b)
15 return a + b;
18 const char hello[] = "Hello World!";
20 char my_program[] =
21 "#include <tcclib.h>\n" /* include the "Simple libc header for TCC" */
22 "extern int add(int a, int b);\n"
23 "extern const char hello[];\n"
24 "int fib(int n)\n"
25 "{\n"
26 " if (n <= 2)\n"
27 " return 1;\n"
28 " else\n"
29 " return fib(n-1) + fib(n-2);\n"
30 "}\n"
31 "\n"
32 "int foo(int n)\n"
33 "{\n"
34 " printf(\"%s\\n\", hello);\n"
35 " printf(\"fib(%d) = %d\\n\", n, fib(n));\n"
36 " printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));\n"
37 " return 0;\n"
38 "}\n";
40 int main(int argc, char **argv)
42 TCCState *s;
43 int i;
44 int (*func)(int);
46 s = tcc_new();
47 if (!s) {
48 fprintf(stderr, "Could not create tcc state\n");
49 exit(1);
52 /* if tcclib.h and libtcc1.a are not installed, where can we find them */
53 for (i = 1; i < argc; ++i) {
54 char *a = argv[i];
55 if (a[0] == '-') {
56 if (a[1] == 'B')
57 tcc_set_lib_path(s, a+2);
58 else if (a[1] == 'I')
59 tcc_add_include_path(s, a+2);
60 else if (a[1] == 'L')
61 tcc_add_library_path(s, a+2);
65 /* MUST BE CALLED before any compilation */
66 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
68 if (tcc_compile_string(s, my_program) == -1)
69 return 1;
71 /* as a test, we add symbols that the compiled program can use.
72 You may also open a dll with tcc_add_dll() and use symbols from that */
73 tcc_add_symbol(s, "add", add);
74 tcc_add_symbol(s, "hello", hello);
76 /* relocate the code */
77 if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0)
78 return 1;
80 /* get entry symbol */
81 func = tcc_get_symbol(s, "foo");
82 if (!func)
83 return 1;
85 /* run the code */
86 func(32);
88 /* delete the state */
89 tcc_delete(s);
91 return 0;