x86-64: LEA with neither disp nor index is MOV
[sbcl.git] / src / compiler / ir1tran.lisp
blob72dcd315db19b9c3eeca7742d06efc7d3c43ca0c
1 ;;;; This file contains code which does the translation from Lisp code
2 ;;;; to the first intermediate representation (IR1).
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB!C")
15 (declaim (special *compiler-error-bailout*))
17 ;;; *CURRENT-FORM-NUMBER* is used in FIND-SOURCE-PATHS to compute the
18 ;;; form number to associate with a source path. This should be bound
19 ;;; to an initial value of 0 before the processing of each truly
20 ;;; top level form.
21 (declaim (type index *current-form-number*))
22 (defvar *current-form-number*)
24 (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)
35 *current-path*)))
37 (defun simplify-source-path-form (form)
38 (if (consp 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))
46 form))
47 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.
60 ;;;
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*)
78 *current-path*)))))
79 (funcall thunk)))
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*))
96 (let ((answer
97 (typecase env
98 (null nil)
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))
110 :notinline)))
112 (defun maybe-defined-here (name where)
113 (if (and (eq :defined where)
114 (member name *fun-names-in-this-file* :test #'equal))
115 :defined-here
116 where))
118 ;;; Return a GLOBAL-VAR structure usable for referencing the global
119 ;;; function NAME.
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.
138 (not latep))
139 (note-undefined-reference name :function))
140 (let ((ftype (proclaimed-ftype name))
141 (notinline (fun-lexically-notinline-p name)))
142 (make-global-var
143 :kind :global-function
144 :%source-name name
145 :type (if (or (eq where :declared)
146 (and (not latep)
147 (not notinline)
148 *derive-function-types*))
149 ftype
150 (specifier-type 'function))
151 :defined-type (if (and (not latep) (not notinline))
152 ftype
153 (specifier-type 'function))
154 :where-from (if notinline
155 where
156 (maybe-defined-here name where))))))
158 ;;; Have some DEFINED-FUN-FUNCTIONALS of a *FREE-FUNS* entry become invalid?
159 ;;; Drop 'em.
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
173 ;;; target.
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))
200 :dead)))))
201 (defined-fun-functionals free-fun)))
202 nil))
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*)))
213 (when old-free-fun
214 (clear-invalid-functionals old-free-fun)
215 old-free-fun))
216 (let ((kind (info :function :kind name)))
217 (ecase kind
218 ((:macro :special-form)
219 (compiler-error "The ~(~S~) name ~S was found ~A."
220 kind name context))
221 ((:function nil)
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)))
228 (make-defined-fun
229 :%source-name name
230 :inline-expansion expansion
231 :inlinep inlinep
232 :where-from (if (eq inlinep :notinline)
233 where
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)))
246 (cond (var
247 (unless (leaf-p var)
248 (aver (and (consp var) (eq (car var) 'macro)))
249 (compiler-error "found macro name ~S ~A" name context))
250 var)
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)
257 found)))
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))
269 existing)
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
281 ((:early :late)
282 (check-deprecated-thing 'variable name))))
283 (setf (gethash name *free-vars*)
284 (case kind
285 (:alien
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
292 (:macro
293 (let ((expansion (info :variable :macro-expansion name))
294 (type (type-specifier (info :variable :type name))))
295 `(macro . (the ,type ,expansion))))
296 (:constant
297 (let ((value (symbol-value name)))
298 ;; Override the values of standard symbols in XC,
299 ;; since we can't redefine them.
300 #+sb-xc-host
301 (when (eql (find-symbol (symbol-name name) :cl) name)
302 (multiple-value-bind (xc-value foundp)
303 (xc-constant-value name)
304 (cond (foundp
305 (setf value xc-value))
306 ((not (eq value name))
307 (compiler-warn
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
313 :%source-name name
314 :type type
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)
324 (typep value
325 '(or
326 #-sb-xc-host
327 (or unboxed-array #!+sb-simd-pack simd-pack)
328 #+sb-xc-host
329 (and array (not (array t)))
330 symbol
331 number
332 character
333 string))) ; subsumed by UNBOXED-ARRAY
334 (grovel (value)
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))
341 (typecase value
342 (cons
343 (grovel (car value))
344 (grovel (cdr value)))
345 (simple-vector
346 (dotimes (i (length value))
347 (grovel (svref value i))))
348 ((vector t)
349 (dotimes (i (length value))
350 (grovel (aref value i))))
351 ((simple-array t)
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))))
358 ((array t)
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)
375 #+sb-xc-host
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)))))
381 (compiler-error
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
389 ;; thru their names.
390 #+sb-xc-host number
391 #-sb-xc-host fixnum))))
392 (emit-make-load-form constant name)
393 (grovel constant))))
394 (values))
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))
438 (temp (make-ctran)))
439 (ensure-block-start prev)
440 (setf (ctran-next prev) nil)
441 (link-node-to-previous-ctran new prev)
442 (use-ctran new temp)
443 (link-node-to-previous-ctran old temp))
444 (values))
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)))
451 (when lvar
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
503 forms ()
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)
508 res)))
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)))
521 (values))
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*)
526 (let ((pos 0)
527 (subform form)
528 (trail form))
529 (declare (fixnum pos))
530 (macrolet ((frob ()
531 `(progn
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)))
536 (cond ((consp fm)
537 ;; If it's a cons, recurse.
538 (sub-find-source-paths fm (cons pos path)))
539 ((eq 'quote fm)
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.
545 (return))
546 ((not (zerop pos))
547 ;; Otherwise store the containing form. It's not
548 ;; perfect, but better than nothing.
549 (note-source-path subform pos path)))
550 (incf pos))
551 (setq subform (cdr subform))
552 (when (eq subform trail) (return)))))
553 (loop
554 (frob)
555 (frob)
556 (setq trail (cdr trail)))))))
558 ;;;; IR1-CONVERT, macroexpansion and special form dispatching
560 (declaim (ftype (sfunction (ctran ctran (or lvar null) t)
561 (values))
562 ir1-convert))
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)
569 `(block ,skip
570 (let ((,condition (catch 'ir1-error-abort
571 (let ((*compiler-error-bailout*
572 (lambda (&optional e)
573 (throw 'ir1-error-abort e))))
574 ,@body
575 (return-from ,skip nil)))))
576 (ir1-convert ,start ,next ,result
577 (make-compiler-error-form ,condition
578 ,form)))))))
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
589 ;; namespace.
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)
594 (cond ((atom form)
595 (cond ((and (symbolp form) (not (keywordp form)))
596 (ir1-convert-var start next result form))
597 ((leaf-p form)
598 (reference-leaf start next result form))
600 (reference-constant start next result form))))
602 (ir1-convert-functoid start next result form)))))
603 (values))
605 ;; Generate a reference to a manifest constant, creating a new leaf
606 ;; if necessary.
607 (defun reference-constant (start next result value)
608 (declare (type ctran start next)
609 (type (or lvar null) result))
610 (ir1-error-bailout (start next result value)
611 (let* ((leaf (find-constant value))
612 (res (make-ref leaf)))
613 (push res (leaf-refs leaf))
614 (link-node-to-previous-ctran res start)
615 (use-continuation res next result)))
616 (values)))
618 ;;; Add FUNCTIONAL to the COMPONENT-REANALYZE-FUNCTIONALS, unless it's
619 ;;; some trivial type for which reanalysis is a trivial no-op, or
620 ;;; unless it doesn't belong in this component at all.
622 ;;; FUNCTIONAL is returned.
623 (defun maybe-reanalyze-functional (functional)
625 (aver (not (eql (functional-kind functional) :deleted))) ; bug 148
626 (aver-live-component *current-component*)
628 ;; When FUNCTIONAL is of a type for which reanalysis isn't a trivial
629 ;; no-op
630 (when (typep functional '(or optional-dispatch clambda))
632 ;; When FUNCTIONAL knows its component
633 (when (lambda-p functional)
634 (aver (eql (lambda-component functional) *current-component*)))
636 (pushnew functional
637 (component-reanalyze-functionals *current-component*)))
639 functional)
641 ;;; Generate a REF node for LEAF, frobbing the LEAF structure as
642 ;;; needed. If LEAF represents a defined function which has already
643 ;;; been converted, and is not :NOTINLINE, then reference the
644 ;;; functional instead.
645 (defun reference-leaf (start next result leaf &optional (name '.anonymous.))
646 (declare (type ctran start next) (type (or lvar null) result) (type leaf leaf))
647 (assure-leaf-live-p leaf)
648 (let* ((type (lexenv-find leaf type-restrictions))
649 (leaf (or (and (defined-fun-p leaf)
650 (not (eq (defined-fun-inlinep leaf)
651 :notinline))
652 (let ((functional (defined-fun-functional leaf)))
653 (when (and functional (not (functional-kind functional)))
654 (maybe-reanalyze-functional functional))))
655 (when (and (lambda-p leaf)
656 (memq (functional-kind leaf)
657 '(nil :optional)))
658 (maybe-reanalyze-functional leaf))
659 leaf))
660 (ref (make-ref leaf name)))
661 (push ref (leaf-refs leaf))
662 (setf (leaf-ever-used leaf) t)
663 (link-node-to-previous-ctran ref start)
664 (cond (type (let* ((ref-ctran (make-ctran))
665 (ref-lvar (make-lvar))
666 (cast (make-cast ref-lvar
667 (make-single-value-type type)
668 (lexenv-policy *lexenv*))))
669 (setf (lvar-dest ref-lvar) cast)
670 (use-continuation ref ref-ctran ref-lvar)
671 (link-node-to-previous-ctran cast ref-ctran)
672 (use-continuation cast next result)))
673 (t (use-continuation ref next result)))))
675 (defun always-boundp (name)
676 (case (info :variable :always-bound name)
677 (:always-bound t)
678 ;; Compiling to fasl considers a symbol always-bound if its
679 ;; :always-bound info value is now T or will eventually be T.
680 (:eventually (producing-fasl-file))))
682 ;;; Convert a reference to a symbolic constant or variable. If the
683 ;;; symbol is entered in the LEXENV-VARS we use that definition,
684 ;;; otherwise we find the current global definition. This is also
685 ;;; where we pick off symbol macro and alien variable references.
686 (defun ir1-convert-var (start next result name)
687 (declare (type ctran start next) (type (or lvar null) result) (symbol name))
688 (let ((var (or (lexenv-find name vars) (find-free-var name))))
689 (if (and (global-var-p var) (not (always-boundp name)))
690 ;; KLUDGE: If the variable may be unbound, convert using SYMEVAL
691 ;; which is not flushable, so that unbound dead variables signal an
692 ;; error (bug 412, lp#722734): checking for null RESULT is not enough,
693 ;; since variables can become dead due to later optimizations.
694 (ir1-convert start next result
695 (if (eq (global-var-kind var) :global)
696 `(sym-global-val ',name)
697 `(symeval ',name)))
698 (etypecase var
699 (leaf
700 (cond
701 ((lambda-var-p var)
702 (let ((home (ctran-home-lambda-or-null start)))
703 (when home
704 (sset-adjoin var (lambda-calls-or-closes home))))
705 (when (lambda-var-ignorep var)
706 ;; (ANSI's specification for the IGNORE declaration requires
707 ;; that this be a STYLE-WARNING, not a full WARNING.)
708 #-sb-xc-host
709 (compiler-style-warn "reading an ignored variable: ~S" name)
710 ;; there's no need for us to accept ANSI's lameness when
711 ;; processing our own code, though.
712 #+sb-xc-host
713 (warn "reading an ignored variable: ~S" name))))
714 (reference-leaf start next result var name))
715 ((cons (eql macro)) ; symbol-macro
716 ;; FIXME: the following comment is probably wrong now.
717 ;; If we warn here on :early and :late deprecation
718 ;; then we get an extra warning somehow.
719 ;; This case signals {EARLY,LATE,FINAL}-DEPRECATION-WARNING
720 ;; for symbol-macros. Includes variables, constants,
721 ;; etc. in :FINAL deprecation.
722 (when (eq (deprecated-thing-p 'variable name) :final)
723 (check-deprecated-thing 'variable name))
724 ;; FIXME: [Free] type declarations. -- APD, 2002-01-26
725 (ir1-convert start next result (cdr var)))
726 (heap-alien-info
727 (ir1-convert start next result `(%heap-alien ',var))))))
728 (values))
730 ;;; Find a compiler-macro for a form, taking FUNCALL into account.
731 (defun find-compiler-macro (opname form)
732 (flet ((legal-cm-name-p (name)
733 (and (legal-fun-name-p name)
734 (or (not (symbolp name))
735 (not (sb!xc:macro-function name *lexenv*))))))
736 (if (eq opname 'funcall)
737 (let ((fun-form (cadr form)))
738 (cond ((and (consp fun-form) (eq 'function (car fun-form))
739 (not (cddr fun-form)))
740 (let ((real-fun (cadr fun-form)))
741 (if (legal-cm-name-p real-fun)
742 (values (sb!xc:compiler-macro-function real-fun *lexenv*)
743 real-fun)
744 (values nil nil))))
745 ((sb!xc:constantp fun-form *lexenv*)
746 (let ((fun (constant-form-value fun-form *lexenv*)))
747 (if (legal-cm-name-p fun)
748 ;; CLHS tells us that local functions must shadow
749 ;; compiler-macro-functions, but since the call is
750 ;; through a name, we are obviously interested
751 ;; in the global function.
752 ;; KLUDGE: CLHS 3.2.2.1.1 also says that it can be
753 ;; "a list whose car is funcall and whose cadr is
754 ;; a list (function name)", that means that
755 ;; (funcall 'name) that gets here doesn't fit the
756 ;; definition.
757 (values (sb!xc:compiler-macro-function fun nil) fun)
758 (values nil nil))))
760 (values nil nil))))
761 (if (legal-fun-name-p opname)
762 (values (sb!xc:compiler-macro-function opname *lexenv*) opname)
763 (values nil nil)))))
765 ;;; If FORM has a usable compiler macro, use it; otherwise return FORM itself.
766 ;;; Return the name of the compiler-macro as a secondary value, if applicable.
767 (defun expand-compiler-macro (form)
768 (binding* ((name (car form))
769 ((cmacro-fun cmacro-fun-name)
770 (find-compiler-macro name form)))
771 (cond
772 ((and cmacro-fun
773 ;; CLHS 3.2.2.1.3 specifies that NOTINLINE
774 ;; suppresses compiler-macros.
775 (not (fun-lexically-notinline-p cmacro-fun-name)))
776 (check-deprecated-thing 'function name)
777 (values (handler-case (careful-expand-macro cmacro-fun form t)
778 (compiler-macro-keyword-problem (condition)
779 (print-compiler-message
780 *error-output* "note: ~A" (list condition))
781 form))
782 cmacro-fun-name))
784 (values form nil)))))
786 ;;; Picks off special forms and compiler-macro expansions, and hands
787 ;;; the rest to IR1-CONVERT-COMMON-FUNCTOID
788 (defun ir1-convert-functoid (start next result form)
789 (let* ((op (car form))
790 (translator (and (symbolp op) (info :function :ir1-convert op))))
791 (if translator
792 (funcall translator start next result form)
793 (multiple-value-bind (res cmacro-fun-name)
794 (expand-compiler-macro form)
795 (cond ((eq res form)
796 (ir1-convert-common-functoid start next result form op))
798 (unless (policy *lexenv* (zerop store-xref-data))
799 (record-call cmacro-fun-name (ctran-block start)
800 *current-path*))
801 (ir1-convert start next result res)))))))
803 ;;; Handles the "common" cases: any other forms except special forms
804 ;;; and compiler-macros.
805 (defun ir1-convert-common-functoid (start next result form op)
806 (cond ((or (symbolp op) (leaf-p op))
807 (let ((lexical-def (if (leaf-p op) op (lexenv-find op funs))))
808 (typecase lexical-def
809 (null
810 (check-deprecated-thing 'function op)
811 (ir1-convert-global-functoid start next result form op))
812 (functional
813 (ir1-convert-local-combination start next result form
814 lexical-def))
815 (global-var
816 (check-deprecated-thing 'function (leaf-source-name lexical-def))
817 (ir1-convert-srctran start next result lexical-def form))
819 (aver (and (consp lexical-def) (eq (car lexical-def) 'macro)))
820 (ir1-convert start next result
821 (careful-expand-macro (cdr lexical-def) form))))))
822 ((or (atom op) (not (eq (car op) 'lambda)))
823 (compiler-error "illegal function call"))
825 ;; implicitly (LAMBDA ..) because the LAMBDA expression is
826 ;; the CAR of an executed form.
827 (ir1-convert start next result `(%funcall ,@form)))))
829 ;;; Convert anything that looks like a global function call.
830 (defun ir1-convert-global-functoid (start next result form fun)
831 (declare (type ctran start next) (type (or lvar null) result)
832 (list form))
833 (when (eql fun 'declare)
834 (compiler-error
835 "~@<There is no function named ~S. ~
836 References to ~S in some contexts (like starts of blocks) are unevaluated ~
837 expressions, but here the expression is being evaluated, which invokes ~
838 undefined behaviour.~@:>" fun fun))
839 ;; FIXME: Couldn't all the INFO calls here be converted into
840 ;; standard CL functions, like MACRO-FUNCTION or something? And what
841 ;; happens with lexically-defined (MACROLET) macros here, anyway?
842 (ecase (info :function :kind fun)
843 (:macro
844 (ir1-convert start next result
845 (careful-expand-macro (info :function :macro-function fun)
846 form))
847 (unless (policy *lexenv* (zerop store-xref-data))
848 (record-macroexpansion fun (ctran-block start) *current-path*)))
849 ((nil :function)
850 (ir1-convert-srctran start next result
851 (find-free-fun fun "shouldn't happen! (no-cmacro)")
852 form))))
854 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
855 ;;; errors which occur during the macroexpansion.
856 (defun careful-expand-macro (fun form &optional cmacro)
857 (flet (;; Return a string to use as a prefix in error reporting,
858 ;; telling something about which form caused the problem.
859 (wherestring ()
860 (let (;; We rely on the printer to abbreviate FORM.
861 (*print-length* 3)
862 (*print-level* 3))
863 (format nil
864 "~@<~A of ~S. Use ~S to intercept.~%~:@>"
865 (cond (cmacro
866 #-sb-xc-host "Error during compiler-macroexpansion"
867 #+sb-xc-host "Error during XC compiler-macroexpansion")
869 #-sb-xc-host "during macroexpansion"
870 #+sb-xc-host "during XC macroexpansion"))
871 form
872 '*break-on-signals*))))
873 (handler-bind (;; KLUDGE: CMU CL in its wisdom (version 2.4.6 for Debian
874 ;; Linux, anyway) raises a CL:WARNING condition (not a
875 ;; CL:STYLE-WARNING) for undefined symbols when converting
876 ;; interpreted functions, causing COMPILE-FILE to think the
877 ;; file has a real problem, causing COMPILE-FILE to return
878 ;; FAILURE-P set (not just WARNINGS-P set). Since undefined
879 ;; symbol warnings are often harmless forward references,
880 ;; and since it'd be inordinately painful to try to
881 ;; eliminate all such forward references, these warnings
882 ;; are basically unavoidable. Thus, we need to coerce the
883 ;; system to work through them, and this code does so, by
884 ;; crudely suppressing all warnings in cross-compilation
885 ;; macroexpansion. -- WHN 19990412
886 #+(and cmu sb-xc-host)
887 (warning (lambda (c)
888 (compiler-notify
889 "~@<~A~:@_~
890 ~A~:@_~
891 ~@<(KLUDGE: That was a non-STYLE WARNING. ~
892 Ordinarily that would cause compilation to ~
893 fail. However, since we're running under ~
894 CMU CL, and since CMU CL emits non-STYLE ~
895 warnings for safe, hard-to-fix things (e.g. ~
896 references to not-yet-defined functions) ~
897 we're going to have to ignore it and ~
898 proceed anyway. Hopefully we're not ~
899 ignoring anything horrible here..)~:@>~:>"
900 (wherestring)
902 (muffle-warning)
903 (bug "no MUFFLE-WARNING restart")))
904 (error
905 (lambda (c)
906 (cond
907 (cmacro
908 ;; The spec is silent on what we should do. Signaling
909 ;; a full warning but declining to expand seems like
910 ;; a conservative and sane thing to do.
911 (compiler-warn "~@<~A~@:_ ~A~:>" (wherestring) c)
912 (return-from careful-expand-macro form))
914 (compiler-error "~@<~A~@:_ ~A~:>"
915 (wherestring) c))))))
916 (funcall (valid-macroexpand-hook) fun form *lexenv*))))
918 ;;;; conversion utilities
920 ;;; Convert a bunch of forms, discarding all the values except the
921 ;;; last. If there aren't any forms, then translate a NIL.
922 (declaim (ftype (sfunction (ctran ctran (or lvar null) list) (values))
923 ir1-convert-progn-body))
924 (defun ir1-convert-progn-body (start next result body)
925 (if (endp body)
926 (reference-constant start next result nil)
927 (let ((this-start start)
928 (forms body))
929 (loop
930 (let ((form (car forms)))
931 (setf this-start
932 (maybe-instrument-progn-like this-start forms form))
933 (when (endp (cdr forms))
934 (ir1-convert this-start next result form)
935 (return))
936 (let ((this-ctran (make-ctran)))
937 (ir1-convert this-start this-ctran nil form)
938 (setq this-start this-ctran
939 forms (cdr forms)))))))
940 (values))
943 ;;;; code coverage
945 ;;; Used as the CDR of the code coverage instrumentation records
946 ;;; (instead of NIL) to ensure that any well-behaving user code will
947 ;;; not have constants EQUAL to that record. This avoids problems with
948 ;;; the records getting coalesced with non-record conses, which then
949 ;;; get mutated when the instrumentation runs. Note that it's
950 ;;; important for multiple records for the same location to be
951 ;;; coalesced. -- JES, 2008-01-02
952 ;;; Use of #. mandates :COMPILE-TOPLEVEL for several Lisps
953 ;;; even though for us it's immediately accessible to EVAL.
954 (eval-when (:compile-toplevel :load-toplevel :execute)
955 (defconstant +code-coverage-unmarked+ '%code-coverage-unmarked%))
957 ;;; Check the policy for whether we should generate code coverage
958 ;;; instrumentation. If not, just return the original START
959 ;;; ctran. Otherwise insert code coverage instrumentation after
960 ;;; START, and return the new ctran.
961 (defun instrument-coverage (start mode form)
962 ;; We don't actually use FORM for anything, it's just convenient to
963 ;; have around when debugging the instrumentation.
964 (declare (ignore form))
965 (if (and (policy *lexenv* (> store-coverage-data 0))
966 *code-coverage-records*
967 *allow-instrumenting*)
968 (let ((path (source-path-original-source *current-path*)))
969 (when mode
970 (push mode path))
971 (if (member (ctran-block start)
972 (gethash path *code-coverage-blocks*))
973 ;; If this source path has already been instrumented in
974 ;; this block, don't instrument it again.
975 start
976 (let ((store
977 ;; Get an interned record cons for the path. A cons
978 ;; with the same object identity must be used for
979 ;; each instrument for the same block.
980 (or (gethash path *code-coverage-records*)
981 (setf (gethash path *code-coverage-records*)
982 (cons path +code-coverage-unmarked+))))
983 (next (make-ctran))
984 (*allow-instrumenting* nil))
985 (push (ctran-block start)
986 (gethash path *code-coverage-blocks*))
987 (ir1-convert start next nil
988 `(locally
989 (declare (optimize speed
990 (safety 0)
991 (debug 0)
992 (check-constant-modification 0)))
993 ;; We're being naughty here, and
994 ;; modifying constant data. That's ok,
995 ;; we know what we're doing.
996 (%rplacd ',store t)))
997 next)))
998 start))
1000 ;;; In contexts where we don't have a source location for FORM
1001 ;;; e.g. due to it not being a cons, but where we have a source
1002 ;;; location for the enclosing cons, use the latter source location if
1003 ;;; available. This works pretty well in practice, since many PROGNish
1004 ;;; macroexpansions will just directly splice a block of forms into
1005 ;;; some enclosing form with `(progn ,@body), thus retaining the
1006 ;;; EQness of the conses.
1007 (defun maybe-instrument-progn-like (start forms form)
1008 (or (when (and *allow-instrumenting*
1009 (not (get-source-path form)))
1010 (let ((*current-path* (get-source-path forms)))
1011 (when *current-path*
1012 (instrument-coverage start nil form))))
1013 start))
1015 (defun record-code-coverage (info cc)
1016 (setf (gethash info *code-coverage-info*) cc))
1018 (defun clear-code-coverage ()
1019 (clrhash *code-coverage-info*))
1021 (defun reset-code-coverage ()
1022 (maphash (lambda (info cc)
1023 (declare (ignore info))
1024 (dolist (cc-entry cc)
1025 (setf (cdr cc-entry) +code-coverage-unmarked+)))
1026 *code-coverage-info*))
1028 (defun code-coverage-record-marked (record)
1029 (aver (consp record))
1030 (ecase (cdr record)
1031 ((#.+code-coverage-unmarked+) nil)
1032 ((t) t)))
1035 ;;;; converting combinations
1037 ;;; Does this form look like something that we should add single-stepping
1038 ;;; instrumentation for?
1039 (defun step-form-p (form)
1040 (flet ((step-symbol-p (symbol)
1041 ;; Consistent treatment of *FOO* vs (SYMBOL-VALUE '*FOO*):
1042 ;; we insert calls to SYMEVAL for most non-lexical
1043 ;; variable references in order to avoid them being elided
1044 ;; if the value is unused.
1045 (if (member symbol '(symeval sym-global-val))
1046 (not (constantp (second form)))
1047 (not (member (symbol-package symbol)
1048 (load-time-value
1049 ;; KLUDGE: packages we're not interested in
1050 ;; stepping.
1051 (mapcar #'find-package '(sb!c sb!int sb!impl
1052 sb!kernel sb!pcl))
1053 t))))))
1054 (and *allow-instrumenting*
1055 (policy *lexenv* (= insert-step-conditions 3))
1056 (listp form)
1057 (symbolp (car form))
1058 (step-symbol-p (car form)))))
1060 ;;; Convert a function call where the function FUN is a LEAF. FORM is
1061 ;;; the source for the call. We return the COMBINATION node so that
1062 ;;; the caller can poke at it if it wants to.
1063 (declaim (ftype (sfunction (ctran ctran (or lvar null) list leaf) combination)
1064 ir1-convert-combination))
1065 (defun ir1-convert-combination (start next result form fun)
1066 (let ((ctran (make-ctran))
1067 (fun-lvar (make-lvar)))
1068 (ir1-convert start ctran fun-lvar `(the (or function symbol) ,fun))
1069 (let ((combination
1070 (ir1-convert-combination-args fun-lvar ctran next result
1071 (cdr form))))
1072 (when (step-form-p form)
1073 ;; Store a string representation of the form in the
1074 ;; combination node. This will let the IR2 translator know
1075 ;; that we want stepper instrumentation for this node. The
1076 ;; string will be stored in the debug-info by DUMP-1-LOCATION.
1077 (setf (combination-step-info combination)
1078 (let ((*print-pretty* t)
1079 (*print-circle* t)
1080 (*print-readably* nil))
1081 (prin1-to-string form))))
1082 combination)))
1084 ;;; Convert the arguments to a call and make the COMBINATION
1085 ;;; node. FUN-LVAR yields the function to call. ARGS is the list of
1086 ;;; arguments for the call, which defaults to the cdr of source. We
1087 ;;; return the COMBINATION node.
1088 (defun ir1-convert-combination-args (fun-lvar start next result args)
1089 (declare (type ctran start next)
1090 (type lvar fun-lvar)
1091 (type (or lvar null) result)
1092 (list args))
1093 (let ((node (make-combination fun-lvar)))
1094 (setf (lvar-dest fun-lvar) node)
1095 (collect ((arg-lvars))
1096 (let ((this-start start)
1097 (forms args))
1098 (dolist (arg args)
1099 (setf this-start
1100 (maybe-instrument-progn-like this-start forms arg))
1101 (setf forms (cdr forms))
1102 (let ((this-ctran (make-ctran))
1103 (this-lvar (make-lvar node)))
1104 (ir1-convert this-start this-ctran this-lvar arg)
1105 (setq this-start this-ctran)
1106 (arg-lvars this-lvar)))
1107 (link-node-to-previous-ctran node this-start)
1108 (use-continuation node next result)
1109 (setf (combination-args node) (arg-lvars))))
1110 node))
1112 ;;; Convert a call to a global function. If not :NOTINLINE, then we do
1113 ;;; source transforms and try out any inline expansion. If there is no
1114 ;;; expansion, but is :INLINE, then give an efficiency note (unless a
1115 ;;; known function which will quite possibly be open-coded.) Next, we
1116 ;;; go to ok-combination conversion.
1117 (defun ir1-convert-srctran (start next result var form)
1118 (declare (type ctran start next) (type (or lvar null) result)
1119 (type global-var var))
1120 (let ((name (leaf-source-name var))
1121 (inlinep (when (defined-fun-p var)
1122 (defined-fun-inlinep var))))
1123 (if (eq inlinep :notinline)
1124 (ir1-convert-combination start next result form var)
1125 (let* ((transform (info :function :source-transform name)))
1126 (if transform
1127 (multiple-value-bind (transformed pass)
1128 (if (functionp transform)
1129 (funcall transform form *lexenv*)
1130 (let ((result
1131 (if (eq (cdr transform) :predicate)
1132 (and (singleton-p (cdr form))
1133 `(%instance-typep
1134 ,(cadr form)
1135 ',(dd-name (car transform))))
1136 (slot-access-transform
1137 (if (consp name) :write :read)
1138 (cdr form) transform))))
1139 (values result (null result))))
1140 (cond (pass
1141 (ir1-convert-maybe-predicate start next result form var))
1143 (unless (policy *lexenv* (zerop store-xref-data))
1144 (record-call name (ctran-block start) *current-path*))
1145 (ir1-convert start next result transformed))))
1146 (ir1-convert-maybe-predicate start next result form var))))))
1148 ;;; KLUDGE: If we insert a synthetic IF for a function with the PREDICATE
1149 ;;; attribute, don't generate any branch coverage instrumentation for it.
1150 (defvar *instrument-if-for-code-coverage* t)
1152 ;;; If the function has the PREDICATE attribute, and the RESULT's DEST
1153 ;;; isn't an IF, then we convert (IF <form> T NIL), ensuring that a
1154 ;;; predicate always appears in a conditional context.
1156 ;;; If the function isn't a predicate, then we call
1157 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
1158 (defun ir1-convert-maybe-predicate (start next result form var)
1159 (declare (type ctran start next)
1160 (type (or lvar null) result)
1161 (list form)
1162 (type global-var var))
1163 (let ((info (info :function :info (leaf-source-name var))))
1164 (if (and info
1165 (ir1-attributep (fun-info-attributes info) predicate)
1166 (not (if-p (and result (lvar-dest result)))))
1167 (let ((*instrument-if-for-code-coverage* nil))
1168 (ir1-convert start next result `(if ,form t nil)))
1169 (ir1-convert-combination-checking-type start next result form var))))
1171 ;;; Actually really convert a global function call that we are allowed
1172 ;;; to early-bind.
1174 ;;; If we know the function type of the function, then we check the
1175 ;;; call for syntactic legality with respect to the declared function
1176 ;;; type. If it is impossible to determine whether the call is correct
1177 ;;; due to non-constant keywords, then we give up, marking the call as
1178 ;;; :FULL to inhibit further error messages. We return true when the
1179 ;;; call is legal.
1181 ;;; If the call is legal, we also propagate type assertions from the
1182 ;;; function type to the arg and result lvars. We do this now so that
1183 ;;; IR1 optimize doesn't have to redundantly do the check later so
1184 ;;; that it can do the type propagation.
1185 (defun ir1-convert-combination-checking-type (start next result form var)
1186 (declare (type ctran start next) (type (or lvar null) result)
1187 (list form)
1188 (type leaf var))
1189 (let* ((node (ir1-convert-combination start next result form var))
1190 (fun-lvar (basic-combination-fun node))
1191 (type (leaf-type var)))
1192 (when (validate-call-type node type var t)
1193 (setf (lvar-%derived-type fun-lvar)
1194 (make-single-value-type type))
1195 (setf (lvar-reoptimize fun-lvar) nil)))
1196 (values))
1198 ;;; Convert a call to a local function, or if the function has already
1199 ;;; been LET converted, then throw FUNCTIONAL to
1200 ;;; LOCALL-ALREADY-LET-CONVERTED. The THROW should only happen when we
1201 ;;; are converting inline expansions for local functions during
1202 ;;; optimization.
1203 (defun ir1-convert-local-combination (start next result form functional)
1204 (assure-functional-live-p functional)
1205 (ir1-convert-combination start next result
1206 form
1207 (maybe-reanalyze-functional functional)))
1209 ;;;; PROCESS-DECLS
1211 ;;; Given a list of LAMBDA-VARs and a variable name, return the
1212 ;;; LAMBDA-VAR for that name, or NIL if it isn't found. We return the
1213 ;;; *last* variable with that name, since LET* bindings may be
1214 ;;; duplicated, and declarations always apply to the last.
1215 (declaim (ftype (sfunction (list symbol) (or lambda-var list))
1216 find-in-bindings))
1217 (defun find-in-bindings (vars name)
1218 (let ((found nil))
1219 (dolist (var vars)
1220 (cond ((leaf-p var)
1221 (when (eq (leaf-source-name var) name)
1222 (setq found var))
1223 (let ((info (lambda-var-arg-info var)))
1224 (when info
1225 (let ((supplied-p (arg-info-supplied-p info)))
1226 (when (and supplied-p
1227 (eq (leaf-source-name supplied-p) name))
1228 (setq found supplied-p))))))
1229 ((and (consp var) (eq (car var) name))
1230 (setf found (cdr var)))))
1231 found))
1233 ;;; Called by PROCESS-DECLS to deal with a variable type declaration.
1234 ;;; If a LAMBDA-VAR being bound, we intersect the type with the var's
1235 ;;; type, otherwise we add a type restriction on the var. If a symbol
1236 ;;; macro, we just wrap a THE around the expansion.
1237 (defun process-type-decl (decl res vars context)
1238 (declare (type list decl vars) (type lexenv res))
1239 (let* ((type-specifier (first decl))
1240 (type (progn
1241 (when (typep type-specifier 'type-specifier)
1242 (check-deprecated-type type-specifier))
1243 (compiler-specifier-type type-specifier))))
1244 (collect ((restr nil cons)
1245 (new-vars nil cons))
1246 (dolist (var-name (rest decl))
1247 (unless (symbolp var-name)
1248 (compiler-error "Variable name is not a symbol: ~S." var-name))
1249 (unless (eq (info :variable :kind var-name) :unknown)
1250 (program-assert-symbol-home-package-unlocked
1251 context var-name "declaring the type of ~A"))
1252 (let* ((bound-var (find-in-bindings vars var-name))
1253 (var (or bound-var
1254 (lexenv-find var-name vars)
1255 (find-free-var var-name))))
1256 (etypecase var
1257 (leaf
1258 (flet
1259 ((process-var (var bound-var)
1260 (let* ((old-type (or (lexenv-find var type-restrictions)
1261 (leaf-type var)))
1262 (int (if (or (fun-type-p type)
1263 (fun-type-p old-type))
1264 type
1265 (type-approx-intersection2
1266 old-type type))))
1267 (cond ((eq int *empty-type*)
1268 (unless (policy *lexenv* (= inhibit-warnings 3))
1269 (warn
1270 'type-warning
1271 :format-control
1272 "The type declarations ~
1273 ~/sb!impl:print-type/ and ~
1274 ~/sb!impl:print-type/ for ~
1275 ~S conflict."
1276 :format-arguments
1277 (list old-type type var-name))))
1278 (bound-var
1279 (setf (leaf-type bound-var) int
1280 (leaf-where-from bound-var) :declared))
1282 (restr (cons var int)))))))
1283 (process-var var bound-var)
1284 (awhen (and (lambda-var-p var)
1285 (lambda-var-specvar var))
1286 (process-var it nil))))
1287 (cons
1288 ;; FIXME: non-ANSI weirdness. [See lp#309122]
1289 (aver (eq (car var) 'macro))
1290 (new-vars `(,var-name . (macro . (the ,(first decl)
1291 ,(cdr var))))))
1292 (heap-alien-info
1293 (compiler-error
1294 "~S is an alien variable, so its type can't be declared."
1295 var-name)))))
1297 (if (or (restr) (new-vars))
1298 (make-lexenv :default res
1299 :type-restrictions (restr)
1300 :vars (new-vars))
1301 res))))
1303 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
1304 ;;; declarations for function variables. In addition to allowing
1305 ;;; declarations for functions being bound, we must also deal with
1306 ;;; declarations that constrain the type of lexically apparent
1307 ;;; functions.
1308 (defun process-ftype-decl (type-specifier res names fvars context)
1309 (declare (type list names fvars)
1310 (type lexenv res))
1311 (let ((type (compiler-specifier-type type-specifier)))
1312 (check-deprecated-type type-specifier)
1313 (unless (csubtypep type (specifier-type 'function))
1314 (compiler-style-warn "ignoring declared FTYPE: ~S (not a function type)"
1315 type-specifier)
1316 (return-from process-ftype-decl res))
1317 (collect ((res nil cons))
1318 (dolist (name names)
1319 (when (fboundp name)
1320 (program-assert-symbol-home-package-unlocked
1321 context name "declaring the ftype of ~A"))
1322 (let ((found (find name fvars :key (lambda (x)
1323 (unless (consp x) ;; macrolet
1324 (leaf-source-name x)))
1325 :test #'equal)))
1326 (cond
1327 (found
1328 (setf (leaf-type found) type)
1329 (assert-definition-type found type
1330 :unwinnage-fun #'compiler-notify
1331 :where "FTYPE declaration"))
1333 (res (cons (find-lexically-apparent-fun
1334 name "in a function type declaration")
1335 type))))))
1336 (if (res)
1337 (make-lexenv :default res :type-restrictions (res))
1338 res))))
1340 ;;; Process a special declaration, returning a new LEXENV. A non-bound
1341 ;;; special declaration is instantiated by throwing a special variable
1342 ;;; into the variables if BINDING-FORM-P is NIL, or otherwise into
1343 ;;; *POST-BINDING-VARIABLE-LEXENV*.
1344 (defun process-special-decl (spec res vars binding-form-p context)
1345 (declare (list spec vars) (type lexenv res))
1346 (collect ((new-venv nil cons))
1347 (dolist (name (cdr spec))
1348 ;; While CLHS seems to allow local SPECIAL declarations for constants,
1349 ;; whatever the semantics are supposed to be is not at all clear to me
1350 ;; -- since constants aren't allowed to be bound it should be a no-op as
1351 ;; no-one can observe the difference portably, but specials are allowed
1352 ;; to be bound... yet nowhere does it say that the special declaration
1353 ;; removes the constantness. Call it a spec bug and prohibit it. Same
1354 ;; for GLOBAL variables.
1355 (let ((kind (info :variable :kind name)))
1356 (unless (member kind '(:special :unknown))
1357 (compiler-error
1358 "Can't declare ~(~A~) variable locally special: ~S"
1359 kind name)))
1360 (program-assert-symbol-home-package-unlocked
1361 context name "declaring ~A special")
1362 (let ((var (find-in-bindings vars name)))
1363 (etypecase var
1364 (cons
1365 (aver (eq (car var) 'macro))
1366 (compiler-error
1367 "~S is a symbol-macro and thus can't be declared special."
1368 name))
1369 (lambda-var
1370 (when (lambda-var-ignorep var)
1371 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1372 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1373 (compiler-style-warn
1374 "The ignored variable ~S is being declared special."
1375 name))
1376 (setf (lambda-var-specvar var)
1377 (specvar-for-binding name)))
1378 (null
1379 (unless (or (assoc name (new-venv) :test #'eq))
1380 (new-venv (cons name (specvar-for-binding name))))))))
1381 (cond (binding-form-p
1382 (setf *post-binding-variable-lexenv*
1383 (append (new-venv) *post-binding-variable-lexenv*))
1384 res)
1385 ((new-venv)
1386 (make-lexenv :default res :vars (new-venv)))
1388 res))))
1390 ;;; Return a DEFINED-FUN which copies a GLOBAL-VAR but for its INLINEP
1391 ;;; (and TYPE if notinline), plus type-restrictions from the lexenv.
1392 (defun make-new-inlinep (var inlinep local-type)
1393 (declare (type global-var var) (type inlinep inlinep))
1394 (let* ((type (if (and (eq inlinep :notinline)
1395 (not (eq (leaf-where-from var) :declared)))
1396 (specifier-type 'function)
1397 (leaf-type var)))
1398 (res (make-defined-fun
1399 :%source-name (leaf-source-name var)
1400 :where-from (leaf-where-from var)
1401 :type (if local-type
1402 (type-intersection local-type type)
1403 type)
1404 :inlinep inlinep)))
1405 (when (defined-fun-p var)
1406 (setf (defined-fun-inline-expansion res)
1407 (defined-fun-inline-expansion var))
1408 (setf (defined-fun-functionals res)
1409 (defined-fun-functionals var)))
1410 ;; FIXME: Is this really right? Needs we not set the FUNCTIONAL
1411 ;; to the original global-var?
1412 res))
1414 ;;; Parse an inline/notinline declaration. If it's a local function we're
1415 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
1416 (defun process-inline-decl (spec res fvars)
1417 (let ((sense (cdr (assoc (first spec) +inlinep-translations+ :test #'eq)))
1418 (new-fenv ()))
1419 (dolist (name (rest spec))
1420 (let ((fvar (find name fvars
1421 :key (lambda (x)
1422 (unless (consp x) ;; macrolet
1423 (leaf-source-name x)))
1424 :test #'equal)))
1425 (if fvar
1426 (setf (functional-inlinep fvar) sense)
1427 (let ((found (find-lexically-apparent-fun
1428 name "in an inline or notinline declaration")))
1429 (etypecase found
1430 (functional
1431 (when (policy *lexenv* (>= speed inhibit-warnings))
1432 (compiler-notify "ignoring ~A declaration not at ~
1433 definition of local function:~% ~S"
1434 sense name)))
1435 (global-var
1436 (let ((type
1437 (cdr (assoc found (lexenv-type-restrictions res)))))
1438 (push (cons name (make-new-inlinep found sense type))
1439 new-fenv))))))))
1440 (if new-fenv
1441 (make-lexenv :default res :funs new-fenv)
1442 res)))
1444 ;;; like FIND-IN-BINDINGS, but looks for #'FOO in the FVARS
1445 (defun find-in-bindings-or-fbindings (name vars fvars)
1446 (declare (list vars fvars))
1447 (typecase name
1448 (atom
1449 (find-in-bindings vars name))
1450 ((cons (eql function) (cons * null))
1451 (find (cadr name) fvars :key (lambda (x)
1452 (if (consp x) ;; MACROLET
1453 (car x)
1454 (leaf-source-name x))) :test #'equal))
1456 (compiler-error "Malformed function or variable name ~S." name))))
1458 ;;; Process an ignore/ignorable declaration, checking for various losing
1459 ;;; conditions.
1460 (defun process-ignore-decl (spec vars fvars lexenv)
1461 (declare (list spec vars fvars))
1462 (dolist (name (rest spec))
1463 (let ((var (find-in-bindings-or-fbindings name vars fvars)))
1464 (cond
1465 ((not var)
1466 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1467 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1468 ;; But, other Lisp hosts signal a full warning, so when building
1469 ;; the cross-compiler, compile it as #'WARN so that in a self-hosted
1470 ;; build we can at least crash in the same way,
1471 ;; until we resolve this question about how severe the warning is.
1472 (multiple-value-call #+sb-xc-host #'warn
1473 #-sb-xc-host #'compiler-style-warn
1474 "~A declaration for ~A: ~A"
1475 (first spec)
1476 (if (symbolp name)
1477 (values
1478 (case (info :variable :kind name)
1479 (:special "a special variable")
1480 (:global "a global lexical variable")
1481 (:alien "a global alien variable")
1482 (t (if (assoc name (lexenv-vars lexenv))
1483 "a variable from outer scope"
1484 "an unknown variable")))
1485 name)
1486 (values
1487 (cond ((assoc (second name) (lexenv-funs lexenv)
1488 :test #'equal)
1489 "a function from outer scope")
1490 ((info :function :kind (second name))
1491 "a global function")
1493 "an unknown function"))
1494 (second name)))))
1495 ((and (consp var)
1496 (or (eq (car var) 'macro)
1497 (and (consp (cdr var))
1498 (eq (cadr var) 'macro))))
1499 ;; Just ignore the IGNORE decl: we don't currently signal style-warnings
1500 ;; for unused macrolet or symbol-macros, so there's no need to do anything.
1502 ((functional-p var)
1503 (setf (leaf-ever-used var) t))
1504 ((and (lambda-var-specvar var) (eq (first spec) 'ignore))
1505 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1506 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1507 (compiler-style-warn "Declaring special variable ~S to be ~A"
1508 name
1509 (first spec)))
1510 ((eq (first spec) 'ignorable)
1511 (setf (leaf-ever-used var) t))
1513 (setf (lambda-var-ignorep var) t)))))
1514 (values))
1516 (defun process-extent-decl (names vars fvars kind)
1517 (let ((extent
1518 (ecase kind
1519 (truly-dynamic-extent
1520 :always-dynamic)
1521 (dynamic-extent
1522 (when *stack-allocate-dynamic-extent*
1523 :maybe-dynamic))
1524 (indefinite-extent
1525 :indefinite))))
1526 (if extent
1527 (dolist (name names)
1528 (cond
1529 ((symbolp name)
1530 (let* ((bound-var (find-in-bindings vars name))
1531 (var (or bound-var
1532 (lexenv-find name vars)
1533 (maybe-find-free-var name))))
1534 (etypecase var
1535 (leaf
1536 (if bound-var
1537 (if (and (leaf-extent var) (neq extent (leaf-extent var)))
1538 (warn "Multiple incompatible extent declarations for ~S?" name)
1539 (setf (leaf-extent var) extent))
1540 (compiler-notify
1541 "Ignoring free ~S declaration: ~S" kind name)))
1542 (cons
1543 (compiler-error "~S on symbol-macro: ~S" kind name))
1544 (heap-alien-info
1545 (compiler-error "~S on alien-variable: ~S" kind name))
1546 (null
1547 (compiler-style-warn
1548 "Unbound variable declared ~S: ~S" kind name)))))
1549 ((and (consp name)
1550 (eq (car name) 'function)
1551 (null (cddr name))
1552 (valid-function-name-p (cadr name))
1553 (neq :indefinite extent))
1554 (let* ((fname (cadr name))
1555 (bound-fun (find fname fvars
1556 :key (lambda (x)
1557 (unless (consp x) ;; macrolet
1558 (leaf-source-name x)))
1559 :test #'equal))
1560 (fun (or bound-fun (lexenv-find fname funs))))
1561 (etypecase fun
1562 (leaf
1563 (if bound-fun
1564 #!+stack-allocatable-closures
1565 (setf (leaf-extent bound-fun) extent)
1566 #!-stack-allocatable-closures
1567 (compiler-notify
1568 "Ignoring DYNAMIC-EXTENT declaration on function ~S ~
1569 (not supported on this platform)." fname)
1570 (compiler-notify
1571 "Ignoring free DYNAMIC-EXTENT declaration: ~S" name)))
1572 (cons
1573 (compiler-error "DYNAMIC-EXTENT on macro: ~S" name))
1574 (null
1575 (compiler-style-warn
1576 "Unbound function declared DYNAMIC-EXTENT: ~S" name)))))
1578 (compiler-error "~S on a weird thing: ~S" kind name))))
1579 (when (policy *lexenv* (= speed 3))
1580 (compiler-notify "Ignoring DYNAMIC-EXTENT declarations: ~S" names)))))
1582 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
1583 ;;; go away, I think.
1584 ;;; Or just rename the declaration to SB-C:RESULT-TYPE so that it's not
1585 ;;; a symbol in the CL package, and then eliminate this switch.
1586 ;;; It's permissible to have implementation-specific declarations.
1587 (defvar *suppress-values-declaration* nil
1588 "If true, processing of the VALUES declaration is inhibited.")
1590 ;;; Process a single declaration spec, augmenting the specified LEXENV
1591 ;;; RES. Return RES and result type. VARS and FVARS are as described
1592 ;;; PROCESS-DECLS.
1593 (defun process-1-decl (raw-spec res vars fvars binding-form-p context)
1594 (declare (type list raw-spec vars fvars))
1595 (declare (type lexenv res))
1596 (let ((spec (canonized-decl-spec raw-spec))
1597 (optimize-qualities))
1598 ;; FIXME: we can end up with a chain of spurious parent lexenvs,
1599 ;; when logically the processing of decls should yield at most
1600 ;; two new lexenvs: one for the bindings and one for post-binding.
1601 ;; It's possible that there's a simple fix of re-linking the resulting
1602 ;; lexenv directly to *lexenv* as its parent.
1603 (values
1604 (case (first spec)
1605 (type
1606 (process-type-decl (cdr spec) res vars context))
1607 ((ignore ignorable)
1608 (process-ignore-decl spec vars fvars res)
1609 res)
1610 (special (process-special-decl spec res vars binding-form-p context))
1611 (ftype
1612 (unless (cdr spec)
1613 (compiler-error "no type specified in FTYPE declaration: ~S" spec))
1614 (process-ftype-decl (second spec) res (cddr spec) fvars context))
1615 ((inline notinline maybe-inline)
1616 (process-inline-decl spec res fvars))
1617 (optimize
1618 (multiple-value-bind (new-policy specified-qualities)
1619 (process-optimize-decl spec (lexenv-policy res))
1620 (setq optimize-qualities specified-qualities)
1621 (make-lexenv :default res :policy new-policy)))
1622 (muffle-conditions
1623 (make-lexenv
1624 :default res
1625 :handled-conditions (process-muffle-conditions-decl
1626 spec (lexenv-handled-conditions res))))
1627 (unmuffle-conditions
1628 (make-lexenv
1629 :default res
1630 :handled-conditions (process-unmuffle-conditions-decl
1631 spec (lexenv-handled-conditions res))))
1632 ((dynamic-extent truly-dynamic-extent indefinite-extent)
1633 (process-extent-decl (cdr spec) vars fvars (first spec))
1634 res)
1635 ((disable-package-locks enable-package-locks)
1636 (make-lexenv
1637 :default res
1638 :disabled-package-locks (process-package-lock-decl
1639 spec (lexenv-disabled-package-locks res))))
1640 ;; We may want to detect LAMBDA-LIST and VALUES decls here,
1641 ;; and report them as "Misplaced" rather than "Unrecognized".
1643 (unless (info :declaration :recognized (first spec))
1644 (compiler-warn "unrecognized declaration ~S" raw-spec))
1645 (let ((fn (info :declaration :handler (first spec))))
1646 (if fn
1647 (funcall fn res spec vars fvars)
1648 res))))
1649 optimize-qualities)))
1651 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1652 ;;; and FUNCTIONAL structures which are being bound. In addition to
1653 ;;; filling in slots in the leaf structures, we return a new LEXENV,
1654 ;;; which reflects pervasive special and function type declarations,
1655 ;;; (NOT)INLINE declarations and OPTIMIZE declarations, and type of
1656 ;;; VALUES declarations. If BINDING-FORM-P is true, the third return
1657 ;;; value is a list of VARs that should not apply to the lexenv of the
1658 ;;; initialization forms for the bindings, but should apply to the body.
1660 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1661 ;;; of LOCALLY.
1662 (defun process-decls (decls vars fvars &key
1663 (lexenv *lexenv*) (binding-form-p nil) (context :compile)
1664 (allow-lambda-list nil))
1665 (declare (list decls vars fvars))
1666 (let ((result-type *wild-type*)
1667 (allow-values-decl allow-lambda-list)
1668 (explicit-check)
1669 (allow-explicit-check allow-lambda-list)
1670 (lambda-list (if allow-lambda-list :unspecified nil))
1671 (optimize-qualities)
1672 (*post-binding-variable-lexenv* nil))
1673 (flet ((process-it (spec decl)
1674 (cond ((atom spec)
1675 (compiler-error "malformed declaration specifier ~S in ~S"
1676 spec decl))
1677 ((and (eq allow-lambda-list t)
1678 (typep spec '(cons (eql lambda-list) (cons t null))))
1679 (setq lambda-list (cadr spec) allow-lambda-list nil))
1680 ((and allow-values-decl
1681 (typep spec '(cons (eql values)))
1682 (not *suppress-values-declaration*))
1683 ;; Why do we allow more than one VALUES decl? I don't know.
1684 (setq result-type
1685 (values-type-intersection
1686 result-type
1687 (compiler-values-specifier-type
1688 (let ((types (cdr spec)))
1689 (if (singleton-p types)
1690 (car types)
1691 `(values ,@types)))))))
1692 ((and allow-explicit-check
1693 (typep spec '(cons (eql explicit-check))))
1694 ;; EXPLICIT-CHECK by itself specifies that all argument and
1695 ;; result types are checked by the function body.
1696 ;; Alternatively, a subset of arguments, and/or :RESULT,
1697 ;; can be specified to indicate that only a subset are
1698 ;; checked; in that case, the compiler asserts all others.
1699 (awhen (remove-if (lambda (x)
1700 (or (member x vars
1701 :test #'eq
1702 :key #'lambda-var-%source-name)
1703 (eq x :result)))
1704 (cdr spec))
1705 (compiler-error "explicit-check list ~S must refer to lambda vars"
1706 it))
1707 (setq explicit-check (or (cdr spec) t)
1708 allow-explicit-check nil)) ; at most one of this decl
1710 (multiple-value-bind (new-env new-qualities)
1711 (process-1-decl spec lexenv vars fvars
1712 binding-form-p context)
1713 (setq lexenv new-env
1714 optimize-qualities
1715 (nconc new-qualities optimize-qualities)))))))
1716 (dolist (decl decls)
1717 (dolist (spec (rest decl))
1718 (if (eq context :compile)
1719 (with-current-source-form (spec decl) ; TODO this is a slight change to the previous code. make sure the behavior is identical
1720 (process-it spec decl))
1721 ;; Kludge: EVAL calls this function to deal with LOCALLY.
1722 (process-it spec decl)))))
1723 (warn-repeated-optimize-qualities (lexenv-policy lexenv) optimize-qualities)
1724 (values lexenv result-type *post-binding-variable-lexenv*
1725 lambda-list explicit-check)))
1727 (defun %processing-decls (decls vars fvars ctran lvar binding-form-p fun)
1728 (multiple-value-bind (*lexenv* result-type post-binding-lexenv)
1729 (process-decls decls vars fvars :binding-form-p binding-form-p)
1730 (cond ((eq result-type *wild-type*)
1731 (funcall fun ctran lvar post-binding-lexenv))
1733 (let ((value-ctran (make-ctran))
1734 (value-lvar (make-lvar)))
1735 (multiple-value-prog1
1736 (funcall fun value-ctran value-lvar post-binding-lexenv)
1737 (let ((cast (make-cast value-lvar result-type
1738 (lexenv-policy *lexenv*))))
1739 (link-node-to-previous-ctran cast value-ctran)
1740 (setf (lvar-dest value-lvar) cast)
1741 (use-continuation cast ctran lvar))))))))
1743 (defmacro processing-decls ((decls vars fvars ctran lvar
1744 &optional post-binding-lexenv)
1745 &body forms)
1746 (check-type ctran symbol)
1747 (check-type lvar symbol)
1748 (let ((post-binding-lexenv-p (not (null post-binding-lexenv)))
1749 (post-binding-lexenv (or post-binding-lexenv (sb!xc:gensym "LEXENV"))))
1750 `(%processing-decls ,decls ,vars ,fvars ,ctran ,lvar
1751 ,post-binding-lexenv-p
1752 (lambda (,ctran ,lvar ,post-binding-lexenv)
1753 (declare (ignorable ,post-binding-lexenv))
1754 ,@forms))))
1756 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1757 ;;; declaration. If there is a global variable of that name, then
1758 ;;; check that it isn't a constant and return it. Otherwise, create an
1759 ;;; anonymous GLOBAL-VAR.
1760 (defun specvar-for-binding (name)
1761 (cond ((not (eq (info :variable :where-from name) :assumed))
1762 (let ((found (find-free-var name)))
1763 (when (heap-alien-info-p found)
1764 (compiler-error
1765 "~S is an alien variable and so can't be declared special."
1766 name))
1767 (unless (global-var-p found)
1768 (compiler-error
1769 "~S is a constant and so can't be declared special."
1770 name))
1771 found))
1773 (make-global-var :kind :special
1774 :%source-name name
1775 :where-from :declared))))