|
@@ -415,7 +415,7 @@ int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
|
|
|
/*
|
|
|
** Format and emit an 'iAsBx' instruction.
|
|
|
*/
|
|
|
-int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) {
|
|
|
+static int codeAsBx (FuncState *fs, OpCode o, int a, int bc) {
|
|
|
unsigned int b = bc + OFFSET_sBx;
|
|
|
lua_assert(getOpMode(o) == iAsBx);
|
|
|
lua_assert(a <= MAXARG_A && b <= MAXARG_Bx);
|
|
@@ -671,7 +671,7 @@ static int fitsBx (lua_Integer i) {
|
|
|
|
|
|
void luaK_int (FuncState *fs, int reg, lua_Integer i) {
|
|
|
if (fitsBx(i))
|
|
|
- luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i));
|
|
|
+ codeAsBx(fs, OP_LOADI, reg, cast_int(i));
|
|
|
else
|
|
|
luaK_codek(fs, reg, luaK_intK(fs, i));
|
|
|
}
|
|
@@ -680,7 +680,7 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) {
|
|
|
static void luaK_float (FuncState *fs, int reg, lua_Number f) {
|
|
|
lua_Integer fi;
|
|
|
if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi))
|
|
|
- luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
|
|
|
+ codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
|
|
|
else
|
|
|
luaK_codek(fs, reg, luaK_numberK(fs, f));
|
|
|
}
|
|
@@ -1025,7 +1025,7 @@ static int luaK_exp2K (FuncState *fs, expdesc *e) {
|
|
|
** in the range of R/K indices).
|
|
|
** Returns 1 iff expression is K.
|
|
|
*/
|
|
|
-int luaK_exp2RK (FuncState *fs, expdesc *e) {
|
|
|
+static int exp2RK (FuncState *fs, expdesc *e) {
|
|
|
if (luaK_exp2K(fs, e))
|
|
|
return 1;
|
|
|
else { /* not a constant in the right range: put it in a register */
|
|
@@ -1037,7 +1037,7 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) {
|
|
|
|
|
|
static void codeABRK (FuncState *fs, OpCode o, int a, int b,
|
|
|
expdesc *ec) {
|
|
|
- int k = luaK_exp2RK(fs, ec);
|
|
|
+ int k = exp2RK(fs, ec);
|
|
|
luaK_codeABCk(fs, o, a, b, ec->u.info, k);
|
|
|
}
|
|
|
|
|
@@ -1215,7 +1215,7 @@ static void codenot (FuncState *fs, expdesc *e) {
|
|
|
|
|
|
|
|
|
/*
|
|
|
-** Check whether expression 'e' is a small literal string
|
|
|
+** Check whether expression 'e' is a short literal string
|
|
|
*/
|
|
|
static int isKstr (FuncState *fs, expdesc *e) {
|
|
|
return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B &&
|
|
@@ -1225,7 +1225,7 @@ static int isKstr (FuncState *fs, expdesc *e) {
|
|
|
/*
|
|
|
** Check whether expression 'e' is a literal integer.
|
|
|
*/
|
|
|
-int luaK_isKint (expdesc *e) {
|
|
|
+static int isKint (expdesc *e) {
|
|
|
return (e->k == VKINT && !hasjumps(e));
|
|
|
}
|
|
|
|
|
@@ -1235,7 +1235,7 @@ int luaK_isKint (expdesc *e) {
|
|
|
** proper range to fit in register C
|
|
|
*/
|
|
|
static int isCint (expdesc *e) {
|
|
|
- return luaK_isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
|
|
|
+ return isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
|
|
|
}
|
|
|
|
|
|
|
|
@@ -1244,7 +1244,7 @@ static int isCint (expdesc *e) {
|
|
|
** proper range to fit in register sC
|
|
|
*/
|
|
|
static int isSCint (expdesc *e) {
|
|
|
- return luaK_isKint(e) && fitsC(e->u.ival);
|
|
|
+ return isKint(e) && fitsC(e->u.ival);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -1283,15 +1283,16 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
|
|
|
if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */
|
|
|
luaK_exp2anyreg(fs, t); /* put it in a register */
|
|
|
if (t->k == VUPVAL) {
|
|
|
+ lua_assert(isKstr(fs, k));
|
|
|
t->u.ind.t = t->u.info; /* upvalue index */
|
|
|
- t->u.ind.idx = k->u.info; /* literal string */
|
|
|
+ t->u.ind.idx = k->u.info; /* literal short string */
|
|
|
t->k = VINDEXUP;
|
|
|
}
|
|
|
else {
|
|
|
/* register index of the table */
|
|
|
t->u.ind.t = (t->k == VLOCAL) ? t->u.var.ridx: t->u.info;
|
|
|
if (isKstr(fs, k)) {
|
|
|
- t->u.ind.idx = k->u.info; /* literal string */
|
|
|
+ t->u.ind.idx = k->u.info; /* literal short string */
|
|
|
t->k = VINDEXSTR;
|
|
|
}
|
|
|
else if (isCint(k)) {
|
|
@@ -1351,6 +1352,35 @@ static int constfolding (FuncState *fs, int op, expdesc *e1,
|
|
|
}
|
|
|
|
|
|
|
|
|
+/*
|
|
|
+** Convert a BinOpr to an OpCode (ORDER OPR - ORDER OP)
|
|
|
+*/
|
|
|
+l_sinline OpCode binopr2op (BinOpr opr, BinOpr baser, OpCode base) {
|
|
|
+ lua_assert(baser <= opr &&
|
|
|
+ ((baser == OPR_ADD && opr <= OPR_SHR) ||
|
|
|
+ (baser == OPR_LT && opr <= OPR_LE)));
|
|
|
+ return cast(OpCode, (cast_int(opr) - cast_int(baser)) + cast_int(base));
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/*
|
|
|
+** Convert a UnOpr to an OpCode (ORDER OPR - ORDER OP)
|
|
|
+*/
|
|
|
+l_sinline OpCode unopr2op (UnOpr opr) {
|
|
|
+ return cast(OpCode, (cast_int(opr) - cast_int(OPR_MINUS)) +
|
|
|
+ cast_int(OP_UNM));
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/*
|
|
|
+** Convert a BinOpr to a tag method (ORDER OPR - ORDER TM)
|
|
|
+*/
|
|
|
+l_sinline TMS binopr2TM (BinOpr opr) {
|
|
|
+ lua_assert(OPR_ADD <= opr && opr <= OPR_SHR);
|
|
|
+ return cast(TMS, (cast_int(opr) - cast_int(OPR_ADD)) + cast_int(TM_ADD));
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
/*
|
|
|
** Emit code for unary expressions that "produce values"
|
|
|
** (everything but 'not').
|
|
@@ -1389,15 +1419,15 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2,
|
|
|
** Emit code for binary expressions that "produce values" over
|
|
|
** two registers.
|
|
|
*/
|
|
|
-static void codebinexpval (FuncState *fs, OpCode op,
|
|
|
+static void codebinexpval (FuncState *fs, BinOpr opr,
|
|
|
expdesc *e1, expdesc *e2, int line) {
|
|
|
+ OpCode op = binopr2op(opr, OPR_ADD, OP_ADD);
|
|
|
int v2 = luaK_exp2anyreg(fs, e2); /* make sure 'e2' is in a register */
|
|
|
/* 'e1' must be already in a register or it is a constant */
|
|
|
lua_assert((VNIL <= e1->k && e1->k <= VKSTR) ||
|
|
|
e1->k == VNONRELOC || e1->k == VRELOC);
|
|
|
lua_assert(OP_ADD <= op && op <= OP_SHR);
|
|
|
- finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN,
|
|
|
- cast(TMS, (op - OP_ADD) + TM_ADD));
|
|
|
+ finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, binopr2TM(opr));
|
|
|
}
|
|
|
|
|
|
|
|
@@ -1418,9 +1448,9 @@ static void codebini (FuncState *fs, OpCode op,
|
|
|
*/
|
|
|
static void codebinK (FuncState *fs, BinOpr opr,
|
|
|
expdesc *e1, expdesc *e2, int flip, int line) {
|
|
|
- TMS event = cast(TMS, opr + TM_ADD);
|
|
|
+ TMS event = binopr2TM(opr);
|
|
|
int v2 = e2->u.info; /* K index */
|
|
|
- OpCode op = cast(OpCode, opr + OP_ADDK);
|
|
|
+ OpCode op = binopr2op(opr, OPR_ADD, OP_ADDK);
|
|
|
finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event);
|
|
|
}
|
|
|
|
|
@@ -1430,7 +1460,7 @@ static void codebinK (FuncState *fs, BinOpr opr,
|
|
|
*/
|
|
|
static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2,
|
|
|
OpCode op, int line, TMS event) {
|
|
|
- if (!luaK_isKint(e2))
|
|
|
+ if (!isKint(e2))
|
|
|
return 0; /* not an integer constant */
|
|
|
else {
|
|
|
lua_Integer i2 = e2->u.ival;
|
|
@@ -1457,10 +1487,9 @@ static void swapexps (expdesc *e1, expdesc *e2) {
|
|
|
*/
|
|
|
static void codebinNoK (FuncState *fs, BinOpr opr,
|
|
|
expdesc *e1, expdesc *e2, int flip, int line) {
|
|
|
- OpCode op = cast(OpCode, opr + OP_ADD);
|
|
|
if (flip)
|
|
|
swapexps(e1, e2); /* back to original order */
|
|
|
- codebinexpval(fs, op, e1, e2, line); /* use standard operators */
|
|
|
+ codebinexpval(fs, opr, e1, e2, line); /* use standard operators */
|
|
|
}
|
|
|
|
|
|
|
|
@@ -1490,7 +1519,7 @@ static void codecommutative (FuncState *fs, BinOpr op,
|
|
|
flip = 1;
|
|
|
}
|
|
|
if (op == OPR_ADD && isSCint(e2)) /* immediate operand? */
|
|
|
- codebini(fs, cast(OpCode, OP_ADDI), e1, e2, flip, line, TM_ADD);
|
|
|
+ codebini(fs, OP_ADDI, e1, e2, flip, line, TM_ADD);
|
|
|
else
|
|
|
codearith(fs, op, e1, e2, flip, line);
|
|
|
}
|
|
@@ -1518,25 +1547,27 @@ static void codebitwise (FuncState *fs, BinOpr opr,
|
|
|
** Emit code for order comparisons. When using an immediate operand,
|
|
|
** 'isfloat' tells whether the original value was a float.
|
|
|
*/
|
|
|
-static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
|
|
|
+static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
|
|
|
int r1, r2;
|
|
|
int im;
|
|
|
int isfloat = 0;
|
|
|
+ OpCode op;
|
|
|
if (isSCnumber(e2, &im, &isfloat)) {
|
|
|
/* use immediate operand */
|
|
|
r1 = luaK_exp2anyreg(fs, e1);
|
|
|
r2 = im;
|
|
|
- op = cast(OpCode, (op - OP_LT) + OP_LTI);
|
|
|
+ op = binopr2op(opr, OPR_LT, OP_LTI);
|
|
|
}
|
|
|
else if (isSCnumber(e1, &im, &isfloat)) {
|
|
|
/* transform (A < B) to (B > A) and (A <= B) to (B >= A) */
|
|
|
r1 = luaK_exp2anyreg(fs, e2);
|
|
|
r2 = im;
|
|
|
- op = (op == OP_LT) ? OP_GTI : OP_GEI;
|
|
|
+ op = binopr2op(opr, OPR_LT, OP_GTI);
|
|
|
}
|
|
|
else { /* regular case, compare two registers */
|
|
|
r1 = luaK_exp2anyreg(fs, e1);
|
|
|
r2 = luaK_exp2anyreg(fs, e2);
|
|
|
+ op = binopr2op(opr, OPR_LT, OP_LT);
|
|
|
}
|
|
|
freeexps(fs, e1, e2);
|
|
|
e1->u.info = condjump(fs, op, r1, r2, isfloat, 1);
|
|
@@ -1562,7 +1593,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
|
|
|
op = OP_EQI;
|
|
|
r2 = im; /* immediate operand */
|
|
|
}
|
|
|
- else if (luaK_exp2RK(fs, e2)) { /* 2nd expression is constant? */
|
|
|
+ else if (exp2RK(fs, e2)) { /* 2nd expression is constant? */
|
|
|
op = OP_EQK;
|
|
|
r2 = e2->u.info; /* constant index */
|
|
|
}
|
|
@@ -1579,16 +1610,16 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
|
|
|
/*
|
|
|
** Apply prefix operation 'op' to expression 'e'.
|
|
|
*/
|
|
|
-void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
|
|
|
+void luaK_prefix (FuncState *fs, UnOpr opr, expdesc *e, int line) {
|
|
|
static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP};
|
|
|
luaK_dischargevars(fs, e);
|
|
|
- switch (op) {
|
|
|
+ switch (opr) {
|
|
|
case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */
|
|
|
- if (constfolding(fs, op + LUA_OPUNM, e, &ef))
|
|
|
+ if (constfolding(fs, opr + LUA_OPUNM, e, &ef))
|
|
|
break;
|
|
|
/* else */ /* FALLTHROUGH */
|
|
|
case OPR_LEN:
|
|
|
- codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line);
|
|
|
+ codeunexpval(fs, unopr2op(opr), e, line);
|
|
|
break;
|
|
|
case OPR_NOT: codenot(fs, e); break;
|
|
|
default: lua_assert(0);
|
|
@@ -1628,7 +1659,7 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
|
|
|
}
|
|
|
case OPR_EQ: case OPR_NE: {
|
|
|
if (!tonumeral(v, NULL))
|
|
|
- luaK_exp2RK(fs, v);
|
|
|
+ exp2RK(fs, v);
|
|
|
/* else keep numeral, which may be an immediate operand */
|
|
|
break;
|
|
|
}
|
|
@@ -1718,30 +1749,27 @@ void luaK_posfix (FuncState *fs, BinOpr opr,
|
|
|
/* coded as (r1 >> -I) */;
|
|
|
}
|
|
|
else /* regular case (two registers) */
|
|
|
- codebinexpval(fs, OP_SHL, e1, e2, line);
|
|
|
+ codebinexpval(fs, opr, e1, e2, line);
|
|
|
break;
|
|
|
}
|
|
|
case OPR_SHR: {
|
|
|
if (isSCint(e2))
|
|
|
codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); /* r1 >> I */
|
|
|
else /* regular case (two registers) */
|
|
|
- codebinexpval(fs, OP_SHR, e1, e2, line);
|
|
|
+ codebinexpval(fs, opr, e1, e2, line);
|
|
|
break;
|
|
|
}
|
|
|
case OPR_EQ: case OPR_NE: {
|
|
|
codeeq(fs, opr, e1, e2);
|
|
|
break;
|
|
|
}
|
|
|
- case OPR_LT: case OPR_LE: {
|
|
|
- OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ);
|
|
|
- codeorder(fs, op, e1, e2);
|
|
|
- break;
|
|
|
- }
|
|
|
case OPR_GT: case OPR_GE: {
|
|
|
/* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */
|
|
|
- OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ);
|
|
|
swapexps(e1, e2);
|
|
|
- codeorder(fs, op, e1, e2);
|
|
|
+ opr = cast(BinOpr, (opr - OPR_GT) + OPR_LT);
|
|
|
+ } /* FALLTHROUGH */
|
|
|
+ case OPR_LT: case OPR_LE: {
|
|
|
+ codeorder(fs, opr, e1, e2);
|
|
|
break;
|
|
|
}
|
|
|
default: lua_assert(0);
|