pull/159/head
Dibyendu Majumdar 6 years ago
parent 62d5bace74
commit ebff2906c7

@ -182,7 +182,7 @@ if (LLVM_JIT)
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)
set(OMR_JIT_SRCS src/ravi_omrjit.c src/ravi_omrjitapi.c)
else()
set(NO_JIT_SRCS src/ravi_nojit.c)
endif()

@ -7,7 +7,6 @@ find_path(OMRJIT_INCLUDE_DIR nj_api.h
find_library(OMRJIT_LIBRARY
NAMES nj libnj libnj.dylib
#NAMES jitbuilder libjitbuilder libjitbuilder.dylib
PATHS
c:/Software/omr/lib
~/Software/omr/lib

@ -340,35 +340,38 @@ static int dmrC_getsymbols(lua_State *L) {
struct symbol_list *symlist;
struct string_list *filelist = NULL;
struct dmr_C *C = new_dmr_C();
const char *codebuffer = luaL_checkstring(L, 1);
char *argv[] = {NULL};
int argc = 0;
int retval = 0;
symlist = dmrC_sparse_initialize(C, argc, argv, &filelist);
struct parser_state parser_state = {
.L = L, .C = C, .idcount = 1, .stack = {0}, .stackpos = -1, .tabidx = 1};
lua_newtable(L);
int luastack = lua_gettop(L);
push_tabindex(&parser_state);
char *buffer = strdup(codebuffer);
dmrC_sparse_buffer(C, "buffer", buffer, 1);
int fd = get_stream_id(C, "buffer");
examine_symbol_list(&parser_state, fd, C->file_scope->symbols);
examine_symbol_list(&parser_state, fd, C->global_scope->symbols);
struct dmr_C *C = new_dmr_C();
if (!setjmp(C->jmpbuf)) {
symlist = dmrC_sparse_initialize(C, argc, argv, &filelist);
struct parser_state parser_state = {.L = L, .C = C, .idcount = 1, .stack = {0}, .stackpos = -1, .tabidx = 1};
lua_newtable(L);
int luastack = lua_gettop(L);
push_tabindex(&parser_state);
dmrC_sparse_buffer(C, "buffer", buffer, 1);
int fd = get_stream_id(C, "buffer");
examine_symbol_list(&parser_state, fd, C->file_scope->symbols);
examine_symbol_list(&parser_state, fd, C->global_scope->symbols);
lua_assert(luastack == lua_gettop(L));
retval = 1;
}
else {
retval = 0;
}
free(buffer);
destroy_dmr_C(C);
lua_assert(luastack == lua_gettop(L));
return 1;
return retval;
}
extern int ravi_compile_C(lua_State *L);
static const luaL_Reg dmrclib[] = {{"getsymbols", dmrC_getsymbols},
{"compileC", ravi_compile_C},
{NULL, NULL}};
LUAMOD_API int raviopen_dmrcluaapi(lua_State *L) {

@ -881,3 +881,7 @@ int raviV_gettraceenabled(lua_State *L) {
if (!G->ravi_state) return 0;
return G->ravi_state->jit->is_tracehook_enabled();
}
extern "C" int ravi_compile_C(lua_State *L) {
return 0;
}

@ -172,3 +172,7 @@ int raviV_gettraceenabled(struct lua_State *L) {
(void)L;
return 0;
}
int ravi_compile_C(lua_State *L) {
return 0;
}

@ -113,6 +113,10 @@ static void register_builtin_arg5(JIT_ContextRef module, const char *name,
JIT_RegisterFunction(module, name, return_type, 5, args, fp);
}
#if !RAVI_TARGET_X64
#error OMRJIT is currently only supported on X64 architecture
#endif
// Initialize the JIT State and attach it to the
// Global Lua State
// If a JIT State already exists then this function
@ -128,6 +132,9 @@ int raviV_initjit(struct lua_State *L) {
jit->opt_level_ = 1;
// The parameter true means we will be dumping stuff as we compile
jit->jit = JIT_CreateContext();
// FIXME Portability - following needs to handle 32-bit arch
//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);
@ -197,6 +204,68 @@ int raviV_initjit(struct lua_State *L) {
// void raviV_settable_i(lua_State *L, const TValue *t, TValue *key, StkId val);
register_builtin_arg4(jit->jit, "raviV_settable_i", raviV_settable_i, JIT_NoType, JIT_Address, JIT_Address, JIT_Address, JIT_Address);
//LUA_API int (lua_isnumber)(lua_State *L, int idx);
register_builtin_arg2(jit->jit, "lua_isnumber", lua_isnumber, JIT_Int32, JIT_Address, JIT_Int32);
//LUA_API int (lua_isstring)(lua_State *L, int idx);
register_builtin_arg2(jit->jit, "lua_isstring", lua_isstring, JIT_Int32, JIT_Address, JIT_Int32);
//LUA_API int (lua_iscfunction)(lua_State *L, int idx);
register_builtin_arg2(jit->jit, "lua_iscfunction", lua_iscfunction, JIT_Int32, JIT_Address, JIT_Int32);
//LUA_API int (lua_isinteger)(lua_State *L, int idx);
register_builtin_arg2(jit->jit, "lua_isinteger", lua_isinteger, JIT_Int32, JIT_Address, JIT_Int32);
//LUA_API int (lua_isuserdata)(lua_State *L, int idx);
register_builtin_arg2(jit->jit, "lua_isuserdata", lua_isuserdata, JIT_Int32, JIT_Address, JIT_Int32);
//LUA_API int (lua_type)(lua_State *L, int idx);
register_builtin_arg2(jit->jit, "lua_type", lua_type, JIT_Int32, JIT_Address, JIT_Int32);
//LUA_API const char *(lua_typename)(lua_State *L, int tp);
register_builtin_arg2(jit->jit, "lua_typename", (void*)lua_typename, JIT_Address, JIT_Address, JIT_Int32);
//LUA_API const char * (ravi_typename)(lua_State *L, int idx);
register_builtin_arg2(jit->jit, "ravi_typename", (void*)ravi_typename, JIT_Address, JIT_Address, JIT_Int32);
//LUA_API lua_Number(lua_tonumberx) (lua_State *L, int idx, int *isnum);
register_builtin_arg3(jit->jit, "lua_tonumberx", lua_tonumberx, JIT_Double, JIT_Address, JIT_Int32, JIT_Address);
//LUA_API lua_Integer(lua_tointegerx) (lua_State *L, int idx, int *isnum);
register_builtin_arg3(jit->jit, "lua_tointegerx", lua_tointegerx, JIT_Int64, JIT_Address, JIT_Int32, JIT_Address);
//LUA_API int (lua_toboolean)(lua_State *L, int idx);
register_builtin_arg2(jit->jit, "lua_toboolean", lua_toboolean, JIT_Int32, JIT_Address, JIT_Int32);
//LUA_API const char *(lua_tolstring)(lua_State *L, int idx, size_t *len);
register_builtin_arg3(jit->jit, "lua_tolstring", (void*)lua_tolstring, JIT_Address, JIT_Address, JIT_Int32, JIT_Address);
//LUA_API size_t(lua_rawlen) (lua_State *L, int idx);
register_builtin_arg2(jit->jit, "lua_rawlen", lua_rawlen, JIT_Int64, JIT_Address, JIT_Int32);
//LUA_API lua_CFunction(lua_tocfunction) (lua_State *L, int idx);
register_builtin_arg2(jit->jit, "lua_tocfunction", lua_tocfunction, JIT_Address, JIT_Address, JIT_Int32);
//LUA_API void *(lua_touserdata)(lua_State *L, int idx);
register_builtin_arg2(jit->jit, "lua_touserdata", lua_touserdata, JIT_Address, JIT_Address, JIT_Int32);
//LUA_API lua_State *(lua_tothread)(lua_State *L, int idx);
register_builtin_arg2(jit->jit, "lua_tothread", lua_tothread, JIT_Address, JIT_Address, JIT_Int32);
//LUA_API const void *(lua_topointer)(lua_State *L, int idx);
register_builtin_arg2(jit->jit, "lua_topointer", (void *)lua_topointer, JIT_Address, JIT_Address, JIT_Int32);
//LUA_API void (lua_arith)(lua_State *L, int op);
register_builtin_arg2(jit->jit, "lua_arith", lua_arith, JIT_NoType, JIT_Address, JIT_Int32);
//LUA_API int (lua_rawequal)(lua_State *L, int idx1, int idx2);
register_builtin_arg3(jit->jit, "lua_rawequal", lua_rawequal, JIT_Int32, JIT_Address, JIT_Int32, JIT_Int32);
//LUA_API int (lua_compare)(lua_State *L, int idx1, int idx2, int op);
register_builtin_arg4(jit->jit, "lua_compare", lua_compare, JIT_Int32, JIT_Address, JIT_Int32, JIT_Int32, JIT_Int32);
//LUA_API void (lua_pushnil)(lua_State *L);
register_builtin_arg1(jit->jit, "lua_pushnil", lua_pushnil, JIT_NoType, JIT_Address);
//LUA_API void (lua_pushnumber)(lua_State *L, lua_Number n);
register_builtin_arg2(jit->jit, "lua_pushnumber", lua_pushnumber, JIT_NoType, JIT_Address, JIT_Double);
//LUA_API void (lua_pushinteger)(lua_State *L, lua_Integer n);
register_builtin_arg2(jit->jit, "lua_pushinteger", lua_pushinteger, JIT_NoType, JIT_Address, JIT_Int64);
//LUA_API const char *(lua_pushlstring)(lua_State *L, const char *s, size_t len);
register_builtin_arg3(jit->jit, "lua_pushlstring", (void*)lua_pushlstring, JIT_Address, JIT_Address, JIT_Address, JIT_Int64);
//LUA_API const char *(lua_pushstring)(lua_State *L, const char *s);
register_builtin_arg2(jit->jit, "lua_pushstring", (void*)lua_pushstring, JIT_Address, JIT_Address, JIT_Address);
//LUA_API void (lua_pushcclosure)(lua_State *L, lua_CFunction fn, int n);
register_builtin_arg3(jit->jit, "lua_pushcclosure", lua_pushcclosure, JIT_NoType, JIT_Address, JIT_Address, JIT_Int32);
// LUA_API void (lua_pushboolean)(lua_State *L, int b);
register_builtin_arg2(jit->jit, "lua_pushboolean", lua_pushboolean, JIT_NoType, JIT_Address, JIT_Int32);
//LUA_API void (lua_pushlightuserdata)(lua_State *L, void *p);
register_builtin_arg2(jit->jit, "lua_pushlightuserdata", lua_pushlightuserdata, JIT_NoType, JIT_Address, JIT_Address);
//LUA_API int (lua_pushthread)(lua_State *L);
register_builtin_arg1(jit->jit, "lua_pushthread", lua_pushthread, JIT_Int32, JIT_Address);
G->ravi_state = jit;
return 0;
}

@ -0,0 +1,165 @@
#include <ravi_omrjit.h>
#include <ravijit.h>
#include <stddef.h>
#include <assert.h>
#define LUA_CORE
#include "lauxlib.h"
#include "lobject.h"
#include "lstate.h"
#include "lua.h"
static const char Lua_header[] = ""
"typedef __SIZE_TYPE__ size_t;\n"
"typedef long long ptrdiff_t;\n"
"typedef long long intptr_t;\n"
"typedef long long int64_t;\n"
"typedef unsigned long long uint64_t;\n"
"typedef int int32_t;\n"
"typedef unsigned int uint32_t;\n"
"typedef short int16_t;\n"
"typedef unsigned short uint16_t;\n"
"typedef char int8_t;\n"
"typedef unsigned char uint8_t;\n"
"typedef size_t lu_mem;\n"
"typedef ptrdiff_t l_mem;\n"
"typedef unsigned char lu_byte;\n"
"typedef uint16_t LuaType;\n"
"#define NULL ((void *)0)\n"
"typedef struct lua_State lua_State;\n"
"#define LUA_TNONE (-1)\n"
"#define LUA_TNIL 0\n"
"#define LUA_TBOOLEAN 1\n"
"#define LUA_TLIGHTUSERDATA 2\n"
"#define LUA_TNUMBER 3\n"
"#define LUA_TSTRING 4\n"
"#define LUA_TTABLE 5\n"
"#define LUA_TFUNCTION 6\n"
"#define LUA_TUSERDATA 7\n"
"#define LUA_TTHREAD 8\n"
"typedef double lua_Number;\n"
"typedef int64_t lua_Integer;\n"
"typedef uint64_t lua_Unsigned;\n"
"typedef int (*lua_CFunction) (lua_State *L);\n"
"extern int (lua_isnumber)(lua_State *L, int idx);\n"
"extern int (lua_isstring)(lua_State *L, int idx);\n"
"extern int (lua_iscfunction)(lua_State *L, int idx);\n"
"extern int (lua_isinteger)(lua_State *L, int idx);\n"
"extern int (lua_isuserdata)(lua_State *L, int idx);\n"
"extern int (lua_type)(lua_State *L, int idx);\n"
"extern const char *(lua_typename)(lua_State *L, int tp);\n"
"extern const char * (ravi_typename)(lua_State *L, int idx);\n"
"extern lua_Number(lua_tonumberx) (lua_State *L, int idx, int *isnum);\n"
"extern lua_Integer(lua_tointegerx) (lua_State *L, int idx, int *isnum);\n"
"extern int (lua_toboolean)(lua_State *L, int idx);\n"
"extern const char *(lua_tolstring)(lua_State *L, int idx, size_t *len);\n"
"extern size_t(lua_rawlen) (lua_State *L, int idx);\n"
"extern lua_CFunction(lua_tocfunction) (lua_State *L, int idx);\n"
"extern void *(lua_touserdata)(lua_State *L, int idx);\n"
"extern lua_State *(lua_tothread)(lua_State *L, int idx);\n"
"extern const void *(lua_topointer)(lua_State *L, int idx);\n"
"#define LUA_OPADD 0\n"
"#define LUA_OPSUB 1\n"
"#define LUA_OPMUL 2\n"
"#define LUA_OPMOD 3\n"
"#define LUA_OPPOW 4\n"
"#define LUA_OPDIV 5\n"
"#define LUA_OPIDIV 6\n"
"#define LUA_OPBAND 7\n"
"#define LUA_OPBOR 8\n"
"#define LUA_OPBXOR 9\n"
"#define LUA_OPSHL 10\n"
"#define LUA_OPSHR 11\n"
"#define LUA_OPUNM 12\n"
"#define LUA_OPBNOT 13\n"
"extern void (lua_arith)(lua_State *L, int op);\n"
"#define LUA_OPEQ 0\n"
"#define LUA_OPLT 1\n"
"#define LUA_OPLE 2\n"
"extern int (lua_rawequal)(lua_State *L, int idx1, int idx2);\n"
"extern int (lua_compare)(lua_State *L, int idx1, int idx2, int op);\n"
"extern void (lua_pushnil)(lua_State *L);\n"
"extern void (lua_pushnumber)(lua_State *L, lua_Number n);\n"
"extern void (lua_pushinteger)(lua_State *L, lua_Integer n);\n"
"extern const char *(lua_pushlstring)(lua_State *L, const char *s, size_t len);\n"
"extern const char *(lua_pushstring)(lua_State *L, const char *s);\n"
"extern void (lua_pushcclosure)(lua_State *L, lua_CFunction fn, int n);\n"
"extern void (lua_pushboolean)(lua_State *L, int b);\n"
"extern void (lua_pushlightuserdata)(lua_State *L, void *p);\n"
"extern int (lua_pushthread)(lua_State *L);\n"
;
enum {
MAX_ARGS = 50,
MAX_BUFFER = 4096
};
static int collect_args(lua_State *L, int tabindex, char *argv[], int maxargs,
char *buf, int buflen) {
char errormessage[128];
int len = lua_rawlen(L, tabindex);
if (len > maxargs) {
snprintf(errormessage, sizeof errormessage,
"Arguments exceed total count %d elements", maxargs);
luaL_argerror(L, 2, errormessage);
len = maxargs;
}
char *p = buf;
char *endp = buf + buflen;
int n = 0;
for (int i = 0; i < len; i++) {
lua_rawgeti(L, tabindex, i + 1);
size_t size = 0;
const char *argument = luaL_checklstring(L, -1, &size);
if (argument && size > 0) {
if (p + size + 1 >= endp) {
snprintf(errormessage, sizeof errormessage,
"Arguments exceed combined size of %d bytes", buflen);
luaL_argerror(L, 2, errormessage);
break;
}
strncpy(p, argument, size + 1);
argv[n] = p;
p += (size + 1);
n++;
}
}
assert(p <= endp);
return n;
}
int ravi_compile_C(lua_State *L) {
global_State *G = G(L);
if (G->ravi_state == NULL) return 0;
JIT_ContextRef context = G->ravi_state->jit;
if (context == NULL) return 0;
const char *codebuffer = NULL;
char *argv[MAX_ARGS + 1] = {NULL};
int argc = 0;
char buf[MAX_BUFFER + 1] = {0};
int guard = 0xfefefefe;
int codearg = 1;
if (lua_istable(L, 1)) {
argc = collect_args(L, 1, argv, MAX_ARGS, buf, sizeof buf);
assert(argc >= 0 && argc <= MAX_ARGS);
assert(argv[MAX_ARGS] == NULL);
assert(guard == 0xfefefefe);
codearg++;
}
codebuffer = luaL_checkstring(L, codearg);
membuff_t mbuf;
membuff_init(&mbuf, strlen(Lua_header) + 4096);
membuff_add_string(&mbuf, Lua_header);
membuff_add_string(&mbuf, codebuffer);
if (!dmrC_omrcompile(argc, argv, context, mbuf.buf)) {
lua_pushboolean(L, false);
}
else {
lua_pushboolean(L, true);
}
membuff_free(&mbuf);
return 1;
}
Loading…
Cancel
Save