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