Exemple #1
0
static cfunc codegen( TCCState *s, const char *prog, const char *fname )
{
    unsigned long val;

    if (s) 
       tcc_delete(s);
    
    s = tcc_new();
    if (!s) 
       return 0;

    tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
    tcc_compile_string(s, prog);

/*     tcc_add_dll("/usr/lib/libm.so"); */

    tcc_add_symbol(s, "TEX", (unsigned long)&TEX);
    tcc_add_symbol(s, "TXB", (unsigned long)&TXB);
    tcc_add_symbol(s, "TXP", (unsigned long)&TXP);


    tcc_relocate(s);
    tcc_get_symbol(s, &val, fname);
    return (cfunc) val;
}
Exemple #2
0
int main(int argc, char **argv)
{
    TCCState *s;
    int (*func)(int);
    unsigned long val;
    
    s = tcc_new();
    if (!s) {
        fprintf(stderr, "Could not create tcc state\n");
        exit(1);
    }

    /* MUST BE CALLED before any compilation or file loading */
    tcc_set_output_type(s, TCC_OUTPUT_MEMORY);

    tcc_compile_string(s, my_program);

    /* as a test, we add a symbol that the compiled program can be
       linked with. You can have a similar result by opening a dll
       with tcc_add_dll(() and using its symbols directly. */
    tcc_add_symbol(s, "add", (unsigned long)&add);
    
    tcc_relocate(s);

    tcc_get_symbol(s, &val, "foo");
    func = (void *)val;

    func(32);

    tcc_delete(s);
    return 0;
}
Exemple #3
0
int main(int argc, char **argv)
{
    TCCState *s;
    int (*foobar_func)(int);

    if(NULL == (s = tcc_new())){
            fprintf(stderr, "Allocation of a new TCC compilation enviroment failed\n");
            return 1;
    }

    tcc_set_output_type(s, TCC_OUTPUT_MEMORY); /*Do everything in memory!*/

    if(tcc_compile_string(s, my_program) > 0) 
            return -1;

    tcc_add_symbol(s, "add", add); /*add in that missing symbol*/

    tcc_relocate(s);

    foobar_func = tcc_get_symbol(s, "foobar");

    printf("foobar returned: %d\n", foobar_func(7));

    tcc_delete(s);

    return 0;
}
Exemple #4
0
void provide_symbols(TCCState *tccs) {
    if(hashtable_count(symbol_table)) {
        struct hashtable_itr *itr = hashtable_iterator(symbol_table);
        do {
            struct symbol *s = hashtable_iterator_value(itr);
            tcc_add_symbol(tccs, s->name, s->addr);
        } while(hashtable_iterator_advance(itr));
        free(itr);
    }
}
Exemple #5
0
int main(int argc, char **argv)
{
    TCCState *s;
    int i;
    int (*func)(int);

    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 */
    for (i = 1; i < argc; ++i) {
        char *a = argv[i];
        if (a[0] == '-') {
            if (a[1] == 'B')
                tcc_set_lib_path(s, a+2);
            else if (a[1] == 'I')
                tcc_add_include_path(s, a+2);
            else if (a[1] == 'L')
                tcc_add_library_path(s, a+2);
        }
    }

    /* MUST BE CALLED before any compilation */
    tcc_set_output_type(s, TCC_OUTPUT_MEMORY);

    if (tcc_compile_string(s, my_program) == -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 */
    func = tcc_get_symbol(s, "foo");
    if (!func)
        return 1;

    /* run the code */
    func(32);

    /* delete the state */
    tcc_delete(s);

    return 0;
}
Exemple #6
0
int main(int argc, char **argv)
{
    TCCState *s;
    int (*func)(int);
    void *mem;
    int size;

    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, my_program) == -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);

    /* get needed size of the code */
    size = tcc_relocate(s, NULL);
    if (size == -1)
        return 1;

    /* allocate memory and copy the code into it */
    mem = malloc(size);
    tcc_relocate(s, mem);

    /* get entry symbol */
    func = tcc_get_symbol(s, "foo");
    if (!func)
        return 1;

    /* delete the state */
    tcc_delete(s);

    /* run the code */
    func(32);

    free(mem);
    return 0;
}
Exemple #7
0
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
}
Exemple #8
0
ST_FUNC int tcc_load_coff(TCCState * s1, int fd)
{
// tktk TokenSym *ts;

    FILE *f;
    unsigned int str_size;
    char *Coff_str_table, *name;
    int i, k;
    struct syment csym;
    char name2[9];
    FILHDR file_hdr;		/* FILE HEADER STRUCTURE              */

    f = fdopen(fd, "rb");
    if (!f) {
	tcc_error("Unable to open .out file for input");
    }

    if (fread(&file_hdr, FILHSZ, 1, f) != 1)
	tcc_error("error reading .out file for input");

    if (fread(&o_filehdr, sizeof(o_filehdr), 1, f) != 1)
	tcc_error("error reading .out file for input");

    // first read the string table

    if (fseek(f, file_hdr.f_symptr + file_hdr.f_nsyms * SYMESZ, SEEK_SET))
	tcc_error("error reading .out file for input");

    if (fread(&str_size, sizeof(int), 1, f) != 1)
	tcc_error("error reading .out file for input");


    Coff_str_table = (char *) tcc_malloc(str_size);

    if (fread(Coff_str_table, str_size - 4, 1, f) != 1)
	tcc_error("error reading .out file for input");

    // read/process all the symbols

    // seek back to symbols

    if (fseek(f, file_hdr.f_symptr, SEEK_SET))
	tcc_error("error reading .out file for input");

    for (i = 0; i < file_hdr.f_nsyms; i++) {
	if (fread(&csym, SYMESZ, 1, f) != 1)
	    tcc_error("error reading .out file for input");

	if (csym._n._n_n._n_zeroes == 0) {
	    name = Coff_str_table + csym._n._n_n._n_offset - 4;
	} else {
	    name = csym._n._n_name;

	    if (name[7] != 0) {
		for (k = 0; k < 8; k++)
		    name2[k] = name[k];

		name2[8] = 0;

		name = name2;
	    }
	}
//              if (strcmp("_DAC_Buffer",name)==0)  // tktk
//                      name[0]=0;

	if (((csym.n_type & 0x30) == 0x20 && csym.n_sclass == 0x2) || ((csym.n_type & 0x30) == 0x30 && csym.n_sclass == 0x2) || (csym.n_type == 0x4 && csym.n_sclass == 0x2) || (csym.n_type == 0x8 && csym.n_sclass == 0x2) ||	// structures
	    (csym.n_type == 0x18 && csym.n_sclass == 0x2) ||	// pointer to structure
	    (csym.n_type == 0x7 && csym.n_sclass == 0x2) ||	// doubles
	    (csym.n_type == 0x6 && csym.n_sclass == 0x2))	// floats
	{
	    // strip off any leading underscore (except for other main routine)

	    if (name[0] == '_' && strcmp(name, "_main") != 0)
		name++;

	    tcc_add_symbol(s1, name, (void*)(uplong)csym.n_value);
	}
	// skip any aux records

	if (csym.n_numaux == 1) {
	    if (fread(&csym, SYMESZ, 1, f) != 1)
		tcc_error("error reading .out file for input");
	    i++;
	}
    }

    return 0;
}
Exemple #9
0
int main(int argc, char **argv)
{
    TCCState *s;
    int (*func)(int);

    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, my_program_1) == -1)
        return 1;
    if (tcc_compile_string(s, my_program_2) == -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 */
    func = tcc_get_symbol(s, "foo");
    if (!func)
        return 1;

    /* run the code */
    func(32);

    /* delete the state */
    tcc_delete(s);

// start again


    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, my_program_1) == -1)
        return 1;
    // if (tcc_compile_string(s, my_program_2) == -1)
    //     return 1;
    if (tcc_compile_string(s, my_program_3) == -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 */
    func = tcc_get_symbol(s, "foo");
    if (!func)
        return 1;

    /* run the code */
    func(32);

    /* delete the state */
    tcc_delete(s);



    return 0;
}
Exemple #10
0
void addSymbols(TCCState* s)
{
	tcc_add_symbol(s, "keystat", &keystat);
	tcc_add_symbol(s, "mousestat", &mousestat);
	tcc_add_symbol(s, "mousex", &mousex);
	tcc_add_symbol(s, "mousey", &mousey);
	tcc_add_symbol(s, "mousexr", &mousexr);
	tcc_add_symbol(s, "mouseyr", &mouseyr);
	tcc_add_symbol(s, "xres", &xres);
	tcc_add_symbol(s, "yres", &yres);
	tcc_add_symbol(s, "scratchpad", &scratchpad);

	tcc_add_symbol(s, "clearColor", (void*)clearColor);

	// IMGUI
	tcc_add_symbol(s, "guiBeginFrame", (void*)imguiBeginFrame);
	tcc_add_symbol(s, "guiEndFrame", (void*)imguiEndFrame);
	tcc_add_symbol(s, "guiBeginScrollArea", (void*)imguiBeginScrollArea);
	tcc_add_symbol(s, "guiEndScrollArea", (void*)imguiEndScrollArea);

	tcc_add_symbol(s, "guiIndent", (void*)imguiIndent);
	tcc_add_symbol(s, "guiUnindent", (void*)imguiUnindent);
	tcc_add_symbol(s, "guiSeparator", (void*)imguiSeparator);
	tcc_add_symbol(s, "guiSeparatorLine", (void*)imguiSeparatorLine);

	tcc_add_symbol(s, "guiButton", (void*)imguiButton);
	tcc_add_symbol(s, "guiItem", (void*)imguiItem);
	tcc_add_symbol(s, "guiCheck", (void*)imguiCheck);
	tcc_add_symbol(s, "guiCollapse", (void*)imguiCollapse);
	tcc_add_symbol(s, "guiLabel", (void*)imguiLabel);
	tcc_add_symbol(s, "guiValue", (void*)imguiValue);
	tcc_add_symbol(s, "guiSlider", (void*)imguiSlider);

	tcc_add_symbol(s, "guiDrawText", (void*)imguiDrawText);
	tcc_add_symbol(s, "guiDrawLine", (void*)imguiDrawLine);
	tcc_add_symbol(s, "guiDrawRoundedRect", (void*)imguiDrawRoundedRect);
	tcc_add_symbol(s, "guiDrawRect", (void*)imguiDrawRect);
}
Exemple #11
0
int loadScript()
{
    TCCState *s;
    char *buf, *source;
    int size, i;

    if (!(buf = lvgGetFileContents("main.c", 0)))
    {
        printf("error: could not open C script.\n");
        return -1;
    }
    source = malloc(strlen((char *)allh_h) + strlen(buf) + 1);
    source[0] = 0;
    strcat(source, (char *)allh_h);
    strcat(source, buf);
    free(buf);

    s = tcc_new();
    tcc_set_error_func(s, 0, tcc_error_func);
    //tcc_set_options(s, "-g");
    tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
    tcc_set_lib_path(s, "./lib");

    if (tcc_compile_string(s, source) == -1)
        goto error;

    for (i = 0; i < sizeof(g_syms)/sizeof(g_syms[0]); i++)
        tcc_add_symbol(s, g_syms[i].m_name, g_syms[i].m_sym);
    tcc_add_symbol(s, "glCreateProgram", glCreateProgram);
    tcc_add_symbol(s, "glCreateShader", glCreateShader);
    tcc_add_symbol(s, "glShaderSource", glShaderSource);
    tcc_add_symbol(s, "glCompileShader", glCompileShader);
    tcc_add_symbol(s, "glGetShaderiv", glGetShaderiv);
    tcc_add_symbol(s, "glGetShaderInfoLog", glGetShaderInfoLog);
    tcc_add_symbol(s, "glAttachShader", glAttachShader);
    tcc_add_symbol(s, "glLinkProgram", glLinkProgram);
    tcc_add_symbol(s, "glGetProgramiv", glGetProgramiv);
    tcc_add_symbol(s, "glGetUniformLocation", glGetUniformLocation);
    tcc_add_symbol(s, "glGetAttribLocation", glGetAttribLocation);
    tcc_add_symbol(s, "glGenBuffers", glGenBuffers);
#ifdef GL3
    //tcc_add_symbol(s, "glGenVertexArrays", glGenVertexArrays);
    //tcc_add_symbol(s, "glBindVertexArray", glBindVertexArray);
#endif
    tcc_add_symbol(s, "glBindBuffer", glBindBuffer);
    tcc_add_symbol(s, "glEnableVertexAttribArray", glEnableVertexAttribArray);
    tcc_add_symbol(s, "glVertexAttribPointer", glVertexAttribPointer);
    tcc_add_symbol(s, "glBindTexture", glBindTexture);
    tcc_add_symbol(s, "glGenTextures", glGenTextures);
    tcc_add_symbol(s, "glTexParameteri", glTexParameteri);
    tcc_add_symbol(s, "glTexImage2D", glTexImage2D);
    tcc_add_symbol(s, "glDetachShader", glDetachShader);
    tcc_add_symbol(s, "glDeleteShader", glDeleteShader);
    tcc_add_symbol(s, "glDeleteProgram", glDeleteProgram);
    tcc_add_symbol(s, "glDeleteTextures", glDeleteTextures);
    tcc_add_symbol(s, "glDeleteBuffers", glDeleteBuffers);
    tcc_add_symbol(s, "glEnable", glEnable);
    tcc_add_symbol(s, "glBlendEquation", glBlendEquation);
    tcc_add_symbol(s, "glBlendFunc", glBlendFunc);
    tcc_add_symbol(s, "glDisable", glDisable);
    tcc_add_symbol(s, "glActiveTexture", glActiveTexture);
    tcc_add_symbol(s, "glUseProgram", glUseProgram);
    tcc_add_symbol(s, "glUniform1i", glUniform1i);
    tcc_add_symbol(s, "glUniformMatrix4fv", glUniformMatrix4fv);
    tcc_add_symbol(s, "glViewport", glViewport);
    tcc_add_symbol(s, "glBufferData", glBufferData);
    tcc_add_symbol(s, "glMapBuffer", glMapBuffer);
#ifdef GL3
    //tcc_add_symbol(s, "glMapBufferRange", glMapBufferRange);
#endif
    tcc_add_symbol(s, "glUnmapBuffer", glUnmapBuffer);
    tcc_add_symbol(s, "glScissor", glScissor);
    tcc_add_symbol(s, "glDrawElements", glDrawElements);
    tcc_add_symbol(s, "glPushAttrib", glPushAttrib);
    tcc_add_symbol(s, "glViewport", glViewport);
    tcc_add_symbol(s, "glMatrixMode", glMatrixMode);
    tcc_add_symbol(s, "glPushMatrix", glPushMatrix);
    tcc_add_symbol(s, "glLoadIdentity", glLoadIdentity);
    tcc_add_symbol(s, "glOrtho", glOrtho);
    tcc_add_symbol(s, "glEnableClientState", glEnableClientState);

    size = tcc_relocate(s, TCC_RELOCATE_AUTO);
    if (size == -1)
        goto error;

    onInit  = tcc_get_symbol(s, "onInit");
    onFrame = tcc_get_symbol(s, "onFrame");
    free(source);
    return 0;
error:
    free(source);
    return -1;
}