diff --git a/bazaar/Tcc/Tcc.cpp b/bazaar/Tcc/Tcc.cpp index 7c2f4525d..07968cbcd 100644 --- a/bazaar/Tcc/Tcc.cpp +++ b/bazaar/Tcc/Tcc.cpp @@ -1,242 +1,245 @@ -#include - -using namespace Upp; - -#include "Tcc.h" - -#if __unix__ -#define T_tcc_new tcc_new -#define T_tcc_delete tcc_delete -#define T_tcc_set_output_type tcc_set_output_type -#define T_tcc_set_error_func tcc_set_error_func -#define T_tcc_get_symbol tcc_get_symbol -#define T_tcc_add_symbol tcc_add_symbol -#define T_tcc_relocate tcc_relocate -#define T_tcc_compile_string tcc_compile_string -#define T_tcc_add_include_path tcc_add_include_path -#define T_tcc_add_library_path tcc_add_library_path -#define T_tcc_output_file tcc_output_file -#endif - - -#if defined(PLATFORM_WIN32) -Tcc::Tcc(const char *dllName) { - Init(dllName); -} -#else -Tcc::Tcc(const char *libPath) { - Init(libPath); -} -#endif - -#if defined(PLATFORM_WIN32) -void Tcc::Init(const char *dllName) -#else -void Tcc::Init(const char *libPath) -#endif -{ -#if defined(PLATFORM_WIN32) - if (dllName == NULL || *dllName == '\0') - dllName = "libtcc.dll"; - hinstLib = LoadLibrary(TEXT(dllName)); - if (hinstLib == NULL) - throw Exc(t_("Tcc library not found")); - - T_tcc_new = (TCCState *(*)(void))GetProcAddress(hinstLib, "tcc_new"); - if (T_tcc_new == NULL) { - FreeLibrary(hinstLib); - throw Exc(Format(t_("Tcc function %s not found"), "tcc_new")); - } - T_tcc_delete = (void (*)(TCCState *))GetProcAddress(hinstLib, "tcc_delete"); - if (T_tcc_delete == NULL) { - FreeLibrary(hinstLib); - throw Exc(Format(t_("Tcc function %s not found"), "tcc_delete")); - } - T_tcc_set_output_type = (int (*)(TCCState *s, int output_type))GetProcAddress(hinstLib, - "tcc_set_output_type"); - if (T_tcc_set_output_type == NULL) { - FreeLibrary(hinstLib); - throw Exc(Format(t_("Tcc function %s not found"), "tcc_set_output_type")); - } - T_tcc_set_error_func = (void (*)(TCCState *s, void *error_opaque, void (*error_func) - (void *opaque, const char *msg)))GetProcAddress(hinstLib, "tcc_set_error_func"); - if (T_tcc_set_error_func == NULL) { - FreeLibrary(hinstLib); - throw Exc(Format(t_("Tcc function %s not found"), "tcc_set_error_func")); - } - T_tcc_get_symbol = (int (*)(TCCState *s, unsigned long *pval, const char *name)) - GetProcAddress(hinstLib, "tcc_get_symbol"); - if (T_tcc_get_symbol == NULL) { - FreeLibrary(hinstLib); - throw Exc(Format(t_("Tcc function %s not found"), "tcc_get_symbol")); - } - T_tcc_add_symbol = (int (*)(TCCState *s, const char *name, unsigned long val)) - GetProcAddress(hinstLib, "tcc_add_symbol"); - if (T_tcc_add_symbol == NULL) { - FreeLibrary(hinstLib); - throw Exc(Format(t_("Tcc function %s not found"), "tcc_add_symbol")); - } - T_tcc_relocate = (int (*)(TCCState *s))GetProcAddress(hinstLib, "tcc_relocate"); - if (T_tcc_relocate == NULL) { - FreeLibrary(hinstLib); - throw Exc(Format(t_("Tcc function %s not found"), "tcc_relocate")); - } - T_tcc_compile_string = (int (*)(TCCState *s, const char *buf))GetProcAddress(hinstLib, - "tcc_compile_string"); - if (T_tcc_compile_string == NULL) { - FreeLibrary(hinstLib); - throw Exc(Format(t_("Tcc function %s not found"), "tcc_compile_string")); - } - T_tcc_add_include_path = (int (*)(TCCState *s, const char *buf))GetProcAddress(hinstLib, - "tcc_add_include_path"); - if (T_tcc_add_include_path == NULL) { - FreeLibrary(hinstLib); - throw Exc(Format(t_("Tcc function %s not found"), "tcc_add_include_path")); - } - T_tcc_add_library_path = (int (*)(TCCState *s, const char *buf))GetProcAddress(hinstLib, - "tcc_add_library_path"); - if (T_tcc_add_library_path == NULL) { - FreeLibrary(hinstLib); - throw Exc(Format(t_("Tcc function %s not found"), "tcc_add_library_path")); - } - T_tcc_output_file = (int (*)(TCCState *s, const char *buf))GetProcAddress(hinstLib, - "tcc_output_file"); - if (T_tcc_output_file == NULL) { - FreeLibrary(hinstLib); - throw Exc(Format(t_("Tcc function %s not found"), "tcc_output_file")); - } -#endif - stateTcc = T_tcc_new(); - if (!stateTcc) - throw Exc("Tcc can not initialize"); -#if !defined(PLATFORM_WIN32) - if (libPath != NULL) - if (*libPath != '\0') - SetLibPath(libPath); -#endif - - T_tcc_set_error_func(stateTcc, NULL, Tcc::DefaultErrorHandler); - errorMsg = ""; - - SetOutputMemory(); -} - -void Tcc::SetOutputExe() -{ - T_tcc_set_output_type(stateTcc, TCC_OUTPUT_EXE); - outputMemory = false; -}; - -void Tcc::SetOutputMemory() -{ - T_tcc_set_output_type(stateTcc, TCC_OUTPUT_MEMORY); - outputMemory = true; -}; - - -String Tcc::errorMsg = ""; -int Tcc::initialProgramLines; - -void Tcc::DefaultErrorHandler(void* opaque, const char* msg) -{ - if (!errorMsg.IsEmpty()) - errorMsg.Cat('\n'); // When calling Relocate this handle can get more than one error - String message = msg; - int linePos = sizeof("Line ")-1; - if (message.Left(linePos) == "Line ") { // Fix the line number in the error message - int endLinePos = message.Find(':', linePos+1); - int line = atoi(message.Mid(linePos, endLinePos-linePos)); - message = Format(t_("Line %d"), line-initialProgramLines) + ":" + message.Mid(endLinePos+1); - } - errorMsg.Cat(message); -} - -Tcc::~Tcc() -{ - if (stateTcc) - T_tcc_delete(stateTcc); -#if defined(PLATFORM_WIN32) - if (hinstLib) - FreeLibrary(hinstLib); -#endif -} - -void tcc_throw(char *str) -{ - throw Exc(str); -} - -void Tcc::Compile(const char *my_program) -{ - program << "// Basic declarations\n" - "typedef int bool;\n" - "#define true 1\n" - "#define false 0\n" - "#define M_PI 3.1415926535897932\n" // It does not seem to be in math.h - "\n" - "void throw(char *str);\n" - "\n"; - initialProgramLines = 0; - int pos = 0; - while((pos = program.Find('\n', pos)) >= 0) { - initialProgramLines++; - pos++; - } - program << my_program; - - if (T_tcc_compile_string(stateTcc, program) != 0) - throw Exc(errorMsg); - if (outputMemory) - AddSymbol("throw", (void *)&tcc_throw); -} - -void Tcc::AddSymbol(const char *funName, void *fun) -{ - if (!outputMemory) - throw Exc(t_("Not possible to add symbols if output to file is defined")); - T_tcc_add_symbol(stateTcc, funName, (unsigned long)fun); - if (!errorMsg.IsEmpty()) - throw Exc(errorMsg); -} - -void Tcc::Link(const char *fileName) -{ - if (outputMemory) { - if (fileName) - throw Exc(t_("Not possible to get file if output to memory is defined")); - if(T_tcc_relocate(stateTcc) != 0) - throw Exc(errorMsg); - } else if (!outputMemory) { - if (!fileName) - throw Exc(t_("File name is necessary if output to file is defined")); - if (T_tcc_output_file(stateTcc, fileName) != 0) - throw Exc(errorMsg); - } -} - -void *Tcc::GetSymbol(const char *funName) -{ - if (!outputMemory) - throw Exc(t_("Not possible to get symbols if output to file is defined")); - unsigned long val = 0; - T_tcc_get_symbol(stateTcc, (unsigned long *)&val, funName); - if (!errorMsg.IsEmpty()) - throw Exc(errorMsg); - - return (void *)val; -} - -bool Tcc::AddIncludePath(const char *path) -{ - bool ret = T_tcc_add_include_path(stateTcc, path) == 0? true: false; - if (!errorMsg.IsEmpty()) - throw Exc(errorMsg); - return ret; -} -bool Tcc::AddLibraryPath(const char *path) -{ - bool ret = T_tcc_add_library_path(stateTcc, path) == 0? true: false; - if (!errorMsg.IsEmpty()) - throw Exc(errorMsg); - return ret; -} +#include + +using namespace Upp; + +#include "Tcc.h" + +#if __unix__ +#define T_tcc_new tcc_new +#define T_tcc_delete tcc_delete +#define T_tcc_set_output_type tcc_set_output_type +#define T_tcc_set_error_func tcc_set_error_func +#define T_tcc_get_symbol tcc_get_symbol +#define T_tcc_add_symbol tcc_add_symbol +#define T_tcc_relocate tcc_relocate +#define T_tcc_compile_string tcc_compile_string +#define T_tcc_add_include_path tcc_add_include_path +#define T_tcc_add_library_path tcc_add_library_path +#define T_tcc_output_file tcc_output_file +#endif + + +#if defined(PLATFORM_WIN32) +Tcc::Tcc(const char *dllName) { + Init(dllName); +} +#else +Tcc::Tcc(const char *libPath) { + Init(libPath); +} +#endif + +#if defined(PLATFORM_WIN32) +void Tcc::Init(const char *dllName) +#else +void Tcc::Init(const char *libPath) +#endif +{ +#if defined(PLATFORM_WIN32) + if (dllName == NULL || *dllName == '\0') + dllName = "libtcc.dll"; + hinstLib = LoadLibrary(TEXT(dllName)); + if (hinstLib == NULL) + throw Exc(t_("Tcc library not found")); + + T_tcc_new = (TCCState *(*)(void))GetProcAddress(hinstLib, "tcc_new"); + if (T_tcc_new == NULL) { + FreeLibrary(hinstLib); + throw Exc(Format(t_("Tcc function %s not found"), "tcc_new")); + } + T_tcc_delete = (void (*)(TCCState *))GetProcAddress(hinstLib, "tcc_delete"); + if (T_tcc_delete == NULL) { + FreeLibrary(hinstLib); + throw Exc(Format(t_("Tcc function %s not found"), "tcc_delete")); + } + T_tcc_set_output_type = (int (*)(TCCState *s, int output_type))GetProcAddress(hinstLib, + "tcc_set_output_type"); + if (T_tcc_set_output_type == NULL) { + FreeLibrary(hinstLib); + throw Exc(Format(t_("Tcc function %s not found"), "tcc_set_output_type")); + } + T_tcc_set_error_func = (void (*)(TCCState *s, void *error_opaque, void (*error_func) + (void *opaque, const char *msg)))GetProcAddress(hinstLib, "tcc_set_error_func"); + if (T_tcc_set_error_func == NULL) { + FreeLibrary(hinstLib); + throw Exc(Format(t_("Tcc function %s not found"), "tcc_set_error_func")); + } + T_tcc_get_symbol = (int (*)(TCCState *s, unsigned long *pval, const char *name)) + GetProcAddress(hinstLib, "tcc_get_symbol"); + if (T_tcc_get_symbol == NULL) { + FreeLibrary(hinstLib); + throw Exc(Format(t_("Tcc function %s not found"), "tcc_get_symbol")); + } + T_tcc_add_symbol = (int (*)(TCCState *s, const char *name, unsigned long val)) + GetProcAddress(hinstLib, "tcc_add_symbol"); + if (T_tcc_add_symbol == NULL) { + FreeLibrary(hinstLib); + throw Exc(Format(t_("Tcc function %s not found"), "tcc_add_symbol")); + } + T_tcc_relocate = (int (*)(TCCState *s))GetProcAddress(hinstLib, "tcc_relocate"); + if (T_tcc_relocate == NULL) { + FreeLibrary(hinstLib); + throw Exc(Format(t_("Tcc function %s not found"), "tcc_relocate")); + } + T_tcc_compile_string = (int (*)(TCCState *s, const char *buf))GetProcAddress(hinstLib, + "tcc_compile_string"); + if (T_tcc_compile_string == NULL) { + FreeLibrary(hinstLib); + throw Exc(Format(t_("Tcc function %s not found"), "tcc_compile_string")); + } + T_tcc_add_include_path = (int (*)(TCCState *s, const char *buf))GetProcAddress(hinstLib, + "tcc_add_include_path"); + if (T_tcc_add_include_path == NULL) { + FreeLibrary(hinstLib); + throw Exc(Format(t_("Tcc function %s not found"), "tcc_add_include_path")); + } + T_tcc_add_library_path = (int (*)(TCCState *s, const char *buf))GetProcAddress(hinstLib, + "tcc_add_library_path"); + if (T_tcc_add_library_path == NULL) { + FreeLibrary(hinstLib); + throw Exc(Format(t_("Tcc function %s not found"), "tcc_add_library_path")); + } + T_tcc_output_file = (int (*)(TCCState *s, const char *buf))GetProcAddress(hinstLib, + "tcc_output_file"); + if (T_tcc_output_file == NULL) { + FreeLibrary(hinstLib); + throw Exc(Format(t_("Tcc function %s not found"), "tcc_output_file")); + } +#endif + stateTcc = T_tcc_new(); + if (!stateTcc) + throw Exc("Tcc can not initialize"); +#if !defined(PLATFORM_WIN32) + if (libPath != NULL) + if (*libPath != '\0') + SetLibPath(libPath); +#endif + + T_tcc_set_error_func(stateTcc, this, Tcc::DefaultErrorHandler); + errorMsg = ""; + + SetOutputMemory(); +} + +void Tcc::SetOutputExe() +{ + T_tcc_set_output_type(stateTcc, TCC_OUTPUT_EXE); + outputMemory = false; +}; + +void Tcc::SetOutputMemory() +{ + T_tcc_set_output_type(stateTcc, TCC_OUTPUT_MEMORY); + outputMemory = true; +}; + +void Tcc::DefaultErrorHandler(void* opaque, const char* msg) +{ + Tcc &tcc = *(Tcc *)opaque; + if (!tcc.errorMsg.IsEmpty()) + tcc.errorMsg.Cat('\n'); // When calling Relocate this handle can get more than one error + String message = msg; + int linePos = sizeof("Line ")-1; + if (message.Left(linePos) == "Line ") { // Fix the line number in the error message + int endLinePos = message.Find(':', linePos+1); + int line = atoi(message.Mid(linePos, endLinePos-linePos)); + message = Format(t_("Line %d"), line - tcc.initProgramLines) + ":" + message.Mid(endLinePos+1); + } + tcc.errorMsg.Cat(message); +} + +Tcc::~Tcc() +{ + if (stateTcc) + T_tcc_delete(stateTcc); +#if defined(PLATFORM_WIN32) + if (hinstLib) + FreeLibrary(hinstLib); +#endif +} + +void tcc_throw(const char *str) +{ + throw Exc(str); +} + +void Tcc::Compile(const char *my_program) +{ + String initString = "// Basic declarations\n" + "typedef int bool;\n" + "#define true 1\n" + "#define false 0\n" + "#define M_PI 3.1415926535897932\n"// It does not seem to be in math.h + "\n" + "void throw(char *str);\n" + "\n"; + initLen = initString.GetCount(); + initProgramLines = 0; + int pos = 0; + while((pos = initString.Find('\n', pos)) >= 0) { + initProgramLines++; + pos++; + } + program = initString + my_program; + + if (T_tcc_compile_string(stateTcc, program) != 0) + throw Exc(errorMsg); + if (outputMemory) + AddSymbol("throw", (void *)&tcc_throw); +} + +void Tcc::AddSymbol(const char *funName, void *fun) +{ + if (!outputMemory) + throw Exc(t_("Not possible to add symbols if output to file is defined")); + T_tcc_add_symbol(stateTcc, funName, (unsigned long)fun); + if (!errorMsg.IsEmpty()) + throw Exc(errorMsg); +} + +void Tcc::Link(const char *fileName) +{ + if (outputMemory) { + if (fileName) + throw Exc(t_("Not possible to get file if output to memory is defined")); + if(T_tcc_relocate(stateTcc) != 0) + throw Exc(errorMsg); + } else if (!outputMemory) { + if (!fileName) + throw Exc(t_("File name is necessary if output to file is defined")); + if (T_tcc_output_file(stateTcc, fileName) != 0) + throw Exc(errorMsg); + } +} + +void *Tcc::GetSymbol(const char *funName) +{ + if (!outputMemory) + throw Exc(t_("Not possible to get symbols if output to file is defined")); + unsigned long val = 0; + T_tcc_get_symbol(stateTcc, (unsigned long *)&val, funName); + if (!errorMsg.IsEmpty()) + throw Exc(errorMsg); + + return (void *)val; +} + +bool Tcc::AddIncludePath(const char *path) +{ + bool ret = T_tcc_add_include_path(stateTcc, path) == 0? true: false; + if (!errorMsg.IsEmpty()) + throw Exc(errorMsg); + return ret; +} + +bool Tcc::AddLibraryPath(const char *path) +{ + bool ret = T_tcc_add_library_path(stateTcc, path) == 0? true: false; + if (!errorMsg.IsEmpty()) + throw Exc(errorMsg); + return ret; +} + +String Tcc::GetProgram() { + return program.Mid(initLen); +} diff --git a/bazaar/Tcc/Tcc.h b/bazaar/Tcc/Tcc.h index 4a80128ef..7d29b7ea7 100644 --- a/bazaar/Tcc/Tcc.h +++ b/bazaar/Tcc/Tcc.h @@ -30,11 +30,7 @@ public: void *GetSymbol(const char *funName); void Link(const char *fileName = 0); - String GetProgram() { - String ret; - ret <<= program; - return ret; - } + String GetProgram(); private: TCCState *stateTcc; @@ -43,8 +39,9 @@ private: #endif static void DefaultErrorHandler(void *opaque, const char *msg); - static String errorMsg; - static int initialProgramLines; + String errorMsg; + int initProgramLines; + int initLen; String program; bool outputMemory; diff --git a/bazaar/Tcc/src.tpp/Tcc$en-us.tpp b/bazaar/Tcc/src.tpp/Tcc$en-us.tpp index 535a07cc0..6d2c70deb 100644 --- a/bazaar/Tcc/src.tpp/Tcc$en-us.tpp +++ b/bazaar/Tcc/src.tpp/Tcc$en-us.tpp @@ -58,7 +58,7 @@ the main program.&] [s3;l480; [%-*@3 funName].contains the name of the function in the C script.&] [s4; &] -[s4; &] +[s1; &] [s2;:Tcc`:`:AddIncludePath`(const char`*`):%- [@(0.0.255) bool]_[* AddIncludePath]([@(0.0.255) c onst]_[@(0.0.255) char]_`*[*@3 path])&] [s3; Sets a directory [%-*@3 path ]where include files referenced in @@ -86,4 +86,11 @@ indicated in Link()&] [s2;:Tcc`:`:SetOutputMemory`(`):%- [@(0.0.255) void]_[* SetOutputMemory]()&] [s3; Defines that the program will be run in memory (option by default)&] [s4;%- &] -[s0; ] \ No newline at end of file +[s0; &] +[ {{10000@1 [s0; [* C script function list. This functions can be called from C +script]]}}&] +[s1;%- &] +[s0;%- [@(0.0.255)2 void][2 _][*2 throw][2 (][@(0.0.255)2 const char][2 _`*][*@3;2 path][2 )]&] +[s3; Throws an Exc exception with message [%-*@3 path].&] +[s4;%- &] +[s0;%- ] \ No newline at end of file