issue #110 Initial implementation of JIT backend using OMRJIT

pull/167/head
Dibyendu Majumdar 6 years ago
parent a3757ce010
commit 47e0cddfaf

@ -6,14 +6,23 @@ enable_language(C)
enable_language(ASM)
enable_testing()
# Get access to CMake helpers
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
# By default JIT is OFF
option(LLVM_JIT "Controls whether LLVM JIT compilation will be enabled, default is OFF" OFF)
option(OMR_JIT "Controls whether NanoJIT compilation will be enabled, default is OFF" OFF)
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 OFF" ON)
option(ASM_VM "Controls whether to use the new VM (not ready yet! so don't turn on)" OFF)
option(LTESTS "Controls whether ltests are enabled in Debug mode" ON)
if (LLVM_JIT)
if (LLVM_JIT AND OMR_JIT)
message(FATAL_ERROR
"Both LLVM_JIT and OMR_JIT cannot be set to ON at the same time")
endif()
if (LLVM_JIT OR OMR_JIT)
set(ASM_VM OFF)
endif()
@ -44,12 +53,18 @@ if (LLVM_JIT)
# library
endif()
if (OMR_JIT)
find_package(OMRJIT REQUIRED)
include_directories(${OMRJIT_INCLUDE_DIRS})
add_definitions(-DUSE_OMRJIT)
endif()
message(STATUS "Computed goto ${COMPUTED_GOTO}")
if (COMPUTED_GOTO AND MSVC)
message(WARNING "Computed goto is not available with MSVC")
endif()
if (NOT LLVM_JIT)
if (NOT LLVM_JIT AND NOT OMR_JIT)
message(WARNING "LLVM will not be enabled; specify -DLLVM_JIT=ON to enable")
endif()
@ -166,6 +181,8 @@ if (LLVM_JIT)
src/ravi_llvmarith1.cpp src/ravi_llvmcall.cpp src/ravi_llvmtable.cpp
src/ravi_llvmarith2.cpp src/ravi_llvmtforcall.cpp src/ravi_llvmrest.cpp
src/ravi_llvmluaapi.cpp)
elseif (OMR_JIT)
set(OMR_JIT_SRCS src/ravi_omrjit.c)
else()
set(NO_JIT_SRCS src/ravi_nojit.c)
endif()
@ -270,6 +287,14 @@ if (LLVM_JIT)
dmr_c/llvm-backend/dmr_c.h
)
include_directories("${PROJECT_SOURCE_DIR}/dmr_c/llvm-backend")
elseif (OMR_JIT)
set(DMR_C_JIT_SRCS
dmr_c/omrjit-backend/sparse-omrjit.c
)
set(DMR_C_JIT_HEADERS
dmr_c/omrjit-backend/dmr_c.h
)
include_directories("${PROJECT_SOURCE_DIR}/dmr_c/omrjit-backend")
else()
set(DMR_C_JIT_HEADERS
dmr_c/null-backend/dmr_c.h
@ -343,6 +368,8 @@ endif()
if (LLVM_JIT)
set (LIBRAVI_NAME libravillvm)
elseif (OMR_JIT)
set (LIBRAVI_NAME libravilomr)
else()
set (LIBRAVI_NAME libravinojit)
endif()
@ -353,6 +380,7 @@ add_library(${LIBRAVI_NAME} ${LIBRAVI_BUILD_TYPE}
${LUA_LIB_SRCS}
${LUA_CORE_SRCS}
${LLVM_JIT_SRCS}
${OMR_JIT_SRCS}
${NO_JIT_SRCS}
${DMR_C_HEADERS}
${DMR_C_SRCS}
@ -370,10 +398,13 @@ endif()
if (LLVM_JIT)
set_target_properties(${LIBRAVI_NAME} PROPERTIES COMPILE_DEFINITIONS "USE_LLVM=1")
endif()
if (OMR_JIT)
set_target_properties(${LIBRAVI_NAME} PROPERTIES COMPILE_DEFINITIONS "USE_OMRJIT=1")
endif()
if (EMBEDDED_DMRC)
set_target_properties(${LIBRAVI_NAME} PROPERTIES COMPILE_DEFINITIONS "USE_DMR_C=1")
endif()
target_link_libraries(${LIBRAVI_NAME} ${EXTRA_LIBRARIES} ${LLVM_LIBS})
target_link_libraries(${LIBRAVI_NAME} ${EXTRA_LIBRARIES} ${LLVM_LIBS} ${OMRJIT_LIBRARIES})
# Main Ravi executable
add_executable(ravi src/lua.c)

@ -0,0 +1,4 @@
mkdir omrjit
cd omrjit
cmake -DCMAKE_INSTALL_PREFIX=c:\Software\ravi -G "Visual Studio 15 2017 Win64" -DCMAKE_BUILD_TYPE=Debug -DOMR_JIT=ON ..
cd ..

@ -0,0 +1,16 @@
find_path(OMRJIT_INCLUDE_DIR nj_api.h
PATHS
c:/Software/omr/include/nj
~/Software/omr/include/nj
NO_DEFAULT_PATH
)
find_library(OMRJIT_LIBRARY
NAMES nj libnj libnj.dylib
PATHS
c:/Software/omr/lib
~/Software/omr/lib
)
set( OMRJIT_INCLUDE_DIRS "${OMRJIT_INCLUDE_DIR}" )
set( OMRJIT_LIBRARIES "${OMRJIT_LIBRARY}" )

@ -0,0 +1,42 @@
/**
* This is a backend code generator for dmr_C that uses
* the JIT engine NanoJIT (https://github.com/dibyendumajumdar/nanojit).
*
* Copyright (C) 2017 Dibyendu Majumdar
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef DMR_C_H
#define DMR_C_H
#include <nj_api.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
bool dmrC_omrcompile(int argc, char **argv, JIT_ContextRef context,
const char *inputbuffer);
#ifdef __cplusplus
}
#endif
#endif

File diff suppressed because it is too large Load Diff

@ -973,6 +973,13 @@ static void predefined_type_size(struct dmr_C *C, const char *name, const char *
static void predefined_macros(struct dmr_C *C)
{
#if LLVM_JIT
dmrC_add_pre_buffer(C, "#define __LLVM_BACKEND__ 1\n");
#elif NANO_JIT
dmrC_add_pre_buffer(C, "#define __NANOJIT_BACKEND__ 1\n");
#elif OMR_JIT
dmrC_add_pre_buffer(C, "#define __OMR_BACKEND__ 1\n");
#endif
dmrC_add_pre_buffer(C, "#define __CHECKER__ 1\n");
predefined_sizeof(C, "SHORT", C->target->bits_in_short);

@ -812,7 +812,7 @@ pseudo_t dmrC_value_pseudo(struct dmr_C *C, struct symbol *type, long long val)
int size = type ? type->bit_size : dmrC_value_size(val);
pseudo_t pseudo;
assert(size <= (sizeof(long long) * 8));
assert(size == -1 || size <= (sizeof(long long) * 8));
FOR_EACH_PTR(*list, pseudo) {
if (pseudo->value == val && pseudo->size == size)
@ -1757,12 +1757,14 @@ static pseudo_t linearize_compound_statement(struct dmr_C *C, struct entrypoint
if (!phi_node)
return pseudo;
#if 0
/* https://github.com/lucvoo/sparse/commit/1609176c9 */
if (dmrC_pseudo_list_size(phi_node->phi_list)==1) {
pseudo = dmrC_first_pseudo(phi_node->phi_list);
assert(pseudo->type == PSEUDO_PHI);
return pseudo->def->src1;
}
#endif
return phi_node->target;
}

@ -0,0 +1,65 @@
/******************************************************************************
* Copyright (C) 2015-2017 Dibyendu Majumdar
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
/******************************************************************************
* Copyright (C) 2015-2018 Dibyendu Majumdar
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
#ifndef RAVI_OMRJIT_H
#define RAVI_OMRJIT_H
#include <ravi_jitshared.h>
#ifdef USE_OMRJIT
#include "dmr_c.h"
struct ravi_State {
JIT_ContextRef jit;
unsigned long long id; // counter to generate function names
int verbosity;
};
#ifdef __cplusplus
};
#endif
#endif /* USE_OMRJIT */
#endif /* RAVI_OMRJIT_H */

@ -679,6 +679,9 @@ bool raviJ_cancompile(Proto *p) {
case OP_NOT:
case OP_SETLIST: break;
#if 0
case OP_RAVI_TOCLOSURE:
case OP_RAVI_TOSTRING:
case OP_RAVI_TOTYPE:
case OP_LOADKX:
case OP_UNM:
#endif

@ -0,0 +1,393 @@
/******************************************************************************
* Copyright (C) 2018 Dibyendu Majumdar
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
#include <ravi_omrjit.h>
#include <ravijit.h>
#include <stddef.h>
#include <assert.h>
// FIXME should be in ravi_State
static int id = 0;
#define LUA_CORE
#include "lauxlib.h"
#include "lobject.h"
#include "lstate.h"
#include "lua.h"
static const char *errortext[] = {
"integer expected",
"number expected",
"integer[] expected",
"number[] expected",
"table expected",
"upvalue of integer type, cannot be set to non integer value",
"upvalue of number type, cannot be set to non number value",
"upvalue of integer[] type, cannot be set to non integer[] value",
"upvalue of number[] type, cannot be set to non number[] value",
"upvalue of table type, cannot be set to non table value",
NULL};
static void raise_error(lua_State *L, int errorcode) {
assert(errorcode >= 0 && errorcode <= Error_upval_needs_table);
luaG_runerror(L, errortext[errorcode]);
}
static void register_builtin_arg1(JIT_ContextRef module, const char *name,
void *fp, JIT_Type return_type,
JIT_Type arg1) {
JIT_Type args[1];
args[0] = arg1;
JIT_RegisterFunction(module, name, return_type, 1, args, fp);
}
static void register_builtin_arg2(JIT_ContextRef module, const char *name,
void *fp, JIT_Type return_type,
JIT_Type arg1,
JIT_Type arg2) {
JIT_Type args[2];
args[0] = arg1;
args[1] = arg2;
JIT_RegisterFunction(module, name, return_type, 2, args, fp);
}
static void register_builtin_arg3(JIT_ContextRef module, const char *name,
void *fp, JIT_Type return_type,
JIT_Type arg1,
JIT_Type arg2,
JIT_Type arg3) {
JIT_Type args[3];
args[0] = arg1;
args[1] = arg2;
args[2] = arg3;
JIT_RegisterFunction(module, name, return_type, 3, args, fp);
}
static void register_builtin_arg4(JIT_ContextRef module, const char *name,
void *fp, JIT_Type return_type,
JIT_Type arg1,
JIT_Type arg2,
JIT_Type arg3,
JIT_Type arg4) {
JIT_Type args[4];
args[0] = arg1;
args[1] = arg2;
args[2] = arg3;
args[3] = arg4;
JIT_RegisterFunction(module, name, return_type, 4, args, fp);
}
static void register_builtin_arg5(JIT_ContextRef module, const char *name,
void *fp, JIT_Type return_type,
JIT_Type arg1,
JIT_Type arg2,
JIT_Type arg3,
JIT_Type arg4,
JIT_Type arg5) {
JIT_Type args[5];
args[0] = arg1;
args[1] = arg2;
args[2] = arg3;
args[3] = arg4;
args[4] = arg5;
JIT_RegisterFunction(module, name, return_type, 5, args, fp);
}
// Initialize the JIT State and attach it to the
// Global Lua State
// If a JIT State already exists then this function
// will return -1
int raviV_initjit(struct lua_State *L) {
global_State *G = G(L);
if (G->ravi_state != NULL) return -1;
ravi_State *jit = (ravi_State *)calloc(1, sizeof(ravi_State));
// The parameter true means we will be dumping stuff as we compile
jit->jit = JIT_CreateContext();
//extern void luaF_close (lua_State *L, StkId level);
register_builtin_arg2(jit->jit, "luaF_close", luaF_close, JIT_NoType, JIT_Address, JIT_Address);
register_builtin_arg2(jit->jit, "raise_error", raise_error, JIT_NoType, JIT_Address, JIT_Int32);
//extern int luaV_tonumber_(const TValue *obj, lua_Number *n);
register_builtin_arg2(jit->jit, "luaV_tonumber_", luaV_tonumber_, JIT_Int32, JIT_Address, JIT_Address);
//extern int luaV_tointeger(const TValue *obj, lua_Integer *p, int mode);
register_builtin_arg3(jit->jit, "luaV_tointeger", luaV_tointeger, JIT_Int32, JIT_Address, JIT_Address, JIT_Int32);
//extern int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres);
register_builtin_arg4(jit->jit, "luaD_poscall", luaD_poscall, JIT_Int32, JIT_Address, JIT_Address, JIT_Address, JIT_Int32);
//extern int luaV_equalobj(lua_State *L, const TValue *t1, const TValue *t2);\n"
register_builtin_arg3(jit->jit, "luaV_equalobj", luaV_equalobj, JIT_Int32, JIT_Address, JIT_Address, JIT_Address);
//extern int luaV_lessthan(lua_State *L, const TValue *l, const TValue *r);\n"
register_builtin_arg3(jit->jit, "luaV_lessthan", luaV_lessthan, JIT_Int32, JIT_Address, JIT_Address, JIT_Address);
//extern int luaV_lessequal(lua_State *L, const TValue *l, const TValue *r);\n"
register_builtin_arg3(jit->jit, "luaV_lessequal", luaV_lessequal, JIT_Int32, JIT_Address, JIT_Address, JIT_Address);
//extern int luaV_execute(lua_State *L);
register_builtin_arg1(jit->jit, "luaV_execute", luaV_execute, JIT_Int32, JIT_Address);
//extern void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val)
register_builtin_arg4(jit->jit, "luaV_gettable", luaV_gettable, JIT_NoType, JIT_Address, JIT_Address, JIT_Address, JIT_Address);
//extern void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val);
register_builtin_arg4(jit->jit, "luaV_settable", luaV_settable, JIT_NoType, JIT_Address, JIT_Address, JIT_Address, JIT_Address);
//int luaD_precall (lua_State *L, StkId func, int nresults, int op_call);
register_builtin_arg4(jit->jit, "luaD_precall", luaD_precall, JIT_Int32, JIT_Address, JIT_Address, JIT_Int32, JIT_Int32);
//extern void raviV_op_newtable(lua_State *L, CallInfo *ci, TValue *ra, int b, int c)
register_builtin_arg5(jit->jit, "raviV_op_newtable", raviV_op_newtable, JIT_NoType, JIT_Address, JIT_Address, JIT_Address, JIT_Int32, JIT_Int32);
//extern void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, TValue *res);
register_builtin_arg5(jit->jit, "luaO_arith", luaO_arith, JIT_NoType, JIT_Address, JIT_Int32, JIT_Address, JIT_Address, JIT_Address);
//extern void raviV_op_newarrayint(lua_State *L, CallInfo *ci, TValue *ra);
register_builtin_arg3(jit->jit, "raviV_op_newarrayint", raviV_op_newarrayint, JIT_NoType, JIT_Address, JIT_Address, JIT_Address);
//extern void raviV_op_newarrayfloat(lua_State *L, CallInfo *ci, TValue *ra);
register_builtin_arg3(jit->jit, "raviV_op_newarrayfloat", raviV_op_newarrayfloat, JIT_NoType, JIT_Address, JIT_Address, JIT_Address);
//LUAI_FUNC void raviV_op_setlist(lua_State *L, CallInfo *ci, TValue *ra, int b, int c);
register_builtin_arg5(jit->jit, "raviV_op_setlist", raviV_op_setlist, JIT_NoType, JIT_Address, JIT_Address, JIT_Address, JIT_Int32, JIT_Int32);
//LUAI_FUNC void raviV_op_concat(lua_State *L, CallInfo *ci, int a, int b, int c);
register_builtin_arg5(jit->jit, "raviV_op_concat", raviV_op_concat, JIT_NoType, JIT_Address, JIT_Address, JIT_Int32, JIT_Int32, JIT_Int32);
//LUAI_FUNC void raviV_op_closure(lua_State *L, CallInfo *ci, LClosure *cl, int a, int Bx);
register_builtin_arg5(jit->jit, "raviV_op_closure", raviV_op_closure, JIT_NoType, JIT_Address, JIT_Address, JIT_Address, JIT_Int32, JIT_Int32);
//LUAI_FUNC void raviV_op_vararg(lua_State *L, CallInfo *ci, LClosure *cl, int a, int b);
register_builtin_arg5(jit->jit, "raviV_op_vararg", raviV_op_vararg, JIT_NoType, JIT_Address, JIT_Address, JIT_Address, JIT_Int32, JIT_Int32);
// void luaV_objlen (lua_State *L, StkId ra, const TValue *rb)
register_builtin_arg3(jit->jit, "luaV_objlen", luaV_objlen, JIT_NoType, JIT_Address, JIT_Address, JIT_Address);
//int luaV_forlimit(const TValue *obj, lua_Integer *p, lua_Integer step, int *stopnow);
register_builtin_arg4(jit->jit, "luaV_forlimit", luaV_forlimit, JIT_Int32, JIT_Address, JIT_Address, JIT_Int64, JIT_Address);
// void raviV_op_setupval(lua_State *L, LClosure *cl, TValue *ra, int b);
register_builtin_arg4(jit->jit, "raviV_op_setupval", raviV_op_setupval, JIT_NoType, JIT_Address, JIT_Address, JIT_Address, JIT_Int32);
register_builtin_arg4(jit->jit, "raviV_op_setupvali", raviV_op_setupvali, JIT_NoType, JIT_Address, JIT_Address, JIT_Address, JIT_Int32);
register_builtin_arg4(jit->jit, "raviV_op_setupvalf", raviV_op_setupvalf, JIT_NoType, JIT_Address, JIT_Address, JIT_Address, JIT_Int32);
register_builtin_arg4(jit->jit, "raviV_op_setupvalai", raviV_op_setupvalai, JIT_NoType, JIT_Address, JIT_Address, JIT_Address, JIT_Int32);
register_builtin_arg4(jit->jit, "raviV_op_setupvalaf", raviV_op_setupvalaf, JIT_NoType, JIT_Address, JIT_Address, JIT_Address, JIT_Int32);
register_builtin_arg4(jit->jit, "raviV_op_setupvalt", raviV_op_setupvalt, JIT_NoType, JIT_Address, JIT_Address, JIT_Address, JIT_Int32);
//extern void luaD_call (lua_State *L, StkId func, int nResults);
register_builtin_arg3(jit->jit, "luaD_call", luaD_call, JIT_NoType, JIT_Address, JIT_Address, JIT_Int32);
//"extern void raviH_set_int(lua_State *L, Table *t, lua_Unsigned key, lua_Integer value);\n"
register_builtin_arg4(jit->jit, "raviH_set_int", raviH_set_int, JIT_NoType, JIT_Address, JIT_Address, JIT_Int64, JIT_Int64);
//"extern void raviH_set_float(lua_State *L, Table *t, lua_Unsigned key, lua_Number value);\n"
register_builtin_arg4(jit->jit, "raviH_set_float", raviH_set_float, JIT_NoType, JIT_Address, JIT_Address, JIT_Int64, JIT_Double);
G->ravi_state = jit;
return 0;
}
// Free up the JIT State
void raviV_close(struct lua_State *L) {
global_State *G = G(L);
if (G->ravi_state == NULL) return;
// All compiled functions will be deleted at this stage
JIT_DestroyContext(G->ravi_state->jit);
free(G->ravi_state);
}
// Dump the IR
void raviV_dumpIR(struct lua_State *L, struct Proto *p) {
global_State *G = G(L);
if (G->ravi_state == NULL)
return;
membuff_t buf;
membuff_init(&buf, 4096);
char fname[30];
snprintf(fname, sizeof fname, "jit", G->ravi_state->id++);
ravi_compile_options_t options = { 0 };
options.codegen_type = RAVI_CODEGEN_ALL;
if (raviJ_codegen(L, p, &options, fname, &buf)) {
printf(buf.buf);
}
membuff_free(&buf);
}
// Dump the LLVM ASM
void raviV_dumpASM(struct lua_State *L, struct Proto *p) {
(void)L;
(void)p;
}
void raviV_setminexeccount(lua_State *L, int value) {
global_State *G = G(L);
if (!G->ravi_state) return;
// G->ravi_state->jit->min_exec_count_ = value;
}
int raviV_getminexeccount(lua_State *L) {
global_State *G = G(L);
// if (!G->ravi_state)
return 0;
// return G->ravi_state->jit->min_exec_count_;
}
void raviV_setmincodesize(lua_State *L, int value) {
global_State *G = G(L);
if (!G->ravi_state) return;
// G->ravi_state->jit->min_code_size_ = value;
}
int raviV_getmincodesize(lua_State *L) {
global_State *G = G(L);
// if (!G->ravi_state)
return 0;
// return G->ravi_state->jit->min_code_size_;
}
void raviV_setauto(lua_State *L, int value) {
global_State *G = G(L);
if (!G->ravi_state) return;
// G->ravi_state->jit->auto_ = value;
}
int raviV_getauto(lua_State *L) {
global_State *G = G(L);
// if (!G->ravi_state)
return 0;
// return G->ravi_state->jit->auto_;
}
// Turn on/off the JIT compiler
void raviV_setjitenabled(lua_State *L, int value) {
global_State *G = G(L);
if (!G->ravi_state) return;
// G->ravi_state->jit->enabled_ = value;
}
int raviV_getjitenabled(lua_State *L) {
global_State *G = G(L);
return G->ravi_state != NULL;
// return G->ravi_state->jit->enabled_;
}
void raviV_setoptlevel(lua_State *L, int value) {
global_State *G = G(L);
if (!G->ravi_state) return;
// G->ravi_state->jit->opt_level_ = value;
}
int raviV_getoptlevel(lua_State *L) {
global_State *G = G(L);
// if (!G->ravi_state)
return 0;
// return G->ravi_state->jit->opt_level_;
}
void raviV_setsizelevel(lua_State *L, int value) {
(void)L;
(void)value;
}
int raviV_getsizelevel(lua_State *L) {
(void)L;
return 0;
}
void raviV_setvalidation(lua_State *L, int value) {
(void)L;
(void)value;
}
int raviV_getvalidation(lua_State *L) {
(void)L;
return 0;
}
void raviV_setverbosity(lua_State *L, int value) {
(void)L;
(void)value;
}
int raviV_getverbosity(lua_State *L) {
(void)L;
return 0;
}
void raviV_setgcstep(lua_State *L, int value) {
(void)L;
(void)value;
}
int raviV_getgcstep(lua_State *L) {
(void)L;
return 0;
}
// Turn on/off the JIT compiler
void raviV_settraceenabled(lua_State *L, int value) {
(void)L;
(void)value;
}
int raviV_gettraceenabled(lua_State *L) {
(void)L;
return 0;
}
int raviV_compile_n(struct lua_State *L, struct Proto *p[], int n,
ravi_compile_options_t *options) {
int count = 0;
for (int i = 0; i < n; i++) {
if (raviV_compile(L, p[i], options)) count++;
}
return count > 0;
}
// Compile a Lua function
// If JIT is turned off then compilation is skipped
// Compilation occurs if either auto compilation is ON (subject to some
// thresholds)
// or if a manual compilation request was made
// Returns true if compilation was successful
int raviV_compile(struct lua_State *L, struct Proto *p,
ravi_compile_options_t *options) {
if (p->ravi_jit.jit_status == RAVI_JIT_COMPILED) return true;
else if (p->ravi_jit.jit_status == RAVI_JIT_CANT_COMPILE) return false;
if (options == NULL || !options->manual_request) return false;
if (!raviJ_cancompile(p)) {
p->ravi_jit.jit_status = RAVI_JIT_CANT_COMPILE;
return false;
}
global_State *G = G(L);
if (G->ravi_state == NULL) return false;
JIT_ContextRef context = G->ravi_state->jit;
if (context == NULL) return false;
membuff_t buf;
membuff_init(&buf, 4096);
char fname[30];
snprintf(fname, sizeof fname, "jit%lld", G->ravi_state->id++);
if (!raviJ_codegen(L, p, options, fname, &buf)) {
membuff_free(&buf);
p->ravi_jit.jit_status = RAVI_JIT_CANT_COMPILE;
return false;
}
int (*fp)(lua_State * L) = NULL;
char *argv[] = {NULL};
if (!dmrC_omrcompile(0, argv, context, buf.buf)) {
//printf("%s\n", buf.buf);
p->ravi_jit.jit_status = RAVI_JIT_CANT_COMPILE;
}
else {
fp = JIT_GetFunction(context, fname);
if (fp != NULL) {
p->ravi_jit.jit_data = NULL;
p->ravi_jit.jit_function = fp;
p->ravi_jit.jit_status = RAVI_JIT_COMPILED;
//printf("Compiled\n");
}
}
membuff_free(&buf);
return fp != NULL;
}
// Free the JIT compiled function
// Note that this is called by the garbage collector
void raviV_freeproto(struct lua_State *L, struct Proto *p) {
(void)L;
(void)p;
}
Loading…
Cancel
Save