1 ;;; disass.el --- disassembler for compiled Emacs Lisp code
3 ;; Copyright (C) 1986, 1991, 2003 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 2, or (at your option)
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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; The single entry point, `disassemble', disassembles a code object generated
30 ;; by the Emacs Lisp byte-compiler. This doesn't invert the compilation
31 ;; operation, not by a long shot, but it's useful for debugging.
34 ;; Original version by Doug Cutting (doug@csli.stanford.edu)
35 ;; Substantially modified by Jamie Zawinski <jwz@lucid.com> for
36 ;; 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 (eq (car object
) 'lambda
)))
61 (setq object
(list '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
)
80 obj
(symbol-function obj
)))
82 (error "Can't disassemble #<subr %s>" name
))
83 (if (and (listp obj
) (eq (car obj
) 'autoload
))
86 (setq obj
(symbol-function name
))))
87 (if (eq (car-safe obj
) 'macro
) ;handle macros
90 (if (and (listp obj
) (eq (car obj
) 'byte-code
))
91 (setq obj
(list 'lambda nil obj
)))
92 (if (and (listp obj
) (not (eq (car obj
) 'lambda
)))
93 (error "not a function"))
95 (if (assq 'byte-code obj
)
97 (if interactive-p
(message (if name
98 "Compiling %s's definition..."
99 "Compiling definition...")
101 (setq obj
(byte-compile obj
))
102 (if interactive-p
(message "Done compiling. Disassembling..."))))
104 (setq obj
(cdr obj
)) ;throw lambda away
105 (setq args
(car obj
)) ;save arg list
106 (setq obj
(cdr obj
)))
107 ((byte-code-function-p obj
)
108 (setq args
(aref obj
0)))
109 (t (error "Compilation failed")))
110 (if (zerop indent
) ; not a nested function
113 (insert (format "byte code%s%s%s:\n"
114 (if (or macro name
) " for" "")
115 (if macro
" macro" "")
116 (if name
(format " %s" name
) "")))))
117 (let ((doc (if (consp obj
)
118 (and (stringp (car obj
)) (car obj
))
119 ;; Use documentation to get lazy-loaded doc string
120 (documentation obj t
))))
121 (if (and doc
(stringp doc
))
122 (progn (and (consp obj
) (setq obj
(cdr obj
)))
124 (princ " doc: " (current-buffer))
125 (if (string-match "\n" doc
)
126 (setq doc
(concat (substring doc
0 (match-beginning 0))
131 (prin1 args
(current-buffer))
133 (let ((interactive (cond ((consp obj
)
134 (assq 'interactive obj
))
136 (list 'interactive
(aref obj
5))))))
139 (setq interactive
(nth 1 interactive
))
140 (if (eq (car-safe (car-safe obj
)) 'interactive
)
141 (setq obj
(cdr obj
)))
143 (insert " interactive: ")
144 (if (eq (car-safe interactive
) 'byte-code
)
147 (disassemble-1 interactive
148 (+ indent disassemble-recursive-indent
)))
149 (let ((print-escape-newlines t
))
150 (prin1 interactive
(current-buffer))))
152 (cond ((and (consp obj
) (assq 'byte-code obj
))
153 (disassemble-1 (assq 'byte-code obj
) indent
))
154 ((byte-code-function-p obj
)
155 (disassemble-1 obj indent
))
157 (insert "Uncompiled body: ")
158 (let ((print-escape-newlines t
))
159 (prin1 (if (cdr obj
) (cons 'progn obj
) (car obj
))
160 (current-buffer))))))
165 (defun disassemble-1 (obj indent
)
166 "Prints the byte-code call OBJ in the current buffer.
167 OBJ should be a call to BYTE-CODE generated by the byte compiler."
168 (let (bytes constvec
)
170 (setq bytes
(car (cdr obj
)) ;the byte code
171 constvec
(car (cdr (cdr obj
)))) ;constant vector
172 ;; If it is lazy-loaded, load it now
174 (setq bytes
(aref obj
1)
175 constvec
(aref obj
2)))
176 (let ((lap (byte-decompile-bytecode (string-as-unibyte bytes
) constvec
))
177 op arg opname pc-value
)
181 (while (setq tmp
(assq 'TAG lap
))
182 (setcar (cdr tmp
) (setq tagno
(1+ tagno
)))
183 (setq lap
(cdr (memq tmp lap
)))))
185 ;; Take off the pc value of the next thing
186 ;; and put it in pc-value.
188 (if (numberp (car lap
))
189 (setq pc-value
(car lap
)
191 ;; Fetch the next op and its arg.
192 (setq op
(car (car lap
))
198 ;; We have a label. Display it, but first its pc value.
200 (insert (format "%d:" pc-value
)))
201 (insert (int-to-string (car arg
))))
202 ;; We have an instruction. Display its pc value first.
204 (insert (format "%d" pc-value
)))
205 (indent-to (+ indent disassemble-column-1-indent
))
207 (string-match "^byte-" (setq opname
(symbol-name op
))))
208 (setq opname
(substring opname
5))
209 (setq opname
"<not-an-opcode>"))
210 (if (eq op
'byte-constant2
)
211 (insert " #### shouldn't have seen constant2 here!\n "))
213 (indent-to (+ indent disassemble-column-1-indent
214 disassemble-column-2-indent
217 (cond ((memq op byte-goto-ops
)
218 (insert (int-to-string (nth 1 arg
))))
219 ((memq op
'(byte-call byte-unbind
220 byte-listN byte-concatN byte-insertN
))
221 (insert (int-to-string arg
)))
222 ((memq op
'(byte-varref byte-varset byte-varbind
))
223 (prin1 (car arg
) (current-buffer)))
224 ((memq op
'(byte-constant byte-constant2
))
227 ;; but if the value of the constant is compiled code, then
228 ;; recursively disassemble it.
229 (cond ((or (byte-code-function-p arg
)
230 (and (eq (car-safe arg
) 'lambda
)
231 (assq 'byte-code arg
))
232 (and (eq (car-safe arg
) 'macro
)
233 (or (byte-code-function-p (cdr arg
))
234 (and (eq (car-safe (cdr arg
)) 'lambda
)
235 (assq 'byte-code
(cdr arg
))))))
236 (cond ((byte-code-function-p arg
)
237 (insert "<compiled-function>\n"))
238 ((eq (car-safe arg
) 'lambda
)
239 (insert "<compiled lambda>"))
240 (t (insert "<compiled macro>\n")))
241 (disassemble-internal
243 (+ indent disassemble-recursive-indent
1)
245 ((eq (car-safe arg
) 'byte-code
)
246 (insert "<byte code>\n")
247 (disassemble-1 ;recurse on byte-code object
249 (+ indent disassemble-recursive-indent
)))
250 ((eq (car-safe (car-safe arg
)) 'byte-code
)
251 (insert "(<byte code>...)\n")
252 (mapcar ;recurse on list of byte-code objects
256 (+ indent disassemble-recursive-indent
)))
259 ;; really just a constant
260 (let ((print-escape-newlines t
))
261 (prin1 arg
(current-buffer))))))
268 ;;; arch-tag: 89482fe4-a087-4761-8dc6-d771054e763a
269 ;;; disass.el ends here