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 ;;; Return a GLOBAL-VAR structure usable for referencing the global
92 (defun find-global-fun (name latep)
93 (unless (info :function :kind name)
94 (setf (info :function :kind name) :function)
95 (setf (info :function :where-from name) :assumed))
96 (let ((where (info :function :where-from name)))
97 (when (and (eq where :assumed)
98 ;; In the ordinary target Lisp, it's silly to report
99 ;; undefinedness when the function is defined in the
100 ;; running Lisp. But at cross-compile time, the current
101 ;; definedness of a function is irrelevant to the
102 ;; definedness at runtime, which is what matters.
103 #-sb-xc-host (not (fboundp name))
104 ;; LATEP is true when the user has indicated that
105 ;; late-late binding is desired by using eg. a quoted
106 ;; symbol -- in which case it makes little sense to
107 ;; complain about undefined functions.
109 (note-undefined-reference name :function))
111 :kind :global-function
113 :type (if (and (not latep)
114 (or *derive-function-types*
116 (and (member name *fun-names-in-this-file*
118 (not (fun-lexically-notinline-p name)))))
119 (info :function :type name)
120 (specifier-type 'function))
123 ;;; Has the *FREE-FUNS* entry FREE-FUN become invalid?
125 ;;; In CMU CL, the answer was implicitly always true, so this
126 ;;; predicate didn't exist.
128 ;;; This predicate was added to fix bug 138 in SBCL. In some obscure
129 ;;; circumstances, it was possible for a *FREE-FUNS* entry to contain a
130 ;;; DEFINED-FUN whose DEFINED-FUN-FUNCTIONAL object contained IR1
131 ;;; stuff (NODEs, BLOCKs...) referring to an already compiled (aka
132 ;;; "dead") component. When this IR1 stuff was reused in a new
133 ;;; component, under further obscure circumstances it could be used by
134 ;;; WITH-IR1-ENVIRONMENT-FROM-NODE to generate a binding for
135 ;;; *CURRENT-COMPONENT*. At that point things got all confused, since
136 ;;; IR1 conversion was sending code to a component which had already
137 ;;; been compiled and would never be compiled again.
138 (defun invalid-free-fun-p (free-fun)
139 ;; There might be other reasons that *FREE-FUN* entries could
140 ;; become invalid, but the only one we've been bitten by so far
141 ;; (sbcl-0.pre7.118) is this one:
142 (and (defined-fun-p free-fun)
143 (let ((functional (defined-fun-functional free-fun)))
145 (eql (functional-kind functional) :deleted))
146 (and (lambda-p functional)
148 ;; (The main reason for this first test is to bail
149 ;; out early in cases where the LAMBDA-COMPONENT
150 ;; call in the second test would fail because links
151 ;; it needs are uninitialized or invalid.)
153 ;; If the BIND node for this LAMBDA is null, then
154 ;; according to the slot comments, the LAMBDA has
155 ;; been deleted or its call has been deleted. In
156 ;; that case, it seems rather questionable to reuse
157 ;; it, and certainly it shouldn't be necessary to
158 ;; reuse it, so we cheerfully declare it invalid.
159 (null (lambda-bind functional))
160 ;; If this IR1 stuff belongs to a dead component,
161 ;; then we can't reuse it without getting into
162 ;; bizarre confusion.
163 (eql (component-info (lambda-component functional))
166 ;;; If NAME already has a valid entry in *FREE-FUNS*, then return
167 ;;; the value. Otherwise, make a new GLOBAL-VAR using information from
168 ;;; the global environment and enter it in *FREE-FUNS*. If NAME
169 ;;; names a macro or special form, then we error out using the
170 ;;; supplied context which indicates what we were trying to do that
171 ;;; demanded a function.
172 (declaim (ftype (sfunction (t string) global-var) find-free-fun))
173 (defun find-free-fun (name context)
174 (or (let ((old-free-fun (gethash name *free-funs*)))
175 (and (not (invalid-free-fun-p old-free-fun))
177 (ecase (info :function :kind name)
178 ;; FIXME: The :MACRO and :SPECIAL-FORM cases could be merged.
180 (compiler-error "The macro name ~S was found ~A." name context))
182 (compiler-error "The special form name ~S was found ~A."
186 (check-fun-name name)
187 (note-if-setf-fun-and-macro name)
188 (let ((expansion (fun-name-inline-expansion name))
189 (inlinep (info :function :inlinep name)))
190 (setf (gethash name *free-funs*)
191 (if (or expansion inlinep)
194 :inline-expansion expansion
196 :where-from (info :function :where-from name)
197 :type (if (eq inlinep :notinline)
198 (specifier-type 'function)
199 (info :function :type name)))
200 (find-global-fun name nil))))))))
202 ;;; Return the LEAF structure for the lexically apparent function
203 ;;; definition of NAME.
204 (declaim (ftype (sfunction (t string) leaf) find-lexically-apparent-fun))
205 (defun find-lexically-apparent-fun (name context)
206 (let ((var (lexenv-find name funs :test #'equal)))
209 (aver (and (consp var) (eq (car var) 'macro)))
210 (compiler-error "found macro name ~S ~A" name context))
213 (find-free-fun name context)))))
215 ;;; Return the LEAF node for a global variable reference to NAME. If
216 ;;; NAME is already entered in *FREE-VARS*, then we just return the
217 ;;; corresponding value. Otherwise, we make a new leaf using
218 ;;; information from the global environment and enter it in
219 ;;; *FREE-VARS*. If the variable is unknown, then we emit a warning.
220 (declaim (ftype (sfunction (t) (or leaf cons heap-alien-info)) find-free-var))
221 (defun find-free-var (name)
222 (unless (symbolp name)
223 (compiler-error "Variable name is not a symbol: ~S." name))
224 (or (gethash name *free-vars*)
225 (let ((kind (info :variable :kind name))
226 (type (info :variable :type name))
227 (where-from (info :variable :where-from name)))
228 (when (and (eq where-from :assumed) (eq kind :global))
229 (note-undefined-reference name :variable))
230 (setf (gethash name *free-vars*)
233 (info :variable :alien-info name))
234 ;; FIXME: The return value in this case should really be
235 ;; of type SB!C::LEAF. I don't feel too badly about it,
236 ;; because the MACRO idiom is scattered throughout this
237 ;; file, but it should be cleaned up so we're not
238 ;; throwing random conses around. --njf 2002-03-23
240 (let ((expansion (info :variable :macro-expansion name))
241 (type (type-specifier (info :variable :type name))))
242 `(macro . (the ,type ,expansion))))
244 (let ((value (info :variable :constant-value name)))
245 (make-constant :value value
247 :type (ctype-of value)
248 :where-from where-from)))
250 (make-global-var :kind kind
253 :where-from where-from)))))))
255 ;;; Grovel over CONSTANT checking for any sub-parts that need to be
256 ;;; processed with MAKE-LOAD-FORM. We have to be careful, because
257 ;;; CONSTANT might be circular. We also check that the constant (and
258 ;;; any subparts) are dumpable at all.
259 (eval-when (:compile-toplevel :load-toplevel :execute)
260 ;; The EVAL-WHEN is necessary for #.(1+ LIST-TO-HASH-TABLE-THRESHOLD)
261 ;; below. -- AL 20010227
262 (def!constant list-to-hash-table-threshold 32))
263 (defun maybe-emit-make-load-forms (constant)
264 (let ((things-processed nil)
266 ;; FIXME: Does this LIST-or-HASH-TABLE messiness give much benefit?
267 (declare (type (or list hash-table) things-processed)
268 (type (integer 0 #.(1+ list-to-hash-table-threshold)) count)
270 (labels ((grovel (value)
271 ;; Unless VALUE is an object which which obviously
272 ;; can't contain other objects
274 '(or #-sb-xc-host unboxed-array
275 #+sb-xc-host (simple-array (unsigned-byte 8) (*))
280 (etypecase things-processed
282 (when (member value things-processed :test #'eq)
283 (return-from grovel nil))
284 (push value things-processed)
286 (when (> count list-to-hash-table-threshold)
287 (let ((things things-processed))
288 (setf things-processed
289 (make-hash-table :test 'eq))
290 (dolist (thing things)
291 (setf (gethash thing things-processed) t)))))
293 (when (gethash value things-processed)
294 (return-from grovel nil))
295 (setf (gethash value things-processed) t)))
299 (grovel (cdr value)))
301 (dotimes (i (length value))
302 (grovel (svref value i))))
304 (dotimes (i (length value))
305 (grovel (aref value i))))
307 ;; Even though the (ARRAY T) branch does the exact
308 ;; same thing as this branch we do this separately
309 ;; so that the compiler can use faster versions of
310 ;; array-total-size and row-major-aref.
311 (dotimes (i (array-total-size value))
312 (grovel (row-major-aref value i))))
314 (dotimes (i (array-total-size value))
315 (grovel (row-major-aref value i))))
316 (;; In the target SBCL, we can dump any instance,
317 ;; but in the cross-compilation host,
318 ;; %INSTANCE-FOO functions don't work on general
319 ;; instances, only on STRUCTURE!OBJECTs.
320 #+sb-xc-host structure!object
321 #-sb-xc-host instance
322 (when (emit-make-load-form value)
323 (dotimes (i (- (%instance-length value)
325 #-sb-xc-host (layout-n-untagged-slots
326 (%instance-ref value 0))))
327 (grovel (%instance-ref value i)))))
330 "Objects of type ~S can't be dumped into fasl files."
331 (type-of value)))))))
335 ;;;; some flow-graph hacking utilities
337 ;;; This function sets up the back link between the node and the
338 ;;; ctran which continues at it.
339 (defun link-node-to-previous-ctran (node ctran)
340 (declare (type node node) (type ctran ctran))
341 (aver (not (ctran-next ctran)))
342 (setf (ctran-next ctran) node)
343 (setf (node-prev node) ctran))
345 ;;; This function is used to set the ctran for a node, and thus
346 ;;; determine what is evaluated next. If the ctran has no block, then
347 ;;; we make it be in the block that the node is in. If the ctran heads
348 ;;; its block, we end our block and link it to that block.
349 #!-sb-fluid (declaim (inline use-ctran))
350 (defun use-ctran (node ctran)
351 (declare (type node node) (type ctran ctran))
352 (if (eq (ctran-kind ctran) :unused)
353 (let ((node-block (ctran-block (node-prev node))))
354 (setf (ctran-block ctran) node-block)
355 (setf (ctran-kind ctran) :inside-block)
356 (setf (ctran-use ctran) node)
357 (setf (node-next node) ctran))
358 (%use-ctran node ctran)))
359 (defun %use-ctran (node ctran)
360 (declare (type node node) (type ctran ctran) (inline member))
361 (let ((block (ctran-block ctran))
362 (node-block (ctran-block (node-prev node))))
363 (aver (eq (ctran-kind ctran) :block-start))
364 (when (block-last node-block)
365 (error "~S has already ended." node-block))
366 (setf (block-last node-block) node)
367 (when (block-succ node-block)
368 (error "~S already has successors." node-block))
369 (setf (block-succ node-block) (list block))
370 (when (memq node-block (block-pred block))
371 (error "~S is already a predecessor of ~S." node-block block))
372 (push node-block (block-pred block))))
374 ;;; This function is used to set the ctran for a node, and thus
375 ;;; determine what receives the value.
376 (defun use-lvar (node lvar)
377 (declare (type valued-node node) (type (or lvar null) lvar))
378 (aver (not (node-lvar node)))
380 (setf (node-lvar node) lvar)
381 (cond ((null (lvar-uses lvar))
382 (setf (lvar-uses lvar) node))
383 ((listp (lvar-uses lvar))
384 (aver (not (memq node (lvar-uses lvar))))
385 (push node (lvar-uses lvar)))
387 (aver (neq node (lvar-uses lvar)))
388 (setf (lvar-uses lvar) (list node (lvar-uses lvar)))))
389 (reoptimize-lvar lvar)))
391 #!-sb-fluid(declaim (inline use-continuation))
392 (defun use-continuation (node ctran lvar)
393 (use-ctran node ctran)
394 (use-lvar node lvar))
396 ;;;; exported functions
398 ;;; This function takes a form and the top level form number for that
399 ;;; form, and returns a lambda representing the translation of that
400 ;;; form in the current global environment. The returned lambda is a
401 ;;; top level lambda that can be called to cause evaluation of the
402 ;;; forms. This lambda is in the initial component. If FOR-VALUE is T,
403 ;;; then the value of the form is returned from the function,
404 ;;; otherwise NIL is returned.
406 ;;; This function may have arbitrary effects on the global environment
407 ;;; due to processing of EVAL-WHENs. All syntax error checking is
408 ;;; done, with erroneous forms being replaced by a proxy which signals
409 ;;; an error if it is evaluated. Warnings about possibly inconsistent
410 ;;; or illegal changes to the global environment will also be given.
412 ;;; We make the initial component and convert the form in a PROGN (and
413 ;;; an optional NIL tacked on the end.) We then return the lambda. We
414 ;;; bind all of our state variables here, rather than relying on the
415 ;;; global value (if any) so that IR1 conversion will be reentrant.
416 ;;; This is necessary for EVAL-WHEN processing, etc.
418 ;;; The hashtables used to hold global namespace info must be
419 ;;; reallocated elsewhere. Note also that *LEXENV* is not bound, so
420 ;;; that local macro definitions can be introduced by enclosing code.
421 (defun ir1-toplevel (form path for-value &optional (allow-instrumenting t))
422 (declare (list path))
423 (let* ((*current-path* path)
424 (component (make-empty-component))
425 (*current-component* component)
426 (*allow-instrumenting* allow-instrumenting))
427 (setf (component-name component) 'initial-component)
428 (setf (component-kind component) :initial)
429 (let* ((forms (if for-value `(,form) `(,form nil)))
430 (res (ir1-convert-lambda-body
432 :debug-name (debug-name 'top-level-form form))))
433 (setf (functional-entry-fun res) res
434 (functional-arg-documentation res) ()
435 (functional-kind res) :toplevel)
438 ;;; This function is called on freshly read forms to record the
439 ;;; initial location of each form (and subform.) Form is the form to
440 ;;; find the paths in, and TLF-NUM is the top level form number of the
441 ;;; truly top level form.
443 ;;; This gets a bit interesting when the source code is circular. This
444 ;;; can (reasonably?) happen in the case of circular list constants.
445 (defun find-source-paths (form tlf-num)
446 (declare (type index tlf-num))
447 (let ((*current-form-number* 0))
448 (sub-find-source-paths form (list tlf-num)))
450 (defun sub-find-source-paths (form path)
451 (unless (get-source-path form)
452 (note-source-path form path)
453 (incf *current-form-number*)
457 (declare (fixnum pos))
460 (when (atom subform) (return))
461 (let ((fm (car subform)))
463 ;; If it's a cons, recurse
464 (sub-find-source-paths fm (cons pos path))
465 ;; Otherwise store the containing form. It's
466 ;; not perfect, but better than nothing.
468 (note-source-path subform pos path)))
470 (setq subform (cdr subform))
471 (when (eq subform trail) (return)))))
475 (setq trail (cdr trail)))))))
477 ;;;; IR1-CONVERT, macroexpansion and special form dispatching
479 (declaim (ftype (sfunction (ctran ctran (or lvar null) t) (values))
481 (macrolet (;; Bind *COMPILER-ERROR-BAILOUT* to a function that throws
482 ;; out of the body and converts a condition signalling form
483 ;; instead. The source form is converted to a string since it
484 ;; may contain arbitrary non-externalizable objects.
485 (ir1-error-bailout ((start next result form) &body body)
486 (with-unique-names (skip condition)
488 (let ((,condition (catch 'ir1-error-abort
489 (let ((*compiler-error-bailout*
490 (lambda (&optional e)
491 (throw 'ir1-error-abort e))))
493 (return-from ,skip nil)))))
494 (ir1-convert ,start ,next ,result
495 (make-compiler-error-form ,condition
498 ;; Translate FORM into IR1. The code is inserted as the NEXT of the
499 ;; CTRAN START. RESULT is the LVAR which receives the value of the
500 ;; FORM to be translated. The translators call this function
501 ;; recursively to translate their subnodes.
503 ;; As a special hack to make life easier in the compiler, a LEAF
504 ;; IR1-converts into a reference to that LEAF structure. This allows
505 ;; the creation using backquote of forms that contain leaf
506 ;; references, without having to introduce dummy names into the
508 (defun ir1-convert (start next result form)
509 (ir1-error-bailout (start next result form)
510 (let* ((*current-path* (or (get-source-path form)
511 (cons form *current-path*)))
512 (start (instrument-coverage start nil form)))
514 (cond ((and (symbolp form) (not (keywordp form)))
515 (ir1-convert-var start next result form))
517 (reference-leaf start next result form))
519 (reference-constant start next result form))))
521 (ir1-convert-functoid start next result form)))))
524 ;; Generate a reference to a manifest constant, creating a new leaf
525 ;; if necessary. If we are producing a fasl file, make sure that
526 ;; MAKE-LOAD-FORM gets used on any parts of the constant that it
528 (defun reference-constant (start next result value)
529 (declare (type ctran start next)
530 (type (or lvar null) result)
531 (inline find-constant))
532 (ir1-error-bailout (start next result value)
533 (when (producing-fasl-file)
534 (maybe-emit-make-load-forms value))
535 (let* ((leaf (find-constant value))
536 (res (make-ref leaf)))
537 (push res (leaf-refs leaf))
538 (link-node-to-previous-ctran res start)
539 (use-continuation res next result)))
542 ;;; Add FUNCTIONAL to the COMPONENT-REANALYZE-FUNCTIONALS, unless it's
543 ;;; some trivial type for which reanalysis is a trivial no-op, or
544 ;;; unless it doesn't belong in this component at all.
546 ;;; FUNCTIONAL is returned.
547 (defun maybe-reanalyze-functional (functional)
549 (aver (not (eql (functional-kind functional) :deleted))) ; bug 148
550 (aver-live-component *current-component*)
552 ;; When FUNCTIONAL is of a type for which reanalysis isn't a trivial
554 (when (typep functional '(or optional-dispatch clambda))
556 ;; When FUNCTIONAL knows its component
557 (when (lambda-p functional)
558 (aver (eql (lambda-component functional) *current-component*)))
561 (component-reanalyze-functionals *current-component*)))
565 ;;; Generate a REF node for LEAF, frobbing the LEAF structure as
566 ;;; needed. If LEAF represents a defined function which has already
567 ;;; been converted, and is not :NOTINLINE, then reference the
568 ;;; functional instead.
569 (defun reference-leaf (start next result leaf)
570 (declare (type ctran start next) (type (or lvar null) result) (type leaf leaf))
571 (when (functional-p leaf)
572 (assure-functional-live-p leaf))
573 (let* ((type (lexenv-find leaf type-restrictions))
574 (leaf (or (and (defined-fun-p leaf)
575 (not (eq (defined-fun-inlinep leaf)
577 (let ((functional (defined-fun-functional leaf)))
578 (when (and functional
579 (not (functional-kind functional))
580 ;; Bug MISC.320: ir1-transform
581 ;; can create a reference to a
582 ;; inline-expanded function,
583 ;; defined in another component.
584 (not (and (lambda-p functional)
585 (neq (lambda-component functional)
586 *current-component*))))
587 (maybe-reanalyze-functional functional))))
588 (when (and (lambda-p leaf)
589 (memq (functional-kind leaf)
591 (maybe-reanalyze-functional leaf))
593 (ref (make-ref leaf)))
594 (push ref (leaf-refs leaf))
595 (setf (leaf-ever-used leaf) t)
596 (link-node-to-previous-ctran ref start)
597 (cond (type (let* ((ref-ctran (make-ctran))
598 (ref-lvar (make-lvar))
599 (cast (make-cast ref-lvar
600 (make-single-value-type type)
601 (lexenv-policy *lexenv*))))
602 (setf (lvar-dest ref-lvar) cast)
603 (use-continuation ref ref-ctran ref-lvar)
604 (link-node-to-previous-ctran cast ref-ctran)
605 (use-continuation cast next result)))
606 (t (use-continuation ref next result)))))
608 ;;; Convert a reference to a symbolic constant or variable. If the
609 ;;; symbol is entered in the LEXENV-VARS we use that definition,
610 ;;; otherwise we find the current global definition. This is also
611 ;;; where we pick off symbol macro and alien variable references.
612 (defun ir1-convert-var (start next result name)
613 (declare (type ctran start next) (type (or lvar null) result) (symbol name))
614 (let ((var (or (lexenv-find name vars) (find-free-var name))))
615 (if (and (global-var-p var) (not result))
616 ;; KLUDGE: If the reference is dead, convert using SYMBOL-VALUE
617 ;; which is not flushable, so that unbound dead variables signal
618 ;; an error (bug 412).
619 (ir1-convert start next result `(symbol-value ',name))
622 (when (lambda-var-p var)
623 (let ((home (ctran-home-lambda-or-null start)))
625 (sset-adjoin var (lambda-calls-or-closes home))))
626 (when (lambda-var-ignorep var)
627 ;; (ANSI's specification for the IGNORE declaration requires
628 ;; that this be a STYLE-WARNING, not a full WARNING.)
630 (compiler-style-warn "reading an ignored variable: ~S" name)
631 ;; there's no need for us to accept ANSI's lameness when
632 ;; processing our own code, though.
634 (warn "reading an ignored variable: ~S" name)))
635 (reference-leaf start next result var))
637 (aver (eq (car var) 'macro))
638 ;; FIXME: [Free] type declarations. -- APD, 2002-01-26
639 (ir1-convert start next result (cdr var)))
641 (ir1-convert start next result `(%heap-alien ',var))))))
644 ;;; Find a compiler-macro for a form, taking FUNCALL into account.
645 (defun find-compiler-macro (opname form)
646 (if (eq opname 'funcall)
647 (let ((fun-form (cadr form)))
648 (cond ((and (consp fun-form) (eq 'function (car fun-form)))
649 (let ((real-fun (cadr fun-form)))
650 (if (legal-fun-name-p real-fun)
651 (values (sb!xc:compiler-macro-function real-fun *lexenv*)
654 ((sb!xc:constantp fun-form *lexenv*)
655 (let ((fun (constant-form-value fun-form *lexenv*)))
656 (if (legal-fun-name-p fun)
657 ;; CLHS tells us that local functions must shadow
658 ;; compiler-macro-functions, but since the call is
659 ;; through a name, we are obviously interested
660 ;; in the global function.
661 (values (sb!xc:compiler-macro-function fun nil) fun)
665 (if (legal-fun-name-p opname)
666 (values (sb!xc:compiler-macro-function opname *lexenv*) opname)
669 ;;; Picks of special forms and compiler-macro expansions, and hands
670 ;;; the rest to IR1-CONVERT-COMMON-FUNCTOID
671 (defun ir1-convert-functoid (start next result form)
672 (let* ((op (car form))
673 (translator (and (symbolp op) (info :function :ir1-convert op))))
675 (when (sb!xc:compiler-macro-function op *lexenv*)
676 (compiler-warn "ignoring compiler macro for special form"))
677 (funcall translator start next result form))
679 (multiple-value-bind (cmacro-fun cmacro-fun-name)
680 (find-compiler-macro op form)
682 ;; CLHS 3.2.2.1.3 specifies that NOTINLINE
683 ;; suppresses compiler-macros.
684 (not (fun-lexically-notinline-p cmacro-fun-name)))
685 (let ((res (careful-expand-macro cmacro-fun form)))
687 (ir1-convert-common-functoid start next result form
689 (ir1-convert start next result res)))
690 (ir1-convert-common-functoid start next result form op)))))))
692 ;;; Handles the "common" cases: any other forms except special forms
693 ;;; and compiler-macros.
694 (defun ir1-convert-common-functoid (start next result form op)
695 (cond ((or (symbolp op) (leaf-p op))
696 (let ((lexical-def (if (leaf-p op) op (lexenv-find op funs))))
697 (typecase lexical-def
699 (ir1-convert-global-functoid start next result form op))
701 (ir1-convert-local-combination start next result form
704 (ir1-convert-srctran start next result lexical-def form))
706 (aver (and (consp lexical-def) (eq (car lexical-def) 'macro)))
707 (ir1-convert start next result
708 (careful-expand-macro (cdr lexical-def) form))))))
709 ((or (atom op) (not (eq (car op) 'lambda)))
710 (compiler-error "illegal function call"))
712 ;; implicitly (LAMBDA ..) because the LAMBDA expression is
713 ;; the CAR of an executed form.
714 (ir1-convert-combination
715 start next result form
716 (ir1-convert-lambda op
717 :debug-name (debug-name 'inline-lambda op))))))
719 ;;; Convert anything that looks like a global function call.
720 (defun ir1-convert-global-functoid (start next result form fun)
721 (declare (type ctran start next) (type (or lvar null) result)
723 ;; FIXME: Couldn't all the INFO calls here be converted into
724 ;; standard CL functions, like MACRO-FUNCTION or something? And what
725 ;; happens with lexically-defined (MACROLET) macros here, anyway?
726 (ecase (info :function :kind fun)
728 (ir1-convert start next result
729 (careful-expand-macro (info :function :macro-function fun)
731 (unless (policy *lexenv* (zerop store-xref-data))
732 (record-macroexpansion fun (ctran-block start) *current-path*)))
734 (ir1-convert-srctran start next result
735 (find-free-fun fun "shouldn't happen! (no-cmacro)")
738 (defun muffle-warning-or-die ()
740 (bug "no MUFFLE-WARNING restart"))
742 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
743 ;;; errors which occur during the macroexpansion.
744 (defun careful-expand-macro (fun form)
745 (let (;; a hint I (WHN) wish I'd known earlier
746 (hint "(hint: For more precise location, try *BREAK-ON-SIGNALS*.)"))
747 (flet (;; Return a string to use as a prefix in error reporting,
748 ;; telling something about which form caused the problem.
750 (let ((*print-pretty* nil)
751 ;; We rely on the printer to abbreviate FORM.
756 #-sb-xc-host "(in macroexpansion of ~S)"
757 ;; longer message to avoid ambiguity "Was it the xc host
758 ;; or the cross-compiler which encountered the problem?"
759 #+sb-xc-host "(in cross-compiler macroexpansion of ~S)"
761 (handler-bind ((style-warning (lambda (c)
763 "~@<~A~:@_~A~@:_~A~:>"
764 (wherestring) hint c)
765 (muffle-warning-or-die)))
766 ;; KLUDGE: CMU CL in its wisdom (version 2.4.6 for
767 ;; Debian Linux, anyway) raises a CL:WARNING
768 ;; condition (not a CL:STYLE-WARNING) for undefined
769 ;; symbols when converting interpreted functions,
770 ;; causing COMPILE-FILE to think the file has a real
771 ;; problem, causing COMPILE-FILE to return FAILURE-P
772 ;; set (not just WARNINGS-P set). Since undefined
773 ;; symbol warnings are often harmless forward
774 ;; references, and since it'd be inordinately painful
775 ;; to try to eliminate all such forward references,
776 ;; these warnings are basically unavoidable. Thus, we
777 ;; need to coerce the system to work through them,
778 ;; and this code does so, by crudely suppressing all
779 ;; warnings in cross-compilation macroexpansion. --
781 #+(and cmu sb-xc-host)
786 ~@<(KLUDGE: That was a non-STYLE WARNING. ~
787 Ordinarily that would cause compilation to ~
788 fail. However, since we're running under ~
789 CMU CL, and since CMU CL emits non-STYLE ~
790 warnings for safe, hard-to-fix things (e.g. ~
791 references to not-yet-defined functions) ~
792 we're going to have to ignore it and ~
793 proceed anyway. Hopefully we're not ~
794 ignoring anything horrible here..)~:@>~:>"
797 (muffle-warning-or-die)))
798 #-(and cmu sb-xc-host)
800 (warn "~@<~A~:@_~A~@:_~A~:>"
801 (wherestring) hint c)
802 (muffle-warning-or-die)))
804 (compiler-error "~@<~A~:@_~A~@:_~A~:>"
805 (wherestring) hint c))))
806 (funcall sb!xc:*macroexpand-hook* fun form *lexenv*)))))
808 ;;;; conversion utilities
810 ;;; Convert a bunch of forms, discarding all the values except the
811 ;;; last. If there aren't any forms, then translate a NIL.
812 (declaim (ftype (sfunction (ctran ctran (or lvar null) list) (values))
813 ir1-convert-progn-body))
814 (defun ir1-convert-progn-body (start next result body)
816 (reference-constant start next result nil)
817 (let ((this-start start)
820 (let ((form (car forms)))
822 (maybe-instrument-progn-like this-start forms form))
823 (when (endp (cdr forms))
824 (ir1-convert this-start next result form)
826 (let ((this-ctran (make-ctran)))
827 (ir1-convert this-start this-ctran nil form)
828 (setq this-start this-ctran
829 forms (cdr forms)))))))
835 ;;; Check the policy for whether we should generate code coverage
836 ;;; instrumentation. If not, just return the original START
837 ;;; ctran. Otherwise insert code coverage instrumentation after
838 ;;; START, and return the new ctran.
839 (defun instrument-coverage (start mode form)
840 ;; We don't actually use FORM for anything, it's just convenient to
841 ;; have around when debugging the instrumentation.
842 (declare (ignore form))
843 (if (and (policy *lexenv* (> store-coverage-data 0))
844 *code-coverage-records*
845 *allow-instrumenting*)
846 (let ((path (source-path-original-source *current-path*)))
849 (if (member (ctran-block start)
850 (gethash path *code-coverage-blocks*))
851 ;; If this source path has already been instrumented in
852 ;; this block, don't instrument it again.
855 ;; Get an interned record cons for the path. A cons
856 ;; with the same object identity must be used for
857 ;; each instrument for the same block.
858 (or (gethash path *code-coverage-records*)
859 (setf (gethash path *code-coverage-records*)
860 (cons path +code-coverage-unmarked+))))
862 (*allow-instrumenting* nil))
863 (push (ctran-block start)
864 (gethash path *code-coverage-blocks*))
865 (let ((*allow-instrumenting* nil))
866 (ir1-convert start next nil
868 (declare (optimize speed
871 (check-constant-modification 0)))
872 ;; We're being naughty here, and
873 ;; modifying constant data. That's ok,
874 ;; we know what we're doing.
875 (%rplacd ',store t))))
879 ;;; In contexts where we don't have a source location for FORM
880 ;;; e.g. due to it not being a cons, but where we have a source
881 ;;; location for the enclosing cons, use the latter source location if
882 ;;; available. This works pretty well in practice, since many PROGNish
883 ;;; macroexpansions will just directly splice a block of forms into
884 ;;; some enclosing form with `(progn ,@body), thus retaining the
885 ;;; EQness of the conses.
886 (defun maybe-instrument-progn-like (start forms form)
887 (or (when (and *allow-instrumenting*
888 (not (get-source-path form)))
889 (let ((*current-path* (get-source-path forms)))
891 (instrument-coverage start nil form))))
894 (defun record-code-coverage (info cc)
895 (setf (gethash info *code-coverage-info*) cc))
897 (defun clear-code-coverage ()
898 (clrhash *code-coverage-info*))
900 (defun reset-code-coverage ()
901 (maphash (lambda (info cc)
902 (declare (ignore info))
903 (dolist (cc-entry cc)
904 (setf (cdr cc-entry) +code-coverage-unmarked+)))
905 *code-coverage-info*))
907 (defun code-coverage-record-marked (record)
908 (aver (consp record))
910 ((#.+code-coverage-unmarked+) nil)
914 ;;;; converting combinations
916 ;;; Does this form look like something that we should add single-stepping
917 ;;; instrumentation for?
918 (defun step-form-p (form)
919 (flet ((step-symbol-p (symbol)
920 (not (member (symbol-package symbol)
922 ;; KLUDGE: packages we're not interested in
924 (mapcar #'find-package '(sb!c sb!int sb!impl
925 sb!kernel sb!pcl)))))))
926 (and *allow-instrumenting*
927 (policy *lexenv* (= insert-step-conditions 3))
930 (step-symbol-p (car form)))))
932 ;;; Convert a function call where the function FUN is a LEAF. FORM is
933 ;;; the source for the call. We return the COMBINATION node so that
934 ;;; the caller can poke at it if it wants to.
935 (declaim (ftype (sfunction (ctran ctran (or lvar null) list leaf) combination)
936 ir1-convert-combination))
937 (defun ir1-convert-combination (start next result form fun)
938 (let ((ctran (make-ctran))
939 (fun-lvar (make-lvar)))
940 (ir1-convert start ctran fun-lvar `(the (or function symbol) ,fun))
942 (ir1-convert-combination-args fun-lvar ctran next result
944 (when (step-form-p form)
945 ;; Store a string representation of the form in the
946 ;; combination node. This will let the IR2 translator know
947 ;; that we want stepper instrumentation for this node. The
948 ;; string will be stored in the debug-info by DUMP-1-LOCATION.
949 (setf (combination-step-info combination)
950 (let ((*print-pretty* t)
952 (*print-readably* nil))
953 (prin1-to-string form))))
956 ;;; Convert the arguments to a call and make the COMBINATION
957 ;;; node. FUN-LVAR yields the function to call. ARGS is the list of
958 ;;; arguments for the call, which defaults to the cdr of source. We
959 ;;; return the COMBINATION node.
960 (defun ir1-convert-combination-args (fun-lvar start next result args)
961 (declare (type ctran start next)
963 (type (or lvar null) result)
965 (let ((node (make-combination fun-lvar)))
966 (setf (lvar-dest fun-lvar) node)
967 (collect ((arg-lvars))
968 (let ((this-start start)
972 (maybe-instrument-progn-like this-start forms arg))
973 (setf forms (cdr forms))
974 (let ((this-ctran (make-ctran))
975 (this-lvar (make-lvar node)))
976 (ir1-convert this-start this-ctran this-lvar arg)
977 (setq this-start this-ctran)
978 (arg-lvars this-lvar)))
979 (link-node-to-previous-ctran node this-start)
980 (use-continuation node next result)
981 (setf (combination-args node) (arg-lvars))))
984 ;;; Convert a call to a global function. If not :NOTINLINE, then we do
985 ;;; source transforms and try out any inline expansion. If there is no
986 ;;; expansion, but is :INLINE, then give an efficiency note (unless a
987 ;;; known function which will quite possibly be open-coded.) Next, we
988 ;;; go to ok-combination conversion.
989 (defun ir1-convert-srctran (start next result var form)
990 (declare (type ctran start next) (type (or lvar null) result)
991 (type global-var var))
992 (let ((inlinep (when (defined-fun-p var)
993 (defined-fun-inlinep var))))
994 (if (eq inlinep :notinline)
995 (ir1-convert-combination start next result form var)
996 (let ((transform (info :function
998 (leaf-source-name var))))
1000 (multiple-value-bind (transformed pass) (funcall transform form)
1002 (ir1-convert-maybe-predicate start next result form var)
1003 (ir1-convert start next result transformed)))
1004 (ir1-convert-maybe-predicate start next result form var))))))
1006 ;;; KLUDGE: If we insert a synthetic IF for a function with the PREDICATE
1007 ;;; attribute, don't generate any branch coverage instrumentation for it.
1008 (defvar *instrument-if-for-code-coverage* t)
1010 ;;; If the function has the PREDICATE attribute, and the RESULT's DEST
1011 ;;; isn't an IF, then we convert (IF <form> T NIL), ensuring that a
1012 ;;; predicate always appears in a conditional context.
1014 ;;; If the function isn't a predicate, then we call
1015 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
1016 (defun ir1-convert-maybe-predicate (start next result form var)
1017 (declare (type ctran start next)
1018 (type (or lvar null) result)
1020 (type global-var var))
1021 (let ((info (info :function :info (leaf-source-name var))))
1023 (ir1-attributep (fun-info-attributes info) predicate)
1024 (not (if-p (and result (lvar-dest result)))))
1025 (let ((*instrument-if-for-code-coverage* nil))
1026 (ir1-convert start next result `(if ,form t nil)))
1027 (ir1-convert-combination-checking-type start next result form var))))
1029 ;;; Actually really convert a global function call that we are allowed
1032 ;;; If we know the function type of the function, then we check the
1033 ;;; call for syntactic legality with respect to the declared function
1034 ;;; type. If it is impossible to determine whether the call is correct
1035 ;;; due to non-constant keywords, then we give up, marking the call as
1036 ;;; :FULL to inhibit further error messages. We return true when the
1039 ;;; If the call is legal, we also propagate type assertions from the
1040 ;;; function type to the arg and result lvars. We do this now so that
1041 ;;; IR1 optimize doesn't have to redundantly do the check later so
1042 ;;; that it can do the type propagation.
1043 (defun ir1-convert-combination-checking-type (start next result form var)
1044 (declare (type ctran start next) (type (or lvar null) result)
1047 (let* ((node (ir1-convert-combination start next result form var))
1048 (fun-lvar (basic-combination-fun node))
1049 (type (leaf-type var)))
1050 (when (validate-call-type node type t)
1051 (setf (lvar-%derived-type fun-lvar)
1052 (make-single-value-type type))
1053 (setf (lvar-reoptimize fun-lvar) nil)))
1056 ;;; Convert a call to a local function, or if the function has already
1057 ;;; been LET converted, then throw FUNCTIONAL to
1058 ;;; LOCALL-ALREADY-LET-CONVERTED. The THROW should only happen when we
1059 ;;; are converting inline expansions for local functions during
1061 (defun ir1-convert-local-combination (start next result form functional)
1062 (assure-functional-live-p functional)
1063 (ir1-convert-combination start next result
1065 (maybe-reanalyze-functional functional)))
1069 ;;; Given a list of LAMBDA-VARs and a variable name, return the
1070 ;;; LAMBDA-VAR for that name, or NIL if it isn't found. We return the
1071 ;;; *last* variable with that name, since LET* bindings may be
1072 ;;; duplicated, and declarations always apply to the last.
1073 (declaim (ftype (sfunction (list symbol) (or lambda-var list))
1075 (defun find-in-bindings (vars name)
1079 (when (eq (leaf-source-name var) name)
1081 (let ((info (lambda-var-arg-info var)))
1083 (let ((supplied-p (arg-info-supplied-p info)))
1084 (when (and supplied-p
1085 (eq (leaf-source-name supplied-p) name))
1086 (setq found supplied-p))))))
1087 ((and (consp var) (eq (car var) name))
1088 (setf found (cdr var)))))
1091 ;;; Called by PROCESS-DECLS to deal with a variable type declaration.
1092 ;;; If a LAMBDA-VAR being bound, we intersect the type with the var's
1093 ;;; type, otherwise we add a type restriction on the var. If a symbol
1094 ;;; macro, we just wrap a THE around the expansion.
1095 (defun process-type-decl (decl res vars context)
1096 (declare (list decl vars) (type lexenv res))
1097 (let ((type (compiler-specifier-type (first decl))))
1098 (collect ((restr nil cons)
1099 (new-vars nil cons))
1100 (dolist (var-name (rest decl))
1101 (when (boundp var-name)
1102 (program-assert-symbol-home-package-unlocked
1103 context var-name "declaring the type of ~A"))
1104 (let* ((bound-var (find-in-bindings vars var-name))
1106 (lexenv-find var-name vars)
1107 (find-free-var var-name))))
1111 ((process-var (var bound-var)
1112 (let* ((old-type (or (lexenv-find var type-restrictions)
1114 (int (if (or (fun-type-p type)
1115 (fun-type-p old-type))
1117 (type-approx-intersection2
1119 (cond ((eq int *empty-type*)
1120 (unless (policy *lexenv* (= inhibit-warnings 3))
1124 "The type declarations ~S and ~S for ~S conflict."
1127 (type-specifier old-type)
1128 (type-specifier type)
1130 (bound-var (setf (leaf-type bound-var) int))
1132 (restr (cons var int)))))))
1133 (process-var var bound-var)
1134 (awhen (and (lambda-var-p var)
1135 (lambda-var-specvar var))
1136 (process-var it nil))))
1138 ;; FIXME: non-ANSI weirdness
1139 (aver (eq (car var) 'macro))
1140 (new-vars `(,var-name . (macro . (the ,(first decl)
1144 "~S is an alien variable, so its type can't be declared."
1147 (if (or (restr) (new-vars))
1148 (make-lexenv :default res
1149 :type-restrictions (restr)
1153 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
1154 ;;; declarations for function variables. In addition to allowing
1155 ;;; declarations for functions being bound, we must also deal with
1156 ;;; declarations that constrain the type of lexically apparent
1158 (defun process-ftype-decl (spec res names fvars context)
1159 (declare (type list names fvars)
1161 (let ((type (compiler-specifier-type spec)))
1162 (collect ((res nil cons))
1163 (dolist (name names)
1164 (when (fboundp name)
1165 (program-assert-symbol-home-package-unlocked
1166 context name "declaring the ftype of ~A"))
1167 (let ((found (find name fvars :key #'leaf-source-name :test #'equal)))
1170 (setf (leaf-type found) type)
1171 (assert-definition-type found type
1172 :unwinnage-fun #'compiler-notify
1173 :where "FTYPE declaration"))
1175 (res (cons (find-lexically-apparent-fun
1176 name "in a function type declaration")
1179 (make-lexenv :default res :type-restrictions (res))
1182 ;;; Process a special declaration, returning a new LEXENV. A non-bound
1183 ;;; special declaration is instantiated by throwing a special variable
1184 ;;; into the variables if BINDING-FORM-P is NIL, or otherwise into
1185 ;;; *POST-BINDING-VARIABLE-LEXENV*.
1186 (defun process-special-decl (spec res vars binding-form-p context)
1187 (declare (list spec vars) (type lexenv res))
1188 (collect ((new-venv nil cons))
1189 (dolist (name (cdr spec))
1190 (program-assert-symbol-home-package-unlocked
1191 context name "declaring ~A special")
1192 (let ((var (find-in-bindings vars name)))
1195 (aver (eq (car var) 'macro))
1197 "~S is a symbol-macro and thus can't be declared special."
1200 (when (lambda-var-ignorep var)
1201 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1202 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1203 (compiler-style-warn
1204 "The ignored variable ~S is being declared special."
1206 (setf (lambda-var-specvar var)
1207 (specvar-for-binding name)))
1209 (unless (or (assoc name (new-venv) :test #'eq))
1210 (new-venv (cons name (specvar-for-binding name))))))))
1211 (cond (binding-form-p
1212 (setf *post-binding-variable-lexenv*
1213 (append (new-venv) *post-binding-variable-lexenv*))
1216 (make-lexenv :default res :vars (new-venv)))
1220 ;;; Return a DEFINED-FUN which copies a GLOBAL-VAR but for its INLINEP
1221 ;;; (and TYPE if notinline), plus type-restrictions from the lexenv.
1222 (defun make-new-inlinep (var inlinep local-type)
1223 (declare (type global-var var) (type inlinep inlinep))
1224 (let* ((type (if (and (eq inlinep :notinline)
1225 (not (eq (leaf-where-from var) :declared)))
1226 (specifier-type 'function)
1228 (res (make-defined-fun
1229 :%source-name (leaf-source-name var)
1230 :where-from (leaf-where-from var)
1231 :type (if local-type
1232 (type-intersection local-type type)
1235 (when (defined-fun-p var)
1236 (setf (defined-fun-inline-expansion res)
1237 (defined-fun-inline-expansion var))
1238 (setf (defined-fun-functional res)
1239 (defined-fun-functional var)))
1240 ;; FIXME: Is this really right? Needs we not set the FUNCTIONAL
1241 ;; to the original global-var?
1244 ;;; Parse an inline/notinline declaration. If it's a local function we're
1245 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
1246 (defun process-inline-decl (spec res fvars)
1247 (let ((sense (cdr (assoc (first spec) *inlinep-translations* :test #'eq)))
1249 (dolist (name (rest spec))
1250 (let ((fvar (find name fvars :key #'leaf-source-name :test #'equal)))
1252 (setf (functional-inlinep fvar) sense)
1253 (let ((found (find-lexically-apparent-fun
1254 name "in an inline or notinline declaration")))
1257 (when (policy *lexenv* (>= speed inhibit-warnings))
1258 (compiler-notify "ignoring ~A declaration not at ~
1259 definition of local function:~% ~S"
1263 (cdr (assoc found (lexenv-type-restrictions res)))))
1264 (push (cons name (make-new-inlinep found sense type))
1267 (make-lexenv :default res :funs new-fenv)
1270 ;;; like FIND-IN-BINDINGS, but looks for #'FOO in the FVARS
1271 (defun find-in-bindings-or-fbindings (name vars fvars)
1272 (declare (list vars fvars))
1274 (destructuring-bind (wot fn-name) name
1275 (unless (eq wot 'function)
1276 (compiler-error "The function or variable name ~S is unrecognizable."
1278 (find fn-name fvars :key #'leaf-source-name :test #'equal))
1279 (find-in-bindings vars name)))
1281 ;;; Process an ignore/ignorable declaration, checking for various losing
1283 (defun process-ignore-decl (spec vars fvars)
1284 (declare (list spec vars fvars))
1285 (dolist (name (rest spec))
1286 (let ((var (find-in-bindings-or-fbindings name vars fvars)))
1289 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1290 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1291 (compiler-style-warn "declaring unknown variable ~S to be ignored"
1293 ;; FIXME: This special case looks like non-ANSI weirdness.
1294 ((and (consp var) (eq (car var) 'macro))
1295 ;; Just ignore the IGNORE decl.
1298 (setf (leaf-ever-used var) t))
1299 ((and (lambda-var-specvar var) (eq (first spec) 'ignore))
1300 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1301 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1302 (compiler-style-warn "declaring special variable ~S to be ignored"
1304 ((eq (first spec) 'ignorable)
1305 (setf (leaf-ever-used var) t))
1307 (setf (lambda-var-ignorep var) t)))))
1310 (defun process-dx-decl (names vars fvars)
1311 (flet ((maybe-notify (control &rest args)
1312 (when (policy *lexenv* (> speed inhibit-warnings))
1313 (apply #'compiler-notify control args))))
1314 (if (policy *lexenv* (= stack-allocate-dynamic-extent 3))
1315 (dolist (name names)
1318 (let* ((bound-var (find-in-bindings vars name))
1320 (lexenv-find name vars)
1321 (find-free-var name))))
1325 (setf (leaf-dynamic-extent var) t)
1327 "ignoring DYNAMIC-EXTENT declaration for free ~S"
1330 (compiler-error "DYNAMIC-EXTENT on symbol-macro: ~S" name))
1332 (compiler-error "DYNAMIC-EXTENT on heap-alien-info: ~S"
1335 (eq (car name) 'function)
1337 (valid-function-name-p (cadr name)))
1338 (let* ((fname (cadr name))
1339 (bound-fun (find fname fvars
1340 :key #'leaf-source-name
1342 (etypecase bound-fun
1344 #!+stack-allocatable-closures
1345 (setf (leaf-dynamic-extent bound-fun) t)
1346 #!-stack-allocatable-closures
1348 "ignoring DYNAMIC-EXTENT declaration on a function ~S ~
1349 (not supported on this platform)." fname))
1351 (compiler-error "DYNAMIC-EXTENT on macro: ~S" fname))
1354 "ignoring DYNAMIC-EXTENT declaration for free ~S"
1356 (t (compiler-error "DYNAMIC-EXTENT on a weird thing: ~S" name))))
1357 (maybe-notify "ignoring DYNAMIC-EXTENT declarations for ~S" names))))
1359 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
1360 ;;; go away, I think.
1361 (defvar *suppress-values-declaration* nil
1363 "If true, processing of the VALUES declaration is inhibited.")
1365 ;;; Process a single declaration spec, augmenting the specified LEXENV
1366 ;;; RES. Return RES and result type. VARS and FVARS are as described
1368 (defun process-1-decl (raw-spec res vars fvars binding-form-p context)
1369 (declare (type list raw-spec vars fvars))
1370 (declare (type lexenv res))
1371 (let ((spec (canonized-decl-spec raw-spec))
1372 (result-type *wild-type*))
1375 (special (process-special-decl spec res vars binding-form-p context))
1378 (compiler-error "no type specified in FTYPE declaration: ~S" spec))
1379 (process-ftype-decl (second spec) res (cddr spec) fvars context))
1380 ((inline notinline maybe-inline)
1381 (process-inline-decl spec res fvars))
1383 (process-ignore-decl spec vars fvars)
1388 :policy (process-optimize-decl spec (lexenv-policy res))))
1392 :handled-conditions (process-muffle-conditions-decl
1393 spec (lexenv-handled-conditions res))))
1394 (unmuffle-conditions
1397 :handled-conditions (process-unmuffle-conditions-decl
1398 spec (lexenv-handled-conditions res))))
1400 (process-type-decl (cdr spec) res vars context))
1402 (unless *suppress-values-declaration*
1403 (let ((types (cdr spec)))
1405 (compiler-values-specifier-type
1406 (if (singleton-p types)
1408 `(values ,@types)))))
1411 (process-dx-decl (cdr spec) vars fvars)
1413 ((disable-package-locks enable-package-locks)
1416 :disabled-package-locks (process-package-lock-decl
1417 spec (lexenv-disabled-package-locks res))))
1419 (unless (info :declaration :recognized (first spec))
1420 (compiler-warn "unrecognized declaration ~S" raw-spec))
1424 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1425 ;;; and FUNCTIONAL structures which are being bound. In addition to
1426 ;;; filling in slots in the leaf structures, we return a new LEXENV,
1427 ;;; which reflects pervasive special and function type declarations,
1428 ;;; (NOT)INLINE declarations and OPTIMIZE declarations, and type of
1429 ;;; VALUES declarations. If BINDING-FORM-P is true, the third return
1430 ;;; value is a list of VARs that should not apply to the lexenv of the
1431 ;;; initialization forms for the bindings, but should apply to the body.
1433 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1435 (defun process-decls (decls vars fvars &key
1436 (lexenv *lexenv*) (binding-form-p nil) (context :compile))
1437 (declare (list decls vars fvars))
1438 (let ((result-type *wild-type*)
1439 (*post-binding-variable-lexenv* nil))
1440 (dolist (decl decls)
1441 (dolist (spec (rest decl))
1442 (unless (consp spec)
1443 (compiler-error "malformed declaration specifier ~S in ~S" spec decl))
1444 (multiple-value-bind (new-env new-result-type)
1445 (process-1-decl spec lexenv vars fvars binding-form-p context)
1446 (setq lexenv new-env)
1447 (unless (eq new-result-type *wild-type*)
1449 (values-type-intersection result-type new-result-type))))))
1450 (values lexenv result-type *post-binding-variable-lexenv*)))
1452 (defun %processing-decls (decls vars fvars ctran lvar binding-form-p fun)
1453 (multiple-value-bind (*lexenv* result-type post-binding-lexenv)
1454 (process-decls decls vars fvars :binding-form-p binding-form-p)
1455 (cond ((eq result-type *wild-type*)
1456 (funcall fun ctran lvar post-binding-lexenv))
1458 (let ((value-ctran (make-ctran))
1459 (value-lvar (make-lvar)))
1460 (multiple-value-prog1
1461 (funcall fun value-ctran value-lvar post-binding-lexenv)
1462 (let ((cast (make-cast value-lvar result-type
1463 (lexenv-policy *lexenv*))))
1464 (link-node-to-previous-ctran cast value-ctran)
1465 (setf (lvar-dest value-lvar) cast)
1466 (use-continuation cast ctran lvar))))))))
1467 (defmacro processing-decls ((decls vars fvars ctran lvar
1468 &optional post-binding-lexenv)
1470 (check-type ctran symbol)
1471 (check-type lvar symbol)
1472 (let ((post-binding-lexenv-p (not (null post-binding-lexenv)))
1473 (post-binding-lexenv (or post-binding-lexenv (gensym))))
1474 `(%processing-decls ,decls ,vars ,fvars ,ctran ,lvar
1475 ,post-binding-lexenv-p
1476 (lambda (,ctran ,lvar ,post-binding-lexenv)
1477 (declare (ignorable ,post-binding-lexenv))
1480 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1481 ;;; declaration. If there is a global variable of that name, then
1482 ;;; check that it isn't a constant and return it. Otherwise, create an
1483 ;;; anonymous GLOBAL-VAR.
1484 (defun specvar-for-binding (name)
1485 (cond ((not (eq (info :variable :where-from name) :assumed))
1486 (let ((found (find-free-var name)))
1487 (when (heap-alien-info-p found)
1489 "~S is an alien variable and so can't be declared special."
1491 (unless (global-var-p found)
1493 "~S is a constant and so can't be declared special."
1497 (make-global-var :kind :special
1499 :where-from :declared))))