realloc text section in code generation
[tinycc.git] / libtcc_test.c
blob1c87f78dccecaa3f64b883910086b39aa7cc96d3
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>
9 #include <libtcc.h>
11 /* this function is called by the generated code */
12 int add(int a, int b)
14 return a + b;
17 char my_program[] =
18 "int fib(int n)\n"
19 "{\n"
20 " if (n <= 2)\n"
21 " return 1;\n"
22 " else\n"
23 " return fib(n-1) + fib(n-2);\n"
24 "}\n"
25 "\n"
26 "int main(int argc, char **argv)\n"
27 "{\n"
28 " int n;\n"
29 " n = atoi(argv[1]);\n"
30 " printf(\"Hello World!\\n\");\n"
31 " printf(\"fib(%d) = %d\\n\", n, fib(n));\n"
32 " printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));\n"
33 " return 0;\n"
34 "}\n";
36 int main(int argc, char **argv)
38 TCCState *s;
39 char *args[3];
41 s = tcc_new();
42 if (!s) {
43 fprintf(stderr, "Could not create tcc state\n");
44 exit(1);
47 tcc_compile_string(s, my_program);
49 /* as a test, we add a symbol that the compiled program can be
50 linked with. You can have a similar result by opening a dll
51 with tcc_add_dll(() and using its symbols directly. */
52 tcc_add_symbol(s, "add", (unsigned long)&add);
54 args[0] = "";
55 args[1] = "32";
56 args[2] = NULL;
58 tcc_run(s, 2, args);
60 tcc_delete(s);
61 return 0;