From e9642ef7766e85271c09def0d4c495a39bc58a5f Mon Sep 17 00:00:00 2001 From: Rerumu Date: Sat, 27 Nov 2021 00:43:25 -0500 Subject: [PATCH] Fix wrapping integer operations --- runtime/luajit.lua | 19 +++++++++++++++++-- src/backend/ast/operation.rs | 6 +++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/runtime/luajit.lua b/runtime/luajit.lua index 90a9afc..115a61c 100644 --- a/runtime/luajit.lua +++ b/runtime/luajit.lua @@ -5,8 +5,6 @@ local ffi = require('ffi') local module = {} -local to_signed = bit.tobit - local vla_u8 = ffi.typeof('uint8_t[?]') local u32 = ffi.typeof('uint32_t') @@ -31,10 +29,27 @@ local function truncate(num) end do + local add = {} + local sub = {} + local mul = {} local div = {} + local to_signed = bit.tobit + + module.add = add + module.sub = sub + module.mul = mul module.div = div + function add.i32(a, b) return to_signed(a + b) end + function add.i64(a, b) return a + b end + + function sub.i32(a, b) return to_signed(a - b) end + function sub.i64(a, b) return a - b end + + function mul.i32(a, b) return to_signed(a * b) end + function mul.i64(a, b) return a * b end + function div.i32(lhs, rhs) if rhs == 0 then error('division by zero') end diff --git a/src/backend/ast/operation.rs b/src/backend/ast/operation.rs index 3bc1298..10d1f55 100644 --- a/src/backend/ast/operation.rs +++ b/src/backend/ast/operation.rs @@ -345,9 +345,9 @@ pub enum BinOp { impl BinOp { pub fn as_operator(self) -> Option<&'static str> { let op = match self { - Self::Add_I32 | Self::Add_I64 | Self::Add_FN => "+", - Self::Sub_I32 | Self::Sub_I64 | Self::Sub_FN => "-", - Self::Mul_I32 | Self::Mul_I64 | Self::Mul_FN => "*", + Self::Add_FN => "+", + Self::Sub_FN => "-", + Self::Mul_FN => "*", Self::Div_FN => "/", Self::RemS_I32 | Self::RemU_I32 | Self::RemS_I64 | Self::RemU_I64 => "%", _ => return None,