0.pre7.94:
[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 ;;;     fun-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 (missing-arg) :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 (missing-arg)
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[~W]~:[ (#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 (missing-arg) :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             "+~W~@[ 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 fun-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~W~%" '.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~W" 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~W:" (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 ~W (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   (toplevel-form-index -1 :type fixnum)
977   (toplevel-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-toplevel-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 ~
1009                                          file index."
1010                                         name)
1011                                   (let ((*read-suppress* t))
1012                                     (dotimes (i local-tlf-index) (read f)))))
1013                            (let ((*readtable* (copy-readtable)))
1014                              (set-dispatch-macro-character
1015                               #\# #\.
1016                               (lambda (stream sub-char &rest rest)
1017                                 (declare (ignore rest sub-char))
1018                                 (let ((token (read stream t nil t)))
1019                                   (format nil "#.~S" token))))
1020                              (read f))
1021                            ))))))))
1022       (:lisp
1023        (aref name tlf-index)))))
1024
1025 (defun cache-valid (loc cache)
1026   (and cache
1027        (and (eq (sb!di:code-location-debug-source loc)
1028                 (sfcache-debug-source cache))
1029             (eq (sb!di:code-location-toplevel-form-offset loc)
1030                 (sfcache-toplevel-form-index cache)))))
1031
1032 (defun get-source-form (loc context &optional cache)
1033   (let* ((cache-valid (cache-valid loc cache))
1034          (tlf-index (sb!di:code-location-toplevel-form-offset loc))
1035          (form-number (sb!di:code-location-form-number loc))
1036          (toplevel-form
1037           (if cache-valid
1038               (sfcache-toplevel-form cache)
1039               (get-toplevel-form (sb!di:code-location-debug-source loc)
1040                                   tlf-index)))
1041          (mapping-table
1042           (if cache-valid
1043               (sfcache-form-number-mapping-table cache)
1044               (sb!di:form-number-translations toplevel-form tlf-index))))
1045     (when (and (not cache-valid) cache)
1046       (setf (sfcache-debug-source cache) (sb!di:code-location-debug-source loc)
1047             (sfcache-toplevel-form-index cache) tlf-index
1048             (sfcache-toplevel-form cache) toplevel-form
1049             (sfcache-form-number-mapping-table cache) mapping-table))
1050     (cond ((null toplevel-form)
1051            nil)
1052           ((> form-number (length mapping-table))
1053            (warn "bogus form-number in form!  The source file has probably ~@
1054                   been changed too much to cope with.")
1055            (when cache
1056              ;; Disable future warnings.
1057              (setf (sfcache-toplevel-form cache) nil))
1058            nil)
1059           (t
1060            (when cache
1061              (setf (sfcache-last-location-retrieved cache) loc)
1062              (setf (sfcache-last-form-retrieved cache) form-number))
1063            (sb!di:source-path-context toplevel-form
1064                                       (aref mapping-table form-number)
1065                                       context)))))
1066
1067 (defun get-different-source-form (loc context &optional cache)
1068   (if (and (cache-valid loc cache)
1069            (or (= (sb!di:code-location-form-number loc)
1070                   (sfcache-last-form-retrieved cache))
1071                (and (sfcache-last-location-retrieved cache)
1072                     (sb!di:code-location=
1073                      loc
1074                      (sfcache-last-location-retrieved cache)))))
1075       (values nil nil)
1076       (values (get-source-form loc context cache) t)))
1077 \f
1078 ;;;; stuff to use debugging info to augment the disassembly
1079
1080 (defun code-fun-map (code)
1081   (declare (type sb!kernel:code-component code))
1082   (sb!c::compiled-debug-info-fun-map (sb!kernel:%code-debug-info code)))
1083
1084 (defstruct (location-group (:copier nil))
1085   (locations #() :type (vector (or list fixnum))))
1086
1087 (defstruct (storage-info (:copier nil))
1088   (groups nil :type list)               ; alist of (name . location-group)
1089   (debug-vars #() :type vector))
1090
1091 ;;; Return the vector of DEBUG-VARs currently associated with DSTATE.
1092 (defun dstate-debug-vars (dstate)
1093   (declare (type disassem-state dstate))
1094   (storage-info-debug-vars (seg-storage-info (dstate-segment dstate))))
1095
1096 ;;; Given the OFFSET of a location within the location-group called
1097 ;;; LG-NAME, see whether there's a current mapping to a source
1098 ;;; variable in DSTATE, and if so, return the offset of that variable
1099 ;;; in the current debug-var vector.
1100 (defun find-valid-storage-location (offset lg-name dstate)
1101   (declare (type offset offset)
1102            (type symbol lg-name)
1103            (type disassem-state dstate))
1104   (let* ((storage-info
1105           (seg-storage-info (dstate-segment dstate)))
1106          (location-group
1107           (and storage-info
1108                (cdr (assoc lg-name (storage-info-groups storage-info)))))
1109          (currently-valid
1110           (dstate-current-valid-locations dstate)))
1111     (and location-group
1112          (not (null currently-valid))
1113          (let ((locations (location-group-locations location-group)))
1114            (and (< offset (length locations))
1115                 (let ((used-by (aref locations offset)))
1116                   (and used-by
1117                        (let ((debug-var-num
1118                               (typecase used-by
1119                                 (fixnum
1120                                  (and (not
1121                                        (zerop (bit currently-valid used-by)))
1122                                       used-by))
1123                                 (list
1124                                  (some (lambda (num)
1125                                          (and (not
1126                                                (zerop
1127                                                 (bit currently-valid num)))
1128                                               num))
1129                                        used-by)))))
1130                          (and debug-var-num
1131                               (progn
1132                                 ;; Found a valid storage reference!
1133                                 ;; can't use it again until it's revalidated...
1134                                 (setf (bit (dstate-current-valid-locations
1135                                             dstate)
1136                                            debug-var-num)
1137                                       0)
1138                                 debug-var-num))
1139                          ))))))))
1140
1141 ;;; Return a new vector which has the same contents as the old one
1142 ;;; VEC, plus new cells (for a total size of NEW-LEN). The additional
1143 ;;; elements are initialized to INITIAL-ELEMENT.
1144 (defun grow-vector (vec new-len &optional initial-element)
1145   (declare (type vector vec)
1146            (type fixnum new-len))
1147   (let ((new
1148          (make-sequence `(vector ,(array-element-type vec) ,new-len)
1149                         new-len
1150                         :initial-element initial-element)))
1151     (dotimes (i (length vec))
1152       (setf (aref new i) (aref vec i)))
1153     new))
1154
1155 ;;; Return a STORAGE-INFO struction describing the object-to-source
1156 ;;; variable mappings from DEBUG-FUN.
1157 (defun storage-info-for-debug-fun (debug-fun)
1158   (declare (type sb!di:debug-fun debug-fun))
1159   (let ((sc-vec sb!c::*backend-sc-numbers*)
1160         (groups nil)
1161         (debug-vars (sb!di::debug-fun-debug-vars
1162                      debug-fun)))
1163     (and debug-vars
1164          (dotimes (debug-var-offset
1165                    (length debug-vars)
1166                    (make-storage-info :groups groups
1167                                       :debug-vars debug-vars))
1168            (let ((debug-var (aref debug-vars debug-var-offset)))
1169              #+nil
1170              (format t ";;; At offset ~W: ~S~%" debug-var-offset debug-var)
1171              (let* ((sc-offset
1172                      (sb!di::compiled-debug-var-sc-offset debug-var))
1173                     (sb-name
1174                      (sb!c:sb-name
1175                       (sb!c:sc-sb (aref sc-vec
1176                                         (sb!c:sc-offset-scn sc-offset))))))
1177                #+nil
1178                (format t ";;; SET: ~S[~W]~%"
1179                        sb-name (sb!c:sc-offset-offset sc-offset))
1180                (unless (null sb-name)
1181                  (let ((group (cdr (assoc sb-name groups))))
1182                    (when (null group)
1183                      (setf group (make-location-group))
1184                      (push `(,sb-name . ,group) groups))
1185                    (let* ((locations (location-group-locations group))
1186                           (length (length locations))
1187                           (offset (sb!c:sc-offset-offset sc-offset)))
1188                      (when (>= offset length)
1189                        (setf locations
1190                              (grow-vector locations
1191                                           (max (* 2 length)
1192                                                (1+ offset))
1193                                           nil)
1194                              (location-group-locations group)
1195                              locations))
1196                      (let ((already-there (aref locations offset)))
1197                        (cond ((null already-there)
1198                               (setf (aref locations offset) debug-var-offset))
1199                              ((eql already-there debug-var-offset))
1200                              (t
1201                               (if (listp already-there)
1202                                   (pushnew debug-var-offset
1203                                            (aref locations offset))
1204                                   (setf (aref locations offset)
1205                                         (list debug-var-offset
1206                                               already-there)))))
1207                        )))))))
1208          )))
1209
1210 (defun source-available-p (debug-fun)
1211   (handler-case
1212       (sb!di:do-debug-fun-blocks (block debug-fun)
1213         (declare (ignore block))
1214         (return t))
1215     (sb!di:no-debug-blocks () nil)))
1216
1217 (defun print-block-boundary (stream dstate)
1218   (let ((os (dstate-output-state dstate)))
1219     (when (not (eq os :beginning))
1220       (when (not (eq os :block-boundary))
1221         (terpri stream))
1222       (setf (dstate-output-state dstate)
1223             :block-boundary))))
1224
1225 ;;; Add hooks to track to track the source code in SEGMENT during
1226 ;;; disassembly. SFCACHE can be either NIL or it can be a
1227 ;;; SOURCE-FORM-CACHE structure, in which case it is used to cache
1228 ;;; forms from files.
1229 (defun add-source-tracking-hooks (segment debug-fun &optional sfcache)
1230   (declare (type segment segment)
1231            (type (or null sb!di:debug-fun) debug-fun)
1232            (type (or null source-form-cache) sfcache))
1233   (let ((last-block-pc -1))
1234     (flet ((add-hook (pc fun &optional before-address)
1235              (push (make-offs-hook
1236                     :offset pc ;; ##### FIX to account for non-zero offs in code
1237                     :function fun
1238                     :before-address before-address)
1239                    (seg-hooks segment))))
1240       (handler-case
1241           (sb!di:do-debug-fun-blocks (block debug-fun)
1242             (let ((first-location-in-block-p t))
1243               (sb!di:do-debug-block-locations (loc block)
1244                 (let ((pc (sb!di::compiled-code-location-pc loc)))
1245
1246                   ;; Put blank lines in at block boundaries
1247                   (when (and first-location-in-block-p
1248                              (/= pc last-block-pc))
1249                     (setf first-location-in-block-p nil)
1250                     (add-hook pc
1251                               (lambda (stream dstate)
1252                                 (print-block-boundary stream dstate))
1253                               t)
1254                     (setf last-block-pc pc))
1255
1256                   ;; Print out corresponding source; this information is not
1257                   ;; all that accurate, but it's better than nothing
1258                   (unless (zerop (sb!di:code-location-form-number loc))
1259                     (multiple-value-bind (form new)
1260                         (get-different-source-form loc 0 sfcache)
1261                       (when new
1262                          (let ((at-block-begin (= pc last-block-pc)))
1263                            (add-hook
1264                             pc
1265                             (lambda (stream dstate)
1266                               (declare (ignore dstate))
1267                               (when stream
1268                                 (unless at-block-begin
1269                                   (terpri stream))
1270                                 (format stream ";;; [~W] "
1271                                         (sb!di:code-location-form-number
1272                                          loc))
1273                                 (prin1-short form stream)
1274                                 (terpri stream)
1275                                 (terpri stream)))
1276                             t)))))
1277
1278                   ;; Keep track of variable live-ness as best we can.
1279                   (let ((live-set
1280                          (copy-seq (sb!di::compiled-code-location-live-set
1281                                     loc))))
1282                     (add-hook
1283                      pc
1284                      (lambda (stream dstate)
1285                        (declare (ignore stream))
1286                        (setf (dstate-current-valid-locations dstate)
1287                              live-set)
1288                        #+nil
1289                        (note (lambda (stream)
1290                                (let ((*print-length* nil))
1291                                  (format stream "live set: ~S"
1292                                          live-set)))
1293                              dstate))))
1294                   ))))
1295         (sb!di:no-debug-blocks () nil)))))
1296
1297 (defun add-debugging-hooks (segment debug-fun &optional sfcache)
1298   (when debug-fun
1299     (setf (seg-storage-info segment)
1300           (storage-info-for-debug-fun debug-fun))
1301     (add-source-tracking-hooks segment debug-fun sfcache)
1302     (let ((kind (sb!di:debug-fun-kind debug-fun)))
1303       (flet ((anh (n)
1304                (push (make-offs-hook
1305                       :offset 0
1306                       :function (lambda (stream dstate)
1307                                   (declare (ignore stream))
1308                                   (note n dstate)))
1309                      (seg-hooks segment))))
1310         (case kind
1311           (:external)
1312           ((nil)
1313            (anh "no-arg-parsing entry point"))
1314           (t
1315            (anh (lambda (stream)
1316                   (format stream "~S entry point" kind)))))))))
1317 \f
1318 ;;; Return a list of the segments of memory containing machine code
1319 ;;; instructions for FUNCTION.
1320 (defun get-function-segments (function)
1321   (declare (type compiled-function function))
1322   (let* ((code (fun-code function))
1323          (fun-map (code-fun-map code))
1324          (fname (sb!kernel:%simple-fun-name function))
1325          (sfcache (make-source-form-cache)))
1326     (let ((first-block-seen-p nil)
1327           (nil-block-seen-p nil)
1328           (last-offset 0)
1329           (last-debug-fun nil)
1330           (segments nil))
1331       (flet ((add-seg (offs len df)
1332                (when (> len 0)
1333                  (push (make-code-segment code offs len
1334                                           :debug-fun df
1335                                           :source-form-cache sfcache)
1336                        segments))))
1337         (dotimes (fmap-index (length fun-map))
1338           (let ((fmap-entry (aref fun-map fmap-index)))
1339             (etypecase fmap-entry
1340               (integer
1341                (when first-block-seen-p
1342                  (add-seg last-offset
1343                           (- fmap-entry last-offset)
1344                           last-debug-fun)
1345                  (setf last-debug-fun nil))
1346                (setf last-offset fmap-entry))
1347               (sb!c::compiled-debug-fun
1348                (let ((name (sb!c::compiled-debug-fun-name fmap-entry))
1349                      (kind (sb!c::compiled-debug-fun-kind fmap-entry)))
1350                  #+nil
1351                  (format t ";;; SAW ~S ~S ~S,~S ~W,~W~%"
1352                          name kind first-block-seen-p nil-block-seen-p
1353                          last-offset
1354                          (sb!c::compiled-debug-fun-start-pc fmap-entry))
1355                  (cond (#+nil (eq last-offset fun-offset)
1356                               (and (equal name fname) (not first-block-seen-p))
1357                               (setf first-block-seen-p t))
1358                        ((eq kind :external)
1359                         (when first-block-seen-p
1360                           (return)))
1361                        ((eq kind nil)
1362                         (when nil-block-seen-p
1363                           (return))
1364                         (when first-block-seen-p
1365                           (setf nil-block-seen-p t))))
1366                  (setf last-debug-fun
1367                        (sb!di::make-compiled-debug-fun fmap-entry code)))))))
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                 (list 
1376                  (make-code-segment code offs (- max-offset offs))))
1377               (nreverse segments)))))))
1378
1379 ;;; Return a list of the segments of memory containing machine code
1380 ;;; instructions for the code-component CODE. If START-OFFSET and/or
1381 ;;; LENGTH is supplied, only that part of the code-segment is used
1382 ;;; (but these are constrained to lie within the code-segment).
1383 (defun get-code-segments (code
1384                           &optional
1385                           (start-offset 0)
1386                           (length (code-inst-area-length code)))
1387   (declare (type sb!kernel:code-component code)
1388            (type offset start-offset)
1389            (type length length))
1390   (let ((segments nil))
1391     (when code
1392       (let ((fun-map (code-fun-map code))
1393             (sfcache (make-source-form-cache)))
1394         (let ((last-offset 0)
1395               (last-debug-fun nil))
1396           (flet ((add-seg (offs len df)
1397                    (let* ((restricted-offs
1398                            (min (max start-offset offs)
1399                                 (+ start-offset length)))
1400                           (restricted-len
1401                            (- (min (max start-offset (+ offs len))
1402                                    (+ start-offset length))
1403                               restricted-offs)))
1404                      (when (> restricted-len 0)
1405                        (push (make-code-segment code
1406                                                 restricted-offs restricted-len
1407                                                 :debug-fun df
1408                                                 :source-form-cache sfcache)
1409                              segments)))))
1410             (dotimes (fun-map-index (length fun-map))
1411               (let ((fun-map-entry (aref fun-map fun-map-index)))
1412                 (etypecase fun-map-entry
1413                   (integer
1414                    (add-seg last-offset (- fun-map-entry last-offset)
1415                             last-debug-fun)
1416                    (setf last-debug-fun nil)
1417                    (setf last-offset fun-map-entry))
1418                   (sb!c::compiled-debug-fun
1419                    (setf last-debug-fun
1420                          (sb!di::make-compiled-debug-fun fun-map-entry
1421                                                          code))))))
1422             (when last-debug-fun
1423               (add-seg last-offset
1424                        (- (code-inst-area-length code) last-offset)
1425                        last-debug-fun))))))
1426     (if (null segments)
1427         (make-code-segment code start-offset length)
1428         (nreverse segments))))
1429 \f
1430 ;;; Return two values: the amount by which the last instruction in the
1431 ;;; segment goes past the end of the segment, and the offset of the
1432 ;;; end of the segment from the beginning of that instruction. If all
1433 ;;; instructions fit perfectly, return 0 and 0.
1434 (defun segment-overflow (segment dstate)
1435   (declare (type segment segment)
1436            (type disassem-state dstate))
1437   (let ((seglen (seg-length segment))
1438         (last-start 0))
1439     (map-segment-instructions (lambda (chunk inst)
1440                                 (declare (ignore chunk inst))
1441                                 (setf last-start (dstate-cur-offs dstate)))
1442                               segment
1443                               dstate)
1444     (values (- (dstate-cur-offs dstate) seglen)
1445             (- seglen last-start))))
1446
1447 ;;; Compute labels for all the memory segments in SEGLIST and adds
1448 ;;; them to DSTATE. It's important to call this function with all the
1449 ;;; segments you're interested in, so that it can find references from
1450 ;;; one to another.
1451 (defun label-segments (seglist dstate)
1452   (declare (type list seglist)
1453            (type disassem-state dstate))
1454   (dolist (seg seglist)
1455     (add-segment-labels seg dstate))
1456   ;; Now remove any labels that don't point anywhere in the segments
1457   ;; we have.
1458   (setf (dstate-labels dstate)
1459         (remove-if (lambda (lab)
1460                      (not
1461                       (some (lambda (seg)
1462                               (let ((start (seg-virtual-location seg)))
1463                                 (<= start
1464                                     (car lab)
1465                                     (+ start (seg-length seg)))))
1466                             seglist)))
1467                    (dstate-labels dstate))))
1468
1469 ;;; Disassemble the machine code instructions in SEGMENT to STREAM.
1470 (defun disassemble-segment (segment stream dstate)
1471   (declare (type segment segment)
1472            (type stream stream)
1473            (type disassem-state dstate))
1474   (let ((*print-pretty* nil)) ; otherwise the pp conses hugely
1475     (number-labels dstate)
1476     (map-segment-instructions
1477      (lambda (chunk inst)
1478        (declare (type dchunk chunk) (type instruction inst))
1479        (let ((printer (inst-printer inst)))
1480          (when printer
1481            (funcall printer chunk inst stream dstate))))
1482      segment
1483      dstate
1484      stream)))
1485
1486 ;;; Disassemble the machine code instructions in each memory segment
1487 ;;; in SEGMENTS in turn to STREAM.
1488 (defun disassemble-segments (segments stream dstate)
1489   (declare (type list segments)
1490            (type stream stream)
1491            (type disassem-state dstate))
1492   (unless (null segments)
1493     (let ((first (car segments))
1494           (last (car (last segments))))
1495       (set-location-printing-range dstate
1496                                   (seg-virtual-location first)
1497                                   (- (+ (seg-virtual-location last)
1498                                         (seg-length last))
1499                                      (seg-virtual-location first)))
1500       (setf (dstate-output-state dstate) :beginning)
1501       (dolist (seg segments)
1502         (disassemble-segment seg stream dstate)))))
1503 \f
1504 ;;;; top level functions
1505
1506 ;;; Disassemble the machine code instructions for FUNCTION.
1507 (defun disassemble-function (function &key
1508                                       (stream *standard-output*)
1509                                       (use-labels t))
1510   (declare (type compiled-function function)
1511            (type stream stream)
1512            (type (member t nil) use-labels))
1513   (let* ((dstate (make-dstate))
1514          (segments (get-function-segments function)))
1515     (when use-labels
1516       (label-segments segments dstate))
1517     (disassemble-segments segments stream dstate)))
1518
1519 (defun compile-function-lambda-expr (function)
1520   (declare (type function function))
1521   (multiple-value-bind (lambda closurep name)
1522       (function-lambda-expression function)
1523     (declare (ignore name))
1524     (when closurep
1525       (error "can't compile a lexical closure"))
1526     (compile nil lambda)))
1527
1528 (defun compiled-function-or-lose (thing &optional (name thing))
1529   (cond ((or (symbolp thing)
1530              (and (listp thing)
1531                   (eq (car thing) 'setf)))
1532          (compiled-function-or-lose (fdefinition thing) thing))
1533         ((functionp thing)
1534          thing)
1535         ((and (listp thing)
1536               (eq (car thing) 'lambda))
1537          (compile nil thing))
1538         (t
1539          (error "can't make a compiled function from ~S" name))))
1540
1541 (defun disassemble (object &key
1542                            (stream *standard-output*)
1543                            (use-labels t))
1544   #!+sb-doc
1545   "Disassemble the compiled code associated with OBJECT, which can be a
1546   function, a lambda expression, or a symbol with a function definition. If
1547   it is not already compiled, the compiler is called to produce something to
1548   disassemble."
1549   (declare (type (or function symbol cons) object)
1550            (type (or (member t) stream) stream)
1551            (type (member t nil) use-labels))
1552   (pprint-logical-block (*standard-output* nil :per-line-prefix "; ")
1553     (disassemble-function (compiled-function-or-lose object)
1554                           :stream stream
1555                           :use-labels use-labels)
1556     nil))
1557
1558 ;;; Disassembles the given area of memory starting at ADDRESS and
1559 ;;; LENGTH long. Note that if CODE-COMPONENT is NIL and this memory
1560 ;;; could move during a GC, you'd better disable it around the call to
1561 ;;; this function.
1562 (defun disassemble-memory (address
1563                            length
1564                            &key
1565                            (stream *standard-output*)
1566                            code-component
1567                            (use-labels t))
1568   (declare (type (or address sb!sys:system-area-pointer) address)
1569            (type length length)
1570            (type stream stream)
1571            (type (or null sb!kernel:code-component) code-component)
1572            (type (member t nil) use-labels))
1573   (let* ((address
1574           (if (sb!sys:system-area-pointer-p address)
1575               (sb!sys:sap-int address)
1576               address))
1577          (dstate (make-dstate))
1578          (segments
1579           (if code-component
1580               (let ((code-offs
1581                      (- address
1582                         (sb!sys:sap-int
1583                          (sb!kernel:code-instructions code-component)))))
1584                 (when (or (< code-offs 0)
1585                           (> code-offs (code-inst-area-length code-component)))
1586                   (error "address ~X not in the code component ~S"
1587                          address code-component))
1588                 (get-code-segments code-component code-offs length))
1589               (list (make-memory-segment address length)))))
1590     (when use-labels
1591       (label-segments segments dstate))
1592     (disassemble-segments segments stream dstate)))
1593
1594 ;;; Disassemble the machine code instructions associated with
1595 ;;; CODE-COMPONENT (this may include multiple entry points).
1596 (defun disassemble-code-component (code-component &key
1597                                                   (stream *standard-output*)
1598                                                   (use-labels t))
1599   (declare (type (or null sb!kernel:code-component compiled-function)
1600                  code-component)
1601            (type stream stream)
1602            (type (member t nil) use-labels))
1603   (let* ((code-component
1604           (if (functionp code-component)
1605               (fun-code code-component)
1606               code-component))
1607          (dstate (make-dstate))
1608          (segments (get-code-segments code-component)))
1609     (when use-labels
1610       (label-segments segments dstate))
1611     (disassemble-segments segments stream dstate)))
1612 \f
1613 ;;; code for making useful segments from arbitrary lists of code-blocks
1614
1615 ;;; the maximum size of an instruction. Note that this includes
1616 ;;; pseudo-instructions like error traps with their associated
1617 ;;; operands, so it should be big enough to include them, i.e. it's
1618 ;;; not just 4 on a risc machine!
1619 (defconstant max-instruction-size 16)
1620
1621 (defun add-block-segments (seg-code-block
1622                            seglist
1623                            location
1624                            connecting-vec
1625                            dstate)
1626   (declare (type list seglist)
1627            (type integer location)
1628            (type (or null (vector (unsigned-byte 8))) connecting-vec)
1629            (type disassem-state dstate))
1630   (flet ((addit (seg overflow)
1631            (let ((length (+ (seg-length seg) overflow)))
1632              (when (> length 0)
1633                (setf (seg-length seg) length)
1634                (incf location length)
1635                (push seg seglist)))))
1636     (let ((connecting-overflow 0)
1637           (amount (length seg-code-block)))
1638       (when connecting-vec
1639         ;; Tack on some of the new block to the old overflow vector.
1640         (let* ((beginning-of-block-amount
1641                 (if seg-code-block (min max-instruction-size amount) 0))
1642                (connecting-vec
1643                 (if seg-code-block
1644                     (concatenate
1645                      '(vector (unsigned-byte 8))
1646                      connecting-vec
1647                      (subseq seg-code-block 0 beginning-of-block-amount))
1648                     connecting-vec)))
1649           (when (and (< (length connecting-vec) max-instruction-size)
1650                      (not (null seg-code-block)))
1651             (return-from add-block-segments
1652               ;; We want connecting vectors to be large enough to hold
1653               ;; any instruction, and since the current seg-code-block
1654               ;; wasn't large enough to do this (and is now entirely
1655               ;; on the end of the overflow-vector), just save it for
1656               ;; next time.
1657               (values seglist location connecting-vec)))
1658           (when (> (length connecting-vec) 0)
1659             (let ((seg
1660                    (make-vector-segment connecting-vec
1661                                         0
1662                                         (- (length connecting-vec)
1663                                            beginning-of-block-amount)
1664                                         :virtual-location location)))
1665               (setf connecting-overflow (segment-overflow seg dstate))
1666               (addit seg connecting-overflow)))))
1667       (cond ((null seg-code-block)
1668              ;; nothing more to add
1669              (values seglist location nil))
1670             ((< (- amount connecting-overflow) max-instruction-size)
1671              ;; We can't create a segment with the minimum size
1672              ;; required for an instruction, so just keep on accumulating
1673              ;; in the overflow vector for the time-being.
1674              (values seglist
1675                      location
1676                      (subseq seg-code-block connecting-overflow amount)))
1677             (t
1678              ;; Put as much as we can into a new segment, and the rest
1679              ;; into the overflow-vector.
1680              (let* ((initial-length
1681                      (- amount connecting-overflow max-instruction-size))
1682                     (seg
1683                      (make-vector-segment seg-code-block
1684                                           connecting-overflow
1685                                           initial-length
1686                                           :virtual-location location))
1687                     (overflow
1688                      (segment-overflow seg dstate)))
1689                (addit seg overflow)
1690                (values seglist
1691                        location
1692                        (subseq seg-code-block
1693                                (+ connecting-overflow (seg-length seg))
1694                                amount))))))))
1695 \f
1696 ;;;; code to disassemble assembler segments
1697
1698 (defun assem-segment-to-disassem-segments (assem-segment dstate)
1699   (declare (type sb!assem:segment assem-segment)
1700            (type disassem-state dstate))
1701   (let ((location 0)
1702         (disassem-segments nil)
1703         (connecting-vec nil))
1704     (sb!assem:on-segment-contents-vectorly
1705      assem-segment
1706      (lambda (seg-code-block)
1707        (multiple-value-setq (disassem-segments location connecting-vec)
1708          (add-block-segments seg-code-block
1709                              disassem-segments
1710                              location
1711                              connecting-vec
1712                              dstate))))
1713     (when connecting-vec
1714       (setf disassem-segments
1715             (add-block-segments nil
1716                                 disassem-segments
1717                                 location
1718                                 connecting-vec
1719                                 dstate)))
1720     (sort disassem-segments #'< :key #'seg-virtual-location)))
1721
1722 ;;; Disassemble the machine code instructions associated with
1723 ;;; ASSEM-SEGMENT (of type assem:segment).
1724 (defun disassemble-assem-segment (assem-segment stream)
1725   (declare (type sb!assem:segment assem-segment)
1726            (type stream stream))
1727   (let* ((dstate (make-dstate))
1728          (disassem-segments
1729           (assem-segment-to-disassem-segments assem-segment dstate)))
1730     (label-segments disassem-segments dstate)
1731     (disassemble-segments disassem-segments stream dstate)))
1732 \f
1733 ;;; routines to find things in the Lisp environment
1734
1735 ;;; an alist of (SYMBOL-SLOT-OFFSET . ACCESS-FUN-NAME) for slots
1736 ;;; in a symbol object that we know about
1737 (defparameter *grokked-symbol-slots*
1738   (sort `((,sb!vm:symbol-value-slot . symbol-value)
1739           (,sb!vm:symbol-plist-slot . symbol-plist)
1740           (,sb!vm:symbol-name-slot . symbol-name)
1741           (,sb!vm:symbol-package-slot . symbol-package))
1742         #'<
1743         :key #'car))
1744
1745 ;;; Given ADDRESS, try and figure out if which slot of which symbol is
1746 ;;; being referred to. Of course we can just give up, so it's not a
1747 ;;; big deal... Return two values, the symbol and the name of the
1748 ;;; access function of the slot.
1749 (defun grok-symbol-slot-ref (address)
1750   (declare (type address address))
1751   (if (not (aligned-p address sb!vm:n-word-bytes))
1752       (values nil nil)
1753       (do ((slots-tail *grokked-symbol-slots* (cdr slots-tail)))
1754           ((null slots-tail)
1755            (values nil nil))
1756         (let* ((field (car slots-tail))
1757                (slot-offset (words-to-bytes (car field)))
1758                (maybe-symbol-addr (- address slot-offset))
1759                (maybe-symbol
1760                 (sb!kernel:make-lisp-obj
1761                  (+ maybe-symbol-addr sb!vm:other-pointer-lowtag))))
1762           (when (symbolp maybe-symbol)
1763             (return (values maybe-symbol (cdr field))))))))
1764
1765 (defvar *address-of-nil-object* (sb!kernel:get-lisp-obj-address nil))
1766
1767 ;;; Given a BYTE-OFFSET from NIL, try and figure out which slot of
1768 ;;; which symbol is being referred to. Of course we can just give up,
1769 ;;; so it's not a big deal... Return two values, the symbol and the
1770 ;;; access function.
1771 (defun grok-nil-indexed-symbol-slot-ref (byte-offset)
1772   (declare (type offset byte-offset))
1773   (grok-symbol-slot-ref (+ *address-of-nil-object* byte-offset)))
1774
1775 ;;; Return the Lisp object located BYTE-OFFSET from NIL.
1776 (defun get-nil-indexed-object (byte-offset)
1777   (declare (type offset byte-offset))
1778   (sb!kernel:make-lisp-obj (+ *address-of-nil-object* byte-offset)))
1779
1780 ;;; Return two values; the Lisp object located at BYTE-OFFSET in the
1781 ;;; constant area of the code-object in the current segment and T, or
1782 ;;; NIL and NIL if there is no code-object in the current segment.
1783 (defun get-code-constant (byte-offset dstate)
1784   #!+sb-doc
1785   (declare (type offset byte-offset)
1786            (type disassem-state dstate))
1787   (let ((code (seg-code (dstate-segment dstate))))
1788     (if code
1789         (values
1790          (sb!kernel:code-header-ref code
1791                                     (ash (+ byte-offset
1792                                             sb!vm:other-pointer-lowtag)
1793                                          (- sb!vm:word-shift)))
1794          t)
1795         (values nil nil))))
1796
1797 (defun get-code-constant-absolute (addr dstate)
1798   (declare (type address addr))
1799   (declare (type disassem-state dstate))
1800   (let ((code (seg-code (dstate-segment dstate))))
1801     (if (null code)
1802       (return-from get-code-constant-absolute (values nil nil)))
1803     (let ((code-size (ash (sb!kernel:get-header-data code) sb!vm:word-shift)))
1804       (sb!sys:without-gcing
1805        (let ((code-addr (- (sb!kernel:get-lisp-obj-address code)
1806                            sb!vm:other-pointer-lowtag)))
1807          (if (or (< addr code-addr) (>= addr (+ code-addr code-size)))
1808            (values nil nil)
1809            (values (sb!kernel:code-header-ref
1810                     code
1811                     (ash (- addr code-addr) (- sb!vm:word-shift)))
1812                    t)))))))
1813
1814 (defvar *assembler-routines-by-addr* nil)
1815
1816 (defvar *foreign-symbols-by-addr* nil)
1817
1818 ;;; Build an address-name hash-table from the name-address hash
1819 (defun invert-address-hash (htable &optional (addr-hash (make-hash-table)))
1820   (maphash (lambda (name address)
1821              (setf (gethash address addr-hash) name))
1822            htable)
1823   addr-hash)
1824
1825 ;;; Return the name of the primitive Lisp assembler routine or foreign
1826 ;;; symbol located at ADDRESS, or NIL if there isn't one.
1827 (defun find-assembler-routine (address)
1828   (declare (type address address))
1829   (when (null *assembler-routines-by-addr*)
1830     (setf *assembler-routines-by-addr*
1831           (invert-address-hash sb!fasl:*assembler-routines*))
1832     (setf *assembler-routines-by-addr*
1833           (invert-address-hash sb!fasl:*static-foreign-symbols*
1834                                *assembler-routines-by-addr*)))
1835   (gethash address *assembler-routines-by-addr*))
1836 \f
1837 ;;;; some handy function for machine-dependent code to use...
1838
1839 #!-sb-fluid (declaim (maybe-inline sap-ref-int read-suffix))
1840
1841 (defun sap-ref-int (sap offset length byte-order)
1842   (declare (type sb!sys:system-area-pointer sap)
1843            (type (unsigned-byte 16) offset)
1844            (type (member 1 2 4) length)
1845            (type (member :little-endian :big-endian) byte-order)
1846            (optimize (speed 3) (safety 0)))
1847   (ecase length
1848     (1 (sb!sys:sap-ref-8 sap offset))
1849     (2 (if (eq byte-order :big-endian)
1850            (+ (ash (sb!sys:sap-ref-8 sap offset) 8)
1851               (sb!sys:sap-ref-8 sap (+ offset 1)))
1852            (+ (ash (sb!sys:sap-ref-8 sap (+ offset 1)) 8)
1853               (sb!sys:sap-ref-8 sap offset))))
1854     (4 (if (eq byte-order :big-endian)
1855            (+ (ash (sb!sys:sap-ref-8 sap offset) 24)
1856               (ash (sb!sys:sap-ref-8 sap (+ 1 offset)) 16)
1857               (ash (sb!sys:sap-ref-8 sap (+ 2 offset)) 8)
1858               (sb!sys:sap-ref-8 sap (+ 3 offset)))
1859            (+ (sb!sys:sap-ref-8 sap offset)
1860               (ash (sb!sys:sap-ref-8 sap (+ 1 offset)) 8)
1861               (ash (sb!sys:sap-ref-8 sap (+ 2 offset)) 16)
1862               (ash (sb!sys:sap-ref-8 sap (+ 3 offset)) 24))))))
1863
1864 (defun read-suffix (length dstate)
1865   (declare (type (member 8 16 32) length)
1866            (type disassem-state dstate)
1867            (optimize (speed 3) (safety 0)))
1868   (let ((length (ecase length (8 1) (16 2) (32 4))))
1869     (declare (type (unsigned-byte 3) length))
1870     (prog1
1871       (sap-ref-int (dstate-segment-sap dstate)
1872                    (dstate-next-offs dstate)
1873                    length
1874                    (dstate-byte-order dstate))
1875       (incf (dstate-next-offs dstate) length))))
1876 \f
1877 ;;;; optional routines to make notes about code
1878
1879 ;;; Store NOTE (which can be either a string or a function with a
1880 ;;; single stream argument) to be printed as an end-of-line comment
1881 ;;; after the current instruction is disassembled.
1882 (defun note (note dstate)
1883   (declare (type (or string function) note)
1884            (type disassem-state dstate))
1885   (push note (dstate-notes dstate)))
1886
1887 (defun prin1-short (thing stream)
1888   (with-print-restrictions
1889     (prin1 thing stream)))
1890
1891 (defun prin1-quoted-short (thing stream)
1892   (if (self-evaluating-p thing)
1893       (prin1-short thing stream)
1894       (prin1-short `',thing stream)))
1895
1896 ;;; Store a note about the lisp constant located BYTE-OFFSET bytes
1897 ;;; from the current code-component, to be printed as an end-of-line
1898 ;;; comment after the current instruction is disassembled.
1899 (defun note-code-constant (byte-offset dstate)
1900   (declare (type offset byte-offset)
1901            (type disassem-state dstate))
1902   (multiple-value-bind (const valid)
1903       (get-code-constant byte-offset dstate)
1904     (when valid
1905       (note (lambda (stream)
1906               (prin1-quoted-short const stream))
1907             dstate))
1908     const))
1909
1910 ;;; Store a note about the lisp constant located at ADDR in the
1911 ;;; current code-component, to be printed as an end-of-line comment
1912 ;;; after the current instruction is disassembled.
1913 (defun note-code-constant-absolute (addr dstate)
1914   (declare (type address addr)
1915            (type disassem-state dstate))
1916   (multiple-value-bind (const valid)
1917       (get-code-constant-absolute addr dstate)
1918     (when valid
1919       (note (lambda (stream)
1920               (prin1-quoted-short const stream))
1921             dstate))
1922     (values const valid)))
1923
1924 ;;; If the memory address located NIL-BYTE-OFFSET bytes from the
1925 ;;; constant NIL is a valid slot in a symbol, store a note describing
1926 ;;; which symbol and slot, to be printed as an end-of-line comment
1927 ;;; after the current instruction is disassembled. Returns non-NIL iff
1928 ;;; a note was recorded.
1929 (defun maybe-note-nil-indexed-symbol-slot-ref (nil-byte-offset dstate)
1930   (declare (type offset nil-byte-offset)
1931            (type disassem-state dstate))
1932   (multiple-value-bind (symbol access-fun)
1933       (grok-nil-indexed-symbol-slot-ref nil-byte-offset)
1934     (when access-fun
1935       (note (lambda (stream)
1936               (prin1 (if (eq access-fun 'symbol-value)
1937                          symbol
1938                          `(,access-fun ',symbol))
1939                      stream))
1940             dstate))
1941     access-fun))
1942
1943 ;;; If the memory address located NIL-BYTE-OFFSET bytes from the
1944 ;;; constant NIL is a valid lisp object, store a note describing which
1945 ;;; symbol and slot, to be printed as an end-of-line comment after the
1946 ;;; current instruction is disassembled. Returns non-NIL iff a note
1947 ;;; was recorded.
1948 (defun maybe-note-nil-indexed-object (nil-byte-offset dstate)
1949   (declare (type offset nil-byte-offset)
1950            (type disassem-state dstate))
1951   (let ((obj (get-nil-indexed-object nil-byte-offset)))
1952     (note (lambda (stream)
1953             (prin1-quoted-short obj stream))
1954           dstate)
1955     t))
1956
1957 ;;; If ADDRESS is the address of a primitive assembler routine or
1958 ;;; foreign symbol, store a note describing which one, to be printed
1959 ;;; as an end-of-line comment after the current instruction is
1960 ;;; disassembled. Returns non-NIL iff a note was recorded. If
1961 ;;; NOTE-ADDRESS-P is non-NIL, a note of the address is also made.
1962 (defun maybe-note-assembler-routine (address note-address-p dstate)
1963   (declare (type disassem-state dstate))
1964   (unless (typep address 'address)
1965     (return-from maybe-note-assembler-routine nil))
1966   (let ((name (find-assembler-routine address)))
1967     (unless (null name)
1968       (note (lambda (stream)
1969               (if note-address-p
1970                   (format stream "#x~8,'0x: ~a" address name)
1971                   (princ name stream)))
1972             dstate))
1973     name))
1974
1975 ;;; If there's a valid mapping from OFFSET in the storage class
1976 ;;; SC-NAME to a source variable, make a note of the source-variable
1977 ;;; name, to be printed as an end-of-line comment after the current
1978 ;;; instruction is disassembled. Returns non-NIL iff a note was
1979 ;;; recorded.
1980 (defun maybe-note-single-storage-ref (offset sc-name dstate)
1981   (declare (type offset offset)
1982            (type symbol sc-name)
1983            (type disassem-state dstate))
1984   (let ((storage-location
1985          (find-valid-storage-location offset sc-name dstate)))
1986     (when storage-location
1987       (note (lambda (stream)
1988               (princ (sb!di:debug-var-symbol
1989                       (aref (storage-info-debug-vars
1990                              (seg-storage-info (dstate-segment dstate)))
1991                             storage-location))
1992                      stream))
1993             dstate)
1994       t)))
1995
1996 ;;; If there's a valid mapping from OFFSET in the storage-base called
1997 ;;; SB-NAME to a source variable, make a note equating ASSOC-WITH with
1998 ;;; the source-variable name, to be printed as an end-of-line comment
1999 ;;; after the current instruction is disassembled. Returns non-NIL iff
2000 ;;; a note was recorded.
2001 (defun maybe-note-associated-storage-ref (offset sb-name assoc-with dstate)
2002   (declare (type offset offset)
2003            (type symbol sb-name)
2004            (type (or symbol string) assoc-with)
2005            (type disassem-state dstate))
2006   (let ((storage-location
2007          (find-valid-storage-location offset sb-name dstate)))
2008     (when storage-location
2009       (note (lambda (stream)
2010               (format stream "~A = ~S"
2011                       assoc-with
2012                       (sb!di:debug-var-symbol
2013                        (aref (dstate-debug-vars dstate)
2014                              storage-location))
2015                       stream))
2016             dstate)
2017       t)))
2018 \f
2019 (defun get-internal-error-name (errnum)
2020   (car (svref sb!c:*backend-internal-errors* errnum)))
2021
2022 (defun get-sc-name (sc-offs)
2023   (sb!c::location-print-name
2024    ;; FIXME: This seems like an awful lot of computation just to get a name.
2025    ;; Couldn't we just use lookup in *BACKEND-SC-NAMES*, without having to cons
2026    ;; up a new object?
2027    (sb!c:make-random-tn :kind :normal
2028                         :sc (svref sb!c:*backend-sc-numbers*
2029                                    (sb!c:sc-offset-scn sc-offs))
2030                         :offset (sb!c:sc-offset-offset sc-offs))))
2031
2032 ;;; When called from an error break instruction's :DISASSEM-CONTROL (or
2033 ;;; :DISASSEM-PRINTER) function, will correctly deal with printing the
2034 ;;; arguments to the break.
2035 ;;;
2036 ;;; ERROR-PARSE-FUN should be a function that accepts:
2037 ;;;   1) a SYSTEM-AREA-POINTER
2038 ;;;   2) a BYTE-OFFSET from the SAP to begin at
2039 ;;;   3) optionally, LENGTH-ONLY, which if non-NIL, means to only return
2040 ;;;      the byte length of the arguments (to avoid unnecessary consing)
2041 ;;; It should read information from the SAP starting at BYTE-OFFSET, and
2042 ;;; return four values:
2043 ;;;   1) the error number
2044 ;;;   2) the total length, in bytes, of the information
2045 ;;;   3) a list of SC-OFFSETs of the locations of the error parameters
2046 ;;;   4) a list of the length (as read from the SAP), in bytes, of each
2047 ;;;      of the return values.
2048 (defun handle-break-args (error-parse-fun stream dstate)
2049   (declare (type function error-parse-fun)
2050            (type (or null stream) stream)
2051            (type disassem-state dstate))
2052   (multiple-value-bind (errnum adjust sc-offsets lengths)
2053       (funcall error-parse-fun
2054                (dstate-segment-sap dstate)
2055                (dstate-next-offs dstate)
2056                (null stream))
2057     (when stream
2058       (setf (dstate-cur-offs dstate)
2059             (dstate-next-offs dstate))
2060       (flet ((emit-err-arg (note)
2061                (let ((num (pop lengths)))
2062                  (print-notes-and-newline stream dstate)
2063                  (print-current-address stream dstate)
2064                  (print-bytes num stream dstate)
2065                  (incf (dstate-cur-offs dstate) num)
2066                  (when note
2067                    (note note dstate)))))
2068         (emit-err-arg nil)
2069         (emit-err-arg (symbol-name (get-internal-error-name errnum)))
2070         (dolist (sc-offs sc-offsets)
2071           (emit-err-arg (get-sc-name sc-offs)))))
2072     (incf (dstate-next-offs dstate)
2073           adjust)))