aboutsummaryrefslogtreecommitdiff
path: root/src/jit
diff options
context:
space:
mode:
authorMike Pall <mike>2026-08-03 10:44:17 +0200
committerMike Pall <mike>2026-08-03 10:44:17 +0200
commitf30aabe82f61dbd6901f6a75dadde0a64dc626d2 (patch)
tree80eda65bf087e10b2a78d9b94bcec3a04ed4d2de /src/jit
parent28084004ee68d576f3f0c9ea61ea448fe3e10f07 (diff)
downloadluajit-f30aabe82f61dbd6901f6a75dadde0a64dc626d2.tar.gz
luajit-f30aabe82f61dbd6901f6a75dadde0a64dc626d2.tar.bz2
luajit-f30aabe82f61dbd6901f6a75dadde0a64dc626d2.zip
Modernize jit.* Lua modules.
Diffstat (limited to 'src/jit')
-rw-r--r--src/jit/bc.lua43
-rw-r--r--src/jit/bcsave.lua47
-rw-r--r--src/jit/dis_arm.lua137
-rw-r--r--src/jit/dis_arm64.lua232
-rw-r--r--src/jit/dis_mips.lua83
-rw-r--r--src/jit/dis_ppc.lua117
-rw-r--r--src/jit/dis_x86.lua152
-rw-r--r--src/jit/dump.lua132
-rw-r--r--src/jit/p.lua14
-rw-r--r--src/jit/v.lua4
10 files changed, 472 insertions, 489 deletions
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 @@
43local jit = require("jit") 43local jit = require("jit")
44local jutil = require("jit.util") 44local jutil = require("jit.util")
45local vmdef = require("jit.vmdef") 45local vmdef = require("jit.vmdef")
46local bit = require("bit")
47local sub, gsub, format = string.sub, string.gsub, string.format 46local sub, gsub, format = string.sub, string.gsub, string.format
48local byte, band, shr = string.byte, bit.band, bit.rshift 47local byte = string.byte
49local funcinfo, funcbc, funck = jutil.funcinfo, jutil.funcbc, jutil.funck 48local funcinfo, funcbc, funck = jutil.funcinfo, jutil.funcbc, jutil.funck
50local funcuvname = jutil.funcuvname 49local funcuvname = jutil.funcuvname
51local bcnames = vmdef.bcnames 50local bcnames = vmdef.bcnames
@@ -65,49 +64,49 @@ end
65local function bcline(func, pc, prefix) 64local function bcline(func, pc, prefix)
66 local ins, m = funcbc(func, pc) 65 local ins, m = funcbc(func, pc)
67 if not ins then return end 66 if not ins then return end
68 local ma, mb, mc = band(m, 7), band(m, 15*8), band(m, 15*128) 67 local ma, mb, mc = m & 7, (m >> 3) & 15, (m >> 7) & 15
69 local a = band(shr(ins, 8), 0xff) 68 local a = (ins >> 8) & 0xff
70 local oidx = 6*band(ins, 0xff) 69 local oidx = 6 * (ins & 0xff)
71 local op = sub(bcnames, oidx+1, oidx+6) 70 local op = sub(bcnames, oidx+1, oidx+6)
72 local s = format("%04d %s %-6s %3s ", 71 local s = format("%04d %s %-6s %3s ",
73 pc, prefix or " ", op, ma == 0 and "" or a) 72 pc, prefix or " ", op, ma == 0 ? "" : a)
74 local d = shr(ins, 16) 73 local d = ins >> 16
75 if mc == 13*128 then -- BCMjump 74 if mc == 13 then -- BCMjump
76 return format("%s=> %04d\n", s, pc+d-0x7fff) 75 return format("%s=> %04d\n", s, pc+d-0x7fff)
77 end 76 end
78 if mb ~= 0 then 77 if mb != 0 then
79 d = band(d, 0xff) 78 d &= 0xff
80 elseif mc == 0 then 79 elseif mc == 0 then
81 return s.."\n" 80 return s.."\n"
82 end 81 end
83 local kc 82 local kc
84 if mc == 10*128 then -- BCMstr 83 if mc == 10 then -- BCMstr
85 kc = funck(func, -d-1) 84 kc = funck(func, -d-1)
86 kc = format(#kc > 40 and '"%.40s"~' or '"%s"', gsub(kc, "%c", ctlsub)) 85 kc = format(#kc > 40 ? '"%.40s"~' : '"%s"', gsub(kc, "%c", ctlsub))
87 elseif mc == 9*128 then -- BCMnum 86 elseif mc == 9 then -- BCMnum
88 kc = funck(func, d) 87 kc = funck(func, d)
89 if op == "TSETM " then kc = kc - 2^52 end 88 if op == "TSETM " then kc -= 2^52 end
90 elseif mc == 12*128 then -- BCMfunc 89 elseif mc == 12 then -- BCMfunc
91 local fi = funcinfo(funck(func, -d-1)) 90 local fi = funcinfo(funck(func, -d-1))
92 if fi.ffid then 91 if fi.ffid then
93 kc = vmdef.ffnames[fi.ffid] 92 kc = vmdef.ffnames[fi.ffid]
94 else 93 else
95 kc = fi.loc 94 kc = fi.loc
96 end 95 end
97 elseif mc == 5*128 then -- BCMuv 96 elseif mc == 5 then -- BCMuv
98 kc = funcuvname(func, d) 97 kc = funcuvname(func, d)
99 end 98 end
100 if ma == 5 then -- BCMuv 99 if ma == 5 then -- BCMuv
101 local ka = funcuvname(func, a) 100 local ka = funcuvname(func, a)
102 if kc then kc = ka.." ; "..kc else kc = ka end 101 if kc then kc = ka.." ; "..kc else kc = ka end
103 end 102 end
104 if mb ~= 0 then 103 if mb != 0 then
105 local b = shr(ins, 24) 104 local b = ins >> 24
106 if kc then return format("%s%3d %3d ; %s\n", s, b, d, kc) end 105 if kc then return format("%s%3d %3d ; %s\n", s, b, d, kc) end
107 return format("%s%3d %3d\n", s, b, d) 106 return format("%s%3d %3d\n", s, b, d)
108 end 107 end
109 if kc then return format("%s%3d ; %s\n", s, d, kc) end 108 if kc then return format("%s%3d ; %s\n", s, d, kc) end
110 if mc == 7*128 and d > 32767 then d = d - 65536 end -- BCMlits 109 if mc == 7 and d > 32767 then d -= 65536 end -- BCMlits
111 return format("%s%3d\n", s, d) 110 return format("%s%3d\n", s, d)
112end 111end
113 112
@@ -117,7 +116,7 @@ local function bctargets(func)
117 for pc=1,1000000000 do 116 for pc=1,1000000000 do
118 local ins, m = funcbc(func, pc) 117 local ins, m = funcbc(func, pc)
119 if not ins then break end 118 if not ins then break end
120 if band(m, 15*128) == 13*128 then target[pc+shr(ins, 16)-0x7fff] = true end 119 if m & (15 << 7) == (13 << 7) then target[pc+(ins >> 16)-0x7fff] = true end
121 end 120 end
122 return target 121 return target
123end 122end
@@ -159,7 +158,7 @@ local function bclistoff()
159 if active then 158 if active then
160 active = false 159 active = false
161 jit.attach(h_list) 160 jit.attach(h_list)
162 if out and out ~= stdout and out ~= stderr then out:close() end 161 if out and out != stdout and out != stderr then out:close() end
163 out = nil 162 out = nil
164 end 163 end
165end 164end
@@ -169,7 +168,7 @@ local function bcliston(outfile)
169 if active then bclistoff() end 168 if active then bclistoff() end
170 if not outfile then outfile = os.getenv("LUAJIT_LISTFILE") end 169 if not outfile then outfile = os.getenv("LUAJIT_LISTFILE") end
171 if outfile then 170 if outfile then
172 out = outfile == "-" and stdout or assert(io.open(outfile, "w")) 171 out = outfile == "-" ? stdout : assert(io.open(outfile, "w"))
173 else 172 else
174 out = stderr 173 out = stderr
175 end 174 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_"
20local type, assert = type, assert 20local type, assert = type, assert
21local format = string.format 21local format = string.format
22local tremove, tconcat = table.remove, table.concat 22local tremove, tconcat = table.remove, table.concat
23local bswap = bit.bswap
23 24
24------------------------------------------------------------------------------ 25------------------------------------------------------------------------------
25 26
@@ -111,7 +112,7 @@ local map_os = {
111local function checkarg(str, map, err) 112local function checkarg(str, map, err)
112 str = str:lower() 113 str = str:lower()
113 local s = check(map[str], "unknown ", err) 114 local s = check(map[str], "unknown ", err)
114 return type(s) == "string" and s or str 115 return type(s) == "string" ? s : str
115end 116end
116 117
117local function detecttype(str) 118local function detecttype(str)
@@ -142,7 +143,7 @@ end
142 143
143local function bcsave_tail(fp, output, s) 144local function bcsave_tail(fp, output, s)
144 local ok, err = fp:write(s) 145 local ok, err = fp:write(s)
145 if ok and output ~= "-" then ok, err = fp:close() end 146 if ok and output != "-" then ok, err = fp:close() end
146 check(ok, "cannot write ", output, ": ", err) 147 check(ok, "cannot write ", output, ": ", err)
147end 148end
148 149
@@ -179,12 +180,12 @@ static const unsigned char %s%s[] = {
179 local t, n, m = {}, 0, 0 180 local t, n, m = {}, 0, 0
180 for i=1,#s do 181 for i=1,#s do
181 local b = tostring(string.byte(s, i)) 182 local b = tostring(string.byte(s, i))
182 m = m + #b + 1 183 m += #b + 1
183 if m > 78 then 184 if m > 78 then
184 fp:write(tconcat(t, ",", 1, n), ",\n") 185 fp:write(tconcat(t, ",", 1, n), ",\n")
185 n, m = 0, #b + 1 186 n, m = 0, #b + 1
186 end 187 end
187 n = n + 1 188 n += 1
188 t[n] = b 189 t[n] = b
189 end 190 end
190 bcsave_tail(fp, output, tconcat(t, ",", 1, n).."\n};\n") 191 bcsave_tail(fp, output, tconcat(t, ",", 1, n).."\n};\n")
@@ -248,19 +249,19 @@ typedef struct {
248 -- Handle different host/target endianess. 249 -- Handle different host/target endianess.
249 local function f32(x) return x end 250 local function f32(x) return x end
250 local f16, fofs = f32, f32 251 local f16, fofs = f32, f32
251 if ffi.abi("be") ~= isbe then 252 if ffi.abi("be") != isbe then
252 f32 = bit.bswap 253 f32 = bswap
253 function f16(x) return bit.rshift(bit.bswap(x), 16) end 254 function f16(x) return bswap(x) >> 16 end
254 if is64 then 255 if is64 then
255 local two32 = ffi.cast("int64_t", 2^32) 256 local two32 = ffi.cast("int64_t", 2^32)
256 function fofs(x) return bit.bswap(x)*two32 end 257 function fofs(x) return bswap(x)*two32 end
257 else 258 else
258 fofs = f32 259 fofs = f32
259 end 260 end
260 end 261 end
261 262
262 -- Create ELF object and fill in header. 263 -- Create ELF object and fill in header.
263 local o = ffi.new(is64 and "ELF64obj" or "ELF32obj") 264 local o = ffi.new(is64 ? "ELF64obj" : "ELF32obj")
264 local hdr = o.hdr 265 local hdr = o.hdr
265 if ctx.os == "bsd" or ctx.os == "other" then -- Determine native hdr.eosabi. 266 if ctx.os == "bsd" or ctx.os == "other" then -- Determine native hdr.eosabi.
266 local bf = assert(io.open("/bin/ls", "rb")) 267 local bf = assert(io.open("/bin/ls", "rb"))
@@ -272,8 +273,8 @@ typedef struct {
272 hdr.emagic = "\127ELF" 273 hdr.emagic = "\127ELF"
273 hdr.eosabi = ({ freebsd=9, netbsd=2, openbsd=12, solaris=6 })[ctx.os] or 0 274 hdr.eosabi = ({ freebsd=9, netbsd=2, openbsd=12, solaris=6 })[ctx.os] or 0
274 end 275 end
275 hdr.eclass = is64 and 2 or 1 276 hdr.eclass = is64 ? 2 : 1
276 hdr.eendian = isbe and 2 or 1 277 hdr.eendian = isbe ? 2 : 1
277 hdr.eversion = 1 278 hdr.eversion = 1
278 hdr.type = f16(1) 279 hdr.type = f16(1)
279 hdr.machine = f16(ai.m) 280 hdr.machine = f16(ai.m)
@@ -294,7 +295,7 @@ typedef struct {
294 sect.align = fofs(1) 295 sect.align = fofs(1)
295 sect.name = f32(ofs) 296 sect.name = f32(ofs)
296 ffi.copy(o.space+ofs, name) 297 ffi.copy(o.space+ofs, name)
297 ofs = ofs + #name+1 298 ofs += #name+1
298 end 299 end
299 o.sect[1].type = f32(2) -- .symtab 300 o.sect[1].type = f32(2) -- .symtab
300 o.sect[1].link = f32(3) 301 o.sect[1].link = f32(3)
@@ -314,7 +315,7 @@ typedef struct {
314 o.sect[3].ofs = fofs(sofs + ofs) 315 o.sect[3].ofs = fofs(sofs + ofs)
315 o.sect[3].size = fofs(#symname+2) 316 o.sect[3].size = fofs(#symname+2)
316 ffi.copy(o.space+ofs+1, symname) 317 ffi.copy(o.space+ofs+1, symname)
317 ofs = ofs + #symname + 2 318 ofs += #symname + 2
318 o.sect[4].type = f32(1) -- .rodata 319 o.sect[4].type = f32(1) -- .rodata
319 o.sect[4].flags = fofs(2) 320 o.sect[4].flags = fofs(2)
320 o.sect[4].ofs = fofs(sofs + ofs) 321 o.sect[4].ofs = fofs(sofs + ofs)
@@ -381,8 +382,8 @@ typedef struct {
381 local function f32(x) return x end 382 local function f32(x) return x end
382 local f16 = f32 383 local f16 = f32
383 if ffi.abi("be") then 384 if ffi.abi("be") then
384 f32 = bit.bswap 385 f32 = bswap
385 function f16(x) return bit.rshift(bit.bswap(x), 16) end 386 function f16(x) return bswap(x) >> 16 end
386 end 387 end
387 388
388 -- Create PE object and fill in header. 389 -- Create PE object and fill in header.
@@ -422,7 +423,7 @@ typedef struct {
422 o.strtabsize = f32(ofs + 4) 423 o.strtabsize = f32(ofs + 4)
423 o.sect[0].ofs = f32(ffi.offsetof(o, "space") + ofs) 424 o.sect[0].ofs = f32(ffi.offsetof(o, "space") + ofs)
424 ffi.copy(o.space + ofs, symexport) 425 ffi.copy(o.space + ofs, symexport)
425 ofs = ofs + #symexport 426 ofs += #symexport
426 o.sect[1].ofs = f32(ffi.offsetof(o, "space") + ofs) 427 o.sect[1].ofs = f32(ffi.offsetof(o, "space") + ofs)
427 428
428 -- Write PE object file. 429 -- Write PE object file.
@@ -475,11 +476,11 @@ typedef struct {
475]] 476]]
476 local symname = '_'..LJBC_PREFIX..ctx.modname 477 local symname = '_'..LJBC_PREFIX..ctx.modname
477 local cputype, cpusubtype = 0x01000007, 3 478 local cputype, cpusubtype = 0x01000007, 3
478 if ctx.arch ~= "x64" then 479 if ctx.arch != "x64" then
479 check(ctx.arch == "arm64", "unsupported architecture for OSX") 480 check(ctx.arch == "arm64", "unsupported architecture for OSX")
480 cputype, cpusubtype = 0x0100000c, 0 481 cputype, cpusubtype = 0x0100000c, 0
481 end 482 end
482 local function aligned(v, a) return bit.band(v+a-1, -a) end 483 local function aligned(v, a) return v+a-1 & -a end
483 484
484 -- Create Mach-O object and fill in header. 485 -- Create Mach-O object and fill in header.
485 local o = ffi.new("mach_obj_64") 486 local o = ffi.new("mach_obj_64")
@@ -579,7 +580,7 @@ local function docmd(...)
579 local gc64 = "" 580 local gc64 = ""
580 while n <= #arg do 581 while n <= #arg do
581 local a = arg[n] 582 local a = arg[n]
582 if type(a) == "string" and a:sub(1, 1) == "-" and a ~= "-" then 583 if type(a) == "string" and a:sub(1, 1) == "-" and a != "-" then
583 tremove(arg, n) 584 tremove(arg, n)
584 if a == "--" then break end 585 if a == "--" then break end
585 for m=2,#a do 586 for m=2,#a do
@@ -595,9 +596,9 @@ local function docmd(...)
595 elseif opt == "d" then 596 elseif opt == "d" then
596 ctx.mode = ctx.mode .. opt 597 ctx.mode = ctx.mode .. opt
597 else 598 else
598 if arg[n] == nil or m ~= #a then usage() end 599 if arg[n] == nil or m != #a then usage() end
599 if opt == "e" then 600 if opt == "e" then
600 if n ~= 1 then usage() end 601 if n != 1 then usage() end
601 ctx.string = true 602 ctx.string = true
602 elseif opt == "n" then 603 elseif opt == "n" then
603 ctx.modname = checkmodname(tremove(arg, n)) 604 ctx.modname = checkmodname(tremove(arg, n))
@@ -615,7 +616,7 @@ local function docmd(...)
615 end 616 end
616 end 617 end
617 else 618 else
618 n = n + 1 619 n += 1
619 end 620 end
620 end 621 end
621 ctx.mode = ctx.mode .. strip .. gc64 622 ctx.mode = ctx.mode .. strip .. gc64
@@ -623,7 +624,7 @@ local function docmd(...)
623 if #arg == 0 or #arg > 2 then usage() end 624 if #arg == 0 or #arg > 2 then usage() end
624 bclist(ctx, arg[1], arg[2] or "-") 625 bclist(ctx, arg[1], arg[2] or "-")
625 else 626 else
626 if #arg ~= 2 then usage() end 627 if #arg != 2 then usage() end
627 bcsave(ctx, arg[1], arg[2]) 628 bcsave(ctx, arg[1], arg[2])
628 end 629 end
629end 630end
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
15local match, gmatch = string.match, string.gmatch 15local match, gmatch = string.match, string.gmatch
16local concat = table.concat 16local concat = table.concat
17local bit = require("bit") 17local bit = require("bit")
18local band, bor, ror, tohex = bit.band, bit.bor, bit.ror, bit.tohex 18local ror, tohex = bit.ror, bit.tohex
19local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift
20 19
21------------------------------------------------------------------------------ 20------------------------------------------------------------------------------
22-- Opcode maps 21-- Opcode maps
@@ -373,7 +372,7 @@ local map_datar = {
373 [16] = { shift = 7, mask = 1, [0] = map_misc, map_mulh, }, 372 [16] = { shift = 7, mask = 1, [0] = map_misc, map_mulh, },
374 _ = { 373 _ = {
375 shift = 0, mask = 0xffffffff, 374 shift = 0, mask = 0xffffffff,
376 [bor(0xe1a00000)] = "nop", 375 [0xe1a00000|0] = "nop",
377 _ = map_data, 376 _ = map_data,
378 } 377 }
379 }, 378 },
@@ -427,7 +426,7 @@ local function putop(ctx, text, operands)
427 local sym = ctx.symtab[ctx.rel] 426 local sym = ctx.symtab[ctx.rel]
428 if sym then 427 if sym then
429 extra = "\t->"..sym 428 extra = "\t->"..sym
430 elseif band(ctx.op, 0x0e000000) ~= 0x0a000000 then 429 elseif ctx.op & 0x0e000000 != 0x0a000000 then
431 extra = "\t; 0x"..tohex(ctx.rel) 430 extra = "\t; 0x"..tohex(ctx.rel)
432 end 431 end
433 end 432 end
@@ -448,47 +447,47 @@ end
448 447
449-- Format operand 2 of load/store opcodes. 448-- Format operand 2 of load/store opcodes.
450local function fmtload(ctx, op, pos) 449local function fmtload(ctx, op, pos)
451 local base = map_gpr[band(rshift(op, 16), 15)] 450 local base = map_gpr[(op >> 16) & 15]
452 local x, ofs 451 local x, ofs
453 local ext = (band(op, 0x04000000) == 0) 452 local ext = (op & 0x04000000 == 0)
454 if not ext and band(op, 0x02000000) == 0 then 453 if not ext and op & 0x02000000 == 0 then
455 ofs = band(op, 4095) 454 ofs = op & 4095
456 if band(op, 0x00800000) == 0 then ofs = -ofs end 455 if op & 0x00800000 == 0 then ofs = -ofs end
457 if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end 456 if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end
458 ofs = "#"..ofs 457 ofs = "#"..ofs
459 elseif ext and band(op, 0x00400000) ~= 0 then 458 elseif ext and op & 0x00400000 != 0 then
460 ofs = band(op, 15) + band(rshift(op, 4), 0xf0) 459 ofs = (op & 0x0f) | ((op >> 4) & 0xf0)
461 if band(op, 0x00800000) == 0 then ofs = -ofs end 460 if op & 0x00800000 == 0 then ofs = -ofs end
462 if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end 461 if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end
463 ofs = "#"..ofs 462 ofs = "#"..ofs
464 else 463 else
465 ofs = map_gpr[band(op, 15)] 464 ofs = map_gpr[op & 15]
466 if ext or band(op, 0xfe0) == 0 then 465 if ext or op & 0xfe0 == 0 then
467 elseif band(op, 0xfe0) == 0x60 then 466 elseif op & 0xfe0 == 0x60 then
468 ofs = format("%s, rrx", ofs) 467 ofs = format("%s, rrx", ofs)
469 else 468 else
470 local sh = band(rshift(op, 7), 31) 469 local sh = (op >> 7) & 31
471 if sh == 0 then sh = 32 end 470 if sh == 0 then sh = 32 end
472 ofs = format("%s, %s #%d", ofs, map_shift[band(rshift(op, 5), 3)], sh) 471 ofs = format("%s, %s #%d", ofs, map_shift[(op >> 5) & 3], sh)
473 end 472 end
474 if band(op, 0x00800000) == 0 then ofs = "-"..ofs end 473 if op & 0x00800000 == 0 then ofs = "-"..ofs end
475 end 474 end
476 if ofs == "#0" then 475 if ofs == "#0" then
477 x = format("[%s]", base) 476 x = format("[%s]", base)
478 elseif band(op, 0x01000000) == 0 then 477 elseif op & 0x01000000 == 0 then
479 x = format("[%s], %s", base, ofs) 478 x = format("[%s], %s", base, ofs)
480 else 479 else
481 x = format("[%s, %s]", base, ofs) 480 x = format("[%s, %s]", base, ofs)
482 end 481 end
483 if band(op, 0x01200000) == 0x01200000 then x = x.."!" end 482 if op & 0x01200000 == 0x01200000 then x ..= "!" end
484 return x 483 return x
485end 484end
486 485
487-- Format operand 2 of vector load/store opcodes. 486-- Format operand 2 of vector load/store opcodes.
488local function fmtvload(ctx, op, pos) 487local function fmtvload(ctx, op, pos)
489 local base = map_gpr[band(rshift(op, 16), 15)] 488 local base = map_gpr[(op >> 16) & 15]
490 local ofs = band(op, 255)*4 489 local ofs = (op & 255) << 2
491 if band(op, 0x00800000) == 0 then ofs = -ofs end 490 if op & 0x00800000 == 0 then ofs = -ofs end
492 if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end 491 if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end
493 if ofs == 0 then 492 if ofs == 0 then
494 return format("[%s]", base) 493 return format("[%s]", base)
@@ -499,9 +498,9 @@ end
499 498
500local function fmtvr(op, vr, sh0, sh1) 499local function fmtvr(op, vr, sh0, sh1)
501 if vr == "s" then 500 if vr == "s" then
502 return format("s%d", 2*band(rshift(op, sh0), 15)+band(rshift(op, sh1), 1)) 501 return format("s%d", ((op >> sh0-1) & 0x1e) | ((op >> sh1) & 1))
503 else 502 else
504 return format("d%d", band(rshift(op, sh0), 15)+band(rshift(op, sh1-4), 16)) 503 return format("d%d", ((op >> sh0) & 15) | ((op >> sh1-4) & 16))
505 end 504 end
506end 505end
507 506
@@ -509,7 +508,7 @@ end
509local function disass_ins(ctx) 508local function disass_ins(ctx)
510 local pos = ctx.pos 509 local pos = ctx.pos
511 local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) 510 local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4)
512 local op = bor(lshift(b3, 24), lshift(b2, 16), lshift(b1, 8), b0) 511 local op = (b3 << 24) | (b2 << 16) | (b1 << 8) | b0
513 local operands = {} 512 local operands = {}
514 local suffix = "" 513 local suffix = ""
515 local last, name, pat 514 local last, name, pat
@@ -517,35 +516,35 @@ local function disass_ins(ctx)
517 ctx.op = op 516 ctx.op = op
518 ctx.rel = nil 517 ctx.rel = nil
519 518
520 local cond = rshift(op, 28) 519 local cond = op >> 28
521 local opat 520 local opat
522 if cond == 15 then 521 if cond == 15 then
523 opat = map_uncondins[band(rshift(op, 25), 7)] 522 opat = map_uncondins[(op >> 25) & 7]
524 else 523 else
525 if cond ~= 14 then suffix = map_cond[cond] end 524 if cond != 14 then suffix = map_cond[cond] end
526 opat = map_condins[band(rshift(op, 25), 7)] 525 opat = map_condins[(op >> 25) & 7]
527 end 526 end
528 while type(opat) ~= "string" do 527 while type(opat) != "string" do
529 if not opat then return unknown(ctx) end 528 if not opat then return unknown(ctx) end
530 opat = opat[band(rshift(op, opat.shift), opat.mask)] or opat._ 529 opat = opat[(op >> opat.shift) & opat.mask] or opat._
531 end 530 end
532 name, pat = match(opat, "^([a-z0-9]*)(.*)") 531 name, pat = match(opat, "^([a-z0-9]*)(.*)")
533 if sub(pat, 1, 1) == "." then 532 if sub(pat, 1, 1) == "." then
534 local s2, p2 = match(pat, "^([a-z0-9.]*)(.*)") 533 local s2, p2 = match(pat, "^([a-z0-9.]*)(.*)")
535 suffix = suffix..s2 534 suffix ..= s2
536 pat = p2 535 pat = p2
537 end 536 end
538 537
539 for p in gmatch(pat, ".") do 538 for p in gmatch(pat, ".") do
540 local x = nil 539 local x = nil
541 if p == "D" then 540 if p == "D" then
542 x = map_gpr[band(rshift(op, 12), 15)] 541 x = map_gpr[(op >> 12) & 15]
543 elseif p == "N" then 542 elseif p == "N" then
544 x = map_gpr[band(rshift(op, 16), 15)] 543 x = map_gpr[(op >> 16) & 15]
545 elseif p == "S" then 544 elseif p == "S" then
546 x = map_gpr[band(rshift(op, 8), 15)] 545 x = map_gpr[(op >> 8) & 15]
547 elseif p == "M" then 546 elseif p == "M" then
548 x = map_gpr[band(op, 15)] 547 x = map_gpr[op & 15]
549 elseif p == "d" then 548 elseif p == "d" then
550 x = fmtvr(op, vr, 12, 22) 549 x = fmtvr(op, vr, 12, 22)
551 elseif p == "n" then 550 elseif p == "n" then
@@ -553,20 +552,20 @@ local function disass_ins(ctx)
553 elseif p == "m" then 552 elseif p == "m" then
554 x = fmtvr(op, vr, 0, 5) 553 x = fmtvr(op, vr, 0, 5)
555 elseif p == "P" then 554 elseif p == "P" then
556 if band(op, 0x02000000) ~= 0 then 555 if op & 0x02000000 != 0 then
557 x = ror(band(op, 255), 2*band(rshift(op, 8), 15)) 556 x = ror(op & 0xff, (op >> 7) & 0x1e)
558 else 557 else
559 x = map_gpr[band(op, 15)] 558 x = map_gpr[op & 15]
560 if band(op, 0xff0) ~= 0 then 559 if op & 0xff0 != 0 then
561 operands[#operands+1] = x 560 operands[#operands+1] = x
562 local s = map_shift[band(rshift(op, 5), 3)] 561 local s = map_shift[(op >> 5) & 3]
563 local r = nil 562 local r = nil
564 if band(op, 0xf90) == 0 then 563 if op & 0xf90 == 0 then
565 if s == "ror" then s = "rrx" else r = "#32" end 564 if s == "ror" then s = "rrx" else r = "#32" end
566 elseif band(op, 0x10) == 0 then 565 elseif op & 0x10 == 0 then
567 r = "#"..band(rshift(op, 7), 31) 566 r = "#"..((op >> 7) & 31)
568 else 567 else
569 r = map_gpr[band(rshift(op, 8), 15)] 568 r = map_gpr[(op >> 8) & 15]
570 end 569 end
571 if name == "mov" then name = s; x = r 570 if name == "mov" then name = s; x = r
572 elseif r then x = format("%s %s", s, r) 571 elseif r then x = format("%s %s", s, r)
@@ -578,8 +577,8 @@ local function disass_ins(ctx)
578 elseif p == "l" then 577 elseif p == "l" then
579 x = fmtvload(ctx, op, pos) 578 x = fmtvload(ctx, op, pos)
580 elseif p == "B" then 579 elseif p == "B" then
581 local addr = ctx.addr + pos + 8 + arshift(lshift(op, 8), 6) 580 local addr = ctx.addr + pos + 8 + ((op << 8) ~>> 6)
582 if cond == 15 then addr = addr + band(rshift(op, 23), 2) end 581 if cond == 15 then addr += (op >> 23) & 2 end
583 ctx.rel = addr 582 ctx.rel = addr
584 x = "0x"..tohex(addr) 583 x = "0x"..tohex(addr)
585 elseif p == "F" then 584 elseif p == "F" then
@@ -587,52 +586,52 @@ local function disass_ins(ctx)
587 elseif p == "G" then 586 elseif p == "G" then
588 vr = "d" 587 vr = "d"
589 elseif p == "." then 588 elseif p == "." then
590 suffix = suffix..(vr == "s" and ".f32" or ".f64") 589 suffix ..= vr == "s" ? ".f32" : ".f64"
591 elseif p == "R" then 590 elseif p == "R" then
592 if band(op, 0x00200000) ~= 0 and #operands == 1 then 591 if op & 0x00200000 != 0 and #operands == 1 then
593 operands[1] = operands[1].."!" 592 operands[1] = operands[1].."!"
594 end 593 end
595 local t = {} 594 local t = {}
596 for i=0,15 do 595 for i=0,15 do
597 if band(rshift(op, i), 1) == 1 then t[#t+1] = map_gpr[i] end 596 if (op >> i) & 1 == 1 then t[#t+1] = map_gpr[i] end
598 end 597 end
599 x = "{"..concat(t, ", ").."}" 598 x = "{"..concat(t, ", ").."}"
600 elseif p == "r" then 599 elseif p == "r" then
601 if band(op, 0x00200000) ~= 0 and #operands == 2 then 600 if op & 0x00200000 != 0 and #operands == 2 then
602 operands[1] = operands[1].."!" 601 operands[1] = operands[1].."!"
603 end 602 end
604 local s = tonumber(sub(last, 2)) 603 local s = tonumber(sub(last, 2))
605 local n = band(op, 255) 604 local n = op & 0xff
606 if vr == "d" then n = rshift(n, 1) end 605 if vr == "d" then n >>= 1 end
607 operands[#operands] = format("{%s-%s%d}", last, vr, s+n-1) 606 operands[#operands] = format("{%s-%s%d}", last, vr, s+n-1)
608 elseif p == "W" then 607 elseif p == "W" then
609 x = band(op, 0x0fff) + band(rshift(op, 4), 0xf000) 608 x = (op & 0x0fff) | ((op >> 4) & 0xf000)
610 elseif p == "T" then 609 elseif p == "T" then
611 x = "#0x"..tohex(band(op, 0x00ffffff), 6) 610 x = "#0x"..tohex(op & 0x00ffffff, 6)
612 elseif p == "U" then 611 elseif p == "U" then
613 x = band(rshift(op, 7), 31) 612 x = (op >> 7) & 31
614 if x == 0 then x = nil end 613 if x == 0 then x = nil end
615 elseif p == "u" then 614 elseif p == "u" then
616 x = band(rshift(op, 7), 31) 615 x = (op >> 7) & 31
617 if band(op, 0x40) == 0 then 616 if op & 0x40 == 0 then
618 if x == 0 then x = nil else x = "lsl #"..x end 617 x = x == 0 ? nil : "lsl #"..x
619 else 618 else
620 if x == 0 then x = "asr #32" else x = "asr #"..x end 619 x = x == 0 ? "asr #32" : "asr #"..x
621 end 620 end
622 elseif p == "v" then 621 elseif p == "v" then
623 x = band(rshift(op, 7), 31) 622 x = (op >> 7) & 31
624 elseif p == "w" then 623 elseif p == "w" then
625 x = band(rshift(op, 16), 31) 624 x = (op >> 16) & 31
626 elseif p == "x" then 625 elseif p == "x" then
627 x = band(rshift(op, 16), 31) + 1 626 x = ((op >> 16) & 31) + 1
628 elseif p == "X" then 627 elseif p == "X" then
629 x = band(rshift(op, 16), 31) - last + 1 628 x = ((op >> 16) & 31) - last + 1
630 elseif p == "Y" then 629 elseif p == "Y" then
631 x = band(rshift(op, 12), 0xf0) + band(op, 0x0f) 630 x = ((op >> 12) & 0xf0) | (op & 0x0f)
632 elseif p == "K" then 631 elseif p == "K" then
633 x = "#0x"..tohex(band(rshift(op, 4), 0x0000fff0) + band(op, 15), 4) 632 x = "#0x"..tohex(((op >> 4) & 0xfff0) | (op & 0x000f), 4)
634 elseif p == "s" then 633 elseif p == "s" then
635 if band(op, 0x00100000) ~= 0 then suffix = "s"..suffix end 634 if op & 0x00100000 != 0 then suffix = "s"..suffix end
636 else 635 else
637 assert(false) 636 assert(false)
638 end 637 end
@@ -651,7 +650,7 @@ end
651-- Disassemble a block of code. 650-- Disassemble a block of code.
652local function disass_block(ctx, ofs, len) 651local function disass_block(ctx, ofs, len)
653 if not ofs then ofs = 0 end 652 if not ofs then ofs = 0 end
654 local stop = len and ofs+len or #ctx.code 653 local stop = len ? ofs+len : #ctx.code
655 ctx.pos = ofs 654 ctx.pos = ofs
656 ctx.rel = nil 655 ctx.rel = nil
657 while ctx.pos < stop do disass_ins(ctx) end 656 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
18local match, gmatch, gsub = string.match, string.gmatch, string.gsub 18local match, gmatch, gsub = string.match, string.gmatch, string.gsub
19local concat = table.concat 19local concat = table.concat
20local bit = require("bit") 20local bit = require("bit")
21local band, bor, bxor, tohex = bit.band, bit.bor, bit.bxor, bit.tohex 21local ror, tohex = bit.ror, bit.tohex
22local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift
23local ror = bit.ror
24 22
25------------------------------------------------------------------------------ 23------------------------------------------------------------------------------
26-- Opcode maps 24-- Opcode maps
@@ -782,35 +780,35 @@ end
782local imm13_rep = { 0x55555555, 0x11111111, 0x01010101, 0x00010001, 0x00000001 } 780local imm13_rep = { 0x55555555, 0x11111111, 0x01010101, 0x00010001, 0x00000001 }
783 781
784local function decode_imm13(op) 782local function decode_imm13(op)
785 local imms = band(rshift(op, 10), 63) 783 local imms = (op >> 10) & 63
786 local immr = band(rshift(op, 16), 63) 784 local immr = (op >> 16) & 63
787 if band(op, 0x00400000) == 0 then 785 if op & 0x00400000 == 0 then
788 local len = 5 786 local len = 5
789 if imms >= 56 then 787 if imms >= 56 then
790 if imms >= 60 then len = 1 else len = 2 end 788 if imms >= 60 then len = 1 else len = 2 end
791 elseif imms >= 48 then len = 3 elseif imms >= 32 then len = 4 end 789 elseif imms >= 48 then len = 3 elseif imms >= 32 then len = 4 end
792 local l = lshift(1, len)-1 790 local l = (1 << len) - 1
793 local s = band(imms, l) 791 local s = imms & l
794 local r = band(immr, l) 792 local r = immr & l
795 local imm = ror(rshift(-1, 31-s), r) 793 local imm = ror(-1 >> 31-s, r)
796 if len ~= 5 then imm = band(imm, lshift(1, l)-1) + rshift(imm, 31-l) end 794 if len != 5 then imm = (imm & ((1 << l) - 1)) | (imm >> 31-l) end
797 imm = imm * imm13_rep[len] 795 imm *= imm13_rep[len]
798 local ix = fmt_hex32(imm) 796 local ix = fmt_hex32(imm)
799 if rshift(op, 31) ~= 0 then 797 if op >> 31 != 0 then
800 return ix..tohex(imm) 798 return ix..tohex(imm)
801 else 799 else
802 return ix 800 return ix
803 end 801 end
804 else 802 else
805 local lo, hi = -1, 0 803 local lo, hi = -1, 0
806 if imms < 32 then lo = rshift(-1, 31-imms) else hi = rshift(-1, 63-imms) end 804 if imms < 32 then lo = -1 >> 31-imms else hi = -1 >> 63-imms end
807 if immr ~= 0 then 805 if immr != 0 then
808 lo, hi = ror(lo, immr), ror(hi, immr) 806 lo, hi = ror(lo, immr), ror(hi, immr)
809 local x = immr == 32 and 0 or band(bxor(lo, hi), lshift(-1, 32-immr)) 807 local x = immr == 32 ? 0 : (lo ~ hi) & (-1 << 32-immr)
810 lo, hi = bxor(lo, x), bxor(hi, x) 808 lo, hi = lo ~ x, hi ~ x
811 if immr >= 32 then lo, hi = hi, lo end 809 if immr >= 32 then lo, hi = hi, lo end
812 end 810 end
813 if hi ~= 0 then 811 if hi != 0 then
814 return fmt_hex32(hi)..tohex(lo) 812 return fmt_hex32(hi)..tohex(lo)
815 else 813 else
816 return fmt_hex32(lo) 814 return fmt_hex32(lo)
@@ -820,33 +818,31 @@ end
820 818
821local function parse_immpc(op, name) 819local function parse_immpc(op, name)
822 if name == "b" or name == "bl" then 820 if name == "b" or name == "bl" then
823 return arshift(lshift(op, 6), 4) 821 return (op << 6) ~>> 4
824 elseif name == "adr" or name == "adrp" then 822 elseif name == "adr" or name == "adrp" then
825 local immlo = band(rshift(op, 29), 3) 823 return (((op << 8) ~>> 13) << 2) | ((op >> 29) & 3)
826 local immhi = lshift(arshift(lshift(op, 8), 13), 2)
827 return bor(immhi, immlo)
828 elseif name == "tbz" or name == "tbnz" then 824 elseif name == "tbz" or name == "tbnz" then
829 return lshift(arshift(lshift(op, 13), 18), 2) 825 return ((op << 13) ~>> 18) << 2
830 else 826 else
831 return lshift(arshift(lshift(op, 8), 13), 2) 827 return ((op << 8) ~>> 13) << 2
832 end 828 end
833end 829end
834 830
835local function parse_fpimm8(op) 831local function parse_fpimm8(op)
836 local sign = band(op, 0x100000) == 0 and 1 or -1 832 local sign = op & 0x100000 == 0 ? 1 : -1
837 local exp = bxor(rshift(arshift(lshift(op, 12), 5), 24), 0x80) - 131 833 local exp = ((((op << 12) ~>> 5) >> 24) ~ 0x80) - 131
838 local frac = 16+band(rshift(op, 13), 15) 834 local frac = 16 + ((op >> 13) & 15)
839 return sign * frac * 2^exp 835 return sign * frac * 2^exp
840end 836end
841 837
842local function decode_fpmovi(op) 838local function decode_fpmovi(op)
843 local lo = rshift(op, 5) 839 local lo = op >> 5
844 local hi = rshift(op, 9) 840 local hi = op >> 9
845 lo = bor(band(lo, 1) * 0xff, band(lo, 2) * 0x7f80, band(lo, 4) * 0x3fc000, 841 lo = ((lo & 1) * 0xff) | ((lo & 2) * 0x7f80) |
846 band(lo, 8) * 0x1fe00000) 842 ((lo & 4) * 0x3fc000) | ((lo & 8) * 0x1fe00000)
847 hi = bor(band(hi, 1) * 0xff, band(hi, 0x80) * 0x1fe, 843 hi = ((hi & 1) * 0xff) | ((hi & 0x80) * 0x1fe) |
848 band(hi, 0x100) * 0xff00, band(hi, 0x200) * 0x7f8000) 844 ((hi & 0x100) * 0xff00) | ((hi & 0x200) * 0x7f8000)
849 if hi ~= 0 then 845 if hi != 0 then
850 return fmt_hex32(hi)..tohex(lo) 846 return fmt_hex32(hi)..tohex(lo)
851 else 847 else
852 return fmt_hex32(lo) 848 return fmt_hex32(lo)
@@ -861,7 +857,7 @@ local function prefer_bfx(sf, uns, imms, immr)
861 if sf == 0 and (imms == 7 or imms == 15) then 857 if sf == 0 and (imms == 7 or imms == 15) then
862 return false 858 return false
863 end 859 end
864 if sf ~= 0 and uns == 0 and (imms == 7 or imms == 15 or imms == 31) then 860 if sf != 0 and uns == 0 and (imms == 7 or imms == 15 or imms == 31) then
865 return false 861 return false
866 end 862 end
867 end 863 end
@@ -872,7 +868,7 @@ end
872local function disass_ins(ctx) 868local function disass_ins(ctx)
873 local pos = ctx.pos 869 local pos = ctx.pos
874 local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) 870 local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4)
875 local op = bor(lshift(b3, 24), lshift(b2, 16), lshift(b1, 8), b0) 871 local op = (b3 << 24) | (b2 << 16) | (b1 << 8) | b0
876 local operands = {} 872 local operands = {}
877 local suffix = "" 873 local suffix = ""
878 local last, name, pat 874 local last, name, pat
@@ -881,26 +877,26 @@ local function disass_ins(ctx)
881 ctx.rel = nil 877 ctx.rel = nil
882 last = nil 878 last = nil
883 local opat 879 local opat
884 opat = map_init[band(rshift(op, 25), 15)] 880 opat = map_init[(op >> 25) & 15]
885 while type(opat) ~= "string" do 881 while type(opat) != "string" do
886 if not opat then return unknown(ctx) end 882 if not opat then return unknown(ctx) end
887 opat = opat[band(rshift(op, opat.shift), opat.mask)] or opat._ 883 opat = opat[(op >> opat.shift) & opat.mask] or opat._
888 end 884 end
889 name, pat = match(opat, "^([a-z0-9]*)(.*)") 885 name, pat = match(opat, "^([a-z0-9]*)(.*)")
890 local altname, pat2 = match(pat, "|([a-z0-9_.|]*)(.*)") 886 local altname, pat2 = match(pat, "|([a-z0-9_.|]*)(.*)")
891 if altname then pat = pat2 end 887 if altname then pat = pat2 end
892 if sub(pat, 1, 1) == "." then 888 if sub(pat, 1, 1) == "." then
893 local s2, p2 = match(pat, "^([a-z0-9.]*)(.*)") 889 local s2, p2 = match(pat, "^([a-z0-9.]*)(.*)")
894 suffix = suffix..s2 890 suffix ..= s2
895 pat = p2 891 pat = p2
896 end 892 end
897 893
898 local rt = match(pat, "[gf]") 894 local rt = match(pat, "[gf]")
899 if rt then 895 if rt then
900 if rt == "g" then 896 if rt == "g" then
901 map_reg = band(op, 0x80000000) ~= 0 and map_regs.x or map_regs.w 897 map_reg = map_regs[op & 0x80000000 != 0 ? "x" : "w"]
902 else 898 else
903 map_reg = band(op, 0x400000) ~= 0 and map_regs.d or map_regs.s 899 map_reg = map_regs[op & 0x400000 != 0 ? "d" : "s"]
904 end 900 end
905 end 901 end
906 902
@@ -909,41 +905,41 @@ local function disass_ins(ctx)
909 for p in gmatch(pat, ".") do 905 for p in gmatch(pat, ".") do
910 local x = nil 906 local x = nil
911 if p == "D" then 907 if p == "D" then
912 local regnum = band(op, 31) 908 local regnum = op & 31
913 x = rt and map_reg[regnum] or match_reg(p, pat, regnum) 909 x = rt ? map_reg[regnum] : match_reg(p, pat, regnum)
914 elseif p == "N" then 910 elseif p == "N" then
915 local regnum = band(rshift(op, 5), 31) 911 local regnum = (op >> 5) & 31
916 x = rt and map_reg[regnum] or match_reg(p, pat, regnum) 912 x = rt ? map_reg[regnum] : match_reg(p, pat, regnum)
917 elseif p == "M" then 913 elseif p == "M" then
918 local regnum = band(rshift(op, 16), 31) 914 local regnum = (op >> 16) & 31
919 x = rt and map_reg[regnum] or match_reg(p, pat, regnum) 915 x = rt ? map_reg[regnum] : match_reg(p, pat, regnum)
920 elseif p == "A" then 916 elseif p == "A" then
921 local regnum = band(rshift(op, 10), 31) 917 local regnum = (op >> 10) & 31
922 x = rt and map_reg[regnum] or match_reg(p, pat, regnum) 918 x = rt ? map_reg[regnum] : match_reg(p, pat, regnum)
923 elseif p == "B" then 919 elseif p == "B" then
924 local addr = ctx.addr + pos + parse_immpc(op, name) 920 local addr = ctx.addr + pos + parse_immpc(op, name)
925 ctx.rel = addr 921 ctx.rel = addr
926 x = format("0x%08x", addr) 922 x = format("0x%08x", addr)
927 elseif p == "T" then 923 elseif p == "T" then
928 x = bor(band(rshift(op, 26), 32), band(rshift(op, 19), 31)) 924 x = ((op >> 26) & 32) | ((op >> 19) & 31)
929 elseif p == "V" then 925 elseif p == "V" then
930 x = band(op, 15) 926 x = op & 15
931 elseif p == "C" then 927 elseif p == "C" then
932 x = map_cond[band(rshift(op, 12), 15)] 928 x = map_cond[(op >> 12) & 15]
933 elseif p == "c" then 929 elseif p == "c" then
934 local rn = band(rshift(op, 5), 31) 930 local rn = (op >> 5) & 31
935 local rm = band(rshift(op, 16), 31) 931 local rm = (op >> 16) & 31
936 local cond = band(rshift(op, 12), 15) 932 local cond = (op >> 12) & 15
937 local invc = bxor(cond, 1) 933 local invc = cond ~ 1
938 x = map_cond[cond] 934 x = map_cond[cond]
939 if altname and cond ~= 14 and cond ~= 15 then 935 if altname and cond != 14 and cond != 15 then
940 local a1, a2 = match(altname, "([^|]*)|(.*)") 936 local a1, a2 = match(altname, "([^|]*)|(.*)")
941 if rn == rm then 937 if rn == rm then
942 local n = #operands 938 local n = #operands
943 operands[n] = nil 939 operands[n] = nil
944 x = map_cond[invc] 940 x = map_cond[invc]
945 if rn ~= 31 then 941 if rn != 31 then
946 if a1 then name = a1 else name = altname end 942 name = a1 ?? altname
947 else 943 else
948 operands[n-1] = nil 944 operands[n-1] = nil
949 name = a2 945 name = a2
@@ -951,65 +947,59 @@ local function disass_ins(ctx)
951 end 947 end
952 end 948 end
953 elseif p == "W" then 949 elseif p == "W" then
954 x = band(rshift(op, 5), 0xffff) 950 x = (op >> 5) & 0xffff
955 elseif p == "Y" then 951 elseif p == "Y" then
956 x = band(rshift(op, 5), 0xffff) 952 x = (op >> 5) & 0xffff
957 local hw = band(rshift(op, 21), 3) 953 local hw = (op >> 21) & 3
958 if altname and (hw == 0 or x ~= 0) then 954 if altname and (hw == 0 or x != 0) then
959 name = altname 955 name = altname
960 end 956 end
961 elseif p == "L" then 957 elseif p == "L" then
962 local rn = map_regs.x[band(rshift(op, 5), 31)] 958 local rn = map_regs.x[(op >> 5) & 31]
963 local imm9 = arshift(lshift(op, 11), 23) 959 local imm9 = (op << 11) ~>> 23
964 if band(op, 0x800) ~= 0 then 960 if op & 0x800 != 0 then
965 x = "["..rn..", #"..imm9.."]!" 961 x = "["..rn..", #"..imm9.."]!"
966 else 962 else
967 x = "["..rn.."], #"..imm9 963 x = "["..rn.."], #"..imm9
968 end 964 end
969 elseif p == "U" then 965 elseif p == "U" then
970 local rn = map_regs.x[band(rshift(op, 5), 31)] 966 local rn = map_regs.x[(op >> 5) & 31]
971 local sz = band(rshift(op, 30), 3) 967 local sz = (op >> 30) & 3
972 local imm12 = lshift(rshift(lshift(op, 10), 20), sz) 968 local imm12 = ((op << 10) >> 20) << sz
973 if imm12 ~= 0 then 969 if imm12 != 0 then
974 x = "["..rn..", #"..imm12.."]" 970 x = "["..rn..", #"..imm12.."]"
975 else 971 else
976 x = "["..rn.."]" 972 x = "["..rn.."]"
977 end 973 end
978 elseif p == "K" then 974 elseif p == "K" then
979 local rn = map_regs.x[band(rshift(op, 5), 31)] 975 local rn = map_regs.x[(op >> 5) & 31]
980 local imm9 = arshift(lshift(op, 11), 23) 976 local imm9 = (op << 11) ~>> 23
981 if imm9 ~= 0 then 977 if imm9 != 0 then
982 x = "["..rn..", #"..imm9.."]" 978 x = "["..rn..", #"..imm9.."]"
983 else 979 else
984 x = "["..rn.."]" 980 x = "["..rn.."]"
985 end 981 end
986 elseif p == "O" then 982 elseif p == "O" then
987 local rn, rm = map_regs.x[band(rshift(op, 5), 31)] 983 local rn = map_regs.x[(op >> 5) & 31]
988 local m = band(rshift(op, 13), 1) 984 local rm = map_regs[op & (1 << 13) == 0 ? "w" : "x"][(op >> 16) & 31]
989 if m == 0 then
990 rm = map_regs.w[band(rshift(op, 16), 31)]
991 else
992 rm = map_regs.x[band(rshift(op, 16), 31)]
993 end
994 x = "["..rn..", "..rm 985 x = "["..rn..", "..rm
995 local opt = band(rshift(op, 13), 7) 986 local opt = (op >> 13) & 7
996 local s = band(rshift(op, 12), 1) 987 local s = (op >> 12) & 1
997 local sz = band(rshift(op, 30), 3) 988 local sz = (op >> 30) & 3
998 -- extension to be applied
999 if opt == 3 then 989 if opt == 3 then
1000 if s == 0 then x = x.."]" 990 if s == 0 then x ..= "]"
1001 else x = x..", lsl #"..sz.."]" end 991 else x = x..", lsl #"..sz.."]" end
1002 elseif opt == 2 or opt == 6 or opt == 7 then 992 elseif opt == 2 or opt == 6 or opt == 7 then
1003 if s == 0 then x = x..", "..map_extend[opt].."]" 993 if s == 0 then x = x..", "..map_extend[opt].."]"
1004 else x = x..", "..map_extend[opt].." #"..sz.."]" end 994 else x = x..", "..map_extend[opt].." #"..sz.."]" end
1005 else 995 else
1006 x = x.."]" 996 x ..= "]"
1007 end 997 end
1008 elseif p == "P" then 998 elseif p == "P" then
1009 local sh = 2 + rshift(op, 31 - band(rshift(op, 26), 1)) 999 local sh = 2 + (op >> (31 - ((op >> 26) & 1)))
1010 local imm7 = lshift(arshift(lshift(op, 10), 25), sh) 1000 local imm7 = ((op << 10) ~>> 25) << sh
1011 local rn = map_regs.x[band(rshift(op, 5), 31)] 1001 local rn = map_regs.x[(op >> 5) & 31]
1012 local ind = band(rshift(op, 23), 3) 1002 local ind = (op >> 23) & 3
1013 if ind == 1 then 1003 if ind == 1 then
1014 x = "["..rn.."], #"..imm7 1004 x = "["..rn.."], #"..imm7
1015 elseif ind == 2 then 1005 elseif ind == 2 then
@@ -1022,9 +1012,9 @@ local function disass_ins(ctx)
1022 x = "["..rn..", #"..imm7.."]!" 1012 x = "["..rn..", #"..imm7.."]!"
1023 end 1013 end
1024 elseif p == "I" then 1014 elseif p == "I" then
1025 local shf = band(rshift(op, 22), 3) 1015 local shf = (op >> 22) & 3
1026 local imm12 = band(rshift(op, 10), 0x0fff) 1016 local imm12 = (op >> 10) & 0x0fff
1027 local rn, rd = band(rshift(op, 5), 31), band(op, 31) 1017 local rn, rd = (op >> 5) & 31, op & 31
1028 if altname == "mov" and shf == 0 and imm12 == 0 and (rn == 31 or rd == 31) then 1018 if altname == "mov" and shf == 0 and imm12 == 0 and (rn == 31 or rd == 31) then
1029 name = altname 1019 name = altname
1030 x = nil 1020 x = nil
@@ -1036,22 +1026,22 @@ local function disass_ins(ctx)
1036 elseif p == "i" then 1026 elseif p == "i" then
1037 x = "#0x"..decode_imm13(op) 1027 x = "#0x"..decode_imm13(op)
1038 elseif p == "1" then 1028 elseif p == "1" then
1039 immr = band(rshift(op, 16), 63) 1029 immr = (op >> 16) & 63
1040 x = immr 1030 x = immr
1041 elseif p == "2" then 1031 elseif p == "2" then
1042 x = band(rshift(op, 10), 63) 1032 x = (op >> 10) & 63
1043 if altname then 1033 if altname then
1044 local a1, a2, a3, a4, a5, a6 = 1034 local a1, a2, a3, a4, a5, a6 =
1045 match(altname, "([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)|(.*)") 1035 match(altname, "([^|]*)|([^|]*)|([^|]*)|([^|]*)|([^|]*)|(.*)")
1046 local sf = band(rshift(op, 26), 32) 1036 local sf = (op >> 26) & 32
1047 local uns = band(rshift(op, 30), 1) 1037 local uns = (op >> 30) & 1
1048 if prefer_bfx(sf, uns, x, immr) then 1038 if prefer_bfx(sf, uns, x, immr) then
1049 name = a2 1039 name = a2
1050 x = x - immr + 1 1040 x = x - immr + 1
1051 elseif immr == 0 and x == 7 then 1041 elseif immr == 0 and x == 7 then
1052 local n = #operands 1042 local n = #operands
1053 operands[n] = nil 1043 operands[n] = nil
1054 if sf ~= 0 then 1044 if sf != 0 then
1055 operands[n-1] = gsub(operands[n-1], "x", "w") 1045 operands[n-1] = gsub(operands[n-1], "x", "w")
1056 end 1046 end
1057 last = operands[n-1] 1047 last = operands[n-1]
@@ -1060,7 +1050,7 @@ local function disass_ins(ctx)
1060 elseif immr == 0 and x == 15 then 1050 elseif immr == 0 and x == 15 then
1061 local n = #operands 1051 local n = #operands
1062 operands[n] = nil 1052 operands[n] = nil
1063 if sf ~= 0 then 1053 if sf != 0 then
1064 operands[n-1] = gsub(operands[n-1], "x", "w") 1054 operands[n-1] = gsub(operands[n-1], "x", "w")
1065 end 1055 end
1066 last = operands[n-1] 1056 last = operands[n-1]
@@ -1071,7 +1061,7 @@ local function disass_ins(ctx)
1071 name = a4 1061 name = a4
1072 local n = #operands 1062 local n = #operands
1073 operands[n] = nil 1063 operands[n] = nil
1074 if sf ~= 0 then 1064 if sf != 0 then
1075 operands[n-1] = gsub(operands[n-1], "x", "w") 1065 operands[n-1] = gsub(operands[n-1], "x", "w")
1076 end 1066 end
1077 last = operands[n-1] 1067 last = operands[n-1]
@@ -1079,7 +1069,7 @@ local function disass_ins(ctx)
1079 name = a3 1069 name = a3
1080 end 1070 end
1081 x = nil 1071 x = nil
1082 elseif band(x, 31) ~= 31 and immr == x+1 and name == "ubfm" then 1072 elseif x & 31 != 31 and immr == x+1 and name == "ubfm" then
1083 name = a4 1073 name = a4
1084 last = "#"..(sf+32 - immr) 1074 last = "#"..(sf+32 - immr)
1085 operands[#operands] = last 1075 operands[#operands] = last
@@ -1088,28 +1078,28 @@ local function disass_ins(ctx)
1088 name = a1 1078 name = a1
1089 last = "#"..(sf+32 - immr) 1079 last = "#"..(sf+32 - immr)
1090 operands[#operands] = last 1080 operands[#operands] = last
1091 x = x + 1 1081 x += 1
1092 end 1082 end
1093 end 1083 end
1094 elseif p == "3" then 1084 elseif p == "3" then
1095 x = band(rshift(op, 10), 63) 1085 x = (op >> 10) & 63
1096 if altname then 1086 if altname then
1097 local a1, a2 = match(altname, "([^|]*)|(.*)") 1087 local a1, a2 = match(altname, "([^|]*)|(.*)")
1098 if x < immr then 1088 if x < immr then
1099 name = a1 1089 name = a1
1100 local sf = band(rshift(op, 26), 32) 1090 local sf = (op >> 26) & 32
1101 last = "#"..(sf+32 - immr) 1091 last = "#"..(sf+32 - immr)
1102 operands[#operands] = last 1092 operands[#operands] = last
1103 x = x + 1 1093 x += 1
1104 else 1094 else
1105 name = a2 1095 name = a2
1106 x = x - immr + 1 1096 x = x - immr + 1
1107 end 1097 end
1108 end 1098 end
1109 elseif p == "4" then 1099 elseif p == "4" then
1110 x = band(rshift(op, 10), 63) 1100 x = (op >> 10) & 63
1111 local rn = band(rshift(op, 5), 31) 1101 local rn = (op >> 5) & 31
1112 local rm = band(rshift(op, 16), 31) 1102 local rm = (op >> 16) & 31
1113 if altname and rn == rm then 1103 if altname and rn == rm then
1114 local n = #operands 1104 local n = #operands
1115 operands[n] = nil 1105 operands[n] = nil
@@ -1117,30 +1107,30 @@ local function disass_ins(ctx)
1117 name = altname 1107 name = altname
1118 end 1108 end
1119 elseif p == "5" then 1109 elseif p == "5" then
1120 x = band(rshift(op, 16), 31) 1110 x = (op >> 16) & 31
1121 elseif p == "S" then 1111 elseif p == "S" then
1122 x = band(rshift(op, 10), 63) 1112 x = (op >> 10) & 63
1123 if x == 0 then x = nil 1113 if x == 0 then x = nil
1124 else x = map_shift[band(rshift(op, 22), 3)].." #"..x end 1114 else x = map_shift[(op >> 22) & 3].." #"..x end
1125 elseif p == "X" then 1115 elseif p == "X" then
1126 local opt = band(rshift(op, 13), 7) 1116 local opt = (op >> 13) & 7
1127 -- Width specifier <R>. 1117 -- Width specifier <R>.
1128 if opt ~= 3 and opt ~= 7 then 1118 if opt != 3 and opt != 7 then
1129 last = map_regs.w[band(rshift(op, 16), 31)] 1119 last = map_regs.w[(op >> 16) & 31]
1130 operands[#operands] = last 1120 operands[#operands] = last
1131 end 1121 end
1132 x = band(rshift(op, 10), 7) 1122 x = (op >> 10) & 7
1133 -- Extension. 1123 -- Extension.
1134 if opt == 2 + band(rshift(op, 31), 1) and 1124 if opt == 2 + ((op >> 31) & 1) and
1135 band(rshift(op, second0 and 5 or 0), 31) == 31 then 1125 (op >> (second0 ? 5 : 0)) & 31 == 31 then
1136 if x == 0 then x = nil 1126 if x == 0 then x = nil
1137 else x = "lsl #"..x end 1127 else x = "lsl #"..x end
1138 else 1128 else
1139 if x == 0 then x = map_extend[band(rshift(op, 13), 7)] 1129 if x == 0 then x = map_extend[(op >> 13) & 7]
1140 else x = map_extend[band(rshift(op, 13), 7)].." #"..x end 1130 else x = map_extend[(op >> 13) & 7].." #"..x end
1141 end 1131 end
1142 elseif p == "R" then 1132 elseif p == "R" then
1143 x = band(rshift(op,21), 3) 1133 x = (op >> 21) & 3
1144 if x == 0 then x = nil 1134 if x == 0 then x = nil
1145 else x = "lsl #"..x*16 end 1135 else x = "lsl #"..x*16 end
1146 elseif p == "z" then 1136 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
15local match, gmatch = string.match, string.gmatch 15local match, gmatch = string.match, string.gmatch
16local concat = table.concat 16local concat = table.concat
17local bit = require("bit") 17local bit = require("bit")
18local band, bor, tohex = bit.band, bit.bor, bit.tohex 18local tohex = bit.tohex
19local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift
20 19
21------------------------------------------------------------------------------ 20------------------------------------------------------------------------------
22-- Extended opcode maps common to all MIPS releases 21-- Extended opcode maps common to all MIPS releases
@@ -477,13 +476,13 @@ end
477local function get_be(ctx) 476local function get_be(ctx)
478 local pos = ctx.pos 477 local pos = ctx.pos
479 local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) 478 local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4)
480 return bor(lshift(b0, 24), lshift(b1, 16), lshift(b2, 8), b3) 479 return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3
481end 480end
482 481
483local function get_le(ctx) 482local function get_le(ctx)
484 local pos = ctx.pos 483 local pos = ctx.pos
485 local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) 484 local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4)
486 return bor(lshift(b3, 24), lshift(b2, 16), lshift(b1, 8), b0) 485 return (b3 << 24) | (b2 << 16) | (b1 << 8) | b0
487end 486end
488 487
489-- Disassemble a single instruction. 488-- Disassemble a single instruction.
@@ -494,13 +493,13 @@ local function disass_ins(ctx)
494 ctx.op = op 493 ctx.op = op
495 ctx.rel = nil 494 ctx.rel = nil
496 495
497 local opat = ctx.map_pri[rshift(op, 26)] 496 local opat = ctx.map_pri[op >> 26]
498 while type(opat) ~= "string" do 497 while type(opat) != "string" do
499 if not opat then return unknown(ctx) end 498 if not opat then return unknown(ctx) end
500 if opat.maprs then 499 if opat.maprs then
501 opat = opat[opat.maprs(band(rshift(op,21),31), band(rshift(op,16),31))] 500 opat = opat[opat.maprs((op >> 21) & 31, (op >> 16) & 31)]
502 else 501 else
503 opat = opat[band(rshift(op, opat.shift), opat.mask)] or opat._ 502 opat = opat[(op >> opat.shift) & opat.mask] or opat._
504 end 503 end
505 end 504 end
506 local name, pat = match(opat, "^([a-z0-9_.]*)(.*)") 505 local name, pat = match(opat, "^([a-z0-9_.]*)(.*)")
@@ -510,82 +509,82 @@ local function disass_ins(ctx)
510 for p in gmatch(pat, ".") do 509 for p in gmatch(pat, ".") do
511 local x = nil 510 local x = nil
512 if p == "S" then 511 if p == "S" then
513 x = map_gpr[band(rshift(op, 21), 31)] 512 x = map_gpr[(op >> 21) & 31]
514 elseif p == "T" then 513 elseif p == "T" then
515 x = map_gpr[band(rshift(op, 16), 31)] 514 x = map_gpr[(op >> 16) & 31]
516 elseif p == "D" then 515 elseif p == "D" then
517 x = map_gpr[band(rshift(op, 11), 31)] 516 x = map_gpr[(op >> 11) & 31]
518 elseif p == "F" then 517 elseif p == "F" then
519 x = "f"..band(rshift(op, 6), 31) 518 x = "f"..((op >> 6) & 31)
520 elseif p == "G" then 519 elseif p == "G" then
521 x = "f"..band(rshift(op, 11), 31) 520 x = "f"..((op >> 11) & 31)
522 elseif p == "H" then 521 elseif p == "H" then
523 x = "f"..band(rshift(op, 16), 31) 522 x = "f"..((op >> 16) & 31)
524 elseif p == "R" then 523 elseif p == "R" then
525 x = "f"..band(rshift(op, 21), 31) 524 x = "f"..((op >> 21) & 31)
526 elseif p == "A" then 525 elseif p == "A" then
527 x = band(rshift(op, 6), 31) 526 x = (op >> 6) & 31
528 elseif p == "a" then 527 elseif p == "a" then
529 x = band(rshift(op, 6), 7) 528 x = (op >> 6) & 7
530 elseif p == "E" then 529 elseif p == "E" then
531 x = band(rshift(op, 6), 31) + 32 530 x = ((op >> 6) & 31) + 32
532 elseif p == "M" then 531 elseif p == "M" then
533 x = band(rshift(op, 11), 31) 532 x = (op >> 11) & 31
534 elseif p == "N" then 533 elseif p == "N" then
535 x = band(rshift(op, 16), 31) 534 x = (op >> 16) & 31
536 elseif p == "C" then 535 elseif p == "C" then
537 x = band(rshift(op, 18), 7) 536 x = (op >> 18) & 7
538 if x == 0 then x = nil end 537 if x == 0 then x = nil end
539 elseif p == "K" then 538 elseif p == "K" then
540 x = band(rshift(op, 11), 31) + 1 539 x = ((op >> 11) & 31) + 1
541 elseif p == "P" then 540 elseif p == "P" then
542 x = band(rshift(op, 11), 31) + 33 541 x = ((op >> 11) & 31) + 33
543 elseif p == "L" then 542 elseif p == "L" then
544 x = band(rshift(op, 11), 31) - last + 1 543 x = ((op >> 11) & 31) - last + 1
545 elseif p == "Q" then 544 elseif p == "Q" then
546 x = band(rshift(op, 11), 31) - last + 33 545 x = ((op >> 11) & 31) - last + 33
547 elseif p == "I" then 546 elseif p == "I" then
548 x = arshift(lshift(op, 16), 16) 547 x = (op << 16) ~>> 16
549 elseif p == "2" then 548 elseif p == "2" then
550 x = arshift(lshift(op, 13), 11) 549 x = (op << 13) ~>> 11
551 elseif p == "3" then 550 elseif p == "3" then
552 x = arshift(lshift(op, 14), 11) 551 x = (op << 14) ~>> 11
553 elseif p == "U" then 552 elseif p == "U" then
554 x = band(op, 0xffff) 553 x = op & 0xffff
555 elseif p == "O" then 554 elseif p == "O" then
556 local disp = arshift(lshift(op, 16), 16) 555 local disp = (op << 16) ~>> 16
557 operands[#operands] = format("%d(%s)", disp, last) 556 operands[#operands] = format("%d(%s)", disp, last)
558 elseif p == "X" then 557 elseif p == "X" then
559 local index = map_gpr[band(rshift(op, 16), 31)] 558 local index = map_gpr[(op >> 16) & 31]
560 operands[#operands] = format("%s(%s)", index, last) 559 operands[#operands] = format("%s(%s)", index, last)
561 elseif p == "B" then 560 elseif p == "B" then
562 x = ctx.addr + ctx.pos + arshift(lshift(op, 16), 14) + 4 561 x = ctx.addr + ctx.pos + ((op << 16) ~>> 14) + 4
563 ctx.rel = x 562 ctx.rel = x
564 x = format("0x%08x", x) 563 x = format("0x%08x", x)
565 elseif p == "b" then 564 elseif p == "b" then
566 x = ctx.addr + ctx.pos + arshift(lshift(op, 11), 9) + 4 565 x = ctx.addr + ctx.pos + ((op << 11) ~>> 9) + 4
567 ctx.rel = x 566 ctx.rel = x
568 x = format("0x%08x", x) 567 x = format("0x%08x", x)
569 elseif p == "#" then 568 elseif p == "#" then
570 x = ctx.addr + ctx.pos + arshift(lshift(op, 6), 4) + 4 569 x = ctx.addr + ctx.pos + ((op << 6) ~>> 4) + 4
571 ctx.rel = x 570 ctx.rel = x
572 x = format("0x%08x", x) 571 x = format("0x%08x", x)
573 elseif p == "J" then 572 elseif p == "J" then
574 local a = ctx.addr + ctx.pos 573 local a = ctx.addr + ctx.pos
575 x = a - band(a, 0x0fffffff) + band(op, 0x03ffffff)*4 574 x = a - (a & 0x0fffffff) + ((op & 0x03ffffff) << 2)
576 ctx.rel = x 575 ctx.rel = x
577 x = format("0x%08x", x) 576 x = format("0x%08x", x)
578 elseif p == "V" then 577 elseif p == "V" then
579 x = band(rshift(op, 8), 7) 578 x = (op >> 8) & 7
580 if x == 0 then x = nil end 579 if x == 0 then x = nil end
581 elseif p == "W" then 580 elseif p == "W" then
582 x = band(op, 7) 581 x = op & 7
583 if x == 0 then x = nil end 582 if x == 0 then x = nil end
584 elseif p == "Y" then 583 elseif p == "Y" then
585 x = band(rshift(op, 6), 0x000fffff) 584 x = (op >> 6) & 0x000fffff
586 if x == 0 then x = nil end 585 if x == 0 then x = nil end
587 elseif p == "Z" then 586 elseif p == "Z" then
588 x = band(rshift(op, 6), 1023) 587 x = (op >> 6) & 1023
589 if x == 0 then x = nil end 588 if x == 0 then x = nil end
590 elseif p == "0" then 589 elseif p == "0" then
591 if last == "r0" or last == 0 then 590 if last == "r0" or last == 0 then
@@ -616,9 +615,9 @@ end
616-- Disassemble a block of code. 615-- Disassemble a block of code.
617local function disass_block(ctx, ofs, len) 616local function disass_block(ctx, ofs, len)
618 if not ofs then ofs = 0 end 617 if not ofs then ofs = 0 end
619 local stop = len and ofs+len or #ctx.code 618 local stop = len ? ofs+len : #ctx.code
620 stop = stop - stop % 4 619 stop &= -4
621 ctx.pos = ofs - ofs % 4 620 ctx.pos = ofs & -4
622 ctx.rel = nil 621 ctx.rel = nil
623 while ctx.pos < stop do disass_ins(ctx) end 622 while ctx.pos < stop do disass_ins(ctx) end
624end 623end
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
17local match, gmatch, gsub = string.match, string.gmatch, string.gsub 17local match, gmatch, gsub = string.match, string.gmatch, string.gsub
18local concat = table.concat 18local concat = table.concat
19local bit = require("bit") 19local bit = require("bit")
20local band, bor, tohex = bit.band, bit.bor, bit.tohex 20local tohex = bit.tohex
21local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift
22 21
23------------------------------------------------------------------------------ 22------------------------------------------------------------------------------
24-- Primary and extended opcode maps 23-- Primary and extended opcode maps
@@ -39,9 +38,9 @@ local map_rlwinm = setmetatable({
39 shift = 0, mask = -1, 38 shift = 0, mask = -1,
40}, 39},
41{ __index = function(t, x) 40{ __index = function(t, x)
42 local rot = band(rshift(x, 11), 31) 41 local rot = (x >> 11) & 31
43 local mb = band(rshift(x, 6), 31) 42 local mb = (x >> 6) & 31
44 local me = band(rshift(x, 1), 31) 43 local me = (x >> 1) & 31
45 if mb == 0 and me == 31-rot then 44 if mb == 0 and me == 31-rot then
46 return "slwiRR~A." 45 return "slwiRR~A."
47 elseif me == 31 and mb == 32-rot then 46 elseif me == 31 and mb == 32-rot then
@@ -167,7 +166,7 @@ local map_ext = setmetatable({
167 [539] = "srdRR~R.", 166 [539] = "srdRR~R.",
168}, 167},
169{ __index = function(t, x) 168{ __index = function(t, x)
170 if band(x, 31) == 15 then return "iselRRRC" end 169 if x & 31 == 15 then return "iselRRRC" end
171 end 170 end
172}) 171})
173 172
@@ -386,9 +385,9 @@ local map_cond = { [0] = "lt", "gt", "eq", "so", "ge", "le", "ne", "ns", }
386-- Format a condition bit. 385-- Format a condition bit.
387local function condfmt(cond) 386local function condfmt(cond)
388 if cond <= 3 then 387 if cond <= 3 then
389 return map_cond[band(cond, 3)] 388 return map_cond[cond & 3]
390 else 389 else
391 return format("4*cr%d+%s", rshift(cond, 2), map_cond[band(cond, 3)]) 390 return format("4*cr%d+%s", cond >> 2, map_cond[cond & 3])
392 end 391 end
393end 392end
394 393
@@ -421,17 +420,17 @@ end
421local function disass_ins(ctx) 420local function disass_ins(ctx)
422 local pos = ctx.pos 421 local pos = ctx.pos
423 local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) 422 local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4)
424 local op = bor(lshift(b0, 24), lshift(b1, 16), lshift(b2, 8), b3) 423 local op = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3
425 local operands = {} 424 local operands = {}
426 local last = nil 425 local last = nil
427 local rs = 21 426 local rs = 21
428 ctx.op = op 427 ctx.op = op
429 ctx.rel = nil 428 ctx.rel = nil
430 429
431 local opat = map_pri[rshift(b0, 2)] 430 local opat = map_pri[b0 >> 2]
432 while type(opat) ~= "string" do 431 while type(opat) != "string" do
433 if not opat then return unknown(ctx) end 432 if not opat then return unknown(ctx) end
434 opat = opat[band(rshift(op, opat.shift), opat.mask)] 433 opat = opat[(op >> opat.shift) & opat.mask]
435 end 434 end
436 local name, pat = match(opat, "^([a-z0-9_.]*)(.*)") 435 local name, pat = match(opat, "^([a-z0-9_.]*)(.*)")
437 local altname, pat2 = match(pat, "|([a-z0-9_.]*)(.*)") 436 local altname, pat2 = match(pat, "|([a-z0-9_.]*)(.*)")
@@ -440,84 +439,84 @@ local function disass_ins(ctx)
440 for p in gmatch(pat, ".") do 439 for p in gmatch(pat, ".") do
441 local x = nil 440 local x = nil
442 if p == "R" then 441 if p == "R" then
443 x = map_gpr[band(rshift(op, rs), 31)] 442 x = map_gpr[(op >> rs) & 31]
444 rs = rs - 5 443 rs -= 5
445 elseif p == "F" then 444 elseif p == "F" then
446 x = "f"..band(rshift(op, rs), 31) 445 x = "f"..((op >> rs) & 31)
447 rs = rs - 5 446 rs -= 5
448 elseif p == "A" then 447 elseif p == "A" then
449 x = band(rshift(op, rs), 31) 448 x = (op >> rs) & 31
450 rs = rs - 5 449 rs -= 5
451 elseif p == "S" then 450 elseif p == "S" then
452 x = arshift(lshift(op, 27-rs), 27) 451 x = (op << 27-rs) ~>> 27
453 rs = rs - 5 452 rs -= 5
454 elseif p == "I" then 453 elseif p == "I" then
455 x = arshift(lshift(op, 16), 16) 454 x = (op << 16) ~>> 16
456 elseif p == "U" then 455 elseif p == "U" then
457 x = band(op, 0xffff) 456 x = op & 0xffff
458 elseif p == "D" or p == "E" then 457 elseif p == "D" or p == "E" then
459 local disp = arshift(lshift(op, 16), 16) 458 local disp = (op << 16) ~>> 16
460 if p == "E" then disp = band(disp, -4) end 459 if p == "E" then disp &= -4 end
461 if last == "r0" then last = "0" end 460 if last == "r0" then last = "0" end
462 operands[#operands] = format("%d(%s)", disp, last) 461 operands[#operands] = format("%d(%s)", disp, last)
463 elseif p >= "2" and p <= "8" then 462 elseif p >= "2" and p <= "8" then
464 local disp = band(rshift(op, rs), 31) * p 463 local disp = ((op >> rs) & 31) * (byte(p) - 0x30)
465 if last == "r0" then last = "0" end 464 if last == "r0" then last = "0" end
466 operands[#operands] = format("%d(%s)", disp, last) 465 operands[#operands] = format("%d(%s)", disp, last)
467 elseif p == "H" then 466 elseif p == "H" then
468 x = band(rshift(op, rs), 31) + lshift(band(op, 2), 4) 467 x = ((op >> rs) & 31) | ((op & 2) << 4)
469 rs = rs - 5 468 rs -= 5
470 elseif p == "M" then 469 elseif p == "M" then
471 x = band(rshift(op, rs), 31) + band(op, 0x20) 470 x = ((op >> rs) & 31) | (op & 0x20)
472 elseif p == "C" then 471 elseif p == "C" then
473 x = condfmt(band(rshift(op, rs), 31)) 472 x = condfmt((op >> rs) & 31)
474 rs = rs - 5 473 rs -= 5
475 elseif p == "B" then 474 elseif p == "B" then
476 local bo = rshift(op, 21) 475 local bo = op >> 21
477 local cond = band(rshift(op, 16), 31) 476 local cond = (op >> 16) & 31
478 local cn = "" 477 local cn = ""
479 rs = rs - 10 478 rs -= 10
480 if band(bo, 4) == 0 then 479 if bo & 4 == 0 then
481 cn = band(bo, 2) == 0 and "dnz" or "dz" 480 cn = bo & 2 == 0 ? "dnz" : "dz"
482 if band(bo, 0x10) == 0 then 481 if bo & 0x10 == 0 then
483 cn = cn..(band(bo, 8) == 0 and "f" or "t") 482 cn ..= bo & 8 == 0 ? "f" : "t"
483 x = condfmt(cond)
484 end 484 end
485 if band(bo, 0x10) == 0 then x = condfmt(cond) end 485 name ..= bo & 1 == (op >> 15) & 1 ? "-" : "+"
486 name = name..(band(bo, 1) == band(rshift(op, 15), 1) and "-" or "+") 486 elseif bo & 0x10 == 0 then
487 elseif band(bo, 0x10) == 0 then 487 cn = map_cond[(cond & 3) | ((bo >> 1) & 4)]
488 cn = map_cond[band(cond, 3) + (band(bo, 8) == 0 and 4 or 0)] 488 if cond > 3 then x = "cr"..(cond >> 2) end
489 if cond > 3 then x = "cr"..rshift(cond, 2) end 489 name ..= bo & 1 == (op >> 15) & 1 ? "-" : "+"
490 name = name..(band(bo, 1) == band(rshift(op, 15), 1) and "-" or "+")
491 end 490 end
492 name = gsub(name, "_", cn) 491 name = gsub(name, "_", cn)
493 elseif p == "J" then 492 elseif p == "J" then
494 x = arshift(lshift(op, 27-rs), 29-rs)*4 493 x = ((op << 27-rs) ~>> 29-rs) << 2
495 if band(op, 2) == 0 then x = ctx.addr + pos + x end 494 if op & 2 == 0 then x = ctx.addr + pos + x end
496 ctx.rel = x 495 ctx.rel = x
497 x = "0x"..tohex(x) 496 x = "0x"..tohex(x)
498 elseif p == "K" then 497 elseif p == "K" then
499 if band(op, 1) ~= 0 then name = name.."l" end 498 if op & 1 != 0 then name ..= "l" end
500 if band(op, 2) ~= 0 then name = name.."a" end 499 if op & 2 != 0 then name ..= "a" end
501 elseif p == "X" or p == "Y" then 500 elseif p == "X" or p == "Y" then
502 x = band(rshift(op, rs+2), 7) 501 x = (op >> rs+2) & 7
503 if x == 0 and p == "Y" then x = nil else x = "cr"..x end 502 x = x == 0 and p == "Y" ? nil : "cr"..x
504 rs = rs - 5 503 rs -= 5
505 elseif p == "W" then 504 elseif p == "W" then
506 x = "cr"..band(op, 7) 505 x = "cr"..(op & 7)
507 elseif p == "Z" then 506 elseif p == "Z" then
508 x = band(rshift(op, rs-4), 255) 507 x = (op >> rs-4) & 0xff
509 rs = rs - 10 508 rs -= 10
510 elseif p == ">" then 509 elseif p == ">" then
511 operands[#operands] = rshift(operands[#operands], 1) 510 operands[#operands] >>= 1
512 elseif p == "0" then 511 elseif p == "0" then
513 if last == "r0" then 512 if last == "r0" then
514 operands[#operands] = nil 513 operands[#operands] = nil
515 if altname then name = altname end 514 if altname then name = altname end
516 end 515 end
517 elseif p == "L" then 516 elseif p == "L" then
518 name = gsub(name, "_", band(op, 0x00200000) ~= 0 and "d" or "w") 517 name = gsub(name, "_", op & 0x00200000 != 0 ? "d" : "w")
519 elseif p == "." then 518 elseif p == "." then
520 if band(op, 1) == 1 then name = name.."." end 519 if op & 1 == 1 then name ..= "." end
521 elseif p == "N" then 520 elseif p == "N" then
522 if op == 0x60000000 then name = "nop"; break end 521 if op == 0x60000000 then name = "nop"; break end
523 elseif p == "~" then 522 elseif p == "~" then
@@ -537,7 +536,7 @@ local function disass_ins(ctx)
537 name = altname 536 name = altname
538 end 537 end
539 elseif p == "-" then 538 elseif p == "-" then
540 rs = rs - 5 539 rs -= 5
541 else 540 else
542 assert(false) 541 assert(false)
543 end 542 end
@@ -553,8 +552,8 @@ end
553local function disass_block(ctx, ofs, len) 552local function disass_block(ctx, ofs, len)
554 if not ofs then ofs = 0 end 553 if not ofs then ofs = 0 end
555 local stop = len and ofs+len or #ctx.code 554 local stop = len and ofs+len or #ctx.code
556 stop = stop - stop % 4 555 stop &= -4
557 ctx.pos = ofs - ofs % 4 556 ctx.pos = ofs & -4
558 ctx.rel = nil 557 ctx.rel = nil
559 while ctx.pos < stop do disass_ins(ctx) end 558 while ctx.pos < stop do disass_ins(ctx) end
560end 559end
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)
421 local hmax = ctx.hexdump 421 local hmax = ctx.hexdump
422 if hmax > 0 then 422 if hmax > 0 then
423 for i=ctx.start,pos-1 do 423 for i=ctx.start,pos-1 do
424 hex = hex..format("%02X", byte(code, i, i)) 424 hex ..= format("%02X", byte(code, i))
425 end 425 end
426 if #hex > hmax then hex = sub(hex, 1, hmax)..". " 426 if #hex > hmax then hex = sub(hex, 1, hmax)..". "
427 else hex = hex..rep(" ", hmax-#hex+2) end 427 else hex ..= rep(" ", hmax-#hex+2) end
428 end 428 end
429 if operands then text = text.." "..operands end 429 if operands then text = text.." "..operands end
430 if ctx.o16 then text = "o16 "..text; ctx.o16 = false end 430 if ctx.o16 then text = "o16 "..text; ctx.o16 = false end
431 if ctx.a32 then text = "a32 "..text; ctx.a32 = false end 431 if ctx.a32 then text = "a32 "..text; ctx.a32 = false end
432 if ctx.rep then text = ctx.rep.." "..text; ctx.rep = false end 432 if ctx.rep then text = ctx.rep.." "..text; ctx.rep = false end
433 if ctx.rex then 433 if ctx.rex then
434 local t = (ctx.rexw and "w" or "")..(ctx.rexr and "r" or "").. 434 local t = (ctx.rexw ? "w" : "")..(ctx.rexr ? "r" : "")..
435 (ctx.rexx and "x" or "")..(ctx.rexb and "b" or "").. 435 (ctx.rexx ? "x" : "")..(ctx.rexb ? "b" : "")..
436 (ctx.vexl and "l" or "") 436 (ctx.vexl ? "l" : "")
437 if ctx.vexv and ctx.vexv ~= 0 then t = t.."v"..ctx.vexv end 437 if ctx.vexv and ctx.vexv != 0 then t = t.."v"..ctx.vexv end
438 if t ~= "" then text = ctx.rex.."."..t.." "..gsub(text, "^ ", "") 438 if t != "" then text = ctx.rex.."."..t.." "..gsub(text, "^ ", "")
439 elseif ctx.rex == "vex" then text = gsub("v"..text, "^v ", "") end 439 elseif ctx.rex == "vex" then text = gsub("v"..text, "^v ", "") end
440 ctx.rexw = false; ctx.rexr = false; ctx.rexx = false; ctx.rexb = false 440 ctx.rexw = false; ctx.rexr = false; ctx.rexx = false; ctx.rexb = false
441 ctx.rex = false; ctx.vexl = false; ctx.vexv = false 441 ctx.rex = false; ctx.vexl = false; ctx.vexv = false
@@ -483,7 +483,7 @@ local function getimm(ctx, pos, n)
483 if pos+n-1 > ctx.stop then return incomplete(ctx) end 483 if pos+n-1 > ctx.stop then return incomplete(ctx) end
484 local code = ctx.code 484 local code = ctx.code
485 if n == 1 then 485 if n == 1 then
486 local b1 = byte(code, pos, pos) 486 local b1 = byte(code, pos)
487 return b1 487 return b1
488 elseif n == 2 then 488 elseif n == 2 then
489 local b1, b2 = byte(code, pos, pos+1) 489 local b1, b2 = byte(code, pos, pos+1)
@@ -521,41 +521,41 @@ local function putpat(ctx, name, pat)
521 if sz == "X" and vexl then sz = "Y"; ctx.vexl = false end 521 if sz == "X" and vexl then sz = "Y"; ctx.vexl = false end
522 regs = map_regs[sz] 522 regs = map_regs[sz]
523 elseif p == "P" then 523 elseif p == "P" then
524 sz = ctx.o16 and "X" or "M"; ctx.o16 = false 524 sz = ctx.o16 ? "X" : "M"; ctx.o16 = false
525 if sz == "X" and vexl then sz = "Y"; ctx.vexl = false end 525 if sz == "X" and vexl then sz = "Y"; ctx.vexl = false end
526 regs = map_regs[sz] 526 regs = map_regs[sz]
527 elseif p == "H" then 527 elseif p == "H" then
528 name = name..(ctx.rexw and "d" or "s") 528 name ..= ctx.rexw ? "d" : "s"
529 ctx.rexw = false 529 ctx.rexw = false
530 elseif p == "S" then 530 elseif p == "S" then
531 name = name..lower(sz) 531 name ..= lower(sz)
532 elseif p == "s" then 532 elseif p == "s" then
533 local imm = getimm(ctx, pos, 1); if not imm then return end 533 local imm = getimm(ctx, pos, 1); if not imm then return end
534 x = imm <= 127 and format("+0x%02x", imm) 534 x = imm <= 127 ? format("+0x%02x", imm)
535 or format("-0x%02x", 256-imm) 535 : format("-0x%02x", 256-imm)
536 pos = pos+1 536 pos += 1
537 elseif p == "u" then 537 elseif p == "u" then
538 local imm = getimm(ctx, pos, 1); if not imm then return end 538 local imm = getimm(ctx, pos, 1); if not imm then return end
539 x = format("0x%02x", imm) 539 x = format("0x%02x", imm)
540 pos = pos+1 540 pos += 1
541 elseif p == "b" then 541 elseif p == "b" then
542 local imm = getimm(ctx, pos, 1); if not imm then return end 542 local imm = getimm(ctx, pos, 1); if not imm then return end
543 x = regs[imm/16+1] 543 x = regs[imm/16+1]
544 pos = pos+1 544 pos += 1
545 elseif p == "w" then 545 elseif p == "w" then
546 local imm = getimm(ctx, pos, 2); if not imm then return end 546 local imm = getimm(ctx, pos, 2); if not imm then return end
547 x = format("0x%x", imm) 547 x = format("0x%x", imm)
548 pos = pos+2 548 pos += 2
549 elseif p == "o" then -- [offset] 549 elseif p == "o" then -- [offset]
550 if ctx.x64 then 550 if ctx.x64 then
551 local imm1 = getimm(ctx, pos, 4); if not imm1 then return end 551 local imm1 = getimm(ctx, pos, 4); if not imm1 then return end
552 local imm2 = getimm(ctx, pos+4, 4); if not imm2 then return end 552 local imm2 = getimm(ctx, pos+4, 4); if not imm2 then return end
553 x = format("[0x%08x%08x]", imm2, imm1) 553 x = format("[0x%08x%08x]", imm2, imm1)
554 pos = pos+8 554 pos += 8
555 else 555 else
556 local imm = getimm(ctx, pos, 4); if not imm then return end 556 local imm = getimm(ctx, pos, 4); if not imm then return end
557 x = format("[0x%08x]", imm) 557 x = format("[0x%08x]", imm)
558 pos = pos+4 558 pos += 4
559 end 559 end
560 elseif p == "i" or p == "I" then 560 elseif p == "i" or p == "I" then
561 local n = map_sz2n[sz] 561 local n = map_sz2n[sz]
@@ -568,21 +568,21 @@ local function putpat(ctx, name, pat)
568 local imm = getimm(ctx, pos, n); if not imm then return end 568 local imm = getimm(ctx, pos, n); if not imm then return end
569 if sz == "Q" and (imm < 0 or imm > 0x7fffffff) then 569 if sz == "Q" and (imm < 0 or imm > 0x7fffffff) then
570 imm = (0xffffffff+1)-imm 570 imm = (0xffffffff+1)-imm
571 x = format(imm > 65535 and "-0x%08x" or "-0x%x", imm) 571 x = format(imm > 65535 ? "-0x%08x" : "-0x%x", imm)
572 else 572 else
573 x = format(imm > 65535 and "0x%08x" or "0x%x", imm) 573 x = format(imm > 65535 ? "0x%08x" : "0x%x", imm)
574 end 574 end
575 end 575 end
576 pos = pos+n 576 pos += n
577 elseif p == "j" then 577 elseif p == "j" then
578 local n = map_sz2n[sz] 578 local n = map_sz2n[sz]
579 if n == 8 then n = 4 end 579 if n == 8 then n = 4 end
580 local imm = getimm(ctx, pos, n); if not imm then return end 580 local imm = getimm(ctx, pos, n); if not imm then return end
581 if sz == "B" and imm > 127 then imm = imm-256 581 if sz == "B" and imm > 127 then imm -= 256
582 elseif imm > 2147483647 then imm = imm-4294967296 end 582 elseif imm > 2147483647 then imm -= 4294967296 end
583 pos = pos+n 583 pos += n
584 imm = imm + pos + ctx.addr 584 imm = imm + pos + ctx.addr
585 if imm > 4294967295 and not ctx.x64 then imm = imm-4294967296 end 585 if imm > 4294967295 and not ctx.x64 then imm -= 4294967296 end
586 ctx.imm = imm 586 ctx.imm = imm
587 if sz == "W" then 587 if sz == "W" then
588 x = format("word 0x%04x", imm%65536) 588 x = format("word 0x%04x", imm%65536)
@@ -593,8 +593,8 @@ local function putpat(ctx, name, pat)
593 x = "0x"..tohex(imm) 593 x = "0x"..tohex(imm)
594 end 594 end
595 elseif p == "R" then 595 elseif p == "R" then
596 local r = byte(code, pos-1, pos-1)%8 596 local r = byte(code, pos-1) & 7
597 if ctx.rexb then r = r + 8; ctx.rexb = false end 597 if ctx.rexb then r += 8; ctx.rexb = false end
598 x = regs[r+1] 598 x = regs[r+1]
599 elseif p == "a" then x = regs[1] 599 elseif p == "a" then x = regs[1]
600 elseif p == "c" then x = "cl" 600 elseif p == "c" then x = "cl"
@@ -605,25 +605,25 @@ local function putpat(ctx, name, pat)
605 mode = ctx.mrm 605 mode = ctx.mrm
606 if not mode then 606 if not mode then
607 if pos > stop then return incomplete(ctx) end 607 if pos > stop then return incomplete(ctx) end
608 mode = byte(code, pos, pos) 608 mode = byte(code, pos)
609 pos = pos+1 609 pos += 1
610 end 610 end
611 rm = mode%8; mode = (mode-rm)/8 611 rm = mode & 7; mode >>= 3
612 sp = mode%8; mode = (mode-sp)/8 612 sp = mode & 7; mode >>= 3
613 sdisp = "" 613 sdisp = ""
614 if mode < 3 then 614 if mode < 3 then
615 if rm == 4 then 615 if rm == 4 then
616 if pos > stop then return incomplete(ctx) end 616 if pos > stop then return incomplete(ctx) end
617 sc = byte(code, pos, pos) 617 sc = byte(code, pos)
618 pos = pos+1 618 pos += 1
619 rm = sc%8; sc = (sc-rm)/8 619 rm = sc & 7; sc >>= 3
620 rx = sc%8; sc = (sc-rx)/8 620 rx = sc & 7; sc >>= 3
621 if ctx.rexx then rx = rx + 8; ctx.rexx = false end 621 if ctx.rexx then rx += 8; ctx.rexx = false end
622 if rx == 4 then rx = nil end 622 if rx == 4 then rx = nil end
623 end 623 end
624 if mode > 0 or rm == 5 then 624 if mode > 0 or rm == 5 then
625 local dsz = mode 625 local dsz = mode
626 if dsz ~= 1 then dsz = 4 end 626 if dsz != 1 then dsz = 4 end
627 local disp = getimm(ctx, pos, dsz); if not disp then return end 627 local disp = getimm(ctx, pos, dsz); if not disp then return end
628 if mode == 0 then rm = nil end 628 if mode == 0 then rm = nil end
629 if rm or rx or (not sc and ctx.x64 and not ctx.a32) then 629 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)
637 else 637 else
638 sdisp = format(ctx.x64 and not ctx.a32 and 638 sdisp = format(ctx.x64 and not ctx.a32 and
639 not (disp >= 0 and disp <= 0x7fffffff) 639 not (disp >= 0 and disp <= 0x7fffffff)
640 and "0xffffffff%08x" or "0x%08x", disp) 640 ? "0xffffffff%08x" : "0x%08x", disp)
641 end 641 end
642 pos = pos+dsz 642 pos += dsz
643 end 643 end
644 end 644 end
645 if rm and ctx.rexb then rm = rm + 8; ctx.rexb = false end 645 if rm and ctx.rexb then rm += 8; ctx.rexb = false end
646 if ctx.rexr then sp = sp + 8; ctx.rexr = false end 646 if ctx.rexr then sp += 8; ctx.rexr = false end
647 end 647 end
648 if p == "m" then 648 if p == "m" then
649 if mode == 3 then x = regs[rm+1] 649 if mode == 3 then x = regs[rm+1]
650 else 650 else
651 local aregs = ctx.a32 and map_regs.D or ctx.aregs 651 local aregs = ctx.a32 ? map_regs.D : ctx.aregs
652 local srm, srx = "", "" 652 local srm, srx = "", ""
653 if rm then srm = aregs[rm+1] 653 if rm then srm = aregs[rm+1]
654 elseif not sc and ctx.x64 and not ctx.a32 then srm = "rip" end 654 elseif not sc and ctx.x64 and not ctx.a32 then srm = "rip" end
655 ctx.a32 = false 655 ctx.a32 = false
656 if rx then 656 if rx then
657 if rm then srm = srm.."+" end 657 if rm then srm ..= "+" end
658 srx = aregs[rx+1] 658 srx = aregs[rx+1]
659 if sc > 0 then srx = srx.."*"..(2^sc) end 659 if sc > 0 then srx = srx.."*"..(1 << sc) end
660 end 660 end
661 x = format("[%s%s%s]", srm, srx, sdisp) 661 x = format("[%s%s%s]", srm, srx, sdisp)
662 end 662 end
@@ -686,7 +686,7 @@ local function putpat(ctx, name, pat)
686 error("bad pattern `"..pat.."'") 686 error("bad pattern `"..pat.."'")
687 end 687 end
688 end 688 end
689 if x then operands = operands and operands..", "..x or x end 689 if x then operands = operands ? operands..", "..x : x end
690 end 690 end
691 ctx.pos = pos 691 ctx.pos = pos
692 return putop(ctx, name, operands) 692 return putop(ctx, name, operands)
@@ -701,8 +701,8 @@ local function getmrm(ctx)
701 if not mrm then 701 if not mrm then
702 local pos = ctx.pos 702 local pos = ctx.pos
703 if pos > ctx.stop then return nil end 703 if pos > ctx.stop then return nil end
704 mrm = byte(ctx.code, pos, pos) 704 mrm = byte(ctx.code, pos)
705 ctx.pos = pos+1 705 ctx.pos = pos + 1
706 ctx.mrm = mrm 706 ctx.mrm = mrm
707 end 707 end
708 return mrm 708 return mrm
@@ -714,7 +714,7 @@ local function dispatch(ctx, opat, patgrp)
714 if match(opat, "%|") then -- MMX/SSE variants depending on prefix. 714 if match(opat, "%|") then -- MMX/SSE variants depending on prefix.
715 local p 715 local p
716 if ctx.rep then 716 if ctx.rep then
717 p = ctx.rep=="rep" and "%|([^%|]*)" or "%|[^%|]*%|[^%|]*%|([^%|]*)" 717 p = ctx.rep == "rep" ? "%|([^%|]*)" : "%|[^%|]*%|[^%|]*%|([^%|]*)"
718 ctx.rep = false 718 ctx.rep = false
719 elseif ctx.o16 then p = "%|[^%|]*%|([^%|]*)"; ctx.o16 = false 719 elseif ctx.o16 then p = "%|[^%|]*%|([^%|]*)"; ctx.o16 = false
720 else p = "^[^%|]*" end 720 else p = "^[^%|]*" end
@@ -726,7 +726,7 @@ local function dispatch(ctx, opat, patgrp)
726 end 726 end
727 if match(opat, "%$") then -- reg$mem variants. 727 if match(opat, "%$") then -- reg$mem variants.
728 local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end 728 local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end
729 opat = match(opat, mrm >= 192 and "^[^%$]*" or "%$(.*)") 729 opat = match(opat, mrm >= 192 ? "^[^%$]*" : "%$(.*)")
730 if opat == "" then return unknown(ctx) end 730 if opat == "" then return unknown(ctx) end
731 end 731 end
732 if opat == "" then return unknown(ctx) end 732 if opat == "" then return unknown(ctx) end
@@ -738,9 +738,8 @@ end
738-- Get a pattern from an opcode map and dispatch to handler. 738-- Get a pattern from an opcode map and dispatch to handler.
739local function dispatchmap(ctx, opcmap) 739local function dispatchmap(ctx, opcmap)
740 local pos = ctx.pos 740 local pos = ctx.pos
741 local opat = opcmap[byte(ctx.code, pos, pos)] 741 local opat = opcmap[byte(ctx.code, pos)]
742 pos = pos + 1 742 ctx.pos = pos + 1
743 ctx.pos = pos
744 return dispatch(ctx, opat) 743 return dispatch(ctx, opat)
745end 744end
746 745
@@ -760,7 +759,7 @@ map_act = {
760 759
761 -- Collect prefixes. 760 -- Collect prefixes.
762 [":"] = function(ctx, name, pat) 761 [":"] = function(ctx, name, pat)
763 ctx[pat == ":" and name or sub(pat, 2)] = name 762 ctx[pat == ":" ? name : sub(pat, 2)] = name
764 if ctx.pos - ctx.start > 5 then return unknown(ctx) end -- Limit #prefixes. 763 if ctx.pos - ctx.start > 5 then return unknown(ctx) end -- Limit #prefixes.
765 end, 764 end,
766 765
@@ -772,7 +771,7 @@ map_act = {
772 -- Use named subtable for opcode group. 771 -- Use named subtable for opcode group.
773 ["!"] = function(ctx, name, pat) 772 ["!"] = function(ctx, name, pat)
774 local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end 773 local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end
775 return dispatch(ctx, map_opcgroup[name][((mrm-(mrm%8))/8)%8+1], sub(pat, 2)) 774 return dispatch(ctx, map_opcgroup[name][((mrm >> 3) & 7)+1], sub(pat, 2))
776 end, 775 end,
777 776
778 -- o16,o32[,o64] variants. 777 -- o16,o32[,o64] variants.
@@ -825,9 +824,9 @@ map_act = {
825 -- Floating point opcode dispatch. 824 -- Floating point opcode dispatch.
826 fp = function(ctx, name, pat) 825 fp = function(ctx, name, pat)
827 local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end 826 local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end
828 local rm = mrm%8 827 local rm = mrm & 7
829 local idx = pat*8 + ((mrm-rm)/8)%8 828 local idx = ((byte(pat) - 0x30) << 3) | ((mrm >> 3) & 7)
830 if mrm >= 192 then idx = idx + 64 end 829 if mrm >= 192 then idx += 64 end
831 local opat = map_opcfp[idx] 830 local opat = map_opcfp[idx]
832 if type(opat) == "table" then opat = opat[rm+1] end 831 if type(opat) == "table" then opat = opat[rm+1] end
833 return dispatch(ctx, opat) 832 return dispatch(ctx, opat)
@@ -847,23 +846,21 @@ map_act = {
847 local pos = ctx.pos 846 local pos = ctx.pos
848 if ctx.mrm then 847 if ctx.mrm then
849 ctx.mrm = nil 848 ctx.mrm = nil
850 pos = pos-1 849 pos -= 1
851 end 850 end
852 local b = byte(ctx.code, pos, pos) 851 local b = byte(ctx.code, pos)
853 if not b then return incomplete(ctx) end 852 if not b then return incomplete(ctx) end
854 pos = pos+1 853 pos += 1
855 if b < 128 then ctx.rexr = true end 854 if b < 128 then ctx.rexr = true end
856 local m = 1 855 local m = 1
857 if pat == "3" then 856 if pat == "3" then
858 m = b%32; b = (b-m)/32 857 m = b & 0x1f
859 local nb = b%2; b = (b-nb)/2 858 if b & 0x20 == 0 then ctx.rexb = true end
860 if nb == 0 then ctx.rexb = true end 859 if b & 0x40 == 0 then ctx.rexx = true end
861 local nx = b%2 860 b = byte(ctx.code, pos)
862 if nx == 0 then ctx.rexx = true end
863 b = byte(ctx.code, pos, pos)
864 if not b then return incomplete(ctx) end 861 if not b then return incomplete(ctx) end
865 pos = pos+1 862 pos += 1
866 if b >= 128 then ctx.rexw = true end 863 if b & 0x80 then ctx.rexw = true end
867 end 864 end
868 ctx.pos = pos 865 ctx.pos = pos
869 local map 866 local map
@@ -871,24 +868,23 @@ map_act = {
871 elseif m == 2 then map = map_opc3["38"] 868 elseif m == 2 then map = map_opc3["38"]
872 elseif m == 3 then map = map_opc3["3a"] 869 elseif m == 3 then map = map_opc3["3a"]
873 else return unknown(ctx) end 870 else return unknown(ctx) end
874 local p = b%4; b = (b-p)/4 871 local p = b & 3
875 if p == 1 then ctx.o16 = "o16" 872 if p == 1 then ctx.o16 = "o16"
876 elseif p == 2 then ctx.rep = "rep" 873 elseif p == 2 then ctx.rep = "rep"
877 elseif p == 3 then ctx.rep = "repne" end 874 elseif p == 3 then ctx.rep = "repne" end
878 local l = b%2; b = (b-l)/2 875 if b & 4 != 0 then ctx.vexl = true end
879 if l ~= 0 then ctx.vexl = true end 876 ctx.vexv = ~(b >> 3) & 15
880 ctx.vexv = (-1-b)%16
881 return dispatchmap(ctx, map) 877 return dispatchmap(ctx, map)
882 end, 878 end,
883 879
884 -- Special case for nop with REX prefix. 880 -- Special case for nop with REX prefix.
885 nop = function(ctx, name, pat) 881 nop = function(ctx, name, pat)
886 return dispatch(ctx, ctx.rex and pat or "nop") 882 return dispatch(ctx, ctx.rex ? pat : "nop")
887 end, 883 end,
888 884
889 -- Special case for 0F 77. 885 -- Special case for 0F 77.
890 emms = function(ctx, name, pat) 886 emms = function(ctx, name, pat)
891 if ctx.rex ~= "vex" then 887 if ctx.rex != "vex" then
892 return putop(ctx, "emms") 888 return putop(ctx, "emms")
893 elseif ctx.vexl then 889 elseif ctx.vexl then
894 ctx.vexl = false 890 ctx.vexl = false
@@ -904,8 +900,8 @@ map_act = {
904-- Disassemble a block of code. 900-- Disassemble a block of code.
905local function disass_block(ctx, ofs, len) 901local function disass_block(ctx, ofs, len)
906 if not ofs then ofs = 0 end 902 if not ofs then ofs = 0 end
907 local stop = len and ofs+len or #ctx.code 903 local stop = len ? ofs+len : #ctx.code
908 ofs = ofs + 1 904 ofs += 1
909 ctx.start = ofs 905 ctx.start = ofs
910 ctx.pos = ofs 906 ctx.pos = ofs
911 ctx.stop = stop 907 ctx.stop = stop
@@ -913,7 +909,7 @@ local function disass_block(ctx, ofs, len)
913 ctx.mrm = false 909 ctx.mrm = false
914 clearprefixes(ctx) 910 clearprefixes(ctx)
915 while ctx.pos <= stop do dispatchmap(ctx, ctx.map1) end 911 while ctx.pos <= stop do dispatchmap(ctx, ctx.map1) end
916 if ctx.pos ~= ctx.start then incomplete(ctx) end 912 if ctx.pos != ctx.start then incomplete(ctx) end
917end 913end
918 914
919-- Extended API: create a disassembler context. Then call ctx:disass(ofs, len). 915-- 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
62local tracemc, tracesnap = jutil.tracemc, jutil.tracesnap 62local tracemc, tracesnap = jutil.tracemc, jutil.tracesnap
63local traceexitstub, ircalladdr = jutil.traceexitstub, jutil.ircalladdr 63local traceexitstub, ircalladdr = jutil.traceexitstub, jutil.ircalladdr
64local bit = require("bit") 64local bit = require("bit")
65local band, shr, tohex = bit.band, bit.rshift, bit.tohex 65local tohex = bit.tohex
66local sub, gsub, format = string.sub, string.gsub, string.format 66local sub, gsub, format = string.sub, string.gsub, string.format
67local byte, rep = string.byte, string.rep 67local byte, rep = string.byte, string.rep
68local type, tostring = type, tostring 68local type, tostring = type, tostring
@@ -90,7 +90,7 @@ local function fillsymtab_tr(tr, nexit)
90 end 90 end
91 for i=0,nexit-1 do 91 for i=0,nexit-1 do
92 local addr = traceexitstub(tr, i) 92 local addr = traceexitstub(tr, i)
93 if addr < 0 then addr = addr + 2^32 end 93 if addr < 0 then addr += 2^32 end
94 t[addr] = tostring(i) 94 t[addr] = tostring(i)
95 end 95 end
96 local addr = traceexitstub(tr, nexit) 96 local addr = traceexitstub(tr, nexit)
@@ -105,9 +105,9 @@ local function fillsymtab(tr, nexit)
105 local ircall = vmdef.ircall 105 local ircall = vmdef.ircall
106 for i=0,#ircall do 106 for i=0,#ircall do
107 local addr = ircalladdr(i) 107 local addr = ircalladdr(i)
108 if addr ~= 0 then 108 if addr != 0 then
109 if maskaddr then addr = band(addr, maskaddr) end 109 if maskaddr then addr &= maskaddr end
110 if addr < 0 then addr = addr + 2^32 end 110 if addr < 0 then addr += 2^32 end
111 t[addr] = ircall[i] 111 t[addr] = ircall[i]
112 end 112 end
113 end 113 end
@@ -123,7 +123,7 @@ local function fillsymtab(tr, nexit)
123 nexit = 1000000 123 nexit = 1000000
124 break 124 break
125 end 125 end
126 if addr < 0 then addr = addr + 2^32 end 126 if addr < 0 then addr += 2^32 end
127 t[addr] = tostring(i) 127 t[addr] = tostring(i)
128 end 128 end
129 nexitsym = nexit 129 nexitsym = nexit
@@ -142,12 +142,12 @@ local function dump_mcode(tr)
142 local mcode, addr, loop = tracemc(tr) 142 local mcode, addr, loop = tracemc(tr)
143 if not mcode then return end 143 if not mcode then return end
144 if not disass then disass = require("jit.dis_"..jit.arch) end 144 if not disass then disass = require("jit.dis_"..jit.arch) end
145 if addr < 0 then addr = addr + 2^32 end 145 if addr < 0 then addr += 2^32 end
146 out:write("---- TRACE ", tr, " mcode ", #mcode, "\n") 146 out:write("---- TRACE ", tr, " mcode ", #mcode, "\n")
147 local ctx = disass.create(mcode, addr, dumpwrite) 147 local ctx = disass.create(mcode, addr, dumpwrite)
148 ctx.hexdump = 0 148 ctx.hexdump = 0
149 ctx.symtab = fillsymtab(tr, info.nexit) 149 ctx.symtab = fillsymtab(tr, info.nexit)
150 if loop ~= 0 then 150 if loop != 0 then
151 symtab[addr+loop] = "LOOP" 151 symtab[addr+loop] = "LOOP"
152 ctx:disass(0, loop) 152 ctx:disass(0, loop)
153 out:write("->LOOP:\n") 153 out:write("->LOOP:\n")
@@ -233,7 +233,7 @@ local html_escape = { ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;", }
233local function colorize_html(s, t, extra) 233local function colorize_html(s, t, extra)
234 s = gsub(s, "[<>&]", html_escape) 234 s = gsub(s, "[<>&]", html_escape)
235 return format('<span class="irt_%s%s">%s</span>', 235 return format('<span class="irt_%s%s">%s</span>',
236 irtype_text[t], extra and " irt_extra" or "", s) 236 irtype_text[t], extra ? " irt_extra" : "", s)
237end 237end
238 238
239local irtype_html = setmetatable({}, 239local irtype_html = setmetatable({},
@@ -268,25 +268,25 @@ local colorize, irtype
268local litname = { 268local litname = {
269 ["SLOAD "] = setmetatable({}, { __index = function(t, mode) 269 ["SLOAD "] = setmetatable({}, { __index = function(t, mode)
270 local s = "" 270 local s = ""
271 if band(mode, 1) ~= 0 then s = s.."P" end 271 if mode & 1 != 0 then s ..= "P" end
272 if band(mode, 2) ~= 0 then s = s.."F" end 272 if mode & 2 != 0 then s ..= "F" end
273 if band(mode, 4) ~= 0 then s = s.."T" end 273 if mode & 4 != 0 then s ..= "T" end
274 if band(mode, 8) ~= 0 then s = s.."C" end 274 if mode & 8 != 0 then s ..= "C" end
275 if band(mode, 16) ~= 0 then s = s.."R" end 275 if mode & 16 != 0 then s ..= "R" end
276 if band(mode, 32) ~= 0 then s = s.."I" end 276 if mode & 32 != 0 then s ..= "I" end
277 if band(mode, 64) ~= 0 then s = s.."K" end 277 if mode & 64 != 0 then s ..= "K" end
278 t[mode] = s 278 t[mode] = s
279 return s 279 return s
280 end}), 280 end}),
281 ["XLOAD "] = { [0] = "", "R", "V", "RV", "U", "RU", "VU", "RVU", }, 281 ["XLOAD "] = { [0] = "", "R", "V", "RV", "U", "RU", "VU", "RVU", },
282 ["CONV "] = setmetatable({}, { __index = function(t, mode) 282 ["CONV "] = setmetatable({}, { __index = function(t, mode)
283 local s = irtype[band(mode, 31)] 283 local s = irtype[mode & 31]
284 s = irtype[band(shr(mode, 5), 31)].."."..s 284 s = irtype[(mode >> 5) & 31].."."..s
285 if band(mode, 0x800) ~= 0 then s = s.." sext" end 285 if mode & 0x800 != 0 then s ..= " sext" end
286 local c = shr(mode, 12) 286 local c = mode >> 12
287 if c == 1 then s = s.." none" 287 if c == 1 then s ..= " none"
288 elseif c == 2 then s = s.." index" 288 elseif c == 2 then s ..= " index"
289 elseif c == 3 then s = s.." check" end 289 elseif c == 3 then s ..= " check" end
290 t[mode] = s 290 t[mode] = s
291 return s 291 return s
292 end}), 292 end}),
@@ -325,16 +325,16 @@ local function formatk(tr, idx, sn)
325 local s 325 local s
326 if tn == "number" then 326 if tn == "number" then
327 if t < 12 then 327 if t < 12 then
328 s = k == 0 and "NULL" or format("[0x%08x]", k) 328 s = k == 0 ? "NULL" : format("[0x%08x]", k)
329 elseif band(sn or 0, 0x30000) ~= 0 then 329 elseif (sn or 0) & 0x30000 != 0 then
330 s = band(sn, 0x20000) ~= 0 and "contpc" or "ftsz" 330 s = sn & 0x20000 != 0 ? "contpc" : "ftsz"
331 elseif k == 2^52+2^51 then 331 elseif k == 2^52+2^51 then
332 s = "bias" 332 s = "bias"
333 else 333 else
334 s = format(0 < k and k < 0x1p-1026 and "%+a" or "%+.14g", k) 334 s = format(0 < k and k < 0x1p-1026 ? "%+a" : "%+.14g", k)
335 end 335 end
336 elseif tn == "string" then 336 elseif tn == "string" then
337 s = format(#k > 20 and '"%.20s"~' or '"%s"', gsub(k, "%c", ctlsub)) 337 s = format(#k > 20 ? '"%.20s"~' : '"%s"', gsub(k, "%c", ctlsub))
338 elseif tn == "function" then 338 elseif tn == "function" then
339 s = fmtfunc(k) 339 s = fmtfunc(k)
340 elseif tn == "table" then 340 elseif tn == "table" then
@@ -348,13 +348,13 @@ local function formatk(tr, idx, sn)
348 end 348 end
349 elseif t == 21 then -- int64_t 349 elseif t == 21 then -- int64_t
350 s = sub(tostring(k), 1, -3) 350 s = sub(tostring(k), 1, -3)
351 if sub(s, 1, 1) ~= "-" then s = "+"..s end 351 if sub(s, 1, 1) != "-" then s = "+"..s end
352 elseif sn == 0x1057fff then -- SNAP(1, SNAP_FRAME | SNAP_NORESTORE, REF_NIL) 352 elseif sn == 0x1057fff then -- SNAP(1, SNAP_FRAME | SNAP_NORESTORE, REF_NIL)
353 return "----" -- Special case for LJ_FR2 slot 1. 353 return "----" -- Special case for LJ_FR2 slot 1.
354 else 354 else
355 s = tostring(k) -- For primitives. 355 s = tostring(k) -- For primitives.
356 end 356 end
357 s = colorize(format("%-4s", s), t, band(sn or 0, 0x100000) ~= 0) 357 s = colorize(format("%-4s", s), t, (sn or 0) & 0x100000 != 0)
358 if slot then 358 if slot then
359 s = format("%s @%d", s, slot) 359 s = format("%s @%d", s, slot)
360 end 360 end
@@ -365,18 +365,18 @@ local function printsnap(tr, snap)
365 local n = 2 365 local n = 2
366 for s=0,snap[1]-1 do 366 for s=0,snap[1]-1 do
367 local sn = snap[n] 367 local sn = snap[n]
368 if shr(sn, 24) == s then 368 if sn >> 24 == s then
369 n = n + 1 369 n += 1
370 local ref = band(sn, 0xffff) - 0x8000 -- REF_BIAS 370 local ref = (sn & 0xffff) - 0x8000 -- REF_BIAS
371 if ref < 0 then 371 if ref < 0 then
372 out:write(formatk(tr, ref, sn)) 372 out:write(formatk(tr, ref, sn))
373 elseif band(sn, 0x80000) ~= 0 then -- SNAP_SOFTFPNUM 373 elseif sn & 0x80000 != 0 then -- SNAP_SOFTFPNUM
374 out:write(colorize(format("%04d/%04d", ref, ref+1), 14)) 374 out:write(colorize(format("%04d/%04d", ref, ref+1), 14))
375 else 375 else
376 local m, ot, op1, op2 = traceir(tr, ref) 376 local m, ot, op1, op2 = traceir(tr, ref)
377 out:write(colorize(format("%04d", ref), band(ot, 31), band(sn, 0x100000) ~= 0)) 377 out:write(colorize(format("%04d", ref), ot & 31, sn & 0x100000 != 0))
378 end 378 end
379 out:write(band(sn, 0x10000) == 0 and " " or "|") -- SNAP_FRAME 379 out:write(sn & 0x10000 == 0 ? " " : "|") -- SNAP_FRAME
380 else 380 else
381 out:write("---- ") 381 out:write("---- ")
382 end 382 end
@@ -398,9 +398,9 @@ end
398-- Return a register name or stack slot for a rid/sp location. 398-- Return a register name or stack slot for a rid/sp location.
399local function ridsp_name(ridsp, ins) 399local function ridsp_name(ridsp, ins)
400 if not disass then disass = require("jit.dis_"..jit.arch) end 400 if not disass then disass = require("jit.dis_"..jit.arch) end
401 local rid, slot = band(ridsp, 0xff), shr(ridsp, 8) 401 local rid, slot = ridsp & 0xff, ridsp >> 8
402 if rid == 253 or rid == 254 then 402 if rid == 253 or rid == 254 then
403 return (slot == 0 or slot == 255) and " {sink" or format(" {%04d", ins-slot) 403 return (slot == 0 or slot == 255) ? " {sink" : format(" {%04d", ins-slot)
404 end 404 end
405 if ridsp > 255 then return format("[%x]", slot*4) end 405 if ridsp > 255 then return format("[%x]", slot*4) end
406 if rid < 128 then return disass.regname(rid) end 406 if rid < 128 then return disass.regname(rid) end
@@ -412,7 +412,7 @@ local function dumpcallfunc(tr, ins)
412 local ctype 412 local ctype
413 if ins > 0 then 413 if ins > 0 then
414 local m, ot, op1, op2 = traceir(tr, ins) 414 local m, ot, op1, op2 = traceir(tr, ins)
415 if band(ot, 31) == 0 then -- nil type means CARG(func, ctype). 415 if ot & 31 == 0 then -- nil type means CARG(func, ctype).
416 ins = op1 416 ins = op1
417 ctype = formatk(tr, op2) 417 ctype = formatk(tr, op2)
418 end 418 end
@@ -431,7 +431,7 @@ local function dumpcallargs(tr, ins)
431 out:write(formatk(tr, ins)) 431 out:write(formatk(tr, ins))
432 else 432 else
433 local m, ot, op1, op2 = traceir(tr, ins) 433 local m, ot, op1, op2 = traceir(tr, ins)
434 local oidx = 6*shr(ot, 8) 434 local oidx = 6 * (ot >> 8)
435 local op = sub(vmdef.irnames, oidx+1, oidx+6) 435 local op = sub(vmdef.irnames, oidx+1, oidx+6)
436 if op == "CARG " then 436 if op == "CARG " then
437 dumpcallargs(tr, op1) 437 dumpcallargs(tr, op1)
@@ -468,12 +468,12 @@ local function dump_ir(tr, dumpsnap, dumpreg)
468 out:write(format(".... SNAP #%-3d [ ", snapno)) 468 out:write(format(".... SNAP #%-3d [ ", snapno))
469 end 469 end
470 printsnap(tr, snap) 470 printsnap(tr, snap)
471 snapno = snapno + 1 471 snapno += 1
472 snap = tracesnap(tr, snapno) 472 snap = tracesnap(tr, snapno)
473 snapref = snap and snap[0] or 65536 473 snapref = snap ? snap[0] : 65536
474 end 474 end
475 local m, ot, op1, op2, ridsp = traceir(tr, ins) 475 local m, ot, op1, op2, ridsp = traceir(tr, ins)
476 local oidx, t = 6*shr(ot, 8), band(ot, 31) 476 local oidx, t = 6 * (ot >> 8), ot & 31
477 local op = sub(irnames, oidx+1, oidx+6) 477 local op = sub(irnames, oidx+1, oidx+6)
478 if op == "LOOP " then 478 if op == "LOOP " then
479 if dumpreg then 479 if dumpreg then
@@ -481,45 +481,45 @@ local function dump_ir(tr, dumpsnap, dumpreg)
481 else 481 else
482 out:write(format("%04d ------ LOOP ------------\n", ins)) 482 out:write(format("%04d ------ LOOP ------------\n", ins))
483 end 483 end
484 elseif op ~= "NOP " and op ~= "CARG " and 484 elseif op != "NOP " and op != "CARG " and
485 (dumpreg or op ~= "RENAME") then 485 (dumpreg or op != "RENAME") then
486 local rid = band(ridsp, 255) 486 local rid = ridsp & 255
487 if dumpreg then 487 if dumpreg then
488 out:write(format("%04d %-6s", ins, ridsp_name(ridsp, ins))) 488 out:write(format("%04d %-6s", ins, ridsp_name(ridsp, ins)))
489 else 489 else
490 out:write(format("%04d ", ins)) 490 out:write(format("%04d ", ins))
491 end 491 end
492 out:write(format("%s%s %s %s ", 492 out:write(format("%s%s %s %s ",
493 (rid == 254 or rid == 253) and "}" or 493 rid == 254 or rid == 253 ? "}" :
494 (band(ot, 128) == 0 and " " or ">"), 494 ot & 128 == 0 ? " " : ">",
495 band(ot, 64) == 0 and " " or "+", 495 ot & 64 == 0 ? " " : "+",
496 irtype[t], op)) 496 irtype[t], op))
497 local m1, m2 = band(m, 3), band(m, 3*4) 497 local m1, m2 = m & 3, m & (3 << 2)
498 if sub(op, 1, 4) == "CALL" then 498 if sub(op, 1, 4) == "CALL" then
499 local ctype 499 local ctype
500 if m2 == 1*4 then -- op2 == IRMlit 500 if m2 == 1 << 2 then -- op2 == IRMlit
501 out:write(format("%-10s (", vmdef.ircall[op2])) 501 out:write(format("%-10s (", vmdef.ircall[op2]))
502 else 502 else
503 ctype = dumpcallfunc(tr, op2) 503 ctype = dumpcallfunc(tr, op2)
504 end 504 end
505 if op1 ~= -1 then dumpcallargs(tr, op1) end 505 if op1 != -1 then dumpcallargs(tr, op1) end
506 out:write(")") 506 out:write(")")
507 if ctype then out:write(" ctype ", ctype) end 507 if ctype then out:write(" ctype ", ctype) end
508 elseif op == "CNEW " and op2 == -1 then 508 elseif op == "CNEW " and op2 == -1 then
509 out:write(formatk(tr, op1)) 509 out:write(formatk(tr, op1))
510 elseif m1 ~= 3 then -- op1 != IRMnone 510 elseif m1 != 3 then -- op1 != IRMnone
511 if op1 < 0 then 511 if op1 < 0 then
512 out:write(formatk(tr, op1)) 512 out:write(formatk(tr, op1))
513 else 513 else
514 out:write(format(m1 == 0 and "%04d" or "#%-3d", op1)) 514 out:write(format(m1 == 0 ? "%04d" : "#%-3d", op1))
515 end 515 end
516 if m2 ~= 3*4 then -- op2 != IRMnone 516 if m2 != 3 << 2 then -- op2 != IRMnone
517 if m2 == 1*4 then -- op2 == IRMlit 517 if m2 == 1 << 2 then -- op2 == IRMlit
518 local litn = litname[op] 518 local litn = litname[op]
519 if litn and litn[op2] then 519 if litn and litn[op2] then
520 out:write(" ", litn[op2]) 520 out:write(" ", litn[op2])
521 elseif op == "UREFO " or op == "UREFC " then 521 elseif op == "UREFO " or op == "UREFC " then
522 out:write(format(" #%-3d", shr(op2, 8))) 522 out:write(format(" #%-3d", op2 >> 8))
523 else 523 else
524 out:write(format(" #%-3d", op2)) 524 out:write(format(" #%-3d", op2))
525 end 525 end
@@ -572,7 +572,7 @@ local function dump_trace(what, tr, func, pc, otr, oex)
572 if what == "start" then 572 if what == "start" then
573 if dumpmode.H then out:write('<pre class="ljdump">\n') end 573 if dumpmode.H then out:write('<pre class="ljdump">\n') end
574 out:write("---- TRACE ", tr, " ", what) 574 out:write("---- TRACE ", tr, " ", what)
575 if otr then out:write(" ", otr, "/", oex == -1 and "stitch" or oex) end 575 if otr then out:write(" ", otr, "/", oex == -1 ? "stitch" : oex) end
576 out:write(" ", fmtfunc(func, pc), "\n") 576 out:write(" ", fmtfunc(func, pc), "\n")
577 elseif what == "stop" or what == "abort" then 577 elseif what == "stop" or what == "abort" then
578 out:write("---- TRACE ", tr, " ", what) 578 out:write("---- TRACE ", tr, " ", what)
@@ -599,7 +599,7 @@ end
599 599
600-- Dump recorded bytecode. 600-- Dump recorded bytecode.
601local function dump_record(tr, func, pc, depth) 601local function dump_record(tr, func, pc, depth)
602 if depth ~= recdepth then 602 if depth != recdepth then
603 recdepth = depth 603 recdepth = depth
604 recprefix = rep(" .", depth) 604 recprefix = rep(" .", depth)
605 end 605 end
@@ -615,7 +615,7 @@ local function dump_record(tr, func, pc, depth)
615 else 615 else
616 out:write(line) 616 out:write(line)
617 end 617 end
618 if pc >= 0 and band(funcbc(func, pc), 0xff) < 16 then -- ORDER BC 618 if pc >= 0 and funcbc(func, pc) & 0xff < 16 then -- ORDER BC
619 out:write(bcline(func, pc+1, recprefix)) -- Write JMP for cond. 619 out:write(bcline(func, pc+1, recprefix)) -- Write JMP for cond.
620 end 620 end
621end 621end
@@ -664,7 +664,7 @@ local function dumpoff()
664 jit.attach(dump_texit) 664 jit.attach(dump_texit)
665 jit.attach(dump_record) 665 jit.attach(dump_record)
666 jit.attach(dump_trace) 666 jit.attach(dump_trace)
667 if out and out ~= stdout and out ~= stderr then out:close() end 667 if out and out != stdout and out != stderr then out:close() end
668 out = nil 668 out = nil
669 end 669 end
670end 670end
@@ -674,16 +674,16 @@ local function dumpon(opt, outfile)
674 if active then dumpoff() end 674 if active then dumpoff() end
675 675
676 local term = os.getenv("TERM") 676 local term = os.getenv("TERM")
677 local colormode = (term and term:match("color") or os.getenv("COLORTERM")) and "A" or "T" 677 local colormode = (term ? (term:match("color")) : os.getenv("COLORTERM")) ? "A" : "T"
678 if opt then 678 if opt then
679 opt = gsub(opt, "[TAH]", function(mode) colormode = mode; return ""; end) 679 opt = gsub(opt, "[TAH]", function(mode) colormode = mode; return ""; end)
680 end 680 end
681 681
682 local m = { t=true, b=true, i=true, m=true, } 682 local m = { t=true, b=true, i=true, m=true, }
683 if opt and opt ~= "" then 683 if opt and opt != "" then
684 local o = sub(opt, 1, 1) 684 local o = sub(opt, 1, 1)
685 if o ~= "+" and o ~= "-" then m = {} end 685 if o != "+" and o != "-" then m = {} end
686 for i=1,#opt do m[sub(opt, i, i)] = (o ~= "-") end 686 for i=1,#opt do m[sub(opt, i, i)] = (o != "-") end
687 end 687 end
688 dumpmode = m 688 dumpmode = m
689 689
@@ -700,7 +700,7 @@ local function dumpon(opt, outfile)
700 700
701 if not outfile then outfile = os.getenv("LUAJIT_DUMPFILE") end 701 if not outfile then outfile = os.getenv("LUAJIT_DUMPFILE") end
702 if outfile then 702 if outfile then
703 out = outfile == "-" and stdout or assert(io.open(outfile, "w")) 703 out = outfile == "-" ? stdout : assert(io.open(outfile, "w"))
704 else 704 else
705 out = stdout 705 out = stdout
706 end 706 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
120local function prof_top(count1, count2, samples, indent) 120local function prof_top(count1, count2, samples, indent)
121 local t, n = {}, 0 121 local t, n = {}, 0
122 for k in pairs(count1) do 122 for k in pairs(count1) do
123 n = n + 1 123 n += 1
124 t[n] = k 124 t[n] = k
125 end 125 end
126 sort(t, function(a, b) return count1[a] > count1[b] end) 126 sort(t, function(a, b) return count1[a] > count1[b] end)
@@ -184,7 +184,7 @@ local function prof_annotate(count1, samples)
184 out:write(format("\n====== %s ======\n", file)) 184 out:write(format("\n====== %s ======\n", file))
185 local fl = files[file] 185 local fl = files[file]
186 local n, show = 1, false 186 local n, show = 1, false
187 if ann ~= 0 then 187 if ann != 0 then
188 for i=1,ann do 188 for i=1,ann do
189 if fl[i] then show = true; out:write("@@ 1 @@\n"); break end 189 if fl[i] then show = true; out:write("@@ 1 @@\n"); break end
190 end 190 end
@@ -195,7 +195,7 @@ local function prof_annotate(count1, samples)
195 break 195 break
196 end 196 end
197 local v = fl[n] 197 local v = fl[n]
198 if ann ~= 0 then 198 if ann != 0 then
199 local v2 = fl[n+ann] 199 local v2 = fl[n+ann]
200 if show then 200 if show then
201 if v2 then show = n+ann elseif v then show = n 201 if v2 then show = n+ann elseif v then show = n
@@ -212,7 +212,7 @@ local function prof_annotate(count1, samples)
212 out:write(format(fmtn, line)) 212 out:write(format(fmtn, line))
213 end 213 end
214 ::next:: 214 ::next::
215 n = n + 1 215 n += 1
216 end 216 end
217 fp:close() 217 fp:close()
218 end 218 end
@@ -226,7 +226,7 @@ local function prof_finish()
226 profile.stop() 226 profile.stop()
227 local samples = prof_samples 227 local samples = prof_samples
228 if samples == 0 then 228 if samples == 0 then
229 if prof_raw ~= true then out:write("[No samples collected]\n") end 229 if prof_raw != true then out:write("[No samples collected]\n") end
230 elseif prof_ann then 230 elseif prof_ann then
231 prof_annotate(prof_count1, samples) 231 prof_annotate(prof_count1, samples)
232 else 232 else
@@ -235,7 +235,7 @@ local function prof_finish()
235 prof_count1 = nil 235 prof_count1 = nil
236 prof_count2 = nil 236 prof_count2 = nil
237 prof_ud = nil 237 prof_ud = nil
238 if out ~= stdout then out:close() end 238 if out != stdout then out:close() end
239 end 239 end
240end 240end
241 241
@@ -270,7 +270,7 @@ local function prof_start(mode)
270 prof_fmt = "pl" 270 prof_fmt = "pl"
271 prof_split = 0 271 prof_split = 0
272 prof_depth = 1 272 prof_depth = 1
273 elseif m.G and scope ~= "" then 273 elseif m.G and scope != "" then
274 prof_fmt = flags..scope.."Z;" 274 prof_fmt = flags..scope.."Z;"
275 prof_depth = -100 275 prof_depth = -100
276 prof_raw = true 276 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)
107 else 107 else
108 if what == "abort" then 108 if what == "abort" then
109 local loc = fmtfunc(func, pc) 109 local loc = fmtfunc(func, pc)
110 if loc ~= startloc then 110 if loc != startloc then
111 out:write(format("[TRACE --- %s%s -- %s at %s]\n", 111 out:write(format("[TRACE --- %s%s -- %s at %s]\n",
112 startex, startloc, fmterr(otr, oex), loc)) 112 startex, startloc, fmterr(otr, oex), loc))
113 else 113 else
@@ -147,7 +147,7 @@ local function dumpoff()
147 if active then 147 if active then
148 active = false 148 active = false
149 jit.attach(dump_trace) 149 jit.attach(dump_trace)
150 if out and out ~= stdout and out ~= stderr then out:close() end 150 if out and out != stdout and out != stderr then out:close() end
151 out = nil 151 out = nil
152 end 152 end
153end 153end