diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-03-27 16:29:39 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-03-27 16:29:39 -0300 |
commit | 7ca8105a2ea7b6a0d7b55b59d273ccd271c35268 (patch) | |
tree | 5fd0116b5e55859844c15b42f77ef3b7db89db80 /lcode.c | |
parent | 94689ac3ad290caf3bada21c389a991f55391987 (diff) | |
download | lua-7ca8105a2ea7b6a0d7b55b59d273ccd271c35268.tar.gz lua-7ca8105a2ea7b6a0d7b55b59d273ccd271c35268.tar.bz2 lua-7ca8105a2ea7b6a0d7b55b59d273ccd271c35268.zip |
More orderliness in casts of enumerations
Diffstat (limited to 'lcode.c')
-rw-r--r-- | lcode.c | 55 |
1 files changed, 41 insertions, 14 deletions
@@ -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 | */ | ||
1357 | l_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 | */ | ||
1368 | l_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 | */ | ||
1377 | l_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 | */ |
1392 | static void codebinexpval (FuncState *fs, BinOpr opr, | 1421 | static 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 | */ |
1420 | static void codebinK (FuncState *fs, BinOpr opr, | 1448 | static 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); |