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