XEmacs 21.2-b1
[chise/xemacs-chise.git.1] / lisp / disass.el
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@netscape.com>
7 ;; Maintainer: Jamie Zawinski <jwz@netscape.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 
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Synched up with: FSF 19.28.
28
29 ;;; Commentary:
30
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.
34
35 ;;
36 ;; Original version by Doug Cutting (doug@csli.stanford.edu)
37 ;; Substantially modified by Jamie Zawinski for
38 ;; the new lapcode-based byte compiler.
39
40 ;;; Code:
41
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)
48
49 (defvar disassemble-column-1-indent 8 "*")
50 (defvar disassemble-column-2-indent 10 "*")
51 (defvar disassemble-recursive-indent 3 "*")
52
53
54 ;;;###autoload
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: "
62                                               obarray 'fboundp t))
63                      nil 0 t))
64   (if (eq (car-safe object) 'byte-code)
65       (setq object (list 'lambda () object)))
66   (or indent (setq indent 0))           ;Default indent to zero
67   (save-excursion
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)))
72       (set-buffer buffer)
73       (disassemble-internal object indent nil)))
74   nil)
75
76
77 (defun disassemble-internal (obj indent interactive-p)
78   (let ((macro 'nil)
79         (name 'nil)
80         args)
81     (while (symbolp obj)
82       (setq name obj
83             obj (symbol-function obj)))
84     (if (subrp obj)
85         (error "Can't disassemble #<subr %s>" name))
86     (if (eq (car-safe obj) 'autoload)
87         (progn
88           (load (elt obj 1))
89           (setq obj (symbol-function name))))
90     (if (eq (car-safe obj) 'macro)      ;handle macros
91         (setq macro t
92               obj (cdr obj)))
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"))
97     (if (consp obj)
98         (if (assq 'byte-code obj)
99             nil
100           (if interactive-p (message (if name
101                                          "Compiling %s's definition..."
102                                        "Compiling definition...")
103                                      name))
104           (setq obj (byte-compile obj))
105           (if interactive-p (message "Done compiling.  Disassembling..."))))
106     (cond ((consp obj)
107            (setq obj (cdr obj))         ;throw lambda away
108            (setq args (car obj))        ;save arg list
109            (setq obj (cdr obj)))
110           (t
111            (setq args (compiled-function-arglist obj))))
112     (if (zerop indent)                  ; not a nested function
113         (progn
114           (indent-to indent)
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
122                      (documentation obj)
123                    (error (format "%S" error))))))
124       (if (and doc (stringp doc))
125           (progn (and (consp obj) (setq obj (cdr obj)))
126                  (indent-to indent)
127                  (princ "  doc:  " (current-buffer))
128                  (let ((frobbed nil))
129                    (if (string-match "\n" doc)
130                        (setq doc (substring doc 0 (match-beginning 0))
131                              frobbed t))
132                    (if (> (length doc) 70)
133                        (setq doc (substring doc 0 65) frobbed t))
134                    (if frobbed (setq doc (concat doc " ..."))))
135                  (insert doc "\n"))))
136     (indent-to indent)
137     (insert "  args: ")
138     (prin1 args (current-buffer))
139     (insert "\n")
140     (if (condition-case ()
141             (commandp obj)                  ; ie interactivep
142           (error nil))
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)))
148           (indent-to indent)
149           (insert " interactive: ")
150           (if (eq (car-safe interactive) 'byte-code)
151               (progn
152                 (insert "\n")
153                 (disassemble-1 interactive
154                                (+ indent disassemble-recursive-indent)))
155             (let ((print-escape-newlines t))
156               (prin1 interactive (current-buffer))))
157           (insert "\n")))
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))
162           (t
163            (insert "Uncompiled body:  ")
164            (let ((print-escape-newlines t))
165              (prin1 (if (cdr obj) (cons 'progn obj) (car obj))
166                     (current-buffer))))))
167   (if interactive-p
168       (message nil)))
169
170
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)
175     (if (consp obj)
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)
182       (let ((tagno 0)
183             tmp
184             (lap lap))
185         (while (setq tmp (assq 'TAG lap))
186           (setcar (cdr tmp) (setq tagno (1+ tagno)))
187           (setq lap (cdr (memq tmp lap)))))
188       (while lap
189         ;; Take off the pc value of the next thing
190         ;; and put it in pc-value.
191         (setq pc-value nil)
192         (if (numberp (car lap))
193             (setq pc-value (car lap)
194                   lap (cdr lap)))
195         ;; Fetch the next op and its arg.
196         (setq op (car (car lap))
197               arg (cdr (car lap)))
198         (setq lap (cdr lap))
199         (indent-to indent)
200         (if (eq 'TAG op)
201             (progn
202               ;; We have a label.  Display it, but first its pc value.
203               (if 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.
207           (if pc-value
208               (insert (format "%d" pc-value)))
209           (indent-to (+ indent disassemble-column-1-indent))
210           (if (and op
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  "))
216           (insert opname)
217           (indent-to (+ indent disassemble-column-1-indent
218                         disassemble-column-2-indent
219                         -1))
220           (insert " ")
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))
229                  ;; it's a constant
230                  (setq arg (car arg))
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
246                          arg
247                          (+ indent disassemble-recursive-indent 1)
248                          nil))
249                        ((eq (car-safe arg) 'byte-code)
250                         (insert "<byte code>\n")
251                         (disassemble-1  ;recurse on byte-code object
252                          arg
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
257                          '(lambda (obj)
258                             (disassemble-1
259                              obj
260                              (+ indent disassemble-recursive-indent)))
261                          arg))
262                        (t
263                         ;; really just a constant
264                         (let ((print-escape-newlines t))
265                           (prin1 arg (current-buffer))))))
266                 )
267           (insert "\n")))))
268   nil)
269
270 (provide 'disass)
271
272 ;;; disass.el ends here