1.1.13: will be tagged as "sbcl-1.1.13"
[sbcl.git] / src / compiler / ir1tran.lisp
blobb2bd2e7aa20026b864a3592c41d4143be60f1a64
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
5 ;;;; more information.
6 ;;;;
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.
13 (in-package "SB!C")
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
20 ;;; top level form.
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.
30 ;;;
31 ;;; It is fairly useless to store symbols, characters, or fixnums in
32 ;;; this table, as 42 is EQ to 42 no matter where in the source it
33 ;;; appears. GET-SOURCE-PATH and NOTE-SOURCE-PATH functions should be
34 ;;; always used to access this table.
35 (declaim (hash-table *source-paths*))
36 (defvar *source-paths*)
38 (declaim (inline source-form-has-path-p))
39 (defun source-form-has-path-p (form)
40 (not (typep form '(or symbol fixnum character))))
42 (defun get-source-path (form)
43 (when (source-form-has-path-p form)
44 (gethash form *source-paths*)))
46 (defun ensure-source-path (form)
47 (or (get-source-path form)
48 (cons (simplify-source-path-form form)
49 *current-path*)))
51 (defun simplify-source-path-form (form)
52 (if (consp form)
53 (let ((op (car form)))
54 ;; In the compiler functions can be directly represented
55 ;; by leaves. Having leaves in the source path is pretty
56 ;; hard on the poor user, however, so replace with the
57 ;; source-name when possible.
58 (if (and (leaf-p op) (leaf-has-source-name-p op))
59 (cons (leaf-source-name op) (cdr form))
60 form))
61 form))
63 (defun note-source-path (form &rest arguments)
64 (when (source-form-has-path-p form)
65 (setf (gethash form *source-paths*)
66 (apply #'list* 'original-source-start *current-form-number* arguments))))
68 ;;; *CURRENT-COMPONENT* is the COMPONENT structure which we link
69 ;;; blocks into as we generate them. This just serves to glue the
70 ;;; emitted blocks together until local call analysis and flow graph
71 ;;; canonicalization figure out what is really going on. We need to
72 ;;; keep track of all the blocks generated so that we can delete them
73 ;;; if they turn out to be unreachable.
74 ;;;
75 ;;; FIXME: It's confusing having one variable named *CURRENT-COMPONENT*
76 ;;; and another named *COMPONENT-BEING-COMPILED*. (In CMU CL they
77 ;;; were called *CURRENT-COMPONENT* and *COMPILE-COMPONENT* respectively,
78 ;;; which was also confusing.)
79 (declaim (type (or component null) *current-component*))
80 (defvar *current-component*)
82 ;;; *CURRENT-PATH* is the source path of the form we are currently
83 ;;; translating. See NODE-SOURCE-PATH in the NODE structure.
84 (declaim (list *current-path*))
85 (defvar *current-path*)
87 (defvar *derive-function-types* nil
88 "Should the compiler assume that function types will never change,
89 so that it can use type information inferred from current definitions
90 to optimize code which uses those definitions? Setting this true
91 gives non-ANSI, early-CMU-CL behavior. It can be useful for improving
92 the efficiency of stable code.")
94 (defvar *fun-names-in-this-file* nil)
96 (defvar *post-binding-variable-lexenv* nil)
98 ;;;; namespace management utilities
100 (defun fun-lexically-notinline-p (name)
101 (let ((fun (lexenv-find name funs :test #'equal)))
102 ;; a declaration will trump a proclamation
103 (if (and fun (defined-fun-p fun))
104 (eq (defined-fun-inlinep fun) :notinline)
105 (eq (info :function :inlinep name) :notinline))))
107 ;; This will get redefined in PCL boot.
108 (declaim (notinline maybe-update-info-for-gf))
109 (defun maybe-update-info-for-gf (name)
110 (declare (ignore name))
111 nil)
113 (defun maybe-defined-here (name where)
114 (if (and (eq :defined where)
115 (member name *fun-names-in-this-file* :test #'equal))
116 :defined-here
117 where))
119 ;;; Return a GLOBAL-VAR structure usable for referencing the global
120 ;;; function NAME.
121 (defun find-global-fun (name latep)
122 (unless (info :function :kind name)
123 (setf (info :function :kind name) :function)
124 (setf (info :function :where-from name) :assumed))
125 (let ((where (info :function :where-from name)))
126 (when (and (eq where :assumed)
127 ;; In the ordinary target Lisp, it's silly to report
128 ;; undefinedness when the function is defined in the
129 ;; running Lisp. But at cross-compile time, the current
130 ;; definedness of a function is irrelevant to the
131 ;; definedness at runtime, which is what matters.
132 #-sb-xc-host (not (fboundp name))
133 ;; LATEP is true when the user has indicated that
134 ;; late-late binding is desired by using eg. a quoted
135 ;; symbol -- in which case it makes little sense to
136 ;; complain about undefined functions.
137 (not latep))
138 (note-undefined-reference name :function))
139 (let ((ftype (info :function :type name))
140 (notinline (fun-lexically-notinline-p name)))
141 (make-global-var
142 :kind :global-function
143 :%source-name name
144 :type (if (or (eq where :declared)
145 (and (not latep)
146 (not notinline)
147 *derive-function-types*))
148 ftype
149 (specifier-type 'function))
150 :defined-type (if (and (not latep) (not notinline))
151 (or (maybe-update-info-for-gf name) ftype)
152 (specifier-type 'function))
153 :where-from (if notinline
154 where
155 (maybe-defined-here name where))))))
157 ;;; Have some DEFINED-FUN-FUNCTIONALS of a *FREE-FUNS* entry become invalid?
158 ;;; Drop 'em.
160 ;;; This was added to fix bug 138 in SBCL. It is possible for a *FREE-FUNS*
161 ;;; entry to contain a DEFINED-FUN whose DEFINED-FUN-FUNCTIONAL object
162 ;;; contained IR1 stuff (NODEs, BLOCKs...) referring to an already compiled
163 ;;; (aka "dead") component. When this IR1 stuff was reused in a new component,
164 ;;; under further obscure circumstances it could be used by
165 ;;; WITH-IR1-ENVIRONMENT-FROM-NODE to generate a binding for
166 ;;; *CURRENT-COMPONENT*. At that point things got all confused, since IR1
167 ;;; conversion was sending code to a component which had already been compiled
168 ;;; and would never be compiled again.
170 ;;; Note: as of 1.0.24.41 this seems to happen only in XC, and the original
171 ;;; BUGS entry also makes it seem like this might not be an issue at all on
172 ;;; target.
173 (defun clear-invalid-functionals (free-fun)
174 ;; There might be other reasons that *FREE-FUN* entries could
175 ;; become invalid, but the only one we've been bitten by so far
176 ;; (sbcl-0.pre7.118) is this one:
177 (when (defined-fun-p free-fun)
178 (setf (defined-fun-functionals free-fun)
179 (delete-if (lambda (functional)
180 (or (eq (functional-kind functional) :deleted)
181 (when (lambda-p functional)
183 ;; (The main reason for this first test is to bail
184 ;; out early in cases where the LAMBDA-COMPONENT
185 ;; call in the second test would fail because links
186 ;; it needs are uninitialized or invalid.)
188 ;; If the BIND node for this LAMBDA is null, then
189 ;; according to the slot comments, the LAMBDA has
190 ;; been deleted or its call has been deleted. In
191 ;; that case, it seems rather questionable to reuse
192 ;; it, and certainly it shouldn't be necessary to
193 ;; reuse it, so we cheerfully declare it invalid.
194 (not (lambda-bind functional))
195 ;; If this IR1 stuff belongs to a dead component,
196 ;; then we can't reuse it without getting into
197 ;; bizarre confusion.
198 (eq (component-info (lambda-component functional))
199 :dead)))))
200 (defined-fun-functionals free-fun)))
201 nil))
203 ;;; If NAME already has a valid entry in *FREE-FUNS*, then return
204 ;;; the value. Otherwise, make a new GLOBAL-VAR using information from
205 ;;; the global environment and enter it in *FREE-FUNS*. If NAME
206 ;;; names a macro or special form, then we error out using the
207 ;;; supplied context which indicates what we were trying to do that
208 ;;; demanded a function.
209 (declaim (ftype (sfunction (t string) global-var) find-free-fun))
210 (defun find-free-fun (name context)
211 (or (let ((old-free-fun (gethash name *free-funs*)))
212 (when old-free-fun
213 (clear-invalid-functionals old-free-fun)
214 old-free-fun))
215 (ecase (info :function :kind name)
216 ;; FIXME: The :MACRO and :SPECIAL-FORM cases could be merged.
217 (:macro
218 (compiler-error "The macro name ~S was found ~A." name context))
219 (:special-form
220 (compiler-error "The special form name ~S was found ~A."
221 name
222 context))
223 ((:function nil)
224 (check-fun-name name)
225 (note-if-setf-fun-and-macro name)
226 (let ((expansion (fun-name-inline-expansion name))
227 (inlinep (info :function :inlinep name)))
228 (setf (gethash name *free-funs*)
229 (if (or expansion inlinep)
230 (let ((where (info :function :where-from name)))
231 (make-defined-fun
232 :%source-name name
233 :inline-expansion expansion
234 :inlinep inlinep
235 :where-from (if (eq inlinep :notinline)
236 where
237 (maybe-defined-here name where))
238 :type (if (and (eq inlinep :notinline)
239 (neq where :declared))
240 (specifier-type 'function)
241 (info :function :type name))))
242 (find-global-fun name nil))))))))
244 ;;; Return the LEAF structure for the lexically apparent function
245 ;;; definition of NAME.
246 (declaim (ftype (sfunction (t string) leaf) find-lexically-apparent-fun))
247 (defun find-lexically-apparent-fun (name context)
248 (let ((var (lexenv-find name funs :test #'equal)))
249 (cond (var
250 (unless (leaf-p var)
251 (aver (and (consp var) (eq (car var) 'macro)))
252 (compiler-error "found macro name ~S ~A" name context))
253 var)
255 (find-free-fun name context)))))
257 (defun maybe-find-free-var (name)
258 (gethash name *free-vars*))
260 ;;; Return the LEAF node for a global variable reference to NAME. If
261 ;;; NAME is already entered in *FREE-VARS*, then we just return the
262 ;;; corresponding value. Otherwise, we make a new leaf using
263 ;;; information from the global environment and enter it in
264 ;;; *FREE-VARS*. If the variable is unknown, then we emit a warning.
265 (declaim (ftype (sfunction (t) (or leaf cons heap-alien-info)) find-free-var))
266 (defun find-free-var (name)
267 (unless (symbolp name)
268 (compiler-error "Variable name is not a symbol: ~S." name))
269 (or (gethash name *free-vars*)
270 (let ((kind (info :variable :kind name))
271 (type (info :variable :type name))
272 (where-from (info :variable :where-from name)))
273 (when (eq kind :unknown)
274 (note-undefined-reference name :variable))
275 (setf (gethash name *free-vars*)
276 (case kind
277 (:alien
278 (info :variable :alien-info name))
279 ;; FIXME: The return value in this case should really be
280 ;; of type SB!C::LEAF. I don't feel too badly about it,
281 ;; because the MACRO idiom is scattered throughout this
282 ;; file, but it should be cleaned up so we're not
283 ;; throwing random conses around. --njf 2002-03-23
284 (:macro
285 (let ((expansion (info :variable :macro-expansion name))
286 (type (type-specifier (info :variable :type name))))
287 `(macro . (the ,type ,expansion))))
288 (:constant
289 (let ((value (symbol-value name)))
290 ;; Override the values of standard symbols in XC,
291 ;; since we can't redefine them.
292 #+sb-xc-host
293 (when (eql (find-symbol (symbol-name name) :cl) name)
294 (multiple-value-bind (xc-value foundp)
295 (info :variable :xc-constant-value name)
296 (cond (foundp
297 (setf value xc-value))
298 ((not (eq value name))
299 (compiler-warn
300 "Using cross-compilation host's definition of ~S: ~A~%"
301 name (symbol-value name))))))
302 (find-constant value name)))
304 (make-global-var :kind kind
305 :%source-name name
306 :type type
307 :where-from where-from)))))))
309 ;;; Grovel over CONSTANT checking for any sub-parts that need to be
310 ;;; processed with MAKE-LOAD-FORM. We have to be careful, because
311 ;;; CONSTANT might be circular. We also check that the constant (and
312 ;;; any subparts) are dumpable at all.
313 (defun maybe-emit-make-load-forms (constant &optional (name nil namep))
314 (let ((xset (alloc-xset)))
315 (labels ((trivialp (value)
316 (typep value
317 '(or
318 #-sb-xc-host unboxed-array
319 #+sb-xc-host (simple-array (unsigned-byte 8) (*))
320 symbol
321 number
322 character
323 string
324 #!+sb-simd-pack
325 #+sb-xc-host nil
326 #-sb-xc-host sb!kernel:simd-pack)))
327 (grovel (value)
328 ;; Unless VALUE is an object which which obviously
329 ;; can't contain other objects
330 (unless (trivialp value)
331 (if (xset-member-p value xset)
332 (return-from grovel nil)
333 (add-to-xset value xset))
334 (typecase value
335 (cons
336 (grovel (car value))
337 (grovel (cdr value)))
338 (simple-vector
339 (dotimes (i (length value))
340 (grovel (svref value i))))
341 ((vector t)
342 (dotimes (i (length value))
343 (grovel (aref value i))))
344 ((simple-array t)
345 ;; Even though the (ARRAY T) branch does the exact
346 ;; same thing as this branch we do this separately
347 ;; so that the compiler can use faster versions of
348 ;; array-total-size and row-major-aref.
349 (dotimes (i (array-total-size value))
350 (grovel (row-major-aref value i))))
351 ((array t)
352 (dotimes (i (array-total-size value))
353 (grovel (row-major-aref value i))))
354 (#+sb-xc-host structure!object
355 #-sb-xc-host instance
356 ;; In the target SBCL, we can dump any instance, but
357 ;; in the cross-compilation host, %INSTANCE-FOO
358 ;; functions don't work on general instances, only on
359 ;; STRUCTURE!OBJECTs.
361 ;; FIXME: What about funcallable instances with
362 ;; user-defined MAKE-LOAD-FORM methods?
363 (when (emit-make-load-form value)
364 (dotimes (i (- (%instance-length value)
365 #+sb-xc-host 0
366 #-sb-xc-host (layout-n-untagged-slots
367 (%instance-ref value 0))))
368 (grovel (%instance-ref value i)))))
370 (compiler-error
371 "Objects of type ~S can't be dumped into fasl files."
372 (type-of value)))))))
373 ;; Dump all non-trivial named constants using the name.
374 (if (and namep (not (typep constant '(or symbol character
375 ;; FIXME: Cold init breaks if we
376 ;; try to reference FP constants
377 ;; thru their names.
378 #+sb-xc-host number
379 #-sb-xc-host fixnum))))
380 (emit-make-load-form constant name)
381 (grovel constant))))
382 (values))
384 ;;;; some flow-graph hacking utilities
386 ;;; This function sets up the back link between the node and the
387 ;;; ctran which continues at it.
388 (defun link-node-to-previous-ctran (node ctran)
389 (declare (type node node) (type ctran ctran))
390 (aver (not (ctran-next ctran)))
391 (setf (ctran-next ctran) node)
392 (setf (node-prev node) ctran))
394 ;;; This function is used to set the ctran for a node, and thus
395 ;;; determine what is evaluated next. If the ctran has no block, then
396 ;;; we make it be in the block that the node is in. If the ctran heads
397 ;;; its block, we end our block and link it to that block.
398 #!-sb-fluid (declaim (inline use-ctran))
399 (defun use-ctran (node ctran)
400 (declare (type node node) (type ctran ctran))
401 (if (eq (ctran-kind ctran) :unused)
402 (let ((node-block (ctran-block (node-prev node))))
403 (setf (ctran-block ctran) node-block)
404 (setf (ctran-kind ctran) :inside-block)
405 (setf (ctran-use ctran) node)
406 (setf (node-next node) ctran))
407 (%use-ctran node ctran)))
408 (defun %use-ctran (node ctran)
409 (declare (type node node) (type ctran ctran) (inline member))
410 (let ((block (ctran-block ctran))
411 (node-block (ctran-block (node-prev node))))
412 (aver (eq (ctran-kind ctran) :block-start))
413 (when (block-last node-block)
414 (error "~S has already ended." node-block))
415 (setf (block-last node-block) node)
416 (when (block-succ node-block)
417 (error "~S already has successors." node-block))
418 (setf (block-succ node-block) (list block))
419 (when (memq node-block (block-pred block))
420 (error "~S is already a predecessor of ~S." node-block block))
421 (push node-block (block-pred block))))
423 ;;; Insert NEW before OLD in the flow-graph.
424 (defun insert-node-before (old new)
425 (let ((prev (node-prev old))
426 (temp (make-ctran)))
427 (ensure-block-start prev)
428 (setf (ctran-next prev) nil)
429 (link-node-to-previous-ctran new prev)
430 (use-ctran new temp)
431 (link-node-to-previous-ctran old temp))
432 (values))
434 ;;; This function is used to set the ctran for a node, and thus
435 ;;; determine what receives the value.
436 (defun use-lvar (node lvar)
437 (declare (type valued-node node) (type (or lvar null) lvar))
438 (aver (not (node-lvar node)))
439 (when lvar
440 (setf (node-lvar node) lvar)
441 (cond ((null (lvar-uses lvar))
442 (setf (lvar-uses lvar) node))
443 ((listp (lvar-uses lvar))
444 (aver (not (memq node (lvar-uses lvar))))
445 (push node (lvar-uses lvar)))
447 (aver (neq node (lvar-uses lvar)))
448 (setf (lvar-uses lvar) (list node (lvar-uses lvar)))))
449 (reoptimize-lvar lvar)))
451 #!-sb-fluid(declaim (inline use-continuation))
452 (defun use-continuation (node ctran lvar)
453 (use-ctran node ctran)
454 (use-lvar node lvar))
456 ;;;; exported functions
458 ;;; This function takes a form and the top level form number for that
459 ;;; form, and returns a lambda representing the translation of that
460 ;;; form in the current global environment. The returned lambda is a
461 ;;; top level lambda that can be called to cause evaluation of the
462 ;;; forms. This lambda is in the initial component. If FOR-VALUE is T,
463 ;;; then the value of the form is returned from the function,
464 ;;; otherwise NIL is returned.
466 ;;; This function may have arbitrary effects on the global environment
467 ;;; due to processing of EVAL-WHENs. All syntax error checking is
468 ;;; done, with erroneous forms being replaced by a proxy which signals
469 ;;; an error if it is evaluated. Warnings about possibly inconsistent
470 ;;; or illegal changes to the global environment will also be given.
472 ;;; We make the initial component and convert the form in a PROGN (and
473 ;;; an optional NIL tacked on the end.) We then return the lambda. We
474 ;;; bind all of our state variables here, rather than relying on the
475 ;;; global value (if any) so that IR1 conversion will be reentrant.
476 ;;; This is necessary for EVAL-WHEN processing, etc.
478 ;;; The hashtables used to hold global namespace info must be
479 ;;; reallocated elsewhere. Note also that *LEXENV* is not bound, so
480 ;;; that local macro definitions can be introduced by enclosing code.
481 (defun ir1-toplevel (form path for-value &optional (allow-instrumenting t))
482 (declare (list path))
483 (let* ((*current-path* path)
484 (component (make-empty-component))
485 (*current-component* component)
486 (*allow-instrumenting* allow-instrumenting))
487 (setf (component-name component) 'initial-component)
488 (setf (component-kind component) :initial)
489 (let* ((forms (if for-value `(,form) `(,form nil)))
490 (res (ir1-convert-lambda-body
491 forms ()
492 :debug-name (debug-name 'top-level-form #+sb-xc-host nil #-sb-xc-host form))))
493 (setf (functional-entry-fun res) res
494 (functional-arg-documentation res) ()
495 (functional-kind res) :toplevel)
496 res)))
498 ;;; This function is called on freshly read forms to record the
499 ;;; initial location of each form (and subform.) Form is the form to
500 ;;; find the paths in, and TLF-NUM is the top level form number of the
501 ;;; truly top level form.
503 ;;; This gets a bit interesting when the source code is circular. This
504 ;;; can (reasonably?) happen in the case of circular list constants.
505 (defun find-source-paths (form tlf-num)
506 (declare (type index tlf-num))
507 (let ((*current-form-number* 0))
508 (sub-find-source-paths form (list tlf-num)))
509 (values))
510 (defun sub-find-source-paths (form path)
511 (unless (get-source-path form)
512 (note-source-path form path)
513 (incf *current-form-number*)
514 (let ((pos 0)
515 (subform form)
516 (trail form))
517 (declare (fixnum pos))
518 (macrolet ((frob ()
519 `(progn
520 (when (atom subform) (return))
521 (let ((fm (car subform)))
522 (cond ((consp fm)
523 ;; If it's a cons, recurse.
524 (sub-find-source-paths fm (cons pos path)))
525 ((eq 'quote fm)
526 ;; Don't look into quoted constants.
527 (return))
528 ((not (zerop pos))
529 ;; Otherwise store the containing form. It's not
530 ;; perfect, but better than nothing.
531 (note-source-path subform pos path)))
532 (incf pos))
533 (setq subform (cdr subform))
534 (when (eq subform trail) (return)))))
535 (loop
536 (frob)
537 (frob)
538 (setq trail (cdr trail)))))))
540 ;;;; IR1-CONVERT, macroexpansion and special form dispatching
542 (declaim (ftype (sfunction (ctran ctran (or lvar null) t &optional t)
543 (values))
544 ir1-convert))
545 (macrolet (;; Bind *COMPILER-ERROR-BAILOUT* to a function that throws
546 ;; out of the body and converts a condition signalling form
547 ;; instead. The source form is converted to a string since it
548 ;; may contain arbitrary non-externalizable objects.
549 (ir1-error-bailout ((start next result form) &body body)
550 (with-unique-names (skip condition)
551 `(block ,skip
552 (let ((,condition (catch 'ir1-error-abort
553 (let ((*compiler-error-bailout*
554 (lambda (&optional e)
555 (throw 'ir1-error-abort e))))
556 ,@body
557 (return-from ,skip nil)))))
558 (ir1-convert ,start ,next ,result
559 (make-compiler-error-form ,condition
560 ,form)))))))
562 ;; Translate FORM into IR1. The code is inserted as the NEXT of the
563 ;; CTRAN START. RESULT is the LVAR which receives the value of the
564 ;; FORM to be translated. The translators call this function
565 ;; recursively to translate their subnodes.
567 ;; As a special hack to make life easier in the compiler, a LEAF
568 ;; IR1-converts into a reference to that LEAF structure. This allows
569 ;; the creation using backquote of forms that contain leaf
570 ;; references, without having to introduce dummy names into the
571 ;; namespace.
572 (defun ir1-convert (start next result form &optional alias)
573 (ir1-error-bailout (start next result form)
574 (let* ((*current-path* (ensure-source-path (or alias form)))
575 (start (instrument-coverage start nil form)))
576 (cond ((atom form)
577 (cond ((and (symbolp form) (not (keywordp form)))
578 (ir1-convert-var start next result form))
579 ((leaf-p form)
580 (reference-leaf start next result form))
582 (reference-constant start next result form))))
584 (ir1-convert-functoid start next result form)))))
585 (values))
587 ;; Generate a reference to a manifest constant, creating a new leaf
588 ;; if necessary.
589 (defun reference-constant (start next result value)
590 (declare (type ctran start next)
591 (type (or lvar null) result))
592 (ir1-error-bailout (start next result value)
593 (let* ((leaf (find-constant value))
594 (res (make-ref leaf)))
595 (push res (leaf-refs leaf))
596 (link-node-to-previous-ctran res start)
597 (use-continuation res next result)))
598 (values)))
600 ;;; Add FUNCTIONAL to the COMPONENT-REANALYZE-FUNCTIONALS, unless it's
601 ;;; some trivial type for which reanalysis is a trivial no-op, or
602 ;;; unless it doesn't belong in this component at all.
604 ;;; FUNCTIONAL is returned.
605 (defun maybe-reanalyze-functional (functional)
607 (aver (not (eql (functional-kind functional) :deleted))) ; bug 148
608 (aver-live-component *current-component*)
610 ;; When FUNCTIONAL is of a type for which reanalysis isn't a trivial
611 ;; no-op
612 (when (typep functional '(or optional-dispatch clambda))
614 ;; When FUNCTIONAL knows its component
615 (when (lambda-p functional)
616 (aver (eql (lambda-component functional) *current-component*)))
618 (pushnew functional
619 (component-reanalyze-functionals *current-component*)))
621 functional)
623 ;;; Generate a REF node for LEAF, frobbing the LEAF structure as
624 ;;; needed. If LEAF represents a defined function which has already
625 ;;; been converted, and is not :NOTINLINE, then reference the
626 ;;; functional instead.
627 (defun reference-leaf (start next result leaf &optional (name '.anonymous.))
628 (declare (type ctran start next) (type (or lvar null) result) (type leaf leaf))
629 (assure-leaf-live-p leaf)
630 (let* ((type (lexenv-find leaf type-restrictions))
631 (leaf (or (and (defined-fun-p leaf)
632 (not (eq (defined-fun-inlinep leaf)
633 :notinline))
634 (let ((functional (defined-fun-functional leaf)))
635 (when (and functional (not (functional-kind functional)))
636 (maybe-reanalyze-functional functional))))
637 (when (and (lambda-p leaf)
638 (memq (functional-kind leaf)
639 '(nil :optional)))
640 (maybe-reanalyze-functional leaf))
641 leaf))
642 (ref (make-ref leaf name)))
643 (push ref (leaf-refs leaf))
644 (setf (leaf-ever-used leaf) t)
645 (link-node-to-previous-ctran ref start)
646 (cond (type (let* ((ref-ctran (make-ctran))
647 (ref-lvar (make-lvar))
648 (cast (make-cast ref-lvar
649 (make-single-value-type type)
650 (lexenv-policy *lexenv*))))
651 (setf (lvar-dest ref-lvar) cast)
652 (use-continuation ref ref-ctran ref-lvar)
653 (link-node-to-previous-ctran cast ref-ctran)
654 (use-continuation cast next result)))
655 (t (use-continuation ref next result)))))
657 ;;; Convert a reference to a symbolic constant or variable. If the
658 ;;; symbol is entered in the LEXENV-VARS we use that definition,
659 ;;; otherwise we find the current global definition. This is also
660 ;;; where we pick off symbol macro and alien variable references.
661 (defun ir1-convert-var (start next result name)
662 (declare (type ctran start next) (type (or lvar null) result) (symbol name))
663 (let ((var (or (lexenv-find name vars) (find-free-var name))))
664 (if (and (global-var-p var) (not (info :variable :always-bound name)))
665 ;; KLUDGE: If the variable may be unbound, convert using SYMBOL-VALUE
666 ;; which is not flushable, so that unbound dead variables signal an
667 ;; error (bug 412, lp#722734): checking for null RESULT is not enough,
668 ;; since variables can become dead due to later optimizations.
669 (ir1-convert start next result
670 (if (eq (global-var-kind var) :global)
671 `(symbol-global-value ',name)
672 `(symbol-value ',name)))
673 (etypecase var
674 (leaf
675 (when (lambda-var-p var)
676 (let ((home (ctran-home-lambda-or-null start)))
677 (when home
678 (sset-adjoin var (lambda-calls-or-closes home))))
679 (when (lambda-var-ignorep var)
680 ;; (ANSI's specification for the IGNORE declaration requires
681 ;; that this be a STYLE-WARNING, not a full WARNING.)
682 #-sb-xc-host
683 (compiler-style-warn "reading an ignored variable: ~S" name)
684 ;; there's no need for us to accept ANSI's lameness when
685 ;; processing our own code, though.
686 #+sb-xc-host
687 (warn "reading an ignored variable: ~S" name)))
688 (when (global-var-p var)
689 (check-deprecated-variable name))
690 (reference-leaf start next result var name))
691 (cons
692 (aver (eq (car var) 'macro))
693 ;; FIXME: [Free] type declarations. -- APD, 2002-01-26
694 (ir1-convert start next result (cdr var)))
695 (heap-alien-info
696 (ir1-convert start next result `(%heap-alien ',var))))))
697 (values))
699 ;;; Find a compiler-macro for a form, taking FUNCALL into account.
700 (defun find-compiler-macro (opname form)
701 (if (eq opname 'funcall)
702 (let ((fun-form (cadr form)))
703 (cond ((and (consp fun-form) (eq 'function (car fun-form))
704 (not (cddr fun-form)))
705 (let ((real-fun (cadr fun-form)))
706 (if (legal-fun-name-p real-fun)
707 (values (sb!xc:compiler-macro-function real-fun *lexenv*)
708 real-fun)
709 (values nil nil))))
710 ((sb!xc:constantp fun-form *lexenv*)
711 (let ((fun (constant-form-value fun-form *lexenv*)))
712 (if (legal-fun-name-p fun)
713 ;; CLHS tells us that local functions must shadow
714 ;; compiler-macro-functions, but since the call is
715 ;; through a name, we are obviously interested
716 ;; in the global function.
717 (values (sb!xc:compiler-macro-function fun nil) fun)
718 (values nil nil))))
720 (values nil nil))))
721 (if (legal-fun-name-p opname)
722 (values (sb!xc:compiler-macro-function opname *lexenv*) opname)
723 (values nil nil))))
725 ;;; Picks of special forms and compiler-macro expansions, and hands
726 ;;; the rest to IR1-CONVERT-COMMON-FUNCTOID
727 (defun ir1-convert-functoid (start next result form)
728 (let* ((op (car form))
729 (translator (and (symbolp op) (info :function :ir1-convert op))))
730 (cond (translator
731 (when (sb!xc:compiler-macro-function op *lexenv*)
732 (compiler-warn "ignoring compiler macro for special form"))
733 (funcall translator start next result form))
735 (multiple-value-bind (cmacro-fun cmacro-fun-name)
736 (find-compiler-macro op form)
737 (if (and cmacro-fun
738 ;; CLHS 3.2.2.1.3 specifies that NOTINLINE
739 ;; suppresses compiler-macros.
740 (not (fun-lexically-notinline-p cmacro-fun-name)))
741 (let ((res (handler-case
742 (careful-expand-macro cmacro-fun form t)
743 (compiler-macro-keyword-problem (c)
744 (print-compiler-message *error-output* "note: ~A" (list c))
745 form))))
746 (cond ((eq res form)
747 (ir1-convert-common-functoid start next result form op))
749 (unless (policy *lexenv* (zerop store-xref-data))
750 (record-call cmacro-fun-name (ctran-block start) *current-path*))
751 (ir1-convert start next result res))))
752 (ir1-convert-common-functoid start next result form op)))))))
754 ;;; Handles the "common" cases: any other forms except special forms
755 ;;; and compiler-macros.
756 (defun ir1-convert-common-functoid (start next result form op)
757 (cond ((or (symbolp op) (leaf-p op))
758 (let ((lexical-def (if (leaf-p op) op (lexenv-find op funs))))
759 (typecase lexical-def
760 (null
761 (ir1-convert-global-functoid start next result form op))
762 (functional
763 (ir1-convert-local-combination start next result form
764 lexical-def))
765 (global-var
766 (ir1-convert-srctran start next result lexical-def form))
768 (aver (and (consp lexical-def) (eq (car lexical-def) 'macro)))
769 (ir1-convert start next result
770 (careful-expand-macro (cdr lexical-def) form))))))
771 ((or (atom op) (not (eq (car op) 'lambda)))
772 (compiler-error "illegal function call"))
774 ;; implicitly (LAMBDA ..) because the LAMBDA expression is
775 ;; the CAR of an executed form.
776 (ir1-convert start next result `(%funcall ,@form)))))
778 ;;; Convert anything that looks like a global function call.
779 (defun ir1-convert-global-functoid (start next result form fun)
780 (declare (type ctran start next) (type (or lvar null) result)
781 (list form))
782 ;; FIXME: Couldn't all the INFO calls here be converted into
783 ;; standard CL functions, like MACRO-FUNCTION or something? And what
784 ;; happens with lexically-defined (MACROLET) macros here, anyway?
785 (ecase (info :function :kind fun)
786 (:macro
787 (ir1-convert start next result
788 (careful-expand-macro (info :function :macro-function fun)
789 form))
790 (unless (policy *lexenv* (zerop store-xref-data))
791 (record-macroexpansion fun (ctran-block start) *current-path*)))
792 ((nil :function)
793 (ir1-convert-srctran start next result
794 (find-free-fun fun "shouldn't happen! (no-cmacro)")
795 form))))
797 (defun muffle-warning-or-die ()
798 (muffle-warning)
799 (bug "no MUFFLE-WARNING restart"))
801 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
802 ;;; errors which occur during the macroexpansion.
803 (defun careful-expand-macro (fun form &optional cmacro)
804 (flet (;; Return a string to use as a prefix in error reporting,
805 ;; telling something about which form caused the problem.
806 (wherestring ()
807 (let (;; We rely on the printer to abbreviate FORM.
808 (*print-length* 3)
809 (*print-level* 3))
810 (format nil
811 "~@<~A of ~S. Use ~S to intercept.~%~:@>"
812 (cond (cmacro
813 #-sb-xc-host "Error during compiler-macroexpansion"
814 #+sb-xc-host "Error during XC compiler-macroexpansion")
816 #-sb-xc-host "during macroexpansion"
817 #+sb-xc-host "during XC macroexpansion"))
818 form
819 '*break-on-signals*))))
820 (handler-bind (;; KLUDGE: CMU CL in its wisdom (version 2.4.6 for Debian
821 ;; Linux, anyway) raises a CL:WARNING condition (not a
822 ;; CL:STYLE-WARNING) for undefined symbols when converting
823 ;; interpreted functions, causing COMPILE-FILE to think the
824 ;; file has a real problem, causing COMPILE-FILE to return
825 ;; FAILURE-P set (not just WARNINGS-P set). Since undefined
826 ;; symbol warnings are often harmless forward references,
827 ;; and since it'd be inordinately painful to try to
828 ;; eliminate all such forward references, these warnings
829 ;; are basically unavoidable. Thus, we need to coerce the
830 ;; system to work through them, and this code does so, by
831 ;; crudely suppressing all warnings in cross-compilation
832 ;; macroexpansion. -- WHN 19990412
833 #+(and cmu sb-xc-host)
834 (warning (lambda (c)
835 (compiler-notify
836 "~@<~A~:@_~
837 ~A~:@_~
838 ~@<(KLUDGE: That was a non-STYLE WARNING. ~
839 Ordinarily that would cause compilation to ~
840 fail. However, since we're running under ~
841 CMU CL, and since CMU CL emits non-STYLE ~
842 warnings for safe, hard-to-fix things (e.g. ~
843 references to not-yet-defined functions) ~
844 we're going to have to ignore it and ~
845 proceed anyway. Hopefully we're not ~
846 ignoring anything horrible here..)~:@>~:>"
847 (wherestring)
849 (muffle-warning-or-die)))
850 (error
851 (lambda (c)
852 (cond
853 (cmacro
854 ;; The spec is silent on what we should do. Signaling
855 ;; a full warning but declining to expand seems like
856 ;; a conservative and sane thing to do.
857 (compiler-warn "~@<~A~@:_ ~A~:>" (wherestring) c)
858 (return-from careful-expand-macro form))
860 (compiler-error "~@<~A~@:_ ~A~:>"
861 (wherestring) c))))))
862 (funcall sb!xc:*macroexpand-hook* fun form *lexenv*))))
864 ;;;; conversion utilities
866 ;;; Convert a bunch of forms, discarding all the values except the
867 ;;; last. If there aren't any forms, then translate a NIL.
868 (declaim (ftype (sfunction (ctran ctran (or lvar null) list) (values))
869 ir1-convert-progn-body))
870 (defun ir1-convert-progn-body (start next result body)
871 (if (endp body)
872 (reference-constant start next result nil)
873 (let ((this-start start)
874 (forms body))
875 (loop
876 (let ((form (car forms)))
877 (setf this-start
878 (maybe-instrument-progn-like this-start forms form))
879 (when (endp (cdr forms))
880 (ir1-convert this-start next result form)
881 (return))
882 (let ((this-ctran (make-ctran)))
883 (ir1-convert this-start this-ctran nil form)
884 (setq this-start this-ctran
885 forms (cdr forms)))))))
886 (values))
889 ;;;; code coverage
891 ;;; Check the policy for whether we should generate code coverage
892 ;;; instrumentation. If not, just return the original START
893 ;;; ctran. Otherwise insert code coverage instrumentation after
894 ;;; START, and return the new ctran.
895 (defun instrument-coverage (start mode form)
896 ;; We don't actually use FORM for anything, it's just convenient to
897 ;; have around when debugging the instrumentation.
898 (declare (ignore form))
899 (if (and (policy *lexenv* (> store-coverage-data 0))
900 *code-coverage-records*
901 *allow-instrumenting*)
902 (let ((path (source-path-original-source *current-path*)))
903 (when mode
904 (push mode path))
905 (if (member (ctran-block start)
906 (gethash path *code-coverage-blocks*))
907 ;; If this source path has already been instrumented in
908 ;; this block, don't instrument it again.
909 start
910 (let ((store
911 ;; Get an interned record cons for the path. A cons
912 ;; with the same object identity must be used for
913 ;; each instrument for the same block.
914 (or (gethash path *code-coverage-records*)
915 (setf (gethash path *code-coverage-records*)
916 (cons path +code-coverage-unmarked+))))
917 (next (make-ctran))
918 (*allow-instrumenting* nil))
919 (push (ctran-block start)
920 (gethash path *code-coverage-blocks*))
921 (let ((*allow-instrumenting* nil))
922 (ir1-convert start next nil
923 `(locally
924 (declare (optimize speed
925 (safety 0)
926 (debug 0)
927 (check-constant-modification 0)))
928 ;; We're being naughty here, and
929 ;; modifying constant data. That's ok,
930 ;; we know what we're doing.
931 (%rplacd ',store t))))
932 next)))
933 start))
935 ;;; In contexts where we don't have a source location for FORM
936 ;;; e.g. due to it not being a cons, but where we have a source
937 ;;; location for the enclosing cons, use the latter source location if
938 ;;; available. This works pretty well in practice, since many PROGNish
939 ;;; macroexpansions will just directly splice a block of forms into
940 ;;; some enclosing form with `(progn ,@body), thus retaining the
941 ;;; EQness of the conses.
942 (defun maybe-instrument-progn-like (start forms form)
943 (or (when (and *allow-instrumenting*
944 (not (get-source-path form)))
945 (let ((*current-path* (get-source-path forms)))
946 (when *current-path*
947 (instrument-coverage start nil form))))
948 start))
950 (defun record-code-coverage (info cc)
951 (setf (gethash info *code-coverage-info*) cc))
953 (defun clear-code-coverage ()
954 (clrhash *code-coverage-info*))
956 (defun reset-code-coverage ()
957 (maphash (lambda (info cc)
958 (declare (ignore info))
959 (dolist (cc-entry cc)
960 (setf (cdr cc-entry) +code-coverage-unmarked+)))
961 *code-coverage-info*))
963 (defun code-coverage-record-marked (record)
964 (aver (consp record))
965 (ecase (cdr record)
966 ((#.+code-coverage-unmarked+) nil)
967 ((t) t)))
970 ;;;; converting combinations
972 ;;; Does this form look like something that we should add single-stepping
973 ;;; instrumentation for?
974 (defun step-form-p (form)
975 (flet ((step-symbol-p (symbol)
976 (and (not (member (symbol-package symbol)
977 (load-time-value
978 ;; KLUDGE: packages we're not interested in
979 ;; stepping.
980 (mapcar #'find-package '(sb!c sb!int sb!impl
981 sb!kernel sb!pcl)))))
982 ;; Consistent treatment of *FOO* vs (SYMBOL-VALUE '*FOO*):
983 ;; we insert calls to SYMBOL-VALUE for most non-lexical
984 ;; variable references in order to avoid them being elided
985 ;; if the value is unused.
986 (or (not (member symbol '(symbol-value symbol-global-value)))
987 (not (constantp (second form)))))))
988 (and *allow-instrumenting*
989 (policy *lexenv* (= insert-step-conditions 3))
990 (listp form)
991 (symbolp (car form))
992 (step-symbol-p (car form)))))
994 ;;; Convert a function call where the function FUN is a LEAF. FORM is
995 ;;; the source for the call. We return the COMBINATION node so that
996 ;;; the caller can poke at it if it wants to.
997 (declaim (ftype (sfunction (ctran ctran (or lvar null) list leaf) combination)
998 ir1-convert-combination))
999 (defun ir1-convert-combination (start next result form fun)
1000 (let ((ctran (make-ctran))
1001 (fun-lvar (make-lvar)))
1002 (ir1-convert start ctran fun-lvar `(the (or function symbol) ,fun))
1003 (let ((combination
1004 (ir1-convert-combination-args fun-lvar ctran next result
1005 (cdr form))))
1006 (when (step-form-p form)
1007 ;; Store a string representation of the form in the
1008 ;; combination node. This will let the IR2 translator know
1009 ;; that we want stepper instrumentation for this node. The
1010 ;; string will be stored in the debug-info by DUMP-1-LOCATION.
1011 (setf (combination-step-info combination)
1012 (let ((*print-pretty* t)
1013 (*print-circle* t)
1014 (*print-readably* nil))
1015 (prin1-to-string form))))
1016 combination)))
1018 ;;; Convert the arguments to a call and make the COMBINATION
1019 ;;; node. FUN-LVAR yields the function to call. ARGS is the list of
1020 ;;; arguments for the call, which defaults to the cdr of source. We
1021 ;;; return the COMBINATION node.
1022 (defun ir1-convert-combination-args (fun-lvar start next result args)
1023 (declare (type ctran start next)
1024 (type lvar fun-lvar)
1025 (type (or lvar null) result)
1026 (list args))
1027 (let ((node (make-combination fun-lvar)))
1028 (setf (lvar-dest fun-lvar) node)
1029 (collect ((arg-lvars))
1030 (let ((this-start start)
1031 (forms args))
1032 (dolist (arg args)
1033 (setf this-start
1034 (maybe-instrument-progn-like this-start forms arg))
1035 (setf forms (cdr forms))
1036 (let ((this-ctran (make-ctran))
1037 (this-lvar (make-lvar node)))
1038 (ir1-convert this-start this-ctran this-lvar arg)
1039 (setq this-start this-ctran)
1040 (arg-lvars this-lvar)))
1041 (link-node-to-previous-ctran node this-start)
1042 (use-continuation node next result)
1043 (setf (combination-args node) (arg-lvars))))
1044 node))
1046 ;;; Convert a call to a global function. If not :NOTINLINE, then we do
1047 ;;; source transforms and try out any inline expansion. If there is no
1048 ;;; expansion, but is :INLINE, then give an efficiency note (unless a
1049 ;;; known function which will quite possibly be open-coded.) Next, we
1050 ;;; go to ok-combination conversion.
1051 (defun ir1-convert-srctran (start next result var form)
1052 (declare (type ctran start next) (type (or lvar null) result)
1053 (type global-var var))
1054 (let ((inlinep (when (defined-fun-p var)
1055 (defined-fun-inlinep var))))
1056 (if (eq inlinep :notinline)
1057 (ir1-convert-combination start next result form var)
1058 (let* ((name (leaf-source-name var))
1059 (transform (info :function :source-transform name)))
1060 (if transform
1061 (multiple-value-bind (transformed pass) (funcall transform form)
1062 (cond (pass
1063 (ir1-convert-maybe-predicate start next result form var))
1065 (unless (policy *lexenv* (zerop store-xref-data))
1066 (record-call name (ctran-block start) *current-path*))
1067 (ir1-convert start next result transformed))))
1068 (ir1-convert-maybe-predicate start next result form var))))))
1070 ;;; KLUDGE: If we insert a synthetic IF for a function with the PREDICATE
1071 ;;; attribute, don't generate any branch coverage instrumentation for it.
1072 (defvar *instrument-if-for-code-coverage* t)
1074 ;;; If the function has the PREDICATE attribute, and the RESULT's DEST
1075 ;;; isn't an IF, then we convert (IF <form> T NIL), ensuring that a
1076 ;;; predicate always appears in a conditional context.
1078 ;;; If the function isn't a predicate, then we call
1079 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
1080 (defun ir1-convert-maybe-predicate (start next result form var)
1081 (declare (type ctran start next)
1082 (type (or lvar null) result)
1083 (list form)
1084 (type global-var var))
1085 (let ((info (info :function :info (leaf-source-name var))))
1086 (if (and info
1087 (ir1-attributep (fun-info-attributes info) predicate)
1088 (not (if-p (and result (lvar-dest result)))))
1089 (let ((*instrument-if-for-code-coverage* nil))
1090 (ir1-convert start next result `(if ,form t nil)))
1091 (ir1-convert-combination-checking-type start next result form var))))
1093 ;;; Actually really convert a global function call that we are allowed
1094 ;;; to early-bind.
1096 ;;; If we know the function type of the function, then we check the
1097 ;;; call for syntactic legality with respect to the declared function
1098 ;;; type. If it is impossible to determine whether the call is correct
1099 ;;; due to non-constant keywords, then we give up, marking the call as
1100 ;;; :FULL to inhibit further error messages. We return true when the
1101 ;;; call is legal.
1103 ;;; If the call is legal, we also propagate type assertions from the
1104 ;;; function type to the arg and result lvars. We do this now so that
1105 ;;; IR1 optimize doesn't have to redundantly do the check later so
1106 ;;; that it can do the type propagation.
1107 (defun ir1-convert-combination-checking-type (start next result form var)
1108 (declare (type ctran start next) (type (or lvar null) result)
1109 (list form)
1110 (type leaf var))
1111 (let* ((node (ir1-convert-combination start next result form var))
1112 (fun-lvar (basic-combination-fun node))
1113 (type (leaf-type var)))
1114 (when (validate-call-type node type var t)
1115 (setf (lvar-%derived-type fun-lvar)
1116 (make-single-value-type type))
1117 (setf (lvar-reoptimize fun-lvar) nil)))
1118 (values))
1120 ;;; Convert a call to a local function, or if the function has already
1121 ;;; been LET converted, then throw FUNCTIONAL to
1122 ;;; LOCALL-ALREADY-LET-CONVERTED. The THROW should only happen when we
1123 ;;; are converting inline expansions for local functions during
1124 ;;; optimization.
1125 (defun ir1-convert-local-combination (start next result form functional)
1126 (assure-functional-live-p functional)
1127 (ir1-convert-combination start next result
1128 form
1129 (maybe-reanalyze-functional functional)))
1131 ;;;; PROCESS-DECLS
1133 ;;; Given a list of LAMBDA-VARs and a variable name, return the
1134 ;;; LAMBDA-VAR for that name, or NIL if it isn't found. We return the
1135 ;;; *last* variable with that name, since LET* bindings may be
1136 ;;; duplicated, and declarations always apply to the last.
1137 (declaim (ftype (sfunction (list symbol) (or lambda-var list))
1138 find-in-bindings))
1139 (defun find-in-bindings (vars name)
1140 (let ((found nil))
1141 (dolist (var vars)
1142 (cond ((leaf-p var)
1143 (when (eq (leaf-source-name var) name)
1144 (setq found var))
1145 (let ((info (lambda-var-arg-info var)))
1146 (when info
1147 (let ((supplied-p (arg-info-supplied-p info)))
1148 (when (and supplied-p
1149 (eq (leaf-source-name supplied-p) name))
1150 (setq found supplied-p))))))
1151 ((and (consp var) (eq (car var) name))
1152 (setf found (cdr var)))))
1153 found))
1155 ;;; Called by PROCESS-DECLS to deal with a variable type declaration.
1156 ;;; If a LAMBDA-VAR being bound, we intersect the type with the var's
1157 ;;; type, otherwise we add a type restriction on the var. If a symbol
1158 ;;; macro, we just wrap a THE around the expansion.
1159 (defun process-type-decl (decl res vars context)
1160 (declare (list decl vars) (type lexenv res))
1161 (let ((type (compiler-specifier-type (first decl))))
1162 (collect ((restr nil cons)
1163 (new-vars nil cons))
1164 (dolist (var-name (rest decl))
1165 (when (boundp var-name)
1166 (program-assert-symbol-home-package-unlocked
1167 context var-name "declaring the type of ~A"))
1168 (let* ((bound-var (find-in-bindings vars var-name))
1169 (var (or bound-var
1170 (lexenv-find var-name vars)
1171 (find-free-var var-name))))
1172 (etypecase var
1173 (leaf
1174 (flet
1175 ((process-var (var bound-var)
1176 (let* ((old-type (or (lexenv-find var type-restrictions)
1177 (leaf-type var)))
1178 (int (if (or (fun-type-p type)
1179 (fun-type-p old-type))
1180 type
1181 (type-approx-intersection2
1182 old-type type))))
1183 (cond ((eq int *empty-type*)
1184 (unless (policy *lexenv* (= inhibit-warnings 3))
1185 (warn
1186 'type-warning
1187 :format-control
1188 "The type declarations ~S and ~S for ~S conflict."
1189 :format-arguments
1190 (list
1191 (type-specifier old-type)
1192 (type-specifier type)
1193 var-name))))
1194 (bound-var
1195 (setf (leaf-type bound-var) int
1196 (leaf-where-from bound-var) :declared))
1198 (restr (cons var int)))))))
1199 (process-var var bound-var)
1200 (awhen (and (lambda-var-p var)
1201 (lambda-var-specvar var))
1202 (process-var it nil))))
1203 (cons
1204 ;; FIXME: non-ANSI weirdness
1205 (aver (eq (car var) 'macro))
1206 (new-vars `(,var-name . (macro . (the ,(first decl)
1207 ,(cdr var))))))
1208 (heap-alien-info
1209 (compiler-error
1210 "~S is an alien variable, so its type can't be declared."
1211 var-name)))))
1213 (if (or (restr) (new-vars))
1214 (make-lexenv :default res
1215 :type-restrictions (restr)
1216 :vars (new-vars))
1217 res))))
1219 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
1220 ;;; declarations for function variables. In addition to allowing
1221 ;;; declarations for functions being bound, we must also deal with
1222 ;;; declarations that constrain the type of lexically apparent
1223 ;;; functions.
1224 (defun process-ftype-decl (spec res names fvars context)
1225 (declare (type list names fvars)
1226 (type lexenv res))
1227 (let ((type (compiler-specifier-type spec)))
1228 (unless (csubtypep type (specifier-type 'function))
1229 (compiler-style-warn "ignoring declared FTYPE: ~S (not a function type)" spec)
1230 (return-from process-ftype-decl res))
1231 (collect ((res nil cons))
1232 (dolist (name names)
1233 (when (fboundp name)
1234 (program-assert-symbol-home-package-unlocked
1235 context name "declaring the ftype of ~A"))
1236 (let ((found (find name fvars :key #'leaf-source-name :test #'equal)))
1237 (cond
1238 (found
1239 (setf (leaf-type found) type)
1240 (assert-definition-type found type
1241 :unwinnage-fun #'compiler-notify
1242 :where "FTYPE declaration"))
1244 (res (cons (find-lexically-apparent-fun
1245 name "in a function type declaration")
1246 type))))))
1247 (if (res)
1248 (make-lexenv :default res :type-restrictions (res))
1249 res))))
1251 ;;; Process a special declaration, returning a new LEXENV. A non-bound
1252 ;;; special declaration is instantiated by throwing a special variable
1253 ;;; into the variables if BINDING-FORM-P is NIL, or otherwise into
1254 ;;; *POST-BINDING-VARIABLE-LEXENV*.
1255 (defun process-special-decl (spec res vars binding-form-p context)
1256 (declare (list spec vars) (type lexenv res))
1257 (collect ((new-venv nil cons))
1258 (dolist (name (cdr spec))
1259 ;; While CLHS seems to allow local SPECIAL declarations for constants,
1260 ;; whatever the semantics are supposed to be is not at all clear to me
1261 ;; -- since constants aren't allowed to be bound it should be a no-op as
1262 ;; no-one can observe the difference portably, but specials are allowed
1263 ;; to be bound... yet nowhere does it say that the special declaration
1264 ;; removes the constantness. Call it a spec bug and prohibit it. Same
1265 ;; for GLOBAL variables.
1266 (let ((kind (info :variable :kind name)))
1267 (unless (member kind '(:special :unknown))
1268 (error "Can't declare ~(~A~) variable locally special: ~S" kind name)))
1269 (program-assert-symbol-home-package-unlocked
1270 context name "declaring ~A special")
1271 (let ((var (find-in-bindings vars name)))
1272 (etypecase var
1273 (cons
1274 (aver (eq (car var) 'macro))
1275 (compiler-error
1276 "~S is a symbol-macro and thus can't be declared special."
1277 name))
1278 (lambda-var
1279 (when (lambda-var-ignorep var)
1280 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1281 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1282 (compiler-style-warn
1283 "The ignored variable ~S is being declared special."
1284 name))
1285 (setf (lambda-var-specvar var)
1286 (specvar-for-binding name)))
1287 (null
1288 (unless (or (assoc name (new-venv) :test #'eq))
1289 (new-venv (cons name (specvar-for-binding name))))))))
1290 (cond (binding-form-p
1291 (setf *post-binding-variable-lexenv*
1292 (append (new-venv) *post-binding-variable-lexenv*))
1293 res)
1294 ((new-venv)
1295 (make-lexenv :default res :vars (new-venv)))
1297 res))))
1299 ;;; Return a DEFINED-FUN which copies a GLOBAL-VAR but for its INLINEP
1300 ;;; (and TYPE if notinline), plus type-restrictions from the lexenv.
1301 (defun make-new-inlinep (var inlinep local-type)
1302 (declare (type global-var var) (type inlinep inlinep))
1303 (let* ((type (if (and (eq inlinep :notinline)
1304 (not (eq (leaf-where-from var) :declared)))
1305 (specifier-type 'function)
1306 (leaf-type var)))
1307 (res (make-defined-fun
1308 :%source-name (leaf-source-name var)
1309 :where-from (leaf-where-from var)
1310 :type (if local-type
1311 (type-intersection local-type type)
1312 type)
1313 :inlinep inlinep)))
1314 (when (defined-fun-p var)
1315 (setf (defined-fun-inline-expansion res)
1316 (defined-fun-inline-expansion var))
1317 (setf (defined-fun-functionals res)
1318 (defined-fun-functionals var)))
1319 ;; FIXME: Is this really right? Needs we not set the FUNCTIONAL
1320 ;; to the original global-var?
1321 res))
1323 ;;; Parse an inline/notinline declaration. If it's a local function we're
1324 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
1325 (defun process-inline-decl (spec res fvars)
1326 (let ((sense (cdr (assoc (first spec) *inlinep-translations* :test #'eq)))
1327 (new-fenv ()))
1328 (dolist (name (rest spec))
1329 (let ((fvar (find name fvars :key #'leaf-source-name :test #'equal)))
1330 (if fvar
1331 (setf (functional-inlinep fvar) sense)
1332 (let ((found (find-lexically-apparent-fun
1333 name "in an inline or notinline declaration")))
1334 (etypecase found
1335 (functional
1336 (when (policy *lexenv* (>= speed inhibit-warnings))
1337 (compiler-notify "ignoring ~A declaration not at ~
1338 definition of local function:~% ~S"
1339 sense name)))
1340 (global-var
1341 (let ((type
1342 (cdr (assoc found (lexenv-type-restrictions res)))))
1343 (push (cons name (make-new-inlinep found sense type))
1344 new-fenv))))))))
1345 (if new-fenv
1346 (make-lexenv :default res :funs new-fenv)
1347 res)))
1349 ;;; like FIND-IN-BINDINGS, but looks for #'FOO in the FVARS
1350 (defun find-in-bindings-or-fbindings (name vars fvars)
1351 (declare (list vars fvars))
1352 (typecase name
1353 (atom
1354 (find-in-bindings vars name))
1355 ((cons (eql function) (cons * null))
1356 (find (cadr name) fvars :key #'leaf-source-name :test #'equal))
1358 (compiler-error "Malformed function or variable name ~S." name))))
1360 ;;; Process an ignore/ignorable declaration, checking for various losing
1361 ;;; conditions.
1362 (defun process-ignore-decl (spec vars fvars)
1363 (declare (list spec vars fvars))
1364 (dolist (name (rest spec))
1365 (let ((var (find-in-bindings-or-fbindings name vars fvars)))
1366 (cond
1367 ((not var)
1368 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1369 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1370 (multiple-value-call #'compiler-style-warn
1371 "~A declaration for ~A: ~A"
1372 (first spec)
1373 (if (symbolp name)
1374 (values
1375 (case (info :variable :kind name)
1376 (:special "a special variable")
1377 (:global "a global lexical variable")
1378 (:alien "a global alien variable")
1379 (t "an unknown variable"))
1380 name)
1381 (values
1382 (if (info :function :kind (second name))
1383 "a global function"
1384 "an unknown function")
1385 (second name)))))
1386 ((and (consp var) (eq (car var) 'macro))
1387 ;; Just ignore the IGNORE decl: we don't currently signal style-warnings
1388 ;; for unused symbol-macros, so there's no need to do anything.
1390 ((functional-p var)
1391 (setf (leaf-ever-used var) t))
1392 ((and (lambda-var-specvar var) (eq (first spec) 'ignore))
1393 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1394 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1395 (compiler-style-warn "Declaring special variable ~S to be ~A"
1396 name
1397 (first spec)))
1398 ((eq (first spec) 'ignorable)
1399 (setf (leaf-ever-used var) t))
1401 (setf (lambda-var-ignorep var) t)))))
1402 (values))
1404 (defun process-extent-decl (names vars fvars kind)
1405 (let ((extent
1406 (ecase kind
1407 (truly-dynamic-extent
1408 :always-dynamic)
1409 (dynamic-extent
1410 (when *stack-allocate-dynamic-extent*
1411 :maybe-dynamic))
1412 (indefinite-extent
1413 :indefinite))))
1414 (if extent
1415 (dolist (name names)
1416 (cond
1417 ((symbolp name)
1418 (let* ((bound-var (find-in-bindings vars name))
1419 (var (or bound-var
1420 (lexenv-find name vars)
1421 (maybe-find-free-var name))))
1422 (etypecase var
1423 (leaf
1424 (if bound-var
1425 (if (and (leaf-extent var) (neq extent (leaf-extent var)))
1426 (warn "Multiple incompatible extent declarations for ~S?" name)
1427 (setf (leaf-extent var) extent))
1428 (compiler-notify
1429 "Ignoring free ~S declaration: ~S" kind name)))
1430 (cons
1431 (compiler-error "~S on symbol-macro: ~S" kind name))
1432 (heap-alien-info
1433 (compiler-error "~S on alien-variable: ~S" kind name))
1434 (null
1435 (compiler-style-warn
1436 "Unbound variable declared ~S: ~S" kind name)))))
1437 ((and (consp name)
1438 (eq (car name) 'function)
1439 (null (cddr name))
1440 (valid-function-name-p (cadr name))
1441 (neq :indefinite extent))
1442 (let* ((fname (cadr name))
1443 (bound-fun (find fname fvars
1444 :key #'leaf-source-name
1445 :test #'equal))
1446 (fun (or bound-fun (lexenv-find fname funs))))
1447 (etypecase fun
1448 (leaf
1449 (if bound-fun
1450 #!+stack-allocatable-closures
1451 (setf (leaf-extent bound-fun) extent)
1452 #!-stack-allocatable-closures
1453 (compiler-notify
1454 "Ignoring DYNAMIC-EXTENT declaration on function ~S ~
1455 (not supported on this platform)." fname)
1456 (compiler-notify
1457 "Ignoring free DYNAMIC-EXTENT declaration: ~S" name)))
1458 (cons
1459 (compiler-error "DYNAMIC-EXTENT on macro: ~S" name))
1460 (null
1461 (compiler-style-warn
1462 "Unbound function declared DYNAMIC-EXTENT: ~S" name)))))
1464 (compiler-error "~S on a weird thing: ~S" kind name))))
1465 (when (policy *lexenv* (= speed 3))
1466 (compiler-notify "Ignoring DYNAMIC-EXTENT declarations: ~S" names)))))
1468 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
1469 ;;; go away, I think.
1470 (defvar *suppress-values-declaration* nil
1471 #!+sb-doc
1472 "If true, processing of the VALUES declaration is inhibited.")
1474 ;;; Process a single declaration spec, augmenting the specified LEXENV
1475 ;;; RES. Return RES and result type. VARS and FVARS are as described
1476 ;;; PROCESS-DECLS.
1477 (defun process-1-decl (raw-spec res vars fvars binding-form-p context)
1478 (declare (type list raw-spec vars fvars))
1479 (declare (type lexenv res))
1480 (let ((spec (canonized-decl-spec raw-spec))
1481 (result-type *wild-type*))
1482 (values
1483 (case (first spec)
1484 (special (process-special-decl spec res vars binding-form-p context))
1485 (ftype
1486 (unless (cdr spec)
1487 (compiler-error "no type specified in FTYPE declaration: ~S" spec))
1488 (process-ftype-decl (second spec) res (cddr spec) fvars context))
1489 ((inline notinline maybe-inline)
1490 (process-inline-decl spec res fvars))
1491 ((ignore ignorable)
1492 (process-ignore-decl spec vars fvars)
1493 res)
1494 (optimize
1495 (make-lexenv
1496 :default res
1497 :policy (process-optimize-decl spec (lexenv-policy res))))
1498 (muffle-conditions
1499 (make-lexenv
1500 :default res
1501 :handled-conditions (process-muffle-conditions-decl
1502 spec (lexenv-handled-conditions res))))
1503 (unmuffle-conditions
1504 (make-lexenv
1505 :default res
1506 :handled-conditions (process-unmuffle-conditions-decl
1507 spec (lexenv-handled-conditions res))))
1508 (type
1509 (process-type-decl (cdr spec) res vars context))
1510 (values
1511 (unless *suppress-values-declaration*
1512 (let ((types (cdr spec)))
1513 (setq result-type
1514 (compiler-values-specifier-type
1515 (if (singleton-p types)
1516 (car types)
1517 `(values ,@types)))))
1518 res))
1519 ((dynamic-extent truly-dynamic-extent indefinite-extent)
1520 (process-extent-decl (cdr spec) vars fvars (first spec))
1521 res)
1522 ((disable-package-locks enable-package-locks)
1523 (make-lexenv
1524 :default res
1525 :disabled-package-locks (process-package-lock-decl
1526 spec (lexenv-disabled-package-locks res))))
1528 (unless (info :declaration :recognized (first spec))
1529 (compiler-warn "unrecognized declaration ~S" raw-spec))
1530 (let ((fn (info :declaration :handler (first spec))))
1531 (if fn
1532 (funcall fn res spec vars fvars)
1533 res))))
1534 result-type)))
1536 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1537 ;;; and FUNCTIONAL structures which are being bound. In addition to
1538 ;;; filling in slots in the leaf structures, we return a new LEXENV,
1539 ;;; which reflects pervasive special and function type declarations,
1540 ;;; (NOT)INLINE declarations and OPTIMIZE declarations, and type of
1541 ;;; VALUES declarations. If BINDING-FORM-P is true, the third return
1542 ;;; value is a list of VARs that should not apply to the lexenv of the
1543 ;;; initialization forms for the bindings, but should apply to the body.
1545 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1546 ;;; of LOCALLY.
1547 (defun process-decls (decls vars fvars &key
1548 (lexenv *lexenv*) (binding-form-p nil) (context :compile))
1549 (declare (list decls vars fvars))
1550 (let ((result-type *wild-type*)
1551 (*post-binding-variable-lexenv* nil))
1552 (dolist (decl decls)
1553 (dolist (spec (rest decl))
1554 (progv
1555 ;; Kludge: EVAL calls this function to deal with LOCALLY.
1556 (when (eq context :compile) (list '*current-path*))
1557 (when (eq context :compile) (list (or (get-source-path spec)
1558 (get-source-path decl)
1559 *current-path*)))
1560 (unless (consp spec)
1561 (compiler-error "malformed declaration specifier ~S in ~S" spec decl))
1562 (multiple-value-bind (new-env new-result-type)
1563 (process-1-decl spec lexenv vars fvars binding-form-p context)
1564 (setq lexenv new-env)
1565 (unless (eq new-result-type *wild-type*)
1566 (setq result-type
1567 (values-type-intersection result-type new-result-type)))))))
1568 (values lexenv result-type *post-binding-variable-lexenv*)))
1570 (defun %processing-decls (decls vars fvars ctran lvar binding-form-p fun)
1571 (multiple-value-bind (*lexenv* result-type post-binding-lexenv)
1572 (process-decls decls vars fvars :binding-form-p binding-form-p)
1573 (cond ((eq result-type *wild-type*)
1574 (funcall fun ctran lvar post-binding-lexenv))
1576 (let ((value-ctran (make-ctran))
1577 (value-lvar (make-lvar)))
1578 (multiple-value-prog1
1579 (funcall fun value-ctran value-lvar post-binding-lexenv)
1580 (let ((cast (make-cast value-lvar result-type
1581 (lexenv-policy *lexenv*))))
1582 (link-node-to-previous-ctran cast value-ctran)
1583 (setf (lvar-dest value-lvar) cast)
1584 (use-continuation cast ctran lvar))))))))
1585 (defmacro processing-decls ((decls vars fvars ctran lvar
1586 &optional post-binding-lexenv)
1587 &body forms)
1588 (check-type ctran symbol)
1589 (check-type lvar symbol)
1590 (let ((post-binding-lexenv-p (not (null post-binding-lexenv)))
1591 (post-binding-lexenv (or post-binding-lexenv (sb!xc:gensym "LEXENV"))))
1592 `(%processing-decls ,decls ,vars ,fvars ,ctran ,lvar
1593 ,post-binding-lexenv-p
1594 (lambda (,ctran ,lvar ,post-binding-lexenv)
1595 (declare (ignorable ,post-binding-lexenv))
1596 ,@forms))))
1598 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1599 ;;; declaration. If there is a global variable of that name, then
1600 ;;; check that it isn't a constant and return it. Otherwise, create an
1601 ;;; anonymous GLOBAL-VAR.
1602 (defun specvar-for-binding (name)
1603 (cond ((not (eq (info :variable :where-from name) :assumed))
1604 (let ((found (find-free-var name)))
1605 (when (heap-alien-info-p found)
1606 (compiler-error
1607 "~S is an alien variable and so can't be declared special."
1608 name))
1609 (unless (global-var-p found)
1610 (compiler-error
1611 "~S is a constant and so can't be declared special."
1612 name))
1613 found))
1615 (make-global-var :kind :special
1616 :%source-name name
1617 :where-from :declared))))