Tcc: Fixed bug in Linux. Now no standard libraries are added

git-svn-id: svn://ultimatepp.org/upp/trunk@2350 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
koldo 2010-05-02 17:57:09 +00:00
parent b561b79ff3
commit 078883f4f4
6 changed files with 253 additions and 253 deletions

View file

@ -1,222 +1,222 @@
#include <Core/Core.h>
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)
#else
Tcc::Tcc()
#endif
{
#if defined(PLATFORM_WIN32)
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");
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()
{
T_tcc_delete(stateTcc);
#if defined(PLATFORM_WIN32)
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;
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 <Core/Core.h>
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)
#else
Tcc::Tcc()
#endif
{
#if defined(PLATFORM_WIN32)
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");
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()
{
T_tcc_delete(stateTcc);
#if defined(PLATFORM_WIN32)
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;
}

View file

@ -7,7 +7,7 @@ class Tcc
{
public:
#if defined(PLATFORM_WIN32)
Tcc(const char *dllFile = "libs.dll");
Tcc(const char *dllFile = "libtcc.dll");
#else
Tcc();
#endif

View file

@ -1,22 +1,21 @@
description "Tiny C Compiler wrapper\377B255,170,0";
uses
Core;
options(POSIX) -DLIBTCC;
options(WIN32) "-DTCC_TARGET_PE -DLIBTCC";
file
Tcc.cpp,
Tcc.h,
libtcc.c,
srcdoc.tpp,
srcimp.tpp,
src.tpp,
src.tpp\Tcc.t,
lib/libtcc.h,
lib/README,
License.txt;
description "Tiny C Compiler wrapper\377B255,170,0";
uses
Core;
options(POSIX) -DLIBTCC;
options(WIN32) "-DTCC_TARGET_PE -DLIBTCC";
file
Tcc.cpp,
Tcc.h,
libtcc.c,
srcdoc.tpp,
srcimp.tpp,
src.tpp,
src.tpp\Tcc.t,
lib/libtcc.h,
lib/README,
License.txt;

View file

@ -10166,6 +10166,7 @@ TCCState *tcc_new(void)
/* XXX: currently the PE linker is not ready to support that */
s->leading_underscore = 1;
#endif
s->nostdlib = 1;
return s;
}

View file

@ -1,3 +1,3 @@
#ifdef __unix__
#include "lib/Tcc.c"
#endif
#ifdef __unix__
#include "lib/tcc.c"
#endif

View file

@ -4,16 +4,16 @@
// Tcc.cpp
T_("Tcc library not found")
esES("Librería Tcc no encontrada")
esES("Librer\303\255a Tcc no encontrada")
T_("Tcc function %s not found")
esES("Función Tcc %s no encontrada")
esES("Funci\303\263n Tcc %s no encontrada")
T_("Line %d")
esES("Línea %d")
esES("L\303\255nea %d")
T_("Not possible to add symbols if output to file is defined")
esES("No es posible añadir símbolos se se ha definido salida a fichero")
esES("No es posible a\303\261adir s\303\255mbolos si se ha definido salida a fichero")
T_("Not possible to get file if output to memory is defined")
esES("No es posible obtener fichero si se ha definido salida en memoria")
@ -22,4 +22,4 @@ T_("File name is necessary if output to file is defined")
esES("Se necesita nombre de fichero si se ha definido salida a fichero")
T_("Not possible to get symbols if output to file is defined")
esES("No es posible obtener símbolos si se ha definido salida a fichero")
esES("No es posible obtener s\303\255mbolos si se ha definido salida a fichero")