1 ;;;; This file contains code which does the translation from Lisp code
2 ;;;; to the first intermediate representation (IR1).
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
15 (declaim (special *compiler-error-bailout
*))
17 ;;; *CURRENT-FORM-NUMBER* is used in FIND-SOURCE-PATHS to compute the
18 ;;; form number to associate with a source path. This should be bound
19 ;;; to an initial value of 0 before the processing of each truly
21 (declaim (type index
*current-form-number
*))
22 (defvar *current-form-number
*)
24 ;;; *SOURCE-PATHS* is a hashtable from source code forms to the path
25 ;;; taken through the source to reach the form. This provides a way to
26 ;;; keep track of the location of original source forms, even when
27 ;;; macroexpansions and other arbitary permutations of the code
28 ;;; happen. This table is initialized by calling FIND-SOURCE-PATHS on
29 ;;; the original source.
31 ;;; It is fairly useless to store symbols, characters, or fixnums in
32 ;;; this table, as 42 is EQ to 42 no matter where in the source it
33 ;;; appears. GET-SOURCE-PATH and NOTE-SOURCE-PATH functions should be
34 ;;; always used to access this table.
35 (declaim (hash-table *source-paths
*))
36 (defvar *source-paths
*)
38 (declaim (inline source-form-has-path-p
))
39 (defun source-form-has-path-p (form)
40 (not (typep form
'(or symbol fixnum character
))))
42 (defun get-source-path (form)
43 (when (source-form-has-path-p form
)
44 (gethash form
*source-paths
*)))
46 (defun ensure-source-path (form)
47 (or (get-source-path form
)
48 (cons (simplify-source-path-form form
)
51 (defun simplify-source-path-form (form)
53 (let ((op (car form
)))
54 ;; In the compiler functions can be directly represented
55 ;; by leaves. Having leaves in the source path is pretty
56 ;; hard on the poor user, however, so replace with the
57 ;; source-name when possible.
58 (if (and (leaf-p op
) (leaf-has-source-name-p op
))
59 (cons (leaf-source-name op
) (cdr form
))
63 (defun note-source-path (form &rest arguments
)
64 (when (source-form-has-path-p form
)
65 (setf (gethash form
*source-paths
*)
66 (apply #'list
* 'original-source-start
*current-form-number
* arguments
))))
68 ;;; *CURRENT-COMPONENT* is the COMPONENT structure which we link
69 ;;; blocks into as we generate them. This just serves to glue the
70 ;;; emitted blocks together until local call analysis and flow graph
71 ;;; canonicalization figure out what is really going on. We need to
72 ;;; keep track of all the blocks generated so that we can delete them
73 ;;; if they turn out to be unreachable.
75 ;;; FIXME: It's confusing having one variable named *CURRENT-COMPONENT*
76 ;;; and another named *COMPONENT-BEING-COMPILED*. (In CMU CL they
77 ;;; were called *CURRENT-COMPONENT* and *COMPILE-COMPONENT* respectively,
78 ;;; which was also confusing.)
79 (declaim (type (or component null
) *current-component
*))
80 (defvar *current-component
*)
82 ;;; *CURRENT-PATH* is the source path of the form we are currently
83 ;;; translating. See NODE-SOURCE-PATH in the NODE structure.
84 (declaim (list *current-path
*))
85 (defvar *current-path
*)
87 (defvar *derive-function-types
* nil
89 "Should the compiler assume that function types will never change,
90 so that it can use type information inferred from current definitions
91 to optimize code which uses those definitions? Setting this true
92 gives non-ANSI, early-CMU-CL behavior. It can be useful for improving
93 the efficiency of stable code.")
95 (defvar *fun-names-in-this-file
* nil
)
97 (defvar *post-binding-variable-lexenv
* nil
)
99 ;;;; namespace management utilities
101 (defun fun-lexically-notinline-p (name)
102 (let ((fun (lexenv-find name funs
:test
#'equal
)))
103 ;; a declaration will trump a proclamation
104 (if (and fun
(defined-fun-p fun
))
105 (eq (defined-fun-inlinep fun
) :notinline
)
106 (eq (info :function
:inlinep name
) :notinline
))))
108 ;; This will get redefined in PCL boot.
109 (declaim (notinline maybe-update-info-for-gf
))
110 (defun maybe-update-info-for-gf (name)
111 (declare (ignore name
))
114 (defun maybe-defined-here (name where
)
115 (if (and (eq :defined where
)
116 (member name
*fun-names-in-this-file
* :test
#'equal
))
120 ;;; Return a GLOBAL-VAR structure usable for referencing the global
122 (defun find-global-fun (name latep
)
123 (unless (info :function
:kind name
)
124 (setf (info :function
:kind name
) :function
)
125 (setf (info :function
:where-from name
) :assumed
))
126 (let ((where (info :function
:where-from name
)))
127 (when (and (eq where
:assumed
)
128 ;; In the ordinary target Lisp, it's silly to report
129 ;; undefinedness when the function is defined in the
130 ;; running Lisp. But at cross-compile time, the current
131 ;; definedness of a function is irrelevant to the
132 ;; definedness at runtime, which is what matters.
133 #-sb-xc-host
(not (fboundp name
))
134 ;; LATEP is true when the user has indicated that
135 ;; late-late binding is desired by using eg. a quoted
136 ;; symbol -- in which case it makes little sense to
137 ;; complain about undefined functions.
139 (note-undefined-reference name
:function
))
140 (let ((ftype (info :function
:type name
))
141 (notinline (fun-lexically-notinline-p name
)))
143 :kind
:global-function
145 :type
(if (or (eq where
:declared
)
148 *derive-function-types
*))
150 (specifier-type 'function
))
151 :defined-type
(if (and (not latep
) (not notinline
))
152 (or (maybe-update-info-for-gf name
) ftype
)
153 (specifier-type 'function
))
154 :where-from
(if notinline
156 (maybe-defined-here name where
))))))
158 ;;; Have some DEFINED-FUN-FUNCTIONALS of a *FREE-FUNS* entry become invalid?
161 ;;; This was added to fix bug 138 in SBCL. It is possible for a *FREE-FUNS*
162 ;;; entry to contain a DEFINED-FUN whose DEFINED-FUN-FUNCTIONAL object
163 ;;; contained IR1 stuff (NODEs, BLOCKs...) referring to an already compiled
164 ;;; (aka "dead") component. When this IR1 stuff was reused in a new component,
165 ;;; under further obscure circumstances it could be used by
166 ;;; WITH-IR1-ENVIRONMENT-FROM-NODE to generate a binding for
167 ;;; *CURRENT-COMPONENT*. At that point things got all confused, since IR1
168 ;;; conversion was sending code to a component which had already been compiled
169 ;;; and would never be compiled again.
171 ;;; Note: as of 1.0.24.41 this seems to happen only in XC, and the original
172 ;;; BUGS entry also makes it seem like this might not be an issue at all on
174 (defun clear-invalid-functionals (free-fun)
175 ;; There might be other reasons that *FREE-FUN* entries could
176 ;; become invalid, but the only one we've been bitten by so far
177 ;; (sbcl-0.pre7.118) is this one:
178 (when (defined-fun-p free-fun
)
179 (setf (defined-fun-functionals free-fun
)
180 (delete-if (lambda (functional)
181 (or (eq (functional-kind functional
) :deleted
)
182 (when (lambda-p functional
)
184 ;; (The main reason for this first test is to bail
185 ;; out early in cases where the LAMBDA-COMPONENT
186 ;; call in the second test would fail because links
187 ;; it needs are uninitialized or invalid.)
189 ;; If the BIND node for this LAMBDA is null, then
190 ;; according to the slot comments, the LAMBDA has
191 ;; been deleted or its call has been deleted. In
192 ;; that case, it seems rather questionable to reuse
193 ;; it, and certainly it shouldn't be necessary to
194 ;; reuse it, so we cheerfully declare it invalid.
195 (not (lambda-bind functional
))
196 ;; If this IR1 stuff belongs to a dead component,
197 ;; then we can't reuse it without getting into
198 ;; bizarre confusion.
199 (eq (component-info (lambda-component functional
))
201 (defined-fun-functionals free-fun
)))
204 ;;; If NAME already has a valid entry in *FREE-FUNS*, then return
205 ;;; the value. Otherwise, make a new GLOBAL-VAR using information from
206 ;;; the global environment and enter it in *FREE-FUNS*. If NAME
207 ;;; names a macro or special form, then we error out using the
208 ;;; supplied context which indicates what we were trying to do that
209 ;;; demanded a function.
210 (declaim (ftype (sfunction (t string
) global-var
) find-free-fun
))
211 (defun find-free-fun (name context
)
212 (or (let ((old-free-fun (gethash name
*free-funs
*)))
214 (clear-invalid-functionals old-free-fun
)
216 (ecase (info :function
:kind name
)
217 ;; FIXME: The :MACRO and :SPECIAL-FORM cases could be merged.
219 (compiler-error "The macro name ~S was found ~A." name context
))
221 (compiler-error "The special form name ~S was found ~A."
225 (check-fun-name 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
)))
233 :inline-expansion expansion
235 :where-from
(if (eq inlinep
:notinline
)
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
)))
251 (aver (and (consp var
) (eq (car var
) 'macro
)))
252 (compiler-error "found macro name ~S ~A" name context
))
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 (deprecation-state (deprecated-thing-p 'variable name
)))
274 (when (and (eq kind
:unknown
) (not deprecation-state
))
275 (note-undefined-reference name
:variable
))
276 (setf (gethash name
*free-vars
*)
279 (info :variable
:alien-info name
))
280 ;; FIXME: The return value in this case should really be
281 ;; of type SB!C::LEAF. I don't feel too badly about it,
282 ;; because the MACRO idiom is scattered throughout this
283 ;; file, but it should be cleaned up so we're not
284 ;; throwing random conses around. --njf 2002-03-23
286 (let ((expansion (info :variable
:macro-expansion name
))
287 (type (type-specifier (info :variable
:type name
))))
288 `(macro .
(the ,type
,expansion
))))
290 (let ((value (symbol-value name
)))
291 ;; Override the values of standard symbols in XC,
292 ;; since we can't redefine them.
294 (when (eql (find-symbol (symbol-name name
) :cl
) name
)
295 (multiple-value-bind (xc-value foundp
)
296 (xc-constant-value name
)
298 (setf value xc-value
))
299 ((not (eq value name
))
301 "Using cross-compilation host's definition of ~S: ~A~%"
302 name
(symbol-value name
))))))
303 (find-constant value name
)))
305 (make-global-var :kind kind
308 :where-from where-from
)))))))
310 ;;; Grovel over CONSTANT checking for any sub-parts that need to be
311 ;;; processed with MAKE-LOAD-FORM. We have to be careful, because
312 ;;; CONSTANT might be circular. We also check that the constant (and
313 ;;; any subparts) are dumpable at all.
314 (defun maybe-emit-make-load-forms (constant &optional
(name nil namep
))
315 (let ((xset (alloc-xset)))
316 (labels ((trivialp (value)
320 (or unboxed-array
#!+sb-simd-pack simd-pack
)
322 (and array
(not (array t
)))
326 string
))) ; subsumed by UNBOXED-ARRAY
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
))
337 (grovel (cdr value
)))
339 (dotimes (i (length value
))
340 (grovel (svref value i
))))
342 (dotimes (i (length value
))
343 (grovel (aref value i
))))
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
))))
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 ;; Behold the wonderfully clear sense of this-
362 ;; WHEN (EMIT-MAKE-LOAD-FORM VALUE)
363 ;; meaning "when you're _NOT_ using a custom load-form"
365 ;; FIXME: What about funcallable instances with
366 ;; user-defined MAKE-LOAD-FORM methods?
367 (when (emit-make-load-form value
)
369 (aver (zerop (layout-raw-slot-metadata
370 (%instance-layout value
))))
371 (do-instance-tagged-slot (i value
)
372 (grovel (%instance-ref value i
)))))
373 ;; The cross-compiler can dump certain instances that are not
374 ;; subtypes of STRUCTURE!OBJECT, as long as it has processed
377 ((satisfies sb
!kernel
::xc-dumpable-structure-instance-p
)
378 (do-instance-tagged-slot (i value
)
379 (grovel (%instance-ref value i
))))
382 "Objects of type ~S can't be dumped into fasl files."
383 (type-of value
)))))))
384 ;; Dump all non-trivial named constants using the name.
385 (if (and namep
(not (typep constant
'(or symbol character
386 ;; FIXME: Cold init breaks if we
387 ;; try to reference FP constants
390 #-sb-xc-host fixnum
))))
391 (emit-make-load-form constant name
)
395 ;;;; some flow-graph hacking utilities
397 ;;; This function sets up the back link between the node and the
398 ;;; ctran which continues at it.
399 (defun link-node-to-previous-ctran (node ctran
)
400 (declare (type node node
) (type ctran ctran
))
401 (aver (not (ctran-next ctran
)))
402 (setf (ctran-next ctran
) node
)
403 (setf (node-prev node
) ctran
))
405 ;;; This function is used to set the ctran for a node, and thus
406 ;;; determine what is evaluated next. If the ctran has no block, then
407 ;;; we make it be in the block that the node is in. If the ctran heads
408 ;;; its block, we end our block and link it to that block.
409 #!-sb-fluid
(declaim (inline use-ctran
))
410 (defun use-ctran (node ctran
)
411 (declare (type node node
) (type ctran ctran
))
412 (if (eq (ctran-kind ctran
) :unused
)
413 (let ((node-block (ctran-block (node-prev node
))))
414 (setf (ctran-block ctran
) node-block
)
415 (setf (ctran-kind ctran
) :inside-block
)
416 (setf (ctran-use ctran
) node
)
417 (setf (node-next node
) ctran
))
418 (%use-ctran node ctran
)))
419 (defun %use-ctran
(node ctran
)
420 (declare (type node node
) (type ctran ctran
) (inline member
))
421 (let ((block (ctran-block ctran
))
422 (node-block (ctran-block (node-prev node
))))
423 (aver (eq (ctran-kind ctran
) :block-start
))
424 (when (block-last node-block
)
425 (error "~S has already ended." node-block
))
426 (setf (block-last node-block
) node
)
427 (when (block-succ node-block
)
428 (error "~S already has successors." node-block
))
429 (setf (block-succ node-block
) (list block
))
430 (when (memq node-block
(block-pred block
))
431 (error "~S is already a predecessor of ~S." node-block block
))
432 (push node-block
(block-pred block
))))
434 ;;; Insert NEW before OLD in the flow-graph.
435 (defun insert-node-before (old new
)
436 (let ((prev (node-prev old
))
438 (ensure-block-start prev
)
439 (setf (ctran-next prev
) nil
)
440 (link-node-to-previous-ctran new prev
)
442 (link-node-to-previous-ctran old temp
))
445 ;;; This function is used to set the ctran for a node, and thus
446 ;;; determine what receives the value.
447 (defun use-lvar (node lvar
)
448 (declare (type valued-node node
) (type (or lvar null
) lvar
))
449 (aver (not (node-lvar node
)))
451 (setf (node-lvar node
) lvar
)
452 (cond ((null (lvar-uses lvar
))
453 (setf (lvar-uses lvar
) node
))
454 ((listp (lvar-uses lvar
))
455 (aver (not (memq node
(lvar-uses lvar
))))
456 (push node
(lvar-uses lvar
)))
458 (aver (neq node
(lvar-uses lvar
)))
459 (setf (lvar-uses lvar
) (list node
(lvar-uses lvar
)))))
460 (reoptimize-lvar lvar
)))
462 #!-sb-fluid
(declaim (inline use-continuation
))
463 (defun use-continuation (node ctran lvar
)
464 (use-ctran node ctran
)
465 (use-lvar node lvar
))
467 ;;;; exported functions
469 ;;; This function takes a form and the top level form number for that
470 ;;; form, and returns a lambda representing the translation of that
471 ;;; form in the current global environment. The returned lambda is a
472 ;;; top level lambda that can be called to cause evaluation of the
473 ;;; forms. This lambda is in the initial component. If FOR-VALUE is T,
474 ;;; then the value of the form is returned from the function,
475 ;;; otherwise NIL is returned.
477 ;;; This function may have arbitrary effects on the global environment
478 ;;; due to processing of EVAL-WHENs. All syntax error checking is
479 ;;; done, with erroneous forms being replaced by a proxy which signals
480 ;;; an error if it is evaluated. Warnings about possibly inconsistent
481 ;;; or illegal changes to the global environment will also be given.
483 ;;; We make the initial component and convert the form in a PROGN (and
484 ;;; an optional NIL tacked on the end.) We then return the lambda. We
485 ;;; bind all of our state variables here, rather than relying on the
486 ;;; global value (if any) so that IR1 conversion will be reentrant.
487 ;;; This is necessary for EVAL-WHEN processing, etc.
489 ;;; The hashtables used to hold global namespace info must be
490 ;;; reallocated elsewhere. Note also that *LEXENV* is not bound, so
491 ;;; that local macro definitions can be introduced by enclosing code.
492 (defun ir1-toplevel (form path for-value
&optional
(allow-instrumenting t
))
493 (declare (list path
))
494 (let* ((*current-path
* path
)
495 (component (make-empty-component))
496 (*current-component
* component
)
497 (*allow-instrumenting
* allow-instrumenting
))
498 (setf (component-name component
) 'initial-component
)
499 (setf (component-kind component
) :initial
)
500 (let* ((forms (if for-value
`(,form
) `(,form nil
)))
501 (res (ir1-convert-lambda-body
503 :debug-name
(debug-name 'top-level-form
#+sb-xc-host nil
#-sb-xc-host form
))))
504 (setf (functional-entry-fun res
) res
505 (functional-arg-documentation res
) ()
506 (functional-kind res
) :toplevel
)
509 ;;; This function is called on freshly read forms to record the
510 ;;; initial location of each form (and subform.) Form is the form to
511 ;;; find the paths in, and TLF-NUM is the top level form number of the
512 ;;; truly top level form.
514 ;;; This gets a bit interesting when the source code is circular. This
515 ;;; can (reasonably?) happen in the case of circular list constants.
516 (defun find-source-paths (form tlf-num
)
517 (declare (type index tlf-num
))
518 (let ((*current-form-number
* 0))
519 (sub-find-source-paths form
(list tlf-num
)))
521 (defun sub-find-source-paths (form path
)
522 (unless (get-source-path form
)
523 (note-source-path form path
)
524 (incf *current-form-number
*)
528 (declare (fixnum pos
))
531 (when (atom subform
) (return))
532 (let ((fm (car subform
)))
533 (when (sb!int
:comma-p fm
)
534 (setf fm
(sb!int
:comma-expr fm
)))
536 ;; If it's a cons, recurse.
537 (sub-find-source-paths fm
(cons pos path
)))
539 ;; Don't look into quoted constants.
540 ;; KLUDGE: this can't actually know about constants.
541 ;; e.g. (let ((quote (error "foo")))) or
542 ;; (list quote (error "foo")) are not
543 ;; constants and yet are ignored.
546 ;; Otherwise store the containing form. It's not
547 ;; perfect, but better than nothing.
548 (note-source-path subform pos path
)))
550 (setq subform
(cdr subform
))
551 (when (eq subform trail
) (return)))))
555 (setq trail
(cdr trail
)))))))
557 ;;;; IR1-CONVERT, macroexpansion and special form dispatching
559 (declaim (ftype (sfunction (ctran ctran
(or lvar null
) t
)
562 (macrolet (;; Bind *COMPILER-ERROR-BAILOUT* to a function that throws
563 ;; out of the body and converts a condition signalling form
564 ;; instead. The source form is converted to a string since it
565 ;; may contain arbitrary non-externalizable objects.
566 (ir1-error-bailout ((start next result form
) &body body
)
567 (with-unique-names (skip condition
)
569 (let ((,condition
(catch 'ir1-error-abort
570 (let ((*compiler-error-bailout
*
571 (lambda (&optional e
)
572 (throw 'ir1-error-abort e
))))
574 (return-from ,skip nil
)))))
575 (ir1-convert ,start
,next
,result
576 (make-compiler-error-form ,condition
579 ;; Translate FORM into IR1. The code is inserted as the NEXT of the
580 ;; CTRAN START. RESULT is the LVAR which receives the value of the
581 ;; FORM to be translated. The translators call this function
582 ;; recursively to translate their subnodes.
584 ;; As a special hack to make life easier in the compiler, a LEAF
585 ;; IR1-converts into a reference to that LEAF structure. This allows
586 ;; the creation using backquote of forms that contain leaf
587 ;; references, without having to introduce dummy names into the
589 (defun ir1-convert (start next result form
)
590 (let* ((*current-path
* (ensure-source-path form
))
591 (start (instrument-coverage start nil form
)))
592 (ir1-error-bailout (start next result form
)
594 (cond ((and (symbolp form
) (not (keywordp form
)))
595 (ir1-convert-var start next result form
))
597 (reference-leaf start next result form
))
599 (reference-constant start next result form
))))
601 (ir1-convert-functoid start next result form
)))))
604 ;; Generate a reference to a manifest constant, creating a new leaf
606 (defun reference-constant (start next result value
)
607 (declare (type ctran start next
)
608 (type (or lvar null
) result
))
609 (ir1-error-bailout (start next result value
)
610 (let* ((leaf (find-constant value
))
611 (res (make-ref leaf
)))
612 (push res
(leaf-refs leaf
))
613 (link-node-to-previous-ctran res start
)
614 (use-continuation res next result
)))
617 ;;; Add FUNCTIONAL to the COMPONENT-REANALYZE-FUNCTIONALS, unless it's
618 ;;; some trivial type for which reanalysis is a trivial no-op, or
619 ;;; unless it doesn't belong in this component at all.
621 ;;; FUNCTIONAL is returned.
622 (defun maybe-reanalyze-functional (functional)
624 (aver (not (eql (functional-kind functional
) :deleted
))) ; bug 148
625 (aver-live-component *current-component
*)
627 ;; When FUNCTIONAL is of a type for which reanalysis isn't a trivial
629 (when (typep functional
'(or optional-dispatch clambda
))
631 ;; When FUNCTIONAL knows its component
632 (when (lambda-p functional
)
633 (aver (eql (lambda-component functional
) *current-component
*)))
636 (component-reanalyze-functionals *current-component
*)))
640 ;;; Generate a REF node for LEAF, frobbing the LEAF structure as
641 ;;; needed. If LEAF represents a defined function which has already
642 ;;; been converted, and is not :NOTINLINE, then reference the
643 ;;; functional instead.
644 (defun reference-leaf (start next result leaf
&optional
(name '.anonymous.
))
645 (declare (type ctran start next
) (type (or lvar null
) result
) (type leaf leaf
))
646 (assure-leaf-live-p leaf
)
647 (let* ((type (lexenv-find leaf type-restrictions
))
648 (leaf (or (and (defined-fun-p leaf
)
649 (not (eq (defined-fun-inlinep leaf
)
651 (let ((functional (defined-fun-functional leaf
)))
652 (when (and functional
(not (functional-kind functional
)))
653 (maybe-reanalyze-functional functional
))))
654 (when (and (lambda-p leaf
)
655 (memq (functional-kind leaf
)
657 (maybe-reanalyze-functional leaf
))
659 (ref (make-ref leaf name
)))
660 (push ref
(leaf-refs leaf
))
661 (setf (leaf-ever-used leaf
) t
)
662 (link-node-to-previous-ctran ref start
)
663 (cond (type (let* ((ref-ctran (make-ctran))
664 (ref-lvar (make-lvar))
665 (cast (make-cast ref-lvar
666 (make-single-value-type type
)
667 (lexenv-policy *lexenv
*))))
668 (setf (lvar-dest ref-lvar
) cast
)
669 (use-continuation ref ref-ctran ref-lvar
)
670 (link-node-to-previous-ctran cast ref-ctran
)
671 (use-continuation cast next result
)))
672 (t (use-continuation ref next result
)))))
674 (defun always-boundp (name)
675 (case (info :variable
:always-bound name
)
677 ;; Compiling to fasl considers a symbol always-bound if its
678 ;; :always-bound info value is now T or will eventually be T.
679 (:eventually
(fasl-output-p *compile-object
*))))
681 ;;; Convert a reference to a symbolic constant or variable. If the
682 ;;; symbol is entered in the LEXENV-VARS we use that definition,
683 ;;; otherwise we find the current global definition. This is also
684 ;;; where we pick off symbol macro and alien variable references.
685 (defun ir1-convert-var (start next result name
)
686 (declare (type ctran start next
) (type (or lvar null
) result
) (symbol name
))
687 (let ((var (or (lexenv-find name vars
) (find-free-var name
))))
688 (if (and (global-var-p var
) (not (always-boundp name
)))
689 ;; KLUDGE: If the variable may be unbound, convert using SYMBOL-VALUE
690 ;; which is not flushable, so that unbound dead variables signal an
691 ;; error (bug 412, lp#722734): checking for null RESULT is not enough,
692 ;; since variables can become dead due to later optimizations.
693 (ir1-convert start next result
694 (if (eq (global-var-kind var
) :global
)
695 `(symbol-global-value ',name
)
696 `(symbol-value ',name
)))
701 (let ((home (ctran-home-lambda-or-null start
)))
703 (sset-adjoin var
(lambda-calls-or-closes home
))))
704 (when (lambda-var-ignorep var
)
705 ;; (ANSI's specification for the IGNORE declaration requires
706 ;; that this be a STYLE-WARNING, not a full WARNING.)
708 (compiler-style-warn "reading an ignored variable: ~S" name
)
709 ;; there's no need for us to accept ANSI's lameness when
710 ;; processing our own code, though.
712 (warn "reading an ignored variable: ~S" name
)))
714 ;; This case signals {EARLY,LATE}-DEPRECATION-WARNING
715 ;; for CONSTANT nodes in :EARLY and :LATE deprecation
716 ;; (constants in :FINAL deprecation are represented as
718 (aver (memq (check-deprecated-thing 'variable name
)
719 '(nil :early
:late
)))))
720 (reference-leaf start next result var name
))
721 ((cons (eql macro
)) ; symbol-macro
722 ;; This case signals {EARLY,LATE,FINAL}-DEPRECATION-WARNING
723 ;; for symbol-macros. Includes variables, constants,
724 ;; etc. in :FINAL deprecation.
725 (check-deprecated-thing 'variable name
)
726 ;; FIXME: [Free] type declarations. -- APD, 2002-01-26
727 (ir1-convert start next result
(cdr var
)))
729 (ir1-convert start next result
`(%heap-alien
',var
))))))
732 ;;; Find a compiler-macro for a form, taking FUNCALL into account.
733 (defun find-compiler-macro (opname form
)
734 (flet ((legal-cm-name-p (name)
735 (and (legal-fun-name-p name
)
736 (or (not (symbolp name
))
737 (not (sb!xc
:macro-function name
*lexenv
*))))))
738 (if (eq opname
'funcall
)
739 (let ((fun-form (cadr form
)))
740 (cond ((and (consp fun-form
) (eq 'function
(car fun-form
))
741 (not (cddr fun-form
)))
742 (let ((real-fun (cadr fun-form
)))
743 (if (legal-cm-name-p real-fun
)
744 (values (sb!xc
:compiler-macro-function real-fun
*lexenv
*)
747 ((sb!xc
:constantp fun-form
*lexenv
*)
748 (let ((fun (constant-form-value fun-form
*lexenv
*)))
749 (if (legal-cm-name-p fun
)
750 ;; CLHS tells us that local functions must shadow
751 ;; compiler-macro-functions, but since the call is
752 ;; through a name, we are obviously interested
753 ;; in the global function.
754 ;; KLUDGE: CLHS 3.2.2.1.1 also says that it can be
755 ;; "a list whose car is funcall and whose cadr is
756 ;; a list (function name)", that means that
757 ;; (funcall 'name) that gets here doesn't fit the
759 (values (sb!xc
:compiler-macro-function fun nil
) fun
)
763 (if (legal-fun-name-p opname
)
764 (values (sb!xc
:compiler-macro-function opname
*lexenv
*) opname
)
767 ;;; If FORM has a usable compiler macro, use it; otherwise return FORM itself.
768 ;;; Return the name of the compiler-macro as a secondary value, if applicable.
769 (defun expand-compiler-macro (form)
770 (binding* ((name (car form
))
771 ((cmacro-fun cmacro-fun-name
)
772 (find-compiler-macro name form
)))
775 ;; CLHS 3.2.2.1.3 specifies that NOTINLINE
776 ;; suppresses compiler-macros.
777 (not (fun-lexically-notinline-p cmacro-fun-name
)))
778 (check-deprecated-thing 'function name
)
779 (values (handler-case (careful-expand-macro cmacro-fun form t
)
780 (compiler-macro-keyword-problem (condition)
781 (print-compiler-message
782 *error-output
* "note: ~A" (list condition
))
786 (values form nil
)))))
788 ;;; Picks off special forms and compiler-macro expansions, and hands
789 ;;; the rest to IR1-CONVERT-COMMON-FUNCTOID
790 (defun ir1-convert-functoid (start next result form
)
791 (let* ((op (car form
))
792 (translator (and (symbolp op
) (info :function
:ir1-convert op
))))
794 (funcall translator start next result form
)
795 (multiple-value-bind (res cmacro-fun-name
)
796 (expand-compiler-macro form
)
798 (ir1-convert-common-functoid start next result form op
))
800 (unless (policy *lexenv
* (zerop store-xref-data
))
801 (record-call cmacro-fun-name
(ctran-block start
)
803 (ir1-convert start next result res
)))))))
805 ;;; Handles the "common" cases: any other forms except special forms
806 ;;; and compiler-macros.
807 (defun ir1-convert-common-functoid (start next result form op
)
808 (cond ((or (symbolp op
) (leaf-p op
))
809 (let ((lexical-def (if (leaf-p op
) op
(lexenv-find op funs
))))
810 (typecase lexical-def
812 (check-deprecated-thing 'function op
)
813 (ir1-convert-global-functoid start next result form op
))
815 (ir1-convert-local-combination start next result form
818 (check-deprecated-thing 'function
(leaf-source-name lexical-def
))
819 (ir1-convert-srctran start next result lexical-def form
))
821 (aver (and (consp lexical-def
) (eq (car lexical-def
) 'macro
)))
822 (ir1-convert start next result
823 (careful-expand-macro (cdr lexical-def
) form
))))))
824 ((or (atom op
) (not (eq (car op
) 'lambda
)))
825 (compiler-error "illegal function call"))
827 ;; implicitly (LAMBDA ..) because the LAMBDA expression is
828 ;; the CAR of an executed form.
829 (ir1-convert start next result
`(%funcall
,@form
)))))
831 ;;; Convert anything that looks like a global function call.
832 (defun ir1-convert-global-functoid (start next result form fun
)
833 (declare (type ctran start next
) (type (or lvar null
) result
)
835 (when (eql fun
'declare
)
837 "~@<There is no function named ~S. ~
838 References to ~S in some contexts (like starts of blocks) are unevaluated ~
839 expressions, but here the expression is being evaluated, which invokes ~
840 undefined behaviour.~@:>" fun fun
))
841 ;; FIXME: Couldn't all the INFO calls here be converted into
842 ;; standard CL functions, like MACRO-FUNCTION or something? And what
843 ;; happens with lexically-defined (MACROLET) macros here, anyway?
844 (ecase (info :function
:kind fun
)
846 (ir1-convert start next result
847 (careful-expand-macro (info :function
:macro-function fun
)
849 (unless (policy *lexenv
* (zerop store-xref-data
))
850 (record-macroexpansion fun
(ctran-block start
) *current-path
*)))
852 (ir1-convert-srctran start next result
853 (find-free-fun fun
"shouldn't happen! (no-cmacro)")
856 (defun muffle-warning-or-die ()
858 (bug "no MUFFLE-WARNING restart"))
860 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
861 ;;; errors which occur during the macroexpansion.
862 (defun careful-expand-macro (fun form
&optional cmacro
)
863 (flet (;; Return a string to use as a prefix in error reporting,
864 ;; telling something about which form caused the problem.
866 (let (;; We rely on the printer to abbreviate FORM.
870 "~@<~A of ~S. Use ~S to intercept.~%~:@>"
872 #-sb-xc-host
"Error during compiler-macroexpansion"
873 #+sb-xc-host
"Error during XC compiler-macroexpansion")
875 #-sb-xc-host
"during macroexpansion"
876 #+sb-xc-host
"during XC macroexpansion"))
878 '*break-on-signals
*))))
879 (handler-bind (;; KLUDGE: CMU CL in its wisdom (version 2.4.6 for Debian
880 ;; Linux, anyway) raises a CL:WARNING condition (not a
881 ;; CL:STYLE-WARNING) for undefined symbols when converting
882 ;; interpreted functions, causing COMPILE-FILE to think the
883 ;; file has a real problem, causing COMPILE-FILE to return
884 ;; FAILURE-P set (not just WARNINGS-P set). Since undefined
885 ;; symbol warnings are often harmless forward references,
886 ;; and since it'd be inordinately painful to try to
887 ;; eliminate all such forward references, these warnings
888 ;; are basically unavoidable. Thus, we need to coerce the
889 ;; system to work through them, and this code does so, by
890 ;; crudely suppressing all warnings in cross-compilation
891 ;; macroexpansion. -- WHN 19990412
892 #+(and cmu sb-xc-host
)
897 ~@<(KLUDGE: That was a non-STYLE WARNING. ~
898 Ordinarily that would cause compilation to ~
899 fail. However, since we're running under ~
900 CMU CL, and since CMU CL emits non-STYLE ~
901 warnings for safe, hard-to-fix things (e.g. ~
902 references to not-yet-defined functions) ~
903 we're going to have to ignore it and ~
904 proceed anyway. Hopefully we're not ~
905 ignoring anything horrible here..)~:@>~:>"
908 (muffle-warning-or-die)))
913 ;; The spec is silent on what we should do. Signaling
914 ;; a full warning but declining to expand seems like
915 ;; a conservative and sane thing to do.
916 (compiler-warn "~@<~A~@:_ ~A~:>" (wherestring) c
)
917 (return-from careful-expand-macro form
))
919 (compiler-error "~@<~A~@:_ ~A~:>"
920 (wherestring) c
))))))
921 (funcall sb
!xc
:*macroexpand-hook
* fun form
*lexenv
*))))
923 ;;;; conversion utilities
925 ;;; Convert a bunch of forms, discarding all the values except the
926 ;;; last. If there aren't any forms, then translate a NIL.
927 (declaim (ftype (sfunction (ctran ctran
(or lvar null
) list
) (values))
928 ir1-convert-progn-body
))
929 (defun ir1-convert-progn-body (start next result body
)
931 (reference-constant start next result nil
)
932 (let ((this-start start
)
935 (let ((form (car forms
)))
937 (maybe-instrument-progn-like this-start forms form
))
938 (when (endp (cdr forms
))
939 (ir1-convert this-start next result form
)
941 (let ((this-ctran (make-ctran)))
942 (ir1-convert this-start this-ctran nil form
)
943 (setq this-start this-ctran
944 forms
(cdr forms
)))))))
950 ;;; Check the policy for whether we should generate code coverage
951 ;;; instrumentation. If not, just return the original START
952 ;;; ctran. Otherwise insert code coverage instrumentation after
953 ;;; START, and return the new ctran.
954 (defun instrument-coverage (start mode form
)
955 ;; We don't actually use FORM for anything, it's just convenient to
956 ;; have around when debugging the instrumentation.
957 (declare (ignore form
))
958 (if (and (policy *lexenv
* (> store-coverage-data
0))
959 *code-coverage-records
*
960 *allow-instrumenting
*)
961 (let ((path (source-path-original-source *current-path
*)))
964 (if (member (ctran-block start
)
965 (gethash path
*code-coverage-blocks
*))
966 ;; If this source path has already been instrumented in
967 ;; this block, don't instrument it again.
970 ;; Get an interned record cons for the path. A cons
971 ;; with the same object identity must be used for
972 ;; each instrument for the same block.
973 (or (gethash path
*code-coverage-records
*)
974 (setf (gethash path
*code-coverage-records
*)
975 (cons path
+code-coverage-unmarked
+))))
977 (*allow-instrumenting
* nil
))
978 (push (ctran-block start
)
979 (gethash path
*code-coverage-blocks
*))
980 (let ((*allow-instrumenting
* nil
))
981 (ir1-convert start next nil
983 (declare (optimize speed
986 (check-constant-modification 0)))
987 ;; We're being naughty here, and
988 ;; modifying constant data. That's ok,
989 ;; we know what we're doing.
990 (%rplacd
',store t
))))
994 ;;; In contexts where we don't have a source location for FORM
995 ;;; e.g. due to it not being a cons, but where we have a source
996 ;;; location for the enclosing cons, use the latter source location if
997 ;;; available. This works pretty well in practice, since many PROGNish
998 ;;; macroexpansions will just directly splice a block of forms into
999 ;;; some enclosing form with `(progn ,@body), thus retaining the
1000 ;;; EQness of the conses.
1001 (defun maybe-instrument-progn-like (start forms form
)
1002 (or (when (and *allow-instrumenting
*
1003 (not (get-source-path form
)))
1004 (let ((*current-path
* (get-source-path forms
)))
1005 (when *current-path
*
1006 (instrument-coverage start nil form
))))
1009 (defun record-code-coverage (info cc
)
1010 (setf (gethash info
*code-coverage-info
*) cc
))
1012 (defun clear-code-coverage ()
1013 (clrhash *code-coverage-info
*))
1015 (defun reset-code-coverage ()
1016 (maphash (lambda (info cc
)
1017 (declare (ignore info
))
1018 (dolist (cc-entry cc
)
1019 (setf (cdr cc-entry
) +code-coverage-unmarked
+)))
1020 *code-coverage-info
*))
1022 (defun code-coverage-record-marked (record)
1023 (aver (consp record
))
1025 ((#.
+code-coverage-unmarked
+) nil
)
1029 ;;;; converting combinations
1031 ;;; Does this form look like something that we should add single-stepping
1032 ;;; instrumentation for?
1033 (defun step-form-p (form)
1034 (flet ((step-symbol-p (symbol)
1035 (and (not (member (symbol-package symbol
)
1037 ;; KLUDGE: packages we're not interested in
1039 (mapcar #'find-package
'(sb!c sb
!int sb
!impl
1040 sb
!kernel sb
!pcl
)))))
1041 ;; Consistent treatment of *FOO* vs (SYMBOL-VALUE '*FOO*):
1042 ;; we insert calls to SYMBOL-VALUE for most non-lexical
1043 ;; variable references in order to avoid them being elided
1044 ;; if the value is unused.
1045 (or (not (member symbol
'(symbol-value symbol-global-value
)))
1046 (not (constantp (second form
)))))))
1047 (and *allow-instrumenting
*
1048 (policy *lexenv
* (= insert-step-conditions
3))
1050 (symbolp (car form
))
1051 (step-symbol-p (car form
)))))
1053 ;;; Convert a function call where the function FUN is a LEAF. FORM is
1054 ;;; the source for the call. We return the COMBINATION node so that
1055 ;;; the caller can poke at it if it wants to.
1056 (declaim (ftype (sfunction (ctran ctran
(or lvar null
) list leaf
) combination
)
1057 ir1-convert-combination
))
1058 (defun ir1-convert-combination (start next result form fun
)
1059 (let ((ctran (make-ctran))
1060 (fun-lvar (make-lvar)))
1061 (ir1-convert start ctran fun-lvar
`(the (or function symbol
) ,fun
))
1063 (ir1-convert-combination-args fun-lvar ctran next result
1065 (when (step-form-p form
)
1066 ;; Store a string representation of the form in the
1067 ;; combination node. This will let the IR2 translator know
1068 ;; that we want stepper instrumentation for this node. The
1069 ;; string will be stored in the debug-info by DUMP-1-LOCATION.
1070 (setf (combination-step-info combination
)
1071 (let ((*print-pretty
* t
)
1073 (*print-readably
* nil
))
1074 (prin1-to-string form
))))
1077 ;;; Convert the arguments to a call and make the COMBINATION
1078 ;;; node. FUN-LVAR yields the function to call. ARGS is the list of
1079 ;;; arguments for the call, which defaults to the cdr of source. We
1080 ;;; return the COMBINATION node.
1081 (defun ir1-convert-combination-args (fun-lvar start next result args
)
1082 (declare (type ctran start next
)
1083 (type lvar fun-lvar
)
1084 (type (or lvar null
) result
)
1086 (let ((node (make-combination fun-lvar
)))
1087 (setf (lvar-dest fun-lvar
) node
)
1088 (collect ((arg-lvars))
1089 (let ((this-start start
)
1093 (maybe-instrument-progn-like this-start forms arg
))
1094 (setf forms
(cdr forms
))
1095 (let ((this-ctran (make-ctran))
1096 (this-lvar (make-lvar node
)))
1097 (ir1-convert this-start this-ctran this-lvar arg
)
1098 (setq this-start this-ctran
)
1099 (arg-lvars this-lvar
)))
1100 (link-node-to-previous-ctran node this-start
)
1101 (use-continuation node next result
)
1102 (setf (combination-args node
) (arg-lvars))))
1105 ;;; Convert a call to a global function. If not :NOTINLINE, then we do
1106 ;;; source transforms and try out any inline expansion. If there is no
1107 ;;; expansion, but is :INLINE, then give an efficiency note (unless a
1108 ;;; known function which will quite possibly be open-coded.) Next, we
1109 ;;; go to ok-combination conversion.
1110 (defun ir1-convert-srctran (start next result var form
)
1111 (declare (type ctran start next
) (type (or lvar null
) result
)
1112 (type global-var var
))
1113 (let ((name (leaf-source-name var
))
1114 (inlinep (when (defined-fun-p var
)
1115 (defined-fun-inlinep var
))))
1116 (if (eq inlinep
:notinline
)
1117 (ir1-convert-combination start next result form var
)
1118 (let* ((transform (info :function
:source-transform name
)))
1120 (multiple-value-bind (transformed pass
)
1121 (if (functionp transform
)
1122 (funcall transform form
*lexenv
*)
1124 (if (eq (cdr transform
) :predicate
)
1125 (and (singleton-p (cdr form
))
1128 ',(dd-name (car transform
))))
1129 (slot-access-transform
1130 (if (consp name
) :write
:read
)
1131 (cdr form
) transform
))))
1132 (values result
(null result
))))
1134 (ir1-convert-maybe-predicate start next result form var
))
1136 (unless (policy *lexenv
* (zerop store-xref-data
))
1137 (record-call name
(ctran-block start
) *current-path
*))
1138 (ir1-convert start next result transformed
))))
1139 (ir1-convert-maybe-predicate start next result form var
))))))
1141 ;;; KLUDGE: If we insert a synthetic IF for a function with the PREDICATE
1142 ;;; attribute, don't generate any branch coverage instrumentation for it.
1143 (defvar *instrument-if-for-code-coverage
* t
)
1145 ;;; If the function has the PREDICATE attribute, and the RESULT's DEST
1146 ;;; isn't an IF, then we convert (IF <form> T NIL), ensuring that a
1147 ;;; predicate always appears in a conditional context.
1149 ;;; If the function isn't a predicate, then we call
1150 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
1151 (defun ir1-convert-maybe-predicate (start next result form var
)
1152 (declare (type ctran start next
)
1153 (type (or lvar null
) result
)
1155 (type global-var var
))
1156 (let ((info (info :function
:info
(leaf-source-name var
))))
1158 (ir1-attributep (fun-info-attributes info
) predicate
)
1159 (not (if-p (and result
(lvar-dest result
)))))
1160 (let ((*instrument-if-for-code-coverage
* nil
))
1161 (ir1-convert start next result
`(if ,form t nil
)))
1162 (ir1-convert-combination-checking-type start next result form var
))))
1164 ;;; Actually really convert a global function call that we are allowed
1167 ;;; If we know the function type of the function, then we check the
1168 ;;; call for syntactic legality with respect to the declared function
1169 ;;; type. If it is impossible to determine whether the call is correct
1170 ;;; due to non-constant keywords, then we give up, marking the call as
1171 ;;; :FULL to inhibit further error messages. We return true when the
1174 ;;; If the call is legal, we also propagate type assertions from the
1175 ;;; function type to the arg and result lvars. We do this now so that
1176 ;;; IR1 optimize doesn't have to redundantly do the check later so
1177 ;;; that it can do the type propagation.
1178 (defun ir1-convert-combination-checking-type (start next result form var
)
1179 (declare (type ctran start next
) (type (or lvar null
) result
)
1182 (let* ((node (ir1-convert-combination start next result form var
))
1183 (fun-lvar (basic-combination-fun node
))
1184 (type (leaf-type var
)))
1185 (when (validate-call-type node type var t
)
1186 (setf (lvar-%derived-type fun-lvar
)
1187 (make-single-value-type type
))
1188 (setf (lvar-reoptimize fun-lvar
) nil
)))
1191 ;;; Convert a call to a local function, or if the function has already
1192 ;;; been LET converted, then throw FUNCTIONAL to
1193 ;;; LOCALL-ALREADY-LET-CONVERTED. The THROW should only happen when we
1194 ;;; are converting inline expansions for local functions during
1196 (defun ir1-convert-local-combination (start next result form functional
)
1197 (assure-functional-live-p functional
)
1198 (ir1-convert-combination start next result
1200 (maybe-reanalyze-functional functional
)))
1204 ;;; Given a list of LAMBDA-VARs and a variable name, return the
1205 ;;; LAMBDA-VAR for that name, or NIL if it isn't found. We return the
1206 ;;; *last* variable with that name, since LET* bindings may be
1207 ;;; duplicated, and declarations always apply to the last.
1208 (declaim (ftype (sfunction (list symbol
) (or lambda-var list
))
1210 (defun find-in-bindings (vars name
)
1214 (when (eq (leaf-source-name var
) name
)
1216 (let ((info (lambda-var-arg-info var
)))
1218 (let ((supplied-p (arg-info-supplied-p info
)))
1219 (when (and supplied-p
1220 (eq (leaf-source-name supplied-p
) name
))
1221 (setq found supplied-p
))))))
1222 ((and (consp var
) (eq (car var
) name
))
1223 (setf found
(cdr var
)))))
1226 ;;; Called by PROCESS-DECLS to deal with a variable type declaration.
1227 ;;; If a LAMBDA-VAR being bound, we intersect the type with the var's
1228 ;;; type, otherwise we add a type restriction on the var. If a symbol
1229 ;;; macro, we just wrap a THE around the expansion.
1230 (defun process-type-decl (decl res vars context
)
1231 (declare (type list decl vars
) (type lexenv res
))
1232 (let* ((type-specifier (first decl
))
1234 (when (typep type-specifier
'type-specifier
)
1235 (check-deprecated-type type-specifier
))
1236 (compiler-specifier-type type-specifier
))))
1237 (collect ((restr nil cons
)
1238 (new-vars nil cons
))
1239 (dolist (var-name (rest decl
))
1240 (unless (symbolp var-name
)
1241 (compiler-error "Variable name is not a symbol: ~S." var-name
))
1242 (when (boundp var-name
)
1243 (program-assert-symbol-home-package-unlocked
1244 context var-name
"declaring the type of ~A"))
1245 (let* ((bound-var (find-in-bindings vars var-name
))
1247 (lexenv-find var-name vars
)
1248 (find-free-var var-name
))))
1252 ((process-var (var bound-var
)
1253 (let* ((old-type (or (lexenv-find var type-restrictions
)
1255 (int (if (or (fun-type-p type
)
1256 (fun-type-p old-type
))
1258 (type-approx-intersection2
1260 (cond ((eq int
*empty-type
*)
1261 (unless (policy *lexenv
* (= inhibit-warnings
3))
1265 "The type declarations ~S and ~S for ~S conflict."
1268 (type-specifier old-type
)
1269 (type-specifier type
)
1272 (setf (leaf-type bound-var
) int
1273 (leaf-where-from bound-var
) :declared
))
1275 (restr (cons var int
)))))))
1276 (process-var var bound-var
)
1277 (awhen (and (lambda-var-p var
)
1278 (lambda-var-specvar var
))
1279 (process-var it nil
))))
1281 ;; FIXME: non-ANSI weirdness. [See lp#309122]
1282 (aver (eq (car var
) 'macro
))
1283 (new-vars `(,var-name .
(macro .
(the ,(first decl
)
1287 "~S is an alien variable, so its type can't be declared."
1290 (if (or (restr) (new-vars))
1291 (make-lexenv :default res
1292 :type-restrictions
(restr)
1296 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
1297 ;;; declarations for function variables. In addition to allowing
1298 ;;; declarations for functions being bound, we must also deal with
1299 ;;; declarations that constrain the type of lexically apparent
1301 (defun process-ftype-decl (type-specifier res names fvars context
)
1302 (declare (type list names fvars
)
1304 (let ((type (compiler-specifier-type type-specifier
)))
1305 (check-deprecated-type type-specifier
)
1306 (unless (csubtypep type
(specifier-type 'function
))
1307 (compiler-style-warn "ignoring declared FTYPE: ~S (not a function type)"
1309 (return-from process-ftype-decl res
))
1310 (collect ((res nil cons
))
1311 (dolist (name names
)
1312 (when (fboundp name
)
1313 (program-assert-symbol-home-package-unlocked
1314 context name
"declaring the ftype of ~A"))
1315 (let ((found (find name fvars
:key
#'leaf-source-name
:test
#'equal
)))
1318 (setf (leaf-type found
) type
)
1319 (assert-definition-type found type
1320 :unwinnage-fun
#'compiler-notify
1321 :where
"FTYPE declaration"))
1323 (res (cons (find-lexically-apparent-fun
1324 name
"in a function type declaration")
1327 (make-lexenv :default res
:type-restrictions
(res))
1330 ;;; Process a special declaration, returning a new LEXENV. A non-bound
1331 ;;; special declaration is instantiated by throwing a special variable
1332 ;;; into the variables if BINDING-FORM-P is NIL, or otherwise into
1333 ;;; *POST-BINDING-VARIABLE-LEXENV*.
1334 (defun process-special-decl (spec res vars binding-form-p context
)
1335 (declare (list spec vars
) (type lexenv res
))
1336 (collect ((new-venv nil cons
))
1337 (dolist (name (cdr spec
))
1338 ;; While CLHS seems to allow local SPECIAL declarations for constants,
1339 ;; whatever the semantics are supposed to be is not at all clear to me
1340 ;; -- since constants aren't allowed to be bound it should be a no-op as
1341 ;; no-one can observe the difference portably, but specials are allowed
1342 ;; to be bound... yet nowhere does it say that the special declaration
1343 ;; removes the constantness. Call it a spec bug and prohibit it. Same
1344 ;; for GLOBAL variables.
1345 (let ((kind (info :variable
:kind name
)))
1346 (unless (member kind
'(:special
:unknown
))
1347 (error "Can't declare ~(~A~) variable locally special: ~S" kind name
)))
1348 (program-assert-symbol-home-package-unlocked
1349 context name
"declaring ~A special")
1350 (let ((var (find-in-bindings vars name
)))
1353 (aver (eq (car var
) 'macro
))
1355 "~S is a symbol-macro and thus can't be declared special."
1358 (when (lambda-var-ignorep var
)
1359 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1360 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1361 (compiler-style-warn
1362 "The ignored variable ~S is being declared special."
1364 (setf (lambda-var-specvar var
)
1365 (specvar-for-binding name
)))
1367 (unless (or (assoc name
(new-venv) :test
#'eq
))
1368 (new-venv (cons name
(specvar-for-binding name
))))))))
1369 (cond (binding-form-p
1370 (setf *post-binding-variable-lexenv
*
1371 (append (new-venv) *post-binding-variable-lexenv
*))
1374 (make-lexenv :default res
:vars
(new-venv)))
1378 ;;; Return a DEFINED-FUN which copies a GLOBAL-VAR but for its INLINEP
1379 ;;; (and TYPE if notinline), plus type-restrictions from the lexenv.
1380 (defun make-new-inlinep (var inlinep local-type
)
1381 (declare (type global-var var
) (type inlinep inlinep
))
1382 (let* ((type (if (and (eq inlinep
:notinline
)
1383 (not (eq (leaf-where-from var
) :declared
)))
1384 (specifier-type 'function
)
1386 (res (make-defined-fun
1387 :%source-name
(leaf-source-name var
)
1388 :where-from
(leaf-where-from var
)
1389 :type
(if local-type
1390 (type-intersection local-type type
)
1393 (when (defined-fun-p var
)
1394 (setf (defined-fun-inline-expansion res
)
1395 (defined-fun-inline-expansion var
))
1396 (setf (defined-fun-functionals res
)
1397 (defined-fun-functionals var
)))
1398 ;; FIXME: Is this really right? Needs we not set the FUNCTIONAL
1399 ;; to the original global-var?
1402 ;;; Parse an inline/notinline declaration. If it's a local function we're
1403 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
1404 (defun process-inline-decl (spec res fvars
)
1405 (let ((sense (cdr (assoc (first spec
) *inlinep-translations
* :test
#'eq
)))
1407 (dolist (name (rest spec
))
1408 (let ((fvar (find name fvars
:key
#'leaf-source-name
:test
#'equal
)))
1410 (setf (functional-inlinep fvar
) sense
)
1411 (let ((found (find-lexically-apparent-fun
1412 name
"in an inline or notinline declaration")))
1415 (when (policy *lexenv
* (>= speed inhibit-warnings
))
1416 (compiler-notify "ignoring ~A declaration not at ~
1417 definition of local function:~% ~S"
1421 (cdr (assoc found
(lexenv-type-restrictions res
)))))
1422 (push (cons name
(make-new-inlinep found sense type
))
1425 (make-lexenv :default res
:funs new-fenv
)
1428 ;;; like FIND-IN-BINDINGS, but looks for #'FOO in the FVARS
1429 (defun find-in-bindings-or-fbindings (name vars fvars
)
1430 (declare (list vars fvars
))
1433 (find-in-bindings vars name
))
1434 ((cons (eql function
) (cons * null
))
1435 (find (cadr name
) fvars
:key
#'leaf-source-name
:test
#'equal
))
1437 (compiler-error "Malformed function or variable name ~S." name
))))
1439 ;;; Process an ignore/ignorable declaration, checking for various losing
1441 (defun process-ignore-decl (spec vars fvars lexenv
)
1442 (declare (list spec vars fvars
))
1443 (dolist (name (rest spec
))
1444 (let ((var (find-in-bindings-or-fbindings name vars fvars
)))
1447 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1448 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1449 ;; But, other Lisp hosts signal a full warning, so when building
1450 ;; the cross-compiler, compile it as #'WARN so that in a self-hosted
1451 ;; build we can at least crash in the same way,
1452 ;; until we resolve this question about how severe the warning is.
1453 (multiple-value-call #+sb-xc-host
#'warn
1454 #-sb-xc-host
#'compiler-style-warn
1455 "~A declaration for ~A: ~A"
1459 (case (info :variable
:kind name
)
1460 (:special
"a special variable")
1461 (:global
"a global lexical variable")
1462 (:alien
"a global alien variable")
1463 (t (if (assoc name
(lexenv-vars lexenv
))
1464 "a variable from outer scope"
1465 "an unknown variable")))
1468 (cond ((assoc (second name
) (lexenv-funs lexenv
)
1470 "a function from outer scope")
1471 ((info :function
:kind
(second name
))
1472 "a global function")
1474 "an unknown function"))
1476 ((and (consp var
) (eq (car var
) 'macro
))
1477 ;; Just ignore the IGNORE decl: we don't currently signal style-warnings
1478 ;; for unused symbol-macros, so there's no need to do anything.
1481 (setf (leaf-ever-used var
) t
))
1482 ((and (lambda-var-specvar var
) (eq (first spec
) 'ignore
))
1483 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1484 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1485 (compiler-style-warn "Declaring special variable ~S to be ~A"
1488 ((eq (first spec
) 'ignorable
)
1489 (setf (leaf-ever-used var
) t
))
1491 (setf (lambda-var-ignorep var
) t
)))))
1494 (defun process-extent-decl (names vars fvars kind
)
1497 (truly-dynamic-extent
1500 (when *stack-allocate-dynamic-extent
*
1505 (dolist (name names
)
1508 (let* ((bound-var (find-in-bindings vars name
))
1510 (lexenv-find name vars
)
1511 (maybe-find-free-var name
))))
1515 (if (and (leaf-extent var
) (neq extent
(leaf-extent var
)))
1516 (warn "Multiple incompatible extent declarations for ~S?" name
)
1517 (setf (leaf-extent var
) extent
))
1519 "Ignoring free ~S declaration: ~S" kind name
)))
1521 (compiler-error "~S on symbol-macro: ~S" kind name
))
1523 (compiler-error "~S on alien-variable: ~S" kind name
))
1525 (compiler-style-warn
1526 "Unbound variable declared ~S: ~S" kind name
)))))
1528 (eq (car name
) 'function
)
1530 (valid-function-name-p (cadr name
))
1531 (neq :indefinite extent
))
1532 (let* ((fname (cadr name
))
1533 (bound-fun (find fname fvars
1534 :key
#'leaf-source-name
1536 (fun (or bound-fun
(lexenv-find fname funs
))))
1540 #!+stack-allocatable-closures
1541 (setf (leaf-extent bound-fun
) extent
)
1542 #!-stack-allocatable-closures
1544 "Ignoring DYNAMIC-EXTENT declaration on function ~S ~
1545 (not supported on this platform)." fname
)
1547 "Ignoring free DYNAMIC-EXTENT declaration: ~S" name
)))
1549 (compiler-error "DYNAMIC-EXTENT on macro: ~S" name
))
1551 (compiler-style-warn
1552 "Unbound function declared DYNAMIC-EXTENT: ~S" name
)))))
1554 (compiler-error "~S on a weird thing: ~S" kind name
))))
1555 (when (policy *lexenv
* (= speed
3))
1556 (compiler-notify "Ignoring DYNAMIC-EXTENT declarations: ~S" names
)))))
1558 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
1559 ;;; go away, I think.
1560 (defvar *suppress-values-declaration
* nil
1562 "If true, processing of the VALUES declaration is inhibited.")
1564 ;;; Process a single declaration spec, augmenting the specified LEXENV
1565 ;;; RES. Return RES and result type. VARS and FVARS are as described
1567 (defun process-1-decl (raw-spec res vars fvars binding-form-p context
)
1568 (declare (type list raw-spec vars fvars
))
1569 (declare (type lexenv res
))
1570 (let ((spec (canonized-decl-spec raw-spec
))
1571 (optimize-qualities)
1572 (result-type *wild-type
*))
1575 (special (process-special-decl spec res vars binding-form-p context
))
1578 (compiler-error "no type specified in FTYPE declaration: ~S" spec
))
1579 (process-ftype-decl (second spec
) res
(cddr spec
) fvars context
))
1580 ((inline notinline maybe-inline
)
1581 (process-inline-decl spec res fvars
))
1583 (process-ignore-decl spec vars fvars res
)
1586 (multiple-value-bind (new-policy specified-qualities
)
1587 (process-optimize-decl spec
(lexenv-policy res
))
1588 (setq optimize-qualities specified-qualities
)
1589 (make-lexenv :default res
:policy new-policy
)))
1593 :handled-conditions
(process-muffle-conditions-decl
1594 spec
(lexenv-handled-conditions res
))))
1595 (unmuffle-conditions
1598 :handled-conditions
(process-unmuffle-conditions-decl
1599 spec
(lexenv-handled-conditions res
))))
1601 (process-type-decl (cdr spec
) res vars context
))
1603 (unless *suppress-values-declaration
*
1604 (let ((types (cdr spec
)))
1606 (compiler-values-specifier-type
1607 (if (singleton-p types
)
1609 `(values ,@types
)))))
1611 ((dynamic-extent truly-dynamic-extent indefinite-extent
)
1612 (process-extent-decl (cdr spec
) vars fvars
(first spec
))
1614 ((disable-package-locks enable-package-locks
)
1617 :disabled-package-locks
(process-package-lock-decl
1618 spec
(lexenv-disabled-package-locks res
))))
1620 (unless (info :declaration
:recognized
(first spec
))
1621 (compiler-warn "unrecognized declaration ~S" raw-spec
))
1622 (let ((fn (info :declaration
:handler
(first spec
))))
1624 (funcall fn res spec vars fvars
)
1627 optimize-qualities
)))
1629 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1630 ;;; and FUNCTIONAL structures which are being bound. In addition to
1631 ;;; filling in slots in the leaf structures, we return a new LEXENV,
1632 ;;; which reflects pervasive special and function type declarations,
1633 ;;; (NOT)INLINE declarations and OPTIMIZE declarations, and type of
1634 ;;; VALUES declarations. If BINDING-FORM-P is true, the third return
1635 ;;; value is a list of VARs that should not apply to the lexenv of the
1636 ;;; initialization forms for the bindings, but should apply to the body.
1638 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1640 (defun process-decls (decls vars fvars
&key
1641 (lexenv *lexenv
*) (binding-form-p nil
) (context :compile
)
1642 (allow-lambda-list nil
))
1643 (declare (list decls vars fvars
))
1644 (let ((result-type *wild-type
*)
1645 (lambda-list (if allow-lambda-list
:unspecified nil
))
1646 (optimize-qualities)
1647 (*post-binding-variable-lexenv
* nil
))
1648 (dolist (decl decls
)
1649 (dolist (spec (rest decl
))
1652 (unless (consp spec
)
1653 (compiler-error "malformed declaration specifier ~S in ~S"
1655 (if (and (typep spec
'(cons (eql lambda-list
) (cons t null
)))
1656 (eq allow-lambda-list t
))
1657 (setq lambda-list
(cadr spec
) allow-lambda-list nil
)
1658 (multiple-value-bind (new-env new-result-type new-qualities
)
1659 (process-1-decl spec lexenv vars fvars
1660 binding-form-p context
)
1661 (setq lexenv new-env
1663 (nconc new-qualities optimize-qualities
))
1664 (unless (eq new-result-type
*wild-type
*)
1666 (values-type-intersection result-type
1667 new-result-type
)))))))
1668 (if (eq context
:compile
)
1669 (let ((*current-path
* (or (get-source-path spec
)
1670 (get-source-path decl
)
1673 ;; Kludge: EVAL calls this function to deal with LOCALLY.
1675 (warn-repeated-optimize-qualities (lexenv-policy lexenv
) optimize-qualities
)
1676 (values lexenv result-type
*post-binding-variable-lexenv
* lambda-list
)))
1678 (defun %processing-decls
(decls vars fvars ctran lvar binding-form-p fun
)
1679 (multiple-value-bind (*lexenv
* result-type post-binding-lexenv
)
1680 (process-decls decls vars fvars
:binding-form-p binding-form-p
)
1681 (cond ((eq result-type
*wild-type
*)
1682 (funcall fun ctran lvar post-binding-lexenv
))
1684 (let ((value-ctran (make-ctran))
1685 (value-lvar (make-lvar)))
1686 (multiple-value-prog1
1687 (funcall fun value-ctran value-lvar post-binding-lexenv
)
1688 (let ((cast (make-cast value-lvar result-type
1689 (lexenv-policy *lexenv
*))))
1690 (link-node-to-previous-ctran cast value-ctran
)
1691 (setf (lvar-dest value-lvar
) cast
)
1692 (use-continuation cast ctran lvar
))))))))
1694 (defmacro processing-decls
((decls vars fvars ctran lvar
1695 &optional post-binding-lexenv
)
1697 (check-type ctran symbol
)
1698 (check-type lvar symbol
)
1699 (let ((post-binding-lexenv-p (not (null post-binding-lexenv
)))
1700 (post-binding-lexenv (or post-binding-lexenv
(sb!xc
:gensym
"LEXENV"))))
1701 `(%processing-decls
,decls
,vars
,fvars
,ctran
,lvar
1702 ,post-binding-lexenv-p
1703 (lambda (,ctran
,lvar
,post-binding-lexenv
)
1704 (declare (ignorable ,post-binding-lexenv
))
1707 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1708 ;;; declaration. If there is a global variable of that name, then
1709 ;;; check that it isn't a constant and return it. Otherwise, create an
1710 ;;; anonymous GLOBAL-VAR.
1711 (defun specvar-for-binding (name)
1712 (cond ((not (eq (info :variable
:where-from name
) :assumed
))
1713 (let ((found (find-free-var name
)))
1714 (when (heap-alien-info-p found
)
1716 "~S is an alien variable and so can't be declared special."
1718 (unless (global-var-p found
)
1720 "~S is a constant and so can't be declared special."
1724 (make-global-var :kind
:special
1726 :where-from
:declared
))))