From 6b273f746835e46614f75d2cb6b3ddd88668feb1 Mon Sep 17 00:00:00 2001 From: Dibyendu Majumdar Date: Sat, 3 Oct 2020 23:32:52 +0100 Subject: [PATCH] issue #198 Make the MIR portion of the compiler optional so that we can just get the C codegen --- CMakeLists.txt | 28 +++++++++++++++------------- src/ravi_complib.c | 12 ++++++++++++ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f7b4a41..24c008c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ 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) +option(RAVICOMP "Controls whether to link in RaviComp" OFF) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") @@ -83,15 +83,15 @@ endif () include(CheckCCompilerFlag) check_c_compiler_flag("-march=native" COMPILER_OPT_ARCH_NATIVE_SUPPORTED) if (COMPILER_OPT_ARCH_NATIVE_SUPPORTED AND NOT CMAKE_C_FLAGS MATCHES "-march=") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native") -endif() + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native") +endif () if (ASAN) set(CMAKE_REQUIRED_FLAGS "-fsanitize=address") check_c_compiler_flag("-fsanitize=address" COMPILER_ASAN_SUPPORTED) if (COMPILER_ASAN_SUPPORTED AND NOT CMAKE_C_FLAGS_DEBUG MATCHES "-fsanitize=address") set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fsanitize=address") - endif() -endif() + endif () +endif () if (LLVM_JIT) find_package(LLVM REQUIRED CONFIG) @@ -135,26 +135,28 @@ else () # CLion seems unable to handle include paths set on sources include_directories("${CMAKE_SOURCE_DIR}/mir;${CMAKE_SOURCE_DIR}/mir/c2mir") endif () - else() + else () set(JIT_SRCS ${NO_JIT_SRCS}) endif () endif () -if (RAVICOMP AND MIR_JIT) +if (RAVICOMP) # Need MIR_JIT for the compiler add-on find_package(RaviComp REQUIRED) set(ADDON_SRCS ${RAVICOMP_SRCS}) - set_property(SOURCE ${RAVICOMP_SRCS} - APPEND - PROPERTY INCLUDE_DIRECTORIES ${RAVICOMP_INCLUDE_DIRS}) set_property(SOURCE ${RAVICOMP_SRCS} APPEND - PROPERTY INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/mir;${CMAKE_SOURCE_DIR}/mir/c2mir") + PROPERTY INCLUDE_DIRECTORIES ${RAVICOMP_INCLUDE_DIRS}) + if (MIR_JIT) + set_property(SOURCE ${RAVICOMP_SRCS} + APPEND + PROPERTY INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/mir;${CMAKE_SOURCE_DIR}/mir/c2mir") + endif () if ($ENV{CLION_IDE}) # CLion seems unable to handle include paths set on sources include_directories(${RAVICOMP_INCLUDE_DIRS}) endif () -endif() +endif () # IDE stuff if (MSVC OR APPLE) @@ -310,7 +312,7 @@ if (RAVICOMP) APPEND PROPERTY COMPILE_DEFINITIONS "USE_RAVICOMP=1") set(USE_RAVICOMP 1) -endif() +endif () if (APPLE) set_property( TARGET ${LIBRAVI_NAME} libravinojit_static diff --git a/src/ravi_complib.c b/src/ravi_complib.c index cbc92de..d245245 100644 --- a/src/ravi_complib.c +++ b/src/ravi_complib.c @@ -172,21 +172,33 @@ static int lua_addUpValue(void* context, Proto* f, struct string_object* name, u static void init_C_compiler(void* context) { struct CompilerContext* ccontext = (struct CompilerContext*)context; +#ifdef USE_MIRJIT mir_prepare(ccontext->jit->jit, 2); +#endif } static void* compile_C(void* context, const char* C_src, unsigned len) { struct CompilerContext* ccontext = (struct CompilerContext*)context; fprintf(stdout, "%s\n", C_src); +#ifdef USE_MIRJIT return mir_compile_C_module(&ccontext->jit->options, ccontext->jit->jit, C_src, "input"); +#else + return NULL; +#endif } static void finish_C_compiler(void* context) { struct CompilerContext* ccontext = (struct CompilerContext*)context; +#ifdef USE_MIRJIT mir_cleanup(ccontext->jit->jit); +#endif } static lua_CFunction get_compiled_function(void* context, void* module, const char* name) { struct CompilerContext* ccontext = (struct CompilerContext*)context; +#if USE_MIRJIT MIR_module_t M = (MIR_module_t)module; return (lua_CFunction)mir_get_func(ccontext->jit->jit, M, name); +#else + return NULL; +#endif } static void lua_setProtoFunction(void* context, Proto* p, lua_CFunction func) { p->ravi_jit.jit_function = func;