0.6.12.22:
[sbcl.git] / src / compiler / target-disassem.lisp
1 ;;;; disassembler-related stuff not needed in cross-compilation host
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!DISASSEM")
13
14 ;;;; FIXME: A lot of stupid package prefixes would go away if DISASSEM
15 ;;;; would use the SB!DI package. And some more would go away if it would
16 ;;;; use SB!SYS (in order to get to the SAP-FOO operators).
17 \f
18 ;;;; combining instructions where one specializes another
19
20 ;;; Return non-NIL if the instruction SPECIAL is a more specific
21 ;;; version of GENERAL (i.e., the same instruction, but with more
22 ;;; constraints).
23 (defun inst-specializes-p (special general)
24   (declare (type instruction special general))
25   (let ((smask (inst-mask special))
26         (gmask (inst-mask general)))
27     (and (dchunk= (inst-id general)
28                   (dchunk-and (inst-id special) gmask))
29          (dchunk-strict-superset-p smask gmask))))
30
31 ;;; a bit arbitrary, but should work ok...
32 ;;;
33 ;;; Return an integer corresponding to the specificity of the
34 ;;; instruction INST.
35 (defun specializer-rank (inst)
36   (declare (type instruction inst))
37   (* (dchunk-count-bits (inst-mask inst)) 4))
38
39 ;;; Order the list of instructions INSTS with more specific (more
40 ;;; constant bits, or same-as argument constains) ones first. Returns
41 ;;; the ordered list.
42 (defun order-specializers (insts)
43   (declare (type list insts))
44   (sort insts #'> :key #'specializer-rank))
45
46 (defun specialization-error (insts)
47   (error "~@<internal disassembler error: ~2I~_Instructions either aren't related or conflict in some way: ~4I~_~S~:>"
48          insts))
49
50 ;;; Given a list of instructions INSTS, Sees if one of these instructions is a
51 ;;; more general form of all the others, in which case they are put into its
52 ;;; specializers list, and it is returned. Otherwise an error is signaled.
53 (defun try-specializing (insts)
54   (declare (type list insts))
55   (let ((masters (copy-list insts)))
56     (dolist (possible-master insts)
57       (dolist (possible-specializer insts)
58         (unless (or (eq possible-specializer possible-master)
59                     (inst-specializes-p possible-specializer possible-master))
60           (setf masters (delete possible-master masters))
61           (return)                      ; exit the inner loop
62           )))
63     (cond ((null masters)
64            (specialization-error insts))
65           ((cdr masters)
66            (error "multiple specializing masters: ~S" masters))
67           (t
68            (let ((master (car masters)))
69              (setf (inst-specializers master)
70                    (order-specializers (remove master insts)))
71              master)))))
72 \f
73 ;;;; choosing an instruction
74
75 #!-sb-fluid (declaim (inline inst-matches-p choose-inst-specialization))
76
77 ;;; Return non-NIL if all constant-bits in INST match CHUNK.
78 (defun inst-matches-p (inst chunk)
79   (declare (type instruction inst)
80            (type dchunk chunk))
81   (dchunk= (dchunk-and (inst-mask inst) chunk) (inst-id inst)))
82
83 ;;; Given an instruction object, INST, and a bit-pattern, CHUNK, pick
84 ;;; the most specific instruction on INST's specializer list whose
85 ;;; constraints are met by CHUNK. If none do, then return INST.
86 (defun choose-inst-specialization (inst chunk)
87   (declare (type instruction inst)
88            (type dchunk chunk))
89   (or (dolist (spec (inst-specializers inst) nil)
90         (declare (type instruction spec))
91         (when (inst-matches-p spec chunk)
92           (return spec)))
93       inst))
94 \f
95 ;;;; searching for an instruction in instruction space
96
97 ;;; Return the instruction object within INST-SPACE corresponding to the
98 ;;; bit-pattern CHUNK, or NIL if there isn't one.
99 (defun find-inst (chunk inst-space)
100   (declare (type dchunk chunk)
101            (type (or null inst-space instruction) inst-space))
102   (etypecase inst-space
103     (null nil)
104     (instruction
105      (if (inst-matches-p inst-space chunk)
106          (choose-inst-specialization inst-space chunk)
107          nil))
108     (inst-space
109      (let* ((mask (ispace-valid-mask inst-space))
110             (id (dchunk-and mask chunk)))
111        (declare (type dchunk id mask))
112        (dolist (choice (ispace-choices inst-space))
113          (declare (type inst-space-choice choice))
114          (when (dchunk= id (ischoice-common-id choice))
115            (return (find-inst chunk (ischoice-subspace choice)))))))))
116 \f
117 ;;;; building the instruction space
118
119 ;;; Returns an instruction-space object corresponding to the list of
120 ;;; instructions INSTS. If the optional parameter INITIAL-MASK is
121 ;;; supplied, only bits it has set are used.
122 (defun build-inst-space (insts &optional (initial-mask dchunk-one))
123   ;; This is done by finding any set of bits that's common to
124   ;; all instructions, building an instruction-space node that selects on those
125   ;; bits, and recursively handle sets of instructions with a common value for
126   ;; these bits (which, since there should be fewer instructions than in INSTS,
127   ;; should have some additional set of bits to select on, etc). If there
128   ;; are no common bits, or all instructions have the same value within those
129   ;; bits, TRY-SPECIALIZING is called, which handles the cases of many
130   ;; variations on a single instruction.
131   (declare (type list insts)
132            (type dchunk initial-mask))
133   (cond ((null insts)
134          nil)
135         ((null (cdr insts))
136          (car insts))
137         (t
138          (let ((vmask (dchunk-copy initial-mask)))
139            (dolist (inst insts)
140              (dchunk-andf vmask (inst-mask inst)))
141            (if (dchunk-zerop vmask)
142                (try-specializing insts)
143                (let ((buckets nil))
144                  (dolist (inst insts)
145                    (let* ((common-id (dchunk-and (inst-id inst) vmask))
146                           (bucket (assoc common-id buckets :test #'dchunk=)))
147                      (cond ((null bucket)
148                             (push (list common-id inst) buckets))
149                            (t
150                             (push inst (cdr bucket))))))
151                  (let ((submask (dchunk-clear initial-mask vmask)))
152                    (if (= (length buckets) 1)
153                        (try-specializing insts)
154                        (make-inst-space
155                         :valid-mask vmask
156                         :choices (mapcar (lambda (bucket)
157                                            (make-inst-space-choice
158                                             :subspace (build-inst-space
159                                                        (cdr bucket)
160                                                        submask)
161                                             :common-id (car bucket)))
162                                          buckets))))))))))
163 \f
164 ;;;; an inst-space printer for debugging purposes
165
166 (defun print-masked-binary (num mask word-size &optional (show word-size))
167   (do ((bit (1- word-size) (1- bit)))
168       ((< bit 0))
169     (write-char (cond ((logbitp bit mask)
170                        (if (logbitp bit num) #\1 #\0))
171                       ((< bit show) #\x)
172                       (t #\space)))))
173
174 (defun print-inst-bits (inst)
175   (print-masked-binary (inst-id inst)
176                        (inst-mask inst)
177                        dchunk-bits
178                        (bytes-to-bits (inst-length inst))))
179
180 ;;; Print a nicely-formatted version of INST-SPACE.
181 (defun print-inst-space (inst-space &optional (indent 0))
182   (etypecase inst-space
183     (null)
184     (instruction
185      (format t "~Vt[~A(~A)~40T" indent
186              (inst-name inst-space)
187              (inst-format-name inst-space))
188      (print-inst-bits inst-space)
189      (dolist (inst (inst-specializers inst-space))
190        (format t "~%~Vt:~A~40T" indent (inst-name inst))
191        (print-inst-bits inst))
192      (write-char #\])
193      (terpri))
194     (inst-space
195      (format t "~Vt---- ~8,'0X ----~%"
196              indent
197              (ispace-valid-mask inst-space))
198      (map nil
199           (lambda (choice)
200             (format t "~Vt~8,'0X ==>~%"
201                     (+ 2 indent)
202                     (ischoice-common-id choice))
203             (print-inst-space (ischoice-subspace choice)
204                               (+ 4 indent)))
205           (ispace-choices inst-space)))))
206 \f
207 ;;;; (The actual disassembly part follows.)
208 \f
209 ;;; Code object layout:
210 ;;;     header-word
211 ;;;     code-size (starting from first inst, in words)
212 ;;;     entry-points (points to first function header)
213 ;;;     debug-info
214 ;;;     trace-table-offset (starting from first inst, in bytes)
215 ;;;     constant1
216 ;;;     constant2
217 ;;;     ...
218 ;;;     <padding to dual-word boundary>
219 ;;;     start of instructions
220 ;;;     ...
221 ;;;     function-headers and lra's buried in here randomly
222 ;;;     ...
223 ;;;     start of trace-table
224 ;;;     <padding to dual-word boundary>
225 ;;;
226 ;;; Function header layout (dual word aligned):
227 ;;;     header-word
228 ;;;     self pointer
229 ;;;     next pointer (next function header)
230 ;;;     name
231 ;;;     arglist
232 ;;;     type
233 ;;;
234 ;;; LRA layout (dual word aligned):
235 ;;;     header-word
236
237 #!-sb-fluid (declaim (inline words-to-bytes bytes-to-words))
238
239 (eval-when (:compile-toplevel :load-toplevel :execute)
240   ;;; Convert a word-offset NUM to a byte-offset.
241   (defun words-to-bytes (num)
242     (declare (type offset num))
243     (ash num sb!vm:word-shift))
244   ) ; EVAL-WHEN
245
246 ;;; Convert a byte-offset NUM to a word-offset.
247 (defun bytes-to-words (num)
248   (declare (type offset num))
249   (ash num (- sb!vm:word-shift)))
250
251 (defconstant lra-size (words-to-bytes 1))
252 \f
253 (defstruct (offs-hook (:copier nil))
254   (offset 0 :type offset)
255   (function (required-argument) :type function)
256   (before-address nil :type (member t nil)))
257
258 (defstruct (segment (:conc-name seg-)
259                     (:constructor %make-segment)
260                     (:copier nil))
261   (sap-maker (required-argument)
262              :type (function () sb!sys:system-area-pointer))
263   (length 0 :type length)
264   (virtual-location 0 :type address)
265   (storage-info nil :type (or null storage-info))
266   (code nil :type (or null sb!kernel:code-component))
267   (hooks nil :type list))
268 (def!method print-object ((seg segment) stream)
269   (print-unreadable-object (seg stream :type t)
270     (let ((addr (sb!sys:sap-int (funcall (seg-sap-maker seg)))))
271       (format stream "#X~X[~D]~:[ (#X~X)~;~*~]~@[ in ~S~]"
272               addr
273               (seg-length seg)
274               (= (seg-virtual-location seg) addr)
275               (seg-virtual-location seg)
276               (seg-code seg)))))
277 \f
278 ;;; All state during disassembly. We store some seemingly redundant
279 ;;; information so that we can allow garbage collect during disassembly and
280 ;;; not get tripped up by a code block being moved...
281 (defstruct (disassem-state (:conc-name dstate-)
282                            (:constructor %make-dstate)
283                            (:copier nil))
284   (cur-offs 0 :type offset)             ; offset of current pos in segment
285   (next-offs 0 :type offset)            ; offset of next position
286
287   (segment-sap (required-argument) :type sb!sys:system-area-pointer)
288                                         ; a sap pointing to our segment
289   (segment nil :type (or null segment)) ; the current segment
290
291   (alignment sb!vm:word-bytes :type alignment) ; what to align to in most cases
292   (byte-order :little-endian
293               :type (member :big-endian :little-endian))
294
295   (properties nil :type list)           ; for user code to hang stuff off of
296   (filtered-values (make-array max-filtered-value-index)
297                    :type filtered-value-vector)
298
299   (addr-print-len nil :type             ; used for prettifying printing
300                   (or null (integer 0 20)))
301   (argument-column 0 :type column)
302   (output-state :beginning              ; to make output look nicer
303                 :type (member :beginning
304                               :block-boundary
305                               nil))
306
307   (labels nil :type list)               ; alist of (address . label-number)
308   (label-hash (make-hash-table)         ; same thing in a different form
309               :type hash-table)
310
311   (fun-hooks nil :type list)            ; list of function
312
313   ;; these next two are popped as they are used
314   (cur-labels nil :type list)           ; alist of (address . label-number)
315   (cur-offs-hooks nil :type list)       ; list of offs-hook
316
317   (notes nil :type list)                ; for the current location
318
319   (current-valid-locations nil          ; currently active source variables
320                            :type (or null (vector bit))))
321 (def!method print-object ((dstate disassem-state) stream)
322   (print-unreadable-object (dstate stream :type t)
323     (format stream
324             "+~D~@[ in ~S~]"
325             (dstate-cur-offs dstate)
326             (dstate-segment dstate))))
327
328 ;;; Return the absolute address of the current instruction in DSTATE.
329 (defun dstate-cur-addr (dstate)
330   (the address (+ (seg-virtual-location (dstate-segment dstate))
331                   (dstate-cur-offs dstate))))
332
333 ;;; Return the absolute address of the next instruction in DSTATE.
334 (defun dstate-next-addr (dstate)
335   (the address (+ (seg-virtual-location (dstate-segment dstate))
336                   (dstate-next-offs dstate))))
337 \f
338 ;;;; function ops
339
340 (defun fun-self (fun)
341   (declare (type compiled-function fun))
342   (sb!kernel:%function-self fun))
343
344 (defun fun-code (fun)
345   (declare (type compiled-function fun))
346   (sb!kernel:function-code-header (fun-self fun)))
347
348 (defun fun-next (fun)
349   (declare (type compiled-function fun))
350   (sb!kernel:%function-next fun))
351
352 (defun fun-address (function)
353   (declare (type compiled-function function))
354   (- (sb!kernel:get-lisp-obj-address function) sb!vm:function-pointer-type))
355
356 ;;; the offset of FUNCTION from the start of its code-component's
357 ;;; instruction area
358 (defun fun-insts-offset (function)
359   (declare (type compiled-function function))
360   (- (fun-address function)
361      (sb!sys:sap-int (sb!kernel:code-instructions (fun-code function)))))
362
363 ;;; the offset of FUNCTION from the start of its code-component
364 (defun fun-offset (function)
365   (declare (type compiled-function function))
366   (words-to-bytes (sb!kernel:get-closure-length function)))
367 \f
368 ;;;; operations on code-components (which hold the instructions for
369 ;;;; one or more functions)
370
371 ;;; Return the length of the instruction area in CODE-COMPONENT.
372 (defun code-inst-area-length (code-component)
373   (declare (type sb!kernel:code-component code-component))
374   (sb!kernel:code-header-ref code-component
375                              sb!vm:code-trace-table-offset-slot))
376
377 ;;; Return the address of the instruction area in CODE-COMPONENT.
378 (defun code-inst-area-address (code-component)
379   (declare (type sb!kernel:code-component code-component))
380   (sb!sys:sap-int (sb!kernel:code-instructions code-component)))
381
382 ;;; Return the first function in CODE-COMPONENT.
383 (defun code-first-function (code-component)
384   (declare (type sb!kernel:code-component code-component))
385   (sb!kernel:code-header-ref code-component
386                              sb!vm:code-trace-table-offset-slot))
387
388 (defun segment-offs-to-code-offs (offset segment)
389   (sb!sys:without-gcing
390    (let* ((seg-base-addr (sb!sys:sap-int (funcall (seg-sap-maker segment))))
391           (code-addr
392            (logandc1 sb!vm:lowtag-mask
393                      (sb!kernel:get-lisp-obj-address (seg-code segment))))
394           (addr (+ offset seg-base-addr)))
395      (declare (type address seg-base-addr code-addr addr))
396      (- addr code-addr))))
397
398 (defun code-offs-to-segment-offs (offset segment)
399   (sb!sys:without-gcing
400    (let* ((seg-base-addr (sb!sys:sap-int (funcall (seg-sap-maker segment))))
401           (code-addr
402            (logandc1 sb!vm:lowtag-mask
403                      (sb!kernel:get-lisp-obj-address (seg-code segment))))
404           (addr (+ offset code-addr)))
405      (declare (type address seg-base-addr code-addr addr))
406      (- addr seg-base-addr))))
407
408 (defun code-insts-offs-to-segment-offs (offset segment)
409   (sb!sys:without-gcing
410    (let* ((seg-base-addr (sb!sys:sap-int (funcall (seg-sap-maker segment))))
411           (code-insts-addr
412            (sb!sys:sap-int (sb!kernel:code-instructions (seg-code segment))))
413           (addr (+ offset code-insts-addr)))
414      (declare (type address seg-base-addr code-insts-addr addr))
415      (- addr seg-base-addr))))
416 \f
417 (defun lra-hook (chunk stream dstate)
418   (declare (type dchunk chunk)
419            (ignore chunk)
420            (type (or null stream) stream)
421            (type disassem-state dstate))
422   (when (and (aligned-p (+ (seg-virtual-location (dstate-segment dstate))
423                            (dstate-cur-offs dstate))
424                         (* 2 sb!vm:word-bytes))
425              ;; Check type.
426              (= (sb!sys:sap-ref-8 (dstate-segment-sap dstate)
427                                   (if (eq (dstate-byte-order dstate)
428                                           :little-endian)
429                                       (dstate-cur-offs dstate)
430                                       (+ (dstate-cur-offs dstate)
431                                          (1- lra-size))))
432                 sb!vm:return-pc-header-type))
433     (unless (null stream)
434       (princ '.lra stream))
435     (incf (dstate-next-offs dstate) lra-size))
436   nil)
437
438 ;;; Print the function-header (entry-point) pseudo-instruction at the
439 ;;; current location in DSTATE to STREAM.
440 (defun fun-header-hook (stream dstate)
441   (declare (type (or null stream) stream)
442            (type disassem-state dstate))
443   (unless (null stream)
444     (let* ((seg (dstate-segment dstate))
445            (code (seg-code seg))
446            (woffs
447             (bytes-to-words
448              (segment-offs-to-code-offs (dstate-cur-offs dstate) seg)))
449            (name
450             (sb!kernel:code-header-ref code
451                                        (+ woffs sb!vm:function-name-slot)))
452            (args
453             (sb!kernel:code-header-ref code
454                                        (+ woffs sb!vm:function-arglist-slot)))
455            (type
456             (sb!kernel:code-header-ref code
457                                        (+ woffs sb!vm:function-type-slot))))
458       (format stream ".~A ~S~:A" 'entry name args)
459       (note (lambda (stream)
460               (format stream "~:S" type)) ; use format to print NIL as ()
461             dstate)))
462   (incf (dstate-next-offs dstate)
463         (words-to-bytes sb!vm:function-code-offset)))
464 \f
465 (defun alignment-hook (chunk stream dstate)
466   (declare (type dchunk chunk)
467            (ignore chunk)
468            (type (or null stream) stream)
469            (type disassem-state dstate))
470   (let ((location
471          (+ (seg-virtual-location (dstate-segment dstate))
472             (dstate-cur-offs dstate)))
473         (alignment (dstate-alignment dstate)))
474     (unless (aligned-p location alignment)
475       (when stream
476         (format stream "~A~Vt~D~%" '.align
477                 (dstate-argument-column dstate)
478                 alignment))
479       (incf(dstate-next-offs dstate)
480            (- (align location alignment) location)))
481     nil))
482
483 (defun rewind-current-segment (dstate segment)
484   (declare (type disassem-state dstate)
485            (type segment segment))
486   (setf (dstate-segment dstate) segment)
487   (setf (dstate-cur-offs-hooks dstate)
488         (stable-sort (nreverse (copy-list (seg-hooks segment)))
489                      (lambda (oh1 oh2)
490                        (or (< (offs-hook-offset oh1) (offs-hook-offset oh2))
491                            (and (= (offs-hook-offset oh1)
492                                    (offs-hook-offset oh2))
493                                 (offs-hook-before-address oh1)
494                                 (not (offs-hook-before-address oh2)))))))
495   (setf (dstate-cur-offs dstate) 0)
496   (setf (dstate-cur-labels dstate) (dstate-labels dstate)))
497
498 (defun do-offs-hooks (before-address stream dstate)
499   (declare (type (or null stream) stream)
500            (type disassem-state dstate))
501   (let ((cur-offs (dstate-cur-offs dstate)))
502     (setf (dstate-next-offs dstate) cur-offs)
503     (loop
504       (let ((next-hook (car (dstate-cur-offs-hooks dstate))))
505         (when (null next-hook)
506           (return))
507         (let ((hook-offs (offs-hook-offset next-hook)))
508           (when (or (> hook-offs cur-offs)
509                     (and (= hook-offs cur-offs)
510                          before-address
511                          (not (offs-hook-before-address next-hook))))
512             (return))
513           (unless (< hook-offs cur-offs)
514             (funcall (offs-hook-function next-hook) stream dstate))
515           (pop (dstate-cur-offs-hooks dstate))
516           (unless (= (dstate-next-offs dstate) cur-offs)
517             (return)))))))
518
519 (defun do-fun-hooks (chunk stream dstate)
520   (let ((hooks (dstate-fun-hooks dstate))
521         (cur-offs (dstate-cur-offs dstate)))
522     (setf (dstate-next-offs dstate) cur-offs)
523     (dolist (hook hooks nil)
524       (let ((prefix-p (funcall hook chunk stream dstate)))
525         (unless (= (dstate-next-offs dstate) cur-offs)
526           (return prefix-p))))))
527
528 (defun handle-bogus-instruction (stream dstate)
529   (let ((alignment (dstate-alignment dstate)))
530     (unless (null stream)
531       (multiple-value-bind (words bytes)
532           (truncate alignment sb!vm:word-bytes)
533         (when (> words 0)
534           (print-words words stream dstate))
535         (when (> bytes 0)
536           (print-bytes bytes stream dstate))))
537     (incf (dstate-next-offs dstate) alignment)))
538
539 ;;; Iterate through the instructions in SEGMENT, calling FUNCTION for
540 ;;; each instruction, with arguments of CHUNK, STREAM, and DSTATE.
541 (defun map-segment-instructions (function segment dstate &optional stream)
542   (declare (type function function)
543            (type segment segment)
544            (type disassem-state dstate)
545            (type (or null stream) stream))
546
547   (let ((ispace (get-inst-space))
548         (prefix-p nil)) ; just processed a prefix inst
549
550     (rewind-current-segment dstate segment)
551
552     (loop
553       (when (>= (dstate-cur-offs dstate)
554                 (seg-length (dstate-segment dstate)))
555         ;; done!
556         (return))
557
558       (setf (dstate-next-offs dstate) (dstate-cur-offs dstate))
559
560       (do-offs-hooks t stream dstate)
561       (unless (or prefix-p (null stream))
562         (print-current-address stream dstate))
563       (do-offs-hooks nil stream dstate)
564
565       (unless (> (dstate-next-offs dstate) (dstate-cur-offs dstate))
566         (sb!sys:without-gcing
567          (setf (dstate-segment-sap dstate) (funcall (seg-sap-maker segment)))
568
569          (let ((chunk
570                 (sap-ref-dchunk (dstate-segment-sap dstate)
571                                 (dstate-cur-offs dstate)
572                                 (dstate-byte-order dstate))))
573            (let ((fun-prefix-p (do-fun-hooks chunk stream dstate)))
574              (if (> (dstate-next-offs dstate) (dstate-cur-offs dstate))
575                  (setf prefix-p fun-prefix-p)
576                  (let ((inst (find-inst chunk ispace)))
577                    (cond ((null inst)
578                           (handle-bogus-instruction stream dstate))
579                          (t
580                           (setf (dstate-next-offs dstate)
581                                 (+ (dstate-cur-offs dstate)
582                                    (inst-length inst)))
583
584                           (let ((prefilter (inst-prefilter inst))
585                                 (control (inst-control inst)))
586                             (when prefilter
587                               (funcall prefilter chunk dstate))
588
589                             (funcall function chunk inst)
590
591                             (setf prefix-p (null (inst-printer inst)))
592
593                             (when control
594                               (funcall control chunk inst stream dstate))))))
595                  )))))
596
597       (setf (dstate-cur-offs dstate) (dstate-next-offs dstate))
598
599       (unless (null stream)
600         (unless prefix-p
601           (print-notes-and-newline stream dstate))
602         (setf (dstate-output-state dstate) nil)))))
603 \f
604 ;;; Make an initial non-printing disassembly pass through DSTATE,
605 ;;; noting any addresses that are referenced by instructions in this
606 ;;; segment.
607 (defun add-segment-labels (segment dstate)
608   ;; add labels at the beginning with a label-number of nil; we'll notice
609   ;; later and fill them in (and sort them)
610   (declare (type disassem-state dstate))
611   (let ((labels (dstate-labels dstate)))
612     (map-segment-instructions
613      (lambda (chunk inst)
614        (declare (type dchunk chunk) (type instruction inst))
615        (let ((labeller (inst-labeller inst)))
616          (when labeller
617            (setf labels (funcall labeller chunk labels dstate)))))
618      segment
619      dstate)
620     (setf (dstate-labels dstate) labels)
621     ;; erase any notes that got there by accident
622     (setf (dstate-notes dstate) nil)))
623
624 ;;; If any labels in DSTATE have been added since the last call to
625 ;;; this function, give them label-numbers, enter them in the
626 ;;; hash-table, and make sure the label list is in sorted order.
627 (defun number-labels (dstate)
628   (let ((labels (dstate-labels dstate)))
629     (when (and labels (null (cdar labels)))
630       ;; at least one label left un-numbered
631       (setf labels (sort labels #'< :key #'car))
632       (let ((max -1)
633             (label-hash (dstate-label-hash dstate)))
634         (dolist (label labels)
635           (when (not (null (cdr label)))
636             (setf max (max max (cdr label)))))
637         (dolist (label labels)
638           (when (null (cdr label))
639             (incf max)
640             (setf (cdr label) max)
641             (setf (gethash (car label) label-hash)
642                   (format nil "L~D" max)))))
643       (setf (dstate-labels dstate) labels))))
644 \f
645 ;;; Get the instruction-space, creating it if necessary.
646 (defun get-inst-space ()
647   (let ((ispace *disassem-inst-space*))
648     (when (null ispace)
649       (let ((insts nil))
650         (maphash (lambda (name inst-flavs)
651                    (declare (ignore name))
652                    (dolist (flav inst-flavs)
653                      (push flav insts)))
654                  *disassem-insts*)
655         (setf ispace (build-inst-space insts)))
656       (setf *disassem-inst-space* ispace))
657     ispace))
658 \f
659 ;;;; Add global hooks.
660
661 (defun add-offs-hook (segment addr hook)
662   (let ((entry (cons addr hook)))
663     (if (null (seg-hooks segment))
664         (setf (seg-hooks segment) (list entry))
665         (push entry (cdr (last (seg-hooks segment)))))))
666
667 (defun add-offs-note-hook (segment addr note)
668   (add-offs-hook segment
669                  addr
670                  (lambda (stream dstate)
671                    (declare (type (or null stream) stream)
672                             (type disassem-state dstate))
673                    (when stream
674                      (note note dstate)))))
675
676 (defun add-offs-comment-hook (segment addr comment)
677   (add-offs-hook segment
678                  addr
679                  (lambda (stream dstate)
680                    (declare (type (or null stream) stream)
681                             (ignore dstate))
682                    (when stream
683                      (write-string ";;; " stream)
684                      (etypecase comment
685                        (string
686                         (write-string comment stream))
687                        (function
688                         (funcall comment stream)))
689                      (terpri stream)))))
690
691 (defun add-fun-hook (dstate function)
692   (push function (dstate-fun-hooks dstate)))
693 \f
694 (defun set-location-printing-range (dstate from length)
695   (setf (dstate-addr-print-len dstate)
696         ;; 4 bits per hex digit
697         (ceiling (integer-length (logxor from (+ from length))) 4)))
698
699 ;;; Print the current address in DSTATE to STREAM, plus any labels that
700 ;;; correspond to it, and leave the cursor in the instruction column.
701 (defun print-current-address (stream dstate)
702   (declare (type stream stream)
703            (type disassem-state dstate))
704   (let* ((location
705           (+ (seg-virtual-location (dstate-segment dstate))
706              (dstate-cur-offs dstate)))
707          (location-column-width *disassem-location-column-width*)
708          (plen (dstate-addr-print-len dstate)))
709
710     (when (null plen)
711       (setf plen location-column-width)
712       (let ((seg (dstate-segment dstate)))
713         (set-location-printing-range dstate
714                                      (seg-virtual-location seg)
715                                      (seg-length seg))))
716     (when (eq (dstate-output-state dstate) :beginning)
717       (setf plen location-column-width))
718
719     (fresh-line stream)
720
721     (setf location-column-width (+ 2 location-column-width))
722     (princ "; " stream)
723
724     ;; print the location
725     ;; [this is equivalent to (format stream "~V,'0x:" plen printed-value), but
726     ;;  usually avoids any consing]
727     (tab0 (- location-column-width plen) stream)
728     (let* ((printed-bits (* 4 plen))
729            (printed-value (ldb (byte printed-bits 0) location))
730            (leading-zeros
731             (truncate (- printed-bits (integer-length printed-value)) 4)))
732       (dotimes (i leading-zeros)
733         (write-char #\0 stream))
734       (unless (zerop printed-value)
735         (write printed-value :stream stream :base 16 :radix nil))
736       (write-char #\: stream))
737
738     ;; print any labels
739     (loop
740       (let* ((next-label (car (dstate-cur-labels dstate)))
741              (label-location (car next-label)))
742         (when (or (null label-location) (> label-location location))
743           (return))
744         (unless (< label-location location)
745           (format stream " L~D:" (cdr next-label)))
746         (pop (dstate-cur-labels dstate))))
747
748     ;; move to the instruction column
749     (tab0 (+ location-column-width 1 label-column-width) stream)
750     ))
751 \f
752 (eval-when (:compile-toplevel :execute)
753   (sb!xc:defmacro with-print-restrictions (&rest body)
754     `(let ((*print-pretty* t)
755            (*print-lines* 2)
756            (*print-length* 4)
757            (*print-level* 3))
758        ,@body)))
759
760 ;;; Print a newline to STREAM, inserting any pending notes in DSTATE
761 ;;; as end-of-line comments. If there is more than one note, a
762 ;;; separate line will be used for each one.
763 (defun print-notes-and-newline (stream dstate)
764   (declare (type stream stream)
765            (type disassem-state dstate))
766   (with-print-restrictions
767     (dolist (note (dstate-notes dstate))
768       (format stream "~Vt " *disassem-note-column*)
769       (pprint-logical-block (stream nil :per-line-prefix "; ")
770       (etypecase note
771         (string
772          (write-string note stream))
773         (function
774            (funcall note stream))))
775       (terpri stream))
776     (fresh-line stream)
777     (setf (dstate-notes dstate) nil)))
778
779 ;;; Disassemble NUM bytes to STREAM as simple `BYTE' instructions.
780 (defun print-bytes (num stream dstate)
781   (declare (type offset num)
782            (type stream stream)
783            (type disassem-state dstate))
784   (format stream "~A~Vt" 'BYTE (dstate-argument-column dstate))
785   (let ((sap (dstate-segment-sap dstate))
786         (start-offs (dstate-cur-offs dstate)))
787     (dotimes (offs num)
788       (unless (zerop offs)
789         (write-string ", " stream))
790       (format stream "#X~2,'0x" (sb!sys:sap-ref-8 sap (+ offs start-offs))))))
791
792 ;;; Disassemble NUM machine-words to STREAM as simple `WORD' instructions.
793 (defun print-words (num stream dstate)
794   (declare (type offset num)
795            (type stream stream)
796            (type disassem-state dstate))
797   (format stream "~A~Vt" 'WORD (dstate-argument-column dstate))
798   (let ((sap (dstate-segment-sap dstate))
799         (start-offs (dstate-cur-offs dstate))
800         (byte-order (dstate-byte-order dstate)))
801     (dotimes (word-offs num)
802       (unless (zerop word-offs)
803         (write-string ", " stream))
804       (let ((word 0) (bit-shift 0))
805         (dotimes (byte-offs sb!vm:word-bytes)
806           (let ((byte
807                  (sb!sys:sap-ref-8
808                         sap
809                         (+ start-offs
810                            (* word-offs sb!vm:word-bytes)
811                            byte-offs))))
812             (setf word
813                   (if (eq byte-order :big-endian)
814                       (+ (ash word sb!vm:byte-bits) byte)
815                       (+ word (ash byte bit-shift))))
816             (incf bit-shift sb!vm:byte-bits)))
817         (format stream "#X~V,'0X" (ash sb!vm:word-bits -2) word)))))
818 \f
819 (defvar *default-dstate-hooks* (list #'lra-hook))
820
821 ;;; Make a disassembler-state object.
822 (defun make-dstate (&optional (fun-hooks *default-dstate-hooks*))
823   (let ((sap
824          (sb!sys:vector-sap (coerce #() '(vector (unsigned-byte 8)))))
825         (alignment *disassem-inst-alignment-bytes*)
826         (arg-column
827          (+ (or *disassem-opcode-column-width* 0)
828             *disassem-location-column-width*
829             1
830             label-column-width)))
831
832     (when (> alignment 1)
833       (push #'alignment-hook fun-hooks))
834
835     (%make-dstate :segment-sap sap
836                   :fun-hooks fun-hooks
837                   :argument-column arg-column
838                   :alignment alignment
839                   :byte-order sb!c:*backend-byte-order*)))
840
841 (defun add-fun-header-hooks (segment)
842   (declare (type segment segment))
843   (do ((fun (sb!kernel:code-header-ref (seg-code segment)
844                                        sb!vm:code-entry-points-slot)
845             (fun-next fun))
846        (length (seg-length segment)))
847       ((null fun))
848     (let ((offset (code-offs-to-segment-offs (fun-offset fun) segment)))
849       (when (<= 0 offset length)
850         (push (make-offs-hook :offset offset :function #'fun-header-hook)
851               (seg-hooks segment))))))
852 \f
853 ;;; A SAP-MAKER is a no-argument function that returns a SAP.
854
855 #!-sb-fluid (declaim (inline sap-maker))
856
857 (defun sap-maker (function input offset)
858   (declare (optimize (speed 3))
859            (type (function (t) sb!sys:system-area-pointer) function)
860            (type offset offset))
861   (let ((old-sap (sb!sys:sap+ (funcall function input) offset)))
862     (declare (type sb!sys:system-area-pointer old-sap))
863     (lambda ()
864       (let ((new-addr
865              (+ (sb!sys:sap-int (funcall function input)) offset)))
866         ;; Saving the sap like this avoids consing except when the sap
867         ;; changes (because the sap-int, arith, etc., get inlined).
868         (declare (type address new-addr))
869         (if (= (sb!sys:sap-int old-sap) new-addr)
870             old-sap
871             (setf old-sap (sb!sys:int-sap new-addr)))))))
872
873 (defun vector-sap-maker (vector offset)
874   (declare (optimize (speed 3))
875            (type offset offset))
876   (sap-maker #'sb!sys:vector-sap vector offset))
877
878 (defun code-sap-maker (code offset)
879   (declare (optimize (speed 3))
880            (type sb!kernel:code-component code)
881            (type offset offset))
882   (sap-maker #'sb!kernel:code-instructions code offset))
883
884 (defun memory-sap-maker (address)
885   (declare (optimize (speed 3))
886            (type address address))
887   (let ((sap (sb!sys:int-sap address)))
888     (lambda () sap)))
889 \f
890 ;;; Return a memory segment located at the system-area-pointer returned by
891 ;;; SAP-MAKER and LENGTH bytes long in the disassem-state object DSTATE.
892 ;;;
893 ;;; &KEY arguments include :VIRTUAL-LOCATION (by default the same as
894 ;;; the address), :DEBUG-FUNCTION, :SOURCE-FORM-CACHE (a
895 ;;; SOURCE-FORM-CACHE object), and :HOOKS (a list of OFFS-HOOK
896 ;;; objects).
897 (defun make-segment (sap-maker length
898                      &key
899                      code virtual-location
900                      debug-function source-form-cache
901                      hooks)
902   (declare (type (function () sb!sys:system-area-pointer) sap-maker)
903            (type length length)
904            (type (or null address) virtual-location)
905            (type (or null sb!di:debug-function) debug-function)
906            (type (or null source-form-cache) source-form-cache))
907   (let* ((segment
908           (%make-segment
909            :sap-maker sap-maker
910            :length length
911            :virtual-location (or virtual-location
912                                  (sb!sys:sap-int (funcall sap-maker)))
913            :hooks hooks
914            :code code)))
915     (add-debugging-hooks segment debug-function source-form-cache)
916     (add-fun-header-hooks segment)
917     segment))
918
919 (defun make-vector-segment (vector offset &rest args)
920   (declare (type vector vector)
921            (type offset offset)
922            (inline make-segment))
923   (apply #'make-segment (vector-sap-maker vector offset) args))
924
925 (defun make-code-segment (code offset length &rest args)
926   (declare (type sb!kernel:code-component code)
927            (type offset offset)
928            (inline make-segment))
929   (apply #'make-segment (code-sap-maker code offset) length :code code args))
930
931 (defun make-memory-segment (address &rest args)
932   (declare (type address address)
933            (inline make-segment))
934   (apply #'make-segment (memory-sap-maker address) args))
935 \f
936 ;;; just for fun
937 (defun print-fun-headers (function)
938   (declare (type compiled-function function))
939   (let* ((self (fun-self function))
940          (code (sb!kernel:function-code-header self)))
941     (format t "Code-header ~S: size: ~S, trace-table-offset: ~S~%"
942             code
943             (sb!kernel:code-header-ref code
944                                        sb!vm:code-code-size-slot)
945             (sb!kernel:code-header-ref code
946                                        sb!vm:code-trace-table-offset-slot))
947     (do ((fun (sb!kernel:code-header-ref code sb!vm:code-entry-points-slot)
948               (fun-next fun)))
949         ((null fun))
950       (let ((fun-offset (sb!kernel:get-closure-length fun)))
951         ;; There is function header fun-offset words from the
952         ;; code header.
953         (format t "Fun-header ~S at offset ~D (words): ~S~A => ~S~%"
954                 fun
955                 fun-offset
956                 (sb!kernel:code-header-ref
957                  code (+ fun-offset sb!vm:function-name-slot))
958                 (sb!kernel:code-header-ref
959                  code (+ fun-offset sb!vm:function-arglist-slot))
960                 (sb!kernel:code-header-ref
961                  code (+ fun-offset sb!vm:function-type-slot)))))))
962 \f
963 ;;; getting at the source code...
964
965 (defstruct (source-form-cache (:conc-name sfcache-)
966                               (:copier nil))
967   (debug-source nil :type (or null sb!di:debug-source))
968   (top-level-form-index -1 :type fixnum)
969   (top-level-form nil :type list)
970   (form-number-mapping-table nil :type (or null (vector list)))
971   (last-location-retrieved nil :type (or null sb!di:code-location))
972   (last-form-retrieved -1 :type fixnum))
973
974 (defun get-top-level-form (debug-source tlf-index)
975   (let ((name (sb!di:debug-source-name debug-source)))
976     (ecase (sb!di:debug-source-from debug-source)
977       (:file
978        (cond ((not (probe-file name))
979               (warn "The source file ~S no longer seems to exist." name)
980               nil)
981              (t
982               (let ((start-positions
983                      (sb!di:debug-source-start-positions debug-source)))
984                 (cond ((null start-positions)
985                        (warn "There is no start positions map.")
986                        nil)
987                       (t
988                        (let* ((local-tlf-index
989                                (- tlf-index
990                                   (sb!di:debug-source-root-number
991                                    debug-source)))
992                               (char-offset
993                                (aref start-positions local-tlf-index)))
994                          (with-open-file (f name)
995                            (cond ((= (sb!di:debug-source-created debug-source)
996                                      (file-write-date name))
997                                   (file-position f char-offset))
998                                  (t
999                                   (warn "Source file ~S has been modified; ~@
1000                                          using form offset instead of file index."
1001                                         name)
1002                                   (let ((*read-suppress* t))
1003                                     (dotimes (i local-tlf-index) (read f)))))
1004                            (let ((*readtable* (copy-readtable)))
1005                              (set-dispatch-macro-character
1006                               #\# #\.
1007                               (lambda (stream sub-char &rest rest)
1008                                 (declare (ignore rest sub-char))
1009                                 (let ((token (read stream t nil t)))
1010                                   (format nil "#.~S" token))))
1011                              (read f))
1012                            ))))))))
1013       (:lisp
1014        (aref name tlf-index)))))
1015
1016 (defun cache-valid (loc cache)
1017   (and cache
1018        (and (eq (sb!di:code-location-debug-source loc)
1019                 (sfcache-debug-source cache))
1020             (eq (sb!di:code-location-top-level-form-offset loc)
1021                 (sfcache-top-level-form-index cache)))))
1022
1023 (defun get-source-form (loc context &optional cache)
1024   (let* ((cache-valid (cache-valid loc cache))
1025          (tlf-index (sb!di:code-location-top-level-form-offset loc))
1026          (form-number (sb!di:code-location-form-number loc))
1027          (top-level-form
1028           (if cache-valid
1029               (sfcache-top-level-form cache)
1030               (get-top-level-form (sb!di:code-location-debug-source loc)
1031                                   tlf-index)))
1032          (mapping-table
1033           (if cache-valid
1034               (sfcache-form-number-mapping-table cache)
1035               (sb!di:form-number-translations top-level-form tlf-index))))
1036     (when (and (not cache-valid) cache)
1037       (setf (sfcache-debug-source cache) (sb!di:code-location-debug-source loc)
1038             (sfcache-top-level-form-index cache) tlf-index
1039             (sfcache-top-level-form cache) top-level-form
1040             (sfcache-form-number-mapping-table cache) mapping-table))
1041     (cond ((null top-level-form)
1042            nil)
1043           ((> form-number (length mapping-table))
1044            (warn "bogus form-number in form!  The source file has probably ~@
1045                   been changed too much to cope with.")
1046            (when cache
1047              ;; Disable future warnings.
1048              (setf (sfcache-top-level-form cache) nil))
1049            nil)
1050           (t
1051            (when cache
1052              (setf (sfcache-last-location-retrieved cache) loc)
1053              (setf (sfcache-last-form-retrieved cache) form-number))
1054            (sb!di:source-path-context top-level-form
1055                                       (aref mapping-table form-number)
1056                                       context)))))
1057
1058 (defun get-different-source-form (loc context &optional cache)
1059   (if (and (cache-valid loc cache)
1060            (or (= (sb!di:code-location-form-number loc)
1061                   (sfcache-last-form-retrieved cache))
1062                (and (sfcache-last-location-retrieved cache)
1063                     (sb!di:code-location=
1064                      loc
1065                      (sfcache-last-location-retrieved cache)))))
1066       (values nil nil)
1067       (values (get-source-form loc context cache) t)))
1068 \f
1069 ;;;; stuff to use debugging-info to augment the disassembly
1070
1071 (defun code-function-map (code)
1072   (declare (type sb!kernel:code-component code))
1073   (sb!di::get-debug-info-function-map (sb!kernel:%code-debug-info code)))
1074
1075 (defstruct (location-group (:copier nil))
1076   (locations #() :type (vector (or list fixnum))))
1077
1078 (defstruct (storage-info (:copier nil))
1079   (groups nil :type list)               ; alist of (name . location-group)
1080   (debug-vars #() :type vector))
1081
1082 ;;; Return the vector of DEBUG-VARs currently associated with DSTATE.
1083 (defun dstate-debug-vars (dstate)
1084   (declare (type disassem-state dstate))
1085   (storage-info-debug-vars (seg-storage-info (dstate-segment dstate))))
1086
1087 ;;; Given the OFFSET of a location within the location-group called
1088 ;;; LG-NAME, see whether there's a current mapping to a source
1089 ;;; variable in DSTATE, and if so, return the offset of that variable
1090 ;;; in the current debug-var vector.
1091 (defun find-valid-storage-location (offset lg-name dstate)
1092   (declare (type offset offset)
1093            (type symbol lg-name)
1094            (type disassem-state dstate))
1095   (let* ((storage-info
1096           (seg-storage-info (dstate-segment dstate)))
1097          (location-group
1098           (and storage-info
1099                (cdr (assoc lg-name (storage-info-groups storage-info)))))
1100          (currently-valid
1101           (dstate-current-valid-locations dstate)))
1102     (and location-group
1103          (not (null currently-valid))
1104          (let ((locations (location-group-locations location-group)))
1105            (and (< offset (length locations))
1106                 (let ((used-by (aref locations offset)))
1107                   (and used-by
1108                        (let ((debug-var-num
1109                               (typecase used-by
1110                                 (fixnum
1111                                  (and (not
1112                                        (zerop (bit currently-valid used-by)))
1113                                       used-by))
1114                                 (list
1115                                  (some (lambda (num)
1116                                          (and (not
1117                                                (zerop
1118                                                 (bit currently-valid num)))
1119                                               num))
1120                                        used-by)))))
1121                          (and debug-var-num
1122                               (progn
1123                                 ;; Found a valid storage reference!
1124                                 ;; can't use it again until it's revalidated...
1125                                 (setf (bit (dstate-current-valid-locations
1126                                             dstate)
1127                                            debug-var-num)
1128                                       0)
1129                                 debug-var-num))
1130                          ))))))))
1131
1132 ;;; Return a new vector which has the same contents as the old one
1133 ;;; VEC, plus new cells (for a total size of NEW-LEN). The additional
1134 ;;; elements are initialized to INITIAL-ELEMENT.
1135 (defun grow-vector (vec new-len &optional initial-element)
1136   (declare (type vector vec)
1137            (type fixnum new-len))
1138   (let ((new
1139          (make-sequence `(vector ,(array-element-type vec) ,new-len)
1140                         new-len
1141                         :initial-element initial-element)))
1142     (dotimes (i (length vec))
1143       (setf (aref new i) (aref vec i)))
1144     new))
1145
1146 ;;; Return a STORAGE-INFO struction describing the object-to-source
1147 ;;; variable mappings from DEBUG-FUNCTION.
1148 (defun storage-info-for-debug-function (debug-function)
1149   (declare (type sb!di:debug-function debug-function))
1150   (let ((sc-vec sb!c::*backend-sc-numbers*)
1151         (groups nil)
1152         (debug-vars (sb!di::debug-function-debug-vars
1153                      debug-function)))
1154     (and debug-vars
1155          (dotimes (debug-var-offset
1156                    (length debug-vars)
1157                    (make-storage-info :groups groups
1158                                       :debug-vars debug-vars))
1159            (let ((debug-var (aref debug-vars debug-var-offset)))
1160              #+nil
1161              (format t ";;; At offset ~D: ~S~%" debug-var-offset debug-var)
1162              (let* ((sc-offset
1163                      (sb!di::compiled-debug-var-sc-offset debug-var))
1164                     (sb-name
1165                      (sb!c:sb-name
1166                       (sb!c:sc-sb (aref sc-vec
1167                                         (sb!c:sc-offset-scn sc-offset))))))
1168                #+nil
1169                (format t ";;; SET: ~S[~D]~%"
1170                        sb-name (sb!c:sc-offset-offset sc-offset))
1171                (unless (null sb-name)
1172                  (let ((group (cdr (assoc sb-name groups))))
1173                    (when (null group)
1174                      (setf group (make-location-group))
1175                      (push `(,sb-name . ,group) groups))
1176                    (let* ((locations (location-group-locations group))
1177                           (length (length locations))
1178                           (offset (sb!c:sc-offset-offset sc-offset)))
1179                      (when (>= offset length)
1180                        (setf locations
1181                              (grow-vector locations
1182                                           (max (* 2 length)
1183                                                (1+ offset))
1184                                           nil)
1185                              (location-group-locations group)
1186                              locations))
1187                      (let ((already-there (aref locations offset)))
1188                        (cond ((null already-there)
1189                               (setf (aref locations offset) debug-var-offset))
1190                              ((eql already-there debug-var-offset))
1191                              (t
1192                               (if (listp already-there)
1193                                   (pushnew debug-var-offset
1194                                            (aref locations offset))
1195                                   (setf (aref locations offset)
1196                                         (list debug-var-offset
1197                                               already-there)))))
1198                        )))))))
1199          )))
1200
1201 (defun source-available-p (debug-function)
1202   (handler-case
1203       (sb!di:do-debug-function-blocks (block debug-function)
1204         (declare (ignore block))
1205         (return t))
1206     (sb!di:no-debug-blocks () nil)))
1207
1208 (defun print-block-boundary (stream dstate)
1209   (let ((os (dstate-output-state dstate)))
1210     (when (not (eq os :beginning))
1211       (when (not (eq os :block-boundary))
1212         (terpri stream))
1213       (setf (dstate-output-state dstate)
1214             :block-boundary))))
1215
1216 ;;; Add hooks to track to track the source code in SEGMENT during
1217 ;;; disassembly. SFCACHE can be either NIL or it can be a
1218 ;;; SOURCE-FORM-CACHE structure, in which case it is used to cache
1219 ;;; forms from files.
1220 (defun add-source-tracking-hooks (segment debug-function &optional sfcache)
1221   (declare (type segment segment)
1222            (type (or null sb!di:debug-function) debug-function)
1223            (type (or null source-form-cache) sfcache))
1224   (let ((last-block-pc -1))
1225     (flet ((add-hook (pc fun &optional before-address)
1226              (push (make-offs-hook
1227                     :offset pc ;; ##### FIX to account for non-zero offs in code
1228                     :function fun
1229                     :before-address before-address)
1230                    (seg-hooks segment))))
1231       (handler-case
1232           (sb!di:do-debug-function-blocks (block debug-function)
1233             (let ((first-location-in-block-p t))
1234               (sb!di:do-debug-block-locations (loc block)
1235                 (let ((pc (sb!di::compiled-code-location-pc loc)))
1236
1237                   ;; Put blank lines in at block boundaries
1238                   (when (and first-location-in-block-p
1239                              (/= pc last-block-pc))
1240                     (setf first-location-in-block-p nil)
1241                     (add-hook pc
1242                               (lambda (stream dstate)
1243                                 (print-block-boundary stream dstate))
1244                               t)
1245                     (setf last-block-pc pc))
1246
1247                   ;; Print out corresponding source; this information is not
1248                   ;; all that accurate, but it's better than nothing
1249                   (unless (zerop (sb!di:code-location-form-number loc))
1250                     (multiple-value-bind (form new)
1251                         (get-different-source-form loc 0 sfcache)
1252                       (when new
1253                          (let ((at-block-begin (= pc last-block-pc)))
1254                            (add-hook
1255                             pc
1256                             (lambda (stream dstate)
1257                               (declare (ignore dstate))
1258                               (when stream
1259                                 (unless at-block-begin
1260                                   (terpri stream))
1261                                 (format stream ";;; [~D] "
1262                                         (sb!di:code-location-form-number
1263                                          loc))
1264                                 (prin1-short form stream)
1265                                 (terpri stream)
1266                                 (terpri stream)))
1267                             t)))))
1268
1269                   ;; Keep track of variable live-ness as best we can.
1270                   (let ((live-set
1271                          (copy-seq (sb!di::compiled-code-location-live-set
1272                                     loc))))
1273                     (add-hook
1274                      pc
1275                      (lambda (stream dstate)
1276                        (declare (ignore stream))
1277                        (setf (dstate-current-valid-locations dstate)
1278                              live-set)
1279                        #+nil
1280                        (note (lambda (stream)
1281                                (let ((*print-length* nil))
1282                                  (format stream "live set: ~S"
1283                                          live-set)))
1284                              dstate))))
1285                   ))))
1286         (sb!di:no-debug-blocks () nil)))))
1287
1288 (defun add-debugging-hooks (segment debug-function &optional sfcache)
1289   (when debug-function
1290     (setf (seg-storage-info segment)
1291           (storage-info-for-debug-function debug-function))
1292     (add-source-tracking-hooks segment debug-function sfcache)
1293     (let ((kind (sb!di:debug-function-kind debug-function)))
1294       (flet ((anh (n)
1295                (push (make-offs-hook
1296                       :offset 0
1297                       :function (lambda (stream dstate)
1298                                   (declare (ignore stream))
1299                                   (note n dstate)))
1300                      (seg-hooks segment))))
1301         (case kind
1302           (:external)
1303           ((nil)
1304            (anh "no-arg-parsing entry point"))
1305           (t
1306            (anh (lambda (stream)
1307                   (format stream "~S entry point" kind)))))))))
1308 \f
1309 ;;; Return a list of the segments of memory containing machine code
1310 ;;; instructions for FUNCTION.
1311 (defun get-function-segments (function)
1312   (declare (type compiled-function function))
1313   (let* ((code (fun-code function))
1314          (function-map (code-function-map code))
1315          (fname (sb!kernel:%function-name function))
1316          (sfcache (make-source-form-cache)))
1317     (let ((first-block-seen-p nil)
1318           (nil-block-seen-p nil)
1319           (last-offset 0)
1320           (last-debug-function nil)
1321           (segments nil))
1322       (flet ((add-seg (offs len df)
1323                (when (> len 0)
1324                  (push (make-code-segment code offs len
1325                                           :debug-function df
1326                                           :source-form-cache sfcache)
1327                        segments))))
1328         (dotimes (fmap-index (length function-map))
1329           (let ((fmap-entry (aref function-map fmap-index)))
1330             (etypecase fmap-entry
1331               (integer
1332                (when first-block-seen-p
1333                  (add-seg last-offset
1334                           (- fmap-entry last-offset)
1335                           last-debug-function)
1336                  (setf last-debug-function nil))
1337                (setf last-offset fmap-entry))
1338               (sb!c::compiled-debug-function
1339                (let ((name (sb!c::compiled-debug-function-name fmap-entry))
1340                      (kind (sb!c::compiled-debug-function-kind fmap-entry)))
1341                  #+nil
1342                  (format t ";;; SAW ~S ~S ~S,~S ~D,~D~%"
1343                          name kind first-block-seen-p nil-block-seen-p
1344                          last-offset
1345                          (sb!c::compiled-debug-function-start-pc fmap-entry))
1346                  (cond (#+nil (eq last-offset fun-offset)
1347                               (and (equal name fname) (not first-block-seen-p))
1348                               (setf first-block-seen-p t))
1349                        ((eq kind :external)
1350                         (when first-block-seen-p
1351                           (return)))
1352                        ((eq kind nil)
1353                         (when nil-block-seen-p
1354                           (return))
1355                         (when first-block-seen-p
1356                           (setf nil-block-seen-p t))))
1357                  (setf last-debug-function
1358                        (sb!di::make-compiled-debug-function fmap-entry code))
1359                  )))))
1360         (let ((max-offset (code-inst-area-length code)))
1361           (when (and first-block-seen-p last-debug-function)
1362             (add-seg last-offset
1363                      (- max-offset last-offset)
1364                      last-debug-function))
1365           (if (null segments)
1366               (let ((offs (fun-insts-offset function)))
1367                 (make-code-segment code offs (- max-offset offs)))
1368               (nreverse segments)))))))
1369
1370 ;;; Return a list of the segments of memory containing machine code
1371 ;;; instructions for the code-component CODE. If START-OFFSET and/or
1372 ;;; LENGTH is supplied, only that part of the code-segment is used
1373 ;;; (but these are constrained to lie within the code-segment).
1374 (defun get-code-segments (code
1375                           &optional
1376                           (start-offset 0)
1377                           (length (code-inst-area-length code)))
1378   (declare (type sb!kernel:code-component code)
1379            (type offset start-offset)
1380            (type length length))
1381   (let ((segments nil))
1382     (when code
1383       (let ((function-map (code-function-map code))
1384             (sfcache (make-source-form-cache)))
1385         (let ((last-offset 0)
1386               (last-debug-function nil))
1387           (flet ((add-seg (offs len df)
1388                    (let* ((restricted-offs
1389                            (min (max start-offset offs)
1390                                 (+ start-offset length)))
1391                           (restricted-len
1392                            (- (min (max start-offset (+ offs len))
1393                                    (+ start-offset length))
1394                               restricted-offs)))
1395                      (when (> restricted-len 0)
1396                        (push (make-code-segment code
1397                                                 restricted-offs restricted-len
1398                                                 :debug-function df
1399                                                 :source-form-cache sfcache)
1400                              segments)))))
1401             (dotimes (fmap-index (length function-map))
1402               (let ((fmap-entry (aref function-map fmap-index)))
1403                 (etypecase fmap-entry
1404                   (integer
1405                    (add-seg last-offset (- fmap-entry last-offset)
1406                             last-debug-function)
1407                    (setf last-debug-function nil)
1408                    (setf last-offset fmap-entry))
1409                   (sb!c::compiled-debug-function
1410                    (setf last-debug-function
1411                          (sb!di::make-compiled-debug-function fmap-entry
1412                                                               code))))))
1413             (when last-debug-function
1414               (add-seg last-offset
1415                        (- (code-inst-area-length code) last-offset)
1416                        last-debug-function))))))
1417     (if (null segments)
1418         (make-code-segment code start-offset length)
1419         (nreverse segments))))
1420 \f
1421 ;;; Return two values: the amount by which the last instruction in the
1422 ;;; segment goes past the end of the segment, and the offset of the
1423 ;;; end of the segment from the beginning of that instruction. If all
1424 ;;; instructions fit perfectly, return 0 and 0.
1425 (defun segment-overflow (segment dstate)
1426   (declare (type segment segment)
1427            (type disassem-state dstate))
1428   (let ((seglen (seg-length segment))
1429         (last-start 0))
1430     (map-segment-instructions (lambda (chunk inst)
1431                                 (declare (ignore chunk inst))
1432                                 (setf last-start (dstate-cur-offs dstate)))
1433                               segment
1434                               dstate)
1435     (values (- (dstate-cur-offs dstate) seglen)
1436             (- seglen last-start))))
1437
1438 ;;; Compute labels for all the memory segments in SEGLIST and adds
1439 ;;; them to DSTATE. It's important to call this function with all the
1440 ;;; segments you're interested in, so that it can find references from
1441 ;;; one to another.
1442 (defun label-segments (seglist dstate)
1443   (declare (type list seglist)
1444            (type disassem-state dstate))
1445   (dolist (seg seglist)
1446     (add-segment-labels seg dstate))
1447   ;; Now remove any labels that don't point anywhere in the segments
1448   ;; we have.
1449   (setf (dstate-labels dstate)
1450         (remove-if (lambda (lab)
1451                      (not
1452                       (some (lambda (seg)
1453                               (let ((start (seg-virtual-location seg)))
1454                                 (<= start
1455                                     (car lab)
1456                                     (+ start (seg-length seg)))))
1457                             seglist)))
1458                    (dstate-labels dstate))))
1459
1460 ;;; Disassemble the machine code instructions in SEGMENT to STREAM.
1461 (defun disassemble-segment (segment stream dstate)
1462   (declare (type segment segment)
1463            (type stream stream)
1464            (type disassem-state dstate))
1465   (let ((*print-pretty* nil)) ; otherwise the pp conses hugely
1466     (number-labels dstate)
1467     (map-segment-instructions
1468      (lambda (chunk inst)
1469        (declare (type dchunk chunk) (type instruction inst))
1470        (let ((printer (inst-printer inst)))
1471          (when printer
1472            (funcall printer chunk inst stream dstate))))
1473      segment
1474      dstate
1475      stream)))
1476
1477 ;;; Disassemble the machine code instructions in each memory segment
1478 ;;; in SEGMENTS in turn to STREAM.
1479 (defun disassemble-segments (segments stream dstate)
1480   (declare (type list segments)
1481            (type stream stream)
1482            (type disassem-state dstate))
1483   (unless (null segments)
1484     (let ((first (car segments))
1485           (last (car (last segments))))
1486       (set-location-printing-range dstate
1487                                   (seg-virtual-location first)
1488                                   (- (+ (seg-virtual-location last)
1489                                         (seg-length last))
1490                                      (seg-virtual-location first)))
1491       (setf (dstate-output-state dstate) :beginning)
1492       (dolist (seg segments)
1493         (disassemble-segment seg stream dstate)))))
1494 \f
1495 ;;;; top-level functions
1496
1497 ;;; Disassemble the machine code instructions for FUNCTION.
1498 (defun disassemble-function (function &key
1499                                       (stream *standard-output*)
1500                                       (use-labels t))
1501   (declare (type compiled-function function)
1502            (type stream stream)
1503            (type (member t nil) use-labels))
1504   (let* ((dstate (make-dstate))
1505          (segments (get-function-segments function)))
1506     (when use-labels
1507       (label-segments segments dstate))
1508     (disassemble-segments segments stream dstate)))
1509
1510 (defun compile-function-lambda-expr (function)
1511   (declare (type function function))
1512   (multiple-value-bind (lambda closurep name)
1513       (function-lambda-expression function)
1514     (declare (ignore name))
1515     (when closurep
1516       (error "can't compile a lexical closure"))
1517     (compile nil lambda)))
1518
1519 (defun compiled-function-or-lose (thing &optional (name thing))
1520   (cond ((or (symbolp thing)
1521              (and (listp thing)
1522                   (eq (car thing) 'setf)))
1523          (compiled-function-or-lose (fdefinition thing) thing))
1524         ((sb!eval:interpreted-function-p thing)
1525          (compile-function-lambda-expr thing))
1526         ((functionp thing)
1527          thing)
1528         ((and (listp thing)
1529               (eq (car thing) 'sb!impl::lambda))
1530          (compile nil thing))
1531         (t
1532          (error "can't make a compiled function from ~S" name))))
1533
1534 (defun disassemble (object &key
1535                            (stream *standard-output*)
1536                            (use-labels t))
1537   #!+sb-doc
1538   "Disassemble the machine code associated with OBJECT, which can be a
1539   function, a lambda expression, or a symbol with a function definition. If
1540   it is not already compiled, the compiler is called to produce something to
1541   disassemble."
1542   (declare (type (or function symbol cons) object)
1543            (type (or (member t) stream) stream)
1544            (type (member t nil) use-labels))
1545   (pprint-logical-block (*standard-output* nil :per-line-prefix "; ")
1546   (let ((fun (compiled-function-or-lose object)))
1547     (if (typep fun 'sb!kernel:byte-function)
1548         (sb!c:disassem-byte-fun fun)
1549         ;; We can't detect closures, so be careful.
1550         (disassemble-function (fun-self fun)
1551                               :stream stream
1552                               :use-labels use-labels)))
1553   nil))
1554
1555 ;;; Disassembles the given area of memory starting at ADDRESS and
1556 ;;; LENGTH long. Note that if CODE-COMPONENT is NIL and this memory
1557 ;;; could move during a GC, you'd better disable it around the call to
1558 ;;; this function.
1559 (defun disassemble-memory (address
1560                            length
1561                            &key
1562                            (stream *standard-output*)
1563                            code-component
1564                            (use-labels t))
1565   (declare (type (or address sb!sys:system-area-pointer) address)
1566            (type length length)
1567            (type stream stream)
1568            (type (or null sb!kernel:code-component) code-component)
1569            (type (member t nil) use-labels))
1570   (let* ((address
1571           (if (sb!sys:system-area-pointer-p address)
1572               (sb!sys:sap-int address)
1573               address))
1574          (dstate (make-dstate))
1575          (segments
1576           (if code-component
1577               (let ((code-offs
1578                      (- address
1579                         (sb!sys:sap-int
1580                          (sb!kernel:code-instructions code-component)))))
1581                 (when (or (< code-offs 0)
1582                           (> code-offs (code-inst-area-length code-component)))
1583                   (error "address ~X not in the code component ~S"
1584                          address code-component))
1585                 (get-code-segments code-component code-offs length))
1586               (list (make-memory-segment address length)))))
1587     (when use-labels
1588       (label-segments segments dstate))
1589     (disassemble-segments segments stream dstate)))
1590
1591 ;;; Disassemble the machine code instructions associated with
1592 ;;; CODE-COMPONENT (this may include multiple entry points).
1593 (defun disassemble-code-component (code-component &key
1594                                                   (stream *standard-output*)
1595                                                   (use-labels t))
1596   (declare (type (or null sb!kernel:code-component compiled-function)
1597                  code-component)
1598            (type stream stream)
1599            (type (member t nil) use-labels))
1600   (let* ((code-component
1601           (if (functionp code-component)
1602               (fun-code code-component)
1603               code-component))
1604          (dstate (make-dstate))
1605          (segments (get-code-segments code-component)))
1606     (when use-labels
1607       (label-segments segments dstate))
1608     (disassemble-segments segments stream dstate)))
1609 \f
1610 ;;; code for making useful segments from arbitrary lists of code-blocks
1611
1612 ;;; the maximum size of an instruction. Note that this includes
1613 ;;; pseudo-instructions like error traps with their associated
1614 ;;; operands, so it should be big enough to include them, i.e. it's
1615 ;;; not just 4 on a risc machine!
1616 (defconstant max-instruction-size 16)
1617
1618 (defun add-block-segments (seg-code-block
1619                            seglist
1620                            location
1621                            connecting-vec
1622                            dstate)
1623   (declare (type list seglist)
1624            (type integer location)
1625            (type (or null (vector (unsigned-byte 8))) connecting-vec)
1626            (type disassem-state dstate))
1627   (flet ((addit (seg overflow)
1628            (let ((length (+ (seg-length seg) overflow)))
1629              (when (> length 0)
1630                (setf (seg-length seg) length)
1631                (incf location length)
1632                (push seg seglist)))))
1633     (let ((connecting-overflow 0)
1634           (amount (length seg-code-block)))
1635       (when connecting-vec
1636         ;; Tack on some of the new block to the old overflow vector.
1637         (let* ((beginning-of-block-amount
1638                 (if seg-code-block (min max-instruction-size amount) 0))
1639                (connecting-vec
1640                 (if seg-code-block
1641                     (concatenate
1642                      '(vector (unsigned-byte 8))
1643                      connecting-vec
1644                      (subseq seg-code-block 0 beginning-of-block-amount))
1645                     connecting-vec)))
1646           (when (and (< (length connecting-vec) max-instruction-size)
1647                      (not (null seg-code-block)))
1648             (return-from add-block-segments
1649               ;; We want connecting vectors to be large enough to hold
1650               ;; any instruction, and since the current seg-code-block
1651               ;; wasn't large enough to do this (and is now entirely
1652               ;; on the end of the overflow-vector), just save it for
1653               ;; next time.
1654               (values seglist location connecting-vec)))
1655           (when (> (length connecting-vec) 0)
1656             (let ((seg
1657                    (make-vector-segment connecting-vec
1658                                         0
1659                                         (- (length connecting-vec)
1660                                            beginning-of-block-amount)
1661                                         :virtual-location location)))
1662               (setf connecting-overflow (segment-overflow seg dstate))
1663               (addit seg connecting-overflow)))))
1664       (cond ((null seg-code-block)
1665              ;; nothing more to add
1666              (values seglist location nil))
1667             ((< (- amount connecting-overflow) max-instruction-size)
1668              ;; We can't create a segment with the minimum size
1669              ;; required for an instruction, so just keep on accumulating
1670              ;; in the overflow vector for the time-being.
1671              (values seglist
1672                      location
1673                      (subseq seg-code-block connecting-overflow amount)))
1674             (t
1675              ;; Put as much as we can into a new segment, and the rest
1676              ;; into the overflow-vector.
1677              (let* ((initial-length
1678                      (- amount connecting-overflow max-instruction-size))
1679                     (seg
1680                      (make-vector-segment seg-code-block
1681                                           connecting-overflow
1682                                           initial-length
1683                                           :virtual-location location))
1684                     (overflow
1685                      (segment-overflow seg dstate)))
1686                (addit seg overflow)
1687                (values seglist
1688                        location
1689                        (subseq seg-code-block
1690                                (+ connecting-overflow (seg-length seg))
1691                                amount))))))))
1692 \f
1693 ;;;; code to disassemble assembler segments
1694
1695 (defun assem-segment-to-disassem-segments (assem-segment dstate)
1696   (declare (type sb!assem:segment assem-segment)
1697            (type disassem-state dstate))
1698   (let ((location 0)
1699         (disassem-segments nil)
1700         (connecting-vec nil))
1701     (sb!assem:on-segment-contents-vectorly
1702      assem-segment
1703      (lambda (seg-code-block)
1704        (multiple-value-setq (disassem-segments location connecting-vec)
1705          (add-block-segments seg-code-block
1706                              disassem-segments
1707                              location
1708                              connecting-vec
1709                              dstate))))
1710     (when connecting-vec
1711       (setf disassem-segments
1712             (add-block-segments nil
1713                                 disassem-segments
1714                                 location
1715                                 connecting-vec
1716                                 dstate)))
1717     (sort disassem-segments #'< :key #'seg-virtual-location)))
1718
1719 ;;; Disassemble the machine code instructions associated with
1720 ;;; ASSEM-SEGMENT (of type assem:segment).
1721 (defun disassemble-assem-segment (assem-segment stream)
1722   (declare (type sb!assem:segment assem-segment)
1723            (type stream stream))
1724   (let* ((dstate (make-dstate))
1725          (disassem-segments
1726           (assem-segment-to-disassem-segments assem-segment dstate)))
1727     (label-segments disassem-segments dstate)
1728     (disassemble-segments disassem-segments stream dstate)))
1729 \f
1730 ;;; routines to find things in the Lisp environment
1731
1732 ;;; an alist of (SYMBOL-SLOT-OFFSET . ACCESS-FUNCTION-NAME) for slots
1733 ;;; in a symbol object that we know about
1734 (defparameter *grokked-symbol-slots*
1735   (sort `((,sb!vm:symbol-value-slot . symbol-value)
1736           (,sb!vm:symbol-plist-slot . symbol-plist)
1737           (,sb!vm:symbol-name-slot . symbol-name)
1738           (,sb!vm:symbol-package-slot . symbol-package))
1739         #'<
1740         :key #'car))
1741
1742 ;;; Given ADDRESS, try and figure out if which slot of which symbol is
1743 ;;; being referred to. Of course we can just give up, so it's not a
1744 ;;; big deal... Return two values, the symbol and the name of the
1745 ;;; access function of the slot.
1746 (defun grok-symbol-slot-ref (address)
1747   (declare (type address address))
1748   (if (not (aligned-p address sb!vm:word-bytes))
1749       (values nil nil)
1750       (do ((slots-tail *grokked-symbol-slots* (cdr slots-tail)))
1751           ((null slots-tail)
1752            (values nil nil))
1753         (let* ((field (car slots-tail))
1754                (slot-offset (words-to-bytes (car field)))
1755                (maybe-symbol-addr (- address slot-offset))
1756                (maybe-symbol
1757                 (sb!kernel:make-lisp-obj
1758                  (+ maybe-symbol-addr sb!vm:other-pointer-type))))
1759           (when (symbolp maybe-symbol)
1760             (return (values maybe-symbol (cdr field))))))))
1761
1762 (defvar *address-of-nil-object* (sb!kernel:get-lisp-obj-address nil))
1763
1764 ;;; Given a BYTE-OFFSET from NIL, try and figure out which slot of
1765 ;;; which symbol is being referred to. Of course we can just give up,
1766 ;;; so it's not a big deal... Return two values, the symbol and the
1767 ;;; access function.
1768 (defun grok-nil-indexed-symbol-slot-ref (byte-offset)
1769   (declare (type offset byte-offset))
1770   (grok-symbol-slot-ref (+ *address-of-nil-object* byte-offset)))
1771
1772 ;;; Return the Lisp object located BYTE-OFFSET from NIL.
1773 (defun get-nil-indexed-object (byte-offset)
1774   (declare (type offset byte-offset))
1775   (sb!kernel:make-lisp-obj (+ *address-of-nil-object* byte-offset)))
1776
1777 ;;; Return two values; the Lisp object located at BYTE-OFFSET in the
1778 ;;; constant area of the code-object in the current segment and T, or
1779 ;;; NIL and NIL if there is no code-object in the current segment.
1780 (defun get-code-constant (byte-offset dstate)
1781   #!+sb-doc
1782   (declare (type offset byte-offset)
1783            (type disassem-state dstate))
1784   (let ((code (seg-code (dstate-segment dstate))))
1785     (if code
1786         (values
1787          (sb!kernel:code-header-ref code
1788                                     (ash (+ byte-offset
1789                                             sb!vm:other-pointer-type)
1790                                          (- sb!vm:word-shift)))
1791          t)
1792         (values nil nil))))
1793
1794 (defun get-code-constant-absolute (addr dstate)
1795   (declare (type address addr))
1796   (declare (type disassem-state dstate))
1797   (let ((code (seg-code (dstate-segment dstate))))
1798     (if (null code)
1799       (return-from get-code-constant-absolute (values nil nil)))
1800     (let ((code-size (ash (sb!kernel:get-header-data code) sb!vm:word-shift)))
1801       (sb!sys:without-gcing
1802        (let ((code-addr (- (sb!kernel:get-lisp-obj-address code)
1803                            sb!vm:other-pointer-type)))
1804          (if (or (< addr code-addr) (>= addr (+ code-addr code-size)))
1805            (values nil nil)
1806            (values (sb!kernel:code-header-ref
1807                     code
1808                     (ash (- addr code-addr) (- sb!vm:word-shift)))
1809                    t)))))))
1810
1811 (defvar *assembler-routines-by-addr* nil)
1812
1813 (defvar *foreign-symbols-by-addr* nil)
1814
1815 ;;; Build an address-name hash-table from the name-address hash
1816 (defun invert-address-hash (htable &optional (addr-hash (make-hash-table)))
1817   (maphash (lambda (name address)
1818              (setf (gethash address addr-hash) name))
1819            htable)
1820   addr-hash)
1821
1822 ;;; Return the name of the primitive Lisp assembler routine or foreign
1823 ;;; symbol located at ADDRESS, or NIL if there isn't one.
1824 (defun find-assembler-routine (address)
1825   (declare (type address address))
1826   (when (null *assembler-routines-by-addr*)
1827     (setf *assembler-routines-by-addr*
1828           (invert-address-hash sb!fasl:*assembler-routines*))
1829     (setf *assembler-routines-by-addr*
1830           (invert-address-hash sb!fasl:*static-foreign-symbols*
1831                                *assembler-routines-by-addr*)))
1832   (gethash address *assembler-routines-by-addr*))
1833 \f
1834 ;;;; some handy function for machine-dependent code to use...
1835
1836 #!-sb-fluid (declaim (maybe-inline sap-ref-int read-suffix))
1837
1838 (defun sap-ref-int (sap offset length byte-order)
1839   (declare (type sb!sys:system-area-pointer sap)
1840            (type (unsigned-byte 16) offset)
1841            (type (member 1 2 4) length)
1842            (type (member :little-endian :big-endian) byte-order)
1843            (optimize (speed 3) (safety 0)))
1844   (ecase length
1845     (1 (sb!sys:sap-ref-8 sap offset))
1846     (2 (if (eq byte-order :big-endian)
1847            (+ (ash (sb!sys:sap-ref-8 sap offset) 8)
1848               (sb!sys:sap-ref-8 sap (+ offset 1)))
1849            (+ (ash (sb!sys:sap-ref-8 sap (+ offset 1)) 8)
1850               (sb!sys:sap-ref-8 sap offset))))
1851     (4 (if (eq byte-order :big-endian)
1852            (+ (ash (sb!sys:sap-ref-8 sap offset) 24)
1853               (ash (sb!sys:sap-ref-8 sap (+ 1 offset)) 16)
1854               (ash (sb!sys:sap-ref-8 sap (+ 2 offset)) 8)
1855               (sb!sys:sap-ref-8 sap (+ 3 offset)))
1856            (+ (sb!sys:sap-ref-8 sap offset)
1857               (ash (sb!sys:sap-ref-8 sap (+ 1 offset)) 8)
1858               (ash (sb!sys:sap-ref-8 sap (+ 2 offset)) 16)
1859               (ash (sb!sys:sap-ref-8 sap (+ 3 offset)) 24))))))
1860
1861 (defun read-suffix (length dstate)
1862   (declare (type (member 8 16 32) length)
1863            (type disassem-state dstate)
1864            (optimize (speed 3) (safety 0)))
1865   (let ((length (ecase length (8 1) (16 2) (32 4))))
1866     (declare (type (unsigned-byte 3) length))
1867     (prog1
1868       (sap-ref-int (dstate-segment-sap dstate)
1869                    (dstate-next-offs dstate)
1870                    length
1871                    (dstate-byte-order dstate))
1872       (incf (dstate-next-offs dstate) length))))
1873 \f
1874 ;;;; optional routines to make notes about code
1875
1876 ;;; Store NOTE (which can be either a string or a function with a
1877 ;;; single stream argument) to be printed as an end-of-line comment
1878 ;;; after the current instruction is disassembled.
1879 (defun note (note dstate)
1880   (declare (type (or string function) note)
1881            (type disassem-state dstate))
1882   (push note (dstate-notes dstate)))
1883
1884 (defun prin1-short (thing stream)
1885   (with-print-restrictions
1886     (prin1 thing stream)))
1887
1888 (defun prin1-quoted-short (thing stream)
1889   (if (self-evaluating-p thing)
1890       (prin1-short thing stream)
1891       (prin1-short `',thing stream)))
1892
1893 ;;; Store a note about the lisp constant located BYTE-OFFSET bytes
1894 ;;; from the current code-component, to be printed as an end-of-line
1895 ;;; comment after the current instruction is disassembled.
1896 (defun note-code-constant (byte-offset dstate)
1897   (declare (type offset byte-offset)
1898            (type disassem-state dstate))
1899   (multiple-value-bind (const valid)
1900       (get-code-constant byte-offset dstate)
1901     (when valid
1902       (note (lambda (stream)
1903               (prin1-quoted-short const stream))
1904             dstate))
1905     const))
1906
1907 ;;; Store a note about the lisp constant located at ADDR in the
1908 ;;; current code-component, to be printed as an end-of-line comment
1909 ;;; after the current instruction is disassembled.
1910 (defun note-code-constant-absolute (addr dstate)
1911   (declare (type address addr)
1912            (type disassem-state dstate))
1913   (multiple-value-bind (const valid)
1914       (get-code-constant-absolute addr dstate)
1915     (when valid
1916       (note (lambda (stream)
1917               (prin1-quoted-short const stream))
1918             dstate))
1919     (values const valid)))
1920
1921 ;;; If the memory address located NIL-BYTE-OFFSET bytes from the
1922 ;;; constant NIL is a valid slot in a symbol, store a note describing
1923 ;;; which symbol and slot, to be printed as an end-of-line comment
1924 ;;; after the current instruction is disassembled. Returns non-NIL iff
1925 ;;; a note was recorded.
1926 (defun maybe-note-nil-indexed-symbol-slot-ref (nil-byte-offset dstate)
1927   (declare (type offset nil-byte-offset)
1928            (type disassem-state dstate))
1929   (multiple-value-bind (symbol access-fun)
1930       (grok-nil-indexed-symbol-slot-ref nil-byte-offset)
1931     (when access-fun
1932       (note (lambda (stream)
1933               (prin1 (if (eq access-fun 'symbol-value)
1934                          symbol
1935                          `(,access-fun ',symbol))
1936                      stream))
1937             dstate))
1938     access-fun))
1939
1940 ;;; If the memory address located NIL-BYTE-OFFSET bytes from the
1941 ;;; constant NIL is a valid lisp object, store a note describing which
1942 ;;; symbol and slot, to be printed as an end-of-line comment after the
1943 ;;; current instruction is disassembled. Returns non-NIL iff a note
1944 ;;; was recorded.
1945 (defun maybe-note-nil-indexed-object (nil-byte-offset dstate)
1946   (declare (type offset nil-byte-offset)
1947            (type disassem-state dstate))
1948   (let ((obj (get-nil-indexed-object nil-byte-offset)))
1949     (note (lambda (stream)
1950             (prin1-quoted-short obj stream))
1951           dstate)
1952     t))
1953
1954 ;;; If ADDRESS is the address of a primitive assembler routine or
1955 ;;; foreign symbol, store a note describing which one, to be printed
1956 ;;; as an end-of-line comment after the current instruction is
1957 ;;; disassembled. Returns non-NIL iff a note was recorded. If
1958 ;;; NOTE-ADDRESS-P is non-NIL, a note of the address is also made.
1959 (defun maybe-note-assembler-routine (address note-address-p dstate)
1960   (declare (type disassem-state dstate))
1961   (unless (typep address 'address)
1962     (return-from maybe-note-assembler-routine nil))
1963   (let ((name (find-assembler-routine address)))
1964     (unless (null name)
1965       (note (lambda (stream)
1966               (if note-address-p
1967                   (format stream "#x~8,'0x: ~a" address name)
1968                   (princ name stream)))
1969             dstate))
1970     name))
1971
1972 ;;; If there's a valid mapping from OFFSET in the storage class
1973 ;;; SC-NAME to a source variable, make a note of the source-variable
1974 ;;; name, to be printed as an end-of-line comment after the current
1975 ;;; instruction is disassembled. Returns non-NIL iff a note was
1976 ;;; recorded.
1977 (defun maybe-note-single-storage-ref (offset sc-name dstate)
1978   (declare (type offset offset)
1979            (type symbol sc-name)
1980            (type disassem-state dstate))
1981   (let ((storage-location
1982          (find-valid-storage-location offset sc-name dstate)))
1983     (when storage-location
1984       (note (lambda (stream)
1985               (princ (sb!di:debug-var-symbol
1986                       (aref (storage-info-debug-vars
1987                              (seg-storage-info (dstate-segment dstate)))
1988                             storage-location))
1989                      stream))
1990             dstate)
1991       t)))
1992
1993 ;;; If there's a valid mapping from OFFSET in the storage-base called
1994 ;;; SB-NAME to a source variable, make a note equating ASSOC-WITH with
1995 ;;; the source-variable name, to be printed as an end-of-line comment
1996 ;;; after the current instruction is disassembled. Returns non-NIL iff
1997 ;;; a note was recorded.
1998 (defun maybe-note-associated-storage-ref (offset sb-name assoc-with dstate)
1999   (declare (type offset offset)
2000            (type symbol sb-name)
2001            (type (or symbol string) assoc-with)
2002            (type disassem-state dstate))
2003   (let ((storage-location
2004          (find-valid-storage-location offset sb-name dstate)))
2005     (when storage-location
2006       (note (lambda (stream)
2007               (format stream "~A = ~S"
2008                       assoc-with
2009                       (sb!di:debug-var-symbol
2010                        (aref (dstate-debug-vars dstate)
2011                              storage-location))
2012                       stream))
2013             dstate)
2014       t)))
2015 \f
2016 (defun get-internal-error-name (errnum)
2017   (car (svref sb!c:*backend-internal-errors* errnum)))
2018
2019 (defun get-sc-name (sc-offs)
2020   (sb!c::location-print-name
2021    ;; FIXME: This seems like an awful lot of computation just to get a name.
2022    ;; Couldn't we just use lookup in *BACKEND-SC-NAMES*, without having to cons
2023    ;; up a new object?
2024    (sb!c:make-random-tn :kind :normal
2025                         :sc (svref sb!c:*backend-sc-numbers*
2026                                    (sb!c:sc-offset-scn sc-offs))
2027                         :offset (sb!c:sc-offset-offset sc-offs))))
2028
2029 ;;; When called from an error break instruction's :DISASSEM-CONTROL (or
2030 ;;; :DISASSEM-PRINTER) function, will correctly deal with printing the
2031 ;;; arguments to the break.
2032 ;;;
2033 ;;; ERROR-PARSE-FUN should be a function that accepts:
2034 ;;;   1) a SYSTEM-AREA-POINTER
2035 ;;;   2) a BYTE-OFFSET from the SAP to begin at
2036 ;;;   3) optionally, LENGTH-ONLY, which if non-NIL, means to only return
2037 ;;;      the byte length of the arguments (to avoid unnecessary consing)
2038 ;;; It should read information from the SAP starting at BYTE-OFFSET, and
2039 ;;; return four values:
2040 ;;;   1) the error number
2041 ;;;   2) the total length, in bytes, of the information
2042 ;;;   3) a list of SC-OFFSETs of the locations of the error parameters
2043 ;;;   4) a list of the length (as read from the SAP), in bytes, of each
2044 ;;;      of the return values.
2045 (defun handle-break-args (error-parse-fun stream dstate)
2046   (declare (type function error-parse-fun)
2047            (type (or null stream) stream)
2048            (type disassem-state dstate))
2049   (multiple-value-bind (errnum adjust sc-offsets lengths)
2050       (funcall error-parse-fun
2051                (dstate-segment-sap dstate)
2052                (dstate-next-offs dstate)
2053                (null stream))
2054     (when stream
2055       (setf (dstate-cur-offs dstate)
2056             (dstate-next-offs dstate))
2057       (flet ((emit-err-arg (note)
2058                (let ((num (pop lengths)))
2059                  (print-notes-and-newline stream dstate)
2060                  (print-current-address stream dstate)
2061                  (print-bytes num stream dstate)
2062                  (incf (dstate-cur-offs dstate) num)
2063                  (when note
2064                    (note note dstate)))))
2065         (emit-err-arg nil)
2066         (emit-err-arg (symbol-name (get-internal-error-name errnum)))
2067         (dolist (sc-offs sc-offsets)
2068           (emit-err-arg (get-sc-name sc-offs)))))
2069     (incf (dstate-next-offs dstate)
2070           adjust)))