Remove compat code in Tramp
[emacs.git] / lisp / emacs-lisp / disass.el
blob8506ed696694840eb1ad7d871df6362c9ada29e3
1 ;;; disass.el --- disassembler for compiled Emacs Lisp code -*- lexical-binding:t -*-
3 ;; Copyright (C) 1986, 1991, 2002-2016 Free Software Foundation, Inc.
5 ;; Author: Doug Cutting <doug@csli.stanford.edu>
6 ;; Jamie Zawinski <jwz@lucid.com>
7 ;; Maintainer: emacs-devel@gnu.org
8 ;; Keywords: internal
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
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.
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.
36 ;;; Code:
38 (require 'macroexp)
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")
45 (defvar disassemble-column-1-indent 8 "*")
46 (defvar disassemble-column-2-indent 10 "*")
48 (defvar disassemble-recursive-indent 3 "*")
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
58 (let* ((fn (function-called-at-point))
59 (prompt (if fn (format "Disassemble function (default %s): " fn)
60 "Disassemble function: "))
61 (def (and fn (symbol-name fn))))
62 (list (intern (completing-read prompt obarray 'fboundp t nil nil def))
63 nil 0 t)))
64 (if (and (consp object) (not (functionp object)))
65 (setq object `(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)
77 (defun disassemble-internal (obj indent interactive-p)
78 (let ((macro 'nil)
79 (name (when (symbolp obj)
80 (prog1 obj
81 (setq obj (indirect-function obj)))))
82 args)
83 (setq obj (autoload-do-load obj name))
84 (if (subrp obj)
85 (error "Can't disassemble #<subr %s>" name))
86 (if (eq (car-safe obj) 'macro) ;Handle macros.
87 (setq macro t
88 obj (cdr obj)))
89 (if (eq (car-safe obj) 'byte-code)
90 (setq obj `(lambda () ,obj)))
91 (when (consp obj)
92 (unless (functionp obj) (error "not a function"))
93 (if (assq 'byte-code obj)
94 nil
95 (if interactive-p (message (if name
96 "Compiling %s's definition..."
97 "Compiling definition...")
98 name))
99 (setq obj (byte-compile obj))
100 (if interactive-p (message "Done compiling. Disassembling..."))))
101 (cond ((consp obj)
102 (setq args (help-function-arglist obj)) ;save arg list
103 (setq obj (cdr obj)) ;throw lambda away
104 (setq obj (cdr obj)))
105 ((byte-code-function-p obj)
106 (setq args (help-function-arglist obj)))
107 (t (error "Compilation failed")))
108 (if (zerop indent) ; not a nested function
109 (progn
110 (indent-to indent)
111 (insert (format "byte code%s%s%s:\n"
112 (if (or macro name) " for" "")
113 (if macro " macro" "")
114 (if name (format " %s" name) "")))))
115 (let ((doc (if (consp obj)
116 (and (stringp (car obj)) (car obj))
117 ;; Use documentation to get lazy-loaded doc string
118 (documentation obj t))))
119 (if (and doc (stringp doc))
120 (progn (and (consp obj) (setq obj (cdr obj)))
121 (indent-to indent)
122 (princ " doc: " (current-buffer))
123 (if (string-match "\n" doc)
124 (setq doc (concat (substring doc 0 (match-beginning 0))
125 " ...")))
126 (insert doc "\n"))))
127 (indent-to indent)
128 (insert " args: ")
129 (prin1 args (current-buffer))
130 (insert "\n")
131 (let ((interactive (interactive-form obj)))
132 (if interactive
133 (progn
134 (setq interactive (nth 1 interactive))
135 (if (eq (car-safe (car-safe obj)) 'interactive)
136 (setq obj (cdr obj)))
137 (indent-to indent)
138 (insert " interactive: ")
139 (if (eq (car-safe interactive) 'byte-code)
140 (progn
141 (insert "\n")
142 (disassemble-1 interactive
143 (+ indent disassemble-recursive-indent)))
144 (let ((print-escape-newlines t))
145 (prin1 interactive (current-buffer))))
146 (insert "\n"))))
147 (cond ((and (consp obj) (assq 'byte-code obj))
148 (disassemble-1 (assq 'byte-code obj) indent))
149 ((byte-code-function-p obj)
150 (disassemble-1 obj indent))
152 (insert "Uncompiled body: ")
153 (let ((print-escape-newlines t))
154 (prin1 (macroexp-progn obj)
155 (current-buffer))))))
156 (if interactive-p
157 (message "")))
160 (defun disassemble-1 (obj indent)
161 "Prints the byte-code call OBJ in the current buffer.
162 OBJ should be a call to BYTE-CODE generated by the byte compiler."
163 (let (bytes constvec)
164 (if (consp obj)
165 (setq bytes (car (cdr obj)) ;the byte code
166 constvec (car (cdr (cdr obj)))) ;constant vector
167 ;; If it is lazy-loaded, load it now
168 (fetch-bytecode obj)
169 (setq bytes (aref obj 1)
170 constvec (aref obj 2)))
171 (let ((lap (byte-decompile-bytecode (string-as-unibyte bytes) constvec))
172 op arg opname pc-value)
173 (let ((tagno 0)
175 (lap lap))
176 (while (setq tmp (assq 'TAG lap))
177 (setcar (cdr tmp) (setq tagno (1+ tagno)))
178 (setq lap (cdr (memq tmp lap)))))
179 (while lap
180 ;; Take off the pc value of the next thing
181 ;; and put it in pc-value.
182 (setq pc-value nil)
183 (if (numberp (car lap))
184 (setq pc-value (car lap)
185 lap (cdr lap)))
186 ;; Fetch the next op and its arg.
187 (setq op (car (car lap))
188 arg (cdr (car lap)))
189 (setq lap (cdr lap))
190 (indent-to indent)
191 (if (eq 'TAG op)
192 (progn
193 ;; We have a label. Display it, but first its pc value.
194 (if pc-value
195 (insert (format "%d:" pc-value)))
196 (insert (int-to-string (car arg))))
197 ;; We have an instruction. Display its pc value first.
198 (if pc-value
199 (insert (format "%d" pc-value)))
200 (indent-to (+ indent disassemble-column-1-indent))
201 (if (and op
202 (string-match "^byte-" (setq opname (symbol-name op))))
203 (setq opname (substring opname 5))
204 (setq opname "<not-an-opcode>"))
205 (if (eq op 'byte-constant2)
206 (insert " #### shouldn't have seen constant2 here!\n "))
207 (insert opname)
208 (indent-to (+ indent disassemble-column-1-indent
209 disassemble-column-2-indent
210 -1))
211 (insert " ")
212 (cond ((memq op byte-goto-ops)
213 (insert (int-to-string (nth 1 arg))))
214 ((memq op '(byte-call byte-unbind
215 byte-listN byte-concatN byte-insertN
216 byte-stack-ref byte-stack-set byte-stack-set2
217 byte-discardN byte-discardN-preserve-tos))
218 (insert (int-to-string arg)))
219 ((memq op '(byte-varref byte-varset byte-varbind))
220 (prin1 (car arg) (current-buffer)))
221 ((memq op '(byte-constant byte-constant2))
222 ;; it's a constant
223 (setq arg (car arg))
224 ;; but if the value of the constant is compiled code, then
225 ;; recursively disassemble it.
226 (cond ((or (byte-code-function-p arg)
227 (and (consp arg) (functionp arg)
228 (assq 'byte-code arg))
229 (and (eq (car-safe arg) 'macro)
230 (or (byte-code-function-p (cdr arg))
231 (and (consp (cdr arg))
232 (functionp (cdr arg))
233 (assq 'byte-code (cdr arg))))))
234 (cond ((byte-code-function-p arg)
235 (insert "<compiled-function>\n"))
236 ((functionp arg)
237 (insert "<compiled lambda>"))
238 (t (insert "<compiled macro>\n")))
239 (disassemble-internal
241 (+ indent disassemble-recursive-indent 1)
242 nil))
243 ((eq (car-safe arg) 'byte-code)
244 (insert "<byte code>\n")
245 (disassemble-1 ;recurse on byte-code object
247 (+ indent disassemble-recursive-indent)))
248 ((eq (car-safe (car-safe arg)) 'byte-code)
249 (insert "(<byte code>...)\n")
250 (mapc ;recurse on list of byte-code objects
251 (lambda (obj)
252 (disassemble-1
254 (+ indent disassemble-recursive-indent)))
255 arg))
257 ;; really just a constant
258 (let ((print-escape-newlines t))
259 (prin1 arg (current-buffer))))))
261 (insert "\n")))))
262 nil)
264 (provide 'disass)
266 ;;; disass.el ends here