/* TODO: store the state globally or so.. */ static int r_lang_tcc_run(struct r_lang_t *lang, const char *code, int len) { TCCState *ts = tcc_new (); /* TODO: set defined vars as global */ //list_for_each(lang->defs) { tcc_compile_string (ts, code); tcc_run (ts, 0, 0);//argc, argv); tcc_delete (ts); return R_TRUE; }
void run_tccode(void) { char * ccode; size_t ccodelen; FILE * ofd; TCCState *s; int rv; void * memp; #ifdef __STRICT_ANSI__ void * iso_workaround; #endif libtcc_specials = 1; ofd = open_memstream(&ccode, &ccodelen); if (ofd == NULL) { perror("open_memstream"); exit(7); } print_ccode(ofd); putc('\0', ofd); fclose(ofd); memp = map_hugeram(); s = tcc_new(); if (s == NULL) { perror("tcc_new()"); exit(7); } tcc_set_output_type(s, TCC_OUTPUT_MEMORY); tcc_compile_string(s, ccode); tcc_add_symbol(s, "mem", memp); /* If our code was read from stdin it'll be done in standard mode, * the stdio stream is now modal (always a bad idea) so it's been switched * to standard mode, stupidly, it's now impossible to switch it back. * * So have the loaded C code use our getch and putch functions. * * The ugly casting is forced by the C99 standard as a (void*) is not a * valid cast for a function pointer. */ #ifdef __STRICT_ANSI__ *(void_func*) &iso_workaround = (void_func) &getch; tcc_add_symbol(s, "getch", iso_workaround); *(void_func*) &iso_workaround = (void_func) &putch; tcc_add_symbol(s, "putch", iso_workaround); #else tcc_add_symbol(s, "getch", &getch); tcc_add_symbol(s, "putch", &putch); #if defined(__TCCLIB_VERSION) && __TCCLIB_VERSION == 0x000925 #define TCCDONE { int (*func)(void); int imagesize; void * image = 0; if (verbose) fprintf(stderr, "Running C Code using libtcc 9.25.\n"); imagesize = tcc_relocate(s, 0); if (imagesize <= 0) { fprintf(stderr, "tcc_relocate failed to return code size.\n"); exit(1); } image = malloc(imagesize); rv = tcc_relocate(s, image); if (rv) { fprintf(stderr, "tcc_relocate failed error=%d\n", rv); exit(1); } /* * The ugly casting is forced by the C99 standard as a (void*) is not a * valid cast for a function pointer. * *(void **) (&func) = tcc_get_symbol(s, "main"); */ func = tcc_get_symbol(s, "main"); if (!func) { fprintf(stderr, "Could not find compiled code entry point\n"); exit(1); } tcc_delete(s); free(ccode); start_runclock(); func(); finish_runclock(&run_time, &io_time); free(image); } #endif #if defined(__TCCLIB_VERSION) && __TCCLIB_VERSION == 0x000926 #define TCCDONE { int (*func)(void); if (verbose) fprintf(stderr, "Running C Code using libtcc 9.26.\n"); rv = tcc_relocate(s); if (rv) { perror("tcc_relocate()"); fprintf(stderr, "tcc_relocate failed return value=%d\n", rv); exit(1); } /* * The ugly casting is forced by the C99 standard as a (void*) is not a * valid cast for a function pointer. *(void **) (&func) = tcc_get_symbol(s, "main"); */ func = tcc_get_symbol(s, "main"); if (!func) { fprintf(stderr, "Could not find compiled code entry point\n"); exit(1); } start_runclock(); func(); finish_runclock(&run_time, &io_time); tcc_delete(s); free(ccode); } #endif #endif #if !defined(TCCDONE) { static char arg0_tcclib[] = "tcclib"; static char * args[] = {arg0_tcclib, 0}; /* Hmm, I want to do the above without named initialisers ... so it looks like this ... but without the const problem. static char * args[] = {"tcclib", 0}; */ if (verbose) fprintf(stderr, "Running C Code using libtcc tcc_run() to compile & run.\n"); rv = tcc_run(s, 1, args); if (verbose && rv) fprintf(stderr, "tcc_run returned %d\n", rv); tcc_delete(s); free(ccode); } #endif }
int main(int argc, char **argv) { TCCState *s; int ret, optind, i; int64_t start_time = 0; s = tcc_new(); optind = tcc_parse_args(s, argc - 1, argv + 1); if (s->do_bench) start_time = getclock_us(); tcc_set_environment(s); if (optind == 0) { help(); return 1; } if (s->option_m) exec_other_tcc(s, argv, s->option_m); if (s->verbose) display_info(s, 0); if (s->print_search_dirs || (s->verbose == 2 && optind == 1)) { tcc_set_output_type(s, TCC_OUTPUT_MEMORY); display_info(s, 1); return 0; } if (s->verbose && optind == 1) return 0; if (s->nb_files == 0) tcc_error("no input files\n"); /* check -c consistency : only single file handled. XXX: checks file type */ if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) { if (s->nb_libraries != 0) tcc_error("cannot specify libraries with -c"); /* accepts only a single input file */ if ((s->nb_files != 1) && s->outfile) { tcc_error("cannot specify multiple files with -c and -o"); } } if (s->output_type == TCC_OUTPUT_PREPROCESS) { if (!s->outfile) { s->ppfp = stdout; } else { s->ppfp = fopen(s->outfile, "w"); if (!s->ppfp) tcc_error("could not write '%s'", s->outfile); } } tcc_set_output_type(s, s->output_type); /* compile or add each files or library */ for(i = ret = 0; i < s->nb_files && ret == 0; i++) { int filetype = *(unsigned char *)s->files[i]; const char *filename = s->files[i] + 1; if (filename[0] == '-' && filename[1] == 'l') { if (tcc_add_library(s, filename + 2) < 0) { tcc_error_noabort("cannot find library 'lib%s'", filename+2); ret = 1; } } else { if (1 == s->verbose) printf("-> %s\n", filename); if (!s->outfile) s->outfile = default_outputfile(s, filename); if (tcc_add_file(s, filename, filetype) < 0) ret = 1; else if (s->output_type == TCC_OUTPUT_OBJ) { ret = !!tcc_output_file(s, s->outfile); if (s->gen_deps && !ret) gen_makedeps(s, s->outfile, s->deps_outfile); if (!ret) { if ((i+1) < s->nb_files) { tcc_delete(s); s = tcc_new(); tcc_parse_args(s, argc - 1, argv + 1); tcc_set_environment(s); if (s->output_type != TCC_OUTPUT_OBJ) tcc_error("interlnal error"); tcc_set_output_type(s, s->output_type); } } } } } if (0 == ret) { if (s->output_type == TCC_OUTPUT_MEMORY) { #ifdef TCC_IS_NATIVE ret = tcc_run(s, argc - 1 - optind, argv + 1 + optind); #else tcc_error_noabort("-run is not available in a cross compiler"); ret = 1; #endif } else if (s->output_type == TCC_OUTPUT_EXE || s->output_type == TCC_OUTPUT_DLL) { ret = !!tcc_output_file(s, s->outfile); if (s->gen_deps && !ret) gen_makedeps(s, s->outfile, s->deps_outfile); } } if (s->do_bench) tcc_print_stats(s, getclock_us() - start_time); tcc_delete(s); return ret; }