/*- Author: Brian Tiffin Dedicated to the public domain Date started: January 2017 Modified: 2017-01-08/13:44-0500 Tectonics: gcc -o library.so -shared -fpic program.c +*/ /* program.c description */ #include #include #include #include "libtcc.h" #include "icall.h" int unitcc(int argc, descriptor argv[]) { /* Crank up tcc */ TCCState *s; int (*func)(int); int result; /* First arg is the C code */ ArgString(1); /* Second arg is the integer parameter */ ArgInteger(2); s = tcc_new(); if (!s) { fprintf(stderr, "Could not create tcc state\n"); exit(1); } /* if tcclib.h and libtcc1.a are not installed, where can we find them */ /* if (argc == 2 && !memcmp(argv[1], "lib_path=",9)) tcc_set_lib_path(s, argv[1]+9); */ /* MUST BE CALLED before any compilation */ tcc_set_output_type(s, TCC_OUTPUT_MEMORY); if (tcc_compile_string(s, StringVal(argv[1])) == -1) return 1; /* as a test, we add a symbol that the compiled program can use. You may also open a dll with tcc_add_dll() and use symbols from that */ /* tcc_add_symbol(s, "add", add); */ /* relocate the code */ if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0) return 1; /* get entry symbol, Unicon passes code that compiles trytcc */ func = tcc_get_symbol(s, "trytcc"); if (!func) return 1; /* run the code */ result = func(IntegerVal(argv[2])); /* delete the state */ tcc_delete(s); RetInteger(result); }