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 (defun find-free-fun (name context)
128 (declare (string context))
129 (declare (values global-var))
130 (or (let ((old-free-fun (gethash name *free-funs*)))
131 (and (not (invalid-free-fun-p old-free-fun))
133 (ecase (info :function :kind name)
134 ;; FIXME: The :MACRO and :SPECIAL-FORM cases could be merged.
136 (compiler-error "The macro name ~S was found ~A." name context))
138 (compiler-error "The special form name ~S was found ~A."
142 (check-fun-name name)
143 (note-if-setf-fun-and-macro name)
144 (let ((expansion (fun-name-inline-expansion name))
145 (inlinep (info :function :inlinep name)))
146 (setf (gethash name *free-funs*)
147 (if (or expansion inlinep)
150 :inline-expansion expansion
152 :where-from (info :function :where-from name)
153 :type (info :function :type name))
154 (find-free-really-fun name))))))))
156 ;;; Return the LEAF structure for the lexically apparent function
157 ;;; definition of NAME.
158 (declaim (ftype (function (t string) leaf) find-lexically-apparent-fun))
159 (defun find-lexically-apparent-fun (name context)
160 (let ((var (lexenv-find name funs :test #'equal)))
163 (aver (and (consp var) (eq (car var) 'macro)))
164 (compiler-error "found macro name ~S ~A" name context))
167 (find-free-fun name context)))))
169 ;;; Return the LEAF node for a global variable reference to NAME. If
170 ;;; NAME is already entered in *FREE-VARS*, then we just return the
171 ;;; corresponding value. Otherwise, we make a new leaf using
172 ;;; information from the global environment and enter it in
173 ;;; *FREE-VARS*. If the variable is unknown, then we emit a warning.
174 (defun find-free-var (name)
175 (declare (values (or leaf heap-alien-info)))
176 (unless (symbolp name)
177 (compiler-error "Variable name is not a symbol: ~S." name))
178 (or (gethash name *free-vars*)
179 (let ((kind (info :variable :kind name))
180 (type (info :variable :type name))
181 (where-from (info :variable :where-from name)))
182 (when (and (eq where-from :assumed) (eq kind :global))
183 (note-undefined-reference name :variable))
184 (setf (gethash name *free-vars*)
187 (info :variable :alien-info name))
189 (let ((value (info :variable :constant-value name)))
190 (make-constant :value value
192 :type (ctype-of value)
193 :where-from where-from)))
195 (make-global-var :kind kind
198 :where-from where-from)))))))
200 ;;; Grovel over CONSTANT checking for any sub-parts that need to be
201 ;;; processed with MAKE-LOAD-FORM. We have to be careful, because
202 ;;; CONSTANT might be circular. We also check that the constant (and
203 ;;; any subparts) are dumpable at all.
204 (eval-when (:compile-toplevel :load-toplevel :execute)
205 ;; The EVAL-WHEN is necessary for #.(1+ LIST-TO-HASH-TABLE-THRESHOLD)
206 ;; below. -- AL 20010227
207 (defconstant list-to-hash-table-threshold 32))
208 (defun maybe-emit-make-load-forms (constant)
209 (let ((things-processed nil)
211 ;; FIXME: Does this LIST-or-HASH-TABLE messiness give much benefit?
212 (declare (type (or list hash-table) things-processed)
213 (type (integer 0 #.(1+ list-to-hash-table-threshold)) count)
215 (labels ((grovel (value)
216 ;; Unless VALUE is an object which which obviously
217 ;; can't contain other objects
219 '(or #-sb-xc-host unboxed-array
224 (etypecase things-processed
226 (when (member value things-processed :test #'eq)
227 (return-from grovel nil))
228 (push value things-processed)
230 (when (> count list-to-hash-table-threshold)
231 (let ((things things-processed))
232 (setf things-processed
233 (make-hash-table :test 'eq))
234 (dolist (thing things)
235 (setf (gethash thing things-processed) t)))))
237 (when (gethash value things-processed)
238 (return-from grovel nil))
239 (setf (gethash value things-processed) t)))
243 (grovel (cdr value)))
245 (dotimes (i (length value))
246 (grovel (svref value i))))
248 (dotimes (i (length value))
249 (grovel (aref value i))))
251 ;; Even though the (ARRAY T) branch does the exact
252 ;; same thing as this branch we do this separately
253 ;; so that the compiler can use faster versions of
254 ;; array-total-size and row-major-aref.
255 (dotimes (i (array-total-size value))
256 (grovel (row-major-aref value i))))
258 (dotimes (i (array-total-size value))
259 (grovel (row-major-aref value i))))
260 (;; In the target SBCL, we can dump any instance,
261 ;; but in the cross-compilation host,
262 ;; %INSTANCE-FOO functions don't work on general
263 ;; instances, only on STRUCTURE!OBJECTs.
264 #+sb-xc-host structure!object
265 #-sb-xc-host instance
266 (when (emit-make-load-form value)
267 (dotimes (i (%instance-length value))
268 (grovel (%instance-ref value i)))))
271 "Objects of type ~S can't be dumped into fasl files."
272 (type-of value)))))))
276 ;;;; some flow-graph hacking utilities
278 ;;; This function sets up the back link between the node and the
279 ;;; continuation which continues at it.
280 (defun link-node-to-previous-continuation (node cont)
281 (declare (type node node) (type continuation cont))
282 (aver (not (continuation-next cont)))
283 (setf (continuation-next cont) node)
284 (setf (node-prev node) cont))
286 ;;; This function is used to set the continuation for a node, and thus
287 ;;; determine what receives the value and what is evaluated next. If
288 ;;; the continuation has no block, then we make it be in the block
289 ;;; that the node is in. If the continuation heads its block, we end
290 ;;; our block and link it to that block. If the continuation is not
291 ;;; currently used, then we set the DERIVED-TYPE for the continuation
292 ;;; to that of the node, so that a little type propagation gets done.
294 ;;; We also deal with a bit of THE's semantics here: we weaken the
295 ;;; assertion on CONT to be no stronger than the assertion on CONT in
296 ;;; our scope. See the IR1-CONVERT method for THE.
297 #!-sb-fluid (declaim (inline use-continuation))
298 (defun use-continuation (node cont)
299 (declare (type node node) (type continuation cont))
300 (let ((node-block (continuation-block (node-prev node))))
301 (case (continuation-kind cont)
303 (setf (continuation-block cont) node-block)
304 (setf (continuation-kind cont) :inside-block)
305 (setf (continuation-use cont) node)
306 (setf (node-cont node) cont))
308 (%use-continuation node cont)))))
309 (defun %use-continuation (node cont)
310 (declare (type node node) (type continuation cont) (inline member))
311 (let ((block (continuation-block cont))
312 (node-block (continuation-block (node-prev node))))
313 (aver (eq (continuation-kind cont) :block-start))
314 (when (block-last node-block)
315 (error "~S has already ended." node-block))
316 (setf (block-last node-block) node)
317 (when (block-succ node-block)
318 (error "~S already has successors." node-block))
319 (setf (block-succ node-block) (list block))
320 (when (memq node-block (block-pred block))
321 (error "~S is already a predecessor of ~S." node-block block))
322 (push node-block (block-pred block))
323 (add-continuation-use node cont)
324 (unless (eq (continuation-asserted-type cont) *wild-type*)
325 (let ((new (values-type-union (continuation-asserted-type cont)
326 (or (lexenv-find cont type-restrictions)
328 (when (type/= new (continuation-asserted-type cont))
329 (setf (continuation-asserted-type cont) new)
330 (reoptimize-continuation cont))))))
332 ;;;; exported functions
334 ;;; This function takes a form and the top level form number for that
335 ;;; form, and returns a lambda representing the translation of that
336 ;;; form in the current global environment. The returned lambda is a
337 ;;; top level lambda that can be called to cause evaluation of the
338 ;;; forms. This lambda is in the initial component. If FOR-VALUE is T,
339 ;;; then the value of the form is returned from the function,
340 ;;; otherwise NIL is returned.
342 ;;; This function may have arbitrary effects on the global environment
343 ;;; due to processing of EVAL-WHENs. All syntax error checking is
344 ;;; done, with erroneous forms being replaced by a proxy which signals
345 ;;; an error if it is evaluated. Warnings about possibly inconsistent
346 ;;; or illegal changes to the global environment will also be given.
348 ;;; We make the initial component and convert the form in a PROGN (and
349 ;;; an optional NIL tacked on the end.) We then return the lambda. We
350 ;;; bind all of our state variables here, rather than relying on the
351 ;;; global value (if any) so that IR1 conversion will be reentrant.
352 ;;; This is necessary for EVAL-WHEN processing, etc.
354 ;;; The hashtables used to hold global namespace info must be
355 ;;; reallocated elsewhere. Note also that *LEXENV* is not bound, so
356 ;;; that local macro definitions can be introduced by enclosing code.
357 (defun ir1-toplevel (form path for-value)
358 (declare (list path))
359 (let* ((*current-path* path)
360 (component (make-empty-component))
361 (*current-component* component))
362 (setf (component-name component) "initial component")
363 (setf (component-kind component) :initial)
364 (let* ((forms (if for-value `(,form) `(,form nil)))
365 (res (ir1-convert-lambda-body
367 :debug-name (debug-namify "top level form ~S" form))))
368 (setf (functional-entry-fun res) res
369 (functional-arg-documentation res) ()
370 (functional-kind res) :toplevel)
373 ;;; *CURRENT-FORM-NUMBER* is used in FIND-SOURCE-PATHS to compute the
374 ;;; form number to associate with a source path. This should be bound
375 ;;; to an initial value of 0 before the processing of each truly
377 (declaim (type index *current-form-number*))
378 (defvar *current-form-number*)
380 ;;; This function is called on freshly read forms to record the
381 ;;; initial location of each form (and subform.) Form is the form to
382 ;;; find the paths in, and TLF-NUM is the top level form number of the
383 ;;; truly top level form.
385 ;;; This gets a bit interesting when the source code is circular. This
386 ;;; can (reasonably?) happen in the case of circular list constants.
387 (defun find-source-paths (form tlf-num)
388 (declare (type index tlf-num))
389 (let ((*current-form-number* 0))
390 (sub-find-source-paths form (list tlf-num)))
392 (defun sub-find-source-paths (form path)
393 (unless (gethash form *source-paths*)
394 (setf (gethash form *source-paths*)
395 (list* 'original-source-start *current-form-number* path))
396 (incf *current-form-number*)
400 (declare (fixnum pos))
403 (when (atom subform) (return))
404 (let ((fm (car subform)))
406 (sub-find-source-paths fm (cons pos path)))
408 (setq subform (cdr subform))
409 (when (eq subform trail) (return)))))
413 (setq trail (cdr trail)))))))
415 ;;;; IR1-CONVERT, macroexpansion and special form dispatching
417 (macrolet (;; Bind *COMPILER-ERROR-BAILOUT* to a function that throws
418 ;; out of the body and converts a proxy form instead.
419 (ir1-error-bailout ((start
423 (proxy ``(error "execution of a form compiled with errors:~% ~S"
426 (let ((skip (gensym "SKIP")))
428 (catch 'ir1-error-abort
429 (let ((*compiler-error-bailout*
431 (throw 'ir1-error-abort nil))))
433 (return-from ,skip nil)))
434 (ir1-convert ,start ,cont ,proxy)))))
436 ;; Translate FORM into IR1. The code is inserted as the NEXT of the
437 ;; continuation START. CONT is the continuation which receives the
438 ;; value of the FORM to be translated. The translators call this
439 ;; function recursively to translate their subnodes.
441 ;; As a special hack to make life easier in the compiler, a LEAF
442 ;; IR1-converts into a reference to that LEAF structure. This allows
443 ;; the creation using backquote of forms that contain leaf
444 ;; references, without having to introduce dummy names into the
446 (declaim (ftype (function (continuation continuation t) (values)) ir1-convert))
447 (defun ir1-convert (start cont form)
448 (ir1-error-bailout (start cont form)
449 (let ((*current-path* (or (gethash form *source-paths*)
450 (cons form *current-path*))))
452 (cond ((and (symbolp form) (not (keywordp form)))
453 (ir1-convert-var start cont form))
455 (reference-leaf start cont form))
457 (reference-constant start cont form)))
458 (let ((opname (car form)))
459 (cond ((symbolp opname)
460 (let ((lexical-def (lexenv-find opname funs)))
461 (typecase lexical-def
462 (null (ir1-convert-global-functoid start cont form))
464 (ir1-convert-local-combination start
469 (ir1-convert-srctran start cont lexical-def form))
471 (aver (and (consp lexical-def)
472 (eq (car lexical-def) 'macro)))
473 (ir1-convert start cont
474 (careful-expand-macro (cdr lexical-def)
476 ((or (atom opname) (not (eq (car opname) 'lambda)))
477 (compiler-error "illegal function call"))
479 ;; implicitly (LAMBDA ..) because the LAMBDA
480 ;; expression is the CAR of an executed form
481 (ir1-convert-combination start
486 :debug-name (debug-namify
491 ;; Generate a reference to a manifest constant, creating a new leaf
492 ;; if necessary. If we are producing a fasl file, make sure that
493 ;; MAKE-LOAD-FORM gets used on any parts of the constant that it
495 (defun reference-constant (start cont value)
496 (declare (type continuation start cont)
497 (inline find-constant))
499 (start cont value '(error "attempt to reference undumpable constant"))
500 (when (producing-fasl-file)
501 (maybe-emit-make-load-forms value))
502 (let* ((leaf (find-constant value))
503 (res (make-ref (leaf-type leaf) leaf)))
504 (push res (leaf-refs leaf))
505 (link-node-to-previous-continuation res start)
506 (use-continuation res cont)))
509 ;;; Add FUNCTIONAL to the COMPONENT-REANALYZE-FUNCTIONALS, unless it's
510 ;;; some trivial type for which reanalysis is a trivial no-op, or
511 ;;; unless it doesn't belong in this component at all.
513 ;;; FUNCTIONAL is returned.
514 (defun maybe-reanalyze-functional (functional)
516 (aver (not (eql (functional-kind functional) :deleted))) ; bug 148
517 (aver-live-component *current-component*)
519 ;; When FUNCTIONAL is of a type for which reanalysis isn't a trivial
521 (when (typep functional '(or optional-dispatch clambda))
523 ;; When FUNCTIONAL knows its component
524 (when (lambda-p functional)
525 (aver (eql (lambda-component functional) *current-component*)))
528 (component-reanalyze-functionals *current-component*)))
532 ;;; Generate a REF node for LEAF, frobbing the LEAF structure as
533 ;;; needed. If LEAF represents a defined function which has already
534 ;;; been converted, and is not :NOTINLINE, then reference the
535 ;;; functional instead.
536 (defun reference-leaf (start cont leaf)
537 (declare (type continuation start cont) (type leaf leaf))
538 (let* ((leaf (or (and (defined-fun-p leaf)
539 (not (eq (defined-fun-inlinep leaf)
541 (let ((functional (defined-fun-functional leaf)))
542 (when (and functional
543 (not (functional-kind functional)))
544 (maybe-reanalyze-functional functional))))
546 (res (make-ref (or (lexenv-find leaf type-restrictions)
549 (push res (leaf-refs leaf))
550 (setf (leaf-ever-used leaf) t)
551 (link-node-to-previous-continuation res start)
552 (use-continuation res cont)))
554 ;;; Convert a reference to a symbolic constant or variable. If the
555 ;;; symbol is entered in the LEXENV-VARS we use that definition,
556 ;;; otherwise we find the current global definition. This is also
557 ;;; where we pick off symbol macro and alien variable references.
558 (defun ir1-convert-var (start cont name)
559 (declare (type continuation start cont) (symbol name))
560 (let ((var (or (lexenv-find name vars) (find-free-var name))))
563 (when (lambda-var-p var)
564 (let ((home (continuation-home-lambda-or-null start)))
566 (pushnew var (lambda-calls-or-closes home))))
567 (when (lambda-var-ignorep var)
568 ;; (ANSI's specification for the IGNORE declaration requires
569 ;; that this be a STYLE-WARNING, not a full WARNING.)
570 (compiler-style-warn "reading an ignored variable: ~S" name)))
571 (reference-leaf start cont var))
573 (aver (eq (car var) 'MACRO))
574 (ir1-convert start cont (cdr var)))
576 (ir1-convert start cont `(%heap-alien ',var)))))
579 ;;; Convert anything that looks like a special form, global function
581 (defun ir1-convert-global-functoid (start cont form)
582 (declare (type continuation start cont) (list form))
583 (let* ((fun (first form))
584 (translator (info :function :ir1-convert fun))
585 (cmacro (info :function :compiler-macro-function fun)))
586 (cond (translator (funcall translator start cont form))
588 (not (eq (info :function :inlinep fun)
590 (let ((res (careful-expand-macro cmacro form)))
592 (ir1-convert-global-functoid-no-cmacro start cont form fun)
593 (ir1-convert start cont res))))
595 (ir1-convert-global-functoid-no-cmacro start cont form fun)))))
597 ;;; Handle the case of where the call was not a compiler macro, or was
598 ;;; a compiler macro and passed.
599 (defun ir1-convert-global-functoid-no-cmacro (start cont form fun)
600 (declare (type continuation start cont) (list form))
601 ;; FIXME: Couldn't all the INFO calls here be converted into
602 ;; standard CL functions, like MACRO-FUNCTION or something?
603 ;; And what happens with lexically-defined (MACROLET) macros
605 (ecase (info :function :kind fun)
609 (careful-expand-macro (info :function :macro-function fun)
612 (ir1-convert-srctran start
614 (find-free-fun fun "shouldn't happen! (no-cmacro)")
617 (defun muffle-warning-or-die ()
619 (bug "no MUFFLE-WARNING restart"))
621 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
622 ;;; errors which occur during the macroexpansion.
623 (defun careful-expand-macro (fun form)
624 (let (;; a hint I (WHN) wish I'd known earlier
625 (hint "(hint: For more precise location, try *BREAK-ON-SIGNALS*.)"))
626 (flet (;; Return a string to use as a prefix in error reporting,
627 ;; telling something about which form caused the problem.
629 (let ((*print-pretty* nil)
630 ;; We rely on the printer to abbreviate FORM.
635 #-sb-xc-host "(in macroexpansion of ~S)"
636 ;; longer message to avoid ambiguity "Was it the xc host
637 ;; or the cross-compiler which encountered the problem?"
638 #+sb-xc-host "(in cross-compiler macroexpansion of ~S)"
640 (handler-bind (;; When cross-compiling, we can get style warnings
641 ;; about e.g. undefined functions. An unhandled
642 ;; CL:STYLE-WARNING (as opposed to a
643 ;; SB!C::COMPILER-NOTE) would cause FAILURE-P to be
644 ;; set on the return from #'SB!XC:COMPILE-FILE, which
645 ;; would falsely indicate an error sufficiently
646 ;; serious that we should stop the build process. To
647 ;; avoid this, we translate CL:STYLE-WARNING
648 ;; conditions from the host Common Lisp into
649 ;; cross-compiler SB!C::COMPILER-NOTE calls. (It
650 ;; might be cleaner to just make Python use
651 ;; CL:STYLE-WARNING internally, so that the
652 ;; significance of any host Common Lisp
653 ;; CL:STYLE-WARNINGs is understood automatically. But
654 ;; for now I'm not motivated to do this. -- WHN
656 (style-warning (lambda (c)
657 (compiler-note "~@<~A~:@_~A~:@_~A~:>"
658 (wherestring) hint c)
659 (muffle-warning-or-die)))
660 ;; KLUDGE: CMU CL in its wisdom (version 2.4.6 for
661 ;; Debian Linux, anyway) raises a CL:WARNING
662 ;; condition (not a CL:STYLE-WARNING) for undefined
663 ;; symbols when converting interpreted functions,
664 ;; causing COMPILE-FILE to think the file has a real
665 ;; problem, causing COMPILE-FILE to return FAILURE-P
666 ;; set (not just WARNINGS-P set). Since undefined
667 ;; symbol warnings are often harmless forward
668 ;; references, and since it'd be inordinately painful
669 ;; to try to eliminate all such forward references,
670 ;; these warnings are basically unavoidable. Thus, we
671 ;; need to coerce the system to work through them,
672 ;; and this code does so, by crudely suppressing all
673 ;; warnings in cross-compilation macroexpansion. --
680 ~@<(KLUDGE: That was a non-STYLE WARNING. ~
681 Ordinarily that would cause compilation to ~
682 fail. However, since we're running under ~
683 CMU CL, and since CMU CL emits non-STYLE ~
684 warnings for safe, hard-to-fix things (e.g. ~
685 references to not-yet-defined functions) ~
686 we're going to have to ignore it and ~
687 proceed anyway. Hopefully we're not ~
688 ignoring anything horrible here..)~:@>~:>"
691 (muffle-warning-or-die)))
693 (compiler-error "~@<~A~:@_~A~@:_~A~:>"
694 (wherestring) hint c))))
695 (funcall sb!xc:*macroexpand-hook* fun form *lexenv*)))))
697 ;;;; conversion utilities
699 ;;; Convert a bunch of forms, discarding all the values except the
700 ;;; last. If there aren't any forms, then translate a NIL.
701 (declaim (ftype (function (continuation continuation list) (values))
702 ir1-convert-progn-body))
703 (defun ir1-convert-progn-body (start cont body)
705 (reference-constant start cont nil)
706 (let ((this-start start)
709 (let ((form (car forms)))
710 (when (endp (cdr forms))
711 (ir1-convert this-start cont form)
713 (let ((this-cont (make-continuation)))
714 (ir1-convert this-start this-cont form)
715 (setq this-start this-cont
716 forms (cdr forms)))))))
719 ;;;; converting combinations
721 ;;; Convert a function call where the function FUN is a LEAF. FORM is
722 ;;; the source for the call. We return the COMBINATION node so that
723 ;;; the caller can poke at it if it wants to.
724 (declaim (ftype (function (continuation continuation list leaf) combination)
725 ir1-convert-combination))
726 (defun ir1-convert-combination (start cont form fun)
727 (let ((fun-cont (make-continuation)))
728 (reference-leaf start fun-cont fun)
729 (ir1-convert-combination-args fun-cont cont (cdr form))))
731 ;;; Convert the arguments to a call and make the COMBINATION
732 ;;; node. FUN-CONT is the continuation which yields the function to
733 ;;; call. ARGS is the list of arguments for the call, which defaults
734 ;;; to the cdr of source. We return the COMBINATION node.
735 (defun ir1-convert-combination-args (fun-cont cont args)
736 (declare (type continuation fun-cont cont) (list args))
737 (let ((node (make-combination fun-cont)))
738 (setf (continuation-dest fun-cont) node)
739 (assert-continuation-type fun-cont
740 (specifier-type '(or function symbol)))
741 (collect ((arg-conts))
742 (let ((this-start fun-cont))
744 (let ((this-cont (make-continuation node)))
745 (ir1-convert this-start this-cont arg)
746 (setq this-start this-cont)
747 (arg-conts this-cont)))
748 (link-node-to-previous-continuation node this-start)
749 (use-continuation node cont)
750 (setf (combination-args node) (arg-conts))))
753 ;;; Convert a call to a global function. If not :NOTINLINE, then we do
754 ;;; source transforms and try out any inline expansion. If there is no
755 ;;; expansion, but is :INLINE, then give an efficiency note (unless a
756 ;;; known function which will quite possibly be open-coded.) Next, we
757 ;;; go to ok-combination conversion.
758 (defun ir1-convert-srctran (start cont var form)
759 (declare (type continuation start cont) (type global-var var))
760 (let ((inlinep (when (defined-fun-p var)
761 (defined-fun-inlinep var))))
762 (if (eq inlinep :notinline)
763 (ir1-convert-combination start cont form var)
764 (let ((transform (info :function
766 (leaf-source-name var))))
768 (multiple-value-bind (result pass) (funcall transform form)
770 (ir1-convert-maybe-predicate start cont form var)
771 (ir1-convert start cont result)))
772 (ir1-convert-maybe-predicate start cont form var))))))
774 ;;; If the function has the PREDICATE attribute, and the CONT's DEST
775 ;;; isn't an IF, then we convert (IF <form> T NIL), ensuring that a
776 ;;; predicate always appears in a conditional context.
778 ;;; If the function isn't a predicate, then we call
779 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
780 (defun ir1-convert-maybe-predicate (start cont form var)
781 (declare (type continuation start cont) (list form) (type global-var var))
782 (let ((info (info :function :info (leaf-source-name var))))
784 (ir1-attributep (fun-info-attributes info) predicate)
785 (not (if-p (continuation-dest cont))))
786 (ir1-convert start cont `(if ,form t nil))
787 (ir1-convert-combination-checking-type start cont form var))))
789 ;;; Actually really convert a global function call that we are allowed
792 ;;; If we know the function type of the function, then we check the
793 ;;; call for syntactic legality with respect to the declared function
794 ;;; type. If it is impossible to determine whether the call is correct
795 ;;; due to non-constant keywords, then we give up, marking the call as
796 ;;; :FULL to inhibit further error messages. We return true when the
799 ;;; If the call is legal, we also propagate type assertions from the
800 ;;; function type to the arg and result continuations. We do this now
801 ;;; so that IR1 optimize doesn't have to redundantly do the check
802 ;;; later so that it can do the type propagation.
803 (defun ir1-convert-combination-checking-type (start cont form var)
804 (declare (type continuation start cont) (list form) (type leaf var))
805 (let* ((node (ir1-convert-combination start cont form var))
806 (fun-cont (basic-combination-fun node))
807 (type (leaf-type var)))
808 (when (validate-call-type node type t)
809 (setf (continuation-%derived-type fun-cont) type)
810 (setf (continuation-reoptimize fun-cont) nil)
811 (setf (continuation-%type-check fun-cont) nil)))
814 ;;; Convert a call to a local function, or if the function has already
815 ;;; been LET converted, then throw FUNCTIONAL to
816 ;;; LOCALL-ALREADY-LET-CONVERTED. The THROW should only happen when we
817 ;;; are converting inline expansions for local functions during
819 (defun ir1-convert-local-combination (start cont form functional)
821 ;; The test here is for "when LET converted", as a translation of
822 ;; the old CMU CL comments into code. Unfortunately, the old CMU CL
823 ;; comments aren't specific enough to tell whether the correct
824 ;; translation is FUNCTIONAL-SOMEWHAT-LETLIKE-P or
825 ;; FUNCTIONAL-LETLIKE-P or what. The old CMU CL code assumed that
826 ;; any non-null FUNCTIONAL-KIND meant that the function "had been
827 ;; LET converted", which might even be right, but seems fragile, so
828 ;; we try to be pickier.
830 ;; looks LET-converted
831 (functional-somewhat-letlike-p functional)
832 ;; It's possible for a LET-converted function to end up
833 ;; deleted later. In that case, for the purposes of this
834 ;; analysis, it is LET-converted: LET-converted functionals
835 ;; are too badly trashed to expand them inline, and deleted
836 ;; LET-converted functionals are even worse.
837 (eql (functional-kind functional) :deleted))
838 (throw 'locall-already-let-converted functional))
839 ;; Any other non-NIL KIND value is a case we haven't found a
840 ;; justification for, and at least some such values (e.g. :EXTERNAL
841 ;; and :TOPLEVEL) seem obviously wrong.
842 (aver (null (functional-kind functional)))
844 (ir1-convert-combination start
847 (maybe-reanalyze-functional functional)))
851 ;;; Given a list of LAMBDA-VARs and a variable name, return the
852 ;;; LAMBDA-VAR for that name, or NIL if it isn't found. We return the
853 ;;; *last* variable with that name, since LET* bindings may be
854 ;;; duplicated, and declarations always apply to the last.
855 (declaim (ftype (function (list symbol) (or lambda-var list))
857 (defun find-in-bindings (vars name)
861 (when (eq (leaf-source-name var) name)
863 (let ((info (lambda-var-arg-info var)))
865 (let ((supplied-p (arg-info-supplied-p info)))
866 (when (and supplied-p
867 (eq (leaf-source-name supplied-p) name))
868 (setq found supplied-p))))))
869 ((and (consp var) (eq (car var) name))
870 (setf found (cdr var)))))
873 ;;; Called by PROCESS-DECLS to deal with a variable type declaration.
874 ;;; If a LAMBDA-VAR being bound, we intersect the type with the var's
875 ;;; type, otherwise we add a type restriction on the var. If a symbol
876 ;;; macro, we just wrap a THE around the expansion.
877 (defun process-type-decl (decl res vars)
878 (declare (list decl vars) (type lexenv res))
879 (let ((type (specifier-type (first decl))))
880 (collect ((restr nil cons)
882 (dolist (var-name (rest decl))
883 (let* ((bound-var (find-in-bindings vars var-name))
885 (lexenv-find var-name vars)
886 (find-free-var var-name))))
889 (let* ((old-type (or (lexenv-find var type-restrictions)
891 (int (if (or (fun-type-p type)
892 (fun-type-p old-type))
894 (type-approx-intersection2 old-type type))))
895 (cond ((eq int *empty-type*)
896 (unless (policy *lexenv* (= inhibit-warnings 3))
898 "The type declarations ~S and ~S for ~S conflict."
899 (type-specifier old-type) (type-specifier type)
901 (bound-var (setf (leaf-type bound-var) int))
903 (restr (cons var int))))))
905 ;; FIXME: non-ANSI weirdness
906 (aver (eq (car var) 'MACRO))
907 (new-vars `(,var-name . (MACRO . (the ,(first decl)
911 "~S is an alien variable, so its type can't be declared."
914 (if (or (restr) (new-vars))
915 (make-lexenv :default res
916 :type-restrictions (restr)
920 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
921 ;;; declarations for function variables. In addition to allowing
922 ;;; declarations for functions being bound, we must also deal with
923 ;;; declarations that constrain the type of lexically apparent
925 (defun process-ftype-decl (spec res names fvars)
926 (declare (list spec names fvars) (type lexenv res))
927 (let ((type (specifier-type spec)))
928 (collect ((res nil cons))
930 (let ((found (find name fvars
931 :key #'leaf-source-name
935 (setf (leaf-type found) type)
936 (assert-definition-type found type
937 :unwinnage-fun #'compiler-note
938 :where "FTYPE declaration"))
940 (res (cons (find-lexically-apparent-fun
941 name "in a function type declaration")
944 (make-lexenv :default res :type-restrictions (res))
947 ;;; Process a special declaration, returning a new LEXENV. A non-bound
948 ;;; special declaration is instantiated by throwing a special variable
949 ;;; into the variables.
950 (defun process-special-decl (spec res vars)
951 (declare (list spec vars) (type lexenv res))
952 (collect ((new-venv nil cons))
953 (dolist (name (cdr spec))
954 (let ((var (find-in-bindings vars name)))
957 (aver (eq (car var) 'MACRO))
959 "~S is a symbol-macro and thus can't be declared special."
962 (when (lambda-var-ignorep var)
963 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
964 ;; requires that this be a STYLE-WARNING, not a full WARNING.
966 "The ignored variable ~S is being declared special."
968 (setf (lambda-var-specvar var)
969 (specvar-for-binding name)))
971 (unless (assoc name (new-venv) :test #'eq)
972 (new-venv (cons name (specvar-for-binding name))))))))
974 (make-lexenv :default res :vars (new-venv))
977 ;;; Return a DEFINED-FUN which copies a GLOBAL-VAR but for its INLINEP.
978 (defun make-new-inlinep (var inlinep)
979 (declare (type global-var var) (type inlinep inlinep))
980 (let ((res (make-defined-fun
981 :%source-name (leaf-source-name var)
982 :where-from (leaf-where-from var)
983 :type (leaf-type var)
985 (when (defined-fun-p var)
986 (setf (defined-fun-inline-expansion res)
987 (defined-fun-inline-expansion var))
988 (setf (defined-fun-functional res)
989 (defined-fun-functional var)))
992 ;;; Parse an inline/notinline declaration. If it's a local function we're
993 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
994 (defun process-inline-decl (spec res fvars)
995 (let ((sense (cdr (assoc (first spec) *inlinep-translations* :test #'eq)))
997 (dolist (name (rest spec))
998 (let ((fvar (find name fvars
999 :key #'leaf-source-name
1002 (setf (functional-inlinep fvar) sense)
1004 (find-lexically-apparent-fun
1005 name "in an inline or notinline declaration")))
1008 (when (policy *lexenv* (>= speed inhibit-warnings))
1009 (compiler-note "ignoring ~A declaration not at ~
1010 definition of local function:~% ~S"
1013 (push (cons name (make-new-inlinep found sense))
1017 (make-lexenv :default res :funs new-fenv)
1020 ;;; like FIND-IN-BINDINGS, but looks for #'FOO in the FVARS
1021 (defun find-in-bindings-or-fbindings (name vars fvars)
1022 (declare (list vars fvars))
1024 (destructuring-bind (wot fn-name) name
1025 (unless (eq wot 'function)
1026 (compiler-error "The function or variable name ~S is unrecognizable."
1028 (find fn-name fvars :key #'leaf-source-name :test #'equal))
1029 (find-in-bindings vars name)))
1031 ;;; Process an ignore/ignorable declaration, checking for various losing
1033 (defun process-ignore-decl (spec vars fvars)
1034 (declare (list spec vars fvars))
1035 (dolist (name (rest spec))
1036 (let ((var (find-in-bindings-or-fbindings name vars fvars)))
1039 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1040 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1041 (compiler-style-warn "declaring unknown variable ~S to be ignored"
1043 ;; FIXME: This special case looks like non-ANSI weirdness.
1044 ((and (consp var) (consp (cdr var)) (eq (cadr var) 'macro))
1045 ;; Just ignore the IGNORE decl.
1048 (setf (leaf-ever-used var) t))
1049 ((lambda-var-specvar var)
1050 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1051 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1052 (compiler-style-warn "declaring special variable ~S to be ignored"
1054 ((eq (first spec) 'ignorable)
1055 (setf (leaf-ever-used var) t))
1057 (setf (lambda-var-ignorep var) t)))))
1060 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
1061 ;;; go away, I think.
1062 (defvar *suppress-values-declaration* nil
1064 "If true, processing of the VALUES declaration is inhibited.")
1066 ;;; Process a single declaration spec, augmenting the specified LEXENV
1067 ;;; RES and returning it as a result. VARS and FVARS are as described in
1069 (defun process-1-decl (raw-spec res vars fvars cont)
1070 (declare (type list raw-spec vars fvars))
1071 (declare (type lexenv res))
1072 (declare (type continuation cont))
1073 (let ((spec (canonized-decl-spec raw-spec)))
1075 (special (process-special-decl spec res vars))
1078 (compiler-error "no type specified in FTYPE declaration: ~S" spec))
1079 (process-ftype-decl (second spec) res (cddr spec) fvars))
1080 ((inline notinline maybe-inline)
1081 (process-inline-decl spec res fvars))
1083 (process-ignore-decl spec vars fvars)
1088 :policy (process-optimize-decl spec (lexenv-policy res))))
1090 (process-type-decl (cdr spec) res vars))
1092 (if *suppress-values-declaration*
1094 (let ((types (cdr spec)))
1095 (ir1ize-the-or-values (if (eql (length types) 1)
1102 (when (policy *lexenv* (> speed inhibit-warnings))
1104 "compiler limitation: ~
1105 ~% There's no special support for DYNAMIC-EXTENT (so it's ignored)."))
1108 (unless (info :declaration :recognized (first spec))
1109 (compiler-warn "unrecognized declaration ~S" raw-spec))
1112 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1113 ;;; and FUNCTIONAL structures which are being bound. In addition to
1114 ;;; filling in slots in the leaf structures, we return a new LEXENV
1115 ;;; which reflects pervasive special and function type declarations,
1116 ;;; (NOT)INLINE declarations and OPTIMIZE declarations. CONT is the
1117 ;;; continuation affected by VALUES declarations.
1119 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1121 (defun process-decls (decls vars fvars cont &optional (env *lexenv*))
1122 (declare (list decls vars fvars) (type continuation cont))
1123 (dolist (decl decls)
1124 (dolist (spec (rest decl))
1125 (unless (consp spec)
1126 (compiler-error "malformed declaration specifier ~S in ~S" spec decl))
1127 (setq env (process-1-decl spec env vars fvars cont))))
1130 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1131 ;;; declaration. If there is a global variable of that name, then
1132 ;;; check that it isn't a constant and return it. Otherwise, create an
1133 ;;; anonymous GLOBAL-VAR.
1134 (defun specvar-for-binding (name)
1135 (cond ((not (eq (info :variable :where-from name) :assumed))
1136 (let ((found (find-free-var name)))
1137 (when (heap-alien-info-p found)
1139 "~S is an alien variable and so can't be declared special."
1141 (unless (global-var-p found)
1143 "~S is a constant and so can't be declared special."
1147 (make-global-var :kind :special
1149 :where-from :declared))))
1153 ;;;; Note: Take a look at the compiler-overview.tex section on "Hairy
1154 ;;;; function representation" before you seriously mess with this
1157 ;;; Verify that the NAME is a legal name for a variable and return a
1158 ;;; VAR structure for it, filling in info if it is globally special.
1159 ;;; If it is losing, we punt with a COMPILER-ERROR. NAMES-SO-FAR is a
1160 ;;; list of names which have previously been bound. If the NAME is in
1161 ;;; this list, then we error out.
1162 (declaim (ftype (function (t list) lambda-var) varify-lambda-arg))
1163 (defun varify-lambda-arg (name names-so-far)
1164 (declare (inline member))
1165 (unless (symbolp name)
1166 (compiler-error "The lambda variable ~S is not a symbol." name))
1167 (when (member name names-so-far :test #'eq)
1168 (compiler-error "The variable ~S occurs more than once in the lambda-list."
1170 (let ((kind (info :variable :kind name)))
1171 (when (or (keywordp name) (eq kind :constant))
1172 (compiler-error "The name of the lambda variable ~S is already in use to name a constant."
1174 (cond ((eq kind :special)
1175 (let ((specvar (find-free-var name)))
1176 (make-lambda-var :%source-name name
1177 :type (leaf-type specvar)
1178 :where-from (leaf-where-from specvar)
1181 (note-lexical-binding name)
1182 (make-lambda-var :%source-name name)))))
1184 ;;; Make the default keyword for a &KEY arg, checking that the keyword
1185 ;;; isn't already used by one of the VARS. We also check that the
1186 ;;; keyword isn't the magical :ALLOW-OTHER-KEYS.
1187 (declaim (ftype (function (symbol list t) keyword) make-keyword-for-arg))
1188 (defun make-keyword-for-arg (symbol vars keywordify)
1189 (let ((key (if (and keywordify (not (keywordp symbol)))
1190 (keywordicate symbol)
1192 (when (eq key :allow-other-keys)
1193 (compiler-error "No &KEY arg can be called :ALLOW-OTHER-KEYS."))
1195 (let ((info (lambda-var-arg-info var)))
1197 (eq (arg-info-kind info) :keyword)
1198 (eq (arg-info-key info) key))
1200 "The keyword ~S appears more than once in the lambda-list."
1204 ;;; Parse a lambda list into a list of VAR structures, stripping off
1205 ;;; any &AUX bindings. Each arg name is checked for legality, and
1206 ;;; duplicate names are checked for. If an arg is globally special,
1207 ;;; the var is marked as :SPECIAL instead of :LEXICAL. &KEY,
1208 ;;; &OPTIONAL and &REST args are annotated with an ARG-INFO structure
1209 ;;; which contains the extra information. If we hit something losing,
1210 ;;; we bug out with COMPILER-ERROR. These values are returned:
1211 ;;; 1. a list of the var structures for each top level argument;
1212 ;;; 2. a flag indicating whether &KEY was specified;
1213 ;;; 3. a flag indicating whether other &KEY args are allowed;
1214 ;;; 4. a list of the &AUX variables; and
1215 ;;; 5. a list of the &AUX values.
1216 (declaim (ftype (function (list) (values list boolean boolean list list))
1218 (defun make-lambda-vars (list)
1219 (multiple-value-bind (required optional restp rest keyp keys allowp aux
1220 morep more-context more-count)
1221 (parse-lambda-list list)
1226 (flet (;; PARSE-DEFAULT deals with defaults and supplied-p args
1227 ;; for optionals and keywords args.
1228 (parse-default (spec info)
1229 (when (consp (cdr spec))
1230 (setf (arg-info-default info) (second spec))
1231 (when (consp (cddr spec))
1232 (let* ((supplied-p (third spec))
1233 (supplied-var (varify-lambda-arg supplied-p
1235 (setf (arg-info-supplied-p info) supplied-var)
1236 (names-so-far supplied-p)
1237 (when (> (length (the list spec)) 3)
1239 "The list ~S is too long to be an arg specifier."
1242 (dolist (name required)
1243 (let ((var (varify-lambda-arg name (names-so-far))))
1245 (names-so-far name)))
1247 (dolist (spec optional)
1249 (let ((var (varify-lambda-arg spec (names-so-far))))
1250 (setf (lambda-var-arg-info var)
1251 (make-arg-info :kind :optional))
1253 (names-so-far spec))
1254 (let* ((name (first spec))
1255 (var (varify-lambda-arg name (names-so-far)))
1256 (info (make-arg-info :kind :optional)))
1257 (setf (lambda-var-arg-info var) info)
1260 (parse-default spec info))))
1263 (let ((var (varify-lambda-arg rest (names-so-far))))
1264 (setf (lambda-var-arg-info var) (make-arg-info :kind :rest))
1266 (names-so-far rest)))
1269 (let ((var (varify-lambda-arg more-context (names-so-far))))
1270 (setf (lambda-var-arg-info var)
1271 (make-arg-info :kind :more-context))
1273 (names-so-far more-context))
1274 (let ((var (varify-lambda-arg more-count (names-so-far))))
1275 (setf (lambda-var-arg-info var)
1276 (make-arg-info :kind :more-count))
1278 (names-so-far more-count)))
1283 (let ((var (varify-lambda-arg spec (names-so-far))))
1284 (setf (lambda-var-arg-info var)
1285 (make-arg-info :kind :keyword
1286 :key (make-keyword-for-arg spec
1290 (names-so-far spec)))
1291 ((atom (first spec))
1292 (let* ((name (first spec))
1293 (var (varify-lambda-arg name (names-so-far)))
1294 (info (make-arg-info
1296 :key (make-keyword-for-arg name (vars) t))))
1297 (setf (lambda-var-arg-info var) info)
1300 (parse-default spec info)))
1302 (let ((head (first spec)))
1303 (unless (proper-list-of-length-p head 2)
1304 (error "malformed &KEY argument specifier: ~S" spec))
1305 (let* ((name (second head))
1306 (var (varify-lambda-arg name (names-so-far)))
1307 (info (make-arg-info
1309 :key (make-keyword-for-arg (first head)
1312 (setf (lambda-var-arg-info var) info)
1315 (parse-default spec info))))))
1319 (let ((var (varify-lambda-arg spec nil)))
1322 (names-so-far spec)))
1324 (unless (proper-list-of-length-p spec 1 2)
1325 (compiler-error "malformed &AUX binding specifier: ~S"
1327 (let* ((name (first spec))
1328 (var (varify-lambda-arg name nil)))
1330 (aux-vals (second spec))
1331 (names-so-far name)))))
1333 (values (vars) keyp allowp (aux-vars) (aux-vals))))))
1335 ;;; This is similar to IR1-CONVERT-PROGN-BODY except that we
1336 ;;; sequentially bind each AUX-VAR to the corresponding AUX-VAL before
1337 ;;; converting the body. If there are no bindings, just convert the
1338 ;;; body, otherwise do one binding and recurse on the rest.
1340 ;;; FIXME: This could and probably should be converted to use
1341 ;;; SOURCE-NAME and DEBUG-NAME. But I (WHN) don't use &AUX bindings,
1342 ;;; so I'm not motivated. Patches will be accepted...
1343 (defun ir1-convert-aux-bindings (start cont body aux-vars aux-vals)
1344 (declare (type continuation start cont) (list body aux-vars aux-vals))
1346 (ir1-convert-progn-body start cont body)
1347 (let ((fun-cont (make-continuation))
1348 (fun (ir1-convert-lambda-body body
1349 (list (first aux-vars))
1350 :aux-vars (rest aux-vars)
1351 :aux-vals (rest aux-vals)
1352 :debug-name (debug-namify
1355 (reference-leaf start fun-cont fun)
1356 (ir1-convert-combination-args fun-cont cont
1357 (list (first aux-vals)))))
1360 ;;; This is similar to IR1-CONVERT-PROGN-BODY except that code to bind
1361 ;;; the SPECVAR for each SVAR to the value of the variable is wrapped
1362 ;;; around the body. If there are no special bindings, we just convert
1363 ;;; the body, otherwise we do one special binding and recurse on the
1366 ;;; We make a cleanup and introduce it into the lexical environment.
1367 ;;; If there are multiple special bindings, the cleanup for the blocks
1368 ;;; will end up being the innermost one. We force CONT to start a
1369 ;;; block outside of this cleanup, causing cleanup code to be emitted
1370 ;;; when the scope is exited.
1371 (defun ir1-convert-special-bindings (start cont body aux-vars aux-vals svars)
1372 (declare (type continuation start cont)
1373 (list body aux-vars aux-vals svars))
1376 (ir1-convert-aux-bindings start cont body aux-vars aux-vals))
1378 (continuation-starts-block cont)
1379 (let ((cleanup (make-cleanup :kind :special-bind))
1381 (next-cont (make-continuation))
1382 (nnext-cont (make-continuation)))
1383 (ir1-convert start next-cont
1384 `(%special-bind ',(lambda-var-specvar var) ,var))
1385 (setf (cleanup-mess-up cleanup) (continuation-use next-cont))
1386 (let ((*lexenv* (make-lexenv :cleanup cleanup)))
1387 (ir1-convert next-cont nnext-cont '(%cleanup-point))
1388 (ir1-convert-special-bindings nnext-cont cont body aux-vars aux-vals
1392 ;;; Create a lambda node out of some code, returning the result. The
1393 ;;; bindings are specified by the list of VAR structures VARS. We deal
1394 ;;; with adding the names to the LEXENV-VARS for the conversion. The
1395 ;;; result is added to the NEW-FUNCTIONALS in the *CURRENT-COMPONENT*
1396 ;;; and linked to the component head and tail.
1398 ;;; We detect special bindings here, replacing the original VAR in the
1399 ;;; lambda list with a temporary variable. We then pass a list of the
1400 ;;; special vars to IR1-CONVERT-SPECIAL-BINDINGS, which actually emits
1401 ;;; the special binding code.
1403 ;;; We ignore any ARG-INFO in the VARS, trusting that someone else is
1404 ;;; dealing with &nonsense.
1406 ;;; AUX-VARS is a list of VAR structures for variables that are to be
1407 ;;; sequentially bound. Each AUX-VAL is a form that is to be evaluated
1408 ;;; to get the initial value for the corresponding AUX-VAR.
1409 (defun ir1-convert-lambda-body (body
1415 (source-name '.anonymous.)
1417 (declare (list body vars aux-vars aux-vals)
1418 (type (or continuation null) result))
1420 ;; We're about to try to put new blocks into *CURRENT-COMPONENT*.
1421 (aver-live-component *current-component*)
1423 (let* ((bind (make-bind))
1424 (lambda (make-lambda :vars vars
1426 :%source-name source-name
1427 :%debug-name debug-name))
1428 (result (or result (make-continuation))))
1430 ;; just to check: This function should fail internal assertions if
1431 ;; we didn't set up a valid debug name above.
1433 ;; (In SBCL we try to make everything have a debug name, since we
1434 ;; lack the omniscient perspective the original implementors used
1435 ;; to decide which things didn't need one.)
1436 (functional-debug-name lambda)
1438 (setf (lambda-home lambda) lambda)
1440 (new-venv nil cons))
1443 ;; As far as I can see, LAMBDA-VAR-HOME should never have
1444 ;; been set before. Let's make sure. -- WHN 2001-09-29
1445 (aver (null (lambda-var-home var)))
1446 (setf (lambda-var-home var) lambda)
1447 (let ((specvar (lambda-var-specvar var)))
1450 (new-venv (cons (leaf-source-name specvar) specvar)))
1452 (note-lexical-binding (leaf-source-name var))
1453 (new-venv (cons (leaf-source-name var) var))))))
1455 (let ((*lexenv* (make-lexenv :vars (new-venv)
1458 (setf (bind-lambda bind) lambda)
1459 (setf (node-lexenv bind) *lexenv*)
1461 (let ((cont1 (make-continuation))
1462 (cont2 (make-continuation)))
1463 (continuation-starts-block cont1)
1464 (link-node-to-previous-continuation bind cont1)
1465 (use-continuation bind cont2)
1466 (ir1-convert-special-bindings cont2 result
1471 ;; (Stuffing this in at IR1 level
1472 ;; like this is pretty crude. And
1473 ;; it's particularly inefficient
1474 ;; to execute it on *every* LAMBDA,
1475 ;; including LET-converted LAMBDAs.
1476 ;; But when SAFETY is high, it's
1477 ;; still arguably an improvement
1478 ;; over the old CMU CL approach of
1479 ;; doing nothing (proactively
1480 ;; waiting for evolution to breed
1481 ;; stronger programmers:-). -- WHN)
1482 `((%detect-stack-exhaustion)
1485 aux-vars aux-vals (svars)))
1487 (let ((block (continuation-block result)))
1489 (let ((return (make-return :result result :lambda lambda))
1490 (tail-set (make-tail-set :funs (list lambda)))
1491 (dummy (make-continuation)))
1492 (setf (lambda-tail-set lambda) tail-set)
1493 (setf (lambda-return lambda) return)
1494 (setf (continuation-dest result) return)
1495 (setf (block-last block) return)
1496 (link-node-to-previous-continuation return result)
1497 (use-continuation return dummy))
1498 (link-blocks block (component-tail *current-component*))))))
1500 (link-blocks (component-head *current-component*) (node-block bind))
1501 (push lambda (component-new-functionals *current-component*))
1505 ;;; Create the actual entry-point function for an optional entry
1506 ;;; point. The lambda binds copies of each of the VARS, then calls FUN
1507 ;;; with the argument VALS and the DEFAULTS. Presumably the VALS refer
1508 ;;; to the VARS by name. The VALS are passed in in reverse order.
1510 ;;; If any of the copies of the vars are referenced more than once,
1511 ;;; then we mark the corresponding var as EVER-USED to inhibit
1512 ;;; "defined but not read" warnings for arguments that are only used
1513 ;;; by default forms.
1514 (defun convert-optional-entry (fun vars vals defaults)
1515 (declare (type clambda fun) (list vars vals defaults))
1516 (let* ((fvars (reverse vars))
1517 (arg-vars (mapcar (lambda (var)
1518 (unless (lambda-var-specvar var)
1519 (note-lexical-binding (leaf-source-name var)))
1521 :%source-name (leaf-source-name var)
1522 :type (leaf-type var)
1523 :where-from (leaf-where-from var)
1524 :specvar (lambda-var-specvar var)))
1526 (fun (ir1-convert-lambda-body `((%funcall ,fun
1530 :debug-name "&OPTIONAL processor")))
1531 (mapc (lambda (var arg-var)
1532 (when (cdr (leaf-refs arg-var))
1533 (setf (leaf-ever-used var) t)))
1537 ;;; This function deals with supplied-p vars in optional arguments. If
1538 ;;; the there is no supplied-p arg, then we just call
1539 ;;; IR1-CONVERT-HAIRY-ARGS on the remaining arguments, and generate a
1540 ;;; optional entry that calls the result. If there is a supplied-p
1541 ;;; var, then we add it into the default vars and throw a T into the
1542 ;;; entry values. The resulting entry point function is returned.
1543 (defun generate-optional-default-entry (res default-vars default-vals
1544 entry-vars entry-vals
1545 vars supplied-p-p body
1546 aux-vars aux-vals cont
1547 source-name debug-name)
1548 (declare (type optional-dispatch res)
1549 (list default-vars default-vals entry-vars entry-vals vars body
1551 (type (or continuation null) cont))
1552 (let* ((arg (first vars))
1553 (arg-name (leaf-source-name arg))
1554 (info (lambda-var-arg-info arg))
1555 (supplied-p (arg-info-supplied-p info))
1557 (ir1-convert-hairy-args
1559 (list* supplied-p arg default-vars)
1560 (list* (leaf-source-name supplied-p) arg-name default-vals)
1561 (cons arg entry-vars)
1562 (list* t arg-name entry-vals)
1563 (rest vars) t body aux-vars aux-vals cont
1564 source-name debug-name)
1565 (ir1-convert-hairy-args
1567 (cons arg default-vars)
1568 (cons arg-name default-vals)
1569 (cons arg entry-vars)
1570 (cons arg-name entry-vals)
1571 (rest vars) supplied-p-p body aux-vars aux-vals cont
1572 source-name debug-name))))
1574 (convert-optional-entry ep default-vars default-vals
1576 (list (arg-info-default info) nil)
1577 (list (arg-info-default info))))))
1579 ;;; Create the MORE-ENTRY function for the OPTIONAL-DISPATCH RES.
1580 ;;; ENTRY-VARS and ENTRY-VALS describe the fixed arguments. REST is
1581 ;;; the var for any &REST arg. KEYS is a list of the &KEY arg vars.
1583 ;;; The most interesting thing that we do is parse keywords. We create
1584 ;;; a bunch of temporary variables to hold the result of the parse,
1585 ;;; and then loop over the supplied arguments, setting the appropriate
1586 ;;; temps for the supplied keyword. Note that it is significant that
1587 ;;; we iterate over the keywords in reverse order --- this implements
1588 ;;; the CL requirement that (when a keyword appears more than once)
1589 ;;; the first value is used.
1591 ;;; If there is no supplied-p var, then we initialize the temp to the
1592 ;;; default and just pass the temp into the main entry. Since
1593 ;;; non-constant &KEY args are forcibly given a supplied-p var, we
1594 ;;; know that the default is constant, and thus safe to evaluate out
1597 ;;; If there is a supplied-p var, then we create temps for both the
1598 ;;; value and the supplied-p, and pass them into the main entry,
1599 ;;; letting it worry about defaulting.
1601 ;;; We deal with :ALLOW-OTHER-KEYS by delaying unknown keyword errors
1602 ;;; until we have scanned all the keywords.
1603 (defun convert-more-entry (res entry-vars entry-vals rest morep keys)
1604 (declare (type optional-dispatch res) (list entry-vars entry-vals keys))
1605 (collect ((arg-vars)
1606 (arg-vals (reverse entry-vals))
1610 (dolist (var (reverse entry-vars))
1611 (arg-vars (make-lambda-var :%source-name (leaf-source-name var)
1612 :type (leaf-type var)
1613 :where-from (leaf-where-from var))))
1615 (let* ((n-context (gensym "N-CONTEXT-"))
1616 (context-temp (make-lambda-var :%source-name n-context))
1617 (n-count (gensym "N-COUNT-"))
1618 (count-temp (make-lambda-var :%source-name n-count
1619 :type (specifier-type 'index))))
1621 (arg-vars context-temp count-temp)
1624 (arg-vals `(%listify-rest-args ,n-context ,n-count)))
1626 (arg-vals n-context)
1629 (when (optional-dispatch-keyp res)
1630 (let ((n-index (gensym "N-INDEX-"))
1631 (n-key (gensym "N-KEY-"))
1632 (n-value-temp (gensym "N-VALUE-TEMP-"))
1633 (n-allowp (gensym "N-ALLOWP-"))
1634 (n-losep (gensym "N-LOSEP-"))
1635 (allowp (or (optional-dispatch-allowp res)
1636 (policy *lexenv* (zerop safety)))))
1638 (temps `(,n-index (1- ,n-count)) n-key n-value-temp)
1639 (body `(declare (fixnum ,n-index) (ignorable ,n-key ,n-value-temp)))
1643 (let* ((info (lambda-var-arg-info key))
1644 (default (arg-info-default info))
1645 (keyword (arg-info-key info))
1646 (supplied-p (arg-info-supplied-p info))
1647 (n-value (gensym "N-VALUE-")))
1648 (temps `(,n-value ,default))
1650 (let ((n-supplied (gensym "N-SUPPLIED-")))
1652 (arg-vals n-value n-supplied)
1653 (tests `((eq ,n-key ',keyword)
1654 (setq ,n-supplied t)
1655 (setq ,n-value ,n-value-temp)))))
1658 (tests `((eq ,n-key ',keyword)
1659 (setq ,n-value ,n-value-temp)))))))
1662 (temps n-allowp n-losep)
1663 (tests `((eq ,n-key :allow-other-keys)
1664 (setq ,n-allowp ,n-value-temp)))
1666 (setq ,n-losep ,n-key))))
1669 `(when (oddp ,n-count)
1670 (%odd-key-args-error)))
1674 (declare (optimize (safety 0)))
1676 (when (minusp ,n-index) (return))
1677 (setf ,n-value-temp (%more-arg ,n-context ,n-index))
1679 (setq ,n-key (%more-arg ,n-context ,n-index))
1684 (body `(when (and ,n-losep (not ,n-allowp))
1685 (%unknown-key-arg-error ,n-losep)))))))
1687 (let ((ep (ir1-convert-lambda-body
1690 (%funcall ,(optional-dispatch-main-entry res)
1691 . ,(arg-vals)))) ; FIXME: What is the '.'? ,@?
1693 :debug-name (debug-namify "~S processing" '&more))))
1694 (setf (optional-dispatch-more-entry res) ep))))
1698 ;;; This is called by IR1-CONVERT-HAIRY-ARGS when we run into a &REST
1699 ;;; or &KEY arg. The arguments are similar to that function, but we
1700 ;;; split off any &REST arg and pass it in separately. REST is the
1701 ;;; &REST arg var, or NIL if there is no &REST arg. KEYS is a list of
1702 ;;; the &KEY argument vars.
1704 ;;; When there are &KEY arguments, we introduce temporary gensym
1705 ;;; variables to hold the values while keyword defaulting is in
1706 ;;; progress to get the required sequential binding semantics.
1708 ;;; This gets interesting mainly when there are &KEY arguments with
1709 ;;; supplied-p vars or non-constant defaults. In either case, pass in
1710 ;;; a supplied-p var. If the default is non-constant, we introduce an
1711 ;;; IF in the main entry that tests the supplied-p var and decides
1712 ;;; whether to evaluate the default or not. In this case, the real
1713 ;;; incoming value is NIL, so we must union NULL with the declared
1714 ;;; type when computing the type for the main entry's argument.
1715 (defun ir1-convert-more (res default-vars default-vals entry-vars entry-vals
1716 rest more-context more-count keys supplied-p-p
1717 body aux-vars aux-vals cont
1718 source-name debug-name)
1719 (declare (type optional-dispatch res)
1720 (list default-vars default-vals entry-vars entry-vals keys body
1722 (type (or continuation null) cont))
1723 (collect ((main-vars (reverse default-vars))
1724 (main-vals default-vals cons)
1731 (main-vars more-context)
1733 (main-vars more-count)
1737 (let* ((info (lambda-var-arg-info key))
1738 (default (arg-info-default info))
1739 (hairy-default (not (sb!xc:constantp default)))
1740 (supplied-p (arg-info-supplied-p info))
1741 (n-val (make-symbol (format nil
1742 "~A-DEFAULTING-TEMP"
1743 (leaf-source-name key))))
1744 (key-type (leaf-type key))
1745 (val-temp (make-lambda-var
1747 :type (if hairy-default
1748 (type-union key-type (specifier-type 'null))
1750 (main-vars val-temp)
1752 (cond ((or hairy-default supplied-p)
1753 (let* ((n-supplied (gensym "N-SUPPLIED-"))
1754 (supplied-temp (make-lambda-var
1755 :%source-name n-supplied)))
1757 (setf (arg-info-supplied-p info) supplied-temp))
1759 (setf (arg-info-default info) nil))
1760 (main-vars supplied-temp)
1761 (cond (hairy-default
1763 (bind-vals `(if ,n-supplied ,n-val ,default)))
1765 (main-vals default nil)
1768 (bind-vars supplied-p)
1769 (bind-vals n-supplied))))
1771 (main-vals (arg-info-default info))
1772 (bind-vals n-val)))))
1774 (let* ((main-entry (ir1-convert-lambda-body
1776 :aux-vars (append (bind-vars) aux-vars)
1777 :aux-vals (append (bind-vals) aux-vals)
1779 :debug-name (debug-namify "varargs entry for ~A"
1780 (as-debug-name source-name
1782 (last-entry (convert-optional-entry main-entry default-vars
1784 (setf (optional-dispatch-main-entry res) main-entry)
1785 (convert-more-entry res entry-vars entry-vals rest more-context keys)
1787 (push (if supplied-p-p
1788 (convert-optional-entry last-entry entry-vars entry-vals ())
1790 (optional-dispatch-entry-points res))
1793 ;;; This function generates the entry point functions for the
1794 ;;; OPTIONAL-DISPATCH RES. We accomplish this by recursion on the list
1795 ;;; of arguments, analyzing the arglist on the way down and generating
1796 ;;; entry points on the way up.
1798 ;;; DEFAULT-VARS is a reversed list of all the argument vars processed
1799 ;;; so far, including supplied-p vars. DEFAULT-VALS is a list of the
1800 ;;; names of the DEFAULT-VARS.
1802 ;;; ENTRY-VARS is a reversed list of processed argument vars,
1803 ;;; excluding supplied-p vars. ENTRY-VALS is a list things that can be
1804 ;;; evaluated to get the values for all the vars from the ENTRY-VARS.
1805 ;;; It has the var name for each required or optional arg, and has T
1806 ;;; for each supplied-p arg.
1808 ;;; VARS is a list of the LAMBDA-VAR structures for arguments that
1809 ;;; haven't been processed yet. SUPPLIED-P-P is true if a supplied-p
1810 ;;; argument has already been processed; only in this case are the
1811 ;;; DEFAULT-XXX and ENTRY-XXX different.
1813 ;;; The result at each point is a lambda which should be called by the
1814 ;;; above level to default the remaining arguments and evaluate the
1815 ;;; body. We cause the body to be evaluated by converting it and
1816 ;;; returning it as the result when the recursion bottoms out.
1818 ;;; Each level in the recursion also adds its entry point function to
1819 ;;; the result OPTIONAL-DISPATCH. For most arguments, the defaulting
1820 ;;; function and the entry point function will be the same, but when
1821 ;;; SUPPLIED-P args are present they may be different.
1823 ;;; When we run into a &REST or &KEY arg, we punt out to
1824 ;;; IR1-CONVERT-MORE, which finishes for us in this case.
1825 (defun ir1-convert-hairy-args (res default-vars default-vals
1826 entry-vars entry-vals
1827 vars supplied-p-p body aux-vars
1829 source-name debug-name)
1830 (declare (type optional-dispatch res)
1831 (list default-vars default-vals entry-vars entry-vals vars body
1833 (type (or continuation null) cont))
1835 (if (optional-dispatch-keyp res)
1836 ;; Handle &KEY with no keys...
1837 (ir1-convert-more res default-vars default-vals
1838 entry-vars entry-vals
1839 nil nil nil vars supplied-p-p body aux-vars
1840 aux-vals cont source-name debug-name)
1841 (let ((fun (ir1-convert-lambda-body
1842 body (reverse default-vars)
1846 :debug-name (debug-namify
1847 "hairy arg processor for ~A"
1848 (as-debug-name source-name
1850 (setf (optional-dispatch-main-entry res) fun)
1851 (push (if supplied-p-p
1852 (convert-optional-entry fun entry-vars entry-vals ())
1854 (optional-dispatch-entry-points res))
1856 ((not (lambda-var-arg-info (first vars)))
1857 (let* ((arg (first vars))
1858 (nvars (cons arg default-vars))
1859 (nvals (cons (leaf-source-name arg) default-vals)))
1860 (ir1-convert-hairy-args res nvars nvals nvars nvals
1861 (rest vars) nil body aux-vars aux-vals
1863 source-name debug-name)))
1865 (let* ((arg (first vars))
1866 (info (lambda-var-arg-info arg))
1867 (kind (arg-info-kind info)))
1870 (let ((ep (generate-optional-default-entry
1871 res default-vars default-vals
1872 entry-vars entry-vals vars supplied-p-p body
1873 aux-vars aux-vals cont
1874 source-name debug-name)))
1875 (push (if supplied-p-p
1876 (convert-optional-entry ep entry-vars entry-vals ())
1878 (optional-dispatch-entry-points res))
1881 (ir1-convert-more res default-vars default-vals
1882 entry-vars entry-vals
1883 arg nil nil (rest vars) supplied-p-p body
1884 aux-vars aux-vals cont
1885 source-name debug-name))
1887 (ir1-convert-more res default-vars default-vals
1888 entry-vars entry-vals
1889 nil arg (second vars) (cddr vars) supplied-p-p
1890 body aux-vars aux-vals cont
1891 source-name debug-name))
1893 (ir1-convert-more res default-vars default-vals
1894 entry-vars entry-vals
1895 nil nil nil vars supplied-p-p body aux-vars
1896 aux-vals cont source-name debug-name)))))))
1898 ;;; This function deals with the case where we have to make an
1899 ;;; OPTIONAL-DISPATCH to represent a LAMBDA. We cons up the result and
1900 ;;; call IR1-CONVERT-HAIRY-ARGS to do the work. When it is done, we
1901 ;;; figure out the MIN-ARGS and MAX-ARGS.
1902 (defun ir1-convert-hairy-lambda (body vars keyp allowp aux-vars aux-vals cont
1904 (source-name '.anonymous.)
1905 (debug-name (debug-namify
1906 "OPTIONAL-DISPATCH ~S"
1908 (declare (list body vars aux-vars aux-vals) (type continuation cont))
1909 (let ((res (make-optional-dispatch :arglist vars
1912 :%source-name source-name
1913 :%debug-name debug-name))
1914 (min (or (position-if #'lambda-var-arg-info vars) (length vars))))
1915 (aver-live-component *current-component*)
1916 (push res (component-new-functionals *current-component*))
1917 (ir1-convert-hairy-args res () () () () vars nil body aux-vars aux-vals
1918 cont source-name debug-name)
1919 (setf (optional-dispatch-min-args res) min)
1920 (setf (optional-dispatch-max-args res)
1921 (+ (1- (length (optional-dispatch-entry-points res))) min))
1925 (setf (functional-kind ep) :optional)
1926 (setf (leaf-ever-used ep) t)
1927 (setf (lambda-optional-dispatch ep) res))))
1928 (dolist (ep (optional-dispatch-entry-points res)) (frob ep))
1929 (frob (optional-dispatch-more-entry res))
1930 (frob (optional-dispatch-main-entry res)))
1934 ;;; Convert a LAMBDA form into a LAMBDA leaf or an OPTIONAL-DISPATCH leaf.
1935 (defun ir1-convert-lambda (form &key (source-name '.anonymous.) debug-name)
1937 (unless (consp form)
1938 (compiler-error "A ~S was found when expecting a lambda expression:~% ~S"
1941 (unless (eq (car form) 'lambda)
1942 (compiler-error "~S was expected but ~S was found:~% ~S"
1946 (unless (and (consp (cdr form)) (listp (cadr form)))
1948 "The lambda expression has a missing or non-list lambda list:~% ~S"
1951 (multiple-value-bind (vars keyp allow-other-keys aux-vars aux-vals)
1952 (make-lambda-vars (cadr form))
1953 (multiple-value-bind (forms decls) (sb!sys:parse-body (cddr form))
1954 (let* ((result-cont (make-continuation))
1955 (*lexenv* (process-decls decls
1956 (append aux-vars vars)
1958 (res (if (or (find-if #'lambda-var-arg-info vars) keyp)
1959 (ir1-convert-hairy-lambda forms vars keyp
1961 aux-vars aux-vals result-cont
1962 :source-name source-name
1963 :debug-name debug-name)
1964 (ir1-convert-lambda-body forms vars
1968 :source-name source-name
1969 :debug-name debug-name))))
1970 (setf (functional-inline-expansion res) form)
1971 (setf (functional-arg-documentation res) (cadr form))
1974 ;;;; defining global functions
1976 ;;; Convert FUN as a lambda in the null environment, but use the
1977 ;;; current compilation policy. Note that FUN may be a
1978 ;;; LAMBDA-WITH-LEXENV, so we may have to augment the environment to
1979 ;;; reflect the state at the definition site.
1980 (defun ir1-convert-inline-lambda (fun &key
1981 (source-name '.anonymous.)
1983 (destructuring-bind (decls macros symbol-macros &rest body)
1984 (if (eq (car fun) 'lambda-with-lexenv)
1986 `(() () () . ,(cdr fun)))
1987 (let ((*lexenv* (make-lexenv
1988 :default (process-decls decls nil nil
1991 :vars (copy-list symbol-macros)
1992 :funs (mapcar (lambda (x)
1994 (macro . ,(coerce (cdr x) 'function))))
1996 :policy (lexenv-policy *lexenv*))))
1997 (ir1-convert-lambda `(lambda ,@body)
1998 :source-name source-name
1999 :debug-name debug-name))))
2001 ;;; Get a DEFINED-FUN object for a function we are about to
2002 ;;; define. If the function has been forward referenced, then
2003 ;;; substitute for the previous references.
2004 (defun get-defined-fun (name)
2005 (proclaim-as-fun-name name)
2006 (let ((found (find-free-fun name "shouldn't happen! (defined-fun)")))
2007 (note-name-defined name :function)
2008 (cond ((not (defined-fun-p found))
2009 (aver (not (info :function :inlinep name)))
2010 (let* ((where-from (leaf-where-from found))
2011 (res (make-defined-fun
2013 :where-from (if (eq where-from :declared)
2015 :type (leaf-type found))))
2016 (substitute-leaf res found)
2017 (setf (gethash name *free-funs*) res)))
2018 ;; If *FREE-FUNS* has a previously converted definition
2019 ;; for this name, then blow it away and try again.
2020 ((defined-fun-functional found)
2021 (remhash name *free-funs*)
2022 (get-defined-fun name))
2025 ;;; Check a new global function definition for consistency with
2026 ;;; previous declaration or definition, and assert argument/result
2027 ;;; types if appropriate. This assertion is suppressed by the
2028 ;;; EXPLICIT-CHECK attribute, which is specified on functions that
2029 ;;; check their argument types as a consequence of type dispatching.
2030 ;;; This avoids redundant checks such as NUMBERP on the args to +, etc.
2031 (defun assert-new-definition (var fun)
2032 (let ((type (leaf-type var))
2033 (for-real (eq (leaf-where-from var) :declared))
2034 (info (info :function :info (leaf-source-name var))))
2035 (assert-definition-type
2037 ;; KLUDGE: Common Lisp is such a dynamic language that in general
2038 ;; all we can do here in general is issue a STYLE-WARNING. It
2039 ;; would be nice to issue a full WARNING in the special case of
2040 ;; of type mismatches within a compilation unit (as in section
2041 ;; 3.2.2.3 of the spec) but at least as of sbcl-0.6.11, we don't
2042 ;; keep track of whether the mismatched data came from the same
2043 ;; compilation unit, so we can't do that. -- WHN 2001-02-11
2044 :lossage-fun #'compiler-style-warn
2045 :unwinnage-fun (cond (info #'compiler-style-warn)
2046 (for-real #'compiler-note)
2051 (ir1-attributep (fun-info-attributes info)
2054 "previous declaration"
2055 "previous definition"))))
2057 ;;; Convert a lambda doing all the basic stuff we would do if we were
2058 ;;; converting a DEFUN. In the old CMU CL system, this was used both
2059 ;;; by the %DEFUN translator and for global inline expansion, but
2060 ;;; since sbcl-0.pre7.something %DEFUN does things differently.
2061 ;;; FIXME: And now it's probably worth rethinking whether this
2062 ;;; function is a good idea.
2064 ;;; Unless a :INLINE function, we temporarily clobber the inline
2065 ;;; expansion. This prevents recursive inline expansion of
2066 ;;; opportunistic pseudo-inlines.
2067 (defun ir1-convert-lambda-for-defun (lambda var expansion converter)
2068 (declare (cons lambda) (function converter) (type defined-fun var))
2069 (let ((var-expansion (defined-fun-inline-expansion var)))
2070 (unless (eq (defined-fun-inlinep var) :inline)
2071 (setf (defined-fun-inline-expansion var) nil))
2072 (let* ((name (leaf-source-name var))
2073 (fun (funcall converter lambda
2075 (fun-info (info :function :info name)))
2076 (setf (functional-inlinep fun) (defined-fun-inlinep var))
2077 (assert-new-definition var fun)
2078 (setf (defined-fun-inline-expansion var) var-expansion)
2079 ;; If definitely not an interpreter stub, then substitute for any
2081 (unless (or (eq (defined-fun-inlinep var) :notinline)
2082 (not *block-compile*)
2084 (or (fun-info-transforms fun-info)
2085 (fun-info-templates fun-info)
2086 (fun-info-ir2-convert fun-info))))
2087 (substitute-leaf fun var)
2088 ;; If in a simple environment, then we can allow backward
2089 ;; references to this function from following top level forms.
2090 (when expansion (setf (defined-fun-functional var) fun)))
2093 ;;; the even-at-compile-time part of DEFUN
2095 ;;; The INLINE-EXPANSION is a LAMBDA-WITH-LEXENV, or NIL if there is
2096 ;;; no inline expansion.
2097 (defun %compiler-defun (name lambda-with-lexenv)
2099 (let ((defined-fun nil)) ; will be set below if we're in the compiler
2101 (when (boundp '*lexenv*) ; when in the compiler
2102 (when sb!xc:*compile-print*
2103 (compiler-mumble "~&; recognizing DEFUN ~S~%" name))
2104 (remhash name *free-funs*)
2105 (setf defined-fun (get-defined-fun name)))
2107 (become-defined-fun-name name)
2109 (cond (lambda-with-lexenv
2110 (setf (info :function :inline-expansion-designator name)
2113 (setf (defined-fun-inline-expansion defined-fun)
2114 lambda-with-lexenv)))
2116 (clear-info :function :inline-expansion-designator name)))
2118 ;; old CMU CL comment:
2119 ;; If there is a type from a previous definition, blast it,
2120 ;; since it is obsolete.
2121 (when (and defined-fun
2122 (eq (leaf-where-from defined-fun) :defined))
2123 (setf (leaf-type defined-fun)
2124 ;; FIXME: If this is a block compilation thing, shouldn't
2125 ;; we be setting the type to the full derived type for the
2126 ;; definition, instead of this most general function type?
2127 (specifier-type 'function))))