From f30aabe82f61dbd6901f6a75dadde0a64dc626d2 Mon Sep 17 00:00:00 2001 From: Mike Pall Date: Mon, 3 Aug 2026 10:44:17 +0200 Subject: Modernize jit.* Lua modules. --- src/jit/bc.lua | 43 +++++----- src/jit/bcsave.lua | 47 +++++----- src/jit/dis_arm.lua | 137 +++++++++++++++-------------- src/jit/dis_arm64.lua | 232 ++++++++++++++++++++++++-------------------------- src/jit/dis_mips.lua | 83 +++++++++--------- src/jit/dis_ppc.lua | 117 +++++++++++++------------ src/jit/dis_x86.lua | 152 ++++++++++++++++----------------- src/jit/dump.lua | 132 ++++++++++++++-------------- src/jit/p.lua | 14 +-- src/jit/v.lua | 4 +- 10 files changed, 472 insertions(+), 489 deletions(-) (limited to 'src') diff --git a/src/jit/bc.lua b/src/jit/bc.lua index 8014d6029..59bc35b83 100644 --- a/src/jit/bc.lua +++ b/src/jit/bc.lua @@ -43,9 +43,8 @@ local jit = require("jit") local jutil = require("jit.util") local vmdef = require("jit.vmdef") -local bit = require("bit") local sub, gsub, format = string.sub, string.gsub, string.format -local byte, band, shr = string.byte, bit.band, bit.rshift +local byte = string.byte local funcinfo, funcbc, funck = jutil.funcinfo, jutil.funcbc, jutil.funck local funcuvname = jutil.funcuvname local bcnames = vmdef.bcnames @@ -65,49 +64,49 @@ end local function bcline(func, pc, prefix) local ins, m = funcbc(func, pc) if not ins then return end - local ma, mb, mc = band(m, 7), band(m, 15*8), band(m, 15*128) - local a = band(shr(ins, 8), 0xff) - local oidx = 6*band(ins, 0xff) + local ma, mb, mc = m & 7, (m >> 3) & 15, (m >> 7) & 15 + local a = (ins >> 8) & 0xff + local oidx = 6 * (ins & 0xff) local op = sub(bcnames, oidx+1, oidx+6) local s = format("%04d %s %-6s %3s ", - pc, prefix or " ", op, ma == 0 and "" or a) - local d = shr(ins, 16) - if mc == 13*128 then -- BCMjump + pc, prefix or " ", op, ma == 0 ? "" : a) + local d = ins >> 16 + if mc == 13 then -- BCMjump return format("%s=> %04d\n", s, pc+d-0x7fff) end - if mb ~= 0 then - d = band(d, 0xff) + if mb != 0 then + d &= 0xff elseif mc == 0 then return s.."\n" end local kc - if mc == 10*128 then -- BCMstr + if mc == 10 then -- BCMstr kc = funck(func, -d-1) - kc = format(#kc > 40 and '"%.40s"~' or '"%s"', gsub(kc, "%c", ctlsub)) - elseif mc == 9*128 then -- BCMnum + kc = format(#kc > 40 ? '"%.40s"~' : '"%s"', gsub(kc, "%c", ctlsub)) + elseif mc == 9 then -- BCMnum kc = funck(func, d) - if op == "TSETM " then kc = kc - 2^52 end - elseif mc == 12*128 then -- BCMfunc + if op == "TSETM " then kc -= 2^52 end + elseif mc == 12 then -- BCMfunc local fi = funcinfo(funck(func, -d-1)) if fi.ffid then kc = vmdef.ffnames[fi.ffid] else kc = fi.loc end - elseif mc == 5*128 then -- BCMuv + elseif mc == 5 then -- BCMuv kc = funcuvname(func, d) end if ma == 5 then -- BCMuv local ka = funcuvname(func, a) if kc then kc = ka.." ; "..kc else kc = ka end end - if mb ~= 0 then - local b = shr(ins, 24) + if mb != 0 then + local b = ins >> 24 if kc then return format("%s%3d %3d ; %s\n", s, b, d, kc) end return format("%s%3d %3d\n", s, b, d) end if kc then return format("%s%3d ; %s\n", s, d, kc) end - if mc == 7*128 and d > 32767 then d = d - 65536 end -- BCMlits + if mc == 7 and d > 32767 then d -= 65536 end -- BCMlits return format("%s%3d\n", s, d) end @@ -117,7 +116,7 @@ local function bctargets(func) for pc=1,1000000000 do local ins, m = funcbc(func, pc) if not ins then break end - if band(m, 15*128) == 13*128 then target[pc+shr(ins, 16)-0x7fff] = true end + if m & (15 << 7) == (13 << 7) then target[pc+(ins >> 16)-0x7fff] = true end end return target end @@ -159,7 +158,7 @@ local function bclistoff() if active then active = false jit.attach(h_list) - if out and out ~= stdout and out ~= stderr then out:close() end + if out and out != stdout and out != stderr then out:close() end out = nil end end @@ -169,7 +168,7 @@ local function bcliston(outfile) if active then bclistoff() end if not outfile then outfile = os.getenv("LUAJIT_LISTFILE") end if outfile then - out = outfile == "-" and stdout or assert(io.open(outfile, "w")) + out = outfile == "-" ? stdout : assert(io.open(outfile, "w")) else out = stderr end diff --git a/src/jit/bcsave.lua b/src/jit/bcsave.lua index 7d19cb064..544b506c1 100644 --- a/src/jit/bcsave.lua +++ b/src/jit/bcsave.lua @@ -20,6 +20,7 @@ local LJBC_PREFIX = "luaJIT_BC_" local type, assert = type, assert local format = string.format local tremove, tconcat = table.remove, table.concat +local bswap = bit.bswap ------------------------------------------------------------------------------ @@ -111,7 +112,7 @@ local map_os = { local function checkarg(str, map, err) str = str:lower() local s = check(map[str], "unknown ", err) - return type(s) == "string" and s or str + return type(s) == "string" ? s : str end local function detecttype(str) @@ -142,7 +143,7 @@ end local function bcsave_tail(fp, output, s) local ok, err = fp:write(s) - if ok and output ~= "-" then ok, err = fp:close() end + if ok and output != "-" then ok, err = fp:close() end check(ok, "cannot write ", output, ": ", err) end @@ -179,12 +180,12 @@ static const unsigned char %s%s[] = { local t, n, m = {}, 0, 0 for i=1,#s do local b = tostring(string.byte(s, i)) - m = m + #b + 1 + m += #b + 1 if m > 78 then fp:write(tconcat(t, ",", 1, n), ",\n") n, m = 0, #b + 1 end - n = n + 1 + n += 1 t[n] = b end bcsave_tail(fp, output, tconcat(t, ",", 1, n).."\n};\n") @@ -248,19 +249,19 @@ typedef struct { -- Handle different host/target endianess. local function f32(x) return x end local f16, fofs = f32, f32 - if ffi.abi("be") ~= isbe then - f32 = bit.bswap - function f16(x) return bit.rshift(bit.bswap(x), 16) end + if ffi.abi("be") != isbe then + f32 = bswap + function f16(x) return bswap(x) >> 16 end if is64 then local two32 = ffi.cast("int64_t", 2^32) - function fofs(x) return bit.bswap(x)*two32 end + function fofs(x) return bswap(x)*two32 end else fofs = f32 end end -- Create ELF object and fill in header. - local o = ffi.new(is64 and "ELF64obj" or "ELF32obj") + local o = ffi.new(is64 ? "ELF64obj" : "ELF32obj") local hdr = o.hdr if ctx.os == "bsd" or ctx.os == "other" then -- Determine native hdr.eosabi. local bf = assert(io.open("/bin/ls", "rb")) @@ -272,8 +273,8 @@ typedef struct { hdr.emagic = "\127ELF" hdr.eosabi = ({ freebsd=9, netbsd=2, openbsd=12, solaris=6 })[ctx.os] or 0 end - hdr.eclass = is64 and 2 or 1 - hdr.eendian = isbe and 2 or 1 + hdr.eclass = is64 ? 2 : 1 + hdr.eendian = isbe ? 2 : 1 hdr.eversion = 1 hdr.type = f16(1) hdr.machine = f16(ai.m) @@ -294,7 +295,7 @@ typedef struct { sect.align = fofs(1) sect.name = f32(ofs) ffi.copy(o.space+ofs, name) - ofs = ofs + #name+1 + ofs += #name+1 end o.sect[1].type = f32(2) -- .symtab o.sect[1].link = f32(3) @@ -314,7 +315,7 @@ typedef struct { o.sect[3].ofs = fofs(sofs + ofs) o.sect[3].size = fofs(#symname+2) ffi.copy(o.space+ofs+1, symname) - ofs = ofs + #symname + 2 + ofs += #symname + 2 o.sect[4].type = f32(1) -- .rodata o.sect[4].flags = fofs(2) o.sect[4].ofs = fofs(sofs + ofs) @@ -381,8 +382,8 @@ typedef struct { local function f32(x) return x end local f16 = f32 if ffi.abi("be") then - f32 = bit.bswap - function f16(x) return bit.rshift(bit.bswap(x), 16) end + f32 = bswap + function f16(x) return bswap(x) >> 16 end end -- Create PE object and fill in header. @@ -422,7 +423,7 @@ typedef struct { o.strtabsize = f32(ofs + 4) o.sect[0].ofs = f32(ffi.offsetof(o, "space") + ofs) ffi.copy(o.space + ofs, symexport) - ofs = ofs + #symexport + ofs += #symexport o.sect[1].ofs = f32(ffi.offsetof(o, "space") + ofs) -- Write PE object file. @@ -475,11 +476,11 @@ typedef struct { ]] local symname = '_'..LJBC_PREFIX..ctx.modname local cputype, cpusubtype = 0x01000007, 3 - if ctx.arch ~= "x64" then + if ctx.arch != "x64" then check(ctx.arch == "arm64", "unsupported architecture for OSX") cputype, cpusubtype = 0x0100000c, 0 end - local function aligned(v, a) return bit.band(v+a-1, -a) end + local function aligned(v, a) return v+a-1 & -a end -- Create Mach-O object and fill in header. local o = ffi.new("mach_obj_64") @@ -579,7 +580,7 @@ local function docmd(...) local gc64 = "" while n <= #arg do local a = arg[n] - if type(a) == "string" and a:sub(1, 1) == "-" and a ~= "-" then + if type(a) == "string" and a:sub(1, 1) == "-" and a != "-" then tremove(arg, n) if a == "--" then break end for m=2,#a do @@ -595,9 +596,9 @@ local function docmd(...) elseif opt == "d" then ctx.mode = ctx.mode .. opt else - if arg[n] == nil or m ~= #a then usage() end + if arg[n] == nil or m != #a then usage() end if opt == "e" then - if n ~= 1 then usage() end + if n != 1 then usage() end ctx.string = true elseif opt == "n" then ctx.modname = checkmodname(tremove(arg, n)) @@ -615,7 +616,7 @@ local function docmd(...) end end else - n = n + 1 + n += 1 end end ctx.mode = ctx.mode .. strip .. gc64 @@ -623,7 +624,7 @@ local function docmd(...) if #arg == 0 or #arg > 2 then usage() end bclist(ctx, arg[1], arg[2] or "-") else - if #arg ~= 2 then usage() end + if #arg != 2 then usage() end bcsave(ctx, arg[1], arg[2]) end end diff --git a/src/jit/dis_arm.lua b/src/jit/dis_arm.lua index 0adc799de..e2334ebbc 100644 --- a/src/jit/dis_arm.lua +++ b/src/jit/dis_arm.lua @@ -15,8 +15,7 @@ local sub, byte, format = string.sub, string.byte, string.format local match, gmatch = string.match, string.gmatch local concat = table.concat local bit = require("bit") -local band, bor, ror, tohex = bit.band, bit.bor, bit.ror, bit.tohex -local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift +local ror, tohex = bit.ror, bit.tohex ------------------------------------------------------------------------------ -- Opcode maps @@ -373,7 +372,7 @@ local map_datar = { [16] = { shift = 7, mask = 1, [0] = map_misc, map_mulh, }, _ = { shift = 0, mask = 0xffffffff, - [bor(0xe1a00000)] = "nop", + [0xe1a00000|0] = "nop", _ = map_data, } }, @@ -427,7 +426,7 @@ local function putop(ctx, text, operands) local sym = ctx.symtab[ctx.rel] if sym then extra = "\t->"..sym - elseif band(ctx.op, 0x0e000000) ~= 0x0a000000 then + elseif ctx.op & 0x0e000000 != 0x0a000000 then extra = "\t; 0x"..tohex(ctx.rel) end end @@ -448,47 +447,47 @@ end -- Format operand 2 of load/store opcodes. local function fmtload(ctx, op, pos) - local base = map_gpr[band(rshift(op, 16), 15)] + local base = map_gpr[(op >> 16) & 15] local x, ofs - local ext = (band(op, 0x04000000) == 0) - if not ext and band(op, 0x02000000) == 0 then - ofs = band(op, 4095) - if band(op, 0x00800000) == 0 then ofs = -ofs end + local ext = (op & 0x04000000 == 0) + if not ext and op & 0x02000000 == 0 then + ofs = op & 4095 + if op & 0x00800000 == 0 then ofs = -ofs end if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end ofs = "#"..ofs - elseif ext and band(op, 0x00400000) ~= 0 then - ofs = band(op, 15) + band(rshift(op, 4), 0xf0) - if band(op, 0x00800000) == 0 then ofs = -ofs end + elseif ext and op & 0x00400000 != 0 then + ofs = (op & 0x0f) | ((op >> 4) & 0xf0) + if op & 0x00800000 == 0 then ofs = -ofs end if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end ofs = "#"..ofs else - ofs = map_gpr[band(op, 15)] - if ext or band(op, 0xfe0) == 0 then - elseif band(op, 0xfe0) == 0x60 then + ofs = map_gpr[op & 15] + if ext or op & 0xfe0 == 0 then + elseif op & 0xfe0 == 0x60 then ofs = format("%s, rrx", ofs) else - local sh = band(rshift(op, 7), 31) + local sh = (op >> 7) & 31 if sh == 0 then sh = 32 end - ofs = format("%s, %s #%d", ofs, map_shift[band(rshift(op, 5), 3)], sh) + ofs = format("%s, %s #%d", ofs, map_shift[(op >> 5) & 3], sh) end - if band(op, 0x00800000) == 0 then ofs = "-"..ofs end + if op & 0x00800000 == 0 then ofs = "-"..ofs end end if ofs == "#0" then x = format("[%s]", base) - elseif band(op, 0x01000000) == 0 then + elseif op & 0x01000000 == 0 then x = format("[%s], %s", base, ofs) else x = format("[%s, %s]", base, ofs) end - if band(op, 0x01200000) == 0x01200000 then x = x.."!" end + if op & 0x01200000 == 0x01200000 then x ..= "!" end return x end -- Format operand 2 of vector load/store opcodes. local function fmtvload(ctx, op, pos) - local base = map_gpr[band(rshift(op, 16), 15)] - local ofs = band(op, 255)*4 - if band(op, 0x00800000) == 0 then ofs = -ofs end + local base = map_gpr[(op >> 16) & 15] + local ofs = (op & 255) << 2 + if op & 0x00800000 == 0 then ofs = -ofs end if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end if ofs == 0 then return format("[%s]", base) @@ -499,9 +498,9 @@ end local function fmtvr(op, vr, sh0, sh1) if vr == "s" then - return format("s%d", 2*band(rshift(op, sh0), 15)+band(rshift(op, sh1), 1)) + return format("s%d", ((op >> sh0-1) & 0x1e) | ((op >> sh1) & 1)) else - return format("d%d", band(rshift(op, sh0), 15)+band(rshift(op, sh1-4), 16)) + return format("d%d", ((op >> sh0) & 15) | ((op >> sh1-4) & 16)) end end @@ -509,7 +508,7 @@ end local function disass_ins(ctx) local pos = ctx.pos local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) - local op = bor(lshift(b3, 24), lshift(b2, 16), lshift(b1, 8), b0) + local op = (b3 << 24) | (b2 << 16) | (b1 << 8) | b0 local operands = {} local suffix = "" local last, name, pat @@ -517,35 +516,35 @@ local function disass_ins(ctx) ctx.op = op ctx.rel = nil - local cond = rshift(op, 28) + local cond = op >> 28 local opat if cond == 15 then - opat = map_uncondins[band(rshift(op, 25), 7)] + opat = map_uncondins[(op >> 25) & 7] else - if cond ~= 14 then suffix = map_cond[cond] end - opat = map_condins[band(rshift(op, 25), 7)] + if cond != 14 then suffix = map_cond[cond] end + opat = map_condins[(op >> 25) & 7] end - while type(opat) ~= "string" do + while type(opat) != "string" do if not opat then return unknown(ctx) end - opat = opat[band(rshift(op, opat.shift), opat.mask)] or opat._ + opat = opat[(op >> opat.shift) & opat.mask] or opat._ end name, pat = match(opat, "^([a-z0-9]*)(.*)") if sub(pat, 1, 1) == "." then local s2, p2 = match(pat, "^([a-z0-9.]*)(.*)") - suffix = suffix..s2 + suffix ..= s2 pat = p2 end for p in gmatch(pat, ".") do local x = nil if p == "D" then - x = map_gpr[band(rshift(op, 12), 15)] + x = map_gpr[(op >> 12) & 15] elseif p == "N" then - x = map_gpr[band(rshift(op, 16), 15)] + x = map_gpr[(op >> 16) & 15] elseif p == "S" then - x = map_gpr[band(rshift(op, 8), 15)] + x = map_gpr[(op >> 8) & 15] elseif p == "M" then - x = map_gpr[band(op, 15)] + x = map_gpr[op & 15] elseif p == "d" then x = fmtvr(op, vr, 12, 22) elseif p == "n" then @@ -553,20 +552,20 @@ local function disass_ins(ctx) elseif p == "m" then x = fmtvr(op, vr, 0, 5) elseif p == "P" then - if band(op, 0x02000000) ~= 0 then - x = ror(band(op, 255), 2*band(rshift(op, 8), 15)) + if op & 0x02000000 != 0 then + x = ror(op & 0xff, (op >> 7) & 0x1e) else - x = map_gpr[band(op, 15)] - if band(op, 0xff0) ~= 0 then + x = map_gpr[op & 15] + if op & 0xff0 != 0 then operands[#operands+1] = x - local s = map_shift[band(rshift(op, 5), 3)] + local s = map_shift[(op >> 5) & 3] local r = nil - if band(op, 0xf90) == 0 then + if op & 0xf90 == 0 then if s == "ror" then s = "rrx" else r = "#32" end - elseif band(op, 0x10) == 0 then - r = "#"..band(rshift(op, 7), 31) + elseif op & 0x10 == 0 then + r = "#"..((op >> 7) & 31) else - r = map_gpr[band(rshift(op, 8), 15)] + r = map_gpr[(op >> 8) & 15] end if name == "mov" then name = s; x = r elseif r then x = format("%s %s", s, r) @@ -578,8 +577,8 @@ local function disass_ins(ctx) elseif p == "l" then x = fmtvload(ctx, op, pos) elseif p == "B" then - local addr = ctx.addr + pos + 8 + arshift(lshift(op, 8), 6) - if cond == 15 then addr = addr + band(rshift(op, 23), 2) end + local addr = ctx.addr + pos + 8 + ((op << 8) ~>> 6) + if cond == 15 then addr += (op >> 23) & 2 end ctx.rel = addr x = "0x"..tohex(addr) elseif p == "F" then @@ -587,52 +586,52 @@ local function disass_ins(ctx) elseif p == "G" then vr = "d" elseif p == "." then - suffix = suffix..(vr == "s" and ".f32" or ".f64") + suffix ..= vr == "s" ? ".f32" : ".f64" elseif p == "R" then - if band(op, 0x00200000) ~= 0 and #operands == 1 then + if op & 0x00200000 != 0 and #operands == 1 then operands[1] = operands[1].."!" end local t = {} for i=0,15 do - if band(rshift(op, i), 1) == 1 then t[#t+1] = map_gpr[i] end + if (op >> i) & 1 == 1 then t[#t+1] = map_gpr[i] end end x = "{"..concat(t, ", ").."}" elseif p == "r" then - if band(op, 0x00200000) ~= 0 and #operands == 2 then + if op & 0x00200000 != 0 and #operands == 2 then operands[1] = operands[1].."!" end local s = tonumber(sub(last, 2)) - local n = band(op, 255) - if vr == "d" then n = rshift(n, 1) end + local n = op & 0xff + if vr == "d" then n >>= 1 end operands[#operands] = format("{%s-%s%d}", last, vr, s+n-1) elseif p == "W" then - x = band(op, 0x0fff) + band(rshift(op, 4), 0xf000) + x = (op & 0x0fff) | ((op >> 4) & 0xf000) elseif p == "T" then - x = "#0x"..tohex(band(op, 0x00ffffff), 6) + x = "#0x"..tohex(op & 0x00ffffff, 6) elseif p == "U" then - x = band(rshift(op, 7), 31) + x = (op >> 7) & 31 if x == 0 then x = nil end elseif p == "u" then - x = band(rshift(op, 7), 31) - if band(op, 0x40) == 0 then - if x == 0 then x = nil else x = "lsl #"..x end + x = (op >> 7) & 31 + if op & 0x40 == 0 then + x = x == 0 ? nil : "lsl #"..x else - if x == 0 then x = "asr #32" else x = "asr #"..x end + x = x == 0 ? "asr #32" : "asr #"..x end elseif p == "v" then - x = band(rshift(op, 7), 31) + x = (op >> 7) & 31 elseif p == "w" then - x = band(rshift(op, 16), 31) + x = (op >> 16) & 31 elseif p == "x" then - x = band(rshift(op, 16), 31) + 1 + x = ((op >> 16) & 31) + 1 elseif p == "X" then - x = band(rshift(op, 16), 31) - last + 1 + x = ((op >> 16) & 31) - last + 1 elseif p == "Y" then - x = band(rshift(op, 12), 0xf0) + band(op, 0x0f) + x = ((op >> 12) & 0xf0) | (op & 0x0f) elseif p == "K" then - x = "#0x"..tohex(band(rshift(op, 4), 0x0000fff0) + band(op, 15), 4) + x = "#0x"..tohex(((op >> 4) & 0xfff0) | (op & 0x000f), 4) elseif p == "s" then - if band(op, 0x00100000) ~= 0 then suffix = "s"..suffix end + if op & 0x00100000 != 0 then suffix = "s"..suffix end else assert(false) end @@ -651,7 +650,7 @@ end -- Disassemble a block of code. local function disass_block(ctx, ofs, len) if not ofs then ofs = 0 end - local stop = len and ofs+len or #ctx.code + local stop = len ? ofs+len : #ctx.code ctx.pos = ofs ctx.rel = nil while ctx.pos < stop do disass_ins(ctx) end diff --git a/src/jit/dis_arm64.lua b/src/jit/dis_arm64.lua index 896fab791..7464421e4 100644 --- a/src/jit/dis_arm64.lua +++ b/src/jit/dis_arm64.lua @@ -18,9 +18,7 @@ local sub, byte, format = string.sub, string.byte, string.format local match, gmatch, gsub = string.match, string.gmatch, string.gsub local concat = table.concat local bit = require("bit") -local band, bor, bxor, tohex = bit.band, bit.bor, bit.bxor, bit.tohex -local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift -local ror = bit.ror +local ror, tohex = bit.ror, bit.tohex ------------------------------------------------------------------------------ -- Opcode maps @@ -782,35 +780,35 @@ end local imm13_rep = { 0x55555555, 0x11111111, 0x01010101, 0x00010001, 0x00000001 } local function decode_imm13(op) - local imms = band(rshift(op, 10), 63) - local immr = band(rshift(op, 16), 63) - if band(op, 0x00400000) == 0 then + local imms = (op >> 10) & 63 + local immr = (op >> 16) & 63 + if op & 0x00400000 == 0 then local len = 5 if imms >= 56 then if imms >= 60 then len = 1 else len = 2 end elseif imms >= 48 then len = 3 elseif imms >= 32 then len = 4 end - local l = lshift(1, len)-1 - local s = band(imms, l) - local r = band(immr, l) - local imm = ror(rshift(-1, 31-s), r) - if len ~= 5 then imm = band(imm, lshift(1, l)-1) + rshift(imm, 31-l) end - imm = imm * imm13_rep[len] + local l = (1 << len) - 1 + local s = imms & l + local r = immr & l + local imm = ror(-1 >> 31-s, r) + if len != 5 then imm = (imm & ((1 << l) - 1)) | (imm >> 31-l) end + imm *= imm13_rep[len] local ix = fmt_hex32(imm) - if rshift(op, 31) ~= 0 then + if op >> 31 != 0 then return ix..tohex(imm) else return ix end else local lo, hi = -1, 0 - if imms < 32 then lo = rshift(-1, 31-imms) else hi = rshift(-1, 63-imms) end - if immr ~= 0 then + if imms < 32 then lo = -1 >> 31-imms else hi = -1 >> 63-imms end + if immr != 0 then lo, hi = ror(lo, immr), ror(hi, immr) - local x = immr == 32 and 0 or band(bxor(lo, hi), lshift(-1, 32-immr)) - lo, hi = bxor(lo, x), bxor(hi, x) + local x = immr == 32 ? 0 : (lo ~ hi) & (-1 << 32-immr) + lo, hi = lo ~ x, hi ~ x if immr >= 32 then lo, hi = hi, lo end end - if hi ~= 0 then + if hi != 0 then return fmt_hex32(hi)..tohex(lo) else return fmt_hex32(lo) @@ -820,33 +818,31 @@ end local function parse_immpc(op, name) if name == "b" or name == "bl" then - return arshift(lshift(op, 6), 4) + return (op << 6) ~>> 4 elseif name == "adr" or name == "adrp" then - local immlo = band(rshift(op, 29), 3) - local immhi = lshift(arshift(lshift(op, 8), 13), 2) - return bor(immhi, immlo) + return (((op << 8) ~>> 13) << 2) | ((op >> 29) & 3) elseif name == "tbz" or name == "tbnz" then - return lshift(arshift(lshift(op, 13), 18), 2) + return ((op << 13) ~>> 18) << 2 else - return lshift(arshift(lshift(op, 8), 13), 2) + return ((op << 8) ~>> 13) << 2 end end local function parse_fpimm8(op) - local sign = band(op, 0x100000) == 0 and 1 or -1 - local exp = bxor(rshift(arshift(lshift(op, 12), 5), 24), 0x80) - 131 - local frac = 16+band(rshift(op, 13), 15) + local sign = op & 0x100000 == 0 ? 1 : -1 + local exp = ((((op << 12) ~>> 5) >> 24) ~ 0x80) - 131 + local frac = 16 + ((op >> 13) & 15) return sign * frac * 2^exp end local function decode_fpmovi(op) - local lo = rshift(op, 5) - local hi = rshift(op, 9) - lo = bor(band(lo, 1) * 0xff, band(lo, 2) * 0x7f80, band(lo, 4) * 0x3fc000, - band(lo, 8) * 0x1fe00000) - hi = bor(band(hi, 1) * 0xff, band(hi, 0x80) * 0x1fe, - band(hi, 0x100) * 0xff00, band(hi, 0x200) * 0x7f8000) - if hi ~= 0 then + local lo = op >> 5 + local hi = op >> 9 + lo = ((lo & 1) * 0xff) | ((lo & 2) * 0x7f80) | + ((lo & 4) * 0x3fc000) | ((lo & 8) * 0x1fe00000) + hi = ((hi & 1) * 0xff) | ((hi & 0x80) * 0x1fe) | + ((hi & 0x100) * 0xff00) | ((hi & 0x200) * 0x7f8000) + if hi != 0 then return fmt_hex32(hi)..tohex(lo) else return fmt_hex32(lo) @@ -861,7 +857,7 @@ local function prefer_bfx(sf, uns, imms, immr) if sf == 0 and (imms == 7 or imms == 15) then return false end - if sf ~= 0 and uns == 0 and (imms == 7 or imms == 15 or imms == 31) then + if sf != 0 and uns == 0 and (imms == 7 or imms == 15 or imms == 31) then return false end end @@ -872,7 +868,7 @@ end local function disass_ins(ctx) local pos = ctx.pos local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) - local op = bor(lshift(b3, 24), lshift(b2, 16), lshift(b1, 8), b0) + local op = (b3 << 24) | (b2 << 16) | (b1 << 8) | b0 local operands = {} local suffix = "" local last, name, pat @@ -881,26 +877,26 @@ local function disass_ins(ctx) ctx.rel = nil last = nil local opat - opat = map_init[band(rshift(op, 25), 15)] - while type(opat) ~= "string" do + opat = map_init[(op >> 25) & 15] + while type(opat) != "string" do if not opat then return unknown(ctx) end - opat = opat[band(rshift(op, opat.shift), opat.mask)] or opat._ + opat = opat[(op >> opat.shift) & opat.mask] or opat._ end name, pat = match(opat, "^([a-z0-9]*)(.*)") local altname, pat2 = match(pat, "|([a-z0-9_.|]*)(.*)") if altname then pat = pat2 end if sub(pat, 1, 1) == "." then local s2, p2 = match(pat, "^([a-z0-9.]*)(.*)") - suffix = suffix..s2 + suffix ..= s2 pat = p2 end local rt = match(pat, "[gf]") if rt then if rt == "g" then - map_reg = band(op, 0x80000000) ~= 0 and map_regs.x or map_regs.w + map_reg = map_regs[op & 0x80000000 != 0 ? "x" : "w"] else - map_reg = band(op, 0x400000) ~= 0 and map_regs.d or map_regs.s + map_reg = map_regs[op & 0x400000 != 0 ? "d" : "s"] end end @@ -909,41 +905,41 @@ local function disass_ins(ctx) for p in gmatch(pat, ".") do local x = nil if p == "D" then - local regnum = band(op, 31) - x = rt and map_reg[regnum] or match_reg(p, pat, regnum) + local regnum = op & 31 + x = rt ? map_reg[regnum] : match_reg(p, pat, regnum) elseif p == "N" then - local regnum = band(rshift(op, 5), 31) - x = rt and map_reg[regnum] or match_reg(p, pat, regnum) + local regnum = (op >> 5) & 31 + x = rt ? map_reg[regnum] : match_reg(p, pat, regnum) elseif p == "M" then - local regnum = band(rshift(op, 16), 31) - x = rt and map_reg[regnum] or match_reg(p, pat, regnum) + local regnum = (op >> 16) & 31 + x = rt ? map_reg[regnum] : match_reg(p, pat, regnum) elseif p == "A" then - local regnum = band(rshift(op, 10), 31) - x = rt and map_reg[regnum] or match_reg(p, pat, regnum) + local regnum = (op >> 10) & 31 + x = rt ? map_reg[regnum] : match_reg(p, pat, regnum) elseif p == "B" then local addr = ctx.addr + pos + parse_immpc(op, name) ctx.rel = addr x = format("0x%08x", addr) elseif p == "T" then - x = bor(band(rshift(op, 26), 32), band(rshift(op, 19), 31)) + x = ((op >> 26) & 32) | ((op >> 19) & 31) elseif p == "V" then - x = band(op, 15) + x = op & 15 elseif p == "C" then - x = map_cond[band(rshift(op, 12), 15)] + x = map_cond[(op >> 12) & 15] elseif p == "c" then - local rn = band(rshift(op, 5), 31) - local rm = band(rshift(op, 16), 31) - local cond = band(rshift(op, 12), 15) - local invc = bxor(cond, 1) + local rn = (op >> 5) & 31 + local rm = (op >> 16) & 31 + local cond = (op >> 12) & 15 + local invc = cond ~ 1 x = map_cond[cond] - if altname and cond ~= 14 and cond ~= 15 then + if altname and cond != 14 and cond != 15 then local a1, a2 = match(altname, "([^|]*)|(.*)") if rn == rm then local n = #operands operands[n] = nil x = map_cond[invc] - if rn ~= 31 then - if a1 then name = a1 else name = altname end + if rn != 31 then + name = a1 ?? altname else operands[n-1] = nil name = a2 @@ -951,65 +947,59 @@ local function disass_ins(ctx) end end elseif p == "W" then - x = band(rshift(op, 5), 0xffff) + x = (op >> 5) & 0xffff elseif p == "Y" then - x = band(rshift(op, 5), 0xffff) - local hw = band(rshift(op, 21), 3) - if altname and (hw == 0 or x ~= 0) then + x = (op >> 5) & 0xffff + local hw = (op >> 21) & 3 + if altname and (hw == 0 or x != 0) then name = altname end elseif p == "L" then - local rn = map_regs.x[band(rshift(op, 5), 31)] - local imm9 = arshift(lshift(op, 11), 23) - if band(op, 0x800) ~= 0 then + local rn = map_regs.x[(op >> 5) & 31] + local imm9 = (op << 11) ~>> 23 + if op & 0x800 != 0 then x = "["..rn..", #"..imm9.."]!" else x = "["..rn.."], #"..imm9 end elseif p == "U" then - local rn = map_regs.x[band(rshift(op, 5), 31)] - local sz = band(rshift(op, 30), 3) - local imm12 = lshift(rshift(lshift(op, 10), 20), sz) - if imm12 ~= 0 then + local rn = map_regs.x[(op >> 5) & 31] + local sz = (op >> 30) & 3 + local imm12 = ((op << 10) >> 20) << sz + if imm12 != 0 then x = "["..rn..", #"..imm12.."]" else x = "["..rn.."]" end elseif p == "K" then - local rn = map_regs.x[band(rshift(op, 5), 31)] - local imm9 = arshift(lshift(op, 11), 23) - if imm9 ~= 0 then + local rn = map_regs.x[(op >> 5) & 31] + local imm9 = (op << 11) ~>> 23 + if imm9 != 0 then x = "["..rn..", #"..imm9.."]" else x = "["..rn.."]" end elseif p == "O" then - local rn, rm = map_regs.x[band(rshift(op, 5), 31)] - local m = band(rshift(op, 13), 1) - if m == 0 then - rm = map_regs.w[band(rshift(op, 16), 31)] - else - rm = map_regs.x[band(rshift(op, 16), 31)] - end + local rn = map_regs.x[(op >> 5) & 31] + local rm = map_regs[op & (1 << 13) == 0 ? "w" : "x"][(op >> 16) & 31] x = "["..rn..", "..rm - local opt = band(rshift(op, 13), 7) - local s = band(rshift(op, 12), 1) - local sz = band(rshift(op, 30), 3) - -- extension to be applied + local opt = (op >> 13) & 7 + local s = (op >> 12) & 1 + local sz = (op >> 30) & 3 if opt == 3 then - if s == 0 then x = x.."]" + if s == 0 then x ..= "]" else x = x..", lsl #"..sz.."]" end elseif opt == 2 or opt == 6 or opt == 7 then if s == 0 then x = x..", "..map_extend[opt].."]" else x = x..", "..map_extend[opt].." #"..sz.."]" end else - x = x.."]" + x ..= "]" end elseif p == "P" then - local sh = 2 + rshift(op, 31 - band(rshift(op, 26), 1)) - local imm7 = lshift(arshift(lshift(op, 10), 25), sh) - local rn = map_regs.x[band(rshift(op, 5), 31)] - local ind = band(rshift(op, 23), 3) + local sh = 2 + (op >> (31 - ((op >> 26) & 1))) + local imm7 = ((op << 10) ~>> 25) << sh + local rn = map_regs.x[(op >> 5) & 31] + local ind = (op >> 23) & 3 if ind == 1 then x = "["..rn.."], #"..imm7 elseif ind == 2 then @@ -1022,9 +1012,9 @@ local function disass_ins(ctx) x = "["..rn..", #"..imm7.."]!" end elseif p == "I" then - local shf = band(rshift(op, 22), 3) - local imm12 = band(rshift(op, 10), 0x0fff) - local rn, rd = band(rshift(op, 5), 31), band(op, 31) + local shf = (op >> 22) & 3 + local imm12 = (op >> 10) & 0x0fff + local rn, rd = (op >> 5) & 31, op & 31 if altname == "mov" and shf == 0 and imm12 == 0 and (rn == 31 or rd == 31) then name = altname x = nil @@ -1036,22 +1026,22 @@ local function disass_ins(ctx) elseif p == "i" then x = "#0x"..decode_imm13(op) elseif p == "1" then - immr = band(rshift(op, 16), 63) + immr = (op >> 16) & 63 x = immr elseif p == "2" then - x = band(rshift(op, 10), 63) + x = (op >> 10) & 63 if altname then local a1, a2, a3, a4, a5, a6 = match(altname, "([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)|(.*)") - local sf = band(rshift(op, 26), 32) - local uns = band(rshift(op, 30), 1) + local sf = (op >> 26) & 32 + local uns = (op >> 30) & 1 if prefer_bfx(sf, uns, x, immr) then name = a2 x = x - immr + 1 elseif immr == 0 and x == 7 then local n = #operands operands[n] = nil - if sf ~= 0 then + if sf != 0 then operands[n-1] = gsub(operands[n-1], "x", "w") end last = operands[n-1] @@ -1060,7 +1050,7 @@ local function disass_ins(ctx) elseif immr == 0 and x == 15 then local n = #operands operands[n] = nil - if sf ~= 0 then + if sf != 0 then operands[n-1] = gsub(operands[n-1], "x", "w") end last = operands[n-1] @@ -1071,7 +1061,7 @@ local function disass_ins(ctx) name = a4 local n = #operands operands[n] = nil - if sf ~= 0 then + if sf != 0 then operands[n-1] = gsub(operands[n-1], "x", "w") end last = operands[n-1] @@ -1079,7 +1069,7 @@ local function disass_ins(ctx) name = a3 end x = nil - elseif band(x, 31) ~= 31 and immr == x+1 and name == "ubfm" then + elseif x & 31 != 31 and immr == x+1 and name == "ubfm" then name = a4 last = "#"..(sf+32 - immr) operands[#operands] = last @@ -1088,28 +1078,28 @@ local function disass_ins(ctx) name = a1 last = "#"..(sf+32 - immr) operands[#operands] = last - x = x + 1 + x += 1 end end elseif p == "3" then - x = band(rshift(op, 10), 63) + x = (op >> 10) & 63 if altname then local a1, a2 = match(altname, "([^|]*)|(.*)") if x < immr then name = a1 - local sf = band(rshift(op, 26), 32) + local sf = (op >> 26) & 32 last = "#"..(sf+32 - immr) operands[#operands] = last - x = x + 1 + x += 1 else name = a2 x = x - immr + 1 end end elseif p == "4" then - x = band(rshift(op, 10), 63) - local rn = band(rshift(op, 5), 31) - local rm = band(rshift(op, 16), 31) + x = (op >> 10) & 63 + local rn = (op >> 5) & 31 + local rm = (op >> 16) & 31 if altname and rn == rm then local n = #operands operands[n] = nil @@ -1117,30 +1107,30 @@ local function disass_ins(ctx) name = altname end elseif p == "5" then - x = band(rshift(op, 16), 31) + x = (op >> 16) & 31 elseif p == "S" then - x = band(rshift(op, 10), 63) + x = (op >> 10) & 63 if x == 0 then x = nil - else x = map_shift[band(rshift(op, 22), 3)].." #"..x end + else x = map_shift[(op >> 22) & 3].." #"..x end elseif p == "X" then - local opt = band(rshift(op, 13), 7) + local opt = (op >> 13) & 7 -- Width specifier . - if opt ~= 3 and opt ~= 7 then - last = map_regs.w[band(rshift(op, 16), 31)] + if opt != 3 and opt != 7 then + last = map_regs.w[(op >> 16) & 31] operands[#operands] = last end - x = band(rshift(op, 10), 7) + x = (op >> 10) & 7 -- Extension. - if opt == 2 + band(rshift(op, 31), 1) and - band(rshift(op, second0 and 5 or 0), 31) == 31 then + if opt == 2 + ((op >> 31) & 1) and + (op >> (second0 ? 5 : 0)) & 31 == 31 then if x == 0 then x = nil else x = "lsl #"..x end else - if x == 0 then x = map_extend[band(rshift(op, 13), 7)] - else x = map_extend[band(rshift(op, 13), 7)].." #"..x end + if x == 0 then x = map_extend[(op >> 13) & 7] + else x = map_extend[(op >> 13) & 7].." #"..x end end elseif p == "R" then - x = band(rshift(op,21), 3) + x = (op >> 21) & 3 if x == 0 then x = nil else x = "lsl #"..x*16 end elseif p == "z" then diff --git a/src/jit/dis_mips.lua b/src/jit/dis_mips.lua index fece89370..2ff68f6be 100644 --- a/src/jit/dis_mips.lua +++ b/src/jit/dis_mips.lua @@ -15,8 +15,7 @@ local byte, format = string.byte, string.format local match, gmatch = string.match, string.gmatch local concat = table.concat local bit = require("bit") -local band, bor, tohex = bit.band, bit.bor, bit.tohex -local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift +local tohex = bit.tohex ------------------------------------------------------------------------------ -- Extended opcode maps common to all MIPS releases @@ -477,13 +476,13 @@ end local function get_be(ctx) local pos = ctx.pos local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) - return bor(lshift(b0, 24), lshift(b1, 16), lshift(b2, 8), b3) + return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3 end local function get_le(ctx) local pos = ctx.pos local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) - return bor(lshift(b3, 24), lshift(b2, 16), lshift(b1, 8), b0) + return (b3 << 24) | (b2 << 16) | (b1 << 8) | b0 end -- Disassemble a single instruction. @@ -494,13 +493,13 @@ local function disass_ins(ctx) ctx.op = op ctx.rel = nil - local opat = ctx.map_pri[rshift(op, 26)] - while type(opat) ~= "string" do + local opat = ctx.map_pri[op >> 26] + while type(opat) != "string" do if not opat then return unknown(ctx) end if opat.maprs then - opat = opat[opat.maprs(band(rshift(op,21),31), band(rshift(op,16),31))] + opat = opat[opat.maprs((op >> 21) & 31, (op >> 16) & 31)] else - opat = opat[band(rshift(op, opat.shift), opat.mask)] or opat._ + opat = opat[(op >> opat.shift) & opat.mask] or opat._ end end local name, pat = match(opat, "^([a-z0-9_.]*)(.*)") @@ -510,82 +509,82 @@ local function disass_ins(ctx) for p in gmatch(pat, ".") do local x = nil if p == "S" then - x = map_gpr[band(rshift(op, 21), 31)] + x = map_gpr[(op >> 21) & 31] elseif p == "T" then - x = map_gpr[band(rshift(op, 16), 31)] + x = map_gpr[(op >> 16) & 31] elseif p == "D" then - x = map_gpr[band(rshift(op, 11), 31)] + x = map_gpr[(op >> 11) & 31] elseif p == "F" then - x = "f"..band(rshift(op, 6), 31) + x = "f"..((op >> 6) & 31) elseif p == "G" then - x = "f"..band(rshift(op, 11), 31) + x = "f"..((op >> 11) & 31) elseif p == "H" then - x = "f"..band(rshift(op, 16), 31) + x = "f"..((op >> 16) & 31) elseif p == "R" then - x = "f"..band(rshift(op, 21), 31) + x = "f"..((op >> 21) & 31) elseif p == "A" then - x = band(rshift(op, 6), 31) + x = (op >> 6) & 31 elseif p == "a" then - x = band(rshift(op, 6), 7) + x = (op >> 6) & 7 elseif p == "E" then - x = band(rshift(op, 6), 31) + 32 + x = ((op >> 6) & 31) + 32 elseif p == "M" then - x = band(rshift(op, 11), 31) + x = (op >> 11) & 31 elseif p == "N" then - x = band(rshift(op, 16), 31) + x = (op >> 16) & 31 elseif p == "C" then - x = band(rshift(op, 18), 7) + x = (op >> 18) & 7 if x == 0 then x = nil end elseif p == "K" then - x = band(rshift(op, 11), 31) + 1 + x = ((op >> 11) & 31) + 1 elseif p == "P" then - x = band(rshift(op, 11), 31) + 33 + x = ((op >> 11) & 31) + 33 elseif p == "L" then - x = band(rshift(op, 11), 31) - last + 1 + x = ((op >> 11) & 31) - last + 1 elseif p == "Q" then - x = band(rshift(op, 11), 31) - last + 33 + x = ((op >> 11) & 31) - last + 33 elseif p == "I" then - x = arshift(lshift(op, 16), 16) + x = (op << 16) ~>> 16 elseif p == "2" then - x = arshift(lshift(op, 13), 11) + x = (op << 13) ~>> 11 elseif p == "3" then - x = arshift(lshift(op, 14), 11) + x = (op << 14) ~>> 11 elseif p == "U" then - x = band(op, 0xffff) + x = op & 0xffff elseif p == "O" then - local disp = arshift(lshift(op, 16), 16) + local disp = (op << 16) ~>> 16 operands[#operands] = format("%d(%s)", disp, last) elseif p == "X" then - local index = map_gpr[band(rshift(op, 16), 31)] + local index = map_gpr[(op >> 16) & 31] operands[#operands] = format("%s(%s)", index, last) elseif p == "B" then - x = ctx.addr + ctx.pos + arshift(lshift(op, 16), 14) + 4 + x = ctx.addr + ctx.pos + ((op << 16) ~>> 14) + 4 ctx.rel = x x = format("0x%08x", x) elseif p == "b" then - x = ctx.addr + ctx.pos + arshift(lshift(op, 11), 9) + 4 + x = ctx.addr + ctx.pos + ((op << 11) ~>> 9) + 4 ctx.rel = x x = format("0x%08x", x) elseif p == "#" then - x = ctx.addr + ctx.pos + arshift(lshift(op, 6), 4) + 4 + x = ctx.addr + ctx.pos + ((op << 6) ~>> 4) + 4 ctx.rel = x x = format("0x%08x", x) elseif p == "J" then local a = ctx.addr + ctx.pos - x = a - band(a, 0x0fffffff) + band(op, 0x03ffffff)*4 + x = a - (a & 0x0fffffff) + ((op & 0x03ffffff) << 2) ctx.rel = x x = format("0x%08x", x) elseif p == "V" then - x = band(rshift(op, 8), 7) + x = (op >> 8) & 7 if x == 0 then x = nil end elseif p == "W" then - x = band(op, 7) + x = op & 7 if x == 0 then x = nil end elseif p == "Y" then - x = band(rshift(op, 6), 0x000fffff) + x = (op >> 6) & 0x000fffff if x == 0 then x = nil end elseif p == "Z" then - x = band(rshift(op, 6), 1023) + x = (op >> 6) & 1023 if x == 0 then x = nil end elseif p == "0" then if last == "r0" or last == 0 then @@ -616,9 +615,9 @@ end -- Disassemble a block of code. local function disass_block(ctx, ofs, len) if not ofs then ofs = 0 end - local stop = len and ofs+len or #ctx.code - stop = stop - stop % 4 - ctx.pos = ofs - ofs % 4 + local stop = len ? ofs+len : #ctx.code + stop &= -4 + ctx.pos = ofs & -4 ctx.rel = nil while ctx.pos < stop do disass_ins(ctx) end end diff --git a/src/jit/dis_ppc.lua b/src/jit/dis_ppc.lua index d8f4cfb78..90eef9408 100644 --- a/src/jit/dis_ppc.lua +++ b/src/jit/dis_ppc.lua @@ -17,8 +17,7 @@ local byte, format = string.byte, string.format local match, gmatch, gsub = string.match, string.gmatch, string.gsub local concat = table.concat local bit = require("bit") -local band, bor, tohex = bit.band, bit.bor, bit.tohex -local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift +local tohex = bit.tohex ------------------------------------------------------------------------------ -- Primary and extended opcode maps @@ -39,9 +38,9 @@ local map_rlwinm = setmetatable({ shift = 0, mask = -1, }, { __index = function(t, x) - local rot = band(rshift(x, 11), 31) - local mb = band(rshift(x, 6), 31) - local me = band(rshift(x, 1), 31) + local rot = (x >> 11) & 31 + local mb = (x >> 6) & 31 + local me = (x >> 1) & 31 if mb == 0 and me == 31-rot then return "slwiRR~A." elseif me == 31 and mb == 32-rot then @@ -167,7 +166,7 @@ local map_ext = setmetatable({ [539] = "srdRR~R.", }, { __index = function(t, x) - if band(x, 31) == 15 then return "iselRRRC" end + if x & 31 == 15 then return "iselRRRC" end end }) @@ -386,9 +385,9 @@ local map_cond = { [0] = "lt", "gt", "eq", "so", "ge", "le", "ne", "ns", } -- Format a condition bit. local function condfmt(cond) if cond <= 3 then - return map_cond[band(cond, 3)] + return map_cond[cond & 3] else - return format("4*cr%d+%s", rshift(cond, 2), map_cond[band(cond, 3)]) + return format("4*cr%d+%s", cond >> 2, map_cond[cond & 3]) end end @@ -421,17 +420,17 @@ end local function disass_ins(ctx) local pos = ctx.pos local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) - local op = bor(lshift(b0, 24), lshift(b1, 16), lshift(b2, 8), b3) + local op = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3 local operands = {} local last = nil local rs = 21 ctx.op = op ctx.rel = nil - local opat = map_pri[rshift(b0, 2)] - while type(opat) ~= "string" do + local opat = map_pri[b0 >> 2] + while type(opat) != "string" do if not opat then return unknown(ctx) end - opat = opat[band(rshift(op, opat.shift), opat.mask)] + opat = opat[(op >> opat.shift) & opat.mask] end local name, pat = match(opat, "^([a-z0-9_.]*)(.*)") local altname, pat2 = match(pat, "|([a-z0-9_.]*)(.*)") @@ -440,84 +439,84 @@ local function disass_ins(ctx) for p in gmatch(pat, ".") do local x = nil if p == "R" then - x = map_gpr[band(rshift(op, rs), 31)] - rs = rs - 5 + x = map_gpr[(op >> rs) & 31] + rs -= 5 elseif p == "F" then - x = "f"..band(rshift(op, rs), 31) - rs = rs - 5 + x = "f"..((op >> rs) & 31) + rs -= 5 elseif p == "A" then - x = band(rshift(op, rs), 31) - rs = rs - 5 + x = (op >> rs) & 31 + rs -= 5 elseif p == "S" then - x = arshift(lshift(op, 27-rs), 27) - rs = rs - 5 + x = (op << 27-rs) ~>> 27 + rs -= 5 elseif p == "I" then - x = arshift(lshift(op, 16), 16) + x = (op << 16) ~>> 16 elseif p == "U" then - x = band(op, 0xffff) + x = op & 0xffff elseif p == "D" or p == "E" then - local disp = arshift(lshift(op, 16), 16) - if p == "E" then disp = band(disp, -4) end + local disp = (op << 16) ~>> 16 + if p == "E" then disp &= -4 end if last == "r0" then last = "0" end operands[#operands] = format("%d(%s)", disp, last) elseif p >= "2" and p <= "8" then - local disp = band(rshift(op, rs), 31) * p + local disp = ((op >> rs) & 31) * (byte(p) - 0x30) if last == "r0" then last = "0" end operands[#operands] = format("%d(%s)", disp, last) elseif p == "H" then - x = band(rshift(op, rs), 31) + lshift(band(op, 2), 4) - rs = rs - 5 + x = ((op >> rs) & 31) | ((op & 2) << 4) + rs -= 5 elseif p == "M" then - x = band(rshift(op, rs), 31) + band(op, 0x20) + x = ((op >> rs) & 31) | (op & 0x20) elseif p == "C" then - x = condfmt(band(rshift(op, rs), 31)) - rs = rs - 5 + x = condfmt((op >> rs) & 31) + rs -= 5 elseif p == "B" then - local bo = rshift(op, 21) - local cond = band(rshift(op, 16), 31) + local bo = op >> 21 + local cond = (op >> 16) & 31 local cn = "" - rs = rs - 10 - if band(bo, 4) == 0 then - cn = band(bo, 2) == 0 and "dnz" or "dz" - if band(bo, 0x10) == 0 then - cn = cn..(band(bo, 8) == 0 and "f" or "t") + rs -= 10 + if bo & 4 == 0 then + cn = bo & 2 == 0 ? "dnz" : "dz" + if bo & 0x10 == 0 then + cn ..= bo & 8 == 0 ? "f" : "t" + x = condfmt(cond) end - if band(bo, 0x10) == 0 then x = condfmt(cond) end - name = name..(band(bo, 1) == band(rshift(op, 15), 1) and "-" or "+") - elseif band(bo, 0x10) == 0 then - cn = map_cond[band(cond, 3) + (band(bo, 8) == 0 and 4 or 0)] - if cond > 3 then x = "cr"..rshift(cond, 2) end - name = name..(band(bo, 1) == band(rshift(op, 15), 1) and "-" or "+") + name ..= bo & 1 == (op >> 15) & 1 ? "-" : "+" + elseif bo & 0x10 == 0 then + cn = map_cond[(cond & 3) | ((bo >> 1) & 4)] + if cond > 3 then x = "cr"..(cond >> 2) end + name ..= bo & 1 == (op >> 15) & 1 ? "-" : "+" end name = gsub(name, "_", cn) elseif p == "J" then - x = arshift(lshift(op, 27-rs), 29-rs)*4 - if band(op, 2) == 0 then x = ctx.addr + pos + x end + x = ((op << 27-rs) ~>> 29-rs) << 2 + if op & 2 == 0 then x = ctx.addr + pos + x end ctx.rel = x x = "0x"..tohex(x) elseif p == "K" then - if band(op, 1) ~= 0 then name = name.."l" end - if band(op, 2) ~= 0 then name = name.."a" end + if op & 1 != 0 then name ..= "l" end + if op & 2 != 0 then name ..= "a" end elseif p == "X" or p == "Y" then - x = band(rshift(op, rs+2), 7) - if x == 0 and p == "Y" then x = nil else x = "cr"..x end - rs = rs - 5 + x = (op >> rs+2) & 7 + x = x == 0 and p == "Y" ? nil : "cr"..x + rs -= 5 elseif p == "W" then - x = "cr"..band(op, 7) + x = "cr"..(op & 7) elseif p == "Z" then - x = band(rshift(op, rs-4), 255) - rs = rs - 10 + x = (op >> rs-4) & 0xff + rs -= 10 elseif p == ">" then - operands[#operands] = rshift(operands[#operands], 1) + operands[#operands] >>= 1 elseif p == "0" then if last == "r0" then operands[#operands] = nil if altname then name = altname end end elseif p == "L" then - name = gsub(name, "_", band(op, 0x00200000) ~= 0 and "d" or "w") + name = gsub(name, "_", op & 0x00200000 != 0 ? "d" : "w") elseif p == "." then - if band(op, 1) == 1 then name = name.."." end + if op & 1 == 1 then name ..= "." end elseif p == "N" then if op == 0x60000000 then name = "nop"; break end elseif p == "~" then @@ -537,7 +536,7 @@ local function disass_ins(ctx) name = altname end elseif p == "-" then - rs = rs - 5 + rs -= 5 else assert(false) end @@ -553,8 +552,8 @@ end local function disass_block(ctx, ofs, len) if not ofs then ofs = 0 end local stop = len and ofs+len or #ctx.code - stop = stop - stop % 4 - ctx.pos = ofs - ofs % 4 + stop &= -4 + ctx.pos = ofs & -4 ctx.rel = nil while ctx.pos < stop do disass_ins(ctx) end end diff --git a/src/jit/dis_x86.lua b/src/jit/dis_x86.lua index 80bf721b1..b85061292 100644 --- a/src/jit/dis_x86.lua +++ b/src/jit/dis_x86.lua @@ -421,21 +421,21 @@ local function putop(ctx, text, operands) local hmax = ctx.hexdump if hmax > 0 then for i=ctx.start,pos-1 do - hex = hex..format("%02X", byte(code, i, i)) + hex ..= format("%02X", byte(code, i)) end if #hex > hmax then hex = sub(hex, 1, hmax)..". " - else hex = hex..rep(" ", hmax-#hex+2) end + else hex ..= rep(" ", hmax-#hex+2) end end if operands then text = text.." "..operands end if ctx.o16 then text = "o16 "..text; ctx.o16 = false end if ctx.a32 then text = "a32 "..text; ctx.a32 = false end if ctx.rep then text = ctx.rep.." "..text; ctx.rep = false end if ctx.rex then - local t = (ctx.rexw and "w" or "")..(ctx.rexr and "r" or "").. - (ctx.rexx and "x" or "")..(ctx.rexb and "b" or "").. - (ctx.vexl and "l" or "") - if ctx.vexv and ctx.vexv ~= 0 then t = t.."v"..ctx.vexv end - if t ~= "" then text = ctx.rex.."."..t.." "..gsub(text, "^ ", "") + local t = (ctx.rexw ? "w" : "")..(ctx.rexr ? "r" : "").. + (ctx.rexx ? "x" : "")..(ctx.rexb ? "b" : "").. + (ctx.vexl ? "l" : "") + if ctx.vexv and ctx.vexv != 0 then t = t.."v"..ctx.vexv end + if t != "" then text = ctx.rex.."."..t.." "..gsub(text, "^ ", "") elseif ctx.rex == "vex" then text = gsub("v"..text, "^v ", "") end ctx.rexw = false; ctx.rexr = false; ctx.rexx = false; ctx.rexb = false ctx.rex = false; ctx.vexl = false; ctx.vexv = false @@ -483,7 +483,7 @@ local function getimm(ctx, pos, n) if pos+n-1 > ctx.stop then return incomplete(ctx) end local code = ctx.code if n == 1 then - local b1 = byte(code, pos, pos) + local b1 = byte(code, pos) return b1 elseif n == 2 then local b1, b2 = byte(code, pos, pos+1) @@ -521,41 +521,41 @@ local function putpat(ctx, name, pat) if sz == "X" and vexl then sz = "Y"; ctx.vexl = false end regs = map_regs[sz] elseif p == "P" then - sz = ctx.o16 and "X" or "M"; ctx.o16 = false + sz = ctx.o16 ? "X" : "M"; ctx.o16 = false if sz == "X" and vexl then sz = "Y"; ctx.vexl = false end regs = map_regs[sz] elseif p == "H" then - name = name..(ctx.rexw and "d" or "s") + name ..= ctx.rexw ? "d" : "s" ctx.rexw = false elseif p == "S" then - name = name..lower(sz) + name ..= lower(sz) elseif p == "s" then local imm = getimm(ctx, pos, 1); if not imm then return end - x = imm <= 127 and format("+0x%02x", imm) - or format("-0x%02x", 256-imm) - pos = pos+1 + x = imm <= 127 ? format("+0x%02x", imm) + : format("-0x%02x", 256-imm) + pos += 1 elseif p == "u" then local imm = getimm(ctx, pos, 1); if not imm then return end x = format("0x%02x", imm) - pos = pos+1 + pos += 1 elseif p == "b" then local imm = getimm(ctx, pos, 1); if not imm then return end x = regs[imm/16+1] - pos = pos+1 + pos += 1 elseif p == "w" then local imm = getimm(ctx, pos, 2); if not imm then return end x = format("0x%x", imm) - pos = pos+2 + pos += 2 elseif p == "o" then -- [offset] if ctx.x64 then local imm1 = getimm(ctx, pos, 4); if not imm1 then return end local imm2 = getimm(ctx, pos+4, 4); if not imm2 then return end x = format("[0x%08x%08x]", imm2, imm1) - pos = pos+8 + pos += 8 else local imm = getimm(ctx, pos, 4); if not imm then return end x = format("[0x%08x]", imm) - pos = pos+4 + pos += 4 end elseif p == "i" or p == "I" then local n = map_sz2n[sz] @@ -568,21 +568,21 @@ local function putpat(ctx, name, pat) local imm = getimm(ctx, pos, n); if not imm then return end if sz == "Q" and (imm < 0 or imm > 0x7fffffff) then imm = (0xffffffff+1)-imm - x = format(imm > 65535 and "-0x%08x" or "-0x%x", imm) + x = format(imm > 65535 ? "-0x%08x" : "-0x%x", imm) else - x = format(imm > 65535 and "0x%08x" or "0x%x", imm) + x = format(imm > 65535 ? "0x%08x" : "0x%x", imm) end end - pos = pos+n + pos += n elseif p == "j" then local n = map_sz2n[sz] if n == 8 then n = 4 end local imm = getimm(ctx, pos, n); if not imm then return end - if sz == "B" and imm > 127 then imm = imm-256 - elseif imm > 2147483647 then imm = imm-4294967296 end - pos = pos+n + if sz == "B" and imm > 127 then imm -= 256 + elseif imm > 2147483647 then imm -= 4294967296 end + pos += n imm = imm + pos + ctx.addr - if imm > 4294967295 and not ctx.x64 then imm = imm-4294967296 end + if imm > 4294967295 and not ctx.x64 then imm -= 4294967296 end ctx.imm = imm if sz == "W" then x = format("word 0x%04x", imm%65536) @@ -593,8 +593,8 @@ local function putpat(ctx, name, pat) x = "0x"..tohex(imm) end elseif p == "R" then - local r = byte(code, pos-1, pos-1)%8 - if ctx.rexb then r = r + 8; ctx.rexb = false end + local r = byte(code, pos-1) & 7 + if ctx.rexb then r += 8; ctx.rexb = false end x = regs[r+1] elseif p == "a" then x = regs[1] elseif p == "c" then x = "cl" @@ -605,25 +605,25 @@ local function putpat(ctx, name, pat) mode = ctx.mrm if not mode then if pos > stop then return incomplete(ctx) end - mode = byte(code, pos, pos) - pos = pos+1 + mode = byte(code, pos) + pos += 1 end - rm = mode%8; mode = (mode-rm)/8 - sp = mode%8; mode = (mode-sp)/8 + rm = mode & 7; mode >>= 3 + sp = mode & 7; mode >>= 3 sdisp = "" if mode < 3 then if rm == 4 then if pos > stop then return incomplete(ctx) end - sc = byte(code, pos, pos) - pos = pos+1 - rm = sc%8; sc = (sc-rm)/8 - rx = sc%8; sc = (sc-rx)/8 - if ctx.rexx then rx = rx + 8; ctx.rexx = false end + sc = byte(code, pos) + pos += 1 + rm = sc & 7; sc >>= 3 + rx = sc & 7; sc >>= 3 + if ctx.rexx then rx += 8; ctx.rexx = false end if rx == 4 then rx = nil end end if mode > 0 or rm == 5 then local dsz = mode - if dsz ~= 1 then dsz = 4 end + if dsz != 1 then dsz = 4 end local disp = getimm(ctx, pos, dsz); if not disp then return end if mode == 0 then rm = nil end if rm or rx or (not sc and ctx.x64 and not ctx.a32) then @@ -637,26 +637,26 @@ local function putpat(ctx, name, pat) else sdisp = format(ctx.x64 and not ctx.a32 and not (disp >= 0 and disp <= 0x7fffffff) - and "0xffffffff%08x" or "0x%08x", disp) + ? "0xffffffff%08x" : "0x%08x", disp) end - pos = pos+dsz + pos += dsz end end - if rm and ctx.rexb then rm = rm + 8; ctx.rexb = false end - if ctx.rexr then sp = sp + 8; ctx.rexr = false end + if rm and ctx.rexb then rm += 8; ctx.rexb = false end + if ctx.rexr then sp += 8; ctx.rexr = false end end if p == "m" then if mode == 3 then x = regs[rm+1] else - local aregs = ctx.a32 and map_regs.D or ctx.aregs + local aregs = ctx.a32 ? map_regs.D : ctx.aregs local srm, srx = "", "" if rm then srm = aregs[rm+1] elseif not sc and ctx.x64 and not ctx.a32 then srm = "rip" end ctx.a32 = false if rx then - if rm then srm = srm.."+" end + if rm then srm ..= "+" end srx = aregs[rx+1] - if sc > 0 then srx = srx.."*"..(2^sc) end + if sc > 0 then srx = srx.."*"..(1 << sc) end end x = format("[%s%s%s]", srm, srx, sdisp) end @@ -686,7 +686,7 @@ local function putpat(ctx, name, pat) error("bad pattern `"..pat.."'") end end - if x then operands = operands and operands..", "..x or x end + if x then operands = operands ? operands..", "..x : x end end ctx.pos = pos return putop(ctx, name, operands) @@ -701,8 +701,8 @@ local function getmrm(ctx) if not mrm then local pos = ctx.pos if pos > ctx.stop then return nil end - mrm = byte(ctx.code, pos, pos) - ctx.pos = pos+1 + mrm = byte(ctx.code, pos) + ctx.pos = pos + 1 ctx.mrm = mrm end return mrm @@ -714,7 +714,7 @@ local function dispatch(ctx, opat, patgrp) if match(opat, "%|") then -- MMX/SSE variants depending on prefix. local p if ctx.rep then - p = ctx.rep=="rep" and "%|([^%|]*)" or "%|[^%|]*%|[^%|]*%|([^%|]*)" + p = ctx.rep == "rep" ? "%|([^%|]*)" : "%|[^%|]*%|[^%|]*%|([^%|]*)" ctx.rep = false elseif ctx.o16 then p = "%|[^%|]*%|([^%|]*)"; ctx.o16 = false else p = "^[^%|]*" end @@ -726,7 +726,7 @@ local function dispatch(ctx, opat, patgrp) end if match(opat, "%$") then -- reg$mem variants. local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end - opat = match(opat, mrm >= 192 and "^[^%$]*" or "%$(.*)") + opat = match(opat, mrm >= 192 ? "^[^%$]*" : "%$(.*)") if opat == "" then return unknown(ctx) end end if opat == "" then return unknown(ctx) end @@ -738,9 +738,8 @@ end -- Get a pattern from an opcode map and dispatch to handler. local function dispatchmap(ctx, opcmap) local pos = ctx.pos - local opat = opcmap[byte(ctx.code, pos, pos)] - pos = pos + 1 - ctx.pos = pos + local opat = opcmap[byte(ctx.code, pos)] + ctx.pos = pos + 1 return dispatch(ctx, opat) end @@ -760,7 +759,7 @@ map_act = { -- Collect prefixes. [":"] = function(ctx, name, pat) - ctx[pat == ":" and name or sub(pat, 2)] = name + ctx[pat == ":" ? name : sub(pat, 2)] = name if ctx.pos - ctx.start > 5 then return unknown(ctx) end -- Limit #prefixes. end, @@ -772,7 +771,7 @@ map_act = { -- Use named subtable for opcode group. ["!"] = function(ctx, name, pat) local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end - return dispatch(ctx, map_opcgroup[name][((mrm-(mrm%8))/8)%8+1], sub(pat, 2)) + return dispatch(ctx, map_opcgroup[name][((mrm >> 3) & 7)+1], sub(pat, 2)) end, -- o16,o32[,o64] variants. @@ -825,9 +824,9 @@ map_act = { -- Floating point opcode dispatch. fp = function(ctx, name, pat) local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end - local rm = mrm%8 - local idx = pat*8 + ((mrm-rm)/8)%8 - if mrm >= 192 then idx = idx + 64 end + local rm = mrm & 7 + local idx = ((byte(pat) - 0x30) << 3) | ((mrm >> 3) & 7) + if mrm >= 192 then idx += 64 end local opat = map_opcfp[idx] if type(opat) == "table" then opat = opat[rm+1] end return dispatch(ctx, opat) @@ -847,23 +846,21 @@ map_act = { local pos = ctx.pos if ctx.mrm then ctx.mrm = nil - pos = pos-1 + pos -= 1 end - local b = byte(ctx.code, pos, pos) + local b = byte(ctx.code, pos) if not b then return incomplete(ctx) end - pos = pos+1 + pos += 1 if b < 128 then ctx.rexr = true end local m = 1 if pat == "3" then - m = b%32; b = (b-m)/32 - local nb = b%2; b = (b-nb)/2 - if nb == 0 then ctx.rexb = true end - local nx = b%2 - if nx == 0 then ctx.rexx = true end - b = byte(ctx.code, pos, pos) + m = b & 0x1f + if b & 0x20 == 0 then ctx.rexb = true end + if b & 0x40 == 0 then ctx.rexx = true end + b = byte(ctx.code, pos) if not b then return incomplete(ctx) end - pos = pos+1 - if b >= 128 then ctx.rexw = true end + pos += 1 + if b & 0x80 then ctx.rexw = true end end ctx.pos = pos local map @@ -871,24 +868,23 @@ map_act = { elseif m == 2 then map = map_opc3["38"] elseif m == 3 then map = map_opc3["3a"] else return unknown(ctx) end - local p = b%4; b = (b-p)/4 + local p = b & 3 if p == 1 then ctx.o16 = "o16" elseif p == 2 then ctx.rep = "rep" elseif p == 3 then ctx.rep = "repne" end - local l = b%2; b = (b-l)/2 - if l ~= 0 then ctx.vexl = true end - ctx.vexv = (-1-b)%16 + if b & 4 != 0 then ctx.vexl = true end + ctx.vexv = ~(b >> 3) & 15 return dispatchmap(ctx, map) end, -- Special case for nop with REX prefix. nop = function(ctx, name, pat) - return dispatch(ctx, ctx.rex and pat or "nop") + return dispatch(ctx, ctx.rex ? pat : "nop") end, -- Special case for 0F 77. emms = function(ctx, name, pat) - if ctx.rex ~= "vex" then + if ctx.rex != "vex" then return putop(ctx, "emms") elseif ctx.vexl then ctx.vexl = false @@ -904,8 +900,8 @@ map_act = { -- Disassemble a block of code. local function disass_block(ctx, ofs, len) if not ofs then ofs = 0 end - local stop = len and ofs+len or #ctx.code - ofs = ofs + 1 + local stop = len ? ofs+len : #ctx.code + ofs += 1 ctx.start = ofs ctx.pos = ofs ctx.stop = stop @@ -913,7 +909,7 @@ local function disass_block(ctx, ofs, len) ctx.mrm = false clearprefixes(ctx) while ctx.pos <= stop do dispatchmap(ctx, ctx.map1) end - if ctx.pos ~= ctx.start then incomplete(ctx) end + if ctx.pos != ctx.start then incomplete(ctx) end end -- Extended API: create a disassembler context. Then call ctx:disass(ofs, len). diff --git a/src/jit/dump.lua b/src/jit/dump.lua index 6a700bbe4..3532e3c4e 100644 --- a/src/jit/dump.lua +++ b/src/jit/dump.lua @@ -62,7 +62,7 @@ local traceinfo, traceir, tracek = jutil.traceinfo, jutil.traceir, jutil.tracek local tracemc, tracesnap = jutil.tracemc, jutil.tracesnap local traceexitstub, ircalladdr = jutil.traceexitstub, jutil.ircalladdr local bit = require("bit") -local band, shr, tohex = bit.band, bit.rshift, bit.tohex +local tohex = bit.tohex local sub, gsub, format = string.sub, string.gsub, string.format local byte, rep = string.byte, string.rep local type, tostring = type, tostring @@ -90,7 +90,7 @@ local function fillsymtab_tr(tr, nexit) end for i=0,nexit-1 do local addr = traceexitstub(tr, i) - if addr < 0 then addr = addr + 2^32 end + if addr < 0 then addr += 2^32 end t[addr] = tostring(i) end local addr = traceexitstub(tr, nexit) @@ -105,9 +105,9 @@ local function fillsymtab(tr, nexit) local ircall = vmdef.ircall for i=0,#ircall do local addr = ircalladdr(i) - if addr ~= 0 then - if maskaddr then addr = band(addr, maskaddr) end - if addr < 0 then addr = addr + 2^32 end + if addr != 0 then + if maskaddr then addr &= maskaddr end + if addr < 0 then addr += 2^32 end t[addr] = ircall[i] end end @@ -123,7 +123,7 @@ local function fillsymtab(tr, nexit) nexit = 1000000 break end - if addr < 0 then addr = addr + 2^32 end + if addr < 0 then addr += 2^32 end t[addr] = tostring(i) end nexitsym = nexit @@ -142,12 +142,12 @@ local function dump_mcode(tr) local mcode, addr, loop = tracemc(tr) if not mcode then return end if not disass then disass = require("jit.dis_"..jit.arch) end - if addr < 0 then addr = addr + 2^32 end + if addr < 0 then addr += 2^32 end out:write("---- TRACE ", tr, " mcode ", #mcode, "\n") local ctx = disass.create(mcode, addr, dumpwrite) ctx.hexdump = 0 ctx.symtab = fillsymtab(tr, info.nexit) - if loop ~= 0 then + if loop != 0 then symtab[addr+loop] = "LOOP" ctx:disass(0, loop) out:write("->LOOP:\n") @@ -233,7 +233,7 @@ local html_escape = { ["<"] = "<", [">"] = ">", ["&"] = "&", } local function colorize_html(s, t, extra) s = gsub(s, "[<>&]", html_escape) return format('%s', - irtype_text[t], extra and " irt_extra" or "", s) + irtype_text[t], extra ? " irt_extra" : "", s) end local irtype_html = setmetatable({}, @@ -268,25 +268,25 @@ local colorize, irtype local litname = { ["SLOAD "] = setmetatable({}, { __index = function(t, mode) local s = "" - if band(mode, 1) ~= 0 then s = s.."P" end - if band(mode, 2) ~= 0 then s = s.."F" end - if band(mode, 4) ~= 0 then s = s.."T" end - if band(mode, 8) ~= 0 then s = s.."C" end - if band(mode, 16) ~= 0 then s = s.."R" end - if band(mode, 32) ~= 0 then s = s.."I" end - if band(mode, 64) ~= 0 then s = s.."K" end + if mode & 1 != 0 then s ..= "P" end + if mode & 2 != 0 then s ..= "F" end + if mode & 4 != 0 then s ..= "T" end + if mode & 8 != 0 then s ..= "C" end + if mode & 16 != 0 then s ..= "R" end + if mode & 32 != 0 then s ..= "I" end + if mode & 64 != 0 then s ..= "K" end t[mode] = s return s end}), ["XLOAD "] = { [0] = "", "R", "V", "RV", "U", "RU", "VU", "RVU", }, ["CONV "] = setmetatable({}, { __index = function(t, mode) - local s = irtype[band(mode, 31)] - s = irtype[band(shr(mode, 5), 31)].."."..s - if band(mode, 0x800) ~= 0 then s = s.." sext" end - local c = shr(mode, 12) - if c == 1 then s = s.." none" - elseif c == 2 then s = s.." index" - elseif c == 3 then s = s.." check" end + local s = irtype[mode & 31] + s = irtype[(mode >> 5) & 31].."."..s + if mode & 0x800 != 0 then s ..= " sext" end + local c = mode >> 12 + if c == 1 then s ..= " none" + elseif c == 2 then s ..= " index" + elseif c == 3 then s ..= " check" end t[mode] = s return s end}), @@ -325,16 +325,16 @@ local function formatk(tr, idx, sn) local s if tn == "number" then if t < 12 then - s = k == 0 and "NULL" or format("[0x%08x]", k) - elseif band(sn or 0, 0x30000) ~= 0 then - s = band(sn, 0x20000) ~= 0 and "contpc" or "ftsz" + s = k == 0 ? "NULL" : format("[0x%08x]", k) + elseif (sn or 0) & 0x30000 != 0 then + s = sn & 0x20000 != 0 ? "contpc" : "ftsz" elseif k == 2^52+2^51 then s = "bias" else - s = format(0 < k and k < 0x1p-1026 and "%+a" or "%+.14g", k) + s = format(0 < k and k < 0x1p-1026 ? "%+a" : "%+.14g", k) end elseif tn == "string" then - s = format(#k > 20 and '"%.20s"~' or '"%s"', gsub(k, "%c", ctlsub)) + s = format(#k > 20 ? '"%.20s"~' : '"%s"', gsub(k, "%c", ctlsub)) elseif tn == "function" then s = fmtfunc(k) elseif tn == "table" then @@ -348,13 +348,13 @@ local function formatk(tr, idx, sn) end elseif t == 21 then -- int64_t s = sub(tostring(k), 1, -3) - if sub(s, 1, 1) ~= "-" then s = "+"..s end + if sub(s, 1, 1) != "-" then s = "+"..s end elseif sn == 0x1057fff then -- SNAP(1, SNAP_FRAME | SNAP_NORESTORE, REF_NIL) return "----" -- Special case for LJ_FR2 slot 1. else s = tostring(k) -- For primitives. end - s = colorize(format("%-4s", s), t, band(sn or 0, 0x100000) ~= 0) + s = colorize(format("%-4s", s), t, (sn or 0) & 0x100000 != 0) if slot then s = format("%s @%d", s, slot) end @@ -365,18 +365,18 @@ local function printsnap(tr, snap) local n = 2 for s=0,snap[1]-1 do local sn = snap[n] - if shr(sn, 24) == s then - n = n + 1 - local ref = band(sn, 0xffff) - 0x8000 -- REF_BIAS + if sn >> 24 == s then + n += 1 + local ref = (sn & 0xffff) - 0x8000 -- REF_BIAS if ref < 0 then out:write(formatk(tr, ref, sn)) - elseif band(sn, 0x80000) ~= 0 then -- SNAP_SOFTFPNUM + elseif sn & 0x80000 != 0 then -- SNAP_SOFTFPNUM out:write(colorize(format("%04d/%04d", ref, ref+1), 14)) else local m, ot, op1, op2 = traceir(tr, ref) - out:write(colorize(format("%04d", ref), band(ot, 31), band(sn, 0x100000) ~= 0)) + out:write(colorize(format("%04d", ref), ot & 31, sn & 0x100000 != 0)) end - out:write(band(sn, 0x10000) == 0 and " " or "|") -- SNAP_FRAME + out:write(sn & 0x10000 == 0 ? " " : "|") -- SNAP_FRAME else out:write("---- ") end @@ -398,9 +398,9 @@ end -- Return a register name or stack slot for a rid/sp location. local function ridsp_name(ridsp, ins) if not disass then disass = require("jit.dis_"..jit.arch) end - local rid, slot = band(ridsp, 0xff), shr(ridsp, 8) + local rid, slot = ridsp & 0xff, ridsp >> 8 if rid == 253 or rid == 254 then - return (slot == 0 or slot == 255) and " {sink" or format(" {%04d", ins-slot) + return (slot == 0 or slot == 255) ? " {sink" : format(" {%04d", ins-slot) end if ridsp > 255 then return format("[%x]", slot*4) end if rid < 128 then return disass.regname(rid) end @@ -412,7 +412,7 @@ local function dumpcallfunc(tr, ins) local ctype if ins > 0 then local m, ot, op1, op2 = traceir(tr, ins) - if band(ot, 31) == 0 then -- nil type means CARG(func, ctype). + if ot & 31 == 0 then -- nil type means CARG(func, ctype). ins = op1 ctype = formatk(tr, op2) end @@ -431,7 +431,7 @@ local function dumpcallargs(tr, ins) out:write(formatk(tr, ins)) else local m, ot, op1, op2 = traceir(tr, ins) - local oidx = 6*shr(ot, 8) + local oidx = 6 * (ot >> 8) local op = sub(vmdef.irnames, oidx+1, oidx+6) if op == "CARG " then dumpcallargs(tr, op1) @@ -468,12 +468,12 @@ local function dump_ir(tr, dumpsnap, dumpreg) out:write(format(".... SNAP #%-3d [ ", snapno)) end printsnap(tr, snap) - snapno = snapno + 1 + snapno += 1 snap = tracesnap(tr, snapno) - snapref = snap and snap[0] or 65536 + snapref = snap ? snap[0] : 65536 end local m, ot, op1, op2, ridsp = traceir(tr, ins) - local oidx, t = 6*shr(ot, 8), band(ot, 31) + local oidx, t = 6 * (ot >> 8), ot & 31 local op = sub(irnames, oidx+1, oidx+6) if op == "LOOP " then if dumpreg then @@ -481,45 +481,45 @@ local function dump_ir(tr, dumpsnap, dumpreg) else out:write(format("%04d ------ LOOP ------------\n", ins)) end - elseif op ~= "NOP " and op ~= "CARG " and - (dumpreg or op ~= "RENAME") then - local rid = band(ridsp, 255) + elseif op != "NOP " and op != "CARG " and + (dumpreg or op != "RENAME") then + local rid = ridsp & 255 if dumpreg then out:write(format("%04d %-6s", ins, ridsp_name(ridsp, ins))) else out:write(format("%04d ", ins)) end out:write(format("%s%s %s %s ", - (rid == 254 or rid == 253) and "}" or - (band(ot, 128) == 0 and " " or ">"), - band(ot, 64) == 0 and " " or "+", + rid == 254 or rid == 253 ? "}" : + ot & 128 == 0 ? " " : ">", + ot & 64 == 0 ? " " : "+", irtype[t], op)) - local m1, m2 = band(m, 3), band(m, 3*4) + local m1, m2 = m & 3, m & (3 << 2) if sub(op, 1, 4) == "CALL" then local ctype - if m2 == 1*4 then -- op2 == IRMlit + if m2 == 1 << 2 then -- op2 == IRMlit out:write(format("%-10s (", vmdef.ircall[op2])) else ctype = dumpcallfunc(tr, op2) end - if op1 ~= -1 then dumpcallargs(tr, op1) end + if op1 != -1 then dumpcallargs(tr, op1) end out:write(")") if ctype then out:write(" ctype ", ctype) end elseif op == "CNEW " and op2 == -1 then out:write(formatk(tr, op1)) - elseif m1 ~= 3 then -- op1 != IRMnone + elseif m1 != 3 then -- op1 != IRMnone if op1 < 0 then out:write(formatk(tr, op1)) else - out:write(format(m1 == 0 and "%04d" or "#%-3d", op1)) + out:write(format(m1 == 0 ? "%04d" : "#%-3d", op1)) end - if m2 ~= 3*4 then -- op2 != IRMnone - if m2 == 1*4 then -- op2 == IRMlit + if m2 != 3 << 2 then -- op2 != IRMnone + if m2 == 1 << 2 then -- op2 == IRMlit local litn = litname[op] if litn and litn[op2] then out:write(" ", litn[op2]) elseif op == "UREFO " or op == "UREFC " then - out:write(format(" #%-3d", shr(op2, 8))) + out:write(format(" #%-3d", op2 >> 8)) else out:write(format(" #%-3d", op2)) end @@ -572,7 +572,7 @@ local function dump_trace(what, tr, func, pc, otr, oex) if what == "start" then if dumpmode.H then out:write('
\n') end
     out:write("---- TRACE ", tr, " ", what)
-    if otr then out:write(" ", otr, "/", oex == -1 and "stitch" or oex) end
+    if otr then out:write(" ", otr, "/", oex == -1 ? "stitch" : oex) end
     out:write(" ", fmtfunc(func, pc), "\n")
   elseif what == "stop" or what == "abort" then
     out:write("---- TRACE ", tr, " ", what)
@@ -599,7 +599,7 @@ end
 
 -- Dump recorded bytecode.
 local function dump_record(tr, func, pc, depth)
-  if depth ~= recdepth then
+  if depth != recdepth then
     recdepth = depth
     recprefix = rep(" .", depth)
   end
@@ -615,7 +615,7 @@ local function dump_record(tr, func, pc, depth)
   else
     out:write(line)
   end
-  if pc >= 0 and band(funcbc(func, pc), 0xff) < 16 then -- ORDER BC
+  if pc >= 0 and funcbc(func, pc) & 0xff < 16 then -- ORDER BC
     out:write(bcline(func, pc+1, recprefix)) -- Write JMP for cond.
   end
 end
@@ -664,7 +664,7 @@ local function dumpoff()
     jit.attach(dump_texit)
     jit.attach(dump_record)
     jit.attach(dump_trace)
-    if out and out ~= stdout and out ~= stderr then out:close() end
+    if out and out != stdout and out != stderr then out:close() end
     out = nil
   end
 end
@@ -674,16 +674,16 @@ local function dumpon(opt, outfile)
   if active then dumpoff() end
 
   local term = os.getenv("TERM")
-  local colormode = (term and term:match("color") or os.getenv("COLORTERM")) and "A" or "T"
+  local colormode = (term ? (term:match("color")) : os.getenv("COLORTERM")) ? "A" : "T"
   if opt then
     opt = gsub(opt, "[TAH]", function(mode) colormode = mode; return ""; end)
   end
 
   local m = { t=true, b=true, i=true, m=true, }
-  if opt and opt ~= "" then
+  if opt and opt != "" then
     local o = sub(opt, 1, 1)
-    if o ~= "+" and o ~= "-" then m = {} end
-    for i=1,#opt do m[sub(opt, i, i)] = (o ~= "-") end
+    if o != "+" and o != "-" then m = {} end
+    for i=1,#opt do m[sub(opt, i, i)] = (o != "-") end
   end
   dumpmode = m
 
@@ -700,7 +700,7 @@ local function dumpon(opt, outfile)
 
   if not outfile then outfile = os.getenv("LUAJIT_DUMPFILE") end
   if outfile then
-    out = outfile == "-" and stdout or assert(io.open(outfile, "w"))
+    out = outfile == "-" ? stdout : assert(io.open(outfile, "w"))
   else
     out = stdout
   end
diff --git a/src/jit/p.lua b/src/jit/p.lua
index 9d938ce56..bb04fbd21 100644
--- a/src/jit/p.lua
+++ b/src/jit/p.lua
@@ -120,7 +120,7 @@ end
 local function prof_top(count1, count2, samples, indent)
   local t, n = {}, 0
   for k in pairs(count1) do
-    n = n + 1
+    n += 1
     t[n] = k
   end
   sort(t, function(a, b) return count1[a] > count1[b] end)
@@ -184,7 +184,7 @@ local function prof_annotate(count1, samples)
     out:write(format("\n====== %s ======\n", file))
     local fl = files[file]
     local n, show = 1, false
-    if ann ~= 0 then
+    if ann != 0 then
       for i=1,ann do
 	if fl[i] then show = true; out:write("@@ 1 @@\n"); break end
       end
@@ -195,7 +195,7 @@ local function prof_annotate(count1, samples)
 	break
       end
       local v = fl[n]
-      if ann ~= 0 then
+      if ann != 0 then
 	local v2 = fl[n+ann]
 	if show then
 	  if v2 then show = n+ann elseif v then show = n
@@ -212,7 +212,7 @@ local function prof_annotate(count1, samples)
 	out:write(format(fmtn, line))
       end
     ::next::
-      n = n + 1
+      n += 1
     end
     fp:close()
   end
@@ -226,7 +226,7 @@ local function prof_finish()
     profile.stop()
     local samples = prof_samples
     if samples == 0 then
-      if prof_raw ~= true then out:write("[No samples collected]\n") end
+      if prof_raw != true then out:write("[No samples collected]\n") end
     elseif prof_ann then
       prof_annotate(prof_count1, samples)
     else
@@ -235,7 +235,7 @@ local function prof_finish()
     prof_count1 = nil
     prof_count2 = nil
     prof_ud = nil
-    if out ~= stdout then out:close() end
+    if out != stdout then out:close() end
   end
 end
 
@@ -270,7 +270,7 @@ local function prof_start(mode)
     prof_fmt = "pl"
     prof_split = 0
     prof_depth = 1
-  elseif m.G and scope ~= "" then
+  elseif m.G and scope != "" then
     prof_fmt = flags..scope.."Z;"
     prof_depth = -100
     prof_raw = true
diff --git a/src/jit/v.lua b/src/jit/v.lua
index 69443d316..849915ab7 100644
--- a/src/jit/v.lua
+++ b/src/jit/v.lua
@@ -107,7 +107,7 @@ local function dump_trace(what, tr, func, pc, otr, oex)
   else
     if what == "abort" then
       local loc = fmtfunc(func, pc)
-      if loc ~= startloc then
+      if loc != startloc then
 	out:write(format("[TRACE --- %s%s -- %s at %s]\n",
 	  startex, startloc, fmterr(otr, oex), loc))
       else
@@ -147,7 +147,7 @@ local function dumpoff()
   if active then
     active = false
     jit.attach(dump_trace)
-    if out and out ~= stdout and out ~= stderr then out:close() end
+    if out and out != stdout and out != stderr then out:close() end
     out = nil
   end
 end
-- 
cgit v1.2.3-55-g6feb