disass.el 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. ;;; disass.el --- disassembler for compiled Emacs Lisp code
  2. ;; Copyright (C) 1986, 1991, 2002-2012 Free Software Foundation, Inc.
  3. ;; Author: Doug Cutting <doug@csli.stanford.edu>
  4. ;; Jamie Zawinski <jwz@lucid.com>
  5. ;; Maintainer: FSF
  6. ;; Keywords: internal
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; The single entry point, `disassemble', disassembles a code object generated
  20. ;; by the Emacs Lisp byte-compiler. This doesn't invert the compilation
  21. ;; operation, not by a long shot, but it's useful for debugging.
  22. ;;
  23. ;; Original version by Doug Cutting (doug@csli.stanford.edu)
  24. ;; Substantially modified by Jamie Zawinski <jwz@lucid.com> for
  25. ;; the new lapcode-based byte compiler.
  26. ;;; Code:
  27. ;;; The variable byte-code-vector is defined by the new bytecomp.el.
  28. ;;; The function byte-decompile-lapcode is defined in byte-opt.el.
  29. ;;; Since we don't use byte-decompile-lapcode, let's try not loading byte-opt.
  30. (require 'byte-compile "bytecomp")
  31. (defvar disassemble-column-1-indent 8 "*")
  32. (defvar disassemble-column-2-indent 10 "*")
  33. (defvar disassemble-recursive-indent 3 "*")
  34. ;;;###autoload
  35. (defun disassemble (object &optional buffer indent interactive-p)
  36. "Print disassembled code for OBJECT in (optional) BUFFER.
  37. OBJECT can be a symbol defined as a function, or a function itself
  38. \(a lambda expression or a compiled-function object).
  39. If OBJECT is not already compiled, we compile it, but do not
  40. redefine OBJECT if it is a symbol."
  41. (interactive (list (intern (completing-read "Disassemble function: "
  42. obarray 'fboundp t))
  43. nil 0 t))
  44. (if (and (consp object) (not (eq (car object) 'lambda)))
  45. (setq object (list 'lambda () object)))
  46. (or indent (setq indent 0)) ;Default indent to zero
  47. (save-excursion
  48. (if (or interactive-p (null buffer))
  49. (with-output-to-temp-buffer "*Disassemble*"
  50. (set-buffer "*Disassemble*")
  51. (disassemble-internal object indent (not interactive-p)))
  52. (set-buffer buffer)
  53. (disassemble-internal object indent nil)))
  54. nil)
  55. (defun disassemble-internal (obj indent interactive-p)
  56. (let ((macro 'nil)
  57. (name 'nil)
  58. (doc 'nil)
  59. args)
  60. (while (symbolp obj)
  61. (setq name obj
  62. obj (symbol-function obj)))
  63. (if (subrp obj)
  64. (error "Can't disassemble #<subr %s>" name))
  65. (when (and (listp obj) (eq (car obj) 'autoload))
  66. (load (nth 1 obj))
  67. (setq obj (symbol-function name)))
  68. (if (eq (car-safe obj) 'macro) ;handle macros
  69. (setq macro t
  70. obj (cdr obj)))
  71. (when (and (listp obj) (eq (car obj) 'closure))
  72. (error "Don't know how to compile an interpreted closure"))
  73. (if (and (listp obj) (eq (car obj) 'byte-code))
  74. (setq obj (list 'lambda nil obj)))
  75. (if (and (listp obj) (not (eq (car obj) 'lambda)))
  76. (error "not a function"))
  77. (if (consp obj)
  78. (if (assq 'byte-code obj)
  79. nil
  80. (if interactive-p (message (if name
  81. "Compiling %s's definition..."
  82. "Compiling definition...")
  83. name))
  84. (setq obj (byte-compile obj))
  85. (if interactive-p (message "Done compiling. Disassembling..."))))
  86. (cond ((consp obj)
  87. (setq obj (cdr obj)) ;throw lambda away
  88. (setq args (car obj)) ;save arg list
  89. (setq obj (cdr obj)))
  90. ((byte-code-function-p obj)
  91. (setq args (aref obj 0)))
  92. (t (error "Compilation failed")))
  93. (if (zerop indent) ; not a nested function
  94. (progn
  95. (indent-to indent)
  96. (insert (format "byte code%s%s%s:\n"
  97. (if (or macro name) " for" "")
  98. (if macro " macro" "")
  99. (if name (format " %s" name) "")))))
  100. (let ((doc (if (consp obj)
  101. (and (stringp (car obj)) (car obj))
  102. ;; Use documentation to get lazy-loaded doc string
  103. (documentation obj t))))
  104. (if (and doc (stringp doc))
  105. (progn (and (consp obj) (setq obj (cdr obj)))
  106. (indent-to indent)
  107. (princ " doc: " (current-buffer))
  108. (if (string-match "\n" doc)
  109. (setq doc (concat (substring doc 0 (match-beginning 0))
  110. " ...")))
  111. (insert doc "\n"))))
  112. (indent-to indent)
  113. (insert " args: ")
  114. (prin1 args (current-buffer))
  115. (insert "\n")
  116. (let ((interactive (cond ((consp obj)
  117. (assq 'interactive obj))
  118. ((> (length obj) 5)
  119. (list 'interactive (aref obj 5))))))
  120. (if interactive
  121. (progn
  122. (setq interactive (nth 1 interactive))
  123. (if (eq (car-safe (car-safe obj)) 'interactive)
  124. (setq obj (cdr obj)))
  125. (indent-to indent)
  126. (insert " interactive: ")
  127. (if (eq (car-safe interactive) 'byte-code)
  128. (progn
  129. (insert "\n")
  130. (disassemble-1 interactive
  131. (+ indent disassemble-recursive-indent)))
  132. (let ((print-escape-newlines t))
  133. (prin1 interactive (current-buffer))))
  134. (insert "\n"))))
  135. (cond ((and (consp obj) (assq 'byte-code obj))
  136. (disassemble-1 (assq 'byte-code obj) indent))
  137. ((byte-code-function-p obj)
  138. (disassemble-1 obj indent))
  139. (t
  140. (insert "Uncompiled body: ")
  141. (let ((print-escape-newlines t))
  142. (prin1 (if (cdr obj) (cons 'progn obj) (car obj))
  143. (current-buffer))))))
  144. (if interactive-p
  145. (message "")))
  146. (defun disassemble-1 (obj indent)
  147. "Prints the byte-code call OBJ in the current buffer.
  148. OBJ should be a call to BYTE-CODE generated by the byte compiler."
  149. (let (bytes constvec)
  150. (if (consp obj)
  151. (setq bytes (car (cdr obj)) ;the byte code
  152. constvec (car (cdr (cdr obj)))) ;constant vector
  153. ;; If it is lazy-loaded, load it now
  154. (fetch-bytecode obj)
  155. (setq bytes (aref obj 1)
  156. constvec (aref obj 2)))
  157. (let ((lap (byte-decompile-bytecode (string-as-unibyte bytes) constvec))
  158. op arg opname pc-value)
  159. (let ((tagno 0)
  160. tmp
  161. (lap lap))
  162. (while (setq tmp (assq 'TAG lap))
  163. (setcar (cdr tmp) (setq tagno (1+ tagno)))
  164. (setq lap (cdr (memq tmp lap)))))
  165. (while lap
  166. ;; Take off the pc value of the next thing
  167. ;; and put it in pc-value.
  168. (setq pc-value nil)
  169. (if (numberp (car lap))
  170. (setq pc-value (car lap)
  171. lap (cdr lap)))
  172. ;; Fetch the next op and its arg.
  173. (setq op (car (car lap))
  174. arg (cdr (car lap)))
  175. (setq lap (cdr lap))
  176. (indent-to indent)
  177. (if (eq 'TAG op)
  178. (progn
  179. ;; We have a label. Display it, but first its pc value.
  180. (if pc-value
  181. (insert (format "%d:" pc-value)))
  182. (insert (int-to-string (car arg))))
  183. ;; We have an instruction. Display its pc value first.
  184. (if pc-value
  185. (insert (format "%d" pc-value)))
  186. (indent-to (+ indent disassemble-column-1-indent))
  187. (if (and op
  188. (string-match "^byte-" (setq opname (symbol-name op))))
  189. (setq opname (substring opname 5))
  190. (setq opname "<not-an-opcode>"))
  191. (if (eq op 'byte-constant2)
  192. (insert " #### shouldn't have seen constant2 here!\n "))
  193. (insert opname)
  194. (indent-to (+ indent disassemble-column-1-indent
  195. disassemble-column-2-indent
  196. -1))
  197. (insert " ")
  198. (cond ((memq op byte-goto-ops)
  199. (insert (int-to-string (nth 1 arg))))
  200. ((memq op '(byte-call byte-unbind
  201. byte-listN byte-concatN byte-insertN
  202. byte-stack-ref byte-stack-set byte-stack-set2
  203. byte-discardN byte-discardN-preserve-tos))
  204. (insert (int-to-string arg)))
  205. ((memq op '(byte-varref byte-varset byte-varbind))
  206. (prin1 (car arg) (current-buffer)))
  207. ((memq op '(byte-constant byte-constant2))
  208. ;; it's a constant
  209. (setq arg (car arg))
  210. ;; but if the value of the constant is compiled code, then
  211. ;; recursively disassemble it.
  212. (cond ((or (byte-code-function-p arg)
  213. (and (eq (car-safe arg) 'lambda)
  214. (assq 'byte-code arg))
  215. (and (eq (car-safe arg) 'macro)
  216. (or (byte-code-function-p (cdr arg))
  217. (and (eq (car-safe (cdr arg)) 'lambda)
  218. (assq 'byte-code (cdr arg))))))
  219. (cond ((byte-code-function-p arg)
  220. (insert "<compiled-function>\n"))
  221. ((eq (car-safe arg) 'lambda)
  222. (insert "<compiled lambda>"))
  223. (t (insert "<compiled macro>\n")))
  224. (disassemble-internal
  225. arg
  226. (+ indent disassemble-recursive-indent 1)
  227. nil))
  228. ((eq (car-safe arg) 'byte-code)
  229. (insert "<byte code>\n")
  230. (disassemble-1 ;recurse on byte-code object
  231. arg
  232. (+ indent disassemble-recursive-indent)))
  233. ((eq (car-safe (car-safe arg)) 'byte-code)
  234. (insert "(<byte code>...)\n")
  235. (mapc ;recurse on list of byte-code objects
  236. (lambda (obj)
  237. (disassemble-1
  238. obj
  239. (+ indent disassemble-recursive-indent)))
  240. arg))
  241. (t
  242. ;; really just a constant
  243. (let ((print-escape-newlines t))
  244. (prin1 arg (current-buffer))))))
  245. )
  246. (insert "\n")))))
  247. nil)
  248. (provide 'disass)
  249. ;;; disass.el ends here