// R(A) := -R(B), floating point // NOT yet used void RaviCodeGenerator::emit_UNMF(RaviFunctionDef *def, int A, int B, int pc) { (void)pc; emit_load_base(def); llvm::Value *ra = emit_gep_register(def, A); llvm::Value *rb = emit_gep_register_or_constant(def, B); llvm::Instruction *lhs = emit_load_reg_n(def, rb); llvm::Value *result = def->builder->CreateFNeg(lhs); emit_store_reg_n_withtype(def, result, ra); }
// Although the name is EQ this actually // implements EQ, LE and LT - by using the supplied lua function to call. void RaviCodeGenerator::emit_EQ(RaviFunctionDef *def, int A, int B, int C, int j, int jA, llvm::Constant *callee, OpCode opCode, int pc) { // case OP_EQ: { // TValue *rb = RKB(i); // TValue *rc = RKC(i); // Protect( // if (cast_int(luaV_equalobj(L, rb, rc)) != GETARG_A(i)) // ci->u.l.savedpc++; // else // donextjump(ci); // ) // } break; emit_debug_trace(def, opCode, pc); // Load pointer to base emit_load_base(def); // Get pointer to register B llvm::Value *regB = emit_gep_register_or_constant(def, B); // Get pointer to register C llvm::Value *regC = emit_gep_register_or_constant(def, C); llvm::Value *result = NULL; switch (opCode) { case OP_RAVI_LT_II: case OP_RAVI_LE_II: case OP_RAVI_EQ_II: { llvm::Instruction *p1 = emit_load_reg_i(def, regB); llvm::Instruction *p2 = emit_load_reg_i(def, regC); switch (opCode) { case OP_RAVI_EQ_II: result = def->builder->CreateICmpEQ(p1, p2, "EQ_II_result"); break; case OP_RAVI_LT_II: result = def->builder->CreateICmpSLT(p1, p2, "LT_II_result"); break; case OP_RAVI_LE_II: result = def->builder->CreateICmpSLE(p1, p2, "LE_II_result"); break; default: assert(0); } result = def->builder->CreateZExt(result, def->types->C_intT, "II_result_int"); } break; case OP_RAVI_LT_FF: case OP_RAVI_LE_FF: case OP_RAVI_EQ_FF: { llvm::Instruction *p1 = emit_load_reg_n(def, regB); llvm::Instruction *p2 = emit_load_reg_n(def, regC); switch (opCode) { case OP_RAVI_EQ_FF: result = def->builder->CreateFCmpOEQ(p1, p2, "EQ_FF_result"); break; case OP_RAVI_LT_FF: result = def->builder->CreateFCmpULT(p1, p2, "LT_FF_result"); break; case OP_RAVI_LE_FF: result = def->builder->CreateFCmpULE(p1, p2, "LE_FF_result"); break; default: assert(0); } result = def->builder->CreateZExt(result, def->types->C_intT, "FF_result_int"); } break; default: // Call luaV_equalobj with register B and C result = CreateCall3(def->builder, callee, def->L, regB, regC); } // Test if result is equal to operand A llvm::Value *result_eq_A = def->builder->CreateICmpEQ( result, llvm::ConstantInt::get(def->types->C_intT, A)); // If result == A then we need to execute the next statement which is a jump llvm::BasicBlock *then_block = llvm::BasicBlock::Create(def->jitState->context(), "if.then", def->f); llvm::BasicBlock *else_block = llvm::BasicBlock::Create(def->jitState->context(), "if.else"); def->builder->CreateCondBr(result_eq_A, then_block, else_block); def->builder->SetInsertPoint(then_block); // if (a > 0) luaF_close(L, ci->u.l.base + a - 1); if (jA > 0) { // jA is the A operand of the Jump instruction // Reload pointer to base as the call to luaV_equalobj() may // have invoked a Lua function and as a result the stack may have // been reallocated - so the previous base pointer could be stale emit_load_base(def); // base + a - 1 llvm::Value *val = emit_gep_register(def, jA - 1); // Call luaF_close CreateCall2(def->builder, def->luaF_closeF, def->L, val); } // Do the jump def->builder->CreateBr(def->jmp_targets[j].jmp1); // Add the else block and make it current so that the next instruction flows // here def->f->getBasicBlockList().push_back(else_block); def->builder->SetInsertPoint(else_block); }
void RaviCodeGenerator::emit_FORPREP(RaviFunctionDef *def, int A, int pc, int pc1) { // case OP_FORPREP: { // if (ttisinteger(init) && ttisinteger(pstep) && // forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) { // /* all values are integer */ // lua_Integer initv = (stopnow ? 0 : ivalue(init)); // setivalue(plimit, ilimit); // setivalue(init, initv - ivalue(pstep)); // } // else { /* try making all values floats */ // if (!tonumber(plimit, &nlimit)) // luaG_runerror(L, "'for' limit must be a number"); // setfltvalue(plimit, nlimit); // if (!tonumber(pstep, &nstep)) // luaG_runerror(L, "'for' step must be a number"); // setfltvalue(pstep, nstep); // if (!tonumber(init, &ninit)) // luaG_runerror(L, "'for' initial value must be a number"); // setfltvalue(init, luai_numsub(L, ninit, nstep)); // } // ci->u.l.savedpc += GETARG_sBx(i); //} break; emit_debug_trace(def, OP_FORPREP, pc1); // Load pointer to base emit_load_base(def); // lua_Integer ilimit; // int stopnow; // lua_Number ninit; lua_Number nlimit; lua_Number nstep; llvm::IRBuilder<> TmpB(def->entry, def->entry->begin()); llvm::Value *ilimit = TmpB.CreateAlloca(def->types->lua_IntegerT, nullptr, "ilimit"); llvm::Value *stopnow = TmpB.CreateAlloca(def->types->C_intT, nullptr, "stopnow"); llvm::Value *nlimit = TmpB.CreateAlloca(def->types->lua_NumberT, nullptr, "nlimit"); llvm::Value *ninit = TmpB.CreateAlloca(def->types->lua_NumberT, nullptr, "ninit"); llvm::Value *nstep = TmpB.CreateAlloca(def->types->lua_NumberT, nullptr, "nstep"); // TValue *init = ra; // TValue *plimit = ra + 1; // TValue *pstep = ra + 2; llvm::Value *init = emit_gep_register(def, A); llvm::Value *plimit = emit_gep_register(def, A + 1); llvm::Value *pstep = emit_gep_register(def, A + 2); // if (ttisinteger(init) && ttisinteger(pstep) && // forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) { // Get init->tt_ llvm::Instruction *pinit_tt = emit_load_type(def, init); // Compare init->tt_ == LUA_TNUMINT llvm::Value *cmp1 = emit_is_value_of_type(def, pinit_tt, LUA__TNUMINT, "init.is.integer"); // Get pstep->tt_ llvm::Instruction *pstep_tt = emit_load_type(def, pstep); // Compare pstep->tt_ == LUA_TNUMINT llvm::Value *icmp2 = emit_is_value_of_type(def, pstep_tt, LUA__TNUMINT, "step.is.integer"); // Get ivalue(pstep) llvm::Instruction *pstep_ivalue = emit_load_reg_i(def, pstep); // Call forlimit() llvm::Value *forlimit_ret = CreateCall4( def->builder, def->luaV_forlimitF, plimit, ilimit, pstep_ivalue, stopnow); // init->tt_ == LUA_TNUMINT && pstep->tt_ == LUA_TNUMINT llvm::Value *and1 = def->builder->CreateAnd(cmp1, icmp2, "init.and.step.are.integers"); // Convert result from forlimit() to bool llvm::Value *tobool = def->builder->CreateICmpNE(forlimit_ret, def->types->kInt[0]); // init->tt_ == LUA_TNUMINT && pstep->tt_ == LUA_TNUMINT && forlimit() llvm::Value *and2 = def->builder->CreateAnd(and1, tobool, "all.integers"); // Create if then else branch llvm::BasicBlock *then1 = llvm::BasicBlock::Create(def->jitState->context(), "if.all.integers", def->f); llvm::BasicBlock *else1 = llvm::BasicBlock::Create(def->jitState->context(), "if.not.all.integers"); def->builder->CreateCondBr(and2, then1, else1); def->builder->SetInsertPoint(then1); // all values are integers // lua_Integer initv = (stopnow ? 0 : ivalue(init)); // Get stopnow llvm::Instruction *stopnow_val = emit_load_local_int(def, stopnow); // Test if stopnow is 0 llvm::Value *stopnow_is_zero = def->builder->CreateICmpEQ( stopnow_val, def->types->kInt[0], "stopnow.is.zero"); // Setup if then else branch for stopnow llvm::BasicBlock *then1_iffalse = llvm::BasicBlock::Create( def->jitState->context(), "if.stopnow.iszero", def->f); llvm::BasicBlock *then1_iftrue = llvm::BasicBlock::Create(def->jitState->context(), "if.stopnow.notzero"); def->builder->CreateCondBr(stopnow_is_zero, then1_iffalse, then1_iftrue); def->builder->SetInsertPoint(then1_iffalse); // stopnow is 0 // Get init->i llvm::Instruction *init_ivalue = emit_load_reg_i(def, init); // Join after the branch def->builder->CreateBr(then1_iftrue); def->f->getBasicBlockList().push_back(then1_iftrue); def->builder->SetInsertPoint(then1_iftrue); // Set initv to 0 if !stopnow else init->i auto phi1 = def->builder->CreatePHI(def->types->lua_IntegerT, 2, "initv"); phi1->addIncoming(init_ivalue, then1_iffalse); phi1->addIncoming(def->types->kluaInteger[0], then1); // setivalue(plimit, ilimit); llvm::Instruction *ilimit_val = emit_load_local_n(def, ilimit); emit_store_reg_i_withtype(def, ilimit_val, plimit); // setivalue(init, initv - ivalue(pstep)); // we aleady know init is LUA_TNUMINT pstep_ivalue = emit_load_reg_i(def, pstep); llvm::Value *sub = def->builder->CreateSub(phi1, pstep_ivalue, "initv-pstep.i", false, true); emit_store_reg_i(def, sub, init); // We are done so jump to forloop lua_assert(def->jmp_targets[pc].jmp1); def->builder->CreateBr(def->jmp_targets[pc].jmp1); // NOW the non-integer case def->f->getBasicBlockList().push_back(else1); def->builder->SetInsertPoint(else1); // ************ PLIMIT - Convert plimit to float llvm::Instruction *plimit_tt = emit_load_type(def, plimit); // Test if already a float cmp1 = emit_is_value_of_type(def, plimit_tt, LUA__TNUMFLT, "limit.is.float"); llvm::BasicBlock *else1_plimit_ifnum = llvm::BasicBlock::Create( def->jitState->context(), "if.limit.isfloat", def->f); llvm::BasicBlock *else1_plimit_elsenum = llvm::BasicBlock::Create(def->jitState->context(), "if.limit.notfloat"); def->builder->CreateCondBr(cmp1, else1_plimit_ifnum, else1_plimit_elsenum); def->builder->SetInsertPoint(else1_plimit_ifnum); // Already a float - copy to nlimit llvm::Instruction *plimit_nvalue_load = emit_load_reg_n(def, plimit); emit_store_local_n(def, plimit_nvalue_load, nlimit); // Go to the PSTEP section llvm::BasicBlock *else1_pstep = llvm::BasicBlock::Create(def->jitState->context(), "if.else.step"); def->builder->CreateBr(else1_pstep); // If plimit was not already a float we need to convert def->f->getBasicBlockList().push_back(else1_plimit_elsenum); def->builder->SetInsertPoint(else1_plimit_elsenum); // Call luaV_tonumber_() llvm::Value *plimit_isnum = CreateCall2(def->builder, def->luaV_tonumberF, plimit, nlimit); llvm::Value *plimit_isnum_bool = def->builder->CreateICmpEQ( plimit_isnum, def->types->kInt[0], "limit.float.ok"); // Did conversion fail? llvm::BasicBlock *else1_plimit_tonum_elsenum = llvm::BasicBlock::Create( def->jitState->context(), "if.limit.float.failed", def->f); def->builder->CreateCondBr(plimit_isnum_bool, else1_plimit_tonum_elsenum, else1_pstep); // Conversion failed, so raise error def->builder->SetInsertPoint(else1_plimit_tonum_elsenum); emit_raise_lua_error(def, "'for' limit must be a number"); def->builder->CreateBr(else1_pstep); // Conversion OK // Update plimit def->f->getBasicBlockList().push_back(else1_pstep); def->builder->SetInsertPoint(else1_pstep); llvm::Instruction *nlimit_load = emit_load_local_n(def, nlimit); emit_store_reg_n_withtype(def, nlimit_load, plimit); // *********** PSTEP - convert pstep to float // Test if already a float pstep_tt = emit_load_type(def, pstep); cmp1 = emit_is_value_of_type(def, pstep_tt, LUA__TNUMFLT, "step.is.float"); llvm::BasicBlock *else1_pstep_ifnum = llvm::BasicBlock::Create( def->jitState->context(), "if.step.isfloat", def->f); llvm::BasicBlock *else1_pstep_elsenum = llvm::BasicBlock::Create(def->jitState->context(), "if.step.notfloat"); def->builder->CreateCondBr(cmp1, else1_pstep_ifnum, else1_pstep_elsenum); def->builder->SetInsertPoint(else1_pstep_ifnum); // We float then copy to nstep llvm::Instruction *pstep_nvalue_load = emit_load_reg_n(def, pstep); emit_store_local_n(def, pstep_nvalue_load, nstep); // Now go to handle initial value llvm::BasicBlock *else1_pinit = llvm::BasicBlock::Create(def->jitState->context(), "if.else.init"); def->builder->CreateBr(else1_pinit); // If pstep was not already a float then we need to convert def->f->getBasicBlockList().push_back(else1_pstep_elsenum); def->builder->SetInsertPoint(else1_pstep_elsenum); // call luaV_tonumber_() llvm::Value *pstep_isnum = CreateCall2(def->builder, def->luaV_tonumberF, pstep, nstep); llvm::Value *pstep_isnum_bool = def->builder->CreateICmpEQ( pstep_isnum, def->types->kInt[0], "step.float.ok"); llvm::BasicBlock *else1_pstep_tonum_elsenum = llvm::BasicBlock::Create( def->jitState->context(), "if.step.float.failed", def->f); def->builder->CreateCondBr(pstep_isnum_bool, else1_pstep_tonum_elsenum, else1_pinit); // If conversion failed raise error def->builder->SetInsertPoint(else1_pstep_tonum_elsenum); emit_raise_lua_error(def, "'for' step must be a number"); def->builder->CreateBr(else1_pinit); // Conversion okay so update pstep def->f->getBasicBlockList().push_back(else1_pinit); def->builder->SetInsertPoint(else1_pinit); llvm::Instruction *nstep_load = emit_load_local_n(def, nstep); emit_store_reg_n_withtype(def, nstep_load, pstep); // *********** PINIT finally handle initial value // Check if it is already a float pinit_tt = emit_load_type(def, init); cmp1 = emit_is_value_of_type(def, pinit_tt, LUA__TNUMFLT, "init.is.float"); llvm::BasicBlock *else1_pinit_ifnum = llvm::BasicBlock::Create( def->jitState->context(), "if.init.is.float", def->f); llvm::BasicBlock *else1_pinit_elsenum = llvm::BasicBlock::Create(def->jitState->context(), "if.init.not.float"); def->builder->CreateCondBr(cmp1, else1_pinit_ifnum, else1_pinit_elsenum); def->builder->SetInsertPoint(else1_pinit_ifnum); // Already float so copy to ninit llvm::Instruction *pinit_nvalue_load = emit_load_reg_n(def, init); emit_store_local_n(def, pinit_nvalue_load, ninit); // Go to final section llvm::BasicBlock *else1_pdone = llvm::BasicBlock::Create(def->jitState->context(), "if.else.done"); def->builder->CreateBr(else1_pdone); // Not a float so we need to convert def->f->getBasicBlockList().push_back(else1_pinit_elsenum); def->builder->SetInsertPoint(else1_pinit_elsenum); // Call luaV_tonumber_() llvm::Value *pinit_isnum = CreateCall2(def->builder, def->luaV_tonumberF, init, ninit); llvm::Value *pinit_isnum_bool = def->builder->CreateICmpEQ( pinit_isnum, def->types->kInt[0], "init.float.ok"); llvm::BasicBlock *else1_pinit_tonum_elsenum = llvm::BasicBlock::Create( def->jitState->context(), "if.init.float.failed", def->f); def->builder->CreateCondBr(pinit_isnum_bool, else1_pinit_tonum_elsenum, else1_pdone); // Conversion failed so raise error def->builder->SetInsertPoint(else1_pinit_tonum_elsenum); emit_raise_lua_error(def, "'for' initial value must be a number"); def->builder->CreateBr(else1_pdone); // Conversion OK so we are nearly done def->f->getBasicBlockList().push_back(else1_pdone); def->builder->SetInsertPoint(else1_pdone); llvm::Instruction *ninit_load = emit_load_local_n(def, ninit); nstep_load = emit_load_local_n(def, nstep); // setfltvalue(init, luai_numsub(L, ninit, nstep)); llvm::Value *init_n = def->builder->CreateFSub(ninit_load, nstep_load, "ninit-nstep"); emit_store_reg_n_withtype(def, init_n, init); // Done so jump to forloop def->builder->CreateBr(def->jmp_targets[pc].jmp1); }