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 ;;;; namespace management utilities
54 ;;; Return a GLOBAL-VAR structure usable for referencing the global
56 (defun find-free-really-fun (name)
57 (unless (info :function :kind name)
58 (setf (info :function :kind name) :function)
59 (setf (info :function :where-from name) :assumed))
61 (let ((where (info :function :where-from name)))
62 (when (and (eq where :assumed)
63 ;; In the ordinary target Lisp, it's silly to report
64 ;; undefinedness when the function is defined in the
65 ;; running Lisp. But at cross-compile time, the current
66 ;; definedness of a function is irrelevant to the
67 ;; definedness at runtime, which is what matters.
68 #-sb-xc-host (not (fboundp name)))
69 (note-undefined-reference name :function))
70 (make-global-var :kind :global-function
72 :type (if (or *derive-function-types*
74 (info :function :type name)
75 (specifier-type 'function))
78 ;;; Has the *FREE-FUNS* entry FREE-FUN become invalid?
80 ;;; In CMU CL, the answer was implicitly always true, so this
81 ;;; predicate didn't exist.
83 ;;; This predicate was added to fix bug 138 in SBCL. In some obscure
84 ;;; circumstances, it was possible for a *FREE-FUNS* entry to contain a
85 ;;; DEFINED-FUN whose DEFINED-FUN-FUNCTIONAL object contained IR1
86 ;;; stuff (NODEs, BLOCKs...) referring to an already compiled (aka
87 ;;; "dead") component. When this IR1 stuff was reused in a new
88 ;;; component, under further obscure circumstances it could be used by
89 ;;; WITH-IR1-ENVIRONMENT-FROM-NODE to generate a binding for
90 ;;; *CURRENT-COMPONENT*. At that point things got all confused, since
91 ;;; IR1 conversion was sending code to a component which had already
92 ;;; been compiled and would never be compiled again.
93 (defun invalid-free-fun-p (free-fun)
94 ;; There might be other reasons that *FREE-FUN* entries could
95 ;; become invalid, but the only one we've been bitten by so far
96 ;; (sbcl-0.pre7.118) is this one:
97 (and (defined-fun-p free-fun)
98 (let ((functional (defined-fun-functional free-fun)))
100 (eql (functional-kind functional) :deleted))
101 (and (lambda-p functional)
103 ;; (The main reason for this first test is to bail
104 ;; out early in cases where the LAMBDA-COMPONENT
105 ;; call in the second test would fail because links
106 ;; it needs are uninitialized or invalid.)
108 ;; If the BIND node for this LAMBDA is null, then
109 ;; according to the slot comments, the LAMBDA has
110 ;; been deleted or its call has been deleted. In
111 ;; that case, it seems rather questionable to reuse
112 ;; it, and certainly it shouldn't be necessary to
113 ;; reuse it, so we cheerfully declare it invalid.
114 (null (lambda-bind functional))
115 ;; If this IR1 stuff belongs to a dead component,
116 ;; then we can't reuse it without getting into
117 ;; bizarre confusion.
118 (eql (component-info (lambda-component functional))
121 ;;; If NAME already has a valid entry in *FREE-FUNS*, then return
122 ;;; the value. Otherwise, make a new GLOBAL-VAR using information from
123 ;;; the global environment and enter it in *FREE-FUNS*. If NAME
124 ;;; names a macro or special form, then we error out using the
125 ;;; supplied context which indicates what we were trying to do that
126 ;;; demanded a function.
127 (declaim (ftype (function (t string) global-var) find-free-fun))
128 (defun find-free-fun (name context)
129 (or (let ((old-free-fun (gethash name *free-funs*)))
130 (and (not (invalid-free-fun-p old-free-fun))
132 (ecase (info :function :kind name)
133 ;; FIXME: The :MACRO and :SPECIAL-FORM cases could be merged.
135 (compiler-error "The macro name ~S was found ~A." name context))
137 (compiler-error "The special form name ~S was found ~A."
141 (check-fun-name name)
142 (note-if-setf-fun-and-macro name)
143 (let ((expansion (fun-name-inline-expansion name))
144 (inlinep (info :function :inlinep name)))
145 (setf (gethash name *free-funs*)
146 (if (or expansion inlinep)
149 :inline-expansion expansion
151 :where-from (info :function :where-from name)
152 :type (info :function :type name))
153 (find-free-really-fun name))))))))
155 ;;; Return the LEAF structure for the lexically apparent function
156 ;;; definition of NAME.
157 (declaim (ftype (function (t string) leaf) find-lexically-apparent-fun))
158 (defun find-lexically-apparent-fun (name context)
159 (let ((var (lexenv-find name funs :test #'equal)))
162 (aver (and (consp var) (eq (car var) 'macro)))
163 (compiler-error "found macro name ~S ~A" name context))
166 (find-free-fun name context)))))
168 ;;; Return the LEAF node for a global variable reference to NAME. If
169 ;;; NAME is already entered in *FREE-VARS*, then we just return the
170 ;;; corresponding value. Otherwise, we make a new leaf using
171 ;;; information from the global environment and enter it in
172 ;;; *FREE-VARS*. If the variable is unknown, then we emit a warning.
173 (declaim (ftype (function (t) (or leaf cons heap-alien-info)) find-free-var))
174 (defun find-free-var (name)
175 (unless (symbolp name)
176 (compiler-error "Variable name is not a symbol: ~S." name))
177 (or (gethash name *free-vars*)
178 (let ((kind (info :variable :kind name))
179 (type (info :variable :type name))
180 (where-from (info :variable :where-from name)))
181 (when (and (eq where-from :assumed) (eq kind :global))
182 (note-undefined-reference name :variable))
183 (setf (gethash name *free-vars*)
186 (info :variable :alien-info name))
187 ;; FIXME: The return value in this case should really be
188 ;; of type SB!C::LEAF. I don't feel too badly about it,
189 ;; because the MACRO idiom is scattered throughout this
190 ;; file, but it should be cleaned up so we're not
191 ;; throwing random conses around. --njf 2002-03-23
193 (let ((expansion (info :variable :macro-expansion name))
194 (type (type-specifier (info :variable :type name))))
195 `(MACRO . (the ,type ,expansion))))
197 (let ((value (info :variable :constant-value name)))
198 (make-constant :value value
200 :type (ctype-of value)
201 :where-from where-from)))
203 (make-global-var :kind kind
206 :where-from where-from)))))))
208 ;;; Grovel over CONSTANT checking for any sub-parts that need to be
209 ;;; processed with MAKE-LOAD-FORM. We have to be careful, because
210 ;;; CONSTANT might be circular. We also check that the constant (and
211 ;;; any subparts) are dumpable at all.
212 (eval-when (:compile-toplevel :load-toplevel :execute)
213 ;; The EVAL-WHEN is necessary for #.(1+ LIST-TO-HASH-TABLE-THRESHOLD)
214 ;; below. -- AL 20010227
215 (def!constant list-to-hash-table-threshold 32))
216 (defun maybe-emit-make-load-forms (constant)
217 (let ((things-processed nil)
219 ;; FIXME: Does this LIST-or-HASH-TABLE messiness give much benefit?
220 (declare (type (or list hash-table) things-processed)
221 (type (integer 0 #.(1+ list-to-hash-table-threshold)) count)
223 (labels ((grovel (value)
224 ;; Unless VALUE is an object which which obviously
225 ;; can't contain other objects
227 '(or #-sb-xc-host unboxed-array
232 (etypecase things-processed
234 (when (member value things-processed :test #'eq)
235 (return-from grovel nil))
236 (push value things-processed)
238 (when (> count list-to-hash-table-threshold)
239 (let ((things things-processed))
240 (setf things-processed
241 (make-hash-table :test 'eq))
242 (dolist (thing things)
243 (setf (gethash thing things-processed) t)))))
245 (when (gethash value things-processed)
246 (return-from grovel nil))
247 (setf (gethash value things-processed) t)))
251 (grovel (cdr value)))
253 (dotimes (i (length value))
254 (grovel (svref value i))))
256 (dotimes (i (length value))
257 (grovel (aref value i))))
259 ;; Even though the (ARRAY T) branch does the exact
260 ;; same thing as this branch we do this separately
261 ;; so that the compiler can use faster versions of
262 ;; array-total-size and row-major-aref.
263 (dotimes (i (array-total-size value))
264 (grovel (row-major-aref value i))))
266 (dotimes (i (array-total-size value))
267 (grovel (row-major-aref value i))))
268 (;; In the target SBCL, we can dump any instance,
269 ;; but in the cross-compilation host,
270 ;; %INSTANCE-FOO functions don't work on general
271 ;; instances, only on STRUCTURE!OBJECTs.
272 #+sb-xc-host structure!object
273 #-sb-xc-host instance
274 (when (emit-make-load-form value)
275 (dotimes (i (%instance-length value))
276 (grovel (%instance-ref value i)))))
279 "Objects of type ~S can't be dumped into fasl files."
280 (type-of value)))))))
284 ;;;; some flow-graph hacking utilities
286 ;;; This function sets up the back link between the node and the
287 ;;; continuation which continues at it.
288 (defun link-node-to-previous-continuation (node cont)
289 (declare (type node node) (type continuation cont))
290 (aver (not (continuation-next cont)))
291 (setf (continuation-next cont) node)
292 (setf (node-prev node) cont))
294 ;;; This function is used to set the continuation for a node, and thus
295 ;;; determine what receives the value and what is evaluated next. If
296 ;;; the continuation has no block, then we make it be in the block
297 ;;; that the node is in. If the continuation heads its block, we end
298 ;;; our block and link it to that block. If the continuation is not
299 ;;; currently used, then we set the DERIVED-TYPE for the continuation
300 ;;; to that of the node, so that a little type propagation gets done.
302 ;;; We also deal with a bit of THE's semantics here: we weaken the
303 ;;; assertion on CONT to be no stronger than the assertion on CONT in
304 ;;; our scope. See the IR1-CONVERT method for THE.
305 #!-sb-fluid (declaim (inline use-continuation))
306 (defun use-continuation (node cont)
307 (declare (type node node) (type continuation cont))
308 (let ((node-block (continuation-block (node-prev node))))
309 (case (continuation-kind cont)
311 (setf (continuation-block cont) node-block)
312 (setf (continuation-kind cont) :inside-block)
313 (setf (continuation-use cont) node)
314 (setf (node-cont node) cont))
316 (%use-continuation node cont)))))
317 (defun %use-continuation (node cont)
318 (declare (type node node) (type continuation cont) (inline member))
319 (let ((block (continuation-block cont))
320 (node-block (continuation-block (node-prev node))))
321 (aver (eq (continuation-kind cont) :block-start))
322 (when (block-last node-block)
323 (error "~S has already ended." node-block))
324 (setf (block-last node-block) node)
325 (when (block-succ node-block)
326 (error "~S already has successors." node-block))
327 (setf (block-succ node-block) (list block))
328 (when (memq node-block (block-pred block))
329 (error "~S is already a predecessor of ~S." node-block block))
330 (push node-block (block-pred block))
331 (add-continuation-use node cont)
332 (unless (eq (continuation-asserted-type cont) *wild-type*)
333 (let ((new (values-type-union (continuation-asserted-type cont)
334 (or (lexenv-find cont type-restrictions)
336 (when (type/= new (continuation-asserted-type cont))
337 (setf (continuation-asserted-type cont) new)
338 (reoptimize-continuation cont))))))
340 ;;;; exported functions
342 ;;; This function takes a form and the top level form number for that
343 ;;; form, and returns a lambda representing the translation of that
344 ;;; form in the current global environment. The returned lambda is a
345 ;;; top level lambda that can be called to cause evaluation of the
346 ;;; forms. This lambda is in the initial component. If FOR-VALUE is T,
347 ;;; then the value of the form is returned from the function,
348 ;;; otherwise NIL is returned.
350 ;;; This function may have arbitrary effects on the global environment
351 ;;; due to processing of EVAL-WHENs. All syntax error checking is
352 ;;; done, with erroneous forms being replaced by a proxy which signals
353 ;;; an error if it is evaluated. Warnings about possibly inconsistent
354 ;;; or illegal changes to the global environment will also be given.
356 ;;; We make the initial component and convert the form in a PROGN (and
357 ;;; an optional NIL tacked on the end.) We then return the lambda. We
358 ;;; bind all of our state variables here, rather than relying on the
359 ;;; global value (if any) so that IR1 conversion will be reentrant.
360 ;;; This is necessary for EVAL-WHEN processing, etc.
362 ;;; The hashtables used to hold global namespace info must be
363 ;;; reallocated elsewhere. Note also that *LEXENV* is not bound, so
364 ;;; that local macro definitions can be introduced by enclosing code.
365 (defun ir1-toplevel (form path for-value)
366 (declare (list path))
367 (let* ((*current-path* path)
368 (component (make-empty-component))
369 (*current-component* component))
370 (setf (component-name component) "initial component")
371 (setf (component-kind component) :initial)
372 (let* ((forms (if for-value `(,form) `(,form nil)))
373 (res (ir1-convert-lambda-body
375 :debug-name (debug-namify "top level form ~S" form))))
376 (setf (functional-entry-fun res) res
377 (functional-arg-documentation res) ()
378 (functional-kind res) :toplevel)
381 ;;; *CURRENT-FORM-NUMBER* is used in FIND-SOURCE-PATHS to compute the
382 ;;; form number to associate with a source path. This should be bound
383 ;;; to an initial value of 0 before the processing of each truly
385 (declaim (type index *current-form-number*))
386 (defvar *current-form-number*)
388 ;;; This function is called on freshly read forms to record the
389 ;;; initial location of each form (and subform.) Form is the form to
390 ;;; find the paths in, and TLF-NUM is the top level form number of the
391 ;;; truly top level form.
393 ;;; This gets a bit interesting when the source code is circular. This
394 ;;; can (reasonably?) happen in the case of circular list constants.
395 (defun find-source-paths (form tlf-num)
396 (declare (type index tlf-num))
397 (let ((*current-form-number* 0))
398 (sub-find-source-paths form (list tlf-num)))
400 (defun sub-find-source-paths (form path)
401 (unless (gethash form *source-paths*)
402 (setf (gethash form *source-paths*)
403 (list* 'original-source-start *current-form-number* path))
404 (incf *current-form-number*)
408 (declare (fixnum pos))
411 (when (atom subform) (return))
412 (let ((fm (car subform)))
414 (sub-find-source-paths fm (cons pos path)))
416 (setq subform (cdr subform))
417 (when (eq subform trail) (return)))))
421 (setq trail (cdr trail)))))))
423 ;;;; IR1-CONVERT, macroexpansion and special form dispatching
425 (macrolet (;; Bind *COMPILER-ERROR-BAILOUT* to a function that throws
426 ;; out of the body and converts a proxy form instead.
427 (ir1-error-bailout ((start
431 (proxy ``(error "execution of a form compiled with errors:~% ~S"
434 (let ((skip (gensym "SKIP")))
436 (catch 'ir1-error-abort
437 (let ((*compiler-error-bailout*
439 (throw 'ir1-error-abort nil))))
441 (return-from ,skip nil)))
442 (ir1-convert ,start ,cont ,proxy)))))
444 ;; Translate FORM into IR1. The code is inserted as the NEXT of the
445 ;; continuation START. CONT is the continuation which receives the
446 ;; value of the FORM to be translated. The translators call this
447 ;; function recursively to translate their subnodes.
449 ;; As a special hack to make life easier in the compiler, a LEAF
450 ;; IR1-converts into a reference to that LEAF structure. This allows
451 ;; the creation using backquote of forms that contain leaf
452 ;; references, without having to introduce dummy names into the
454 (declaim (ftype (function (continuation continuation t) (values)) ir1-convert))
455 (defun ir1-convert (start cont form)
456 (ir1-error-bailout (start cont form)
457 (let ((*current-path* (or (gethash form *source-paths*)
458 (cons form *current-path*))))
460 (cond ((and (symbolp form) (not (keywordp form)))
461 (ir1-convert-var start cont form))
463 (reference-leaf start cont form))
465 (reference-constant start cont form)))
466 (let ((opname (car form)))
467 (cond ((symbolp opname)
468 (let ((lexical-def (lexenv-find opname funs)))
469 (typecase lexical-def
470 (null (ir1-convert-global-functoid start cont form))
472 (ir1-convert-local-combination start
477 (ir1-convert-srctran start cont lexical-def form))
479 (aver (and (consp lexical-def)
480 (eq (car lexical-def) 'macro)))
481 (ir1-convert start cont
482 (careful-expand-macro (cdr lexical-def)
484 ((or (atom opname) (not (eq (car opname) 'lambda)))
485 (compiler-error "illegal function call"))
487 ;; implicitly (LAMBDA ..) because the LAMBDA
488 ;; expression is the CAR of an executed form
489 (ir1-convert-combination start
494 :debug-name (debug-namify
499 ;; Generate a reference to a manifest constant, creating a new leaf
500 ;; if necessary. If we are producing a fasl file, make sure that
501 ;; MAKE-LOAD-FORM gets used on any parts of the constant that it
503 (defun reference-constant (start cont value)
504 (declare (type continuation start cont)
505 (inline find-constant))
507 (start cont value '(error "attempt to reference undumpable constant"))
508 (when (producing-fasl-file)
509 (maybe-emit-make-load-forms value))
510 (let* ((leaf (find-constant value))
511 (res (make-ref (leaf-type leaf) leaf)))
512 (push res (leaf-refs leaf))
513 (link-node-to-previous-continuation res start)
514 (use-continuation res cont)))
517 ;;; Add FUNCTIONAL to the COMPONENT-REANALYZE-FUNCTIONALS, unless it's
518 ;;; some trivial type for which reanalysis is a trivial no-op, or
519 ;;; unless it doesn't belong in this component at all.
521 ;;; FUNCTIONAL is returned.
522 (defun maybe-reanalyze-functional (functional)
524 (aver (not (eql (functional-kind functional) :deleted))) ; bug 148
525 (aver-live-component *current-component*)
527 ;; When FUNCTIONAL is of a type for which reanalysis isn't a trivial
529 (when (typep functional '(or optional-dispatch clambda))
531 ;; When FUNCTIONAL knows its component
532 (when (lambda-p functional)
533 (aver (eql (lambda-component functional) *current-component*)))
536 (component-reanalyze-functionals *current-component*)))
540 ;;; Generate a REF node for LEAF, frobbing the LEAF structure as
541 ;;; needed. If LEAF represents a defined function which has already
542 ;;; been converted, and is not :NOTINLINE, then reference the
543 ;;; functional instead.
544 (defun reference-leaf (start cont leaf)
545 (declare (type continuation start cont) (type leaf leaf))
546 (let* ((leaf (or (and (defined-fun-p leaf)
547 (not (eq (defined-fun-inlinep leaf)
549 (let ((functional (defined-fun-functional leaf)))
550 (when (and functional
551 (not (functional-kind functional)))
552 (maybe-reanalyze-functional functional))))
554 (res (make-ref (or (lexenv-find leaf type-restrictions)
557 (push res (leaf-refs leaf))
558 (setf (leaf-ever-used leaf) t)
559 (link-node-to-previous-continuation res start)
560 (use-continuation res cont)))
562 ;;; Convert a reference to a symbolic constant or variable. If the
563 ;;; symbol is entered in the LEXENV-VARS we use that definition,
564 ;;; otherwise we find the current global definition. This is also
565 ;;; where we pick off symbol macro and alien variable references.
566 (defun ir1-convert-var (start cont name)
567 (declare (type continuation start cont) (symbol name))
568 (let ((var (or (lexenv-find name vars) (find-free-var name))))
571 (when (lambda-var-p var)
572 (let ((home (continuation-home-lambda-or-null start)))
574 (pushnew var (lambda-calls-or-closes home))))
575 (when (lambda-var-ignorep var)
576 ;; (ANSI's specification for the IGNORE declaration requires
577 ;; that this be a STYLE-WARNING, not a full WARNING.)
578 (compiler-style-warn "reading an ignored variable: ~S" name)))
579 (reference-leaf start cont var))
581 (aver (eq (car var) 'MACRO))
582 (ir1-convert start cont (cdr var)))
584 (ir1-convert start cont `(%heap-alien ',var)))))
587 ;;; Convert anything that looks like a special form, global function
589 (defun ir1-convert-global-functoid (start cont form)
590 (declare (type continuation start cont) (list form))
591 (let* ((fun (first form))
592 (translator (info :function :ir1-convert fun))
593 (cmacro (info :function :compiler-macro-function fun)))
594 (cond (translator (funcall translator start cont form))
596 (not (eq (info :function :inlinep fun)
598 (let ((res (careful-expand-macro cmacro form)))
600 (ir1-convert-global-functoid-no-cmacro start cont form fun)
601 (ir1-convert start cont res))))
603 (ir1-convert-global-functoid-no-cmacro start cont form fun)))))
605 ;;; Handle the case of where the call was not a compiler macro, or was
606 ;;; a compiler macro and passed.
607 (defun ir1-convert-global-functoid-no-cmacro (start cont form fun)
608 (declare (type continuation start cont) (list form))
609 ;; FIXME: Couldn't all the INFO calls here be converted into
610 ;; standard CL functions, like MACRO-FUNCTION or something?
611 ;; And what happens with lexically-defined (MACROLET) macros
613 (ecase (info :function :kind fun)
617 (careful-expand-macro (info :function :macro-function fun)
620 (ir1-convert-srctran start
622 (find-free-fun fun "shouldn't happen! (no-cmacro)")
625 (defun muffle-warning-or-die ()
627 (bug "no MUFFLE-WARNING restart"))
629 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
630 ;;; errors which occur during the macroexpansion.
631 (defun careful-expand-macro (fun form)
632 (let (;; a hint I (WHN) wish I'd known earlier
633 (hint "(hint: For more precise location, try *BREAK-ON-SIGNALS*.)"))
634 (flet (;; Return a string to use as a prefix in error reporting,
635 ;; telling something about which form caused the problem.
637 (let ((*print-pretty* nil)
638 ;; We rely on the printer to abbreviate FORM.
643 #-sb-xc-host "(in macroexpansion of ~S)"
644 ;; longer message to avoid ambiguity "Was it the xc host
645 ;; or the cross-compiler which encountered the problem?"
646 #+sb-xc-host "(in cross-compiler macroexpansion of ~S)"
648 (handler-bind (;; When cross-compiling, we can get style warnings
649 ;; about e.g. undefined functions. An unhandled
650 ;; CL:STYLE-WARNING (as opposed to a
651 ;; SB!C::COMPILER-NOTE) would cause FAILURE-P to be
652 ;; set on the return from #'SB!XC:COMPILE-FILE, which
653 ;; would falsely indicate an error sufficiently
654 ;; serious that we should stop the build process. To
655 ;; avoid this, we translate CL:STYLE-WARNING
656 ;; conditions from the host Common Lisp into
657 ;; cross-compiler SB!C::COMPILER-NOTE calls. (It
658 ;; might be cleaner to just make Python use
659 ;; CL:STYLE-WARNING internally, so that the
660 ;; significance of any host Common Lisp
661 ;; CL:STYLE-WARNINGs is understood automatically. But
662 ;; for now I'm not motivated to do this. -- WHN
664 (style-warning (lambda (c)
665 (compiler-note "~@<~A~:@_~A~:@_~A~:>"
666 (wherestring) hint c)
667 (muffle-warning-or-die)))
668 ;; KLUDGE: CMU CL in its wisdom (version 2.4.6 for
669 ;; Debian Linux, anyway) raises a CL:WARNING
670 ;; condition (not a CL:STYLE-WARNING) for undefined
671 ;; symbols when converting interpreted functions,
672 ;; causing COMPILE-FILE to think the file has a real
673 ;; problem, causing COMPILE-FILE to return FAILURE-P
674 ;; set (not just WARNINGS-P set). Since undefined
675 ;; symbol warnings are often harmless forward
676 ;; references, and since it'd be inordinately painful
677 ;; to try to eliminate all such forward references,
678 ;; these warnings are basically unavoidable. Thus, we
679 ;; need to coerce the system to work through them,
680 ;; and this code does so, by crudely suppressing all
681 ;; warnings in cross-compilation macroexpansion. --
688 ~@<(KLUDGE: That was a non-STYLE WARNING. ~
689 Ordinarily that would cause compilation to ~
690 fail. However, since we're running under ~
691 CMU CL, and since CMU CL emits non-STYLE ~
692 warnings for safe, hard-to-fix things (e.g. ~
693 references to not-yet-defined functions) ~
694 we're going to have to ignore it and ~
695 proceed anyway. Hopefully we're not ~
696 ignoring anything horrible here..)~:@>~:>"
699 (muffle-warning-or-die)))
701 (compiler-error "~@<~A~:@_~A~@:_~A~:>"
702 (wherestring) hint c))))
703 (funcall sb!xc:*macroexpand-hook* fun form *lexenv*)))))
705 ;;;; conversion utilities
707 ;;; Convert a bunch of forms, discarding all the values except the
708 ;;; last. If there aren't any forms, then translate a NIL.
709 (declaim (ftype (function (continuation continuation list) (values))
710 ir1-convert-progn-body))
711 (defun ir1-convert-progn-body (start cont body)
713 (reference-constant start cont nil)
714 (let ((this-start start)
717 (let ((form (car forms)))
718 (when (endp (cdr forms))
719 (ir1-convert this-start cont form)
721 (let ((this-cont (make-continuation)))
722 (ir1-convert this-start this-cont form)
723 (setq this-start this-cont
724 forms (cdr forms)))))))
727 ;;;; converting combinations
729 ;;; Convert a function call where the function FUN is a LEAF. FORM is
730 ;;; the source for the call. We return the COMBINATION node so that
731 ;;; the caller can poke at it if it wants to.
732 (declaim (ftype (function (continuation continuation list leaf) combination)
733 ir1-convert-combination))
734 (defun ir1-convert-combination (start cont form fun)
735 (let ((fun-cont (make-continuation)))
736 (reference-leaf start fun-cont fun)
737 (ir1-convert-combination-args fun-cont cont (cdr form))))
739 ;;; Convert the arguments to a call and make the COMBINATION
740 ;;; node. FUN-CONT is the continuation which yields the function to
741 ;;; call. ARGS is the list of arguments for the call, which defaults
742 ;;; to the cdr of source. We return the COMBINATION node.
743 (defun ir1-convert-combination-args (fun-cont cont args)
744 (declare (type continuation fun-cont cont) (list args))
745 (let ((node (make-combination fun-cont)))
746 (setf (continuation-dest fun-cont) node)
747 (assert-continuation-type fun-cont
748 (specifier-type '(or function symbol)))
749 (collect ((arg-conts))
750 (let ((this-start fun-cont))
752 (let ((this-cont (make-continuation node)))
753 (ir1-convert this-start this-cont arg)
754 (setq this-start this-cont)
755 (arg-conts this-cont)))
756 (link-node-to-previous-continuation node this-start)
757 (use-continuation node cont)
758 (setf (combination-args node) (arg-conts))))
761 ;;; Convert a call to a global function. If not :NOTINLINE, then we do
762 ;;; source transforms and try out any inline expansion. If there is no
763 ;;; expansion, but is :INLINE, then give an efficiency note (unless a
764 ;;; known function which will quite possibly be open-coded.) Next, we
765 ;;; go to ok-combination conversion.
766 (defun ir1-convert-srctran (start cont var form)
767 (declare (type continuation start cont) (type global-var var))
768 (let ((inlinep (when (defined-fun-p var)
769 (defined-fun-inlinep var))))
770 (if (eq inlinep :notinline)
771 (ir1-convert-combination start cont form var)
772 (let ((transform (info :function
774 (leaf-source-name var))))
776 (multiple-value-bind (result pass) (funcall transform form)
778 (ir1-convert-maybe-predicate start cont form var)
779 (ir1-convert start cont result)))
780 (ir1-convert-maybe-predicate start cont form var))))))
782 ;;; If the function has the PREDICATE attribute, and the CONT's DEST
783 ;;; isn't an IF, then we convert (IF <form> T NIL), ensuring that a
784 ;;; predicate always appears in a conditional context.
786 ;;; If the function isn't a predicate, then we call
787 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
788 (defun ir1-convert-maybe-predicate (start cont form var)
789 (declare (type continuation start cont) (list form) (type global-var var))
790 (let ((info (info :function :info (leaf-source-name var))))
792 (ir1-attributep (fun-info-attributes info) predicate)
793 (not (if-p (continuation-dest cont))))
794 (ir1-convert start cont `(if ,form t nil))
795 (ir1-convert-combination-checking-type start cont form var))))
797 ;;; Actually really convert a global function call that we are allowed
800 ;;; If we know the function type of the function, then we check the
801 ;;; call for syntactic legality with respect to the declared function
802 ;;; type. If it is impossible to determine whether the call is correct
803 ;;; due to non-constant keywords, then we give up, marking the call as
804 ;;; :FULL to inhibit further error messages. We return true when the
807 ;;; If the call is legal, we also propagate type assertions from the
808 ;;; function type to the arg and result continuations. We do this now
809 ;;; so that IR1 optimize doesn't have to redundantly do the check
810 ;;; later so that it can do the type propagation.
811 (defun ir1-convert-combination-checking-type (start cont form var)
812 (declare (type continuation start cont) (list form) (type leaf var))
813 (let* ((node (ir1-convert-combination start cont form var))
814 (fun-cont (basic-combination-fun node))
815 (type (leaf-type var)))
816 (when (validate-call-type node type t)
817 (setf (continuation-%derived-type fun-cont) type)
818 (setf (continuation-reoptimize fun-cont) nil)
819 (setf (continuation-%type-check fun-cont) nil)))
822 ;;; Convert a call to a local function, or if the function has already
823 ;;; been LET converted, then throw FUNCTIONAL to
824 ;;; LOCALL-ALREADY-LET-CONVERTED. The THROW should only happen when we
825 ;;; are converting inline expansions for local functions during
827 (defun ir1-convert-local-combination (start cont form functional)
829 ;; The test here is for "when LET converted", as a translation of
830 ;; the old CMU CL comments into code. Unfortunately, the old CMU CL
831 ;; comments aren't specific enough to tell whether the correct
832 ;; translation is FUNCTIONAL-SOMEWHAT-LETLIKE-P or
833 ;; FUNCTIONAL-LETLIKE-P or what. The old CMU CL code assumed that
834 ;; any non-null FUNCTIONAL-KIND meant that the function "had been
835 ;; LET converted", which might even be right, but seems fragile, so
836 ;; we try to be pickier.
838 ;; looks LET-converted
839 (functional-somewhat-letlike-p functional)
840 ;; It's possible for a LET-converted function to end up
841 ;; deleted later. In that case, for the purposes of this
842 ;; analysis, it is LET-converted: LET-converted functionals
843 ;; are too badly trashed to expand them inline, and deleted
844 ;; LET-converted functionals are even worse.
845 (eql (functional-kind functional) :deleted))
846 (throw 'locall-already-let-converted functional))
847 ;; Any other non-NIL KIND value is a case we haven't found a
848 ;; justification for, and at least some such values (e.g. :EXTERNAL
849 ;; and :TOPLEVEL) seem obviously wrong.
850 (aver (null (functional-kind functional)))
852 (ir1-convert-combination start
855 (maybe-reanalyze-functional functional)))
859 ;;; Given a list of LAMBDA-VARs and a variable name, return the
860 ;;; LAMBDA-VAR for that name, or NIL if it isn't found. We return the
861 ;;; *last* variable with that name, since LET* bindings may be
862 ;;; duplicated, and declarations always apply to the last.
863 (declaim (ftype (function (list symbol) (or lambda-var list))
865 (defun find-in-bindings (vars name)
869 (when (eq (leaf-source-name var) name)
871 (let ((info (lambda-var-arg-info var)))
873 (let ((supplied-p (arg-info-supplied-p info)))
874 (when (and supplied-p
875 (eq (leaf-source-name supplied-p) name))
876 (setq found supplied-p))))))
877 ((and (consp var) (eq (car var) name))
878 (setf found (cdr var)))))
881 ;;; Called by PROCESS-DECLS to deal with a variable type declaration.
882 ;;; If a LAMBDA-VAR being bound, we intersect the type with the var's
883 ;;; type, otherwise we add a type restriction on the var. If a symbol
884 ;;; macro, we just wrap a THE around the expansion.
885 (defun process-type-decl (decl res vars)
886 (declare (list decl vars) (type lexenv res))
887 (let ((type (specifier-type (first decl))))
888 (collect ((restr nil cons)
890 (dolist (var-name (rest decl))
891 (let* ((bound-var (find-in-bindings vars var-name))
893 (lexenv-find var-name vars)
894 (find-free-var var-name))))
897 (let* ((old-type (or (lexenv-find var type-restrictions)
899 (int (if (or (fun-type-p type)
900 (fun-type-p old-type))
902 (type-approx-intersection2 old-type type))))
903 (cond ((eq int *empty-type*)
904 (unless (policy *lexenv* (= inhibit-warnings 3))
906 "The type declarations ~S and ~S for ~S conflict."
907 (type-specifier old-type) (type-specifier type)
909 (bound-var (setf (leaf-type bound-var) int))
911 (restr (cons var int))))))
913 ;; FIXME: non-ANSI weirdness
914 (aver (eq (car var) 'MACRO))
915 (new-vars `(,var-name . (MACRO . (the ,(first decl)
919 "~S is an alien variable, so its type can't be declared."
922 (if (or (restr) (new-vars))
923 (make-lexenv :default res
924 :type-restrictions (restr)
928 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
929 ;;; declarations for function variables. In addition to allowing
930 ;;; declarations for functions being bound, we must also deal with
931 ;;; declarations that constrain the type of lexically apparent
933 (defun process-ftype-decl (spec res names fvars)
934 (declare (list spec names fvars) (type lexenv res))
935 (let ((type (specifier-type spec)))
936 (collect ((res nil cons))
938 (let ((found (find name fvars
939 :key #'leaf-source-name
943 (setf (leaf-type found) type)
944 (assert-definition-type found type
945 :unwinnage-fun #'compiler-note
946 :where "FTYPE declaration"))
948 (res (cons (find-lexically-apparent-fun
949 name "in a function type declaration")
952 (make-lexenv :default res :type-restrictions (res))
955 ;;; Process a special declaration, returning a new LEXENV. A non-bound
956 ;;; special declaration is instantiated by throwing a special variable
957 ;;; into the variables.
958 (defun process-special-decl (spec res vars)
959 (declare (list spec vars) (type lexenv res))
960 (collect ((new-venv nil cons))
961 (dolist (name (cdr spec))
962 (let ((var (find-in-bindings vars name)))
965 (aver (eq (car var) 'MACRO))
967 "~S is a symbol-macro and thus can't be declared special."
970 (when (lambda-var-ignorep var)
971 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
972 ;; requires that this be a STYLE-WARNING, not a full WARNING.
974 "The ignored variable ~S is being declared special."
976 (setf (lambda-var-specvar var)
977 (specvar-for-binding name)))
979 (unless (assoc name (new-venv) :test #'eq)
980 (new-venv (cons name (specvar-for-binding name))))))))
982 (make-lexenv :default res :vars (new-venv))
985 ;;; Return a DEFINED-FUN which copies a GLOBAL-VAR but for its INLINEP.
986 (defun make-new-inlinep (var inlinep)
987 (declare (type global-var var) (type inlinep inlinep))
988 (let ((res (make-defined-fun
989 :%source-name (leaf-source-name var)
990 :where-from (leaf-where-from var)
991 :type (leaf-type var)
993 (when (defined-fun-p var)
994 (setf (defined-fun-inline-expansion res)
995 (defined-fun-inline-expansion var))
996 (setf (defined-fun-functional res)
997 (defined-fun-functional var)))
1000 ;;; Parse an inline/notinline declaration. If it's a local function we're
1001 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
1002 (defun process-inline-decl (spec res fvars)
1003 (let ((sense (cdr (assoc (first spec) *inlinep-translations* :test #'eq)))
1005 (dolist (name (rest spec))
1006 (let ((fvar (find name fvars
1007 :key #'leaf-source-name
1010 (setf (functional-inlinep fvar) sense)
1012 (find-lexically-apparent-fun
1013 name "in an inline or notinline declaration")))
1016 (when (policy *lexenv* (>= speed inhibit-warnings))
1017 (compiler-note "ignoring ~A declaration not at ~
1018 definition of local function:~% ~S"
1021 (push (cons name (make-new-inlinep found sense))
1025 (make-lexenv :default res :funs new-fenv)
1028 ;;; like FIND-IN-BINDINGS, but looks for #'FOO in the FVARS
1029 (defun find-in-bindings-or-fbindings (name vars fvars)
1030 (declare (list vars fvars))
1032 (destructuring-bind (wot fn-name) name
1033 (unless (eq wot 'function)
1034 (compiler-error "The function or variable name ~S is unrecognizable."
1036 (find fn-name fvars :key #'leaf-source-name :test #'equal))
1037 (find-in-bindings vars name)))
1039 ;;; Process an ignore/ignorable declaration, checking for various losing
1041 (defun process-ignore-decl (spec vars fvars)
1042 (declare (list spec vars fvars))
1043 (dolist (name (rest spec))
1044 (let ((var (find-in-bindings-or-fbindings name vars fvars)))
1047 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1048 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1049 (compiler-style-warn "declaring unknown variable ~S to be ignored"
1051 ;; FIXME: This special case looks like non-ANSI weirdness.
1052 ((and (consp var) (consp (cdr var)) (eq (cadr var) 'macro))
1053 ;; Just ignore the IGNORE decl.
1056 (setf (leaf-ever-used var) t))
1057 ((and (lambda-var-specvar var) (eq (first spec) 'ignore))
1058 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1059 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1060 (compiler-style-warn "declaring special variable ~S to be ignored"
1062 ((eq (first spec) 'ignorable)
1063 (setf (leaf-ever-used var) t))
1065 (setf (lambda-var-ignorep var) t)))))
1068 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
1069 ;;; go away, I think.
1070 (defvar *suppress-values-declaration* nil
1072 "If true, processing of the VALUES declaration is inhibited.")
1074 ;;; Process a single declaration spec, augmenting the specified LEXENV
1075 ;;; RES and returning it as a result. VARS and FVARS are as described in
1077 (defun process-1-decl (raw-spec res vars fvars cont)
1078 (declare (type list raw-spec vars fvars))
1079 (declare (type lexenv res))
1080 (declare (type continuation cont))
1081 (let ((spec (canonized-decl-spec raw-spec)))
1083 (special (process-special-decl spec res vars))
1086 (compiler-error "no type specified in FTYPE declaration: ~S" spec))
1087 (process-ftype-decl (second spec) res (cddr spec) fvars))
1088 ((inline notinline maybe-inline)
1089 (process-inline-decl spec res fvars))
1091 (process-ignore-decl spec vars fvars)
1096 :policy (process-optimize-decl spec (lexenv-policy res))))
1098 (process-type-decl (cdr spec) res vars))
1100 (if *suppress-values-declaration*
1102 (let ((types (cdr spec)))
1103 (ir1ize-the-or-values (if (eql (length types) 1)
1110 (when (policy *lexenv* (> speed inhibit-warnings))
1112 "compiler limitation: ~
1113 ~% There's no special support for DYNAMIC-EXTENT (so it's ignored)."))
1116 (unless (info :declaration :recognized (first spec))
1117 (compiler-warn "unrecognized declaration ~S" raw-spec))
1120 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1121 ;;; and FUNCTIONAL structures which are being bound. In addition to
1122 ;;; filling in slots in the leaf structures, we return a new LEXENV
1123 ;;; which reflects pervasive special and function type declarations,
1124 ;;; (NOT)INLINE declarations and OPTIMIZE declarations. CONT is the
1125 ;;; continuation affected by VALUES declarations.
1127 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1129 (defun process-decls (decls vars fvars cont &optional (env *lexenv*))
1130 (declare (list decls vars fvars) (type continuation cont))
1131 (dolist (decl decls)
1132 (dolist (spec (rest decl))
1133 (unless (consp spec)
1134 (compiler-error "malformed declaration specifier ~S in ~S" spec decl))
1135 (setq env (process-1-decl spec env vars fvars cont))))
1138 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1139 ;;; declaration. If there is a global variable of that name, then
1140 ;;; check that it isn't a constant and return it. Otherwise, create an
1141 ;;; anonymous GLOBAL-VAR.
1142 (defun specvar-for-binding (name)
1143 (cond ((not (eq (info :variable :where-from name) :assumed))
1144 (let ((found (find-free-var name)))
1145 (when (heap-alien-info-p found)
1147 "~S is an alien variable and so can't be declared special."
1149 (unless (global-var-p found)
1151 "~S is a constant and so can't be declared special."
1155 (make-global-var :kind :special
1157 :where-from :declared))))
1161 ;;;; Note: Take a look at the compiler-overview.tex section on "Hairy
1162 ;;;; function representation" before you seriously mess with this
1165 ;;; Verify that the NAME is a legal name for a variable and return a
1166 ;;; VAR structure for it, filling in info if it is globally special.
1167 ;;; If it is losing, we punt with a COMPILER-ERROR. NAMES-SO-FAR is a
1168 ;;; list of names which have previously been bound. If the NAME is in
1169 ;;; this list, then we error out.
1170 (declaim (ftype (function (t list) lambda-var) varify-lambda-arg))
1171 (defun varify-lambda-arg (name names-so-far)
1172 (declare (inline member))
1173 (unless (symbolp name)
1174 (compiler-error "The lambda variable ~S is not a symbol." name))
1175 (when (member name names-so-far :test #'eq)
1176 (compiler-error "The variable ~S occurs more than once in the lambda list."
1178 (let ((kind (info :variable :kind name)))
1179 (when (or (keywordp name) (eq kind :constant))
1180 (compiler-error "The name of the lambda variable ~S is already in use to name a constant."
1182 (cond ((eq kind :special)
1183 (let ((specvar (find-free-var name)))
1184 (make-lambda-var :%source-name name
1185 :type (leaf-type specvar)
1186 :where-from (leaf-where-from specvar)
1189 (make-lambda-var :%source-name name)))))
1191 ;;; Make the default keyword for a &KEY arg, checking that the keyword
1192 ;;; isn't already used by one of the VARS. We also check that the
1193 ;;; keyword isn't the magical :ALLOW-OTHER-KEYS.
1194 (declaim (ftype (function (symbol list t) keyword) make-keyword-for-arg))
1195 (defun make-keyword-for-arg (symbol vars keywordify)
1196 (let ((key (if (and keywordify (not (keywordp symbol)))
1197 (keywordicate symbol)
1199 (when (eq key :allow-other-keys)
1200 (compiler-error "No &KEY arg can be called :ALLOW-OTHER-KEYS."))
1202 (let ((info (lambda-var-arg-info var)))
1204 (eq (arg-info-kind info) :keyword)
1205 (eq (arg-info-key info) key))
1207 "The keyword ~S appears more than once in the lambda list."
1211 ;;; Parse a lambda list into a list of VAR structures, stripping off
1212 ;;; any &AUX bindings. Each arg name is checked for legality, and
1213 ;;; duplicate names are checked for. If an arg is globally special,
1214 ;;; the var is marked as :SPECIAL instead of :LEXICAL. &KEY,
1215 ;;; &OPTIONAL and &REST args are annotated with an ARG-INFO structure
1216 ;;; which contains the extra information. If we hit something losing,
1217 ;;; we bug out with COMPILER-ERROR. These values are returned:
1218 ;;; 1. a list of the var structures for each top level argument;
1219 ;;; 2. a flag indicating whether &KEY was specified;
1220 ;;; 3. a flag indicating whether other &KEY args are allowed;
1221 ;;; 4. a list of the &AUX variables; and
1222 ;;; 5. a list of the &AUX values.
1223 (declaim (ftype (function (list) (values list boolean boolean list list))
1225 (defun make-lambda-vars (list)
1226 (multiple-value-bind (required optional restp rest keyp keys allowp aux
1227 morep more-context more-count)
1228 (parse-lambda-list list)
1233 (flet (;; PARSE-DEFAULT deals with defaults and supplied-p args
1234 ;; for optionals and keywords args.
1235 (parse-default (spec info)
1236 (when (consp (cdr spec))
1237 (setf (arg-info-default info) (second spec))
1238 (when (consp (cddr spec))
1239 (let* ((supplied-p (third spec))
1240 (supplied-var (varify-lambda-arg supplied-p
1242 (setf (arg-info-supplied-p info) supplied-var)
1243 (names-so-far supplied-p)
1244 (when (> (length (the list spec)) 3)
1246 "The list ~S is too long to be an arg specifier."
1249 (dolist (name required)
1250 (let ((var (varify-lambda-arg name (names-so-far))))
1252 (names-so-far name)))
1254 (dolist (spec optional)
1256 (let ((var (varify-lambda-arg spec (names-so-far))))
1257 (setf (lambda-var-arg-info var)
1258 (make-arg-info :kind :optional))
1260 (names-so-far spec))
1261 (let* ((name (first spec))
1262 (var (varify-lambda-arg name (names-so-far)))
1263 (info (make-arg-info :kind :optional)))
1264 (setf (lambda-var-arg-info var) info)
1267 (parse-default spec info))))
1270 (let ((var (varify-lambda-arg rest (names-so-far))))
1271 (setf (lambda-var-arg-info var) (make-arg-info :kind :rest))
1273 (names-so-far rest)))
1276 (let ((var (varify-lambda-arg more-context (names-so-far))))
1277 (setf (lambda-var-arg-info var)
1278 (make-arg-info :kind :more-context))
1280 (names-so-far more-context))
1281 (let ((var (varify-lambda-arg more-count (names-so-far))))
1282 (setf (lambda-var-arg-info var)
1283 (make-arg-info :kind :more-count))
1285 (names-so-far more-count)))
1290 (let ((var (varify-lambda-arg spec (names-so-far))))
1291 (setf (lambda-var-arg-info var)
1292 (make-arg-info :kind :keyword
1293 :key (make-keyword-for-arg spec
1297 (names-so-far spec)))
1298 ((atom (first spec))
1299 (let* ((name (first spec))
1300 (var (varify-lambda-arg name (names-so-far)))
1301 (info (make-arg-info
1303 :key (make-keyword-for-arg name (vars) t))))
1304 (setf (lambda-var-arg-info var) info)
1307 (parse-default spec info)))
1309 (let ((head (first spec)))
1310 (unless (proper-list-of-length-p head 2)
1311 (error "malformed &KEY argument specifier: ~S" spec))
1312 (let* ((name (second head))
1313 (var (varify-lambda-arg name (names-so-far)))
1314 (info (make-arg-info
1316 :key (make-keyword-for-arg (first head)
1319 (setf (lambda-var-arg-info var) info)
1322 (parse-default spec info))))))
1326 (let ((var (varify-lambda-arg spec nil)))
1329 (names-so-far spec)))
1331 (unless (proper-list-of-length-p spec 1 2)
1332 (compiler-error "malformed &AUX binding specifier: ~S"
1334 (let* ((name (first spec))
1335 (var (varify-lambda-arg name nil)))
1337 (aux-vals (second spec))
1338 (names-so-far name)))))
1340 (values (vars) keyp allowp (aux-vars) (aux-vals))))))
1342 ;;; This is similar to IR1-CONVERT-PROGN-BODY except that we
1343 ;;; sequentially bind each AUX-VAR to the corresponding AUX-VAL before
1344 ;;; converting the body. If there are no bindings, just convert the
1345 ;;; body, otherwise do one binding and recurse on the rest.
1347 ;;; FIXME: This could and probably should be converted to use
1348 ;;; SOURCE-NAME and DEBUG-NAME. But I (WHN) don't use &AUX bindings,
1349 ;;; so I'm not motivated. Patches will be accepted...
1350 (defun ir1-convert-aux-bindings (start cont body aux-vars aux-vals)
1351 (declare (type continuation start cont) (list body aux-vars aux-vals))
1353 (ir1-convert-progn-body start cont body)
1354 (let ((fun-cont (make-continuation))
1355 (fun (ir1-convert-lambda-body body
1356 (list (first aux-vars))
1357 :aux-vars (rest aux-vars)
1358 :aux-vals (rest aux-vals)
1359 :debug-name (debug-namify
1362 (reference-leaf start fun-cont fun)
1363 (ir1-convert-combination-args fun-cont cont
1364 (list (first aux-vals)))))
1367 ;;; This is similar to IR1-CONVERT-PROGN-BODY except that code to bind
1368 ;;; the SPECVAR for each SVAR to the value of the variable is wrapped
1369 ;;; around the body. If there are no special bindings, we just convert
1370 ;;; the body, otherwise we do one special binding and recurse on the
1373 ;;; We make a cleanup and introduce it into the lexical environment.
1374 ;;; If there are multiple special bindings, the cleanup for the blocks
1375 ;;; will end up being the innermost one. We force CONT to start a
1376 ;;; block outside of this cleanup, causing cleanup code to be emitted
1377 ;;; when the scope is exited.
1378 (defun ir1-convert-special-bindings (start cont body aux-vars aux-vals svars)
1379 (declare (type continuation start cont)
1380 (list body aux-vars aux-vals svars))
1383 (ir1-convert-aux-bindings start cont body aux-vars aux-vals))
1385 (continuation-starts-block cont)
1386 (let ((cleanup (make-cleanup :kind :special-bind))
1388 (next-cont (make-continuation))
1389 (nnext-cont (make-continuation)))
1390 (ir1-convert start next-cont
1391 `(%special-bind ',(lambda-var-specvar var) ,var))
1392 (setf (cleanup-mess-up cleanup) (continuation-use next-cont))
1393 (let ((*lexenv* (make-lexenv :cleanup cleanup)))
1394 (ir1-convert next-cont nnext-cont '(%cleanup-point))
1395 (ir1-convert-special-bindings nnext-cont cont body aux-vars aux-vals
1399 ;;; Create a lambda node out of some code, returning the result. The
1400 ;;; bindings are specified by the list of VAR structures VARS. We deal
1401 ;;; with adding the names to the LEXENV-VARS for the conversion. The
1402 ;;; result is added to the NEW-FUNCTIONALS in the *CURRENT-COMPONENT*
1403 ;;; and linked to the component head and tail.
1405 ;;; We detect special bindings here, replacing the original VAR in the
1406 ;;; lambda list with a temporary variable. We then pass a list of the
1407 ;;; special vars to IR1-CONVERT-SPECIAL-BINDINGS, which actually emits
1408 ;;; the special binding code.
1410 ;;; We ignore any ARG-INFO in the VARS, trusting that someone else is
1411 ;;; dealing with &nonsense.
1413 ;;; AUX-VARS is a list of VAR structures for variables that are to be
1414 ;;; sequentially bound. Each AUX-VAL is a form that is to be evaluated
1415 ;;; to get the initial value for the corresponding AUX-VAR.
1416 (defun ir1-convert-lambda-body (body
1422 (source-name '.anonymous.)
1424 (declare (list body vars aux-vars aux-vals)
1425 (type (or continuation null) result))
1427 ;; We're about to try to put new blocks into *CURRENT-COMPONENT*.
1428 (aver-live-component *current-component*)
1430 (let* ((bind (make-bind))
1431 (lambda (make-lambda :vars vars
1433 :%source-name source-name
1434 :%debug-name debug-name))
1435 (result (or result (make-continuation))))
1437 ;; just to check: This function should fail internal assertions if
1438 ;; we didn't set up a valid debug name above.
1440 ;; (In SBCL we try to make everything have a debug name, since we
1441 ;; lack the omniscient perspective the original implementors used
1442 ;; to decide which things didn't need one.)
1443 (functional-debug-name lambda)
1445 (setf (lambda-home lambda) lambda)
1447 (new-venv nil cons))
1450 ;; As far as I can see, LAMBDA-VAR-HOME should never have
1451 ;; been set before. Let's make sure. -- WHN 2001-09-29
1452 (aver (null (lambda-var-home var)))
1453 (setf (lambda-var-home var) lambda)
1454 (let ((specvar (lambda-var-specvar var)))
1457 (new-venv (cons (leaf-source-name specvar) specvar)))
1459 (note-lexical-binding (leaf-source-name var))
1460 (new-venv (cons (leaf-source-name var) var))))))
1462 (let ((*lexenv* (make-lexenv :vars (new-venv)
1465 (setf (bind-lambda bind) lambda)
1466 (setf (node-lexenv bind) *lexenv*)
1468 (let ((cont1 (make-continuation))
1469 (cont2 (make-continuation)))
1470 (continuation-starts-block cont1)
1471 (link-node-to-previous-continuation bind cont1)
1472 (use-continuation bind cont2)
1473 (ir1-convert-special-bindings cont2 result body
1474 aux-vars aux-vals (svars)))
1476 (let ((block (continuation-block result)))
1478 (let ((return (make-return :result result :lambda lambda))
1479 (tail-set (make-tail-set :funs (list lambda)))
1480 (dummy (make-continuation)))
1481 (setf (lambda-tail-set lambda) tail-set)
1482 (setf (lambda-return lambda) return)
1483 (setf (continuation-dest result) return)
1484 (setf (block-last block) return)
1485 (link-node-to-previous-continuation return result)
1486 (use-continuation return dummy))
1487 (link-blocks block (component-tail *current-component*))))))
1489 (link-blocks (component-head *current-component*) (node-block bind))
1490 (push lambda (component-new-functionals *current-component*))
1494 ;;; Create the actual entry-point function for an optional entry
1495 ;;; point. The lambda binds copies of each of the VARS, then calls FUN
1496 ;;; with the argument VALS and the DEFAULTS. Presumably the VALS refer
1497 ;;; to the VARS by name. The VALS are passed in in reverse order.
1499 ;;; If any of the copies of the vars are referenced more than once,
1500 ;;; then we mark the corresponding var as EVER-USED to inhibit
1501 ;;; "defined but not read" warnings for arguments that are only used
1502 ;;; by default forms.
1503 (defun convert-optional-entry (fun vars vals defaults)
1504 (declare (type clambda fun) (list vars vals defaults))
1505 (let* ((fvars (reverse vars))
1506 (arg-vars (mapcar (lambda (var)
1507 (unless (lambda-var-specvar var)
1508 (note-lexical-binding (leaf-source-name var)))
1510 :%source-name (leaf-source-name var)
1511 :type (leaf-type var)
1512 :where-from (leaf-where-from var)
1513 :specvar (lambda-var-specvar var)))
1515 (fun (ir1-convert-lambda-body `((%funcall ,fun
1519 :debug-name "&OPTIONAL processor")))
1520 (mapc (lambda (var arg-var)
1521 (when (cdr (leaf-refs arg-var))
1522 (setf (leaf-ever-used var) t)))
1526 ;;; This function deals with supplied-p vars in optional arguments. If
1527 ;;; the there is no supplied-p arg, then we just call
1528 ;;; IR1-CONVERT-HAIRY-ARGS on the remaining arguments, and generate a
1529 ;;; optional entry that calls the result. If there is a supplied-p
1530 ;;; var, then we add it into the default vars and throw a T into the
1531 ;;; entry values. The resulting entry point function is returned.
1532 (defun generate-optional-default-entry (res default-vars default-vals
1533 entry-vars entry-vals
1534 vars supplied-p-p body
1535 aux-vars aux-vals cont
1536 source-name debug-name)
1537 (declare (type optional-dispatch res)
1538 (list default-vars default-vals entry-vars entry-vals vars body
1540 (type (or continuation null) cont))
1541 (let* ((arg (first vars))
1542 (arg-name (leaf-source-name arg))
1543 (info (lambda-var-arg-info arg))
1544 (supplied-p (arg-info-supplied-p info))
1546 (ir1-convert-hairy-args
1548 (list* supplied-p arg default-vars)
1549 (list* (leaf-source-name supplied-p) arg-name default-vals)
1550 (cons arg entry-vars)
1551 (list* t arg-name entry-vals)
1552 (rest vars) t body aux-vars aux-vals cont
1553 source-name debug-name)
1554 (ir1-convert-hairy-args
1556 (cons arg default-vars)
1557 (cons arg-name default-vals)
1558 (cons arg entry-vars)
1559 (cons arg-name entry-vals)
1560 (rest vars) supplied-p-p body aux-vars aux-vals cont
1561 source-name debug-name))))
1563 (convert-optional-entry ep default-vars default-vals
1565 (list (arg-info-default info) nil)
1566 (list (arg-info-default info))))))
1568 ;;; Create the MORE-ENTRY function for the OPTIONAL-DISPATCH RES.
1569 ;;; ENTRY-VARS and ENTRY-VALS describe the fixed arguments. REST is
1570 ;;; the var for any &REST arg. KEYS is a list of the &KEY arg vars.
1572 ;;; The most interesting thing that we do is parse keywords. We create
1573 ;;; a bunch of temporary variables to hold the result of the parse,
1574 ;;; and then loop over the supplied arguments, setting the appropriate
1575 ;;; temps for the supplied keyword. Note that it is significant that
1576 ;;; we iterate over the keywords in reverse order --- this implements
1577 ;;; the CL requirement that (when a keyword appears more than once)
1578 ;;; the first value is used.
1580 ;;; If there is no supplied-p var, then we initialize the temp to the
1581 ;;; default and just pass the temp into the main entry. Since
1582 ;;; non-constant &KEY args are forcibly given a supplied-p var, we
1583 ;;; know that the default is constant, and thus safe to evaluate out
1586 ;;; If there is a supplied-p var, then we create temps for both the
1587 ;;; value and the supplied-p, and pass them into the main entry,
1588 ;;; letting it worry about defaulting.
1590 ;;; We deal with :ALLOW-OTHER-KEYS by delaying unknown keyword errors
1591 ;;; until we have scanned all the keywords.
1592 (defun convert-more-entry (res entry-vars entry-vals rest morep keys)
1593 (declare (type optional-dispatch res) (list entry-vars entry-vals keys))
1594 (collect ((arg-vars)
1595 (arg-vals (reverse entry-vals))
1599 (dolist (var (reverse entry-vars))
1600 (arg-vars (make-lambda-var :%source-name (leaf-source-name var)
1601 :type (leaf-type var)
1602 :where-from (leaf-where-from var))))
1604 (let* ((n-context (gensym "N-CONTEXT-"))
1605 (context-temp (make-lambda-var :%source-name n-context))
1606 (n-count (gensym "N-COUNT-"))
1607 (count-temp (make-lambda-var :%source-name n-count
1608 :type (specifier-type 'index))))
1610 (arg-vars context-temp count-temp)
1613 (arg-vals `(%listify-rest-args ,n-context ,n-count)))
1615 (arg-vals n-context)
1618 (when (optional-dispatch-keyp res)
1619 (let ((n-index (gensym "N-INDEX-"))
1620 (n-key (gensym "N-KEY-"))
1621 (n-value-temp (gensym "N-VALUE-TEMP-"))
1622 (n-allowp (gensym "N-ALLOWP-"))
1623 (n-losep (gensym "N-LOSEP-"))
1624 (allowp (or (optional-dispatch-allowp res)
1625 (policy *lexenv* (zerop safety)))))
1627 (temps `(,n-index (1- ,n-count)) n-key n-value-temp)
1628 (body `(declare (fixnum ,n-index) (ignorable ,n-key ,n-value-temp)))
1632 (let* ((info (lambda-var-arg-info key))
1633 (default (arg-info-default info))
1634 (keyword (arg-info-key info))
1635 (supplied-p (arg-info-supplied-p info))
1636 (n-value (gensym "N-VALUE-")))
1637 (temps `(,n-value ,default))
1639 (let ((n-supplied (gensym "N-SUPPLIED-")))
1641 (arg-vals n-value n-supplied)
1642 (tests `((eq ,n-key ',keyword)
1643 (setq ,n-supplied t)
1644 (setq ,n-value ,n-value-temp)))))
1647 (tests `((eq ,n-key ',keyword)
1648 (setq ,n-value ,n-value-temp)))))))
1651 (temps n-allowp n-losep)
1652 (tests `((eq ,n-key :allow-other-keys)
1653 (setq ,n-allowp ,n-value-temp)))
1655 (setq ,n-losep ,n-key))))
1658 `(when (oddp ,n-count)
1659 (%odd-key-args-error)))
1663 (declare (optimize (safety 0)))
1665 (when (minusp ,n-index) (return))
1666 (setf ,n-value-temp (%more-arg ,n-context ,n-index))
1668 (setq ,n-key (%more-arg ,n-context ,n-index))
1673 (body `(when (and ,n-losep (not ,n-allowp))
1674 (%unknown-key-arg-error ,n-losep)))))))
1676 (let ((ep (ir1-convert-lambda-body
1679 (%funcall ,(optional-dispatch-main-entry res)
1680 . ,(arg-vals)))) ; FIXME: What is the '.'? ,@?
1682 :debug-name (debug-namify "~S processing" '&more))))
1683 (setf (optional-dispatch-more-entry res) ep))))
1687 ;;; This is called by IR1-CONVERT-HAIRY-ARGS when we run into a &REST
1688 ;;; or &KEY arg. The arguments are similar to that function, but we
1689 ;;; split off any &REST arg and pass it in separately. REST is the
1690 ;;; &REST arg var, or NIL if there is no &REST arg. KEYS is a list of
1691 ;;; the &KEY argument vars.
1693 ;;; When there are &KEY arguments, we introduce temporary gensym
1694 ;;; variables to hold the values while keyword defaulting is in
1695 ;;; progress to get the required sequential binding semantics.
1697 ;;; This gets interesting mainly when there are &KEY arguments with
1698 ;;; supplied-p vars or non-constant defaults. In either case, pass in
1699 ;;; a supplied-p var. If the default is non-constant, we introduce an
1700 ;;; IF in the main entry that tests the supplied-p var and decides
1701 ;;; whether to evaluate the default or not. In this case, the real
1702 ;;; incoming value is NIL, so we must union NULL with the declared
1703 ;;; type when computing the type for the main entry's argument.
1704 (defun ir1-convert-more (res default-vars default-vals entry-vars entry-vals
1705 rest more-context more-count keys supplied-p-p
1706 body aux-vars aux-vals cont
1707 source-name debug-name)
1708 (declare (type optional-dispatch res)
1709 (list default-vars default-vals entry-vars entry-vals keys body
1711 (type (or continuation null) cont))
1712 (collect ((main-vars (reverse default-vars))
1713 (main-vals default-vals cons)
1720 (main-vars more-context)
1722 (main-vars more-count)
1726 (let* ((info (lambda-var-arg-info key))
1727 (default (arg-info-default info))
1728 (hairy-default (not (sb!xc:constantp default)))
1729 (supplied-p (arg-info-supplied-p info))
1730 (n-val (make-symbol (format nil
1731 "~A-DEFAULTING-TEMP"
1732 (leaf-source-name key))))
1733 (key-type (leaf-type key))
1734 (val-temp (make-lambda-var
1736 :type (if hairy-default
1737 (type-union key-type (specifier-type 'null))
1739 (main-vars val-temp)
1741 (cond ((or hairy-default supplied-p)
1742 (let* ((n-supplied (gensym "N-SUPPLIED-"))
1743 (supplied-temp (make-lambda-var
1744 :%source-name n-supplied)))
1746 (setf (arg-info-supplied-p info) supplied-temp))
1748 (setf (arg-info-default info) nil))
1749 (main-vars supplied-temp)
1750 (cond (hairy-default
1752 (bind-vals `(if ,n-supplied ,n-val ,default)))
1754 (main-vals default nil)
1757 (bind-vars supplied-p)
1758 (bind-vals n-supplied))))
1760 (main-vals (arg-info-default info))
1761 (bind-vals n-val)))))
1763 (let* ((main-entry (ir1-convert-lambda-body
1765 :aux-vars (append (bind-vars) aux-vars)
1766 :aux-vals (append (bind-vals) aux-vals)
1768 :debug-name (debug-namify "varargs entry for ~A"
1769 (as-debug-name source-name
1771 (last-entry (convert-optional-entry main-entry default-vars
1773 (setf (optional-dispatch-main-entry res) main-entry)
1774 (convert-more-entry res entry-vars entry-vals rest more-context keys)
1776 (push (if supplied-p-p
1777 (convert-optional-entry last-entry entry-vars entry-vals ())
1779 (optional-dispatch-entry-points res))
1782 ;;; This function generates the entry point functions for the
1783 ;;; OPTIONAL-DISPATCH RES. We accomplish this by recursion on the list
1784 ;;; of arguments, analyzing the arglist on the way down and generating
1785 ;;; entry points on the way up.
1787 ;;; DEFAULT-VARS is a reversed list of all the argument vars processed
1788 ;;; so far, including supplied-p vars. DEFAULT-VALS is a list of the
1789 ;;; names of the DEFAULT-VARS.
1791 ;;; ENTRY-VARS is a reversed list of processed argument vars,
1792 ;;; excluding supplied-p vars. ENTRY-VALS is a list things that can be
1793 ;;; evaluated to get the values for all the vars from the ENTRY-VARS.
1794 ;;; It has the var name for each required or optional arg, and has T
1795 ;;; for each supplied-p arg.
1797 ;;; VARS is a list of the LAMBDA-VAR structures for arguments that
1798 ;;; haven't been processed yet. SUPPLIED-P-P is true if a supplied-p
1799 ;;; argument has already been processed; only in this case are the
1800 ;;; DEFAULT-XXX and ENTRY-XXX different.
1802 ;;; The result at each point is a lambda which should be called by the
1803 ;;; above level to default the remaining arguments and evaluate the
1804 ;;; body. We cause the body to be evaluated by converting it and
1805 ;;; returning it as the result when the recursion bottoms out.
1807 ;;; Each level in the recursion also adds its entry point function to
1808 ;;; the result OPTIONAL-DISPATCH. For most arguments, the defaulting
1809 ;;; function and the entry point function will be the same, but when
1810 ;;; SUPPLIED-P args are present they may be different.
1812 ;;; When we run into a &REST or &KEY arg, we punt out to
1813 ;;; IR1-CONVERT-MORE, which finishes for us in this case.
1814 (defun ir1-convert-hairy-args (res default-vars default-vals
1815 entry-vars entry-vals
1816 vars supplied-p-p body aux-vars
1818 source-name debug-name)
1819 (declare (type optional-dispatch res)
1820 (list default-vars default-vals entry-vars entry-vals vars body
1822 (type (or continuation null) cont))
1824 (if (optional-dispatch-keyp res)
1825 ;; Handle &KEY with no keys...
1826 (ir1-convert-more res default-vars default-vals
1827 entry-vars entry-vals
1828 nil nil nil vars supplied-p-p body aux-vars
1829 aux-vals cont source-name debug-name)
1830 (let ((fun (ir1-convert-lambda-body
1831 body (reverse default-vars)
1835 :debug-name (debug-namify
1836 "hairy arg processor for ~A"
1837 (as-debug-name source-name
1839 (setf (optional-dispatch-main-entry res) fun)
1840 (push (if supplied-p-p
1841 (convert-optional-entry fun entry-vars entry-vals ())
1843 (optional-dispatch-entry-points res))
1845 ((not (lambda-var-arg-info (first vars)))
1846 (let* ((arg (first vars))
1847 (nvars (cons arg default-vars))
1848 (nvals (cons (leaf-source-name arg) default-vals)))
1849 (ir1-convert-hairy-args res nvars nvals nvars nvals
1850 (rest vars) nil body aux-vars aux-vals
1852 source-name debug-name)))
1854 (let* ((arg (first vars))
1855 (info (lambda-var-arg-info arg))
1856 (kind (arg-info-kind info)))
1859 (let ((ep (generate-optional-default-entry
1860 res default-vars default-vals
1861 entry-vars entry-vals vars supplied-p-p body
1862 aux-vars aux-vals cont
1863 source-name debug-name)))
1864 (push (if supplied-p-p
1865 (convert-optional-entry ep entry-vars entry-vals ())
1867 (optional-dispatch-entry-points res))
1870 (ir1-convert-more res default-vars default-vals
1871 entry-vars entry-vals
1872 arg nil nil (rest vars) supplied-p-p body
1873 aux-vars aux-vals cont
1874 source-name debug-name))
1876 (ir1-convert-more res default-vars default-vals
1877 entry-vars entry-vals
1878 nil arg (second vars) (cddr vars) supplied-p-p
1879 body aux-vars aux-vals cont
1880 source-name debug-name))
1882 (ir1-convert-more res default-vars default-vals
1883 entry-vars entry-vals
1884 nil nil nil vars supplied-p-p body aux-vars
1885 aux-vals cont source-name debug-name)))))))
1887 ;;; This function deals with the case where we have to make an
1888 ;;; OPTIONAL-DISPATCH to represent a LAMBDA. We cons up the result and
1889 ;;; call IR1-CONVERT-HAIRY-ARGS to do the work. When it is done, we
1890 ;;; figure out the MIN-ARGS and MAX-ARGS.
1891 (defun ir1-convert-hairy-lambda (body vars keyp allowp aux-vars aux-vals cont
1893 (source-name '.anonymous.)
1894 (debug-name (debug-namify
1895 "OPTIONAL-DISPATCH ~S"
1897 (declare (list body vars aux-vars aux-vals) (type continuation cont))
1898 (let ((res (make-optional-dispatch :arglist vars
1901 :%source-name source-name
1902 :%debug-name debug-name))
1903 (min (or (position-if #'lambda-var-arg-info vars) (length vars))))
1904 (aver-live-component *current-component*)
1905 (push res (component-new-functionals *current-component*))
1906 (ir1-convert-hairy-args res () () () () vars nil body aux-vars aux-vals
1907 cont source-name debug-name)
1908 (setf (optional-dispatch-min-args res) min)
1909 (setf (optional-dispatch-max-args res)
1910 (+ (1- (length (optional-dispatch-entry-points res))) min))
1914 (setf (functional-kind ep) :optional)
1915 (setf (leaf-ever-used ep) t)
1916 (setf (lambda-optional-dispatch ep) res))))
1917 (dolist (ep (optional-dispatch-entry-points res)) (frob ep))
1918 (frob (optional-dispatch-more-entry res))
1919 (frob (optional-dispatch-main-entry res)))
1923 ;;; Convert a LAMBDA form into a LAMBDA leaf or an OPTIONAL-DISPATCH leaf.
1924 (defun ir1-convert-lambda (form &key (source-name '.anonymous.) debug-name)
1926 (unless (consp form)
1927 (compiler-error "A ~S was found when expecting a lambda expression:~% ~S"
1930 (unless (eq (car form) 'lambda)
1931 (compiler-error "~S was expected but ~S was found:~% ~S"
1935 (unless (and (consp (cdr form)) (listp (cadr form)))
1937 "The lambda expression has a missing or non-list lambda list:~% ~S"
1940 (multiple-value-bind (vars keyp allow-other-keys aux-vars aux-vals)
1941 (make-lambda-vars (cadr form))
1942 (multiple-value-bind (forms decls) (sb!sys:parse-body (cddr form))
1943 (let* ((result-cont (make-continuation))
1944 (*lexenv* (process-decls decls
1945 (append aux-vars vars)
1947 (res (if (or (find-if #'lambda-var-arg-info vars) keyp)
1948 (ir1-convert-hairy-lambda forms vars keyp
1950 aux-vars aux-vals result-cont
1951 :source-name source-name
1952 :debug-name debug-name)
1953 (ir1-convert-lambda-body forms vars
1957 :source-name source-name
1958 :debug-name debug-name))))
1959 (setf (functional-inline-expansion res) form)
1960 (setf (functional-arg-documentation res) (cadr form))
1963 ;;;; defining global functions
1965 ;;; Convert FUN as a lambda in the null environment, but use the
1966 ;;; current compilation policy. Note that FUN may be a
1967 ;;; LAMBDA-WITH-LEXENV, so we may have to augment the environment to
1968 ;;; reflect the state at the definition site.
1969 (defun ir1-convert-inline-lambda (fun &key
1970 (source-name '.anonymous.)
1972 (destructuring-bind (decls macros symbol-macros &rest body)
1973 (if (eq (car fun) 'lambda-with-lexenv)
1975 `(() () () . ,(cdr fun)))
1976 (let ((*lexenv* (make-lexenv
1977 :default (process-decls decls nil nil
1980 :vars (copy-list symbol-macros)
1981 :funs (mapcar (lambda (x)
1983 (macro . ,(coerce (cdr x) 'function))))
1985 :policy (lexenv-policy *lexenv*))))
1986 (ir1-convert-lambda `(lambda ,@body)
1987 :source-name source-name
1988 :debug-name debug-name))))
1990 ;;; Get a DEFINED-FUN object for a function we are about to
1991 ;;; define. If the function has been forward referenced, then
1992 ;;; substitute for the previous references.
1993 (defun get-defined-fun (name)
1994 (proclaim-as-fun-name name)
1995 (let ((found (find-free-fun name "shouldn't happen! (defined-fun)")))
1996 (note-name-defined name :function)
1997 (cond ((not (defined-fun-p found))
1998 (aver (not (info :function :inlinep name)))
1999 (let* ((where-from (leaf-where-from found))
2000 (res (make-defined-fun
2002 :where-from (if (eq where-from :declared)
2004 :type (leaf-type found))))
2005 (substitute-leaf res found)
2006 (setf (gethash name *free-funs*) res)))
2007 ;; If *FREE-FUNS* has a previously converted definition
2008 ;; for this name, then blow it away and try again.
2009 ((defined-fun-functional found)
2010 (remhash name *free-funs*)
2011 (get-defined-fun name))
2014 ;;; Check a new global function definition for consistency with
2015 ;;; previous declaration or definition, and assert argument/result
2016 ;;; types if appropriate. This assertion is suppressed by the
2017 ;;; EXPLICIT-CHECK attribute, which is specified on functions that
2018 ;;; check their argument types as a consequence of type dispatching.
2019 ;;; This avoids redundant checks such as NUMBERP on the args to +, etc.
2020 (defun assert-new-definition (var fun)
2021 (let ((type (leaf-type var))
2022 (for-real (eq (leaf-where-from var) :declared))
2023 (info (info :function :info (leaf-source-name var))))
2024 (assert-definition-type
2026 ;; KLUDGE: Common Lisp is such a dynamic language that in general
2027 ;; all we can do here in general is issue a STYLE-WARNING. It
2028 ;; would be nice to issue a full WARNING in the special case of
2029 ;; of type mismatches within a compilation unit (as in section
2030 ;; 3.2.2.3 of the spec) but at least as of sbcl-0.6.11, we don't
2031 ;; keep track of whether the mismatched data came from the same
2032 ;; compilation unit, so we can't do that. -- WHN 2001-02-11
2033 :lossage-fun #'compiler-style-warn
2034 :unwinnage-fun (cond (info #'compiler-style-warn)
2035 (for-real #'compiler-note)
2040 (ir1-attributep (fun-info-attributes info)
2043 "previous declaration"
2044 "previous definition"))))
2046 ;;; Convert a lambda doing all the basic stuff we would do if we were
2047 ;;; converting a DEFUN. In the old CMU CL system, this was used both
2048 ;;; by the %DEFUN translator and for global inline expansion, but
2049 ;;; since sbcl-0.pre7.something %DEFUN does things differently.
2050 ;;; FIXME: And now it's probably worth rethinking whether this
2051 ;;; function is a good idea.
2053 ;;; Unless a :INLINE function, we temporarily clobber the inline
2054 ;;; expansion. This prevents recursive inline expansion of
2055 ;;; opportunistic pseudo-inlines.
2056 (defun ir1-convert-lambda-for-defun (lambda var expansion converter)
2057 (declare (cons lambda) (function converter) (type defined-fun var))
2058 (let ((var-expansion (defined-fun-inline-expansion var)))
2059 (unless (eq (defined-fun-inlinep var) :inline)
2060 (setf (defined-fun-inline-expansion var) nil))
2061 (let* ((name (leaf-source-name var))
2062 (fun (funcall converter lambda
2064 (fun-info (info :function :info name)))
2065 (setf (functional-inlinep fun) (defined-fun-inlinep var))
2066 (assert-new-definition var fun)
2067 (setf (defined-fun-inline-expansion var) var-expansion)
2068 ;; If definitely not an interpreter stub, then substitute for
2069 ;; any old references.
2070 (unless (or (eq (defined-fun-inlinep var) :notinline)
2071 (not *block-compile*)
2073 (or (fun-info-transforms fun-info)
2074 (fun-info-templates fun-info)
2075 (fun-info-ir2-convert fun-info))))
2076 (substitute-leaf fun var)
2077 ;; If in a simple environment, then we can allow backward
2078 ;; references to this function from following top level forms.
2079 (when expansion (setf (defined-fun-functional var) fun)))
2082 ;;; the even-at-compile-time part of DEFUN
2084 ;;; The INLINE-EXPANSION is a LAMBDA-WITH-LEXENV, or NIL if there is
2085 ;;; no inline expansion.
2086 (defun %compiler-defun (name lambda-with-lexenv)
2088 (let ((defined-fun nil)) ; will be set below if we're in the compiler
2090 (when (boundp '*lexenv*) ; when in the compiler
2091 (when sb!xc:*compile-print*
2092 (compiler-mumble "~&; recognizing DEFUN ~S~%" name))
2093 (remhash name *free-funs*)
2094 (setf defined-fun (get-defined-fun name)))
2096 (become-defined-fun-name name)
2098 (cond (lambda-with-lexenv
2099 (setf (info :function :inline-expansion-designator name)
2102 (setf (defined-fun-inline-expansion defined-fun)
2103 lambda-with-lexenv)))
2105 (clear-info :function :inline-expansion-designator name)))
2107 ;; old CMU CL comment:
2108 ;; If there is a type from a previous definition, blast it,
2109 ;; since it is obsolete.
2110 (when (and defined-fun
2111 (eq (leaf-where-from defined-fun) :defined))
2112 (setf (leaf-type defined-fun)
2113 ;; FIXME: If this is a block compilation thing, shouldn't
2114 ;; we be setting the type to the full derived type for the
2115 ;; definition, instead of this most general function type?
2116 (specifier-type 'function))))