issue #126 add support for dmr_C based LLVM backend. Not enabled yet.

pull/168/head
Dibyendu Majumdar 5 years ago
parent b1396c8886
commit 9dfcd7c2b8

@ -875,6 +875,12 @@ class RaviCodeGenerator {
public:
RaviCodeGenerator(RaviJITState *jitState);
// Compile given function if possible
// The p->ravi_jit structure will be updated
// Note that if a function fails to compile then
// a flag is set so that it doesn't get compiled again
bool alt_compile(lua_State *L, Proto *p, std::shared_ptr<RaviJITModule> module, ravi_compile_options_t *options);
// Compile given function if possible
// The p->ravi_jit structure will be updated
// Note that if a function fails to compile then

@ -23,6 +23,7 @@
#include <ravijit.h>
#include <ravi_llvmcodegen.h>
#include <ravi_jitshared.h>
#include <dmr_c.h>
namespace ravi {
@ -1306,6 +1307,64 @@ llvm::Value *RaviCodeGenerator::emit_gep_upval_value(
return emit_gep(def, "value", pupval, 0, 2);
}
bool RaviCodeGenerator::alt_compile(lua_State *L, Proto *p,
std::shared_ptr<RaviJITModule> module,
ravi_compile_options_t *options) {
if (p->ravi_jit.jit_status != RAVI_JIT_NOT_COMPILED) {
return false;
}
if (module->owner()->get_compiling_flag()) return false;
if (!raviJ_cancompile(p)) {
p->ravi_jit.jit_status = RAVI_JIT_CANT_COMPILE;
return false;
}
auto M = module->module();
LLVMModuleRef moduleRef = llvm::wrap(M);
// Set flag so we can avoid recursive calls
module->owner()->set_compiling_flag(true);
membuff_t buf;
membuff_init(&buf, 4096);
const char *fname = unique_function_name();
if (!raviJ_codegen(L, p, options, fname, &buf)) {
p->ravi_jit.jit_status = RAVI_JIT_CANT_COMPILE;
}
else {
if (options->manual_request && module->owner()->get_verbosity()) {
ravi_writestring(L, buf.buf, strlen(buf.buf));
ravi_writeline(L);
}
char *argv[] = {(char *)fname, NULL};
if (!dmrC_llvmcompile(2, argv, moduleRef, buf.buf)) {
p->ravi_jit.jit_status = RAVI_JIT_CANT_COMPILE;
}
else {
p->ravi_jit.jit_function = nullptr;
std::unique_ptr<ravi::RaviJITFunction> func =
std::unique_ptr<RaviJITFunction>(new RaviJITFunction(&p->ravi_jit.jit_function, module, fname));
if (func->function() == nullptr) {
fprintf(stderr, "LLVM Compilation failed\n");
exit(1);
}
bool doVerify = module->owner()->get_validation() != 0;
if (doVerify && llvm::verifyFunction(*func->function(), &llvm::errs())) {
func->dump();
fprintf(stderr, "LLVM Code Verification failed\n");
exit(1);
}
ravi::RaviJITFunction *llvm_func = func.release();
p->ravi_jit.jit_data = reinterpret_cast<void *>(llvm_func);
p->ravi_jit.jit_status = RAVI_JIT_COMPILED;
}
}
membuff_free(&buf);
module->owner()->set_compiling_flag(false);
return p->ravi_jit.jit_status == RAVI_JIT_COMPILED;
}
bool RaviCodeGenerator::compile(lua_State *L, Proto *p,
std::shared_ptr<RaviJITModule> module,
ravi_compile_options_t *options) {

@ -39,6 +39,30 @@ namespace ravi {
// see below
static std::atomic_int init;
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",
"for llimit must be a number",
"for step must be a number",
"for initial value must be a number",
"array index is out of bounds",
"string expected",
"closure expected",
"type mismatch: wrong userdata type",
NULL};
static void raise_error(lua_State *L, int errorcode) {
assert(errorcode >= 0 && errorcode <= Error_type_mismatch);
luaG_runerror(L, errortext[errorcode]);
}
static struct {
const char *name;
void *address;
@ -106,7 +130,43 @@ static struct {
{"lua_concat", reinterpret_cast<void *>(lua_concat)},
{"lua_len", reinterpret_cast<void *>(lua_len)},
{"lua_stringtonumber", reinterpret_cast<void *>(lua_stringtonumber)},
{"luaF_close", reinterpret_cast<void *>(luaF_close)},
{"raise_error", reinterpret_cast<void *>(raise_error)},
{"luaV_tonumber_", reinterpret_cast<void *>(luaV_tonumber_)},
{"luaV_tointeger", reinterpret_cast<void *>(luaV_tointeger)},
{"luaD_poscall", reinterpret_cast<void *>(luaD_poscall)},
{"luaV_equalobj", reinterpret_cast<void *>(luaV_equalobj)},
{"luaV_lessthan", reinterpret_cast<void *>(luaV_lessthan)},
{"luaV_lessequal", reinterpret_cast<void *>(luaV_lessequal)},
{"luaV_execute", reinterpret_cast<void *>(luaV_execute)},
{"luaV_gettable", reinterpret_cast<void *>(luaV_gettable)},
{"luaV_settable", reinterpret_cast<void *>(luaV_settable)},
{"luaD_precall", reinterpret_cast<void *>(luaD_precall)},
{"raviV_op_newtable", reinterpret_cast<void *>(raviV_op_newtable)},
{"luaO_arith", reinterpret_cast<void *>(luaO_arith)},
{"raviV_op_newarrayint", reinterpret_cast<void *>(raviV_op_newarrayint)},
{"raviV_op_newarrayfloat", reinterpret_cast<void *>(raviV_op_newarrayfloat)},
{"raviV_op_setlist", reinterpret_cast<void *>(raviV_op_setlist)},
{"raviV_op_concat", reinterpret_cast<void *>(raviV_op_concat)},
{"raviV_op_closure", reinterpret_cast<void *>(raviV_op_closure)},
{"raviV_op_vararg", reinterpret_cast<void *>(raviV_op_vararg)},
{"luaV_objlen", reinterpret_cast<void *>(luaV_objlen)},
{"luaV_forlimit", reinterpret_cast<void *>(luaV_forlimit)},
{"raviV_op_setupval", reinterpret_cast<void *>(raviV_op_setupval)},
{"raviV_op_setupvali", reinterpret_cast<void *>(raviV_op_setupvali)},
{"raviV_op_setupvalf", reinterpret_cast<void *>(raviV_op_setupvalf)},
{"raviV_op_setupvalai", reinterpret_cast<void *>(raviV_op_setupvalai)},
{"raviV_op_setupvalaf", reinterpret_cast<void *>(raviV_op_setupvalaf)},
{"raviV_op_setupvalt", reinterpret_cast<void *>(raviV_op_setupvalt)},
{"luaD_call", reinterpret_cast<void *>(luaD_call)},
{"raviH_set_int", reinterpret_cast<void *>(raviH_set_int)},
{"raviH_set_float", reinterpret_cast<void *>(raviH_set_float)},
{"raviV_check_usertype", reinterpret_cast<void *>(raviV_check_usertype)},
{"luaT_trybinTM", reinterpret_cast<void *>(luaT_trybinTM)},
{"raviV_gettable_sskey", reinterpret_cast<void *>(raviV_gettable_sskey)},
{"raviV_settable_sskey", reinterpret_cast<void *>(raviV_settable_sskey)},
{"raviV_gettable_i", reinterpret_cast<void *>(raviV_gettable_i)},
{"raviV_settable_i", reinterpret_cast<void *>(raviV_settable_i)},
{"printf", reinterpret_cast<void *>(printf)},
{"puts", reinterpret_cast<void *>(puts)},
{nullptr, nullptr}};

Loading…
Cancel
Save