| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
local jit = require("jit") |
| 44 |
local jutil = require("jit.util") |
| 45 |
local vmdef = require("jit.vmdef") |
| 46 |
local bit = require("bit") |
| 47 |
local sub, gsub, format = string.sub, string.gsub, string.format |
| 48 |
local byte, band, shr = string.byte, bit.band, bit.rshift |
| 49 |
local funcinfo, funcbc, funck = jutil.funcinfo, jutil.funcbc, jutil.funck |
| 50 |
local funcuvname = jutil.funcuvname |
| 51 |
local bcnames = vmdef.bcnames |
| 52 |
local stdout, stderr = io.stdout, io.stderr |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
local function ctlsub(c) |
| 57 |
if c == "\n" then return "\\n" |
| 58 |
elseif c == "\r" then return "\\r" |
| 59 |
elseif c == "\t" then return "\\t" |
| 60 |
else return format("\\%03d", byte(c)) |
| 61 |
end |
| 62 |
end |
| 63 |
|
| 64 |
|
| 65 |
local function bcline(func, pc, prefix) |
| 66 |
local ins, m = funcbc(func, pc) |
| 67 |
if not ins then return end |
| 68 |
local ma, mb, mc = band(m, 7), band(m, 15*8), band(m, 15*128) |
| 69 |
local a = band(shr(ins, 8), 0xff) |
| 70 |
local oidx = 6*band(ins, 0xff) |
| 71 |
local op = sub(bcnames, oidx+1, oidx+6) |
| 72 |
local s = format("%04d %s %-6s %3s ", |
| 73 |
pc, prefix or " ", op, ma == 0 and "" or a) |
| 74 |
local d = shr(ins, 16) |
| 75 |
if mc == 13*128 then |
| 76 |
return format("%s=> %04d\n", s, pc+d-0x7fff) |
| 77 |
end |
| 78 |
if mb ~= 0 then |
| 79 |
d = band(d, 0xff) |
| 80 |
elseif mc == 0 then |
| 81 |
return s.."\n" |
| 82 |
end |
| 83 |
local kc |
| 84 |
if mc == 10*128 then |
| 85 |
kc = funck(func, -d-1) |
| 86 |
kc = format(#kc > 40 and '"%.40s"~' or '"%s"', gsub(kc, "%c", ctlsub)) |
| 87 |
elseif mc == 9*128 then |
| 88 |
kc = funck(func, d) |
| 89 |
if op == "TSETM " then kc = kc - 2^52 end |
| 90 |
elseif mc == 12*128 then |
| 91 |
local fi = funcinfo(funck(func, -d-1)) |
| 92 |
if fi.ffid then |
| 93 |
kc = vmdef.ffnames[fi.ffid] |
| 94 |
else |
| 95 |
kc = fi.loc |
| 96 |
end |
| 97 |
elseif mc == 5*128 then |
| 98 |
kc = funcuvname(func, d) |
| 99 |
end |
| 100 |
if ma == 5 then |
| 101 |
local ka = funcuvname(func, a) |
| 102 |
if kc then kc = ka.." ; "..kc else kc = ka end |
| 103 |
end |
| 104 |
if mb ~= 0 then |
| 105 |
local b = shr(ins, 24) |
| 106 |
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) |
| 108 |
end |
| 109 |
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 |
| 111 |
return format("%s%3d\n", s, d) |
| 112 |
end |
| 113 |
|
| 114 |
|
| 115 |
local function bctargets(func) |
| 116 |
local target = {} |
| 117 |
for pc=1,1000000000 do |
| 118 |
local ins, m = funcbc(func, pc) |
| 119 |
if not ins then break end |
| 120 |
if band(m, 15*128) == 13*128 then target[pc+shr(ins, 16)-0x7fff] = true end |
| 121 |
end |
| 122 |
return target |
| 123 |
end |
| 124 |
|
| 125 |
|
| 126 |
local function bcdump(func, out, all) |
| 127 |
if not out then out = stdout end |
| 128 |
local fi = funcinfo(func) |
| 129 |
if all and fi.children then |
| 130 |
for n=-1,-1000000000,-1 do |
| 131 |
local k = funck(func, n) |
| 132 |
if not k then break end |
| 133 |
if type(k) == "proto" then bcdump(k, out, true) end |
| 134 |
end |
| 135 |
end |
| 136 |
out:write(format("-- BYTECODE -- %s-%d\n", fi.loc, fi.lastlinedefined)) |
| 137 |
local target = bctargets(func) |
| 138 |
for pc=1,1000000000 do |
| 139 |
local s = bcline(func, pc, target[pc] and "=>") |
| 140 |
if not s then break end |
| 141 |
out:write(s) |
| 142 |
end |
| 143 |
out:write("\n") |
| 144 |
out:flush() |
| 145 |
end |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
local active, out |
| 151 |
|
| 152 |
|
| 153 |
local function h_list(func) |
| 154 |
return bcdump(func, out) |
| 155 |
end |
| 156 |
|
| 157 |
|
| 158 |
local function bclistoff() |
| 159 |
if active then |
| 160 |
active = false |
| 161 |
jit.attach(h_list) |
| 162 |
if out and out ~= stdout and out ~= stderr then out:close() end |
| 163 |
out = nil |
| 164 |
end |
| 165 |
end |
| 166 |
|
| 167 |
|
| 168 |
local function bcliston(outfile) |
| 169 |
if active then bclistoff() end |
| 170 |
if not outfile then outfile = os.getenv("LUAJIT_LISTFILE") end |
| 171 |
if outfile then |
| 172 |
out = outfile == "-" and stdout or assert(io.open(outfile, "w")) |
| 173 |
else |
| 174 |
out = stderr |
| 175 |
end |
| 176 |
jit.attach(h_list, "bc") |
| 177 |
active = true |
| 178 |
end |
| 179 |
|
| 180 |
|
| 181 |
return { |
| 182 |
line = bcline, |
| 183 |
dump = bcdump, |
| 184 |
targets = bctargets, |
| 185 |
on = bcliston, |
| 186 |
off = bclistoff, |
| 187 |
start = bcliston |
| 188 |
} |
| 189 |
|