]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/disass.el
12cf605cce91030cae2952479b304c2fb283037a
[gnu-emacs] / lisp / emacs-lisp / disass.el
1 ;;; disass.el --- disassembler for compiled Emacs Lisp code -*- lexical-binding:t -*-
2
3 ;; Copyright (C) 1986, 1991, 2002-2015 Free Software Foundation, Inc.
4
5 ;; Author: Doug Cutting <doug@csli.stanford.edu>
6 ;; Jamie Zawinski <jwz@lucid.com>
7 ;; Maintainer: emacs-devel@gnu.org
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 3 of the License, or
15 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; The single entry point, `disassemble', disassembles a code object generated
28 ;; by the Emacs Lisp byte-compiler. This doesn't invert the compilation
29 ;; operation, not by a long shot, but it's useful for debugging.
30
31 ;;
32 ;; Original version by Doug Cutting (doug@csli.stanford.edu)
33 ;; Substantially modified by Jamie Zawinski <jwz@lucid.com> for
34 ;; the new lapcode-based byte compiler.
35
36 ;;; Code:
37
38 (require 'macroexp)
39
40 ;; The variable byte-code-vector is defined by the new bytecomp.el.
41 ;; The function byte-decompile-lapcode is defined in byte-opt.el.
42 ;; Since we don't use byte-decompile-lapcode, let's try not loading byte-opt.
43 (require 'byte-compile "bytecomp")
44
45 (defvar disassemble-column-1-indent 8 "*")
46 (defvar disassemble-column-2-indent 10 "*")
47
48 (defvar disassemble-recursive-indent 3 "*")
49
50 ;;;###autoload
51 (defun disassemble (object &optional buffer indent interactive-p)
52 "Print disassembled code for OBJECT in (optional) BUFFER.
53 OBJECT can be a symbol defined as a function, or a function itself
54 \(a lambda expression or a compiled-function object).
55 If OBJECT is not already compiled, we compile it, but do not
56 redefine OBJECT if it is a symbol."
57 (interactive (list (intern (completing-read "Disassemble function: "
58 obarray 'fboundp t))
59 nil 0 t))
60 (if (and (consp object) (not (functionp object)))
61 (setq object `(lambda () ,object)))
62 (or indent (setq indent 0)) ;Default indent to zero
63 (save-excursion
64 (if (or interactive-p (null buffer))
65 (with-output-to-temp-buffer "*Disassemble*"
66 (set-buffer "*Disassemble*")
67 (disassemble-internal object indent (not interactive-p)))
68 (set-buffer buffer)
69 (disassemble-internal object indent nil)))
70 nil)
71
72
73 (defun disassemble-internal (obj indent interactive-p)
74 (let ((macro 'nil)
75 (name (when (symbolp obj)
76 (prog1 obj
77 (setq obj (indirect-function obj)))))
78 args)
79 (setq obj (autoload-do-load obj name))
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 (eq (car-safe obj) 'byte-code)
86 (setq obj `(lambda () ,obj)))
87 (when (consp obj)
88 (unless (functionp obj) (error "not a function"))
89 (if (assq 'byte-code obj)
90 nil
91 (if interactive-p (message (if name
92 "Compiling %s's definition..."
93 "Compiling definition...")
94 name))
95 (setq obj (byte-compile obj))
96 (if interactive-p (message "Done compiling. Disassembling..."))))
97 (cond ((consp obj)
98 (setq args (help-function-arglist obj)) ;save arg list
99 (setq obj (cdr obj)) ;throw lambda away
100 (setq obj (cdr obj)))
101 ((byte-code-function-p obj)
102 (setq args (help-function-arglist obj)))
103 (t (error "Compilation failed")))
104 (if (zerop indent) ; not a nested function
105 (progn
106 (indent-to indent)
107 (insert (format "byte code%s%s%s:\n"
108 (if (or macro name) " for" "")
109 (if macro " macro" "")
110 (if name (format " %s" name) "")))))
111 (let ((doc (if (consp obj)
112 (and (stringp (car obj)) (car obj))
113 ;; Use documentation to get lazy-loaded doc string
114 (documentation obj t))))
115 (if (and doc (stringp doc))
116 (progn (and (consp obj) (setq obj (cdr obj)))
117 (indent-to indent)
118 (princ " doc: " (current-buffer))
119 (if (string-match "\n" doc)
120 (setq doc (concat (substring doc 0 (match-beginning 0))
121 " ...")))
122 (insert doc "\n"))))
123 (indent-to indent)
124 (insert " args: ")
125 (prin1 args (current-buffer))
126 (insert "\n")
127 (let ((interactive (interactive-form obj)))
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 (macroexp-progn 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 ;; If it is lazy-loaded, load it now
164 (fetch-bytecode obj)
165 (setq bytes (aref obj 1)
166 constvec (aref obj 2)))
167 (let ((lap (byte-decompile-bytecode (string-as-unibyte bytes) constvec))
168 op arg opname pc-value)
169 (let ((tagno 0)
170 tmp
171 (lap lap))
172 (while (setq tmp (assq 'TAG lap))
173 (setcar (cdr tmp) (setq tagno (1+ tagno)))
174 (setq lap (cdr (memq tmp lap)))))
175 (while lap
176 ;; Take off the pc value of the next thing
177 ;; and put it in pc-value.
178 (setq pc-value nil)
179 (if (numberp (car lap))
180 (setq pc-value (car lap)
181 lap (cdr lap)))
182 ;; Fetch the next op and its arg.
183 (setq op (car (car lap))
184 arg (cdr (car lap)))
185 (setq lap (cdr lap))
186 (indent-to indent)
187 (if (eq 'TAG op)
188 (progn
189 ;; We have a label. Display it, but first its pc value.
190 (if pc-value
191 (insert (format "%d:" pc-value)))
192 (insert (int-to-string (car arg))))
193 ;; We have an instruction. Display its pc value first.
194 (if pc-value
195 (insert (format "%d" pc-value)))
196 (indent-to (+ indent disassemble-column-1-indent))
197 (if (and op
198 (string-match "^byte-" (setq opname (symbol-name op))))
199 (setq opname (substring opname 5))
200 (setq opname "<not-an-opcode>"))
201 (if (eq op 'byte-constant2)
202 (insert " #### shouldn't have seen constant2 here!\n "))
203 (insert opname)
204 (indent-to (+ indent disassemble-column-1-indent
205 disassemble-column-2-indent
206 -1))
207 (insert " ")
208 (cond ((memq op byte-goto-ops)
209 (insert (int-to-string (nth 1 arg))))
210 ((memq op '(byte-call byte-unbind
211 byte-listN byte-concatN byte-insertN
212 byte-stack-ref byte-stack-set byte-stack-set2
213 byte-discardN byte-discardN-preserve-tos))
214 (insert (int-to-string arg)))
215 ((memq op '(byte-varref byte-varset byte-varbind))
216 (prin1 (car arg) (current-buffer)))
217 ((memq op '(byte-constant byte-constant2))
218 ;; it's a constant
219 (setq arg (car arg))
220 ;; but if the value of the constant is compiled code, then
221 ;; recursively disassemble it.
222 (cond ((or (byte-code-function-p arg)
223 (and (consp arg) (functionp arg)
224 (assq 'byte-code arg))
225 (and (eq (car-safe arg) 'macro)
226 (or (byte-code-function-p (cdr arg))
227 (and (consp (cdr arg))
228 (functionp (cdr arg))
229 (assq 'byte-code (cdr arg))))))
230 (cond ((byte-code-function-p arg)
231 (insert "<compiled-function>\n"))
232 ((functionp arg)
233 (insert "<compiled lambda>"))
234 (t (insert "<compiled macro>\n")))
235 (disassemble-internal
236 arg
237 (+ indent disassemble-recursive-indent 1)
238 nil))
239 ((eq (car-safe arg) 'byte-code)
240 (insert "<byte code>\n")
241 (disassemble-1 ;recurse on byte-code object
242 arg
243 (+ indent disassemble-recursive-indent)))
244 ((eq (car-safe (car-safe arg)) 'byte-code)
245 (insert "(<byte code>...)\n")
246 (mapc ;recurse on list of byte-code objects
247 (lambda (obj)
248 (disassemble-1
249 obj
250 (+ indent disassemble-recursive-indent)))
251 arg))
252 (t
253 ;; really just a constant
254 (let ((print-escape-newlines t))
255 (prin1 arg (current-buffer))))))
256 )
257 (insert "\n")))))
258 nil)
259
260 (provide 'disass)
261
262 ;;; disass.el ends here