bc.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. ----------------------------------------------------------------------------
  2. -- LuaJIT bytecode listing module.
  3. --
  4. -- Copyright (C) 2005-2011 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 op = sub(bcnames, oidx+1, oidx+6)
  70. local s = format("%04d %s %-6s %3s ",
  71. pc, prefix or " ", op, ma == 0 and "" or a)
  72. local d = shr(ins, 16)
  73. if mc == 13*128 then -- BCMjump
  74. return format("%s=> %04d\n", s, pc+d-0x7fff)
  75. end
  76. if mb ~= 0 then
  77. d = band(d, 0xff)
  78. elseif mc == 0 then
  79. return s.."\n"
  80. end
  81. local kc
  82. if mc == 10*128 then -- BCMstr
  83. kc = funck(func, -d-1)
  84. kc = format(#kc > 40 and '"%.40s"~' or '"%s"', gsub(kc, "%c", ctlsub))
  85. elseif mc == 9*128 then -- BCMnum
  86. kc = funck(func, d)
  87. if op == "TSETM " then kc = kc - 2^52 end
  88. elseif mc == 12*128 then -- BCMfunc
  89. local fi = funcinfo(funck(func, -d-1))
  90. if fi.ffid then
  91. kc = vmdef.ffnames[fi.ffid]
  92. else
  93. kc = fi.loc
  94. end
  95. elseif mc == 5*128 then -- BCMuv
  96. kc = funcuvname(func, d)
  97. end
  98. if ma == 5 then -- BCMuv
  99. local ka = funcuvname(func, a)
  100. if kc then kc = ka.." ; "..kc else kc = ka end
  101. end
  102. if mb ~= 0 then
  103. local b = shr(ins, 24)
  104. if kc then return format("%s%3d %3d ; %s\n", s, b, d, kc) end
  105. return format("%s%3d %3d\n", s, b, d)
  106. end
  107. if kc then return format("%s%3d ; %s\n", s, d, kc) end
  108. if mc == 7*128 and d > 32767 then d = d - 65536 end -- BCMlits
  109. return format("%s%3d\n", s, d)
  110. end
  111. -- Collect branch targets of a function.
  112. local function bctargets(func)
  113. local target = {}
  114. for pc=1,1000000000 do
  115. local ins, m = funcbc(func, pc)
  116. if not ins then break end
  117. if band(m, 15*128) == 13*128 then target[pc+shr(ins, 16)-0x7fff] = true end
  118. end
  119. return target
  120. end
  121. -- Dump bytecode instructions of a function.
  122. local function bcdump(func, out)
  123. if not out then out = stdout end
  124. local fi = funcinfo(func)
  125. out:write(format("-- BYTECODE -- %s-%d\n", fi.loc, fi.lastlinedefined))
  126. local target = bctargets(func)
  127. for pc=1,1000000000 do
  128. local s = bcline(func, pc, target[pc] and "=>")
  129. if not s then break end
  130. out:write(s)
  131. end
  132. out:write("\n")
  133. out:flush()
  134. end
  135. ------------------------------------------------------------------------------
  136. -- Active flag and output file handle.
  137. local active, out
  138. -- List handler.
  139. local function h_list(func)
  140. return bcdump(func, out)
  141. end
  142. -- Detach list handler.
  143. local function bclistoff()
  144. if active then
  145. active = false
  146. jit.attach(h_list)
  147. if out and out ~= stdout and out ~= stderr then out:close() end
  148. out = nil
  149. end
  150. end
  151. -- Open the output file and attach list handler.
  152. local function bcliston(outfile)
  153. if active then bclistoff() end
  154. if not outfile then outfile = os.getenv("LUAJIT_LISTFILE") end
  155. if outfile then
  156. out = outfile == "-" and stdout or assert(io.open(outfile, "w"))
  157. else
  158. out = stderr
  159. end
  160. jit.attach(h_list, "bc")
  161. active = true
  162. end
  163. -- Public module functions.
  164. module(...)
  165. line = bcline
  166. dump = bcdump
  167. targets = bctargets
  168. on = bcliston
  169. off = bclistoff
  170. start = bcliston -- For -j command line option.