Skip to content

Commit e4e8252

Browse files
committed
Merge pull request #18 from q66/master
fix bytecode generator error with nan constant expressions (0/0)
2 parents c20b24a + c4e78b3 commit e4e8252

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lang/ast-const-eval.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ local function dirop_compute(o, a, b)
99
if o == '+' then return a + b
1010
elseif o == '-' then return a - b
1111
elseif o == '*' then return a * b
12-
elseif o == '/' then return a / b
12+
elseif o == '/' then return (a ~= 0 or b ~= 0) and (a / b) or nil
1313
elseif o == '%' then return a % b
1414
elseif o == '^' then return a ^ b
1515
end

lang/generator.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,21 @@ function ExpressionRule:BinaryExpression(node, dest, jreg)
289289
elseif dirop[o] then
290290
local atag, a = self:expr_toanyreg_tagged(node.left, EXPR_EMIT_VN)
291291
local btag, b = self:expr_toanyreg_tagged(node.right, EXPR_EMIT_VN)
292+
if atag == "N" and btag == "N" then
293+
-- handle "nan" values here the same way LuaJIT does
294+
-- usually, both operands will always be 0 when both constant but
295+
-- re-check just to make sure, in order to trigger the assert when
296+
-- there's a bug in the generator
297+
local aval = const_eval(node.left)
298+
local bval = const_eval(node.right)
299+
if aval == 0 and bval == 0 then
300+
atag, a = "V", self.ctx.freereg
301+
self.ctx:op_load(self.ctx:nextreg(), 0)
302+
else
303+
assert(false, "operands are both constants")
304+
end
305+
end
292306
self.ctx.freereg = free
293-
assert(not (atag == 'N' and btag == 'N'), "operands are both constants")
294307
self.ctx:op_infix(dirop[o], dest, atag, a, btag, b)
295308
else
296309
local a = self:expr_toanyreg(node.left)

0 commit comments

Comments
 (0)