Contenu connexe
Similaire à Libtcc and gwan
Similaire à Libtcc and gwan (20)
Plus de DaeMyung Kang (20)
Libtcc and gwan
- 5. C Script
#! /usr/bin/tcc -run
int main()
{
printf("Hello Worldn");
return 0;
}
- 7. Code Generation
int add(int a, int b)
{
return a + b;
}
char my_program[] =
"int foo(int n)n"
"{n"
" printf("Hello World!n");n"
" printf("fib(%d) = %dn", n, fib(n));n"
" printf("add(%d, %d) = %dn", n, 2 * n, add(n, 2 * n));n"
" return 0;n"
"}n";
- 8. Code Generation
TCCState *s;
s = tcc_new();
tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
if (tcc_compile_string(s, my_program) == -1)
return 1;
tcc_add_symbol(s, "add", add);
size = tcc_relocate(s, NULL);
if (size == -1)
return 1;
mem = malloc(size);
tcc_relocate(s, mem);
func = tcc_get_symbol(s, "foo");
if (!func)
return 1;
tcc_delete(s);
func(32);
free(mem);