0.pre7.86.flaky7.27:
[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[~D]~:[ (#X~X)~;~*~]~@[ in ~S~]"
272               addr
273               (seg-length seg)
274               (= (seg-virtual-location seg) addr)
275               (seg-virtual-location seg)
276               (seg-code seg)))))
277 \f
278 ;;; All state during disassembly. We store some seemingly redundant
279 ;;; information so that we can allow garbage collect during disassembly and
280 ;;; not get tripped up by a code block being moved...
281 (defstruct (disassem-state (:conc-name dstate-)
282                            (:constructor %make-dstate)
283                            (:copier nil))
284   ;; offset of current pos in segment
285   (cur-offs 0 :type offset)             
286   ;; offset of next position
287   (next-offs 0 :type offset)            
288   ;; a sap pointing to our segment
289   (segment-sap (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             "+~D~@[ in ~S~]"
330             (dstate-cur-offs dstate)
331             (dstate-segment dstate))))
332
333 ;;; Return the absolute address of the current instruction in DSTATE.
334 (defun dstate-cur-addr (dstate)
335   (the address (+ (seg-virtual-location (dstate-segment dstate))
336                   (dstate-cur-offs dstate))))
337
338 ;;; Return the absolute address of the next instruction in DSTATE.
339 (defun dstate-next-addr (dstate)
340   (the address (+ (seg-virtual-location (dstate-segment dstate))
341                   (dstate-next-offs dstate))))
342 \f
343 ;;;; function ops
344
345 (defun fun-self (fun)
346   (declare (type compiled-function fun))
347   (sb!kernel:%simple-fun-self fun))
348
349 (defun fun-code (fun)
350   (declare (type compiled-function fun))
351   (sb!kernel:fun-code-header (fun-self fun)))
352
353 (defun fun-next (fun)
354   (declare (type compiled-function fun))
355   (sb!kernel:%simple-fun-next fun))
356
357 (defun fun-address (function)
358   (declare (type compiled-function function))
359   (- (sb!kernel:get-lisp-obj-address function) sb!vm:fun-pointer-lowtag))
360
361 ;;; the offset of FUNCTION from the start of its code-component's
362 ;;; instruction area
363 (defun fun-insts-offset (function)
364   (declare (type compiled-function function))
365   (- (fun-address function)
366      (sb!sys:sap-int (sb!kernel:code-instructions (fun-code function)))))
367
368 ;;; the offset of FUNCTION from the start of its code-component
369 (defun fun-offset (function)
370   (declare (type compiled-function function))
371   (words-to-bytes (sb!kernel:get-closure-length function)))
372 \f
373 ;;;; operations on code-components (which hold the instructions for
374 ;;;; one or more functions)
375
376 ;;; Return the length of the instruction area in CODE-COMPONENT.
377 (defun code-inst-area-length (code-component)
378   (declare (type sb!kernel:code-component code-component))
379   (sb!kernel:code-header-ref code-component
380                              sb!vm:code-trace-table-offset-slot))
381
382 ;;; Return the address of the instruction area in CODE-COMPONENT.
383 (defun code-inst-area-address (code-component)
384   (declare (type sb!kernel:code-component code-component))
385   (sb!sys:sap-int (sb!kernel:code-instructions code-component)))
386
387 ;;; Return the first function in CODE-COMPONENT.
388 (defun code-first-function (code-component)
389   (declare (type sb!kernel:code-component code-component))
390   (sb!kernel:code-header-ref code-component
391                              sb!vm:code-trace-table-offset-slot))
392
393 (defun segment-offs-to-code-offs (offset segment)
394   (sb!sys:without-gcing
395    (let* ((seg-base-addr (sb!sys:sap-int (funcall (seg-sap-maker segment))))
396           (code-addr
397            (logandc1 sb!vm:lowtag-mask
398                      (sb!kernel:get-lisp-obj-address (seg-code segment))))
399           (addr (+ offset seg-base-addr)))
400      (declare (type address seg-base-addr code-addr addr))
401      (- addr code-addr))))
402
403 (defun code-offs-to-segment-offs (offset segment)
404   (sb!sys:without-gcing
405    (let* ((seg-base-addr (sb!sys:sap-int (funcall (seg-sap-maker segment))))
406           (code-addr
407            (logandc1 sb!vm:lowtag-mask
408                      (sb!kernel:get-lisp-obj-address (seg-code segment))))
409           (addr (+ offset code-addr)))
410      (declare (type address seg-base-addr code-addr addr))
411      (- addr seg-base-addr))))
412
413 (defun code-insts-offs-to-segment-offs (offset segment)
414   (sb!sys:without-gcing
415    (let* ((seg-base-addr (sb!sys:sap-int (funcall (seg-sap-maker segment))))
416           (code-insts-addr
417            (sb!sys:sap-int (sb!kernel:code-instructions (seg-code segment))))
418           (addr (+ offset code-insts-addr)))
419      (declare (type address seg-base-addr code-insts-addr addr))
420      (- addr seg-base-addr))))
421 \f
422 (defun lra-hook (chunk stream dstate)
423   (declare (type dchunk chunk)
424            (ignore chunk)
425            (type (or null stream) stream)
426            (type disassem-state dstate))
427   (when (and (aligned-p (+ (seg-virtual-location (dstate-segment dstate))
428                            (dstate-cur-offs dstate))
429                         (* 2 sb!vm:n-word-bytes))
430              ;; Check type.
431              (= (sb!sys:sap-ref-8 (dstate-segment-sap dstate)
432                                   (if (eq (dstate-byte-order dstate)
433                                           :little-endian)
434                                       (dstate-cur-offs dstate)
435                                       (+ (dstate-cur-offs dstate)
436                                          (1- lra-size))))
437                 sb!vm:return-pc-header-widetag))
438     (unless (null stream)
439       (princ '.lra stream))
440     (incf (dstate-next-offs dstate) lra-size))
441   nil)
442
443 ;;; Print the 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~D~%" '.align
485                 (dstate-argument-column dstate)
486                 alignment))
487       (incf(dstate-next-offs dstate)
488            (- (align location alignment) location)))
489     nil))
490
491 (defun rewind-current-segment (dstate segment)
492   (declare (type disassem-state dstate)
493            (type segment segment))
494   (setf (dstate-segment dstate) segment)
495   (setf (dstate-cur-offs-hooks dstate)
496         (stable-sort (nreverse (copy-list (seg-hooks segment)))
497                      (lambda (oh1 oh2)
498                        (or (< (offs-hook-offset oh1) (offs-hook-offset oh2))
499                            (and (= (offs-hook-offset oh1)
500                                    (offs-hook-offset oh2))
501                                 (offs-hook-before-address oh1)
502                                 (not (offs-hook-before-address oh2)))))))
503   (setf (dstate-cur-offs dstate) 0)
504   (setf (dstate-cur-labels dstate) (dstate-labels dstate)))
505
506 (defun do-offs-hooks (before-address stream dstate)
507   (declare (type (or null stream) stream)
508            (type disassem-state dstate))
509   (let ((cur-offs (dstate-cur-offs dstate)))
510     (setf (dstate-next-offs dstate) cur-offs)
511     (loop
512       (let ((next-hook (car (dstate-cur-offs-hooks dstate))))
513         (when (null next-hook)
514           (return))
515         (let ((hook-offs (offs-hook-offset next-hook)))
516           (when (or (> hook-offs cur-offs)
517                     (and (= hook-offs cur-offs)
518                          before-address
519                          (not (offs-hook-before-address next-hook))))
520             (return))
521           (unless (< hook-offs cur-offs)
522             (funcall (offs-hook-function next-hook) stream dstate))
523           (pop (dstate-cur-offs-hooks dstate))
524           (unless (= (dstate-next-offs dstate) cur-offs)
525             (return)))))))
526
527 (defun do-fun-hooks (chunk stream dstate)
528   (let ((hooks (dstate-fun-hooks dstate))
529         (cur-offs (dstate-cur-offs dstate)))
530     (setf (dstate-next-offs dstate) cur-offs)
531     (dolist (hook hooks nil)
532       (let ((prefix-p (funcall hook chunk stream dstate)))
533         (unless (= (dstate-next-offs dstate) cur-offs)
534           (return prefix-p))))))
535
536 (defun handle-bogus-instruction (stream dstate)
537   (let ((alignment (dstate-alignment dstate)))
538     (unless (null stream)
539       (multiple-value-bind (words bytes)
540           (truncate alignment sb!vm:n-word-bytes)
541         (when (> words 0)
542           (print-words words stream dstate))
543         (when (> bytes 0)
544           (print-bytes bytes stream dstate))))
545     (incf (dstate-next-offs dstate) alignment)))
546
547 ;;; Iterate through the instructions in SEGMENT, calling FUNCTION for
548 ;;; each instruction, with arguments of CHUNK, STREAM, and DSTATE.
549 (defun map-segment-instructions (function segment dstate &optional stream)
550   (declare (type function function)
551            (type segment segment)
552            (type disassem-state dstate)
553            (type (or null stream) stream))
554
555   (let ((ispace (get-inst-space))
556         (prefix-p nil)) ; just processed a prefix inst
557
558     (rewind-current-segment dstate segment)
559
560     (loop
561       (when (>= (dstate-cur-offs dstate)
562                 (seg-length (dstate-segment dstate)))
563         ;; done!
564         (return))
565
566       (setf (dstate-next-offs dstate) (dstate-cur-offs dstate))
567
568       (do-offs-hooks t stream dstate)
569       (unless (or prefix-p (null stream))
570         (print-current-address stream dstate))
571       (do-offs-hooks nil stream dstate)
572
573       (unless (> (dstate-next-offs dstate) (dstate-cur-offs dstate))
574         (sb!sys:without-gcing
575          (setf (dstate-segment-sap dstate) (funcall (seg-sap-maker segment)))
576
577          (let ((chunk
578                 (sap-ref-dchunk (dstate-segment-sap dstate)
579                                 (dstate-cur-offs dstate)
580                                 (dstate-byte-order dstate))))
581            (let ((fun-prefix-p (do-fun-hooks chunk stream dstate)))
582              (if (> (dstate-next-offs dstate) (dstate-cur-offs dstate))
583                  (setf prefix-p fun-prefix-p)
584                  (let ((inst (find-inst chunk ispace)))
585                    (cond ((null inst)
586                           (handle-bogus-instruction stream dstate))
587                          (t
588                           (setf (dstate-next-offs dstate)
589                                 (+ (dstate-cur-offs dstate)
590                                    (inst-length inst)))
591
592                           (let ((prefilter (inst-prefilter inst))
593                                 (control (inst-control inst)))
594                             (when prefilter
595                               (funcall prefilter chunk dstate))
596
597                             (funcall function chunk inst)
598
599                             (setf prefix-p (null (inst-printer inst)))
600
601                             (when control
602                               (funcall control chunk inst stream dstate))))))
603                  )))))
604
605       (setf (dstate-cur-offs dstate) (dstate-next-offs dstate))
606
607       (unless (null stream)
608         (unless prefix-p
609           (print-notes-and-newline stream dstate))
610         (setf (dstate-output-state dstate) nil)))))
611 \f
612 ;;; Make an initial non-printing disassembly pass through DSTATE,
613 ;;; noting any addresses that are referenced by instructions in this
614 ;;; segment.
615 (defun add-segment-labels (segment dstate)
616   ;; add labels at the beginning with a label-number of nil; we'll notice
617   ;; later and fill them in (and sort them)
618   (declare (type disassem-state dstate))
619   (let ((labels (dstate-labels dstate)))
620     (map-segment-instructions
621      (lambda (chunk inst)
622        (declare (type dchunk chunk) (type instruction inst))
623        (let ((labeller (inst-labeller inst)))
624          (when labeller
625            (setf labels (funcall labeller chunk labels dstate)))))
626      segment
627      dstate)
628     (setf (dstate-labels dstate) labels)
629     ;; erase any notes that got there by accident
630     (setf (dstate-notes dstate) nil)))
631
632 ;;; If any labels in DSTATE have been added since the last call to
633 ;;; this function, give them label-numbers, enter them in the
634 ;;; hash-table, and make sure the label list is in sorted order.
635 (defun number-labels (dstate)
636   (let ((labels (dstate-labels dstate)))
637     (when (and labels (null (cdar labels)))
638       ;; at least one label left un-numbered
639       (setf labels (sort labels #'< :key #'car))
640       (let ((max -1)
641             (label-hash (dstate-label-hash dstate)))
642         (dolist (label labels)
643           (when (not (null (cdr label)))
644             (setf max (max max (cdr label)))))
645         (dolist (label labels)
646           (when (null (cdr label))
647             (incf max)
648             (setf (cdr label) max)
649             (setf (gethash (car label) label-hash)
650                   (format nil "L~D" max)))))
651       (setf (dstate-labels dstate) labels))))
652 \f
653 ;;; Get the instruction-space, creating it if necessary.
654 (defun get-inst-space ()
655   (let ((ispace *disassem-inst-space*))
656     (when (null ispace)
657       (let ((insts nil))
658         (maphash (lambda (name inst-flavs)
659                    (declare (ignore name))
660                    (dolist (flav inst-flavs)
661                      (push flav insts)))
662                  *disassem-insts*)
663         (setf ispace (build-inst-space insts)))
664       (setf *disassem-inst-space* ispace))
665     ispace))
666 \f
667 ;;;; Add global hooks.
668
669 (defun add-offs-hook (segment addr hook)
670   (let ((entry (cons addr hook)))
671     (if (null (seg-hooks segment))
672         (setf (seg-hooks segment) (list entry))
673         (push entry (cdr (last (seg-hooks segment)))))))
674
675 (defun add-offs-note-hook (segment addr note)
676   (add-offs-hook segment
677                  addr
678                  (lambda (stream dstate)
679                    (declare (type (or null stream) stream)
680                             (type disassem-state dstate))
681                    (when stream
682                      (note note dstate)))))
683
684 (defun add-offs-comment-hook (segment addr comment)
685   (add-offs-hook segment
686                  addr
687                  (lambda (stream dstate)
688                    (declare (type (or null stream) stream)
689                             (ignore dstate))
690                    (when stream
691                      (write-string ";;; " stream)
692                      (etypecase comment
693                        (string
694                         (write-string comment stream))
695                        (function
696                         (funcall comment stream)))
697                      (terpri stream)))))
698
699 (defun add-fun-hook (dstate function)
700   (push function (dstate-fun-hooks dstate)))
701 \f
702 (defun set-location-printing-range (dstate from length)
703   (setf (dstate-addr-print-len dstate)
704         ;; 4 bits per hex digit
705         (ceiling (integer-length (logxor from (+ from length))) 4)))
706
707 ;;; Print the current address in DSTATE to STREAM, plus any labels that
708 ;;; correspond to it, and leave the cursor in the instruction column.
709 (defun print-current-address (stream dstate)
710   (declare (type stream stream)
711            (type disassem-state dstate))
712   (let* ((location
713           (+ (seg-virtual-location (dstate-segment dstate))
714              (dstate-cur-offs dstate)))
715          (location-column-width *disassem-location-column-width*)
716          (plen (dstate-addr-print-len dstate)))
717
718     (when (null plen)
719       (setf plen location-column-width)
720       (let ((seg (dstate-segment dstate)))
721         (set-location-printing-range dstate
722                                      (seg-virtual-location seg)
723                                      (seg-length seg))))
724     (when (eq (dstate-output-state dstate) :beginning)
725       (setf plen location-column-width))
726
727     (fresh-line stream)
728
729     (setf location-column-width (+ 2 location-column-width))
730     (princ "; " stream)
731
732     ;; print the location
733     ;; [this is equivalent to (format stream "~V,'0x:" plen printed-value), but
734     ;;  usually avoids any consing]
735     (tab0 (- location-column-width plen) stream)
736     (let* ((printed-bits (* 4 plen))
737            (printed-value (ldb (byte printed-bits 0) location))
738            (leading-zeros
739             (truncate (- printed-bits (integer-length printed-value)) 4)))
740       (dotimes (i leading-zeros)
741         (write-char #\0 stream))
742       (unless (zerop printed-value)
743         (write printed-value :stream stream :base 16 :radix nil))
744       (write-char #\: stream))
745
746     ;; print any labels
747     (loop
748       (let* ((next-label (car (dstate-cur-labels dstate)))
749              (label-location (car next-label)))
750         (when (or (null label-location) (> label-location location))
751           (return))
752         (unless (< label-location location)
753           (format stream " L~D:" (cdr next-label)))
754         (pop (dstate-cur-labels dstate))))
755
756     ;; move to the instruction column
757     (tab0 (+ location-column-width 1 label-column-width) stream)
758     ))
759 \f
760 (eval-when (:compile-toplevel :execute)
761   (sb!xc:defmacro with-print-restrictions (&rest body)
762     `(let ((*print-pretty* t)
763            (*print-lines* 2)
764            (*print-length* 4)
765            (*print-level* 3))
766        ,@body)))
767
768 ;;; Print a newline to STREAM, inserting any pending notes in DSTATE
769 ;;; as end-of-line comments. If there is more than one note, a
770 ;;; separate line will be used for each one.
771 (defun print-notes-and-newline (stream dstate)
772   (declare (type stream stream)
773            (type disassem-state dstate))
774   (with-print-restrictions
775     (dolist (note (dstate-notes dstate))
776       (format stream "~Vt " *disassem-note-column*)
777       (pprint-logical-block (stream nil :per-line-prefix "; ")
778       (etypecase note
779         (string
780          (write-string note stream))
781         (function
782          (funcall note stream))))
783       (terpri stream))
784     (fresh-line stream)
785     (setf (dstate-notes dstate) nil)))
786
787 ;;; Disassemble NUM bytes to STREAM as simple `BYTE' instructions.
788 (defun print-bytes (num stream dstate)
789   (declare (type offset num)
790            (type stream stream)
791            (type disassem-state dstate))
792   (format stream "~A~Vt" 'BYTE (dstate-argument-column dstate))
793   (let ((sap (dstate-segment-sap dstate))
794         (start-offs (dstate-cur-offs dstate)))
795     (dotimes (offs num)
796       (unless (zerop offs)
797         (write-string ", " stream))
798       (format stream "#X~2,'0x" (sb!sys:sap-ref-8 sap (+ offs start-offs))))))
799
800 ;;; Disassemble NUM machine-words to STREAM as simple `WORD' instructions.
801 (defun print-words (num stream dstate)
802   (declare (type offset num)
803            (type stream stream)
804            (type disassem-state dstate))
805   (format stream "~A~Vt" 'WORD (dstate-argument-column dstate))
806   (let ((sap (dstate-segment-sap dstate))
807         (start-offs (dstate-cur-offs dstate))
808         (byte-order (dstate-byte-order dstate)))
809     (dotimes (word-offs num)
810       (unless (zerop word-offs)
811         (write-string ", " stream))
812       (let ((word 0) (bit-shift 0))
813         (dotimes (byte-offs sb!vm:n-word-bytes)
814           (let ((byte
815                  (sb!sys:sap-ref-8
816                         sap
817                         (+ start-offs
818                            (* word-offs sb!vm:n-word-bytes)
819                            byte-offs))))
820             (setf word
821                   (if (eq byte-order :big-endian)
822                       (+ (ash word sb!vm:n-byte-bits) byte)
823                       (+ word (ash byte bit-shift))))
824             (incf bit-shift sb!vm:n-byte-bits)))
825         (format stream "#X~V,'0X" (ash sb!vm:n-word-bits -2) word)))))
826 \f
827 (defvar *default-dstate-hooks* (list #'lra-hook))
828
829 ;;; Make a disassembler-state object.
830 (defun make-dstate (&optional (fun-hooks *default-dstate-hooks*))
831   (let ((sap
832          (sb!sys:vector-sap (coerce #() '(vector (unsigned-byte 8)))))
833         (alignment *disassem-inst-alignment-bytes*)
834         (arg-column
835          (+ (or *disassem-opcode-column-width* 0)
836             *disassem-location-column-width*
837             1
838             label-column-width)))
839
840     (when (> alignment 1)
841       (push #'alignment-hook fun-hooks))
842
843     (%make-dstate :segment-sap sap
844                   :fun-hooks fun-hooks
845                   :argument-column arg-column
846                   :alignment alignment
847                   :byte-order sb!c:*backend-byte-order*)))
848
849 (defun add-fun-header-hooks (segment)
850   (declare (type segment segment))
851   (do ((fun (sb!kernel:code-header-ref (seg-code segment)
852                                        sb!vm:code-entry-points-slot)
853             (fun-next fun))
854        (length (seg-length segment)))
855       ((null fun))
856     (let ((offset (code-offs-to-segment-offs (fun-offset fun) segment)))
857       (when (<= 0 offset length)
858         (push (make-offs-hook :offset offset :function #'fun-header-hook)
859               (seg-hooks segment))))))
860 \f
861 ;;; A SAP-MAKER is a no-argument function that returns a SAP.
862
863 #!-sb-fluid (declaim (inline sap-maker))
864
865 (defun sap-maker (function input offset)
866   (declare (optimize (speed 3))
867            (type (function (t) sb!sys:system-area-pointer) function)
868            (type offset offset))
869   (let ((old-sap (sb!sys:sap+ (funcall function input) offset)))
870     (declare (type sb!sys:system-area-pointer old-sap))
871     (lambda ()
872       (let ((new-addr
873              (+ (sb!sys:sap-int (funcall function input)) offset)))
874         ;; Saving the sap like this avoids consing except when the sap
875         ;; changes (because the sap-int, arith, etc., get inlined).
876         (declare (type address new-addr))
877         (if (= (sb!sys:sap-int old-sap) new-addr)
878             old-sap
879             (setf old-sap (sb!sys:int-sap new-addr)))))))
880
881 (defun vector-sap-maker (vector offset)
882   (declare (optimize (speed 3))
883            (type offset offset))
884   (sap-maker #'sb!sys:vector-sap vector offset))
885
886 (defun code-sap-maker (code offset)
887   (declare (optimize (speed 3))
888            (type sb!kernel:code-component code)
889            (type offset offset))
890   (sap-maker #'sb!kernel:code-instructions code offset))
891
892 (defun memory-sap-maker (address)
893   (declare (optimize (speed 3))
894            (type address address))
895   (let ((sap (sb!sys:int-sap address)))
896     (lambda () sap)))
897 \f
898 ;;; Return a memory segment located at the system-area-pointer returned by
899 ;;; SAP-MAKER and LENGTH bytes long in the disassem-state object DSTATE.
900 ;;;
901 ;;; &KEY arguments include :VIRTUAL-LOCATION (by default the same as
902 ;;; the address), :DEBUG-FUN, :SOURCE-FORM-CACHE (a
903 ;;; SOURCE-FORM-CACHE object), and :HOOKS (a list of OFFS-HOOK
904 ;;; objects).
905 (defun make-segment (sap-maker length
906                      &key
907                      code virtual-location
908                      debug-fun source-form-cache
909                      hooks)
910   (declare (type (function () sb!sys:system-area-pointer) sap-maker)
911            (type length length)
912            (type (or null address) virtual-location)
913            (type (or null sb!di:debug-fun) debug-fun)
914            (type (or null source-form-cache) source-form-cache))
915   (let* ((segment
916           (%make-segment
917            :sap-maker sap-maker
918            :length length
919            :virtual-location (or virtual-location
920                                  (sb!sys:sap-int (funcall sap-maker)))
921            :hooks hooks
922            :code code)))
923     (add-debugging-hooks segment debug-fun source-form-cache)
924     (add-fun-header-hooks segment)
925     segment))
926
927 (defun make-vector-segment (vector offset &rest args)
928   (declare (type vector vector)
929            (type offset offset)
930            (inline make-segment))
931   (apply #'make-segment (vector-sap-maker vector offset) args))
932
933 (defun make-code-segment (code offset length &rest args)
934   (declare (type sb!kernel:code-component code)
935            (type offset offset)
936            (inline make-segment))
937   (apply #'make-segment (code-sap-maker code offset) length :code code args))
938
939 (defun make-memory-segment (address &rest args)
940   (declare (type address address)
941            (inline make-segment))
942   (apply #'make-segment (memory-sap-maker address) args))
943 \f
944 ;;; just for fun
945 (defun print-fun-headers (function)
946   (declare (type compiled-function function))
947   (let* ((self (fun-self function))
948          (code (sb!kernel:fun-code-header self)))
949     (format t "Code-header ~S: size: ~S, trace-table-offset: ~S~%"
950             code
951             (sb!kernel:code-header-ref code
952                                        sb!vm:code-code-size-slot)
953             (sb!kernel:code-header-ref code
954                                        sb!vm:code-trace-table-offset-slot))
955     (do ((fun (sb!kernel:code-header-ref code sb!vm:code-entry-points-slot)
956               (fun-next fun)))
957         ((null fun))
958       (let ((fun-offset (sb!kernel:get-closure-length fun)))
959         ;; There is function header fun-offset words from the
960         ;; code header.
961         (format t "Fun-header ~S at offset ~D (words): ~S~A => ~S~%"
962                 fun
963                 fun-offset
964                 (sb!kernel:code-header-ref
965                  code (+ fun-offset sb!vm:simple-fun-name-slot))
966                 (sb!kernel:code-header-ref
967                  code (+ fun-offset sb!vm:simple-fun-arglist-slot))
968                 (sb!kernel:code-header-ref
969                  code (+ fun-offset sb!vm:simple-fun-type-slot)))))))
970 \f
971 ;;; getting at the source code...
972
973 (defstruct (source-form-cache (:conc-name sfcache-)
974                               (:copier nil))
975   (debug-source nil :type (or null sb!di:debug-source))
976   (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!di::get-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 ~D: ~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[~D]~%"
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 ";;; [~D] "
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 ~D,~D~%"
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                  )))))
1369         (let ((max-offset (code-inst-area-length code)))
1370           (when (and first-block-seen-p last-debug-fun)
1371             (add-seg last-offset
1372                      (- max-offset last-offset)
1373                      last-debug-fun))
1374           (if (null segments)
1375               (let ((offs (fun-insts-offset function)))
1376                 (list 
1377                  (make-code-segment code offs (- max-offset offs))))
1378               (nreverse segments)))))))
1379
1380 ;;; Return a list of the segments of memory containing machine code
1381 ;;; instructions for the code-component CODE. If START-OFFSET and/or
1382 ;;; LENGTH is supplied, only that part of the code-segment is used
1383 ;;; (but these are constrained to lie within the code-segment).
1384 (defun get-code-segments (code
1385                           &optional
1386                           (start-offset 0)
1387                           (length (code-inst-area-length code)))
1388   (declare (type sb!kernel:code-component code)
1389            (type offset start-offset)
1390            (type length length))
1391   (let ((segments nil))
1392     (when code
1393       (let ((fun-map (code-fun-map code))
1394             (sfcache (make-source-form-cache)))
1395         (let ((last-offset 0)
1396               (last-debug-fun nil))
1397           (flet ((add-seg (offs len df)
1398                    (let* ((restricted-offs
1399                            (min (max start-offset offs)
1400                                 (+ start-offset length)))
1401                           (restricted-len
1402                            (- (min (max start-offset (+ offs len))
1403                                    (+ start-offset length))
1404                               restricted-offs)))
1405                      (when (> restricted-len 0)
1406                        (push (make-code-segment code
1407                                                 restricted-offs restricted-len
1408                                                 :debug-fun df
1409                                                 :source-form-cache sfcache)
1410                              segments)))))
1411             (dotimes (fun-map-index (length fun-map))
1412               (let ((fun-map-entry (aref fun-map fun-map-index)))
1413                 (etypecase fun-map-entry
1414                   (integer
1415                    (add-seg last-offset (- fun-map-entry last-offset)
1416                             last-debug-fun)
1417                    (setf last-debug-fun nil)
1418                    (setf last-offset fun-map-entry))
1419                   (sb!c::compiled-debug-fun
1420                    (setf last-debug-fun
1421                          (sb!di::make-compiled-debug-fun fun-map-entry
1422                                                          code))))))
1423             (when last-debug-fun
1424               (add-seg last-offset
1425                        (- (code-inst-area-length code) last-offset)
1426                        last-debug-fun))))))
1427     (if (null segments)
1428         (make-code-segment code start-offset length)
1429         (nreverse segments))))
1430 \f
1431 ;;; Return two values: the amount by which the last instruction in the
1432 ;;; segment goes past the end of the segment, and the offset of the
1433 ;;; end of the segment from the beginning of that instruction. If all
1434 ;;; instructions fit perfectly, return 0 and 0.
1435 (defun segment-overflow (segment dstate)
1436   (declare (type segment segment)
1437            (type disassem-state dstate))
1438   (let ((seglen (seg-length segment))
1439         (last-start 0))
1440     (map-segment-instructions (lambda (chunk inst)
1441                                 (declare (ignore chunk inst))
1442                                 (setf last-start (dstate-cur-offs dstate)))
1443                               segment
1444                               dstate)
1445     (values (- (dstate-cur-offs dstate) seglen)
1446             (- seglen last-start))))
1447
1448 ;;; Compute labels for all the memory segments in SEGLIST and adds
1449 ;;; them to DSTATE. It's important to call this function with all the
1450 ;;; segments you're interested in, so that it can find references from
1451 ;;; one to another.
1452 (defun label-segments (seglist dstate)
1453   (declare (type list seglist)
1454            (type disassem-state dstate))
1455   (dolist (seg seglist)
1456     (add-segment-labels seg dstate))
1457   ;; Now remove any labels that don't point anywhere in the segments
1458   ;; we have.
1459   (setf (dstate-labels dstate)
1460         (remove-if (lambda (lab)
1461                      (not
1462                       (some (lambda (seg)
1463                               (let ((start (seg-virtual-location seg)))
1464                                 (<= start
1465                                     (car lab)
1466                                     (+ start (seg-length seg)))))
1467                             seglist)))
1468                    (dstate-labels dstate))))
1469
1470 ;;; Disassemble the machine code instructions in SEGMENT to STREAM.
1471 (defun disassemble-segment (segment stream dstate)
1472   (declare (type segment segment)
1473            (type stream stream)
1474            (type disassem-state dstate))
1475   (let ((*print-pretty* nil)) ; otherwise the pp conses hugely
1476     (number-labels dstate)
1477     (map-segment-instructions
1478      (lambda (chunk inst)
1479        (declare (type dchunk chunk) (type instruction inst))
1480        (let ((printer (inst-printer inst)))
1481          (when printer
1482            (funcall printer chunk inst stream dstate))))
1483      segment
1484      dstate
1485      stream)))
1486
1487 ;;; Disassemble the machine code instructions in each memory segment
1488 ;;; in SEGMENTS in turn to STREAM.
1489 (defun disassemble-segments (segments stream dstate)
1490   (declare (type list segments)
1491            (type stream stream)
1492            (type disassem-state dstate))
1493   (unless (null segments)
1494     (let ((first (car segments))
1495           (last (car (last segments))))
1496       (set-location-printing-range dstate
1497                                   (seg-virtual-location first)
1498                                   (- (+ (seg-virtual-location last)
1499                                         (seg-length last))
1500                                      (seg-virtual-location first)))
1501       (setf (dstate-output-state dstate) :beginning)
1502       (dolist (seg segments)
1503         (disassemble-segment seg stream dstate)))))
1504 \f
1505 ;;;; top level functions
1506
1507 ;;; Disassemble the machine code instructions for FUNCTION.
1508 (defun disassemble-function (function &key
1509                                       (stream *standard-output*)
1510                                       (use-labels t))
1511   (declare (type compiled-function function)
1512            (type stream stream)
1513            (type (member t nil) use-labels))
1514   (let* ((dstate (make-dstate))
1515          (segments (get-function-segments function)))
1516     (when use-labels
1517       (label-segments segments dstate))
1518     (disassemble-segments segments stream dstate)))
1519
1520 (defun compile-function-lambda-expr (function)
1521   (declare (type function function))
1522   (multiple-value-bind (lambda closurep name)
1523       (function-lambda-expression function)
1524     (declare (ignore name))
1525     (when closurep
1526       (error "can't compile a lexical closure"))
1527     (compile nil lambda)))
1528
1529 (defun compiled-function-or-lose (thing &optional (name thing))
1530   (cond ((or (symbolp thing)
1531              (and (listp thing)
1532                   (eq (car thing) 'setf)))
1533          (compiled-function-or-lose (fdefinition thing) thing))
1534         ((functionp thing)
1535          thing)
1536         ((and (listp thing)
1537               (eq (car thing) 'lambda))
1538          (compile nil thing))
1539         (t
1540          (error "can't make a compiled function from ~S" name))))
1541
1542 (defun disassemble (object &key
1543                            (stream *standard-output*)
1544                            (use-labels t))
1545   #!+sb-doc
1546   "Disassemble the compiled code associated with OBJECT, which can be a
1547   function, a lambda expression, or a symbol with a function definition. If
1548   it is not already compiled, the compiler is called to produce something to
1549   disassemble."
1550   (declare (type (or function symbol cons) object)
1551            (type (or (member t) stream) stream)
1552            (type (member t nil) use-labels))
1553   (pprint-logical-block (*standard-output* nil :per-line-prefix "; ")
1554     (disassemble-function (compiled-function-or-lose object)
1555                           :stream stream
1556                           :use-labels use-labels)
1557     nil))
1558
1559 ;;; Disassembles the given area of memory starting at ADDRESS and
1560 ;;; LENGTH long. Note that if CODE-COMPONENT is NIL and this memory
1561 ;;; could move during a GC, you'd better disable it around the call to
1562 ;;; this function.
1563 (defun disassemble-memory (address
1564                            length
1565                            &key
1566                            (stream *standard-output*)
1567                            code-component
1568                            (use-labels t))
1569   (declare (type (or address sb!sys:system-area-pointer) address)
1570            (type length length)
1571            (type stream stream)
1572            (type (or null sb!kernel:code-component) code-component)
1573            (type (member t nil) use-labels))
1574   (let* ((address
1575           (if (sb!sys:system-area-pointer-p address)
1576               (sb!sys:sap-int address)
1577               address))
1578          (dstate (make-dstate))
1579          (segments
1580           (if code-component
1581               (let ((code-offs
1582                      (- address
1583                         (sb!sys:sap-int
1584                          (sb!kernel:code-instructions code-component)))))
1585                 (when (or (< code-offs 0)
1586                           (> code-offs (code-inst-area-length code-component)))
1587                   (error "address ~X not in the code component ~S"
1588                          address code-component))
1589                 (get-code-segments code-component code-offs length))
1590               (list (make-memory-segment address length)))))
1591     (when use-labels
1592       (label-segments segments dstate))
1593     (disassemble-segments segments stream dstate)))
1594
1595 ;;; Disassemble the machine code instructions associated with
1596 ;;; CODE-COMPONENT (this may include multiple entry points).
1597 (defun disassemble-code-component (code-component &key
1598                                                   (stream *standard-output*)
1599                                                   (use-labels t))
1600   (declare (type (or null sb!kernel:code-component compiled-function)
1601                  code-component)
1602            (type stream stream)
1603            (type (member t nil) use-labels))
1604   (let* ((code-component
1605           (if (functionp code-component)
1606               (fun-code code-component)
1607               code-component))
1608          (dstate (make-dstate))
1609          (segments (get-code-segments code-component)))
1610     (when use-labels
1611       (label-segments segments dstate))
1612     (disassemble-segments segments stream dstate)))
1613 \f
1614 ;;; code for making useful segments from arbitrary lists of code-blocks
1615
1616 ;;; the maximum size of an instruction. Note that this includes
1617 ;;; pseudo-instructions like error traps with their associated
1618 ;;; operands, so it should be big enough to include them, i.e. it's
1619 ;;; not just 4 on a risc machine!
1620 (defconstant max-instruction-size 16)
1621
1622 (defun add-block-segments (seg-code-block
1623                            seglist
1624                            location
1625                            connecting-vec
1626                            dstate)
1627   (declare (type list seglist)
1628            (type integer location)
1629            (type (or null (vector (unsigned-byte 8))) connecting-vec)
1630            (type disassem-state dstate))
1631   (flet ((addit (seg overflow)
1632            (let ((length (+ (seg-length seg) overflow)))
1633              (when (> length 0)
1634                (setf (seg-length seg) length)
1635                (incf location length)
1636                (push seg seglist)))))
1637     (let ((connecting-overflow 0)
1638           (amount (length seg-code-block)))
1639       (when connecting-vec
1640         ;; Tack on some of the new block to the old overflow vector.
1641         (let* ((beginning-of-block-amount
1642                 (if seg-code-block (min max-instruction-size amount) 0))
1643                (connecting-vec
1644                 (if seg-code-block
1645                     (concatenate
1646                      '(vector (unsigned-byte 8))
1647                      connecting-vec
1648                      (subseq seg-code-block 0 beginning-of-block-amount))
1649                     connecting-vec)))
1650           (when (and (< (length connecting-vec) max-instruction-size)
1651                      (not (null seg-code-block)))
1652             (return-from add-block-segments
1653               ;; We want connecting vectors to be large enough to hold
1654               ;; any instruction, and since the current seg-code-block
1655               ;; wasn't large enough to do this (and is now entirely
1656               ;; on the end of the overflow-vector), just save it for
1657               ;; next time.
1658               (values seglist location connecting-vec)))
1659           (when (> (length connecting-vec) 0)
1660             (let ((seg
1661                    (make-vector-segment connecting-vec
1662                                         0
1663                                         (- (length connecting-vec)
1664                                            beginning-of-block-amount)
1665                                         :virtual-location location)))
1666               (setf connecting-overflow (segment-overflow seg dstate))
1667               (addit seg connecting-overflow)))))
1668       (cond ((null seg-code-block)
1669              ;; nothing more to add
1670              (values seglist location nil))
1671             ((< (- amount connecting-overflow) max-instruction-size)
1672              ;; We can't create a segment with the minimum size
1673              ;; required for an instruction, so just keep on accumulating
1674              ;; in the overflow vector for the time-being.
1675              (values seglist
1676                      location
1677                      (subseq seg-code-block connecting-overflow amount)))
1678             (t
1679              ;; Put as much as we can into a new segment, and the rest
1680              ;; into the overflow-vector.
1681              (let* ((initial-length
1682                      (- amount connecting-overflow max-instruction-size))
1683                     (seg
1684                      (make-vector-segment seg-code-block
1685                                           connecting-overflow
1686                                           initial-length
1687                                           :virtual-location location))
1688                     (overflow
1689                      (segment-overflow seg dstate)))
1690                (addit seg overflow)
1691                (values seglist
1692                        location
1693                        (subseq seg-code-block
1694                                (+ connecting-overflow (seg-length seg))
1695                                amount))))))))
1696 \f
1697 ;;;; code to disassemble assembler segments
1698
1699 (defun assem-segment-to-disassem-segments (assem-segment dstate)
1700   (declare (type sb!assem:segment assem-segment)
1701            (type disassem-state dstate))
1702   (let ((location 0)
1703         (disassem-segments nil)
1704         (connecting-vec nil))
1705     (sb!assem:on-segment-contents-vectorly
1706      assem-segment
1707      (lambda (seg-code-block)
1708        (multiple-value-setq (disassem-segments location connecting-vec)
1709          (add-block-segments seg-code-block
1710                              disassem-segments
1711                              location
1712                              connecting-vec
1713                              dstate))))
1714     (when connecting-vec
1715       (setf disassem-segments
1716             (add-block-segments nil
1717                                 disassem-segments
1718                                 location
1719                                 connecting-vec
1720                                 dstate)))
1721     (sort disassem-segments #'< :key #'seg-virtual-location)))
1722
1723 ;;; Disassemble the machine code instructions associated with
1724 ;;; ASSEM-SEGMENT (of type assem:segment).
1725 (defun disassemble-assem-segment (assem-segment stream)
1726   (declare (type sb!assem:segment assem-segment)
1727            (type stream stream))
1728   (let* ((dstate (make-dstate))
1729          (disassem-segments
1730           (assem-segment-to-disassem-segments assem-segment dstate)))
1731     (label-segments disassem-segments dstate)
1732     (disassemble-segments disassem-segments stream dstate)))
1733 \f
1734 ;;; routines to find things in the Lisp environment
1735
1736 ;;; an alist of (SYMBOL-SLOT-OFFSET . ACCESS-FUN-NAME) for slots
1737 ;;; in a symbol object that we know about
1738 (defparameter *grokked-symbol-slots*
1739   (sort `((,sb!vm:symbol-value-slot . symbol-value)
1740           (,sb!vm:symbol-plist-slot . symbol-plist)
1741           (,sb!vm:symbol-name-slot . symbol-name)
1742           (,sb!vm:symbol-package-slot . symbol-package))
1743         #'<
1744         :key #'car))
1745
1746 ;;; Given ADDRESS, try and figure out if which slot of which symbol is
1747 ;;; being referred to. Of course we can just give up, so it's not a
1748 ;;; big deal... Return two values, the symbol and the name of the
1749 ;;; access function of the slot.
1750 (defun grok-symbol-slot-ref (address)
1751   (declare (type address address))
1752   (if (not (aligned-p address sb!vm:n-word-bytes))
1753       (values nil nil)
1754       (do ((slots-tail *grokked-symbol-slots* (cdr slots-tail)))
1755           ((null slots-tail)
1756            (values nil nil))
1757         (let* ((field (car slots-tail))
1758                (slot-offset (words-to-bytes (car field)))
1759                (maybe-symbol-addr (- address slot-offset))
1760                (maybe-symbol
1761                 (sb!kernel:make-lisp-obj
1762                  (+ maybe-symbol-addr sb!vm:other-pointer-lowtag))))
1763           (when (symbolp maybe-symbol)
1764             (return (values maybe-symbol (cdr field))))))))
1765
1766 (defvar *address-of-nil-object* (sb!kernel:get-lisp-obj-address nil))
1767
1768 ;;; Given a BYTE-OFFSET from NIL, try and figure out which slot of
1769 ;;; which symbol is being referred to. Of course we can just give up,
1770 ;;; so it's not a big deal... Return two values, the symbol and the
1771 ;;; access function.
1772 (defun grok-nil-indexed-symbol-slot-ref (byte-offset)
1773   (declare (type offset byte-offset))
1774   (grok-symbol-slot-ref (+ *address-of-nil-object* byte-offset)))
1775
1776 ;;; Return the Lisp object located BYTE-OFFSET from NIL.
1777 (defun get-nil-indexed-object (byte-offset)
1778   (declare (type offset byte-offset))
1779   (sb!kernel:make-lisp-obj (+ *address-of-nil-object* byte-offset)))
1780
1781 ;;; Return two values; the Lisp object located at BYTE-OFFSET in the
1782 ;;; constant area of the code-object in the current segment and T, or
1783 ;;; NIL and NIL if there is no code-object in the current segment.
1784 (defun get-code-constant (byte-offset dstate)
1785   #!+sb-doc
1786   (declare (type offset byte-offset)
1787            (type disassem-state dstate))
1788   (let ((code (seg-code (dstate-segment dstate))))
1789     (if code
1790         (values
1791          (sb!kernel:code-header-ref code
1792                                     (ash (+ byte-offset
1793                                             sb!vm:other-pointer-lowtag)
1794                                          (- sb!vm:word-shift)))
1795          t)
1796         (values nil nil))))
1797
1798 (defun get-code-constant-absolute (addr dstate)
1799   (declare (type address addr))
1800   (declare (type disassem-state dstate))
1801   (let ((code (seg-code (dstate-segment dstate))))
1802     (if (null code)
1803       (return-from get-code-constant-absolute (values nil nil)))
1804     (let ((code-size (ash (sb!kernel:get-header-data code) sb!vm:word-shift)))
1805       (sb!sys:without-gcing
1806        (let ((code-addr (- (sb!kernel:get-lisp-obj-address code)
1807                            sb!vm:other-pointer-lowtag)))
1808          (if (or (< addr code-addr) (>= addr (+ code-addr code-size)))
1809            (values nil nil)
1810            (values (sb!kernel:code-header-ref
1811                     code
1812                     (ash (- addr code-addr) (- sb!vm:word-shift)))
1813                    t)))))))
1814
1815 (defvar *assembler-routines-by-addr* nil)
1816
1817 (defvar *foreign-symbols-by-addr* nil)
1818
1819 ;;; Build an address-name hash-table from the name-address hash
1820 (defun invert-address-hash (htable &optional (addr-hash (make-hash-table)))
1821   (maphash (lambda (name address)
1822              (setf (gethash address addr-hash) name))
1823            htable)
1824   addr-hash)
1825
1826 ;;; Return the name of the primitive Lisp assembler routine or foreign
1827 ;;; symbol located at ADDRESS, or NIL if there isn't one.
1828 (defun find-assembler-routine (address)
1829   (declare (type address address))
1830   (when (null *assembler-routines-by-addr*)
1831     (setf *assembler-routines-by-addr*
1832           (invert-address-hash sb!fasl:*assembler-routines*))
1833     (setf *assembler-routines-by-addr*
1834           (invert-address-hash sb!fasl:*static-foreign-symbols*
1835                                *assembler-routines-by-addr*)))
1836   (gethash address *assembler-routines-by-addr*))
1837 \f
1838 ;;;; some handy function for machine-dependent code to use...
1839
1840 #!-sb-fluid (declaim (maybe-inline sap-ref-int read-suffix))
1841
1842 (defun sap-ref-int (sap offset length byte-order)
1843   (declare (type sb!sys:system-area-pointer sap)
1844            (type (unsigned-byte 16) offset)
1845            (type (member 1 2 4) length)
1846            (type (member :little-endian :big-endian) byte-order)
1847            (optimize (speed 3) (safety 0)))
1848   (ecase length
1849     (1 (sb!sys:sap-ref-8 sap offset))
1850     (2 (if (eq byte-order :big-endian)
1851            (+ (ash (sb!sys:sap-ref-8 sap offset) 8)
1852               (sb!sys:sap-ref-8 sap (+ offset 1)))
1853            (+ (ash (sb!sys:sap-ref-8 sap (+ offset 1)) 8)
1854               (sb!sys:sap-ref-8 sap offset))))
1855     (4 (if (eq byte-order :big-endian)
1856            (+ (ash (sb!sys:sap-ref-8 sap offset) 24)
1857               (ash (sb!sys:sap-ref-8 sap (+ 1 offset)) 16)
1858               (ash (sb!sys:sap-ref-8 sap (+ 2 offset)) 8)
1859               (sb!sys:sap-ref-8 sap (+ 3 offset)))
1860            (+ (sb!sys:sap-ref-8 sap offset)
1861               (ash (sb!sys:sap-ref-8 sap (+ 1 offset)) 8)
1862               (ash (sb!sys:sap-ref-8 sap (+ 2 offset)) 16)
1863               (ash (sb!sys:sap-ref-8 sap (+ 3 offset)) 24))))))
1864
1865 (defun read-suffix (length dstate)
1866   (declare (type (member 8 16 32) length)
1867            (type disassem-state dstate)
1868            (optimize (speed 3) (safety 0)))
1869   (let ((length (ecase length (8 1) (16 2) (32 4))))
1870     (declare (type (unsigned-byte 3) length))
1871     (prog1
1872       (sap-ref-int (dstate-segment-sap dstate)
1873                    (dstate-next-offs dstate)
1874                    length
1875                    (dstate-byte-order dstate))
1876       (incf (dstate-next-offs dstate) length))))
1877 \f
1878 ;;;; optional routines to make notes about code
1879
1880 ;;; Store NOTE (which can be either a string or a function with a
1881 ;;; single stream argument) to be printed as an end-of-line comment
1882 ;;; after the current instruction is disassembled.
1883 (defun note (note dstate)
1884   (declare (type (or string function) note)
1885            (type disassem-state dstate))
1886   (push note (dstate-notes dstate)))
1887
1888 (defun prin1-short (thing stream)
1889   (with-print-restrictions
1890     (prin1 thing stream)))
1891
1892 (defun prin1-quoted-short (thing stream)
1893   (if (self-evaluating-p thing)
1894       (prin1-short thing stream)
1895       (prin1-short `',thing stream)))
1896
1897 ;;; Store a note about the lisp constant located BYTE-OFFSET bytes
1898 ;;; from the current code-component, to be printed as an end-of-line
1899 ;;; comment after the current instruction is disassembled.
1900 (defun note-code-constant (byte-offset dstate)
1901   (declare (type offset byte-offset)
1902            (type disassem-state dstate))
1903   (multiple-value-bind (const valid)
1904       (get-code-constant byte-offset dstate)
1905     (when valid
1906       (note (lambda (stream)
1907               (prin1-quoted-short const stream))
1908             dstate))
1909     const))
1910
1911 ;;; Store a note about the lisp constant located at ADDR in the
1912 ;;; current code-component, to be printed as an end-of-line comment
1913 ;;; after the current instruction is disassembled.
1914 (defun note-code-constant-absolute (addr dstate)
1915   (declare (type address addr)
1916            (type disassem-state dstate))
1917   (multiple-value-bind (const valid)
1918       (get-code-constant-absolute addr dstate)
1919     (when valid
1920       (note (lambda (stream)
1921               (prin1-quoted-short const stream))
1922             dstate))
1923     (values const valid)))
1924
1925 ;;; If the memory address located NIL-BYTE-OFFSET bytes from the
1926 ;;; constant NIL is a valid slot in a symbol, store a note describing
1927 ;;; which symbol and slot, to be printed as an end-of-line comment
1928 ;;; after the current instruction is disassembled. Returns non-NIL iff
1929 ;;; a note was recorded.
1930 (defun maybe-note-nil-indexed-symbol-slot-ref (nil-byte-offset dstate)
1931   (declare (type offset nil-byte-offset)
1932            (type disassem-state dstate))
1933   (multiple-value-bind (symbol access-fun)
1934       (grok-nil-indexed-symbol-slot-ref nil-byte-offset)
1935     (when access-fun
1936       (note (lambda (stream)
1937               (prin1 (if (eq access-fun 'symbol-value)
1938                          symbol
1939                          `(,access-fun ',symbol))
1940                      stream))
1941             dstate))
1942     access-fun))
1943
1944 ;;; If the memory address located NIL-BYTE-OFFSET bytes from the
1945 ;;; constant NIL is a valid lisp object, store a note describing which
1946 ;;; symbol and slot, to be printed as an end-of-line comment after the
1947 ;;; current instruction is disassembled. Returns non-NIL iff a note
1948 ;;; was recorded.
1949 (defun maybe-note-nil-indexed-object (nil-byte-offset dstate)
1950   (declare (type offset nil-byte-offset)
1951            (type disassem-state dstate))
1952   (let ((obj (get-nil-indexed-object nil-byte-offset)))
1953     (note (lambda (stream)
1954             (prin1-quoted-short obj stream))
1955           dstate)
1956     t))
1957
1958 ;;; If ADDRESS is the address of a primitive assembler routine or
1959 ;;; foreign symbol, store a note describing which one, to be printed
1960 ;;; as an end-of-line comment after the current instruction is
1961 ;;; disassembled. Returns non-NIL iff a note was recorded. If
1962 ;;; NOTE-ADDRESS-P is non-NIL, a note of the address is also made.
1963 (defun maybe-note-assembler-routine (address note-address-p dstate)
1964   (declare (type disassem-state dstate))
1965   (unless (typep address 'address)
1966     (return-from maybe-note-assembler-routine nil))
1967   (let ((name (find-assembler-routine address)))
1968     (unless (null name)
1969       (note (lambda (stream)
1970               (if note-address-p
1971                   (format stream "#x~8,'0x: ~a" address name)
1972                   (princ name stream)))
1973             dstate))
1974     name))
1975
1976 ;;; If there's a valid mapping from OFFSET in the storage class
1977 ;;; SC-NAME to a source variable, make a note of the source-variable
1978 ;;; name, to be printed as an end-of-line comment after the current
1979 ;;; instruction is disassembled. Returns non-NIL iff a note was
1980 ;;; recorded.
1981 (defun maybe-note-single-storage-ref (offset sc-name dstate)
1982   (declare (type offset offset)
1983            (type symbol sc-name)
1984            (type disassem-state dstate))
1985   (let ((storage-location
1986          (find-valid-storage-location offset sc-name dstate)))
1987     (when storage-location
1988       (note (lambda (stream)
1989               (princ (sb!di:debug-var-symbol
1990                       (aref (storage-info-debug-vars
1991                              (seg-storage-info (dstate-segment dstate)))
1992                             storage-location))
1993                      stream))
1994             dstate)
1995       t)))
1996
1997 ;;; If there's a valid mapping from OFFSET in the storage-base called
1998 ;;; SB-NAME to a source variable, make a note equating ASSOC-WITH with
1999 ;;; the source-variable name, to be printed as an end-of-line comment
2000 ;;; after the current instruction is disassembled. Returns non-NIL iff
2001 ;;; a note was recorded.
2002 (defun maybe-note-associated-storage-ref (offset sb-name assoc-with dstate)
2003   (declare (type offset offset)
2004            (type symbol sb-name)
2005            (type (or symbol string) assoc-with)
2006            (type disassem-state dstate))
2007   (let ((storage-location
2008          (find-valid-storage-location offset sb-name dstate)))
2009     (when storage-location
2010       (note (lambda (stream)
2011               (format stream "~A = ~S"
2012                       assoc-with
2013                       (sb!di:debug-var-symbol
2014                        (aref (dstate-debug-vars dstate)
2015                              storage-location))
2016                       stream))
2017             dstate)
2018       t)))
2019 \f
2020 (defun get-internal-error-name (errnum)
2021   (car (svref sb!c:*backend-internal-errors* errnum)))
2022
2023 (defun get-sc-name (sc-offs)
2024   (sb!c::location-print-name
2025    ;; FIXME: This seems like an awful lot of computation just to get a name.
2026    ;; Couldn't we just use lookup in *BACKEND-SC-NAMES*, without having to cons
2027    ;; up a new object?
2028    (sb!c:make-random-tn :kind :normal
2029                         :sc (svref sb!c:*backend-sc-numbers*
2030                                    (sb!c:sc-offset-scn sc-offs))
2031                         :offset (sb!c:sc-offset-offset sc-offs))))
2032
2033 ;;; When called from an error break instruction's :DISASSEM-CONTROL (or
2034 ;;; :DISASSEM-PRINTER) function, will correctly deal with printing the
2035 ;;; arguments to the break.
2036 ;;;
2037 ;;; ERROR-PARSE-FUN should be a function that accepts:
2038 ;;;   1) a SYSTEM-AREA-POINTER
2039 ;;;   2) a BYTE-OFFSET from the SAP to begin at
2040 ;;;   3) optionally, LENGTH-ONLY, which if non-NIL, means to only return
2041 ;;;      the byte length of the arguments (to avoid unnecessary consing)
2042 ;;; It should read information from the SAP starting at BYTE-OFFSET, and
2043 ;;; return four values:
2044 ;;;   1) the error number
2045 ;;;   2) the total length, in bytes, of the information
2046 ;;;   3) a list of SC-OFFSETs of the locations of the error parameters
2047 ;;;   4) a list of the length (as read from the SAP), in bytes, of each
2048 ;;;      of the return values.
2049 (defun handle-break-args (error-parse-fun stream dstate)
2050   (declare (type function error-parse-fun)
2051            (type (or null stream) stream)
2052            (type disassem-state dstate))
2053   (multiple-value-bind (errnum adjust sc-offsets lengths)
2054       (funcall error-parse-fun
2055                (dstate-segment-sap dstate)
2056                (dstate-next-offs dstate)
2057                (null stream))
2058     (when stream
2059       (setf (dstate-cur-offs dstate)
2060             (dstate-next-offs dstate))
2061       (flet ((emit-err-arg (note)
2062                (let ((num (pop lengths)))
2063                  (print-notes-and-newline stream dstate)
2064                  (print-current-address stream dstate)
2065                  (print-bytes num stream dstate)
2066                  (incf (dstate-cur-offs dstate) num)
2067                  (when note
2068                    (note note dstate)))))
2069         (emit-err-arg nil)
2070         (emit-err-arg (symbol-name (get-internal-error-name errnum)))
2071         (dolist (sc-offs sc-offsets)
2072           (emit-err-arg (get-sc-name sc-offs)))))
2073     (incf (dstate-next-offs dstate)
2074           adjust)))