| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
(require 'byte-compile "bytecomp") |
|---|
| 45 |
|
|---|
| 46 |
(defvar disassemble-column-1-indent 8 "*") |
|---|
| 47 |
(defvar disassemble-column-2-indent 10 "*") |
|---|
| 48 |
|
|---|
| 49 |
(defvar disassemble-recursive-indent 3 "*") |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
(defun disassemble (object &optional buffer indent interactive-p) |
|---|
| 53 |
"Print disassembled code for OBJECT in (optional) BUFFER. |
|---|
| 54 |
OBJECT can be a symbol defined as a function, or a function itself |
|---|
| 55 |
\(a lambda expression or a compiled-function object). |
|---|
| 56 |
If OBJECT is not already compiled, we compile it, but do not |
|---|
| 57 |
redefine OBJECT if it is a symbol." |
|---|
| 58 |
(interactive (list (intern (completing-read "Disassemble function: " |
|---|
| 59 |
obarray 'fboundp t)) |
|---|
| 60 |
nil 0 t)) |
|---|
| 61 |
(if (and (consp object) (not (eq (car object) 'lambda))) |
|---|
| 62 |
(setq object (list 'lambda () object))) |
|---|
| 63 |
(or indent (setq indent 0)) |
|---|
| 64 |
(save-excursion |
|---|
| 65 |
(if (or interactive-p (null buffer)) |
|---|
| 66 |
(with-output-to-temp-buffer "*Disassemble*" |
|---|
| 67 |
(set-buffer "*Disassemble*") |
|---|
| 68 |
(disassemble-internal object indent (not interactive-p))) |
|---|
| 69 |
(set-buffer buffer) |
|---|
| 70 |
(disassemble-internal object indent nil))) |
|---|
| 71 |
nil) |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
(defun disassemble-internal (obj indent interactive-p) |
|---|
| 75 |
(let ((macro 'nil) |
|---|
| 76 |
(name 'nil) |
|---|
| 77 |
(doc 'nil) |
|---|
| 78 |
args) |
|---|
| 79 |
(while (symbolp obj) |
|---|
| 80 |
(setq name obj |
|---|
| 81 |
obj (symbol-function obj))) |
|---|
| 82 |
(if (subrp obj) |
|---|
| 83 |
(error "Can't disassemble #<subr %s>" name)) |
|---|
| 84 |
(if (and (listp obj) (eq (car obj) 'autoload)) |
|---|
| 85 |
(progn |
|---|
| 86 |
(load (nth 1 obj)) |
|---|
| 87 |
(setq obj (symbol-function name)))) |
|---|
| 88 |
(if (eq (car-safe obj) 'macro) |
|---|
| 89 |
(setq macro t |
|---|
| 90 |
obj (cdr obj))) |
|---|
| 91 |
(if (and (listp obj) (eq (car obj) 'byte-code)) |
|---|
| 92 |
(setq obj (list 'lambda nil obj))) |
|---|
| 93 |
(if (and (listp obj) (not (eq (car obj) 'lambda))) |
|---|
| 94 |
(error "not a function")) |
|---|
| 95 |
(if (consp obj) |
|---|
| 96 |
(if (assq 'byte-code obj) |
|---|
| 97 |
nil |
|---|
| 98 |
(if interactive-p (message (if name |
|---|
| 99 |
"Compiling %s's definition..." |
|---|
| 100 |
"Compiling definition...") |
|---|
| 101 |
name)) |
|---|
| 102 |
(setq obj (byte-compile obj)) |
|---|
| 103 |
(if interactive-p (message "Done compiling. Disassembling...")))) |
|---|
| 104 |
(cond ((consp obj) |
|---|
| 105 |
(setq obj (cdr obj)) |
|---|
| 106 |
(setq args (car obj)) |
|---|
| 107 |
(setq obj (cdr obj))) |
|---|
| 108 |
((byte-code-function-p obj) |
|---|
| 109 |
(setq args (aref obj 0))) |
|---|
| 110 |
(t (error "Compilation failed"))) |
|---|
| 111 |
(if (zerop indent) |
|---|
| 112 |
(progn |
|---|
| 113 |
(indent-to indent) |
|---|
| 114 |
(insert (format "byte code%s%s%s:\n" |
|---|
| 115 |
(if (or macro name) " for" "") |
|---|
| 116 |
(if macro " macro" "") |
|---|
| 117 |
(if name (format " %s" name) ""))))) |
|---|
| 118 |
(let ((doc (if (consp obj) |
|---|
| 119 |
(and (stringp (car obj)) (car obj)) |
|---|
| 120 |
|
|---|
| 121 |
(documentation obj t)))) |
|---|
| 122 |
(if (and doc (stringp doc)) |
|---|
| 123 |
(progn (and (consp obj) (setq obj (cdr obj))) |
|---|
| 124 |
(indent-to indent) |
|---|
| 125 |
(princ " doc: " (current-buffer)) |
|---|
| 126 |
(if (string-match "\n" doc) |
|---|
| 127 |
(setq doc (concat (substring doc 0 (match-beginning 0)) |
|---|
| 128 |
" ..."))) |
|---|
| 129 |
(insert doc "\n")))) |
|---|
| 130 |
(indent-to indent) |
|---|
| 131 |
(insert " args: ") |
|---|
| 132 |
(prin1 args (current-buffer)) |
|---|
| 133 |
(insert "\n") |
|---|
| 134 |
(let ((interactive (cond ((consp obj) |
|---|
| 135 |
(assq 'interactive obj)) |
|---|
| 136 |
((> (length obj) 5) |
|---|
| 137 |
(list 'interactive (aref obj 5)))))) |
|---|
| 138 |
(if interactive |
|---|
| 139 |
(progn |
|---|
| 140 |
(setq interactive (nth 1 interactive)) |
|---|
| 141 |
(if (eq (car-safe (car-safe obj)) 'interactive) |
|---|
| 142 |
(setq obj (cdr obj))) |
|---|
| 143 |
(indent-to indent) |
|---|
| 144 |
(insert " interactive: ") |
|---|
| 145 |
(if (eq (car-safe interactive) 'byte-code) |
|---|
| 146 |
(progn |
|---|
| 147 |
(insert "\n") |
|---|
| 148 |
(disassemble-1 interactive |
|---|
| 149 |
(+ indent disassemble-recursive-indent))) |
|---|
| 150 |
(let ((print-escape-newlines t)) |
|---|
| 151 |
(prin1 interactive (current-buffer)))) |
|---|
| 152 |
(insert "\n")))) |
|---|
| 153 |
(cond ((and (consp obj) (assq 'byte-code obj)) |
|---|
| 154 |
(disassemble-1 (assq 'byte-code obj) indent)) |
|---|
| 155 |
((byte-code-function-p obj) |
|---|
| 156 |
(disassemble-1 obj indent)) |
|---|
| 157 |
(t |
|---|
| 158 |
(insert "Uncompiled body: ") |
|---|
| 159 |
(let ((print-escape-newlines t)) |
|---|
| 160 |
(prin1 (if (cdr obj) (cons 'progn obj) (car obj)) |
|---|
| 161 |
(current-buffer)))))) |
|---|
| 162 |
(if interactive-p |
|---|
| 163 |
(message ""))) |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
(defun disassemble-1 (obj indent) |
|---|
| 167 |
"Prints the byte-code call OBJ in the current buffer. |
|---|
| 168 |
OBJ should be a call to BYTE-CODE generated by the byte compiler." |
|---|
| 169 |
(let (bytes constvec) |
|---|
| 170 |
(if (consp obj) |
|---|
| 171 |
(setq bytes (car (cdr obj)) |
|---|
| 172 |
constvec (car (cdr (cdr obj)))) |
|---|
| 173 |
|
|---|
| 174 |
(fetch-bytecode obj) |
|---|
| 175 |
(setq bytes (aref obj 1) |
|---|
| 176 |
constvec (aref obj 2))) |
|---|
| 177 |
(let ((lap (byte-decompile-bytecode (string-as-unibyte bytes) constvec)) |
|---|
| 178 |
op arg opname pc-value) |
|---|
| 179 |
(let ((tagno 0) |
|---|
| 180 |
tmp |
|---|
| 181 |
(lap lap)) |
|---|
| 182 |
(while (setq tmp (assq 'TAG lap)) |
|---|
| 183 |
(setcar (cdr tmp) (setq tagno (1+ tagno))) |
|---|
| 184 |
(setq lap (cdr (memq tmp lap))))) |
|---|
| 185 |
(while lap |
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
(setq pc-value nil) |
|---|
| 189 |
(if (numberp (car lap)) |
|---|
| 190 |
(setq pc-value (car lap) |
|---|
| 191 |
lap (cdr lap))) |
|---|
| 192 |
|
|---|
| 193 |
(setq op (car (car lap)) |
|---|
| 194 |
arg (cdr (car lap))) |
|---|
| 195 |
(setq lap (cdr lap)) |
|---|
| 196 |
(indent-to indent) |
|---|
| 197 |
(if (eq 'TAG op) |
|---|
| 198 |
(progn |
|---|
| 199 |
|
|---|
| 200 |
(if pc-value |
|---|
| 201 |
(insert (format "%d:" pc-value))) |
|---|
| 202 |
(insert (int-to-string (car arg)))) |
|---|
| 203 |
|
|---|
| 204 |
(if pc-value |
|---|
| 205 |
(insert (format "%d" pc-value))) |
|---|
| 206 |
(indent-to (+ indent disassemble-column-1-indent)) |
|---|
| 207 |
(if (and op |
|---|
| 208 |
(string-match "^byte-" (setq opname (symbol-name op)))) |
|---|
| 209 |
(setq opname (substring opname 5)) |
|---|
| 210 |
(setq opname "<not-an-opcode>")) |
|---|
| 211 |
(if (eq op 'byte-constant2) |
|---|
| 212 |
(insert " #### shouldn't have seen constant2 here!\n ")) |
|---|
| 213 |
(insert opname) |
|---|
| 214 |
(indent-to (+ indent disassemble-column-1-indent |
|---|
| 215 |
disassemble-column-2-indent |
|---|
| 216 |
-1)) |
|---|
| 217 |
(insert " ") |
|---|
| 218 |
(cond ((memq op byte-goto-ops) |
|---|
| 219 |
(insert (int-to-string (nth 1 arg)))) |
|---|
| 220 |
((memq op '(byte-call byte-unbind |
|---|
| 221 |
byte-listN byte-concatN byte-insertN)) |
|---|
| 222 |
(insert (int-to-string arg))) |
|---|
| 223 |
((memq op '(byte-varref byte-varset byte-varbind)) |
|---|
| 224 |
(prin1 (car arg) (current-buffer))) |
|---|
| 225 |
((memq op '(byte-constant byte-constant2)) |
|---|
| 226 |
|
|---|
| 227 |
(setq arg (car arg)) |
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
(cond ((or (byte-code-function-p arg) |
|---|
| 231 |
(and (eq (car-safe arg) 'lambda) |
|---|
| 232 |
(assq 'byte-code arg)) |
|---|
| 233 |
(and (eq (car-safe arg) 'macro) |
|---|
| 234 |
(or (byte-code-function-p (cdr arg)) |
|---|
| 235 |
(and (eq (car-safe (cdr arg)) 'lambda) |
|---|
| 236 |
(assq 'byte-code (cdr arg)))))) |
|---|
| 237 |
(cond ((byte-code-function-p arg) |
|---|
| 238 |
(insert "<compiled-function>\n")) |
|---|
| 239 |
((eq (car-safe arg) 'lambda) |
|---|
| 240 |
(insert "<compiled lambda>")) |
|---|
| 241 |
(t (insert "<compiled macro>\n"))) |
|---|
| 242 |
(disassemble-internal |
|---|
| 243 |
arg |
|---|
| 244 |
(+ indent disassemble-recursive-indent 1) |
|---|
| 245 |
nil)) |
|---|
| 246 |
((eq (car-safe arg) 'byte-code) |
|---|
| 247 |
(insert "<byte code>\n") |
|---|
| 248 |
(disassemble-1 |
|---|
| 249 |
arg |
|---|
| 250 |
(+ indent disassemble-recursive-indent))) |
|---|
| 251 |
((eq (car-safe (car-safe arg)) 'byte-code) |
|---|
| 252 |
(insert "(<byte code>...)\n") |
|---|
| 253 |
(mapcar |
|---|
| 254 |
'(lambda (obj) |
|---|
| 255 |
(disassemble-1 |
|---|
| 256 |
obj |
|---|
| 257 |
(+ indent disassemble-recursive-indent))) |
|---|
| 258 |
arg)) |
|---|
| 259 |
(t |
|---|
| 260 |
|
|---|
| 261 |
(let ((print-escape-newlines t)) |
|---|
| 262 |
(prin1 arg (current-buffer)))))) |
|---|
| 263 |
) |
|---|
| 264 |
(insert "\n"))))) |
|---|
| 265 |
nil) |
|---|
| 266 |
|
|---|
| 267 |
(provide 'disass) |
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|