issue #100 change the way GETTABLE_SK, SETTABLE_SK, GETTABUP_SK and SETTABUP_SK are compiled

gccjit-ravi534
Dibyendu Majumdar 8 years ago
parent 7665f6b7c4
commit a2709d8403

@ -269,6 +269,8 @@ struct LuaLLVMTypes {
llvm::FunctionType *raviV_op_setupvalaiT;
llvm::FunctionType *raviV_op_setupvalafT;
llvm::FunctionType *raviV_op_setupvaltT;
llvm::FunctionType *raviV_gettable_sskeyT;
llvm::FunctionType *raviV_settable_sskeyT;
llvm::FunctionType *raviH_set_intT;
llvm::FunctionType *raviH_set_floatT;
@ -638,6 +640,8 @@ struct RaviFunctionDef {
llvm::Function *raviV_op_setupvalaiF;
llvm::Function *raviV_op_setupvalafF;
llvm::Function *raviV_op_setupvaltF;
llvm::Function *raviV_gettable_sskeyF;
llvm::Function *raviV_settable_sskeyF;
// array setters
llvm::Function *raviH_set_intF;
@ -1061,13 +1065,14 @@ class RaviCodeGenerator {
void emit_SETTABLE_I(RaviFunctionDef *def, int A, int B, int C, int pc);
void emit_SETTABLE_SK(RaviFunctionDef *def, int A, int B, int C, int pc);
void emit_GETTABLE(RaviFunctionDef *def, int A, int B, int C, int pc);
void emit_GETTABLE_S(RaviFunctionDef *def, int A, int B, int C, int pc,
TString *key);
void emit_GETTABLE_SK(RaviFunctionDef *def, int A, int B, int C, int pc,
TString *key);
void emit_GETTABLE_SK(RaviFunctionDef *def, int A, int B, int C, int pc);
void emit_GETTABLE_I(RaviFunctionDef *def, int A, int B, int C, int pc);
@ -1095,11 +1100,12 @@ class RaviCodeGenerator {
void emit_GETTABUP(RaviFunctionDef *def, int A, int B, int C, int pc);
void emit_GETTABUP_SK(RaviFunctionDef *def, int A, int B, int C, int pc,
TString *key);
void emit_GETTABUP_SK(RaviFunctionDef *def, int A, int B, int C, int pc);
void emit_SETTABUP(RaviFunctionDef *def, int A, int B, int C, int pc);
void emit_SETTABUP_SK(RaviFunctionDef *def, int A, int B, int C, int pc);
void emit_NEWARRAYINT(RaviFunctionDef *def, int A, int pc);
void emit_NEWARRAYFLOAT(RaviFunctionDef *def, int A, int pc);

@ -20,8 +20,8 @@
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
#include <ravijit.h>
#include "ravi_llvmcodegen.h"
#include <ravijit.h>
namespace ravi {
@ -940,10 +940,9 @@ bool RaviCodeGenerator::canCompile(Proto *p) {
case OP_RAVI_SETUPVALAI:
case OP_RAVI_SETUPVALAF:
case OP_RAVI_SETUPVALT:
case OP_RAVI_SELF_S:
case OP_RAVI_SELF_S:
case OP_RAVI_SELF_SK:
case OP_RAVI_GETTABUP_SK:
break;
case OP_RAVI_GETTABUP_SK: break;
default: return false;
}
}
@ -1166,6 +1165,12 @@ void RaviCodeGenerator::emit_extern_declarations(RaviFunctionDef *def) {
def->raviV_op_setupvaltF = def->raviF->addExternFunction(
def->types->raviV_op_setupvaltT,
reinterpret_cast<void *>(&raviV_op_setupvalt), "raviV_op_setupvalt");
def->raviV_settable_sskeyF = def->raviF->addExternFunction(
def->types->raviV_settable_sskeyT,
reinterpret_cast<void *>(&raviV_settable_sskey), "raviV_settable_sskey");
def->raviV_gettable_sskeyF = def->raviF->addExternFunction(
def->types->raviV_gettable_sskeyT,
reinterpret_cast<void *>(&raviV_gettable_sskey), "raviV_gettable_sskey");
def->ravi_dump_valueF = def->raviF->addExternFunction(
def->types->ravi_dump_valueT, reinterpret_cast<void *>(&ravi_dump_value),
@ -1214,7 +1219,7 @@ void RaviCodeGenerator::emit_extern_declarations(RaviFunctionDef *def) {
llvm::FunctionType::get(def->types->lua_NumberT, args, false);
#ifdef LUA_32BITS
def->floorFunc =
def->raviF->module()->getOrInsertFunction("floorf", floorType);
def->raviF->module()->getOrInsertFunction("floorf", floorType);
#else
def->floorFunc =
def->raviF->module()->getOrInsertFunction("floor", floorType);
@ -1317,15 +1322,14 @@ llvm::Value *RaviCodeGenerator::emit_gep_upval_value(
bool RaviCodeGenerator::compile(lua_State *L, Proto *p,
std::shared_ptr<RaviJITModule> module,
ravi_compile_options_t *options) {
if (p->ravi_jit.jit_status == RAVI_JIT_COMPILED)
return true;
if (p->ravi_jit.jit_status == RAVI_JIT_COMPILED) return true;
bool doVerify = options ? options->verification_level != 0 : 0;
bool omitArrayGetRangeCheck =
options ? options->omit_array_get_range_check != 0 : 0;
if (p->ravi_jit.jit_status != RAVI_JIT_NOT_COMPILED || !canCompile(p)) {
//fprintf(stderr, "failed to compile!\n");
// fprintf(stderr, "failed to compile!\n");
return false;
}
@ -1634,13 +1638,18 @@ bool RaviCodeGenerator::compile(lua_State *L, Proto *p,
emit_CALL(def, A, B, C, pc);
} break;
case OP_RAVI_SETTABLE_SK:
case OP_RAVI_SETTABLE_S: {
int B = GETARG_B(i);
int C = GETARG_C(i);
emit_SETTABLE_SK(def, A, B, C, pc);
} break;
case OP_RAVI_SETTABLE_I: /*{
int B = GETARG_B(i);
int C = GETARG_C(i);
emit_SETTABLE_I(def, A, B, C, pc);
} break; */
case OP_RAVI_SETTABLE_SK:
case OP_RAVI_SETTABLE_S:
case OP_SETTABLE: {
int B = GETARG_B(i);
int C = GETARG_C(i);
@ -1656,49 +1665,38 @@ bool RaviCodeGenerator::compile(lua_State *L, Proto *p,
emit_GETTABLE_S(def, A, B, C, pc, key);
} break;
#if 0
// There is a BUG in the code being emitted
// See the emit method for details
case OP_RAVI_GETTABLE_SK: {
case OP_RAVI_SELF_S: {
int C = GETARG_C(i);
int B = GETARG_B(i);
lua_assert(ISK(C));
TValue *kv = k + INDEXK(C);
TString *key = tsvalue(kv);
lua_assert(key->tt == LUA_TSHRSTR);
emit_GETTABLE_SK(def, A, B, C, pc, key);
emit_SELF_S(def, A, B, C, pc, key);
} break;
#endif
#if 0
// There is a BUG in the code being emitted
// Also crashes the tests so code generation is incorrect
case OP_RAVI_GETTABUP_SK: {
int C = GETARG_C(i);
case OP_RAVI_GETTABLE_I: {
int B = GETARG_B(i);
lua_assert(ISK(C));
TValue *kv = k + INDEXK(C);
TString *key = tsvalue(kv);
lua_assert(key->tt == LUA_TSHRSTR);
emit_GETTABUP_SK(def, A, B, C, pc, key);
int C = GETARG_C(i);
emit_GETTABLE_I(def, A, B, C, pc);
} break;
#endif
case OP_RAVI_SELF_S: {
#if 0
// There is a BUG in the code being emitted
// See the emit method for details
case OP_RAVI_GETTABLE_SK: {
int C = GETARG_C(i);
int B = GETARG_B(i);
lua_assert(ISK(C));
TValue *kv = k + INDEXK(C);
TString *key = tsvalue(kv);
lua_assert(key->tt == LUA_TSHRSTR);
emit_SELF_S(def, A, B, C, pc, key);
emit_GETTABLE_SK(def, A, B, C, pc, key);
} break;
case OP_RAVI_GETTABLE_I: {
#endif
case OP_RAVI_GETTABLE_SK: {
int B = GETARG_B(i);
int C = GETARG_C(i);
emit_GETTABLE_I(def, A, B, C, pc);
emit_GETTABLE_SK(def, A, B, C, pc);
} break;
#if 1
case OP_RAVI_GETTABLE_SK:
#endif
case OP_GETTABLE: {
int B = GETARG_B(i);
int C = GETARG_C(i);
@ -1748,9 +1746,24 @@ bool RaviCodeGenerator::compile(lua_State *L, Proto *p,
emit_MOVETAB(def, A, B, pc);
} break;
#if 1
case OP_RAVI_GETTABUP_SK:
#if 0
// There is a BUG in the code being emitted
// Also crashes the tests so code generation is incorrect
case OP_RAVI_GETTABUP_SK: {
int C = GETARG_C(i);
int B = GETARG_B(i);
lua_assert(ISK(C));
TValue *kv = k + INDEXK(C);
TString *key = tsvalue(kv);
lua_assert(key->tt == LUA_TSHRSTR);
emit_GETTABUP_SK(def, A, B, C, pc, key);
} break;
#endif
case OP_RAVI_GETTABUP_SK: {
int B = GETARG_B(i);
int C = GETARG_C(i);
emit_GETTABUP_SK(def, A, B, C, pc);
} break;
case OP_GETTABUP: {
int B = GETARG_B(i);
int C = GETARG_C(i);

@ -103,6 +103,21 @@ void RaviCodeGenerator::emit_SETTABLE(RaviFunctionDef *def, int A, int B, int C,
CreateCall4(def->builder, def->luaV_settableF, def->L, ra, rb, rc);
}
// R(A)[RK(B)] := RK(C)
void RaviCodeGenerator::emit_SETTABLE_SK(RaviFunctionDef *def, int A, int B, int C,
int pc) {
// Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
bool traced = emit_debug_trace(def, OP_RAVI_SETTABLE_SK, pc);
// Below may invoke metamethod so we set savedpc
if (!traced) emit_update_savedpc(def, pc);
emit_load_base(def);
llvm::Value *ra = emit_gep_register(def, A);
llvm::Value *rb = emit_gep_register_or_constant(def, B);
llvm::Value *rc = emit_gep_register_or_constant(def, C);
CreateCall4(def->builder, def->raviV_settable_sskeyF, def->L, ra, rb, rc);
}
// R(A) := R(B)[RK(C)]
void RaviCodeGenerator::emit_GETTABLE(RaviFunctionDef *def, int A, int B, int C,
int pc) {
@ -117,6 +132,21 @@ void RaviCodeGenerator::emit_GETTABLE(RaviFunctionDef *def, int A, int B, int C,
CreateCall4(def->builder, def->luaV_gettableF, def->L, rb, rc, ra);
}
// R(A) := R(B)[RK(C)]
void RaviCodeGenerator::emit_GETTABLE_SK(RaviFunctionDef *def, int A, int B, int C,
int pc) {
// Protect(luaV_gettable(L, RB(i), RKC(i), ra));
bool traced = emit_debug_trace(def, OP_RAVI_GETTABLE_SK, pc);
// Below may invoke metamethod so we set savedpc
if (!traced) emit_update_savedpc(def, pc);
emit_load_base(def);
llvm::Value *ra = emit_gep_register(def, A);
llvm::Value *rb = emit_gep_register(def, B);
llvm::Value *rc = emit_gep_register_or_constant(def, C);
CreateCall4(def->builder, def->raviV_gettable_sskeyF, def->L, rb, rc, ra);
}
// R(A) := R(B)[RK(C)]
// This is a more optimized version that attempts to do an inline
// array get first and only if that fails it falls back on calling
@ -372,6 +402,7 @@ void RaviCodeGenerator::emit_GETTABLE_S(RaviFunctionDef *def, int A, int B,
emit_common_GETTABLE_S(def, A, B, C, key);
}
#if 0
void RaviCodeGenerator::emit_GETTABLE_SK(RaviFunctionDef *def, int A, int B,
int C, int pc, TString *key) {
emit_debug_trace(def, OP_RAVI_GETTABLE_SK, pc);
@ -415,6 +446,7 @@ void RaviCodeGenerator::emit_GETTABLE_SK(RaviFunctionDef *def, int A, int B,
def->f->getBasicBlockList().push_back(done);
def->builder->SetInsertPoint(done);
}
#endif
void RaviCodeGenerator::emit_GETTABLE_AF(RaviFunctionDef *def, int A, int B,
int C, bool omitArrayGetRangeCheck,
@ -750,6 +782,26 @@ void RaviCodeGenerator::emit_GETTABUP(RaviFunctionDef *def, int A, int B, int C,
CreateCall4(def->builder, def->luaV_gettableF, def->L, v, rc, ra);
}
// R(A) := UpValue[B][RK(C)]
void RaviCodeGenerator::emit_GETTABUP_SK(RaviFunctionDef *def, int A, int B, int C,
int pc) {
// int b = GETARG_B(i);
// Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra));
bool traced = emit_debug_trace(def, OP_RAVI_GETTABUP_SK, pc);
// Below may invoke metamethod so we set savedpc
if (!traced) emit_update_savedpc(def, pc);
emit_load_base(def);
llvm::Value *ra = emit_gep_register(def, A);
llvm::Value *rc = emit_gep_register_or_constant(def, C);
llvm::Value *upval_ptr = emit_gep_upvals(def, B);
llvm::Instruction *upval = emit_load_pupval(def, upval_ptr);
llvm::Value *v = emit_load_upval_v(def, upval);
CreateCall4(def->builder, def->raviV_gettable_sskeyF, def->L, v, rc, ra);
}
#if 0
// R(A) := UpValue[B][RK(C)]
void RaviCodeGenerator::emit_GETTABUP_SK(RaviFunctionDef *def, int A, int B,
int C, int pc, TString *key) {
@ -803,6 +855,7 @@ void RaviCodeGenerator::emit_GETTABUP_SK(RaviFunctionDef *def, int A, int B,
def->f->getBasicBlockList().push_back(done);
def->builder->SetInsertPoint(done);
}
#endif
// UpValue[A][RK(B)] := RK(C)
void RaviCodeGenerator::emit_SETTABUP(RaviFunctionDef *def, int A, int B, int C,
@ -822,6 +875,24 @@ void RaviCodeGenerator::emit_SETTABUP(RaviFunctionDef *def, int A, int B, int C,
CreateCall4(def->builder, def->luaV_settableF, def->L, v, rb, rc);
}
// UpValue[A][RK(B)] := RK(C)
void RaviCodeGenerator::emit_SETTABUP_SK(RaviFunctionDef *def, int A, int B, int C,
int pc) {
// int a = GETARG_A(i);
// Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i)));
bool traced = emit_debug_trace(def, OP_RAVI_SETTABLE_SK, pc);
if (!traced) emit_update_savedpc(def, pc);
emit_load_base(def);
llvm::Value *rb = emit_gep_register_or_constant(def, B);
llvm::Value *rc = emit_gep_register_or_constant(def, C);
llvm::Value *upval_ptr = emit_gep_upvals(def, A);
llvm::Instruction *upval = emit_load_pupval(def, upval_ptr);
llvm::Value *v = emit_load_upval_v(def, upval);
CreateCall4(def->builder, def->raviV_settable_sskeyF, def->L, v, rb, rc);
}
void RaviCodeGenerator::emit_NEWARRAYINT(RaviFunctionDef *def, int A, int pc) {
emit_debug_trace(def, OP_RAVI_NEWARRAYI, pc);
emit_load_base(def);

@ -797,6 +797,11 @@ LuaLLVMTypes::LuaLLVMTypes(llvm::LLVMContext &context) : mdbuilder(context) {
// void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val)
luaV_settableT =
llvm::FunctionType::get(llvm::Type::getVoidTy(context), elements, false);
raviV_gettable_sskeyT =
llvm::FunctionType::get(llvm::Type::getVoidTy(context), elements, false);
raviV_settable_sskeyT =
llvm::FunctionType::get(llvm::Type::getVoidTy(context), elements, false);
// void luaV_finishget (lua_State *L, const TValue *t, TValue *key,
// StkId val, const TValue *slot);
elements.push_back(pTValueT);

Loading…
Cancel
Save