]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/disass.el
(byte-compile, byte-compile-keep-pending)
[gnu-emacs] / lisp / emacs-lisp / disass.el
1 ;;; disass.el --- disassembler for compiled Emacs Lisp code
2
3 ;;; Copyright (C) 1986, 1991 Free Software Foundation, Inc.
4
5 ;; Author: Doug Cutting <doug@csli.stanford.edu>
6 ;; Jamie Zawinski <jwz@lucid.com>
7 ;; Maintainer: Jamie Zawinski <jwz@lucid.com>
8 ;; Keywords: internal
9
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
14 ;; the Free Software Foundation; either version 2, or (at your option)
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
26 ;;; Commentary:
27
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.
36
37 ;;; Code:
38
39 ;;; The variable byte-code-vector is defined by the new bytecomp.el.
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.
42 (require 'byte-compile "bytecomp")
43
44 (defvar disassemble-column-1-indent 5 "*")
45 (defvar disassemble-column-2-indent 10 "*")
46
47 (defvar disassemble-recursive-indent 3 "*")
48
49 ;;;###autoload
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))
82 (if (eq (car-safe obj) 'macro) ;handle macros
83 (setq macro t
84 obj (cdr obj)))
85 (if (and (listp obj) (not (eq (car obj) 'lambda)))
86 (error "not a function"))
87 (if (consp obj)
88 (if (assq 'byte-code obj)
89 nil
90 (if interactive-p (message (if name
91 "Compiling %s's definition..."
92 "Compiling definition...")
93 name))
94 (setq obj (byte-compile obj))
95 (if interactive-p (message "Done compiling. Disassembling..."))))
96 (cond ((consp obj)
97 (setq obj (cdr obj)) ;throw lambda away
98 (setq args (car obj)) ;save arg list
99 (setq obj (cdr obj)))
100 (t
101 (setq args (aref obj 0))))
102 (if (zerop indent) ; not a nested function
103 (progn
104 (indent-to indent)
105 (insert (format "byte code%s%s%s:\n"
106 (if (or macro name) " for" "")
107 (if macro " macro" "")
108 (if name (format " %s" name) "")))))
109 (let ((doc (if (consp obj)
110 (and (stringp (car obj)) (car obj))
111 (and (> (length obj) 4) (aref obj 4)))))
112 (if (and doc (stringp doc))
113 (progn (and (consp obj) (setq obj (cdr obj)))
114 (indent-to indent)
115 (princ " doc: " (current-buffer))
116 (if (string-match "\n" doc)
117 (setq doc (concat (substring doc 0 (match-beginning 0))
118 " ...")))
119 (insert doc "\n"))))
120 (indent-to indent)
121 (insert " args: ")
122 (prin1 args (current-buffer))
123 (insert "\n")
124 (let ((interactive (cond ((consp obj)
125 (assq 'interactive obj))
126 ((> (length obj) 5)
127 (list 'interactive (aref obj 5))))))
128 (if interactive
129 (progn
130 (setq interactive (nth 1 interactive))
131 (if (eq (car-safe (car-safe obj)) 'interactive)
132 (setq obj (cdr obj)))
133 (indent-to indent)
134 (insert " interactive: ")
135 (if (eq (car-safe interactive) 'byte-code)
136 (progn
137 (insert "\n")
138 (disassemble-1 interactive
139 (+ indent disassemble-recursive-indent)))
140 (let ((print-escape-newlines t))
141 (prin1 interactive (current-buffer))))
142 (insert "\n"))))
143 (cond ((and (consp obj) (assq 'byte-code obj))
144 (disassemble-1 (assq 'byte-code obj) indent))
145 ((byte-code-function-p obj)
146 (disassemble-1 obj indent))
147 (t
148 (insert "Uncompiled body: ")
149 (let ((print-escape-newlines t))
150 (prin1 (if (cdr obj) (cons 'progn obj) (car obj))
151 (current-buffer))))))
152 (if interactive-p
153 (message "")))
154
155
156 (defun disassemble-1 (obj indent)
157 "Prints the byte-code call OBJ in the current buffer.
158 OBJ should be a call to BYTE-CODE generated by the byte compiler."
159 (let (bytes constvec)
160 (if (consp obj)
161 (setq bytes (car (cdr obj)) ;the byte code
162 constvec (car (cdr (cdr obj)))) ;constant vector
163 (setq bytes (aref obj 1)
164 constvec (aref obj 2)))
165 (let ((lap (byte-decompile-bytecode bytes constvec))
166 op arg opname)
167 (let ((tagno 0)
168 tmp
169 (lap lap))
170 (while (setq tmp (assq 'TAG lap))
171 (setcar (cdr tmp) (setq tagno (1+ tagno)))
172 (setq lap (cdr (memq tmp lap)))))
173 (while lap
174 (setq op (car (car lap))
175 arg (cdr (car lap)))
176 (indent-to indent)
177 (if (eq 'TAG op)
178 (insert (int-to-string (car arg)) ":")
179
180 (indent-to (+ indent disassemble-column-1-indent))
181 (if (and op
182 (string-match "^byte-" (setq opname (symbol-name op))))
183 (setq opname (substring opname 5))
184 (setq opname "<not-an-opcode>"))
185 (if (eq op 'byte-constant2)
186 (insert " #### shouldn't have seen constant2 here!\n "))
187 (insert opname)
188 (indent-to (+ indent disassemble-column-1-indent
189 disassemble-column-2-indent
190 -1))
191 (insert " ")
192 (cond ((memq op byte-goto-ops)
193 (insert (int-to-string (nth 1 arg))))
194 ((memq op '(byte-call byte-unbind
195 byte-listN byte-concatN byte-insertN))
196 (insert (int-to-string arg)))
197 ((memq op '(byte-varref byte-varset byte-varbind))
198 (prin1 (car arg) (current-buffer)))
199 ((memq op '(byte-constant byte-constant2))
200 ;; it's a constant
201 (setq arg (car arg))
202 ;; but if the value of the constant is compiled code, then
203 ;; recursively disassemble it.
204 (cond ((or (byte-code-function-p arg)
205 (and (eq (car-safe arg) 'lambda)
206 (assq 'byte-code arg))
207 (and (eq (car-safe arg) 'macro)
208 (or (byte-code-function-p (cdr arg))
209 (and (eq (car-safe (cdr arg)) 'lambda)
210 (assq 'byte-code (cdr arg))))))
211 (cond ((byte-code-function-p arg)
212 (insert "<compiled-function>\n"))
213 ((eq (car-safe arg) 'lambda)
214 (insert "<compiled lambda>"))
215 (t (insert "<compiled macro>\n")))
216 (disassemble-internal
217 arg
218 (+ indent disassemble-recursive-indent 1)
219 nil))
220 ((eq (car-safe arg) 'byte-code)
221 (insert "<byte code>\n")
222 (disassemble-1 ;recurse on byte-code object
223 arg
224 (+ indent disassemble-recursive-indent)))
225 ((eq (car-safe (car-safe arg)) 'byte-code)
226 (insert "(<byte code>...)\n")
227 (mapcar ;recurse on list of byte-code objects
228 '(lambda (obj)
229 (disassemble-1
230 obj
231 (+ indent disassemble-recursive-indent)))
232 arg))
233 (t
234 ;; really just a constant
235 (let ((print-escape-newlines t))
236 (prin1 arg (current-buffer))))))
237 )
238 (insert "\n"))
239 (setq lap (cdr lap)))))
240 nil)
241
242 ;;; disass.el ends here