bitwise ops wip

pull/81/head
Dibyendu Majumdar 9 years ago
parent 7ed12db6de
commit bb6d02c08f

@ -68,5 +68,7 @@ LUAI_FUNC void raviV_op_concat(lua_State *L, CallInfo *ci, int a, int b, int c);
LUAI_FUNC void raviV_op_closure(lua_State *L, CallInfo *ci, LClosure *cl, int a, int Bx);
LUAI_FUNC void raviV_op_vararg(lua_State *L, CallInfo *ci, LClosure *cl, int a, int b);
LUAI_FUNC void raviV_op_setupval(lua_State *L, LClosure *cl, TValue *ra, int b);
LUAI_FUNC void raviV_op_shl(lua_State *L, TValue *ra, TValue *rb, TValue *rc);
LUAI_FUNC void raviV_op_shr(lua_State *L, TValue *ra, TValue *rb, TValue *rc);
#endif

@ -247,6 +247,8 @@ struct LuaLLVMTypes {
llvm::FunctionType *raviV_op_concatT;
llvm::FunctionType *raviV_op_closureT;
llvm::FunctionType *raviV_op_varargT;
llvm::FunctionType *raviV_op_shrT;
llvm::FunctionType *raviV_op_shlT;
llvm::FunctionType *raviH_set_intT;
llvm::FunctionType *raviH_set_floatT;
@ -517,6 +519,8 @@ struct RaviFunctionDef {
llvm::Function *raviV_op_concatF;
llvm::Function *raviV_op_closureF;
llvm::Function *raviV_op_varargF;
llvm::Function *raviV_op_shrF;
llvm::Function *raviV_op_shlF;
llvm::Function *raviH_set_intF;
llvm::Function *raviH_set_floatF;
@ -948,6 +952,8 @@ public:
void emit_BITWISE_BINARY_OP(RaviFunctionDef *def, OpCode op, int A, int B, int C);
void emit_BITWISE_SHIFT_OP(RaviFunctionDef *def, OpCode op, int A, int B, int C);
void emit_BNOT_I(RaviFunctionDef *def, int A, int B);
private:

@ -1767,7 +1767,25 @@ void raviV_op_setupval(lua_State *L, LClosure *cl, TValue *ra, int b) {
luaC_upvalbarrier(L, uv);
}
void raviV_op_shl(lua_State *L, TValue *ra, TValue *rb, TValue *rc) {
lua_Integer ib;
lua_Integer ic;
if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
setivalue(ra, luaV_shiftl(ib, ic));
} else {
luaT_trybinTM(L, rb, rc, ra, TM_SHL);
}
}
void raviV_op_shr(lua_State *L, TValue *ra, TValue *rb, TValue *rc) {
lua_Integer ib;
lua_Integer ic;
if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
setivalue(ra, luaV_shiftl(ib, -ic));
} else {
luaT_trybinTM(L, rb, rc, ra, TM_SHR);
}
}
/* }================================================================== */

@ -331,6 +331,7 @@ void RaviCodeGenerator::emit_BITWISE_BINARY_OP(RaviFunctionDef *def, OpCode op,
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);
llvm::Value *lhs = emit_load_register_or_constant_i(def, B);
@ -348,18 +349,36 @@ void RaviCodeGenerator::emit_BITWISE_BINARY_OP(RaviFunctionDef *def, OpCode op,
case OP_RAVI_BXOR_II:
result = def->builder->CreateXor(lhs, rhs, "");
break;
default:
fprintf(stderr, "unexpected value of opcode %d\n", (int)op);
lua_assert(false);
abort();
}
emit_store_reg_i_withtype(def, result, ra);
}
void RaviCodeGenerator::emit_BITWISE_SHIFT_OP(RaviFunctionDef *def, OpCode op,
int A, int B, int C) {
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);
switch (op) {
// FIXME Lua has specific requirements for shift operators
case OP_SHL:
case OP_RAVI_SHL_II:
result = def->builder->CreateShl(lhs, rhs, "");
CreateCall4(def->builder, def->raviV_op_shlF, def->L, ra, rb, rc);
break;
case OP_SHR:
case OP_RAVI_SHR_II:
result = def->builder->CreateLShr(lhs, rhs, "");
CreateCall4(def->builder, def->raviV_op_shrF, def->L, ra, rb, rc);
break;
default:
fprintf(stderr, "unexpected value of opcode %d\n", (int)op);
lua_assert(false);
abort();
}
emit_store_reg_i_withtype(def, result, ra);
}
void RaviCodeGenerator::emit_BNOT_I(RaviFunctionDef *def, int A, int B) {

@ -722,9 +722,11 @@ bool RaviCodeGenerator::canCompile(Proto *p) {
case OP_RAVI_BAND_II:
case OP_RAVI_BOR_II:
case OP_RAVI_BXOR_II:
case OP_RAVI_BNOT_I:
case OP_RAVI_SHL_II:
case OP_RAVI_SHR_II:
case OP_RAVI_BNOT_I:
// case OP_SHR:
// case OP_SHL:
break;
default:
return false;
@ -862,6 +864,12 @@ void RaviCodeGenerator::emit_extern_declarations(RaviFunctionDef *def) {
def->raviH_set_floatF = def->raviF->addExternFunction(
def->types->raviH_set_floatT, reinterpret_cast<void *>(&raviH_set_float),
"raviH_set_float");
def->raviV_op_shlF = def->raviF->addExternFunction(
def->types->raviV_op_shlT, reinterpret_cast<void *>(&raviV_op_shl),
"raviV_op_shl");
def->raviV_op_shrF = def->raviF->addExternFunction(
def->types->raviV_op_shrT, reinterpret_cast<void *>(&raviV_op_shr),
"raviV_op_shr");
// Create printf declaration
std::vector<llvm::Type *> args;
@ -1343,8 +1351,6 @@ void RaviCodeGenerator::compile(lua_State *L, Proto *p, bool doDump,
emit_SETUPVAL(def, A, B);
} break;
case OP_RAVI_SHR_II:
case OP_RAVI_SHL_II:
case OP_RAVI_BXOR_II:
case OP_RAVI_BOR_II:
case OP_RAVI_BAND_II: {
@ -1353,6 +1359,15 @@ void RaviCodeGenerator::compile(lua_State *L, Proto *p, bool doDump,
emit_BITWISE_BINARY_OP(def, op, A, B, C);
} break;
case OP_SHR:
case OP_SHL:
case OP_RAVI_SHL_II:
case OP_RAVI_SHR_II: {
int B = GETARG_B(i);
int C = GETARG_C(i);
emit_BITWISE_SHIFT_OP(def, op, A, B, C);
} break;
case OP_ADD: {
int B = GETARG_B(i);
int C = GETARG_C(i);

@ -863,6 +863,16 @@ LuaLLVMTypes::LuaLLVMTypes(llvm::LLVMContext &context) : mdbuilder(context) {
raviV_op_varargT =
llvm::FunctionType::get(llvm::Type::getVoidTy(context), elements, false);
elements.clear();
elements.push_back(plua_StateT);
elements.push_back(pTValueT);
elements.push_back(pTValueT);
elements.push_back(pTValueT);
raviV_op_shlT =
llvm::FunctionType::get(llvm::Type::getVoidTy(context), elements, false);
raviV_op_shrT =
llvm::FunctionType::get(llvm::Type::getVoidTy(context), elements, false);
// void raviH_set_int(lua_State *L, Table *t, unsigned int key, lua_Integer
// value);
elements.clear();

Loading…
Cancel
Save