07075b5a940ed4c8d4e7256a4feec0b47cbd9de9
[sbcl.git] / src / compiler / debug-dump.lisp
1 ;;;; stuff that creates debugger information from the compiler's
2 ;;;; internal data structures
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!C")
14
15 (file-comment
16   "$Header$")
17
18 (deftype byte-buffer () '(vector (unsigned-byte 8)))
19 (defvar *byte-buffer*)
20 (declaim (type byte-buffer *byte-buffer*))
21 \f
22 ;;;; debug blocks
23
24 (deftype location-kind ()
25   '(member :unknown-return :known-return :internal-error :non-local-exit
26            :block-start :call-site :single-value-return :non-local-entry))
27
28 ;;; The Location-Info structure holds the information what we need about
29 ;;; locations which code generation decided were "interesting".
30 (defstruct (location-info
31             (:constructor make-location-info (kind label vop)))
32   ;; The kind of location noted.
33   (kind nil :type location-kind)
34   ;; The label pointing to the interesting code location.
35   (label nil :type (or label index null))
36   ;; The VOP that emitted this location (for node, save-set, ir2-block, etc.)
37   (vop nil :type vop))
38
39 ;;; Called during code generation in places where there is an "interesting"
40 ;;; location: some place where we are likely to end up in the debugger, and
41 ;;; thus want debug info.
42 (defun note-debug-location (vop label kind)
43   (declare (type vop vop) (type (or label null) label)
44            (type location-kind kind))
45   (let ((location (make-location-info kind label vop)))
46     (setf (ir2-block-locations (vop-block vop))
47           (nconc (ir2-block-locations (vop-block vop))
48                  (list location)))
49     location))
50
51 #!-sb-fluid (declaim (inline ir2-block-environment))
52 (defun ir2-block-environment (2block)
53   (declare (type ir2-block 2block))
54   (block-environment (ir2-block-block 2block)))
55
56 ;;; Given a local conflicts vector and an IR2 block to represent the set of
57 ;;; live TNs, and the Var-Locs hash-table representing the variables dumped,
58 ;;; compute a bit-vector representing the set of live variables. If the TN is
59 ;;; environment-live, we only mark it as live when it is in scope at Node.
60 (defun compute-live-vars (live node block var-locs vop)
61   (declare (type ir2-block block) (type local-tn-bit-vector live)
62            (type hash-table var-locs) (type node node)
63            (type (or vop null) vop))
64   (let ((res (make-array (logandc2 (+ (hash-table-count var-locs) 7) 7)
65                          :element-type 'bit
66                          :initial-element 0))
67         (spilled (gethash vop
68                           (ir2-component-spilled-vops
69                            (component-info *component-being-compiled*)))))
70     (do-live-tns (tn live block)
71       (let ((leaf (tn-leaf tn)))
72         (when (and (lambda-var-p leaf)
73                    (or (not (member (tn-kind tn)
74                                     '(:environment :debug-environment)))
75                        (rassoc leaf (lexenv-variables (node-lexenv node))))
76                    (or (null spilled)
77                        (not (member tn spilled))))
78           (let ((num (gethash leaf var-locs)))
79             (when num
80               (setf (sbit res num) 1))))))
81     res))
82
83 ;;; The PC for the location most recently dumped.
84 (defvar *previous-location*)
85 (declaim (type index *previous-location*))
86
87 ;;; Dump a compiled debug-location into *BYTE-BUFFER* that describes the
88 ;;; code/source map and live info. If true, VOP is the VOP associated with
89 ;;; this location, for use in determining whether TNs are spilled.
90 (defun dump-1-location (node block kind tlf-num label live var-locs vop)
91   (declare (type node node) (type ir2-block block)
92            (type local-tn-bit-vector live)
93            (type (or label index) label)
94            (type location-kind kind) (type (or index null) tlf-num)
95            (type hash-table var-locs) (type (or vop null) vop))
96
97   (vector-push-extend
98    (dpb (position-or-lose kind compiled-code-location-kinds)
99         compiled-code-location-kind-byte
100         0)
101    *byte-buffer*)
102
103   (let ((loc (if (target-fixnump label) label (label-position label))))
104     (write-var-integer (- loc *previous-location*) *byte-buffer*)
105     (setq *previous-location* loc))
106
107   (let ((path (node-source-path node)))
108     (unless tlf-num
109       (write-var-integer (source-path-tlf-number path) *byte-buffer*))
110     (write-var-integer (source-path-form-number path) *byte-buffer*))
111
112   (write-packed-bit-vector (compute-live-vars live node block var-locs vop)
113                            *byte-buffer*)
114
115   (values))
116
117 ;;; Extract context info from a Location-Info structure and use it to dump a
118 ;;; compiled code-location.
119 (defun dump-location-from-info (loc tlf-num var-locs)
120   (declare (type location-info loc) (type (or index null) tlf-num)
121            (type hash-table var-locs))
122   (let ((vop (location-info-vop loc)))
123     (dump-1-location (vop-node vop)
124                      (vop-block vop)
125                      (location-info-kind loc)
126                      tlf-num
127                      (location-info-label loc)
128                      (vop-save-set vop)
129                      var-locs
130                      vop))
131   (values))
132
133 ;;; Scan all the blocks, determining if all locations are in the same TLF,
134 ;;; and returning it or NIL.
135 (defun find-tlf-number (fun)
136   (declare (type clambda fun))
137   (let ((res (source-path-tlf-number (node-source-path (lambda-bind fun)))))
138     (declare (type (or index null) res))
139     (do-environment-ir2-blocks (2block (lambda-environment fun))
140       (let ((block (ir2-block-block 2block)))
141         (when (eq (block-info block) 2block)
142           (unless (eql (source-path-tlf-number
143                         (node-source-path
144                          (continuation-next
145                           (block-start block))))
146                        res)
147             (setq res nil)))
148         
149         (dolist (loc (ir2-block-locations 2block))
150           (unless (eql (source-path-tlf-number
151                         (node-source-path
152                          (vop-node (location-info-vop loc))))
153                        res)
154             (setq res nil)))))
155     res))
156
157 ;;; Dump out the number of locations and the locations for Block.
158 (defun dump-block-locations (block locations tlf-num var-locs)
159   (declare (type cblock block) (list locations))
160   (if (and locations
161            (eq (location-info-kind (first locations))
162                :non-local-entry))
163       (write-var-integer (length locations) *byte-buffer*)
164       (let ((2block (block-info block)))
165         (write-var-integer (+ (length locations) 1) *byte-buffer*)
166         (dump-1-location (continuation-next (block-start block))
167                          2block :block-start tlf-num
168                          (ir2-block-%label 2block)
169                          (ir2-block-live-out 2block)
170                          var-locs
171                          nil)))
172   (dolist (loc locations)
173     (dump-location-from-info loc tlf-num var-locs))
174   (values))
175
176 ;;; Dump the successors of Block, being careful not to fly into space on
177 ;;; weird successors.
178 (defun dump-block-successors (block env)
179   (declare (type cblock block) (type environment env))
180   (let* ((tail (component-tail (block-component block)))
181          (succ (block-succ block))
182          (valid-succ
183           (if (and succ
184                    (or (eq (car succ) tail)
185                        (not (eq (block-environment (car succ)) env))))
186               ()
187               succ)))
188     (vector-push-extend
189      (dpb (length valid-succ) compiled-debug-block-nsucc-byte 0)
190      *byte-buffer*)
191     (let ((base (block-number
192                  (node-block
193                   (lambda-bind (environment-function env))))))
194       (dolist (b valid-succ)
195         (write-var-integer
196          (the index (- (block-number b) base))
197          *byte-buffer*))))
198   (values))
199
200 ;;; Return a vector and an integer (or null) suitable for use as the BLOCKS
201 ;;; and TLF-NUMBER in Fun's debug-function. This requires two passes to
202 ;;; compute:
203 ;;; -- Scan all blocks, dumping the header and successors followed by all the
204 ;;;    non-elsewhere locations.
205 ;;; -- Dump the elsewhere block header and all the elsewhere locations (if
206 ;;;    any.)
207 (defun compute-debug-blocks (fun var-locs)
208   (declare (type clambda fun) (type hash-table var-locs))
209   (setf (fill-pointer *byte-buffer*) 0)
210   (let ((*previous-location* 0)
211         (tlf-num (find-tlf-number fun))
212         (env (lambda-environment fun))
213         (prev-locs nil)
214         (prev-block nil))
215     (collect ((elsewhere))
216       (do-environment-ir2-blocks (2block env)
217         (let ((block (ir2-block-block 2block)))
218           (when (eq (block-info block) 2block)
219             (when prev-block
220               (dump-block-locations prev-block prev-locs tlf-num var-locs))
221             (setq prev-block block  prev-locs ())
222             (dump-block-successors block env)))
223         
224         (collect ((here prev-locs))
225           (dolist (loc (ir2-block-locations 2block))
226             (if (label-elsewhere-p (location-info-label loc))
227                 (elsewhere loc)
228                 (here loc)))
229           (setq prev-locs (here))))
230
231       (dump-block-locations prev-block prev-locs tlf-num var-locs)
232
233       (when (elsewhere)
234         (vector-push-extend compiled-debug-block-elsewhere-p *byte-buffer*)
235         (write-var-integer (length (elsewhere)) *byte-buffer*)
236         (dolist (loc (elsewhere))
237           (dump-location-from-info loc tlf-num var-locs))))
238
239     (values (copy-seq *byte-buffer*) tlf-num)))
240 \f
241 ;;; Return a list of DEBUG-SOURCE structures containing information derived
242 ;;; from Info. Unless :BYTE-COMPILE T was specified, we always dump the
243 ;;; Start-Positions, since it is too hard figure out whether we need them or
244 ;;; not.
245 (defun debug-source-for-info (info)
246   (declare (type source-info info))
247   (assert (not (source-info-current-file info)))
248   (mapcar #'(lambda (x)
249               (let ((res (make-debug-source
250                           :from :file
251                           :comment (file-info-comment x)
252                           :created (file-info-write-date x)
253                           :compiled (source-info-start-time info)
254                           :source-root (file-info-source-root x)
255                           :start-positions
256                           (unless (eq *byte-compile* 't)
257                             (coerce-to-smallest-eltype
258                              (file-info-positions x)))))
259                     (name (file-info-name x)))
260                 (etypecase name
261                   ((member :lisp)
262                    (setf (debug-source-from res) name)
263                    (setf (debug-source-name res)
264                          (coerce (file-info-forms x) 'simple-vector)))
265                   (pathname
266                    (let* ((untruename (file-info-untruename x))
267                           (dir (pathname-directory untruename)))
268                      (setf (debug-source-name res)
269                            (namestring
270                             (if (and dir (eq (first dir) :absolute))
271                                 untruename
272                                 name))))))
273                 res))
274           (source-info-files info)))
275
276 ;;; Given an arbitrary sequence, coerce it to an unsigned vector if
277 ;;; possible. Ordinarily we coerce it to the smallest specialized vector
278 ;;; we can. However, we also have a special hack for cross-compiling at
279 ;;; bootstrap time, when arbitrarily-specialized aren't fully supported:
280 ;;; in that case, we coerce it only to a vector whose element size is an
281 ;;; integer multiple of output byte size.
282 (defun coerce-to-smallest-eltype (seq)
283   (let ((maxoid #-sb-xc-host 0
284                 ;; An initial value value of 255 prevents us from specializing
285                 ;; the array to anything smaller than (UNSIGNED-BYTE 8), which
286                 ;; keeps the cross-compiler's portable specialized array output
287                 ;; functions happy.
288                 #+sb-xc-host 255))
289     (flet ((frob (x)
290              (if (typep x 'unsigned-byte)
291                (when (>= x maxoid)
292                  (setf maxoid x))
293                (return-from coerce-to-smallest-eltype
294                  (coerce seq 'simple-vector)))))
295       (if (listp seq)
296         (dolist (i seq)
297           (frob i))
298         (dovector (i seq)
299           (frob i)))
300       (coerce seq `(simple-array (integer 0 ,maxoid) (*))))))
301 \f
302 ;;;; variables
303
304 ;;; Return a SC-OFFSET describing TN's location.
305 (defun tn-sc-offset (tn)
306   (declare (type tn tn))
307   (make-sc-offset (sc-number (tn-sc tn))
308                   (tn-offset tn)))
309
310 ;;; Dump info to represent Var's location being TN. ID is an integer that
311 ;;; makes Var's name unique in the function. Buffer is the vector we stick the
312 ;;; result in. If Minimal is true, we suppress name dumping, and set the
313 ;;; minimal flag.
314 ;;;
315 ;;; The debug-var is only marked as always-live if the TN is
316 ;;; environment live and is an argument. If a :debug-environment TN, then we
317 ;;; also exclude set variables, since the variable is not guaranteed to be live
318 ;;; everywhere in that case.
319 (defun dump-1-variable (fun var tn id minimal buffer)
320   (declare (type lambda-var var) (type (or tn null) tn) (type index id)
321            (type clambda fun))
322   (let* ((name (leaf-name var))
323          (save-tn (and tn (tn-save-tn tn)))
324          (kind (and tn (tn-kind tn)))
325          (flags 0))
326     (declare (type index flags))
327     (when minimal
328       (setq flags (logior flags compiled-debug-var-minimal-p))
329       (unless tn
330         (setq flags (logior flags compiled-debug-var-deleted-p))))
331     (when (and (or (eq kind :environment)
332                    (and (eq kind :debug-environment)
333                         (null (basic-var-sets var))))
334                (not (gethash tn (ir2-component-spilled-tns
335                                  (component-info *component-being-compiled*))))
336                (eq (lambda-var-home var) fun))
337       (setq flags (logior flags compiled-debug-var-environment-live)))
338     (when save-tn
339       (setq flags (logior flags compiled-debug-var-save-loc-p)))
340     (unless (or (zerop id) minimal)
341       (setq flags (logior flags compiled-debug-var-id-p)))
342     (vector-push-extend flags buffer)
343     (unless minimal
344       (vector-push-extend name buffer)
345       (unless (zerop id)
346         (vector-push-extend id buffer)))
347     (if tn
348         (vector-push-extend (tn-sc-offset tn) buffer)
349         (assert minimal))
350     (when save-tn
351       (vector-push-extend (tn-sc-offset save-tn) buffer)))
352   (values))
353
354 ;;; Return a vector suitable for use as the DEBUG-FUNCTION-VARIABLES of FUN.
355 ;;; LEVEL is the current DEBUG-INFO quality. VAR-LOCS is a hashtable in which
356 ;;; we enter the translation from LAMBDA-VARS to the relative position of that
357 ;;; variable's location in the resulting vector.
358 (defun compute-variables (fun level var-locs)
359   (declare (type clambda fun) (type hash-table var-locs))
360   (collect ((vars))
361     (labels ((frob-leaf (leaf tn gensym-p)
362                (let ((name (leaf-name leaf)))
363                  (when (and name (leaf-refs leaf) (tn-offset tn)
364                             (or gensym-p (symbol-package name)))
365                    (vars (cons leaf tn)))))
366              (frob-lambda (x gensym-p)
367                (dolist (leaf (lambda-vars x))
368                  (frob-leaf leaf (leaf-info leaf) gensym-p))))
369       (frob-lambda fun t)
370       (when (>= level 2)
371         (dolist (x (ir2-environment-environment
372                     (environment-info (lambda-environment fun))))
373           (let ((thing (car x)))
374             (when (lambda-var-p thing)
375               (frob-leaf thing (cdr x) (= level 3)))))
376         
377         (dolist (let (lambda-lets fun))
378           (frob-lambda let (= level 3)))))
379
380     (let ((sorted (sort (vars) #'string<
381                         :key #'(lambda (x)
382                                  (symbol-name (leaf-name (car x))))))
383           (prev-name nil)
384           (id 0)
385           (i 0)
386           (buffer (make-array 0 :fill-pointer 0 :adjustable t)))
387       (declare (type (or simple-string null) prev-name)
388                (type index id i))
389       (dolist (x sorted)
390         (let* ((var (car x))
391                (name (symbol-name (leaf-name var))))
392           (cond ((and prev-name (string= prev-name name))
393                  (incf id))
394                 (t
395                  (setq id 0  prev-name name)))
396           (dump-1-variable fun var (cdr x) id nil buffer)
397           (setf (gethash var var-locs) i))
398         (incf i))
399       (coerce buffer 'simple-vector))))
400
401 ;;; Return a vector suitable for use as the DEBUG-FUNCTION-VARIABLES of
402 ;;; FUN, representing the arguments to FUN in minimal variable format.
403 (defun compute-minimal-variables (fun)
404   (declare (type clambda fun))
405   (let ((buffer (make-array 0 :fill-pointer 0 :adjustable t)))
406     (dolist (var (lambda-vars fun))
407       (dump-1-variable fun var (leaf-info var) 0 t buffer))
408     (coerce buffer 'simple-vector)))
409
410 ;;; Return Var's relative position in the function's variables (determined
411 ;;; from the Var-Locs hashtable.)  If Var is deleted, the return DELETED.
412 (defun debug-location-for (var var-locs)
413   (declare (type lambda-var var) (type hash-table var-locs))
414   (let ((res (gethash var var-locs)))
415     (cond (res)
416           (t
417            (assert (or (null (leaf-refs var))
418                        (not (tn-offset (leaf-info var)))))
419            'deleted))))
420 \f
421 ;;;; arguments/returns
422
423 ;;; Return a vector to be used as the COMPILED-DEBUG-FUNCTION-ARGUMENTS for
424 ;;; Fun. If fun is the MAIN-ENTRY for an optional dispatch, then look at the
425 ;;; ARGLIST to determine the syntax, otherwise pretend all arguments are fixed.
426 ;;;
427 ;;; ### This assumption breaks down in EPs other than the main-entry, since
428 ;;; they may or may not have supplied-p vars, etc.
429 (defun compute-arguments (fun var-locs)
430   (declare (type clambda fun) (type hash-table var-locs))
431   (collect ((res))
432     (let ((od (lambda-optional-dispatch fun)))
433       (if (and od (eq (optional-dispatch-main-entry od) fun))
434           (let ((actual-vars (lambda-vars fun))
435                 (saw-optional nil))
436             (dolist (arg (optional-dispatch-arglist od))
437               (let ((info (lambda-var-arg-info arg))
438                     (actual (pop actual-vars)))
439                 (cond (info
440                        (case (arg-info-kind info)
441                          (:keyword
442                           (res (arg-info-keyword info)))
443                          (:rest
444                           (res 'rest-arg))
445                          (:more-context
446                           (res 'more-arg))
447                          (:optional
448                           (unless saw-optional
449                             (res 'optional-args)
450                             (setq saw-optional t))))
451                        (res (debug-location-for actual var-locs))
452                        (when (arg-info-supplied-p info)
453                          (res 'supplied-p)
454                          (res (debug-location-for (pop actual-vars) var-locs))))
455                       (t
456                        (res (debug-location-for actual var-locs)))))))
457           (dolist (var (lambda-vars fun))
458             (res (debug-location-for var var-locs)))))
459
460     (coerce-to-smallest-eltype (res))))
461
462 ;;; Return a vector of SC offsets describing Fun's return locations. (Must
463 ;;; be known values return...)
464 (defun compute-debug-returns (fun)
465   (coerce-to-smallest-eltype
466    (mapcar #'(lambda (loc)
467                (tn-sc-offset loc))
468            (return-info-locations (tail-set-info (lambda-tail-set fun))))))
469 \f
470 ;;;; debug functions
471
472 ;;; Return a C-D-F structure with all the mandatory slots filled in.
473 (defun dfun-from-fun (fun)
474   (declare (type clambda fun))
475   (let* ((2env (environment-info (lambda-environment fun)))
476          (dispatch (lambda-optional-dispatch fun))
477          (main-p (and dispatch
478                       (eq fun (optional-dispatch-main-entry dispatch)))))
479     (make-compiled-debug-function
480      :name (cond ((leaf-name fun))
481                  ((let ((ef (functional-entry-function
482                              fun)))
483                     (and ef (leaf-name ef))))
484                  ((and main-p (leaf-name dispatch)))
485                  (t
486                   (component-name
487                    (block-component (node-block (lambda-bind fun))))))
488      :kind (if main-p nil (functional-kind fun))
489      :return-pc (tn-sc-offset (ir2-environment-return-pc 2env))
490      :old-fp (tn-sc-offset (ir2-environment-old-fp 2env))
491      :start-pc (label-position (ir2-environment-environment-start 2env))
492      :elsewhere-pc (label-position (ir2-environment-elsewhere-start 2env)))))
493
494 ;;; Return a complete C-D-F structure for Fun. This involves determining
495 ;;; the DEBUG-INFO level and filling in optional slots as appropriate.
496 (defun compute-1-debug-function (fun var-locs)
497   (declare (type clambda fun) (type hash-table var-locs))
498   (let* ((dfun (dfun-from-fun fun))
499          (actual-level
500           (cookie-debug (lexenv-cookie (node-lexenv (lambda-bind fun)))))
501          (level (if #!+sb-dyncount *collect-dynamic-statistics*
502                     #!-sb-dyncount nil
503                     (max actual-level 2)
504                     actual-level)))
505     (cond ((zerop level))
506           ((and (<= level 1)
507                 (let ((od (lambda-optional-dispatch fun)))
508                   (or (not od)
509                       (not (eq (optional-dispatch-main-entry od) fun)))))
510            (setf (compiled-debug-function-variables dfun)
511                  (compute-minimal-variables fun))
512            (setf (compiled-debug-function-arguments dfun) :minimal))
513           (t
514            (setf (compiled-debug-function-variables dfun)
515                  (compute-variables fun level var-locs))
516            (setf (compiled-debug-function-arguments dfun)
517                  (compute-arguments fun var-locs))))
518
519     (when (>= level 2)
520       (multiple-value-bind (blocks tlf-num) (compute-debug-blocks fun var-locs)
521         (setf (compiled-debug-function-tlf-number dfun) tlf-num)
522         (setf (compiled-debug-function-blocks dfun) blocks)))
523
524     (if (external-entry-point-p fun)
525         (setf (compiled-debug-function-returns dfun) :standard)
526         (let ((info (tail-set-info (lambda-tail-set fun))))
527           (when info
528             (cond ((eq (return-info-kind info) :unknown)
529                    (setf (compiled-debug-function-returns dfun)
530                          :standard))
531                   ((/= level 0)
532                    (setf (compiled-debug-function-returns dfun)
533                          (compute-debug-returns fun)))))))
534     dfun))
535 \f
536 ;;;; minimal debug functions
537
538 ;;; Return true if Dfun can be represented as a minimal debug function.
539 ;;; Dfun is a cons (<start offset> . C-D-F).
540 (defun debug-function-minimal-p (dfun)
541   (declare (type cons dfun))
542   (let ((dfun (cdr dfun)))
543     (and (member (compiled-debug-function-arguments dfun) '(:minimal nil))
544          (null (compiled-debug-function-blocks dfun)))))
545
546 ;;; Dump a packed binary representation of a Dfun into *byte-buffer*.
547 ;;; Prev-Start and Start are the byte offsets in the code where the previous
548 ;;; function started and where this one starts. Prev-Elsewhere is the previous
549 ;;; function's elsewhere PC.
550 (defun dump-1-minimal-dfun (dfun prev-start start prev-elsewhere)
551   (declare (type compiled-debug-function dfun)
552            (type index prev-start start prev-elsewhere))
553   (let* ((name (compiled-debug-function-name dfun))
554          (setf-p (and (consp name) (eq (car name) 'setf)
555                       (consp (cdr name)) (symbolp (cadr name))))
556          (base-name (if setf-p (cadr name) name))
557          (pkg (when (symbolp base-name)
558                 (symbol-package base-name)))
559          (name-rep
560           (cond ((stringp base-name)
561                  minimal-debug-function-name-component)
562                 ((not pkg)
563                  minimal-debug-function-name-uninterned)
564                 ((eq pkg *package*)
565                  minimal-debug-function-name-symbol)
566                 (t
567                  minimal-debug-function-name-packaged))))
568     (assert (or (atom name) setf-p))
569     (let ((options 0))
570       (setf (ldb minimal-debug-function-name-style-byte options) name-rep)
571       (setf (ldb minimal-debug-function-kind-byte options)
572             (position-or-lose (compiled-debug-function-kind dfun)
573                       minimal-debug-function-kinds))
574       (setf (ldb minimal-debug-function-returns-byte options)
575             (etypecase (compiled-debug-function-returns dfun)
576               ((member :standard) minimal-debug-function-returns-standard)
577               ((member :fixed) minimal-debug-function-returns-fixed)
578               (vector minimal-debug-function-returns-specified)))
579       (vector-push-extend options *byte-buffer*))
580
581     (let ((flags 0))
582       (when setf-p
583         (setq flags (logior flags minimal-debug-function-setf-bit)))
584       (when (compiled-debug-function-nfp dfun)
585         (setq flags (logior flags minimal-debug-function-nfp-bit)))
586       (when (compiled-debug-function-variables dfun)
587         (setq flags (logior flags minimal-debug-function-variables-bit)))
588       (vector-push-extend flags *byte-buffer*))
589
590     (when (eql name-rep minimal-debug-function-name-packaged)
591       (write-var-string (package-name pkg) *byte-buffer*))
592     (unless (stringp base-name)
593       (write-var-string (symbol-name base-name) *byte-buffer*))
594
595     (let ((vars (compiled-debug-function-variables dfun)))
596       (when vars
597         (let ((len (length vars)))
598           (write-var-integer len *byte-buffer*)
599           (dotimes (i len)
600             (vector-push-extend (aref vars i) *byte-buffer*)))))
601
602     (let ((returns (compiled-debug-function-returns dfun)))
603       (when (vectorp returns)
604         (let ((len (length returns)))
605           (write-var-integer len *byte-buffer*)
606           (dotimes (i len)
607             (write-var-integer (aref returns i) *byte-buffer*)))))
608
609     (write-var-integer (compiled-debug-function-return-pc dfun)
610                        *byte-buffer*)
611     (write-var-integer (compiled-debug-function-old-fp dfun)
612                        *byte-buffer*)
613     (when (compiled-debug-function-nfp dfun)
614       (write-var-integer (compiled-debug-function-nfp dfun)
615                          *byte-buffer*))
616     (write-var-integer (- start prev-start) *byte-buffer*)
617     (write-var-integer (- (compiled-debug-function-start-pc dfun) start)
618                        *byte-buffer*)
619     (write-var-integer (- (compiled-debug-function-elsewhere-pc dfun)
620                           prev-elsewhere)
621                        *byte-buffer*)))
622
623 ;;; Return a byte-vector holding all the debug functions for a component in
624 ;;; the packed binary minimal-debug-function format.
625 (defun compute-minimal-debug-functions (dfuns)
626   (declare (list dfuns))
627   (setf (fill-pointer *byte-buffer*) 0)
628   (let ((prev-start 0)
629         (prev-elsewhere 0))
630     (dolist (dfun dfuns)
631       (let ((start (car dfun))
632             (elsewhere (compiled-debug-function-elsewhere-pc (cdr dfun))))
633         (dump-1-minimal-dfun (cdr dfun) prev-start start prev-elsewhere)
634         (setq prev-start start  prev-elsewhere elsewhere))))
635   (copy-seq *byte-buffer*))
636 \f
637 ;;;; full component dumping
638
639 ;;; Compute the full form (simple-vector) function map.
640 (defun compute-debug-function-map (sorted)
641   (declare (list sorted))
642   (let* ((len (1- (* (length sorted) 2)))
643          (funs-vec (make-array len)))
644     (do ((i -1 (+ i 2))
645          (sorted sorted (cdr sorted)))
646         ((= i len))
647       (declare (fixnum i))
648       (let ((dfun (car sorted)))
649         (unless (minusp i)
650           (setf (svref funs-vec i) (car dfun)))
651         (setf (svref funs-vec (1+ i)) (cdr dfun))))
652     funs-vec))
653
654 ;;; Return a DEBUG-INFO structure describing COMPONENT. This has to be
655 ;;; called after assembly so that source map information is available.
656 (defun debug-info-for-component (component)
657   (declare (type component component))
658   (collect ((dfuns))
659     (let ((var-locs (make-hash-table :test 'eq))
660           ;; FIXME: What is *BYTE-BUFFER* for? Has it become dead code now that
661           ;; we no longer use minimal-debug-function representation?
662           (*byte-buffer* (make-array 10
663                                      :element-type '(unsigned-byte 8)
664                                      :fill-pointer 0
665                                      :adjustable t)))
666       (dolist (fun (component-lambdas component))
667         (clrhash var-locs)
668         (dfuns (cons (label-position
669                       (block-label (node-block (lambda-bind fun))))
670                      (compute-1-debug-function fun var-locs))))
671       (let* ((sorted (sort (dfuns) #'< :key #'car))
672              ;; FIXME: CMU CL had
673              ;;    (IF (EVERY #'DEBUG-FUNCTION-MINIMAL-P SORTED)
674              ;; (COMPUTE-MINIMAL-DEBUG-FUNCTIONS SORTED)
675              ;; (COMPUTE-DEBUG-FUNCTION-MAP SORTED))
676              ;; here. We've gotten rid of the minimal-debug-function case in
677              ;; SBCL because the minimal representation couldn't be made to
678              ;; transform properly under package renaming. Now that that
679              ;; case is gone, a lot of code is dead, and once everything is
680              ;; known to work, the dead code should be deleted.
681              (function-map (compute-debug-function-map sorted)))
682         (make-compiled-debug-info :name (component-name component)
683                                   :function-map function-map)))))
684 \f
685 ;;; Write BITS out to BYTE-BUFFER in backend byte order. The length of BITS
686 ;;; must be evenly divisible by eight.
687 (defun write-packed-bit-vector (bits byte-buffer)
688   (declare (type simple-bit-vector bits) (type byte-buffer byte-buffer))
689   (multiple-value-bind (initial step done)
690       (ecase *backend-byte-order*
691         (:little-endian (values 0  1  8))
692         (:big-endian    (values 7 -1 -1)))
693     (let ((shift initial)
694           (byte 0))
695       (dotimes (i (length bits))
696         (let ((int (aref bits i)))
697           (setf byte (logior byte (ash int shift)))
698           (incf shift step))
699         (when (= shift done)
700           (vector-push-extend byte byte-buffer)
701           (setf shift initial
702                 byte 0)))
703       (unless (= shift initial)
704         (vector-push-extend byte byte-buffer))))
705   (values))