38#define hasjumps(e) ((e)->t != (e)->f)
41static int codesJ (
FuncState *fs, OpCode o,
int sj,
int k);
46l_noret luaK_semerror (
LexState *ls,
const char *msg) {
48 luaX_syntaxerror(ls, msg);
61 if (v) setivalue(v, e->u.ival);
64 if (v) setfltvalue(v, e->u.nval);
75 lua_assert(e->k == VCONST);
76 return &fs->ls->dyd->actvar.arr[e->u.info].k;
98 setsvalue(fs->ls->L, v, e->u.strval);
102 setobj(fs->ls->L, v, const2val(fs, e));
105 default:
return tonumeral(e, v);
116static Instruction *previousinstruction (
FuncState *fs) {
117 static const Instruction invalidinstruction = ~(Instruction)0;
118 if (fs->pc > fs->lasttarget)
119 return &fs->f->code[fs->pc - 1];
121 return cast(Instruction*, &invalidinstruction);
131void luaK_nil (
FuncState *fs,
int from,
int n) {
132 int l = from + n - 1;
133 Instruction *previous = previousinstruction(fs);
134 if (GET_OPCODE(*previous) == OP_LOADNIL) {
135 int pfrom = GETARG_A(*previous);
136 int pl = pfrom + GETARG_B(*previous);
137 if ((pfrom <= from && from <= pl + 1) ||
138 (from <= pfrom && pfrom <= l + 1)) {
139 if (pfrom < from) from = pfrom;
141 SETARG_A(*previous, from);
142 SETARG_B(*previous, l - from);
146 luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0);
154static int getjump (
FuncState *fs,
int pc) {
155 int offset = GETARG_sJ(fs->f->code[pc]);
156 if (offset == NO_JUMP)
159 return (pc+1)+offset;
167static void fixjump (
FuncState *fs,
int pc,
int dest) {
168 Instruction *jmp = &fs->f->code[pc];
169 int offset = dest - (pc + 1);
170 lua_assert(dest != NO_JUMP);
171 if (!(-OFFSET_sJ <= offset && offset <= MAXARG_sJ - OFFSET_sJ))
172 luaX_syntaxerror(fs->ls,
"control structure too long");
173 lua_assert(GET_OPCODE(*jmp) == OP_JMP);
174 SETARG_sJ(*jmp, offset);
181void luaK_concat (
FuncState *fs,
int *l1,
int l2) {
182 if (l2 == NO_JUMP)
return;
183 else if (*l1 == NO_JUMP)
188 while ((next = getjump(fs, list)) != NO_JUMP)
190 fixjump(fs, list, l2);
200 return codesJ(fs, OP_JMP, NO_JUMP, 0);
207void luaK_ret (
FuncState *fs,
int first,
int nret) {
210 case 0: op = OP_RETURN0;
break;
211 case 1: op = OP_RETURN1;
break;
212 default: op = OP_RETURN;
break;
214 luaK_codeABC(fs, op, first, nret + 1, 0);
222static int condjump (
FuncState *fs, OpCode op,
int A,
int B,
int C,
int k) {
223 luaK_codeABCk(fs, op, A, B, C, k);
224 return luaK_jump(fs);
233 fs->lasttarget = fs->pc;
243static Instruction *getjumpcontrol (
FuncState *fs,
int pc) {
244 Instruction *pi = &fs->f->code[pc];
245 if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
259static int patchtestreg (
FuncState *fs,
int node,
int reg) {
260 Instruction *i = getjumpcontrol(fs, node);
261 if (GET_OPCODE(*i) != OP_TESTSET)
263 if (reg != NO_REG && reg != GETARG_B(*i))
268 *i = CREATE_ABCk(OP_TEST, GETARG_B(*i), 0, 0, GETARG_k(*i));
277static void removevalues (
FuncState *fs,
int list) {
278 for (; list != NO_JUMP; list = getjump(fs, list))
279 patchtestreg(fs, list, NO_REG);
288static void patchlistaux (
FuncState *fs,
int list,
int vtarget,
int reg,
290 while (list != NO_JUMP) {
291 int next = getjump(fs, list);
292 if (patchtestreg(fs, list, reg))
293 fixjump(fs, list, vtarget);
295 fixjump(fs, list, dtarget);
306void luaK_patchlist (
FuncState *fs,
int list,
int target) {
307 lua_assert(target <= fs->pc);
308 patchlistaux(fs, list, target, NO_REG, target);
312void luaK_patchtohere (
FuncState *fs,
int list) {
313 int hr = luaK_getlabel(fs);
314 luaK_patchlist(fs, list, hr);
319#define LIMLINEDIFF 0x80
330 int linedif = line - fs->previousline;
332 if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ >= MAXIWTHABS) {
333 luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo,
334 f->sizeabslineinfo,
AbsLineInfo, MAX_INT,
"lines");
335 f->abslineinfo[fs->nabslineinfo].pc = pc;
336 f->abslineinfo[fs->nabslineinfo++].line = line;
337 linedif = ABSLINEINFO;
340 luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte,
342 f->lineinfo[pc] = linedif;
343 fs->previousline = line;
353static void removelastlineinfo (
FuncState *fs) {
356 if (f->lineinfo[pc] != ABSLINEINFO) {
357 fs->previousline -= f->lineinfo[pc];
361 lua_assert(f->abslineinfo[fs->nabslineinfo - 1].pc == pc);
363 fs->iwthabs = MAXIWTHABS + 1;
372static void removelastinstruction (
FuncState *fs) {
373 removelastlineinfo(fs);
382int luaK_code (
FuncState *fs, Instruction i) {
385 luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
387 f->code[fs->pc++] = i;
388 savelineinfo(fs, f, fs->ls->lastline);
397int luaK_codeABCk (
FuncState *fs, OpCode o,
int a,
int b,
int c,
int k) {
398 lua_assert(getOpMode(o) == iABC);
399 lua_assert(a <= MAXARG_A && b <= MAXARG_B &&
400 c <= MAXARG_C && (k & ~1) == 0);
401 return luaK_code(fs, CREATE_ABCk(o, a, b, c, k));
408int luaK_codeABx (
FuncState *fs, OpCode o,
int a,
unsigned int bc) {
409 lua_assert(getOpMode(o) == iABx);
410 lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx);
411 return luaK_code(fs, CREATE_ABx(o, a, bc));
418int luaK_codeAsBx (
FuncState *fs, OpCode o,
int a,
int bc) {
419 unsigned int b = bc + OFFSET_sBx;
420 lua_assert(getOpMode(o) == iAsBx);
421 lua_assert(a <= MAXARG_A && b <= MAXARG_Bx);
422 return luaK_code(fs, CREATE_ABx(o, a, b));
429static int codesJ (
FuncState *fs, OpCode o,
int sj,
int k) {
430 unsigned int j = sj + OFFSET_sJ;
431 lua_assert(getOpMode(o) == isJ);
432 lua_assert(j <= MAXARG_sJ && (k & ~1) == 0);
433 return luaK_code(fs, CREATE_sJ(o, j, k));
440static int codeextraarg (
FuncState *fs,
int a) {
441 lua_assert(a <= MAXARG_Ax);
442 return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
451static int luaK_codek (
FuncState *fs,
int reg,
int k) {
453 return luaK_codeABx(fs, OP_LOADK, reg, k);
455 int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
466void luaK_checkstack (
FuncState *fs,
int n) {
467 int newstack = fs->freereg + n;
468 if (newstack > fs->f->maxstacksize) {
469 if (newstack >= MAXREGS)
470 luaX_syntaxerror(fs->ls,
471 "function or expression needs too many registers");
472 fs->f->maxstacksize = cast_byte(newstack);
480void luaK_reserveregs (
FuncState *fs,
int n) {
481 luaK_checkstack(fs, n);
491static void freereg (
FuncState *fs,
int reg) {
492 if (reg >= luaY_nvarstack(fs)) {
494 lua_assert(reg == fs->freereg);
502static void freeregs (
FuncState *fs,
int r1,
int r2) {
518 if (e->k == VNONRELOC)
519 freereg(fs, e->u.info);
528 int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1;
529 int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1;
530 freeregs(fs, r1, r2);
547 const TValue *idx = luaH_get(fs->ls->h, key);
549 if (ttisinteger(idx)) {
550 k = cast_int(ivalue(idx));
552 if (k < fs->nk && ttypetag(&f->k[k]) == ttypetag(v) &&
553 luaV_rawequalobj(&f->k[k], v))
562 luaH_finishset(L, fs->ls->h, key, idx, &val);
563 luaM_growvector(L, f->k, k, f->sizek,
TValue, MAXARG_Ax,
"constants");
564 while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
565 setobj(L, &f->k[k], v);
567 luaC_barrier(L, f, v);
577 setsvalue(fs->ls->L, &o, s);
578 return addk(fs, &o, &o);
585static int luaK_intK (
FuncState *fs, lua_Integer n) {
588 return addk(fs, &o, &o);
602static int luaK_numberK (
FuncState *fs, lua_Number r) {
606 if (!luaV_flttointeger(r, &ik, F2Ieq))
607 return addk(fs, &o, &o);
609 const int nbm = l_floatatt(MANT_DIG);
610 const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1);
611 const lua_Number k = (ik == 0) ? q : r + r*q;
615 lua_assert(!luaV_flttointeger(k, &ik, F2Ieq) ||
616 l_mathop(fabs)(r) >= l_mathop(1e6));
617 return addk(fs, &kv, &o);
628 return addk(fs, &o, &o);
638 return addk(fs, &o, &o);
649 sethvalue(fs->ls->L, &k, fs->ls->h);
650 return addk(fs, &k, &v);
659static int fitsC (lua_Integer i) {
660 return (l_castS2U(i) + OFFSET_sC <= cast_uint(MAXARG_C));
667static int fitsBx (lua_Integer i) {
668 return (-OFFSET_sBx <= i && i <= MAXARG_Bx - OFFSET_sBx);
672void luaK_int (
FuncState *fs,
int reg, lua_Integer i) {
674 luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i));
676 luaK_codek(fs, reg, luaK_intK(fs, i));
680static void luaK_float (
FuncState *fs,
int reg, lua_Number f) {
682 if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi))
683 luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
685 luaK_codek(fs, reg, luaK_numberK(fs, f));
693 switch (ttypetag(v)) {
695 e->k = VKINT; e->u.ival = ivalue(v);
698 e->k = VKFLT; e->u.nval = fltvalue(v);
709 case LUA_VSHRSTR:
case LUA_VLNGSTR:
710 e->k = VKSTR; e->u.strval = tsvalue(v);
712 default: lua_assert(0);
722 Instruction *pc = &getinstruction(fs, e);
724 SETARG_C(*pc, nresults + 1);
726 lua_assert(e->k == VVARARG);
727 SETARG_C(*pc, nresults + 1);
728 SETARG_A(*pc, fs->freereg);
729 luaK_reserveregs(fs, 1);
738 lua_assert(e->k == VKSTR);
739 e->u.info = stringK(fs, e->u.strval);
757 lua_assert(GETARG_C(getinstruction(fs, e)) == 2);
759 e->u.info = GETARG_A(getinstruction(fs, e));
761 else if (e->k == VVARARG) {
762 SETARG_C(getinstruction(fs, e), 2);
775 const2exp(const2val(fs, e), e);
779 e->u.info = e->u.var.ridx;
784 e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
789 e->u.info = luaK_codeABC(fs, OP_GETTABUP, 0, e->u.ind.t, e->u.ind.idx);
794 freereg(fs, e->u.ind.t);
795 e->u.info = luaK_codeABC(fs, OP_GETI, 0, e->u.ind.t, e->u.ind.idx);
800 freereg(fs, e->u.ind.t);
801 e->u.info = luaK_codeABC(fs, OP_GETFIELD, 0, e->u.ind.t, e->u.ind.idx);
806 freeregs(fs, e->u.ind.t, e->u.ind.idx);
807 e->u.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.ind.t, e->u.ind.idx);
811 case VVARARG:
case VCALL: {
812 luaK_setoneret(fs, e);
826 luaK_dischargevars(fs, e);
829 luaK_nil(fs, reg, 1);
833 luaK_codeABC(fs, OP_LOADFALSE, reg, 0, 0);
837 luaK_codeABC(fs, OP_LOADTRUE, reg, 0, 0);
844 luaK_codek(fs, reg, e->u.info);
848 luaK_float(fs, reg, e->u.nval);
852 luaK_int(fs, reg, e->u.ival);
856 Instruction *pc = &getinstruction(fs, e);
861 if (reg != e->u.info)
862 luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
866 lua_assert(e->k == VJMP);
881 if (e->k != VNONRELOC) {
882 luaK_reserveregs(fs, 1);
883 discharge2reg(fs, e, fs->freereg-1);
888static int code_loadbool (
FuncState *fs,
int A, OpCode op) {
890 return luaK_codeABC(fs, op, A, 0, 0);
898static int need_value (
FuncState *fs,
int list) {
899 for (; list != NO_JUMP; list = getjump(fs, list)) {
900 Instruction i = *getjumpcontrol(fs, list);
901 if (GET_OPCODE(i) != OP_TESTSET)
return 1;
915 discharge2reg(fs, e, reg);
917 luaK_concat(fs, &e->t, e->u.info);
922 if (need_value(fs, e->t) || need_value(fs, e->f)) {
923 int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
924 p_f = code_loadbool(fs, reg, OP_LFALSESKIP);
925 p_t = code_loadbool(fs, reg, OP_LOADTRUE);
927 luaK_patchtohere(fs, fj);
929 final = luaK_getlabel(fs);
930 patchlistaux(fs, e->f,
final, reg, p_f);
931 patchlistaux(fs, e->t,
final, reg, p_t);
933 e->f = e->t = NO_JUMP;
943 luaK_dischargevars(fs, e);
945 luaK_reserveregs(fs, 1);
946 exp2reg(fs, e, fs->freereg - 1);
955 luaK_dischargevars(fs, e);
956 if (e->k == VNONRELOC) {
959 if (e->u.info >= luaY_nvarstack(fs)) {
960 exp2reg(fs, e, e->u.info);
967 luaK_exp2nextreg(fs, e);
977 if (e->k != VUPVAL || hasjumps(e))
978 luaK_exp2anyreg(fs, e);
988 luaK_exp2anyreg(fs, e);
990 luaK_dischargevars(fs, e);
1002 case VTRUE: info = boolT(fs);
break;
1003 case VFALSE: info = boolF(fs);
break;
1004 case VNIL: info = nilK(fs);
break;
1005 case VKINT: info = luaK_intK(fs, e->u.ival);
break;
1006 case VKFLT: info = luaK_numberK(fs, e->u.nval);
break;
1007 case VKSTR: info = stringK(fs, e->u.strval);
break;
1008 case VK: info = e->u.info;
break;
1011 if (info <= MAXINDEXRK) {
1029 if (luaK_exp2K(fs, e))
1032 luaK_exp2anyreg(fs, e);
1038static void codeABRK (
FuncState *fs, OpCode o,
int a,
int b,
1040 int k = luaK_exp2RK(fs, ec);
1041 luaK_codeABCk(fs, o, a, b, ec->u.info, k);
1052 exp2reg(fs, ex, var->u.var.ridx);
1056 int e = luaK_exp2anyreg(fs, ex);
1057 luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
1061 codeABRK(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, ex);
1065 codeABRK(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, ex);
1069 codeABRK(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, ex);
1073 codeABRK(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, ex);
1076 default: lua_assert(0);
1087 luaK_exp2anyreg(fs, e);
1090 e->u.info = fs->freereg;
1092 luaK_reserveregs(fs, 2);
1093 codeABRK(fs, OP_SELF, e->u.info, ereg, key);
1102 Instruction *pc = getjumpcontrol(fs, e->u.info);
1103 lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
1104 GET_OPCODE(*pc) != OP_TEST);
1105 SETARG_k(*pc, (GETARG_k(*pc) ^ 1));
1116 if (e->k == VRELOC) {
1117 Instruction ie = getinstruction(fs, e);
1118 if (GET_OPCODE(ie) == OP_NOT) {
1119 removelastinstruction(fs);
1120 return condjump(fs, OP_TEST, GETARG_B(ie), 0, 0, !cond);
1124 discharge2anyreg(fs, e);
1126 return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 0, cond);
1135 luaK_dischargevars(fs, e);
1138 negatecondition(fs, e);
1142 case VK:
case VKFLT:
case VKINT:
case VKSTR:
case VTRUE: {
1147 pc = jumponcond(fs, e, 0);
1151 luaK_concat(fs, &e->f, pc);
1152 luaK_patchtohere(fs, e->t);
1162 luaK_dischargevars(fs, e);
1168 case VNIL:
case VFALSE: {
1173 pc = jumponcond(fs, e, 1);
1177 luaK_concat(fs, &e->t, pc);
1178 luaK_patchtohere(fs, e->f);
1188 case VNIL:
case VFALSE: {
1192 case VK:
case VKFLT:
case VKINT:
case VKSTR:
case VTRUE: {
1197 negatecondition(fs, e);
1202 discharge2anyreg(fs, e);
1204 e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
1208 default: lua_assert(0);
1211 {
int temp = e->f; e->f = e->t; e->t = temp; }
1212 removevalues(fs, e->f);
1213 removevalues(fs, e->t);
1221 return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B &&
1222 ttisshrstring(&fs->f->k[e->u.info]));
1229 return (e->k == VKINT && !hasjumps(e));
1237static int isCint (
expdesc *e) {
1238 return luaK_isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
1246static int isSCint (
expdesc *e) {
1247 return luaK_isKint(e) && fitsC(e->u.ival);
1255static int isSCnumber (
expdesc *e,
int *pi,
int *isfloat) {
1259 else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, F2Ieq))
1263 if (!hasjumps(e) && fitsC(i)) {
1264 *pi = int2sC(cast_int(i));
1281 lua_assert(!hasjumps(t) &&
1282 (t->k == VLOCAL || t->k == VNONRELOC || t->k == VUPVAL));
1283 if (t->k == VUPVAL && !isKstr(fs, k))
1284 luaK_exp2anyreg(fs, t);
1285 if (t->k == VUPVAL) {
1286 t->u.ind.t = t->u.info;
1287 t->u.ind.idx = k->u.info;
1292 t->u.ind.t = (t->k == VLOCAL) ? t->u.var.ridx: t->u.info;
1293 if (isKstr(fs, k)) {
1294 t->u.ind.idx = k->u.info;
1297 else if (isCint(k)) {
1298 t->u.ind.idx = cast_int(k->u.ival);
1302 t->u.ind.idx = luaK_exp2anyreg(fs, k);
1316 case LUA_OPBAND:
case LUA_OPBOR:
case LUA_OPBXOR:
1317 case LUA_OPSHL:
case LUA_OPSHR:
case LUA_OPBNOT: {
1319 return (luaV_tointegerns(v1, &i, LUA_FLOORN2I) &&
1320 luaV_tointegerns(v2, &i, LUA_FLOORN2I));
1322 case LUA_OPDIV:
case LUA_OPIDIV:
case LUA_OPMOD:
1323 return (nvalue(v2) != 0);
1336 if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
1338 luaO_rawarith(fs->ls->L, op, &v1, &v2, &res);
1339 if (ttisinteger(&res)) {
1341 e1->u.ival = ivalue(&res);
1344 lua_Number n = fltvalue(&res);
1345 if (luai_numisnan(n) || n == 0)
1357l_sinline OpCode binopr2op (BinOpr opr, BinOpr baser, OpCode base) {
1358 lua_assert(baser <= opr &&
1359 ((baser == OPR_ADD && opr <= OPR_SHR) ||
1360 (baser == OPR_LT && opr <= OPR_LE)));
1361 return cast(OpCode, (cast_int(opr) - cast_int(baser)) + cast_int(base));
1368l_sinline OpCode unopr2op (UnOpr opr) {
1369 return cast(OpCode, (cast_int(opr) - cast_int(OPR_MINUS)) +
1377l_sinline TMS binopr2TM (BinOpr opr) {
1378 lua_assert(OPR_ADD <= opr && opr <= OPR_SHR);
1379 return cast(TMS, (cast_int(opr) - cast_int(OPR_ADD)) + cast_int(TM_ADD));
1389 int r = luaK_exp2anyreg(fs, e);
1391 e->u.info = luaK_codeABC(fs, op, 0, r, 0);
1393 luaK_fixline(fs, line);
1404 OpCode op,
int v2,
int flip,
int line,
1405 OpCode mmop, TMS event) {
1406 int v1 = luaK_exp2anyreg(fs, e1);
1407 int pc = luaK_codeABCk(fs, op, 0, v1, v2, 0);
1408 freeexps(fs, e1, e2);
1411 luaK_fixline(fs, line);
1412 luaK_codeABCk(fs, mmop, v1, v2, event, flip);
1413 luaK_fixline(fs, line);
1421static void codebinexpval (
FuncState *fs, BinOpr opr,
1423 OpCode op = binopr2op(opr, OPR_ADD, OP_ADD);
1424 int v2 = luaK_exp2anyreg(fs, e2);
1426 lua_assert((VNIL <= e1->k && e1->k <= VKSTR) ||
1427 e1->k == VNONRELOC || e1->k == VRELOC);
1428 lua_assert(OP_ADD <= op && op <= OP_SHR);
1429 finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, binopr2TM(opr));
1436static void codebini (
FuncState *fs, OpCode op,
1439 int v2 = int2sC(cast_int(e2->u.ival));
1440 lua_assert(e2->k == VKINT);
1441 finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINI, event);
1448static void codebinK (
FuncState *fs, BinOpr opr,
1450 TMS
event = binopr2TM(opr);
1451 int v2 = e2->u.info;
1452 OpCode op = binopr2op(opr, OPR_ADD, OP_ADDK);
1453 finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event);
1461 OpCode op,
int line, TMS event) {
1462 if (!luaK_isKint(e2))
1465 lua_Integer i2 = e2->u.ival;
1466 if (!(fitsC(i2) && fitsC(-i2)))
1469 int v2 = cast_int(i2);
1470 finishbinexpval(fs, e1, e2, op, int2sC(-v2), 0, line, OP_MMBINI, event);
1472 SETARG_B(fs->f->code[fs->pc - 1], int2sC(v2));
1480 expdesc temp = *e1; *e1 = *e2; *e2 = temp;
1487static void codebinNoK (
FuncState *fs, BinOpr opr,
1491 codebinexpval(fs, opr, e1, e2, line);
1499static void codearith (
FuncState *fs, BinOpr opr,
1501 if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2))
1502 codebinK(fs, opr, e1, e2, flip, line);
1504 codebinNoK(fs, opr, e1, e2, flip, line);
1513static void codecommutative (
FuncState *fs, BinOpr op,
1516 if (tonumeral(e1, NULL)) {
1520 if (op == OPR_ADD && isSCint(e2))
1521 codebini(fs, OP_ADDI, e1, e2, flip, line, TM_ADD);
1523 codearith(fs, op, e1, e2, flip, line);
1531static void codebitwise (
FuncState *fs, BinOpr opr,
1534 if (e1->k == VKINT) {
1538 if (e2->k == VKINT && luaK_exp2K(fs, e2))
1539 codebinK(fs, opr, e1, e2, flip, line);
1541 codebinNoK(fs, opr, e1, e2, flip, line);
1554 if (isSCnumber(e2, &im, &isfloat)) {
1556 r1 = luaK_exp2anyreg(fs, e1);
1558 op = binopr2op(opr, OPR_LT, OP_LTI);
1560 else if (isSCnumber(e1, &im, &isfloat)) {
1562 r1 = luaK_exp2anyreg(fs, e2);
1564 op = binopr2op(opr, OPR_LT, OP_GTI);
1567 r1 = luaK_exp2anyreg(fs, e1);
1568 r2 = luaK_exp2anyreg(fs, e2);
1569 op = binopr2op(opr, OPR_LT, OP_LT);
1571 freeexps(fs, e1, e2);
1572 e1->u.info = condjump(fs, op, r1, r2, isfloat, 1);
1586 if (e1->k != VNONRELOC) {
1587 lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT);
1590 r1 = luaK_exp2anyreg(fs, e1);
1591 if (isSCnumber(e2, &im, &isfloat)) {
1595 else if (luaK_exp2RK(fs, e2)) {
1601 r2 = luaK_exp2anyreg(fs, e2);
1603 freeexps(fs, e1, e2);
1604 e1->u.info = condjump(fs, op, r1, r2, isfloat, (opr == OPR_EQ));
1613 static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP};
1614 luaK_dischargevars(fs, e);
1616 case OPR_MINUS:
case OPR_BNOT:
1617 if (constfolding(fs, opr + LUA_OPUNM, e, &ef))
1621 codeunexpval(fs, unopr2op(opr), e, line);
1623 case OPR_NOT: codenot(fs, e);
break;
1624 default: lua_assert(0);
1634 luaK_dischargevars(fs, v);
1637 luaK_goiftrue(fs, v);
1641 luaK_goiffalse(fs, v);
1645 luaK_exp2nextreg(fs, v);
1648 case OPR_ADD:
case OPR_SUB:
1649 case OPR_MUL:
case OPR_DIV:
case OPR_IDIV:
1650 case OPR_MOD:
case OPR_POW:
1651 case OPR_BAND:
case OPR_BOR:
case OPR_BXOR:
1652 case OPR_SHL:
case OPR_SHR: {
1653 if (!tonumeral(v, NULL))
1654 luaK_exp2anyreg(fs, v);
1659 case OPR_EQ:
case OPR_NE: {
1660 if (!tonumeral(v, NULL))
1665 case OPR_LT:
case OPR_LE:
1666 case OPR_GT:
case OPR_GE: {
1668 if (!isSCnumber(v, &dummy, &dummy2))
1669 luaK_exp2anyreg(fs, v);
1673 default: lua_assert(0);
1683 Instruction *ie2 = previousinstruction(fs);
1684 if (GET_OPCODE(*ie2) == OP_CONCAT) {
1685 int n = GETARG_B(*ie2);
1686 lua_assert(e1->u.info + 1 == GETARG_A(*ie2));
1688 SETARG_A(*ie2, e1->u.info);
1689 SETARG_B(*ie2, n + 1);
1692 luaK_codeABC(fs, OP_CONCAT, e1->u.info, 2, 0);
1694 luaK_fixline(fs, line);
1702void luaK_posfix (
FuncState *fs, BinOpr opr,
1704 luaK_dischargevars(fs, e2);
1705 if (foldbinop(opr) && constfolding(fs, opr + LUA_OPADD, e1, e2))
1709 lua_assert(e1->t == NO_JUMP);
1710 luaK_concat(fs, &e2->f, e1->f);
1715 lua_assert(e1->f == NO_JUMP);
1716 luaK_concat(fs, &e2->t, e1->t);
1721 luaK_exp2nextreg(fs, e2);
1722 codeconcat(fs, e1, e2, line);
1725 case OPR_ADD:
case OPR_MUL: {
1726 codecommutative(fs, opr, e1, e2, line);
1730 if (finishbinexpneg(fs, e1, e2, OP_ADDI, line, TM_SUB))
1734 case OPR_DIV:
case OPR_IDIV:
case OPR_MOD:
case OPR_POW: {
1735 codearith(fs, opr, e1, e2, 0, line);
1738 case OPR_BAND:
case OPR_BOR:
case OPR_BXOR: {
1739 codebitwise(fs, opr, e1, e2, line);
1745 codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL);
1747 else if (finishbinexpneg(fs, e1, e2, OP_SHRI, line, TM_SHL)) {
1751 codebinexpval(fs, opr, e1, e2, line);
1756 codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR);
1758 codebinexpval(fs, opr, e1, e2, line);
1761 case OPR_EQ:
case OPR_NE: {
1762 codeeq(fs, opr, e1, e2);
1765 case OPR_GT:
case OPR_GE: {
1768 opr = cast(BinOpr, (opr - OPR_GT) + OPR_LT);
1770 case OPR_LT:
case OPR_LE: {
1771 codeorder(fs, opr, e1, e2);
1774 default: lua_assert(0);
1783void luaK_fixline (
FuncState *fs,
int line) {
1784 removelastlineinfo(fs);
1785 savelineinfo(fs, fs->f, line);
1789void luaK_settablesize (
FuncState *fs,
int pc,
int ra,
int asize,
int hsize) {
1790 Instruction *inst = &fs->f->code[pc];
1791 int rb = (hsize != 0) ? luaO_ceillog2(hsize) + 1 : 0;
1792 int extra = asize / (MAXARG_C + 1);
1793 int rc = asize % (MAXARG_C + 1);
1794 int k = (extra > 0);
1795 *inst = CREATE_ABCk(OP_NEWTABLE, ra, rb, rc, k);
1796 *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra);
1807void luaK_setlist (
FuncState *fs,
int base,
int nelems,
int tostore) {
1808 lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH);
1809 if (tostore == LUA_MULTRET)
1811 if (nelems <= MAXARG_C)
1812 luaK_codeABC(fs, OP_SETLIST, base, tostore, nelems);
1814 int extra = nelems / (MAXARG_C + 1);
1815 nelems %= (MAXARG_C + 1);
1816 luaK_codeABCk(fs, OP_SETLIST, base, tostore, nelems, 1);
1817 codeextraarg(fs, extra);
1819 fs->freereg = base + 1;
1826static int finaltarget (Instruction *
code,
int i) {
1828 for (count = 0; count < 100; count++) {
1829 Instruction pc =
code[i];
1830 if (GET_OPCODE(pc) != OP_JMP)
1833 i += GETARG_sJ(pc) + 1;
1846 for (i = 0; i < fs->pc; i++) {
1847 Instruction *pc = &p->code[i];
1848 lua_assert(i == 0 || isOT(*(pc - 1)) == isIT(*pc));
1849 switch (GET_OPCODE(*pc)) {
1850 case OP_RETURN0:
case OP_RETURN1: {
1851 if (!(fs->needclose || p->is_vararg))
1854 SET_OPCODE(*pc, OP_RETURN);
1856 case OP_RETURN:
case OP_TAILCALL: {
1860 SETARG_C(*pc, p->numparams + 1);
1864 int target = finaltarget(p->code, i);
1865 fixjump(fs, i, target);