Skip to content

Commit

Permalink
implement OP_SETUPVAL and OP_SETTABUP
Browse files Browse the repository at this point in the history
  • Loading branch information
Dibyendu Majumdar committed Mar 26, 2015
1 parent 4a88634 commit 822fc9b
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 4 deletions.
8 changes: 8 additions & 0 deletions include/ravi_llvmcodegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ struct LuaLLVMTypes {
llvm::FunctionType *luaV_modT;
llvm::FunctionType *luaV_objlenT;
llvm::FunctionType *luaV_divT;
llvm::FunctionType *luaC_upvalbarrierT;

llvm::FunctionType *luaV_executeT;
llvm::FunctionType *luaV_gettableT;
Expand Down Expand Up @@ -413,6 +414,7 @@ struct RaviFunctionDef {
llvm::Constant *luaV_modF;
llvm::Constant *luaV_divF;
llvm::Constant *luaV_objlenF;
llvm::Constant *luaC_upvalbarrierF;

// Some cheats - these correspond to OPCODEs that
// are not inlined as of now
Expand Down Expand Up @@ -728,9 +730,15 @@ class RaviCodeGenerator {
void emit_GETUPVAL(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Value *proto, int A, int B);

void emit_SETUPVAL(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Value *proto, int A, int B);

void emit_GETTABUP(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Value *proto, int A, int B, int C);

void emit_SETTABUP(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Value *proto, int A, int B, int C);

void emit_NEWARRAYINT(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Value *proto, int A);

Expand Down
15 changes: 15 additions & 0 deletions ravi-tests/ravi_tests1.ravi
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,18 @@ end
assert(ravi.compile(x))
assert(x() == 5)
print("test 34 OK")

-- test 35
function x()
local x = 1
local f = function()
x=x+1
return x
end
return f
end
f=x()
assert(ravi.compile(f))
assert(f() == 2)
assert(f() == 3)
print("test 35 OK")
8 changes: 4 additions & 4 deletions readthedocs/ravi-jit-status.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ Note that if a Lua functions contains a bytecode that cannot be be JITed then th
+-------------------------+----------+--------------------------------------------------+
| OP_GETTABLE | YES | R(A) := R(B)[RK(C)] |
+-------------------------+----------+--------------------------------------------------+
| OP_SETTABUP | NO | UpValue[A][RK(B)] := RK(C) |
| OP_SETTABUP | YES | UpValue[A][RK(B)] := RK(C) |
+-------------------------+----------+--------------------------------------------------+
| OP_SETUPVAL | NO | UpValue[B] := R(A) |
| OP_SETUPVAL | YES | UpValue[B] := R(A) |
+-------------------------+----------+--------------------------------------------------+
| OP_SETTABLE | YES | R(A)[RK(B)] := RK(C) |
+-------------------------+----------+--------------------------------------------------+
Expand All @@ -45,7 +45,7 @@ Note that if a Lua functions contains a bytecode that cannot be be JITed then th
+-------------------------+----------+--------------------------------------------------+
| OP_MOD | YES | R(A) := RK(B) % RK(C) |
+-------------------------+----------+--------------------------------------------------+
| OP_POW | NO | R(A) := RK(B) ^ RK(C) |
| OP_POW | YES | R(A) := RK(B) ^ RK(C) |
+-------------------------+----------+--------------------------------------------------+
| OP_DIV | YES | R(A) := RK(B) / RK(C) |
+-------------------------+----------+--------------------------------------------------+
Expand All @@ -61,7 +61,7 @@ Note that if a Lua functions contains a bytecode that cannot be be JITed then th
+-------------------------+----------+--------------------------------------------------+
| OP_SHR | NO | R(A) := RK(B) >> RK(C) |
+-------------------------+----------+--------------------------------------------------+
| OP_UNM | NO | R(A) := -R(B) |
| OP_UNM | YES | R(A) := -R(B) |
+-------------------------+----------+--------------------------------------------------+
| OP_BNOT | NO | R(A) := ~R(B) |
+-------------------------+----------+--------------------------------------------------+
Expand Down
14 changes: 14 additions & 0 deletions src/ravi_llvmcodegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ bool RaviCodeGenerator::canCompile(Proto *p) {
case OP_SETTABLE:
case OP_GETTABLE:
case OP_GETUPVAL:
case OP_SETUPVAL:
case OP_GETTABUP:
case OP_SETTABUP:
case OP_NEWTABLE:
case OP_SETLIST:
case OP_RAVI_NEWARRAYI:
Expand Down Expand Up @@ -411,6 +413,9 @@ void RaviCodeGenerator::emit_extern_declarations(RaviFunctionDef *def) {
def->luaV_objlenF = def->raviF->addExternFunction(
def->types->luaV_objlenT, reinterpret_cast<void *>(&luaV_objlen),
"luaV_objlen");
def->luaC_upvalbarrierF = def->raviF->addExternFunction(
def->types->luaC_upvalbarrierT,
reinterpret_cast<void *>(&luaC_upvalbarrier_), "luaC_upvalbarrier_");

// Create printf declaration
std::vector<llvm::Type *> args;
Expand Down Expand Up @@ -789,6 +794,15 @@ void RaviCodeGenerator::compile(lua_State *L, Proto *p) {
int B = GETARG_B(i);
emit_GETUPVAL(&def, L_ci, proto, A, B);
} break;
case OP_SETTABUP: {
int B = GETARG_B(i);
int C = GETARG_C(i);
emit_SETTABUP(&def, L_ci, proto, A, B, C);
} break;
case OP_SETUPVAL: {
int B = GETARG_B(i);
emit_SETUPVAL(&def, L_ci, proto, A, B);
} break;

case OP_ADD: {
int B = GETARG_B(i);
Expand Down
58 changes: 58 additions & 0 deletions src/ravi_llvmtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,47 @@ void RaviCodeGenerator::emit_GETUPVAL(RaviFunctionDef *def, llvm::Value *L_ci,
emit_assign(def, ra, v);
}

// UpValue[B] := R(A)
void RaviCodeGenerator::emit_SETUPVAL(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Value *proto, int A, int B) {

// UpVal *uv = cl->upvals[GETARG_B(i)];
// setobj(L, uv->v, ra);
// luaC_upvalbarrier(L, uv);

llvm::Instruction *base_ptr = emit_load_base(def);
llvm::Value *ra = emit_gep_ra(def, base_ptr, A);
llvm::Value *upval_ptr = emit_gep_upvals(def, def->p_LClosure, B);
llvm::Instruction *upval = emit_load_pupval(def, upval_ptr);
llvm::Value *v = emit_load_upval_v(def, upval);
emit_assign(def, v, ra);

llvm::Value *type = emit_load_type(def, v);
llvm::Value *is_collectible =
def->builder->CreateAnd(type, def->types->kInt[BIT_ISCOLLECTABLE]);

llvm::Value *value = emit_gep_upval_value(def, upval);
llvm::Value *cmp = def->builder->CreateICmpNE(v, value, "v.ne.value");
llvm::Value *tobool = def->builder->CreateICmpEQ(
is_collectible, def->types->kInt[0], "not.collectible");
llvm::Value *orcond =
def->builder->CreateOr(cmp, tobool, "v.ne.value.or.not.collectible");

llvm::BasicBlock *then =
llvm::BasicBlock::Create(def->jitState->context(), "if.then", def->f);
llvm::BasicBlock *end =
llvm::BasicBlock::Create(def->jitState->context(), "if.end");

def->builder->CreateCondBr(orcond, end, then);
def->builder->SetInsertPoint(then);

def->builder->CreateCall2(def->luaC_upvalbarrierF, def->L, upval);
def->builder->CreateBr(end);

def->f->getBasicBlockList().push_back(end);
def->builder->SetInsertPoint(end);
}

// R(A) := UpValue[B][RK(C)]
void RaviCodeGenerator::emit_GETTABUP(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Value *proto, int A, int B, int C) {
Expand All @@ -84,6 +125,23 @@ void RaviCodeGenerator::emit_GETTABUP(RaviFunctionDef *def, llvm::Value *L_ci,
def->builder->CreateCall4(def->luaV_gettableF, def->L, v, rc, ra);
}

// UpValue[A][RK(B)] := RK(C)
void RaviCodeGenerator::emit_SETTABUP(RaviFunctionDef *def, llvm::Value *L_ci,
llvm::Value *proto, int A, int B, int C) {

// int a = GETARG_A(i);
// Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i)));

llvm::Instruction *base_ptr = emit_load_base(def);
llvm::Value *rb = emit_gep_rkb(def, base_ptr, B);
llvm::Value *rc = emit_gep_rkb(def, base_ptr, C);

llvm::Value *upval_ptr = emit_gep_upvals(def, def->p_LClosure, A);
llvm::Instruction *upval = emit_load_pupval(def, upval_ptr);
llvm::Value *v = emit_load_upval_v(def, upval);
def->builder->CreateCall4(def->luaV_settableF, def->L, v, rb, rc);
}

void RaviCodeGenerator::emit_NEWARRAYINT(RaviFunctionDef *def,
llvm::Value *L_ci, llvm::Value *proto,
int A) {
Expand Down
6 changes: 6 additions & 0 deletions src/ravi_llvmtypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,12 @@ LuaLLVMTypes::LuaLLVMTypes(llvm::LLVMContext &context) : mdbuilder(context) {
elements.push_back(StkIdT);
luaD_poscallT = llvm::FunctionType::get(C_intT, elements, false);

// void luaC_upvalbarrier_ (lua_State *L, UpVal *uv)
elements.clear();
elements.push_back(plua_StateT);
elements.push_back(pUpValT);
luaC_upvalbarrierT = llvm::FunctionType::get(llvm::Type::getVoidTy(context), elements, false);

// int luaD_precall (lua_State *L, StkId func, int nresults, int compile);
elements.clear();
elements.push_back(plua_StateT);
Expand Down

0 comments on commit 822fc9b

Please sign in to comment.