Change DynASM bit operations to use Lua BitOp.
This commit is contained in:
@@ -26,6 +26,8 @@ local _s = string
|
||||
local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char
|
||||
local match, gmatch = _s.match, _s.gmatch
|
||||
local concat, sort = table.concat, table.sort
|
||||
local bit = bit or require("bit")
|
||||
local band, shl, sar, tohex = bit.band, bit.lshift, bit.arshift, bit.tohex
|
||||
|
||||
-- Inherited tables and callbacks.
|
||||
local g_opt, g_arch
|
||||
@@ -60,11 +62,6 @@ local secpos = 1
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
-- Return 8 digit hex number.
|
||||
local function tohex(x)
|
||||
return sub(format("%08x", x), -8) -- Avoid 64 bit portability problem in Lua.
|
||||
end
|
||||
|
||||
-- Dump action names and numbers.
|
||||
local function dumpactions(out)
|
||||
out:write("DynASM encoding engine action codes:\n")
|
||||
@@ -639,16 +636,14 @@ end
|
||||
local function parse_imm(imm, bits, shift, scale, signed)
|
||||
local n = tonumber(imm)
|
||||
if n then
|
||||
if n % 2^scale == 0 then
|
||||
n = n / 2^scale
|
||||
local m = sar(n, scale)
|
||||
if shl(m, scale) == n then
|
||||
if signed then
|
||||
if n >= 0 then
|
||||
if n < 2^(bits-1) then return n*2^shift end
|
||||
else
|
||||
if n >= -(2^(bits-1))-1 then return (n+2^bits)*2^shift end
|
||||
end
|
||||
local s = sar(m, bits-1)
|
||||
if s == 0 then return shl(m, shift)
|
||||
elseif s == -1 then return shl(m + shl(1, bits), shift) end
|
||||
else
|
||||
if n >= 0 and n <= 2^bits-1 then return n*2^shift end
|
||||
if sar(m, bits) == 0 then return shl(m, shift) end
|
||||
end
|
||||
end
|
||||
werror("out of range immediate `"..imm.."'")
|
||||
@@ -664,7 +659,7 @@ end
|
||||
local function parse_disp(disp)
|
||||
local imm, reg = match(disp, "^(.*)%(([%w_:]+)%)$")
|
||||
if imm then
|
||||
local r = parse_gpr(reg)*2^21
|
||||
local r = shl(parse_gpr(reg), 21)
|
||||
local extname = match(imm, "^extern%s+(%S+)$")
|
||||
if extname then
|
||||
waction("REL_EXT", map_extern[extname], nil, 1)
|
||||
@@ -678,7 +673,7 @@ local function parse_disp(disp)
|
||||
local r, tp = parse_gpr(reg)
|
||||
if tp then
|
||||
waction("IMM", 32768+16*32, format(tp.ctypefmt, tailr))
|
||||
return r*2^21
|
||||
return shl(r, 21)
|
||||
end
|
||||
end
|
||||
werror("bad displacement `"..disp.."'")
|
||||
@@ -689,7 +684,7 @@ local function parse_index(idx)
|
||||
if rt then
|
||||
rt = parse_gpr(rt)
|
||||
rs = parse_gpr(rs)
|
||||
return rt*2^16 + rs*2^21
|
||||
return shl(rt, 16) + shl(rs, 21)
|
||||
end
|
||||
werror("bad index `"..idx.."'")
|
||||
end
|
||||
@@ -740,19 +735,19 @@ map_op[".template__"] = function(params, template, nparams)
|
||||
-- Process each character.
|
||||
for p in gmatch(sub(template, 9), ".") do
|
||||
if p == "D" then
|
||||
op = op + parse_gpr(params[n]) * 2^11; n = n + 1
|
||||
op = op + shl(parse_gpr(params[n]), 11); n = n + 1
|
||||
elseif p == "T" then
|
||||
op = op + parse_gpr(params[n]) * 2^16; n = n + 1
|
||||
op = op + shl(parse_gpr(params[n]), 16); n = n + 1
|
||||
elseif p == "S" then
|
||||
op = op + parse_gpr(params[n]) * 2^21; n = n + 1
|
||||
op = op + shl(parse_gpr(params[n]), 21); n = n + 1
|
||||
elseif p == "F" then
|
||||
op = op + parse_fpr(params[n]) * 2^6; n = n + 1
|
||||
op = op + shl(parse_fpr(params[n]), 6); n = n + 1
|
||||
elseif p == "G" then
|
||||
op = op + parse_fpr(params[n]) * 2^11; n = n + 1
|
||||
op = op + shl(parse_fpr(params[n]), 11); n = n + 1
|
||||
elseif p == "H" then
|
||||
op = op + parse_fpr(params[n]) * 2^16; n = n + 1
|
||||
op = op + shl(parse_fpr(params[n]), 16); n = n + 1
|
||||
elseif p == "R" then
|
||||
op = op + parse_fpr(params[n]) * 2^21; n = n + 1
|
||||
op = op + shl(parse_fpr(params[n]), 21); n = n + 1
|
||||
elseif p == "I" then
|
||||
op = op + parse_imm(params[n], 16, 0, 0, true); n = n + 1
|
||||
elseif p == "U" then
|
||||
@@ -783,8 +778,7 @@ map_op[".template__"] = function(params, template, nparams)
|
||||
elseif p == "Z" then
|
||||
op = op + parse_imm(params[n], 10, 6, 0, false); n = n + 1
|
||||
elseif p == "=" then
|
||||
local d = ((op - op % 2^11) / 2^11) % 32
|
||||
op = op + d * 2^16 -- Copy D to T for clz, clo.
|
||||
op = op + shl(band(op, 0xf800), 5) -- Copy D to T for clz, clo.
|
||||
else
|
||||
assert(false)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user