/[cmucl]/src/compiler/debug-dump.lisp
ViewVC logotype

Contents of /src/compiler/debug-dump.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.45 - (hide annotations)
Mon Oct 6 12:39:58 2003 UTC (10 years, 6 months ago) by gerd
Branch: MAIN
Changes since 1.44: +11 -1 lines
	FIXME: Dead code elimination sometimes leaves spurious references
	to unused lambda-vars.  Unused vars are not packed, and so have a
	tn but a null tn-offset.  Some of these cases have been fixed, but
	not all of them, and since it's not sure if/when all of them will
	be fixed, add a hack for these cases.

	* src/compiler/debug-dump.lisp (dump-1-variable): If the
	tn-offset of a tn is null, set the tn to nil.

	(compile nil
          '(lambda (a b)
             (declare (optimize (speed 3) (debug 1)))
             (let ((v7
                    (let ((v2 (block b5 (return-from b5 (if t b -4)))))
                         a)))
                  -65667836)))
	 => error nil is not integer, in dump-1-variable.

	This is caused by an exit from the return-from being deleted,
	while leaving the exit's value untouched, which leads to a
	remaining reference to lambda-var b, which is unused and therefore
	not being packed.

	* src/compiler/ir1opt.lisp (maybe-delete-exit): When no node
	receives the value of the exit, flush the dest of the exit's
	value.
1 wlott 1.1 ;;; -*- Package: C; Log: C.Log -*-
2     ;;;
3     ;;; **********************************************************************
4 ram 1.21 ;;; This code was written as part of the CMU Common Lisp project at
5     ;;; Carnegie Mellon University, and has been placed in the public domain.
6     ;;;
7     (ext:file-comment
8 gerd 1.45 "$Header: /tiger/var/lib/cvsroots/cmucl/src/compiler/debug-dump.lisp,v 1.45 2003/10/06 12:39:58 gerd Exp $")
9 ram 1.21 ;;;
10 wlott 1.1 ;;; **********************************************************************
11     ;;;
12     ;;; This file contains stuff that creates debugger information from the
13     ;;; compiler's internal data structures.
14     ;;;
15     ;;; Written by Rob MacLachlan
16     ;;;
17 wlott 1.31 (in-package :c)
18 wlott 1.1
19 ram 1.22 (defvar *byte-buffer*)
20     (declaim (type (vector (unsigned-byte 8)) *byte-buffer*))
21 ram 1.2
22 ram 1.3
23     ;;;; Debug blocks:
24 ram 1.2
25 ram 1.3 (deftype location-kind ()
26 ram 1.6 '(member :unknown-return :known-return :internal-error :non-local-exit
27 wlott 1.28 :block-start :call-site :single-value-return :non-local-entry))
28 ram 1.3
29    
30     ;;; The Location-Info structure holds the information what we need about
31     ;;; locations which code generation decided were "interesting".
32     ;;;
33     (defstruct (location-info
34     (:constructor make-location-info (kind label vop)))
35     ;;
36     ;; The kind of location noted.
37     (kind nil :type location-kind)
38     ;;
39     ;; The label pointing to the interesting code location.
40 wlott 1.31 (label nil :type (or label index null))
41 ram 1.3 ;;
42     ;; The VOP that emitted this location (for node, save-set, ir2-block, etc.)
43     (vop nil :type vop))
44    
45     ;;; NOTE-DEBUG-LOCATION -- Interface
46     ;;;
47     ;;; Called during code generation in places where there is an "interesting"
48     ;;; location: some place where we are likely to end up in the debugger, and
49     ;;; thus want debug info.
50     ;;;
51 ram 1.8 (defun note-debug-location (vop label kind)
52 wlott 1.31 (declare (type vop vop) (type (or label null) label)
53 wlott 1.28 (type location-kind kind))
54 wlott 1.31 (let ((location (make-location-info kind label vop)))
55     (setf (ir2-block-locations (vop-block vop))
56     (nconc (ir2-block-locations (vop-block vop))
57     (list location)))
58     location))
59 ram 1.3
60    
61     ;;; IR2-BLOCK-ENVIRONMENT -- Interface
62     ;;;
63 pw 1.42 (declaim (inline ir2-block-environment))
64 ram 1.3 (defun ir2-block-environment (2block)
65     (declare (type ir2-block 2block))
66 ram 1.15 (block-environment (ir2-block-block 2block)))
67 ram 1.3
68    
69     ;;; COMPUTE-LIVE-VARS -- Internal
70     ;;;
71     ;;; Given a local conflicts vector and an IR2 block to represent the set of
72 ram 1.22 ;;; live TNs, and the Var-Locs hash-table representing the variables dumped,
73 ram 1.15 ;;; compute a bit-vector representing the set of live variables. If the TN is
74     ;;; environment-live, we only mark it as live when it is in scope at Node.
75 ram 1.3 ;;;
76 ram 1.18 (defun compute-live-vars (live node block var-locs vop)
77 ram 1.3 (declare (type ir2-block block) (type local-tn-bit-vector live)
78 ram 1.18 (type hash-table var-locs) (type node node)
79     (type (or vop null) vop))
80 ram 1.8 (let ((res (make-array (logandc2 (+ (hash-table-count var-locs) 7) 7)
81 ram 1.3 :element-type 'bit
82 ram 1.18 :initial-element 0))
83     (spilled (gethash vop
84     (ir2-component-spilled-vops
85     (component-info *compile-component*)))))
86 ram 1.3 (do-live-tns (tn live block)
87     (let ((leaf (tn-leaf tn)))
88 ram 1.15 (when (and (lambda-var-p leaf)
89 ram 1.16 (or (not (member (tn-kind tn)
90     '(:environment :debug-environment)))
91 ram 1.18 (rassoc leaf (lexenv-variables (node-lexenv node))))
92     (or (null spilled)
93     (not (member tn spilled))))
94 ram 1.3 (let ((num (gethash leaf var-locs)))
95     (when num
96 ram 1.7 (setf (sbit res num) 1))))))
97     res))
98 ram 1.3
99    
100     ;;; The PC for the location most recently dumped.
101     ;;;
102     (defvar *previous-location*)
103 pw 1.42 (declaim (type index *previous-location*))
104 ram 1.3
105     ;;; DUMP-1-LOCATION -- Internal
106     ;;;
107     ;;; Dump a compiled debug-location into *BYTE-BUFFER* that describes the
108 ram 1.18 ;;; code/source map and live info. If true, VOP is the VOP associated with
109     ;;; this location, for use in determining whether TNs are spilled.
110 ram 1.3 ;;;
111 ram 1.18 (defun dump-1-location (node block kind tlf-num label live var-locs vop)
112 ram 1.3 (declare (type node node) (type ir2-block block)
113 wlott 1.30 (type local-tn-bit-vector live)
114     (type (or label index) label)
115 ram 1.6 (type location-kind kind) (type (or index null) tlf-num)
116 ram 1.18 (type hash-table var-locs) (type (or vop null) vop))
117 ram 1.3
118     (vector-push-extend
119 ram 1.38 (dpb (eposition kind compiled-code-location-kinds)
120 ram 1.8 compiled-code-location-kind-byte
121     0)
122 ram 1.3 *byte-buffer*)
123    
124 wlott 1.30 (let ((loc (if (fixnump label) label (label-position label))))
125 ram 1.3 (write-var-integer (- loc *previous-location*) *byte-buffer*)
126     (setq *previous-location* loc))
127 ram 1.15
128     (let ((path (node-source-path node)))
129     (unless tlf-num
130     (write-var-integer (source-path-tlf-number path) *byte-buffer*))
131     (write-var-integer (source-path-form-number path) *byte-buffer*))
132 ram 1.3
133 ram 1.18 (write-packed-bit-vector (compute-live-vars live node block var-locs vop)
134 ram 1.3 *byte-buffer*)
135    
136     (undefined-value))
137    
138    
139     ;;; DUMP-LOCATION-FROM-INFO -- Internal
140     ;;;
141     ;;; Extract context info from a Location-Info structure and use it to dump a
142     ;;; compiled code-location.
143     ;;;
144 ram 1.7 (defun dump-location-from-info (loc tlf-num var-locs)
145     (declare (type location-info loc) (type (or index null) tlf-num)
146     (type hash-table var-locs))
147 ram 1.3 (let ((vop (location-info-vop loc)))
148     (dump-1-location (vop-node vop)
149     (vop-block vop)
150     (location-info-kind loc)
151     tlf-num
152     (location-info-label loc)
153     (vop-save-set vop)
154 ram 1.18 var-locs
155     vop))
156 ram 1.3 (undefined-value))
157    
158    
159 ram 1.32 ;;; FIND-TLF-NUMBER -- Internal
160 ram 1.12 ;;;
161 ram 1.32 ;;; Scan all the blocks, determining if all locations are in the same TLF,
162     ;;; and returing it or NIL.
163 ram 1.12 ;;;
164 ram 1.32 (defun find-tlf-number (fun)
165 ram 1.12 (declare (type clambda fun))
166 ram 1.32 (let ((res (source-path-tlf-number (node-source-path (lambda-bind fun)))))
167     (declare (type (or index null) res))
168 ram 1.12 (do-environment-ir2-blocks (2block (lambda-environment fun))
169     (let ((block (ir2-block-block 2block)))
170     (when (eq (block-info block) 2block)
171 ram 1.15 (unless (eql (source-path-tlf-number
172     (node-source-path
173     (continuation-next
174     (block-start block))))
175 ram 1.12 res)
176     (setq res nil)))
177    
178     (dolist (loc (ir2-block-locations 2block))
179 ram 1.15 (unless (eql (source-path-tlf-number
180     (node-source-path
181     (vop-node (location-info-vop loc))))
182 ram 1.12 res)
183     (setq res nil)))))
184     res))
185    
186    
187     ;;; DUMP-BLOCK-LOCATIONS -- Internal
188     ;;;
189     ;;; Dump out the number of locations and the locations for Block.
190     ;;;
191     (defun dump-block-locations (block locations tlf-num var-locs)
192     (declare (type cblock block) (list locations))
193 wlott 1.28 (if (and locations
194 wlott 1.29 (eq (location-info-kind (first locations))
195 wlott 1.28 :non-local-entry))
196     (write-var-integer (length locations) *byte-buffer*)
197     (let ((2block (block-info block)))
198     (write-var-integer (+ (length locations) 1) *byte-buffer*)
199     (dump-1-location (continuation-next (block-start block))
200     2block :block-start tlf-num
201     (ir2-block-%label 2block)
202     (ir2-block-live-out 2block)
203     var-locs
204     nil)))
205 ram 1.12 (dolist (loc locations)
206     (dump-location-from-info loc tlf-num var-locs))
207     (undefined-value))
208    
209    
210     ;;; DUMP-BLOCK-SUCCESSORS -- Internal
211     ;;;
212     ;;; Dump the successors of Block, being careful not to fly into space on
213     ;;; weird successors.
214     ;;;
215     (defun dump-block-successors (block env)
216     (declare (type cblock block) (type environment env))
217     (let* ((tail (component-tail (block-component block)))
218     (succ (block-succ block))
219     (valid-succ
220     (if (and succ
221     (or (eq (car succ) tail)
222 ram 1.15 (not (eq (block-environment (car succ)) env))))
223 ram 1.12 ()
224     succ)))
225     (vector-push-extend
226     (dpb (length valid-succ) compiled-debug-block-nsucc-byte 0)
227     *byte-buffer*)
228 ram 1.32 (let ((base (block-number
229     (node-block
230     (lambda-bind (environment-function env))))))
231     (dolist (b valid-succ)
232     (write-var-integer
233     (the index (- (block-number b) base))
234     *byte-buffer*))))
235 ram 1.12 (undefined-value))
236    
237    
238 ram 1.3 ;;; COMPUTE-DEBUG-BLOCKS -- Internal
239     ;;;
240     ;;; Return a vector and an integer (or null) suitable for use as the BLOCKS
241 ram 1.33 ;;; and TLF-NUMBER in Fun's debug-function. This requires two passes to
242 ram 1.3 ;;; compute:
243     ;;; -- Scan all blocks, dumping the header and successors followed by all the
244     ;;; non-elsewhere locations.
245 ram 1.8 ;;; -- Dump the elsewhere block header and all the elsewhere locations (if
246     ;;; any.)
247 ram 1.3 ;;;
248     (defun compute-debug-blocks (fun var-locs)
249     (declare (type clambda fun) (type hash-table var-locs))
250     (setf (fill-pointer *byte-buffer*) 0)
251     (let ((*previous-location* 0)
252 ram 1.32 (tlf-num (find-tlf-number fun))
253 ram 1.12 (env (lambda-environment fun))
254     (prev-locs nil)
255     (prev-block nil))
256     (collect ((elsewhere))
257     (do-environment-ir2-blocks (2block env)
258 ram 1.3 (let ((block (ir2-block-block 2block)))
259     (when (eq (block-info block) 2block)
260 ram 1.12 (when prev-block
261     (dump-block-locations prev-block prev-locs tlf-num var-locs))
262     (setq prev-block block prev-locs ())
263     (dump-block-successors block env)))
264    
265     (collect ((here prev-locs))
266 ram 1.3 (dolist (loc (ir2-block-locations 2block))
267 ram 1.12 (if (label-elsewhere-p (location-info-label loc))
268     (elsewhere loc)
269     (here loc)))
270     (setq prev-locs (here))))
271 ram 1.3
272 ram 1.12 (dump-block-locations prev-block prev-locs tlf-num var-locs)
273 ram 1.3
274 ram 1.8 (when (elsewhere)
275     (vector-push-extend compiled-debug-block-elsewhere-p *byte-buffer*)
276     (write-var-integer (length (elsewhere)) *byte-buffer*)
277     (dolist (loc (elsewhere))
278     (dump-location-from-info loc tlf-num var-locs))))
279    
280 ram 1.3 (values (copy-seq *byte-buffer*) tlf-num)))
281    
282    
283 wlott 1.1 ;;; DEBUG-SOURCE-FOR-INFO -- Interface
284     ;;;
285     ;;; Return a list of DEBUG-SOURCE structures containing information derived
286 ram 1.37 ;;; from Info. Unless :BYTE-COMPILE T was specified, we always dump the
287     ;;; Start-Positions, since it is too hard figure out whether we need them or
288     ;;; not.
289 wlott 1.1 ;;;
290     (defun debug-source-for-info (info)
291     (declare (type source-info info))
292     (assert (not (source-info-current-file info)))
293     (mapcar #'(lambda (x)
294 ram 1.34 (let ((res (make-debug-source
295 wlott 1.1 :from :file
296 ram 1.11 :comment (file-info-comment x)
297 wlott 1.1 :created (file-info-write-date x)
298     :compiled (source-info-start-time info)
299     :source-root (file-info-source-root x)
300 ram 1.6 :start-positions
301 ram 1.37 (unless (eq *byte-compile* 't)
302     (coerce-to-smallest-eltype
303     (file-info-positions x)))))
304 ram 1.35 (name (file-info-name x)))
305 ram 1.34 (etypecase name
306 ram 1.36 ((member :stream :lisp)
307 ram 1.34 (setf (debug-source-from res) name)
308     (setf (debug-source-name res)
309     (coerce (file-info-forms x) 'simple-vector)))
310     (pathname
311     (let* ((untruename (file-info-untruename x))
312     (dir (pathname-directory untruename)))
313     (setf (debug-source-name res)
314     (namestring
315     (if (and dir (eq (first dir) :absolute))
316     untruename
317     name))))))
318 wlott 1.1 res))
319     (source-info-files info)))
320    
321    
322 ram 1.3 ;;; COERCE-TO-SMALLEST-ELTYPE -- Internal
323     ;;;
324     ;;; Given an arbirtary sequence, coerce it to an unsigned vector if
325     ;;; possible.
326     ;;;
327     (defun coerce-to-smallest-eltype (seq)
328 ram 1.18 (declare (type sequence seq))
329 ram 1.3 (let ((max 0))
330 ram 1.18 (declare (type (or index null) max))
331 ram 1.3 (macrolet ((frob ()
332 ram 1.18 '(if (and (typep val 'index) max)
333 ram 1.3 (when (> val max)
334     (setq max val))
335     (setq max nil))))
336     (if (listp seq)
337 ram 1.6 (dolist (val seq)
338 ram 1.3 (frob))
339     (dotimes (i (length seq))
340     (let ((val (aref seq i)))
341     (frob)))))
342    
343     (if max
344 wlott 1.40 (coerce seq `(simple-array (integer 0 ,max) (*)))
345 ram 1.3 (coerce seq 'simple-vector))))
346    
347    
348 ram 1.15 ;;;; Variables:
349 ram 1.3
350 ram 1.2 ;;; TN-SC-OFFSET -- Internal
351 wlott 1.1 ;;;
352 ram 1.2 ;;; Return a SC-OFFSET describing TN's location.
353 wlott 1.1 ;;;
354 ram 1.2 (defun tn-sc-offset (tn)
355     (declare (type tn tn))
356     (make-sc-offset (sc-number (tn-sc tn))
357     (tn-offset tn)))
358 wlott 1.1
359    
360 ram 1.3 ;;; DUMP-1-VARIABLE -- Internal
361 ram 1.2 ;;;
362     ;;; Dump info to represent Var's location being TN. ID is an integer that
363     ;;; makes Var's name unique in the function. Buffer is the vector we stick the
364 ram 1.22 ;;; result in. If Minimal is true, we suppress name dumping, and set the
365     ;;; minimal flag.
366 ram 1.2 ;;;
367 ram 1.16 ;;; The debug-variable is only marked as always-live if the TN is
368     ;;; environment live and is an argument. If a :debug-environment TN, then we
369     ;;; also exclude set variables, since the variable is not guranteed to be live
370     ;;; everywhere in that case.
371     ;;;
372 ram 1.22 (defun dump-1-variable (fun var tn id minimal buffer)
373     (declare (type lambda-var var) (type (or tn null) tn) (type index id)
374 ram 1.15 (type clambda fun))
375 ram 1.2 (let* ((name (leaf-name var))
376     (package (symbol-package name))
377     (package-p (and package (not (eq package *package*))))
378 ram 1.22 (save-tn (and tn (tn-save-tn tn)))
379     (kind (and tn (tn-kind tn)))
380 ram 1.2 (flags 0))
381 ram 1.18 (declare (type index flags))
382 gerd 1.45 ;;
383     ;; FIXME: Dead code elimination sometimes leaves spurious
384     ;; references to unused lambda-vars. Unused vars are not packed,
385     ;; and so have a tn but a null tn-offset. Some of these cases
386     ;; have been fixed, but not all of them, and since it's not sure
387     ;; if/when all of them will be fixed, add a hack for these cases.
388     ;; -- gerd 2003-10-06
389     (when (and tn (null (tn-offset tn)))
390     (setq tn nil))
391    
392 ram 1.22 (cond (minimal
393     (setq flags (logior flags compiled-debug-variable-minimal-p))
394     (unless tn
395     (setq flags (logior flags compiled-debug-variable-deleted-p))))
396     (t
397     (unless package
398     (setq flags (logior flags compiled-debug-variable-uninterned)))
399     (when package-p
400     (setq flags (logior flags compiled-debug-variable-packaged)))))
401 ram 1.16 (when (and (or (eq kind :environment)
402     (and (eq kind :debug-environment)
403     (null (basic-var-sets var))))
404 ram 1.18 (not (gethash tn (ir2-component-spilled-tns
405     (component-info *compile-component*))))
406 ram 1.15 (eq (lambda-var-home var) fun))
407 ram 1.8 (setq flags (logior flags compiled-debug-variable-environment-live)))
408 ram 1.2 (when save-tn
409 ram 1.8 (setq flags (logior flags compiled-debug-variable-save-loc-p)))
410 ram 1.22 (unless (or (zerop id) minimal)
411 ram 1.8 (setq flags (logior flags compiled-debug-variable-id-p)))
412 ram 1.2 (vector-push-extend flags buffer)
413 ram 1.22 (unless minimal
414     (write-var-string (symbol-name name) buffer)
415     (when package-p
416     (write-var-string (package-name package) buffer))
417     (unless (zerop id)
418     (write-var-integer id buffer)))
419     (if tn
420     (write-var-integer (tn-sc-offset tn) buffer)
421     (assert minimal))
422 ram 1.2 (when save-tn
423     (write-var-integer (tn-sc-offset save-tn) buffer)))
424     (undefined-value))
425    
426    
427 wlott 1.1 ;;; COMPUTE-VARIABLES -- Internal
428     ;;;
429     ;;; Return a vector suitable for use as the DEBUG-FUNCTION-VARIABLES of Fun.
430 ram 1.2 ;;; Level is the current DEBUG-INFO quality. Var-Locs is a hashtable in which
431     ;;; we enter the translation from LAMBDA-VARS to the relative position of that
432     ;;; variable's location in the resulting vector.
433 wlott 1.1 ;;;
434 ram 1.2 (defun compute-variables (fun level var-locs)
435 wlott 1.1 (declare (type clambda fun) (type hash-table var-locs))
436 ram 1.2 (collect ((vars))
437     (labels ((frob-leaf (leaf tn gensym-p)
438 wlott 1.1 (let ((name (leaf-name leaf)))
439 ram 1.20 (when (and name (leaf-refs leaf) (tn-offset tn)
440 ram 1.2 (or gensym-p (symbol-package name)))
441     (vars (cons leaf tn)))))
442     (frob-lambda (x gensym-p)
443     (dolist (leaf (lambda-vars x))
444     (frob-leaf leaf (leaf-info leaf) gensym-p))))
445     (frob-lambda fun t)
446     (when (>= level 2)
447     (dolist (x (ir2-environment-environment
448     (environment-info (lambda-environment fun))))
449     (let ((thing (car x)))
450     (when (lambda-var-p thing)
451     (frob-leaf thing (cdr x) (= level 3)))))
452    
453     (dolist (let (lambda-lets fun))
454 ram 1.6 (frob-lambda let (= level 3)))))
455 wlott 1.1
456 ram 1.2 (setf (fill-pointer *byte-buffer*) 0)
457     (let ((sorted (sort (vars) #'string<
458     :key #'(lambda (x)
459     (symbol-name (leaf-name (car x))))))
460     (prev-name nil)
461     (id 0)
462     (i 0))
463 ram 1.18 (declare (type (or simple-string null) prev-name)
464     (type index id i))
465 ram 1.2 (dolist (x sorted)
466     (let* ((var (car x))
467     (name (symbol-name (leaf-name var))))
468     (cond ((and prev-name (string= prev-name name))
469     (incf id))
470     (t
471     (setq id 0 prev-name name)))
472 ram 1.22 (dump-1-variable fun var (cdr x) id nil *byte-buffer*)
473 ram 1.3 (setf (gethash var var-locs) i))
474 ram 1.2 (incf i)))
475 wlott 1.1
476 ram 1.2 (copy-seq *byte-buffer*)))
477 wlott 1.1
478 ram 1.2
479 ram 1.22 ;;; COMPUTE-MINIMAL-VARIABLES -- Internal
480     ;;;
481     ;;; Dump out the arguments to Fun in the minimal variable format.
482     ;;;
483     (defun compute-minimal-variables (fun)
484     (declare (type clambda fun))
485     (setf (fill-pointer *byte-buffer*) 0)
486     (dolist (var (lambda-vars fun))
487     (dump-1-variable fun var (leaf-info var) 0 t *byte-buffer*))
488     (copy-seq *byte-buffer*))
489    
490    
491 ram 1.2 ;;; DEBUG-LOCATION-FOR -- Internal
492     ;;;
493     ;;; Return Var's relative position in the function's variables (determined
494 ram 1.8 ;;; from the Var-Locs hashtable.) If Var is deleted, the return DELETED.
495 ram 1.2 ;;;
496     (defun debug-location-for (var var-locs)
497 ram 1.7 (declare (type lambda-var var) (type hash-table var-locs))
498 ram 1.2 (let ((res (gethash var var-locs)))
499 ram 1.8 (cond (res)
500     (t
501 ram 1.20 (assert (or (null (leaf-refs var))
502     (not (tn-offset (leaf-info var)))))
503 ram 1.8 'deleted))))
504 ram 1.2
505 ram 1.3
506     ;;;; Arguments/returns:
507 ram 1.2
508 wlott 1.1 ;;; COMPUTE-ARGUMENTS -- Internal
509     ;;;
510     ;;; Return a vector to be used as the COMPILED-DEBUG-FUNCTION-ARGUMENTS for
511     ;;; Fun. If fun is the MAIN-ENTRY for an optional dispatch, then look at the
512     ;;; ARGLIST to determine the syntax, otherwise pretend all arguments are fixed.
513     ;;;
514     ;;; ### This assumption breaks down in EPs other than the main-entry, since
515     ;;; they may or may not have supplied-p vars, etc.
516     ;;;
517     (defun compute-arguments (fun var-locs)
518     (declare (type clambda fun) (type hash-table var-locs))
519     (collect ((res))
520     (let ((od (lambda-optional-dispatch fun)))
521     (if (and od (eq (optional-dispatch-main-entry od) fun))
522 ram 1.12 (let ((actual-vars (lambda-vars fun))
523     (saw-optional nil))
524 wlott 1.1 (dolist (arg (optional-dispatch-arglist od))
525     (let ((info (lambda-var-arg-info arg))
526     (actual (pop actual-vars)))
527     (cond (info
528     (case (arg-info-kind info)
529     (:keyword
530     (res (arg-info-keyword info)))
531     (:rest
532 ram 1.12 (res 'rest-arg))
533 wlott 1.39 (:more-context
534     (res 'more-arg))
535 ram 1.12 (:optional
536     (unless saw-optional
537     (res 'optional-args)
538     (setq saw-optional t))))
539 wlott 1.1 (res (debug-location-for actual var-locs))
540     (when (arg-info-supplied-p info)
541     (res 'supplied-p)
542     (res (debug-location-for (pop actual-vars) var-locs))))
543     (t
544     (res (debug-location-for actual var-locs)))))))
545     (dolist (var (lambda-vars fun))
546     (res (debug-location-for var var-locs)))))
547    
548 ram 1.3 (coerce-to-smallest-eltype (res))))
549 wlott 1.1
550    
551     ;;; COMPUTE-DEBUG-RETURNS -- Internal
552     ;;;
553 ram 1.8 ;;; Return a vector of SC offsets describing Fun's return locations. (Must
554     ;;; be known values return...)
555 wlott 1.1 ;;;
556     (defun compute-debug-returns (fun)
557 ram 1.3 (coerce-to-smallest-eltype
558     (mapcar #'(lambda (loc)
559     (tn-sc-offset loc))
560     (return-info-locations (tail-set-info (lambda-tail-set fun))))))
561 wlott 1.1
562 ram 1.3
563 ram 1.22 ;;;; Debug functions:
564    
565     ;;; DFUN-FROM-FUN -- Internal
566     ;;;
567     ;;; Return a C-D-F structure with all the mandatory slots filled in.
568     ;;;
569     (defun dfun-from-fun (fun)
570     (declare (type clambda fun))
571     (let* ((2env (environment-info (lambda-environment fun)))
572     (dispatch (lambda-optional-dispatch fun))
573     (main-p (and dispatch
574     (eq fun (optional-dispatch-main-entry dispatch)))))
575     (make-compiled-debug-function
576     :name (cond ((leaf-name fun))
577     ((let ((ef (functional-entry-function
578     fun)))
579     (and ef (leaf-name ef))))
580     ((and main-p (leaf-name dispatch)))
581     (t
582     (component-name
583     (block-component (node-block (lambda-bind fun))))))
584     :kind (if main-p nil (functional-kind fun))
585     :return-pc (tn-sc-offset (ir2-environment-return-pc 2env))
586     :old-fp (tn-sc-offset (ir2-environment-old-fp 2env))
587     :start-pc (label-position (ir2-environment-environment-start 2env))
588     :elsewhere-pc (label-position (ir2-environment-elsewhere-start 2env)))))
589    
590    
591     ;;; COMPUTE-1-DEBUG-FUNCTION -- Internal
592     ;;;
593     ;;; Return a complete C-D-F structure for Fun. This involves determining
594     ;;; the DEBUG-INFO level and filling in optional slots as appropriate.
595     ;;;
596     (defun compute-1-debug-function (fun var-locs)
597     (declare (type clambda fun) (type hash-table var-locs))
598     (let* ((dfun (dfun-from-fun fun))
599 ram 1.32 (actual-level
600     (cookie-debug (lexenv-cookie (node-lexenv (lambda-bind fun)))))
601     (level (if *collect-dynamic-statistics*
602     (max actual-level 2)
603     actual-level)))
604 ram 1.22 (cond ((zerop level))
605     ((and (<= level 1)
606     (let ((od (lambda-optional-dispatch fun)))
607     (or (not od)
608     (not (eq (optional-dispatch-main-entry od) fun)))))
609     (setf (compiled-debug-function-variables dfun)
610     (compute-minimal-variables fun))
611     (setf (compiled-debug-function-arguments dfun) :minimal))
612     (t
613 ram 1.24 (setf (compiled-debug-function-variables dfun)
614     (compute-variables fun level var-locs))
615     (setf (compiled-debug-function-arguments dfun)
616     (compute-arguments fun var-locs))))
617 ram 1.22
618     (when (>= level 2)
619     (multiple-value-bind (blocks tlf-num)
620     (compute-debug-blocks fun var-locs)
621     (setf (compiled-debug-function-tlf-number dfun) tlf-num)
622     (setf (compiled-debug-function-blocks dfun) blocks)))
623    
624     (if (external-entry-point-p fun)
625     (setf (compiled-debug-function-returns dfun) :standard)
626 ram 1.26 (let ((info (tail-set-info (lambda-tail-set fun))))
627     (when info
628     (cond ((eq (return-info-kind info) :unknown)
629     (setf (compiled-debug-function-returns dfun)
630     :standard))
631     ((/= level 0)
632     (setf (compiled-debug-function-returns dfun)
633     (compute-debug-returns fun)))))))
634 ram 1.22 dfun))
635    
636    
637     ;;;; Minimal debug functions:
638    
639     ;;; DEBUG-FUNCTION-MINIMAL-P -- Internal
640     ;;;
641     ;;; Return true if Dfun can be represented as a minimal debug function.
642     ;;; Dfun is a cons (<start offset> . C-D-F).
643     ;;;
644     (defun debug-function-minimal-p (dfun)
645     (declare (type cons dfun))
646     (let ((dfun (cdr dfun)))
647     (and (member (compiled-debug-function-arguments dfun) '(:minimal nil))
648     (null (compiled-debug-function-blocks dfun)))))
649    
650    
651     ;;; DUMP-1-MINIMAL-DFUN -- Internal
652     ;;;
653     ;;; Dump a packed binary representation of a Dfun into *byte-buffer*.
654     ;;; Prev-Start and Start are the byte offsets in the code where the previous
655     ;;; function started and where this one starts. Prev-Elsewhere is the previous
656     ;;; function's elsewhere PC.
657     ;;;
658     (defun dump-1-minimal-dfun (dfun prev-start start prev-elsewhere)
659     (declare (type compiled-debug-function dfun)
660     (type index prev-start start prev-elsewhere))
661     (let* ((name (compiled-debug-function-name dfun))
662     (setf-p (and (consp name) (eq (car name) 'setf)
663     (consp (cdr name)) (symbolp (cadr name))))
664 emarsden 1.44 (base-name (if (stringp name) name
665     (multiple-value-bind (valid block-name)
666     (valid-function-name-p name)
667     (assert valid)
668     block-name)))
669 ram 1.22 (pkg (when (symbolp base-name)
670     (symbol-package base-name)))
671     (name-rep
672     (cond ((stringp base-name)
673     minimal-debug-function-name-component)
674     ((not pkg)
675     minimal-debug-function-name-uninterned)
676     ((eq pkg *package*)
677     minimal-debug-function-name-symbol)
678     (t
679     minimal-debug-function-name-packaged))))
680     (let ((options 0))
681     (setf (ldb minimal-debug-function-name-style-byte options) name-rep)
682     (setf (ldb minimal-debug-function-kind-byte options)
683 ram 1.38 (eposition (compiled-debug-function-kind dfun)
684 ram 1.22 minimal-debug-function-kinds))
685     (setf (ldb minimal-debug-function-returns-byte options)
686     (etypecase (compiled-debug-function-returns dfun)
687     ((member :standard) minimal-debug-function-returns-standard)
688     ((member :fixed) minimal-debug-function-returns-fixed)
689     (vector minimal-debug-function-returns-specified)))
690     (vector-push-extend options *byte-buffer*))
691    
692     (let ((flags 0))
693     (when setf-p
694     (setq flags (logior flags minimal-debug-function-setf-bit)))
695     (when (compiled-debug-function-nfp dfun)
696     (setq flags (logior flags minimal-debug-function-nfp-bit)))
697     (when (compiled-debug-function-variables dfun)
698     (setq flags (logior flags minimal-debug-function-variables-bit)))
699     (vector-push-extend flags *byte-buffer*))
700    
701     (when (eql name-rep minimal-debug-function-name-packaged)
702     (write-var-string (package-name pkg) *byte-buffer*))
703     (unless (stringp base-name)
704     (write-var-string (symbol-name base-name) *byte-buffer*))
705    
706     (let ((vars (compiled-debug-function-variables dfun)))
707     (when vars
708     (let ((len (length vars)))
709     (write-var-integer len *byte-buffer*)
710     (dotimes (i len)
711     (vector-push-extend (aref vars i) *byte-buffer*)))))
712    
713     (let ((returns (compiled-debug-function-returns dfun)))
714     (when (vectorp returns)
715     (let ((len (length returns)))
716     (write-var-integer len *byte-buffer*)
717     (dotimes (i len)
718     (write-var-integer (aref returns i) *byte-buffer*)))))
719    
720     (write-var-integer (compiled-debug-function-return-pc dfun)
721     *byte-buffer*)
722     (write-var-integer (compiled-debug-function-old-fp dfun)
723     *byte-buffer*)
724     (when (compiled-debug-function-nfp dfun)
725     (write-var-integer (compiled-debug-function-nfp dfun)
726     *byte-buffer*))
727     (write-var-integer (- start prev-start) *byte-buffer*)
728     (write-var-integer (- (compiled-debug-function-start-pc dfun) start)
729     *byte-buffer*)
730     (write-var-integer (- (compiled-debug-function-elsewhere-pc dfun)
731     prev-elsewhere)
732     *byte-buffer*)))
733    
734    
735     ;;; COMPUTE-MINIMAL-DEBUG-FUNCTIONS -- Internal
736     ;;;
737     ;;; Return a byte-vector holding all the debug functions for a component in
738     ;;; the packed binary minimal-debug-function format.
739     ;;;
740     (defun compute-minimal-debug-functions (dfuns)
741     (declare (list dfuns))
742     (setf (fill-pointer *byte-buffer*) 0)
743     (let ((prev-start 0)
744     (prev-elsewhere 0))
745     (dolist (dfun dfuns)
746     (let ((start (car dfun))
747     (elsewhere (compiled-debug-function-elsewhere-pc (cdr dfun))))
748     (dump-1-minimal-dfun (cdr dfun) prev-start start prev-elsewhere)
749     (setq prev-start start prev-elsewhere elsewhere))))
750     (copy-seq *byte-buffer*))
751    
752    
753     ;;;; Full component dumping:
754    
755     ;;; COMPUTE-DEBUG-FUNCTION-MAP -- Internal
756     ;;;
757     ;;; Compute the full form (simple-vector) function map.
758     ;;;
759     (defun compute-debug-function-map (sorted)
760     (declare (list sorted))
761     (let* ((len (1- (* (length sorted) 2)))
762     (funs-vec (make-array len)))
763     (do ((i -1 (+ i 2))
764     (sorted sorted (cdr sorted)))
765     ((= i len))
766     (declare (fixnum i))
767     (let ((dfun (car sorted)))
768     (unless (minusp i)
769     (setf (svref funs-vec i) (car dfun)))
770     (setf (svref funs-vec (1+ i)) (cdr dfun))))
771     funs-vec))
772    
773    
774 wlott 1.1 ;;; DEBUG-INFO-FOR-COMPONENT -- Interface
775     ;;;
776 ram 1.22 ;;; Return a debug-info structure describing component. This has to be
777     ;;; called after assembly so that source map information is available.
778     ;;;
779 wlott 1.17 (defun debug-info-for-component (component)
780     (declare (type component component))
781 ram 1.18 (let ((res (make-compiled-debug-info :name (component-name component)
782 ram 1.2 :package (package-name *package*))))
783 wlott 1.1 (collect ((dfuns))
784 ram 1.22 (let ((var-locs (make-hash-table :test #'eq))
785     (*byte-buffer*
786     (make-array 10 :element-type '(unsigned-byte 8)
787     :fill-pointer 0 :adjustable t)))
788 wlott 1.1 (dolist (fun (component-lambdas component))
789     (clrhash var-locs)
790 ram 1.22 (dfuns (cons (label-position
791     (block-label (node-block (lambda-bind fun))))
792 ram 1.23 (compute-1-debug-function fun var-locs))))
793    
794     (let ((sorted (sort (dfuns) #'< :key #'car)))
795     (setf (compiled-debug-info-function-map res)
796     (if (every #'debug-function-minimal-p sorted)
797     (compute-minimal-debug-functions sorted)
798     (compute-debug-function-map sorted))))))
799 wlott 1.1
800     res))

  ViewVC Help
Powered by ViewVC 1.1.5