1 ;;;; This file contains code which does the translation from Lisp code
2 ;;;; to the first intermediate representation (IR1).
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
15 (declaim (special *compiler-error-bailout*))
17 ;;; *SOURCE-PATHS* is a hashtable from source code forms to the path
18 ;;; taken through the source to reach the form. This provides a way to
19 ;;; keep track of the location of original source forms, even when
20 ;;; macroexpansions and other arbitary permutations of the code
21 ;;; happen. This table is initialized by calling FIND-SOURCE-PATHS on
22 ;;; the original source.
23 (declaim (hash-table *source-paths*))
24 (defvar *source-paths*)
26 ;;; *CURRENT-COMPONENT* is the COMPONENT structure which we link
27 ;;; blocks into as we generate them. This just serves to glue the
28 ;;; emitted blocks together until local call analysis and flow graph
29 ;;; canonicalization figure out what is really going on. We need to
30 ;;; keep track of all the blocks generated so that we can delete them
31 ;;; if they turn out to be unreachable.
33 ;;; FIXME: It's confusing having one variable named *CURRENT-COMPONENT*
34 ;;; and another named *COMPONENT-BEING-COMPILED*. (In CMU CL they
35 ;;; were called *CURRENT-COMPONENT* and *COMPILE-COMPONENT* respectively,
36 ;;; which was also confusing.)
37 (declaim (type (or component null) *current-component*))
38 (defvar *current-component*)
40 ;;; *CURRENT-PATH* is the source path of the form we are currently
41 ;;; translating. See NODE-SOURCE-PATH in the NODE structure.
42 (declaim (list *current-path*))
43 (defvar *current-path*)
45 (defvar *derive-function-types* nil
46 "Should the compiler assume that function types will never change,
47 so that it can use type information inferred from current definitions
48 to optimize code which uses those definitions? Setting this true
49 gives non-ANSI, early-CMU-CL behavior. It can be useful for improving
50 the efficiency of stable code.")
52 (defvar *fun-names-in-this-file* nil)
54 ;;;; namespace management utilities
56 (defun fun-lexically-notinline-p (name)
57 (let ((fun (lexenv-find name funs :test #'equal)))
58 ;; a declaration will trump a proclamation
59 (if (and fun (defined-fun-p fun))
60 (eq (defined-fun-inlinep fun) :notinline)
61 (eq (info :function :inlinep name) :notinline))))
63 ;;; Return a GLOBAL-VAR structure usable for referencing the global
65 (defun find-free-really-fun (name)
66 (unless (info :function :kind name)
67 (setf (info :function :kind name) :function)
68 (setf (info :function :where-from name) :assumed))
70 (let ((where (info :function :where-from name)))
71 (when (and (eq where :assumed)
72 ;; In the ordinary target Lisp, it's silly to report
73 ;; undefinedness when the function is defined in the
74 ;; running Lisp. But at cross-compile time, the current
75 ;; definedness of a function is irrelevant to the
76 ;; definedness at runtime, which is what matters.
77 #-sb-xc-host (not (fboundp name)))
78 (note-undefined-reference name :function))
80 :kind :global-function
82 :type (if (or *derive-function-types*
84 (and (member name *fun-names-in-this-file* :test #'equal)
85 (not (fun-lexically-notinline-p name))))
86 (info :function :type name)
87 (specifier-type 'function))
90 ;;; Has the *FREE-FUNS* entry FREE-FUN become invalid?
92 ;;; In CMU CL, the answer was implicitly always true, so this
93 ;;; predicate didn't exist.
95 ;;; This predicate was added to fix bug 138 in SBCL. In some obscure
96 ;;; circumstances, it was possible for a *FREE-FUNS* entry to contain a
97 ;;; DEFINED-FUN whose DEFINED-FUN-FUNCTIONAL object contained IR1
98 ;;; stuff (NODEs, BLOCKs...) referring to an already compiled (aka
99 ;;; "dead") component. When this IR1 stuff was reused in a new
100 ;;; component, under further obscure circumstances it could be used by
101 ;;; WITH-IR1-ENVIRONMENT-FROM-NODE to generate a binding for
102 ;;; *CURRENT-COMPONENT*. At that point things got all confused, since
103 ;;; IR1 conversion was sending code to a component which had already
104 ;;; been compiled and would never be compiled again.
105 (defun invalid-free-fun-p (free-fun)
106 ;; There might be other reasons that *FREE-FUN* entries could
107 ;; become invalid, but the only one we've been bitten by so far
108 ;; (sbcl-0.pre7.118) is this one:
109 (and (defined-fun-p free-fun)
110 (let ((functional (defined-fun-functional free-fun)))
112 (eql (functional-kind functional) :deleted))
113 (and (lambda-p functional)
115 ;; (The main reason for this first test is to bail
116 ;; out early in cases where the LAMBDA-COMPONENT
117 ;; call in the second test would fail because links
118 ;; it needs are uninitialized or invalid.)
120 ;; If the BIND node for this LAMBDA is null, then
121 ;; according to the slot comments, the LAMBDA has
122 ;; been deleted or its call has been deleted. In
123 ;; that case, it seems rather questionable to reuse
124 ;; it, and certainly it shouldn't be necessary to
125 ;; reuse it, so we cheerfully declare it invalid.
126 (null (lambda-bind functional))
127 ;; If this IR1 stuff belongs to a dead component,
128 ;; then we can't reuse it without getting into
129 ;; bizarre confusion.
130 (eql (component-info (lambda-component functional))
133 ;;; If NAME already has a valid entry in *FREE-FUNS*, then return
134 ;;; the value. Otherwise, make a new GLOBAL-VAR using information from
135 ;;; the global environment and enter it in *FREE-FUNS*. If NAME
136 ;;; names a macro or special form, then we error out using the
137 ;;; supplied context which indicates what we were trying to do that
138 ;;; demanded a function.
139 (declaim (ftype (sfunction (t string) global-var) find-free-fun))
140 (defun find-free-fun (name context)
141 (or (let ((old-free-fun (gethash name *free-funs*)))
142 (and (not (invalid-free-fun-p old-free-fun))
144 (ecase (info :function :kind name)
145 ;; FIXME: The :MACRO and :SPECIAL-FORM cases could be merged.
147 (compiler-error "The macro name ~S was found ~A." name context))
149 (compiler-error "The special form name ~S was found ~A."
153 (check-fun-name name)
154 (note-if-setf-fun-and-macro name)
155 (let ((expansion (fun-name-inline-expansion name))
156 (inlinep (info :function :inlinep name)))
157 (setf (gethash name *free-funs*)
158 (if (or expansion inlinep)
161 :inline-expansion expansion
163 :where-from (info :function :where-from name)
164 :type (if (eq inlinep :notinline)
165 (specifier-type 'function)
166 (info :function :type name)))
167 (find-free-really-fun name))))))))
169 ;;; Return the LEAF structure for the lexically apparent function
170 ;;; definition of NAME.
171 (declaim (ftype (sfunction (t string) leaf) find-lexically-apparent-fun))
172 (defun find-lexically-apparent-fun (name context)
173 (let ((var (lexenv-find name funs :test #'equal)))
176 (aver (and (consp var) (eq (car var) 'macro)))
177 (compiler-error "found macro name ~S ~A" name context))
180 (find-free-fun name context)))))
182 ;;; Return the LEAF node for a global variable reference to NAME. If
183 ;;; NAME is already entered in *FREE-VARS*, then we just return the
184 ;;; corresponding value. Otherwise, we make a new leaf using
185 ;;; information from the global environment and enter it in
186 ;;; *FREE-VARS*. If the variable is unknown, then we emit a warning.
187 (declaim (ftype (sfunction (t) (or leaf cons heap-alien-info)) find-free-var))
188 (defun find-free-var (name)
189 (unless (symbolp name)
190 (compiler-error "Variable name is not a symbol: ~S." name))
191 (or (gethash name *free-vars*)
192 (let ((kind (info :variable :kind name))
193 (type (info :variable :type name))
194 (where-from (info :variable :where-from name)))
195 (when (and (eq where-from :assumed) (eq kind :global))
196 (note-undefined-reference name :variable))
197 (setf (gethash name *free-vars*)
200 (info :variable :alien-info name))
201 ;; FIXME: The return value in this case should really be
202 ;; of type SB!C::LEAF. I don't feel too badly about it,
203 ;; because the MACRO idiom is scattered throughout this
204 ;; file, but it should be cleaned up so we're not
205 ;; throwing random conses around. --njf 2002-03-23
207 (let ((expansion (info :variable :macro-expansion name))
208 (type (type-specifier (info :variable :type name))))
209 `(MACRO . (the ,type ,expansion))))
211 (let ((value (info :variable :constant-value name)))
212 (make-constant :value value
214 :type (ctype-of value)
215 :where-from where-from)))
217 (make-global-var :kind kind
220 :where-from where-from)))))))
222 ;;; Grovel over CONSTANT checking for any sub-parts that need to be
223 ;;; processed with MAKE-LOAD-FORM. We have to be careful, because
224 ;;; CONSTANT might be circular. We also check that the constant (and
225 ;;; any subparts) are dumpable at all.
226 (eval-when (:compile-toplevel :load-toplevel :execute)
227 ;; The EVAL-WHEN is necessary for #.(1+ LIST-TO-HASH-TABLE-THRESHOLD)
228 ;; below. -- AL 20010227
229 (def!constant list-to-hash-table-threshold 32))
230 (defun maybe-emit-make-load-forms (constant)
231 (let ((things-processed nil)
233 ;; FIXME: Does this LIST-or-HASH-TABLE messiness give much benefit?
234 (declare (type (or list hash-table) things-processed)
235 (type (integer 0 #.(1+ list-to-hash-table-threshold)) count)
237 (labels ((grovel (value)
238 ;; Unless VALUE is an object which which obviously
239 ;; can't contain other objects
241 '(or #-sb-xc-host unboxed-array
242 #+sb-xc-host (simple-array (unsigned-byte 8) (*))
247 (etypecase things-processed
249 (when (member value things-processed :test #'eq)
250 (return-from grovel nil))
251 (push value things-processed)
253 (when (> count list-to-hash-table-threshold)
254 (let ((things things-processed))
255 (setf things-processed
256 (make-hash-table :test 'eq))
257 (dolist (thing things)
258 (setf (gethash thing things-processed) t)))))
260 (when (gethash value things-processed)
261 (return-from grovel nil))
262 (setf (gethash value things-processed) t)))
266 (grovel (cdr value)))
268 (dotimes (i (length value))
269 (grovel (svref value i))))
271 (dotimes (i (length value))
272 (grovel (aref value i))))
274 ;; Even though the (ARRAY T) branch does the exact
275 ;; same thing as this branch we do this separately
276 ;; so that the compiler can use faster versions of
277 ;; array-total-size and row-major-aref.
278 (dotimes (i (array-total-size value))
279 (grovel (row-major-aref value i))))
281 (dotimes (i (array-total-size value))
282 (grovel (row-major-aref value i))))
283 (;; In the target SBCL, we can dump any instance,
284 ;; but in the cross-compilation host,
285 ;; %INSTANCE-FOO functions don't work on general
286 ;; instances, only on STRUCTURE!OBJECTs.
287 #+sb-xc-host structure!object
288 #-sb-xc-host instance
289 (when (emit-make-load-form value)
290 (dotimes (i (%instance-length value))
291 (grovel (%instance-ref value i)))))
294 "Objects of type ~S can't be dumped into fasl files."
295 (type-of value)))))))
299 ;;;; some flow-graph hacking utilities
301 ;;; This function sets up the back link between the node and the
302 ;;; ctran which continues at it.
303 (defun link-node-to-previous-ctran (node ctran)
304 (declare (type node node) (type ctran ctran))
305 (aver (not (ctran-next ctran)))
306 (setf (ctran-next ctran) node)
307 (setf (node-prev node) ctran))
309 ;;; This function is used to set the ctran for a node, and thus
310 ;;; determine what is evaluated next. If the ctran has no block, then
311 ;;; we make it be in the block that the node is in. If the ctran heads
312 ;;; its block, we end our block and link it to that block.
313 #!-sb-fluid (declaim (inline use-ctran))
314 (defun use-ctran (node ctran)
315 (declare (type node node) (type ctran ctran))
316 (if (eq (ctran-kind ctran) :unused)
317 (let ((node-block (ctran-block (node-prev node))))
318 (setf (ctran-block ctran) node-block)
319 (setf (ctran-kind ctran) :inside-block)
320 (setf (ctran-use ctran) node)
321 (setf (node-next node) ctran))
322 (%use-ctran node ctran)))
323 (defun %use-ctran (node ctran)
324 (declare (type node node) (type ctran ctran) (inline member))
325 (let ((block (ctran-block ctran))
326 (node-block (ctran-block (node-prev node))))
327 (aver (eq (ctran-kind ctran) :block-start))
328 (when (block-last node-block)
329 (error "~S has already ended." node-block))
330 (setf (block-last node-block) node)
331 (when (block-succ node-block)
332 (error "~S already has successors." node-block))
333 (setf (block-succ node-block) (list block))
334 (when (memq node-block (block-pred block))
335 (error "~S is already a predecessor of ~S." node-block block))
336 (push node-block (block-pred block))))
338 ;;; This function is used to set the ctran for a node, and thus
339 ;;; determine what receives the value.
340 (defun use-lvar (node lvar)
341 (declare (type valued-node node) (type (or lvar null) lvar))
342 (aver (not (node-lvar node)))
344 (setf (node-lvar node) lvar)
345 (cond ((null (lvar-uses lvar))
346 (setf (lvar-uses lvar) node))
347 ((listp (lvar-uses lvar))
348 (aver (not (memq node (lvar-uses lvar))))
349 (push node (lvar-uses lvar)))
351 (aver (neq node (lvar-uses lvar)))
352 (setf (lvar-uses lvar) (list node (lvar-uses lvar)))))
353 (reoptimize-lvar lvar)))
355 #!-sb-fluid(declaim (inline use-continuation))
356 (defun use-continuation (node ctran lvar)
357 (use-ctran node ctran)
358 (use-lvar node lvar))
360 ;;;; exported functions
362 ;;; This function takes a form and the top level form number for that
363 ;;; form, and returns a lambda representing the translation of that
364 ;;; form in the current global environment. The returned lambda is a
365 ;;; top level lambda that can be called to cause evaluation of the
366 ;;; forms. This lambda is in the initial component. If FOR-VALUE is T,
367 ;;; then the value of the form is returned from the function,
368 ;;; otherwise NIL is returned.
370 ;;; This function may have arbitrary effects on the global environment
371 ;;; due to processing of EVAL-WHENs. All syntax error checking is
372 ;;; done, with erroneous forms being replaced by a proxy which signals
373 ;;; an error if it is evaluated. Warnings about possibly inconsistent
374 ;;; or illegal changes to the global environment will also be given.
376 ;;; We make the initial component and convert the form in a PROGN (and
377 ;;; an optional NIL tacked on the end.) We then return the lambda. We
378 ;;; bind all of our state variables here, rather than relying on the
379 ;;; global value (if any) so that IR1 conversion will be reentrant.
380 ;;; This is necessary for EVAL-WHEN processing, etc.
382 ;;; The hashtables used to hold global namespace info must be
383 ;;; reallocated elsewhere. Note also that *LEXENV* is not bound, so
384 ;;; that local macro definitions can be introduced by enclosing code.
385 (defun ir1-toplevel (form path for-value)
386 (declare (list path))
387 (let* ((*current-path* path)
388 (component (make-empty-component))
389 (*current-component* component)
390 (*allow-instrumenting* t))
391 (setf (component-name component) "initial component")
392 (setf (component-kind component) :initial)
393 (let* ((forms (if for-value `(,form) `(,form nil)))
394 (res (ir1-convert-lambda-body
396 :debug-name (debug-namify "top level form " form))))
397 (setf (functional-entry-fun res) res
398 (functional-arg-documentation res) ()
399 (functional-kind res) :toplevel)
402 ;;; *CURRENT-FORM-NUMBER* is used in FIND-SOURCE-PATHS to compute the
403 ;;; form number to associate with a source path. This should be bound
404 ;;; to an initial value of 0 before the processing of each truly
406 (declaim (type index *current-form-number*))
407 (defvar *current-form-number*)
409 ;;; This function is called on freshly read forms to record the
410 ;;; initial location of each form (and subform.) Form is the form to
411 ;;; find the paths in, and TLF-NUM is the top level form number of the
412 ;;; truly top level form.
414 ;;; This gets a bit interesting when the source code is circular. This
415 ;;; can (reasonably?) happen in the case of circular list constants.
416 (defun find-source-paths (form tlf-num)
417 (declare (type index tlf-num))
418 (let ((*current-form-number* 0))
419 (sub-find-source-paths form (list tlf-num)))
421 (defun sub-find-source-paths (form path)
422 (unless (gethash form *source-paths*)
423 (setf (gethash form *source-paths*)
424 (list* 'original-source-start *current-form-number* path))
425 (incf *current-form-number*)
429 (declare (fixnum pos))
432 (when (atom subform) (return))
433 (let ((fm (car subform)))
435 (sub-find-source-paths fm (cons pos path)))
437 (setq subform (cdr subform))
438 (when (eq subform trail) (return)))))
442 (setq trail (cdr trail)))))))
444 ;;;; IR1-CONVERT, macroexpansion and special form dispatching
446 (declaim (ftype (sfunction (ctran ctran (or lvar null) t) (values))
448 (macrolet (;; Bind *COMPILER-ERROR-BAILOUT* to a function that throws
449 ;; out of the body and converts a condition signalling form
450 ;; instead. The source form is converted to a string since it
451 ;; may contain arbitrary non-externalizable objects.
452 (ir1-error-bailout ((start next result form) &body body)
453 (with-unique-names (skip condition)
455 (let ((,condition (catch 'ir1-error-abort
456 (let ((*compiler-error-bailout*
457 (lambda (&optional e)
458 (throw 'ir1-error-abort e))))
460 (return-from ,skip nil)))))
461 (ir1-convert ,start ,next ,result
462 (make-compiler-error-form ,condition ,form)))))))
464 ;; Translate FORM into IR1. The code is inserted as the NEXT of the
465 ;; CTRAN START. RESULT is the LVAR which receives the value of the
466 ;; FORM to be translated. The translators call this function
467 ;; recursively to translate their subnodes.
469 ;; As a special hack to make life easier in the compiler, a LEAF
470 ;; IR1-converts into a reference to that LEAF structure. This allows
471 ;; the creation using backquote of forms that contain leaf
472 ;; references, without having to introduce dummy names into the
474 (defun ir1-convert (start next result form)
475 (ir1-error-bailout (start next result form)
476 (let ((*current-path* (or (gethash form *source-paths*)
477 (cons form *current-path*))))
478 (cond ((step-form-p form)
479 (ir1-convert-step start next result form))
481 (cond ((and (symbolp form) (not (keywordp form)))
482 (ir1-convert-var start next result form))
484 (reference-leaf start next result form))
486 (reference-constant start next result form))))
488 (let ((opname (car form)))
489 (cond ((or (symbolp opname) (leaf-p opname))
490 (let ((lexical-def (if (leaf-p opname)
492 (lexenv-find opname funs))))
493 (typecase lexical-def
495 (ir1-convert-global-functoid start next result
498 (ir1-convert-local-combination start next result
502 (ir1-convert-srctran start next result
505 (aver (and (consp lexical-def)
506 (eq (car lexical-def) 'macro)))
507 (ir1-convert start next result
508 (careful-expand-macro (cdr lexical-def)
510 ((or (atom opname) (not (eq (car opname) 'lambda)))
511 (compiler-error "illegal function call"))
513 ;; implicitly (LAMBDA ..) because the LAMBDA
514 ;; expression is the CAR of an executed form
515 (ir1-convert-combination start next result
519 :debug-name (debug-namify
524 ;; Generate a reference to a manifest constant, creating a new leaf
525 ;; if necessary. If we are producing a fasl file, make sure that
526 ;; MAKE-LOAD-FORM gets used on any parts of the constant that it
528 (defun reference-constant (start next result value)
529 (declare (type ctran start next)
530 (type (or lvar null) result)
531 (inline find-constant))
532 (ir1-error-bailout (start next result value)
533 (when (producing-fasl-file)
534 (maybe-emit-make-load-forms value))
535 (let* ((leaf (find-constant value))
536 (res (make-ref leaf)))
537 (push res (leaf-refs leaf))
538 (link-node-to-previous-ctran res start)
539 (use-continuation res next result)))
542 ;;; Add FUNCTIONAL to the COMPONENT-REANALYZE-FUNCTIONALS, unless it's
543 ;;; some trivial type for which reanalysis is a trivial no-op, or
544 ;;; unless it doesn't belong in this component at all.
546 ;;; FUNCTIONAL is returned.
547 (defun maybe-reanalyze-functional (functional)
549 (aver (not (eql (functional-kind functional) :deleted))) ; bug 148
550 (aver-live-component *current-component*)
552 ;; When FUNCTIONAL is of a type for which reanalysis isn't a trivial
554 (when (typep functional '(or optional-dispatch clambda))
556 ;; When FUNCTIONAL knows its component
557 (when (lambda-p functional)
558 (aver (eql (lambda-component functional) *current-component*)))
561 (component-reanalyze-functionals *current-component*)))
565 ;;; Generate a REF node for LEAF, frobbing the LEAF structure as
566 ;;; needed. If LEAF represents a defined function which has already
567 ;;; been converted, and is not :NOTINLINE, then reference the
568 ;;; functional instead.
569 (defun reference-leaf (start next result leaf)
570 (declare (type ctran start next) (type (or lvar null) result) (type leaf leaf))
571 (when (functional-p leaf)
572 (assure-functional-live-p leaf))
573 (let* ((type (lexenv-find leaf type-restrictions))
574 (leaf (or (and (defined-fun-p leaf)
575 (not (eq (defined-fun-inlinep leaf)
577 (let ((functional (defined-fun-functional leaf)))
578 (when (and functional
579 (not (functional-kind functional))
580 ;; Bug MISC.320: ir1-transform
581 ;; can create a reference to a
582 ;; inline-expanded function,
583 ;; defined in another component.
584 (not (and (lambda-p functional)
585 (neq (lambda-component functional)
586 *current-component*))))
587 (maybe-reanalyze-functional functional))))
588 (when (and (lambda-p leaf)
589 (memq (functional-kind leaf)
591 (maybe-reanalyze-functional leaf))
593 (ref (make-ref leaf)))
594 (push ref (leaf-refs leaf))
595 (setf (leaf-ever-used leaf) t)
596 (link-node-to-previous-ctran ref start)
597 (cond (type (let* ((ref-ctran (make-ctran))
598 (ref-lvar (make-lvar))
599 (cast (make-cast ref-lvar
600 (make-single-value-type type)
601 (lexenv-policy *lexenv*))))
602 (setf (lvar-dest ref-lvar) cast)
603 (use-continuation ref ref-ctran ref-lvar)
604 (link-node-to-previous-ctran cast ref-ctran)
605 (use-continuation cast next result)))
606 (t (use-continuation ref next result)))))
608 ;;; Convert a reference to a symbolic constant or variable. If the
609 ;;; symbol is entered in the LEXENV-VARS we use that definition,
610 ;;; otherwise we find the current global definition. This is also
611 ;;; where we pick off symbol macro and alien variable references.
612 (defun ir1-convert-var (start next result name)
613 (declare (type ctran start next) (type (or lvar null) result) (symbol name))
614 (let ((var (or (lexenv-find name vars) (find-free-var name))))
617 (when (lambda-var-p var)
618 (let ((home (ctran-home-lambda-or-null start)))
620 (pushnew var (lambda-calls-or-closes home))))
621 (when (lambda-var-ignorep var)
622 ;; (ANSI's specification for the IGNORE declaration requires
623 ;; that this be a STYLE-WARNING, not a full WARNING.)
625 (compiler-style-warn "reading an ignored variable: ~S" name)
626 ;; there's no need for us to accept ANSI's lameness when
627 ;; processing our own code, though.
629 (warn "reading an ignored variable: ~S" name)))
630 (reference-leaf start next result var))
632 (aver (eq (car var) 'MACRO))
633 ;; FIXME: [Free] type declarations. -- APD, 2002-01-26
634 (ir1-convert start next result (cdr var)))
636 (ir1-convert start next result `(%heap-alien ',var)))))
639 ;;; Convert anything that looks like a special form, global function
640 ;;; or compiler-macro call.
641 (defun ir1-convert-global-functoid (start next result form)
642 (declare (type ctran start next) (type (or lvar null) result) (list form))
643 (let* ((fun-name (first form))
644 (translator (info :function :ir1-convert fun-name))
645 (cmacro-fun (sb!xc:compiler-macro-function fun-name *lexenv*)))
648 (compiler-warn "ignoring compiler macro for special form"))
649 (funcall translator start next result form))
651 ;; gotcha: If you look up the DEFINE-COMPILER-MACRO
652 ;; macro in the ANSI spec, you might think that
653 ;; suppressing compiler-macro expansion when NOTINLINE
654 ;; is some pre-ANSI hack. However, if you look up the
655 ;; NOTINLINE declaration, you'll find that ANSI
656 ;; requires this behavior after all.
657 (not (eq (info :function :inlinep fun-name) :notinline)))
658 (let ((res (careful-expand-macro cmacro-fun form)))
660 (ir1-convert-global-functoid-no-cmacro
661 start next result form fun-name)
662 (ir1-convert start next result res))))
664 (ir1-convert-global-functoid-no-cmacro start next result
667 ;;; Handle the case of where the call was not a compiler macro, or was
668 ;;; a compiler macro and passed.
669 (defun ir1-convert-global-functoid-no-cmacro (start next result form fun)
670 (declare (type ctran start next) (type (or lvar null) result)
672 ;; FIXME: Couldn't all the INFO calls here be converted into
673 ;; standard CL functions, like MACRO-FUNCTION or something?
674 ;; And what happens with lexically-defined (MACROLET) macros
676 (ecase (info :function :kind fun)
678 (ir1-convert start next result
679 (careful-expand-macro (info :function :macro-function fun)
682 (ir1-convert-srctran start next result
683 (find-free-fun fun "shouldn't happen! (no-cmacro)")
686 (defun muffle-warning-or-die ()
688 (bug "no MUFFLE-WARNING restart"))
690 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
691 ;;; errors which occur during the macroexpansion.
692 (defun careful-expand-macro (fun form)
693 (let (;; a hint I (WHN) wish I'd known earlier
694 (hint "(hint: For more precise location, try *BREAK-ON-SIGNALS*.)"))
695 (flet (;; Return a string to use as a prefix in error reporting,
696 ;; telling something about which form caused the problem.
698 (let ((*print-pretty* nil)
699 ;; We rely on the printer to abbreviate FORM.
704 #-sb-xc-host "(in macroexpansion of ~S)"
705 ;; longer message to avoid ambiguity "Was it the xc host
706 ;; or the cross-compiler which encountered the problem?"
707 #+sb-xc-host "(in cross-compiler macroexpansion of ~S)"
709 (handler-bind ((style-warning (lambda (c)
711 "~@<~A~:@_~A~@:_~A~:>"
712 (wherestring) hint c)
713 (muffle-warning-or-die)))
714 ;; KLUDGE: CMU CL in its wisdom (version 2.4.6 for
715 ;; Debian Linux, anyway) raises a CL:WARNING
716 ;; condition (not a CL:STYLE-WARNING) for undefined
717 ;; symbols when converting interpreted functions,
718 ;; causing COMPILE-FILE to think the file has a real
719 ;; problem, causing COMPILE-FILE to return FAILURE-P
720 ;; set (not just WARNINGS-P set). Since undefined
721 ;; symbol warnings are often harmless forward
722 ;; references, and since it'd be inordinately painful
723 ;; to try to eliminate all such forward references,
724 ;; these warnings are basically unavoidable. Thus, we
725 ;; need to coerce the system to work through them,
726 ;; and this code does so, by crudely suppressing all
727 ;; warnings in cross-compilation macroexpansion. --
729 #+(and cmu sb-xc-host)
734 ~@<(KLUDGE: That was a non-STYLE WARNING. ~
735 Ordinarily that would cause compilation to ~
736 fail. However, since we're running under ~
737 CMU CL, and since CMU CL emits non-STYLE ~
738 warnings for safe, hard-to-fix things (e.g. ~
739 references to not-yet-defined functions) ~
740 we're going to have to ignore it and ~
741 proceed anyway. Hopefully we're not ~
742 ignoring anything horrible here..)~:@>~:>"
745 (muffle-warning-or-die)))
746 #-(and cmu sb-xc-host)
748 (warn "~@<~A~:@_~A~@:_~A~:>"
749 (wherestring) hint c)
750 (muffle-warning-or-die)))
752 (compiler-error "~@<~A~:@_~A~@:_~A~:>"
753 (wherestring) hint c))))
754 (funcall sb!xc:*macroexpand-hook* fun form *lexenv*)))))
756 ;;;; conversion utilities
758 ;;; Convert a bunch of forms, discarding all the values except the
759 ;;; last. If there aren't any forms, then translate a NIL.
760 (declaim (ftype (sfunction (ctran ctran (or lvar null) list) (values))
761 ir1-convert-progn-body))
762 (defun ir1-convert-progn-body (start next result body)
764 (reference-constant start next result nil)
765 (let ((this-start start)
768 (let ((form (car forms)))
769 (when (endp (cdr forms))
770 (ir1-convert this-start next result form)
772 (let ((this-ctran (make-ctran)))
773 (ir1-convert this-start this-ctran nil form)
774 (setq this-start this-ctran
775 forms (cdr forms)))))))
778 ;;;; converting combinations
780 ;;; Convert a function call where the function FUN is a LEAF. FORM is
781 ;;; the source for the call. We return the COMBINATION node so that
782 ;;; the caller can poke at it if it wants to.
783 (declaim (ftype (sfunction (ctran ctran (or lvar null) list leaf) combination)
784 ir1-convert-combination))
785 (defun ir1-convert-combination (start next result form fun)
786 (let ((ctran (make-ctran))
787 (fun-lvar (make-lvar)))
788 (ir1-convert start ctran fun-lvar `(the (or function symbol) ,fun))
789 (ir1-convert-combination-args fun-lvar ctran next result (cdr form))))
791 ;;; Convert the arguments to a call and make the COMBINATION
792 ;;; node. FUN-LVAR yields the function to call. ARGS is the list of
793 ;;; arguments for the call, which defaults to the cdr of source. We
794 ;;; return the COMBINATION node.
795 (defun ir1-convert-combination-args (fun-lvar start next result args)
796 (declare (type ctran start next)
798 (type (or lvar null) result)
800 (let ((node (make-combination fun-lvar)))
801 (setf (lvar-dest fun-lvar) node)
802 (collect ((arg-lvars))
803 (let ((this-start start))
805 (let ((this-ctran (make-ctran))
806 (this-lvar (make-lvar node)))
807 (ir1-convert this-start this-ctran this-lvar arg)
808 (setq this-start this-ctran)
809 (arg-lvars this-lvar)))
810 (link-node-to-previous-ctran node this-start)
811 (use-continuation node next result)
812 (setf (combination-args node) (arg-lvars))))
815 ;;; Convert a call to a global function. If not :NOTINLINE, then we do
816 ;;; source transforms and try out any inline expansion. If there is no
817 ;;; expansion, but is :INLINE, then give an efficiency note (unless a
818 ;;; known function which will quite possibly be open-coded.) Next, we
819 ;;; go to ok-combination conversion.
820 (defun ir1-convert-srctran (start next result var form)
821 (declare (type ctran start next) (type (or lvar null) result)
822 (type global-var var))
823 (let ((inlinep (when (defined-fun-p var)
824 (defined-fun-inlinep var))))
825 (if (eq inlinep :notinline)
826 (ir1-convert-combination start next result form var)
827 (let ((transform (info :function
829 (leaf-source-name var))))
831 (multiple-value-bind (transformed pass) (funcall transform form)
833 (ir1-convert-maybe-predicate start next result form var)
834 (ir1-convert start next result transformed)))
835 (ir1-convert-maybe-predicate start next result form var))))))
837 ;;; If the function has the PREDICATE attribute, and the RESULT's DEST
838 ;;; isn't an IF, then we convert (IF <form> T NIL), ensuring that a
839 ;;; predicate always appears in a conditional context.
841 ;;; If the function isn't a predicate, then we call
842 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
843 (defun ir1-convert-maybe-predicate (start next result form var)
844 (declare (type ctran start next)
845 (type (or lvar null) result)
847 (type global-var var))
848 (let ((info (info :function :info (leaf-source-name var))))
850 (ir1-attributep (fun-info-attributes info) predicate)
851 (not (if-p (and result (lvar-dest result)))))
852 (ir1-convert start next result `(if ,form t nil))
853 (ir1-convert-combination-checking-type start next result form var))))
855 ;;; Actually really convert a global function call that we are allowed
858 ;;; If we know the function type of the function, then we check the
859 ;;; call for syntactic legality with respect to the declared function
860 ;;; type. If it is impossible to determine whether the call is correct
861 ;;; due to non-constant keywords, then we give up, marking the call as
862 ;;; :FULL to inhibit further error messages. We return true when the
865 ;;; If the call is legal, we also propagate type assertions from the
866 ;;; function type to the arg and result lvars. We do this now so that
867 ;;; IR1 optimize doesn't have to redundantly do the check later so
868 ;;; that it can do the type propagation.
869 (defun ir1-convert-combination-checking-type (start next result form var)
870 (declare (type ctran start next) (type (or lvar null) result)
873 (let* ((node (ir1-convert-combination start next result form var))
874 (fun-lvar (basic-combination-fun node))
875 (type (leaf-type var)))
876 (when (validate-call-type node type t)
877 (setf (lvar-%derived-type fun-lvar)
878 (make-single-value-type type))
879 (setf (lvar-reoptimize fun-lvar) nil)))
882 ;;; Convert a call to a local function, or if the function has already
883 ;;; been LET converted, then throw FUNCTIONAL to
884 ;;; LOCALL-ALREADY-LET-CONVERTED. The THROW should only happen when we
885 ;;; are converting inline expansions for local functions during
887 (defun ir1-convert-local-combination (start next result form functional)
888 (assure-functional-live-p functional)
889 (ir1-convert-combination start next result
891 (maybe-reanalyze-functional functional)))
895 ;;; Given a list of LAMBDA-VARs and a variable name, return the
896 ;;; LAMBDA-VAR for that name, or NIL if it isn't found. We return the
897 ;;; *last* variable with that name, since LET* bindings may be
898 ;;; duplicated, and declarations always apply to the last.
899 (declaim (ftype (sfunction (list symbol) (or lambda-var list))
901 (defun find-in-bindings (vars name)
905 (when (eq (leaf-source-name var) name)
907 (let ((info (lambda-var-arg-info var)))
909 (let ((supplied-p (arg-info-supplied-p info)))
910 (when (and supplied-p
911 (eq (leaf-source-name supplied-p) name))
912 (setq found supplied-p))))))
913 ((and (consp var) (eq (car var) name))
914 (setf found (cdr var)))))
917 ;;; Called by PROCESS-DECLS to deal with a variable type declaration.
918 ;;; If a LAMBDA-VAR being bound, we intersect the type with the var's
919 ;;; type, otherwise we add a type restriction on the var. If a symbol
920 ;;; macro, we just wrap a THE around the expansion.
921 (defun process-type-decl (decl res vars)
922 (declare (list decl vars) (type lexenv res))
923 (let ((type (compiler-specifier-type (first decl))))
924 (collect ((restr nil cons)
926 (dolist (var-name (rest decl))
927 (when (boundp var-name)
928 (compiler-assert-symbol-home-package-unlocked
929 var-name "declaring the type of ~A"))
930 (let* ((bound-var (find-in-bindings vars var-name))
932 (lexenv-find var-name vars)
933 (find-free-var var-name))))
937 ((process-var (var bound-var)
938 (let* ((old-type (or (lexenv-find var type-restrictions)
940 (int (if (or (fun-type-p type)
941 (fun-type-p old-type))
943 (type-approx-intersection2
945 (cond ((eq int *empty-type*)
946 (unless (policy *lexenv* (= inhibit-warnings 3))
950 "The type declarations ~S and ~S for ~S conflict."
953 (type-specifier old-type)
954 (type-specifier type)
956 (bound-var (setf (leaf-type bound-var) int))
958 (restr (cons var int)))))))
959 (process-var var bound-var)
960 (awhen (and (lambda-var-p var)
961 (lambda-var-specvar var))
962 (process-var it nil))))
964 ;; FIXME: non-ANSI weirdness
965 (aver (eq (car var) 'MACRO))
966 (new-vars `(,var-name . (MACRO . (the ,(first decl)
970 "~S is an alien variable, so its type can't be declared."
973 (if (or (restr) (new-vars))
974 (make-lexenv :default res
975 :type-restrictions (restr)
979 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
980 ;;; declarations for function variables. In addition to allowing
981 ;;; declarations for functions being bound, we must also deal with
982 ;;; declarations that constrain the type of lexically apparent
984 (defun process-ftype-decl (spec res names fvars)
985 (declare (type list names fvars)
987 (let ((type (compiler-specifier-type spec)))
988 (collect ((res nil cons))
991 (compiler-assert-symbol-home-package-unlocked name
992 "declaring the ftype of ~A"))
993 (let ((found (find name fvars
994 :key #'leaf-source-name
998 (setf (leaf-type found) type)
999 (assert-definition-type found type
1000 :unwinnage-fun #'compiler-notify
1001 :where "FTYPE declaration"))
1003 (res (cons (find-lexically-apparent-fun
1004 name "in a function type declaration")
1007 (make-lexenv :default res :type-restrictions (res))
1010 ;;; Process a special declaration, returning a new LEXENV. A non-bound
1011 ;;; special declaration is instantiated by throwing a special variable
1012 ;;; into the variables.
1013 (defun process-special-decl (spec res vars)
1014 (declare (list spec vars) (type lexenv res))
1015 (collect ((new-venv nil cons))
1016 (dolist (name (cdr spec))
1017 (compiler-assert-symbol-home-package-unlocked name "declaring ~A special")
1018 (let ((var (find-in-bindings vars name)))
1021 (aver (eq (car var) 'MACRO))
1023 "~S is a symbol-macro and thus can't be declared special."
1026 (when (lambda-var-ignorep var)
1027 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1028 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1029 (compiler-style-warn
1030 "The ignored variable ~S is being declared special."
1032 (setf (lambda-var-specvar var)
1033 (specvar-for-binding name)))
1035 (unless (assoc name (new-venv) :test #'eq)
1036 (new-venv (cons name (specvar-for-binding name))))))))
1038 (make-lexenv :default res :vars (new-venv))
1041 ;;; Return a DEFINED-FUN which copies a GLOBAL-VAR but for its INLINEP
1042 ;;; (and TYPE if notinline).
1043 (defun make-new-inlinep (var inlinep)
1044 (declare (type global-var var) (type inlinep inlinep))
1045 (let ((res (make-defined-fun
1046 :%source-name (leaf-source-name var)
1047 :where-from (leaf-where-from var)
1048 :type (if (and (eq inlinep :notinline)
1049 (not (eq (leaf-where-from var) :declared)))
1050 (specifier-type 'function)
1053 (when (defined-fun-p var)
1054 (setf (defined-fun-inline-expansion res)
1055 (defined-fun-inline-expansion var))
1056 (setf (defined-fun-functional res)
1057 (defined-fun-functional var)))
1060 ;;; Parse an inline/notinline declaration. If it's a local function we're
1061 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
1062 (defun process-inline-decl (spec res fvars)
1063 (let ((sense (cdr (assoc (first spec) *inlinep-translations* :test #'eq)))
1065 (dolist (name (rest spec))
1066 (let ((fvar (find name fvars
1067 :key #'leaf-source-name
1070 (setf (functional-inlinep fvar) sense)
1072 (find-lexically-apparent-fun
1073 name "in an inline or notinline declaration")))
1076 (when (policy *lexenv* (>= speed inhibit-warnings))
1077 (compiler-notify "ignoring ~A declaration not at ~
1078 definition of local function:~% ~S"
1081 (push (cons name (make-new-inlinep found sense))
1085 (make-lexenv :default res :funs new-fenv)
1088 ;;; like FIND-IN-BINDINGS, but looks for #'FOO in the FVARS
1089 (defun find-in-bindings-or-fbindings (name vars fvars)
1090 (declare (list vars fvars))
1092 (destructuring-bind (wot fn-name) name
1093 (unless (eq wot 'function)
1094 (compiler-error "The function or variable name ~S is unrecognizable."
1096 (find fn-name fvars :key #'leaf-source-name :test #'equal))
1097 (find-in-bindings vars name)))
1099 ;;; Process an ignore/ignorable declaration, checking for various losing
1101 (defun process-ignore-decl (spec vars fvars)
1102 (declare (list spec vars fvars))
1103 (dolist (name (rest spec))
1104 (let ((var (find-in-bindings-or-fbindings name vars fvars)))
1107 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1108 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1109 (compiler-style-warn "declaring unknown variable ~S to be ignored"
1111 ;; FIXME: This special case looks like non-ANSI weirdness.
1112 ((and (consp var) (eq (car var) 'macro))
1113 ;; Just ignore the IGNORE decl.
1116 (setf (leaf-ever-used var) t))
1117 ((and (lambda-var-specvar var) (eq (first spec) 'ignore))
1118 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1119 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1120 (compiler-style-warn "declaring special variable ~S to be ignored"
1122 ((eq (first spec) 'ignorable)
1123 (setf (leaf-ever-used var) t))
1125 (setf (lambda-var-ignorep var) t)))))
1128 (defun process-dx-decl (names vars fvars)
1129 (flet ((maybe-notify (control &rest args)
1130 (when (policy *lexenv* (> speed inhibit-warnings))
1131 (apply #'compiler-notify control args))))
1132 (if (policy *lexenv* (= stack-allocate-dynamic-extent 3))
1133 (dolist (name names)
1136 (let* ((bound-var (find-in-bindings vars name))
1138 (lexenv-find name vars)
1139 (find-free-var name))))
1143 (setf (leaf-dynamic-extent var) t)
1145 "ignoring DYNAMIC-EXTENT declaration for free ~S"
1148 (compiler-error "DYNAMIC-EXTENT on symbol-macro: ~S" name))
1150 (compiler-error "DYNAMIC-EXTENT on heap-alien-info: ~S"
1153 (eq (car name) 'function)
1155 (valid-function-name-p (cadr name)))
1156 (let* ((fname (cadr name))
1157 (bound-fun (find fname fvars
1158 :key #'leaf-source-name
1160 (etypecase bound-fun
1162 #!+stack-allocatable-closures
1163 (setf (leaf-dynamic-extent bound-fun) t)
1164 #!-stack-allocatable-closures
1166 "ignoring DYNAMIC-EXTENT declaration on a function ~S ~
1167 (not supported on this platform)." fname))
1169 (compiler-error "DYNAMIC-EXTENT on macro: ~S" fname))
1172 "ignoring DYNAMIC-EXTENT declaration for free ~S"
1174 (t (compiler-error "DYNAMIC-EXTENT on a weird thing: ~S" name))))
1175 (maybe-notify "ignoring DYNAMIC-EXTENT declarations for ~S" names))))
1177 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
1178 ;;; go away, I think.
1179 (defvar *suppress-values-declaration* nil
1181 "If true, processing of the VALUES declaration is inhibited.")
1183 ;;; Process a single declaration spec, augmenting the specified LEXENV
1184 ;;; RES. Return RES and result type. VARS and FVARS are as described
1186 (defun process-1-decl (raw-spec res vars fvars)
1187 (declare (type list raw-spec vars fvars))
1188 (declare (type lexenv res))
1189 (let ((spec (canonized-decl-spec raw-spec))
1190 (result-type *wild-type*))
1193 (special (process-special-decl spec res vars))
1196 (compiler-error "no type specified in FTYPE declaration: ~S" spec))
1197 (process-ftype-decl (second spec) res (cddr spec) fvars))
1198 ((inline notinline maybe-inline)
1199 (process-inline-decl spec res fvars))
1201 (process-ignore-decl spec vars fvars)
1206 :policy (process-optimize-decl spec (lexenv-policy res))))
1210 :handled-conditions (process-muffle-conditions-decl
1211 spec (lexenv-handled-conditions res))))
1212 (unmuffle-conditions
1215 :handled-conditions (process-unmuffle-conditions-decl
1216 spec (lexenv-handled-conditions res))))
1218 (process-type-decl (cdr spec) res vars))
1220 (unless *suppress-values-declaration*
1221 (let ((types (cdr spec)))
1223 (compiler-values-specifier-type
1224 (if (singleton-p types)
1226 `(values ,@types)))))
1229 (process-dx-decl (cdr spec) vars fvars)
1231 ((disable-package-locks enable-package-locks)
1234 :disabled-package-locks (process-package-lock-decl
1235 spec (lexenv-disabled-package-locks res))))
1237 (unless (info :declaration :recognized (first spec))
1238 (compiler-warn "unrecognized declaration ~S" raw-spec))
1242 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1243 ;;; and FUNCTIONAL structures which are being bound. In addition to
1244 ;;; filling in slots in the leaf structures, we return a new LEXENV,
1245 ;;; which reflects pervasive special and function type declarations,
1246 ;;; (NOT)INLINE declarations and OPTIMIZE declarations, and type of
1247 ;;; VALUES declarations.
1249 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1251 (defun process-decls (decls vars fvars &optional (env *lexenv*))
1252 (declare (list decls vars fvars))
1253 (let ((result-type *wild-type*))
1254 (dolist (decl decls)
1255 (dolist (spec (rest decl))
1256 (unless (consp spec)
1257 (compiler-error "malformed declaration specifier ~S in ~S" spec decl))
1258 (multiple-value-bind (new-env new-result-type)
1259 (process-1-decl spec env vars fvars)
1261 (unless (eq new-result-type *wild-type*)
1263 (values-type-intersection result-type new-result-type))))))
1264 (values env result-type)))
1266 (defun %processing-decls (decls vars fvars ctran lvar fun)
1267 (multiple-value-bind (*lexenv* result-type)
1268 (process-decls decls vars fvars)
1269 (cond ((eq result-type *wild-type*)
1270 (funcall fun ctran lvar))
1272 (let ((value-ctran (make-ctran))
1273 (value-lvar (make-lvar)))
1274 (multiple-value-prog1
1275 (funcall fun value-ctran value-lvar)
1276 (let ((cast (make-cast value-lvar result-type
1277 (lexenv-policy *lexenv*))))
1278 (link-node-to-previous-ctran cast value-ctran)
1279 (setf (lvar-dest value-lvar) cast)
1280 (use-continuation cast ctran lvar))))))))
1281 (defmacro processing-decls ((decls vars fvars ctran lvar) &body forms)
1282 (check-type ctran symbol)
1283 (check-type lvar symbol)
1284 `(%processing-decls ,decls ,vars ,fvars ,ctran ,lvar
1285 (lambda (,ctran ,lvar) ,@forms)))
1287 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1288 ;;; declaration. If there is a global variable of that name, then
1289 ;;; check that it isn't a constant and return it. Otherwise, create an
1290 ;;; anonymous GLOBAL-VAR.
1291 (defun specvar-for-binding (name)
1292 (cond ((not (eq (info :variable :where-from name) :assumed))
1293 (let ((found (find-free-var name)))
1294 (when (heap-alien-info-p found)
1296 "~S is an alien variable and so can't be declared special."
1298 (unless (global-var-p found)
1300 "~S is a constant and so can't be declared special."
1304 (make-global-var :kind :special
1306 :where-from :declared))))