Пример #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;
}
Пример #2
0
int main(int, char **)
{
    char program[] = "int foo() { return 123; }";
    TCCState *s = tcc_new();

    tcc_set_output_type(s, TCC_OUTPUT_MEMORY);

    if (tcc_compile_string(s, program) != -1)
    {
        int size = tcc_relocate(s, nullptr);
        if (size > 0)
        {
            void *mem = malloc(size);
            tcc_relocate(s, mem);

            int(*foo)() = (int(*)())tcc_get_symbol(s, "foo");
            if (foo != nullptr)
            {
                printf("%d", foo());
            }

            free(mem);
        }
    }
    tcc_delete(s);

    return 0;
}
Пример #3
0
/*
 * Compile source code and call a callback with a pointer to the symbol "f".
 */
static int run_callback(const char *src, callback_type callback) {
  TCCState *s;
  int result;
  void *ptr;
  
  s = tcc_new();
  if (!s)
    return -1;
  if (tccdir)
    tcc_set_lib_path(s, tccdir);
  if (include_dir) {
    if (tcc_add_include_path(s, include_dir) == -1)
      return -1;
  }
  if (tcc_set_output_type(s, TCC_OUTPUT_MEMORY) == -1)
    return -1;
  if (tcc_compile_string(s, src) == -1)
    return -1;
  if (tcc_relocate(s, TCC_RELOCATE_AUTO) == -1)
    return -1;
  
  ptr = tcc_get_symbol(s, "f");
  if (!ptr)
    return -1;
  result = callback(ptr);
  
  tcc_delete(s);
  
  return result;
}
Пример #4
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;
}
Пример #5
0
/*time_t lastmodtime;*/
int recompile()
{
	/*
	struct stat file_stat;
	int err = stat("update.c", &file_stat);

	if( !g_tcc )
	{
	lastmodtime = file_stat.st_mtime;		
	}
	else
	{
	if( lastmodtime == file_stat.st_mtime )
	return 0;
	lastmodtime = file_stat.st_mtime;
	}
	*/
	printf("recompile\n");
	FILE* f = fopen( "update.c", "rb" );	// MUST be "rb" on windows - othersize CRLF will be converted to \n, and file size will be wrong (CRLF:2bytes, \n:1bytes)
	fseek(f, 0L, SEEK_END);
	long sz = ftell(f);
	fseek(f, 0L, SEEK_SET);
	char* program = (char*)malloc( sz+1 );
	fread( program, sz, 1, f ); 
	program[sz] = '\0';
	fclose(f);

	TCCState* s = tcc_new();
	tcc_set_output_type(s, TCC_OUTPUT_MEMORY);

	int n;
	tcc_set_error_func(s, &n, compile_err);

	addSymbols(s);

	int compileResult = tcc_compile_string(s, program);
	free( program );

	if ( compileResult == -1)
	{
		return 1;
	}
	
	tcc_add_library_path(s,".");
	
	/* relocate the code */
	if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0)
		return 1;

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

	if( g_tcc ) tcc_delete(g_tcc);
	g_tcc = s;

	printf("recompile ok\n");
	return 0;
}
Пример #6
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;
}
Пример #7
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;
}
Пример #8
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;
}
Пример #9
0
PyObject* tcc_compil(PyObject* self, PyObject* args)
{
	char* func_name;
	char* func_code;
	int (*entry)(void);
	TCCState *tcc_state = NULL;
	PyObject* ret;

	tcc_state = tcc_init_state();

	if (!PyArg_ParseTuple(args, "ss", &func_name, &func_code))
		return NULL;

	if (tcc_compile_string(tcc_state, func_code) != 0) {
		fprintf(stderr, "Erreur de compilation !\n");
		fprintf(stderr, "%s\n", func_code);
		exit(1);
	}
	/* XXX configure tinycc install with --disable-static */
	if (tcc_relocate(tcc_state, TCC_RELOCATE_AUTO) < 0) {
		fprintf(stderr, "tcc relocate error\n");
		exit(1);
	}
	entry = tcc_get_symbol(tcc_state, func_name);
	if (!entry){
		fprintf(stderr, "Erreur de symbole %s!\n", func_name);
		fprintf(stderr, "%s\n", func_name);
		exit(1);
	}

	ret = PyTuple_New(2);
	if (ret == NULL) {
		fprintf(stderr, "Erreur alloc %s!\n", func_name);
		fprintf(stderr, "%s\n", func_name);
		exit(1);
	}

	PyTuple_SetItem(ret, 0, PyLong_FromUnsignedLongLong((intptr_t) tcc_state));
	PyTuple_SetItem(ret, 1, PyLong_FromUnsignedLongLong((intptr_t) entry));

	return ret;

}
Пример #10
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
}
Пример #11
0
Файл: pitu.c Проект: fglock/Pitu
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;
}
Пример #12
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;
}