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 simplify-source-path-form (form)
48 (let ((op (car form)))
49 ;; In the compiler functions can be directly represented
50 ;; by leaves. Having leaves in the source path is pretty
51 ;; hard on the poor user, however, so replace with the
52 ;; source-name when possible.
53 (if (and (leaf-p op) (leaf-has-source-name-p op))
54 (cons (leaf-source-name op) (cdr form))
58 (defun note-source-path (form &rest arguments)
59 (when (source-form-has-path-p form)
60 (setf (gethash form *source-paths*)
61 (apply #'list* 'original-source-start *current-form-number* arguments))))
63 ;;; *CURRENT-COMPONENT* is the COMPONENT structure which we link
64 ;;; blocks into as we generate them. This just serves to glue the
65 ;;; emitted blocks together until local call analysis and flow graph
66 ;;; canonicalization figure out what is really going on. We need to
67 ;;; keep track of all the blocks generated so that we can delete them
68 ;;; if they turn out to be unreachable.
70 ;;; FIXME: It's confusing having one variable named *CURRENT-COMPONENT*
71 ;;; and another named *COMPONENT-BEING-COMPILED*. (In CMU CL they
72 ;;; were called *CURRENT-COMPONENT* and *COMPILE-COMPONENT* respectively,
73 ;;; which was also confusing.)
74 (declaim (type (or component null) *current-component*))
75 (defvar *current-component*)
77 ;;; *CURRENT-PATH* is the source path of the form we are currently
78 ;;; translating. See NODE-SOURCE-PATH in the NODE structure.
79 (declaim (list *current-path*))
80 (defvar *current-path*)
82 (defvar *derive-function-types* nil
83 "Should the compiler assume that function types will never change,
84 so that it can use type information inferred from current definitions
85 to optimize code which uses those definitions? Setting this true
86 gives non-ANSI, early-CMU-CL behavior. It can be useful for improving
87 the efficiency of stable code.")
89 (defvar *fun-names-in-this-file* nil)
91 (defvar *post-binding-variable-lexenv* nil)
93 ;;;; namespace management utilities
95 (defun fun-lexically-notinline-p (name)
96 (let ((fun (lexenv-find name funs :test #'equal)))
97 ;; a declaration will trump a proclamation
98 (if (and fun (defined-fun-p fun))
99 (eq (defined-fun-inlinep fun) :notinline)
100 (eq (info :function :inlinep name) :notinline))))
102 ;; This will get redefined in PCL boot.
103 (declaim (notinline maybe-update-info-for-gf))
104 (defun maybe-update-info-for-gf (name)
105 (declare (ignore name))
108 (defun maybe-defined-here (name where)
109 (if (and (eq :defined where)
110 (member name *fun-names-in-this-file* :test #'equal))
114 ;;; Return a GLOBAL-VAR structure usable for referencing the global
116 (defun find-global-fun (name latep)
117 (unless (info :function :kind name)
118 (setf (info :function :kind name) :function)
119 (setf (info :function :where-from name) :assumed))
120 (let ((where (info :function :where-from name)))
121 (when (and (eq where :assumed)
122 ;; In the ordinary target Lisp, it's silly to report
123 ;; undefinedness when the function is defined in the
124 ;; running Lisp. But at cross-compile time, the current
125 ;; definedness of a function is irrelevant to the
126 ;; definedness at runtime, which is what matters.
127 #-sb-xc-host (not (fboundp name))
128 ;; LATEP is true when the user has indicated that
129 ;; late-late binding is desired by using eg. a quoted
130 ;; symbol -- in which case it makes little sense to
131 ;; complain about undefined functions.
133 (note-undefined-reference name :function))
134 (let ((ftype (info :function :type name))
135 (notinline (fun-lexically-notinline-p name)))
137 :kind :global-function
139 :type (if (or (eq where :declared)
142 *derive-function-types*))
144 (specifier-type 'function))
145 :defined-type (if (and (not latep) (not notinline))
146 (or (maybe-update-info-for-gf name) ftype)
147 (specifier-type 'function))
148 :where-from (if notinline
150 (maybe-defined-here name where))))))
152 ;;; Have some DEFINED-FUN-FUNCTIONALS of a *FREE-FUNS* entry become invalid?
155 ;;; This was added to fix bug 138 in SBCL. It is possible for a *FREE-FUNS*
156 ;;; entry to contain a DEFINED-FUN whose DEFINED-FUN-FUNCTIONAL object
157 ;;; contained IR1 stuff (NODEs, BLOCKs...) referring to an already compiled
158 ;;; (aka "dead") component. When this IR1 stuff was reused in a new component,
159 ;;; under further obscure circumstances it could be used by
160 ;;; WITH-IR1-ENVIRONMENT-FROM-NODE to generate a binding for
161 ;;; *CURRENT-COMPONENT*. At that point things got all confused, since IR1
162 ;;; conversion was sending code to a component which had already been compiled
163 ;;; and would never be compiled again.
165 ;;; Note: as of 1.0.24.41 this seems to happen only in XC, and the original
166 ;;; BUGS entry also makes it seem like this might not be an issue at all on
168 (defun clear-invalid-functionals (free-fun)
169 ;; There might be other reasons that *FREE-FUN* entries could
170 ;; become invalid, but the only one we've been bitten by so far
171 ;; (sbcl-0.pre7.118) is this one:
172 (when (defined-fun-p free-fun)
173 (setf (defined-fun-functionals free-fun)
174 (delete-if (lambda (functional)
175 (or (eq (functional-kind functional) :deleted)
176 (when (lambda-p functional)
178 ;; (The main reason for this first test is to bail
179 ;; out early in cases where the LAMBDA-COMPONENT
180 ;; call in the second test would fail because links
181 ;; it needs are uninitialized or invalid.)
183 ;; If the BIND node for this LAMBDA is null, then
184 ;; according to the slot comments, the LAMBDA has
185 ;; been deleted or its call has been deleted. In
186 ;; that case, it seems rather questionable to reuse
187 ;; it, and certainly it shouldn't be necessary to
188 ;; reuse it, so we cheerfully declare it invalid.
189 (not (lambda-bind functional))
190 ;; If this IR1 stuff belongs to a dead component,
191 ;; then we can't reuse it without getting into
192 ;; bizarre confusion.
193 (eq (component-info (lambda-component functional))
195 (defined-fun-functionals free-fun)))
198 ;;; If NAME already has a valid entry in *FREE-FUNS*, then return
199 ;;; the value. Otherwise, make a new GLOBAL-VAR using information from
200 ;;; the global environment and enter it in *FREE-FUNS*. If NAME
201 ;;; names a macro or special form, then we error out using the
202 ;;; supplied context which indicates what we were trying to do that
203 ;;; demanded a function.
204 (declaim (ftype (sfunction (t string) global-var) find-free-fun))
205 (defun find-free-fun (name context)
206 (or (let ((old-free-fun (gethash name *free-funs*)))
208 (clear-invalid-functionals old-free-fun)
210 (ecase (info :function :kind name)
211 ;; FIXME: The :MACRO and :SPECIAL-FORM cases could be merged.
213 (compiler-error "The macro name ~S was found ~A." name context))
215 (compiler-error "The special form name ~S was found ~A."
219 (check-fun-name name)
220 (note-if-setf-fun-and-macro name)
221 (let ((expansion (fun-name-inline-expansion name))
222 (inlinep (info :function :inlinep name)))
223 (setf (gethash name *free-funs*)
224 (if (or expansion inlinep)
225 (let ((where (info :function :where-from name)))
228 :inline-expansion expansion
230 :where-from (if (eq inlinep :notinline)
232 (maybe-defined-here name where))
233 :type (if (and (eq inlinep :notinline)
234 (neq where :declared))
235 (specifier-type 'function)
236 (info :function :type name))))
237 (find-global-fun name nil))))))))
239 ;;; Return the LEAF structure for the lexically apparent function
240 ;;; definition of NAME.
241 (declaim (ftype (sfunction (t string) leaf) find-lexically-apparent-fun))
242 (defun find-lexically-apparent-fun (name context)
243 (let ((var (lexenv-find name funs :test #'equal)))
246 (aver (and (consp var) (eq (car var) 'macro)))
247 (compiler-error "found macro name ~S ~A" name context))
250 (find-free-fun name context)))))
252 (defun maybe-find-free-var (name)
253 (gethash name *free-vars*))
255 ;;; Return the LEAF node for a global variable reference to NAME. If
256 ;;; NAME is already entered in *FREE-VARS*, then we just return the
257 ;;; corresponding value. Otherwise, we make a new leaf using
258 ;;; information from the global environment and enter it in
259 ;;; *FREE-VARS*. If the variable is unknown, then we emit a warning.
260 (declaim (ftype (sfunction (t) (or leaf cons heap-alien-info)) find-free-var))
261 (defun find-free-var (name)
262 (unless (symbolp name)
263 (compiler-error "Variable name is not a symbol: ~S." name))
264 (or (gethash name *free-vars*)
265 (let ((kind (info :variable :kind name))
266 (type (info :variable :type name))
267 (where-from (info :variable :where-from name)))
268 (when (eq kind :unknown)
269 (note-undefined-reference name :variable))
270 (setf (gethash name *free-vars*)
273 (info :variable :alien-info name))
274 ;; FIXME: The return value in this case should really be
275 ;; of type SB!C::LEAF. I don't feel too badly about it,
276 ;; because the MACRO idiom is scattered throughout this
277 ;; file, but it should be cleaned up so we're not
278 ;; throwing random conses around. --njf 2002-03-23
280 (let ((expansion (info :variable :macro-expansion name))
281 (type (type-specifier (info :variable :type name))))
282 `(macro . (the ,type ,expansion))))
284 (let ((value (symbol-value name)))
285 ;; Override the values of standard symbols in XC,
286 ;; since we can't redefine them.
288 (when (eql (find-symbol (symbol-name name) :cl) name)
289 (multiple-value-bind (xc-value foundp)
290 (info :variable :xc-constant-value name)
292 (setf value xc-value))
293 ((not (eq value name))
295 "Using cross-compilation host's definition of ~S: ~A~%"
296 name (symbol-value name))))))
297 (find-constant value name)))
299 (make-global-var :kind kind
302 :where-from where-from)))))))
304 ;;; Grovel over CONSTANT checking for any sub-parts that need to be
305 ;;; processed with MAKE-LOAD-FORM. We have to be careful, because
306 ;;; CONSTANT might be circular. We also check that the constant (and
307 ;;; any subparts) are dumpable at all.
308 (defun maybe-emit-make-load-forms (constant &optional (name nil namep))
309 (let ((xset (alloc-xset)))
310 (labels ((trivialp (value)
313 #-sb-xc-host unboxed-array
314 #+sb-xc-host (simple-array (unsigned-byte 8) (*))
320 ;; Unless VALUE is an object which which obviously
321 ;; can't contain other objects
322 (unless (trivialp value)
323 (if (xset-member-p value xset)
324 (return-from grovel nil)
325 (add-to-xset value xset))
329 (grovel (cdr value)))
331 (dotimes (i (length value))
332 (grovel (svref value i))))
334 (dotimes (i (length value))
335 (grovel (aref value i))))
337 ;; Even though the (ARRAY T) branch does the exact
338 ;; same thing as this branch we do this separately
339 ;; so that the compiler can use faster versions of
340 ;; array-total-size and row-major-aref.
341 (dotimes (i (array-total-size value))
342 (grovel (row-major-aref value i))))
344 (dotimes (i (array-total-size value))
345 (grovel (row-major-aref value i))))
346 (#+sb-xc-host structure!object
347 #-sb-xc-host instance
348 ;; In the target SBCL, we can dump any instance, but
349 ;; in the cross-compilation host, %INSTANCE-FOO
350 ;; functions don't work on general instances, only on
351 ;; STRUCTURE!OBJECTs.
353 ;; FIXME: What about funcallable instances with
354 ;; user-defined MAKE-LOAD-FORM methods?
355 (when (emit-make-load-form value)
356 (dotimes (i (- (%instance-length value)
358 #-sb-xc-host (layout-n-untagged-slots
359 (%instance-ref value 0))))
360 (grovel (%instance-ref value i)))))
363 "Objects of type ~S can't be dumped into fasl files."
364 (type-of value)))))))
365 ;; Dump all non-trivial named constants using the name.
366 (if (and namep (not (typep constant '(or symbol character
367 ;; FIXME: Cold init breaks if we
368 ;; try to reference FP constants
371 #-sb-xc-host fixnum))))
372 (emit-make-load-form constant name)
376 ;;;; some flow-graph hacking utilities
378 ;;; This function sets up the back link between the node and the
379 ;;; ctran which continues at it.
380 (defun link-node-to-previous-ctran (node ctran)
381 (declare (type node node) (type ctran ctran))
382 (aver (not (ctran-next ctran)))
383 (setf (ctran-next ctran) node)
384 (setf (node-prev node) ctran))
386 ;;; This function is used to set the ctran for a node, and thus
387 ;;; determine what is evaluated next. If the ctran has no block, then
388 ;;; we make it be in the block that the node is in. If the ctran heads
389 ;;; its block, we end our block and link it to that block.
390 #!-sb-fluid (declaim (inline use-ctran))
391 (defun use-ctran (node ctran)
392 (declare (type node node) (type ctran ctran))
393 (if (eq (ctran-kind ctran) :unused)
394 (let ((node-block (ctran-block (node-prev node))))
395 (setf (ctran-block ctran) node-block)
396 (setf (ctran-kind ctran) :inside-block)
397 (setf (ctran-use ctran) node)
398 (setf (node-next node) ctran))
399 (%use-ctran node ctran)))
400 (defun %use-ctran (node ctran)
401 (declare (type node node) (type ctran ctran) (inline member))
402 (let ((block (ctran-block ctran))
403 (node-block (ctran-block (node-prev node))))
404 (aver (eq (ctran-kind ctran) :block-start))
405 (when (block-last node-block)
406 (error "~S has already ended." node-block))
407 (setf (block-last node-block) node)
408 (when (block-succ node-block)
409 (error "~S already has successors." node-block))
410 (setf (block-succ node-block) (list block))
411 (when (memq node-block (block-pred block))
412 (error "~S is already a predecessor of ~S." node-block block))
413 (push node-block (block-pred block))))
415 ;;; Insert NEW before OLD in the flow-graph.
416 (defun insert-node-before (old new)
417 (let ((prev (node-prev old))
419 (ensure-block-start prev)
420 (setf (ctran-next prev) nil)
421 (link-node-to-previous-ctran new prev)
423 (link-node-to-previous-ctran old temp))
426 ;;; This function is used to set the ctran for a node, and thus
427 ;;; determine what receives the value.
428 (defun use-lvar (node lvar)
429 (declare (type valued-node node) (type (or lvar null) lvar))
430 (aver (not (node-lvar node)))
432 (setf (node-lvar node) lvar)
433 (cond ((null (lvar-uses lvar))
434 (setf (lvar-uses lvar) node))
435 ((listp (lvar-uses lvar))
436 (aver (not (memq node (lvar-uses lvar))))
437 (push node (lvar-uses lvar)))
439 (aver (neq node (lvar-uses lvar)))
440 (setf (lvar-uses lvar) (list node (lvar-uses lvar)))))
441 (reoptimize-lvar lvar)))
443 #!-sb-fluid(declaim (inline use-continuation))
444 (defun use-continuation (node ctran lvar)
445 (use-ctran node ctran)
446 (use-lvar node lvar))
448 ;;;; exported functions
450 ;;; This function takes a form and the top level form number for that
451 ;;; form, and returns a lambda representing the translation of that
452 ;;; form in the current global environment. The returned lambda is a
453 ;;; top level lambda that can be called to cause evaluation of the
454 ;;; forms. This lambda is in the initial component. If FOR-VALUE is T,
455 ;;; then the value of the form is returned from the function,
456 ;;; otherwise NIL is returned.
458 ;;; This function may have arbitrary effects on the global environment
459 ;;; due to processing of EVAL-WHENs. All syntax error checking is
460 ;;; done, with erroneous forms being replaced by a proxy which signals
461 ;;; an error if it is evaluated. Warnings about possibly inconsistent
462 ;;; or illegal changes to the global environment will also be given.
464 ;;; We make the initial component and convert the form in a PROGN (and
465 ;;; an optional NIL tacked on the end.) We then return the lambda. We
466 ;;; bind all of our state variables here, rather than relying on the
467 ;;; global value (if any) so that IR1 conversion will be reentrant.
468 ;;; This is necessary for EVAL-WHEN processing, etc.
470 ;;; The hashtables used to hold global namespace info must be
471 ;;; reallocated elsewhere. Note also that *LEXENV* is not bound, so
472 ;;; that local macro definitions can be introduced by enclosing code.
473 (defun ir1-toplevel (form path for-value &optional (allow-instrumenting t))
474 (declare (list path))
475 (let* ((*current-path* path)
476 (component (make-empty-component))
477 (*current-component* component)
478 (*allow-instrumenting* allow-instrumenting))
479 (setf (component-name component) 'initial-component)
480 (setf (component-kind component) :initial)
481 (let* ((forms (if for-value `(,form) `(,form nil)))
482 (res (ir1-convert-lambda-body
484 :debug-name (debug-name 'top-level-form #+sb-xc-host nil #-sb-xc-host form))))
485 (setf (functional-entry-fun res) res
486 (functional-arg-documentation res) ()
487 (functional-kind res) :toplevel)
490 ;;; This function is called on freshly read forms to record the
491 ;;; initial location of each form (and subform.) Form is the form to
492 ;;; find the paths in, and TLF-NUM is the top level form number of the
493 ;;; truly top level form.
495 ;;; This gets a bit interesting when the source code is circular. This
496 ;;; can (reasonably?) happen in the case of circular list constants.
497 (defun find-source-paths (form tlf-num)
498 (declare (type index tlf-num))
499 (let ((*current-form-number* 0))
500 (sub-find-source-paths form (list tlf-num)))
502 (defun sub-find-source-paths (form path)
503 (unless (get-source-path form)
504 (note-source-path form path)
505 (incf *current-form-number*)
509 (declare (fixnum pos))
512 (when (atom subform) (return))
513 (let ((fm (car subform)))
515 ;; If it's a cons, recurse.
516 (sub-find-source-paths fm (cons pos path)))
518 ;; Don't look into quoted constants.
521 ;; Otherwise store the containing form. It's not
522 ;; perfect, but better than nothing.
523 (note-source-path subform pos path)))
525 (setq subform (cdr subform))
526 (when (eq subform trail) (return)))))
530 (setq trail (cdr trail)))))))
532 ;;;; IR1-CONVERT, macroexpansion and special form dispatching
534 (declaim (ftype (sfunction (ctran ctran (or lvar null) t) (values))
536 (macrolet (;; Bind *COMPILER-ERROR-BAILOUT* to a function that throws
537 ;; out of the body and converts a condition signalling form
538 ;; instead. The source form is converted to a string since it
539 ;; may contain arbitrary non-externalizable objects.
540 (ir1-error-bailout ((start next result form) &body body)
541 (with-unique-names (skip condition)
543 (let ((,condition (catch 'ir1-error-abort
544 (let ((*compiler-error-bailout*
545 (lambda (&optional e)
546 (throw 'ir1-error-abort e))))
548 (return-from ,skip nil)))))
549 (ir1-convert ,start ,next ,result
550 (make-compiler-error-form ,condition
553 ;; Translate FORM into IR1. The code is inserted as the NEXT of the
554 ;; CTRAN START. RESULT is the LVAR which receives the value of the
555 ;; FORM to be translated. The translators call this function
556 ;; recursively to translate their subnodes.
558 ;; As a special hack to make life easier in the compiler, a LEAF
559 ;; IR1-converts into a reference to that LEAF structure. This allows
560 ;; the creation using backquote of forms that contain leaf
561 ;; references, without having to introduce dummy names into the
563 (defun ir1-convert (start next result form)
564 (ir1-error-bailout (start next result form)
565 (let* ((*current-path* (or (get-source-path form)
566 (cons (simplify-source-path-form form)
568 (start (instrument-coverage start nil form)))
570 (cond ((and (symbolp form) (not (keywordp form)))
571 (ir1-convert-var start next result form))
573 (reference-leaf start next result form))
575 (reference-constant start next result form))))
577 (ir1-convert-functoid start next result form)))))
580 ;; Generate a reference to a manifest constant, creating a new leaf
582 (defun reference-constant (start next result value)
583 (declare (type ctran start next)
584 (type (or lvar null) result))
585 (ir1-error-bailout (start next result value)
586 (let* ((leaf (find-constant value))
587 (res (make-ref leaf)))
588 (push res (leaf-refs leaf))
589 (link-node-to-previous-ctran res start)
590 (use-continuation res next result)))
593 ;;; Add FUNCTIONAL to the COMPONENT-REANALYZE-FUNCTIONALS, unless it's
594 ;;; some trivial type for which reanalysis is a trivial no-op, or
595 ;;; unless it doesn't belong in this component at all.
597 ;;; FUNCTIONAL is returned.
598 (defun maybe-reanalyze-functional (functional)
600 (aver (not (eql (functional-kind functional) :deleted))) ; bug 148
601 (aver-live-component *current-component*)
603 ;; When FUNCTIONAL is of a type for which reanalysis isn't a trivial
605 (when (typep functional '(or optional-dispatch clambda))
607 ;; When FUNCTIONAL knows its component
608 (when (lambda-p functional)
609 (aver (eql (lambda-component functional) *current-component*)))
612 (component-reanalyze-functionals *current-component*)))
616 ;;; Generate a REF node for LEAF, frobbing the LEAF structure as
617 ;;; needed. If LEAF represents a defined function which has already
618 ;;; been converted, and is not :NOTINLINE, then reference the
619 ;;; functional instead.
620 (defun reference-leaf (start next result leaf &optional (name '.anonymous.))
621 (declare (type ctran start next) (type (or lvar null) result) (type leaf leaf))
622 (assure-leaf-live-p leaf)
623 (let* ((type (lexenv-find leaf type-restrictions))
624 (leaf (or (and (defined-fun-p leaf)
625 (not (eq (defined-fun-inlinep leaf)
627 (let ((functional (defined-fun-functional leaf)))
628 (when (and functional (not (functional-kind functional)))
629 (maybe-reanalyze-functional functional))))
630 (when (and (lambda-p leaf)
631 (memq (functional-kind leaf)
633 (maybe-reanalyze-functional leaf))
635 (ref (make-ref leaf name)))
636 (push ref (leaf-refs leaf))
637 (setf (leaf-ever-used leaf) t)
638 (link-node-to-previous-ctran ref start)
639 (cond (type (let* ((ref-ctran (make-ctran))
640 (ref-lvar (make-lvar))
641 (cast (make-cast ref-lvar
642 (make-single-value-type type)
643 (lexenv-policy *lexenv*))))
644 (setf (lvar-dest ref-lvar) cast)
645 (use-continuation ref ref-ctran ref-lvar)
646 (link-node-to-previous-ctran cast ref-ctran)
647 (use-continuation cast next result)))
648 (t (use-continuation ref next result)))))
650 ;;; Convert a reference to a symbolic constant or variable. If the
651 ;;; symbol is entered in the LEXENV-VARS we use that definition,
652 ;;; otherwise we find the current global definition. This is also
653 ;;; where we pick off symbol macro and alien variable references.
654 (defun ir1-convert-var (start next result name)
655 (declare (type ctran start next) (type (or lvar null) result) (symbol name))
656 (let ((var (or (lexenv-find name vars) (find-free-var name))))
657 (if (and (global-var-p var) (not result))
658 ;; KLUDGE: If the reference is dead, convert using SYMBOL-VALUE
659 ;; which is not flushable, so that unbound dead variables signal
660 ;; an error (bug 412).
661 (ir1-convert start next result
662 (if (eq (global-var-kind var) :global)
663 `(symbol-global-value ',name)
664 `(symbol-value ',name)))
667 (when (lambda-var-p var)
668 (let ((home (ctran-home-lambda-or-null start)))
670 (sset-adjoin var (lambda-calls-or-closes home))))
671 (when (lambda-var-ignorep var)
672 ;; (ANSI's specification for the IGNORE declaration requires
673 ;; that this be a STYLE-WARNING, not a full WARNING.)
675 (compiler-style-warn "reading an ignored variable: ~S" name)
676 ;; there's no need for us to accept ANSI's lameness when
677 ;; processing our own code, though.
679 (warn "reading an ignored variable: ~S" name)))
680 (reference-leaf start next result var name))
682 (aver (eq (car var) 'macro))
683 ;; FIXME: [Free] type declarations. -- APD, 2002-01-26
684 (ir1-convert start next result (cdr var)))
686 (ir1-convert start next result `(%heap-alien ',var))))))
689 ;;; Find a compiler-macro for a form, taking FUNCALL into account.
690 (defun find-compiler-macro (opname form)
691 (if (eq opname 'funcall)
692 (let ((fun-form (cadr form)))
693 (cond ((and (consp fun-form) (eq 'function (car fun-form)))
694 (let ((real-fun (cadr fun-form)))
695 (if (legal-fun-name-p real-fun)
696 (values (sb!xc:compiler-macro-function real-fun *lexenv*)
699 ((sb!xc:constantp fun-form *lexenv*)
700 (let ((fun (constant-form-value fun-form *lexenv*)))
701 (if (legal-fun-name-p fun)
702 ;; CLHS tells us that local functions must shadow
703 ;; compiler-macro-functions, but since the call is
704 ;; through a name, we are obviously interested
705 ;; in the global function.
706 (values (sb!xc:compiler-macro-function fun nil) fun)
710 (if (legal-fun-name-p opname)
711 (values (sb!xc:compiler-macro-function opname *lexenv*) opname)
714 ;;; Picks of special forms and compiler-macro expansions, and hands
715 ;;; the rest to IR1-CONVERT-COMMON-FUNCTOID
716 (defun ir1-convert-functoid (start next result form)
717 (let* ((op (car form))
718 (translator (and (symbolp op) (info :function :ir1-convert op))))
720 (when (sb!xc:compiler-macro-function op *lexenv*)
721 (compiler-warn "ignoring compiler macro for special form"))
722 (funcall translator start next result form))
724 (multiple-value-bind (cmacro-fun cmacro-fun-name)
725 (find-compiler-macro op form)
727 ;; CLHS 3.2.2.1.3 specifies that NOTINLINE
728 ;; suppresses compiler-macros.
729 (not (fun-lexically-notinline-p cmacro-fun-name)))
730 (let ((res (careful-expand-macro cmacro-fun form t)))
732 (ir1-convert-common-functoid start next result form op))
734 (unless (policy *lexenv* (zerop store-xref-data))
735 (record-call cmacro-fun-name (ctran-block start) *current-path*))
736 (ir1-convert start next result res))))
737 (ir1-convert-common-functoid start next result form op)))))))
739 ;;; Handles the "common" cases: any other forms except special forms
740 ;;; and compiler-macros.
741 (defun ir1-convert-common-functoid (start next result form op)
742 (cond ((or (symbolp op) (leaf-p op))
743 (let ((lexical-def (if (leaf-p op) op (lexenv-find op funs))))
744 (typecase lexical-def
746 (ir1-convert-global-functoid start next result form op))
748 (ir1-convert-local-combination start next result form
751 (ir1-convert-srctran start next result lexical-def form))
753 (aver (and (consp lexical-def) (eq (car lexical-def) 'macro)))
754 (ir1-convert start next result
755 (careful-expand-macro (cdr lexical-def) form))))))
756 ((or (atom op) (not (eq (car op) 'lambda)))
757 (compiler-error "illegal function call"))
759 ;; implicitly (LAMBDA ..) because the LAMBDA expression is
760 ;; the CAR of an executed form.
761 (ir1-convert-combination
762 start next result form
763 (ir1-convert-lambda op
764 :debug-name (debug-name 'inline-lambda op))))))
766 ;;; Convert anything that looks like a global function call.
767 (defun ir1-convert-global-functoid (start next result form fun)
768 (declare (type ctran start next) (type (or lvar null) result)
770 ;; FIXME: Couldn't all the INFO calls here be converted into
771 ;; standard CL functions, like MACRO-FUNCTION or something? And what
772 ;; happens with lexically-defined (MACROLET) macros here, anyway?
773 (ecase (info :function :kind fun)
775 (ir1-convert start next result
776 (careful-expand-macro (info :function :macro-function fun)
778 (unless (policy *lexenv* (zerop store-xref-data))
779 (record-macroexpansion fun (ctran-block start) *current-path*)))
781 (ir1-convert-srctran start next result
782 (find-free-fun fun "shouldn't happen! (no-cmacro)")
785 (defun muffle-warning-or-die ()
787 (bug "no MUFFLE-WARNING restart"))
789 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
790 ;;; errors which occur during the macroexpansion.
791 (defun careful-expand-macro (fun form &optional cmacro)
792 (flet (;; Return a string to use as a prefix in error reporting,
793 ;; telling something about which form caused the problem.
795 (let (;; We rely on the printer to abbreviate FORM.
800 #-sb-xc-host "~@<~;during ~A of ~S. Use ~S to intercept:~%~:@>"
801 ;; longer message to avoid ambiguity "Was it the xc host
802 ;; or the cross-compiler which encountered the problem?"
803 #+sb-xc-host "~@<~;during cross-compiler ~A of ~S. Use ~S to intercept:~%~:@>"
804 (if cmacro "compiler-macroexpansion" "macroexpansion")
806 '*break-on-signals*))))
807 (handler-bind (;; KLUDGE: CMU CL in its wisdom (version 2.4.6 for Debian
808 ;; Linux, anyway) raises a CL:WARNING condition (not a
809 ;; CL:STYLE-WARNING) for undefined symbols when converting
810 ;; interpreted functions, causing COMPILE-FILE to think the
811 ;; file has a real problem, causing COMPILE-FILE to return
812 ;; FAILURE-P set (not just WARNINGS-P set). Since undefined
813 ;; symbol warnings are often harmless forward references,
814 ;; and since it'd be inordinately painful to try to
815 ;; eliminate all such forward references, these warnings
816 ;; are basically unavoidable. Thus, we need to coerce the
817 ;; system to work through them, and this code does so, by
818 ;; crudely suppressing all warnings in cross-compilation
819 ;; macroexpansion. -- WHN 19990412
820 #+(and cmu sb-xc-host)
825 ~@<(KLUDGE: That was a non-STYLE WARNING. ~
826 Ordinarily that would cause compilation to ~
827 fail. However, since we're running under ~
828 CMU CL, and since CMU CL emits non-STYLE ~
829 warnings for safe, hard-to-fix things (e.g. ~
830 references to not-yet-defined functions) ~
831 we're going to have to ignore it and ~
832 proceed anyway. Hopefully we're not ~
833 ignoring anything horrible here..)~:@>~:>"
836 (muffle-warning-or-die)))
838 (compiler-error "~@<~A~@:_ ~A~:>"
840 (funcall sb!xc:*macroexpand-hook* fun form *lexenv*))))
842 ;;;; conversion utilities
844 ;;; Convert a bunch of forms, discarding all the values except the
845 ;;; last. If there aren't any forms, then translate a NIL.
846 (declaim (ftype (sfunction (ctran ctran (or lvar null) list) (values))
847 ir1-convert-progn-body))
848 (defun ir1-convert-progn-body (start next result body)
850 (reference-constant start next result nil)
851 (let ((this-start start)
854 (let ((form (car forms)))
856 (maybe-instrument-progn-like this-start forms form))
857 (when (endp (cdr forms))
858 (ir1-convert this-start next result form)
860 (let ((this-ctran (make-ctran)))
861 (ir1-convert this-start this-ctran nil form)
862 (setq this-start this-ctran
863 forms (cdr forms)))))))
869 ;;; Check the policy for whether we should generate code coverage
870 ;;; instrumentation. If not, just return the original START
871 ;;; ctran. Otherwise insert code coverage instrumentation after
872 ;;; START, and return the new ctran.
873 (defun instrument-coverage (start mode form)
874 ;; We don't actually use FORM for anything, it's just convenient to
875 ;; have around when debugging the instrumentation.
876 (declare (ignore form))
877 (if (and (policy *lexenv* (> store-coverage-data 0))
878 *code-coverage-records*
879 *allow-instrumenting*)
880 (let ((path (source-path-original-source *current-path*)))
883 (if (member (ctran-block start)
884 (gethash path *code-coverage-blocks*))
885 ;; If this source path has already been instrumented in
886 ;; this block, don't instrument it again.
889 ;; Get an interned record cons for the path. A cons
890 ;; with the same object identity must be used for
891 ;; each instrument for the same block.
892 (or (gethash path *code-coverage-records*)
893 (setf (gethash path *code-coverage-records*)
894 (cons path +code-coverage-unmarked+))))
896 (*allow-instrumenting* nil))
897 (push (ctran-block start)
898 (gethash path *code-coverage-blocks*))
899 (let ((*allow-instrumenting* nil))
900 (ir1-convert start next nil
902 (declare (optimize speed
905 (check-constant-modification 0)))
906 ;; We're being naughty here, and
907 ;; modifying constant data. That's ok,
908 ;; we know what we're doing.
909 (%rplacd ',store t))))
913 ;;; In contexts where we don't have a source location for FORM
914 ;;; e.g. due to it not being a cons, but where we have a source
915 ;;; location for the enclosing cons, use the latter source location if
916 ;;; available. This works pretty well in practice, since many PROGNish
917 ;;; macroexpansions will just directly splice a block of forms into
918 ;;; some enclosing form with `(progn ,@body), thus retaining the
919 ;;; EQness of the conses.
920 (defun maybe-instrument-progn-like (start forms form)
921 (or (when (and *allow-instrumenting*
922 (not (get-source-path form)))
923 (let ((*current-path* (get-source-path forms)))
925 (instrument-coverage start nil form))))
928 (defun record-code-coverage (info cc)
929 (setf (gethash info *code-coverage-info*) cc))
931 (defun clear-code-coverage ()
932 (clrhash *code-coverage-info*))
934 (defun reset-code-coverage ()
935 (maphash (lambda (info cc)
936 (declare (ignore info))
937 (dolist (cc-entry cc)
938 (setf (cdr cc-entry) +code-coverage-unmarked+)))
939 *code-coverage-info*))
941 (defun code-coverage-record-marked (record)
942 (aver (consp record))
944 ((#.+code-coverage-unmarked+) nil)
948 ;;;; converting combinations
950 ;;; Does this form look like something that we should add single-stepping
951 ;;; instrumentation for?
952 (defun step-form-p (form)
953 (flet ((step-symbol-p (symbol)
954 (not (member (symbol-package symbol)
956 ;; KLUDGE: packages we're not interested in
958 (mapcar #'find-package '(sb!c sb!int sb!impl
959 sb!kernel sb!pcl)))))))
960 (and *allow-instrumenting*
961 (policy *lexenv* (= insert-step-conditions 3))
964 (step-symbol-p (car form)))))
966 ;;; Convert a function call where the function FUN is a LEAF. FORM is
967 ;;; the source for the call. We return the COMBINATION node so that
968 ;;; the caller can poke at it if it wants to.
969 (declaim (ftype (sfunction (ctran ctran (or lvar null) list leaf) combination)
970 ir1-convert-combination))
971 (defun ir1-convert-combination (start next result form fun)
972 (let ((ctran (make-ctran))
973 (fun-lvar (make-lvar)))
974 (ir1-convert start ctran fun-lvar `(the (or function symbol) ,fun))
976 (ir1-convert-combination-args fun-lvar ctran next result
978 (when (step-form-p form)
979 ;; Store a string representation of the form in the
980 ;; combination node. This will let the IR2 translator know
981 ;; that we want stepper instrumentation for this node. The
982 ;; string will be stored in the debug-info by DUMP-1-LOCATION.
983 (setf (combination-step-info combination)
984 (let ((*print-pretty* t)
986 (*print-readably* nil))
987 (prin1-to-string form))))
990 ;;; Convert the arguments to a call and make the COMBINATION
991 ;;; node. FUN-LVAR yields the function to call. ARGS is the list of
992 ;;; arguments for the call, which defaults to the cdr of source. We
993 ;;; return the COMBINATION node.
994 (defun ir1-convert-combination-args (fun-lvar start next result args)
995 (declare (type ctran start next)
997 (type (or lvar null) result)
999 (let ((node (make-combination fun-lvar)))
1000 (setf (lvar-dest fun-lvar) node)
1001 (collect ((arg-lvars))
1002 (let ((this-start start)
1006 (maybe-instrument-progn-like this-start forms arg))
1007 (setf forms (cdr forms))
1008 (let ((this-ctran (make-ctran))
1009 (this-lvar (make-lvar node)))
1010 (ir1-convert this-start this-ctran this-lvar arg)
1011 (setq this-start this-ctran)
1012 (arg-lvars this-lvar)))
1013 (link-node-to-previous-ctran node this-start)
1014 (use-continuation node next result)
1015 (setf (combination-args node) (arg-lvars))))
1018 ;;; Convert a call to a global function. If not :NOTINLINE, then we do
1019 ;;; source transforms and try out any inline expansion. If there is no
1020 ;;; expansion, but is :INLINE, then give an efficiency note (unless a
1021 ;;; known function which will quite possibly be open-coded.) Next, we
1022 ;;; go to ok-combination conversion.
1023 (defun ir1-convert-srctran (start next result var form)
1024 (declare (type ctran start next) (type (or lvar null) result)
1025 (type global-var var))
1026 (let ((inlinep (when (defined-fun-p var)
1027 (defined-fun-inlinep var))))
1028 (if (eq inlinep :notinline)
1029 (ir1-convert-combination start next result form var)
1030 (let* ((name (leaf-source-name var))
1031 (transform (info :function :source-transform name)))
1033 (multiple-value-bind (transformed pass) (funcall transform form)
1035 (ir1-convert-maybe-predicate start next result form var))
1037 (unless (policy *lexenv* (zerop store-xref-data))
1038 (record-call name (ctran-block start) *current-path*))
1039 (ir1-convert start next result transformed))))
1040 (ir1-convert-maybe-predicate start next result form var))))))
1042 ;;; KLUDGE: If we insert a synthetic IF for a function with the PREDICATE
1043 ;;; attribute, don't generate any branch coverage instrumentation for it.
1044 (defvar *instrument-if-for-code-coverage* t)
1046 ;;; If the function has the PREDICATE attribute, and the RESULT's DEST
1047 ;;; isn't an IF, then we convert (IF <form> T NIL), ensuring that a
1048 ;;; predicate always appears in a conditional context.
1050 ;;; If the function isn't a predicate, then we call
1051 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
1052 (defun ir1-convert-maybe-predicate (start next result form var)
1053 (declare (type ctran start next)
1054 (type (or lvar null) result)
1056 (type global-var var))
1057 (let ((info (info :function :info (leaf-source-name var))))
1059 (ir1-attributep (fun-info-attributes info) predicate)
1060 (not (if-p (and result (lvar-dest result)))))
1061 (let ((*instrument-if-for-code-coverage* nil))
1062 (ir1-convert start next result `(if ,form t nil)))
1063 (ir1-convert-combination-checking-type start next result form var))))
1065 ;;; Actually really convert a global function call that we are allowed
1068 ;;; If we know the function type of the function, then we check the
1069 ;;; call for syntactic legality with respect to the declared function
1070 ;;; type. If it is impossible to determine whether the call is correct
1071 ;;; due to non-constant keywords, then we give up, marking the call as
1072 ;;; :FULL to inhibit further error messages. We return true when the
1075 ;;; If the call is legal, we also propagate type assertions from the
1076 ;;; function type to the arg and result lvars. We do this now so that
1077 ;;; IR1 optimize doesn't have to redundantly do the check later so
1078 ;;; that it can do the type propagation.
1079 (defun ir1-convert-combination-checking-type (start next result form var)
1080 (declare (type ctran start next) (type (or lvar null) result)
1083 (let* ((node (ir1-convert-combination start next result form var))
1084 (fun-lvar (basic-combination-fun node))
1085 (type (leaf-type var)))
1086 (when (validate-call-type node type var t)
1087 (setf (lvar-%derived-type fun-lvar)
1088 (make-single-value-type type))
1089 (setf (lvar-reoptimize fun-lvar) nil)))
1092 ;;; Convert a call to a local function, or if the function has already
1093 ;;; been LET converted, then throw FUNCTIONAL to
1094 ;;; LOCALL-ALREADY-LET-CONVERTED. The THROW should only happen when we
1095 ;;; are converting inline expansions for local functions during
1097 (defun ir1-convert-local-combination (start next result form functional)
1098 (assure-functional-live-p functional)
1099 (ir1-convert-combination start next result
1101 (maybe-reanalyze-functional functional)))
1105 ;;; Given a list of LAMBDA-VARs and a variable name, return the
1106 ;;; LAMBDA-VAR for that name, or NIL if it isn't found. We return the
1107 ;;; *last* variable with that name, since LET* bindings may be
1108 ;;; duplicated, and declarations always apply to the last.
1109 (declaim (ftype (sfunction (list symbol) (or lambda-var list))
1111 (defun find-in-bindings (vars name)
1115 (when (eq (leaf-source-name var) name)
1117 (let ((info (lambda-var-arg-info var)))
1119 (let ((supplied-p (arg-info-supplied-p info)))
1120 (when (and supplied-p
1121 (eq (leaf-source-name supplied-p) name))
1122 (setq found supplied-p))))))
1123 ((and (consp var) (eq (car var) name))
1124 (setf found (cdr var)))))
1127 ;;; Called by PROCESS-DECLS to deal with a variable type declaration.
1128 ;;; If a LAMBDA-VAR being bound, we intersect the type with the var's
1129 ;;; type, otherwise we add a type restriction on the var. If a symbol
1130 ;;; macro, we just wrap a THE around the expansion.
1131 (defun process-type-decl (decl res vars context)
1132 (declare (list decl vars) (type lexenv res))
1133 (let ((type (compiler-specifier-type (first decl))))
1134 (collect ((restr nil cons)
1135 (new-vars nil cons))
1136 (dolist (var-name (rest decl))
1137 (when (boundp var-name)
1138 (program-assert-symbol-home-package-unlocked
1139 context var-name "declaring the type of ~A"))
1140 (let* ((bound-var (find-in-bindings vars var-name))
1142 (lexenv-find var-name vars)
1143 (find-free-var var-name))))
1147 ((process-var (var bound-var)
1148 (let* ((old-type (or (lexenv-find var type-restrictions)
1150 (int (if (or (fun-type-p type)
1151 (fun-type-p old-type))
1153 (type-approx-intersection2
1155 (cond ((eq int *empty-type*)
1156 (unless (policy *lexenv* (= inhibit-warnings 3))
1160 "The type declarations ~S and ~S for ~S conflict."
1163 (type-specifier old-type)
1164 (type-specifier type)
1167 (setf (leaf-type bound-var) int
1168 (leaf-where-from bound-var) :declared))
1170 (restr (cons var int)))))))
1171 (process-var var bound-var)
1172 (awhen (and (lambda-var-p var)
1173 (lambda-var-specvar var))
1174 (process-var it nil))))
1176 ;; FIXME: non-ANSI weirdness
1177 (aver (eq (car var) 'macro))
1178 (new-vars `(,var-name . (macro . (the ,(first decl)
1182 "~S is an alien variable, so its type can't be declared."
1185 (if (or (restr) (new-vars))
1186 (make-lexenv :default res
1187 :type-restrictions (restr)
1191 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
1192 ;;; declarations for function variables. In addition to allowing
1193 ;;; declarations for functions being bound, we must also deal with
1194 ;;; declarations that constrain the type of lexically apparent
1196 (defun process-ftype-decl (spec res names fvars context)
1197 (declare (type list names fvars)
1199 (let ((type (compiler-specifier-type spec)))
1200 (collect ((res nil cons))
1201 (dolist (name names)
1202 (when (fboundp name)
1203 (program-assert-symbol-home-package-unlocked
1204 context name "declaring the ftype of ~A"))
1205 (let ((found (find name fvars :key #'leaf-source-name :test #'equal)))
1208 (setf (leaf-type found) type)
1209 (assert-definition-type found type
1210 :unwinnage-fun #'compiler-notify
1211 :where "FTYPE declaration"))
1213 (res (cons (find-lexically-apparent-fun
1214 name "in a function type declaration")
1217 (make-lexenv :default res :type-restrictions (res))
1220 ;;; Process a special declaration, returning a new LEXENV. A non-bound
1221 ;;; special declaration is instantiated by throwing a special variable
1222 ;;; into the variables if BINDING-FORM-P is NIL, or otherwise into
1223 ;;; *POST-BINDING-VARIABLE-LEXENV*.
1224 (defun process-special-decl (spec res vars binding-form-p context)
1225 (declare (list spec vars) (type lexenv res))
1226 (collect ((new-venv nil cons))
1227 (dolist (name (cdr spec))
1228 ;; While CLHS seems to allow local SPECIAL declarations for constants,
1229 ;; whatever the semantics are supposed to be is not at all clear to me
1230 ;; -- since constants aren't allowed to be bound it should be a no-op as
1231 ;; no-one can observe the difference portably, but specials are allowed
1232 ;; to be bound... yet nowhere does it say that the special declaration
1233 ;; removes the constantness. Call it a spec bug and prohibit it. Same
1234 ;; for GLOBAL variables.
1235 (let ((kind (info :variable :kind name)))
1236 (unless (member kind '(:special :unknown))
1237 (error "Can't declare ~(~A~) variable locally special: ~S" kind name)))
1238 (program-assert-symbol-home-package-unlocked
1239 context name "declaring ~A special")
1240 (let ((var (find-in-bindings vars name)))
1243 (aver (eq (car var) 'macro))
1245 "~S is a symbol-macro and thus can't be declared special."
1248 (when (lambda-var-ignorep var)
1249 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1250 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1251 (compiler-style-warn
1252 "The ignored variable ~S is being declared special."
1254 (setf (lambda-var-specvar var)
1255 (specvar-for-binding name)))
1257 (unless (or (assoc name (new-venv) :test #'eq))
1258 (new-venv (cons name (specvar-for-binding name))))))))
1259 (cond (binding-form-p
1260 (setf *post-binding-variable-lexenv*
1261 (append (new-venv) *post-binding-variable-lexenv*))
1264 (make-lexenv :default res :vars (new-venv)))
1268 ;;; Return a DEFINED-FUN which copies a GLOBAL-VAR but for its INLINEP
1269 ;;; (and TYPE if notinline), plus type-restrictions from the lexenv.
1270 (defun make-new-inlinep (var inlinep local-type)
1271 (declare (type global-var var) (type inlinep inlinep))
1272 (let* ((type (if (and (eq inlinep :notinline)
1273 (not (eq (leaf-where-from var) :declared)))
1274 (specifier-type 'function)
1276 (res (make-defined-fun
1277 :%source-name (leaf-source-name var)
1278 :where-from (leaf-where-from var)
1279 :type (if local-type
1280 (type-intersection local-type type)
1283 (when (defined-fun-p var)
1284 (setf (defined-fun-inline-expansion res)
1285 (defined-fun-inline-expansion var))
1286 (setf (defined-fun-functionals res)
1287 (defined-fun-functionals var)))
1288 ;; FIXME: Is this really right? Needs we not set the FUNCTIONAL
1289 ;; to the original global-var?
1292 ;;; Parse an inline/notinline declaration. If it's a local function we're
1293 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
1294 (defun process-inline-decl (spec res fvars)
1295 (let ((sense (cdr (assoc (first spec) *inlinep-translations* :test #'eq)))
1297 (dolist (name (rest spec))
1298 (let ((fvar (find name fvars :key #'leaf-source-name :test #'equal)))
1300 (setf (functional-inlinep fvar) sense)
1301 (let ((found (find-lexically-apparent-fun
1302 name "in an inline or notinline declaration")))
1305 (when (policy *lexenv* (>= speed inhibit-warnings))
1306 (compiler-notify "ignoring ~A declaration not at ~
1307 definition of local function:~% ~S"
1311 (cdr (assoc found (lexenv-type-restrictions res)))))
1312 (push (cons name (make-new-inlinep found sense type))
1315 (make-lexenv :default res :funs new-fenv)
1318 ;;; like FIND-IN-BINDINGS, but looks for #'FOO in the FVARS
1319 (defun find-in-bindings-or-fbindings (name vars fvars)
1320 (declare (list vars fvars))
1322 (destructuring-bind (wot fn-name) name
1323 (unless (eq wot 'function)
1324 (compiler-error "The function or variable name ~S is unrecognizable."
1326 (find fn-name fvars :key #'leaf-source-name :test #'equal))
1327 (find-in-bindings vars name)))
1329 ;;; Process an ignore/ignorable declaration, checking for various losing
1331 (defun process-ignore-decl (spec vars fvars)
1332 (declare (list spec vars fvars))
1333 (dolist (name (rest spec))
1334 (let ((var (find-in-bindings-or-fbindings name vars fvars)))
1337 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1338 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1339 (compiler-style-warn "declaring unknown variable ~S to be ignored"
1341 ;; FIXME: This special case looks like non-ANSI weirdness.
1342 ((and (consp var) (eq (car var) 'macro))
1343 ;; Just ignore the IGNORE decl.
1346 (setf (leaf-ever-used var) t))
1347 ((and (lambda-var-specvar var) (eq (first spec) 'ignore))
1348 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1349 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1350 (compiler-style-warn "declaring special variable ~S to be ignored"
1352 ((eq (first spec) 'ignorable)
1353 (setf (leaf-ever-used var) t))
1355 (setf (lambda-var-ignorep var) t)))))
1358 (defun process-dx-decl (names vars fvars kind)
1359 (let ((dx (cond ((eq 'truly-dynamic-extent kind)
1361 ((and (eq 'dynamic-extent kind)
1362 *stack-allocate-dynamic-extent*)
1365 (dolist (name names)
1368 (let* ((bound-var (find-in-bindings vars name))
1370 (lexenv-find name vars)
1371 (maybe-find-free-var name))))
1375 (setf (leaf-dynamic-extent var) dx)
1377 "Ignoring free DYNAMIC-EXTENT declaration: ~S" name)))
1379 (compiler-error "DYNAMIC-EXTENT on symbol-macro: ~S" name))
1381 (compiler-error "DYNAMIC-EXTENT on alien-variable: ~S"
1384 (compiler-style-warn
1385 "Unbound variable declared DYNAMIC-EXTENT: ~S" name)))))
1387 (eq (car name) 'function)
1389 (valid-function-name-p (cadr name)))
1390 (let* ((fname (cadr name))
1391 (bound-fun (find fname fvars
1392 :key #'leaf-source-name
1394 (fun (or bound-fun (lexenv-find fname funs))))
1398 #!+stack-allocatable-closures
1399 (setf (leaf-dynamic-extent bound-fun) dx)
1400 #!-stack-allocatable-closures
1402 "Ignoring DYNAMIC-EXTENT declaration on function ~S ~
1403 (not supported on this platform)." fname)
1405 "Ignoring free DYNAMIC-EXTENT declaration: ~S" name)))
1407 (compiler-error "DYNAMIC-EXTENT on macro: ~S" name))
1409 (compiler-style-warn
1410 "Unbound function declared DYNAMIC-EXTENT: ~S" name)))))
1412 (compiler-error "DYNAMIC-EXTENT on a weird thing: ~S" name))))
1413 (when (policy *lexenv* (= speed 3))
1414 (compiler-notify "Ignoring DYNAMIC-EXTENT declarations: ~S" names)))))
1416 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
1417 ;;; go away, I think.
1418 (defvar *suppress-values-declaration* nil
1420 "If true, processing of the VALUES declaration is inhibited.")
1422 ;;; Process a single declaration spec, augmenting the specified LEXENV
1423 ;;; RES. Return RES and result type. VARS and FVARS are as described
1425 (defun process-1-decl (raw-spec res vars fvars binding-form-p context)
1426 (declare (type list raw-spec vars fvars))
1427 (declare (type lexenv res))
1428 (let ((spec (canonized-decl-spec raw-spec))
1429 (result-type *wild-type*))
1432 (special (process-special-decl spec res vars binding-form-p context))
1435 (compiler-error "no type specified in FTYPE declaration: ~S" spec))
1436 (process-ftype-decl (second spec) res (cddr spec) fvars context))
1437 ((inline notinline maybe-inline)
1438 (process-inline-decl spec res fvars))
1440 (process-ignore-decl spec vars fvars)
1445 :policy (process-optimize-decl spec (lexenv-policy res))))
1449 :handled-conditions (process-muffle-conditions-decl
1450 spec (lexenv-handled-conditions res))))
1451 (unmuffle-conditions
1454 :handled-conditions (process-unmuffle-conditions-decl
1455 spec (lexenv-handled-conditions res))))
1457 (process-type-decl (cdr spec) res vars context))
1459 (unless *suppress-values-declaration*
1460 (let ((types (cdr spec)))
1462 (compiler-values-specifier-type
1463 (if (singleton-p types)
1465 `(values ,@types)))))
1467 ((dynamic-extent truly-dynamic-extent)
1468 (process-dx-decl (cdr spec) vars fvars (first spec))
1470 ((disable-package-locks enable-package-locks)
1473 :disabled-package-locks (process-package-lock-decl
1474 spec (lexenv-disabled-package-locks res))))
1476 (unless (info :declaration :recognized (first spec))
1477 (compiler-warn "unrecognized declaration ~S" raw-spec))
1478 (let ((fn (info :declaration :handler (first spec))))
1480 (funcall fn res spec vars fvars)
1484 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1485 ;;; and FUNCTIONAL structures which are being bound. In addition to
1486 ;;; filling in slots in the leaf structures, we return a new LEXENV,
1487 ;;; which reflects pervasive special and function type declarations,
1488 ;;; (NOT)INLINE declarations and OPTIMIZE declarations, and type of
1489 ;;; VALUES declarations. If BINDING-FORM-P is true, the third return
1490 ;;; value is a list of VARs that should not apply to the lexenv of the
1491 ;;; initialization forms for the bindings, but should apply to the body.
1493 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1495 (defun process-decls (decls vars fvars &key
1496 (lexenv *lexenv*) (binding-form-p nil) (context :compile))
1497 (declare (list decls vars fvars))
1498 (let ((result-type *wild-type*)
1499 (*post-binding-variable-lexenv* nil))
1500 (dolist (decl decls)
1501 (dolist (spec (rest decl))
1503 ;; Kludge: EVAL calls this function to deal with LOCALLY.
1504 (when (eq context :compile) (list '*current-path*))
1505 (when (eq context :compile) (list (or (get-source-path spec)
1506 (get-source-path decl)
1508 (unless (consp spec)
1509 (compiler-error "malformed declaration specifier ~S in ~S" spec decl))
1510 (multiple-value-bind (new-env new-result-type)
1511 (process-1-decl spec lexenv vars fvars binding-form-p context)
1512 (setq lexenv new-env)
1513 (unless (eq new-result-type *wild-type*)
1515 (values-type-intersection result-type new-result-type)))))))
1516 (values lexenv result-type *post-binding-variable-lexenv*)))
1518 (defun %processing-decls (decls vars fvars ctran lvar binding-form-p fun)
1519 (multiple-value-bind (*lexenv* result-type post-binding-lexenv)
1520 (process-decls decls vars fvars :binding-form-p binding-form-p)
1521 (cond ((eq result-type *wild-type*)
1522 (funcall fun ctran lvar post-binding-lexenv))
1524 (let ((value-ctran (make-ctran))
1525 (value-lvar (make-lvar)))
1526 (multiple-value-prog1
1527 (funcall fun value-ctran value-lvar post-binding-lexenv)
1528 (let ((cast (make-cast value-lvar result-type
1529 (lexenv-policy *lexenv*))))
1530 (link-node-to-previous-ctran cast value-ctran)
1531 (setf (lvar-dest value-lvar) cast)
1532 (use-continuation cast ctran lvar))))))))
1533 (defmacro processing-decls ((decls vars fvars ctran lvar
1534 &optional post-binding-lexenv)
1536 (check-type ctran symbol)
1537 (check-type lvar symbol)
1538 (let ((post-binding-lexenv-p (not (null post-binding-lexenv)))
1539 (post-binding-lexenv (or post-binding-lexenv (sb!xc:gensym "LEXENV"))))
1540 `(%processing-decls ,decls ,vars ,fvars ,ctran ,lvar
1541 ,post-binding-lexenv-p
1542 (lambda (,ctran ,lvar ,post-binding-lexenv)
1543 (declare (ignorable ,post-binding-lexenv))
1546 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1547 ;;; declaration. If there is a global variable of that name, then
1548 ;;; check that it isn't a constant and return it. Otherwise, create an
1549 ;;; anonymous GLOBAL-VAR.
1550 (defun specvar-for-binding (name)
1551 (cond ((not (eq (info :variable :where-from name) :assumed))
1552 (let ((found (find-free-var name)))
1553 (when (heap-alien-info-p found)
1555 "~S is an alien variable and so can't be declared special."
1557 (unless (global-var-p found)
1559 "~S is a constant and so can't be declared special."
1563 (make-global-var :kind :special
1565 :where-from :declared))))