~ubuntu-branches/ubuntu/trusty/luajit/trusty

« back to all changes in this revision

Viewing changes to lib/bc.lua

  • Committer: Package Import Robot
  • Author(s): Enrico Tassi
  • Date: 2012-11-03 14:07:56 UTC
  • mfrom: (1.2.1) (15.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20121103140756-z0zcnyrwqlvuc2m5
Tags: 2.0.0+dfsg-1
New upstream release

Show diffs side-by-side

added added

removed removed

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