annotate lisp/disass.el @ 5890:8704b7957585

#'set-locale-for-language-environment, bind a local variable correctly. lisp/ChangeLog addition: 2015-04-11 Aidan Kehoe <kehoea@parhasard.net> * mule/mule-cmds.el (set-locale-for-language-environment): Bind `position' as a local variable here, as was the original intention.
author Aidan Kehoe <kehoea@parhasard.net>
date Sat, 11 Apr 2015 18:34:14 +0100
parents bbe4146603db
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 ;;; disass.el --- disassembler for compiled Emacs Lisp code
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
3 ;;; Copyright (C) 1986, 1991-1994 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5 ;; Author: Doug Cutting <doug@csli.stanford.edu>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6 ;; Jamie Zawinski <jwz@jwz.org>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 ;; Maintainer: XEmacs Development Team
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 ;; Keywords: internal
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10 ;; This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11
5402
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4783
diff changeset
12 ;; XEmacs is free software: you can redistribute it and/or modify it
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4783
diff changeset
13 ;; under the terms of the GNU General Public License as published by the
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4783
diff changeset
14 ;; Free Software Foundation, either version 3 of the License, or (at your
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4783
diff changeset
15 ;; option) any later version.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16
5402
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4783
diff changeset
17 ;; XEmacs is distributed in the hope that it will be useful, but WITHOUT
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4783
diff changeset
18 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4783
diff changeset
19 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4783
diff changeset
20 ;; for more details.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
5402
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4783
diff changeset
23 ;; along with XEmacs. If not, see <http://www.gnu.org/licenses/>.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25 ;;; Synched up with: FSF 19.28.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27 ;;; Commentary:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29 ;; The single entry point, `disassemble', disassembles a code object generated
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 ;; by the Emacs Lisp byte-compiler. This doesn't invert the compilation
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31 ;; operation, not by a long shot, but it's useful for debugging.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34 ;; Original version by Doug Cutting (doug@csli.stanford.edu)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 ;; Substantially modified by Jamie Zawinski for
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 ;; the new lapcode-based byte compiler.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 ;;; Code:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 (require 'byte-optimize)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 (defvar disassemble-column-1-indent 8 "*")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43 (defvar disassemble-column-2-indent 10 "*")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44 (defvar disassemble-recursive-indent 3 "*")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 ;;;###autoload
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47 (defun disassemble (object &optional buffer indent interactive-p)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 "Print disassembled code for OBJECT in (optional) BUFFER.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 OBJECT can be a symbol defined as a function, or a function itself
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50 \(a lambda expression or a compiled-function object).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 If OBJECT is not already compiled, we compile it, but do not
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52 redefine OBJECT if it is a symbol."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 (interactive (list (intern (completing-read "Disassemble function: "
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 obarray 'fboundp t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55 nil 0 t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 (if (eq (car-safe object) 'byte-code)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57 (setq object (list 'lambda () object)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 (or indent (setq indent 0)) ;Default indent to zero
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 (save-excursion
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60 (if (or interactive-p (null buffer))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 (with-output-to-temp-buffer "*Disassemble*"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 (set-buffer "*Disassemble*")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63 (disassemble-internal object indent (not interactive-p)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 (set-buffer buffer)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65 (disassemble-internal object indent nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69 (defun disassemble-internal (obj indent interactive-p)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70 (let ((macro nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 (name nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 args)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73 (while (symbolp obj)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 (setq name obj
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75 obj (symbol-function obj)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76 (if (subrp obj)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77 (error "Can't disassemble #<subr %s>" name))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 (if (eq (car-safe obj) 'autoload)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 (load (elt obj 1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 (setq obj (symbol-function name))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 (if (eq (car-safe obj) 'macro) ;handle macros
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83 (setq macro t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84 obj (cdr obj)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 (if (and (listp obj) (eq (car obj) 'byte-code))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86 (setq obj (list 'lambda nil obj)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87 (if (and (listp obj) (not (eq (car obj) 'lambda)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 (error "not a function"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89 (if (consp obj)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 (if (assq 'byte-code obj)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91 nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 (if interactive-p (message (if name
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93 "Compiling %s's definition..."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94 "Compiling definition...")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 name))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 (setq obj (byte-compile obj))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97 (if interactive-p (message "Done compiling. Disassembling..."))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98 (cond ((consp obj)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99 (setq obj (cdr obj)) ;throw lambda away
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 (setq args (car obj)) ;save arg list
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101 (setq obj (cdr obj)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102 (t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103 (setq args (compiled-function-arglist obj))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 (if (zerop indent) ; not a nested function
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106 (indent-to indent)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 (insert (format "byte code%s%s%s:\n"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108 (if (or macro name) " for" "")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109 (if macro " macro" "")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110 (if name (format " %s" name) "")))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 (let ((doc (if (consp obj)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112 (and (stringp (car obj)) (car obj))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113 (condition-case error
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114 (documentation obj)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
115 (error (format "%S" error))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116 (if (and doc (stringp doc))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 (progn (and (consp obj) (setq obj (cdr obj)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118 (indent-to indent)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119 (princ " doc: " (current-buffer))
5882
bbe4146603db Reduce regexp usage, now CL-oriented non-regexp code available, core Lisp
Aidan Kehoe <kehoea@parhasard.net>
parents: 5402
diff changeset
120 (let ((frobbed nil)
bbe4146603db Reduce regexp usage, now CL-oriented non-regexp code available, core Lisp
Aidan Kehoe <kehoea@parhasard.net>
parents: 5402
diff changeset
121 (position (position ?\n doc)))
bbe4146603db Reduce regexp usage, now CL-oriented non-regexp code available, core Lisp
Aidan Kehoe <kehoea@parhasard.net>
parents: 5402
diff changeset
122 (if position
bbe4146603db Reduce regexp usage, now CL-oriented non-regexp code available, core Lisp
Aidan Kehoe <kehoea@parhasard.net>
parents: 5402
diff changeset
123 (setq doc (subseq doc 0 position)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124 frobbed t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125 (if (> (length doc) 70)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126 (setq doc (substring doc 0 65) frobbed t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 (if frobbed (setq doc (concat doc " ..."))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128 (insert doc "\n"))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129 (indent-to indent)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130 (insert " args: ")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 (prin1 args (current-buffer))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132 (insert "\n")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
133 (if (condition-case ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134 (commandp obj) ; ie interactivep
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135 (error nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136 (let ((interactive (if (consp obj)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137 (elt (assq 'interactive obj) 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138 (elt (compiled-function-interactive obj) 1))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139 (if (eq (car-safe (car-safe obj)) 'interactive)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140 (setq obj (cdr obj)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141 (indent-to indent)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142 (insert " interactive: ")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143 (if (eq (car-safe interactive) 'byte-code)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
144 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
145 (insert "\n")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
146 (disassemble-1 interactive
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
147 (+ indent disassemble-recursive-indent)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
148 (let ((print-escape-newlines t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
149 (prin1 interactive (current-buffer))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 (insert "\n")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
151 (cond ((and (consp obj) (assq 'byte-code obj))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152 (disassemble-1 (assq 'byte-code obj) indent))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153 ((compiled-function-p obj)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
154 (disassemble-1 obj indent))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155 (t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
156 (insert "Uncompiled body: ")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157 (let ((print-escape-newlines t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
158 (prin1 (if (cdr obj) (cons 'progn obj) (car obj))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159 (current-buffer))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 (if interactive-p
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161 (message nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164 (defun disassemble-1 (obj indent)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
165 "Print the byte-code call OBJ in the current buffer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 OBJ should be a compiled-function object generated by the byte compiler."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167 (let (bytes constvec)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168 (if (consp obj)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169 (setq bytes (car (cdr obj)) ; the byte code
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 constvec (car (cdr (cdr obj)))) ; constant vector
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171 (setq bytes (compiled-function-instructions obj)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172 constvec (compiled-function-constants obj)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173 (let ((lap (byte-decompile-bytecode bytes constvec))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174 op arg opname pc-value)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175 (let ((tagno 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176 tmp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177 (lap lap))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
178 (while (setq tmp (assq 'TAG lap))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179 (setcar (cdr tmp) (setq tagno (1+ tagno)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180 (setq lap (cdr (memq tmp lap)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181 (while lap
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182 ;; Take off the pc value of the next thing
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183 ;; and put it in pc-value.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184 (setq pc-value nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185 (if (numberp (car lap))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186 (setq pc-value (car lap)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
187 lap (cdr lap)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188 ;; Fetch the next op and its arg.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
189 (setq op (car (car lap))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 arg (cdr (car lap)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191 (setq lap (cdr lap))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192 (indent-to indent)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193 (if (eq 'TAG op)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
195 ;; We have a label. Display it, but first its pc value.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196 (if pc-value
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
197 (insert (format "%d:" pc-value)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
198 (insert (int-to-string (car arg))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199 ;; We have an instruction. Display its pc value first.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
200 (if pc-value
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201 (insert (format "%d" pc-value)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
202 (indent-to (+ indent disassemble-column-1-indent))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203 (if (and op
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204 (string-match "^byte-" (setq opname (symbol-name op))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
205 (setq opname (substring opname 5))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
206 (setq opname "<not-an-opcode>"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207 (if (eq op 'byte-constant2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208 (insert " #### shouldn't have seen constant2 here!\n "))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209 (insert opname)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 (indent-to (+ indent disassemble-column-1-indent
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211 disassemble-column-2-indent
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212 -1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213 (insert " ")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
214 (cond ((memq op byte-goto-ops)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
215 (insert (int-to-string (nth 1 arg))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216 ((memq op '(byte-call byte-unbind
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 byte-listN byte-concatN byte-insertN))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218 (insert (int-to-string arg)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219 ((memq op '(byte-varref byte-varset byte-varbind))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220 (prin1 (car arg) (current-buffer)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
221 ((memq op '(byte-constant byte-constant2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
222 ;; it's a constant
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
223 (setq arg (car arg))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
224 ;; but if the value of the constant is compiled code, then
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225 ;; recursively disassemble it.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
226 (cond ((or (compiled-function-p arg)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
227 (and (eq (car-safe arg) 'lambda)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
228 (assq 'byte-code arg))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
229 (and (eq (car-safe arg) 'macro)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
230 (or (compiled-function-p (cdr arg))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231 (and (eq (car-safe (cdr arg)) 'lambda)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232 (assq 'byte-code (cdr arg))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
233 (cond ((compiled-function-p arg)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234 (insert "<compiled-function>\n"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235 ((eq (car-safe arg) 'lambda)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 (insert "<compiled lambda>"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237 (t (insert "<compiled macro>\n")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238 (disassemble-internal
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
239 arg
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240 (+ indent disassemble-recursive-indent 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241 nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 ((eq (car-safe arg) 'byte-code)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243 (insert "<byte code>\n")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244 (disassemble-1 ;recurse on byte-code object
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
245 arg
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
246 (+ indent disassemble-recursive-indent)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
247 ((eq (car-safe (car-safe arg)) 'byte-code)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248 (insert "(<byte code>...)\n")
4783
e29fcfd8df5f Eliminate most core code byte-compile warnings.
Aidan Kehoe <kehoea@parhasard.net>
parents: 428
diff changeset
249 (mapc ;recurse on list of byte-code objects
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250 #'(lambda (obj)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 (disassemble-1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252 obj
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253 (+ indent disassemble-recursive-indent)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254 arg))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 (t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256 ;; really just a constant
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
257 (let ((print-escape-newlines t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258 (prin1 arg (current-buffer))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259 )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260 (insert "\n")))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261 nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263 (provide 'disass)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
264
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
265 ;;; disass.el ends here