Commit | Line | Data |
---|---|---|
fd7fa35a ER |
1 | ;;; disass.el --- disassembler for compiled Emacs Lisp code |
2 | ||
73b0cd50 | 3 | ;; Copyright (C) 1986, 1991, 2002-2011 Free Software Foundation, Inc. |
9750e079 | 4 | |
fd7fa35a ER |
5 | ;; Author: Doug Cutting <doug@csli.stanford.edu> |
6 | ;; Jamie Zawinski <jwz@lucid.com> | |
dc0485c4 | 7 | ;; Maintainer: FSF |
e9571d2a | 8 | ;; Keywords: internal |
fd7fa35a | 9 | |
1c393159 JB |
10 | ;; This file is part of GNU Emacs. |
11 | ||
d6cba7ae | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
1c393159 | 13 | ;; it under the terms of the GNU General Public License as published by |
d6cba7ae GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
1c393159 JB |
16 | |
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. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
d6cba7ae | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
1c393159 | 24 | |
fd7fa35a ER |
25 | ;;; Commentary: |
26 | ||
e41b2db1 ER |
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. | |
30 | ||
31 | ;; | |
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. | |
fd7fa35a ER |
35 | |
36 | ;;; Code: | |
1c393159 JB |
37 | |
38 | ;;; The variable byte-code-vector is defined by the new bytecomp.el. | |
c29118da JB |
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. | |
d28a1925 | 41 | (require 'byte-compile "bytecomp") |
1c393159 | 42 | |
3c7e6b9d | 43 | (defvar disassemble-column-1-indent 8 "*") |
1c393159 JB |
44 | (defvar disassemble-column-2-indent 10 "*") |
45 | ||
46 | (defvar disassemble-recursive-indent 3 "*") | |
47 | ||
dbc4e1c1 | 48 | ;;;###autoload |
1c393159 JB |
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: " | |
56 | obarray 'fboundp t)) | |
57 | nil 0 t)) | |
dc0485c4 | 58 | (if (and (consp object) (not (eq (car object) 'lambda))) |
1c393159 JB |
59 | (setq object (list 'lambda () object))) |
60 | (or indent (setq indent 0)) ;Default indent to zero | |
61 | (save-excursion | |
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))) | |
66 | (set-buffer buffer) | |
67 | (disassemble-internal object indent nil))) | |
68 | nil) | |
69 | ||
70 | ||
71 | (defun disassemble-internal (obj indent interactive-p) | |
72 | (let ((macro 'nil) | |
73 | (name 'nil) | |
74 | (doc 'nil) | |
75 | args) | |
76 | (while (symbolp obj) | |
77 | (setq name obj | |
78 | obj (symbol-function obj))) | |
79 | (if (subrp obj) | |
80 | (error "Can't disassemble #<subr %s>" name)) | |
b9598260 SM |
81 | (when (and (listp obj) (eq (car obj) 'autoload)) |
82 | (load (nth 1 obj)) | |
83 | (setq obj (symbol-function name))) | |
1c393159 JB |
84 | (if (eq (car-safe obj) 'macro) ;handle macros |
85 | (setq macro t | |
86 | obj (cdr obj))) | |
b9598260 | 87 | (when (and (listp obj) (eq (car obj) 'closure)) |
23aba0ea | 88 | (error "Don't know how to compile an interpreted closure")) |
8dff74b7 | 89 | (if (and (listp obj) (eq (car obj) 'byte-code)) |
a1506d29 | 90 | (setq obj (list 'lambda nil obj))) |
1c393159 JB |
91 | (if (and (listp obj) (not (eq (car obj) 'lambda))) |
92 | (error "not a function")) | |
93 | (if (consp obj) | |
94 | (if (assq 'byte-code obj) | |
95 | nil | |
96 | (if interactive-p (message (if name | |
97 | "Compiling %s's definition..." | |
98 | "Compiling definition...") | |
99 | name)) | |
100 | (setq obj (byte-compile obj)) | |
101 | (if interactive-p (message "Done compiling. Disassembling...")))) | |
102 | (cond ((consp obj) | |
103 | (setq obj (cdr obj)) ;throw lambda away | |
104 | (setq args (car obj)) ;save arg list | |
105 | (setq obj (cdr obj))) | |
c757fb07 RS |
106 | ((byte-code-function-p obj) |
107 | (setq args (aref obj 0))) | |
108 | (t (error "Compilation failed"))) | |
1c393159 JB |
109 | (if (zerop indent) ; not a nested function |
110 | (progn | |
111 | (indent-to indent) | |
112 | (insert (format "byte code%s%s%s:\n" | |
113 | (if (or macro name) " for" "") | |
114 | (if macro " macro" "") | |
115 | (if name (format " %s" name) ""))))) | |
116 | (let ((doc (if (consp obj) | |
117 | (and (stringp (car obj)) (car obj)) | |
3fd4909e RS |
118 | ;; Use documentation to get lazy-loaded doc string |
119 | (documentation obj t)))) | |
1c393159 JB |
120 | (if (and doc (stringp doc)) |
121 | (progn (and (consp obj) (setq obj (cdr obj))) | |
122 | (indent-to indent) | |
123 | (princ " doc: " (current-buffer)) | |
124 | (if (string-match "\n" doc) | |
125 | (setq doc (concat (substring doc 0 (match-beginning 0)) | |
126 | " ..."))) | |
127 | (insert doc "\n")))) | |
128 | (indent-to indent) | |
129 | (insert " args: ") | |
130 | (prin1 args (current-buffer)) | |
131 | (insert "\n") | |
132 | (let ((interactive (cond ((consp obj) | |
133 | (assq 'interactive obj)) | |
134 | ((> (length obj) 5) | |
135 | (list 'interactive (aref obj 5)))))) | |
136 | (if interactive | |
137 | (progn | |
138 | (setq interactive (nth 1 interactive)) | |
139 | (if (eq (car-safe (car-safe obj)) 'interactive) | |
140 | (setq obj (cdr obj))) | |
141 | (indent-to indent) | |
142 | (insert " interactive: ") | |
143 | (if (eq (car-safe interactive) 'byte-code) | |
144 | (progn | |
145 | (insert "\n") | |
146 | (disassemble-1 interactive | |
147 | (+ indent disassemble-recursive-indent))) | |
148 | (let ((print-escape-newlines t)) | |
149 | (prin1 interactive (current-buffer)))) | |
150 | (insert "\n")))) | |
151 | (cond ((and (consp obj) (assq 'byte-code obj)) | |
152 | (disassemble-1 (assq 'byte-code obj) indent)) | |
dbc4e1c1 | 153 | ((byte-code-function-p obj) |
1c393159 JB |
154 | (disassemble-1 obj indent)) |
155 | (t | |
156 | (insert "Uncompiled body: ") | |
157 | (let ((print-escape-newlines t)) | |
158 | (prin1 (if (cdr obj) (cons 'progn obj) (car obj)) | |
159 | (current-buffer)))))) | |
160 | (if interactive-p | |
161 | (message ""))) | |
162 | ||
163 | ||
164 | (defun disassemble-1 (obj indent) | |
165 | "Prints the byte-code call OBJ in the current buffer. | |
166 | OBJ should be a call to BYTE-CODE generated by the byte compiler." | |
167 | (let (bytes constvec) | |
168 | (if (consp obj) | |
169 | (setq bytes (car (cdr obj)) ;the byte code | |
170 | constvec (car (cdr (cdr obj)))) ;constant vector | |
3fd4909e RS |
171 | ;; If it is lazy-loaded, load it now |
172 | (fetch-bytecode obj) | |
d18a808f | 173 | (setq bytes (aref obj 1) |
1c393159 | 174 | constvec (aref obj 2))) |
d18a808f | 175 | (let ((lap (byte-decompile-bytecode (string-as-unibyte bytes) constvec)) |
3c7e6b9d | 176 | op arg opname pc-value) |
1c393159 JB |
177 | (let ((tagno 0) |
178 | tmp | |
179 | (lap lap)) | |
180 | (while (setq tmp (assq 'TAG lap)) | |
181 | (setcar (cdr tmp) (setq tagno (1+ tagno))) | |
182 | (setq lap (cdr (memq tmp lap))))) | |
183 | (while lap | |
3c7e6b9d RS |
184 | ;; Take off the pc value of the next thing |
185 | ;; and put it in pc-value. | |
186 | (setq pc-value nil) | |
187 | (if (numberp (car lap)) | |
188 | (setq pc-value (car lap) | |
189 | lap (cdr lap))) | |
190 | ;; Fetch the next op and its arg. | |
1c393159 JB |
191 | (setq op (car (car lap)) |
192 | arg (cdr (car lap))) | |
3c7e6b9d | 193 | (setq lap (cdr lap)) |
1c393159 JB |
194 | (indent-to indent) |
195 | (if (eq 'TAG op) | |
3c7e6b9d RS |
196 | (progn |
197 | ;; We have a label. Display it, but first its pc value. | |
198 | (if pc-value | |
199 | (insert (format "%d:" pc-value))) | |
200 | (insert (int-to-string (car arg)))) | |
201 | ;; We have an instruction. Display its pc value first. | |
202 | (if pc-value | |
203 | (insert (format "%d" pc-value))) | |
1c393159 JB |
204 | (indent-to (+ indent disassemble-column-1-indent)) |
205 | (if (and op | |
206 | (string-match "^byte-" (setq opname (symbol-name op)))) | |
207 | (setq opname (substring opname 5)) | |
208 | (setq opname "<not-an-opcode>")) | |
209 | (if (eq op 'byte-constant2) | |
210 | (insert " #### shouldn't have seen constant2 here!\n ")) | |
211 | (insert opname) | |
212 | (indent-to (+ indent disassemble-column-1-indent | |
213 | disassemble-column-2-indent | |
214 | -1)) | |
215 | (insert " ") | |
216 | (cond ((memq op byte-goto-ops) | |
217 | (insert (int-to-string (nth 1 arg)))) | |
218 | ((memq op '(byte-call byte-unbind | |
b9598260 SM |
219 | byte-listN byte-concatN byte-insertN |
220 | byte-stack-ref byte-stack-set byte-stack-set2 | |
221 | byte-discardN byte-discardN-preserve-tos)) | |
1c393159 JB |
222 | (insert (int-to-string arg))) |
223 | ((memq op '(byte-varref byte-varset byte-varbind)) | |
224 | (prin1 (car arg) (current-buffer))) | |
225 | ((memq op '(byte-constant byte-constant2)) | |
226 | ;; it's a constant | |
227 | (setq arg (car arg)) | |
228 | ;; but if the value of the constant is compiled code, then | |
229 | ;; recursively disassemble it. | |
dbc4e1c1 | 230 | (cond ((or (byte-code-function-p arg) |
1c393159 JB |
231 | (and (eq (car-safe arg) 'lambda) |
232 | (assq 'byte-code arg)) | |
233 | (and (eq (car-safe arg) 'macro) | |
dbc4e1c1 | 234 | (or (byte-code-function-p (cdr arg)) |
1c393159 JB |
235 | (and (eq (car-safe (cdr arg)) 'lambda) |
236 | (assq 'byte-code (cdr arg)))))) | |
dbc4e1c1 | 237 | (cond ((byte-code-function-p arg) |
1c393159 JB |
238 | (insert "<compiled-function>\n")) |
239 | ((eq (car-safe arg) 'lambda) | |
240 | (insert "<compiled lambda>")) | |
241 | (t (insert "<compiled macro>\n"))) | |
242 | (disassemble-internal | |
243 | arg | |
244 | (+ indent disassemble-recursive-indent 1) | |
245 | nil)) | |
246 | ((eq (car-safe arg) 'byte-code) | |
247 | (insert "<byte code>\n") | |
248 | (disassemble-1 ;recurse on byte-code object | |
249 | arg | |
250 | (+ indent disassemble-recursive-indent))) | |
251 | ((eq (car-safe (car-safe arg)) 'byte-code) | |
252 | (insert "(<byte code>...)\n") | |
56a1431a | 253 | (mapc ;recurse on list of byte-code objects |
1c393159 JB |
254 | '(lambda (obj) |
255 | (disassemble-1 | |
256 | obj | |
257 | (+ indent disassemble-recursive-indent))) | |
258 | arg)) | |
259 | (t | |
260 | ;; really just a constant | |
261 | (let ((print-escape-newlines t)) | |
262 | (prin1 arg (current-buffer)))))) | |
263 | ) | |
3c7e6b9d | 264 | (insert "\n"))))) |
1c393159 | 265 | nil) |
fd7fa35a | 266 | |
896546cd RS |
267 | (provide 'disass) |
268 | ||
fd7fa35a | 269 | ;;; disass.el ends here |