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) lambda-var) varify-lambda-arg))
28 (defun varify-lambda-arg (name names-so-far)
29 (declare (inline member))
30 (unless (symbolp name)
31 (compiler-error "The lambda variable ~S is not a symbol." name))
32 (when (member name names-so-far :test #'eq)
33 (compiler-error "The variable ~S occurs more than once in the lambda list."
35 (let ((kind (info :variable :kind name)))
36 (when (or (keywordp name) (eq kind :constant))
37 (compiler-error "The name of the lambda variable ~S is already in use to name a constant."
39 (cond ((eq kind :special)
40 (let ((specvar (find-free-var name)))
41 (make-lambda-var :%source-name name
42 :type (leaf-type specvar)
43 :where-from (leaf-where-from specvar)
46 (make-lambda-var :%source-name name)))))
48 ;;; Make the default keyword for a &KEY arg, checking that the keyword
49 ;;; isn't already used by one of the VARS.
50 (declaim (ftype (sfunction (symbol list t) symbol) make-keyword-for-arg))
51 (defun make-keyword-for-arg (symbol vars keywordify)
52 (let ((key (if (and keywordify (not (keywordp symbol)))
56 (let ((info (lambda-var-arg-info var)))
58 (eq (arg-info-kind info) :keyword)
59 (eq (arg-info-key info) key))
61 "The keyword ~S appears more than once in the lambda list."
65 ;;; Parse a lambda list into a list of VAR structures, stripping off
66 ;;; any &AUX bindings. Each arg name is checked for legality, and
67 ;;; duplicate names are checked for. If an arg is globally special,
68 ;;; the var is marked as :SPECIAL instead of :LEXICAL. &KEY,
69 ;;; &OPTIONAL and &REST args are annotated with an ARG-INFO structure
70 ;;; which contains the extra information. If we hit something losing,
71 ;;; we bug out with COMPILER-ERROR. These values are returned:
72 ;;; 1. a list of the var structures for each top level argument;
73 ;;; 2. a flag indicating whether &KEY was specified;
74 ;;; 3. a flag indicating whether other &KEY args are allowed;
75 ;;; 4. a list of the &AUX variables; and
76 ;;; 5. a list of the &AUX values.
77 (declaim (ftype (sfunction (list) (values list boolean boolean list list))
79 (defun make-lambda-vars (list)
80 (multiple-value-bind (required optional restp rest keyp keys allowp auxp aux
81 morep more-context more-count)
82 (parse-lambda-list list)
83 (declare (ignore auxp)) ; since we just iterate over AUX regardless
88 (flet (;; PARSE-DEFAULT deals with defaults and supplied-p args
89 ;; for optionals and keywords args.
90 (parse-default (spec info)
91 (when (consp (cdr spec))
92 (setf (arg-info-default info) (second spec))
93 (when (consp (cddr spec))
94 (let* ((supplied-p (third spec))
95 (supplied-var (varify-lambda-arg supplied-p
97 (setf (arg-info-supplied-p info) supplied-var)
98 (names-so-far supplied-p)
99 (when (> (length (the list spec)) 3)
101 "The list ~S is too long to be an arg specifier."
104 (dolist (name required)
105 (let ((var (varify-lambda-arg name (names-so-far))))
107 (names-so-far name)))
109 (dolist (spec optional)
111 (let ((var (varify-lambda-arg spec (names-so-far))))
112 (setf (lambda-var-arg-info var)
113 (make-arg-info :kind :optional))
116 (let* ((name (first spec))
117 (var (varify-lambda-arg name (names-so-far)))
118 (info (make-arg-info :kind :optional)))
119 (setf (lambda-var-arg-info var) info)
122 (parse-default spec info))))
125 (let ((var (varify-lambda-arg rest (names-so-far))))
126 (setf (lambda-var-arg-info var) (make-arg-info :kind :rest))
128 (names-so-far rest)))
131 (let ((var (varify-lambda-arg more-context (names-so-far))))
132 (setf (lambda-var-arg-info var)
133 (make-arg-info :kind :more-context))
135 (names-so-far more-context))
136 (let ((var (varify-lambda-arg more-count (names-so-far))))
137 (setf (lambda-var-arg-info var)
138 (make-arg-info :kind :more-count))
140 (names-so-far more-count)))
145 (let ((var (varify-lambda-arg spec (names-so-far))))
146 (setf (lambda-var-arg-info var)
147 (make-arg-info :kind :keyword
148 :key (make-keyword-for-arg spec
152 (names-so-far spec)))
154 (let* ((name (first spec))
155 (var (varify-lambda-arg name (names-so-far)))
158 :key (make-keyword-for-arg name (vars) t))))
159 (setf (lambda-var-arg-info var) info)
162 (parse-default spec info)))
164 (let ((head (first spec)))
165 (unless (proper-list-of-length-p head 2)
166 (error "malformed &KEY argument specifier: ~S" spec))
167 (let* ((name (second head))
168 (var (varify-lambda-arg name (names-so-far)))
171 :key (make-keyword-for-arg (first head)
174 (setf (lambda-var-arg-info var) info)
177 (parse-default spec info))))))
181 (let ((var (varify-lambda-arg spec nil)))
184 (names-so-far spec)))
186 (unless (proper-list-of-length-p spec 1 2)
187 (compiler-error "malformed &AUX binding specifier: ~S"
189 (let* ((name (first spec))
190 (var (varify-lambda-arg name nil)))
192 (aux-vals (second spec))
193 (names-so-far name)))))
195 (values (vars) keyp allowp (aux-vars) (aux-vals))))))
197 ;;; This is similar to IR1-CONVERT-PROGN-BODY except that we
198 ;;; sequentially bind each AUX-VAR to the corresponding AUX-VAL before
199 ;;; converting the body. If there are no bindings, just convert the
200 ;;; body, otherwise do one binding and recurse on the rest.
202 ;;; FIXME: This could and probably should be converted to use
203 ;;; SOURCE-NAME and DEBUG-NAME. But I (WHN) don't use &AUX bindings,
204 ;;; so I'm not motivated. Patches will be accepted...
205 (defun ir1-convert-aux-bindings (start next result body aux-vars aux-vals
207 (declare (type ctran start next) (type (or lvar null) result)
208 (list body aux-vars aux-vals))
210 (let ((*lexenv* (make-lexenv :vars (copy-list post-binding-lexenv))))
211 (ir1-convert-progn-body start next result body))
212 (let ((ctran (make-ctran))
213 (fun-lvar (make-lvar))
214 (fun (ir1-convert-lambda-body body
215 (list (first aux-vars))
216 :aux-vars (rest aux-vars)
217 :aux-vals (rest aux-vals)
218 :post-binding-lexenv post-binding-lexenv
219 :debug-name (debug-name
222 (reference-leaf start ctran fun-lvar fun)
223 (ir1-convert-combination-args fun-lvar ctran next result
224 (list (first aux-vals)))))
227 ;;; This is similar to IR1-CONVERT-PROGN-BODY except that code to bind
228 ;;; the SPECVAR for each SVAR to the value of the variable is wrapped
229 ;;; around the body. If there are no special bindings, we just convert
230 ;;; the body, otherwise we do one special binding and recurse on the
233 ;;; We make a cleanup and introduce it into the lexical
234 ;;; environment. If there are multiple special bindings, the cleanup
235 ;;; for the blocks will end up being the innermost one. We force NEXT
236 ;;; to start a block outside of this cleanup, causing cleanup code to
237 ;;; be emitted when the scope is exited.
238 (defun ir1-convert-special-bindings
239 (start next result body aux-vars aux-vals svars post-binding-lexenv)
240 (declare (type ctran start next) (type (or lvar null) result)
241 (list body aux-vars aux-vals svars))
244 (ir1-convert-aux-bindings start next result body aux-vars aux-vals
245 post-binding-lexenv))
247 (ctran-starts-block next)
248 (let ((cleanup (make-cleanup :kind :special-bind))
250 (bind-ctran (make-ctran))
251 (cleanup-ctran (make-ctran)))
252 (ir1-convert start bind-ctran nil
253 `(%special-bind ',(lambda-var-specvar var) ,var))
254 (setf (cleanup-mess-up cleanup) (ctran-use bind-ctran))
255 (let ((*lexenv* (make-lexenv :cleanup cleanup)))
256 (ir1-convert bind-ctran cleanup-ctran nil '(%cleanup-point))
257 (ir1-convert-special-bindings cleanup-ctran next result
258 body aux-vars aux-vals
260 post-binding-lexenv)))))
263 ;;; Create a lambda node out of some code, returning the result. The
264 ;;; bindings are specified by the list of VAR structures VARS. We deal
265 ;;; with adding the names to the LEXENV-VARS for the conversion. The
266 ;;; result is added to the NEW-FUNCTIONALS in the *CURRENT-COMPONENT*
267 ;;; and linked to the component head and tail.
269 ;;; We detect special bindings here, replacing the original VAR in the
270 ;;; lambda list with a temporary variable. We then pass a list of the
271 ;;; special vars to IR1-CONVERT-SPECIAL-BINDINGS, which actually emits
272 ;;; the special binding code.
274 ;;; We ignore any ARG-INFO in the VARS, trusting that someone else is
275 ;;; dealing with &NONSENSE, except for &REST vars with DYNAMIC-EXTENT.
277 ;;; AUX-VARS is a list of VAR structures for variables that are to be
278 ;;; sequentially bound. Each AUX-VAL is a form that is to be evaluated
279 ;;; to get the initial value for the corresponding AUX-VAR.
280 (defun ir1-convert-lambda-body (body
285 (source-name '.anonymous.)
287 (note-lexical-bindings t)
290 (declare (list body vars aux-vars aux-vals))
292 ;; We're about to try to put new blocks into *CURRENT-COMPONENT*.
293 (aver-live-component *current-component*)
295 (let* ((bind (make-bind))
296 (lambda (make-lambda :vars vars
298 :%source-name source-name
299 :%debug-name debug-name
300 :system-lambda-p system-lambda))
301 (result-ctran (make-ctran))
302 (result-lvar (make-lvar)))
304 (awhen (lexenv-lambda *lexenv*)
305 (push lambda (lambda-children it))
306 (setf (lambda-parent lambda) it))
308 ;; just to check: This function should fail internal assertions if
309 ;; we didn't set up a valid debug name above.
311 ;; (In SBCL we try to make everything have a debug name, since we
312 ;; lack the omniscient perspective the original implementors used
313 ;; to decide which things didn't need one.)
314 (functional-debug-name lambda)
316 (setf (lambda-home lambda) lambda)
321 ;; As far as I can see, LAMBDA-VAR-HOME should never have
322 ;; been set before. Let's make sure. -- WHN 2001-09-29
323 (aver (not (lambda-var-home var)))
324 (setf (lambda-var-home var) lambda)
325 (let ((specvar (lambda-var-specvar var)))
328 (new-venv (cons (leaf-source-name specvar) specvar)))
330 (when note-lexical-bindings
331 (note-lexical-binding (leaf-source-name var)))
332 (new-venv (cons (leaf-source-name var) var))))))
334 (let ((*lexenv* (make-lexenv :vars (new-venv)
337 (setf (bind-lambda bind) lambda)
338 (setf (node-lexenv bind) *lexenv*)
340 (let ((block (ctran-starts-block result-ctran)))
341 (let ((return (make-return :result result-lvar :lambda lambda))
342 (tail-set (make-tail-set :funs (list lambda))))
343 (setf (lambda-tail-set lambda) tail-set)
344 (setf (lambda-return lambda) return)
345 (setf (lvar-dest result-lvar) return)
346 (link-node-to-previous-ctran return result-ctran)
347 (setf (block-last block) return))
348 (link-blocks block (component-tail *current-component*)))
350 (with-component-last-block (*current-component*
351 (ctran-block result-ctran))
352 (let ((prebind-ctran (make-ctran))
353 (postbind-ctran (make-ctran)))
354 (ctran-starts-block prebind-ctran)
355 (link-node-to-previous-ctran bind prebind-ctran)
356 (use-ctran bind postbind-ctran)
357 (ir1-convert-special-bindings postbind-ctran result-ctran
359 aux-vars aux-vals (svars)
360 post-binding-lexenv)))))
362 (link-blocks (component-head *current-component*) (node-block bind))
363 (push lambda (component-new-functionals *current-component*))
367 ;;; Entry point CLAMBDAs have a special kind
368 (defun register-entry-point (entry dispatcher)
369 (declare (type clambda entry)
370 (type optional-dispatch dispatcher))
371 (setf (functional-kind entry) :optional)
372 (setf (leaf-ever-used entry) t)
373 (setf (lambda-optional-dispatch entry) dispatcher)
376 ;;; Create the actual entry-point function for an optional entry
377 ;;; point. The lambda binds copies of each of the VARS, then calls FUN
378 ;;; with the argument VALS and the DEFAULTS. Presumably the VALS refer
379 ;;; to the VARS by name. The VALS are passed in the reverse order.
381 ;;; If any of the copies of the vars are referenced more than once,
382 ;;; then we mark the corresponding var as EVER-USED to inhibit
383 ;;; "defined but not read" warnings for arguments that are only used
384 ;;; by default forms.
385 (defun convert-optional-entry (fun vars vals defaults name)
386 (declare (type clambda fun) (list vars vals defaults))
387 (let* ((fvars (reverse vars))
388 (arg-vars (mapcar (lambda (var)
390 :%source-name (leaf-source-name var)
391 :type (leaf-type var)
392 :where-from (leaf-where-from var)
393 :specvar (lambda-var-specvar var)))
395 (fun (collect ((default-bindings)
397 (dolist (default defaults)
398 (if (sb!xc:constantp default)
399 (default-vals default)
400 (let ((var (gensym)))
401 (default-bindings `(,var ,default))
402 (default-vals var))))
403 (let ((bindings (default-bindings))
404 (call `(%funcall ,fun ,@(reverse vals) ,@(default-vals))))
405 (ir1-convert-lambda-body (if bindings
406 `((let (,@bindings) ,call))
409 ;; FIXME: Would be nice to
410 ;; share these names instead
411 ;; of consing up several
412 ;; identical ones. Oh well.
413 :debug-name (debug-name
416 :note-lexical-bindings nil
417 :system-lambda t)))))
418 (mapc (lambda (var arg-var)
419 (when (cdr (leaf-refs arg-var))
420 (setf (leaf-ever-used var) t)))
424 ;;; This function deals with supplied-p vars in optional arguments. If
425 ;;; the there is no supplied-p arg, then we just call
426 ;;; IR1-CONVERT-HAIRY-ARGS on the remaining arguments, and generate a
427 ;;; optional entry that calls the result. If there is a supplied-p
428 ;;; var, then we add it into the default vars and throw a T into the
429 ;;; entry values. The resulting entry point function is returned.
430 (defun generate-optional-default-entry (res default-vars default-vals
431 entry-vars entry-vals
432 vars supplied-p-p body
434 source-name debug-name
435 force post-binding-lexenv
437 (declare (type optional-dispatch res)
438 (list default-vars default-vals entry-vars entry-vals vars body
440 (let* ((arg (first vars))
441 (arg-name (leaf-source-name arg))
442 (info (lambda-var-arg-info arg))
443 (default (arg-info-default info))
444 (supplied-p (arg-info-supplied-p info))
446 (not (sb!xc:constantp (arg-info-default info)))))
448 (ir1-convert-hairy-args
450 (list* supplied-p arg default-vars)
451 (list* (leaf-source-name supplied-p) arg-name default-vals)
452 (cons arg entry-vars)
453 (list* t arg-name entry-vals)
454 (rest vars) t body aux-vars aux-vals
455 source-name debug-name
456 force post-binding-lexenv system-lambda)
457 (ir1-convert-hairy-args
459 (cons arg default-vars)
460 (cons arg-name default-vals)
461 (cons arg entry-vars)
462 (cons arg-name entry-vals)
463 (rest vars) supplied-p-p body aux-vars aux-vals
464 source-name debug-name
465 force post-binding-lexenv system-lambda))))
467 ;; We want to delay converting the entry, but there exist
468 ;; problems: hidden references should not be established to
469 ;; lambdas of kind NIL should not have (otherwise the compiler
470 ;; might let-convert or delete them) and to variables.
471 (let ((name (or debug-name source-name)))
473 supplied-p-p ; this entry will be of kind NIL
474 (and (lambda-p ep) (eq (lambda-kind ep) nil)))
475 (convert-optional-entry ep
476 default-vars default-vals
477 (if supplied-p (list default nil) (list default))
479 (let* ((default `',(constant-form-value default))
480 (defaults (if supplied-p (list default nil) (list default))))
481 ;; DEFAULT can contain a reference to a
482 ;; to-be-optimized-away function/block/tag, so better to
483 ;; reduce code now (but we possibly lose syntax checking
484 ;; in an unreachable code).
486 (register-entry-point
487 (convert-optional-entry (force ep)
488 default-vars default-vals
493 ;;; Create the MORE-ENTRY function for the OPTIONAL-DISPATCH RES.
494 ;;; ENTRY-VARS and ENTRY-VALS describe the fixed arguments. REST is
495 ;;; the var for any &REST arg. KEYS is a list of the &KEY arg vars.
497 ;;; The most interesting thing that we do is parse keywords. We create
498 ;;; a bunch of temporary variables to hold the result of the parse,
499 ;;; and then loop over the supplied arguments, setting the appropriate
500 ;;; temps for the supplied keyword. Note that it is significant that
501 ;;; we iterate over the keywords in reverse order --- this implements
502 ;;; the CL requirement that (when a keyword appears more than once)
503 ;;; the first value is used.
505 ;;; If there is no supplied-p var, then we initialize the temp to the
506 ;;; default and just pass the temp into the main entry. Since
507 ;;; non-constant &KEY args are forcibly given a supplied-p var, we
508 ;;; know that the default is constant, and thus safe to evaluate out
511 ;;; If there is a supplied-p var, then we create temps for both the
512 ;;; value and the supplied-p, and pass them into the main entry,
513 ;;; letting it worry about defaulting.
515 ;;; We deal with :ALLOW-OTHER-KEYS by delaying unknown keyword errors
516 ;;; until we have scanned all the keywords.
517 (defun convert-more-entry (res entry-vars entry-vals rest morep keys name)
518 (declare (type optional-dispatch res) (list entry-vars entry-vals keys))
520 (arg-vals (reverse entry-vals))
524 (dolist (var (reverse entry-vars))
525 (arg-vars (make-lambda-var :%source-name (leaf-source-name var)
526 :type (leaf-type var)
527 :where-from (leaf-where-from var))))
529 (let* ((n-context (gensym "N-CONTEXT-"))
530 (context-temp (make-lambda-var :%source-name n-context))
531 (n-count (gensym "N-COUNT-"))
532 (count-temp (make-lambda-var :%source-name n-count
533 :type (specifier-type 'index))))
535 (arg-vars context-temp count-temp)
538 (arg-vals `(%listify-rest-args
539 ,n-context ,n-count)))
544 ;; The reason for all the noise with
545 ;; STACK-GROWS-DOWNWARD-NOT-UPWARD is to enable generation of
546 ;; slightly more efficient code on x86oid processors. (We can
547 ;; hoist the negation of the index outside the main parsing loop
548 ;; and take advantage of the base+index+displacement addressing
550 (when (optional-dispatch-keyp res)
551 (let ((n-index (gensym "N-INDEX-"))
552 (n-key (gensym "N-KEY-"))
553 (n-value-temp (gensym "N-VALUE-TEMP-"))
554 (n-allowp (gensym "N-ALLOWP-"))
555 (n-losep (gensym "N-LOSEP-"))
556 (allowp (or (optional-dispatch-allowp res)
557 (policy *lexenv* (zerop safety))))
560 (temps #!-stack-grows-downward-not-upward
561 `(,n-index (1- ,n-count))
562 #!+stack-grows-downward-not-upward
563 `(,n-index (- (1- ,n-count)))
564 #!-stack-grows-downward-not-upward n-value-temp
565 #!-stack-grows-downward-not-upward n-key)
566 (body `(declare (fixnum ,n-index)
567 #!-stack-grows-downward-not-upward
568 (ignorable ,n-value-temp ,n-key)))
572 (let* ((info (lambda-var-arg-info key))
573 (default (arg-info-default info))
574 (keyword (arg-info-key info))
575 (supplied-p (arg-info-supplied-p info))
576 (n-value (gensym "N-VALUE-"))
577 (clause (cond (supplied-p
578 (let ((n-supplied (gensym "N-SUPPLIED-")))
580 (arg-vals n-value n-supplied)
581 `((eq ,n-key ',keyword)
583 (setq ,n-value ,n-value-temp))))
586 `((eq ,n-key ',keyword)
587 (setq ,n-value ,n-value-temp))))))
588 (when (and (not allowp) (eq keyword :allow-other-keys))
589 (setq found-allow-p t)
591 (append clause `((setq ,n-allowp ,n-value-temp)))))
593 (temps `(,n-value ,default))
597 (temps n-allowp n-losep)
598 (unless found-allow-p
599 (tests `((eq ,n-key :allow-other-keys)
600 (setq ,n-allowp ,n-value-temp))))
602 (setq ,n-losep (list ,n-key)))))
605 `(when (oddp ,n-count)
606 (%odd-key-args-error)))
609 #!-stack-grows-downward-not-upward
611 (declare (optimize (safety 0)))
613 (when (minusp ,n-index) (return))
614 (setf ,n-value-temp (%more-arg ,n-context ,n-index))
616 (setq ,n-key (%more-arg ,n-context ,n-index))
619 #!+stack-grows-downward-not-upward
620 `(locally (declare (optimize (safety 0)))
622 (when (plusp ,n-index) (return))
623 (multiple-value-bind (,n-value-temp ,n-key)
624 (%more-kw-arg ,n-context ,n-index)
625 (declare (ignorable ,n-value-temp ,n-key))
630 (body `(when (and ,n-losep (not ,n-allowp))
631 (%unknown-key-arg-error (car ,n-losep))))))))
633 (let ((ep (ir1-convert-lambda-body
636 (%funcall ,(optional-dispatch-main-entry res)
639 :debug-name (debug-name '&more-processor name)
640 :note-lexical-bindings nil
642 (setf (optional-dispatch-more-entry res)
643 (register-entry-point ep res)))))
647 ;;; This is called by IR1-CONVERT-HAIRY-ARGS when we run into a &REST
648 ;;; or &KEY arg. The arguments are similar to that function, but we
649 ;;; split off any &REST arg and pass it in separately. REST is the
650 ;;; &REST arg var, or NIL if there is no &REST arg. KEYS is a list of
651 ;;; the &KEY argument vars.
653 ;;; When there are &KEY arguments, we introduce temporary gensym
654 ;;; variables to hold the values while keyword defaulting is in
655 ;;; progress to get the required sequential binding semantics.
657 ;;; This gets interesting mainly when there are &KEY arguments with
658 ;;; supplied-p vars or non-constant defaults. In either case, pass in
659 ;;; a supplied-p var. If the default is non-constant, we introduce an
660 ;;; IF in the main entry that tests the supplied-p var and decides
661 ;;; whether to evaluate the default or not. In this case, the real
662 ;;; incoming value is NIL, so we must union NULL with the declared
663 ;;; type when computing the type for the main entry's argument.
664 (defun ir1-convert-more (res default-vars default-vals entry-vars entry-vals
665 rest more-context more-count keys supplied-p-p
666 body aux-vars aux-vals source-name debug-name
667 post-binding-lexenv system-lambda)
668 (declare (type optional-dispatch res)
669 (list default-vars default-vals entry-vars entry-vals keys body
671 (collect ((main-vars (reverse default-vars))
672 (main-vals default-vals cons)
679 (main-vars more-context)
681 (main-vars more-count)
685 (let* ((info (lambda-var-arg-info key))
686 (default (arg-info-default info))
687 (hairy-default (not (sb!xc:constantp default)))
688 (supplied-p (arg-info-supplied-p info))
689 (n-val (make-symbol (format nil
691 (leaf-source-name key))))
692 (val-temp (make-lambda-var :%source-name n-val)))
695 (cond ((or hairy-default supplied-p)
696 (let* ((n-supplied (gensym "N-SUPPLIED-"))
697 (supplied-temp (make-lambda-var
698 :%source-name n-supplied)))
700 (setf (arg-info-supplied-p info) supplied-temp))
702 (setf (arg-info-default info) nil))
703 (main-vars supplied-temp)
706 (bind-vals `(if ,n-supplied ,n-val ,default)))
708 (main-vals default nil)
711 (bind-vars supplied-p)
712 (bind-vals n-supplied))))
714 (main-vals (arg-info-default info))
715 (bind-vals n-val)))))
717 (let* ((name (or debug-name source-name))
718 (main-entry (ir1-convert-lambda-body
720 :aux-vars (append (bind-vars) aux-vars)
721 :aux-vals (append (bind-vals) aux-vals)
722 :post-binding-lexenv post-binding-lexenv
723 :debug-name (debug-name 'varargs-entry name)
724 :system-lambda system-lambda))
725 (last-entry (convert-optional-entry main-entry default-vars
726 (main-vals) () name)))
727 (setf (optional-dispatch-main-entry res)
728 (register-entry-point main-entry res))
729 (convert-more-entry res entry-vars entry-vals rest more-context keys
732 (push (register-entry-point
734 (convert-optional-entry last-entry entry-vars entry-vals
738 (optional-dispatch-entry-points res))
741 ;;; This function generates the entry point functions for the
742 ;;; OPTIONAL-DISPATCH RES. We accomplish this by recursion on the list
743 ;;; of arguments, analyzing the arglist on the way down and generating
744 ;;; entry points on the way up.
746 ;;; DEFAULT-VARS is a reversed list of all the argument vars processed
747 ;;; so far, including supplied-p vars. DEFAULT-VALS is a list of the
748 ;;; names of the DEFAULT-VARS.
750 ;;; ENTRY-VARS is a reversed list of processed argument vars,
751 ;;; excluding supplied-p vars. ENTRY-VALS is a list things that can be
752 ;;; evaluated to get the values for all the vars from the ENTRY-VARS.
753 ;;; It has the var name for each required or optional arg, and has T
754 ;;; for each supplied-p arg.
756 ;;; VARS is a list of the LAMBDA-VAR structures for arguments that
757 ;;; haven't been processed yet. SUPPLIED-P-P is true if a supplied-p
758 ;;; argument has already been processed; only in this case are the
759 ;;; DEFAULT-XXX and ENTRY-XXX different.
761 ;;; The result at each point is a lambda which should be called by the
762 ;;; above level to default the remaining arguments and evaluate the
763 ;;; body. We cause the body to be evaluated by converting it and
764 ;;; returning it as the result when the recursion bottoms out.
766 ;;; Each level in the recursion also adds its entry point function to
767 ;;; the result OPTIONAL-DISPATCH. For most arguments, the defaulting
768 ;;; function and the entry point function will be the same, but when
769 ;;; SUPPLIED-P args are present they may be different.
771 ;;; When we run into a &REST or &KEY arg, we punt out to
772 ;;; IR1-CONVERT-MORE, which finishes for us in this case.
773 (defun ir1-convert-hairy-args (res default-vars default-vals
774 entry-vars entry-vals
775 vars supplied-p-p body aux-vars
777 source-name debug-name
778 force post-binding-lexenv
780 (declare (type optional-dispatch res)
781 (list default-vars default-vals entry-vars entry-vals vars body
783 (aver (or debug-name (neq '.anonymous. source-name)))
785 (if (optional-dispatch-keyp res)
786 ;; Handle &KEY with no keys...
787 (ir1-convert-more res default-vars default-vals
788 entry-vars entry-vals
789 nil nil nil vars supplied-p-p body aux-vars
790 aux-vals source-name debug-name
791 post-binding-lexenv system-lambda)
792 (let* ((name (or debug-name source-name))
793 (fun (ir1-convert-lambda-body
794 body (reverse default-vars)
797 :post-binding-lexenv post-binding-lexenv
798 :debug-name (debug-name 'hairy-arg-processor name)
799 :system-lambda system-lambda)))
801 (setf (optional-dispatch-main-entry res) fun)
802 (register-entry-point fun res)
803 (push (if supplied-p-p
804 (register-entry-point
805 (convert-optional-entry fun entry-vars entry-vals ()
809 (optional-dispatch-entry-points res))
811 ((not (lambda-var-arg-info (first vars)))
812 (let* ((arg (first vars))
813 (nvars (cons arg default-vars))
814 (nvals (cons (leaf-source-name arg) default-vals)))
815 (ir1-convert-hairy-args res nvars nvals nvars nvals
816 (rest vars) nil body aux-vars aux-vals
817 source-name debug-name
818 nil post-binding-lexenv system-lambda)))
820 (let* ((arg (first vars))
821 (info (lambda-var-arg-info arg))
822 (kind (arg-info-kind info)))
825 (let ((ep (generate-optional-default-entry
826 res default-vars default-vals
827 entry-vars entry-vals vars supplied-p-p body
829 source-name debug-name
830 force post-binding-lexenv
832 ;; See GENERATE-OPTIONAL-DEFAULT-ENTRY.
833 (push (if (lambda-p ep)
834 (register-entry-point
836 (convert-optional-entry
837 ep entry-vars entry-vals nil
838 (or debug-name source-name))
841 (progn (aver (not supplied-p-p))
843 (optional-dispatch-entry-points res))
846 (ir1-convert-more res default-vars default-vals
847 entry-vars entry-vals
848 arg nil nil (rest vars) supplied-p-p body
850 source-name debug-name
851 post-binding-lexenv system-lambda))
853 (ir1-convert-more res default-vars default-vals
854 entry-vars entry-vals
855 nil arg (second vars) (cddr vars) supplied-p-p
856 body aux-vars aux-vals
857 source-name debug-name
858 post-binding-lexenv system-lambda))
860 (ir1-convert-more res default-vars default-vals
861 entry-vars entry-vals
862 nil nil nil vars supplied-p-p body aux-vars
863 aux-vals source-name debug-name
864 post-binding-lexenv system-lambda)))))))
866 ;;; This function deals with the case where we have to make an
867 ;;; OPTIONAL-DISPATCH to represent a LAMBDA. We cons up the result and
868 ;;; call IR1-CONVERT-HAIRY-ARGS to do the work. When it is done, we
869 ;;; figure out the MIN-ARGS and MAX-ARGS.
870 (defun ir1-convert-hairy-lambda (body vars keyp allowp aux-vars aux-vals
871 &key post-binding-lexenv
872 (source-name '.anonymous.)
873 debug-name system-lambda)
874 (declare (list body vars aux-vars aux-vals))
875 (aver (or debug-name (neq '.anonymous. source-name)))
876 (let ((res (make-optional-dispatch :arglist vars
879 :%source-name source-name
880 :%debug-name debug-name
881 :plist `(:ir1-environment
884 (min (or (position-if #'lambda-var-arg-info vars) (length vars))))
885 (aver-live-component *current-component*)
886 (push res (component-new-functionals *current-component*))
887 (ir1-convert-hairy-args res () () () () vars nil body aux-vars aux-vals
888 source-name debug-name nil post-binding-lexenv
890 (setf (optional-dispatch-min-args res) min)
891 (setf (optional-dispatch-max-args res)
892 (+ (1- (length (optional-dispatch-entry-points res))) min))
896 ;;; Convert a LAMBDA form into a LAMBDA leaf or an OPTIONAL-DISPATCH leaf.
897 (defun ir1-convert-lambda (form &key (source-name '.anonymous.)
898 debug-name maybe-add-debug-catch
901 (compiler-error "A ~S was found when expecting a lambda expression:~% ~S"
904 (unless (eq (car form) 'lambda)
905 (compiler-error "~S was expected but ~S was found:~% ~S"
909 (unless (and (consp (cdr form)) (listp (cadr form)))
911 "The lambda expression has a missing or non-list lambda list:~% ~S"
913 (when (and system-lambda maybe-add-debug-catch)
914 (bug "Both SYSTEM-LAMBDA and MAYBE-ADD-DEBUG-CATCH specified"))
915 (unless (or debug-name (neq '.anonymous. source-name))
916 (setf debug-name (name-lambdalike form)))
917 (multiple-value-bind (vars keyp allow-other-keys aux-vars aux-vals)
918 (make-lambda-vars (cadr form))
919 (multiple-value-bind (forms decls) (parse-body (cddr form))
920 (binding* (((*lexenv* result-type post-binding-lexenv)
921 (process-decls decls (append aux-vars vars) nil
923 (debug-catch-p (and maybe-add-debug-catch
924 *allow-instrumenting*
926 (>= insert-debug-catch 2))))
927 (forms (if debug-catch-p
928 (wrap-forms-in-debug-catch forms)
930 (forms (if (eq result-type *wild-type*)
932 `((the ,result-type (progn ,@forms)))))
933 (*allow-instrumenting* (and (not system-lambda) *allow-instrumenting*))
934 (res (cond ((or (find-if #'lambda-var-arg-info vars) keyp)
935 (ir1-convert-hairy-lambda forms vars keyp
938 :post-binding-lexenv post-binding-lexenv
939 :source-name source-name
940 :debug-name debug-name
941 :system-lambda system-lambda))
943 (ir1-convert-lambda-body forms vars
946 :post-binding-lexenv post-binding-lexenv
947 :source-name source-name
948 :debug-name debug-name
949 :system-lambda system-lambda)))))
950 (setf (functional-inline-expansion res) form)
951 (setf (functional-arg-documentation res) (cadr form))
952 (when (boundp '*lambda-conversions*)
953 ;; KLUDGE: Not counting TL-XEPs is a lie, of course, but
954 ;; keeps things less confusing to users of TIME, where this
956 (unless (and (consp debug-name) (eq 'tl-xep (car debug-name)))
957 (incf *lambda-conversions*)))
960 (defun wrap-forms-in-debug-catch (forms)
961 #!+unwind-to-frame-and-call-vop
962 `((multiple-value-prog1
965 ;; Just ensure that there won't be any tail-calls, IR2 magic will
968 #!-unwind-to-frame-and-call-vop
969 `( ;; Normally, we'll return from this block with the below RETURN-FROM.
972 ;; If DEBUG-CATCH-TAG is thrown (with a thunk as the value) the
973 ;; RETURN-FROM is elided and we funcall the thunk instead. That
974 ;; thunk might either return a value (for a RETURN-FROM-FRAME)
975 ;; or call this same function again (for a RESTART-FRAME).
976 ;; -- JES, 2007-01-09
979 ;; Use a constant catch tag instead of consing a new one for every
980 ;; entry to this block. The uniquencess of the catch tags is
981 ;; ensured when the tag is throw by the debugger. It'll allocate a
982 ;; new tag, and modify the reference this tag in the proper
983 ;; catch-block structure to refer to that new tag. This
984 ;; significantly decreases the runtime cost of high debug levels.
985 ;; -- JES, 2007-01-09
986 (catch 'debug-catch-tag
987 (return-from return-value-tag
991 ;;; helper for LAMBDA-like things, to massage them into a form
992 ;;; suitable for IR1-CONVERT-LAMBDA.
993 (defun ir1-convert-lambdalike (thing
995 (source-name '.anonymous.)
997 (when (and (not debug-name) (eq '.anonymous. source-name))
998 (setf debug-name (name-lambdalike thing)))
1001 (ir1-convert-lambda thing
1002 :maybe-add-debug-catch t
1003 :source-name source-name
1004 :debug-name debug-name))
1006 (deprecation-warning 'instance-lambda 'lambda)
1007 (ir1-convert-lambda `(lambda ,@(cdr thing))
1008 :source-name source-name
1009 :debug-name debug-name))
1011 (let ((name (cadr thing))
1012 (lambda-expression `(lambda ,@(cddr thing))))
1013 (if (and name (legal-fun-name-p name))
1014 (let ((defined-fun-res (get-defined-fun name))
1015 (res (ir1-convert-lambda lambda-expression
1016 :maybe-add-debug-catch t
1017 :source-name name)))
1018 (assert-global-function-definition-type name res)
1019 (push res (defined-fun-functionals defined-fun-res))
1020 (unless (eq (defined-fun-inlinep defined-fun-res) :notinline)
1023 (policy ref (> recognize-self-calls 0)))
1024 res defined-fun-res))
1026 (ir1-convert-lambda lambda-expression
1027 :maybe-add-debug-catch t
1029 (or name (name-lambdalike thing))))))
1030 ((lambda-with-lexenv)
1031 (ir1-convert-inline-lambda thing
1032 :source-name source-name
1033 :debug-name debug-name))))
1035 ;;;; defining global functions
1037 ;;; Convert FUN as a lambda in the null environment, but use the
1038 ;;; current compilation policy. Note that FUN may be a
1039 ;;; LAMBDA-WITH-LEXENV, so we may have to augment the environment to
1040 ;;; reflect the state at the definition site.
1041 (defun ir1-convert-inline-lambda (fun
1043 (source-name '.anonymous.)
1046 (when (and (not debug-name) (eq '.anonymous. source-name))
1047 (setf debug-name (name-lambdalike fun)))
1048 (destructuring-bind (decls macros symbol-macros &rest body)
1049 (if (eq (car fun) 'lambda-with-lexenv)
1051 `(() () () . ,(cdr fun)))
1052 (let* ((*lexenv* (make-lexenv
1053 :default (process-decls decls nil nil
1054 :lexenv (make-null-lexenv))
1055 :vars (copy-list symbol-macros)
1056 :funs (mapcar (lambda (x)
1058 (macro . ,(coerce (cdr x) 'function))))
1060 ;; Inherit MUFFLE-CONDITIONS from the call-site lexenv
1061 ;; rather than the definition-site lexenv, since it seems
1062 ;; like a much more common case.
1063 :handled-conditions (lexenv-handled-conditions *lexenv*)
1064 :policy (lexenv-policy *lexenv*)))
1065 (clambda (ir1-convert-lambda `(lambda ,@body)
1066 :source-name source-name
1067 :debug-name debug-name
1068 :system-lambda system-lambda)))
1069 (setf (functional-inline-expanded clambda) t)
1072 ;;; Get a DEFINED-FUN object for a function we are about to define. If
1073 ;;; the function has been forward referenced, then substitute for the
1074 ;;; previous references.
1075 (defun get-defined-fun (name)
1076 (proclaim-as-fun-name name)
1077 (let ((found (find-free-fun name "shouldn't happen! (defined-fun)")))
1078 (note-name-defined name :function)
1079 (cond ((not (defined-fun-p found))
1080 (aver (not (info :function :inlinep name)))
1081 (let* ((where-from (leaf-where-from found))
1082 (res (make-defined-fun
1084 :where-from (if (eq where-from :declared)
1086 :type (leaf-type found))))
1087 (substitute-leaf res found)
1088 (setf (gethash name *free-funs*) res)))
1089 ;; If *FREE-FUNS* has a previously converted definition
1090 ;; for this name, then blow it away and try again.
1091 ((defined-fun-functionals found)
1092 (remhash name *free-funs*)
1093 (get-defined-fun name))
1096 ;;; Check a new global function definition for consistency with
1097 ;;; previous declaration or definition, and assert argument/result
1098 ;;; types if appropriate. This assertion is suppressed by the
1099 ;;; EXPLICIT-CHECK attribute, which is specified on functions that
1100 ;;; check their argument types as a consequence of type dispatching.
1101 ;;; This avoids redundant checks such as NUMBERP on the args to +, etc.
1102 (defun assert-new-definition (var fun)
1103 (let ((type (leaf-type var))
1104 (for-real (eq (leaf-where-from var) :declared))
1105 (info (info :function :info (leaf-source-name var))))
1106 (assert-definition-type
1108 ;; KLUDGE: Common Lisp is such a dynamic language that in general
1109 ;; all we can do here in general is issue a STYLE-WARNING. It
1110 ;; would be nice to issue a full WARNING in the special case of
1111 ;; of type mismatches within a compilation unit (as in section
1112 ;; 3.2.2.3 of the spec) but at least as of sbcl-0.6.11, we don't
1113 ;; keep track of whether the mismatched data came from the same
1114 ;; compilation unit, so we can't do that. -- WHN 2001-02-11
1115 :lossage-fun #'compiler-style-warn
1116 :unwinnage-fun (cond (info #'compiler-style-warn)
1117 (for-real #'compiler-notify)
1122 (ir1-attributep (fun-info-attributes info)
1125 "previous declaration"
1126 "previous definition"))))
1128 ;;; Used for global inline expansion. Earlier something like this was
1129 ;;; used by %DEFUN too. FIXME: And now it's probably worth rethinking
1130 ;;; whether this function is a good idea at all.
1131 (defun ir1-convert-inline-expansion (name expansion var inlinep info)
1132 ;; Unless a :INLINE function, we temporarily clobber the inline
1133 ;; expansion. This prevents recursive inline expansion of
1134 ;; opportunistic pseudo-inlines.
1135 (unless (eq inlinep :inline)
1136 (setf (defined-fun-inline-expansion var) nil))
1137 (let ((fun (ir1-convert-inline-lambda expansion
1139 ;; prevent instrumentation of
1140 ;; known function expansions
1141 :system-lambda (and info t))))
1142 (setf (functional-inlinep fun) inlinep)
1143 (assert-new-definition var fun)
1144 (setf (defined-fun-inline-expansion var) expansion)
1145 ;; substitute for any old references
1146 (unless (or (not *block-compile*)
1148 (or (fun-info-transforms info)
1149 (fun-info-templates info)
1150 (fun-info-ir2-convert info))))
1151 (substitute-leaf fun var))
1154 ;;; the even-at-compile-time part of DEFUN
1156 ;;; The INLINE-EXPANSION is a LAMBDA-WITH-LEXENV, or NIL if there is
1157 ;;; no inline expansion.
1158 (defun %compiler-defun (name lambda-with-lexenv compile-toplevel)
1159 (let ((defined-fun nil)) ; will be set below if we're in the compiler
1160 (when compile-toplevel
1161 ;; better be in the compiler
1162 (aver (boundp '*lexenv*))
1163 (remhash name *free-funs*)
1164 (setf defined-fun (get-defined-fun name))
1165 (aver (fasl-output-p *compile-object*))
1166 (if (member name *fun-names-in-this-file* :test #'equal)
1167 (warn 'duplicate-definition :name name)
1168 (push name *fun-names-in-this-file*)))
1170 (become-defined-fun-name name)
1172 (cond (lambda-with-lexenv
1173 (setf (info :function :inline-expansion-designator name)
1176 (setf (defined-fun-inline-expansion defined-fun)
1177 lambda-with-lexenv)))
1179 (clear-info :function :inline-expansion-designator name)))
1181 ;; old CMU CL comment:
1182 ;; If there is a type from a previous definition, blast it,
1183 ;; since it is obsolete.
1184 (when (and defined-fun
1185 (eq (leaf-where-from defined-fun) :defined))
1186 (setf (leaf-type defined-fun)
1187 ;; FIXME: If this is a block compilation thing, shouldn't
1188 ;; we be setting the type to the full derived type for the
1189 ;; definition, instead of this most general function type?
1190 (specifier-type 'function))))
1195 ;;; Entry point utilities
1197 ;;; Return a function for the Nth entry point.
1198 (defun optional-dispatch-entry-point-fun (dispatcher n)
1199 (declare (type optional-dispatch dispatcher)
1200 (type unsigned-byte n))
1201 (let* ((env (getf (optional-dispatch-plist dispatcher) :ir1-environment))
1202 (*lexenv* (first env))
1203 (*current-path* (second env)))
1204 (force (nth n (optional-dispatch-entry-points dispatcher)))))