Mercurial > emacs
annotate lisp/emacs-lisp/disass.el @ 105403:670a96f450ae
*** empty log message ***
author | Michael Albinus <michael.albinus@gmx.de> |
---|---|
date | Sat, 03 Oct 2009 12:36:21 +0000 |
parents | a9dc0e7c3f2b |
children | 1d1d5d9bd884 |
rev | line source |
---|---|
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
1 ;;; disass.el --- disassembler for compiled Emacs Lisp code |
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
2 |
64751
5b1a238fcbb4
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64085
diff
changeset
|
3 ;; Copyright (C) 1986, 1991, 2002, 2003, 2004, |
100908 | 4 ;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
845 | 5 |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
6 ;; Author: Doug Cutting <doug@csli.stanford.edu> |
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
7 ;; Jamie Zawinski <jwz@lucid.com> |
55044
3b81dccc5b32
(disassemble): Handle lambda-exp as arg.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
8 ;; Maintainer: FSF |
2247
2c7997f249eb
Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1821
diff
changeset
|
9 ;; Keywords: internal |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
10 |
757 | 11 ;; This file is part of GNU Emacs. |
12 | |
94655
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
13 ;; GNU Emacs is free software: you can redistribute it and/or modify |
757 | 14 ;; it under the terms of the GNU General Public License as published by |
94655
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
15 ;; the Free Software Foundation, either version 3 of the License, or |
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
16 ;; (at your option) any later version. |
757 | 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 | |
94655
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
757 | 25 |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
26 ;;; Commentary: |
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
27 |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
28 ;; The single entry point, `disassemble', disassembles a code object generated |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
29 ;; by the Emacs Lisp byte-compiler. This doesn't invert the compilation |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
30 ;; operation, not by a long shot, but it's useful for debugging. |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
31 |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
32 ;; |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
33 ;; Original version by Doug Cutting (doug@csli.stanford.edu) |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
34 ;; Substantially modified by Jamie Zawinski <jwz@lucid.com> for |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
35 ;; the new lapcode-based byte compiler. |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
36 |
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
37 ;;; Code: |
757 | 38 |
39 ;;; The variable byte-code-vector is defined by the new bytecomp.el. | |
802 | 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. | |
1606
4303c30b29de
* disass.el (byte-compile): Specify that the 'byte-compile feature
Jim Blandy <jimb@redhat.com>
parents:
845
diff
changeset
|
42 (require 'byte-compile "bytecomp") |
757 | 43 |
8293
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
44 (defvar disassemble-column-1-indent 8 "*") |
757 | 45 (defvar disassemble-column-2-indent 10 "*") |
46 | |
47 (defvar disassemble-recursive-indent 3 "*") | |
48 | |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1606
diff
changeset
|
49 ;;;###autoload |
757 | 50 (defun disassemble (object &optional buffer indent interactive-p) |
51 "Print disassembled code for OBJECT in (optional) BUFFER. | |
52 OBJECT can be a symbol defined as a function, or a function itself | |
53 \(a lambda expression or a compiled-function object). | |
54 If OBJECT is not already compiled, we compile it, but do not | |
55 redefine OBJECT if it is a symbol." | |
56 (interactive (list (intern (completing-read "Disassemble function: " | |
57 obarray 'fboundp t)) | |
58 nil 0 t)) | |
55044
3b81dccc5b32
(disassemble): Handle lambda-exp as arg.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
59 (if (and (consp object) (not (eq (car object) 'lambda))) |
757 | 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) | |
76 args) | |
77 (while (symbolp obj) | |
78 (setq name obj | |
79 obj (symbol-function obj))) | |
80 (if (subrp obj) | |
81 (error "Can't disassemble #<subr %s>" name)) | |
3767
660b7b4e3e40
(disassemble-internal): If function is autoload, load it.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
82 (if (and (listp obj) (eq (car obj) 'autoload)) |
660b7b4e3e40
(disassemble-internal): If function is autoload, load it.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
83 (progn |
660b7b4e3e40
(disassemble-internal): If function is autoload, load it.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
84 (load (nth 1 obj)) |
660b7b4e3e40
(disassemble-internal): If function is autoload, load it.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
85 (setq obj (symbol-function name)))) |
757 | 86 (if (eq (car-safe obj) 'macro) ;handle macros |
87 (setq macro t | |
88 obj (cdr obj))) | |
4789
86318d7728f5
(disassemble-internal): Allow a call to byte-code as argument.
Richard M. Stallman <rms@gnu.org>
parents:
3767
diff
changeset
|
89 (if (and (listp obj) (eq (car obj) 'byte-code)) |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
22995
diff
changeset
|
90 (setq obj (list 'lambda nil obj))) |
757 | 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))) | |
14960
8fe7e5e09773
(disassemble-internal): Graceful error if compile failed.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
106 ((byte-code-function-p obj) |
8fe7e5e09773
(disassemble-internal): Graceful error if compile failed.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
107 (setq args (aref obj 0))) |
8fe7e5e09773
(disassemble-internal): Graceful error if compile failed.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
108 (t (error "Compilation failed"))) |
757 | 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)) | |
13340
53bbedbefdb2
(disassemble-internal): Handle lazy-loaded doc strings.
Richard M. Stallman <rms@gnu.org>
parents:
8293
diff
changeset
|
118 ;; Use documentation to get lazy-loaded doc string |
53bbedbefdb2
(disassemble-internal): Handle lazy-loaded doc strings.
Richard M. Stallman <rms@gnu.org>
parents:
8293
diff
changeset
|
119 (documentation obj t)))) |
757 | 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)) | |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1606
diff
changeset
|
153 ((byte-code-function-p obj) |
757 | 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 | |
13340
53bbedbefdb2
(disassemble-internal): Handle lazy-loaded doc strings.
Richard M. Stallman <rms@gnu.org>
parents:
8293
diff
changeset
|
171 ;; If it is lazy-loaded, load it now |
53bbedbefdb2
(disassemble-internal): Handle lazy-loaded doc strings.
Richard M. Stallman <rms@gnu.org>
parents:
8293
diff
changeset
|
172 (fetch-bytecode obj) |
22995
bc5bd04bf914
(disassemble-1): Move the call to
Richard M. Stallman <rms@gnu.org>
parents:
22055
diff
changeset
|
173 (setq bytes (aref obj 1) |
757 | 174 constvec (aref obj 2))) |
22995
bc5bd04bf914
(disassemble-1): Move the call to
Richard M. Stallman <rms@gnu.org>
parents:
22055
diff
changeset
|
175 (let ((lap (byte-decompile-bytecode (string-as-unibyte bytes) constvec)) |
8293
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
176 op arg opname pc-value) |
757 | 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 | |
8293
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
184 ;; Take off the pc value of the next thing |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
185 ;; and put it in pc-value. |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
186 (setq pc-value nil) |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
187 (if (numberp (car lap)) |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
188 (setq pc-value (car lap) |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
189 lap (cdr lap))) |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
190 ;; Fetch the next op and its arg. |
757 | 191 (setq op (car (car lap)) |
192 arg (cdr (car lap))) | |
8293
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
193 (setq lap (cdr lap)) |
757 | 194 (indent-to indent) |
195 (if (eq 'TAG op) | |
8293
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
196 (progn |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
197 ;; We have a label. Display it, but first its pc value. |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
198 (if pc-value |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
199 (insert (format "%d:" pc-value))) |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
200 (insert (int-to-string (car arg)))) |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
201 ;; We have an instruction. Display its pc value first. |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
202 (if pc-value |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
203 (insert (format "%d" pc-value))) |
757 | 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 | |
219 byte-listN byte-concatN byte-insertN)) | |
220 (insert (int-to-string arg))) | |
221 ((memq op '(byte-varref byte-varset byte-varbind)) | |
222 (prin1 (car arg) (current-buffer))) | |
223 ((memq op '(byte-constant byte-constant2)) | |
224 ;; it's a constant | |
225 (setq arg (car arg)) | |
226 ;; but if the value of the constant is compiled code, then | |
227 ;; recursively disassemble it. | |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1606
diff
changeset
|
228 (cond ((or (byte-code-function-p arg) |
757 | 229 (and (eq (car-safe arg) 'lambda) |
230 (assq 'byte-code arg)) | |
231 (and (eq (car-safe arg) 'macro) | |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1606
diff
changeset
|
232 (or (byte-code-function-p (cdr arg)) |
757 | 233 (and (eq (car-safe (cdr arg)) 'lambda) |
234 (assq 'byte-code (cdr arg)))))) | |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1606
diff
changeset
|
235 (cond ((byte-code-function-p arg) |
757 | 236 (insert "<compiled-function>\n")) |
237 ((eq (car-safe arg) 'lambda) | |
238 (insert "<compiled lambda>")) | |
239 (t (insert "<compiled macro>\n"))) | |
240 (disassemble-internal | |
241 arg | |
242 (+ indent disassemble-recursive-indent 1) | |
243 nil)) | |
244 ((eq (car-safe arg) 'byte-code) | |
245 (insert "<byte code>\n") | |
246 (disassemble-1 ;recurse on byte-code object | |
247 arg | |
248 (+ indent disassemble-recursive-indent))) | |
249 ((eq (car-safe (car-safe arg)) 'byte-code) | |
250 (insert "(<byte code>...)\n") | |
84896
5b112f6186e3
(disassemble-1): Use `mapc' rather than `mapcar'.
Juanma Barranquero <lekktu@gmail.com>
parents:
78217
diff
changeset
|
251 (mapc ;recurse on list of byte-code objects |
757 | 252 '(lambda (obj) |
253 (disassemble-1 | |
254 obj | |
255 (+ indent disassemble-recursive-indent))) | |
256 arg)) | |
257 (t | |
258 ;; really just a constant | |
259 (let ((print-escape-newlines t)) | |
260 (prin1 arg (current-buffer)))))) | |
261 ) | |
8293
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
262 (insert "\n"))))) |
757 | 263 nil) |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
264 |
18383 | 265 (provide 'disass) |
266 | |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
87649
diff
changeset
|
267 ;; arch-tag: 89482fe4-a087-4761-8dc6-d771054e763a |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
268 ;;; disass.el ends here |