1 ;;; disass.el --- disassembler for compiled Emacs Lisp code
3 ;;; Copyright (C) 1986, 1991-1994 Free Software Foundation, Inc.
5 ;; Author: Doug Cutting <doug@csli.stanford.edu>
6 ;; Jamie Zawinski <jwz@netscape.com>
7 ;; Maintainer: Jamie Zawinski <jwz@netscape.com>
10 ;; This file is part of XEmacs.
12 ;; XEmacs is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; XEmacs is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with XEmacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Synched up with: FSF 19.28.
31 ;; The single entry point, `disassemble', disassembles a code object generated
32 ;; by the Emacs Lisp byte-compiler. This doesn't invert the compilation
33 ;; operation, not by a long shot, but it's useful for debugging.
36 ;; Original version by Doug Cutting (doug@csli.stanford.edu)
37 ;; Substantially modified by Jamie Zawinski for
38 ;; the new lapcode-based byte compiler.
42 ;;; The variable byte-code-vector is defined by the new bytecomp.el.
43 ;;; The function byte-decompile-lapcode is defined in byte-opt.el.
44 ;;; Since we don't use byte-decompile-lapcode, let's try not loading byte-opt.
45 ;;; The variable byte-code-vector is defined by the new bytecomp.el.
46 ;;; The function byte-decompile-lapcode is defined in byte-optimize.el.
47 (require 'byte-optimize)
49 (defvar disassemble-column-1-indent 8 "*")
50 (defvar disassemble-column-2-indent 10 "*")
51 (defvar disassemble-recursive-indent 3 "*")
55 (defun disassemble (object &optional buffer indent interactive-p)
56 "Print disassembled code for OBJECT in (optional) BUFFER.
57 OBJECT can be a symbol defined as a function, or a function itself
58 \(a lambda expression or a compiled-function object).
59 If OBJECT is not already compiled, we compile it, but do not
60 redefine OBJECT if it is a symbol."
61 (interactive (list (intern (completing-read "Disassemble function: "
64 (if (eq (car-safe object) 'byte-code)
65 (setq object (list 'lambda () object)))
66 (or indent (setq indent 0)) ;Default indent to zero
68 (if (or interactive-p (null buffer))
69 (with-output-to-temp-buffer "*Disassemble*"
70 (set-buffer "*Disassemble*")
71 (disassemble-internal object indent (not interactive-p)))
73 (disassemble-internal object indent nil)))
77 (defun disassemble-internal (obj indent interactive-p)
83 obj (symbol-function obj)))
85 (error "Can't disassemble #<subr %s>" name))
86 (if (eq (car-safe obj) 'autoload)
89 (setq obj (symbol-function name))))
90 (if (eq (car-safe obj) 'macro) ;handle macros
93 (if (and (listp obj) (eq (car obj) 'byte-code))
94 (setq obj (list 'lambda nil obj)))
95 (if (and (listp obj) (not (eq (car obj) 'lambda)))
96 (error "not a function"))
98 (if (assq 'byte-code obj)
100 (if interactive-p (message (if name
101 "Compiling %s's definition..."
102 "Compiling definition...")
104 (setq obj (byte-compile obj))
105 (if interactive-p (message "Done compiling. Disassembling..."))))
107 (setq obj (cdr obj)) ;throw lambda away
108 (setq args (car obj)) ;save arg list
109 (setq obj (cdr obj)))
111 (setq args (compiled-function-arglist obj))))
112 (if (zerop indent) ; not a nested function
115 (insert (format "byte code%s%s%s:\n"
116 (if (or macro name) " for" "")
117 (if macro " macro" "")
118 (if name (format " %s" name) "")))))
119 (let ((doc (if (consp obj)
120 (and (stringp (car obj)) (car obj))
121 (condition-case error
123 (error (format "%S" error))))))
124 (if (and doc (stringp doc))
125 (progn (and (consp obj) (setq obj (cdr obj)))
127 (princ " doc: " (current-buffer))
129 (if (string-match "\n" doc)
130 (setq doc (substring doc 0 (match-beginning 0))
132 (if (> (length doc) 70)
133 (setq doc (substring doc 0 65) frobbed t))
134 (if frobbed (setq doc (concat doc " ..."))))
138 (prin1 args (current-buffer))
140 (if (condition-case ()
141 (commandp obj) ; ie interactivep
143 (let ((interactive (if (consp obj)
144 (elt (assq 'interactive obj) 1)
145 (elt (compiled-function-interactive obj) 1))))
146 (if (eq (car-safe (car-safe obj)) 'interactive)
147 (setq obj (cdr obj)))
149 (insert " interactive: ")
150 (if (eq (car-safe interactive) 'byte-code)
153 (disassemble-1 interactive
154 (+ indent disassemble-recursive-indent)))
155 (let ((print-escape-newlines t))
156 (prin1 interactive (current-buffer))))
158 (cond ((and (consp obj) (assq 'byte-code obj))
159 (disassemble-1 (assq 'byte-code obj) indent))
160 ((compiled-function-p obj)
161 (disassemble-1 obj indent))
163 (insert "Uncompiled body: ")
164 (let ((print-escape-newlines t))
165 (prin1 (if (cdr obj) (cons 'progn obj) (car obj))
166 (current-buffer))))))
171 (defun disassemble-1 (obj indent)
172 "Prints the byte-code call OBJ in the current buffer.
173 OBJ should be a call to BYTE-CODE generated by the byte compiler."
174 (let (bytes constvec)
176 (setq bytes (car (cdr obj)) ; the byte code
177 constvec (car (cdr (cdr obj)))) ; constant vector
178 (setq bytes (compiled-function-instructions obj)
179 constvec (compiled-function-constants obj)))
180 (let ((lap (byte-decompile-bytecode bytes constvec))
181 op arg opname pc-value)
185 (while (setq tmp (assq 'TAG lap))
186 (setcar (cdr tmp) (setq tagno (1+ tagno)))
187 (setq lap (cdr (memq tmp lap)))))
189 ;; Take off the pc value of the next thing
190 ;; and put it in pc-value.
192 (if (numberp (car lap))
193 (setq pc-value (car lap)
195 ;; Fetch the next op and its arg.
196 (setq op (car (car lap))
202 ;; We have a label. Display it, but first its pc value.
204 (insert (format "%d:" pc-value)))
205 (insert (int-to-string (car arg))))
206 ;; We have an instruction. Display its pc value first.
208 (insert (format "%d" pc-value)))
209 (indent-to (+ indent disassemble-column-1-indent))
211 (string-match "^byte-" (setq opname (symbol-name op))))
212 (setq opname (substring opname 5))
213 (setq opname "<not-an-opcode>"))
214 (if (eq op 'byte-constant2)
215 (insert " #### shouldn't have seen constant2 here!\n "))
217 (indent-to (+ indent disassemble-column-1-indent
218 disassemble-column-2-indent
221 (cond ((memq op byte-goto-ops)
222 (insert (int-to-string (nth 1 arg))))
223 ((memq op '(byte-call byte-unbind
224 byte-listN byte-concatN byte-insertN))
225 (insert (int-to-string arg)))
226 ((memq op '(byte-varref byte-varset byte-varbind))
227 (prin1 (car arg) (current-buffer)))
228 ((memq op '(byte-constant byte-constant2))
231 ;; but if the value of the constant is compiled code, then
232 ;; recursively disassemble it.
233 (cond ((or (compiled-function-p arg)
234 (and (eq (car-safe arg) 'lambda)
235 (assq 'byte-code arg))
236 (and (eq (car-safe arg) 'macro)
237 (or (compiled-function-p (cdr arg))
238 (and (eq (car-safe (cdr arg)) 'lambda)
239 (assq 'byte-code (cdr arg))))))
240 (cond ((compiled-function-p arg)
241 (insert "<compiled-function>\n"))
242 ((eq (car-safe arg) 'lambda)
243 (insert "<compiled lambda>"))
244 (t (insert "<compiled macro>\n")))
245 (disassemble-internal
247 (+ indent disassemble-recursive-indent 1)
249 ((eq (car-safe arg) 'byte-code)
250 (insert "<byte code>\n")
251 (disassemble-1 ;recurse on byte-code object
253 (+ indent disassemble-recursive-indent)))
254 ((eq (car-safe (car-safe arg)) 'byte-code)
255 (insert "(<byte code>...)\n")
256 (mapcar ;recurse on list of byte-code objects
260 (+ indent disassemble-recursive-indent)))
263 ;; really just a constant
264 (let ((print-escape-newlines t))
265 (prin1 arg (current-buffer))))))
272 ;;; disass.el ends here