Start adding integer support for Luau

This commit is contained in:
Rerumu 2022-05-19 00:10:31 -04:00
parent 0366a74d0a
commit d5c9028be4
2 changed files with 248 additions and 140 deletions

View File

@ -1,37 +1,23 @@
local module = {} local module = {}
local math_floor = math.floor local MAX_SIGNED = 0x7fffffff
local math_ceil = math.ceil local BIT_SET_32 = 0x100000000
local bit32 = bit32 local to_u32 = bit32.band
local bit_band = bit32.band
local function no_op(x) local num_from_u32 = I64.from_u32
return x local num_into_u32 = I64.into_u32
end
local function to_u32(x) local function to_i32(num)
return bit_band(x, 0xFFFFFFFF) if num > MAX_SIGNED then
end num = num - BIT_SET_32
local function to_i32(x)
if x > 0x7FFFFFFF then
x = x - 0x100000000
end end
return x return num
end end
local function wrap_i32(x) local function no_op(num)
return to_i32(to_u32(x)) return num
end
local function truncate(num)
if num >= 0 then
return math_floor(num)
else
return math_ceil(num)
end
end end
do do
@ -40,33 +26,45 @@ do
local mul = {} local mul = {}
local div = {} local div = {}
local assert = assert
function add.i32(a, b) function add.i32(a, b)
return wrap_i32(a + b) return to_u32(a + b)
end end
add.i64 = I64.add
function sub.i32(a, b) function sub.i32(a, b)
return wrap_i32(a - b) return to_u32(a - b)
end end
sub.i64 = I64.subtract
function mul.i32(a, b) function mul.i32(a, b)
return wrap_i32(a * b) return to_u32(a * b)
end end
mul.i64 = I64.multiply
function div.i32(lhs, rhs) function div.i32(lhs, rhs)
assert(rhs ~= 0, "division by zero") assert(rhs ~= 0, "division by zero")
return truncate(lhs / rhs) lhs = to_i32(lhs)
rhs = to_i32(rhs)
return to_u32(lhs / rhs)
end end
div.i64 = I64.divide_signed
function div.u32(lhs, rhs) function div.u32(lhs, rhs)
assert(rhs ~= 0, "division by zero") assert(rhs ~= 0, "division by zero")
lhs = to_u32(lhs) return to_u32(lhs / rhs)
rhs = to_u32(rhs)
return to_i32(math.floor(lhs / rhs))
end end
div.u64 = I64.divide_unsigned
module.add = add module.add = add
module.sub = sub module.sub = sub
module.mul = mul module.mul = mul
@ -78,6 +76,8 @@ do
local ctz = {} local ctz = {}
local popcnt = {} local popcnt = {}
local bit_and = bit32.band
clz.i32 = bit32.countlz clz.i32 = bit32.countlz
ctz.i32 = bit32.countrz ctz.i32 = bit32.countrz
@ -85,7 +85,7 @@ do
local count = 0 local count = 0
while num ~= 0 do while num ~= 0 do
num = bit32.band(num, num - 1) num = bit_and(num, num - 1)
count = count + 1 count = count + 1
end end
@ -98,71 +98,55 @@ do
end end
do do
local eqz = {}
local eq = {}
local ne = {}
local le = {} local le = {}
local lt = {} local lt = {}
local ge = {} local ge = {}
local gt = {} local gt = {}
local function to_boolean(cond) local num_is_equal = I64.is_equal
if cond then local num_is_greater_signed = I64.is_greater_signed
return 1 local num_is_greater_unsigned = I64.is_greater_unsigned
else local num_is_less_signed = I64.is_less_signed
return 0 local num_is_less_unsigned = I64.is_less_unsigned
end
end
function eq.i32(lhs, rhs)
return to_boolean(lhs == rhs)
end
function eq.num(lhs, rhs)
return to_boolean(lhs == rhs)
end
function eqz.i32(lhs)
return to_boolean(lhs == 0)
end
function ne.i32(lhs, rhs)
return to_boolean(lhs ~= rhs)
end
function ne.num(lhs, rhs)
return to_boolean(lhs ~= rhs)
end
function ge.i32(lhs, rhs) function ge.i32(lhs, rhs)
return to_boolean(lhs >= rhs) return to_i32(lhs) >= to_i32(rhs)
end end
function ge.u32(lhs, rhs)
return to_boolean(to_u32(lhs) >= to_u32(rhs)) function ge.i64(lhs, rhs)
return num_is_greater_signed(lhs, rhs) or num_is_equal(lhs, rhs)
end
function ge.u64(lhs, rhs)
return num_is_greater_unsigned(lhs, rhs) or num_is_equal(lhs, rhs)
end end
function gt.i32(lhs, rhs) function gt.i32(lhs, rhs)
return to_boolean(lhs > rhs) return to_i32(lhs) > to_i32(rhs)
end
function gt.u32(lhs, rhs)
return to_boolean(to_u32(lhs) > to_u32(rhs))
end end
gt.i64 = num_is_greater_signed
gt.u64 = num_is_greater_unsigned
function le.i32(lhs, rhs) function le.i32(lhs, rhs)
return to_boolean(lhs <= rhs) return to_i32(lhs) <= to_i32(rhs)
end end
function le.u32(lhs, rhs)
return to_boolean(to_u32(lhs) <= to_u32(rhs)) function le.i64(lhs, rhs)
return num_is_less_signed(lhs, rhs) or num_is_equal(lhs, rhs)
end
function le.u64(lhs, rhs)
return num_is_less_unsigned(lhs, rhs) or num_is_equal(lhs, rhs)
end end
function lt.i32(lhs, rhs) function lt.i32(lhs, rhs)
return to_boolean(lhs < rhs) return to_i32(lhs) < to_i32(rhs)
end
function lt.u32(lhs, rhs)
return to_boolean(to_u32(lhs) < to_u32(rhs))
end end
module.eqz = eqz lt.i64 = num_is_less_signed
module.eq = eq lt.u64 = num_is_less_unsigned
module.ne = ne
module.le = le module.le = le
module.lt = lt module.lt = lt
module.ge = ge module.ge = ge
@ -176,12 +160,16 @@ do
local bnot = {} local bnot = {}
band.i32 = bit32.band band.i32 = bit32.band
band.i64 = I64.bit_and
bnot.i32 = bit32.bnot bnot.i32 = bit32.bnot
bnot.i64 = I64.bit_not
bor.i32 = bit32.bor bor.i32 = bit32.bor
bor.i64 = I64.bit_or
bxor.i32 = bit32.bxor bxor.i32 = bit32.bxor
bxor.i64 = I64.bit_xor
module.band = band module.band = band
module.bor = bor module.bor = bor
@ -196,14 +184,20 @@ do
local rotr = {} local rotr = {}
rotl.i32 = bit32.lrotate rotl.i32 = bit32.lrotate
rotl.i64 = bit32.lrotate
rotr.i32 = bit32.rrotate rotr.i32 = bit32.rrotate
rotr.i64 = bit32.rrotate
shl.i32 = bit32.lshift shl.i32 = bit32.lshift
shl.i64 = bit32.lshift
shl.u32 = bit32.lshift shl.u32 = bit32.lshift
shl.u64 = bit32.lshift
shr.i32 = bit32.arshift shr.i32 = bit32.arshift
shr.i64 = bit32.arshift
shr.u32 = bit32.rshift shr.u32 = bit32.rshift
shr.u64 = bit32.rshift
module.shl = shl module.shl = shl
module.shr = shr module.shr = shr
@ -218,19 +212,123 @@ do
local convert = {} local convert = {}
local reinterpret = {} local reinterpret = {}
trunc.i32_f32 = truncate local math_ceil = math.ceil
trunc.i32_f64 = truncate local math_floor = math.floor
trunc.u32_f32 = truncate
trunc.u32_f64 = truncate
extend.i64_i32 = no_op local string_pack = string.pack
local string_unpack = string.unpack
function convert.f32_i32(num) local num_from_u64 = I64.from_u64
return num local num_into_u64 = I64.into_u64
local num_negate = I64.negate
local num_is_negative = I64.is_negative
function wrap.i32_i64(num)
local data_1, _ = num_into_u32(num)
return data_1
end end
function convert.f64_i32(num) trunc.i32_f32 = to_u32
return num trunc.i32_f64 = to_u32
trunc.u32_f32 = no_op
trunc.u32_f64 = no_op
function trunc.i64_f32(num)
if num < 0 then
local temp = num_from_u64(-math_ceil(num))
return num_negate(temp)
else
local temp = math_floor(num)
return num_from_u64(temp)
end
end
function trunc.i64_f64(num)
if num < 0 then
local temp = num_from_u64(-math_ceil(num))
return num_negate(temp)
else
local temp = math_floor(num)
return num_from_u64(temp)
end
end
trunc.u64_f32 = num_from_u64
trunc.u64_f64 = num_from_u64
function extend.i64_i32(num)
if num > MAX_SIGNED then
local temp = num_from_u32(-num + BIT_SET_32, 0)
return num_negate(temp)
else
return num_from_u32(num, 0)
end
end
function extend.u64_i32(num)
return num_from_u32(num, 0)
end
convert.f32_i32 = no_op
convert.f32_u32 = no_op
function convert.f32_i64(num)
if num_is_negative(num) then
local temp = num_negate(num)
return -num_into_u64(temp)
else
return num_into_u64(num)
end
end
convert.f32_u64 = num_into_u64
convert.f64_i32 = to_i32
convert.f64_u32 = no_op
function convert.f64_i64(num)
if num_is_negative(num) then
local temp = num_negate(num)
return -num_into_u64(temp)
else
return num_into_u64(num)
end
end
convert.f64_u64 = num_into_u64
function reinterpret.i32_f32(num)
local packed = string_pack("f", num)
return string_unpack("<I4", packed)
end
function reinterpret.i64_f64(num)
local packed = string_pack("d", num)
local data_1, data_2 = string_unpack("<I4I4", packed)
return num_from_u32(data_1, data_2)
end
function reinterpret.f32_i32(num)
local packed = string_pack("<I4", num)
return string_unpack("f", packed)
end
function reinterpret.f64_i64(num)
local data_1, data_2 = num_into_u32(num)
local packed = string_pack("<I4I4", data_1, data_2)
return string_unpack("d", packed)
end end
module.wrap = wrap module.wrap = wrap
@ -245,38 +343,32 @@ do
local store = {} local store = {}
local allocator = {} local allocator = {}
local function rip_u64(x) local bit_extract = bit32.extract
return math.floor(x / 0x100000000), x % 0x100000000 local bit_replace = bit32.replace
local bit_bor = bit32.bor
local bit_lshift = bit32.lshift
local bit_rshift = bit32.rshift
local math_floor = math.floor
local string_byte = string.byte
local string_unpack = string.unpack
local function load_byte(data, addr)
local value = data[math_floor(addr / 4)] or 0
return bit_extract(value, addr % 4 * 8, 8)
end end
local function merge_u64(hi, lo) local function store_byte(data, addr, value)
return hi * 0x100000000 + lo local adjust = math_floor(addr / 4)
end
local function black_mask_byte(value, offset) data[adjust] = bit_replace(data[adjust] or 0, value, addr % 4 * 8, 8)
local mask = bit32.lshift(0xFF, offset * 8)
return bit32.band(value, bit32.bnot(mask))
end
local function load_byte(memory, addr)
local offset = addr % 4
local value = memory.data[(addr - offset) / 4] or 0
return bit32.band(bit32.rshift(value, offset * 8), 0xFF)
end
local function store_byte(memory, addr, value)
local offset = addr % 4
local adjust = (addr - offset) / 4
local lhs = bit32.lshift(bit32.band(value, 0xFF), offset * 8)
local rhs = black_mask_byte(memory.data[adjust] or 0, offset)
memory.data[adjust] = bit32.bor(lhs, rhs)
end end
function load.i32_i8(memory, addr) function load.i32_i8(memory, addr)
local b = load_byte(memory, addr) local b = load_byte(memory.data, addr)
if b > 0x7F then if b > 0x7F then
b = b - 0x100 b = b - 0x100
@ -285,71 +377,82 @@ do
return b return b
end end
load.i32_u8 = load_byte function load.i32_u8(memory, addr)
return load_byte(memory.data, addr)
end
function load.i32(memory, addr) function load.i32(memory, addr)
local data = memory.data
if addr % 4 == 0 then if addr % 4 == 0 then
-- aligned read -- aligned read
return memory.data[addr / 4] or 0 return data[addr / 4] or 0
else else
-- unaligned read -- unaligned read
local b1 = load_byte(memory, addr) local b1 = load_byte(data, addr)
local b2 = bit32.lshift(load_byte(memory, addr + 1), 8) local b2 = bit_lshift(load_byte(data, addr + 1), 8)
local b3 = bit32.lshift(load_byte(memory, addr + 2), 16) local b3 = bit_lshift(load_byte(data, addr + 2), 16)
local b4 = bit32.lshift(load_byte(memory, addr + 3), 24) local b4 = bit_lshift(load_byte(data, addr + 3), 24)
return bit32.bor(b1, b2, b3, b4) return bit_bor(b1, b2, b3, b4)
end end
end end
local load_i32 = load.i32
function load.i64(memory, addr) function load.i64(memory, addr)
local hi = load.i32(memory, addr + 4) local data_1 = load_i32(memory, addr)
local lo = load.i32(memory, addr) local data_2 = load_i32(memory, addr + 4)
return merge_u64(hi, lo) return num_from_u32(data_1, data_2)
end end
store.i32_n8 = store_byte function store.i32_i8(memory, addr, value)
store_byte(memory.data, addr, value)
end
function store.i32(memory, addr, value) function store.i32(memory, addr, value)
local data = memory.data
if addr % 4 == 0 then if addr % 4 == 0 then
-- aligned write -- aligned write
memory.data[addr / 4] = value data[addr / 4] = value
else else
-- unaligned write -- unaligned write
store_byte(memory, addr, value) store_byte(data, addr, value)
store_byte(memory, addr + 1, bit32.rshift(value, 8)) store_byte(data, addr + 1, bit_rshift(value, 8))
store_byte(memory, addr + 2, bit32.rshift(value, 16)) store_byte(data, addr + 2, bit_rshift(value, 16))
store_byte(memory, addr + 3, bit32.rshift(value, 24)) store_byte(data, addr + 3, bit_rshift(value, 24))
end end
end end
local store_i32 = store.i32
function store.i64(memory, addr, value) function store.i64(memory, addr, value)
local hi, lo = rip_u64(value) local data_1, data_2 = num_into_u32(value)
store.i32(memory, addr, lo) store_i32(memory, addr, data_1)
store.i32(memory, addr + 4, hi) store_i32(memory, addr + 4, data_2)
end end
function allocator.new(min, max) function allocator.new(min, max)
return { min = min, max = max, data = {} } return { min = min, max = max, data = {} }
end end
function allocator.init(memory, offset, data) local store_i8 = store.i32_n8
local store_i8 = module.store.i32_n8
local store_i32 = module.store.i32
function allocator.init(memory, offset, data)
local len = #data local len = #data
local rem = len % 4 local rem = len % 4
for i = 1, len - rem, 4 do for i = 1, len - rem, 4 do
local v = string.unpack("<I4", data, i) local v = string_unpack("<I4", data, i)
store_i32(memory, offset + i - 1, v) store_i32(memory, offset + i - 1, v)
end end
for i = len - rem + 1, len do for i = len - rem + 1, len do
local v = string.byte(data, i) local v = string_byte(data, i)
store_i8(memory, offset + i - 1, v) store_i8(memory, offset + i - 1, v)
end end

View File

@ -1,4 +1,9 @@
pub static RUNTIME: &str = include_str!("../runtime/runtime.lua"); pub static RUNTIME: &str = concat!(
"local I64 = (function() ",
include_str!("../runtime/numeric.lua"),
" end)()\n",
include_str!("../runtime/runtime.lua")
);
pub use translator::translate; pub use translator::translate;