aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-03-27 16:29:39 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-03-27 16:29:39 -0300
commit7ca8105a2ea7b6a0d7b55b59d273ccd271c35268 (patch)
tree5fd0116b5e55859844c15b42f77ef3b7db89db80
parent94689ac3ad290caf3bada21c389a991f55391987 (diff)
downloadlua-7ca8105a2ea7b6a0d7b55b59d273ccd271c35268.tar.gz
lua-7ca8105a2ea7b6a0d7b55b59d273ccd271c35268.tar.bz2
lua-7ca8105a2ea7b6a0d7b55b59d273ccd271c35268.zip
More orderliness in casts of enumerations
-rw-r--r--lcode.c55
-rw-r--r--ldebug.c29
2 files changed, 57 insertions, 27 deletions
diff --git a/lcode.c b/lcode.c
index 236ce05c..1a371ca9 100644
--- a/lcode.c
+++ b/lcode.c
@@ -1352,6 +1352,35 @@ static int constfolding (FuncState *fs, int op, expdesc *e1,
1352 1352
1353 1353
1354/* 1354/*
1355** Convert a BinOpr to an OpCode (ORDER OPR - ORDER OP)
1356*/
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));
1362}
1363
1364
1365/*
1366** Convert a UnOpr to an OpCode (ORDER OPR - ORDER OP)
1367*/
1368l_sinline OpCode unopr2op (UnOpr opr) {
1369 return cast(OpCode, (cast_int(opr) - cast_int(OPR_MINUS)) +
1370 cast_int(OP_UNM));
1371}
1372
1373
1374/*
1375** Convert a BinOpr to a tag method (ORDER OPR - ORDER TM)
1376*/
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));
1380}
1381
1382
1383/*
1355** Emit code for unary expressions that "produce values" 1384** Emit code for unary expressions that "produce values"
1356** (everything but 'not'). 1385** (everything but 'not').
1357** Expression to produce final result will be encoded in 'e'. 1386** Expression to produce final result will be encoded in 'e'.
@@ -1391,14 +1420,13 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2,
1391*/ 1420*/
1392static void codebinexpval (FuncState *fs, BinOpr opr, 1421static void codebinexpval (FuncState *fs, BinOpr opr,
1393 expdesc *e1, expdesc *e2, int line) { 1422 expdesc *e1, expdesc *e2, int line) {
1394 OpCode op = cast(OpCode, opr + OP_ADD); 1423 OpCode op = binopr2op(opr, OPR_ADD, OP_ADD);
1395 int v2 = luaK_exp2anyreg(fs, e2); /* make sure 'e2' is in a register */ 1424 int v2 = luaK_exp2anyreg(fs, e2); /* make sure 'e2' is in a register */
1396 /* 'e1' must be already in a register or it is a constant */ 1425 /* 'e1' must be already in a register or it is a constant */
1397 lua_assert((VNIL <= e1->k && e1->k <= VKSTR) || 1426 lua_assert((VNIL <= e1->k && e1->k <= VKSTR) ||
1398 e1->k == VNONRELOC || e1->k == VRELOC); 1427 e1->k == VNONRELOC || e1->k == VRELOC);
1399 lua_assert(OP_ADD <= op && op <= OP_SHR); 1428 lua_assert(OP_ADD <= op && op <= OP_SHR);
1400 finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, 1429 finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, binopr2TM(opr));
1401 cast(TMS, opr + TM_ADD));
1402} 1430}
1403 1431
1404 1432
@@ -1419,9 +1447,9 @@ static void codebini (FuncState *fs, OpCode op,
1419*/ 1447*/
1420static void codebinK (FuncState *fs, BinOpr opr, 1448static void codebinK (FuncState *fs, BinOpr opr,
1421 expdesc *e1, expdesc *e2, int flip, int line) { 1449 expdesc *e1, expdesc *e2, int flip, int line) {
1422 TMS event = cast(TMS, opr + TM_ADD); 1450 TMS event = binopr2TM(opr);
1423 int v2 = e2->u.info; /* K index */ 1451 int v2 = e2->u.info; /* K index */
1424 OpCode op = cast(OpCode, opr + OP_ADDK); 1452 OpCode op = binopr2op(opr, OPR_ADD, OP_ADDK);
1425 finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event); 1453 finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event);
1426} 1454}
1427 1455
@@ -1527,18 +1555,18 @@ static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
1527 /* use immediate operand */ 1555 /* use immediate operand */
1528 r1 = luaK_exp2anyreg(fs, e1); 1556 r1 = luaK_exp2anyreg(fs, e1);
1529 r2 = im; 1557 r2 = im;
1530 op = cast(OpCode, (opr - OPR_LT) + OP_LTI); 1558 op = binopr2op(opr, OPR_LT, OP_LTI);
1531 } 1559 }
1532 else if (isSCnumber(e1, &im, &isfloat)) { 1560 else if (isSCnumber(e1, &im, &isfloat)) {
1533 /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */ 1561 /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */
1534 r1 = luaK_exp2anyreg(fs, e2); 1562 r1 = luaK_exp2anyreg(fs, e2);
1535 r2 = im; 1563 r2 = im;
1536 op = cast(OpCode, (opr - OPR_LT) + OP_GTI); 1564 op = binopr2op(opr, OPR_LT, OP_GTI);
1537 } 1565 }
1538 else { /* regular case, compare two registers */ 1566 else { /* regular case, compare two registers */
1539 r1 = luaK_exp2anyreg(fs, e1); 1567 r1 = luaK_exp2anyreg(fs, e1);
1540 r2 = luaK_exp2anyreg(fs, e2); 1568 r2 = luaK_exp2anyreg(fs, e2);
1541 op = cast(OpCode, (opr - OPR_EQ) + OP_EQ); 1569 op = binopr2op(opr, OPR_LT, OP_LT);
1542 } 1570 }
1543 freeexps(fs, e1, e2); 1571 freeexps(fs, e1, e2);
1544 e1->u.info = condjump(fs, op, r1, r2, isfloat, 1); 1572 e1->u.info = condjump(fs, op, r1, r2, isfloat, 1);
@@ -1590,7 +1618,7 @@ void luaK_prefix (FuncState *fs, UnOpr opr, expdesc *e, int line) {
1590 break; 1618 break;
1591 /* else */ /* FALLTHROUGH */ 1619 /* else */ /* FALLTHROUGH */
1592 case OPR_LEN: 1620 case OPR_LEN:
1593 codeunexpval(fs, cast(OpCode, opr + OP_UNM), e, line); 1621 codeunexpval(fs, unopr2op(opr), e, line);
1594 break; 1622 break;
1595 case OPR_NOT: codenot(fs, e); break; 1623 case OPR_NOT: codenot(fs, e); break;
1596 default: lua_assert(0); 1624 default: lua_assert(0);
@@ -1734,14 +1762,13 @@ void luaK_posfix (FuncState *fs, BinOpr opr,
1734 codeeq(fs, opr, e1, e2); 1762 codeeq(fs, opr, e1, e2);
1735 break; 1763 break;
1736 } 1764 }
1737 case OPR_LT: case OPR_LE: {
1738 codeorder(fs, opr, e1, e2);
1739 break;
1740 }
1741 case OPR_GT: case OPR_GE: { 1765 case OPR_GT: case OPR_GE: {
1742 /* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */ 1766 /* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */
1743 swapexps(e1, e2); 1767 swapexps(e1, e2);
1744 codeorder(fs, (opr - OPR_NE) + OPR_EQ, e1, e2); 1768 opr = cast(BinOpr, (opr - OPR_GT) + OPR_LT);
1769 } /* FALLTHROUGH */
1770 case OPR_LT: case OPR_LE: {
1771 codeorder(fs, opr, e1, e2);
1745 break; 1772 break;
1746 } 1773 }
1747 default: lua_assert(0); 1774 default: lua_assert(0);
diff --git a/ldebug.c b/ldebug.c
index 3fae5cf2..7a61a780 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -656,18 +656,19 @@ static const char *funcnamefromcall (lua_State *L, CallInfo *ci,
656 656
657 657
658/* 658/*
659** Check whether pointer 'o' points to some value in the stack 659** Check whether pointer 'o' points to some value in the stack frame of
660** frame of the current function. Because 'o' may not point to a 660** the current function and, if so, returns its index. Because 'o' may
661** value in this stack, we cannot compare it with the region 661** not point to a value in this stack, we cannot compare it with the
662** boundaries (undefined behaviour in ISO C). 662** region boundaries (undefined behaviour in ISO C).
663*/ 663*/
664static int isinstack (CallInfo *ci, const TValue *o) { 664static int instack (CallInfo *ci, const TValue *o) {
665 StkId pos; 665 int pos;
666 for (pos = ci->func.p + 1; pos < ci->top.p; pos++) { 666 StkId base = ci->func.p + 1;
667 if (o == s2v(pos)) 667 for (pos = 0; base + pos < ci->top.p; pos++) {
668 return 1; 668 if (o == s2v(base + pos))
669 return pos;
669 } 670 }
670 return 0; /* not found */ 671 return -1; /* not found */
671} 672}
672 673
673 674
@@ -708,9 +709,11 @@ static const char *varinfo (lua_State *L, const TValue *o) {
708 const char *kind = NULL; 709 const char *kind = NULL;
709 if (isLua(ci)) { 710 if (isLua(ci)) {
710 kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ 711 kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
711 if (!kind && isinstack(ci, o)) /* no? try a register */ 712 if (!kind) { /* not an upvalue? */
712 kind = getobjname(ci_func(ci)->p, currentpc(ci), 713 int reg = instack(ci, o); /* try a register */
713 cast_int(cast(StkId, o) - (ci->func.p + 1)), &name); 714 if (reg >= 0) /* is 'o' a register? */
715 kind = getobjname(ci_func(ci)->p, currentpc(ci), reg, &name);
716 }
714 } 717 }
715 return formatvarinfo(L, kind, name); 718 return formatvarinfo(L, kind, name);
716} 719}