tinycc-devel
[Top][All Lists]
Advanced

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

Re: [Tinycc-devel] accessing tinycc from C program


From: Rüdiger Plantiko
Subject: Re: [Tinycc-devel] accessing tinycc from C program
Date: Wed, 19 Aug 2009 23:42:23 +0200
User-agent: Thunderbird 2.0.0.22 (Windows/20090605)

Thank you, Felix, for the example code! I could not directly use it on my Win 32 laptop, but I combined it with the recipe for delayed loading of TCC as described by Benjamin Maggi in http://maggidev.com - and now I have my first runnable example of TCC used as interpreter. At runtime it needs to access libtcc1.a. Here is the combination of Benjamin's
with your example.

Regards,
Rüdiger

* ------------------------------------------------------------------------------------
#include <windows.h>

struct TCCState;
typedef struct TCCState TCCState;

// Windows Handle for TCC library
HINSTANCE _tcc_lib = NULL;

// Macro for loading a pointer to a libtcc function
#define load_TCC_function(name)                 \
name = GetProcAddress(_tcc_lib, #name );        \
if ( NULL == name ) {                           \
 printf("Error: could not load " #name );      \
 FreeLibrary(_tcc_lib);                        \
 return FALSE;                                 \
} /* Declarations of pointers to libtcc functions */ TCCState *(__stdcall *tcc_new)(void);
int       (__stdcall *tcc_compile_string)(TCCState *s, const char *buf);
int       (__stdcall *tcc_relocate)(TCCState *s1, void *ptr);
void *    (__stdcall *tcc_get_symbol)(TCCState *s, const char *name);


/* * Call this function at the start of your program to load the interpreter * It will make it's functions available
*/
BOOL loadTcc(void)
{
_tcc_lib = LoadLibrary("libtcc.dll"); if(!_tcc_lib) {
 printf("Could not load library libtcc.dll");
return FALSE; } load_TCC_function( tcc_new ) load_TCC_function( tcc_compile_string )
load_TCC_function( tcc_relocate )
load_TCC_function( tcc_get_symbol )
// Add more functions if needed return TRUE;
}

// Call this function when your program exits
// or when you no longer need to use the libtcc

BOOL UnLoadTCC(void) { if(_tcc_lib) FreeLibrary(_tcc_lib);
 }

int main( char* argv[] ) {

 int testint;
 int (*testfct)(void);
 void* ptr = malloc(100000); // too much.
 TCCState* s;

 loadTcc( );
s = tcc_new( );
 tcc_compile_string(s, "int bar(){ return(7-5);}\n" );

 tcc_relocate(s, ptr);
 testfct = tcc_get_symbol( s, "bar" );
 if ( ! testfct ) {
   printf( "no symbol from string\n" );
   return (2);
   }

 testint = testfct();

 printf( "integer from string: %i\n", testint );
 UnLoadTCC();
}

Felix Salfelder schrieb:
Hi tinycc developers.

Thanks for your work!
I am using libtcc to generate object code from C at run time. An example
code for something like this is still missing.
Furthermore it would be great to be able to hand a file descriptor to
the compiler (instead of only a file name or a string).

In the attachment you find a libtcc patch and an example application
addressing this. Add it to the repo if you like.

regards
felix
------------------------------------------------------------------------

_______________________________________________
Tinycc-devel mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/tinycc-devel





reply via email to

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