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 ,n-context ,n-count)))
552 ;; The reason for all the noise with
553 ;; STACK-GROWS-DOWNWARD-NOT-UPWARD is to enable generation of
554 ;; slightly more efficient code on x86oid processors. (We can
555 ;; hoist the negation of the index outside the main parsing loop
556 ;; and take advantage of the base+index+displacement addressing
558 (when (optional-dispatch-keyp res)
559 (let ((n-index (gensym "N-INDEX-"))
560 (n-key (gensym "N-KEY-"))
561 (n-value-temp (gensym "N-VALUE-TEMP-"))
562 (n-allowp (gensym "N-ALLOWP-"))
563 (n-lose (gensym "N-LOSE-"))
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-lose n-losep)
607 (unless found-allow-p
608 (tests `((eq ,n-key :allow-other-keys)
609 (setq ,n-allowp ,n-value-temp))))
615 `(when (oddp ,n-count)
616 (%odd-key-args-error)))
619 #!-stack-grows-downward-not-upward
621 (declare (optimize (safety 0)))
623 (when (minusp ,n-index) (return))
624 (setf ,n-value-temp (%more-arg ,n-context ,n-index))
626 (setq ,n-key (%more-arg ,n-context ,n-index))
629 #!+stack-grows-downward-not-upward
630 `(locally (declare (optimize (safety 0)))
632 (when (plusp ,n-index) (return))
633 (multiple-value-bind (,n-value-temp ,n-key)
634 (%more-kw-arg ,n-context ,n-index)
635 (declare (ignorable ,n-value-temp ,n-key))
640 (body `(when (and ,n-losep (not ,n-allowp))
641 (%unknown-key-arg-error ,n-lose)))))))
643 (let ((ep (ir1-convert-lambda-body
646 (%funcall ,(optional-dispatch-main-entry res)
649 :debug-name (debug-name '&more-processor name)
650 :note-lexical-bindings nil
652 (setf (optional-dispatch-more-entry res)
653 (register-entry-point ep res)))))
657 ;;; This is called by IR1-CONVERT-HAIRY-ARGS when we run into a &REST
658 ;;; or &KEY arg. The arguments are similar to that function, but we
659 ;;; split off any &REST arg and pass it in separately. REST is the
660 ;;; &REST arg var, or NIL if there is no &REST arg. KEYS is a list of
661 ;;; the &KEY argument vars.
663 ;;; When there are &KEY arguments, we introduce temporary gensym
664 ;;; variables to hold the values while keyword defaulting is in
665 ;;; progress to get the required sequential binding semantics.
667 ;;; This gets interesting mainly when there are &KEY arguments with
668 ;;; supplied-p vars or non-constant defaults. In either case, pass in
669 ;;; a supplied-p var. If the default is non-constant, we introduce an
670 ;;; IF in the main entry that tests the supplied-p var and decides
671 ;;; whether to evaluate the default or not. In this case, the real
672 ;;; incoming value is NIL, so we must union NULL with the declared
673 ;;; type when computing the type for the main entry's argument.
674 (defun ir1-convert-more (res default-vars default-vals entry-vars entry-vals
675 rest more-context more-count keys supplied-p-p
676 body aux-vars aux-vals source-name debug-name
677 post-binding-lexenv system-lambda)
678 (declare (type optional-dispatch res)
679 (list default-vars default-vals entry-vars entry-vals keys body
681 (collect ((main-vars (reverse default-vars))
682 (main-vals default-vals cons)
688 (unless (lambda-var-ignorep rest)
689 ;; Make up two extra variables, and squirrel them away in
690 ;; ARG-INFO-DEFAULT for transforming (VALUES-LIST REST) into
691 ;; (%MORE-ARG-VALUES CONTEXT 0 COUNT) when possible.
692 (let* ((context-name (gensym "REST-CONTEXT"))
693 (context (make-lambda-var :%source-name context-name
694 :arg-info (make-arg-info :kind :more-context)))
695 (count-name (gensym "REST-COUNT"))
696 (count (make-lambda-var :%source-name count-name
697 :arg-info (make-arg-info :kind :more-count)
698 :type (specifier-type 'index))))
699 (setf (arg-info-default (lambda-var-arg-info rest)) (list context count)
700 (lambda-var-ever-used context) t
701 (lambda-var-ever-used count) t)
702 (setf more-context context
705 (main-vars more-context)
707 (main-vars more-count)
711 (let* ((info (lambda-var-arg-info key))
712 (default (arg-info-default info))
713 (hairy-default (not (sb!xc:constantp default)))
714 (supplied-p (arg-info-supplied-p info))
715 (n-val (make-symbol (format nil
717 (leaf-source-name key))))
718 (val-temp (make-lambda-var :%source-name n-val)))
721 (cond ((or hairy-default supplied-p)
722 (let* ((n-supplied (gensym "N-SUPPLIED-"))
723 (supplied-temp (make-lambda-var
724 :%source-name n-supplied)))
726 (setf (arg-info-supplied-p info) supplied-temp))
728 (setf (arg-info-default info) nil))
729 (main-vars supplied-temp)
732 (bind-vals `(if ,n-supplied ,n-val ,default)))
734 (main-vals default nil)
737 (bind-vars supplied-p)
738 (bind-vals n-supplied))))
740 (main-vals (arg-info-default info))
741 (bind-vals n-val)))))
743 (let* ((name (or debug-name source-name))
744 (main-entry (ir1-convert-lambda-body
746 :aux-vars (append (bind-vars) aux-vars)
747 :aux-vals (append (bind-vals) aux-vals)
748 :post-binding-lexenv post-binding-lexenv
749 :debug-name (debug-name 'varargs-entry name)
750 :system-lambda system-lambda))
751 (last-entry (convert-optional-entry main-entry default-vars
752 (main-vals) () name)))
753 (setf (optional-dispatch-main-entry res)
754 (register-entry-point main-entry res))
755 (convert-more-entry res entry-vars entry-vals rest more-context keys
758 (push (register-entry-point
760 (convert-optional-entry last-entry entry-vars entry-vals
764 (optional-dispatch-entry-points res))
767 ;;; This function generates the entry point functions for the
768 ;;; OPTIONAL-DISPATCH RES. We accomplish this by recursion on the list
769 ;;; of arguments, analyzing the arglist on the way down and generating
770 ;;; entry points on the way up.
772 ;;; DEFAULT-VARS is a reversed list of all the argument vars processed
773 ;;; so far, including supplied-p vars. DEFAULT-VALS is a list of the
774 ;;; names of the DEFAULT-VARS.
776 ;;; ENTRY-VARS is a reversed list of processed argument vars,
777 ;;; excluding supplied-p vars. ENTRY-VALS is a list things that can be
778 ;;; evaluated to get the values for all the vars from the ENTRY-VARS.
779 ;;; It has the var name for each required or optional arg, and has T
780 ;;; for each supplied-p arg.
782 ;;; VARS is a list of the LAMBDA-VAR structures for arguments that
783 ;;; haven't been processed yet. SUPPLIED-P-P is true if a supplied-p
784 ;;; argument has already been processed; only in this case are the
785 ;;; DEFAULT-XXX and ENTRY-XXX different.
787 ;;; The result at each point is a lambda which should be called by the
788 ;;; above level to default the remaining arguments and evaluate the
789 ;;; body. We cause the body to be evaluated by converting it and
790 ;;; returning it as the result when the recursion bottoms out.
792 ;;; Each level in the recursion also adds its entry point function to
793 ;;; the result OPTIONAL-DISPATCH. For most arguments, the defaulting
794 ;;; function and the entry point function will be the same, but when
795 ;;; SUPPLIED-P args are present they may be different.
797 ;;; When we run into a &REST or &KEY arg, we punt out to
798 ;;; IR1-CONVERT-MORE, which finishes for us in this case.
799 (defun ir1-convert-hairy-args (res default-vars default-vals
800 entry-vars entry-vals
801 vars supplied-p-p body aux-vars
803 source-name debug-name
804 force post-binding-lexenv
806 (declare (type optional-dispatch res)
807 (list default-vars default-vals entry-vars entry-vals vars body
809 (aver (or debug-name (neq '.anonymous. source-name)))
811 (if (optional-dispatch-keyp res)
812 ;; Handle &KEY with no keys...
813 (ir1-convert-more res default-vars default-vals
814 entry-vars entry-vals
815 nil nil nil vars supplied-p-p body aux-vars
816 aux-vals source-name debug-name
817 post-binding-lexenv system-lambda)
818 (let* ((name (or debug-name source-name))
819 (fun (ir1-convert-lambda-body
820 body (reverse default-vars)
823 :post-binding-lexenv post-binding-lexenv
824 :debug-name (debug-name 'hairy-arg-processor name)
825 :system-lambda system-lambda)))
827 (setf (optional-dispatch-main-entry res) fun)
828 (register-entry-point fun res)
829 (push (if supplied-p-p
830 (register-entry-point
831 (convert-optional-entry fun entry-vars entry-vals ()
835 (optional-dispatch-entry-points res))
837 ((not (lambda-var-arg-info (first vars)))
838 (let* ((arg (first vars))
839 (nvars (cons arg default-vars))
840 (nvals (cons (leaf-source-name arg) default-vals)))
841 (ir1-convert-hairy-args res nvars nvals nvars nvals
842 (rest vars) nil body aux-vars aux-vals
843 source-name debug-name
844 nil post-binding-lexenv system-lambda)))
846 (let* ((arg (first vars))
847 (info (lambda-var-arg-info arg))
848 (kind (arg-info-kind info)))
851 (let ((ep (generate-optional-default-entry
852 res default-vars default-vals
853 entry-vars entry-vals vars supplied-p-p body
855 source-name debug-name
856 force post-binding-lexenv
858 ;; See GENERATE-OPTIONAL-DEFAULT-ENTRY.
859 (push (if (lambda-p ep)
860 (register-entry-point
862 (convert-optional-entry
863 ep entry-vars entry-vals nil
864 (or debug-name source-name))
867 (progn (aver (not supplied-p-p))
869 (optional-dispatch-entry-points res))
872 (ir1-convert-more res default-vars default-vals
873 entry-vars entry-vals
874 arg nil nil (rest vars) supplied-p-p body
876 source-name debug-name
877 post-binding-lexenv system-lambda))
879 (ir1-convert-more res default-vars default-vals
880 entry-vars entry-vals
881 nil arg (second vars) (cddr vars) supplied-p-p
882 body aux-vars aux-vals
883 source-name debug-name
884 post-binding-lexenv system-lambda))
886 (ir1-convert-more res default-vars default-vals
887 entry-vars entry-vals
888 nil nil nil vars supplied-p-p body aux-vars
889 aux-vals source-name debug-name
890 post-binding-lexenv system-lambda)))))))
892 ;;; This function deals with the case where we have to make an
893 ;;; OPTIONAL-DISPATCH to represent a LAMBDA. We cons up the result and
894 ;;; call IR1-CONVERT-HAIRY-ARGS to do the work. When it is done, we
895 ;;; figure out the MIN-ARGS and MAX-ARGS.
896 (defun ir1-convert-hairy-lambda (body vars keyp allowp aux-vars aux-vals
897 &key post-binding-lexenv
898 (source-name '.anonymous.)
899 debug-name system-lambda)
900 (declare (list body vars aux-vars aux-vals))
901 (aver (or debug-name (neq '.anonymous. source-name)))
902 (let ((res (make-optional-dispatch :arglist vars
905 :%source-name source-name
906 :%debug-name debug-name
907 :plist `(:ir1-environment
910 (min (or (position-if #'lambda-var-arg-info vars) (length vars))))
911 (aver-live-component *current-component*)
912 (push res (component-new-functionals *current-component*))
913 (ir1-convert-hairy-args res () () () () vars nil body aux-vars aux-vals
914 source-name debug-name nil post-binding-lexenv
916 (setf (optional-dispatch-min-args res) min)
917 (setf (optional-dispatch-max-args res)
918 (+ (1- (length (optional-dispatch-entry-points res))) min))
922 ;;; Convert a LAMBDA form into a LAMBDA leaf or an OPTIONAL-DISPATCH leaf.
923 (defun ir1-convert-lambda (form &key (source-name '.anonymous.)
924 debug-name maybe-add-debug-catch
927 (compiler-error "A ~S was found when expecting a lambda expression:~% ~S"
930 (unless (eq (car form) 'lambda)
931 (compiler-error "~S was expected but ~S was found:~% ~S"
935 (unless (and (consp (cdr form)) (listp (cadr form)))
937 "The lambda expression has a missing or non-list lambda list:~% ~S"
939 (when (and system-lambda maybe-add-debug-catch)
940 (bug "Both SYSTEM-LAMBDA and MAYBE-ADD-DEBUG-CATCH specified"))
941 (unless (or debug-name (neq '.anonymous. source-name))
942 (setf debug-name (name-lambdalike form)))
943 (multiple-value-bind (vars keyp allow-other-keys aux-vars aux-vals)
944 (make-lambda-vars (cadr form))
945 (multiple-value-bind (forms decls doc) (parse-body (cddr form))
946 (binding* (((*lexenv* result-type post-binding-lexenv)
947 (process-decls decls (append aux-vars vars) nil
949 (debug-catch-p (and maybe-add-debug-catch
950 *allow-instrumenting*
952 (>= insert-debug-catch 2))))
953 (forms (if debug-catch-p
954 (wrap-forms-in-debug-catch forms)
956 (forms (if (eq result-type *wild-type*)
958 `((the ,(type-specifier result-type) (progn ,@forms)))))
959 (*allow-instrumenting* (and (not system-lambda) *allow-instrumenting*))
960 (res (cond ((or (find-if #'lambda-var-arg-info vars) keyp)
961 (ir1-convert-hairy-lambda forms vars keyp
964 :post-binding-lexenv post-binding-lexenv
965 :source-name source-name
966 :debug-name debug-name
967 :system-lambda system-lambda))
969 (ir1-convert-lambda-body forms vars
972 :post-binding-lexenv post-binding-lexenv
973 :source-name source-name
974 :debug-name debug-name
975 :system-lambda system-lambda)))))
976 (setf (functional-inline-expansion res) form)
977 (setf (functional-arg-documentation res) (cadr form))
978 (setf (functional-documentation res) doc)
979 (when (boundp '*lambda-conversions*)
980 ;; KLUDGE: Not counting TL-XEPs is a lie, of course, but
981 ;; keeps things less confusing to users of TIME, where this
983 (unless (and (consp debug-name) (eq 'tl-xep (car debug-name)))
984 (incf *lambda-conversions*)))
987 (defun wrap-forms-in-debug-catch (forms)
988 #!+unwind-to-frame-and-call-vop
989 `((multiple-value-prog1
992 ;; Just ensure that there won't be any tail-calls, IR2 magic will
995 #!-unwind-to-frame-and-call-vop
996 `( ;; Normally, we'll return from this block with the below RETURN-FROM.
999 ;; If DEBUG-CATCH-TAG is thrown (with a thunk as the value) the
1000 ;; RETURN-FROM is elided and we funcall the thunk instead. That
1001 ;; thunk might either return a value (for a RETURN-FROM-FRAME)
1002 ;; or call this same function again (for a RESTART-FRAME).
1003 ;; -- JES, 2007-01-09
1006 ;; Use a constant catch tag instead of consing a new one for every
1007 ;; entry to this block. The uniquencess of the catch tags is
1008 ;; ensured when the tag is throw by the debugger. It'll allocate a
1009 ;; new tag, and modify the reference this tag in the proper
1010 ;; catch-block structure to refer to that new tag. This
1011 ;; significantly decreases the runtime cost of high debug levels.
1012 ;; -- JES, 2007-01-09
1013 (catch 'debug-catch-tag
1014 (return-from return-value-tag
1018 ;;; helper for LAMBDA-like things, to massage them into a form
1019 ;;; suitable for IR1-CONVERT-LAMBDA.
1020 (defun ir1-convert-lambdalike (thing
1022 (source-name '.anonymous.)
1024 (when (and (not debug-name) (eq '.anonymous. source-name))
1025 (setf debug-name (name-lambdalike thing)))
1028 (ir1-convert-lambda thing
1029 :maybe-add-debug-catch t
1030 :source-name source-name
1031 :debug-name debug-name))
1033 (let ((name (cadr thing))
1034 (lambda-expression `(lambda ,@(cddr thing))))
1035 (if (and name (legal-fun-name-p name))
1036 (let ((defined-fun-res (get-defined-fun name (second lambda-expression)))
1037 (res (ir1-convert-lambda lambda-expression
1038 :maybe-add-debug-catch t
1039 :source-name name)))
1040 (assert-global-function-definition-type name res)
1041 (push res (defined-fun-functionals defined-fun-res))
1042 (unless (eq (defined-fun-inlinep defined-fun-res) :notinline)
1045 (policy ref (> recognize-self-calls 0)))
1046 res defined-fun-res))
1048 (ir1-convert-lambda lambda-expression
1049 :maybe-add-debug-catch t
1051 (or name (name-lambdalike thing))))))
1052 ((lambda-with-lexenv)
1053 (ir1-convert-inline-lambda thing
1054 :source-name source-name
1055 :debug-name debug-name))))
1057 ;;;; defining global functions
1059 ;;; Convert FUN as a lambda in the null environment, but use the
1060 ;;; current compilation policy. Note that FUN may be a
1061 ;;; LAMBDA-WITH-LEXENV, so we may have to augment the environment to
1062 ;;; reflect the state at the definition site.
1063 (defun ir1-convert-inline-lambda (fun
1065 (source-name '.anonymous.)
1068 (when (and (not debug-name) (eq '.anonymous. source-name))
1069 (setf debug-name (name-lambdalike fun)))
1070 (destructuring-bind (decls macros symbol-macros &rest body)
1071 (if (eq (car fun) 'lambda-with-lexenv)
1073 `(() () () . ,(cdr fun)))
1074 (let* ((*lexenv* (make-lexenv
1075 :default (process-decls decls nil nil
1076 :lexenv (make-null-lexenv))
1077 :vars (copy-list symbol-macros)
1078 :funs (mapcar (lambda (x)
1080 (macro . ,(coerce (cdr x) 'function))))
1082 ;; Inherit MUFFLE-CONDITIONS from the call-site lexenv
1083 ;; rather than the definition-site lexenv, since it seems
1084 ;; like a much more common case.
1085 :handled-conditions (lexenv-handled-conditions *lexenv*)
1086 :policy (lexenv-policy *lexenv*)))
1087 (clambda (ir1-convert-lambda `(lambda ,@body)
1088 :source-name source-name
1089 :debug-name debug-name
1090 :system-lambda system-lambda)))
1091 (setf (functional-inline-expanded clambda) t)
1094 ;;; Given a lambda-list, return a FUN-TYPE object representing the signature:
1095 ;;; return type is *, and each individual arguments type is T -- but we get
1096 ;;; the argument counts and keywords.
1097 (defun ftype-from-lambda-list (lambda-list)
1098 (multiple-value-bind (req opt restp rest-name keyp key-list allowp morep)
1099 (parse-lambda-list lambda-list)
1100 (declare (ignore rest-name))
1102 (mapcar (constantly t) list)))
1103 (let ((reqs (t req))
1104 (opts (when opt (cons '&optional (t opt))))
1105 ;; When it comes to building a type, &REST means pretty much the
1106 ;; same thing as &MORE.
1107 (rest (when (or morep restp) (list '&rest t)))
1109 (cons '&key (mapcar (lambda (spec)
1110 (let ((key/var (if (consp spec)
1113 (list (if (consp key/var)
1115 (keywordicate key/var))
1118 (allow (when allowp (list '&allow-other-keys))))
1119 (specifier-type `(function (,@reqs ,@opts ,@rest ,@keys ,@allow) *))))))
1121 ;;; Get a DEFINED-FUN object for a function we are about to define. If
1122 ;;; the function has been forward referenced, then substitute for the
1123 ;;; previous references.
1124 (defun get-defined-fun (name &optional (lambda-list nil lp))
1125 (proclaim-as-fun-name name)
1126 (when (boundp '*free-funs*)
1127 (let ((found (find-free-fun name "shouldn't happen! (defined-fun)")))
1128 (note-name-defined name :function)
1129 (cond ((not (defined-fun-p found))
1130 (aver (not (info :function :inlinep name)))
1131 (let* ((where-from (leaf-where-from found))
1132 (res (make-defined-fun
1134 :where-from (if (eq where-from :declared)
1137 :type (if (eq :declared where-from)
1140 (ftype-from-lambda-list lambda-list)
1141 (specifier-type 'function))))))
1142 (substitute-leaf res found)
1143 (setf (gethash name *free-funs*) res)))
1144 ;; If *FREE-FUNS* has a previously converted definition
1145 ;; for this name, then blow it away and try again.
1146 ((defined-fun-functionals found)
1147 (remhash name *free-funs*)
1148 (get-defined-fun name lambda-list))
1151 ;;; Check a new global function definition for consistency with
1152 ;;; previous declaration or definition, and assert argument/result
1153 ;;; types if appropriate. This assertion is suppressed by the
1154 ;;; EXPLICIT-CHECK attribute, which is specified on functions that
1155 ;;; check their argument types as a consequence of type dispatching.
1156 ;;; This avoids redundant checks such as NUMBERP on the args to +, etc.
1157 (defun assert-new-definition (var fun)
1158 (let ((type (leaf-type var))
1159 (for-real (eq (leaf-where-from var) :declared))
1160 (info (info :function :info (leaf-source-name var))))
1161 (assert-definition-type
1163 ;; KLUDGE: Common Lisp is such a dynamic language that in general
1164 ;; all we can do here in general is issue a STYLE-WARNING. It
1165 ;; would be nice to issue a full WARNING in the special case of
1166 ;; of type mismatches within a compilation unit (as in section
1167 ;; 3.2.2.3 of the spec) but at least as of sbcl-0.6.11, we don't
1168 ;; keep track of whether the mismatched data came from the same
1169 ;; compilation unit, so we can't do that. -- WHN 2001-02-11
1170 :lossage-fun #'compiler-style-warn
1171 :unwinnage-fun (cond (info #'compiler-style-warn)
1172 (for-real #'compiler-notify)
1177 (ir1-attributep (fun-info-attributes info)
1180 "previous declaration"
1181 "previous definition"))))
1183 ;;; Used for global inline expansion. Earlier something like this was
1184 ;;; used by %DEFUN too. FIXME: And now it's probably worth rethinking
1185 ;;; whether this function is a good idea at all.
1186 (defun ir1-convert-inline-expansion (name expansion var inlinep info)
1187 ;; Unless a :INLINE function, we temporarily clobber the inline
1188 ;; expansion. This prevents recursive inline expansion of
1189 ;; opportunistic pseudo-inlines.
1190 (unless (eq inlinep :inline)
1191 (setf (defined-fun-inline-expansion var) nil))
1192 (let ((fun (ir1-convert-inline-lambda expansion
1194 ;; prevent instrumentation of
1195 ;; known function expansions
1196 :system-lambda (and info t))))
1197 (setf (functional-inlinep fun) inlinep)
1198 (assert-new-definition var fun)
1199 (setf (defined-fun-inline-expansion var) expansion)
1200 ;; Associate VAR with the FUN -- and in case of an optional dispatch
1201 ;; with the various entry-points. This allows XREF to know where the
1202 ;; inline CLAMBDA comes from.
1203 (flet ((note-inlining (f)
1206 (setf (functional-inline-expanded f) var))
1208 ;; Delayed entry-point.
1210 (setf (functional-inline-expanded (cdr f)) var)
1211 (let ((old-thunk (cdr f)))
1212 (setf (cdr f) (lambda ()
1213 (let ((g (funcall old-thunk)))
1214 (setf (functional-inline-expanded g) var)
1217 (when (optional-dispatch-p fun)
1218 (note-inlining (optional-dispatch-main-entry fun))
1219 (note-inlining (optional-dispatch-more-entry fun))
1220 (mapc #'note-inlining (optional-dispatch-entry-points fun))))
1221 ;; substitute for any old references
1222 (unless (or (not *block-compile*)
1224 (or (fun-info-transforms info)
1225 (fun-info-templates info)
1226 (fun-info-ir2-convert info))))
1227 (substitute-leaf fun var))
1230 (defun %set-inline-expansion (name defined-fun inline-lambda)
1231 (cond (inline-lambda
1232 (setf (info :function :inline-expansion-designator name)
1235 (setf (defined-fun-inline-expansion defined-fun)
1238 (clear-info :function :inline-expansion-designator name))))
1240 ;;; the even-at-compile-time part of DEFUN
1242 ;;; The INLINE-LAMBDA is a LAMBDA-WITH-LEXENV, or NIL if there is no
1243 ;;; inline expansion.
1244 (defun %compiler-defun (name inline-lambda compile-toplevel)
1245 (let ((defined-fun nil)) ; will be set below if we're in the compiler
1246 (when compile-toplevel
1247 (with-single-package-locked-error
1248 (:symbol name "defining ~S as a function")
1251 (get-defined-fun name (fifth inline-lambda))
1252 (get-defined-fun name))))
1253 (when (boundp '*lexenv*)
1254 (remhash name *free-funs*)
1255 (aver (fasl-output-p *compile-object*))
1256 (if (member name *fun-names-in-this-file* :test #'equal)
1257 (warn 'duplicate-definition :name name)
1258 (push name *fun-names-in-this-file*)))
1259 (%set-inline-expansion name defined-fun inline-lambda))
1261 (become-defined-fun-name name)
1263 ;; old CMU CL comment:
1264 ;; If there is a type from a previous definition, blast it,
1265 ;; since it is obsolete.
1266 (when (and defined-fun (neq :declared (leaf-where-from defined-fun)))
1267 (setf (leaf-type defined-fun)
1268 ;; FIXME: If this is a block compilation thing, shouldn't
1269 ;; we be setting the type to the full derived type for the
1270 ;; definition, instead of this most general function type?
1271 (specifier-type 'function))))
1276 ;;; Entry point utilities
1278 ;;; Return a function for the Nth entry point.
1279 (defun optional-dispatch-entry-point-fun (dispatcher n)
1280 (declare (type optional-dispatch dispatcher)
1281 (type unsigned-byte n))
1282 (let* ((env (getf (optional-dispatch-plist dispatcher) :ir1-environment))
1283 (*lexenv* (first env))
1284 (*current-path* (second env)))
1285 (force (nth n (optional-dispatch-entry-points dispatcher)))))