1 ;;;; This file contains code which does the translation from Lisp code
2 ;;;; to the first intermediate representation (IR1).
4 ;;;; This software is part of the SBCL system. See the README file for
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.
15 (declaim (special *compiler-error-bailout
*))
17 ;;; *CURRENT-FORM-NUMBER* is used in FIND-SOURCE-PATHS to compute the
18 ;;; form number to associate with a source path. This should be bound
19 ;;; to an initial value of 0 before the processing of each truly
21 (declaim (type index
*current-form-number
*))
22 (defvar *current-form-number
*)
24 ;;; *SOURCE-PATHS* is a hashtable from source code forms to the path
25 ;;; taken through the source to reach the form. This provides a way to
26 ;;; keep track of the location of original source forms, even when
27 ;;; macroexpansions and other arbitary permutations of the code
28 ;;; happen. This table is initialized by calling FIND-SOURCE-PATHS on
29 ;;; the original source.
31 ;;; It is fairly useless to store symbols, characters, or fixnums in
32 ;;; this table, as 42 is EQ to 42 no matter where in the source it
33 ;;; appears. GET-SOURCE-PATH and NOTE-SOURCE-PATH functions should be
34 ;;; always used to access this table.
35 (declaim (hash-table *source-paths
*))
36 (defvar *source-paths
*)
38 (declaim (inline source-form-has-path-p
))
39 (defun source-form-has-path-p (form)
40 (not (typep form
'(or symbol fixnum character
))))
42 (defun get-source-path (form)
43 (when (source-form-has-path-p form
)
44 (gethash form
*source-paths
*)))
46 (defun ensure-source-path (form)
47 (or (get-source-path form
)
48 (cons (simplify-source-path-form form
)
51 (defun simplify-source-path-form (form)
53 (let ((op (car form
)))
54 ;; In the compiler functions can be directly represented
55 ;; by leaves. Having leaves in the source path is pretty
56 ;; hard on the poor user, however, so replace with the
57 ;; source-name when possible.
58 (if (and (leaf-p op
) (leaf-has-source-name-p op
))
59 (cons (leaf-source-name op
) (cdr form
))
63 (defun note-source-path (form &rest arguments
)
64 (when (source-form-has-path-p form
)
65 (setf (gethash form
*source-paths
*)
66 (apply #'list
* 'original-source-start
*current-form-number
* arguments
))))
68 ;;; *CURRENT-COMPONENT* is the COMPONENT structure which we link
69 ;;; blocks into as we generate them. This just serves to glue the
70 ;;; emitted blocks together until local call analysis and flow graph
71 ;;; canonicalization figure out what is really going on. We need to
72 ;;; keep track of all the blocks generated so that we can delete them
73 ;;; if they turn out to be unreachable.
75 ;;; FIXME: It's confusing having one variable named *CURRENT-COMPONENT*
76 ;;; and another named *COMPONENT-BEING-COMPILED*. (In CMU CL they
77 ;;; were called *CURRENT-COMPONENT* and *COMPILE-COMPONENT* respectively,
78 ;;; which was also confusing.)
79 (declaim (type (or component null
) *current-component
*))
80 (defvar *current-component
*)
82 ;;; *CURRENT-PATH* is the source path of the form we are currently
83 ;;; translating. See NODE-SOURCE-PATH in the NODE structure.
84 (declaim (list *current-path
*))
85 (defvar *current-path
*)
87 (defun call-with-current-source-form (thunk &rest forms
)
88 (let ((*current-path
* (when (and (some #'identity forms
)
89 (boundp '*source-paths
*))
90 (or (some #'get-source-path forms
)
91 (when (boundp '*current-path
*)
95 (defvar *derive-function-types
* nil
96 "Should the compiler assume that function types will never change,
97 so that it can use type information inferred from current definitions
98 to optimize code which uses those definitions? Setting this true
99 gives non-ANSI, early-CMU-CL behavior. It can be useful for improving
100 the efficiency of stable code.")
102 (defvar *fun-names-in-this-file
* nil
)
104 (defvar *post-binding-variable-lexenv
* nil
)
106 ;;;; namespace management utilities
108 ;; As with LEXENV-FIND, we assume use of *LEXENV*, but macroexpanders
109 ;; receive an explicit environment and should pass it.
110 ;; A declaration will trump a proclamation.
111 (defun fun-lexically-notinline-p (name &optional
(env *lexenv
*))
115 #!+(and sb-fasteval
(host-feature sb-xc
))
116 (sb!interpreter
:basic-env
117 (sb!interpreter
::fun-lexically-notinline-p name env
))
119 (let ((fun (cdr (assoc name
(lexenv-funs env
) :test
#'equal
))))
120 ;; FIXME: this seems to omit FUNCTIONAL
121 (when (defined-fun-p fun
)
122 (return-from fun-lexically-notinline-p
123 (eq (defined-fun-inlinep fun
) :notinline
))))))))
124 ;; If ANSWER is NIL, go for the global value
125 (eq (or answer
(info :function
:inlinep name
))
128 (defun maybe-defined-here (name where
)
129 (if (and (eq :defined where
)
130 (member name
*fun-names-in-this-file
* :test
#'equal
))
134 ;;; Return a GLOBAL-VAR structure usable for referencing the global
136 (defun find-global-fun (name latep
)
137 (unless (info :function
:kind name
)
138 (setf (info :function
:kind name
) :function
)
139 (setf (info :function
:where-from name
) :assumed
))
140 (let ((where (info :function
:where-from name
)))
141 (when (and (eq where
:assumed
)
142 ;; In the ordinary target Lisp, it's silly to report
143 ;; undefinedness when the function is defined in the
144 ;; running Lisp. But at cross-compile time, the current
145 ;; definedness of a function is irrelevant to the
146 ;; definedness at runtime, which is what matters.
147 #-sb-xc-host
(not (fboundp name
))
148 ;; LATEP is true when the user has indicated that
149 ;; late-late binding is desired by using eg. a quoted
150 ;; symbol -- in which case it makes little sense to
151 ;; complain about undefined functions.
153 (note-undefined-reference name
:function
))
154 (let ((ftype (proclaimed-ftype name
))
155 (notinline (fun-lexically-notinline-p name
)))
157 :kind
:global-function
159 :type
(if (or (eq where
:declared
)
162 *derive-function-types
*))
164 (specifier-type 'function
))
165 :defined-type
(if (and (not latep
) (not notinline
))
167 (specifier-type 'function
))
168 :where-from
(if notinline
170 (maybe-defined-here name where
))))))
172 ;;; Have some DEFINED-FUN-FUNCTIONALS of a *FREE-FUNS* entry become invalid?
175 ;;; This was added to fix bug 138 in SBCL. It is possible for a *FREE-FUNS*
176 ;;; entry to contain a DEFINED-FUN whose DEFINED-FUN-FUNCTIONAL object
177 ;;; contained IR1 stuff (NODEs, BLOCKs...) referring to an already compiled
178 ;;; (aka "dead") component. When this IR1 stuff was reused in a new component,
179 ;;; under further obscure circumstances it could be used by
180 ;;; WITH-IR1-ENVIRONMENT-FROM-NODE to generate a binding for
181 ;;; *CURRENT-COMPONENT*. At that point things got all confused, since IR1
182 ;;; conversion was sending code to a component which had already been compiled
183 ;;; and would never be compiled again.
185 ;;; Note: as of 1.0.24.41 this seems to happen only in XC, and the original
186 ;;; BUGS entry also makes it seem like this might not be an issue at all on
188 (defun clear-invalid-functionals (free-fun)
189 ;; There might be other reasons that *FREE-FUN* entries could
190 ;; become invalid, but the only one we've been bitten by so far
191 ;; (sbcl-0.pre7.118) is this one:
192 (when (defined-fun-p free-fun
)
193 (setf (defined-fun-functionals free-fun
)
194 (delete-if (lambda (functional)
195 (or (eq (functional-kind functional
) :deleted
)
196 (when (lambda-p functional
)
198 ;; (The main reason for this first test is to bail
199 ;; out early in cases where the LAMBDA-COMPONENT
200 ;; call in the second test would fail because links
201 ;; it needs are uninitialized or invalid.)
203 ;; If the BIND node for this LAMBDA is null, then
204 ;; according to the slot comments, the LAMBDA has
205 ;; been deleted or its call has been deleted. In
206 ;; that case, it seems rather questionable to reuse
207 ;; it, and certainly it shouldn't be necessary to
208 ;; reuse it, so we cheerfully declare it invalid.
209 (not (lambda-bind functional
))
210 ;; If this IR1 stuff belongs to a dead component,
211 ;; then we can't reuse it without getting into
212 ;; bizarre confusion.
213 (eq (component-info (lambda-component functional
))
215 (defined-fun-functionals free-fun
)))
218 ;;; If NAME already has a valid entry in *FREE-FUNS*, then return
219 ;;; the value. Otherwise, make a new GLOBAL-VAR using information from
220 ;;; the global environment and enter it in *FREE-FUNS*. If NAME
221 ;;; names a macro or special form, then we error out using the
222 ;;; supplied context which indicates what we were trying to do that
223 ;;; demanded a function.
224 (declaim (ftype (sfunction (t string
) global-var
) find-free-fun
))
225 (defun find-free-fun (name context
)
226 (or (let ((old-free-fun (gethash name
*free-funs
*)))
228 (clear-invalid-functionals old-free-fun
)
230 (let ((kind (info :function
:kind name
)))
232 ((:macro
:special-form
)
233 (compiler-error "The ~(~S~) name ~S was found ~A."
236 (check-fun-name name
)
237 (let ((expansion (fun-name-inline-expansion name
))
238 (inlinep (info :function
:inlinep name
)))
239 (setf (gethash name
*free-funs
*)
240 (if (or expansion inlinep
)
241 (let ((where (info :function
:where-from name
)))
244 :inline-expansion expansion
246 :where-from
(if (eq inlinep
:notinline
)
248 (maybe-defined-here name where
))
249 :type
(if (and (eq inlinep
:notinline
)
250 (neq where
:declared
))
251 (specifier-type 'function
)
252 (proclaimed-ftype name
))))
253 (find-global-fun name nil
)))))))))
255 ;;; Return the LEAF structure for the lexically apparent function
256 ;;; definition of NAME.
257 (declaim (ftype (sfunction (t string
) leaf
) find-lexically-apparent-fun
))
258 (defun find-lexically-apparent-fun (name context
)
259 (let ((var (lexenv-find name funs
:test
#'equal
)))
262 (aver (and (consp var
) (eq (car var
) 'macro
)))
263 (compiler-error "found macro name ~S ~A" name context
))
266 (find-free-fun name context
)))))
268 (defun maybe-find-free-var (name)
269 (gethash name
*free-vars
*))
271 ;;; Return the LEAF node for a global variable reference to NAME. If
272 ;;; NAME is already entered in *FREE-VARS*, then we just return the
273 ;;; corresponding value. Otherwise, we make a new leaf using
274 ;;; information from the global environment and enter it in
275 ;;; *FREE-VARS*. If the variable is unknown, then we emit a warning.
276 (declaim (ftype (sfunction (t) (or leaf cons heap-alien-info
)) find-free-var
))
277 (defun find-free-var (name)
278 (unless (symbolp name
)
279 (compiler-error "Variable name is not a symbol: ~S." name
))
280 (or (gethash name
*free-vars
*)
281 (let ((kind (info :variable
:kind name
))
282 (type (info :variable
:type name
))
283 (where-from (info :variable
:where-from name
))
284 (deprecation-state (deprecated-thing-p 'variable name
)))
285 (when (and (eq kind
:unknown
) (not deprecation-state
))
286 (note-undefined-reference name
:variable
))
287 (setf (gethash name
*free-vars
*)
290 (info :variable
:alien-info name
))
291 ;; FIXME: The return value in this case should really be
292 ;; of type SB!C::LEAF. I don't feel too badly about it,
293 ;; because the MACRO idiom is scattered throughout this
294 ;; file, but it should be cleaned up so we're not
295 ;; throwing random conses around. --njf 2002-03-23
297 (let ((expansion (info :variable
:macro-expansion name
))
298 (type (type-specifier (info :variable
:type name
))))
299 `(macro .
(the ,type
,expansion
))))
301 (let ((value (symbol-value name
)))
302 ;; Override the values of standard symbols in XC,
303 ;; since we can't redefine them.
305 (when (eql (find-symbol (symbol-name name
) :cl
) name
)
306 (multiple-value-bind (xc-value foundp
)
307 (xc-constant-value name
)
309 (setf value xc-value
))
310 ((not (eq value name
))
312 "Using cross-compilation host's definition of ~S: ~A~%"
313 name
(symbol-value name
))))))
314 (find-constant value name
)))
316 (make-global-var :kind kind
319 :where-from where-from
)))))))
321 ;;; Grovel over CONSTANT checking for any sub-parts that need to be
322 ;;; processed with MAKE-LOAD-FORM. We have to be careful, because
323 ;;; CONSTANT might be circular. We also check that the constant (and
324 ;;; any subparts) are dumpable at all.
325 (defun maybe-emit-make-load-forms (constant &optional
(name nil namep
))
326 (let ((xset (alloc-xset)))
327 (labels ((trivialp (value)
331 (or unboxed-array
#!+sb-simd-pack simd-pack
)
333 (and array
(not (array t
)))
337 string
))) ; subsumed by UNBOXED-ARRAY
339 ;; Unless VALUE is an object which which obviously
340 ;; can't contain other objects
341 (unless (trivialp value
)
342 (if (xset-member-p value xset
)
343 (return-from grovel nil
)
344 (add-to-xset value xset
))
348 (grovel (cdr value
)))
350 (dotimes (i (length value
))
351 (grovel (svref value i
))))
353 (dotimes (i (length value
))
354 (grovel (aref value i
))))
356 ;; Even though the (ARRAY T) branch does the exact
357 ;; same thing as this branch we do this separately
358 ;; so that the compiler can use faster versions of
359 ;; array-total-size and row-major-aref.
360 (dotimes (i (array-total-size value
))
361 (grovel (row-major-aref value i
))))
363 (dotimes (i (array-total-size value
))
364 (grovel (row-major-aref value i
))))
365 (#+sb-xc-host structure
!object
366 #-sb-xc-host instance
367 ;; In the target SBCL, we can dump any instance, but
368 ;; in the cross-compilation host, %INSTANCE-FOO
369 ;; functions don't work on general instances, only on
370 ;; STRUCTURE!OBJECTs.
372 ;; Behold the wonderfully clear sense of this-
373 ;; WHEN (EMIT-MAKE-LOAD-FORM VALUE)
374 ;; meaning "when you're _NOT_ using a custom load-form"
376 ;; FIXME: What about funcallable instances with
377 ;; user-defined MAKE-LOAD-FORM methods?
378 (when (emit-make-load-form value
)
380 (aver (eql (layout-bitmap (%instance-layout value
))
381 sb
!kernel
::+layout-all-tagged
+))
382 (do-instance-tagged-slot (i value
)
383 (grovel (%instance-ref value i
)))))
386 "Objects of type ~/sb!impl:print-type-specifier/ ~
387 can't be dumped into fasl files."
388 (type-of value
)))))))
389 ;; Dump all non-trivial named constants using the name.
390 (if (and namep
(not (typep constant
'(or symbol character
391 ;; FIXME: Cold init breaks if we
392 ;; try to reference FP constants
395 #-sb-xc-host fixnum
))))
396 (emit-make-load-form constant name
)
400 ;;;; some flow-graph hacking utilities
402 ;;; This function sets up the back link between the node and the
403 ;;; ctran which continues at it.
404 (defun link-node-to-previous-ctran (node ctran
)
405 (declare (type node node
) (type ctran ctran
))
406 (aver (not (ctran-next ctran
)))
407 (setf (ctran-next ctran
) node
)
408 (setf (node-prev node
) ctran
))
410 ;;; This function is used to set the ctran for a node, and thus
411 ;;; determine what is evaluated next. If the ctran has no block, then
412 ;;; we make it be in the block that the node is in. If the ctran heads
413 ;;; its block, we end our block and link it to that block.
414 #!-sb-fluid
(declaim (inline use-ctran
))
415 (defun use-ctran (node ctran
)
416 (declare (type node node
) (type ctran ctran
))
417 (if (eq (ctran-kind ctran
) :unused
)
418 (let ((node-block (ctran-block (node-prev node
))))
419 (setf (ctran-block ctran
) node-block
)
420 (setf (ctran-kind ctran
) :inside-block
)
421 (setf (ctran-use ctran
) node
)
422 (setf (node-next node
) ctran
))
423 (%use-ctran node ctran
)))
424 (defun %use-ctran
(node ctran
)
425 (declare (type node node
) (type ctran ctran
) (inline member
))
426 (let ((block (ctran-block ctran
))
427 (node-block (ctran-block (node-prev node
))))
428 (aver (eq (ctran-kind ctran
) :block-start
))
429 (when (block-last node-block
)
430 (error "~S has already ended." node-block
))
431 (setf (block-last node-block
) node
)
432 (when (block-succ node-block
)
433 (error "~S already has successors." node-block
))
434 (setf (block-succ node-block
) (list block
))
435 (when (memq node-block
(block-pred block
))
436 (error "~S is already a predecessor of ~S." node-block block
))
437 (push node-block
(block-pred block
))))
439 ;;; Insert NEW before OLD in the flow-graph.
440 (defun insert-node-before (old new
)
441 (let ((prev (node-prev old
))
443 (ensure-block-start prev
)
444 (setf (ctran-next prev
) nil
)
445 (link-node-to-previous-ctran new prev
)
447 (link-node-to-previous-ctran old temp
))
450 ;;; This function is used to set the ctran for a node, and thus
451 ;;; determine what receives the value.
452 (defun use-lvar (node lvar
)
453 (declare (type valued-node node
) (type (or lvar null
) lvar
))
454 (aver (not (node-lvar node
)))
456 (setf (node-lvar node
) lvar
)
457 (cond ((null (lvar-uses lvar
))
458 (setf (lvar-uses lvar
) node
))
459 ((listp (lvar-uses lvar
))
460 (aver (not (memq node
(lvar-uses lvar
))))
461 (push node
(lvar-uses lvar
)))
463 (aver (neq node
(lvar-uses lvar
)))
464 (setf (lvar-uses lvar
) (list node
(lvar-uses lvar
)))))
465 (reoptimize-lvar lvar
)))
467 #!-sb-fluid
(declaim (inline use-continuation
))
468 (defun use-continuation (node ctran lvar
)
469 (use-ctran node ctran
)
470 (use-lvar node lvar
))
472 ;;;; exported functions
474 ;;; This function takes a form and the top level form number for that
475 ;;; form, and returns a lambda representing the translation of that
476 ;;; form in the current global environment. The returned lambda is a
477 ;;; top level lambda that can be called to cause evaluation of the
478 ;;; forms. This lambda is in the initial component. If FOR-VALUE is T,
479 ;;; then the value of the form is returned from the function,
480 ;;; otherwise NIL is returned.
482 ;;; This function may have arbitrary effects on the global environment
483 ;;; due to processing of EVAL-WHENs. All syntax error checking is
484 ;;; done, with erroneous forms being replaced by a proxy which signals
485 ;;; an error if it is evaluated. Warnings about possibly inconsistent
486 ;;; or illegal changes to the global environment will also be given.
488 ;;; We make the initial component and convert the form in a PROGN (and
489 ;;; an optional NIL tacked on the end.) We then return the lambda. We
490 ;;; bind all of our state variables here, rather than relying on the
491 ;;; global value (if any) so that IR1 conversion will be reentrant.
492 ;;; This is necessary for EVAL-WHEN processing, etc.
494 ;;; The hashtables used to hold global namespace info must be
495 ;;; reallocated elsewhere. Note also that *LEXENV* is not bound, so
496 ;;; that local macro definitions can be introduced by enclosing code.
497 (defun ir1-toplevel (form path for-value
&optional
(allow-instrumenting t
))
498 (declare (list path
))
499 (let* ((*current-path
* path
)
500 (component (make-empty-component))
501 (*current-component
* component
)
502 (*allow-instrumenting
* allow-instrumenting
))
503 (setf (component-name component
) 'initial-component
)
504 (setf (component-kind component
) :initial
)
505 (let* ((forms (if for-value
`(,form
) `(,form nil
)))
506 (res (ir1-convert-lambda-body
508 :debug-name
(debug-name 'top-level-form
#+sb-xc-host nil
#-sb-xc-host form
))))
509 (setf (functional-entry-fun res
) res
510 (functional-arg-documentation res
) ()
511 (functional-kind res
) :toplevel
)
514 ;;; This function is called on freshly read forms to record the
515 ;;; initial location of each form (and subform.) Form is the form to
516 ;;; find the paths in, and TLF-NUM is the top level form number of the
517 ;;; truly top level form.
519 ;;; This gets a bit interesting when the source code is circular. This
520 ;;; can (reasonably?) happen in the case of circular list constants.
521 (defun find-source-paths (form tlf-num
)
522 (declare (type index tlf-num
))
523 (let ((*current-form-number
* 0))
524 (sub-find-source-paths form
(list tlf-num
)))
526 (defun sub-find-source-paths (form path
)
527 (unless (get-source-path form
)
528 (note-source-path form path
)
529 (incf *current-form-number
*)
533 (declare (fixnum pos
))
536 (when (atom subform
) (return))
537 (let ((fm (car subform
)))
538 (when (sb!int
:comma-p fm
)
539 (setf fm
(sb!int
:comma-expr fm
)))
541 ;; If it's a cons, recurse.
542 (sub-find-source-paths fm
(cons pos path
)))
544 ;; Don't look into quoted constants.
545 ;; KLUDGE: this can't actually know about constants.
546 ;; e.g. (let ((quote (error "foo")))) or
547 ;; (list quote (error "foo")) are not
548 ;; constants and yet are ignored.
551 ;; Otherwise store the containing form. It's not
552 ;; perfect, but better than nothing.
553 (note-source-path subform pos path
)))
555 (setq subform
(cdr subform
))
556 (when (eq subform trail
) (return)))))
560 (setq trail
(cdr trail
)))))))
562 ;;;; IR1-CONVERT, macroexpansion and special form dispatching
564 (declaim (ftype (sfunction (ctran ctran
(or lvar null
) t
)
567 (macrolet (;; Bind *COMPILER-ERROR-BAILOUT* to a function that throws
568 ;; out of the body and converts a condition signalling form
569 ;; instead. The source form is converted to a string since it
570 ;; may contain arbitrary non-externalizable objects.
571 (ir1-error-bailout ((start next result form
) &body body
)
572 (with-unique-names (skip condition
)
574 (let ((,condition
(catch 'ir1-error-abort
575 (let ((*compiler-error-bailout
*
576 (lambda (&optional e
)
577 (throw 'ir1-error-abort e
))))
579 (return-from ,skip nil
)))))
580 (ir1-convert ,start
,next
,result
581 (make-compiler-error-form ,condition
584 ;; Translate FORM into IR1. The code is inserted as the NEXT of the
585 ;; CTRAN START. RESULT is the LVAR which receives the value of the
586 ;; FORM to be translated. The translators call this function
587 ;; recursively to translate their subnodes.
589 ;; As a special hack to make life easier in the compiler, a LEAF
590 ;; IR1-converts into a reference to that LEAF structure. This allows
591 ;; the creation using backquote of forms that contain leaf
592 ;; references, without having to introduce dummy names into the
594 (defun ir1-convert (start next result form
)
595 (let* ((*current-path
* (ensure-source-path form
))
596 (start (instrument-coverage start nil form
)))
597 (ir1-error-bailout (start next result form
)
599 (cond ((and (symbolp form
) (not (keywordp form
)))
600 (ir1-convert-var start next result form
))
602 (reference-leaf start next result form
))
604 (reference-constant start next result form
))))
606 (ir1-convert-functoid start next result form
)))))
609 ;; Generate a reference to a manifest constant, creating a new leaf
611 (defun reference-constant (start next result value
)
612 (declare (type ctran start next
)
613 (type (or lvar null
) result
))
614 (ir1-error-bailout (start next result value
)
615 (let* ((leaf (find-constant value
))
616 (res (make-ref leaf
)))
617 (push res
(leaf-refs leaf
))
618 (link-node-to-previous-ctran res start
)
619 (use-continuation res next result
)))
622 ;;; Add FUNCTIONAL to the COMPONENT-REANALYZE-FUNCTIONALS, unless it's
623 ;;; some trivial type for which reanalysis is a trivial no-op, or
624 ;;; unless it doesn't belong in this component at all.
626 ;;; FUNCTIONAL is returned.
627 (defun maybe-reanalyze-functional (functional)
629 (aver (not (eql (functional-kind functional
) :deleted
))) ; bug 148
630 (aver-live-component *current-component
*)
632 ;; When FUNCTIONAL is of a type for which reanalysis isn't a trivial
634 (when (typep functional
'(or optional-dispatch clambda
))
636 ;; When FUNCTIONAL knows its component
637 (when (lambda-p functional
)
638 (aver (eql (lambda-component functional
) *current-component
*)))
641 (component-reanalyze-functionals *current-component
*)))
645 ;;; Generate a REF node for LEAF, frobbing the LEAF structure as
646 ;;; needed. If LEAF represents a defined function which has already
647 ;;; been converted, and is not :NOTINLINE, then reference the
648 ;;; functional instead.
649 (defun reference-leaf (start next result leaf
&optional
(name '.anonymous.
))
650 (declare (type ctran start next
) (type (or lvar null
) result
) (type leaf leaf
))
651 (assure-leaf-live-p leaf
)
652 (let* ((type (lexenv-find leaf type-restrictions
))
653 (leaf (or (and (defined-fun-p leaf
)
654 (not (eq (defined-fun-inlinep leaf
)
656 (let ((functional (defined-fun-functional leaf
)))
657 (when (and functional
(not (functional-kind functional
)))
658 (maybe-reanalyze-functional functional
))))
659 (when (and (lambda-p leaf
)
660 (memq (functional-kind leaf
)
662 (maybe-reanalyze-functional leaf
))
664 (ref (make-ref leaf name
)))
665 (push ref
(leaf-refs leaf
))
666 (setf (leaf-ever-used leaf
) t
)
667 (link-node-to-previous-ctran ref start
)
668 (cond (type (let* ((ref-ctran (make-ctran))
669 (ref-lvar (make-lvar))
670 (cast (make-cast ref-lvar
671 (make-single-value-type type
)
672 (lexenv-policy *lexenv
*))))
673 (setf (lvar-dest ref-lvar
) cast
)
674 (use-continuation ref ref-ctran ref-lvar
)
675 (link-node-to-previous-ctran cast ref-ctran
)
676 (use-continuation cast next result
)))
677 (t (use-continuation ref next result
)))))
679 (defun always-boundp (name)
680 (case (info :variable
:always-bound name
)
682 ;; Compiling to fasl considers a symbol always-bound if its
683 ;; :always-bound info value is now T or will eventually be T.
684 (:eventually
(producing-fasl-file))))
686 ;;; Convert a reference to a symbolic constant or variable. If the
687 ;;; symbol is entered in the LEXENV-VARS we use that definition,
688 ;;; otherwise we find the current global definition. This is also
689 ;;; where we pick off symbol macro and alien variable references.
690 (defun ir1-convert-var (start next result name
)
691 (declare (type ctran start next
) (type (or lvar null
) result
) (symbol name
))
692 (let ((var (or (lexenv-find name vars
) (find-free-var name
))))
693 (if (and (global-var-p var
) (not (always-boundp name
)))
694 ;; KLUDGE: If the variable may be unbound, convert using SYMBOL-VALUE
695 ;; which is not flushable, so that unbound dead variables signal an
696 ;; error (bug 412, lp#722734): checking for null RESULT is not enough,
697 ;; since variables can become dead due to later optimizations.
698 (ir1-convert start next result
699 (if (eq (global-var-kind var
) :global
)
700 `(symbol-global-value ',name
)
701 `(symbol-value ',name
)))
706 (let ((home (ctran-home-lambda-or-null start
)))
708 (sset-adjoin var
(lambda-calls-or-closes home
))))
709 (when (lambda-var-ignorep var
)
710 ;; (ANSI's specification for the IGNORE declaration requires
711 ;; that this be a STYLE-WARNING, not a full WARNING.)
713 (compiler-style-warn "reading an ignored variable: ~S" name
)
714 ;; there's no need for us to accept ANSI's lameness when
715 ;; processing our own code, though.
717 (warn "reading an ignored variable: ~S" name
)))
719 ;; This case signals {EARLY,LATE}-DEPRECATION-WARNING
720 ;; for CONSTANT nodes in :EARLY and :LATE deprecation
721 ;; (constants in :FINAL deprecation are represented as
723 (aver (memq (check-deprecated-thing 'variable name
)
724 '(nil :early
:late
)))))
725 (reference-leaf start next result var name
))
726 ((cons (eql macro
)) ; symbol-macro
727 ;; This case signals {EARLY,LATE,FINAL}-DEPRECATION-WARNING
728 ;; for symbol-macros. Includes variables, constants,
729 ;; etc. in :FINAL deprecation.
730 (check-deprecated-thing 'variable name
)
731 ;; FIXME: [Free] type declarations. -- APD, 2002-01-26
732 (ir1-convert start next result
(cdr var
)))
734 (ir1-convert start next result
`(%heap-alien
',var
))))))
737 ;;; Find a compiler-macro for a form, taking FUNCALL into account.
738 (defun find-compiler-macro (opname form
)
739 (flet ((legal-cm-name-p (name)
740 (and (legal-fun-name-p name
)
741 (or (not (symbolp name
))
742 (not (sb!xc
:macro-function name
*lexenv
*))))))
743 (if (eq opname
'funcall
)
744 (let ((fun-form (cadr form
)))
745 (cond ((and (consp fun-form
) (eq 'function
(car fun-form
))
746 (not (cddr fun-form
)))
747 (let ((real-fun (cadr fun-form
)))
748 (if (legal-cm-name-p real-fun
)
749 (values (sb!xc
:compiler-macro-function real-fun
*lexenv
*)
752 ((sb!xc
:constantp fun-form
*lexenv
*)
753 (let ((fun (constant-form-value fun-form
*lexenv
*)))
754 (if (legal-cm-name-p fun
)
755 ;; CLHS tells us that local functions must shadow
756 ;; compiler-macro-functions, but since the call is
757 ;; through a name, we are obviously interested
758 ;; in the global function.
759 ;; KLUDGE: CLHS 3.2.2.1.1 also says that it can be
760 ;; "a list whose car is funcall and whose cadr is
761 ;; a list (function name)", that means that
762 ;; (funcall 'name) that gets here doesn't fit the
764 (values (sb!xc
:compiler-macro-function fun nil
) fun
)
768 (if (legal-fun-name-p opname
)
769 (values (sb!xc
:compiler-macro-function opname
*lexenv
*) opname
)
772 ;;; If FORM has a usable compiler macro, use it; otherwise return FORM itself.
773 ;;; Return the name of the compiler-macro as a secondary value, if applicable.
774 (defun expand-compiler-macro (form)
775 (binding* ((name (car form
))
776 ((cmacro-fun cmacro-fun-name
)
777 (find-compiler-macro name form
)))
780 ;; CLHS 3.2.2.1.3 specifies that NOTINLINE
781 ;; suppresses compiler-macros.
782 (not (fun-lexically-notinline-p cmacro-fun-name
)))
783 (check-deprecated-thing 'function name
)
784 (values (handler-case (careful-expand-macro cmacro-fun form t
)
785 (compiler-macro-keyword-problem (condition)
786 (print-compiler-message
787 *error-output
* "note: ~A" (list condition
))
791 (values form nil
)))))
793 ;;; Picks off special forms and compiler-macro expansions, and hands
794 ;;; the rest to IR1-CONVERT-COMMON-FUNCTOID
795 (defun ir1-convert-functoid (start next result form
)
796 (let* ((op (car form
))
797 (translator (and (symbolp op
) (info :function
:ir1-convert op
))))
799 (funcall translator start next result form
)
800 (multiple-value-bind (res cmacro-fun-name
)
801 (expand-compiler-macro form
)
803 (ir1-convert-common-functoid start next result form op
))
805 (unless (policy *lexenv
* (zerop store-xref-data
))
806 (record-call cmacro-fun-name
(ctran-block start
)
808 (ir1-convert start next result res
)))))))
810 ;;; Handles the "common" cases: any other forms except special forms
811 ;;; and compiler-macros.
812 (defun ir1-convert-common-functoid (start next result form op
)
813 (cond ((or (symbolp op
) (leaf-p op
))
814 (let ((lexical-def (if (leaf-p op
) op
(lexenv-find op funs
))))
815 (typecase lexical-def
817 (check-deprecated-thing 'function op
)
818 (ir1-convert-global-functoid start next result form op
))
820 (ir1-convert-local-combination start next result form
823 (check-deprecated-thing 'function
(leaf-source-name lexical-def
))
824 (ir1-convert-srctran start next result lexical-def form
))
826 (aver (and (consp lexical-def
) (eq (car lexical-def
) 'macro
)))
827 (ir1-convert start next result
828 (careful-expand-macro (cdr lexical-def
) form
))))))
829 ((or (atom op
) (not (eq (car op
) 'lambda
)))
830 (compiler-error "illegal function call"))
832 ;; implicitly (LAMBDA ..) because the LAMBDA expression is
833 ;; the CAR of an executed form.
834 (ir1-convert start next result
`(%funcall
,@form
)))))
836 ;;; Convert anything that looks like a global function call.
837 (defun ir1-convert-global-functoid (start next result form fun
)
838 (declare (type ctran start next
) (type (or lvar null
) result
)
840 (when (eql fun
'declare
)
842 "~@<There is no function named ~S. ~
843 References to ~S in some contexts (like starts of blocks) are unevaluated ~
844 expressions, but here the expression is being evaluated, which invokes ~
845 undefined behaviour.~@:>" fun fun
))
846 ;; FIXME: Couldn't all the INFO calls here be converted into
847 ;; standard CL functions, like MACRO-FUNCTION or something? And what
848 ;; happens with lexically-defined (MACROLET) macros here, anyway?
849 (ecase (info :function
:kind fun
)
851 (ir1-convert start next result
852 (careful-expand-macro (info :function
:macro-function fun
)
854 (unless (policy *lexenv
* (zerop store-xref-data
))
855 (record-macroexpansion fun
(ctran-block start
) *current-path
*)))
857 (ir1-convert-srctran start next result
858 (find-free-fun fun
"shouldn't happen! (no-cmacro)")
861 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
862 ;;; errors which occur during the macroexpansion.
863 (defun careful-expand-macro (fun form
&optional cmacro
)
864 (flet (;; Return a string to use as a prefix in error reporting,
865 ;; telling something about which form caused the problem.
867 (let (;; We rely on the printer to abbreviate FORM.
871 "~@<~A of ~S. Use ~S to intercept.~%~:@>"
873 #-sb-xc-host
"Error during compiler-macroexpansion"
874 #+sb-xc-host
"Error during XC compiler-macroexpansion")
876 #-sb-xc-host
"during macroexpansion"
877 #+sb-xc-host
"during XC macroexpansion"))
879 '*break-on-signals
*))))
880 (handler-bind (;; KLUDGE: CMU CL in its wisdom (version 2.4.6 for Debian
881 ;; Linux, anyway) raises a CL:WARNING condition (not a
882 ;; CL:STYLE-WARNING) for undefined symbols when converting
883 ;; interpreted functions, causing COMPILE-FILE to think the
884 ;; file has a real problem, causing COMPILE-FILE to return
885 ;; FAILURE-P set (not just WARNINGS-P set). Since undefined
886 ;; symbol warnings are often harmless forward references,
887 ;; and since it'd be inordinately painful to try to
888 ;; eliminate all such forward references, these warnings
889 ;; are basically unavoidable. Thus, we need to coerce the
890 ;; system to work through them, and this code does so, by
891 ;; crudely suppressing all warnings in cross-compilation
892 ;; macroexpansion. -- WHN 19990412
893 #+(and cmu sb-xc-host
)
898 ~@<(KLUDGE: That was a non-STYLE WARNING. ~
899 Ordinarily that would cause compilation to ~
900 fail. However, since we're running under ~
901 CMU CL, and since CMU CL emits non-STYLE ~
902 warnings for safe, hard-to-fix things (e.g. ~
903 references to not-yet-defined functions) ~
904 we're going to have to ignore it and ~
905 proceed anyway. Hopefully we're not ~
906 ignoring anything horrible here..)~:@>~:>"
910 (bug "no MUFFLE-WARNING restart")))
915 ;; The spec is silent on what we should do. Signaling
916 ;; a full warning but declining to expand seems like
917 ;; a conservative and sane thing to do.
918 (compiler-warn "~@<~A~@:_ ~A~:>" (wherestring) c
)
919 (return-from careful-expand-macro form
))
921 (compiler-error "~@<~A~@:_ ~A~:>"
922 (wherestring) c
))))))
923 (funcall (valid-macroexpand-hook) fun form
*lexenv
*))))
925 ;;;; conversion utilities
927 ;;; Convert a bunch of forms, discarding all the values except the
928 ;;; last. If there aren't any forms, then translate a NIL.
929 (declaim (ftype (sfunction (ctran ctran
(or lvar null
) list
) (values))
930 ir1-convert-progn-body
))
931 (defun ir1-convert-progn-body (start next result body
)
933 (reference-constant start next result nil
)
934 (let ((this-start start
)
937 (let ((form (car forms
)))
939 (maybe-instrument-progn-like this-start forms form
))
940 (when (endp (cdr forms
))
941 (ir1-convert this-start next result form
)
943 (let ((this-ctran (make-ctran)))
944 (ir1-convert this-start this-ctran nil form
)
945 (setq this-start this-ctran
946 forms
(cdr forms
)))))))
952 ;;; Used as the CDR of the code coverage instrumentation records
953 ;;; (instead of NIL) to ensure that any well-behaving user code will
954 ;;; not have constants EQUAL to that record. This avoids problems with
955 ;;; the records getting coalesced with non-record conses, which then
956 ;;; get mutated when the instrumentation runs. Note that it's
957 ;;; important for multiple records for the same location to be
958 ;;; coalesced. -- JES, 2008-01-02
959 ;;; Use of #. mandates :COMPILE-TOPLEVEL for several Lisps
960 ;;; even though for us it's immediately accessible to EVAL.
961 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
962 (defconstant +code-coverage-unmarked
+ '%code-coverage-unmarked%
))
964 ;;; Check the policy for whether we should generate code coverage
965 ;;; instrumentation. If not, just return the original START
966 ;;; ctran. Otherwise insert code coverage instrumentation after
967 ;;; START, and return the new ctran.
968 (defun instrument-coverage (start mode form
)
969 ;; We don't actually use FORM for anything, it's just convenient to
970 ;; have around when debugging the instrumentation.
971 (declare (ignore form
))
972 (if (and (policy *lexenv
* (> store-coverage-data
0))
973 *code-coverage-records
*
974 *allow-instrumenting
*)
975 (let ((path (source-path-original-source *current-path
*)))
978 (if (member (ctran-block start
)
979 (gethash path
*code-coverage-blocks
*))
980 ;; If this source path has already been instrumented in
981 ;; this block, don't instrument it again.
984 ;; Get an interned record cons for the path. A cons
985 ;; with the same object identity must be used for
986 ;; each instrument for the same block.
987 (or (gethash path
*code-coverage-records
*)
988 (setf (gethash path
*code-coverage-records
*)
989 (cons path
+code-coverage-unmarked
+))))
991 (*allow-instrumenting
* nil
))
992 (push (ctran-block start
)
993 (gethash path
*code-coverage-blocks
*))
994 (let ((*allow-instrumenting
* nil
))
995 (ir1-convert start next nil
997 (declare (optimize speed
1000 (check-constant-modification 0)))
1001 ;; We're being naughty here, and
1002 ;; modifying constant data. That's ok,
1003 ;; we know what we're doing.
1004 (%rplacd
',store t
))))
1008 ;;; In contexts where we don't have a source location for FORM
1009 ;;; e.g. due to it not being a cons, but where we have a source
1010 ;;; location for the enclosing cons, use the latter source location if
1011 ;;; available. This works pretty well in practice, since many PROGNish
1012 ;;; macroexpansions will just directly splice a block of forms into
1013 ;;; some enclosing form with `(progn ,@body), thus retaining the
1014 ;;; EQness of the conses.
1015 (defun maybe-instrument-progn-like (start forms form
)
1016 (or (when (and *allow-instrumenting
*
1017 (not (get-source-path form
)))
1018 (let ((*current-path
* (get-source-path forms
)))
1019 (when *current-path
*
1020 (instrument-coverage start nil form
))))
1023 (defun record-code-coverage (info cc
)
1024 (setf (gethash info
*code-coverage-info
*) cc
))
1026 (defun clear-code-coverage ()
1027 (clrhash *code-coverage-info
*))
1029 (defun reset-code-coverage ()
1030 (maphash (lambda (info cc
)
1031 (declare (ignore info
))
1032 (dolist (cc-entry cc
)
1033 (setf (cdr cc-entry
) +code-coverage-unmarked
+)))
1034 *code-coverage-info
*))
1036 (defun code-coverage-record-marked (record)
1037 (aver (consp record
))
1039 ((#.
+code-coverage-unmarked
+) nil
)
1043 ;;;; converting combinations
1045 ;;; Does this form look like something that we should add single-stepping
1046 ;;; instrumentation for?
1047 (defun step-form-p (form)
1048 (flet ((step-symbol-p (symbol)
1049 (and (not (member (symbol-package symbol
)
1051 ;; KLUDGE: packages we're not interested in
1053 (mapcar #'find-package
'(sb!c sb
!int sb
!impl
1054 sb
!kernel sb
!pcl
)) t
)))
1055 ;; Consistent treatment of *FOO* vs (SYMBOL-VALUE '*FOO*):
1056 ;; we insert calls to SYMBOL-VALUE for most non-lexical
1057 ;; variable references in order to avoid them being elided
1058 ;; if the value is unused.
1059 (or (not (member symbol
'(symbol-value symbol-global-value
)))
1060 (not (constantp (second form
)))))))
1061 (and *allow-instrumenting
*
1062 (policy *lexenv
* (= insert-step-conditions
3))
1064 (symbolp (car form
))
1065 (step-symbol-p (car form
)))))
1067 ;;; Convert a function call where the function FUN is a LEAF. FORM is
1068 ;;; the source for the call. We return the COMBINATION node so that
1069 ;;; the caller can poke at it if it wants to.
1070 (declaim (ftype (sfunction (ctran ctran
(or lvar null
) list leaf
) combination
)
1071 ir1-convert-combination
))
1072 (defun ir1-convert-combination (start next result form fun
)
1073 (let ((ctran (make-ctran))
1074 (fun-lvar (make-lvar)))
1075 (ir1-convert start ctran fun-lvar
`(the (or function symbol
) ,fun
))
1077 (ir1-convert-combination-args fun-lvar ctran next result
1079 (when (step-form-p form
)
1080 ;; Store a string representation of the form in the
1081 ;; combination node. This will let the IR2 translator know
1082 ;; that we want stepper instrumentation for this node. The
1083 ;; string will be stored in the debug-info by DUMP-1-LOCATION.
1084 (setf (combination-step-info combination
)
1085 (let ((*print-pretty
* t
)
1087 (*print-readably
* nil
))
1088 (prin1-to-string form
))))
1091 ;;; Convert the arguments to a call and make the COMBINATION
1092 ;;; node. FUN-LVAR yields the function to call. ARGS is the list of
1093 ;;; arguments for the call, which defaults to the cdr of source. We
1094 ;;; return the COMBINATION node.
1095 (defun ir1-convert-combination-args (fun-lvar start next result args
)
1096 (declare (type ctran start next
)
1097 (type lvar fun-lvar
)
1098 (type (or lvar null
) result
)
1100 (let ((node (make-combination fun-lvar
)))
1101 (setf (lvar-dest fun-lvar
) node
)
1102 (collect ((arg-lvars))
1103 (let ((this-start start
)
1107 (maybe-instrument-progn-like this-start forms arg
))
1108 (setf forms
(cdr forms
))
1109 (let ((this-ctran (make-ctran))
1110 (this-lvar (make-lvar node
)))
1111 (ir1-convert this-start this-ctran this-lvar arg
)
1112 (setq this-start this-ctran
)
1113 (arg-lvars this-lvar
)))
1114 (link-node-to-previous-ctran node this-start
)
1115 (use-continuation node next result
)
1116 (setf (combination-args node
) (arg-lvars))))
1119 ;;; Convert a call to a global function. If not :NOTINLINE, then we do
1120 ;;; source transforms and try out any inline expansion. If there is no
1121 ;;; expansion, but is :INLINE, then give an efficiency note (unless a
1122 ;;; known function which will quite possibly be open-coded.) Next, we
1123 ;;; go to ok-combination conversion.
1124 (defun ir1-convert-srctran (start next result var form
)
1125 (declare (type ctran start next
) (type (or lvar null
) result
)
1126 (type global-var var
))
1127 (let ((name (leaf-source-name var
))
1128 (inlinep (when (defined-fun-p var
)
1129 (defined-fun-inlinep var
))))
1130 (if (eq inlinep
:notinline
)
1131 (ir1-convert-combination start next result form var
)
1132 (let* ((transform (info :function
:source-transform name
)))
1134 (multiple-value-bind (transformed pass
)
1135 (if (functionp transform
)
1136 (funcall transform form
*lexenv
*)
1138 (if (eq (cdr transform
) :predicate
)
1139 (and (singleton-p (cdr form
))
1142 ',(dd-name (car transform
))))
1143 (slot-access-transform
1144 (if (consp name
) :write
:read
)
1145 (cdr form
) transform
))))
1146 (values result
(null result
))))
1148 (ir1-convert-maybe-predicate start next result form var
))
1150 (unless (policy *lexenv
* (zerop store-xref-data
))
1151 (record-call name
(ctran-block start
) *current-path
*))
1152 (ir1-convert start next result transformed
))))
1153 (ir1-convert-maybe-predicate start next result form var
))))))
1155 ;;; KLUDGE: If we insert a synthetic IF for a function with the PREDICATE
1156 ;;; attribute, don't generate any branch coverage instrumentation for it.
1157 (defvar *instrument-if-for-code-coverage
* t
)
1159 ;;; If the function has the PREDICATE attribute, and the RESULT's DEST
1160 ;;; isn't an IF, then we convert (IF <form> T NIL), ensuring that a
1161 ;;; predicate always appears in a conditional context.
1163 ;;; If the function isn't a predicate, then we call
1164 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
1165 (defun ir1-convert-maybe-predicate (start next result form var
)
1166 (declare (type ctran start next
)
1167 (type (or lvar null
) result
)
1169 (type global-var var
))
1170 (let ((info (info :function
:info
(leaf-source-name var
))))
1172 (ir1-attributep (fun-info-attributes info
) predicate
)
1173 (not (if-p (and result
(lvar-dest result
)))))
1174 (let ((*instrument-if-for-code-coverage
* nil
))
1175 (ir1-convert start next result
`(if ,form t nil
)))
1176 (ir1-convert-combination-checking-type start next result form var
))))
1178 ;;; Actually really convert a global function call that we are allowed
1181 ;;; If we know the function type of the function, then we check the
1182 ;;; call for syntactic legality with respect to the declared function
1183 ;;; type. If it is impossible to determine whether the call is correct
1184 ;;; due to non-constant keywords, then we give up, marking the call as
1185 ;;; :FULL to inhibit further error messages. We return true when the
1188 ;;; If the call is legal, we also propagate type assertions from the
1189 ;;; function type to the arg and result lvars. We do this now so that
1190 ;;; IR1 optimize doesn't have to redundantly do the check later so
1191 ;;; that it can do the type propagation.
1192 (defun ir1-convert-combination-checking-type (start next result form var
)
1193 (declare (type ctran start next
) (type (or lvar null
) result
)
1196 (let* ((node (ir1-convert-combination start next result form var
))
1197 (fun-lvar (basic-combination-fun node
))
1198 (type (leaf-type var
)))
1199 (when (validate-call-type node type var t
)
1200 (setf (lvar-%derived-type fun-lvar
)
1201 (make-single-value-type type
))
1202 (setf (lvar-reoptimize fun-lvar
) nil
)))
1205 ;;; Convert a call to a local function, or if the function has already
1206 ;;; been LET converted, then throw FUNCTIONAL to
1207 ;;; LOCALL-ALREADY-LET-CONVERTED. The THROW should only happen when we
1208 ;;; are converting inline expansions for local functions during
1210 (defun ir1-convert-local-combination (start next result form functional
)
1211 (assure-functional-live-p functional
)
1212 (ir1-convert-combination start next result
1214 (maybe-reanalyze-functional functional
)))
1218 ;;; Given a list of LAMBDA-VARs and a variable name, return the
1219 ;;; LAMBDA-VAR for that name, or NIL if it isn't found. We return the
1220 ;;; *last* variable with that name, since LET* bindings may be
1221 ;;; duplicated, and declarations always apply to the last.
1222 (declaim (ftype (sfunction (list symbol
) (or lambda-var list
))
1224 (defun find-in-bindings (vars name
)
1228 (when (eq (leaf-source-name var
) name
)
1230 (let ((info (lambda-var-arg-info var
)))
1232 (let ((supplied-p (arg-info-supplied-p info
)))
1233 (when (and supplied-p
1234 (eq (leaf-source-name supplied-p
) name
))
1235 (setq found supplied-p
))))))
1236 ((and (consp var
) (eq (car var
) name
))
1237 (setf found
(cdr var
)))))
1240 ;;; Called by PROCESS-DECLS to deal with a variable type declaration.
1241 ;;; If a LAMBDA-VAR being bound, we intersect the type with the var's
1242 ;;; type, otherwise we add a type restriction on the var. If a symbol
1243 ;;; macro, we just wrap a THE around the expansion.
1244 (defun process-type-decl (decl res vars context
)
1245 (declare (type list decl vars
) (type lexenv res
))
1246 (let* ((type-specifier (first decl
))
1248 (when (typep type-specifier
'type-specifier
)
1249 (check-deprecated-type type-specifier
))
1250 (compiler-specifier-type type-specifier
))))
1251 (collect ((restr nil cons
)
1252 (new-vars nil cons
))
1253 (dolist (var-name (rest decl
))
1254 (unless (symbolp var-name
)
1255 (compiler-error "Variable name is not a symbol: ~S." var-name
))
1256 (unless (eq (info :variable
:kind var-name
) :unknown
)
1257 (program-assert-symbol-home-package-unlocked
1258 context var-name
"declaring the type of ~A"))
1259 (let* ((bound-var (find-in-bindings vars var-name
))
1261 (lexenv-find var-name vars
)
1262 (find-free-var var-name
))))
1266 ((process-var (var bound-var
)
1267 (let* ((old-type (or (lexenv-find var type-restrictions
)
1269 (int (if (or (fun-type-p type
)
1270 (fun-type-p old-type
))
1272 (type-approx-intersection2
1274 (cond ((eq int
*empty-type
*)
1275 (unless (policy *lexenv
* (= inhibit-warnings
3))
1279 "The type declarations ~
1280 ~/sb!impl:print-type/ and ~
1281 ~/sb!impl:print-type/ for ~
1284 (list old-type type var-name
))))
1286 (setf (leaf-type bound-var
) int
1287 (leaf-where-from bound-var
) :declared
))
1289 (restr (cons var int
)))))))
1290 (process-var var bound-var
)
1291 (awhen (and (lambda-var-p var
)
1292 (lambda-var-specvar var
))
1293 (process-var it nil
))))
1295 ;; FIXME: non-ANSI weirdness. [See lp#309122]
1296 (aver (eq (car var
) 'macro
))
1297 (new-vars `(,var-name .
(macro .
(the ,(first decl
)
1301 "~S is an alien variable, so its type can't be declared."
1304 (if (or (restr) (new-vars))
1305 (make-lexenv :default res
1306 :type-restrictions
(restr)
1310 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
1311 ;;; declarations for function variables. In addition to allowing
1312 ;;; declarations for functions being bound, we must also deal with
1313 ;;; declarations that constrain the type of lexically apparent
1315 (defun process-ftype-decl (type-specifier res names fvars context
)
1316 (declare (type list names fvars
)
1318 (let ((type (compiler-specifier-type type-specifier
)))
1319 (check-deprecated-type type-specifier
)
1320 (unless (csubtypep type
(specifier-type 'function
))
1321 (compiler-style-warn "ignoring declared FTYPE: ~S (not a function type)"
1323 (return-from process-ftype-decl res
))
1324 (collect ((res nil cons
))
1325 (dolist (name names
)
1326 (when (fboundp name
)
1327 (program-assert-symbol-home-package-unlocked
1328 context name
"declaring the ftype of ~A"))
1329 (let ((found (find name fvars
:key
#'leaf-source-name
:test
#'equal
)))
1332 (setf (leaf-type found
) type
)
1333 (assert-definition-type found type
1334 :unwinnage-fun
#'compiler-notify
1335 :where
"FTYPE declaration"))
1337 (res (cons (find-lexically-apparent-fun
1338 name
"in a function type declaration")
1341 (make-lexenv :default res
:type-restrictions
(res))
1344 ;;; Process a special declaration, returning a new LEXENV. A non-bound
1345 ;;; special declaration is instantiated by throwing a special variable
1346 ;;; into the variables if BINDING-FORM-P is NIL, or otherwise into
1347 ;;; *POST-BINDING-VARIABLE-LEXENV*.
1348 (defun process-special-decl (spec res vars binding-form-p context
)
1349 (declare (list spec vars
) (type lexenv res
))
1350 (collect ((new-venv nil cons
))
1351 (dolist (name (cdr spec
))
1352 ;; While CLHS seems to allow local SPECIAL declarations for constants,
1353 ;; whatever the semantics are supposed to be is not at all clear to me
1354 ;; -- since constants aren't allowed to be bound it should be a no-op as
1355 ;; no-one can observe the difference portably, but specials are allowed
1356 ;; to be bound... yet nowhere does it say that the special declaration
1357 ;; removes the constantness. Call it a spec bug and prohibit it. Same
1358 ;; for GLOBAL variables.
1359 (let ((kind (info :variable
:kind name
)))
1360 (unless (member kind
'(:special
:unknown
))
1362 "Can't declare ~(~A~) variable locally special: ~S"
1364 (program-assert-symbol-home-package-unlocked
1365 context name
"declaring ~A special")
1366 (let ((var (find-in-bindings vars name
)))
1369 (aver (eq (car var
) 'macro
))
1371 "~S is a symbol-macro and thus can't be declared special."
1374 (when (lambda-var-ignorep var
)
1375 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1376 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1377 (compiler-style-warn
1378 "The ignored variable ~S is being declared special."
1380 (setf (lambda-var-specvar var
)
1381 (specvar-for-binding name
)))
1383 (unless (or (assoc name
(new-venv) :test
#'eq
))
1384 (new-venv (cons name
(specvar-for-binding name
))))))))
1385 (cond (binding-form-p
1386 (setf *post-binding-variable-lexenv
*
1387 (append (new-venv) *post-binding-variable-lexenv
*))
1390 (make-lexenv :default res
:vars
(new-venv)))
1394 ;;; Return a DEFINED-FUN which copies a GLOBAL-VAR but for its INLINEP
1395 ;;; (and TYPE if notinline), plus type-restrictions from the lexenv.
1396 (defun make-new-inlinep (var inlinep local-type
)
1397 (declare (type global-var var
) (type inlinep inlinep
))
1398 (let* ((type (if (and (eq inlinep
:notinline
)
1399 (not (eq (leaf-where-from var
) :declared
)))
1400 (specifier-type 'function
)
1402 (res (make-defined-fun
1403 :%source-name
(leaf-source-name var
)
1404 :where-from
(leaf-where-from var
)
1405 :type
(if local-type
1406 (type-intersection local-type type
)
1409 (when (defined-fun-p var
)
1410 (setf (defined-fun-inline-expansion res
)
1411 (defined-fun-inline-expansion var
))
1412 (setf (defined-fun-functionals res
)
1413 (defined-fun-functionals var
)))
1414 ;; FIXME: Is this really right? Needs we not set the FUNCTIONAL
1415 ;; to the original global-var?
1418 ;;; Parse an inline/notinline declaration. If it's a local function we're
1419 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
1420 (defun process-inline-decl (spec res fvars
)
1421 (let ((sense (cdr (assoc (first spec
) *inlinep-translations
* :test
#'eq
)))
1423 (dolist (name (rest spec
))
1424 (let ((fvar (find name fvars
:key
#'leaf-source-name
:test
#'equal
)))
1426 (setf (functional-inlinep fvar
) sense
)
1427 (let ((found (find-lexically-apparent-fun
1428 name
"in an inline or notinline declaration")))
1431 (when (policy *lexenv
* (>= speed inhibit-warnings
))
1432 (compiler-notify "ignoring ~A declaration not at ~
1433 definition of local function:~% ~S"
1437 (cdr (assoc found
(lexenv-type-restrictions res
)))))
1438 (push (cons name
(make-new-inlinep found sense type
))
1441 (make-lexenv :default res
:funs new-fenv
)
1444 ;;; like FIND-IN-BINDINGS, but looks for #'FOO in the FVARS
1445 (defun find-in-bindings-or-fbindings (name vars fvars
)
1446 (declare (list vars fvars
))
1449 (find-in-bindings vars name
))
1450 ((cons (eql function
) (cons * null
))
1451 (find (cadr name
) fvars
:key
#'leaf-source-name
:test
#'equal
))
1453 (compiler-error "Malformed function or variable name ~S." name
))))
1455 ;;; Process an ignore/ignorable declaration, checking for various losing
1457 (defun process-ignore-decl (spec vars fvars lexenv
)
1458 (declare (list spec vars fvars
))
1459 (dolist (name (rest spec
))
1460 (let ((var (find-in-bindings-or-fbindings name vars fvars
)))
1463 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1464 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1465 ;; But, other Lisp hosts signal a full warning, so when building
1466 ;; the cross-compiler, compile it as #'WARN so that in a self-hosted
1467 ;; build we can at least crash in the same way,
1468 ;; until we resolve this question about how severe the warning is.
1469 (multiple-value-call #+sb-xc-host
#'warn
1470 #-sb-xc-host
#'compiler-style-warn
1471 "~A declaration for ~A: ~A"
1475 (case (info :variable
:kind name
)
1476 (:special
"a special variable")
1477 (:global
"a global lexical variable")
1478 (:alien
"a global alien variable")
1479 (t (if (assoc name
(lexenv-vars lexenv
))
1480 "a variable from outer scope"
1481 "an unknown variable")))
1484 (cond ((assoc (second name
) (lexenv-funs lexenv
)
1486 "a function from outer scope")
1487 ((info :function
:kind
(second name
))
1488 "a global function")
1490 "an unknown function"))
1492 ((and (consp var
) (eq (car var
) 'macro
))
1493 ;; Just ignore the IGNORE decl: we don't currently signal style-warnings
1494 ;; for unused symbol-macros, so there's no need to do anything.
1497 (setf (leaf-ever-used var
) t
))
1498 ((and (lambda-var-specvar var
) (eq (first spec
) 'ignore
))
1499 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1500 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1501 (compiler-style-warn "Declaring special variable ~S to be ~A"
1504 ((eq (first spec
) 'ignorable
)
1505 (setf (leaf-ever-used var
) t
))
1507 (setf (lambda-var-ignorep var
) t
)))))
1510 (defun process-extent-decl (names vars fvars kind
)
1513 (truly-dynamic-extent
1516 (when *stack-allocate-dynamic-extent
*
1521 (dolist (name names
)
1524 (let* ((bound-var (find-in-bindings vars name
))
1526 (lexenv-find name vars
)
1527 (maybe-find-free-var name
))))
1531 (if (and (leaf-extent var
) (neq extent
(leaf-extent var
)))
1532 (warn "Multiple incompatible extent declarations for ~S?" name
)
1533 (setf (leaf-extent var
) extent
))
1535 "Ignoring free ~S declaration: ~S" kind name
)))
1537 (compiler-error "~S on symbol-macro: ~S" kind name
))
1539 (compiler-error "~S on alien-variable: ~S" kind name
))
1541 (compiler-style-warn
1542 "Unbound variable declared ~S: ~S" kind name
)))))
1544 (eq (car name
) 'function
)
1546 (valid-function-name-p (cadr name
))
1547 (neq :indefinite extent
))
1548 (let* ((fname (cadr name
))
1549 (bound-fun (find fname fvars
1550 :key
#'leaf-source-name
1552 (fun (or bound-fun
(lexenv-find fname funs
))))
1556 #!+stack-allocatable-closures
1557 (setf (leaf-extent bound-fun
) extent
)
1558 #!-stack-allocatable-closures
1560 "Ignoring DYNAMIC-EXTENT declaration on function ~S ~
1561 (not supported on this platform)." fname
)
1563 "Ignoring free DYNAMIC-EXTENT declaration: ~S" name
)))
1565 (compiler-error "DYNAMIC-EXTENT on macro: ~S" name
))
1567 (compiler-style-warn
1568 "Unbound function declared DYNAMIC-EXTENT: ~S" name
)))))
1570 (compiler-error "~S on a weird thing: ~S" kind name
))))
1571 (when (policy *lexenv
* (= speed
3))
1572 (compiler-notify "Ignoring DYNAMIC-EXTENT declarations: ~S" names
)))))
1574 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
1575 ;;; go away, I think.
1576 ;;; Or just rename the declaration to SB-C:RESULT-TYPE so that it's not
1577 ;;; a symbol in the CL package, and then eliminate this switch.
1578 ;;; It's permissible to have implementation-specific declarations.
1579 (defvar *suppress-values-declaration
* nil
1580 "If true, processing of the VALUES declaration is inhibited.")
1582 ;;; Process a single declaration spec, augmenting the specified LEXENV
1583 ;;; RES. Return RES and result type. VARS and FVARS are as described
1585 (defun process-1-decl (raw-spec res vars fvars binding-form-p context
)
1586 (declare (type list raw-spec vars fvars
))
1587 (declare (type lexenv res
))
1588 (let ((spec (canonized-decl-spec raw-spec
))
1589 (optimize-qualities))
1590 ;; FIXME: we can end up with a chain of spurious parent lexenvs,
1591 ;; when logically the processing of decls should yield at most
1592 ;; two new lexenvs: one for the bindings and one for post-binding.
1593 ;; It's possible that there's a simple fix of re-linking the resulting
1594 ;; lexenv directly to *lexenv* as its parent.
1598 (process-type-decl (cdr spec
) res vars context
))
1600 (process-ignore-decl spec vars fvars res
)
1602 (special (process-special-decl spec res vars binding-form-p context
))
1605 (compiler-error "no type specified in FTYPE declaration: ~S" spec
))
1606 (process-ftype-decl (second spec
) res
(cddr spec
) fvars context
))
1607 ((inline notinline maybe-inline
)
1608 (process-inline-decl spec res fvars
))
1610 (multiple-value-bind (new-policy specified-qualities
)
1611 (process-optimize-decl spec
(lexenv-policy res
))
1612 (setq optimize-qualities specified-qualities
)
1613 (make-lexenv :default res
:policy new-policy
)))
1617 :handled-conditions
(process-muffle-conditions-decl
1618 spec
(lexenv-handled-conditions res
))))
1619 (unmuffle-conditions
1622 :handled-conditions
(process-unmuffle-conditions-decl
1623 spec
(lexenv-handled-conditions res
))))
1624 ((dynamic-extent truly-dynamic-extent indefinite-extent
)
1625 (process-extent-decl (cdr spec
) vars fvars
(first spec
))
1627 ((disable-package-locks enable-package-locks
)
1630 :disabled-package-locks
(process-package-lock-decl
1631 spec
(lexenv-disabled-package-locks res
))))
1632 ;; We may want to detect LAMBDA-LIST and VALUES decls here,
1633 ;; and report them as "Misplaced" rather than "Unrecognized".
1635 (unless (info :declaration
:recognized
(first spec
))
1636 (compiler-warn "unrecognized declaration ~S" raw-spec
))
1637 (let ((fn (info :declaration
:handler
(first spec
))))
1639 (funcall fn res spec vars fvars
)
1641 optimize-qualities
)))
1643 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1644 ;;; and FUNCTIONAL structures which are being bound. In addition to
1645 ;;; filling in slots in the leaf structures, we return a new LEXENV,
1646 ;;; which reflects pervasive special and function type declarations,
1647 ;;; (NOT)INLINE declarations and OPTIMIZE declarations, and type of
1648 ;;; VALUES declarations. If BINDING-FORM-P is true, the third return
1649 ;;; value is a list of VARs that should not apply to the lexenv of the
1650 ;;; initialization forms for the bindings, but should apply to the body.
1652 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1654 (defun process-decls (decls vars fvars
&key
1655 (lexenv *lexenv
*) (binding-form-p nil
) (context :compile
)
1656 (allow-lambda-list nil
))
1657 (declare (list decls vars fvars
))
1658 (let ((result-type *wild-type
*)
1659 (allow-values-decl allow-lambda-list
)
1661 (allow-explicit-check allow-lambda-list
)
1662 (lambda-list (if allow-lambda-list
:unspecified nil
))
1663 (optimize-qualities)
1664 (*post-binding-variable-lexenv
* nil
))
1665 (flet ((process-it (spec decl
)
1667 (compiler-error "malformed declaration specifier ~S in ~S"
1669 ((and (eq allow-lambda-list t
)
1670 (typep spec
'(cons (eql lambda-list
) (cons t null
))))
1671 (setq lambda-list
(cadr spec
) allow-lambda-list nil
))
1672 ((and allow-values-decl
1673 (typep spec
'(cons (eql values
)))
1674 (not *suppress-values-declaration
*))
1675 ;; Why do we allow more than one VALUES decl? I don't know.
1677 (values-type-intersection
1679 (compiler-values-specifier-type
1680 (let ((types (cdr spec
)))
1681 (if (singleton-p types
)
1683 `(values ,@types
)))))))
1684 ((and allow-explicit-check
1685 (typep spec
'(cons (eql explicit-check
))))
1686 ;; EXPLICIT-CHECK by itself specifies that all argument and
1687 ;; result types are checked by the function body.
1688 ;; Alternatively, a subset of arguments, and/or :RESULT,
1689 ;; can be specified to indicate that only a subset are
1690 ;; checked; in that case, the compiler asserts all others.
1691 (awhen (remove-if (lambda (x)
1694 :key
#'lambda-var-%source-name
)
1697 (compiler-error "explicit-check list ~S must refer to lambda vars"
1699 (setq explicit-check
(or (cdr spec
) t
)
1700 allow-explicit-check nil
)) ; at most one of this decl
1702 (multiple-value-bind (new-env new-qualities
)
1703 (process-1-decl spec lexenv vars fvars
1704 binding-form-p context
)
1705 (setq lexenv new-env
1707 (nconc new-qualities optimize-qualities
)))))))
1708 (dolist (decl decls
)
1709 (dolist (spec (rest decl
))
1710 (if (eq context
:compile
)
1711 (with-current-source-form (spec decl
) ; TODO this is a slight change to the previous code. make sure the behavior is identical
1712 (process-it spec decl
))
1713 ;; Kludge: EVAL calls this function to deal with LOCALLY.
1714 (process-it spec decl
)))))
1715 (warn-repeated-optimize-qualities (lexenv-policy lexenv
) optimize-qualities
)
1716 (values lexenv result-type
*post-binding-variable-lexenv
*
1717 lambda-list explicit-check
)))
1719 (defun %processing-decls
(decls vars fvars ctran lvar binding-form-p fun
)
1720 (multiple-value-bind (*lexenv
* result-type post-binding-lexenv
)
1721 (process-decls decls vars fvars
:binding-form-p binding-form-p
)
1722 (cond ((eq result-type
*wild-type
*)
1723 (funcall fun ctran lvar post-binding-lexenv
))
1725 (let ((value-ctran (make-ctran))
1726 (value-lvar (make-lvar)))
1727 (multiple-value-prog1
1728 (funcall fun value-ctran value-lvar post-binding-lexenv
)
1729 (let ((cast (make-cast value-lvar result-type
1730 (lexenv-policy *lexenv
*))))
1731 (link-node-to-previous-ctran cast value-ctran
)
1732 (setf (lvar-dest value-lvar
) cast
)
1733 (use-continuation cast ctran lvar
))))))))
1735 (defmacro processing-decls
((decls vars fvars ctran lvar
1736 &optional post-binding-lexenv
)
1738 (check-type ctran symbol
)
1739 (check-type lvar symbol
)
1740 (let ((post-binding-lexenv-p (not (null post-binding-lexenv
)))
1741 (post-binding-lexenv (or post-binding-lexenv
(sb!xc
:gensym
"LEXENV"))))
1742 `(%processing-decls
,decls
,vars
,fvars
,ctran
,lvar
1743 ,post-binding-lexenv-p
1744 (lambda (,ctran
,lvar
,post-binding-lexenv
)
1745 (declare (ignorable ,post-binding-lexenv
))
1748 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1749 ;;; declaration. If there is a global variable of that name, then
1750 ;;; check that it isn't a constant and return it. Otherwise, create an
1751 ;;; anonymous GLOBAL-VAR.
1752 (defun specvar-for-binding (name)
1753 (cond ((not (eq (info :variable
:where-from name
) :assumed
))
1754 (let ((found (find-free-var name
)))
1755 (when (heap-alien-info-p found
)
1757 "~S is an alien variable and so can't be declared special."
1759 (unless (global-var-p found
)
1761 "~S is a constant and so can't be declared special."
1765 (make-global-var :kind
:special
1767 :where-from
:declared
))))