comparison lisp/bytecomp/disass.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children 0293115a14e9
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
1 ;;; disass.el --- disassembler for compiled Emacs Lisp code
2
3 ;;; Copyright (C) 1986, 1991-1994 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 XEmacs.
11
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)
15 ;; any later version.
16
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.
21
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 Free
24 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 ;;; Synched up with: FSF 19.28.
27
28 ;;; Commentary:
29
30 ;; The single entry point, `disassemble', disassembles a code object generated
31 ;; by the Emacs Lisp byte-compiler. This doesn't invert the compilation
32 ;; operation, not by a long shot, but it's useful for debugging.
33
34 ;;
35 ;; Original version by Doug Cutting (doug@csli.stanford.edu)
36 ;; Substantially modified by Jamie Zawinski <jwz@lucid.com> for
37 ;; the new lapcode-based byte compiler.
38
39 ;;; Code:
40
41 ;;; The variable byte-code-vector is defined by the new bytecomp.el.
42 ;;; The function byte-decompile-lapcode is defined in byte-opt.el.
43 ;;; Since we don't use byte-decompile-lapcode, let's try not loading byte-opt.
44 ;;; The variable byte-code-vector is defined by the new bytecomp.el.
45 ;;; The function byte-decompile-lapcode is defined in byte-optimize.el.
46 (require 'byte-optimize)
47
48 (defvar disassemble-column-1-indent 8 "*")
49 (defvar disassemble-column-2-indent 10 "*")
50 (defvar disassemble-recursive-indent 3 "*")
51
52
53 ;;;###autoload
54 (defun disassemble (object &optional buffer indent interactive-p)
55 "Print disassembled code for OBJECT in (optional) BUFFER.
56 OBJECT can be a symbol defined as a function, or a function itself
57 \(a lambda expression or a compiled-function object).
58 If OBJECT is not already compiled, we compile it, but do not
59 redefine OBJECT if it is a symbol."
60 (interactive (list (intern (completing-read "Disassemble function: "
61 obarray 'fboundp t))
62 nil 0 t))
63 (if (eq (car-safe object) 'byte-code)
64 (setq object (list 'lambda () object)))
65 (or indent (setq indent 0)) ;Default indent to zero
66 (save-excursion
67 (if (or interactive-p (null buffer))
68 (with-output-to-temp-buffer "*Disassemble*"
69 (set-buffer "*Disassemble*")
70 (disassemble-internal object indent (not interactive-p)))
71 (set-buffer buffer)
72 (disassemble-internal object indent nil)))
73 nil)
74
75
76 (defun disassemble-internal (obj indent interactive-p)
77 (let ((macro 'nil)
78 (name 'nil)
79 args)
80 (while (symbolp obj)
81 (setq name obj
82 obj (symbol-function obj)))
83 (if (subrp obj)
84 (error "Can't disassemble #<subr %s>" name))
85 (if (eq (car-safe obj) 'autoload)
86 (progn
87 (load (elt obj 1))
88 (setq obj (symbol-function name))))
89 (if (eq (car-safe obj) 'macro) ;handle macros
90 (setq macro t
91 obj (cdr obj)))
92 (if (and (listp obj) (eq (car obj) 'byte-code))
93 (setq obj (list 'lambda nil obj)))
94 (if (and (listp obj) (not (eq (car obj) 'lambda)))
95 (error "not a function"))
96 (if (consp obj)
97 (if (assq 'byte-code obj)
98 nil
99 (if interactive-p (message (if name
100 "Compiling %s's definition..."
101 "Compiling definition...")
102 name))
103 (setq obj (byte-compile obj))
104 (if interactive-p (message "Done compiling. Disassembling..."))))
105 (cond ((consp obj)
106 (setq obj (cdr obj)) ;throw lambda away
107 (setq args (car obj)) ;save arg list
108 (setq obj (cdr obj)))
109 (t
110 (setq args (compiled-function-arglist obj))))
111 (if (zerop indent) ; not a nested function
112 (progn
113 (indent-to indent)
114 (insert (format "byte code%s%s%s:\n"
115 (if (or macro name) " for" "")
116 (if macro " macro" "")
117 (if name (format " %s" name) "")))))
118 (let ((doc (if (consp obj)
119 (and (stringp (car obj)) (car obj))
120 (condition-case error
121 (documentation obj)
122 (error (format "%S" error))))))
123 (if (and doc (stringp doc))
124 (progn (and (consp obj) (setq obj (cdr obj)))
125 (indent-to indent)
126 (princ " doc: " (current-buffer))
127 (let ((frobbed nil))
128 (if (string-match "\n" doc)
129 (setq doc (substring doc 0 (match-beginning 0))
130 frobbed t))
131 (if (> (length doc) 70)
132 (setq doc (substring doc 0 65) frobbed t))
133 (if frobbed (setq doc (concat doc " ..."))))
134 (insert doc "\n"))))
135 (indent-to indent)
136 (insert " args: ")
137 (prin1 args (current-buffer))
138 (insert "\n")
139 (if (condition-case ()
140 (commandp obj) ; ie interactivep
141 (error nil))
142 (let ((interactive (if (consp obj)
143 (elt (assq 'interactive obj) 1)
144 (elt (compiled-function-interactive obj) 1))))
145 (if (eq (car-safe (car-safe obj)) 'interactive)
146 (setq obj (cdr obj)))
147 (indent-to indent)
148 (insert " interactive: ")
149 (if (eq (car-safe interactive) 'byte-code)
150 (progn
151 (insert "\n")
152 (disassemble-1 interactive
153 (+ indent disassemble-recursive-indent)))
154 (let ((print-escape-newlines t))
155 (prin1 interactive (current-buffer))))
156 (insert "\n")))
157 (cond ((and (consp obj) (assq 'byte-code obj))
158 (disassemble-1 (assq 'byte-code obj) indent))
159 ((compiled-function-p obj)
160 (disassemble-1 obj indent))
161 (t
162 (insert "Uncompiled body: ")
163 (let ((print-escape-newlines t))
164 (prin1 (if (cdr obj) (cons 'progn obj) (car obj))
165 (current-buffer))))))
166 (if interactive-p
167 (message nil)))
168
169
170 (defun disassemble-1 (obj indent)
171 "Prints the byte-code call OBJ in the current buffer.
172 OBJ should be a call to BYTE-CODE generated by the byte compiler."
173 (let (bytes constvec)
174 (if (consp obj)
175 (setq bytes (car (cdr obj)) ; the byte code
176 constvec (car (cdr (cdr obj)))) ; constant vector
177 (setq bytes (elt obj 1)
178 constvec (elt obj 2)))
179 (let ((lap (byte-decompile-bytecode bytes constvec))
180 op arg opname pc-value)
181 (let ((tagno 0)
182 tmp
183 (lap lap))
184 (while (setq tmp (assq 'TAG lap))
185 (setcar (cdr tmp) (setq tagno (1+ tagno)))
186 (setq lap (cdr (memq tmp lap)))))
187 (while lap
188 ;; Take off the pc value of the next thing
189 ;; and put it in pc-value.
190 (setq pc-value nil)
191 (if (numberp (car lap))
192 (setq pc-value (car lap)
193 lap (cdr lap)))
194 ;; Fetch the next op and its arg.
195 (setq op (car (car lap))
196 arg (cdr (car lap)))
197 (setq lap (cdr lap))
198 (indent-to indent)
199 (if (eq 'TAG op)
200 (progn
201 ;; We have a label. Display it, but first its pc value.
202 (if pc-value
203 (insert (format "%d:" pc-value)))
204 (insert (int-to-string (car arg))))
205 ;; We have an instruction. Display its pc value first.
206 (if pc-value
207 (insert (format "%d" pc-value)))
208 (indent-to (+ indent disassemble-column-1-indent))
209 (if (and op
210 (string-match "^byte-" (setq opname (symbol-name op))))
211 (setq opname (substring opname 5))
212 (setq opname "<not-an-opcode>"))
213 (if (eq op 'byte-constant2)
214 (insert " #### shouldn't have seen constant2 here!\n "))
215 (insert opname)
216 (indent-to (+ indent disassemble-column-1-indent
217 disassemble-column-2-indent
218 -1))
219 (insert " ")
220 (cond ((memq op byte-goto-ops)
221 (insert (int-to-string (nth 1 arg))))
222 ((memq op '(byte-call byte-unbind
223 byte-listN byte-concatN byte-insertN))
224 (insert (int-to-string arg)))
225 ((memq op '(byte-varref byte-varset byte-varbind))
226 (prin1 (car arg) (current-buffer)))
227 ((memq op '(byte-constant byte-constant2))
228 ;; it's a constant
229 (setq arg (car arg))
230 ;; but if the value of the constant is compiled code, then
231 ;; recursively disassemble it.
232 (cond ((or (compiled-function-p arg)
233 (and (eq (car-safe arg) 'lambda)
234 (assq 'byte-code arg))
235 (and (eq (car-safe arg) 'macro)
236 (or (compiled-function-p (cdr arg))
237 (and (eq (car-safe (cdr arg)) 'lambda)
238 (assq 'byte-code (cdr arg))))))
239 (cond ((compiled-function-p arg)
240 (insert "<compiled-function>\n"))
241 ((eq (car-safe arg) 'lambda)
242 (insert "<compiled lambda>"))
243 (t (insert "<compiled macro>\n")))
244 (disassemble-internal
245 arg
246 (+ indent disassemble-recursive-indent 1)
247 nil))
248 ((eq (car-safe arg) 'byte-code)
249 (insert "<byte code>\n")
250 (disassemble-1 ;recurse on byte-code object
251 arg
252 (+ indent disassemble-recursive-indent)))
253 ((eq (car-safe (car-safe arg)) 'byte-code)
254 (insert "(<byte code>...)\n")
255 (mapcar ;recurse on list of byte-code objects
256 '(lambda (obj)
257 (disassemble-1
258 obj
259 (+ indent disassemble-recursive-indent)))
260 arg))
261 (t
262 ;; really just a constant
263 (let ((print-escape-newlines t))
264 (prin1 arg (current-buffer))))))
265 )
266 (insert "\n")))))
267 nil)