bc.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. ----------------------------------------------------------------------------
  2. -- LuaJIT bytecode listing module.
  3. --
  4. -- Copyright (C) 2005-2010 Mike Pall. All rights reserved.
  5. -- Released under the MIT/X license. See Copyright Notice in luajit.h
  6. ----------------------------------------------------------------------------
  7. --
  8. -- This module lists the bytecode of a Lua function. If it's loaded by -jbc
  9. -- it hooks into the parser and lists all functions of a chunk as they
  10. -- are parsed.
  11. --
  12. -- Example usage:
  13. --
  14. -- luajit -jbc -e 'local x=0; for i=1,1e6 do x=x+i end; print(x)'
  15. -- luajit -jbc=- foo.lua
  16. -- luajit -jbc=foo.list foo.lua
  17. --
  18. -- Default output is to stderr. To redirect the output to a file, pass a
  19. -- filename as an argument (use '-' for stdout) or set the environment
  20. -- variable LUAJIT_LISTFILE. The file is overwritten every time the module
  21. -- is started.
  22. --
  23. -- This module can also be used programmatically:
  24. --
  25. -- local bc = require("jit.bc")
  26. --
  27. -- local function foo() print("hello") end
  28. --
  29. -- bc.dump(foo) --> -- BYTECODE -- [...]
  30. -- print(bc.line(foo, 2)) --> 0002 KSTR 1 1 ; "hello"
  31. --
  32. -- local out = {
  33. -- -- Do something with each line:
  34. -- write = function(t, ...) io.write(...) end,
  35. -- close = function(t) end,
  36. -- flush = function(t) end,
  37. -- }
  38. -- bc.dump(foo, out)
  39. --
  40. ------------------------------------------------------------------------------
  41. -- Cache some library functions and objects.
  42. local jit = require("jit")
  43. assert(jit.version_num == 20000, "LuaJIT core/library version mismatch")
  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. local function ctlsub(c)
  55. if c == "\n" then return "\\n"
  56. elseif c == "\r" then return "\\r"
  57. elseif c == "\t" then return "\\t"
  58. elseif c == "\r" then return "\\r"
  59. else return format("\\%03d", byte(c))
  60. end
  61. end
  62. -- Return one bytecode line.
  63. local function bcline(func, pc, prefix)
  64. local ins, m = funcbc(func, pc)
  65. if not ins then return end
  66. local ma, mb, mc = band(m, 7), band(m, 15*8), band(m, 15*128)
  67. local a = band(shr(ins, 8), 0xff)
  68. local oidx = 6*band(ins, 0xff)
  69. local s = format("%04d %s %-6s %3s ",
  70. pc, prefix or " ", sub(bcnames, oidx+1, oidx+6), ma == 0 and "" or a)
  71. local d = shr(ins, 16)
  72. if mc == 13*128 then -- BCMjump
  73. return format("%s=> %04d\n", s, pc+d-0x7fff)
  74. end
  75. if mb ~= 0 then
  76. d = band(d, 0xff)
  77. elseif mc == 0 then
  78. return s.."\n"
  79. end
  80. local kc
  81. if mc == 10*128 then -- BCMstr
  82. kc = funck(func, -d-1)
  83. kc = format(#kc > 40 and '"%.40s"~' or '"%s"', gsub(kc, "%c", ctlsub))
  84. elseif mc == 9*128 then -- BCMnum
  85. kc = funck(func, d)
  86. elseif mc == 12*128 then -- BCMfunc
  87. local fi = funcinfo(funck(func, -d-1))
  88. if fi.ffid then
  89. kc = vmdef.ffnames[fi.ffid]
  90. else
  91. kc = fi.loc
  92. end
  93. elseif mc == 5*128 then -- BCMuv
  94. kc = funcuvname(func, d)
  95. end
  96. if ma == 5 then -- BCMuv
  97. local ka = funcuvname(func, a)
  98. if kc then kc = ka.." ; "..kc else kc = ka end
  99. end
  100. if mb ~= 0 then
  101. local b = shr(ins, 24)
  102. if kc then return format("%s%3d %3d ; %s\n", s, b, d, kc) end
  103. return format("%s%3d %3d\n", s, b, d)
  104. end
  105. if kc then return format("%s%3d ; %s\n", s, d, kc) end
  106. if mc == 7*128 and d > 32767 then d = d - 65536 end -- BCMlits
  107. return format("%s%3d\n", s, d)
  108. end
  109. -- Collect branch targets of a function.
  110. local function bctargets(func)
  111. local target = {}
  112. for pc=1,1000000000 do
  113. local ins, m = funcbc(func, pc)
  114. if not ins then break end
  115. if band(m, 15*128) == 13*128 then target[pc+shr(ins, 16)-0x7fff] = true end
  116. end
  117. return target
  118. end
  119. -- Dump bytecode instructions of a function.
  120. local function bcdump(func, out)
  121. if not out then out = stdout end
  122. local fi = funcinfo(func)
  123. out:write(format("-- BYTECODE -- %s-%d\n", fi.loc, fi.lastlinedefined))
  124. local target = bctargets(func)
  125. for pc=1,1000000000 do
  126. local s = bcline(func, pc, target[pc] and "=>")
  127. if not s then break end
  128. out:write(s)
  129. end
  130. out:write("\n")
  131. out:flush()
  132. end
  133. ------------------------------------------------------------------------------
  134. -- Active flag and output file handle.
  135. local active, out
  136. -- List handler.
  137. local function h_list(func)
  138. return bcdump(func, out)
  139. end
  140. -- Detach list handler.
  141. local function bclistoff()
  142. if active then
  143. active = false
  144. jit.attach(h_list)
  145. if out and out ~= stdout and out ~= stderr then out:close() end
  146. out = nil
  147. end
  148. end
  149. -- Open the output file and attach list handler.
  150. local function bcliston(outfile)
  151. if active then bclistoff() end
  152. if not outfile then outfile = os.getenv("LUAJIT_LISTFILE") end
  153. if outfile then
  154. out = outfile == "-" and stdout or assert(io.open(outfile, "w"))
  155. else
  156. out = stderr
  157. end
  158. jit.attach(h_list, "bc")
  159. active = true
  160. end
  161. -- Public module functions.
  162. module(...)
  163. line = bcline
  164. dump = bcdump
  165. targets = bctargets
  166. on = bcliston
  167. off = bclistoff
  168. start = bcliston -- For -j command line option.