annotate lisp/emacs-lisp/disass.el @ 3009:1b3cab5f40e4

* Makefile.in (install): Refer to the variables INSTALLABLES and INSTALLABLE_SCRIPTS, instead of writing them out.
author Jim Blandy <jimb@redhat.com>
date Mon, 24 May 1993 05:54:23 +0000
parents 10e417efb12a
children 660b7b4e3e40
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
845
213978acbc1e entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 811
diff changeset
3 ;;; Copyright (C) 1986, 1991 Free Software Foundation, Inc.
213978acbc1e entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 811
diff changeset
4
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
5 ;; Author: Doug Cutting <doug@csli.stanford.edu>
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
6 ;; Jamie Zawinski <jwz@lucid.com>
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
7 ;; Maintainer: Jamie Zawinski <jwz@lucid.com>
2247
2c7997f249eb Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1821
diff changeset
8 ;; Keywords: internal
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
9
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
10 ;; This file is part of GNU Emacs.
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
11
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
13 ;; 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
14 ;; the Free Software Foundation; either version 2, or (at your option)
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
15 ;; any later version.
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
16
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
17 ;; GNU Emacs is distributed in the hope that it will be useful,
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
20 ;; GNU General Public License for more details.
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
21
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
25
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
26 ;;; Commentary:
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
27
2307
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2247
diff changeset
28 ;; 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
29 ;; 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
30 ;; 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
31
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2247
diff changeset
32 ;;
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2247
diff changeset
33 ;; 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
34 ;; 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
35 ;; the new lapcode-based byte compiler.
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
36
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
37 ;;; Code:
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
38
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
39 ;;; 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
40 ;;; The function byte-decompile-lapcode is defined in byte-opt.el.
dbed75bc5381 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 757
diff changeset
41 ;;; 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
42 (require 'byte-compile "bytecomp")
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
43
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
44 (defvar disassemble-column-1-indent 5 "*")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
45 (defvar disassemble-column-2-indent 10 "*")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
46
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
47 (defvar disassemble-recursive-indent 3 "*")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
48
1821
04fb1d3d6992 JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents: 1606
diff changeset
49 ;;;###autoload
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
50 (defun disassemble (object &optional buffer indent interactive-p)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
51 "Print disassembled code for OBJECT in (optional) BUFFER.
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
52 OBJECT can be a symbol defined as a function, or a function itself
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
53 \(a lambda expression or a compiled-function object).
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
54 If OBJECT is not already compiled, we compile it, but do not
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
55 redefine OBJECT if it is a symbol."
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
56 (interactive (list (intern (completing-read "Disassemble function: "
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
57 obarray 'fboundp t))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
58 nil 0 t))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
59 (if (eq (car-safe object) 'byte-code)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
60 (setq object (list 'lambda () object)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
61 (or indent (setq indent 0)) ;Default indent to zero
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
62 (save-excursion
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
63 (if (or interactive-p (null buffer))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
64 (with-output-to-temp-buffer "*Disassemble*"
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
65 (set-buffer "*Disassemble*")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
66 (disassemble-internal object indent (not interactive-p)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
67 (set-buffer buffer)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
68 (disassemble-internal object indent nil)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
69 nil)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
70
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
71
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
72 (defun disassemble-internal (obj indent interactive-p)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
73 (let ((macro 'nil)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
74 (name 'nil)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
75 (doc 'nil)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
76 args)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
77 (while (symbolp obj)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
78 (setq name obj
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
79 obj (symbol-function obj)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
80 (if (subrp obj)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
81 (error "Can't disassemble #<subr %s>" name))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
82 (if (eq (car-safe obj) 'macro) ;handle macros
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
83 (setq macro t
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
84 obj (cdr obj)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
85 (if (and (listp obj) (not (eq (car obj) 'lambda)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
86 (error "not a function"))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
87 (if (consp obj)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
88 (if (assq 'byte-code obj)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
89 nil
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
90 (if interactive-p (message (if name
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
91 "Compiling %s's definition..."
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
92 "Compiling definition...")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
93 name))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
94 (setq obj (byte-compile obj))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
95 (if interactive-p (message "Done compiling. Disassembling..."))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
96 (cond ((consp obj)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
97 (setq obj (cdr obj)) ;throw lambda away
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
98 (setq args (car obj)) ;save arg list
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
99 (setq obj (cdr obj)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
100 (t
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
101 (setq args (aref obj 0))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
102 (if (zerop indent) ; not a nested function
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
103 (progn
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
104 (indent-to indent)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
105 (insert (format "byte code%s%s%s:\n"
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
106 (if (or macro name) " for" "")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
107 (if macro " macro" "")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
108 (if name (format " %s" name) "")))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
109 (let ((doc (if (consp obj)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
110 (and (stringp (car obj)) (car obj))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
111 (and (> (length obj) 4) (aref obj 4)))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
112 (if (and doc (stringp doc))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
113 (progn (and (consp obj) (setq obj (cdr obj)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
114 (indent-to indent)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
115 (princ " doc: " (current-buffer))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
116 (if (string-match "\n" doc)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
117 (setq doc (concat (substring doc 0 (match-beginning 0))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
118 " ...")))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
119 (insert doc "\n"))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
120 (indent-to indent)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
121 (insert " args: ")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
122 (prin1 args (current-buffer))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
123 (insert "\n")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
124 (let ((interactive (cond ((consp obj)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
125 (assq 'interactive obj))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
126 ((> (length obj) 5)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
127 (list 'interactive (aref obj 5))))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
128 (if interactive
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
129 (progn
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
130 (setq interactive (nth 1 interactive))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
131 (if (eq (car-safe (car-safe obj)) 'interactive)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
132 (setq obj (cdr obj)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
133 (indent-to indent)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
134 (insert " interactive: ")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
135 (if (eq (car-safe interactive) 'byte-code)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
136 (progn
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
137 (insert "\n")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
138 (disassemble-1 interactive
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
139 (+ indent disassemble-recursive-indent)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
140 (let ((print-escape-newlines t))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
141 (prin1 interactive (current-buffer))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
142 (insert "\n"))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
143 (cond ((and (consp obj) (assq 'byte-code obj))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
144 (disassemble-1 (assq 'byte-code obj) indent))
1821
04fb1d3d6992 JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents: 1606
diff changeset
145 ((byte-code-function-p obj)
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
146 (disassemble-1 obj indent))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
147 (t
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
148 (insert "Uncompiled body: ")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
149 (let ((print-escape-newlines t))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
150 (prin1 (if (cdr obj) (cons 'progn obj) (car obj))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
151 (current-buffer))))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
152 (if interactive-p
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
153 (message "")))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
154
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
155
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
156 (defun disassemble-1 (obj indent)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
157 "Prints the byte-code call OBJ in the current buffer.
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
158 OBJ should be a call to BYTE-CODE generated by the byte compiler."
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
159 (let (bytes constvec)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
160 (if (consp obj)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
161 (setq bytes (car (cdr obj)) ;the byte code
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
162 constvec (car (cdr (cdr obj)))) ;constant vector
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
163 (setq bytes (aref obj 1)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
164 constvec (aref obj 2)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
165 (let ((lap (byte-decompile-bytecode bytes constvec))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
166 op arg opname)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
167 (let ((tagno 0)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
168 tmp
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
169 (lap lap))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
170 (while (setq tmp (assq 'TAG lap))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
171 (setcar (cdr tmp) (setq tagno (1+ tagno)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
172 (setq lap (cdr (memq tmp lap)))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
173 (while lap
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
174 (setq op (car (car lap))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
175 arg (cdr (car lap)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
176 (indent-to indent)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
177 (if (eq 'TAG op)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
178 (insert (int-to-string (car arg)) ":")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
179
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
180 (indent-to (+ indent disassemble-column-1-indent))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
181 (if (and op
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
182 (string-match "^byte-" (setq opname (symbol-name op))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
183 (setq opname (substring opname 5))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
184 (setq opname "<not-an-opcode>"))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
185 (if (eq op 'byte-constant2)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
186 (insert " #### shouldn't have seen constant2 here!\n "))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
187 (insert opname)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
188 (indent-to (+ indent disassemble-column-1-indent
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
189 disassemble-column-2-indent
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
190 -1))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
191 (insert " ")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
192 (cond ((memq op byte-goto-ops)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
193 (insert (int-to-string (nth 1 arg))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
194 ((memq op '(byte-call byte-unbind
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
195 byte-listN byte-concatN byte-insertN))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
196 (insert (int-to-string arg)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
197 ((memq op '(byte-varref byte-varset byte-varbind))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
198 (prin1 (car arg) (current-buffer)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
199 ((memq op '(byte-constant byte-constant2))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
200 ;; it's a constant
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
201 (setq arg (car arg))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
202 ;; but if the value of the constant is compiled code, then
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
203 ;; recursively disassemble it.
1821
04fb1d3d6992 JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents: 1606
diff changeset
204 (cond ((or (byte-code-function-p arg)
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
205 (and (eq (car-safe arg) 'lambda)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
206 (assq 'byte-code arg))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
207 (and (eq (car-safe arg) 'macro)
1821
04fb1d3d6992 JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents: 1606
diff changeset
208 (or (byte-code-function-p (cdr arg))
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
209 (and (eq (car-safe (cdr arg)) 'lambda)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
210 (assq 'byte-code (cdr arg))))))
1821
04fb1d3d6992 JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents: 1606
diff changeset
211 (cond ((byte-code-function-p arg)
757
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
212 (insert "<compiled-function>\n"))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
213 ((eq (car-safe arg) 'lambda)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
214 (insert "<compiled lambda>"))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
215 (t (insert "<compiled macro>\n")))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
216 (disassemble-internal
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
217 arg
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
218 (+ indent disassemble-recursive-indent 1)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
219 nil))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
220 ((eq (car-safe arg) 'byte-code)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
221 (insert "<byte code>\n")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
222 (disassemble-1 ;recurse on byte-code object
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
223 arg
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
224 (+ indent disassemble-recursive-indent)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
225 ((eq (car-safe (car-safe arg)) 'byte-code)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
226 (insert "(<byte code>...)\n")
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
227 (mapcar ;recurse on list of byte-code objects
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
228 '(lambda (obj)
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
229 (disassemble-1
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
230 obj
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
231 (+ indent disassemble-recursive-indent)))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
232 arg))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
233 (t
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
234 ;; really just a constant
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
235 (let ((print-escape-newlines t))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
236 (prin1 arg (current-buffer))))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
237 )
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
238 (insert "\n"))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
239 (setq lap (cdr lap)))))
745b7fc3a3d3 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
240 nil)
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
241
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 806
diff changeset
242 ;;; disass.el ends here