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