aboutsummaryrefslogtreecommitdiff
path: root/lcode.c
diff options
context:
space:
mode:
Diffstat (limited to 'lcode.c')
-rw-r--r--lcode.c104
1 files changed, 66 insertions, 38 deletions
diff --git a/lcode.c b/lcode.c
index 911dbd5f..caac6ba3 100644
--- a/lcode.c
+++ b/lcode.c
@@ -415,7 +415,7 @@ int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
415/* 415/*
416** Format and emit an 'iAsBx' instruction. 416** Format and emit an 'iAsBx' instruction.
417*/ 417*/
418int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) { 418static int codeAsBx (FuncState *fs, OpCode o, int a, int bc) {
419 unsigned int b = bc + OFFSET_sBx; 419 unsigned int b = bc + OFFSET_sBx;
420 lua_assert(getOpMode(o) == iAsBx); 420 lua_assert(getOpMode(o) == iAsBx);
421 lua_assert(a <= MAXARG_A && b <= MAXARG_Bx); 421 lua_assert(a <= MAXARG_A && b <= MAXARG_Bx);
@@ -671,7 +671,7 @@ static int fitsBx (lua_Integer i) {
671 671
672void luaK_int (FuncState *fs, int reg, lua_Integer i) { 672void luaK_int (FuncState *fs, int reg, lua_Integer i) {
673 if (fitsBx(i)) 673 if (fitsBx(i))
674 luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i)); 674 codeAsBx(fs, OP_LOADI, reg, cast_int(i));
675 else 675 else
676 luaK_codek(fs, reg, luaK_intK(fs, i)); 676 luaK_codek(fs, reg, luaK_intK(fs, i));
677} 677}
@@ -680,7 +680,7 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) {
680static void luaK_float (FuncState *fs, int reg, lua_Number f) { 680static void luaK_float (FuncState *fs, int reg, lua_Number f) {
681 lua_Integer fi; 681 lua_Integer fi;
682 if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi)) 682 if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi))
683 luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi)); 683 codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
684 else 684 else
685 luaK_codek(fs, reg, luaK_numberK(fs, f)); 685 luaK_codek(fs, reg, luaK_numberK(fs, f));
686} 686}
@@ -1025,7 +1025,7 @@ static int luaK_exp2K (FuncState *fs, expdesc *e) {
1025** in the range of R/K indices). 1025** in the range of R/K indices).
1026** Returns 1 iff expression is K. 1026** Returns 1 iff expression is K.
1027*/ 1027*/
1028int luaK_exp2RK (FuncState *fs, expdesc *e) { 1028static int exp2RK (FuncState *fs, expdesc *e) {
1029 if (luaK_exp2K(fs, e)) 1029 if (luaK_exp2K(fs, e))
1030 return 1; 1030 return 1;
1031 else { /* not a constant in the right range: put it in a register */ 1031 else { /* not a constant in the right range: put it in a register */
@@ -1037,7 +1037,7 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) {
1037 1037
1038static void codeABRK (FuncState *fs, OpCode o, int a, int b, 1038static void codeABRK (FuncState *fs, OpCode o, int a, int b,
1039 expdesc *ec) { 1039 expdesc *ec) {
1040 int k = luaK_exp2RK(fs, ec); 1040 int k = exp2RK(fs, ec);
1041 luaK_codeABCk(fs, o, a, b, ec->u.info, k); 1041 luaK_codeABCk(fs, o, a, b, ec->u.info, k);
1042} 1042}
1043 1043
@@ -1215,7 +1215,7 @@ static void codenot (FuncState *fs, expdesc *e) {
1215 1215
1216 1216
1217/* 1217/*
1218** Check whether expression 'e' is a small literal string 1218** Check whether expression 'e' is a short literal string
1219*/ 1219*/
1220static int isKstr (FuncState *fs, expdesc *e) { 1220static int isKstr (FuncState *fs, expdesc *e) {
1221 return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B && 1221 return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B &&
@@ -1225,7 +1225,7 @@ static int isKstr (FuncState *fs, expdesc *e) {
1225/* 1225/*
1226** Check whether expression 'e' is a literal integer. 1226** Check whether expression 'e' is a literal integer.
1227*/ 1227*/
1228int luaK_isKint (expdesc *e) { 1228static int isKint (expdesc *e) {
1229 return (e->k == VKINT && !hasjumps(e)); 1229 return (e->k == VKINT && !hasjumps(e));
1230} 1230}
1231 1231
@@ -1235,7 +1235,7 @@ int luaK_isKint (expdesc *e) {
1235** proper range to fit in register C 1235** proper range to fit in register C
1236*/ 1236*/
1237static int isCint (expdesc *e) { 1237static int isCint (expdesc *e) {
1238 return luaK_isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C)); 1238 return isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
1239} 1239}
1240 1240
1241 1241
@@ -1244,7 +1244,7 @@ static int isCint (expdesc *e) {
1244** proper range to fit in register sC 1244** proper range to fit in register sC
1245*/ 1245*/
1246static int isSCint (expdesc *e) { 1246static int isSCint (expdesc *e) {
1247 return luaK_isKint(e) && fitsC(e->u.ival); 1247 return isKint(e) && fitsC(e->u.ival);
1248} 1248}
1249 1249
1250 1250
@@ -1283,15 +1283,16 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
1283 if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */ 1283 if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */
1284 luaK_exp2anyreg(fs, t); /* put it in a register */ 1284 luaK_exp2anyreg(fs, t); /* put it in a register */
1285 if (t->k == VUPVAL) { 1285 if (t->k == VUPVAL) {
1286 lua_assert(isKstr(fs, k));
1286 t->u.ind.t = t->u.info; /* upvalue index */ 1287 t->u.ind.t = t->u.info; /* upvalue index */
1287 t->u.ind.idx = k->u.info; /* literal string */ 1288 t->u.ind.idx = k->u.info; /* literal short string */
1288 t->k = VINDEXUP; 1289 t->k = VINDEXUP;
1289 } 1290 }
1290 else { 1291 else {
1291 /* register index of the table */ 1292 /* register index of the table */
1292 t->u.ind.t = (t->k == VLOCAL) ? t->u.var.ridx: t->u.info; 1293 t->u.ind.t = (t->k == VLOCAL) ? t->u.var.ridx: t->u.info;
1293 if (isKstr(fs, k)) { 1294 if (isKstr(fs, k)) {
1294 t->u.ind.idx = k->u.info; /* literal string */ 1295 t->u.ind.idx = k->u.info; /* literal short string */
1295 t->k = VINDEXSTR; 1296 t->k = VINDEXSTR;
1296 } 1297 }
1297 else if (isCint(k)) { 1298 else if (isCint(k)) {
@@ -1352,6 +1353,35 @@ static int constfolding (FuncState *fs, int op, expdesc *e1,
1352 1353
1353 1354
1354/* 1355/*
1356** Convert a BinOpr to an OpCode (ORDER OPR - ORDER OP)
1357*/
1358l_sinline OpCode binopr2op (BinOpr opr, BinOpr baser, OpCode base) {
1359 lua_assert(baser <= opr &&
1360 ((baser == OPR_ADD && opr <= OPR_SHR) ||
1361 (baser == OPR_LT && opr <= OPR_LE)));
1362 return cast(OpCode, (cast_int(opr) - cast_int(baser)) + cast_int(base));
1363}
1364
1365
1366/*
1367** Convert a UnOpr to an OpCode (ORDER OPR - ORDER OP)
1368*/
1369l_sinline OpCode unopr2op (UnOpr opr) {
1370 return cast(OpCode, (cast_int(opr) - cast_int(OPR_MINUS)) +
1371 cast_int(OP_UNM));
1372}
1373
1374
1375/*
1376** Convert a BinOpr to a tag method (ORDER OPR - ORDER TM)
1377*/
1378l_sinline TMS binopr2TM (BinOpr opr) {
1379 lua_assert(OPR_ADD <= opr && opr <= OPR_SHR);
1380 return cast(TMS, (cast_int(opr) - cast_int(OPR_ADD)) + cast_int(TM_ADD));
1381}
1382
1383
1384/*
1355** Emit code for unary expressions that "produce values" 1385** Emit code for unary expressions that "produce values"
1356** (everything but 'not'). 1386** (everything but 'not').
1357** Expression to produce final result will be encoded in 'e'. 1387** Expression to produce final result will be encoded in 'e'.
@@ -1389,15 +1419,15 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2,
1389** Emit code for binary expressions that "produce values" over 1419** Emit code for binary expressions that "produce values" over
1390** two registers. 1420** two registers.
1391*/ 1421*/
1392static void codebinexpval (FuncState *fs, OpCode op, 1422static void codebinexpval (FuncState *fs, BinOpr opr,
1393 expdesc *e1, expdesc *e2, int line) { 1423 expdesc *e1, expdesc *e2, int line) {
1424 OpCode op = binopr2op(opr, OPR_ADD, OP_ADD);
1394 int v2 = luaK_exp2anyreg(fs, e2); /* make sure 'e2' is in a register */ 1425 int v2 = luaK_exp2anyreg(fs, e2); /* make sure 'e2' is in a register */
1395 /* 'e1' must be already in a register or it is a constant */ 1426 /* 'e1' must be already in a register or it is a constant */
1396 lua_assert((VNIL <= e1->k && e1->k <= VKSTR) || 1427 lua_assert((VNIL <= e1->k && e1->k <= VKSTR) ||
1397 e1->k == VNONRELOC || e1->k == VRELOC); 1428 e1->k == VNONRELOC || e1->k == VRELOC);
1398 lua_assert(OP_ADD <= op && op <= OP_SHR); 1429 lua_assert(OP_ADD <= op && op <= OP_SHR);
1399 finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, 1430 finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, binopr2TM(opr));
1400 cast(TMS, (op - OP_ADD) + TM_ADD));
1401} 1431}
1402 1432
1403 1433
@@ -1418,9 +1448,9 @@ static void codebini (FuncState *fs, OpCode op,
1418*/ 1448*/
1419static void codebinK (FuncState *fs, BinOpr opr, 1449static void codebinK (FuncState *fs, BinOpr opr,
1420 expdesc *e1, expdesc *e2, int flip, int line) { 1450 expdesc *e1, expdesc *e2, int flip, int line) {
1421 TMS event = cast(TMS, opr + TM_ADD); 1451 TMS event = binopr2TM(opr);
1422 int v2 = e2->u.info; /* K index */ 1452 int v2 = e2->u.info; /* K index */
1423 OpCode op = cast(OpCode, opr + OP_ADDK); 1453 OpCode op = binopr2op(opr, OPR_ADD, OP_ADDK);
1424 finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event); 1454 finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event);
1425} 1455}
1426 1456
@@ -1430,7 +1460,7 @@ static void codebinK (FuncState *fs, BinOpr opr,
1430*/ 1460*/
1431static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2, 1461static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2,
1432 OpCode op, int line, TMS event) { 1462 OpCode op, int line, TMS event) {
1433 if (!luaK_isKint(e2)) 1463 if (!isKint(e2))
1434 return 0; /* not an integer constant */ 1464 return 0; /* not an integer constant */
1435 else { 1465 else {
1436 lua_Integer i2 = e2->u.ival; 1466 lua_Integer i2 = e2->u.ival;
@@ -1457,10 +1487,9 @@ static void swapexps (expdesc *e1, expdesc *e2) {
1457*/ 1487*/
1458static void codebinNoK (FuncState *fs, BinOpr opr, 1488static void codebinNoK (FuncState *fs, BinOpr opr,
1459 expdesc *e1, expdesc *e2, int flip, int line) { 1489 expdesc *e1, expdesc *e2, int flip, int line) {
1460 OpCode op = cast(OpCode, opr + OP_ADD);
1461 if (flip) 1490 if (flip)
1462 swapexps(e1, e2); /* back to original order */ 1491 swapexps(e1, e2); /* back to original order */
1463 codebinexpval(fs, op, e1, e2, line); /* use standard operators */ 1492 codebinexpval(fs, opr, e1, e2, line); /* use standard operators */
1464} 1493}
1465 1494
1466 1495
@@ -1490,7 +1519,7 @@ static void codecommutative (FuncState *fs, BinOpr op,
1490 flip = 1; 1519 flip = 1;
1491 } 1520 }
1492 if (op == OPR_ADD && isSCint(e2)) /* immediate operand? */ 1521 if (op == OPR_ADD && isSCint(e2)) /* immediate operand? */
1493 codebini(fs, cast(OpCode, OP_ADDI), e1, e2, flip, line, TM_ADD); 1522 codebini(fs, OP_ADDI, e1, e2, flip, line, TM_ADD);
1494 else 1523 else
1495 codearith(fs, op, e1, e2, flip, line); 1524 codearith(fs, op, e1, e2, flip, line);
1496} 1525}
@@ -1518,25 +1547,27 @@ static void codebitwise (FuncState *fs, BinOpr opr,
1518** Emit code for order comparisons. When using an immediate operand, 1547** Emit code for order comparisons. When using an immediate operand,
1519** 'isfloat' tells whether the original value was a float. 1548** 'isfloat' tells whether the original value was a float.
1520*/ 1549*/
1521static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { 1550static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
1522 int r1, r2; 1551 int r1, r2;
1523 int im; 1552 int im;
1524 int isfloat = 0; 1553 int isfloat = 0;
1554 OpCode op;
1525 if (isSCnumber(e2, &im, &isfloat)) { 1555 if (isSCnumber(e2, &im, &isfloat)) {
1526 /* use immediate operand */ 1556 /* use immediate operand */
1527 r1 = luaK_exp2anyreg(fs, e1); 1557 r1 = luaK_exp2anyreg(fs, e1);
1528 r2 = im; 1558 r2 = im;
1529 op = cast(OpCode, (op - OP_LT) + OP_LTI); 1559 op = binopr2op(opr, OPR_LT, OP_LTI);
1530 } 1560 }
1531 else if (isSCnumber(e1, &im, &isfloat)) { 1561 else if (isSCnumber(e1, &im, &isfloat)) {
1532 /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */ 1562 /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */
1533 r1 = luaK_exp2anyreg(fs, e2); 1563 r1 = luaK_exp2anyreg(fs, e2);
1534 r2 = im; 1564 r2 = im;
1535 op = (op == OP_LT) ? OP_GTI : OP_GEI; 1565 op = binopr2op(opr, OPR_LT, OP_GTI);
1536 } 1566 }
1537 else { /* regular case, compare two registers */ 1567 else { /* regular case, compare two registers */
1538 r1 = luaK_exp2anyreg(fs, e1); 1568 r1 = luaK_exp2anyreg(fs, e1);
1539 r2 = luaK_exp2anyreg(fs, e2); 1569 r2 = luaK_exp2anyreg(fs, e2);
1570 op = binopr2op(opr, OPR_LT, OP_LT);
1540 } 1571 }
1541 freeexps(fs, e1, e2); 1572 freeexps(fs, e1, e2);
1542 e1->u.info = condjump(fs, op, r1, r2, isfloat, 1); 1573 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) {
1562 op = OP_EQI; 1593 op = OP_EQI;
1563 r2 = im; /* immediate operand */ 1594 r2 = im; /* immediate operand */
1564 } 1595 }
1565 else if (luaK_exp2RK(fs, e2)) { /* 2nd expression is constant? */ 1596 else if (exp2RK(fs, e2)) { /* 2nd expression is constant? */
1566 op = OP_EQK; 1597 op = OP_EQK;
1567 r2 = e2->u.info; /* constant index */ 1598 r2 = e2->u.info; /* constant index */
1568 } 1599 }
@@ -1579,16 +1610,16 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
1579/* 1610/*
1580** Apply prefix operation 'op' to expression 'e'. 1611** Apply prefix operation 'op' to expression 'e'.
1581*/ 1612*/
1582void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { 1613void luaK_prefix (FuncState *fs, UnOpr opr, expdesc *e, int line) {
1583 static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP}; 1614 static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP};
1584 luaK_dischargevars(fs, e); 1615 luaK_dischargevars(fs, e);
1585 switch (op) { 1616 switch (opr) {
1586 case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */ 1617 case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */
1587 if (constfolding(fs, op + LUA_OPUNM, e, &ef)) 1618 if (constfolding(fs, opr + LUA_OPUNM, e, &ef))
1588 break; 1619 break;
1589 /* else */ /* FALLTHROUGH */ 1620 /* else */ /* FALLTHROUGH */
1590 case OPR_LEN: 1621 case OPR_LEN:
1591 codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line); 1622 codeunexpval(fs, unopr2op(opr), e, line);
1592 break; 1623 break;
1593 case OPR_NOT: codenot(fs, e); break; 1624 case OPR_NOT: codenot(fs, e); break;
1594 default: lua_assert(0); 1625 default: lua_assert(0);
@@ -1628,7 +1659,7 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
1628 } 1659 }
1629 case OPR_EQ: case OPR_NE: { 1660 case OPR_EQ: case OPR_NE: {
1630 if (!tonumeral(v, NULL)) 1661 if (!tonumeral(v, NULL))
1631 luaK_exp2RK(fs, v); 1662 exp2RK(fs, v);
1632 /* else keep numeral, which may be an immediate operand */ 1663 /* else keep numeral, which may be an immediate operand */
1633 break; 1664 break;
1634 } 1665 }
@@ -1718,30 +1749,27 @@ void luaK_posfix (FuncState *fs, BinOpr opr,
1718 /* coded as (r1 >> -I) */; 1749 /* coded as (r1 >> -I) */;
1719 } 1750 }
1720 else /* regular case (two registers) */ 1751 else /* regular case (two registers) */
1721 codebinexpval(fs, OP_SHL, e1, e2, line); 1752 codebinexpval(fs, opr, e1, e2, line);
1722 break; 1753 break;
1723 } 1754 }
1724 case OPR_SHR: { 1755 case OPR_SHR: {
1725 if (isSCint(e2)) 1756 if (isSCint(e2))
1726 codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); /* r1 >> I */ 1757 codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); /* r1 >> I */
1727 else /* regular case (two registers) */ 1758 else /* regular case (two registers) */
1728 codebinexpval(fs, OP_SHR, e1, e2, line); 1759 codebinexpval(fs, opr, e1, e2, line);
1729 break; 1760 break;
1730 } 1761 }
1731 case OPR_EQ: case OPR_NE: { 1762 case OPR_EQ: case OPR_NE: {
1732 codeeq(fs, opr, e1, e2); 1763 codeeq(fs, opr, e1, e2);
1733 break; 1764 break;
1734 } 1765 }
1735 case OPR_LT: case OPR_LE: {
1736 OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ);
1737 codeorder(fs, op, e1, e2);
1738 break;
1739 }
1740 case OPR_GT: case OPR_GE: { 1766 case OPR_GT: case OPR_GE: {
1741 /* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */ 1767 /* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */
1742 OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ);
1743 swapexps(e1, e2); 1768 swapexps(e1, e2);
1744 codeorder(fs, op, e1, e2); 1769 opr = cast(BinOpr, (opr - OPR_GT) + OPR_LT);
1770 } /* FALLTHROUGH */
1771 case OPR_LT: case OPR_LE: {
1772 codeorder(fs, opr, e1, e2);
1745 break; 1773 break;
1746 } 1774 }
1747 default: lua_assert(0); 1775 default: lua_assert(0);