Advertisement
Guest User

libtcc_bind.cpp

a guest
Jul 17th, 2013
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.13 KB | None | 0 0
  1. #include "libtcc_bind.h"
  2. #include <sdl/SDL.h>
  3.  
  4. //
  5. #define mapfunc( X, Y, Z ) { X = (Y) SDL_LoadFunction( libtcc, Z ); }
  6.  
  7. //
  8. static void *libtcc  = NULL;
  9. static void *context = NULL;
  10.  
  11. // helper defines
  12. #define cstr const char *
  13. #define vptr void *
  14. #define fptr void (*)(vptr, cstr)
  15.  
  16. // tcc state
  17. vptr  (*tcc_new         )( void             ) = NULL;
  18. void  (*tcc_delete      )( vptr             ) = NULL;
  19. void  (*tcc_set_lib_path    )( vptr, cstr           ) = NULL;
  20. void  (*tcc_set_error_func  )( vptr, vptr, fptr     ) = NULL;
  21. int   (*tcc_set_options     )( vptr, cstr           ) = NULL;
  22.  
  23. // pre processor
  24. int   (*tcc_add_include_path    )( vptr, cstr           ) = NULL;
  25. int   (*tcc_add_sysinclude_path )( vptr, cstr           ) = NULL;
  26. void  (*tcc_define_symbol   )( vptr, cstr, cstr     ) = NULL;
  27. void  (*tcc_undefine_symbol )( vptr, cstr           ) = NULL;
  28.  
  29. // compiling
  30. int   (*tcc_add_file        )( vptr, cstr           ) = NULL;
  31. int   (*tcc_compile_string  )( vptr, cstr           ) = NULL;
  32.  
  33. // linking commands
  34. void  (*tcc_set_output_type )( vptr, int            ) = NULL;
  35. int   (*tcc_add_library_path    )( vptr, cstr           ) = NULL;
  36. int   (*tcc_add_library     )( vptr, cstr           ) = NULL;
  37. int   (*tcc_add_symbol      )( vptr, cstr, const vptr   ) = NULL;
  38. int   (*tcc_output_file     )( vptr, cstr           ) = NULL;
  39. int   (*tcc_run         )( vptr, int, char **       ) = NULL;
  40. int   (*tcc_relocate        )( vptr, vptr           ) = NULL;
  41. vptr  (*tcc_get_symbol      )( vptr, cstr           ) = NULL;
  42.  
  43. //
  44. static bool dynlink_tcc( void )
  45. {
  46.     //
  47.     libtcc = SDL_LoadObject( "libtcc.dll" );
  48.     if ( libtcc == NULL )
  49.         return false;
  50.     //
  51.     mapfunc(
  52.         tcc_add_symbol,
  53.         int (*)( void*, const char *, const void * ),
  54.         "tcc_add_symbol"
  55.     );
  56.     mapfunc(
  57.         tcc_new,
  58.         void* (*)( void ),
  59.         "tcc_new"
  60.     );
  61.     mapfunc(
  62.         tcc_delete,
  63.         void (*)( void* ),
  64.         "tcc_delete"
  65.     );
  66.     mapfunc(
  67.         tcc_set_error_func,
  68.         void (*)( void*, void*, void (*)(void*, const char*) ),
  69.         "tcc_set_error_func"
  70.     );
  71.     mapfunc(
  72.         tcc_add_file,
  73.         int (*)( void*, const char* ),
  74.         "tcc_add_file"
  75.     );
  76.     mapfunc(
  77.         tcc_compile_string,
  78.         int (*)( void*, const char* ),
  79.         "tcc_compile_string"
  80.     );
  81.     mapfunc(
  82.         tcc_relocate,
  83.         int (*)( void*, void* ),
  84.         "tcc_relocate"
  85.     );
  86.     mapfunc(
  87.         tcc_get_symbol,
  88.         void* (*)( void*, const char* ),
  89.         "tcc_get_symbol"
  90.     );
  91.     mapfunc(
  92.         tcc_set_lib_path,
  93.         void (*)( void*, const char * ),
  94.         "tcc_set_lib_path"
  95.     );
  96.     mapfunc(
  97.         tcc_set_output_type,
  98.         void (*)( void*, int ),
  99.         "tcc_set_output_type"
  100.     );
  101.     mapfunc(
  102.         tcc_set_options,
  103.         int (*)(void *s, const char *str ),
  104.         "tcc_set_options"
  105.     );
  106.     mapfunc(
  107.         tcc_add_include_path,
  108.         int (*)(void *s, const char *pathname ),
  109.         "tcc_add_include_path"
  110.     );
  111.     mapfunc(
  112.         tcc_add_sysinclude_path,
  113.         int (*)(void *s, const char *pathname ),
  114.         "tcc_add_sysinclude_path"
  115.     );
  116.     mapfunc(
  117.         tcc_define_symbol,
  118.         void (*)(void *s, const char *sym, const char *value ),
  119.         "tcc_define_symbol"
  120.     );
  121.     mapfunc(
  122.         tcc_undefine_symbol,
  123.         void (*)(void *s, const char *sym ),
  124.         "tcc_undefine_symbol"
  125.     );
  126.     mapfunc(
  127.         tcc_add_library_path,
  128.         int  (*)(void *s, const char *pathname ),
  129.         "tcc_add_library_path"
  130.     );
  131.     mapfunc(
  132.         tcc_add_library,
  133.         int  (*)(void *s, const char *libraryname ),
  134.         "tcc_add_library"
  135.     );
  136.     mapfunc(
  137.         tcc_output_file,
  138.         int  (*)(void *s, const char *filename ),
  139.         "tcc_output_file"
  140.     );
  141.     mapfunc(
  142.         tcc_run,
  143.         int  (*)(void *s, int argc, char **argv ),
  144.         "tcc_run"
  145.     );
  146.     //
  147.     return true;
  148. }
  149.  
  150. //
  151. static void error_tcc( void *opaque, const char *msg )
  152. {
  153.     /* logWrite( "TCC Error:\n%s\n", msg ); */
  154. }
  155.  
  156. //
  157. static bool create_tcc_context( void )
  158. {
  159.     //
  160.     context = tcc_new( );
  161.     if ( context == NULL )
  162.         return false;
  163.     //
  164.     tcc_set_error_func ( context, 0, error_tcc );
  165.     if ( tcc_add_file( context, "./lib/libtcc1.a" ) == -1 ) return false;
  166.     tcc_set_output_type( context, TCC_OUTPUT_MEMORY );
  167.     //
  168.     return true;
  169. }
  170.  
  171. //
  172. bool libtcc_start( void )
  173. {
  174.     // dynamicaly load tcc
  175.     if (! dynlink_tcc       ( ) ) return false;
  176.     if (! create_tcc_context( ) ) return false;
  177.     //
  178.     return true;
  179. }
  180.  
  181. //
  182. void libtcc_stop( void )
  183. {
  184.     //
  185.     if ( context != NULL )
  186.         tcc_delete( context );
  187.     //
  188.     if ( libtcc != NULL )
  189.         SDL_UnloadObject( libtcc );
  190.     libtcc = NULL;
  191. }
  192.  
  193. //
  194. void *libtcc_getContext( void )
  195. {
  196.     return context;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement