tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Tinycc-devel] multiple states in parallel?


From: Rainer Machne
Subject: [Tinycc-devel] multiple states in parallel?
Date: Tue, 15 May 2007 19:42:36 +0200
User-agent: Thunderbird 1.5.0.10 (X11/20070302)

Hi,

I am currently trying to integrate TCC in our software. It so far works great and we get very nice performance enhancements.

These emails in your archive are related to my problem, I think:

http://lists.gnu.org/archive/html/tinycc-devel/2005-10/msg00047.html
http://lists.gnu.org/archive/html/tinycc-devel/2003-05/msg00003.html
http://lists.gnu.org/archive/html/tinycc-devel/2005-06/msg00024.html


I have to use multiple states (created with tcc_new) in my program, and it works fine.

Only when the states are freed (with tcc_delete), I get a segmentation fault, with a glibc error messages, either "munmap_chunk(): invalid pointer: ..." or " double free or corruption".

As an example I included a second state in the libtcc_test.c program, see attachment.

I am using the tinycc-rl-1.0.0 branch in Fedora.

Are there any patches for this, yet? Should I use another version of TCC?


Thanks,
Rainer Machne


/*
 * Simple Test program for libtcc
 *
 * libtcc can be useful to use tcc as a "backend" for a code generator.
 */
#include <stdlib.h>
#include <stdio.h>

#include "../libtcc.h"

/* this function is called by the generated code */
int add(int a, int b)
{
    return a + b;
}

char my_program2[] = "void simple(int n){printf(\"HELLO WORLD AGAIN %d\\n\", 
n);}";

char my_program[] = 
"int fib(int n)\n"
"{\n"
"    if (n <= 2)\n"
"        return 1;\n"
"    else\n"
"        return fib(n-1) + fib(n-2);\n"
"}\n"
"\n"
"int foo(int n)\n"
"{\n"
"    printf(\"Hello World!\\n\");\n"
"    printf(\"fib(%d) = %d\\n\", n, fib(n));\n"
"    printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));\n"
"    return 0;\n"
"}\n";

int main(int argc, char **argv)
{
  TCCState *s, *s2;
    int (*func)(int);
    int (*func2)(int);

    unsigned long val, val2;
    
    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_enable_debug(s);*/
    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);

    /* add a second state */
    s2 = tcc_new();
    tcc_set_output_type(s2, TCC_OUTPUT_MEMORY);
    tcc_compile_string(s2, my_program2);
    tcc_relocate(s2);
    
    tcc_get_symbol(s, &val2, "simple");
    func2 = (void *)val2;
    func2(30);
    
    tcc_delete(s);
    tcc_delete(s2);
    return 0;
}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]