1 ;;; disass.el --- disassembler for compiled Emacs Lisp code -*- lexical-binding:t -*-
3 ;; Copyright (C) 1986, 1991, 2002-2015 Free Software Foundation, Inc.
5 ;; Author: Doug Cutting <doug@csli.stanford.edu>
6 ;; Jamie Zawinski <jwz@lucid.com>
7 ;; Maintainer: emacs-devel@gnu.org
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.
40 ;; The variable byte-code-vector is defined by the new bytecomp.el.
41 ;; The function byte-decompile-lapcode is defined in byte-opt.el.
42 ;; Since we don't use byte-decompile-lapcode, let's try not loading byte-opt.
43 (require 'byte-compile
"bytecomp")
45 (defvar disassemble-column-1-indent
8 "*")
46 (defvar disassemble-column-2-indent
10 "*")
48 (defvar disassemble-recursive-indent
3 "*")
51 (defun disassemble (object &optional buffer indent interactive-p
)
52 "Print disassembled code for OBJECT in (optional) BUFFER.
53 OBJECT can be a symbol defined as a function, or a function itself
54 \(a lambda expression or a compiled-function object).
55 If OBJECT is not already compiled, we compile it, but do not
56 redefine OBJECT if it is a symbol."
57 (interactive (list (intern (completing-read "Disassemble function: "
60 (if (and (consp object
) (not (functionp object
)))
61 (setq object
`(lambda () ,object
)))
62 (or indent
(setq indent
0)) ;Default indent to zero
64 (if (or interactive-p
(null buffer
))
65 (with-output-to-temp-buffer "*Disassemble*"
66 (set-buffer "*Disassemble*")
67 (disassemble-internal object indent
(not interactive-p
)))
69 (disassemble-internal object indent nil
)))
73 (defun disassemble-internal (obj indent interactive-p
)
75 (name (when (symbolp obj
)
77 (setq obj
(indirect-function obj
)))))
79 (setq obj
(autoload-do-load obj name
))
81 (error "Can't disassemble #<subr %s>" name
))
82 (if (eq (car-safe obj
) 'macro
) ;Handle macros.
85 (if (eq (car-safe obj
) 'byte-code
)
86 (setq obj
`(lambda () ,obj
)))
88 (unless (functionp obj
) (error "not a function"))
89 (if (assq 'byte-code obj
)
91 (if interactive-p
(message (if name
92 "Compiling %s's definition..."
93 "Compiling definition...")
95 (setq obj
(byte-compile obj
))
96 (if interactive-p
(message "Done compiling. Disassembling..."))))
98 (setq args
(help-function-arglist obj
)) ;save arg list
99 (setq obj
(cdr obj
)) ;throw lambda away
100 (setq obj
(cdr obj
)))
101 ((byte-code-function-p obj
)
102 (setq args
(help-function-arglist obj
)))
103 (t (error "Compilation failed")))
104 (if (zerop indent
) ; not a nested function
107 (insert (format "byte code%s%s%s:\n"
108 (if (or macro name
) " for" "")
109 (if macro
" macro" "")
110 (if name
(format " %s" name
) "")))))
111 (let ((doc (if (consp obj
)
112 (and (stringp (car obj
)) (car obj
))
113 ;; Use documentation to get lazy-loaded doc string
114 (documentation obj t
))))
115 (if (and doc
(stringp doc
))
116 (progn (and (consp obj
) (setq obj
(cdr obj
)))
118 (princ " doc: " (current-buffer))
119 (if (string-match "\n" doc
)
120 (setq doc
(concat (substring doc
0 (match-beginning 0))
125 (prin1 args
(current-buffer))
127 (let ((interactive (interactive-form obj
)))
130 (setq interactive
(nth 1 interactive
))
131 (if (eq (car-safe (car-safe obj
)) 'interactive
)
132 (setq obj
(cdr obj
)))
134 (insert " interactive: ")
135 (if (eq (car-safe interactive
) 'byte-code
)
138 (disassemble-1 interactive
139 (+ indent disassemble-recursive-indent
)))
140 (let ((print-escape-newlines t
))
141 (prin1 interactive
(current-buffer))))
143 (cond ((and (consp obj
) (assq 'byte-code obj
))
144 (disassemble-1 (assq 'byte-code obj
) indent
))
145 ((byte-code-function-p obj
)
146 (disassemble-1 obj indent
))
148 (insert "Uncompiled body: ")
149 (let ((print-escape-newlines t
))
150 (prin1 (macroexp-progn obj
)
151 (current-buffer))))))
156 (defun disassemble-1 (obj indent
)
157 "Prints the byte-code call OBJ in the current buffer.
158 OBJ should be a call to BYTE-CODE generated by the byte compiler."
159 (let (bytes constvec
)
161 (setq bytes
(car (cdr obj
)) ;the byte code
162 constvec
(car (cdr (cdr obj
)))) ;constant vector
163 ;; If it is lazy-loaded, load it now
165 (setq bytes
(aref obj
1)
166 constvec
(aref obj
2)))
167 (let ((lap (byte-decompile-bytecode (string-as-unibyte bytes
) constvec
))
168 op arg opname pc-value
)
172 (while (setq tmp
(assq 'TAG lap
))
173 (setcar (cdr tmp
) (setq tagno
(1+ tagno
)))
174 (setq lap
(cdr (memq tmp lap
)))))
176 ;; Take off the pc value of the next thing
177 ;; and put it in pc-value.
179 (if (numberp (car lap
))
180 (setq pc-value
(car lap
)
182 ;; Fetch the next op and its arg.
183 (setq op
(car (car lap
))
189 ;; We have a label. Display it, but first its pc value.
191 (insert (format "%d:" pc-value
)))
192 (insert (int-to-string (car arg
))))
193 ;; We have an instruction. Display its pc value first.
195 (insert (format "%d" pc-value
)))
196 (indent-to (+ indent disassemble-column-1-indent
))
198 (string-match "^byte-" (setq opname
(symbol-name op
))))
199 (setq opname
(substring opname
5))
200 (setq opname
"<not-an-opcode>"))
201 (if (eq op
'byte-constant2
)
202 (insert " #### shouldn't have seen constant2 here!\n "))
204 (indent-to (+ indent disassemble-column-1-indent
205 disassemble-column-2-indent
208 (cond ((memq op byte-goto-ops
)
209 (insert (int-to-string (nth 1 arg
))))
210 ((memq op
'(byte-call byte-unbind
211 byte-listN byte-concatN byte-insertN
212 byte-stack-ref byte-stack-set byte-stack-set2
213 byte-discardN byte-discardN-preserve-tos
))
214 (insert (int-to-string arg
)))
215 ((memq op
'(byte-varref byte-varset byte-varbind
))
216 (prin1 (car arg
) (current-buffer)))
217 ((memq op
'(byte-constant byte-constant2
))
220 ;; but if the value of the constant is compiled code, then
221 ;; recursively disassemble it.
222 (cond ((or (byte-code-function-p arg
)
223 (and (consp arg
) (functionp arg
)
224 (assq 'byte-code arg
))
225 (and (eq (car-safe arg
) 'macro
)
226 (or (byte-code-function-p (cdr arg
))
227 (and (consp (cdr arg
))
228 (functionp (cdr arg
))
229 (assq 'byte-code
(cdr arg
))))))
230 (cond ((byte-code-function-p arg
)
231 (insert "<compiled-function>\n"))
233 (insert "<compiled lambda>"))
234 (t (insert "<compiled macro>\n")))
235 (disassemble-internal
237 (+ indent disassemble-recursive-indent
1)
239 ((eq (car-safe arg
) 'byte-code
)
240 (insert "<byte code>\n")
241 (disassemble-1 ;recurse on byte-code object
243 (+ indent disassemble-recursive-indent
)))
244 ((eq (car-safe (car-safe arg
)) 'byte-code
)
245 (insert "(<byte code>...)\n")
246 (mapc ;recurse on list of byte-code objects
250 (+ indent disassemble-recursive-indent
)))
253 ;; really just a constant
254 (let ((print-escape-newlines t
))
255 (prin1 arg
(current-buffer))))))
262 ;;; disass.el ends here