annotate lisp/emacs-lisp/disass.el @ 73790:920f49f4917c

(BUGS): Remove "Build failure under Suse 10.0" was due to a local miss-configuration.
author Reiner Steib <Reiner.Steib@gmx.de>
date Mon, 06 Nov 2006 20:15:21 +0000
parents 067115a6e738
children 7a3f13e2dd57 c5406394f567
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
1 ;;; disass.el --- disassembler for compiled Emacs Lisp code
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
2
64751
5b1a238fcbb4 Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 64085
diff changeset
3 ;; Copyright (C) 1986, 1991, 2002, 2003, 2004,
68648
067115a6e738 Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 64751
diff changeset
4 ;; 2005, 2006 Free Software Foundation, Inc.
845
213978acbc1e entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 811
diff changeset
5
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
6 ;; Author: Doug Cutting <doug@csli.stanford.edu>
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
7 ;; Jamie Zawinski <jwz@lucid.com>
55044
3b81dccc5b32 (disassemble): Handle lambda-exp as arg.
Richard M. Stallman <rms@gnu.org>
parents: 52401
diff changeset
8 ;; Maintainer: FSF
2247
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1821
diff changeset
9 ;; Keywords: internal
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
10
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
11 ;; This file is part of GNU Emacs.
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
12
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
14 ;; it under the terms of the GNU General Public License as published by
806
d42e1151eed8 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 802
diff changeset
15 ;; the Free Software Foundation; either version 2, or (at your option)
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
16 ;; any later version.
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
17
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
18 ;; GNU Emacs is distributed in the hope that it will be useful,
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
21 ;; GNU General Public License for more details.
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
22
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
23 ;; You should have received a copy of the GNU General Public License
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 13340
diff changeset
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
64085
18a818a2ee7c Update FSF's address.
Lute Kamstra <lute@gnu.org>
parents: 55044
diff changeset
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18a818a2ee7c Update FSF's address.
Lute Kamstra <lute@gnu.org>
parents: 55044
diff changeset
26 ;; Boston, MA 02110-1301, USA.
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
27
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
28 ;;; Commentary:
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
29
2307
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2247
diff changeset
30 ;; The single entry point, `disassemble', disassembles a code object generated
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2247
diff changeset
31 ;; by the Emacs Lisp byte-compiler. This doesn't invert the compilation
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2247
diff changeset
32 ;; operation, not by a long shot, but it's useful for debugging.
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2247
diff changeset
33
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2247
diff changeset
34 ;;
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2247
diff changeset
35 ;; Original version by Doug Cutting (doug@csli.stanford.edu)
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2247
diff changeset
36 ;; Substantially modified by Jamie Zawinski <jwz@lucid.com> for
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2247
diff changeset
37 ;; the new lapcode-based byte compiler.
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
38
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
39 ;;; Code:
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
40
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
41 ;;; The variable byte-code-vector is defined by the new bytecomp.el.
802
dbed75bc5381 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 757
diff changeset
42 ;;; The function byte-decompile-lapcode is defined in byte-opt.el.
dbed75bc5381 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 757
diff changeset
43 ;;; Since we don't use byte-decompile-lapcode, let's try not loading byte-opt.
1606
4303c30b29de * disass.el (byte-compile): Specify that the 'byte-compile feature
Jim Blandy <jimb@redhat.com>
parents: 845
diff changeset
44 (require 'byte-compile "bytecomp")
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
45
8293
74218ea236fe (disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents: 4789
diff changeset
46 (defvar disassemble-column-1-indent 8 "*")
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
47 (defvar disassemble-column-2-indent 10 "*")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
48
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
49 (defvar disassemble-recursive-indent 3 "*")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
50
1821
04fb1d3d6992 JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents: 1606
diff changeset
51 ;;;###autoload
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
52 (defun disassemble (object &optional buffer indent interactive-p)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
53 "Print disassembled code for OBJECT in (optional) BUFFER.
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
54 OBJECT can be a symbol defined as a function, or a function itself
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
55 \(a lambda expression or a compiled-function object).
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
56 If OBJECT is not already compiled, we compile it, but do not
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
57 redefine OBJECT if it is a symbol."
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
58 (interactive (list (intern (completing-read "Disassemble function: "
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
59 obarray 'fboundp t))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
60 nil 0 t))
55044
3b81dccc5b32 (disassemble): Handle lambda-exp as arg.
Richard M. Stallman <rms@gnu.org>
parents: 52401
diff changeset
61 (if (and (consp object) (not (eq (car object) 'lambda)))
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
62 (setq object (list 'lambda () object)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
63 (or indent (setq indent 0)) ;Default indent to zero
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
64 (save-excursion
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
65 (if (or interactive-p (null buffer))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
66 (with-output-to-temp-buffer "*Disassemble*"
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
67 (set-buffer "*Disassemble*")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
68 (disassemble-internal object indent (not interactive-p)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
69 (set-buffer buffer)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
70 (disassemble-internal object indent nil)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
71 nil)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
72
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
73
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
74 (defun disassemble-internal (obj indent interactive-p)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
75 (let ((macro 'nil)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
76 (name 'nil)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
77 (doc 'nil)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
78 args)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
79 (while (symbolp obj)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
80 (setq name obj
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
81 obj (symbol-function obj)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
82 (if (subrp obj)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
83 (error "Can't disassemble #<subr %s>" name))
3767
660b7b4e3e40 (disassemble-internal): If function is autoload, load it.
Richard M. Stallman <rms@gnu.org>
parents: 2307
diff changeset
84 (if (and (listp obj) (eq (car obj) 'autoload))
660b7b4e3e40 (disassemble-internal): If function is autoload, load it.
Richard M. Stallman <rms@gnu.org>
parents: 2307
diff changeset
85 (progn
660b7b4e3e40 (disassemble-internal): If function is autoload, load it.
Richard M. Stallman <rms@gnu.org>
parents: 2307
diff changeset
86 (load (nth 1 obj))
660b7b4e3e40 (disassemble-internal): If function is autoload, load it.
Richard M. Stallman <rms@gnu.org>
parents: 2307
diff changeset
87 (setq obj (symbol-function name))))
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
88 (if (eq (car-safe obj) 'macro) ;handle macros
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
89 (setq macro t
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
90 obj (cdr obj)))
4789
86318d7728f5 (disassemble-internal): Allow a call to byte-code as argument.
Richard M. Stallman <rms@gnu.org>
parents: 3767
diff changeset
91 (if (and (listp obj) (eq (car obj) 'byte-code))
49598
0d8b17d428b5 Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 22995
diff changeset
92 (setq obj (list 'lambda nil obj)))
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
93 (if (and (listp obj) (not (eq (car obj) 'lambda)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
94 (error "not a function"))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
95 (if (consp obj)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
96 (if (assq 'byte-code obj)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
97 nil
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
98 (if interactive-p (message (if name
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
99 "Compiling %s's definition..."
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
100 "Compiling definition...")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
101 name))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
102 (setq obj (byte-compile obj))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
103 (if interactive-p (message "Done compiling. Disassembling..."))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
104 (cond ((consp obj)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
105 (setq obj (cdr obj)) ;throw lambda away
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
106 (setq args (car obj)) ;save arg list
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
107 (setq obj (cdr obj)))
14960
8fe7e5e09773 (disassemble-internal): Graceful error if compile failed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
108 ((byte-code-function-p obj)
8fe7e5e09773 (disassemble-internal): Graceful error if compile failed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
109 (setq args (aref obj 0)))
8fe7e5e09773 (disassemble-internal): Graceful error if compile failed.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
110 (t (error "Compilation failed")))
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
111 (if (zerop indent) ; not a nested function
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
112 (progn
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
113 (indent-to indent)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
114 (insert (format "byte code%s%s%s:\n"
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
115 (if (or macro name) " for" "")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
116 (if macro " macro" "")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
117 (if name (format " %s" name) "")))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
118 (let ((doc (if (consp obj)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
119 (and (stringp (car obj)) (car obj))
13340
53bbedbefdb2 (disassemble-internal): Handle lazy-loaded doc strings.
Richard M. Stallman <rms@gnu.org>
parents: 8293
diff changeset
120 ;; Use documentation to get lazy-loaded doc string
53bbedbefdb2 (disassemble-internal): Handle lazy-loaded doc strings.
Richard M. Stallman <rms@gnu.org>
parents: 8293
diff changeset
121 (documentation obj t))))
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
122 (if (and doc (stringp doc))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
123 (progn (and (consp obj) (setq obj (cdr obj)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
124 (indent-to indent)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
125 (princ " doc: " (current-buffer))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
126 (if (string-match "\n" doc)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
127 (setq doc (concat (substring doc 0 (match-beginning 0))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
128 " ...")))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
129 (insert doc "\n"))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
130 (indent-to indent)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
131 (insert " args: ")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
132 (prin1 args (current-buffer))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
133 (insert "\n")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
134 (let ((interactive (cond ((consp obj)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
135 (assq 'interactive obj))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
136 ((> (length obj) 5)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
137 (list 'interactive (aref obj 5))))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
138 (if interactive
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
139 (progn
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
140 (setq interactive (nth 1 interactive))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
141 (if (eq (car-safe (car-safe obj)) 'interactive)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
142 (setq obj (cdr obj)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
143 (indent-to indent)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
144 (insert " interactive: ")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
145 (if (eq (car-safe interactive) 'byte-code)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
146 (progn
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
147 (insert "\n")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
148 (disassemble-1 interactive
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
149 (+ indent disassemble-recursive-indent)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
150 (let ((print-escape-newlines t))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
151 (prin1 interactive (current-buffer))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
152 (insert "\n"))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
153 (cond ((and (consp obj) (assq 'byte-code obj))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
154 (disassemble-1 (assq 'byte-code obj) indent))
1821
04fb1d3d6992 JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents: 1606
diff changeset
155 ((byte-code-function-p obj)
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
156 (disassemble-1 obj indent))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
157 (t
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
158 (insert "Uncompiled body: ")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
159 (let ((print-escape-newlines t))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
160 (prin1 (if (cdr obj) (cons 'progn obj) (car obj))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
161 (current-buffer))))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
162 (if interactive-p
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
163 (message "")))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
164
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
165
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
166 (defun disassemble-1 (obj indent)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
167 "Prints the byte-code call OBJ in the current buffer.
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
168 OBJ should be a call to BYTE-CODE generated by the byte compiler."
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
169 (let (bytes constvec)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
170 (if (consp obj)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
171 (setq bytes (car (cdr obj)) ;the byte code
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
172 constvec (car (cdr (cdr obj)))) ;constant vector
13340
53bbedbefdb2 (disassemble-internal): Handle lazy-loaded doc strings.
Richard M. Stallman <rms@gnu.org>
parents: 8293
diff changeset
173 ;; If it is lazy-loaded, load it now
53bbedbefdb2 (disassemble-internal): Handle lazy-loaded doc strings.
Richard M. Stallman <rms@gnu.org>
parents: 8293
diff changeset
174 (fetch-bytecode obj)
22995
bc5bd04bf914 (disassemble-1): Move the call to
Richard M. Stallman <rms@gnu.org>
parents: 22055
diff changeset
175 (setq bytes (aref obj 1)
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
176 constvec (aref obj 2)))
22995
bc5bd04bf914 (disassemble-1): Move the call to
Richard M. Stallman <rms@gnu.org>
parents: 22055
diff changeset
177 (let ((lap (byte-decompile-bytecode (string-as-unibyte bytes) constvec))
8293
74218ea236fe (disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents: 4789
diff changeset
178 op arg opname pc-value)
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
179 (let ((tagno 0)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
180 tmp
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
181 (lap lap))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
182 (while (setq tmp (assq 'TAG lap))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
183 (setcar (cdr tmp) (setq tagno (1+ tagno)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
184 (setq lap (cdr (memq tmp lap)))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
185 (while lap
8293
74218ea236fe (disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents: 4789
diff changeset
186 ;; Take off the pc value of the next thing
74218ea236fe (disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents: 4789
diff changeset
187 ;; and put it in pc-value.
74218ea236fe (disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents: 4789
diff changeset
188 (setq pc-value nil)
74218ea236fe (disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents: 4789
diff changeset
189 (if (numberp (car lap))
74218ea236fe (disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents: 4789
diff changeset
190 (setq pc-value (car lap)
74218ea236fe (disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents: 4789
diff changeset
191 lap (cdr lap)))
74218ea236fe (disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents: 4789
diff changeset
192 ;; Fetch the next op and its arg.
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
193 (setq op (car (car lap))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
194 arg (cdr (car lap)))
8293
74218ea236fe (disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents: 4789
diff changeset
195 (setq lap (cdr lap))
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
196 (indent-to indent)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
197 (if (eq 'TAG op)
8293
74218ea236fe (disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents: 4789
diff changeset
198 (progn
74218ea236fe (disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents: 4789
diff changeset
199 ;; We have a label. Display it, but first its pc value.
74218ea236fe (disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents: 4789
diff changeset
200 (if pc-value
74218ea236fe (disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents: 4789
diff changeset
201 (insert (format "%d:" pc-value)))
74218ea236fe (disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents: 4789
diff changeset
202 (insert (int-to-string (car arg))))
74218ea236fe (disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents: 4789
diff changeset
203 ;; We have an instruction. Display its pc value first.
74218ea236fe (disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents: 4789
diff changeset
204 (if pc-value
74218ea236fe (disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents: 4789
diff changeset
205 (insert (format "%d" pc-value)))
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
206 (indent-to (+ indent disassemble-column-1-indent))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
207 (if (and op
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
208 (string-match "^byte-" (setq opname (symbol-name op))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
209 (setq opname (substring opname 5))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
210 (setq opname "<not-an-opcode>"))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
211 (if (eq op 'byte-constant2)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
212 (insert " #### shouldn't have seen constant2 here!\n "))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
213 (insert opname)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
214 (indent-to (+ indent disassemble-column-1-indent
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
215 disassemble-column-2-indent
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
216 -1))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
217 (insert " ")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
218 (cond ((memq op byte-goto-ops)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
219 (insert (int-to-string (nth 1 arg))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
220 ((memq op '(byte-call byte-unbind
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
221 byte-listN byte-concatN byte-insertN))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
222 (insert (int-to-string arg)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
223 ((memq op '(byte-varref byte-varset byte-varbind))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
224 (prin1 (car arg) (current-buffer)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
225 ((memq op '(byte-constant byte-constant2))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
226 ;; it's a constant
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
227 (setq arg (car arg))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
228 ;; but if the value of the constant is compiled code, then
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
229 ;; recursively disassemble it.
1821
04fb1d3d6992 JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents: 1606
diff changeset
230 (cond ((or (byte-code-function-p arg)
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
231 (and (eq (car-safe arg) 'lambda)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
232 (assq 'byte-code arg))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
233 (and (eq (car-safe arg) 'macro)
1821
04fb1d3d6992 JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents: 1606
diff changeset
234 (or (byte-code-function-p (cdr arg))
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
235 (and (eq (car-safe (cdr arg)) 'lambda)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
236 (assq 'byte-code (cdr arg))))))
1821
04fb1d3d6992 JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents: 1606
diff changeset
237 (cond ((byte-code-function-p arg)
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
238 (insert "<compiled-function>\n"))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
239 ((eq (car-safe arg) 'lambda)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
240 (insert "<compiled lambda>"))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
241 (t (insert "<compiled macro>\n")))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
242 (disassemble-internal
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
243 arg
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
244 (+ indent disassemble-recursive-indent 1)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
245 nil))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
246 ((eq (car-safe arg) 'byte-code)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
247 (insert "<byte code>\n")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
248 (disassemble-1 ;recurse on byte-code object
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
249 arg
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
250 (+ indent disassemble-recursive-indent)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
251 ((eq (car-safe (car-safe arg)) 'byte-code)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
252 (insert "(<byte code>...)\n")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
253 (mapcar ;recurse on list of byte-code objects
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
254 '(lambda (obj)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
255 (disassemble-1
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
256 obj
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
257 (+ indent disassemble-recursive-indent)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
258 arg))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
259 (t
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
260 ;; really just a constant
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
261 (let ((print-escape-newlines t))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
262 (prin1 arg (current-buffer))))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
263 )
8293
74218ea236fe (disassemble-1): Display the pc values.
Richard M. Stallman <rms@gnu.org>
parents: 4789
diff changeset
264 (insert "\n")))))
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
265 nil)
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
266
18383
11218164bc54 Add provide call.
Richard M. Stallman <rms@gnu.org>
parents: 14960
diff changeset
267 (provide 'disass)
11218164bc54 Add provide call.
Richard M. Stallman <rms@gnu.org>
parents: 14960
diff changeset
268
52401
695cf19ef79e Add arch taglines
Miles Bader <miles@gnu.org>
parents: 51581
diff changeset
269 ;;; arch-tag: 89482fe4-a087-4761-8dc6-d771054e763a
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
270 ;;; disass.el ends here