Mercurial > emacs
annotate lisp/emacs-lisp/disass.el @ 12244:ac7375e60931
Update GPL to version 2.
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Thu, 15 Jun 1995 20:42:24 +0000 |
parents | 74218ea236fe |
children | 53bbedbefdb2 |
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 |
845 | 3 ;;; Copyright (C) 1986, 1991 Free Software Foundation, Inc. |
4 | |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
5 ;; Author: Doug Cutting <doug@csli.stanford.edu> |
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
6 ;; Jamie Zawinski <jwz@lucid.com> |
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
7 ;; Maintainer: Jamie Zawinski <jwz@lucid.com> |
2247
2c7997f249eb
Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1821
diff
changeset
|
8 ;; Keywords: internal |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
9 |
757 | 10 ;; This file is part of GNU Emacs. |
11 | |
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 | |
806 | 14 ;; the Free Software Foundation; either version 2, or (at your option) |
757 | 15 ;; any later version. |
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 | |
23 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
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)) | |
59 (if (eq (car-safe object) 'byte-code) | |
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)) |
86318d7728f5
(disassemble-internal): Allow a call to byte-code as argument.
Richard M. Stallman <rms@gnu.org>
parents:
3767
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))) | |
106 (t | |
107 (setq args (aref obj 0)))) | |
108 (if (zerop indent) ; not a nested function | |
109 (progn | |
110 (indent-to indent) | |
111 (insert (format "byte code%s%s%s:\n" | |
112 (if (or macro name) " for" "") | |
113 (if macro " macro" "") | |
114 (if name (format " %s" name) ""))))) | |
115 (let ((doc (if (consp obj) | |
116 (and (stringp (car obj)) (car obj)) | |
117 (and (> (length obj) 4) (aref obj 4))))) | |
118 (if (and doc (stringp doc)) | |
119 (progn (and (consp obj) (setq obj (cdr obj))) | |
120 (indent-to indent) | |
121 (princ " doc: " (current-buffer)) | |
122 (if (string-match "\n" doc) | |
123 (setq doc (concat (substring doc 0 (match-beginning 0)) | |
124 " ..."))) | |
125 (insert doc "\n")))) | |
126 (indent-to indent) | |
127 (insert " args: ") | |
128 (prin1 args (current-buffer)) | |
129 (insert "\n") | |
130 (let ((interactive (cond ((consp obj) | |
131 (assq 'interactive obj)) | |
132 ((> (length obj) 5) | |
133 (list 'interactive (aref obj 5)))))) | |
134 (if interactive | |
135 (progn | |
136 (setq interactive (nth 1 interactive)) | |
137 (if (eq (car-safe (car-safe obj)) 'interactive) | |
138 (setq obj (cdr obj))) | |
139 (indent-to indent) | |
140 (insert " interactive: ") | |
141 (if (eq (car-safe interactive) 'byte-code) | |
142 (progn | |
143 (insert "\n") | |
144 (disassemble-1 interactive | |
145 (+ indent disassemble-recursive-indent))) | |
146 (let ((print-escape-newlines t)) | |
147 (prin1 interactive (current-buffer)))) | |
148 (insert "\n")))) | |
149 (cond ((and (consp obj) (assq 'byte-code obj)) | |
150 (disassemble-1 (assq 'byte-code obj) indent)) | |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1606
diff
changeset
|
151 ((byte-code-function-p obj) |
757 | 152 (disassemble-1 obj indent)) |
153 (t | |
154 (insert "Uncompiled body: ") | |
155 (let ((print-escape-newlines t)) | |
156 (prin1 (if (cdr obj) (cons 'progn obj) (car obj)) | |
157 (current-buffer)))))) | |
158 (if interactive-p | |
159 (message ""))) | |
160 | |
161 | |
162 (defun disassemble-1 (obj indent) | |
163 "Prints the byte-code call OBJ in the current buffer. | |
164 OBJ should be a call to BYTE-CODE generated by the byte compiler." | |
165 (let (bytes constvec) | |
166 (if (consp obj) | |
167 (setq bytes (car (cdr obj)) ;the byte code | |
168 constvec (car (cdr (cdr obj)))) ;constant vector | |
169 (setq bytes (aref obj 1) | |
170 constvec (aref obj 2))) | |
171 (let ((lap (byte-decompile-bytecode bytes constvec)) | |
8293
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
172 op arg opname pc-value) |
757 | 173 (let ((tagno 0) |
174 tmp | |
175 (lap lap)) | |
176 (while (setq tmp (assq 'TAG lap)) | |
177 (setcar (cdr tmp) (setq tagno (1+ tagno))) | |
178 (setq lap (cdr (memq tmp lap))))) | |
179 (while lap | |
8293
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
180 ;; 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
|
181 ;; and put it in pc-value. |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
182 (setq pc-value nil) |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
183 (if (numberp (car lap)) |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
184 (setq pc-value (car lap) |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
185 lap (cdr lap))) |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
186 ;; Fetch the next op and its arg. |
757 | 187 (setq op (car (car lap)) |
188 arg (cdr (car lap))) | |
8293
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
189 (setq lap (cdr lap)) |
757 | 190 (indent-to indent) |
191 (if (eq 'TAG op) | |
8293
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
192 (progn |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
193 ;; 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
|
194 (if pc-value |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
195 (insert (format "%d:" pc-value))) |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
196 (insert (int-to-string (car arg)))) |
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
197 ;; 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
|
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))) |
757 | 200 (indent-to (+ indent disassemble-column-1-indent)) |
201 (if (and op | |
202 (string-match "^byte-" (setq opname (symbol-name op)))) | |
203 (setq opname (substring opname 5)) | |
204 (setq opname "<not-an-opcode>")) | |
205 (if (eq op 'byte-constant2) | |
206 (insert " #### shouldn't have seen constant2 here!\n ")) | |
207 (insert opname) | |
208 (indent-to (+ indent disassemble-column-1-indent | |
209 disassemble-column-2-indent | |
210 -1)) | |
211 (insert " ") | |
212 (cond ((memq op byte-goto-ops) | |
213 (insert (int-to-string (nth 1 arg)))) | |
214 ((memq op '(byte-call byte-unbind | |
215 byte-listN byte-concatN byte-insertN)) | |
216 (insert (int-to-string arg))) | |
217 ((memq op '(byte-varref byte-varset byte-varbind)) | |
218 (prin1 (car arg) (current-buffer))) | |
219 ((memq op '(byte-constant byte-constant2)) | |
220 ;; it's a constant | |
221 (setq arg (car arg)) | |
222 ;; but if the value of the constant is compiled code, then | |
223 ;; recursively disassemble it. | |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1606
diff
changeset
|
224 (cond ((or (byte-code-function-p arg) |
757 | 225 (and (eq (car-safe arg) 'lambda) |
226 (assq 'byte-code arg)) | |
227 (and (eq (car-safe arg) 'macro) | |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1606
diff
changeset
|
228 (or (byte-code-function-p (cdr arg)) |
757 | 229 (and (eq (car-safe (cdr arg)) 'lambda) |
230 (assq 'byte-code (cdr arg)))))) | |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1606
diff
changeset
|
231 (cond ((byte-code-function-p arg) |
757 | 232 (insert "<compiled-function>\n")) |
233 ((eq (car-safe arg) 'lambda) | |
234 (insert "<compiled lambda>")) | |
235 (t (insert "<compiled macro>\n"))) | |
236 (disassemble-internal | |
237 arg | |
238 (+ indent disassemble-recursive-indent 1) | |
239 nil)) | |
240 ((eq (car-safe arg) 'byte-code) | |
241 (insert "<byte code>\n") | |
242 (disassemble-1 ;recurse on byte-code object | |
243 arg | |
244 (+ indent disassemble-recursive-indent))) | |
245 ((eq (car-safe (car-safe arg)) 'byte-code) | |
246 (insert "(<byte code>...)\n") | |
247 (mapcar ;recurse on list of byte-code objects | |
248 '(lambda (obj) | |
249 (disassemble-1 | |
250 obj | |
251 (+ indent disassemble-recursive-indent))) | |
252 arg)) | |
253 (t | |
254 ;; really just a constant | |
255 (let ((print-escape-newlines t)) | |
256 (prin1 arg (current-buffer)))))) | |
257 ) | |
8293
74218ea236fe
(disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents:
4789
diff
changeset
|
258 (insert "\n"))))) |
757 | 259 nil) |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
260 |
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
261 ;;; disass.el ends here |