1 | /*
|
---|
2 | * Simple Test program for libtcc
|
---|
3 | *
|
---|
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>
|
---|
9 |
|
---|
10 | #include "libtcc.h"
|
---|
11 |
|
---|
12 | /* this function is called by the generated code */
|
---|
13 | int add(int a, int b)
|
---|
14 | {
|
---|
15 | return a + b;
|
---|
16 | }
|
---|
17 |
|
---|
18 | /* this strinc is referenced by the generated code */
|
---|
19 | const char hello[] = "Hello World!";
|
---|
20 |
|
---|
21 | char my_program[] =
|
---|
22 | "#include <tcclib.h>\n" /* include the "Simple libc header for TCC" */
|
---|
23 | "extern int add(int a, int b);\n"
|
---|
24 | "#ifdef _WIN32\n" /* dynamically linked data needs 'dllimport' */
|
---|
25 | " __attribute__((dllimport))\n"
|
---|
26 | "#endif\n"
|
---|
27 | "extern const char hello[];\n"
|
---|
28 | "int fib(int n)\n"
|
---|
29 | "{\n"
|
---|
30 | " if (n <= 2)\n"
|
---|
31 | " return 1;\n"
|
---|
32 | " else\n"
|
---|
33 | " return fib(n-1) + fib(n-2);\n"
|
---|
34 | "}\n"
|
---|
35 | "\n"
|
---|
36 | "int foo(int n)\n"
|
---|
37 | "{\n"
|
---|
38 | " printf(\"%s\\n\", hello);\n"
|
---|
39 | " printf(\"fib(%d) = %d\\n\", n, fib(n));\n"
|
---|
40 | " printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));\n"
|
---|
41 | " return 0;\n"
|
---|
42 | "}\n";
|
---|
43 |
|
---|
44 | int main(int argc, char **argv)
|
---|
45 | {
|
---|
46 | TCCState *s;
|
---|
47 | int i;
|
---|
48 | int (*func)(int);
|
---|
49 |
|
---|
50 | s = tcc_new();
|
---|
51 | if (!s) {
|
---|
52 | fprintf(stderr, "Could not create tcc state\n");
|
---|
53 | exit(1);
|
---|
54 | }
|
---|
55 |
|
---|
56 | /* if tcclib.h and libtcc1.a are not installed, where can we find them */
|
---|
57 | for (i = 1; i < argc; ++i) {
|
---|
58 | char *a = argv[i];
|
---|
59 | if (a[0] == '-') {
|
---|
60 | if (a[1] == 'B')
|
---|
61 | tcc_set_lib_path(s, a+2);
|
---|
62 | else if (a[1] == 'I')
|
---|
63 | tcc_add_include_path(s, a+2);
|
---|
64 | else if (a[1] == 'L')
|
---|
65 | tcc_add_library_path(s, a+2);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | /* MUST BE CALLED before any compilation */
|
---|
70 | tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
|
---|
71 |
|
---|
72 | if (tcc_compile_string(s, my_program) == -1)
|
---|
73 | return 1;
|
---|
74 |
|
---|
75 | /* as a test, we add symbols that the compiled program can use.
|
---|
76 | You may also open a dll with tcc_add_dll() and use symbols from that */
|
---|
77 | tcc_add_symbol(s, "add", add);
|
---|
78 | tcc_add_symbol(s, "hello", hello);
|
---|
79 |
|
---|
80 | /* relocate the code */
|
---|
81 | if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0)
|
---|
82 | return 1;
|
---|
83 |
|
---|
84 | /* get entry symbol */
|
---|
85 | func = tcc_get_symbol(s, "foo");
|
---|
86 | if (!func)
|
---|
87 | return 1;
|
---|
88 |
|
---|
89 | /* run the code */
|
---|
90 | func(32);
|
---|
91 |
|
---|
92 | /* delete the state */
|
---|
93 | tcc_delete(s);
|
---|
94 |
|
---|
95 | return 0;
|
---|
96 | }
|
---|