1 ;;;; This file contains code which does the translation of lambda
2 ;;;; forms from Lisp code to the first intermediate representation
5 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
18 ;;;; Note: Take a look at the compiler-overview.tex section on "Hairy
19 ;;;; function representation" before you seriously mess with this
22 ;;; Verify that the NAME is a legal name for a variable and return a
23 ;;; VAR structure for it, filling in info if it is globally special.
24 ;;; If it is losing, we punt with a COMPILER-ERROR. NAMES-SO-FAR is a
25 ;;; list of names which have previously been bound. If the NAME is in
26 ;;; this list, then we error out.
27 (declaim (ftype (sfunction (t list &optional t) lambda-var) varify-lambda-arg))
28 (defun varify-lambda-arg (name names-so-far &optional (context "lambda list"))
29 (declare (inline member))
30 (unless (symbolp name)
31 (compiler-error "~S is not a symbol, and cannot be used as a variable." name))
32 (when (member name names-so-far :test #'eq)
33 (compiler-error "The variable ~S occurs more than once in the ~A."
36 (let ((kind (info :variable :kind name)))
37 (cond ((keywordp name)
38 (compiler-error "~S is a keyword, and cannot be used as a local variable."
41 (compiler-error "~@<~S names a defined constant, and cannot be used as a ~
45 (compiler-error "~@<~S names a global lexical variable, and cannot be used ~
46 as a local variable.~:@>"
49 (let ((specvar (find-free-var name)))
50 (make-lambda-var :%source-name name
51 :type (leaf-type specvar)
52 :where-from (leaf-where-from specvar)
55 (make-lambda-var :%source-name name)))))
57 ;;; Make the default keyword for a &KEY arg, checking that the keyword
58 ;;; isn't already used by one of the VARS.
59 (declaim (ftype (sfunction (symbol list t) symbol) make-keyword-for-arg))
60 (defun make-keyword-for-arg (symbol vars keywordify)
61 (let ((key (if (and keywordify (not (keywordp symbol)))
65 (let ((info (lambda-var-arg-info var)))
67 (eq (arg-info-kind info) :keyword)
68 (eq (arg-info-key info) key))
70 "The keyword ~S appears more than once in the lambda list."
74 ;;; Parse a lambda list into a list of VAR structures, stripping off
75 ;;; any &AUX bindings. Each arg name is checked for legality, and
76 ;;; duplicate names are checked for. If an arg is globally special,
77 ;;; the var is marked as :SPECIAL instead of :LEXICAL. &KEY,
78 ;;; &OPTIONAL and &REST args are annotated with an ARG-INFO structure
79 ;;; which contains the extra information. If we hit something losing,
80 ;;; we bug out with COMPILER-ERROR. These values are returned:
81 ;;; 1. a list of the var structures for each top level argument;
82 ;;; 2. a flag indicating whether &KEY was specified;
83 ;;; 3. a flag indicating whether other &KEY args are allowed;
84 ;;; 4. a list of the &AUX variables; and
85 ;;; 5. a list of the &AUX values.
86 (declaim (ftype (sfunction (list) (values list boolean boolean list list))
88 (defun make-lambda-vars (list)
89 (multiple-value-bind (required optional restp rest keyp keys allowp auxp aux
90 morep more-context more-count)
91 (parse-lambda-list list)
92 (declare (ignore auxp)) ; since we just iterate over AUX regardless
97 (flet (;; PARSE-DEFAULT deals with defaults and supplied-p args
98 ;; for optionals and keywords args.
99 (parse-default (spec info)
100 (when (consp (cdr spec))
101 (setf (arg-info-default info) (second spec))
102 (when (consp (cddr spec))
103 (let* ((supplied-p (third spec))
104 (supplied-var (varify-lambda-arg supplied-p
106 (setf (arg-info-supplied-p info) supplied-var)
107 (names-so-far supplied-p)
108 (when (> (length (the list spec)) 3)
110 "The list ~S is too long to be an arg specifier."
113 (dolist (name required)
114 (let ((var (varify-lambda-arg name (names-so-far))))
116 (names-so-far name)))
118 (dolist (spec optional)
120 (let ((var (varify-lambda-arg spec (names-so-far))))
121 (setf (lambda-var-arg-info var)
122 (make-arg-info :kind :optional))
125 (let* ((name (first spec))
126 (var (varify-lambda-arg name (names-so-far)))
127 (info (make-arg-info :kind :optional)))
128 (setf (lambda-var-arg-info var) info)
131 (parse-default spec info))))
134 (let ((var (varify-lambda-arg rest (names-so-far))))
135 (setf (lambda-var-arg-info var) (make-arg-info :kind :rest))
137 (names-so-far rest)))
140 (let ((var (varify-lambda-arg more-context (names-so-far))))
141 (setf (lambda-var-arg-info var)
142 (make-arg-info :kind :more-context))
144 (names-so-far more-context))
145 (let ((var (varify-lambda-arg more-count (names-so-far))))
146 (setf (lambda-var-arg-info var)
147 (make-arg-info :kind :more-count))
149 (names-so-far more-count)))
154 (let ((var (varify-lambda-arg spec (names-so-far))))
155 (setf (lambda-var-arg-info var)
156 (make-arg-info :kind :keyword
157 :key (make-keyword-for-arg spec
161 (names-so-far spec)))
163 (let* ((name (first spec))
164 (var (varify-lambda-arg name (names-so-far)))
167 :key (make-keyword-for-arg name (vars) t))))
168 (setf (lambda-var-arg-info var) info)
171 (parse-default spec info)))
173 (let ((head (first spec)))
174 (unless (proper-list-of-length-p head 2)
175 (error "malformed &KEY argument specifier: ~S" spec))
176 (let* ((name (second head))
177 (var (varify-lambda-arg name (names-so-far)))
180 :key (make-keyword-for-arg (first head)
183 (setf (lambda-var-arg-info var) info)
186 (parse-default spec info))))))
190 (let ((var (varify-lambda-arg spec nil)))
193 (names-so-far spec)))
195 (unless (proper-list-of-length-p spec 1 2)
196 (compiler-error "malformed &AUX binding specifier: ~S"
198 (let* ((name (first spec))
199 (var (varify-lambda-arg name nil)))
201 (aux-vals (second spec))
202 (names-so-far name)))))
204 (values (vars) keyp allowp (aux-vars) (aux-vals))))))
206 ;;; This is similar to IR1-CONVERT-PROGN-BODY except that we
207 ;;; sequentially bind each AUX-VAR to the corresponding AUX-VAL before
208 ;;; converting the body. If there are no bindings, just convert the
209 ;;; body, otherwise do one binding and recurse on the rest.
211 ;;; FIXME: This could and probably should be converted to use
212 ;;; SOURCE-NAME and DEBUG-NAME. But I (WHN) don't use &AUX bindings,
213 ;;; so I'm not motivated. Patches will be accepted...
214 (defun ir1-convert-aux-bindings (start next result body aux-vars aux-vals
216 (declare (type ctran start next) (type (or lvar null) result)
217 (list body aux-vars aux-vals))
219 (let ((*lexenv* (make-lexenv :vars (copy-list post-binding-lexenv))))
220 (ir1-convert-progn-body start next result body))
221 (let ((ctran (make-ctran))
222 (fun-lvar (make-lvar))
223 (fun (ir1-convert-lambda-body body
224 (list (first aux-vars))
225 :aux-vars (rest aux-vars)
226 :aux-vals (rest aux-vals)
227 :post-binding-lexenv post-binding-lexenv
228 :debug-name (debug-name
231 (reference-leaf start ctran fun-lvar fun)
232 (ir1-convert-combination-args fun-lvar ctran next result
233 (list (first aux-vals)))))
236 ;;; This is similar to IR1-CONVERT-PROGN-BODY except that code to bind
237 ;;; the SPECVAR for each SVAR to the value of the variable is wrapped
238 ;;; around the body. If there are no special bindings, we just convert
239 ;;; the body, otherwise we do one special binding and recurse on the
242 ;;; We make a cleanup and introduce it into the lexical
243 ;;; environment. If there are multiple special bindings, the cleanup
244 ;;; for the blocks will end up being the innermost one. We force NEXT
245 ;;; to start a block outside of this cleanup, causing cleanup code to
246 ;;; be emitted when the scope is exited.
247 (defun ir1-convert-special-bindings
248 (start next result body aux-vars aux-vals svars post-binding-lexenv)
249 (declare (type ctran start next) (type (or lvar null) result)
250 (list body aux-vars aux-vals svars))
253 (ir1-convert-aux-bindings start next result body aux-vars aux-vals
254 post-binding-lexenv))
256 (ctran-starts-block next)
257 (let ((cleanup (make-cleanup :kind :special-bind))
259 (bind-ctran (make-ctran))
260 (cleanup-ctran (make-ctran)))
261 (ir1-convert start bind-ctran nil
262 `(%special-bind ',(lambda-var-specvar var) ,var))
263 (setf (cleanup-mess-up cleanup) (ctran-use bind-ctran))
264 (let ((*lexenv* (make-lexenv :cleanup cleanup)))
265 (ir1-convert bind-ctran cleanup-ctran nil '(%cleanup-point))
266 (ir1-convert-special-bindings cleanup-ctran next result
267 body aux-vars aux-vals
269 post-binding-lexenv)))))
272 ;;; Create a lambda node out of some code, returning the result. The
273 ;;; bindings are specified by the list of VAR structures VARS. We deal
274 ;;; with adding the names to the LEXENV-VARS for the conversion. The
275 ;;; result is added to the NEW-FUNCTIONALS in the *CURRENT-COMPONENT*
276 ;;; and linked to the component head and tail.
278 ;;; We detect special bindings here, replacing the original VAR in the
279 ;;; lambda list with a temporary variable. We then pass a list of the
280 ;;; special vars to IR1-CONVERT-SPECIAL-BINDINGS, which actually emits
281 ;;; the special binding code.
283 ;;; We ignore any ARG-INFO in the VARS, trusting that someone else is
284 ;;; dealing with &NONSENSE, except for &REST vars with DYNAMIC-EXTENT.
286 ;;; AUX-VARS is a list of VAR structures for variables that are to be
287 ;;; sequentially bound. Each AUX-VAL is a form that is to be evaluated
288 ;;; to get the initial value for the corresponding AUX-VAR.
289 (defun ir1-convert-lambda-body (body
294 (source-name '.anonymous.)
296 (note-lexical-bindings t)
299 (declare (list body vars aux-vars aux-vals))
301 ;; We're about to try to put new blocks into *CURRENT-COMPONENT*.
302 (aver-live-component *current-component*)
304 (let* ((bind (make-bind))
305 (lambda (make-lambda :vars vars
307 :%source-name source-name
308 :%debug-name debug-name
309 :system-lambda-p system-lambda))
310 (result-ctran (make-ctran))
311 (result-lvar (make-lvar)))
313 (awhen (lexenv-lambda *lexenv*)
314 (push lambda (lambda-children it))
315 (setf (lambda-parent lambda) it))
317 ;; just to check: This function should fail internal assertions if
318 ;; we didn't set up a valid debug name above.
320 ;; (In SBCL we try to make everything have a debug name, since we
321 ;; lack the omniscient perspective the original implementors used
322 ;; to decide which things didn't need one.)
323 (functional-debug-name lambda)
325 (setf (lambda-home lambda) lambda)
330 ;; As far as I can see, LAMBDA-VAR-HOME should never have
331 ;; been set before. Let's make sure. -- WHN 2001-09-29
332 (aver (not (lambda-var-home var)))
333 (setf (lambda-var-home var) lambda)
334 (let ((specvar (lambda-var-specvar var)))
337 (new-venv (cons (leaf-source-name specvar) specvar)))
339 (when note-lexical-bindings
340 (note-lexical-binding (leaf-source-name var)))
341 (new-venv (cons (leaf-source-name var) var))))))
343 (let ((*lexenv* (make-lexenv :vars (new-venv)
346 (setf (bind-lambda bind) lambda)
347 (setf (node-lexenv bind) *lexenv*)
349 (let ((block (ctran-starts-block result-ctran)))
350 (let ((return (make-return :result result-lvar :lambda lambda))
351 (tail-set (make-tail-set :funs (list lambda))))
352 (setf (lambda-tail-set lambda) tail-set)
353 (setf (lambda-return lambda) return)
354 (setf (lvar-dest result-lvar) return)
355 (link-node-to-previous-ctran return result-ctran)
356 (setf (block-last block) return))
357 (link-blocks block (component-tail *current-component*)))
359 (with-component-last-block (*current-component*
360 (ctran-block result-ctran))
361 (let ((prebind-ctran (make-ctran))
362 (postbind-ctran (make-ctran)))
363 (ctran-starts-block prebind-ctran)
364 (link-node-to-previous-ctran bind prebind-ctran)
365 (use-ctran bind postbind-ctran)
366 (ir1-convert-special-bindings postbind-ctran result-ctran
368 aux-vars aux-vals (svars)
369 post-binding-lexenv)))))
371 (link-blocks (component-head *current-component*) (node-block bind))
372 (push lambda (component-new-functionals *current-component*))
376 ;;; Entry point CLAMBDAs have a special kind
377 (defun register-entry-point (entry dispatcher)
378 (declare (type clambda entry)
379 (type optional-dispatch dispatcher))
380 (setf (functional-kind entry) :optional)
381 (setf (leaf-ever-used entry) t)
382 (setf (lambda-optional-dispatch entry) dispatcher)
385 ;;; Create the actual entry-point function for an optional entry
386 ;;; point. The lambda binds copies of each of the VARS, then calls FUN
387 ;;; with the argument VALS and the DEFAULTS. Presumably the VALS refer
388 ;;; to the VARS by name. The VALS are passed in the reverse order.
390 ;;; If any of the copies of the vars are referenced more than once,
391 ;;; then we mark the corresponding var as EVER-USED to inhibit
392 ;;; "defined but not read" warnings for arguments that are only used
393 ;;; by default forms.
394 (defun convert-optional-entry (fun vars vals defaults name)
395 (declare (type clambda fun) (list vars vals defaults))
396 (let* ((fvars (reverse vars))
397 (arg-vars (mapcar (lambda (var)
399 :%source-name (leaf-source-name var)
400 :type (leaf-type var)
401 :where-from (leaf-where-from var)
402 :specvar (lambda-var-specvar var)))
404 (fun (collect ((default-bindings)
406 (dolist (default defaults)
407 (if (sb!xc:constantp default)
408 (default-vals default)
409 (let ((var (gensym)))
410 (default-bindings `(,var ,default))
411 (default-vals var))))
412 (let ((bindings (default-bindings))
413 (call `(%funcall ,fun ,@(reverse vals) ,@(default-vals))))
414 (ir1-convert-lambda-body (if bindings
415 `((let (,@bindings) ,call))
418 ;; FIXME: Would be nice to
419 ;; share these names instead
420 ;; of consing up several
421 ;; identical ones. Oh well.
422 :debug-name (debug-name
425 :note-lexical-bindings nil
426 :system-lambda t)))))
427 (mapc (lambda (var arg-var)
428 (when (cdr (leaf-refs arg-var))
429 (setf (leaf-ever-used var) t)))
433 ;;; This function deals with supplied-p vars in optional arguments. If
434 ;;; the there is no supplied-p arg, then we just call
435 ;;; IR1-CONVERT-HAIRY-ARGS on the remaining arguments, and generate a
436 ;;; optional entry that calls the result. If there is a supplied-p
437 ;;; var, then we add it into the default vars and throw a T into the
438 ;;; entry values. The resulting entry point function is returned.
439 (defun generate-optional-default-entry (res default-vars default-vals
440 entry-vars entry-vals
441 vars supplied-p-p body
443 source-name debug-name
444 force post-binding-lexenv
446 (declare (type optional-dispatch res)
447 (list default-vars default-vals entry-vars entry-vals vars body
449 (let* ((arg (first vars))
450 (arg-name (leaf-source-name arg))
451 (info (lambda-var-arg-info arg))
452 (default (arg-info-default info))
453 (supplied-p (arg-info-supplied-p info))
455 (not (sb!xc:constantp (arg-info-default info)))))
457 (ir1-convert-hairy-args
459 (list* supplied-p arg default-vars)
460 (list* (leaf-source-name supplied-p) arg-name default-vals)
461 (cons arg entry-vars)
462 (list* t arg-name entry-vals)
463 (rest vars) t body aux-vars aux-vals
464 source-name debug-name
465 force post-binding-lexenv system-lambda)
466 (ir1-convert-hairy-args
468 (cons arg default-vars)
469 (cons arg-name default-vals)
470 (cons arg entry-vars)
471 (cons arg-name entry-vals)
472 (rest vars) supplied-p-p body aux-vars aux-vals
473 source-name debug-name
474 force post-binding-lexenv system-lambda))))
476 ;; We want to delay converting the entry, but there exist
477 ;; problems: hidden references should not be established to
478 ;; lambdas of kind NIL should not have (otherwise the compiler
479 ;; might let-convert or delete them) and to variables.
480 (let ((name (or debug-name source-name)))
482 supplied-p-p ; this entry will be of kind NIL
483 (and (lambda-p ep) (eq (lambda-kind ep) nil)))
484 (convert-optional-entry ep
485 default-vars default-vals
486 (if supplied-p (list default nil) (list default))
488 (let* ((default `',(constant-form-value default))
489 (defaults (if supplied-p (list default nil) (list default))))
490 ;; DEFAULT can contain a reference to a
491 ;; to-be-optimized-away function/block/tag, so better to
492 ;; reduce code now (but we possibly lose syntax checking
493 ;; in an unreachable code).
495 (register-entry-point
496 (convert-optional-entry (force ep)
497 default-vars default-vals
502 ;;; Create the MORE-ENTRY function for the OPTIONAL-DISPATCH RES.
503 ;;; ENTRY-VARS and ENTRY-VALS describe the fixed arguments. REST is
504 ;;; the var for any &REST arg. KEYS is a list of the &KEY arg vars.
506 ;;; The most interesting thing that we do is parse keywords. We create
507 ;;; a bunch of temporary variables to hold the result of the parse,
508 ;;; and then loop over the supplied arguments, setting the appropriate
509 ;;; temps for the supplied keyword. Note that it is significant that
510 ;;; we iterate over the keywords in reverse order --- this implements
511 ;;; the CL requirement that (when a keyword appears more than once)
512 ;;; the first value is used.
514 ;;; If there is no supplied-p var, then we initialize the temp to the
515 ;;; default and just pass the temp into the main entry. Since
516 ;;; non-constant &KEY args are forcibly given a supplied-p var, we
517 ;;; know that the default is constant, and thus safe to evaluate out
520 ;;; If there is a supplied-p var, then we create temps for both the
521 ;;; value and the supplied-p, and pass them into the main entry,
522 ;;; letting it worry about defaulting.
524 ;;; We deal with :ALLOW-OTHER-KEYS by delaying unknown keyword errors
525 ;;; until we have scanned all the keywords.
526 (defun convert-more-entry (res entry-vars entry-vals rest morep keys name)
527 (declare (type optional-dispatch res) (list entry-vars entry-vals keys))
529 (arg-vals (reverse entry-vals))
533 (dolist (var (reverse entry-vars))
534 (arg-vars (make-lambda-var :%source-name (leaf-source-name var)
535 :type (leaf-type var)
536 :where-from (leaf-where-from var))))
538 (let* ((n-context (gensym "N-CONTEXT-"))
539 (context-temp (make-lambda-var :%source-name n-context))
540 (n-count (gensym "N-COUNT-"))
541 (count-temp (make-lambda-var :%source-name n-count
542 :type (specifier-type 'index))))
544 (arg-vars context-temp count-temp)
547 (arg-vals `(%listify-rest-args
548 ,n-context ,n-count)))
553 ;; The reason for all the noise with
554 ;; STACK-GROWS-DOWNWARD-NOT-UPWARD is to enable generation of
555 ;; slightly more efficient code on x86oid processors. (We can
556 ;; hoist the negation of the index outside the main parsing loop
557 ;; and take advantage of the base+index+displacement addressing
559 (when (optional-dispatch-keyp res)
560 (let ((n-index (gensym "N-INDEX-"))
561 (n-key (gensym "N-KEY-"))
562 (n-value-temp (gensym "N-VALUE-TEMP-"))
563 (n-allowp (gensym "N-ALLOWP-"))
564 (n-losep (gensym "N-LOSEP-"))
565 (allowp (or (optional-dispatch-allowp res)
566 (policy *lexenv* (zerop safety))))
569 (temps #!-stack-grows-downward-not-upward
570 `(,n-index (1- ,n-count))
571 #!+stack-grows-downward-not-upward
572 `(,n-index (- (1- ,n-count)))
573 #!-stack-grows-downward-not-upward n-value-temp
574 #!-stack-grows-downward-not-upward n-key)
575 (body `(declare (fixnum ,n-index)
576 #!-stack-grows-downward-not-upward
577 (ignorable ,n-value-temp ,n-key)))
581 (let* ((info (lambda-var-arg-info key))
582 (default (arg-info-default info))
583 (keyword (arg-info-key info))
584 (supplied-p (arg-info-supplied-p info))
585 (n-value (gensym "N-VALUE-"))
586 (clause (cond (supplied-p
587 (let ((n-supplied (gensym "N-SUPPLIED-")))
589 (arg-vals n-value n-supplied)
590 `((eq ,n-key ',keyword)
592 (setq ,n-value ,n-value-temp))))
595 `((eq ,n-key ',keyword)
596 (setq ,n-value ,n-value-temp))))))
597 (when (and (not allowp) (eq keyword :allow-other-keys))
598 (setq found-allow-p t)
600 (append clause `((setq ,n-allowp ,n-value-temp)))))
602 (temps `(,n-value ,default))
606 (temps n-allowp n-losep)
607 (unless found-allow-p
608 (tests `((eq ,n-key :allow-other-keys)
609 (setq ,n-allowp ,n-value-temp))))
611 (setq ,n-losep (list ,n-key)))))
614 `(when (oddp ,n-count)
615 (%odd-key-args-error)))
618 #!-stack-grows-downward-not-upward
620 (declare (optimize (safety 0)))
622 (when (minusp ,n-index) (return))
623 (setf ,n-value-temp (%more-arg ,n-context ,n-index))
625 (setq ,n-key (%more-arg ,n-context ,n-index))
628 #!+stack-grows-downward-not-upward
629 `(locally (declare (optimize (safety 0)))
631 (when (plusp ,n-index) (return))
632 (multiple-value-bind (,n-value-temp ,n-key)
633 (%more-kw-arg ,n-context ,n-index)
634 (declare (ignorable ,n-value-temp ,n-key))
639 (body `(when (and ,n-losep (not ,n-allowp))
640 (%unknown-key-arg-error (car ,n-losep))))))))
642 (let ((ep (ir1-convert-lambda-body
645 (%funcall ,(optional-dispatch-main-entry res)
648 :debug-name (debug-name '&more-processor name)
649 :note-lexical-bindings nil
651 (setf (optional-dispatch-more-entry res)
652 (register-entry-point ep res)))))
656 ;;; This is called by IR1-CONVERT-HAIRY-ARGS when we run into a &REST
657 ;;; or &KEY arg. The arguments are similar to that function, but we
658 ;;; split off any &REST arg and pass it in separately. REST is the
659 ;;; &REST arg var, or NIL if there is no &REST arg. KEYS is a list of
660 ;;; the &KEY argument vars.
662 ;;; When there are &KEY arguments, we introduce temporary gensym
663 ;;; variables to hold the values while keyword defaulting is in
664 ;;; progress to get the required sequential binding semantics.
666 ;;; This gets interesting mainly when there are &KEY arguments with
667 ;;; supplied-p vars or non-constant defaults. In either case, pass in
668 ;;; a supplied-p var. If the default is non-constant, we introduce an
669 ;;; IF in the main entry that tests the supplied-p var and decides
670 ;;; whether to evaluate the default or not. In this case, the real
671 ;;; incoming value is NIL, so we must union NULL with the declared
672 ;;; type when computing the type for the main entry's argument.
673 (defun ir1-convert-more (res default-vars default-vals entry-vars entry-vals
674 rest more-context more-count keys supplied-p-p
675 body aux-vars aux-vals source-name debug-name
676 post-binding-lexenv system-lambda)
677 (declare (type optional-dispatch res)
678 (list default-vars default-vals entry-vars entry-vals keys body
680 (collect ((main-vars (reverse default-vars))
681 (main-vals default-vals cons)
688 (main-vars more-context)
690 (main-vars more-count)
694 (let* ((info (lambda-var-arg-info key))
695 (default (arg-info-default info))
696 (hairy-default (not (sb!xc:constantp default)))
697 (supplied-p (arg-info-supplied-p info))
698 (n-val (make-symbol (format nil
700 (leaf-source-name key))))
701 (val-temp (make-lambda-var :%source-name n-val)))
704 (cond ((or hairy-default supplied-p)
705 (let* ((n-supplied (gensym "N-SUPPLIED-"))
706 (supplied-temp (make-lambda-var
707 :%source-name n-supplied)))
709 (setf (arg-info-supplied-p info) supplied-temp))
711 (setf (arg-info-default info) nil))
712 (main-vars supplied-temp)
715 (bind-vals `(if ,n-supplied ,n-val ,default)))
717 (main-vals default nil)
720 (bind-vars supplied-p)
721 (bind-vals n-supplied))))
723 (main-vals (arg-info-default info))
724 (bind-vals n-val)))))
726 (let* ((name (or debug-name source-name))
727 (main-entry (ir1-convert-lambda-body
729 :aux-vars (append (bind-vars) aux-vars)
730 :aux-vals (append (bind-vals) aux-vals)
731 :post-binding-lexenv post-binding-lexenv
732 :debug-name (debug-name 'varargs-entry name)
733 :system-lambda system-lambda))
734 (last-entry (convert-optional-entry main-entry default-vars
735 (main-vals) () name)))
736 (setf (optional-dispatch-main-entry res)
737 (register-entry-point main-entry res))
738 (convert-more-entry res entry-vars entry-vals rest more-context keys
741 (push (register-entry-point
743 (convert-optional-entry last-entry entry-vars entry-vals
747 (optional-dispatch-entry-points res))
750 ;;; This function generates the entry point functions for the
751 ;;; OPTIONAL-DISPATCH RES. We accomplish this by recursion on the list
752 ;;; of arguments, analyzing the arglist on the way down and generating
753 ;;; entry points on the way up.
755 ;;; DEFAULT-VARS is a reversed list of all the argument vars processed
756 ;;; so far, including supplied-p vars. DEFAULT-VALS is a list of the
757 ;;; names of the DEFAULT-VARS.
759 ;;; ENTRY-VARS is a reversed list of processed argument vars,
760 ;;; excluding supplied-p vars. ENTRY-VALS is a list things that can be
761 ;;; evaluated to get the values for all the vars from the ENTRY-VARS.
762 ;;; It has the var name for each required or optional arg, and has T
763 ;;; for each supplied-p arg.
765 ;;; VARS is a list of the LAMBDA-VAR structures for arguments that
766 ;;; haven't been processed yet. SUPPLIED-P-P is true if a supplied-p
767 ;;; argument has already been processed; only in this case are the
768 ;;; DEFAULT-XXX and ENTRY-XXX different.
770 ;;; The result at each point is a lambda which should be called by the
771 ;;; above level to default the remaining arguments and evaluate the
772 ;;; body. We cause the body to be evaluated by converting it and
773 ;;; returning it as the result when the recursion bottoms out.
775 ;;; Each level in the recursion also adds its entry point function to
776 ;;; the result OPTIONAL-DISPATCH. For most arguments, the defaulting
777 ;;; function and the entry point function will be the same, but when
778 ;;; SUPPLIED-P args are present they may be different.
780 ;;; When we run into a &REST or &KEY arg, we punt out to
781 ;;; IR1-CONVERT-MORE, which finishes for us in this case.
782 (defun ir1-convert-hairy-args (res default-vars default-vals
783 entry-vars entry-vals
784 vars supplied-p-p body aux-vars
786 source-name debug-name
787 force post-binding-lexenv
789 (declare (type optional-dispatch res)
790 (list default-vars default-vals entry-vars entry-vals vars body
792 (aver (or debug-name (neq '.anonymous. source-name)))
794 (if (optional-dispatch-keyp res)
795 ;; Handle &KEY with no keys...
796 (ir1-convert-more res default-vars default-vals
797 entry-vars entry-vals
798 nil nil nil vars supplied-p-p body aux-vars
799 aux-vals source-name debug-name
800 post-binding-lexenv system-lambda)
801 (let* ((name (or debug-name source-name))
802 (fun (ir1-convert-lambda-body
803 body (reverse default-vars)
806 :post-binding-lexenv post-binding-lexenv
807 :debug-name (debug-name 'hairy-arg-processor name)
808 :system-lambda system-lambda)))
810 (setf (optional-dispatch-main-entry res) fun)
811 (register-entry-point fun res)
812 (push (if supplied-p-p
813 (register-entry-point
814 (convert-optional-entry fun entry-vars entry-vals ()
818 (optional-dispatch-entry-points res))
820 ((not (lambda-var-arg-info (first vars)))
821 (let* ((arg (first vars))
822 (nvars (cons arg default-vars))
823 (nvals (cons (leaf-source-name arg) default-vals)))
824 (ir1-convert-hairy-args res nvars nvals nvars nvals
825 (rest vars) nil body aux-vars aux-vals
826 source-name debug-name
827 nil post-binding-lexenv system-lambda)))
829 (let* ((arg (first vars))
830 (info (lambda-var-arg-info arg))
831 (kind (arg-info-kind info)))
834 (let ((ep (generate-optional-default-entry
835 res default-vars default-vals
836 entry-vars entry-vals vars supplied-p-p body
838 source-name debug-name
839 force post-binding-lexenv
841 ;; See GENERATE-OPTIONAL-DEFAULT-ENTRY.
842 (push (if (lambda-p ep)
843 (register-entry-point
845 (convert-optional-entry
846 ep entry-vars entry-vals nil
847 (or debug-name source-name))
850 (progn (aver (not supplied-p-p))
852 (optional-dispatch-entry-points res))
855 (ir1-convert-more res default-vars default-vals
856 entry-vars entry-vals
857 arg nil nil (rest vars) supplied-p-p body
859 source-name debug-name
860 post-binding-lexenv system-lambda))
862 (ir1-convert-more res default-vars default-vals
863 entry-vars entry-vals
864 nil arg (second vars) (cddr vars) supplied-p-p
865 body aux-vars aux-vals
866 source-name debug-name
867 post-binding-lexenv system-lambda))
869 (ir1-convert-more res default-vars default-vals
870 entry-vars entry-vals
871 nil nil nil vars supplied-p-p body aux-vars
872 aux-vals source-name debug-name
873 post-binding-lexenv system-lambda)))))))
875 ;;; This function deals with the case where we have to make an
876 ;;; OPTIONAL-DISPATCH to represent a LAMBDA. We cons up the result and
877 ;;; call IR1-CONVERT-HAIRY-ARGS to do the work. When it is done, we
878 ;;; figure out the MIN-ARGS and MAX-ARGS.
879 (defun ir1-convert-hairy-lambda (body vars keyp allowp aux-vars aux-vals
880 &key post-binding-lexenv
881 (source-name '.anonymous.)
882 debug-name system-lambda)
883 (declare (list body vars aux-vars aux-vals))
884 (aver (or debug-name (neq '.anonymous. source-name)))
885 (let ((res (make-optional-dispatch :arglist vars
888 :%source-name source-name
889 :%debug-name debug-name
890 :plist `(:ir1-environment
893 (min (or (position-if #'lambda-var-arg-info vars) (length vars))))
894 (aver-live-component *current-component*)
895 (push res (component-new-functionals *current-component*))
896 (ir1-convert-hairy-args res () () () () vars nil body aux-vars aux-vals
897 source-name debug-name nil post-binding-lexenv
899 (setf (optional-dispatch-min-args res) min)
900 (setf (optional-dispatch-max-args res)
901 (+ (1- (length (optional-dispatch-entry-points res))) min))
905 ;;; Convert a LAMBDA form into a LAMBDA leaf or an OPTIONAL-DISPATCH leaf.
906 (defun ir1-convert-lambda (form &key (source-name '.anonymous.)
907 debug-name maybe-add-debug-catch
910 (compiler-error "A ~S was found when expecting a lambda expression:~% ~S"
913 (unless (eq (car form) 'lambda)
914 (compiler-error "~S was expected but ~S was found:~% ~S"
918 (unless (and (consp (cdr form)) (listp (cadr form)))
920 "The lambda expression has a missing or non-list lambda list:~% ~S"
922 (when (and system-lambda maybe-add-debug-catch)
923 (bug "Both SYSTEM-LAMBDA and MAYBE-ADD-DEBUG-CATCH specified"))
924 (unless (or debug-name (neq '.anonymous. source-name))
925 (setf debug-name (name-lambdalike form)))
926 (multiple-value-bind (vars keyp allow-other-keys aux-vars aux-vals)
927 (make-lambda-vars (cadr form))
928 (multiple-value-bind (forms decls doc) (parse-body (cddr form))
929 (binding* (((*lexenv* result-type post-binding-lexenv)
930 (process-decls decls (append aux-vars vars) nil
932 (debug-catch-p (and maybe-add-debug-catch
933 *allow-instrumenting*
935 (>= insert-debug-catch 2))))
936 (forms (if debug-catch-p
937 (wrap-forms-in-debug-catch forms)
939 (forms (if (eq result-type *wild-type*)
941 `((the ,result-type (progn ,@forms)))))
942 (*allow-instrumenting* (and (not system-lambda) *allow-instrumenting*))
943 (res (cond ((or (find-if #'lambda-var-arg-info vars) keyp)
944 (ir1-convert-hairy-lambda forms vars keyp
947 :post-binding-lexenv post-binding-lexenv
948 :source-name source-name
949 :debug-name debug-name
950 :system-lambda system-lambda))
952 (ir1-convert-lambda-body forms vars
955 :post-binding-lexenv post-binding-lexenv
956 :source-name source-name
957 :debug-name debug-name
958 :system-lambda system-lambda)))))
959 (setf (functional-inline-expansion res) form)
960 (setf (functional-arg-documentation res) (cadr form))
961 (setf (functional-documentation res) doc)
962 (when (boundp '*lambda-conversions*)
963 ;; KLUDGE: Not counting TL-XEPs is a lie, of course, but
964 ;; keeps things less confusing to users of TIME, where this
966 (unless (and (consp debug-name) (eq 'tl-xep (car debug-name)))
967 (incf *lambda-conversions*)))
970 (defun wrap-forms-in-debug-catch (forms)
971 #!+unwind-to-frame-and-call-vop
972 `((multiple-value-prog1
975 ;; Just ensure that there won't be any tail-calls, IR2 magic will
978 #!-unwind-to-frame-and-call-vop
979 `( ;; Normally, we'll return from this block with the below RETURN-FROM.
982 ;; If DEBUG-CATCH-TAG is thrown (with a thunk as the value) the
983 ;; RETURN-FROM is elided and we funcall the thunk instead. That
984 ;; thunk might either return a value (for a RETURN-FROM-FRAME)
985 ;; or call this same function again (for a RESTART-FRAME).
986 ;; -- JES, 2007-01-09
989 ;; Use a constant catch tag instead of consing a new one for every
990 ;; entry to this block. The uniquencess of the catch tags is
991 ;; ensured when the tag is throw by the debugger. It'll allocate a
992 ;; new tag, and modify the reference this tag in the proper
993 ;; catch-block structure to refer to that new tag. This
994 ;; significantly decreases the runtime cost of high debug levels.
995 ;; -- JES, 2007-01-09
996 (catch 'debug-catch-tag
997 (return-from return-value-tag
1001 ;;; helper for LAMBDA-like things, to massage them into a form
1002 ;;; suitable for IR1-CONVERT-LAMBDA.
1003 (defun ir1-convert-lambdalike (thing
1005 (source-name '.anonymous.)
1007 (when (and (not debug-name) (eq '.anonymous. source-name))
1008 (setf debug-name (name-lambdalike thing)))
1011 (ir1-convert-lambda thing
1012 :maybe-add-debug-catch t
1013 :source-name source-name
1014 :debug-name debug-name))
1016 (let ((name (cadr thing))
1017 (lambda-expression `(lambda ,@(cddr thing))))
1018 (if (and name (legal-fun-name-p name))
1019 (let ((defined-fun-res (get-defined-fun name (second lambda-expression)))
1020 (res (ir1-convert-lambda lambda-expression
1021 :maybe-add-debug-catch t
1022 :source-name name)))
1023 (assert-global-function-definition-type name res)
1024 (push res (defined-fun-functionals defined-fun-res))
1025 (unless (eq (defined-fun-inlinep defined-fun-res) :notinline)
1028 (policy ref (> recognize-self-calls 0)))
1029 res defined-fun-res))
1031 (ir1-convert-lambda lambda-expression
1032 :maybe-add-debug-catch t
1034 (or name (name-lambdalike thing))))))
1035 ((lambda-with-lexenv)
1036 (ir1-convert-inline-lambda thing
1037 :source-name source-name
1038 :debug-name debug-name))))
1040 ;;;; defining global functions
1042 ;;; Convert FUN as a lambda in the null environment, but use the
1043 ;;; current compilation policy. Note that FUN may be a
1044 ;;; LAMBDA-WITH-LEXENV, so we may have to augment the environment to
1045 ;;; reflect the state at the definition site.
1046 (defun ir1-convert-inline-lambda (fun
1048 (source-name '.anonymous.)
1051 (when (and (not debug-name) (eq '.anonymous. source-name))
1052 (setf debug-name (name-lambdalike fun)))
1053 (destructuring-bind (decls macros symbol-macros &rest body)
1054 (if (eq (car fun) 'lambda-with-lexenv)
1056 `(() () () . ,(cdr fun)))
1057 (let* ((*lexenv* (make-lexenv
1058 :default (process-decls decls nil nil
1059 :lexenv (make-null-lexenv))
1060 :vars (copy-list symbol-macros)
1061 :funs (mapcar (lambda (x)
1063 (macro . ,(coerce (cdr x) 'function))))
1065 ;; Inherit MUFFLE-CONDITIONS from the call-site lexenv
1066 ;; rather than the definition-site lexenv, since it seems
1067 ;; like a much more common case.
1068 :handled-conditions (lexenv-handled-conditions *lexenv*)
1069 :policy (lexenv-policy *lexenv*)))
1070 (clambda (ir1-convert-lambda `(lambda ,@body)
1071 :source-name source-name
1072 :debug-name debug-name
1073 :system-lambda system-lambda)))
1074 (setf (functional-inline-expanded clambda) t)
1077 ;;; Given a lambda-list, return a FUN-TYPE object representing the signature:
1078 ;;; return type is *, and each individual arguments type is T -- but we get
1079 ;;; the argument counts and keywords.
1080 (defun ftype-from-lambda-list (lambda-list)
1081 (multiple-value-bind (req opt restp rest-name keyp key-list allowp morep)
1082 (parse-lambda-list lambda-list)
1083 (declare (ignore rest-name))
1085 (mapcar (constantly t) list)))
1086 (let ((reqs (t req))
1087 (opts (when opt (cons '&optional (t opt))))
1088 ;; When it comes to building a type, &REST means pretty much the
1089 ;; same thing as &MORE.
1090 (rest (when (or morep restp) (list '&rest t)))
1092 (cons '&key (mapcar (lambda (spec)
1093 (let ((key/var (if (consp spec)
1096 (list (if (consp key/var)
1098 (keywordicate key/var))
1101 (allow (when allowp (list '&allow-other-keys))))
1102 (specifier-type `(function (,@reqs ,@opts ,@rest ,@keys ,@allow) *))))))
1104 ;;; Get a DEFINED-FUN object for a function we are about to define. If
1105 ;;; the function has been forward referenced, then substitute for the
1106 ;;; previous references.
1107 (defun get-defined-fun (name &optional (lambda-list nil lp))
1108 (proclaim-as-fun-name name)
1109 (when (boundp '*free-funs*)
1110 (let ((found (find-free-fun name "shouldn't happen! (defined-fun)")))
1111 (note-name-defined name :function)
1112 (cond ((not (defined-fun-p found))
1113 (aver (not (info :function :inlinep name)))
1114 (let* ((where-from (leaf-where-from found))
1115 (res (make-defined-fun
1117 :where-from (if (eq where-from :declared)
1120 :type (if (eq :declared where-from)
1123 (ftype-from-lambda-list lambda-list)
1124 (specifier-type 'function))))))
1125 (substitute-leaf res found)
1126 (setf (gethash name *free-funs*) res)))
1127 ;; If *FREE-FUNS* has a previously converted definition
1128 ;; for this name, then blow it away and try again.
1129 ((defined-fun-functionals found)
1130 (remhash name *free-funs*)
1131 (get-defined-fun name lambda-list))
1134 ;;; Check a new global function definition for consistency with
1135 ;;; previous declaration or definition, and assert argument/result
1136 ;;; types if appropriate. This assertion is suppressed by the
1137 ;;; EXPLICIT-CHECK attribute, which is specified on functions that
1138 ;;; check their argument types as a consequence of type dispatching.
1139 ;;; This avoids redundant checks such as NUMBERP on the args to +, etc.
1140 (defun assert-new-definition (var fun)
1141 (let ((type (leaf-type var))
1142 (for-real (eq (leaf-where-from var) :declared))
1143 (info (info :function :info (leaf-source-name var))))
1144 (assert-definition-type
1146 ;; KLUDGE: Common Lisp is such a dynamic language that in general
1147 ;; all we can do here in general is issue a STYLE-WARNING. It
1148 ;; would be nice to issue a full WARNING in the special case of
1149 ;; of type mismatches within a compilation unit (as in section
1150 ;; 3.2.2.3 of the spec) but at least as of sbcl-0.6.11, we don't
1151 ;; keep track of whether the mismatched data came from the same
1152 ;; compilation unit, so we can't do that. -- WHN 2001-02-11
1153 :lossage-fun #'compiler-style-warn
1154 :unwinnage-fun (cond (info #'compiler-style-warn)
1155 (for-real #'compiler-notify)
1160 (ir1-attributep (fun-info-attributes info)
1163 "previous declaration"
1164 "previous definition"))))
1166 ;;; Used for global inline expansion. Earlier something like this was
1167 ;;; used by %DEFUN too. FIXME: And now it's probably worth rethinking
1168 ;;; whether this function is a good idea at all.
1169 (defun ir1-convert-inline-expansion (name expansion var inlinep info)
1170 ;; Unless a :INLINE function, we temporarily clobber the inline
1171 ;; expansion. This prevents recursive inline expansion of
1172 ;; opportunistic pseudo-inlines.
1173 (unless (eq inlinep :inline)
1174 (setf (defined-fun-inline-expansion var) nil))
1175 (let ((fun (ir1-convert-inline-lambda expansion
1177 ;; prevent instrumentation of
1178 ;; known function expansions
1179 :system-lambda (and info t))))
1180 (setf (functional-inlinep fun) inlinep)
1181 (assert-new-definition var fun)
1182 (setf (defined-fun-inline-expansion var) expansion)
1183 ;; Associate VAR with the FUN -- and in case of an optional dispatch
1184 ;; with the various entry-points. This allows XREF to know where the
1185 ;; inline CLAMBDA comes from.
1186 (flet ((note-inlining (f)
1189 (setf (functional-inline-expanded f) var))
1191 ;; Delayed entry-point.
1193 (setf (functional-inline-expanded (cdr f)) var)
1194 (let ((old-thunk (cdr f)))
1195 (setf (cdr f) (lambda ()
1196 (let ((g (funcall old-thunk)))
1197 (setf (functional-inline-expanded g) var)
1200 (when (optional-dispatch-p fun)
1201 (note-inlining (optional-dispatch-main-entry fun))
1202 (note-inlining (optional-dispatch-more-entry fun))
1203 (mapc #'note-inlining (optional-dispatch-entry-points fun))))
1204 ;; substitute for any old references
1205 (unless (or (not *block-compile*)
1207 (or (fun-info-transforms info)
1208 (fun-info-templates info)
1209 (fun-info-ir2-convert info))))
1210 (substitute-leaf fun var))
1213 (defun %set-inline-expansion (name defined-fun inline-lambda)
1214 (cond (inline-lambda
1215 (setf (info :function :inline-expansion-designator name)
1218 (setf (defined-fun-inline-expansion defined-fun)
1221 (clear-info :function :inline-expansion-designator name))))
1223 ;;; the even-at-compile-time part of DEFUN
1225 ;;; The INLINE-LAMBDA is a LAMBDA-WITH-LEXENV, or NIL if there is no
1226 ;;; inline expansion.
1227 (defun %compiler-defun (name inline-lambda compile-toplevel)
1228 (let ((defined-fun nil)) ; will be set below if we're in the compiler
1229 (when compile-toplevel
1230 (with-single-package-locked-error
1231 (:symbol name "defining ~S as a function")
1234 (get-defined-fun name (fifth inline-lambda))
1235 (get-defined-fun name))))
1236 (when (boundp '*lexenv*)
1237 (remhash name *free-funs*)
1238 (aver (fasl-output-p *compile-object*))
1239 (if (member name *fun-names-in-this-file* :test #'equal)
1240 (warn 'duplicate-definition :name name)
1241 (push name *fun-names-in-this-file*)))
1242 (%set-inline-expansion name defined-fun inline-lambda))
1244 (become-defined-fun-name name)
1246 ;; old CMU CL comment:
1247 ;; If there is a type from a previous definition, blast it,
1248 ;; since it is obsolete.
1249 (when (and defined-fun (neq :declared (leaf-where-from defined-fun)))
1250 (setf (leaf-type defined-fun)
1251 ;; FIXME: If this is a block compilation thing, shouldn't
1252 ;; we be setting the type to the full derived type for the
1253 ;; definition, instead of this most general function type?
1254 (specifier-type 'function))))
1259 ;;; Entry point utilities
1261 ;;; Return a function for the Nth entry point.
1262 (defun optional-dispatch-entry-point-fun (dispatcher n)
1263 (declare (type optional-dispatch dispatcher)
1264 (type unsigned-byte n))
1265 (let* ((env (getf (optional-dispatch-plist dispatcher) :ir1-environment))
1266 (*lexenv* (first env))
1267 (*current-path* (second env)))
1268 (force (nth n (optional-dispatch-entry-points dispatcher)))))