issue #126 add a couple of simple hello world examples

gccjit-ravi534
Dibyendu Majumdar 7 years ago
parent 340e2f1769
commit 1a26a5d43b

@ -468,6 +468,9 @@ class RaviJITFunction {
llvm::FunctionType *type,
llvm::GlobalValue::LinkageTypes linkage,
const std::string &name);
RaviJITFunction(lua_CFunction *p, std::shared_ptr<RaviJITModule> module,
const std::string &name);
~RaviJITFunction();
const std::string &name() const { return name_; }

@ -0,0 +1,38 @@
-- Print anything - including nested tables
local function table_print (tt, indent, done)
done = done or {}
indent = indent or 0
if type(tt) == "table" then
for key, value in pairs (tt) do
io.write(string.rep (" ", indent)) -- indent it
if type (value) == "table" and not done [value] then
done [value] = true
io.write(string.format("[%s] => table\n", tostring (key)));
io.write(string.rep (" ", indent+4)) -- indent it
io.write("(\n");
table_print (value, indent + 7, done)
io.write(string.rep (" ", indent+4)) -- indent it
io.write(")\n");
else
io.write(string.format("[%s] => %s\n",
tostring (key), tostring(value)))
end
end
else
io.write(tostring(tt) .. "\n")
end
end
local somecode = [[
extern int adder(int a, int b);
int adder(int a, int b)
{
return a+b;
}
]]
local symbols = dmrc.getsymbols(somecode)
table_print(symbols)

@ -0,0 +1,20 @@
local testfunc = [[
struct lua_State;
extern int puts(const char *);
extern int TestFunc(struct lua_State *L);
int TestFunc(struct lua_State *L)
{
puts("hello world!\n");
return 0;
}
]]
local ctx = llvm.context()
local m = ctx:newmodule()
m:compileC(testfunc)
local f = m:getfunction("TestFunc")
local callable = f:compile()
callable()

@ -130,8 +130,8 @@ RaviJITState::RaviJITState()
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
llvm::InitializeNativeTargetAsmParser();
//TODO see email trail on resolving symbols in process
//llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
// TODO see email trail on resolving symbols in process
// llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
init++;
}
triple_ = llvm::sys::getProcessTriple();
@ -233,7 +233,7 @@ RaviJITModule::~RaviJITModule() {
delete module_;
owner_->decr_allocated_modules();
#if 1
//fprintf(stderr, "module destroyed\n");
// fprintf(stderr, "module destroyed\n");
#endif
}
@ -262,6 +262,14 @@ RaviJITFunction::RaviJITFunction(lua_CFunction *p,
id_ = module_->addFunction(this);
}
RaviJITFunction::RaviJITFunction(lua_CFunction *p,
std::shared_ptr<RaviJITModule> module,
const std::string &name)
: module_(module), name_(name), ptr_(nullptr), func_ptrptr_(p) {
function_ = module_->module()->getFunction(name);
id_ = module_->addFunction(this);
}
RaviJITFunction::~RaviJITFunction() {
// Remove this function from parent
// fprintf(stderr, "function destroyed\n");
@ -387,8 +395,12 @@ void RaviJITModule::finalize(bool doDump) {
// Lua Proto structure
void RaviJITFunction::setFunctionPtr() {
lua_assert(ptr_ == nullptr);
ptr_ = engine()->getPointerToFunction(function_);
*func_ptrptr_ = (lua_CFunction)ptr_;
// function_ may be nullptr if it was obtained by name from the module, so
// we need to check that
if (function_) {
ptr_ = engine()->getPointerToFunction(function_);
*func_ptrptr_ = (lua_CFunction)ptr_;
}
}
llvm::Function *RaviJITModule::addExternFunction(llvm::FunctionType *type,

@ -240,7 +240,7 @@ static void alloc_LLVM_module(lua_State *L,
/* __gc for ModuleHolder */
static int collect_LLVM_module(lua_State *L) {
ModuleHolder *mh = check_LLVM_module(L, 1);
//printf("Module released: usecount %d\n", (int)mh->M.use_count());
// printf("Module released: usecount %d\n", (int)mh->M.use_count());
mh->~ModuleHolder();
return 0;
}
@ -321,6 +321,7 @@ static void alloc_LLVM_phinode(lua_State *L, llvm::PHINode *phi) {
h->phi = phi;
}
// Creates a new module to hold a Lua C Function
static MainFunctionHolder *alloc_LLVM_mainfunction(lua_State *L,
ravi::RaviJITState *jit,
llvm::FunctionType *type,
@ -338,13 +339,28 @@ static MainFunctionHolder *alloc_LLVM_mainfunction(lua_State *L,
return h;
}
// References an existing function in a Module
static MainFunctionHolder *alloc_LLVM_luaCfunction(
lua_State *L, std::shared_ptr<ravi::RaviJITModule> module,
const char *name) {
MainFunctionHolder *h =
(MainFunctionHolder *)lua_newuserdata(L, sizeof(MainFunctionHolder));
h->func = nullptr;
h->compiled_func = nullptr;
h->arg1 = nullptr;
raviL_getmetatable(L, LLVM_mainfunction);
lua_setmetatable(L, -2);
h->func = new ravi::RaviJITFunction(&h->compiled_func, module, name);
return h;
}
/* __gc for FunctionHolder */
static int collect_LLVM_mainfunction(lua_State *L) {
MainFunctionHolder *builder = check_LLVM_mainfunction(L, 1);
if (builder->func) {
delete builder->func;
builder->func = nullptr;
//printf("collected function\n");
// printf("collected function\n");
}
return 0;
}
@ -655,6 +671,14 @@ static int module_newfunction(lua_State *L) {
return 1;
}
static int module_getfunction(lua_State *L) {
ModuleHolder *mh = check_LLVM_module(L, 1);
const char *name = luaL_checkstring(L, 2);
alloc_LLVM_luaCfunction(L, mh->M, name);
// TODO we should check that the function signature is correct
return 1;
}
static int module_compile_C(lua_State *L) {
ModuleHolder *mh = check_LLVM_module(L, 1);
const char *codebuffer = luaL_checkstring(L, 2);
@ -674,13 +698,12 @@ static int module_compile_C(lua_State *L) {
}
static int module_generate_code(lua_State *L) {
ModuleHolder *mh = check_LLVM_module(L, 1);
mh->M->runpasses();
mh->M->finalize();
return 0;
ModuleHolder *mh = check_LLVM_module(L, 1);
mh->M->runpasses();
mh->M->finalize();
return 0;
}
static int context_new_basicblock(lua_State *L) {
ContextHolder *context = check_LLVM_context(L, 1);
const char *name = luaL_checkstring(L, 2);
@ -744,6 +767,7 @@ static int irbuilder_condbranch(lua_State *L) {
static int func_compile(lua_State *L) {
MainFunctionHolder *f = check_LLVM_mainfunction(L, 1);
if (!f->func->function()) return 0;
if (!f->compiled_func) {
f->func->raviModule()->runpasses();
f->func->raviModule()->finalize();
@ -1255,11 +1279,10 @@ static const luaL_Reg llvmlib[] = {
static const luaL_Reg structtype_methods[] = {{"setbody", struct_add_members},
{NULL, NULL}};
static const luaL_Reg module_methods[] = {{"newfunction", module_newfunction},
{"compileC", module_compile_C},
{"dump", dump_content},
{"generatecode", module_generate_code},
{NULL, NULL}};
static const luaL_Reg module_methods[] = {
{"newfunction", module_newfunction}, {"getfunction", module_getfunction},
{"compileC", module_compile_C}, {"dump", dump_content},
{"generatecode", module_generate_code}, {NULL, NULL}};
static const luaL_Reg main_function_methods[] = {
{"appendblock", func_append_basicblock},

Loading…
Cancel
Save