Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "libtcc_bind.h"
- #include <sdl/SDL.h>
- //
- #define mapfunc( X, Y, Z ) { X = (Y) SDL_LoadFunction( libtcc, Z ); }
- //
- static void *libtcc = NULL;
- static void *context = NULL;
- // helper defines
- #define cstr const char *
- #define vptr void *
- #define fptr void (*)(vptr, cstr)
- // tcc state
- vptr (*tcc_new )( void ) = NULL;
- void (*tcc_delete )( vptr ) = NULL;
- void (*tcc_set_lib_path )( vptr, cstr ) = NULL;
- void (*tcc_set_error_func )( vptr, vptr, fptr ) = NULL;
- int (*tcc_set_options )( vptr, cstr ) = NULL;
- // pre processor
- int (*tcc_add_include_path )( vptr, cstr ) = NULL;
- int (*tcc_add_sysinclude_path )( vptr, cstr ) = NULL;
- void (*tcc_define_symbol )( vptr, cstr, cstr ) = NULL;
- void (*tcc_undefine_symbol )( vptr, cstr ) = NULL;
- // compiling
- int (*tcc_add_file )( vptr, cstr ) = NULL;
- int (*tcc_compile_string )( vptr, cstr ) = NULL;
- // linking commands
- void (*tcc_set_output_type )( vptr, int ) = NULL;
- int (*tcc_add_library_path )( vptr, cstr ) = NULL;
- int (*tcc_add_library )( vptr, cstr ) = NULL;
- int (*tcc_add_symbol )( vptr, cstr, const vptr ) = NULL;
- int (*tcc_output_file )( vptr, cstr ) = NULL;
- int (*tcc_run )( vptr, int, char ** ) = NULL;
- int (*tcc_relocate )( vptr, vptr ) = NULL;
- vptr (*tcc_get_symbol )( vptr, cstr ) = NULL;
- //
- static bool dynlink_tcc( void )
- {
- //
- libtcc = SDL_LoadObject( "libtcc.dll" );
- if ( libtcc == NULL )
- return false;
- //
- mapfunc(
- tcc_add_symbol,
- int (*)( void*, const char *, const void * ),
- "tcc_add_symbol"
- );
- mapfunc(
- tcc_new,
- void* (*)( void ),
- "tcc_new"
- );
- mapfunc(
- tcc_delete,
- void (*)( void* ),
- "tcc_delete"
- );
- mapfunc(
- tcc_set_error_func,
- void (*)( void*, void*, void (*)(void*, const char*) ),
- "tcc_set_error_func"
- );
- mapfunc(
- tcc_add_file,
- int (*)( void*, const char* ),
- "tcc_add_file"
- );
- mapfunc(
- tcc_compile_string,
- int (*)( void*, const char* ),
- "tcc_compile_string"
- );
- mapfunc(
- tcc_relocate,
- int (*)( void*, void* ),
- "tcc_relocate"
- );
- mapfunc(
- tcc_get_symbol,
- void* (*)( void*, const char* ),
- "tcc_get_symbol"
- );
- mapfunc(
- tcc_set_lib_path,
- void (*)( void*, const char * ),
- "tcc_set_lib_path"
- );
- mapfunc(
- tcc_set_output_type,
- void (*)( void*, int ),
- "tcc_set_output_type"
- );
- mapfunc(
- tcc_set_options,
- int (*)(void *s, const char *str ),
- "tcc_set_options"
- );
- mapfunc(
- tcc_add_include_path,
- int (*)(void *s, const char *pathname ),
- "tcc_add_include_path"
- );
- mapfunc(
- tcc_add_sysinclude_path,
- int (*)(void *s, const char *pathname ),
- "tcc_add_sysinclude_path"
- );
- mapfunc(
- tcc_define_symbol,
- void (*)(void *s, const char *sym, const char *value ),
- "tcc_define_symbol"
- );
- mapfunc(
- tcc_undefine_symbol,
- void (*)(void *s, const char *sym ),
- "tcc_undefine_symbol"
- );
- mapfunc(
- tcc_add_library_path,
- int (*)(void *s, const char *pathname ),
- "tcc_add_library_path"
- );
- mapfunc(
- tcc_add_library,
- int (*)(void *s, const char *libraryname ),
- "tcc_add_library"
- );
- mapfunc(
- tcc_output_file,
- int (*)(void *s, const char *filename ),
- "tcc_output_file"
- );
- mapfunc(
- tcc_run,
- int (*)(void *s, int argc, char **argv ),
- "tcc_run"
- );
- //
- return true;
- }
- //
- static void error_tcc( void *opaque, const char *msg )
- {
- /* logWrite( "TCC Error:\n%s\n", msg ); */
- }
- //
- static bool create_tcc_context( void )
- {
- //
- context = tcc_new( );
- if ( context == NULL )
- return false;
- //
- tcc_set_error_func ( context, 0, error_tcc );
- if ( tcc_add_file( context, "./lib/libtcc1.a" ) == -1 ) return false;
- tcc_set_output_type( context, TCC_OUTPUT_MEMORY );
- //
- return true;
- }
- //
- bool libtcc_start( void )
- {
- // dynamicaly load tcc
- if (! dynlink_tcc ( ) ) return false;
- if (! create_tcc_context( ) ) return false;
- //
- return true;
- }
- //
- void libtcc_stop( void )
- {
- //
- if ( context != NULL )
- tcc_delete( context );
- //
- if ( libtcc != NULL )
- SDL_UnloadObject( libtcc );
- libtcc = NULL;
- }
- //
- void *libtcc_getContext( void )
- {
- return context;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement