Mercurial > hg > xemacs-beta
annotate lisp/disass.el @ 5909:d138e600aa3a
Placate the C++ compiler, read_rational().
src/ChangeLog addition:
2015-05-09 Aidan Kehoe <kehoea@parhasard.net>
* lread.c (read_rational):
Cast the call to memchr() here to placate the C++ compiler.
| author | Aidan Kehoe <kehoea@parhasard.net> |
|---|---|
| date | Sat, 09 May 2015 01:23:45 +0100 |
| parents | bbe4146603db |
| children |
| rev | line source |
|---|---|
| 428 | 1 ;;; disass.el --- disassembler for compiled Emacs Lisp code |
| 2 | |
| 3 ;;; Copyright (C) 1986, 1991-1994 Free Software Foundation, Inc. | |
| 4 | |
| 5 ;; Author: Doug Cutting <doug@csli.stanford.edu> | |
| 6 ;; Jamie Zawinski <jwz@jwz.org> | |
| 7 ;; Maintainer: XEmacs Development Team | |
| 8 ;; Keywords: internal | |
| 9 | |
| 10 ;; This file is part of XEmacs. | |
| 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 | 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 | 21 |
| 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 | 24 |
| 25 ;;; Synched up with: FSF 19.28. | |
| 26 | |
| 27 ;;; Commentary: | |
| 28 | |
| 29 ;; The single entry point, `disassemble', disassembles a code object generated | |
| 30 ;; by the Emacs Lisp byte-compiler. This doesn't invert the compilation | |
| 31 ;; operation, not by a long shot, but it's useful for debugging. | |
| 32 | |
| 33 ;; | |
| 34 ;; Original version by Doug Cutting (doug@csli.stanford.edu) | |
| 35 ;; Substantially modified by Jamie Zawinski for | |
| 36 ;; the new lapcode-based byte compiler. | |
| 37 | |
| 38 ;;; Code: | |
| 39 | |
| 40 (require 'byte-optimize) | |
| 41 | |
| 42 (defvar disassemble-column-1-indent 8 "*") | |
| 43 (defvar disassemble-column-2-indent 10 "*") | |
| 44 (defvar disassemble-recursive-indent 3 "*") | |
| 45 | |
| 46 ;;;###autoload | |
| 47 (defun disassemble (object &optional buffer indent interactive-p) | |
| 48 "Print disassembled code for OBJECT in (optional) BUFFER. | |
| 49 OBJECT can be a symbol defined as a function, or a function itself | |
| 50 \(a lambda expression or a compiled-function object). | |
| 51 If OBJECT is not already compiled, we compile it, but do not | |
| 52 redefine OBJECT if it is a symbol." | |
| 53 (interactive (list (intern (completing-read "Disassemble function: " | |
| 54 obarray 'fboundp t)) | |
| 55 nil 0 t)) | |
| 56 (if (eq (car-safe object) 'byte-code) | |
| 57 (setq object (list 'lambda () object))) | |
| 58 (or indent (setq indent 0)) ;Default indent to zero | |
| 59 (save-excursion | |
| 60 (if (or interactive-p (null buffer)) | |
| 61 (with-output-to-temp-buffer "*Disassemble*" | |
| 62 (set-buffer "*Disassemble*") | |
| 63 (disassemble-internal object indent (not interactive-p))) | |
| 64 (set-buffer buffer) | |
| 65 (disassemble-internal object indent nil))) | |
| 66 nil) | |
| 67 | |
| 68 | |
| 69 (defun disassemble-internal (obj indent interactive-p) | |
| 70 (let ((macro nil) | |
| 71 (name nil) | |
| 72 args) | |
| 73 (while (symbolp obj) | |
| 74 (setq name obj | |
| 75 obj (symbol-function obj))) | |
| 76 (if (subrp obj) | |
| 77 (error "Can't disassemble #<subr %s>" name)) | |
| 78 (if (eq (car-safe obj) 'autoload) | |
| 79 (progn | |
| 80 (load (elt obj 1)) | |
| 81 (setq obj (symbol-function name)))) | |
| 82 (if (eq (car-safe obj) 'macro) ;handle macros | |
| 83 (setq macro t | |
| 84 obj (cdr obj))) | |
| 85 (if (and (listp obj) (eq (car obj) 'byte-code)) | |
| 86 (setq obj (list 'lambda nil obj))) | |
| 87 (if (and (listp obj) (not (eq (car obj) 'lambda))) | |
| 88 (error "not a function")) | |
| 89 (if (consp obj) | |
| 90 (if (assq 'byte-code obj) | |
| 91 nil | |
| 92 (if interactive-p (message (if name | |
| 93 "Compiling %s's definition..." | |
| 94 "Compiling definition...") | |
| 95 name)) | |
| 96 (setq obj (byte-compile obj)) | |
| 97 (if interactive-p (message "Done compiling. Disassembling...")))) | |
| 98 (cond ((consp obj) | |
| 99 (setq obj (cdr obj)) ;throw lambda away | |
| 100 (setq args (car obj)) ;save arg list | |
| 101 (setq obj (cdr obj))) | |
| 102 (t | |
| 103 (setq args (compiled-function-arglist obj)))) | |
| 104 (if (zerop indent) ; not a nested function | |
| 105 (progn | |
| 106 (indent-to indent) | |
| 107 (insert (format "byte code%s%s%s:\n" | |
| 108 (if (or macro name) " for" "") | |
| 109 (if macro " macro" "") | |
| 110 (if name (format " %s" name) ""))))) | |
| 111 (let ((doc (if (consp obj) | |
| 112 (and (stringp (car obj)) (car obj)) | |
| 113 (condition-case error | |
| 114 (documentation obj) | |
| 115 (error (format "%S" error)))))) | |
| 116 (if (and doc (stringp doc)) | |
| 117 (progn (and (consp obj) (setq obj (cdr obj))) | |
| 118 (indent-to indent) | |
| 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 | 124 frobbed t)) |
| 125 (if (> (length doc) 70) | |
| 126 (setq doc (substring doc 0 65) frobbed t)) | |
| 127 (if frobbed (setq doc (concat doc " ...")))) | |
| 128 (insert doc "\n")))) | |
| 129 (indent-to indent) | |
| 130 (insert " args: ") | |
| 131 (prin1 args (current-buffer)) | |
| 132 (insert "\n") | |
| 133 (if (condition-case () | |
| 134 (commandp obj) ; ie interactivep | |
| 135 (error nil)) | |
| 136 (let ((interactive (if (consp obj) | |
| 137 (elt (assq 'interactive obj) 1) | |
| 138 (elt (compiled-function-interactive obj) 1)))) | |
| 139 (if (eq (car-safe (car-safe obj)) 'interactive) | |
| 140 (setq obj (cdr obj))) | |
| 141 (indent-to indent) | |
| 142 (insert " interactive: ") | |
| 143 (if (eq (car-safe interactive) 'byte-code) | |
| 144 (progn | |
| 145 (insert "\n") | |
| 146 (disassemble-1 interactive | |
| 147 (+ indent disassemble-recursive-indent))) | |
| 148 (let ((print-escape-newlines t)) | |
| 149 (prin1 interactive (current-buffer)))) | |
| 150 (insert "\n"))) | |
| 151 (cond ((and (consp obj) (assq 'byte-code obj)) | |
| 152 (disassemble-1 (assq 'byte-code obj) indent)) | |
| 153 ((compiled-function-p obj) | |
| 154 (disassemble-1 obj indent)) | |
| 155 (t | |
| 156 (insert "Uncompiled body: ") | |
| 157 (let ((print-escape-newlines t)) | |
| 158 (prin1 (if (cdr obj) (cons 'progn obj) (car obj)) | |
| 159 (current-buffer)))))) | |
| 160 (if interactive-p | |
| 161 (message nil))) | |
| 162 | |
| 163 | |
| 164 (defun disassemble-1 (obj indent) | |
| 165 "Print the byte-code call OBJ in the current buffer. | |
| 166 OBJ should be a compiled-function object generated by the byte compiler." | |
| 167 (let (bytes constvec) | |
| 168 (if (consp obj) | |
| 169 (setq bytes (car (cdr obj)) ; the byte code | |
| 170 constvec (car (cdr (cdr obj)))) ; constant vector | |
| 171 (setq bytes (compiled-function-instructions obj) | |
| 172 constvec (compiled-function-constants obj))) | |
| 173 (let ((lap (byte-decompile-bytecode bytes constvec)) | |
| 174 op arg opname pc-value) | |
| 175 (let ((tagno 0) | |
| 176 tmp | |
| 177 (lap lap)) | |
| 178 (while (setq tmp (assq 'TAG lap)) | |
| 179 (setcar (cdr tmp) (setq tagno (1+ tagno))) | |
| 180 (setq lap (cdr (memq tmp lap))))) | |
| 181 (while lap | |
| 182 ;; Take off the pc value of the next thing | |
| 183 ;; and put it in pc-value. | |
| 184 (setq pc-value nil) | |
| 185 (if (numberp (car lap)) | |
| 186 (setq pc-value (car lap) | |
| 187 lap (cdr lap))) | |
| 188 ;; Fetch the next op and its arg. | |
| 189 (setq op (car (car lap)) | |
| 190 arg (cdr (car lap))) | |
| 191 (setq lap (cdr lap)) | |
| 192 (indent-to indent) | |
| 193 (if (eq 'TAG op) | |
| 194 (progn | |
| 195 ;; We have a label. Display it, but first its pc value. | |
| 196 (if pc-value | |
| 197 (insert (format "%d:" pc-value))) | |
| 198 (insert (int-to-string (car arg)))) | |
| 199 ;; We have an instruction. Display its pc value first. | |
| 200 (if pc-value | |
| 201 (insert (format "%d" pc-value))) | |
| 202 (indent-to (+ indent disassemble-column-1-indent)) | |
| 203 (if (and op | |
| 204 (string-match "^byte-" (setq opname (symbol-name op)))) | |
| 205 (setq opname (substring opname 5)) | |
| 206 (setq opname "<not-an-opcode>")) | |
| 207 (if (eq op 'byte-constant2) | |
| 208 (insert " #### shouldn't have seen constant2 here!\n ")) | |
| 209 (insert opname) | |
| 210 (indent-to (+ indent disassemble-column-1-indent | |
| 211 disassemble-column-2-indent | |
| 212 -1)) | |
| 213 (insert " ") | |
| 214 (cond ((memq op byte-goto-ops) | |
| 215 (insert (int-to-string (nth 1 arg)))) | |
| 216 ((memq op '(byte-call byte-unbind | |
| 217 byte-listN byte-concatN byte-insertN)) | |
| 218 (insert (int-to-string arg))) | |
| 219 ((memq op '(byte-varref byte-varset byte-varbind)) | |
| 220 (prin1 (car arg) (current-buffer))) | |
| 221 ((memq op '(byte-constant byte-constant2)) | |
| 222 ;; it's a constant | |
| 223 (setq arg (car arg)) | |
| 224 ;; but if the value of the constant is compiled code, then | |
| 225 ;; recursively disassemble it. | |
| 226 (cond ((or (compiled-function-p arg) | |
| 227 (and (eq (car-safe arg) 'lambda) | |
| 228 (assq 'byte-code arg)) | |
| 229 (and (eq (car-safe arg) 'macro) | |
| 230 (or (compiled-function-p (cdr arg)) | |
| 231 (and (eq (car-safe (cdr arg)) 'lambda) | |
| 232 (assq 'byte-code (cdr arg)))))) | |
| 233 (cond ((compiled-function-p arg) | |
| 234 (insert "<compiled-function>\n")) | |
| 235 ((eq (car-safe arg) 'lambda) | |
| 236 (insert "<compiled lambda>")) | |
| 237 (t (insert "<compiled macro>\n"))) | |
| 238 (disassemble-internal | |
| 239 arg | |
| 240 (+ indent disassemble-recursive-indent 1) | |
| 241 nil)) | |
| 242 ((eq (car-safe arg) 'byte-code) | |
| 243 (insert "<byte code>\n") | |
| 244 (disassemble-1 ;recurse on byte-code object | |
| 245 arg | |
| 246 (+ indent disassemble-recursive-indent))) | |
| 247 ((eq (car-safe (car-safe arg)) 'byte-code) | |
| 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 | 250 #'(lambda (obj) |
| 251 (disassemble-1 | |
| 252 obj | |
| 253 (+ indent disassemble-recursive-indent))) | |
| 254 arg)) | |
| 255 (t | |
| 256 ;; really just a constant | |
| 257 (let ((print-escape-newlines t)) | |
| 258 (prin1 arg (current-buffer)))))) | |
| 259 ) | |
| 260 (insert "\n"))))) | |
| 261 nil) | |
| 262 | |
| 263 (provide 'disass) | |
| 264 | |
| 265 ;;; disass.el ends here |
