Mercurial > emacs
annotate lisp/emacs-lisp/disass.el @ 2232:4f9d60f7de9d
Add standard library headers.
author | Eric S. Raymond <esr@snark.thyrsus.com> |
---|---|
date | Wed, 17 Mar 1993 16:56:02 +0000 |
parents | 04fb1d3d6992 |
children | 2c7997f249eb |
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> |
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
8 ;; Keyword: internal |
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 |
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
28 ;;; Original version by Doug Cutting (doug@csli.stanford.edu) |
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
29 ;;; Substantially modified by Jamie Zawinski <jwz@lucid.com> for |
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
30 ;;; the new lapcode-based byte compiler. |
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
31 |
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
32 ;;; Code: |
757 | 33 |
34 ;;; The variable byte-code-vector is defined by the new bytecomp.el. | |
802 | 35 ;;; The function byte-decompile-lapcode is defined in byte-opt.el. |
36 ;;; 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
|
37 (require 'byte-compile "bytecomp") |
757 | 38 |
39 (defvar disassemble-column-1-indent 5 "*") | |
40 (defvar disassemble-column-2-indent 10 "*") | |
41 | |
42 (defvar disassemble-recursive-indent 3 "*") | |
43 | |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1606
diff
changeset
|
44 ;;;###autoload |
757 | 45 (defun disassemble (object &optional buffer indent interactive-p) |
46 "Print disassembled code for OBJECT in (optional) BUFFER. | |
47 OBJECT can be a symbol defined as a function, or a function itself | |
48 \(a lambda expression or a compiled-function object). | |
49 If OBJECT is not already compiled, we compile it, but do not | |
50 redefine OBJECT if it is a symbol." | |
51 (interactive (list (intern (completing-read "Disassemble function: " | |
52 obarray 'fboundp t)) | |
53 nil 0 t)) | |
54 (if (eq (car-safe object) 'byte-code) | |
55 (setq object (list 'lambda () object))) | |
56 (or indent (setq indent 0)) ;Default indent to zero | |
57 (save-excursion | |
58 (if (or interactive-p (null buffer)) | |
59 (with-output-to-temp-buffer "*Disassemble*" | |
60 (set-buffer "*Disassemble*") | |
61 (disassemble-internal object indent (not interactive-p))) | |
62 (set-buffer buffer) | |
63 (disassemble-internal object indent nil))) | |
64 nil) | |
65 | |
66 | |
67 (defun disassemble-internal (obj indent interactive-p) | |
68 (let ((macro 'nil) | |
69 (name 'nil) | |
70 (doc 'nil) | |
71 args) | |
72 (while (symbolp obj) | |
73 (setq name obj | |
74 obj (symbol-function obj))) | |
75 (if (subrp obj) | |
76 (error "Can't disassemble #<subr %s>" name)) | |
77 (if (eq (car-safe obj) 'macro) ;handle macros | |
78 (setq macro t | |
79 obj (cdr obj))) | |
80 (if (and (listp obj) (not (eq (car obj) 'lambda))) | |
81 (error "not a function")) | |
82 (if (consp obj) | |
83 (if (assq 'byte-code obj) | |
84 nil | |
85 (if interactive-p (message (if name | |
86 "Compiling %s's definition..." | |
87 "Compiling definition...") | |
88 name)) | |
89 (setq obj (byte-compile obj)) | |
90 (if interactive-p (message "Done compiling. Disassembling...")))) | |
91 (cond ((consp obj) | |
92 (setq obj (cdr obj)) ;throw lambda away | |
93 (setq args (car obj)) ;save arg list | |
94 (setq obj (cdr obj))) | |
95 (t | |
96 (setq args (aref obj 0)))) | |
97 (if (zerop indent) ; not a nested function | |
98 (progn | |
99 (indent-to indent) | |
100 (insert (format "byte code%s%s%s:\n" | |
101 (if (or macro name) " for" "") | |
102 (if macro " macro" "") | |
103 (if name (format " %s" name) ""))))) | |
104 (let ((doc (if (consp obj) | |
105 (and (stringp (car obj)) (car obj)) | |
106 (and (> (length obj) 4) (aref obj 4))))) | |
107 (if (and doc (stringp doc)) | |
108 (progn (and (consp obj) (setq obj (cdr obj))) | |
109 (indent-to indent) | |
110 (princ " doc: " (current-buffer)) | |
111 (if (string-match "\n" doc) | |
112 (setq doc (concat (substring doc 0 (match-beginning 0)) | |
113 " ..."))) | |
114 (insert doc "\n")))) | |
115 (indent-to indent) | |
116 (insert " args: ") | |
117 (prin1 args (current-buffer)) | |
118 (insert "\n") | |
119 (let ((interactive (cond ((consp obj) | |
120 (assq 'interactive obj)) | |
121 ((> (length obj) 5) | |
122 (list 'interactive (aref obj 5)))))) | |
123 (if interactive | |
124 (progn | |
125 (setq interactive (nth 1 interactive)) | |
126 (if (eq (car-safe (car-safe obj)) 'interactive) | |
127 (setq obj (cdr obj))) | |
128 (indent-to indent) | |
129 (insert " interactive: ") | |
130 (if (eq (car-safe interactive) 'byte-code) | |
131 (progn | |
132 (insert "\n") | |
133 (disassemble-1 interactive | |
134 (+ indent disassemble-recursive-indent))) | |
135 (let ((print-escape-newlines t)) | |
136 (prin1 interactive (current-buffer)))) | |
137 (insert "\n")))) | |
138 (cond ((and (consp obj) (assq 'byte-code obj)) | |
139 (disassemble-1 (assq 'byte-code obj) indent)) | |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1606
diff
changeset
|
140 ((byte-code-function-p obj) |
757 | 141 (disassemble-1 obj indent)) |
142 (t | |
143 (insert "Uncompiled body: ") | |
144 (let ((print-escape-newlines t)) | |
145 (prin1 (if (cdr obj) (cons 'progn obj) (car obj)) | |
146 (current-buffer)))))) | |
147 (if interactive-p | |
148 (message ""))) | |
149 | |
150 | |
151 (defun disassemble-1 (obj indent) | |
152 "Prints the byte-code call OBJ in the current buffer. | |
153 OBJ should be a call to BYTE-CODE generated by the byte compiler." | |
154 (let (bytes constvec) | |
155 (if (consp obj) | |
156 (setq bytes (car (cdr obj)) ;the byte code | |
157 constvec (car (cdr (cdr obj)))) ;constant vector | |
158 (setq bytes (aref obj 1) | |
159 constvec (aref obj 2))) | |
160 (let ((lap (byte-decompile-bytecode bytes constvec)) | |
161 op arg opname) | |
162 (let ((tagno 0) | |
163 tmp | |
164 (lap lap)) | |
165 (while (setq tmp (assq 'TAG lap)) | |
166 (setcar (cdr tmp) (setq tagno (1+ tagno))) | |
167 (setq lap (cdr (memq tmp lap))))) | |
168 (while lap | |
169 (setq op (car (car lap)) | |
170 arg (cdr (car lap))) | |
171 (indent-to indent) | |
172 (if (eq 'TAG op) | |
173 (insert (int-to-string (car arg)) ":") | |
174 | |
175 (indent-to (+ indent disassemble-column-1-indent)) | |
176 (if (and op | |
177 (string-match "^byte-" (setq opname (symbol-name op)))) | |
178 (setq opname (substring opname 5)) | |
179 (setq opname "<not-an-opcode>")) | |
180 (if (eq op 'byte-constant2) | |
181 (insert " #### shouldn't have seen constant2 here!\n ")) | |
182 (insert opname) | |
183 (indent-to (+ indent disassemble-column-1-indent | |
184 disassemble-column-2-indent | |
185 -1)) | |
186 (insert " ") | |
187 (cond ((memq op byte-goto-ops) | |
188 (insert (int-to-string (nth 1 arg)))) | |
189 ((memq op '(byte-call byte-unbind | |
190 byte-listN byte-concatN byte-insertN)) | |
191 (insert (int-to-string arg))) | |
192 ((memq op '(byte-varref byte-varset byte-varbind)) | |
193 (prin1 (car arg) (current-buffer))) | |
194 ((memq op '(byte-constant byte-constant2)) | |
195 ;; it's a constant | |
196 (setq arg (car arg)) | |
197 ;; but if the value of the constant is compiled code, then | |
198 ;; recursively disassemble it. | |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1606
diff
changeset
|
199 (cond ((or (byte-code-function-p arg) |
757 | 200 (and (eq (car-safe arg) 'lambda) |
201 (assq 'byte-code arg)) | |
202 (and (eq (car-safe arg) 'macro) | |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1606
diff
changeset
|
203 (or (byte-code-function-p (cdr arg)) |
757 | 204 (and (eq (car-safe (cdr arg)) 'lambda) |
205 (assq 'byte-code (cdr arg)))))) | |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1606
diff
changeset
|
206 (cond ((byte-code-function-p arg) |
757 | 207 (insert "<compiled-function>\n")) |
208 ((eq (car-safe arg) 'lambda) | |
209 (insert "<compiled lambda>")) | |
210 (t (insert "<compiled macro>\n"))) | |
211 (disassemble-internal | |
212 arg | |
213 (+ indent disassemble-recursive-indent 1) | |
214 nil)) | |
215 ((eq (car-safe arg) 'byte-code) | |
216 (insert "<byte code>\n") | |
217 (disassemble-1 ;recurse on byte-code object | |
218 arg | |
219 (+ indent disassemble-recursive-indent))) | |
220 ((eq (car-safe (car-safe arg)) 'byte-code) | |
221 (insert "(<byte code>...)\n") | |
222 (mapcar ;recurse on list of byte-code objects | |
223 '(lambda (obj) | |
224 (disassemble-1 | |
225 obj | |
226 (+ indent disassemble-recursive-indent))) | |
227 arg)) | |
228 (t | |
229 ;; really just a constant | |
230 (let ((print-escape-newlines t)) | |
231 (prin1 arg (current-buffer)))))) | |
232 ) | |
233 (insert "\n")) | |
234 (setq lap (cdr lap))))) | |
235 nil) | |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
236 |
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
806
diff
changeset
|
237 ;;; disass.el ends here |