コード例 #1
0
ファイル: abitest.c プロジェクト: Amorph/tcc
/*
 * 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;
}
コード例 #2
0
ファイル: libtcc_test.c プロジェクト: raldoni/tcc
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;
}
コード例 #3
0
ファイル: libtcc_test.c プロジェクト: 00shiv/Nimrod
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;
}
コード例 #4
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;
}
コード例 #5
0
ファイル: script_tcc.c プロジェクト: lieff/lvg
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;
}