bc.lua 5.3 KB

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