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