issue #128 add initial skeleton files

gccjit-ravi534
Dibyendu Majumdar 7 years ago
parent f07efc720d
commit 95779e058c

@ -12,6 +12,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
# Only one can be ON, and at last one of them must be ON
option(LLVM_JIT "Controls whether LLVM JIT compilation will be enabled, default is OFF" OFF)
option(GCC_JIT "Controls whether GCC JIT compilation will be enabled, default is OFF" OFF)
option(NANO_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(EMBEDDED_DMRC "Controls whether the embedded dmrC feature should be enabled, default is OFF" OFF)
@ -19,6 +20,12 @@ option(EMBEDDED_DMRC "Controls whether the embedded dmrC feature should be enabl
if (LLVM_JIT AND GCC_JIT)
message(FATAL_ERROR
"Both LLVM_JIT and GCC_JIT cannot be set to ON at the same time")
elseif (LLVM_JIT AND NANO_JIT)
message(FATAL_ERROR
"Both LLVM_JIT and NANO_JIT cannot be set to ON at the same time")
elseif (GCC_JIT AND NANO_JIT)
message(FATAL_ERROR
"Both GCC_JIT and NANO_JIT cannot be set to ON at the same time")
endif()
if (STATIC_BUILD)
@ -39,6 +46,10 @@ if (LLVM_JIT)
add_definitions(-DUSE_LLVM)
endif()
if (NANO_JIT)
set(EMBEDDED_DMRC ON)
endif()
if (EMBEDDED_DMRC)
add_definitions(-DUSE_DMR_C)
endif()
@ -52,8 +63,14 @@ if (GCC_JIT)
add_definitions(-DUSE_GCCJIT)
endif()
if (NOT LLVM_JIT AND NOT GCC_JIT)
message(WARNING "Neither LLVM nor gccjit will be enabled; specify -DLLVM_JIT or -DGCC_JIT to enable")
if (NANO_JIT)
find_package(NanoJIT REQUIRED)
include_directories(${NANOJITEXTRA_INCLUDE_DIRS})
add_definitions(-DUSE_NANOJIT)
endif()
if (NOT LLVM_JIT AND NOT GCC_JIT AND NOT NANO_JIT)
message(WARNING "Neither LLVM, NanoJIT nor gccjit will be enabled; specify -DLLVM_JIT, -DNANO_JIT or -DGCC_JIT to enable")
endif()
if (MSVC)
@ -138,7 +155,10 @@ if (GCC_JIT)
src/ravi_gccarith1.c src/ravi_gcccall.c src/ravi_gcctable.c
src/ravi_gccarith2.c src/ravi_gcctforcall.c src/ravi_gccrest.c)
endif ()
if (NOT LLVM_JIT AND NOT GCC_JIT)
if (NANO_JIT)
set(NANO_JIT_SRCS src/ravi_nanojit.c)
endif()
if (NOT LLVM_JIT AND NOT GCC_JIT AND NOT NANO_JIT)
set(NO_JIT_SRCS src/ravi_nojit.c)
endif()
# define the lua core source files
@ -241,6 +261,15 @@ if (EMBEDDED_DMRC)
include_directories("${PROJECT_SOURCE_DIR}/dmr_c/llvm-backend")
endif()
if (NANO_JIT)
set(DMR_C_JIT_SRCS
dmr_c/nanojit-backend/sparse-nanojit.c
dmr_c/nanojit-backend/dmr_c.h
)
include_directories("${PROJECT_SOURCE_DIR}/dmr_c/nanojit-backend")
endif()
if (MSVC OR APPLE)
source_group("dmrC Headers" FILES ${DMR_C_HEADERS})
@ -309,6 +338,8 @@ if (LLVM_JIT)
set (LIBRAVI_NAME libravillvm)
elseif (GCC_JIT)
set (LIBRAVI_NAME libravigccjit)
elseif (NANO_JIT)
set (LIBRAVI_NAME libravinanojit)
else()
set (LIBRAVI_NAME libravinojit)
endif()
@ -320,6 +351,7 @@ add_library(${LIBRAVI_NAME} ${LIBRAVI_BUILD_TYPE}
${LUA_CORE_SRCS}
${LLVM_JIT_SRCS}
${GCC_JIT_SRCS}
${NANO_JIT_SRCS}
${NO_JIT_SRCS}
${DMR_C_HEADERS}
${DMR_C_SRCS}
@ -333,7 +365,7 @@ if (NOT STATIC_BUILD)
set_target_properties(${LIBRAVI_NAME} PROPERTIES PREFIX "")
endif ()
endif()
target_link_libraries(${LIBRAVI_NAME} ${EXTRA_LIBRARIES} ${LLVM_LIBS} ${GCCJIT_LIBRARIES})
target_link_libraries(${LIBRAVI_NAME} ${EXTRA_LIBRARIES} ${LLVM_LIBS} ${GCCJIT_LIBRARIES} ${NANOJITEXTRA_LIBRARIES})
# Ravi executable
add_executable(ravi src/lua.c)
@ -358,7 +390,7 @@ add_test(TestVM test_vm)
add_test(TestMisc test_misc)
# Build VSCode Debug Adapter for Ravi
if (STATIC_BUILD AND NOT GCC_JIT)
if (STATIC_BUILD AND NOT GCC_JIT AND NOT NANO_JIT)
add_executable(testravidebug vscode-debugger/src/testravidebug.c vscode-debugger/src/json.c vscode-debugger/src/protocol.c)
target_link_libraries(testravidebug ${LIBRAVI_NAME})

@ -0,0 +1,16 @@
find_path(NANOJITEXTRA_INCLUDE_DIR nanojitextra.h
PATHS
c:/ravi/include/nanojit
~/ravi/include/nanojit
NO_DEFAULT_PATH
)
find_library(NANOJITEXTRA_LIBRARY
NAMES nanojitextra libnanojitextra libnanojitextra.dylib
PATHS
c:/ravi/lib
~/ravi/lib
)
set( NANOJITEXTRA_INCLUDE_DIRS "${NANOJITEXTRA_INCLUDE_DIR}" )
set( NANOJITEXTRA_LIBRARIES "${NANOJITEXTRA_LIBRARY}" )

@ -0,0 +1,90 @@
/******************************************************************************
* 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.
******************************************************************************/
#ifndef RAVI_NANOJIT_H
#define RAVI_NANOJIT_H
#ifdef USE_NANOJIT
#include "dmr_c.h"
#ifdef __cplusplus
extern "C" {
#endif
// TODO we probably do not need all the headers
// below
#define LUA_CORE
#include "lprefix.h"
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stddef.h>
#include "lua.h"
#include "ldebug.h"
#include "ldo.h"
#include "lfunc.h"
#include "lobject.h"
#include "lopcodes.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "lvm.h"
typedef enum {
LUA__TNIL = LUA_TNIL,
LUA__TBOOLEAN = LUA_TBOOLEAN,
LUA__TLIGHTUSERDATA = LUA_TLIGHTUSERDATA,
LUA__TNUMBER = LUA_TNUMBER,
LUA__TSTRING = ctb(LUA_TSTRING),
LUA__TTABLE = ctb(LUA_TTABLE),
RAVI__TLTABLE = ctb(LUA_TTABLE),
RAVI__TIARRAY = ctb(RAVI_TIARRAY),
RAVI__TFARRAY = ctb(RAVI_TFARRAY),
LUA__TFUNCTION = ctb(LUA_TFUNCTION),
LUA__TUSERDATA = ctb(LUA_TUSERDATA),
LUA__TTHREAD = ctb(LUA_TTHREAD),
LUA__TLCL = ctb(LUA_TLCL),
LUA__TLCF = LUA_TLCF,
LUA__TCCL = ctb(LUA_TCCL),
LUA__TSHRSTR = ctb(LUA_TSHRSTR),
LUA__TLNGSTR = ctb(LUA_TLNGSTR),
LUA__TNUMFLT = LUA_TNUMFLT,
LUA__TNUMINT = LUA_TNUMINT
} lua_typecode_t;
#ifdef __cplusplus
};
#endif
#endif /* USE_GCCJIT */
#endif /* RAVI_RAVI_GCCJIT_H */

@ -44,6 +44,8 @@
#define RAVI_OPTION_STRING3 " LLVM"
#elif USE_GCCJIT
#define RAVI_OPTION_STRING3 " gccjit"
#elif USE_NANOJIT
#define RAVI_OPTION_STRING3 " nanojit"
#else
#define RAVI_OPTION_STRING3 " nojit"
#endif

@ -0,0 +1,291 @@
/******************************************************************************
* Copyright (C) 2015 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 <stddef.h>
#include <ravijit.h>
#include "ravi_nanojit.h"
#if 0
ravi_gcc_context_t *ravi_jit_new_context(void) {
ravi_gcc_context_t *ravi = NULL;
gcc_jit_context *gcc_ctx = gcc_jit_context_acquire();
if (!gcc_ctx) {
fprintf(stderr, "failed to allocate a GCC JIT context\n");
goto on_error;
}
ravi = (ravi_gcc_context_t *)calloc(1, sizeof(ravi_gcc_context_t));
if (!ravi) {
fprintf(stderr, "failed to allocate a Ravi JIT context\n");
goto on_error;
}
ravi->context = gcc_ctx;
ravi->auto_ = false;
ravi->enabled_ = true;
ravi->min_code_size_ = 150;
ravi->min_exec_count_ = 50;
ravi->opt_level_ = 3;
ravi->size_level_ = 0;
if (!ravi_setup_lua_types(ravi)) {
fprintf(stderr, "failed to setup types\n");
goto on_error;
}
ravi->parent_result_ = gcc_jit_context_compile(ravi->context);
if (gcc_jit_context_get_first_error(ravi->context)) {
fprintf(stderr, "aborting due to JIT error: %s\n",
gcc_jit_context_get_first_error(ravi->context));
abort();
}
return ravi;
on_error:
if (ravi) {
ravi_jit_context_free(ravi);
} else if (gcc_ctx) {
gcc_jit_context_release(gcc_ctx);
}
return NULL;
}
void ravi_jit_context_free(ravi_gcc_context_t *ravi) {
if (ravi == NULL)
return;
if (ravi->parent_result_) {
gcc_jit_result_release(ravi->parent_result_);
ravi->parent_result_ = NULL;
}
if (ravi->context) {
gcc_jit_context_release(ravi->context);
ravi->context = NULL;
}
if (ravi->types) {
free(ravi->types);
ravi->types = NULL;
}
free(ravi);
}
ravi_gcc_codegen_t *ravi_jit_new_codegen(ravi_gcc_context_t *ravi) {
ravi_gcc_codegen_t *cg = NULL;
cg = (ravi_gcc_codegen_t *)calloc(1, sizeof(ravi_gcc_codegen_t));
if (cg == NULL) {
fprintf(stderr, "error creating a new context: out of memory\n");
goto on_error;
}
cg->id = 1;
cg->temp[0] = 0;
cg->ravi = ravi;
return cg;
on_error:
if (cg)
ravi_jit_codegen_free(cg);
return NULL;
}
void ravi_jit_codegen_free(ravi_gcc_codegen_t *codegen) {
if (codegen == NULL)
return;
free(codegen);
}
bool ravi_jit_has_errored(ravi_gcc_context_t *ravi) {
const char *msg = gcc_jit_context_get_first_error(ravi->context);
if (msg) {
fprintf(stderr, "JIT error: %s\n", msg);
return true;
}
return false;
}
#endif
// TODO we probably do not need all the headers
// below
#define LUA_CORE
#include "lua.h"
#include "lobject.h"
#include "lstate.h"
#include "lauxlib.h"
// 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));
//jit->jit = ravi_jit_new_context();
//jit->code_generator = ravi_jit_new_codegen(jit->jit);
//G->ravi_state = jit;
return -1;
}
// Free up the JIT State
void raviV_close(struct lua_State *L) {
global_State *G = G(L);
if (G->ravi_state == NULL)
return;
//ravi_jit_codegen_free(G->ravi_state->code_generator);
//ravi_jit_context_free(G->ravi_state->jit);
//free(G->ravi_state);
}
// Dump the LLVM IR
void raviV_dumpIR(struct lua_State *L, struct Proto *p) {
(void)L;
(void)p;
}
// 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);
//if (!G->ravi_state)
return 0;
//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_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) {
return false;
}
// 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