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