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