From 34c6b33a2fb49b6e64f55c8ac369428ddbc74bf5 Mon Sep 17 00:00:00 2001 From: Dibyendu Majumdar Date: Thu, 27 Aug 2020 01:24:01 +0100 Subject: [PATCH] issue #198 WIP add interface to RaviCompiler project --- CMakeLists.txt | 28 ++++++++++++--- cmake/FindRaviComp.cmake | 14 ++++++++ include/lualib.h | 6 ++-- ravi-config.h.in | 1 + src/linit.c | 3 ++ src/ravi_complib.c | 75 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 121 insertions(+), 6 deletions(-) create mode 100644 cmake/FindRaviComp.cmake create mode 100644 src/ravi_complib.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 068755d..e7471f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,9 @@ option(STATIC_BUILD "Build static version of Ravi, default is OFF" OFF) option(COMPUTED_GOTO "Controls whether the interpreter switch will use computed gotos on gcc/clang, default is ON" ON) option(LTESTS "Controls whether ltests are enabled in Debug mode; note requires Debug build" ON) option(ASAN "Controls whether address sanitizer should be enabled" OFF) +option(RAVICOMP "Controls whether to link in RaviComp" ON) + +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") # By default on non-Windows platforms we enable MIR JIT if (NOT WIN32 @@ -66,6 +69,7 @@ set(C2MIR_SRCS mir/c2mir/c2mir.c) set(MIR_JIT_SRCS src/ravi_mirjit.c) set(NO_JIT_SRCS src/ravi_nojit.c) set(LUA_CMD_SRCS src/lua.c) +set(RAVICOMP_SRCS src/ravi_complib.c) file(GLOB RAVI_HEADERS "${PROJECT_SOURCE_DIR}/include/*.h") if (COMPUTED_GOTO AND NOT MSVC) @@ -89,7 +93,6 @@ if (ASAN) endif() endif() - if (LLVM_JIT) find_package(LLVM REQUIRED CONFIG) message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") @@ -133,10 +136,18 @@ else () endif () endif () +if (RAVICOMP) + find_package(RaviComp REQUIRED) + set(ADDON_SRCS ${RAVICOMP_SRCS}) + set_property(SOURCE ${RAVICOMP_SRCS} + APPEND + PROPERTY INCLUDE_DIRECTORIES ${RAVICOMP_INCLUDE_DIRS}) +endif() + # IDE stuff if (MSVC OR APPLE) source_group("Ravi Headers" FILES ${RAVI_HEADERS}) - source_group("Ravi Source Files" FILES ${LUA_CORE_SRCS} ${LUA_LIB_SRCS} ${JIT_SRCS}) + source_group("Ravi Source Files" FILES ${LUA_CORE_SRCS} ${LUA_LIB_SRCS} ${JIT_SRCS} ${ADDON_SRCS}) endif () # Misc setup @@ -208,6 +219,7 @@ if (LLVM_JIT) message(STATUS "LLVM_LIBS ${LLVM_LIBS}") endif () + set(LIBRAVI_NAME libravi) #Main library @@ -215,8 +227,9 @@ add_library(${LIBRAVI_NAME} ${LIBRAVI_BUILD_TYPE} ${RAVI_HEADERS} ${LUA_LIB_SRCS} ${LUA_CORE_SRCS} - ${JIT_SRCS}) -target_link_libraries(${LIBRAVI_NAME} ${EXTRA_LIBRARIES} ${LLVM_LIBS} ${MIRJIT_LIBRARIES}) + ${JIT_SRCS} + ${ADDON_SRCS}) +target_link_libraries(${LIBRAVI_NAME} ${EXTRA_LIBRARIES} ${LLVM_LIBS} ${MIRJIT_LIBRARIES} ${RAVICOMP_LIBRARIES}) # Main Ravi executable add_executable(ravi ${LUA_CMD_SRCS}) @@ -279,6 +292,13 @@ if (NOT STATIC_BUILD) else () set_target_properties(${LIBRAVI_NAME} PROPERTIES PREFIX "") endif () +if (RAVICOMP) + set_property( + TARGET ${LIBRAVI_NAME} + APPEND + PROPERTY COMPILE_DEFINITIONS "USE_RAVICOMP=1") + set(USE_RAVICOMP 1) +endif() if (APPLE) set_property( TARGET ${LIBRAVI_NAME} libravinojit_static diff --git a/cmake/FindRaviComp.cmake b/cmake/FindRaviComp.cmake new file mode 100644 index 0000000..fd9c10d --- /dev/null +++ b/cmake/FindRaviComp.cmake @@ -0,0 +1,14 @@ +find_path(RAVICOMP_INCLUDE_DIRS ravi_compiler.h + PATHS + c:/Software/ravicomp/include/ravicomp + ~/Software/ravicomp/include/ravicomp + NO_DEFAULT_PATH + ) + +find_library(RAVICOMP_LIBRARIES + NAMES ravicomp + PATHS + c:/Software/ravicomp/lib + ~/Software/ravicomp/lib + ) + diff --git a/include/lualib.h b/include/lualib.h index 1579358..50f7dac 100644 --- a/include/lualib.h +++ b/include/lualib.h @@ -54,8 +54,10 @@ LUAMOD_API int (luaopen_package) (lua_State *L); #define LUA_RAVILIBNAME "ravi" LUAMOD_API int (raviopen_jit)(lua_State *L); -#define LUA_ASTLIBNAME "ast" -LUAMOD_API int (raviopen_ast_library)(lua_State *L); +#define LUA_RAVICOMPLIBNAME "compiler" +LUAMOD_API int (raviopen_compiler)(lua_State *L); + + /* open all previous libraries */ LUALIB_API void (luaL_openlibs) (lua_State *L); diff --git a/ravi-config.h.in b/ravi-config.h.in index 0e21edc..bf82f93 100644 --- a/ravi-config.h.in +++ b/ravi-config.h.in @@ -5,5 +5,6 @@ #cmakedefine USE_LLVM @USE_LLVM@ #cmakedefine USE_OMRJIT @USE_OMRJIT@ #cmakedefine USE_MIRJIT @USE_MIRJIT@ +#cmakedefine USE_RAVICOMP @USE_RAVICOMP@ #endif //_REDUKTI_RAVI_CONFIG_H_IN_H diff --git a/src/linit.c b/src/linit.c index d3f44b3..d2c2ac4 100644 --- a/src/linit.c +++ b/src/linit.c @@ -54,6 +54,9 @@ static const luaL_Reg loadedlibs[] = { {LUA_BITLIBNAME, luaopen_bit32}, #endif {LUAJIT_BITLIBNAME, luaopen_bit }, +#if defined(USE_RAVICOMP) + {LUA_RAVICOMPLIBNAME, raviopen_compiler }, +#endif {NULL, NULL} }; diff --git a/src/ravi_complib.c b/src/ravi_complib.c new file mode 100644 index 0000000..77fb268 --- /dev/null +++ b/src/ravi_complib.c @@ -0,0 +1,75 @@ +#include "ravi_api.h" + +#define LUA_CORE + +#include "lua.h" +#include "lapi.h" +#include "lauxlib.h" +#include "lfunc.h" +#include "lmem.h" +#include "lstring.h" +#include "ltable.h" + +struct CompilerContext { + lua_State* L; + Table* h; /* to avoid collection/reuse strings */ +}; + +static Proto* lua_newProto(void* context, Proto* parent) { + struct CompilerContext* ccontext = (struct CompilerContext*)context; + lua_State* L = ccontext->L; + Proto* p = luaF_newproto(L); + if (parent) { + int old_size = parent->sizep; + int new_size = parent->sizep + 1; + luaM_growvector(L, parent->p, old_size, new_size, Proto*, MAXARG_Bx, "functions"); + parent->p[parent->sizep++] = p; + // luaC_objbarrier(L, f, clp); + } + return p; +} + +/* +** creates a new string and anchors it in scanner's table so that +** it will not be collected until the end of the compilation +** (by that time it should be anchored somewhere) +*/ +//TString* create_newstring(lua_State* L, Table *h, const char* str, size_t l) { +// TValue* o; /* entry for 'str' */ +// TString* ts = luaS_newlstr(L, str, l); /* create new string */ +// setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */ +// o = luaH_set(L, h, L->top - 1); +// if (ttisnil(o)) { /* not in use yet? */ +// /* boolean value does not need GC barrier; +// table has no metatable, so it does not need to invalidate cache */ +// setbvalue(o, 1); /* t[string] = true */ +// luaC_checkGC(L); +// } +// else { /* string already present */ +// ts = tsvalue(keyfromval(o)); /* re-use value previously stored */ +// } +// L->top--; /* remove string from stack */ +// return ts; +//} + +/* Add a string constant to Proto and return its index */ +static int lua_newStringConstant(void *context, Proto* proto, const char* s, unsigned len) { + struct CompilerContext* ccontext = (struct CompilerContext*)context; + lua_State* L = ccontext->L; + Table* h = ccontext->h; + //TString* ts = create_newstring(L, h, s, len); + +} + +/* Compile the C code for the given proto, and C source */ +void lua_compileProto(lua_State* L, Proto* proto, const char* C_src, unsigned len) { + +} + +static const luaL_Reg ravilib[] = { + {NULL, NULL} }; + +int (raviopen_compiler)(lua_State *L) { + luaL_newlib(L, ravilib); + return 1; +} \ No newline at end of file