Re-structure and decouple AST from generator
This commit is contained in:
@@ -0,0 +1,430 @@
|
||||
local Numeric = {}
|
||||
|
||||
Numeric.__index = Numeric
|
||||
|
||||
local bit_band = bit32.band
|
||||
local bit_bnot = bit32.bnot
|
||||
local bit_bor = bit32.bor
|
||||
local bit_xor = bit32.bxor
|
||||
|
||||
local bit_lshift = bit32.lshift
|
||||
local bit_rshift = bit32.rshift
|
||||
local bit_arshift = bit32.arshift
|
||||
|
||||
local math_floor = math.floor
|
||||
|
||||
local N_2_TO_31 = 0x80000000
|
||||
local N_2_TO_32 = 0x100000000
|
||||
|
||||
local VAL_ZERO
|
||||
local VAL_ONE
|
||||
local VAL_2_TO_24
|
||||
|
||||
local op_is_equal
|
||||
local op_is_greater_unsigned
|
||||
local op_is_less_unsigned
|
||||
local op_is_negative
|
||||
local op_is_zero
|
||||
|
||||
local op_bnot
|
||||
local op_negate
|
||||
|
||||
-- TODO: Eventually support Vector3
|
||||
local function from_u32(low, high)
|
||||
return setmetatable({ low, high }, Numeric)
|
||||
end
|
||||
|
||||
local function to_u32(value)
|
||||
return value[1], value[2]
|
||||
end
|
||||
|
||||
local function from_f64(value)
|
||||
if value < 0 then
|
||||
return op_negate(from_f64(-value))
|
||||
else
|
||||
return from_u32(value % N_2_TO_32, math_floor(value / N_2_TO_32))
|
||||
end
|
||||
end
|
||||
|
||||
local function to_f64(value)
|
||||
local low, high = to_u32(value)
|
||||
|
||||
return low + high * N_2_TO_32
|
||||
end
|
||||
|
||||
local function op_add(lhs, rhs)
|
||||
local low_a, high_a = to_u32(lhs)
|
||||
local low_b, high_b = to_u32(rhs)
|
||||
|
||||
local low = low_a + low_b
|
||||
local high = high_a + high_b
|
||||
|
||||
if low >= N_2_TO_32 then
|
||||
low = low - N_2_TO_32
|
||||
high = high + 1
|
||||
end
|
||||
|
||||
if high >= N_2_TO_32 then
|
||||
high = high - N_2_TO_32
|
||||
end
|
||||
|
||||
return from_u32(low, high)
|
||||
end
|
||||
|
||||
local function op_subtract(lhs, rhs)
|
||||
local low_a, high_a = to_u32(lhs)
|
||||
local low_b, high_b = to_u32(rhs)
|
||||
|
||||
local low = low_a - low_b
|
||||
local high = high_a - high_b
|
||||
|
||||
if low < 0 then
|
||||
low = low + N_2_TO_32
|
||||
high = high - 1
|
||||
end
|
||||
|
||||
if high < 0 then
|
||||
high = high + N_2_TO_32
|
||||
end
|
||||
|
||||
return from_u32(low, high)
|
||||
end
|
||||
|
||||
local function set_absolute(lhs, rhs)
|
||||
local has_negative = false
|
||||
|
||||
if op_is_negative(lhs) then
|
||||
lhs = op_negate(lhs)
|
||||
has_negative = not has_negative
|
||||
end
|
||||
|
||||
if op_is_negative(rhs) then
|
||||
rhs = op_negate(rhs)
|
||||
has_negative = not has_negative
|
||||
end
|
||||
|
||||
return has_negative, lhs, rhs
|
||||
end
|
||||
|
||||
local function op_multiply(lhs, rhs)
|
||||
if op_is_zero(lhs) or op_is_zero(rhs) then
|
||||
return VAL_ZERO
|
||||
end
|
||||
|
||||
local has_negative
|
||||
|
||||
has_negative, lhs, rhs = set_absolute(lhs, rhs)
|
||||
|
||||
-- If both longs are small, use float multiplication
|
||||
if op_is_less_unsigned(lhs, VAL_2_TO_24) and op_is_less_unsigned(rhs, VAL_2_TO_24) then
|
||||
local low_a = to_u32(lhs)
|
||||
local low_b = to_u32(rhs)
|
||||
local result = from_f64(low_a * low_b)
|
||||
|
||||
if has_negative then
|
||||
result = op_negate(result)
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
-- Divide each long into 4 chunks of 16 bits, and then add up 4x4 products.
|
||||
-- We can skip products that would overflow.
|
||||
local low_a, high_a = to_u32(lhs)
|
||||
local low_b, high_b = to_u32(rhs)
|
||||
|
||||
local a48 = bit_rshift(high_a, 16)
|
||||
local a32 = bit_band(high_a, 0xFFFF)
|
||||
local a16 = bit_rshift(low_a, 16)
|
||||
local a00 = bit_band(low_a, 0xFFFF)
|
||||
|
||||
local b48 = bit_rshift(high_b, 16)
|
||||
local b32 = bit_band(high_b, 0xFFFF)
|
||||
local b16 = bit_rshift(low_b, 16)
|
||||
local b00 = bit_band(low_b, 0xFFFF)
|
||||
|
||||
local c48, c32, c16, c00 = 0, 0, 0, 0
|
||||
|
||||
c00 = c00 + a00 * b00
|
||||
c16 = c16 + bit_rshift(c00, 16)
|
||||
c00 = bit_band(c00, 0xFFFF)
|
||||
c16 = c16 + a16 * b00
|
||||
c32 = c32 + bit_rshift(c16, 16)
|
||||
c16 = bit_band(c16, 0xFFFF)
|
||||
c16 = c16 + a00 * b16
|
||||
c32 = c32 + bit_rshift(c16, 16)
|
||||
c16 = bit_band(c16, 0xFFFF)
|
||||
c32 = c32 + a32 * b00
|
||||
c48 = c48 + bit_rshift(c32, 16)
|
||||
c32 = bit_band(c32, 0xFFFF)
|
||||
c32 = c32 + a16 * b16
|
||||
c48 = c48 + bit_rshift(c32, 16)
|
||||
c32 = bit_band(c32, 0xFFFF)
|
||||
c32 = c32 + a00 * b32
|
||||
c48 = c48 + bit_rshift(c32, 16)
|
||||
c32 = bit_band(c32, 0xFFFF)
|
||||
c48 = c48 + a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48
|
||||
c48 = bit_band(c48, 0xFFFF)
|
||||
|
||||
local low_v = bit_bor(bit_lshift(c16, 16), c00)
|
||||
local high_v = bit_bor(bit_lshift(c48, 16), c32)
|
||||
local result = from_u32(low_v, high_v)
|
||||
|
||||
if has_negative then
|
||||
result = op_negate(result)
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
local math_ceil = math.ceil
|
||||
local math_log = math.log
|
||||
local math_max = math.max
|
||||
local math_pow = math.pow
|
||||
|
||||
local function get_approx_delta(rem, rhs)
|
||||
local approx = math_max(1, math_floor(rem / rhs))
|
||||
local log = math_ceil(math_log(approx, 2))
|
||||
local delta = log <= 48 and 1 or math_pow(2, log - 48)
|
||||
|
||||
return approx, delta
|
||||
end
|
||||
|
||||
local function op_divide_unsigned(lhs, rhs)
|
||||
if op_is_zero(rhs) then
|
||||
error("division by zero")
|
||||
elseif op_is_zero(lhs) then
|
||||
return 0
|
||||
end
|
||||
|
||||
local rhs_number = to_f64(rhs)
|
||||
local rem = lhs
|
||||
local res = VAL_ZERO
|
||||
|
||||
while op_is_greater_unsigned(rem, rhs) or op_is_equal(rem, rhs) do
|
||||
local res_approx, delta = get_approx_delta(to_f64(rem), rhs_number)
|
||||
local res_temp = from_f64(res_approx)
|
||||
local rem_temp = op_multiply(res_temp, rhs)
|
||||
|
||||
while op_is_negative(rem_temp) or op_is_greater_unsigned(rem_temp, rem) do
|
||||
res_approx = res_approx - delta
|
||||
res_temp = from_f64(res_approx)
|
||||
rem_temp = op_multiply(res_temp, rhs)
|
||||
end
|
||||
|
||||
if op_is_zero(res_temp) then
|
||||
res_temp = VAL_ONE
|
||||
end
|
||||
|
||||
res = op_add(res, res_temp)
|
||||
rem = op_subtract(rem, rem_temp)
|
||||
end
|
||||
|
||||
return res
|
||||
end
|
||||
|
||||
local function op_divide_signed(lhs, rhs)
|
||||
local has_negative
|
||||
|
||||
has_negative, lhs, rhs = set_absolute(lhs, rhs)
|
||||
|
||||
local result = op_divide_unsigned(lhs, rhs)
|
||||
|
||||
if has_negative then
|
||||
result = op_negate(result)
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
function op_negate(value)
|
||||
return op_add(op_bnot(value), VAL_ONE)
|
||||
end
|
||||
|
||||
local function op_band(lhs, rhs)
|
||||
local low_a, high_a = to_u32(lhs)
|
||||
local low_b, high_b = to_u32(rhs)
|
||||
|
||||
return from_u32(bit_band(low_a, low_b), bit_band(high_a, high_b))
|
||||
end
|
||||
|
||||
function op_bnot(value)
|
||||
local low, high = to_u32(value)
|
||||
|
||||
return from_u32(bit_bnot(low), bit_bnot(high))
|
||||
end
|
||||
|
||||
local function op_bor(lhs, rhs)
|
||||
local low_a, high_a = to_u32(lhs)
|
||||
local low_b, high_b = to_u32(rhs)
|
||||
|
||||
return from_u32(bit_bor(low_a, low_b), bit_bor(high_a, high_b))
|
||||
end
|
||||
|
||||
local function op_bxor(lhs, rhs)
|
||||
local low_a, high_a = to_u32(lhs)
|
||||
local low_b, high_b = to_u32(rhs)
|
||||
|
||||
return from_u32(bit_xor(low_a, low_b), bit_xor(high_a, high_b))
|
||||
end
|
||||
|
||||
local function op_shift_left(lhs, rhs)
|
||||
local count = to_f64(rhs)
|
||||
|
||||
if count < 32 then
|
||||
local low_a, high_a = to_u32(lhs)
|
||||
|
||||
local low_v = bit_lshift(low_a, count)
|
||||
local high_v = bit_bor(bit_lshift(high_a, count), bit_rshift(low_a, 32 - count))
|
||||
|
||||
return from_u32(low_v, high_v)
|
||||
else
|
||||
local _, high_a = to_u32(lhs)
|
||||
|
||||
local high_v = bit_lshift(high_a, count - 32)
|
||||
|
||||
return from_u32(0, high_v)
|
||||
end
|
||||
end
|
||||
|
||||
local function op_shift_right_unsigned(lhs, rhs)
|
||||
local count = to_f64(rhs)
|
||||
|
||||
if count < 32 then
|
||||
local low_a, high_a = to_u32(lhs)
|
||||
|
||||
local low_v = bit_bor(bit_rshift(low_a, count), bit_lshift(high_a, 32 - count))
|
||||
local high_v = bit_rshift(high_a, count)
|
||||
|
||||
return from_u32(low_v, high_v)
|
||||
elseif numBits == 32 then
|
||||
local _, high_a = to_u32(lhs)
|
||||
|
||||
return from_u32(high_a, 0)
|
||||
else
|
||||
local _, high_a = to_u32(lhs)
|
||||
|
||||
return from_u32(bit_rshift(high_a, count - 32), 0)
|
||||
end
|
||||
end
|
||||
|
||||
local function op_shift_right_signed(lhs, rhs)
|
||||
local count = to_f64(rhs)
|
||||
|
||||
if count < 32 then
|
||||
local low_a, high_a = to_u32(lhs)
|
||||
|
||||
local low_v = bit_bor(bit_rshift(low_a, count), bit_lshift(high_a, 32 - count))
|
||||
local high_v = bit_arshift(high_a, count)
|
||||
|
||||
return from_u32(low_v, high_v)
|
||||
else
|
||||
local low_a, high_a = to_u32(lhs)
|
||||
|
||||
local low_v = bit_arshift(high_a, count - 32)
|
||||
local high_v = high_a > N_2_TO_31 and N_2_TO_32 - 1 or 0
|
||||
|
||||
return from_u32(low_v, high_v)
|
||||
end
|
||||
end
|
||||
|
||||
function op_is_negative(value)
|
||||
local _, high = to_u32(value)
|
||||
|
||||
return high > N_2_TO_31
|
||||
end
|
||||
|
||||
function op_is_zero(value)
|
||||
local low, high = to_u32(value)
|
||||
|
||||
return low == 0 and high == 0
|
||||
end
|
||||
|
||||
function op_is_equal(lhs, rhs)
|
||||
local low_a, high_a = to_u32(lhs)
|
||||
local low_b, high_b = to_u32(rhs)
|
||||
|
||||
return low_a == low_b and high_a == high_b
|
||||
end
|
||||
|
||||
function op_is_less_unsigned(lhs, rhs)
|
||||
local low_a, high_a = to_u32(lhs)
|
||||
local low_b, high_b = to_u32(rhs)
|
||||
|
||||
return high_a < high_b or (high_a == high_b and low_a < low_b)
|
||||
end
|
||||
|
||||
function op_is_greater_unsigned(lhs, rhs)
|
||||
local low_a, high_a = to_u32(lhs)
|
||||
local low_b, high_b = to_u32(rhs)
|
||||
|
||||
return high_a > high_b or (high_a == high_b and low_a > low_b)
|
||||
end
|
||||
|
||||
local function op_is_less_signed(lhs, rhs)
|
||||
local neg_a = op_is_negative(lhs)
|
||||
local neg_b = op_is_negative(rhs)
|
||||
|
||||
if neg_a and not neg_b then
|
||||
return true
|
||||
elseif not neg_a and neg_b then
|
||||
return false
|
||||
else
|
||||
return op_is_negative(op_subtract(lhs, rhs))
|
||||
end
|
||||
end
|
||||
|
||||
local function op_is_greater_signed(lhs, rhs)
|
||||
local neg_a = op_is_negative(lhs)
|
||||
local neg_b = op_is_negative(rhs)
|
||||
|
||||
if neg_a and not neg_b then
|
||||
return false
|
||||
elseif not neg_a and neg_b then
|
||||
return true
|
||||
else
|
||||
return op_is_negative(op_subtract(rhs, lhs))
|
||||
end
|
||||
end
|
||||
|
||||
local function to_bytes_le(value)
|
||||
local low, high = to_u32(value)
|
||||
|
||||
return {
|
||||
bit_band(low, 0xFF),
|
||||
bit_band(bit_rshift(low, 8), 0xFF),
|
||||
bit_band(bit_rshift(low, 16), 0xFF),
|
||||
bit_band(bit_rshift(low, 24), 0xFF),
|
||||
bit_band(high, 0xFF),
|
||||
bit_band(bit_rshift(high, 8), 0xFF),
|
||||
bit_band(bit_rshift(high, 16), 0xFF),
|
||||
bit_band(bit_rshift(high, 24), 0xFF),
|
||||
}
|
||||
end
|
||||
|
||||
VAL_ZERO = from_f64(0)
|
||||
VAL_ONE = from_f64(1)
|
||||
VAL_2_TO_24 = from_f64(0x1000000)
|
||||
|
||||
Numeric.from_f64 = from_f64
|
||||
Numeric.from_u32 = from_u32
|
||||
|
||||
Numeric.__add = op_add
|
||||
Numeric.__sub = op_subtract
|
||||
Numeric.__mul = op_multiply
|
||||
Numeric.__div = op_divide_unsigned
|
||||
|
||||
Numeric.__unm = op_negate
|
||||
|
||||
Numeric.__eq = op_is_equal
|
||||
Numeric.__lt = op_is_less_unsigned
|
||||
|
||||
function Numeric.__le(lhs, rhs)
|
||||
return op_is_less_unsigned(lhs, rhs) or op_is_equal(lhs, rhs)
|
||||
end
|
||||
|
||||
function Numeric.__tostring(value)
|
||||
return tostring(to_f64(value))
|
||||
end
|
||||
|
||||
return Numeric
|
||||
@@ -0,0 +1,376 @@
|
||||
local module = {}
|
||||
|
||||
local math_floor = math.floor
|
||||
local math_ceil = math.ceil
|
||||
|
||||
local bit32 = bit32
|
||||
local bit_band = bit32.band
|
||||
|
||||
local function no_op(x)
|
||||
return x
|
||||
end
|
||||
|
||||
local function to_u32(x)
|
||||
return bit_band(x, 0xFFFFFFFF)
|
||||
end
|
||||
|
||||
local function to_i32(x)
|
||||
if x > 0x7FFFFFFF then
|
||||
x = x - 0x100000000
|
||||
end
|
||||
|
||||
return x
|
||||
end
|
||||
|
||||
local function wrap_i32(x)
|
||||
return to_i32(to_u32(x))
|
||||
end
|
||||
|
||||
local function truncate(num)
|
||||
if num >= 0 then
|
||||
return math_floor(num)
|
||||
else
|
||||
return math_ceil(num)
|
||||
end
|
||||
end
|
||||
|
||||
do
|
||||
local add = {}
|
||||
local sub = {}
|
||||
local mul = {}
|
||||
local div = {}
|
||||
|
||||
function add.i32(a, b)
|
||||
return wrap_i32(a + b)
|
||||
end
|
||||
|
||||
function sub.i32(a, b)
|
||||
return wrap_i32(a - b)
|
||||
end
|
||||
|
||||
function mul.i32(a, b)
|
||||
return wrap_i32(a * b)
|
||||
end
|
||||
|
||||
function div.i32(lhs, rhs)
|
||||
assert(rhs ~= 0, "division by zero")
|
||||
|
||||
return truncate(lhs / rhs)
|
||||
end
|
||||
|
||||
function div.u32(lhs, rhs)
|
||||
assert(rhs ~= 0, "division by zero")
|
||||
|
||||
lhs = to_u32(lhs)
|
||||
rhs = to_u32(rhs)
|
||||
|
||||
return to_i32(math.floor(lhs / rhs))
|
||||
end
|
||||
|
||||
module.add = add
|
||||
module.sub = sub
|
||||
module.mul = mul
|
||||
module.div = div
|
||||
end
|
||||
|
||||
do
|
||||
local clz = {}
|
||||
local ctz = {}
|
||||
local popcnt = {}
|
||||
|
||||
clz.i32 = bit32.countlz
|
||||
ctz.i32 = bit32.countrz
|
||||
|
||||
function popcnt.i32(num)
|
||||
local count = 0
|
||||
|
||||
while num ~= 0 do
|
||||
num = bit32.band(num, num - 1)
|
||||
count = count + 1
|
||||
end
|
||||
|
||||
return count
|
||||
end
|
||||
|
||||
module.clz = clz
|
||||
module.ctz = ctz
|
||||
module.popcnt = popcnt
|
||||
end
|
||||
|
||||
do
|
||||
local eqz = {}
|
||||
local eq = {}
|
||||
local ne = {}
|
||||
local le = {}
|
||||
local lt = {}
|
||||
local ge = {}
|
||||
local gt = {}
|
||||
|
||||
local function to_boolean(cond)
|
||||
if cond then
|
||||
return 1
|
||||
else
|
||||
return 0
|
||||
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)
|
||||
return to_boolean(lhs >= rhs)
|
||||
end
|
||||
function ge.u32(lhs, rhs)
|
||||
return to_boolean(to_u32(lhs) >= to_u32(rhs))
|
||||
end
|
||||
|
||||
function gt.i32(lhs, rhs)
|
||||
return to_boolean(lhs > rhs)
|
||||
end
|
||||
function gt.u32(lhs, rhs)
|
||||
return to_boolean(to_u32(lhs) > to_u32(rhs))
|
||||
end
|
||||
|
||||
function le.i32(lhs, rhs)
|
||||
return to_boolean(lhs <= rhs)
|
||||
end
|
||||
function le.u32(lhs, rhs)
|
||||
return to_boolean(to_u32(lhs) <= to_u32(rhs))
|
||||
end
|
||||
|
||||
function lt.i32(lhs, rhs)
|
||||
return to_boolean(lhs < rhs)
|
||||
end
|
||||
function lt.u32(lhs, rhs)
|
||||
return to_boolean(to_u32(lhs) < to_u32(rhs))
|
||||
end
|
||||
|
||||
module.eqz = eqz
|
||||
module.eq = eq
|
||||
module.ne = ne
|
||||
module.le = le
|
||||
module.lt = lt
|
||||
module.ge = ge
|
||||
module.gt = gt
|
||||
end
|
||||
|
||||
do
|
||||
local band = {}
|
||||
local bor = {}
|
||||
local bxor = {}
|
||||
local bnot = {}
|
||||
|
||||
band.i32 = bit32.band
|
||||
|
||||
bnot.i32 = bit32.bnot
|
||||
|
||||
bor.i32 = bit32.bor
|
||||
|
||||
bxor.i32 = bit32.bxor
|
||||
|
||||
module.band = band
|
||||
module.bor = bor
|
||||
module.bxor = bxor
|
||||
module.bnot = bnot
|
||||
end
|
||||
|
||||
do
|
||||
local shl = {}
|
||||
local shr = {}
|
||||
local rotl = {}
|
||||
local rotr = {}
|
||||
|
||||
rotl.i32 = bit32.lrotate
|
||||
|
||||
rotr.i32 = bit32.rrotate
|
||||
|
||||
shl.i32 = bit32.lshift
|
||||
shl.u32 = bit32.lshift
|
||||
|
||||
shr.i32 = bit32.arshift
|
||||
shr.u32 = bit32.rshift
|
||||
|
||||
module.shl = shl
|
||||
module.shr = shr
|
||||
module.rotl = rotl
|
||||
module.rotr = rotr
|
||||
end
|
||||
|
||||
do
|
||||
local wrap = {}
|
||||
local trunc = {}
|
||||
local extend = {}
|
||||
local convert = {}
|
||||
local reinterpret = {}
|
||||
|
||||
trunc.i32_f32 = truncate
|
||||
trunc.i32_f64 = truncate
|
||||
trunc.u32_f32 = truncate
|
||||
trunc.u32_f64 = truncate
|
||||
|
||||
extend.i64_i32 = no_op
|
||||
|
||||
function convert.f32_i32(num)
|
||||
return num
|
||||
end
|
||||
|
||||
function convert.f64_i32(num)
|
||||
return num
|
||||
end
|
||||
|
||||
module.wrap = wrap
|
||||
module.trunc = trunc
|
||||
module.extend = extend
|
||||
module.convert = convert
|
||||
module.reinterpret = reinterpret
|
||||
end
|
||||
|
||||
do
|
||||
local load = {}
|
||||
local store = {}
|
||||
local allocator = {}
|
||||
|
||||
local function rip_u64(x)
|
||||
return math.floor(x / 0x100000000), x % 0x100000000
|
||||
end
|
||||
|
||||
local function merge_u64(hi, lo)
|
||||
return hi * 0x100000000 + lo
|
||||
end
|
||||
|
||||
local function black_mask_byte(value, offset)
|
||||
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
|
||||
|
||||
function load.i32_i8(memory, addr)
|
||||
local b = load_byte(memory, addr)
|
||||
|
||||
if b > 0x7F then
|
||||
b = b - 0x100
|
||||
end
|
||||
|
||||
return b
|
||||
end
|
||||
|
||||
load.i32_u8 = load_byte
|
||||
|
||||
function load.i32(memory, addr)
|
||||
if addr % 4 == 0 then
|
||||
-- aligned read
|
||||
return memory.data[addr / 4] or 0
|
||||
else
|
||||
-- unaligned read
|
||||
local b1 = load_byte(memory, addr)
|
||||
local b2 = bit32.lshift(load_byte(memory, addr + 1), 8)
|
||||
local b3 = bit32.lshift(load_byte(memory, addr + 2), 16)
|
||||
local b4 = bit32.lshift(load_byte(memory, addr + 3), 24)
|
||||
|
||||
return bit32.bor(b1, b2, b3, b4)
|
||||
end
|
||||
end
|
||||
|
||||
function load.i64(memory, addr)
|
||||
local hi = load.i32(memory, addr + 4)
|
||||
local lo = load.i32(memory, addr)
|
||||
|
||||
return merge_u64(hi, lo)
|
||||
end
|
||||
|
||||
store.i32_n8 = store_byte
|
||||
|
||||
function store.i32(memory, addr, value)
|
||||
if addr % 4 == 0 then
|
||||
-- aligned write
|
||||
memory.data[addr / 4] = value
|
||||
else
|
||||
-- unaligned write
|
||||
store_byte(memory, addr, value)
|
||||
store_byte(memory, addr + 1, bit32.rshift(value, 8))
|
||||
store_byte(memory, addr + 2, bit32.rshift(value, 16))
|
||||
store_byte(memory, addr + 3, bit32.rshift(value, 24))
|
||||
end
|
||||
end
|
||||
|
||||
function store.i64(memory, addr, value)
|
||||
local hi, lo = rip_u64(value)
|
||||
|
||||
store.i32(memory, addr, lo)
|
||||
store.i32(memory, addr + 4, hi)
|
||||
end
|
||||
|
||||
function allocator.new(min, max)
|
||||
return { min = min, max = max, data = {} }
|
||||
end
|
||||
|
||||
function allocator.init(memory, offset, data)
|
||||
local store_i8 = module.store.i32_n8
|
||||
local store_i32 = module.store.i32
|
||||
|
||||
local len = #data
|
||||
local rem = len % 4
|
||||
|
||||
for i = 1, len - rem, 4 do
|
||||
local v = string.unpack("<I4", data, i)
|
||||
|
||||
store_i32(memory, offset + i - 1, v)
|
||||
end
|
||||
|
||||
for i = len - rem + 1, len do
|
||||
local v = string.byte(data, i)
|
||||
|
||||
store_i8(memory, offset + i - 1, v)
|
||||
end
|
||||
end
|
||||
|
||||
function allocator.grow(memory, num)
|
||||
local old = memory.min
|
||||
local new = old + num
|
||||
|
||||
if new > memory.max then
|
||||
return -1
|
||||
else
|
||||
memory.min = new
|
||||
|
||||
return old
|
||||
end
|
||||
end
|
||||
|
||||
module.load = load
|
||||
module.store = store
|
||||
module.allocator = allocator
|
||||
end
|
||||
|
||||
return module
|
||||
Reference in New Issue
Block a user