diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-04-25 14:42:51 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-04-25 14:42:51 -0300 |
commit | c764ca71a639f5585b5f466bea25dc42b855a4b0 (patch) | |
tree | 0caf66be3a21dbba591d2d16cdf201e83f37d7b0 /lcode.c | |
parent | 295cde94545b00afc8624bd388db805504d356bd (diff) | |
download | lua-c764ca71a639f5585b5f466bea25dc42b855a4b0.tar.gz lua-c764ca71a639f5585b5f466bea25dc42b855a4b0.tar.bz2 lua-c764ca71a639f5585b5f466bea25dc42b855a4b0.zip |
Bug: Wrong code generation in bitwise operations
Diffstat (limited to 'lcode.c')
-rw-r--r-- | lcode.c | 16 |
1 files changed, 10 insertions, 6 deletions
@@ -1391,7 +1391,10 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, | |||
1391 | */ | 1391 | */ |
1392 | static void codebinexpval (FuncState *fs, OpCode op, | 1392 | static void codebinexpval (FuncState *fs, OpCode op, |
1393 | expdesc *e1, expdesc *e2, int line) { | 1393 | expdesc *e1, expdesc *e2, int line) { |
1394 | int v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */ | 1394 | 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 */ | ||
1396 | lua_assert((VNIL <= e1->k && e1->k <= VKSTR) || | ||
1397 | e1->k == VNONRELOC || e1->k == VRELOC); | ||
1395 | lua_assert(OP_ADD <= op && op <= OP_SHR); | 1398 | lua_assert(OP_ADD <= op && op <= OP_SHR); |
1396 | finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, | 1399 | finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, |
1397 | cast(TMS, (op - OP_ADD) + TM_ADD)); | 1400 | cast(TMS, (op - OP_ADD) + TM_ADD)); |
@@ -1478,7 +1481,7 @@ static void codecommutative (FuncState *fs, BinOpr op, | |||
1478 | 1481 | ||
1479 | 1482 | ||
1480 | /* | 1483 | /* |
1481 | ** Code bitwise operations; they are all associative, so the function | 1484 | ** Code bitwise operations; they are all commutative, so the function |
1482 | ** tries to put an integer constant as the 2nd operand (a K operand). | 1485 | ** tries to put an integer constant as the 2nd operand (a K operand). |
1483 | */ | 1486 | */ |
1484 | static void codebitwise (FuncState *fs, BinOpr opr, | 1487 | static void codebitwise (FuncState *fs, BinOpr opr, |
@@ -1486,11 +1489,11 @@ static void codebitwise (FuncState *fs, BinOpr opr, | |||
1486 | int flip = 0; | 1489 | int flip = 0; |
1487 | int v2; | 1490 | int v2; |
1488 | OpCode op; | 1491 | OpCode op; |
1489 | if (e1->k == VKINT && luaK_exp2RK(fs, e1)) { | 1492 | if (e1->k == VKINT && luaK_exp2K(fs, e1)) { |
1490 | swapexps(e1, e2); /* 'e2' will be the constant operand */ | 1493 | swapexps(e1, e2); /* 'e2' will be the constant operand */ |
1491 | flip = 1; | 1494 | flip = 1; |
1492 | } | 1495 | } |
1493 | else if (!(e2->k == VKINT && luaK_exp2RK(fs, e2))) { /* no constants? */ | 1496 | else if (!(e2->k == VKINT && luaK_exp2K(fs, e2))) { /* no constants? */ |
1494 | op = cast(OpCode, opr + OP_ADD); | 1497 | op = cast(OpCode, opr + OP_ADD); |
1495 | codebinexpval(fs, op, e1, e2, line); /* all-register opcodes */ | 1498 | codebinexpval(fs, op, e1, e2, line); /* all-register opcodes */ |
1496 | return; | 1499 | return; |
@@ -1551,7 +1554,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { | |||
1551 | op = OP_EQI; | 1554 | op = OP_EQI; |
1552 | r2 = im; /* immediate operand */ | 1555 | r2 = im; /* immediate operand */ |
1553 | } | 1556 | } |
1554 | else if (luaK_exp2RK(fs, e2)) { /* 1st expression is constant? */ | 1557 | else if (luaK_exp2RK(fs, e2)) { /* 2nd expression is constant? */ |
1555 | op = OP_EQK; | 1558 | op = OP_EQK; |
1556 | r2 = e2->u.info; /* constant index */ | 1559 | r2 = e2->u.info; /* constant index */ |
1557 | } | 1560 | } |
@@ -1611,7 +1614,8 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { | |||
1611 | case OPR_SHL: case OPR_SHR: { | 1614 | case OPR_SHL: case OPR_SHR: { |
1612 | if (!tonumeral(v, NULL)) | 1615 | if (!tonumeral(v, NULL)) |
1613 | luaK_exp2anyreg(fs, v); | 1616 | luaK_exp2anyreg(fs, v); |
1614 | /* else keep numeral, which may be folded with 2nd operand */ | 1617 | /* else keep numeral, which may be folded or used as an immediate |
1618 | operand */ | ||
1615 | break; | 1619 | break; |
1616 | } | 1620 | } |
1617 | case OPR_EQ: case OPR_NE: { | 1621 | case OPR_EQ: case OPR_NE: { |