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