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 (declaim (inline source-form-has-path-p
))
25 (defun source-form-has-path-p (form)
26 (not (typep form
'(or symbol fixnum character
))))
28 (defun get-source-path (form)
29 (when (source-form-has-path-p form
)
30 (gethash form
*source-paths
*)))
32 (defun ensure-source-path (form)
33 (or (get-source-path form
)
34 (cons (simplify-source-path-form form
)
37 (defun simplify-source-path-form (form)
39 (let ((op (car form
)))
40 ;; In the compiler functions can be directly represented
41 ;; by leaves. Having leaves in the source path is pretty
42 ;; hard on the poor user, however, so replace with the
43 ;; source-name when possible.
44 (if (and (leaf-p op
) (leaf-has-source-name-p op
))
45 (cons (leaf-source-name op
) (cdr form
))
49 (defun note-source-path (form &rest arguments
)
50 (when (source-form-has-path-p form
)
51 (setf (gethash form
*source-paths
*)
52 (apply #'list
* 'original-source-start
*current-form-number
* arguments
))))
54 ;;; *CURRENT-COMPONENT* is the COMPONENT structure which we link
55 ;;; blocks into as we generate them. This just serves to glue the
56 ;;; emitted blocks together until local call analysis and flow graph
57 ;;; canonicalization figure out what is really going on. We need to
58 ;;; keep track of all the blocks generated so that we can delete them
59 ;;; if they turn out to be unreachable.
61 ;;; FIXME: It's confusing having one variable named *CURRENT-COMPONENT*
62 ;;; and another named *COMPONENT-BEING-COMPILED*. (In CMU CL they
63 ;;; were called *CURRENT-COMPONENT* and *COMPILE-COMPONENT* respectively,
64 ;;; which was also confusing.)
65 (declaim (type (or component null
) *current-component
*))
66 (defvar *current-component
*)
68 ;;; *CURRENT-PATH* is the source path of the form we are currently
69 ;;; translating. See NODE-SOURCE-PATH in the NODE structure.
70 (declaim (list *current-path
*))
71 (defvar *current-path
*)
73 (defun call-with-current-source-form (thunk &rest forms
)
74 (let ((*current-path
* (when (and (some #'identity forms
)
75 (boundp '*source-paths
*))
76 (or (some #'get-source-path forms
)
77 (when (boundp '*current-path
*)
81 (defvar *derive-function-types
* nil
82 "Should the compiler assume that function types will never change,
83 so that it can use type information inferred from current definitions
84 to optimize code which uses those definitions? Setting this true
85 gives non-ANSI, early-CMU-CL behavior. It can be useful for improving
86 the efficiency of stable code.")
88 (defvar *post-binding-variable-lexenv
* nil
)
90 ;;;; namespace management utilities
92 ;; As with LEXENV-FIND, we assume use of *LEXENV*, but macroexpanders
93 ;; receive an explicit environment and should pass it.
94 ;; A declaration will trump a proclamation.
95 (defun fun-lexically-notinline-p (name &optional
(env *lexenv
*))
99 #!+(and sb-fasteval
(host-feature sb-xc
))
100 (sb!interpreter
:basic-env
101 (sb!interpreter
::fun-lexically-notinline-p name env
))
103 (let ((fun (cdr (assoc name
(lexenv-funs env
) :test
#'equal
))))
104 ;; FIXME: this seems to omit FUNCTIONAL
105 (when (defined-fun-p fun
)
106 (return-from fun-lexically-notinline-p
107 (eq (defined-fun-inlinep fun
) :notinline
))))))))
108 ;; If ANSWER is NIL, go for the global value
109 (eq (or answer
(info :function
:inlinep name
))
112 (defun maybe-defined-here (name where
)
113 (if (and (eq :defined where
)
114 (member name
*fun-names-in-this-file
* :test
#'equal
))
118 ;;; Return a GLOBAL-VAR structure usable for referencing the global
120 (defun find-global-fun (name latep
)
121 (unless (info :function
:kind name
)
122 (setf (info :function
:kind name
) :function
)
123 (setf (info :function
:where-from name
) :assumed
))
124 (let ((where (info :function
:where-from name
)))
125 (when (and (eq where
:assumed
)
126 ;; Slot accessors are defined just-in-time, if not already.
127 (not (typep name
'(cons (eql sb
!pcl
::slot-accessor
))))
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 (proclaimed-ftype 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
))
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 (let ((kind (info :function
:kind name
)))
218 ((:macro
:special-form
)
219 (compiler-error "The ~(~S~) name ~S was found ~A."
222 (check-fun-name name
)
223 (let ((expansion (fun-name-inline-expansion name
))
224 (inlinep (info :function
:inlinep name
)))
225 (setf (gethash name
*free-funs
*)
226 (if (or expansion inlinep
)
227 (let ((where (info :function
:where-from name
)))
230 :inline-expansion expansion
232 :where-from
(if (eq inlinep
:notinline
)
234 (maybe-defined-here name where
))
235 :type
(if (and (eq inlinep
:notinline
)
236 (neq where
:declared
))
237 (specifier-type 'function
)
238 (proclaimed-ftype name
))))
239 (find-global-fun name nil
)))))))))
241 ;;; Return the LEAF structure for the lexically apparent function
242 ;;; definition of NAME.
243 (declaim (ftype (sfunction (t string
) leaf
) find-lexically-apparent-fun
))
244 (defun find-lexically-apparent-fun (name context
)
245 (let ((var (lexenv-find name funs
:test
#'equal
)))
248 (aver (and (consp var
) (eq (car var
) 'macro
)))
249 (compiler-error "found macro name ~S ~A" name context
))
252 (find-free-fun name context
)))))
254 (defun maybe-find-free-var (name)
255 (let ((found (gethash name
*free-vars
*)))
256 (unless (eq found
:deprecated
)
259 ;;; Return the LEAF node for a global variable reference to NAME. If
260 ;;; NAME is already entered in *FREE-VARS*, then we just return the
261 ;;; corresponding value. Otherwise, we make a new leaf using
262 ;;; information from the global environment and enter it in
263 ;;; *FREE-VARS*. If the variable is unknown, then we emit a warning.
264 (declaim (ftype (sfunction (t) (or leaf cons heap-alien-info
)) find-free-var
))
265 (defun find-free-var (name &aux
(existing (gethash name
*free-vars
*)))
266 (unless (symbolp name
)
267 (compiler-error "Variable name is not a symbol: ~S." name
))
268 (or (when (and existing
(neq existing
:deprecated
))
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 ;; For deprecated vars, warn about LET and LAMBDA bindings, SETQ, and ref.
277 ;; Don't warn again if the name was already seen by the transform
278 ;; of SYMBOL[-GLOBAL]-VALUE.
279 (unless (eq existing
:deprecated
)
280 (case deprecation-state
282 (check-deprecated-thing 'variable name
))))
283 (setf (gethash name
*free-vars
*)
286 (info :variable
:alien-info name
))
287 ;; FIXME: The return value in this case should really be
288 ;; of type SB!C::LEAF. I don't feel too badly about it,
289 ;; because the MACRO idiom is scattered throughout this
290 ;; file, but it should be cleaned up so we're not
291 ;; throwing random conses around. --njf 2002-03-23
293 (let ((expansion (info :variable
:macro-expansion name
))
294 (type (type-specifier (info :variable
:type name
))))
295 `(macro .
(the ,type
,expansion
))))
297 (let ((value (symbol-value name
)))
298 ;; Override the values of standard symbols in XC,
299 ;; since we can't redefine them.
301 (when (eql (find-symbol (symbol-name name
) :cl
) name
)
302 (multiple-value-bind (xc-value foundp
)
303 (xc-constant-value name
)
305 (setf value xc-value
))
306 ((not (eq value name
))
308 "Using cross-compilation host's definition of ~S: ~A~%"
309 name
(symbol-value name
))))))
310 (find-constant value name
)))
312 (make-global-var :kind kind
315 :where-from where-from
)))))))
317 ;;; Grovel over CONSTANT checking for any sub-parts that need to be
318 ;;; processed with MAKE-LOAD-FORM. We have to be careful, because
319 ;;; CONSTANT might be circular. We also check that the constant (and
320 ;;; any subparts) are dumpable at all.
321 (defun maybe-emit-make-load-forms (constant &optional
(name nil namep
))
322 (let ((xset (alloc-xset)))
323 (labels ((trivialp (value)
327 (or unboxed-array
#!+sb-simd-pack simd-pack
)
329 (and array
(not (array t
)))
333 string
))) ; subsumed by UNBOXED-ARRAY
335 ;; Unless VALUE is an object which which obviously
336 ;; can't contain other objects
337 (unless (trivialp value
)
338 (if (xset-member-p value xset
)
339 (return-from grovel nil
)
340 (add-to-xset value xset
))
344 (grovel (cdr value
)))
346 (dotimes (i (length value
))
347 (grovel (svref value i
))))
349 (dotimes (i (length value
))
350 (grovel (aref value i
))))
352 ;; Even though the (ARRAY T) branch does the exact
353 ;; same thing as this branch we do this separately
354 ;; so that the compiler can use faster versions of
355 ;; array-total-size and row-major-aref.
356 (dotimes (i (array-total-size value
))
357 (grovel (row-major-aref value i
))))
359 (dotimes (i (array-total-size value
))
360 (grovel (row-major-aref value i
))))
361 (#+sb-xc-host structure
!object
362 #-sb-xc-host instance
363 ;; In the target SBCL, we can dump any instance, but
364 ;; in the cross-compilation host, %INSTANCE-FOO
365 ;; functions don't work on general instances, only on
366 ;; STRUCTURE!OBJECTs.
368 ;; Behold the wonderfully clear sense of this-
369 ;; WHEN (EMIT-MAKE-LOAD-FORM VALUE)
370 ;; meaning "when you're _NOT_ using a custom load-form"
372 ;; FIXME: What about funcallable instances with
373 ;; user-defined MAKE-LOAD-FORM methods?
374 (when (emit-make-load-form value
)
376 (aver (eql (layout-bitmap (%instance-layout value
))
377 sb
!kernel
::+layout-all-tagged
+))
378 (do-instance-tagged-slot (i value
)
379 (grovel (%instance-ref value i
)))))
382 "Objects of type ~/sb!impl:print-type-specifier/ ~
383 can't be dumped into fasl files."
384 (type-of value
)))))))
385 ;; Dump all non-trivial named constants using the name.
386 (if (and namep
(not (typep constant
'(or symbol character
387 ;; FIXME: Cold init breaks if we
388 ;; try to reference FP constants
391 #-sb-xc-host fixnum
))))
392 (emit-make-load-form constant name
)
396 ;;;; some flow-graph hacking utilities
398 ;;; This function sets up the back link between the node and the
399 ;;; ctran which continues at it.
400 (defun link-node-to-previous-ctran (node ctran
)
401 (declare (type node node
) (type ctran ctran
))
402 (aver (not (ctran-next ctran
)))
403 (setf (ctran-next ctran
) node
)
404 (setf (node-prev node
) ctran
))
406 ;;; This function is used to set the ctran for a node, and thus
407 ;;; determine what is evaluated next. If the ctran has no block, then
408 ;;; we make it be in the block that the node is in. If the ctran heads
409 ;;; its block, we end our block and link it to that block.
410 #!-sb-fluid
(declaim (inline use-ctran
))
411 (defun use-ctran (node ctran
)
412 (declare (type node node
) (type ctran ctran
))
413 (if (eq (ctran-kind ctran
) :unused
)
414 (let ((node-block (ctran-block (node-prev node
))))
415 (setf (ctran-block ctran
) node-block
)
416 (setf (ctran-kind ctran
) :inside-block
)
417 (setf (ctran-use ctran
) node
)
418 (setf (node-next node
) ctran
))
419 (%use-ctran node ctran
)))
420 (defun %use-ctran
(node ctran
)
421 (declare (type node node
) (type ctran ctran
) (inline member
))
422 (let ((block (ctran-block ctran
))
423 (node-block (ctran-block (node-prev node
))))
424 (aver (eq (ctran-kind ctran
) :block-start
))
425 (when (block-last node-block
)
426 (error "~S has already ended." node-block
))
427 (setf (block-last node-block
) node
)
428 (when (block-succ node-block
)
429 (error "~S already has successors." node-block
))
430 (setf (block-succ node-block
) (list block
))
431 (when (memq node-block
(block-pred block
))
432 (error "~S is already a predecessor of ~S." node-block block
))
433 (push node-block
(block-pred block
))))
435 ;;; Insert NEW before OLD in the flow-graph.
436 (defun insert-node-before (old new
)
437 (let ((prev (node-prev old
))
439 (ensure-block-start prev
)
440 (setf (ctran-next prev
) nil
)
441 (link-node-to-previous-ctran new prev
)
443 (link-node-to-previous-ctran old temp
))
446 ;;; This function is used to set the ctran for a node, and thus
447 ;;; determine what receives the value.
448 (defun use-lvar (node lvar
)
449 (declare (type valued-node node
) (type (or lvar null
) lvar
))
450 (aver (not (node-lvar node
)))
452 (setf (node-lvar node
) lvar
)
453 (cond ((null (lvar-uses lvar
))
454 (setf (lvar-uses lvar
) node
))
455 ((listp (lvar-uses lvar
))
456 (aver (not (memq node
(lvar-uses lvar
))))
457 (push node
(lvar-uses lvar
)))
459 (aver (neq node
(lvar-uses lvar
)))
460 (setf (lvar-uses lvar
) (list node
(lvar-uses lvar
)))))
461 (reoptimize-lvar lvar
)))
463 #!-sb-fluid
(declaim (inline use-continuation
))
464 (defun use-continuation (node ctran lvar
)
465 (use-ctran node ctran
)
466 (use-lvar node lvar
))
468 ;;;; exported functions
470 ;;; This function takes a form and the top level form number for that
471 ;;; form, and returns a lambda representing the translation of that
472 ;;; form in the current global environment. The returned lambda is a
473 ;;; top level lambda that can be called to cause evaluation of the
474 ;;; forms. This lambda is in the initial component. If FOR-VALUE is T,
475 ;;; then the value of the form is returned from the function,
476 ;;; otherwise NIL is returned.
478 ;;; This function may have arbitrary effects on the global environment
479 ;;; due to processing of EVAL-WHENs. All syntax error checking is
480 ;;; done, with erroneous forms being replaced by a proxy which signals
481 ;;; an error if it is evaluated. Warnings about possibly inconsistent
482 ;;; or illegal changes to the global environment will also be given.
484 ;;; We make the initial component and convert the form in a PROGN (and
485 ;;; an optional NIL tacked on the end.) We then return the lambda. We
486 ;;; bind all of our state variables here, rather than relying on the
487 ;;; global value (if any) so that IR1 conversion will be reentrant.
488 ;;; This is necessary for EVAL-WHEN processing, etc.
490 ;;; The hashtables used to hold global namespace info must be
491 ;;; reallocated elsewhere. Note also that *LEXENV* is not bound, so
492 ;;; that local macro definitions can be introduced by enclosing code.
493 (defun ir1-toplevel (form path for-value
&optional
(allow-instrumenting t
))
494 (declare (list path
))
495 (let* ((*current-path
* path
)
496 (component (make-empty-component))
497 (*current-component
* component
)
498 (*allow-instrumenting
* allow-instrumenting
))
499 (setf (component-name component
) 'initial-component
)
500 (setf (component-kind component
) :initial
)
501 (let* ((forms (if for-value
`(,form
) `(,form nil
)))
502 (res (ir1-convert-lambda-body
504 :debug-name
(debug-name 'top-level-form
#+sb-xc-host nil
#-sb-xc-host form
))))
505 (setf (functional-entry-fun res
) res
506 (functional-arg-documentation res
) ()
507 (functional-kind res
) :toplevel
)
510 ;;; This function is called on freshly read forms to record the
511 ;;; initial location of each form (and subform.) Form is the form to
512 ;;; find the paths in, and TLF-NUM is the top level form number of the
513 ;;; truly top level form.
515 ;;; This gets a bit interesting when the source code is circular. This
516 ;;; can (reasonably?) happen in the case of circular list constants.
517 (defun find-source-paths (form tlf-num
)
518 (declare (type index tlf-num
))
519 (let ((*current-form-number
* 0))
520 (sub-find-source-paths form
(list tlf-num
)))
522 (defun sub-find-source-paths (form path
)
523 (unless (get-source-path form
)
524 (note-source-path form path
)
525 (incf *current-form-number
*)
529 (declare (fixnum pos
))
532 (when (atom subform
) (return))
533 (let ((fm (car subform
)))
534 (when (sb!int
:comma-p fm
)
535 (setf fm
(sb!int
:comma-expr fm
)))
537 ;; If it's a cons, recurse.
538 (sub-find-source-paths fm
(cons pos path
)))
540 ;; Don't look into quoted constants.
541 ;; KLUDGE: this can't actually know about constants.
542 ;; e.g. (let ((quote (error "foo")))) or
543 ;; (list quote (error "foo")) are not
544 ;; constants and yet are ignored.
547 ;; Otherwise store the containing form. It's not
548 ;; perfect, but better than nothing.
549 (note-source-path subform pos path
)))
551 (setq subform
(cdr subform
))
552 (when (eq subform trail
) (return)))))
556 (setq trail
(cdr trail
)))))))
558 ;;;; IR1-CONVERT, macroexpansion and special form dispatching
560 (declaim (ftype (sfunction (ctran ctran
(or lvar null
) t
)
563 (macrolet (;; Bind *COMPILER-ERROR-BAILOUT* to a function that throws
564 ;; out of the body and converts a condition signalling form
565 ;; instead. The source form is converted to a string since it
566 ;; may contain arbitrary non-externalizable objects.
567 (ir1-error-bailout ((start next result form
) &body body
)
568 (with-unique-names (skip condition
)
570 (let ((,condition
(catch 'ir1-error-abort
571 (let ((*compiler-error-bailout
*
572 (lambda (&optional e
)
573 (throw 'ir1-error-abort e
))))
575 (return-from ,skip nil
)))))
576 (ir1-convert ,start
,next
,result
577 (make-compiler-error-form ,condition
580 ;; Translate FORM into IR1. The code is inserted as the NEXT of the
581 ;; CTRAN START. RESULT is the LVAR which receives the value of the
582 ;; FORM to be translated. The translators call this function
583 ;; recursively to translate their subnodes.
585 ;; As a special hack to make life easier in the compiler, a LEAF
586 ;; IR1-converts into a reference to that LEAF structure. This allows
587 ;; the creation using backquote of forms that contain leaf
588 ;; references, without having to introduce dummy names into the
590 (defun ir1-convert (start next result form
)
591 (let* ((*current-path
* (ensure-source-path form
))
592 (start (instrument-coverage start nil form
)))
593 (ir1-error-bailout (start next result form
)
595 (cond ((and (symbolp form
) (not (keywordp form
)))
596 (ir1-convert-var start next result form
))
598 (reference-leaf start next result form
))
600 (reference-constant start next result form
))))
601 ((not (proper-list-p form
))
602 (compiler-error "~@<~S is not a proper list.~@:>" form
))
604 (ir1-convert-functoid start next result form
)))))
607 ;; Generate a reference to a manifest constant, creating a new leaf
609 (defun reference-constant (start next result value
)
610 (declare (type ctran start next
)
611 (type (or lvar null
) result
))
612 (ir1-error-bailout (start next result value
)
613 (let* ((leaf (find-constant value
))
614 (res (make-ref leaf
)))
615 (push res
(leaf-refs leaf
))
616 (link-node-to-previous-ctran res start
)
617 (use-continuation res next result
)))
620 ;;; Add FUNCTIONAL to the COMPONENT-REANALYZE-FUNCTIONALS, unless it's
621 ;;; some trivial type for which reanalysis is a trivial no-op, or
622 ;;; unless it doesn't belong in this component at all.
624 ;;; FUNCTIONAL is returned.
625 (defun maybe-reanalyze-functional (functional)
627 (aver (not (eql (functional-kind functional
) :deleted
))) ; bug 148
628 (aver-live-component *current-component
*)
630 ;; When FUNCTIONAL is of a type for which reanalysis isn't a trivial
632 (when (typep functional
'(or optional-dispatch clambda
))
634 ;; When FUNCTIONAL knows its component
635 (when (lambda-p functional
)
636 (aver (eql (lambda-component functional
) *current-component
*)))
639 (component-reanalyze-functionals *current-component
*)))
643 ;;; Generate a REF node for LEAF, frobbing the LEAF structure as
644 ;;; needed. If LEAF represents a defined function which has already
645 ;;; been converted, and is not :NOTINLINE, then reference the
646 ;;; functional instead.
647 (defun reference-leaf (start next result leaf
&optional
(name '.anonymous.
))
648 (declare (type ctran start next
) (type (or lvar null
) result
) (type leaf leaf
))
649 (assure-leaf-live-p leaf
)
650 (let* ((type (lexenv-find leaf type-restrictions
))
651 (leaf (or (and (defined-fun-p leaf
)
652 (not (eq (defined-fun-inlinep leaf
)
654 (let ((functional (defined-fun-functional leaf
)))
655 (when (and functional
(not (functional-kind functional
)))
656 (maybe-reanalyze-functional functional
))))
657 (when (and (lambda-p leaf
)
658 (memq (functional-kind leaf
)
660 (maybe-reanalyze-functional leaf
))
662 (ref (make-ref leaf name
)))
663 (push ref
(leaf-refs leaf
))
664 (setf (leaf-ever-used leaf
) t
)
665 (link-node-to-previous-ctran ref start
)
666 (cond (type (let* ((ref-ctran (make-ctran))
667 (ref-lvar (make-lvar))
668 (cast (make-cast ref-lvar
669 (make-single-value-type type
)
670 (lexenv-policy *lexenv
*))))
671 (setf (lvar-dest ref-lvar
) cast
)
672 (use-continuation ref ref-ctran ref-lvar
)
673 (link-node-to-previous-ctran cast ref-ctran
)
674 (use-continuation cast next result
)))
675 (t (use-continuation ref next result
)))))
677 (defun always-boundp (name)
678 (case (info :variable
:always-bound name
)
680 ;; Compiling to fasl considers a symbol always-bound if its
681 ;; :always-bound info value is now T or will eventually be T.
682 (:eventually
(producing-fasl-file))))
684 ;;; Convert a reference to a symbolic constant or variable. If the
685 ;;; symbol is entered in the LEXENV-VARS we use that definition,
686 ;;; otherwise we find the current global definition. This is also
687 ;;; where we pick off symbol macro and alien variable references.
688 (defun ir1-convert-var (start next result name
)
689 (declare (type ctran start next
) (type (or lvar null
) result
) (symbol name
))
690 (let ((var (or (lexenv-find name vars
) (find-free-var name
))))
691 (if (and (global-var-p var
) (not (always-boundp name
)))
692 ;; KLUDGE: If the variable may be unbound, convert using SYMEVAL
693 ;; which is not flushable, so that unbound dead variables signal an
694 ;; error (bug 412, lp#722734): checking for null RESULT is not enough,
695 ;; since variables can become dead due to later optimizations.
696 (ir1-convert start next result
697 (if (eq (global-var-kind var
) :global
)
698 `(sym-global-val ',name
)
704 (let ((home (ctran-home-lambda-or-null start
)))
706 (sset-adjoin var
(lambda-calls-or-closes home
))))
707 (when (lambda-var-ignorep var
)
708 ;; (ANSI's specification for the IGNORE declaration requires
709 ;; that this be a STYLE-WARNING, not a full WARNING.)
711 (compiler-style-warn "reading an ignored variable: ~S" name
)
712 ;; there's no need for us to accept ANSI's lameness when
713 ;; processing our own code, though.
715 (warn "reading an ignored variable: ~S" name
))))
716 (reference-leaf start next result var name
))
717 ((cons (eql macro
)) ; symbol-macro
718 ;; FIXME: the following comment is probably wrong now.
719 ;; If we warn here on :early and :late deprecation
720 ;; then we get an extra warning somehow.
721 ;; This case signals {EARLY,LATE,FINAL}-DEPRECATION-WARNING
722 ;; for symbol-macros. Includes variables, constants,
723 ;; etc. in :FINAL deprecation.
724 (when (eq (deprecated-thing-p 'variable name
) :final
)
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 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
857 ;;; errors which occur during the macroexpansion.
858 (defun careful-expand-macro (fun form
&optional cmacro
)
859 (flet (;; Return a string to use as a prefix in error reporting,
860 ;; telling something about which form caused the problem.
862 (let (;; We rely on the printer to abbreviate FORM.
866 "~@<~A of ~S. Use ~S to intercept.~%~:@>"
868 #-sb-xc-host
"Error during compiler-macroexpansion"
869 #+sb-xc-host
"Error during XC compiler-macroexpansion")
871 #-sb-xc-host
"during macroexpansion"
872 #+sb-xc-host
"during XC macroexpansion"))
874 '*break-on-signals
*))))
875 (handler-bind (;; KLUDGE: CMU CL in its wisdom (version 2.4.6 for Debian
876 ;; Linux, anyway) raises a CL:WARNING condition (not a
877 ;; CL:STYLE-WARNING) for undefined symbols when converting
878 ;; interpreted functions, causing COMPILE-FILE to think the
879 ;; file has a real problem, causing COMPILE-FILE to return
880 ;; FAILURE-P set (not just WARNINGS-P set). Since undefined
881 ;; symbol warnings are often harmless forward references,
882 ;; and since it'd be inordinately painful to try to
883 ;; eliminate all such forward references, these warnings
884 ;; are basically unavoidable. Thus, we need to coerce the
885 ;; system to work through them, and this code does so, by
886 ;; crudely suppressing all warnings in cross-compilation
887 ;; macroexpansion. -- WHN 19990412
888 #+(and cmu sb-xc-host
)
893 ~@<(KLUDGE: That was a non-STYLE WARNING. ~
894 Ordinarily that would cause compilation to ~
895 fail. However, since we're running under ~
896 CMU CL, and since CMU CL emits non-STYLE ~
897 warnings for safe, hard-to-fix things (e.g. ~
898 references to not-yet-defined functions) ~
899 we're going to have to ignore it and ~
900 proceed anyway. Hopefully we're not ~
901 ignoring anything horrible here..)~:@>~:>"
905 (bug "no MUFFLE-WARNING restart")))
910 ;; The spec is silent on what we should do. Signaling
911 ;; a full warning but declining to expand seems like
912 ;; a conservative and sane thing to do.
913 (compiler-warn "~@<~A~@:_ ~A~:>" (wherestring) c
)
914 (return-from careful-expand-macro form
))
916 (compiler-error "~@<~A~@:_ ~A~:>"
917 (wherestring) c
))))))
918 (funcall (valid-macroexpand-hook) fun form
*lexenv
*))))
920 ;;;; conversion utilities
922 ;;; Convert a bunch of forms, discarding all the values except the
923 ;;; last. If there aren't any forms, then translate a NIL.
924 (declaim (ftype (sfunction (ctran ctran
(or lvar null
) list
) (values))
925 ir1-convert-progn-body
))
926 (defun ir1-convert-progn-body (start next result body
)
928 (reference-constant start next result nil
)
929 (let ((this-start start
)
932 (let ((form (car forms
)))
934 (maybe-instrument-progn-like this-start forms form
))
935 (when (endp (cdr forms
))
936 (ir1-convert this-start next result form
)
938 (let ((this-ctran (make-ctran)))
939 (ir1-convert this-start this-ctran nil form
)
940 (setq this-start this-ctran
941 forms
(cdr forms
)))))))
947 ;;; Used as the CDR of the code coverage instrumentation records
948 ;;; (instead of NIL) to ensure that any well-behaving user code will
949 ;;; not have constants EQUAL to that record. This avoids problems with
950 ;;; the records getting coalesced with non-record conses, which then
951 ;;; get mutated when the instrumentation runs. Note that it's
952 ;;; important for multiple records for the same location to be
953 ;;; coalesced. -- JES, 2008-01-02
954 ;;; Use of #. mandates :COMPILE-TOPLEVEL for several Lisps
955 ;;; even though for us it's immediately accessible to EVAL.
956 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
957 (defconstant +code-coverage-unmarked
+ '%code-coverage-unmarked%
))
959 ;;; Check the policy for whether we should generate code coverage
960 ;;; instrumentation. If not, just return the original START
961 ;;; ctran. Otherwise insert code coverage instrumentation after
962 ;;; START, and return the new ctran.
963 (defun instrument-coverage (start mode form
964 &aux
(metadata *compiler-coverage-metadata
*))
965 ;; We don't actually use FORM for anything, it's just convenient to
966 ;; have around when debugging the instrumentation.
967 (declare (ignore form
))
969 (policy *lexenv
* (> store-coverage-data
0))
970 *allow-instrumenting
*)
971 (let ((path (source-path-original-source *current-path
*)))
974 (if (member (ctran-block start
)
975 (gethash path
(code-coverage-blocks metadata
)))
976 ;; If this source path has already been instrumented in
977 ;; this block, don't instrument it again.
980 ;; Get an interned record cons for the path. A cons
981 ;; with the same object identity must be used for
982 ;; each instrument for the same block.
983 (ensure-gethash path
(code-coverage-records metadata
)
984 (cons path
+code-coverage-unmarked
+)))
986 (*allow-instrumenting
* nil
))
987 (push (ctran-block start
)
988 (gethash path
(code-coverage-blocks metadata
)))
989 (ir1-convert start next nil
991 (declare (optimize speed
994 (check-constant-modification 0)))
995 ;; We're being naughty here, and
996 ;; modifying constant data. That's ok,
997 ;; we know what we're doing.
998 (%rplacd
',store t
)))
1002 ;;; In contexts where we don't have a source location for FORM
1003 ;;; e.g. due to it not being a cons, but where we have a source
1004 ;;; location for the enclosing cons, use the latter source location if
1005 ;;; available. This works pretty well in practice, since many PROGNish
1006 ;;; macroexpansions will just directly splice a block of forms into
1007 ;;; some enclosing form with `(progn ,@body), thus retaining the
1008 ;;; EQness of the conses.
1009 (defun maybe-instrument-progn-like (start forms form
)
1010 (or (when (and *allow-instrumenting
*
1011 (not (get-source-path form
)))
1012 (let ((*current-path
* (get-source-path forms
)))
1013 (when *current-path
*
1014 (instrument-coverage start nil form
))))
1017 (defun record-code-coverage (info cc
)
1018 (setf (gethash info
*code-coverage-info
*) cc
))
1020 (defun clear-code-coverage ()
1021 (clrhash *code-coverage-info
*))
1023 (defun reset-code-coverage ()
1024 (maphash (lambda (info cc
)
1025 (declare (ignore info
))
1026 (dolist (cc-entry cc
)
1027 (setf (cdr cc-entry
) +code-coverage-unmarked
+)))
1028 *code-coverage-info
*))
1030 (defun code-coverage-record-marked (record)
1031 (aver (consp record
))
1033 ((#.
+code-coverage-unmarked
+) nil
)
1037 ;;;; converting combinations
1039 ;;; Does this form look like something that we should add single-stepping
1040 ;;; instrumentation for?
1041 (defun step-form-p (form)
1042 (flet ((step-symbol-p (symbol)
1043 ;; Consistent treatment of *FOO* vs (SYMBOL-VALUE '*FOO*):
1044 ;; we insert calls to SYMEVAL for most non-lexical
1045 ;; variable references in order to avoid them being elided
1046 ;; if the value is unused.
1047 (if (member symbol
'(symeval sym-global-val
))
1048 (not (constantp (second form
)))
1049 (not (member (symbol-package symbol
)
1051 ;; KLUDGE: packages we're not interested in
1053 (mapcar #'find-package
'(sb!c sb
!int sb
!impl
1056 (and *allow-instrumenting
*
1057 (policy *lexenv
* (= insert-step-conditions
3))
1059 (symbolp (car form
))
1060 (step-symbol-p (car form
)))))
1062 ;;; Convert a function call where the function FUN is a LEAF. FORM is
1063 ;;; the source for the call. We return the COMBINATION node so that
1064 ;;; the caller can poke at it if it wants to.
1065 (declaim (ftype (sfunction (ctran ctran
(or lvar null
) list leaf
) combination
)
1066 ir1-convert-combination
))
1067 (defun ir1-convert-combination (start next result form fun
)
1068 (let ((ctran (make-ctran))
1069 (fun-lvar (make-lvar)))
1070 (ir1-convert start ctran fun-lvar
`(the (or function symbol
) ,fun
))
1072 (ir1-convert-combination-args fun-lvar ctran next result
1074 (when (step-form-p form
)
1075 ;; Store a string representation of the form in the
1076 ;; combination node. This will let the IR2 translator know
1077 ;; that we want stepper instrumentation for this node. The
1078 ;; string will be stored in the debug-info by DUMP-1-LOCATION.
1079 (setf (combination-step-info combination
)
1080 (let ((*print-pretty
* t
)
1082 (*print-readably
* nil
))
1083 (prin1-to-string form
))))
1086 ;;; Convert the arguments to a call and make the COMBINATION
1087 ;;; node. FUN-LVAR yields the function to call. ARGS is the list of
1088 ;;; arguments for the call, which defaults to the cdr of source. We
1089 ;;; return the COMBINATION node.
1090 (defun ir1-convert-combination-args (fun-lvar start next result args
)
1091 (declare (type ctran start next
)
1092 (type lvar fun-lvar
)
1093 (type (or lvar null
) result
)
1095 (let ((node (make-combination fun-lvar
)))
1096 (setf (lvar-dest fun-lvar
) node
)
1097 (collect ((arg-lvars))
1098 (let ((this-start start
)
1102 (aver (not (lvar-dest arg
)))
1104 (setf (lvar-dest arg
) node
))
1107 (maybe-instrument-progn-like this-start forms arg
))
1108 (let ((this-ctran (make-ctran))
1109 (this-lvar (make-lvar node
)))
1110 (ir1-convert this-start this-ctran this-lvar arg
)
1111 (setq this-start this-ctran
)
1112 (arg-lvars this-lvar
))))
1114 (link-node-to-previous-ctran node this-start
)
1115 (use-continuation node next result
)
1116 (setf (combination-args node
) (arg-lvars))))
1119 ;;; Convert a call to a global function. If not :NOTINLINE, then we do
1120 ;;; source transforms and try out any inline expansion. If there is no
1121 ;;; expansion, but is :INLINE, then give an efficiency note (unless a
1122 ;;; known function which will quite possibly be open-coded.) Next, we
1123 ;;; go to ok-combination conversion.
1124 (defun ir1-convert-srctran (start next result var form
)
1125 (declare (type ctran start next
) (type (or lvar null
) result
)
1126 (type global-var var
))
1127 (let ((name (leaf-source-name var
))
1128 (inlinep (when (defined-fun-p var
)
1129 (defined-fun-inlinep var
))))
1130 (if (eq inlinep
:notinline
)
1131 (ir1-convert-combination start next result form var
)
1132 (let* ((transform (info :function
:source-transform name
)))
1134 (multiple-value-bind (transformed pass
)
1135 (if (functionp transform
)
1136 (funcall transform form
*lexenv
*)
1138 (if (eq (cdr transform
) :predicate
)
1139 (and (singleton-p (cdr form
))
1142 ',(dd-name (car transform
))))
1143 (slot-access-transform
1144 (if (consp name
) :write
:read
)
1145 (cdr form
) transform
))))
1146 (values result
(null result
))))
1148 (ir1-convert-maybe-predicate start next result form var
))
1150 (unless (policy *lexenv
* (zerop store-xref-data
))
1151 (record-call name
(ctran-block start
) *current-path
*))
1152 (ir1-convert start next result transformed
))))
1153 (ir1-convert-maybe-predicate start next result form var
))))))
1155 ;;; KLUDGE: If we insert a synthetic IF for a function with the PREDICATE
1156 ;;; attribute, don't generate any branch coverage instrumentation for it.
1157 (defvar *instrument-if-for-code-coverage
* t
)
1159 ;;; If the function has the PREDICATE attribute, and the RESULT's DEST
1160 ;;; isn't an IF, then we convert (IF <form> T NIL), ensuring that a
1161 ;;; predicate always appears in a conditional context.
1163 ;;; If the function isn't a predicate, then we call
1164 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
1165 (defun ir1-convert-maybe-predicate (start next result form var
)
1166 (declare (type ctran start next
)
1167 (type (or lvar null
) result
)
1169 (type global-var var
))
1170 (let ((info (info :function
:info
(leaf-source-name var
))))
1172 (ir1-attributep (fun-info-attributes info
) predicate
)
1173 (not (if-p (and result
(lvar-dest result
)))))
1174 (let ((*instrument-if-for-code-coverage
* nil
))
1175 (ir1-convert start next result
`(if ,form t nil
)))
1176 (ir1-convert-combination-checking-type start next result form var
))))
1178 ;;; Actually really convert a global function call that we are allowed
1181 ;;; If we know the function type of the function, then we check the
1182 ;;; call for syntactic legality with respect to the declared function
1183 ;;; type. If it is impossible to determine whether the call is correct
1184 ;;; due to non-constant keywords, then we give up, marking the call as
1185 ;;; :FULL to inhibit further error messages. We return true when the
1188 ;;; If the call is legal, we also propagate type assertions from the
1189 ;;; function type to the arg and result lvars. We do this now so that
1190 ;;; IR1 optimize doesn't have to redundantly do the check later so
1191 ;;; that it can do the type propagation.
1192 (defun ir1-convert-combination-checking-type (start next result form var
)
1193 (declare (type ctran start next
) (type (or lvar null
) result
)
1196 (let* ((node (ir1-convert-combination start next result form var
))
1197 (fun-lvar (basic-combination-fun node
))
1198 (type (leaf-type var
)))
1199 (when (validate-call-type node type var t
)
1200 (setf (lvar-%derived-type fun-lvar
)
1201 (make-single-value-type type
))
1202 (setf (lvar-reoptimize fun-lvar
) nil
)))
1205 ;;; Convert a call to a local function, or if the function has already
1206 ;;; been LET converted, then throw FUNCTIONAL to
1207 ;;; LOCALL-ALREADY-LET-CONVERTED. The THROW should only happen when we
1208 ;;; are converting inline expansions for local functions during
1210 (defun ir1-convert-local-combination (start next result form functional
)
1211 (assure-functional-live-p functional
)
1212 (ir1-convert-combination start next result
1214 (maybe-reanalyze-functional functional
)))
1218 ;;; Given a list of LAMBDA-VARs and a variable name, return the
1219 ;;; LAMBDA-VAR for that name, or NIL if it isn't found. We return the
1220 ;;; *last* variable with that name, since LET* bindings may be
1221 ;;; duplicated, and declarations always apply to the last.
1222 (declaim (ftype (sfunction (list symbol
) (or lambda-var list
))
1224 (defun find-in-bindings (vars name
)
1228 (when (eq (leaf-source-name var
) name
)
1230 (let ((info (lambda-var-arg-info var
)))
1232 (let ((supplied-p (arg-info-supplied-p info
)))
1233 (when (and supplied-p
1234 (eq (leaf-source-name supplied-p
) name
))
1235 (setq found supplied-p
))))))
1236 ((and (consp var
) (eq (car var
) name
))
1237 (setf found
(cdr var
)))))
1240 ;;; Called by PROCESS-DECLS to deal with a variable type declaration.
1241 ;;; If a LAMBDA-VAR being bound, we intersect the type with the var's
1242 ;;; type, otherwise we add a type restriction on the var. If a symbol
1243 ;;; macro, we just wrap a THE around the expansion.
1244 (defun process-type-decl (decl res vars context
)
1245 (declare (type list decl vars
) (type lexenv res
))
1246 (let* ((type-specifier (first decl
))
1248 (when (typep type-specifier
'type-specifier
)
1249 (check-deprecated-type type-specifier
))
1250 (compiler-specifier-type type-specifier
))))
1251 (collect ((restr nil cons
)
1252 (new-vars nil cons
))
1253 (dolist (var-name (rest decl
))
1254 (unless (symbolp var-name
)
1255 (compiler-error "Variable name is not a symbol: ~S." var-name
))
1256 (unless (eq (info :variable
:kind var-name
) :unknown
)
1257 (program-assert-symbol-home-package-unlocked
1258 context var-name
"declaring the type of ~A"))
1259 (let* ((bound-var (find-in-bindings vars var-name
))
1261 (lexenv-find var-name vars
)
1262 (find-free-var var-name
))))
1266 ((process-var (var bound-var
)
1267 (let* ((old-type (or (lexenv-find var type-restrictions
)
1269 (int (if (or (fun-type-p type
)
1270 (fun-type-p old-type
))
1272 (type-approx-intersection2
1274 (cond ((eq int
*empty-type
*)
1275 (unless (policy *lexenv
* (= inhibit-warnings
3))
1279 "The type declarations ~
1280 ~/sb!impl:print-type/ and ~
1281 ~/sb!impl:print-type/ for ~
1284 (list old-type type var-name
))))
1286 (setf (leaf-type bound-var
) int
1287 (leaf-where-from bound-var
) :declared
))
1289 (restr (cons var int
)))))))
1290 (process-var var bound-var
)
1291 (awhen (and (lambda-var-p var
)
1292 (lambda-var-specvar var
))
1293 (process-var it nil
))))
1295 ;; FIXME: non-ANSI weirdness. [See lp#309122]
1296 (aver (eq (car var
) 'macro
))
1297 (new-vars `(,var-name .
(macro .
(the ,(first decl
)
1301 "~S is an alien variable, so its type can't be declared."
1304 (if (or (restr) (new-vars))
1305 (make-lexenv :default res
1306 :type-restrictions
(restr)
1310 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
1311 ;;; declarations for function variables. In addition to allowing
1312 ;;; declarations for functions being bound, we must also deal with
1313 ;;; declarations that constrain the type of lexically apparent
1315 (defun process-ftype-decl (type-specifier res names fvars context
)
1316 (declare (type list names fvars
)
1318 (let ((type (compiler-specifier-type type-specifier
)))
1319 (check-deprecated-type type-specifier
)
1320 (unless (csubtypep type
(specifier-type 'function
))
1321 (compiler-style-warn "ignoring declared FTYPE: ~S (not a function type)"
1323 (return-from process-ftype-decl res
))
1324 (collect ((res nil cons
))
1325 (dolist (name names
)
1326 (when (fboundp name
)
1327 (program-assert-symbol-home-package-unlocked
1328 context name
"declaring the ftype of ~A"))
1329 (let ((found (find name fvars
:key
(lambda (x)
1330 (unless (consp x
) ;; macrolet
1331 (leaf-source-name x
)))
1335 (setf (leaf-type found
) type
)
1336 (assert-definition-type found type
1337 :unwinnage-fun
#'compiler-notify
1338 :where
"FTYPE declaration"))
1340 (res (cons (find-lexically-apparent-fun
1341 name
"in a function type declaration")
1344 (make-lexenv :default res
:type-restrictions
(res))
1347 ;;; Process a special declaration, returning a new LEXENV. A non-bound
1348 ;;; special declaration is instantiated by throwing a special variable
1349 ;;; into the variables if BINDING-FORM-P is NIL, or otherwise into
1350 ;;; *POST-BINDING-VARIABLE-LEXENV*.
1351 (defun process-special-decl (spec res vars binding-form-p context
)
1352 (declare (list spec vars
) (type lexenv res
))
1353 (collect ((new-venv nil cons
))
1354 (dolist (name (cdr spec
))
1355 ;; While CLHS seems to allow local SPECIAL declarations for constants,
1356 ;; whatever the semantics are supposed to be is not at all clear to me
1357 ;; -- since constants aren't allowed to be bound it should be a no-op as
1358 ;; no-one can observe the difference portably, but specials are allowed
1359 ;; to be bound... yet nowhere does it say that the special declaration
1360 ;; removes the constantness. Call it a spec bug and prohibit it. Same
1361 ;; for GLOBAL variables.
1362 (let ((kind (info :variable
:kind name
)))
1363 (unless (member kind
'(:special
:unknown
))
1365 "Can't declare ~(~A~) variable locally special: ~S"
1367 (program-assert-symbol-home-package-unlocked
1368 context name
"declaring ~A special")
1369 (let ((var (find-in-bindings vars name
)))
1372 (aver (eq (car var
) 'macro
))
1374 "~S is a symbol-macro and thus can't be declared special."
1377 (when (lambda-var-ignorep var
)
1378 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1379 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1380 (compiler-style-warn
1381 "The ignored variable ~S is being declared special."
1383 (setf (lambda-var-specvar var
)
1384 (specvar-for-binding name
)))
1386 (unless (or (assoc name
(new-venv) :test
#'eq
))
1387 (new-venv (cons name
(specvar-for-binding name
))))))))
1388 (cond (binding-form-p
1389 (setf *post-binding-variable-lexenv
*
1390 (append (new-venv) *post-binding-variable-lexenv
*))
1393 (make-lexenv :default res
:vars
(new-venv)))
1397 ;;; Return a DEFINED-FUN which copies a GLOBAL-VAR but for its INLINEP
1398 ;;; (and TYPE if notinline), plus type-restrictions from the lexenv.
1399 (defun make-new-inlinep (var inlinep local-type
)
1400 (declare (type global-var var
) (type inlinep inlinep
))
1401 (let* ((type (if (and (eq inlinep
:notinline
)
1402 (not (eq (leaf-where-from var
) :declared
)))
1403 (specifier-type 'function
)
1405 (res (make-defined-fun
1406 :%source-name
(leaf-source-name var
)
1407 :where-from
(leaf-where-from var
)
1408 :type
(if local-type
1409 (type-intersection local-type type
)
1412 (when (defined-fun-p var
)
1413 (setf (defined-fun-inline-expansion res
)
1414 (defined-fun-inline-expansion var
))
1415 (setf (defined-fun-functionals res
)
1416 (defined-fun-functionals var
)))
1417 ;; FIXME: Is this really right? Needs we not set the FUNCTIONAL
1418 ;; to the original global-var?
1421 ;;; Parse an inline/notinline declaration. If it's a local function we're
1422 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
1423 (defun process-inline-decl (spec res fvars
)
1424 (let ((sense (cdr (assoc (first spec
) +inlinep-translations
+ :test
#'eq
)))
1426 (dolist (name (rest spec
))
1427 (let ((fvar (find name fvars
1429 (unless (consp x
) ;; macrolet
1430 (leaf-source-name x
)))
1433 (setf (functional-inlinep fvar
) sense
)
1434 (let ((found (find-lexically-apparent-fun
1435 name
"in an inline or notinline declaration")))
1438 (when (policy *lexenv
* (>= speed inhibit-warnings
))
1439 (compiler-notify "ignoring ~A declaration not at ~
1440 definition of local function:~% ~S"
1444 (cdr (assoc found
(lexenv-type-restrictions res
)))))
1445 (push (cons name
(make-new-inlinep found sense type
))
1448 (make-lexenv :default res
:funs new-fenv
)
1451 ;;; like FIND-IN-BINDINGS, but looks for #'FOO in the FVARS
1452 (defun find-in-bindings-or-fbindings (name vars fvars
)
1453 (declare (list vars fvars
))
1456 (find-in-bindings vars name
))
1457 ((cons (eql function
) (cons * null
))
1458 (find (cadr name
) fvars
:key
(lambda (x)
1459 (if (consp x
) ;; MACROLET
1461 (leaf-source-name x
))) :test
#'equal
))
1463 (compiler-error "Malformed function or variable name ~S." name
))))
1465 ;;; Process an ignore/ignorable declaration, checking for various losing
1467 (defun process-ignore-decl (spec vars fvars lexenv
)
1468 (declare (list spec vars fvars
))
1469 (dolist (name (rest spec
))
1470 (let ((var (find-in-bindings-or-fbindings name vars fvars
)))
1473 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1474 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1475 ;; But, other Lisp hosts signal a full warning, so when building
1476 ;; the cross-compiler, compile it as #'WARN so that in a self-hosted
1477 ;; build we can at least crash in the same way,
1478 ;; until we resolve this question about how severe the warning is.
1479 (multiple-value-call #+sb-xc-host
#'warn
1480 #-sb-xc-host
#'compiler-style-warn
1481 "~A declaration for ~A: ~A"
1485 (case (info :variable
:kind name
)
1486 (:special
"a special variable")
1487 (:global
"a global lexical variable")
1488 (:alien
"a global alien variable")
1489 (t (if (assoc name
(lexenv-vars lexenv
))
1490 "a variable from outer scope"
1491 "an unknown variable")))
1494 (cond ((assoc (second name
) (lexenv-funs lexenv
)
1496 "a function from outer scope")
1497 ((info :function
:kind
(second name
))
1498 "a global function")
1500 "an unknown function"))
1503 (or (eq (car var
) 'macro
)
1504 (and (consp (cdr var
))
1505 (eq (cadr var
) 'macro
))))
1506 ;; Just ignore the IGNORE decl: we don't currently signal style-warnings
1507 ;; for unused macrolet or symbol-macros, so there's no need to do anything.
1510 (setf (leaf-ever-used var
) t
))
1511 ((and (lambda-var-specvar var
) (eq (first spec
) 'ignore
))
1512 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1513 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1514 (compiler-style-warn "Declaring special variable ~S to be ~A"
1517 ((eq (first spec
) 'ignorable
)
1518 (setf (leaf-ever-used var
) t
))
1520 (setf (lambda-var-ignorep var
) t
)))))
1523 (defun process-extent-decl (names vars fvars kind
)
1526 (truly-dynamic-extent
1529 (when *stack-allocate-dynamic-extent
*
1534 (dolist (name names
)
1537 (let* ((bound-var (find-in-bindings vars name
))
1539 (lexenv-find name vars
)
1540 (maybe-find-free-var name
))))
1544 (if (and (leaf-extent var
) (neq extent
(leaf-extent var
)))
1545 (warn "Multiple incompatible extent declarations for ~S?" name
)
1546 (setf (leaf-extent var
) extent
))
1548 "Ignoring free ~S declaration: ~S" kind name
)))
1550 (compiler-error "~S on symbol-macro: ~S" kind name
))
1552 (compiler-error "~S on alien-variable: ~S" kind name
))
1554 (compiler-style-warn
1555 "Unbound variable declared ~S: ~S" kind name
)))))
1557 (eq (car name
) 'function
)
1559 (valid-function-name-p (cadr name
))
1560 (neq :indefinite extent
))
1561 (let* ((fname (cadr name
))
1562 (bound-fun (find fname fvars
1564 (unless (consp x
) ;; macrolet
1565 (leaf-source-name x
)))
1567 (fun (or bound-fun
(lexenv-find fname funs
))))
1571 #!+stack-allocatable-closures
1572 (setf (leaf-extent bound-fun
) extent
)
1573 #!-stack-allocatable-closures
1575 "Ignoring DYNAMIC-EXTENT declaration on function ~S ~
1576 (not supported on this platform)." fname
)
1578 "Ignoring free DYNAMIC-EXTENT declaration: ~S" name
)))
1580 (compiler-error "DYNAMIC-EXTENT on macro: ~S" name
))
1582 (compiler-style-warn
1583 "Unbound function declared DYNAMIC-EXTENT: ~S" name
)))))
1585 (compiler-error "~S on a weird thing: ~S" kind name
))))
1586 (when (policy *lexenv
* (= speed
3))
1587 (compiler-notify "Ignoring DYNAMIC-EXTENT declarations: ~S" names
)))))
1589 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
1590 ;;; go away, I think.
1591 ;;; Or just rename the declaration to SB-C:RESULT-TYPE so that it's not
1592 ;;; a symbol in the CL package, and then eliminate this switch.
1593 ;;; It's permissible to have implementation-specific declarations.
1594 (defvar *suppress-values-declaration
* nil
1595 "If true, processing of the VALUES declaration is inhibited.")
1597 ;;; Process a single declaration spec, augmenting the specified LEXENV
1598 ;;; RES. Return RES and result type. VARS and FVARS are as described
1600 (defun process-1-decl (raw-spec res vars fvars binding-form-p context
)
1601 (declare (type list raw-spec vars fvars
))
1602 (declare (type lexenv res
))
1603 (let ((spec (canonized-decl-spec raw-spec
))
1604 (optimize-qualities))
1605 ;; FIXME: we can end up with a chain of spurious parent lexenvs,
1606 ;; when logically the processing of decls should yield at most
1607 ;; two new lexenvs: one for the bindings and one for post-binding.
1608 ;; It's possible that there's a simple fix of re-linking the resulting
1609 ;; lexenv directly to *lexenv* as its parent.
1613 (process-type-decl (cdr spec
) res vars context
))
1615 (process-ignore-decl spec vars fvars res
)
1617 (special (process-special-decl spec res vars binding-form-p context
))
1620 (compiler-error "no type specified in FTYPE declaration: ~S" spec
))
1621 (process-ftype-decl (second spec
) res
(cddr spec
) fvars context
))
1622 ((inline notinline maybe-inline
)
1623 (process-inline-decl spec res fvars
))
1625 (multiple-value-bind (new-policy specified-qualities
)
1626 (process-optimize-decl spec
(lexenv-policy res
))
1627 (setq optimize-qualities specified-qualities
)
1628 (make-lexenv :default res
:policy new-policy
)))
1632 :handled-conditions
(process-muffle-conditions-decl
1633 spec
(lexenv-handled-conditions res
))))
1634 (unmuffle-conditions
1637 :handled-conditions
(process-unmuffle-conditions-decl
1638 spec
(lexenv-handled-conditions res
))))
1639 ((dynamic-extent truly-dynamic-extent indefinite-extent
)
1640 (process-extent-decl (cdr spec
) vars fvars
(first spec
))
1642 ((disable-package-locks enable-package-locks
)
1645 :disabled-package-locks
(process-package-lock-decl
1646 spec
(lexenv-disabled-package-locks res
))))
1647 ;; We may want to detect LAMBDA-LIST and VALUES decls here,
1648 ;; and report them as "Misplaced" rather than "Unrecognized".
1650 (unless (info :declaration
:recognized
(first spec
))
1651 (compiler-warn "unrecognized declaration ~S" raw-spec
))
1652 (let ((fn (info :declaration
:handler
(first spec
))))
1654 (funcall fn res spec vars fvars
)
1656 optimize-qualities
)))
1658 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1659 ;;; and FUNCTIONAL structures which are being bound. In addition to
1660 ;;; filling in slots in the leaf structures, we return a new LEXENV,
1661 ;;; which reflects pervasive special and function type declarations,
1662 ;;; (NOT)INLINE declarations and OPTIMIZE declarations, and type of
1663 ;;; VALUES declarations. If BINDING-FORM-P is true, the third return
1664 ;;; value is a list of VARs that should not apply to the lexenv of the
1665 ;;; initialization forms for the bindings, but should apply to the body.
1667 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1669 (defun process-decls (decls vars fvars
&key
1670 (lexenv *lexenv
*) (binding-form-p nil
) (context :compile
)
1671 (allow-lambda-list nil
))
1672 (declare (list decls vars fvars
))
1673 (let ((result-type *wild-type
*)
1674 (allow-values-decl allow-lambda-list
)
1676 (allow-explicit-check allow-lambda-list
)
1677 (lambda-list (if allow-lambda-list
:unspecified nil
))
1678 (optimize-qualities)
1679 (*post-binding-variable-lexenv
* nil
))
1680 (flet ((process-it (spec decl
)
1682 (compiler-error "malformed declaration specifier ~S in ~S"
1684 ((and (eq allow-lambda-list t
)
1685 (typep spec
'(cons (eql lambda-list
) (cons t null
))))
1686 (setq lambda-list
(cadr spec
) allow-lambda-list nil
))
1687 ((and allow-values-decl
1688 (typep spec
'(cons (eql values
)))
1689 (not *suppress-values-declaration
*))
1690 ;; Why do we allow more than one VALUES decl? I don't know.
1692 (values-type-intersection
1694 (compiler-values-specifier-type
1695 (let ((types (cdr spec
)))
1696 (if (singleton-p types
)
1698 `(values ,@types
)))))))
1699 ((and allow-explicit-check
1700 (typep spec
'(cons (eql explicit-check
))))
1701 ;; EXPLICIT-CHECK by itself specifies that all argument and
1702 ;; result types are checked by the function body.
1703 ;; Alternatively, a subset of arguments, and/or :RESULT,
1704 ;; can be specified to indicate that only a subset are
1705 ;; checked; in that case, the compiler asserts all others.
1706 (awhen (remove-if (lambda (x)
1709 :key
#'lambda-var-%source-name
)
1712 (compiler-error "explicit-check list ~S must refer to lambda vars"
1714 (setq explicit-check
(or (cdr spec
) t
)
1715 allow-explicit-check nil
)) ; at most one of this decl
1717 (multiple-value-bind (new-env new-qualities
)
1718 (process-1-decl spec lexenv vars fvars
1719 binding-form-p context
)
1720 (setq lexenv new-env
1722 (nconc new-qualities optimize-qualities
)))))))
1723 (dolist (decl decls
)
1724 (dolist (spec (rest decl
))
1725 (if (eq context
:compile
)
1726 (with-current-source-form (spec decl
) ; TODO this is a slight change to the previous code. make sure the behavior is identical
1727 (process-it spec decl
))
1728 ;; Kludge: EVAL calls this function to deal with LOCALLY.
1729 (process-it spec decl
)))))
1730 (warn-repeated-optimize-qualities (lexenv-policy lexenv
) optimize-qualities
)
1731 (values lexenv result-type
*post-binding-variable-lexenv
*
1732 lambda-list explicit-check
)))
1734 (defun %processing-decls
(decls vars fvars ctran lvar binding-form-p fun
)
1735 (multiple-value-bind (*lexenv
* result-type post-binding-lexenv
)
1736 (process-decls decls vars fvars
:binding-form-p binding-form-p
)
1737 (cond ((eq result-type
*wild-type
*)
1738 (funcall fun ctran lvar post-binding-lexenv
))
1740 (let ((value-ctran (make-ctran))
1741 (value-lvar (make-lvar)))
1742 (multiple-value-prog1
1743 (funcall fun value-ctran value-lvar post-binding-lexenv
)
1744 (let ((cast (make-cast value-lvar result-type
1745 (lexenv-policy *lexenv
*))))
1746 (link-node-to-previous-ctran cast value-ctran
)
1747 (setf (lvar-dest value-lvar
) cast
)
1748 (use-continuation cast ctran lvar
))))))))
1750 (defmacro processing-decls
((decls vars fvars ctran lvar
1751 &optional post-binding-lexenv
)
1753 (check-type ctran symbol
)
1754 (check-type lvar symbol
)
1755 (let ((post-binding-lexenv-p (not (null post-binding-lexenv
)))
1756 (post-binding-lexenv (or post-binding-lexenv
(sb!xc
:gensym
"LEXENV"))))
1757 `(%processing-decls
,decls
,vars
,fvars
,ctran
,lvar
1758 ,post-binding-lexenv-p
1759 (lambda (,ctran
,lvar
,post-binding-lexenv
)
1760 (declare (ignorable ,post-binding-lexenv
))
1763 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1764 ;;; declaration. If there is a global variable of that name, then
1765 ;;; check that it isn't a constant and return it. Otherwise, create an
1766 ;;; anonymous GLOBAL-VAR.
1767 (defun specvar-for-binding (name)
1768 (cond ((not (eq (info :variable
:where-from name
) :assumed
))
1769 (let ((found (find-free-var name
)))
1770 (when (heap-alien-info-p found
)
1772 "~S is an alien variable and so can't be declared special."
1774 (unless (global-var-p found
)
1776 "~S is a constant and so can't be declared special."
1780 (make-global-var :kind
:special
1782 :where-from
:declared
))))