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 note-source-path (form &rest arguments)
47 (when (source-form-has-path-p form)
48 (setf (gethash form *source-paths*)
49 (apply #'list* 'original-source-start *current-form-number* arguments))))
51 ;;; *CURRENT-COMPONENT* is the COMPONENT structure which we link
52 ;;; blocks into as we generate them. This just serves to glue the
53 ;;; emitted blocks together until local call analysis and flow graph
54 ;;; canonicalization figure out what is really going on. We need to
55 ;;; keep track of all the blocks generated so that we can delete them
56 ;;; if they turn out to be unreachable.
58 ;;; FIXME: It's confusing having one variable named *CURRENT-COMPONENT*
59 ;;; and another named *COMPONENT-BEING-COMPILED*. (In CMU CL they
60 ;;; were called *CURRENT-COMPONENT* and *COMPILE-COMPONENT* respectively,
61 ;;; which was also confusing.)
62 (declaim (type (or component null) *current-component*))
63 (defvar *current-component*)
65 ;;; *CURRENT-PATH* is the source path of the form we are currently
66 ;;; translating. See NODE-SOURCE-PATH in the NODE structure.
67 (declaim (list *current-path*))
68 (defvar *current-path*)
70 (defvar *derive-function-types* nil
71 "Should the compiler assume that function types will never change,
72 so that it can use type information inferred from current definitions
73 to optimize code which uses those definitions? Setting this true
74 gives non-ANSI, early-CMU-CL behavior. It can be useful for improving
75 the efficiency of stable code.")
77 (defvar *fun-names-in-this-file* nil)
79 (defvar *post-binding-variable-lexenv* nil)
81 ;;;; namespace management utilities
83 (defun fun-lexically-notinline-p (name)
84 (let ((fun (lexenv-find name funs :test #'equal)))
85 ;; a declaration will trump a proclamation
86 (if (and fun (defined-fun-p fun))
87 (eq (defined-fun-inlinep fun) :notinline)
88 (eq (info :function :inlinep name) :notinline))))
90 ;; This will get redefined in PCL boot.
91 (declaim (notinline update-info-for-gf))
92 (defun maybe-update-info-for-gf (name)
93 (declare (ignorable name))
96 ;;; Return a GLOBAL-VAR structure usable for referencing the global
98 (defun find-global-fun (name latep)
99 (unless (info :function :kind name)
100 (setf (info :function :kind name) :function)
101 (setf (info :function :where-from name) :assumed))
102 (let ((where (info :function :where-from name)))
103 (when (and (eq where :assumed)
104 ;; In the ordinary target Lisp, it's silly to report
105 ;; undefinedness when the function is defined in the
106 ;; running Lisp. But at cross-compile time, the current
107 ;; definedness of a function is irrelevant to the
108 ;; definedness at runtime, which is what matters.
109 #-sb-xc-host (not (fboundp name))
110 ;; LATEP is true when the user has indicated that
111 ;; late-late binding is desired by using eg. a quoted
112 ;; symbol -- in which case it makes little sense to
113 ;; complain about undefined functions.
115 (note-undefined-reference name :function))
117 :kind :global-function
119 :type (if (and (not latep)
120 (or *derive-function-types*
121 (member where '(:declared :defined-method))
122 (and (member name *fun-names-in-this-file*
124 (not (fun-lexically-notinline-p name)))))
126 (maybe-update-info-for-gf name)
127 (info :function :type name))
128 (specifier-type 'function))
129 :defined-type (if (eq where :defined)
130 (info :function :type name)
134 ;;; Has the *FREE-FUNS* entry FREE-FUN become invalid?
136 ;;; In CMU CL, the answer was implicitly always true, so this
137 ;;; predicate didn't exist.
139 ;;; This predicate was added to fix bug 138 in SBCL. In some obscure
140 ;;; circumstances, it was possible for a *FREE-FUNS* entry to contain a
141 ;;; DEFINED-FUN whose DEFINED-FUN-FUNCTIONAL object contained IR1
142 ;;; stuff (NODEs, BLOCKs...) referring to an already compiled (aka
143 ;;; "dead") component. When this IR1 stuff was reused in a new
144 ;;; component, under further obscure circumstances it could be used by
145 ;;; WITH-IR1-ENVIRONMENT-FROM-NODE to generate a binding for
146 ;;; *CURRENT-COMPONENT*. At that point things got all confused, since
147 ;;; IR1 conversion was sending code to a component which had already
148 ;;; been compiled and would never be compiled again.
149 (defun invalid-free-fun-p (free-fun)
150 ;; There might be other reasons that *FREE-FUN* entries could
151 ;; become invalid, but the only one we've been bitten by so far
152 ;; (sbcl-0.pre7.118) is this one:
153 (and (defined-fun-p free-fun)
154 (let ((functional (defined-fun-functional free-fun)))
156 (eql (functional-kind functional) :deleted))
157 (and (lambda-p functional)
159 ;; (The main reason for this first test is to bail
160 ;; out early in cases where the LAMBDA-COMPONENT
161 ;; call in the second test would fail because links
162 ;; it needs are uninitialized or invalid.)
164 ;; If the BIND node for this LAMBDA is null, then
165 ;; according to the slot comments, the LAMBDA has
166 ;; been deleted or its call has been deleted. In
167 ;; that case, it seems rather questionable to reuse
168 ;; it, and certainly it shouldn't be necessary to
169 ;; reuse it, so we cheerfully declare it invalid.
170 (null (lambda-bind functional))
171 ;; If this IR1 stuff belongs to a dead component,
172 ;; then we can't reuse it without getting into
173 ;; bizarre confusion.
174 (eql (component-info (lambda-component functional))
177 ;;; If NAME already has a valid entry in *FREE-FUNS*, then return
178 ;;; the value. Otherwise, make a new GLOBAL-VAR using information from
179 ;;; the global environment and enter it in *FREE-FUNS*. If NAME
180 ;;; names a macro or special form, then we error out using the
181 ;;; supplied context which indicates what we were trying to do that
182 ;;; demanded a function.
183 (declaim (ftype (sfunction (t string) global-var) find-free-fun))
184 (defun find-free-fun (name context)
185 (or (let ((old-free-fun (gethash name *free-funs*)))
186 (and (not (invalid-free-fun-p old-free-fun))
188 (ecase (info :function :kind name)
189 ;; FIXME: The :MACRO and :SPECIAL-FORM cases could be merged.
191 (compiler-error "The macro name ~S was found ~A." name context))
193 (compiler-error "The special form name ~S was found ~A."
197 (check-fun-name name)
198 (note-if-setf-fun-and-macro name)
199 (let ((expansion (fun-name-inline-expansion name))
200 (inlinep (info :function :inlinep name)))
201 (setf (gethash name *free-funs*)
202 (if (or expansion inlinep)
205 :inline-expansion expansion
207 :where-from (info :function :where-from name)
208 :type (if (eq inlinep :notinline)
209 (specifier-type 'function)
210 (info :function :type name)))
211 (find-global-fun name nil))))))))
213 ;;; Return the LEAF structure for the lexically apparent function
214 ;;; definition of NAME.
215 (declaim (ftype (sfunction (t string) leaf) find-lexically-apparent-fun))
216 (defun find-lexically-apparent-fun (name context)
217 (let ((var (lexenv-find name funs :test #'equal)))
220 (aver (and (consp var) (eq (car var) 'macro)))
221 (compiler-error "found macro name ~S ~A" name context))
224 (find-free-fun name context)))))
226 ;;; Return the LEAF node for a global variable reference to NAME. If
227 ;;; NAME is already entered in *FREE-VARS*, then we just return the
228 ;;; corresponding value. Otherwise, we make a new leaf using
229 ;;; information from the global environment and enter it in
230 ;;; *FREE-VARS*. If the variable is unknown, then we emit a warning.
231 (declaim (ftype (sfunction (t) (or leaf cons heap-alien-info)) find-free-var))
232 (defun find-free-var (name)
233 (unless (symbolp name)
234 (compiler-error "Variable name is not a symbol: ~S." name))
235 (or (gethash name *free-vars*)
236 (let ((kind (info :variable :kind name))
237 (type (info :variable :type name))
238 (where-from (info :variable :where-from name)))
239 (when (and (eq where-from :assumed) (eq kind :global))
240 (note-undefined-reference name :variable))
241 (setf (gethash name *free-vars*)
244 (info :variable :alien-info name))
245 ;; FIXME: The return value in this case should really be
246 ;; of type SB!C::LEAF. I don't feel too badly about it,
247 ;; because the MACRO idiom is scattered throughout this
248 ;; file, but it should be cleaned up so we're not
249 ;; throwing random conses around. --njf 2002-03-23
251 (let ((expansion (info :variable :macro-expansion name))
252 (type (type-specifier (info :variable :type name))))
253 `(macro . (the ,type ,expansion))))
255 (let ((value (symbol-value name)))
256 ;; Override the values of standard symbols in XC,
257 ;; since we can't redefine them.
259 (when (eql (find-symbol (symbol-name name) :cl) name)
260 (multiple-value-bind (xc-value foundp)
261 (info :variable :xc-constant-value name)
263 (setf value xc-value))
264 ((not (eq value name))
266 "Using cross-compilation host's definition of ~S: ~A~%"
267 name (symbol-value name))))))
268 (find-constant value name)))
270 (make-global-var :kind kind
273 :where-from where-from)))))))
275 ;;; Grovel over CONSTANT checking for any sub-parts that need to be
276 ;;; processed with MAKE-LOAD-FORM. We have to be careful, because
277 ;;; CONSTANT might be circular. We also check that the constant (and
278 ;;; any subparts) are dumpable at all.
279 (defun maybe-emit-make-load-forms (constant &optional (name nil namep))
280 (let ((xset (alloc-xset)))
281 (labels ((trivialp (value)
284 #-sb-xc-host unboxed-array
285 #+sb-xc-host (simple-array (unsigned-byte 8) (*))
291 ;; Unless VALUE is an object which which obviously
292 ;; can't contain other objects
293 (unless (trivialp value)
294 (if (xset-member-p value xset)
295 (return-from grovel nil)
296 (add-to-xset value xset))
300 (grovel (cdr value)))
302 (dotimes (i (length value))
303 (grovel (svref value i))))
305 (dotimes (i (length value))
306 (grovel (aref value i))))
308 ;; Even though the (ARRAY T) branch does the exact
309 ;; same thing as this branch we do this separately
310 ;; so that the compiler can use faster versions of
311 ;; array-total-size and row-major-aref.
312 (dotimes (i (array-total-size value))
313 (grovel (row-major-aref value i))))
315 (dotimes (i (array-total-size value))
316 (grovel (row-major-aref value i))))
317 (#+sb-xc-host structure!object
318 #-sb-xc-host instance
319 ;; In the target SBCL, we can dump any instance, but
320 ;; in the cross-compilation host, %INSTANCE-FOO
321 ;; functions don't work on general instances, only on
322 ;; STRUCTURE!OBJECTs.
324 ;; FIXME: What about funcallable instances with
325 ;; user-defined MAKE-LOAD-FORM methods?
326 (when (emit-make-load-form value)
327 (dotimes (i (- (%instance-length value)
329 #-sb-xc-host (layout-n-untagged-slots
330 (%instance-ref value 0))))
331 (grovel (%instance-ref value i)))))
334 "Objects of type ~S can't be dumped into fasl files."
335 (type-of value)))))))
336 ;; Dump all non-trivial named constants using the name.
337 (if (and namep (not (typep constant '(or symbol character
338 ;; FIXME: Cold init breaks if we
339 ;; try to reference FP constants
342 #-sb-xc-host fixnum))))
343 (emit-make-load-form constant name)
347 ;;;; some flow-graph hacking utilities
349 ;;; This function sets up the back link between the node and the
350 ;;; ctran which continues at it.
351 (defun link-node-to-previous-ctran (node ctran)
352 (declare (type node node) (type ctran ctran))
353 (aver (not (ctran-next ctran)))
354 (setf (ctran-next ctran) node)
355 (setf (node-prev node) ctran))
357 ;;; This function is used to set the ctran for a node, and thus
358 ;;; determine what is evaluated next. If the ctran has no block, then
359 ;;; we make it be in the block that the node is in. If the ctran heads
360 ;;; its block, we end our block and link it to that block.
361 #!-sb-fluid (declaim (inline use-ctran))
362 (defun use-ctran (node ctran)
363 (declare (type node node) (type ctran ctran))
364 (if (eq (ctran-kind ctran) :unused)
365 (let ((node-block (ctran-block (node-prev node))))
366 (setf (ctran-block ctran) node-block)
367 (setf (ctran-kind ctran) :inside-block)
368 (setf (ctran-use ctran) node)
369 (setf (node-next node) ctran))
370 (%use-ctran node ctran)))
371 (defun %use-ctran (node ctran)
372 (declare (type node node) (type ctran ctran) (inline member))
373 (let ((block (ctran-block ctran))
374 (node-block (ctran-block (node-prev node))))
375 (aver (eq (ctran-kind ctran) :block-start))
376 (when (block-last node-block)
377 (error "~S has already ended." node-block))
378 (setf (block-last node-block) node)
379 (when (block-succ node-block)
380 (error "~S already has successors." node-block))
381 (setf (block-succ node-block) (list block))
382 (when (memq node-block (block-pred block))
383 (error "~S is already a predecessor of ~S." node-block block))
384 (push node-block (block-pred block))))
386 ;;; Insert NEW before OLD in the flow-graph.
387 (defun insert-node-before (old new)
388 (let ((prev (node-prev old))
390 (ensure-block-start prev)
391 (setf (ctran-next prev) nil)
392 (link-node-to-previous-ctran new prev)
394 (link-node-to-previous-ctran old temp))
397 ;;; This function is used to set the ctran for a node, and thus
398 ;;; determine what receives the value.
399 (defun use-lvar (node lvar)
400 (declare (type valued-node node) (type (or lvar null) lvar))
401 (aver (not (node-lvar node)))
403 (setf (node-lvar node) lvar)
404 (cond ((null (lvar-uses lvar))
405 (setf (lvar-uses lvar) node))
406 ((listp (lvar-uses lvar))
407 (aver (not (memq node (lvar-uses lvar))))
408 (push node (lvar-uses lvar)))
410 (aver (neq node (lvar-uses lvar)))
411 (setf (lvar-uses lvar) (list node (lvar-uses lvar)))))
412 (reoptimize-lvar lvar)))
414 #!-sb-fluid(declaim (inline use-continuation))
415 (defun use-continuation (node ctran lvar)
416 (use-ctran node ctran)
417 (use-lvar node lvar))
419 ;;;; exported functions
421 ;;; This function takes a form and the top level form number for that
422 ;;; form, and returns a lambda representing the translation of that
423 ;;; form in the current global environment. The returned lambda is a
424 ;;; top level lambda that can be called to cause evaluation of the
425 ;;; forms. This lambda is in the initial component. If FOR-VALUE is T,
426 ;;; then the value of the form is returned from the function,
427 ;;; otherwise NIL is returned.
429 ;;; This function may have arbitrary effects on the global environment
430 ;;; due to processing of EVAL-WHENs. All syntax error checking is
431 ;;; done, with erroneous forms being replaced by a proxy which signals
432 ;;; an error if it is evaluated. Warnings about possibly inconsistent
433 ;;; or illegal changes to the global environment will also be given.
435 ;;; We make the initial component and convert the form in a PROGN (and
436 ;;; an optional NIL tacked on the end.) We then return the lambda. We
437 ;;; bind all of our state variables here, rather than relying on the
438 ;;; global value (if any) so that IR1 conversion will be reentrant.
439 ;;; This is necessary for EVAL-WHEN processing, etc.
441 ;;; The hashtables used to hold global namespace info must be
442 ;;; reallocated elsewhere. Note also that *LEXENV* is not bound, so
443 ;;; that local macro definitions can be introduced by enclosing code.
444 (defun ir1-toplevel (form path for-value &optional (allow-instrumenting t))
445 (declare (list path))
446 (let* ((*current-path* path)
447 (component (make-empty-component))
448 (*current-component* component)
449 (*allow-instrumenting* allow-instrumenting))
450 (setf (component-name component) 'initial-component)
451 (setf (component-kind component) :initial)
452 (let* ((forms (if for-value `(,form) `(,form nil)))
453 (res (ir1-convert-lambda-body
455 :debug-name (debug-name 'top-level-form form))))
456 (setf (functional-entry-fun res) res
457 (functional-arg-documentation res) ()
458 (functional-kind res) :toplevel)
461 ;;; This function is called on freshly read forms to record the
462 ;;; initial location of each form (and subform.) Form is the form to
463 ;;; find the paths in, and TLF-NUM is the top level form number of the
464 ;;; truly top level form.
466 ;;; This gets a bit interesting when the source code is circular. This
467 ;;; can (reasonably?) happen in the case of circular list constants.
468 (defun find-source-paths (form tlf-num)
469 (declare (type index tlf-num))
470 (let ((*current-form-number* 0))
471 (sub-find-source-paths form (list tlf-num)))
473 (defun sub-find-source-paths (form path)
474 (unless (get-source-path form)
475 (note-source-path form path)
476 (incf *current-form-number*)
480 (declare (fixnum pos))
483 (when (atom subform) (return))
484 (let ((fm (car subform)))
486 ;; If it's a cons, recurse
487 (sub-find-source-paths fm (cons pos path))
488 ;; Otherwise store the containing form. It's
489 ;; not perfect, but better than nothing.
491 (note-source-path subform pos path)))
493 (setq subform (cdr subform))
494 (when (eq subform trail) (return)))))
498 (setq trail (cdr trail)))))))
500 ;;;; IR1-CONVERT, macroexpansion and special form dispatching
502 (declaim (ftype (sfunction (ctran ctran (or lvar null) t) (values))
504 (macrolet (;; Bind *COMPILER-ERROR-BAILOUT* to a function that throws
505 ;; out of the body and converts a condition signalling form
506 ;; instead. The source form is converted to a string since it
507 ;; may contain arbitrary non-externalizable objects.
508 (ir1-error-bailout ((start next result form) &body body)
509 (with-unique-names (skip condition)
511 (let ((,condition (catch 'ir1-error-abort
512 (let ((*compiler-error-bailout*
513 (lambda (&optional e)
514 (throw 'ir1-error-abort e))))
516 (return-from ,skip nil)))))
517 (ir1-convert ,start ,next ,result
518 (make-compiler-error-form ,condition
521 ;; Translate FORM into IR1. The code is inserted as the NEXT of the
522 ;; CTRAN START. RESULT is the LVAR which receives the value of the
523 ;; FORM to be translated. The translators call this function
524 ;; recursively to translate their subnodes.
526 ;; As a special hack to make life easier in the compiler, a LEAF
527 ;; IR1-converts into a reference to that LEAF structure. This allows
528 ;; the creation using backquote of forms that contain leaf
529 ;; references, without having to introduce dummy names into the
531 (defun ir1-convert (start next result form)
532 (ir1-error-bailout (start next result form)
533 (let* ((*current-path* (or (get-source-path form)
534 (cons form *current-path*)))
535 (start (instrument-coverage start nil form)))
537 (cond ((and (symbolp form) (not (keywordp form)))
538 (ir1-convert-var start next result form))
540 (reference-leaf start next result form))
542 (reference-constant start next result form))))
544 (ir1-convert-functoid start next result form)))))
547 ;; Generate a reference to a manifest constant, creating a new leaf
549 (defun reference-constant (start next result value)
550 (declare (type ctran start next)
551 (type (or lvar null) result))
552 (ir1-error-bailout (start next result value)
553 (let* ((leaf (find-constant value))
554 (res (make-ref leaf)))
555 (push res (leaf-refs leaf))
556 (link-node-to-previous-ctran res start)
557 (use-continuation res next result)))
560 ;;; Add FUNCTIONAL to the COMPONENT-REANALYZE-FUNCTIONALS, unless it's
561 ;;; some trivial type for which reanalysis is a trivial no-op, or
562 ;;; unless it doesn't belong in this component at all.
564 ;;; FUNCTIONAL is returned.
565 (defun maybe-reanalyze-functional (functional)
567 (aver (not (eql (functional-kind functional) :deleted))) ; bug 148
568 (aver-live-component *current-component*)
570 ;; When FUNCTIONAL is of a type for which reanalysis isn't a trivial
572 (when (typep functional '(or optional-dispatch clambda))
574 ;; When FUNCTIONAL knows its component
575 (when (lambda-p functional)
576 (aver (eql (lambda-component functional) *current-component*)))
579 (component-reanalyze-functionals *current-component*)))
583 ;;; Generate a REF node for LEAF, frobbing the LEAF structure as
584 ;;; needed. If LEAF represents a defined function which has already
585 ;;; been converted, and is not :NOTINLINE, then reference the
586 ;;; functional instead.
587 (defun reference-leaf (start next result leaf &optional (name '.anonymous.))
588 (declare (type ctran start next) (type (or lvar null) result) (type leaf leaf))
589 (when (functional-p leaf)
590 (assure-functional-live-p leaf))
591 (let* ((type (lexenv-find leaf type-restrictions))
592 (leaf (or (and (defined-fun-p leaf)
593 (not (eq (defined-fun-inlinep leaf)
595 (let ((functional (defined-fun-functional leaf)))
596 (when (and functional
597 (not (functional-kind functional))
598 ;; Bug MISC.320: ir1-transform
599 ;; can create a reference to a
600 ;; inline-expanded function,
601 ;; defined in another component.
602 (not (and (lambda-p functional)
603 (neq (lambda-component functional)
604 *current-component*))))
605 (maybe-reanalyze-functional functional))))
606 (when (and (lambda-p leaf)
607 (memq (functional-kind leaf)
609 (maybe-reanalyze-functional leaf))
611 (ref (make-ref leaf name)))
612 (push ref (leaf-refs leaf))
613 (setf (leaf-ever-used leaf) t)
614 (link-node-to-previous-ctran ref start)
615 (cond (type (let* ((ref-ctran (make-ctran))
616 (ref-lvar (make-lvar))
617 (cast (make-cast ref-lvar
618 (make-single-value-type type)
619 (lexenv-policy *lexenv*))))
620 (setf (lvar-dest ref-lvar) cast)
621 (use-continuation ref ref-ctran ref-lvar)
622 (link-node-to-previous-ctran cast ref-ctran)
623 (use-continuation cast next result)))
624 (t (use-continuation ref next result)))))
626 ;;; Convert a reference to a symbolic constant or variable. If the
627 ;;; symbol is entered in the LEXENV-VARS we use that definition,
628 ;;; otherwise we find the current global definition. This is also
629 ;;; where we pick off symbol macro and alien variable references.
630 (defun ir1-convert-var (start next result name)
631 (declare (type ctran start next) (type (or lvar null) result) (symbol name))
632 (let ((var (or (lexenv-find name vars) (find-free-var name))))
633 (if (and (global-var-p var) (not result))
634 ;; KLUDGE: If the reference is dead, convert using SYMBOL-VALUE
635 ;; which is not flushable, so that unbound dead variables signal
636 ;; an error (bug 412).
637 (ir1-convert start next result `(symbol-value ',name))
640 (when (lambda-var-p var)
641 (let ((home (ctran-home-lambda-or-null start)))
643 (sset-adjoin var (lambda-calls-or-closes home))))
644 (when (lambda-var-ignorep var)
645 ;; (ANSI's specification for the IGNORE declaration requires
646 ;; that this be a STYLE-WARNING, not a full WARNING.)
648 (compiler-style-warn "reading an ignored variable: ~S" name)
649 ;; there's no need for us to accept ANSI's lameness when
650 ;; processing our own code, though.
652 (warn "reading an ignored variable: ~S" name)))
653 (reference-leaf start next result var name))
655 (aver (eq (car var) 'macro))
656 ;; FIXME: [Free] type declarations. -- APD, 2002-01-26
657 (ir1-convert start next result (cdr var)))
659 (ir1-convert start next result `(%heap-alien ',var))))))
662 ;;; Find a compiler-macro for a form, taking FUNCALL into account.
663 (defun find-compiler-macro (opname form)
664 (if (eq opname 'funcall)
665 (let ((fun-form (cadr form)))
666 (cond ((and (consp fun-form) (eq 'function (car fun-form)))
667 (let ((real-fun (cadr fun-form)))
668 (if (legal-fun-name-p real-fun)
669 (values (sb!xc:compiler-macro-function real-fun *lexenv*)
672 ((sb!xc:constantp fun-form *lexenv*)
673 (let ((fun (constant-form-value fun-form *lexenv*)))
674 (if (legal-fun-name-p fun)
675 ;; CLHS tells us that local functions must shadow
676 ;; compiler-macro-functions, but since the call is
677 ;; through a name, we are obviously interested
678 ;; in the global function.
679 (values (sb!xc:compiler-macro-function fun nil) fun)
683 (if (legal-fun-name-p opname)
684 (values (sb!xc:compiler-macro-function opname *lexenv*) opname)
687 ;;; Picks of special forms and compiler-macro expansions, and hands
688 ;;; the rest to IR1-CONVERT-COMMON-FUNCTOID
689 (defun ir1-convert-functoid (start next result form)
690 (let* ((op (car form))
691 (translator (and (symbolp op) (info :function :ir1-convert op))))
693 (when (sb!xc:compiler-macro-function op *lexenv*)
694 (compiler-warn "ignoring compiler macro for special form"))
695 (funcall translator start next result form))
697 (multiple-value-bind (cmacro-fun cmacro-fun-name)
698 (find-compiler-macro op form)
700 ;; CLHS 3.2.2.1.3 specifies that NOTINLINE
701 ;; suppresses compiler-macros.
702 (not (fun-lexically-notinline-p cmacro-fun-name)))
703 (let ((res (careful-expand-macro cmacro-fun form)))
705 (ir1-convert-common-functoid start next result form
707 (ir1-convert start next result res)))
708 (ir1-convert-common-functoid start next result form op)))))))
710 ;;; Handles the "common" cases: any other forms except special forms
711 ;;; and compiler-macros.
712 (defun ir1-convert-common-functoid (start next result form op)
713 (cond ((or (symbolp op) (leaf-p op))
714 (let ((lexical-def (if (leaf-p op) op (lexenv-find op funs))))
715 (typecase lexical-def
717 (ir1-convert-global-functoid start next result form op))
719 (ir1-convert-local-combination start next result form
722 (ir1-convert-srctran start next result lexical-def form))
724 (aver (and (consp lexical-def) (eq (car lexical-def) 'macro)))
725 (ir1-convert start next result
726 (careful-expand-macro (cdr lexical-def) form))))))
727 ((or (atom op) (not (eq (car op) 'lambda)))
728 (compiler-error "illegal function call"))
730 ;; implicitly (LAMBDA ..) because the LAMBDA expression is
731 ;; the CAR of an executed form.
732 (ir1-convert-combination
733 start next result form
734 (ir1-convert-lambda op
735 :debug-name (debug-name 'inline-lambda op))))))
737 ;;; Convert anything that looks like a global function call.
738 (defun ir1-convert-global-functoid (start next result form fun)
739 (declare (type ctran start next) (type (or lvar null) result)
741 ;; FIXME: Couldn't all the INFO calls here be converted into
742 ;; standard CL functions, like MACRO-FUNCTION or something? And what
743 ;; happens with lexically-defined (MACROLET) macros here, anyway?
744 (ecase (info :function :kind fun)
746 (ir1-convert start next result
747 (careful-expand-macro (info :function :macro-function fun)
749 (unless (policy *lexenv* (zerop store-xref-data))
750 (record-macroexpansion fun (ctran-block start) *current-path*)))
752 (ir1-convert-srctran start next result
753 (find-free-fun fun "shouldn't happen! (no-cmacro)")
756 (defun muffle-warning-or-die ()
758 (bug "no MUFFLE-WARNING restart"))
760 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
761 ;;; errors which occur during the macroexpansion.
762 (defun careful-expand-macro (fun form)
763 (let (;; a hint I (WHN) wish I'd known earlier
764 (hint "(hint: For more precise location, try *BREAK-ON-SIGNALS*.)"))
765 (flet (;; Return a string to use as a prefix in error reporting,
766 ;; telling something about which form caused the problem.
768 (let ((*print-pretty* nil)
769 ;; We rely on the printer to abbreviate FORM.
774 #-sb-xc-host "(in macroexpansion of ~S)"
775 ;; longer message to avoid ambiguity "Was it the xc host
776 ;; or the cross-compiler which encountered the problem?"
777 #+sb-xc-host "(in cross-compiler macroexpansion of ~S)"
779 (handler-bind ((style-warning (lambda (c)
781 "~@<~A~:@_~A~@:_~A~:>"
782 (wherestring) hint c)
783 (muffle-warning-or-die)))
784 ;; KLUDGE: CMU CL in its wisdom (version 2.4.6 for
785 ;; Debian Linux, anyway) raises a CL:WARNING
786 ;; condition (not a CL:STYLE-WARNING) for undefined
787 ;; symbols when converting interpreted functions,
788 ;; causing COMPILE-FILE to think the file has a real
789 ;; problem, causing COMPILE-FILE to return FAILURE-P
790 ;; set (not just WARNINGS-P set). Since undefined
791 ;; symbol warnings are often harmless forward
792 ;; references, and since it'd be inordinately painful
793 ;; to try to eliminate all such forward references,
794 ;; these warnings are basically unavoidable. Thus, we
795 ;; need to coerce the system to work through them,
796 ;; and this code does so, by crudely suppressing all
797 ;; warnings in cross-compilation macroexpansion. --
799 #+(and cmu sb-xc-host)
804 ~@<(KLUDGE: That was a non-STYLE WARNING. ~
805 Ordinarily that would cause compilation to ~
806 fail. However, since we're running under ~
807 CMU CL, and since CMU CL emits non-STYLE ~
808 warnings for safe, hard-to-fix things (e.g. ~
809 references to not-yet-defined functions) ~
810 we're going to have to ignore it and ~
811 proceed anyway. Hopefully we're not ~
812 ignoring anything horrible here..)~:@>~:>"
815 (muffle-warning-or-die)))
816 #-(and cmu sb-xc-host)
818 (warn "~@<~A~:@_~A~@:_~A~:>"
819 (wherestring) hint c)
820 (muffle-warning-or-die)))
822 (compiler-error "~@<~A~:@_~A~@:_~A~:>"
823 (wherestring) hint c))))
824 (funcall sb!xc:*macroexpand-hook* fun form *lexenv*)))))
826 ;;;; conversion utilities
828 ;;; Convert a bunch of forms, discarding all the values except the
829 ;;; last. If there aren't any forms, then translate a NIL.
830 (declaim (ftype (sfunction (ctran ctran (or lvar null) list) (values))
831 ir1-convert-progn-body))
832 (defun ir1-convert-progn-body (start next result body)
834 (reference-constant start next result nil)
835 (let ((this-start start)
838 (let ((form (car forms)))
840 (maybe-instrument-progn-like this-start forms form))
841 (when (endp (cdr forms))
842 (ir1-convert this-start next result form)
844 (let ((this-ctran (make-ctran)))
845 (ir1-convert this-start this-ctran nil form)
846 (setq this-start this-ctran
847 forms (cdr forms)))))))
853 ;;; Check the policy for whether we should generate code coverage
854 ;;; instrumentation. If not, just return the original START
855 ;;; ctran. Otherwise insert code coverage instrumentation after
856 ;;; START, and return the new ctran.
857 (defun instrument-coverage (start mode form)
858 ;; We don't actually use FORM for anything, it's just convenient to
859 ;; have around when debugging the instrumentation.
860 (declare (ignore form))
861 (if (and (policy *lexenv* (> store-coverage-data 0))
862 *code-coverage-records*
863 *allow-instrumenting*)
864 (let ((path (source-path-original-source *current-path*)))
867 (if (member (ctran-block start)
868 (gethash path *code-coverage-blocks*))
869 ;; If this source path has already been instrumented in
870 ;; this block, don't instrument it again.
873 ;; Get an interned record cons for the path. A cons
874 ;; with the same object identity must be used for
875 ;; each instrument for the same block.
876 (or (gethash path *code-coverage-records*)
877 (setf (gethash path *code-coverage-records*)
878 (cons path +code-coverage-unmarked+))))
880 (*allow-instrumenting* nil))
881 (push (ctran-block start)
882 (gethash path *code-coverage-blocks*))
883 (let ((*allow-instrumenting* nil))
884 (ir1-convert start next nil
886 (declare (optimize speed
889 (check-constant-modification 0)))
890 ;; We're being naughty here, and
891 ;; modifying constant data. That's ok,
892 ;; we know what we're doing.
893 (%rplacd ',store t))))
897 ;;; In contexts where we don't have a source location for FORM
898 ;;; e.g. due to it not being a cons, but where we have a source
899 ;;; location for the enclosing cons, use the latter source location if
900 ;;; available. This works pretty well in practice, since many PROGNish
901 ;;; macroexpansions will just directly splice a block of forms into
902 ;;; some enclosing form with `(progn ,@body), thus retaining the
903 ;;; EQness of the conses.
904 (defun maybe-instrument-progn-like (start forms form)
905 (or (when (and *allow-instrumenting*
906 (not (get-source-path form)))
907 (let ((*current-path* (get-source-path forms)))
909 (instrument-coverage start nil form))))
912 (defun record-code-coverage (info cc)
913 (setf (gethash info *code-coverage-info*) cc))
915 (defun clear-code-coverage ()
916 (clrhash *code-coverage-info*))
918 (defun reset-code-coverage ()
919 (maphash (lambda (info cc)
920 (declare (ignore info))
921 (dolist (cc-entry cc)
922 (setf (cdr cc-entry) +code-coverage-unmarked+)))
923 *code-coverage-info*))
925 (defun code-coverage-record-marked (record)
926 (aver (consp record))
928 ((#.+code-coverage-unmarked+) nil)
932 ;;;; converting combinations
934 ;;; Does this form look like something that we should add single-stepping
935 ;;; instrumentation for?
936 (defun step-form-p (form)
937 (flet ((step-symbol-p (symbol)
938 (not (member (symbol-package symbol)
940 ;; KLUDGE: packages we're not interested in
942 (mapcar #'find-package '(sb!c sb!int sb!impl
943 sb!kernel sb!pcl)))))))
944 (and *allow-instrumenting*
945 (policy *lexenv* (= insert-step-conditions 3))
948 (step-symbol-p (car form)))))
950 ;;; Convert a function call where the function FUN is a LEAF. FORM is
951 ;;; the source for the call. We return the COMBINATION node so that
952 ;;; the caller can poke at it if it wants to.
953 (declaim (ftype (sfunction (ctran ctran (or lvar null) list leaf) combination)
954 ir1-convert-combination))
955 (defun ir1-convert-combination (start next result form fun)
956 (let ((ctran (make-ctran))
957 (fun-lvar (make-lvar)))
958 (ir1-convert start ctran fun-lvar `(the (or function symbol) ,fun))
960 (ir1-convert-combination-args fun-lvar ctran next result
962 (when (step-form-p form)
963 ;; Store a string representation of the form in the
964 ;; combination node. This will let the IR2 translator know
965 ;; that we want stepper instrumentation for this node. The
966 ;; string will be stored in the debug-info by DUMP-1-LOCATION.
967 (setf (combination-step-info combination)
968 (let ((*print-pretty* t)
970 (*print-readably* nil))
971 (prin1-to-string form))))
974 ;;; Convert the arguments to a call and make the COMBINATION
975 ;;; node. FUN-LVAR yields the function to call. ARGS is the list of
976 ;;; arguments for the call, which defaults to the cdr of source. We
977 ;;; return the COMBINATION node.
978 (defun ir1-convert-combination-args (fun-lvar start next result args)
979 (declare (type ctran start next)
981 (type (or lvar null) result)
983 (let ((node (make-combination fun-lvar)))
984 (setf (lvar-dest fun-lvar) node)
985 (collect ((arg-lvars))
986 (let ((this-start start)
990 (maybe-instrument-progn-like this-start forms arg))
991 (setf forms (cdr forms))
992 (let ((this-ctran (make-ctran))
993 (this-lvar (make-lvar node)))
994 (ir1-convert this-start this-ctran this-lvar arg)
995 (setq this-start this-ctran)
996 (arg-lvars this-lvar)))
997 (link-node-to-previous-ctran node this-start)
998 (use-continuation node next result)
999 (setf (combination-args node) (arg-lvars))))
1002 ;;; Convert a call to a global function. If not :NOTINLINE, then we do
1003 ;;; source transforms and try out any inline expansion. If there is no
1004 ;;; expansion, but is :INLINE, then give an efficiency note (unless a
1005 ;;; known function which will quite possibly be open-coded.) Next, we
1006 ;;; go to ok-combination conversion.
1007 (defun ir1-convert-srctran (start next result var form)
1008 (declare (type ctran start next) (type (or lvar null) result)
1009 (type global-var var))
1010 (let ((inlinep (when (defined-fun-p var)
1011 (defined-fun-inlinep var))))
1012 (if (eq inlinep :notinline)
1013 (ir1-convert-combination start next result form var)
1014 (let ((transform (info :function
1016 (leaf-source-name var))))
1018 (multiple-value-bind (transformed pass) (funcall transform form)
1020 (ir1-convert-maybe-predicate start next result form var)
1021 (ir1-convert start next result transformed)))
1022 (ir1-convert-maybe-predicate start next result form var))))))
1024 ;;; KLUDGE: If we insert a synthetic IF for a function with the PREDICATE
1025 ;;; attribute, don't generate any branch coverage instrumentation for it.
1026 (defvar *instrument-if-for-code-coverage* t)
1028 ;;; If the function has the PREDICATE attribute, and the RESULT's DEST
1029 ;;; isn't an IF, then we convert (IF <form> T NIL), ensuring that a
1030 ;;; predicate always appears in a conditional context.
1032 ;;; If the function isn't a predicate, then we call
1033 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
1034 (defun ir1-convert-maybe-predicate (start next result form var)
1035 (declare (type ctran start next)
1036 (type (or lvar null) result)
1038 (type global-var var))
1039 (let ((info (info :function :info (leaf-source-name var))))
1041 (ir1-attributep (fun-info-attributes info) predicate)
1042 (not (if-p (and result (lvar-dest result)))))
1043 (let ((*instrument-if-for-code-coverage* nil))
1044 (ir1-convert start next result `(if ,form t nil)))
1045 (ir1-convert-combination-checking-type start next result form var))))
1047 ;;; Actually really convert a global function call that we are allowed
1050 ;;; If we know the function type of the function, then we check the
1051 ;;; call for syntactic legality with respect to the declared function
1052 ;;; type. If it is impossible to determine whether the call is correct
1053 ;;; due to non-constant keywords, then we give up, marking the call as
1054 ;;; :FULL to inhibit further error messages. We return true when the
1057 ;;; If the call is legal, we also propagate type assertions from the
1058 ;;; function type to the arg and result lvars. We do this now so that
1059 ;;; IR1 optimize doesn't have to redundantly do the check later so
1060 ;;; that it can do the type propagation.
1061 (defun ir1-convert-combination-checking-type (start next result form var)
1062 (declare (type ctran start next) (type (or lvar null) result)
1065 (let* ((node (ir1-convert-combination start next result form var))
1066 (fun-lvar (basic-combination-fun node))
1067 (type (leaf-type var))
1068 (defined-type (leaf-defined-type var)))
1069 (when (validate-call-type node type defined-type t)
1070 (setf (lvar-%derived-type fun-lvar)
1071 (make-single-value-type type))
1072 (setf (lvar-reoptimize fun-lvar) nil)))
1075 ;;; Convert a call to a local function, or if the function has already
1076 ;;; been LET converted, then throw FUNCTIONAL to
1077 ;;; LOCALL-ALREADY-LET-CONVERTED. The THROW should only happen when we
1078 ;;; are converting inline expansions for local functions during
1080 (defun ir1-convert-local-combination (start next result form functional)
1081 (assure-functional-live-p functional)
1082 (ir1-convert-combination start next result
1084 (maybe-reanalyze-functional functional)))
1088 ;;; Given a list of LAMBDA-VARs and a variable name, return the
1089 ;;; LAMBDA-VAR for that name, or NIL if it isn't found. We return the
1090 ;;; *last* variable with that name, since LET* bindings may be
1091 ;;; duplicated, and declarations always apply to the last.
1092 (declaim (ftype (sfunction (list symbol) (or lambda-var list))
1094 (defun find-in-bindings (vars name)
1098 (when (eq (leaf-source-name var) name)
1100 (let ((info (lambda-var-arg-info var)))
1102 (let ((supplied-p (arg-info-supplied-p info)))
1103 (when (and supplied-p
1104 (eq (leaf-source-name supplied-p) name))
1105 (setq found supplied-p))))))
1106 ((and (consp var) (eq (car var) name))
1107 (setf found (cdr var)))))
1110 ;;; Called by PROCESS-DECLS to deal with a variable type declaration.
1111 ;;; If a LAMBDA-VAR being bound, we intersect the type with the var's
1112 ;;; type, otherwise we add a type restriction on the var. If a symbol
1113 ;;; macro, we just wrap a THE around the expansion.
1114 (defun process-type-decl (decl res vars context)
1115 (declare (list decl vars) (type lexenv res))
1116 (let ((type (compiler-specifier-type (first decl))))
1117 (collect ((restr nil cons)
1118 (new-vars nil cons))
1119 (dolist (var-name (rest decl))
1120 (when (boundp var-name)
1121 (program-assert-symbol-home-package-unlocked
1122 context var-name "declaring the type of ~A"))
1123 (let* ((bound-var (find-in-bindings vars var-name))
1125 (lexenv-find var-name vars)
1126 (find-free-var var-name))))
1130 ((process-var (var bound-var)
1131 (let* ((old-type (or (lexenv-find var type-restrictions)
1133 (int (if (or (fun-type-p type)
1134 (fun-type-p old-type))
1136 (type-approx-intersection2
1138 (cond ((eq int *empty-type*)
1139 (unless (policy *lexenv* (= inhibit-warnings 3))
1143 "The type declarations ~S and ~S for ~S conflict."
1146 (type-specifier old-type)
1147 (type-specifier type)
1149 (bound-var (setf (leaf-type bound-var) int))
1151 (restr (cons var int)))))))
1152 (process-var var bound-var)
1153 (awhen (and (lambda-var-p var)
1154 (lambda-var-specvar var))
1155 (process-var it nil))))
1157 ;; FIXME: non-ANSI weirdness
1158 (aver (eq (car var) 'macro))
1159 (new-vars `(,var-name . (macro . (the ,(first decl)
1163 "~S is an alien variable, so its type can't be declared."
1166 (if (or (restr) (new-vars))
1167 (make-lexenv :default res
1168 :type-restrictions (restr)
1172 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
1173 ;;; declarations for function variables. In addition to allowing
1174 ;;; declarations for functions being bound, we must also deal with
1175 ;;; declarations that constrain the type of lexically apparent
1177 (defun process-ftype-decl (spec res names fvars context)
1178 (declare (type list names fvars)
1180 (let ((type (compiler-specifier-type spec)))
1181 (collect ((res nil cons))
1182 (dolist (name names)
1183 (when (fboundp name)
1184 (program-assert-symbol-home-package-unlocked
1185 context name "declaring the ftype of ~A"))
1186 (let ((found (find name fvars :key #'leaf-source-name :test #'equal)))
1189 (setf (leaf-type found) type)
1190 (assert-definition-type found type
1191 :unwinnage-fun #'compiler-notify
1192 :where "FTYPE declaration"))
1194 (res (cons (find-lexically-apparent-fun
1195 name "in a function type declaration")
1198 (make-lexenv :default res :type-restrictions (res))
1201 ;;; Process a special declaration, returning a new LEXENV. A non-bound
1202 ;;; special declaration is instantiated by throwing a special variable
1203 ;;; into the variables if BINDING-FORM-P is NIL, or otherwise into
1204 ;;; *POST-BINDING-VARIABLE-LEXENV*.
1205 (defun process-special-decl (spec res vars binding-form-p context)
1206 (declare (list spec vars) (type lexenv res))
1207 (collect ((new-venv nil cons))
1208 (dolist (name (cdr spec))
1209 (program-assert-symbol-home-package-unlocked
1210 context name "declaring ~A special")
1211 (let ((var (find-in-bindings vars name)))
1214 (aver (eq (car var) 'macro))
1216 "~S is a symbol-macro and thus can't be declared special."
1219 (when (lambda-var-ignorep var)
1220 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1221 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1222 (compiler-style-warn
1223 "The ignored variable ~S is being declared special."
1225 (setf (lambda-var-specvar var)
1226 (specvar-for-binding name)))
1228 (unless (or (assoc name (new-venv) :test #'eq))
1229 (new-venv (cons name (specvar-for-binding name))))))))
1230 (cond (binding-form-p
1231 (setf *post-binding-variable-lexenv*
1232 (append (new-venv) *post-binding-variable-lexenv*))
1235 (make-lexenv :default res :vars (new-venv)))
1239 ;;; Return a DEFINED-FUN which copies a GLOBAL-VAR but for its INLINEP
1240 ;;; (and TYPE if notinline), plus type-restrictions from the lexenv.
1241 (defun make-new-inlinep (var inlinep local-type)
1242 (declare (type global-var var) (type inlinep inlinep))
1243 (let* ((type (if (and (eq inlinep :notinline)
1244 (not (eq (leaf-where-from var) :declared)))
1245 (specifier-type 'function)
1247 (res (make-defined-fun
1248 :%source-name (leaf-source-name var)
1249 :where-from (leaf-where-from var)
1250 :type (if local-type
1251 (type-intersection local-type type)
1254 (when (defined-fun-p var)
1255 (setf (defined-fun-inline-expansion res)
1256 (defined-fun-inline-expansion var))
1257 (setf (defined-fun-functional res)
1258 (defined-fun-functional var)))
1259 ;; FIXME: Is this really right? Needs we not set the FUNCTIONAL
1260 ;; to the original global-var?
1263 ;;; Parse an inline/notinline declaration. If it's a local function we're
1264 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
1265 (defun process-inline-decl (spec res fvars)
1266 (let ((sense (cdr (assoc (first spec) *inlinep-translations* :test #'eq)))
1268 (dolist (name (rest spec))
1269 (let ((fvar (find name fvars :key #'leaf-source-name :test #'equal)))
1271 (setf (functional-inlinep fvar) sense)
1272 (let ((found (find-lexically-apparent-fun
1273 name "in an inline or notinline declaration")))
1276 (when (policy *lexenv* (>= speed inhibit-warnings))
1277 (compiler-notify "ignoring ~A declaration not at ~
1278 definition of local function:~% ~S"
1282 (cdr (assoc found (lexenv-type-restrictions res)))))
1283 (push (cons name (make-new-inlinep found sense type))
1286 (make-lexenv :default res :funs new-fenv)
1289 ;;; like FIND-IN-BINDINGS, but looks for #'FOO in the FVARS
1290 (defun find-in-bindings-or-fbindings (name vars fvars)
1291 (declare (list vars fvars))
1293 (destructuring-bind (wot fn-name) name
1294 (unless (eq wot 'function)
1295 (compiler-error "The function or variable name ~S is unrecognizable."
1297 (find fn-name fvars :key #'leaf-source-name :test #'equal))
1298 (find-in-bindings vars name)))
1300 ;;; Process an ignore/ignorable declaration, checking for various losing
1302 (defun process-ignore-decl (spec vars fvars)
1303 (declare (list spec vars fvars))
1304 (dolist (name (rest spec))
1305 (let ((var (find-in-bindings-or-fbindings name vars fvars)))
1308 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1309 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1310 (compiler-style-warn "declaring unknown variable ~S to be ignored"
1312 ;; FIXME: This special case looks like non-ANSI weirdness.
1313 ((and (consp var) (eq (car var) 'macro))
1314 ;; Just ignore the IGNORE decl.
1317 (setf (leaf-ever-used var) t))
1318 ((and (lambda-var-specvar var) (eq (first spec) 'ignore))
1319 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1320 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1321 (compiler-style-warn "declaring special variable ~S to be ignored"
1323 ((eq (first spec) 'ignorable)
1324 (setf (leaf-ever-used var) t))
1326 (setf (lambda-var-ignorep var) t)))))
1329 (defun process-dx-decl (names vars fvars kind)
1330 (flet ((maybe-notify (control &rest args)
1331 (when (policy *lexenv* (> speed inhibit-warnings))
1332 (apply #'compiler-notify control args))))
1333 (let ((dx (cond ((eq 'truly-dynamic-extent kind)
1335 ((and (eq 'dynamic-extent kind)
1336 *stack-allocate-dynamic-extent*)
1339 (dolist (name names)
1342 (let* ((bound-var (find-in-bindings vars name))
1344 (lexenv-find name vars)
1345 (find-free-var name))))
1349 (setf (leaf-dynamic-extent var) dx)
1351 "ignoring DYNAMIC-EXTENT declaration for free ~S"
1354 (compiler-error "DYNAMIC-EXTENT on symbol-macro: ~S" name))
1356 (compiler-error "DYNAMIC-EXTENT on heap-alien-info: ~S"
1359 (eq (car name) 'function)
1361 (valid-function-name-p (cadr name)))
1362 (let* ((fname (cadr name))
1363 (bound-fun (find fname fvars
1364 :key #'leaf-source-name
1366 (etypecase bound-fun
1368 #!+stack-allocatable-closures
1369 (setf (leaf-dynamic-extent bound-fun) dx)
1370 #!-stack-allocatable-closures
1372 "ignoring DYNAMIC-EXTENT declaration on a function ~S ~
1373 (not supported on this platform)." fname))
1375 (compiler-error "DYNAMIC-EXTENT on macro: ~S" fname))
1378 "ignoring DYNAMIC-EXTENT declaration for free ~S"
1380 (t (compiler-error "DYNAMIC-EXTENT on a weird thing: ~S" name))))
1381 (maybe-notify "ignoring DYNAMIC-EXTENT declarations for ~S" names)))))
1383 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
1384 ;;; go away, I think.
1385 (defvar *suppress-values-declaration* nil
1387 "If true, processing of the VALUES declaration is inhibited.")
1389 ;;; Process a single declaration spec, augmenting the specified LEXENV
1390 ;;; RES. Return RES and result type. VARS and FVARS are as described
1392 (defun process-1-decl (raw-spec res vars fvars binding-form-p context)
1393 (declare (type list raw-spec vars fvars))
1394 (declare (type lexenv res))
1395 (let ((spec (canonized-decl-spec raw-spec))
1396 (result-type *wild-type*))
1399 (special (process-special-decl spec res vars binding-form-p context))
1402 (compiler-error "no type specified in FTYPE declaration: ~S" spec))
1403 (process-ftype-decl (second spec) res (cddr spec) fvars context))
1404 ((inline notinline maybe-inline)
1405 (process-inline-decl spec res fvars))
1407 (process-ignore-decl spec vars fvars)
1412 :policy (process-optimize-decl spec (lexenv-policy res))))
1416 :handled-conditions (process-muffle-conditions-decl
1417 spec (lexenv-handled-conditions res))))
1418 (unmuffle-conditions
1421 :handled-conditions (process-unmuffle-conditions-decl
1422 spec (lexenv-handled-conditions res))))
1424 (process-type-decl (cdr spec) res vars context))
1426 (unless *suppress-values-declaration*
1427 (let ((types (cdr spec)))
1429 (compiler-values-specifier-type
1430 (if (singleton-p types)
1432 `(values ,@types)))))
1434 ((dynamic-extent truly-dynamic-extent)
1435 (process-dx-decl (cdr spec) vars fvars (first spec))
1437 ((disable-package-locks enable-package-locks)
1440 :disabled-package-locks (process-package-lock-decl
1441 spec (lexenv-disabled-package-locks res))))
1443 (unless (info :declaration :recognized (first spec))
1444 (compiler-warn "unrecognized declaration ~S" raw-spec))
1448 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1449 ;;; and FUNCTIONAL structures which are being bound. In addition to
1450 ;;; filling in slots in the leaf structures, we return a new LEXENV,
1451 ;;; which reflects pervasive special and function type declarations,
1452 ;;; (NOT)INLINE declarations and OPTIMIZE declarations, and type of
1453 ;;; VALUES declarations. If BINDING-FORM-P is true, the third return
1454 ;;; value is a list of VARs that should not apply to the lexenv of the
1455 ;;; initialization forms for the bindings, but should apply to the body.
1457 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1459 (defun process-decls (decls vars fvars &key
1460 (lexenv *lexenv*) (binding-form-p nil) (context :compile))
1461 (declare (list decls vars fvars))
1462 (let ((result-type *wild-type*)
1463 (*post-binding-variable-lexenv* nil))
1464 (dolist (decl decls)
1465 (dolist (spec (rest decl))
1466 (unless (consp spec)
1467 (compiler-error "malformed declaration specifier ~S in ~S" spec decl))
1468 (multiple-value-bind (new-env new-result-type)
1469 (process-1-decl spec lexenv vars fvars binding-form-p context)
1470 (setq lexenv new-env)
1471 (unless (eq new-result-type *wild-type*)
1473 (values-type-intersection result-type new-result-type))))))
1474 (values lexenv result-type *post-binding-variable-lexenv*)))
1476 (defun %processing-decls (decls vars fvars ctran lvar binding-form-p fun)
1477 (multiple-value-bind (*lexenv* result-type post-binding-lexenv)
1478 (process-decls decls vars fvars :binding-form-p binding-form-p)
1479 (cond ((eq result-type *wild-type*)
1480 (funcall fun ctran lvar post-binding-lexenv))
1482 (let ((value-ctran (make-ctran))
1483 (value-lvar (make-lvar)))
1484 (multiple-value-prog1
1485 (funcall fun value-ctran value-lvar post-binding-lexenv)
1486 (let ((cast (make-cast value-lvar result-type
1487 (lexenv-policy *lexenv*))))
1488 (link-node-to-previous-ctran cast value-ctran)
1489 (setf (lvar-dest value-lvar) cast)
1490 (use-continuation cast ctran lvar))))))))
1491 (defmacro processing-decls ((decls vars fvars ctran lvar
1492 &optional post-binding-lexenv)
1494 (check-type ctran symbol)
1495 (check-type lvar symbol)
1496 (let ((post-binding-lexenv-p (not (null post-binding-lexenv)))
1497 (post-binding-lexenv (or post-binding-lexenv (gensym))))
1498 `(%processing-decls ,decls ,vars ,fvars ,ctran ,lvar
1499 ,post-binding-lexenv-p
1500 (lambda (,ctran ,lvar ,post-binding-lexenv)
1501 (declare (ignorable ,post-binding-lexenv))
1504 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1505 ;;; declaration. If there is a global variable of that name, then
1506 ;;; check that it isn't a constant and return it. Otherwise, create an
1507 ;;; anonymous GLOBAL-VAR.
1508 (defun specvar-for-binding (name)
1509 (cond ((not (eq (info :variable :where-from name) :assumed))
1510 (let ((found (find-free-var name)))
1511 (when (heap-alien-info-p found)
1513 "~S is an alien variable and so can't be declared special."
1515 (unless (global-var-p found)
1517 "~S is a constant and so can't be declared special."
1521 (make-global-var :kind :special
1523 :where-from :declared))))