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 | |
parent | 295cde94545b00afc8624bd388db805504d356bd (diff) | |
download | lua-c764ca71a639f5585b5f466bea25dc42b855a4b0.tar.gz lua-c764ca71a639f5585b5f466bea25dc42b855a4b0.tar.bz2 lua-c764ca71a639f5585b5f466bea25dc42b855a4b0.zip |
Bug: Wrong code generation in bitwise operations
-rw-r--r-- | lcode.c | 16 | ||||
-rw-r--r-- | ltests.h | 7 | ||||
-rw-r--r-- | testes/constructs.lua | 25 |
3 files changed, 42 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: { |
@@ -125,6 +125,13 @@ LUA_API void *debug_realloc (void *ud, void *block, | |||
125 | #define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; } | 125 | #define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; } |
126 | 126 | ||
127 | 127 | ||
128 | /* | ||
129 | ** This one is not compatible with tests for opcode optimizations, | ||
130 | ** as it blocks some optimizations | ||
131 | #define MAXINDEXRK 0 | ||
132 | */ | ||
133 | |||
134 | |||
128 | /* make stack-overflow tests run faster */ | 135 | /* make stack-overflow tests run faster */ |
129 | #undef LUAI_MAXSTACK | 136 | #undef LUAI_MAXSTACK |
130 | #define LUAI_MAXSTACK 50000 | 137 | #define LUAI_MAXSTACK 50000 |
diff --git a/testes/constructs.lua b/testes/constructs.lua index a74a8c04..0d9ec92d 100644 --- a/testes/constructs.lua +++ b/testes/constructs.lua | |||
@@ -103,6 +103,31 @@ do -- test old bug (first name could not be an `upvalue') | |||
103 | local a; function f(x) x={a=1}; x={x=1}; x={G=1} end | 103 | local a; function f(x) x={a=1}; x={x=1}; x={G=1} end |
104 | end | 104 | end |
105 | 105 | ||
106 | |||
107 | do -- bug since 5.4.0 | ||
108 | -- create code with a table using more than 256 constants | ||
109 | local code = {"local x = {"} | ||
110 | for i = 1, 257 do | ||
111 | code[#code + 1] = i .. ".1," | ||
112 | end | ||
113 | code[#code + 1] = "};" | ||
114 | code = table.concat(code) | ||
115 | |||
116 | -- add "ret" to the end of that code and checks that | ||
117 | -- it produces the expected value "val" | ||
118 | local function check (ret, val) | ||
119 | local code = code .. ret | ||
120 | code = load(code) | ||
121 | assert(code() == val) | ||
122 | end | ||
123 | |||
124 | check("return (1 ~ (2 or 3))", 1 ~ 2) | ||
125 | check("return (1 | (2 or 3))", 1 | 2) | ||
126 | check("return (1 + (2 or 3))", 1 + 2) | ||
127 | check("return (1 << (2 or 3))", 1 << 2) | ||
128 | end | ||
129 | |||
130 | |||
106 | function f (i) | 131 | function f (i) |
107 | if type(i) ~= 'number' then return i,'jojo'; end; | 132 | if type(i) ~= 'number' then return i,'jojo'; end; |
108 | if i > 0 then return i, f(i-1); end; | 133 | if i > 0 then return i, f(i-1); end; |