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