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 (cond ((or (keywordp name
) (eq kind
:constant
))
37 (compiler-error "The name of the lambda variable ~S is already in use to name a constant."
40 (compiler-error "The name of the lambda variable ~S is already in use to name a global variable."
42 (cond ((eq kind
:special
)
43 (let ((specvar (find-free-var name
)))
44 (make-lambda-var :%source-name name
45 :type
(leaf-type specvar
)
46 :where-from
(leaf-where-from specvar
)
49 (make-lambda-var :%source-name name
)))))
51 ;;; Make the default keyword for a &KEY arg, checking that the keyword
52 ;;; isn't already used by one of the VARS.
53 (declaim (ftype (sfunction (symbol list t
) symbol
) make-keyword-for-arg
))
54 (defun make-keyword-for-arg (symbol vars keywordify
)
55 (let ((key (if (and keywordify
(not (keywordp symbol
)))
59 (let ((info (lambda-var-arg-info var
)))
61 (eq (arg-info-kind info
) :keyword
)
62 (eq (arg-info-key info
) key
))
64 "The keyword ~S appears more than once in the lambda list."
68 ;;; Parse a lambda list into a list of VAR structures, stripping off
69 ;;; any &AUX bindings. Each arg name is checked for legality, and
70 ;;; duplicate names are checked for. If an arg is globally special,
71 ;;; the var is marked as :SPECIAL instead of :LEXICAL. &KEY,
72 ;;; &OPTIONAL and &REST args are annotated with an ARG-INFO structure
73 ;;; which contains the extra information. If we hit something losing,
74 ;;; we bug out with COMPILER-ERROR. These values are returned:
75 ;;; 1. a list of the var structures for each top level argument;
76 ;;; 2. a flag indicating whether &KEY was specified;
77 ;;; 3. a flag indicating whether other &KEY args are allowed;
78 ;;; 4. a list of the &AUX variables; and
79 ;;; 5. a list of the &AUX values.
80 (declaim (ftype (sfunction (list) (values list boolean boolean list list
))
82 (defun make-lambda-vars (list)
83 (multiple-value-bind (required optional restp rest keyp keys allowp auxp aux
84 morep more-context more-count
)
85 (parse-lambda-list list
)
86 (declare (ignore auxp
)) ; since we just iterate over AUX regardless
91 (flet (;; PARSE-DEFAULT deals with defaults and supplied-p args
92 ;; for optionals and keywords args.
93 (parse-default (spec info
)
94 (when (consp (cdr spec
))
95 (setf (arg-info-default info
) (second spec
))
96 (when (consp (cddr spec
))
97 (let* ((supplied-p (third spec
))
98 (supplied-var (varify-lambda-arg supplied-p
100 (setf (arg-info-supplied-p info
) supplied-var
)
101 (names-so-far supplied-p
)
102 (when (> (length (the list spec
)) 3)
104 "The list ~S is too long to be an arg specifier."
107 (dolist (name required
)
108 (let ((var (varify-lambda-arg name
(names-so-far))))
110 (names-so-far name
)))
112 (dolist (spec optional
)
114 (let ((var (varify-lambda-arg spec
(names-so-far))))
115 (setf (lambda-var-arg-info var
)
116 (make-arg-info :kind
:optional
))
119 (let* ((name (first spec
))
120 (var (varify-lambda-arg name
(names-so-far)))
121 (info (make-arg-info :kind
:optional
)))
122 (setf (lambda-var-arg-info var
) info
)
125 (parse-default spec info
))))
128 (let ((var (varify-lambda-arg rest
(names-so-far))))
129 (setf (lambda-var-arg-info var
) (make-arg-info :kind
:rest
))
131 (names-so-far rest
)))
134 (let ((var (varify-lambda-arg more-context
(names-so-far))))
135 (setf (lambda-var-arg-info var
)
136 (make-arg-info :kind
:more-context
))
138 (names-so-far more-context
))
139 (let ((var (varify-lambda-arg more-count
(names-so-far))))
140 (setf (lambda-var-arg-info var
)
141 (make-arg-info :kind
:more-count
))
143 (names-so-far more-count
)))
148 (let ((var (varify-lambda-arg spec
(names-so-far))))
149 (setf (lambda-var-arg-info var
)
150 (make-arg-info :kind
:keyword
151 :key
(make-keyword-for-arg spec
155 (names-so-far spec
)))
157 (let* ((name (first spec
))
158 (var (varify-lambda-arg name
(names-so-far)))
161 :key
(make-keyword-for-arg name
(vars) t
))))
162 (setf (lambda-var-arg-info var
) info
)
165 (parse-default spec info
)))
167 (let ((head (first spec
)))
168 (unless (proper-list-of-length-p head
2)
169 (error "malformed &KEY argument specifier: ~S" spec
))
170 (let* ((name (second head
))
171 (var (varify-lambda-arg name
(names-so-far)))
174 :key
(make-keyword-for-arg (first head
)
177 (setf (lambda-var-arg-info var
) info
)
180 (parse-default spec info
))))))
184 (let ((var (varify-lambda-arg spec nil
)))
187 (names-so-far spec
)))
189 (unless (proper-list-of-length-p spec
1 2)
190 (compiler-error "malformed &AUX binding specifier: ~S"
192 (let* ((name (first spec
))
193 (var (varify-lambda-arg name nil
)))
195 (aux-vals (second spec
))
196 (names-so-far name
)))))
198 (values (vars) keyp allowp
(aux-vars) (aux-vals))))))
200 ;;; This is similar to IR1-CONVERT-PROGN-BODY except that we
201 ;;; sequentially bind each AUX-VAR to the corresponding AUX-VAL before
202 ;;; converting the body. If there are no bindings, just convert the
203 ;;; body, otherwise do one binding and recurse on the rest.
205 ;;; FIXME: This could and probably should be converted to use
206 ;;; SOURCE-NAME and DEBUG-NAME. But I (WHN) don't use &AUX bindings,
207 ;;; so I'm not motivated. Patches will be accepted...
208 (defun ir1-convert-aux-bindings (start next result body aux-vars aux-vals
210 (declare (type ctran start next
) (type (or lvar null
) result
)
211 (list body aux-vars aux-vals
))
213 (let ((*lexenv
* (make-lexenv :vars
(copy-list post-binding-lexenv
))))
214 (ir1-convert-progn-body start next result body
))
215 (let ((ctran (make-ctran))
216 (fun-lvar (make-lvar))
217 (fun (ir1-convert-lambda-body body
218 (list (first aux-vars
))
219 :aux-vars
(rest aux-vars
)
220 :aux-vals
(rest aux-vals
)
221 :post-binding-lexenv post-binding-lexenv
222 :debug-name
(debug-name
225 (reference-leaf start ctran fun-lvar fun
)
226 (ir1-convert-combination-args fun-lvar ctran next result
227 (list (first aux-vals
)))))
230 ;;; This is similar to IR1-CONVERT-PROGN-BODY except that code to bind
231 ;;; the SPECVAR for each SVAR to the value of the variable is wrapped
232 ;;; around the body. If there are no special bindings, we just convert
233 ;;; the body, otherwise we do one special binding and recurse on the
236 ;;; We make a cleanup and introduce it into the lexical
237 ;;; environment. If there are multiple special bindings, the cleanup
238 ;;; for the blocks will end up being the innermost one. We force NEXT
239 ;;; to start a block outside of this cleanup, causing cleanup code to
240 ;;; be emitted when the scope is exited.
241 (defun ir1-convert-special-bindings
242 (start next result body aux-vars aux-vals svars post-binding-lexenv
)
243 (declare (type ctran start next
) (type (or lvar null
) result
)
244 (list body aux-vars aux-vals svars
))
247 (ir1-convert-aux-bindings start next result body aux-vars aux-vals
248 post-binding-lexenv
))
250 (ctran-starts-block next
)
251 (let ((cleanup (make-cleanup :kind
:special-bind
))
253 (bind-ctran (make-ctran))
254 (cleanup-ctran (make-ctran)))
255 (ir1-convert start bind-ctran nil
256 `(%special-bind
',(lambda-var-specvar var
) ,var
))
257 (setf (cleanup-mess-up cleanup
) (ctran-use bind-ctran
))
258 (let ((*lexenv
* (make-lexenv :cleanup cleanup
)))
259 (ir1-convert bind-ctran cleanup-ctran nil
'(%cleanup-point
))
260 (ir1-convert-special-bindings cleanup-ctran next result
261 body aux-vars aux-vals
263 post-binding-lexenv
)))))
266 ;;; Create a lambda node out of some code, returning the result. The
267 ;;; bindings are specified by the list of VAR structures VARS. We deal
268 ;;; with adding the names to the LEXENV-VARS for the conversion. The
269 ;;; result is added to the NEW-FUNCTIONALS in the *CURRENT-COMPONENT*
270 ;;; and linked to the component head and tail.
272 ;;; We detect special bindings here, replacing the original VAR in the
273 ;;; lambda list with a temporary variable. We then pass a list of the
274 ;;; special vars to IR1-CONVERT-SPECIAL-BINDINGS, which actually emits
275 ;;; the special binding code.
277 ;;; We ignore any ARG-INFO in the VARS, trusting that someone else is
278 ;;; dealing with &NONSENSE, except for &REST vars with DYNAMIC-EXTENT.
280 ;;; AUX-VARS is a list of VAR structures for variables that are to be
281 ;;; sequentially bound. Each AUX-VAL is a form that is to be evaluated
282 ;;; to get the initial value for the corresponding AUX-VAR.
283 (defun ir1-convert-lambda-body (body
288 (source-name '.anonymous.
)
290 (note-lexical-bindings t
)
293 (declare (list body vars aux-vars aux-vals
))
295 ;; We're about to try to put new blocks into *CURRENT-COMPONENT*.
296 (aver-live-component *current-component
*)
298 (let* ((bind (make-bind))
299 (lambda (make-lambda :vars vars
301 :%source-name source-name
302 :%debug-name debug-name
303 :system-lambda-p system-lambda
))
304 (result-ctran (make-ctran))
305 (result-lvar (make-lvar)))
307 (awhen (lexenv-lambda *lexenv
*)
308 (push lambda
(lambda-children it
))
309 (setf (lambda-parent lambda
) it
))
311 ;; just to check: This function should fail internal assertions if
312 ;; we didn't set up a valid debug name above.
314 ;; (In SBCL we try to make everything have a debug name, since we
315 ;; lack the omniscient perspective the original implementors used
316 ;; to decide which things didn't need one.)
317 (functional-debug-name lambda
)
319 (setf (lambda-home lambda
) lambda
)
324 ;; As far as I can see, LAMBDA-VAR-HOME should never have
325 ;; been set before. Let's make sure. -- WHN 2001-09-29
326 (aver (not (lambda-var-home var
)))
327 (setf (lambda-var-home var
) lambda
)
328 (let ((specvar (lambda-var-specvar var
)))
331 (new-venv (cons (leaf-source-name specvar
) specvar
)))
333 (when note-lexical-bindings
334 (note-lexical-binding (leaf-source-name var
)))
335 (new-venv (cons (leaf-source-name var
) var
))))))
337 (let ((*lexenv
* (make-lexenv :vars
(new-venv)
340 (setf (bind-lambda bind
) lambda
)
341 (setf (node-lexenv bind
) *lexenv
*)
343 (let ((block (ctran-starts-block result-ctran
)))
344 (let ((return (make-return :result result-lvar
:lambda lambda
))
345 (tail-set (make-tail-set :funs
(list lambda
))))
346 (setf (lambda-tail-set lambda
) tail-set
)
347 (setf (lambda-return lambda
) return
)
348 (setf (lvar-dest result-lvar
) return
)
349 (link-node-to-previous-ctran return result-ctran
)
350 (setf (block-last block
) return
))
351 (link-blocks block
(component-tail *current-component
*)))
353 (with-component-last-block (*current-component
*
354 (ctran-block result-ctran
))
355 (let ((prebind-ctran (make-ctran))
356 (postbind-ctran (make-ctran)))
357 (ctran-starts-block prebind-ctran
)
358 (link-node-to-previous-ctran bind prebind-ctran
)
359 (use-ctran bind postbind-ctran
)
360 (ir1-convert-special-bindings postbind-ctran result-ctran
362 aux-vars aux-vals
(svars)
363 post-binding-lexenv
)))))
365 (link-blocks (component-head *current-component
*) (node-block bind
))
366 (push lambda
(component-new-functionals *current-component
*))
370 ;;; Entry point CLAMBDAs have a special kind
371 (defun register-entry-point (entry dispatcher
)
372 (declare (type clambda entry
)
373 (type optional-dispatch dispatcher
))
374 (setf (functional-kind entry
) :optional
)
375 (setf (leaf-ever-used entry
) t
)
376 (setf (lambda-optional-dispatch entry
) dispatcher
)
379 ;;; Create the actual entry-point function for an optional entry
380 ;;; point. The lambda binds copies of each of the VARS, then calls FUN
381 ;;; with the argument VALS and the DEFAULTS. Presumably the VALS refer
382 ;;; to the VARS by name. The VALS are passed in the reverse order.
384 ;;; If any of the copies of the vars are referenced more than once,
385 ;;; then we mark the corresponding var as EVER-USED to inhibit
386 ;;; "defined but not read" warnings for arguments that are only used
387 ;;; by default forms.
388 (defun convert-optional-entry (fun vars vals defaults name
)
389 (declare (type clambda fun
) (list vars vals defaults
))
390 (let* ((fvars (reverse vars
))
391 (arg-vars (mapcar (lambda (var)
393 :%source-name
(leaf-source-name var
)
394 :type
(leaf-type var
)
395 :where-from
(leaf-where-from var
)
396 :specvar
(lambda-var-specvar var
)))
398 (fun (collect ((default-bindings)
400 (dolist (default defaults
)
401 (if (sb!xc
:constantp default
)
402 (default-vals default
)
403 (let ((var (gensym)))
404 (default-bindings `(,var
,default
))
405 (default-vals var
))))
406 (let ((bindings (default-bindings))
407 (call `(%funcall
,fun
,@(reverse vals
) ,@(default-vals))))
408 (ir1-convert-lambda-body (if bindings
409 `((let (,@bindings
) ,call
))
412 ;; FIXME: Would be nice to
413 ;; share these names instead
414 ;; of consing up several
415 ;; identical ones. Oh well.
416 :debug-name
(debug-name
419 :note-lexical-bindings nil
420 :system-lambda t
)))))
421 (mapc (lambda (var arg-var
)
422 (when (cdr (leaf-refs arg-var
))
423 (setf (leaf-ever-used var
) t
)))
427 ;;; This function deals with supplied-p vars in optional arguments. If
428 ;;; the there is no supplied-p arg, then we just call
429 ;;; IR1-CONVERT-HAIRY-ARGS on the remaining arguments, and generate a
430 ;;; optional entry that calls the result. If there is a supplied-p
431 ;;; var, then we add it into the default vars and throw a T into the
432 ;;; entry values. The resulting entry point function is returned.
433 (defun generate-optional-default-entry (res default-vars default-vals
434 entry-vars entry-vals
435 vars supplied-p-p body
437 source-name debug-name
438 force post-binding-lexenv
440 (declare (type optional-dispatch res
)
441 (list default-vars default-vals entry-vars entry-vals vars body
443 (let* ((arg (first vars
))
444 (arg-name (leaf-source-name arg
))
445 (info (lambda-var-arg-info arg
))
446 (default (arg-info-default info
))
447 (supplied-p (arg-info-supplied-p info
))
449 (not (sb!xc
:constantp
(arg-info-default info
)))))
451 (ir1-convert-hairy-args
453 (list* supplied-p arg default-vars
)
454 (list* (leaf-source-name supplied-p
) arg-name default-vals
)
455 (cons arg entry-vars
)
456 (list* t arg-name entry-vals
)
457 (rest vars
) t body aux-vars aux-vals
458 source-name debug-name
459 force post-binding-lexenv system-lambda
)
460 (ir1-convert-hairy-args
462 (cons arg default-vars
)
463 (cons arg-name default-vals
)
464 (cons arg entry-vars
)
465 (cons arg-name entry-vals
)
466 (rest vars
) supplied-p-p body aux-vars aux-vals
467 source-name debug-name
468 force post-binding-lexenv system-lambda
))))
470 ;; We want to delay converting the entry, but there exist
471 ;; problems: hidden references should not be established to
472 ;; lambdas of kind NIL should not have (otherwise the compiler
473 ;; might let-convert or delete them) and to variables.
474 (let ((name (or debug-name source-name
)))
476 supplied-p-p
; this entry will be of kind NIL
477 (and (lambda-p ep
) (eq (lambda-kind ep
) nil
)))
478 (convert-optional-entry ep
479 default-vars default-vals
480 (if supplied-p
(list default nil
) (list default
))
482 (let* ((default `',(constant-form-value default
))
483 (defaults (if supplied-p
(list default nil
) (list default
))))
484 ;; DEFAULT can contain a reference to a
485 ;; to-be-optimized-away function/block/tag, so better to
486 ;; reduce code now (but we possibly lose syntax checking
487 ;; in an unreachable code).
489 (register-entry-point
490 (convert-optional-entry (force ep
)
491 default-vars default-vals
496 ;;; Create the MORE-ENTRY function for the OPTIONAL-DISPATCH RES.
497 ;;; ENTRY-VARS and ENTRY-VALS describe the fixed arguments. REST is
498 ;;; the var for any &REST arg. KEYS is a list of the &KEY arg vars.
500 ;;; The most interesting thing that we do is parse keywords. We create
501 ;;; a bunch of temporary variables to hold the result of the parse,
502 ;;; and then loop over the supplied arguments, setting the appropriate
503 ;;; temps for the supplied keyword. Note that it is significant that
504 ;;; we iterate over the keywords in reverse order --- this implements
505 ;;; the CL requirement that (when a keyword appears more than once)
506 ;;; the first value is used.
508 ;;; If there is no supplied-p var, then we initialize the temp to the
509 ;;; default and just pass the temp into the main entry. Since
510 ;;; non-constant &KEY args are forcibly given a supplied-p var, we
511 ;;; know that the default is constant, and thus safe to evaluate out
514 ;;; If there is a supplied-p var, then we create temps for both the
515 ;;; value and the supplied-p, and pass them into the main entry,
516 ;;; letting it worry about defaulting.
518 ;;; We deal with :ALLOW-OTHER-KEYS by delaying unknown keyword errors
519 ;;; until we have scanned all the keywords.
520 (defun convert-more-entry (res entry-vars entry-vals rest morep keys name
)
521 (declare (type optional-dispatch res
) (list entry-vars entry-vals keys
))
523 (arg-vals (reverse entry-vals
))
527 (dolist (var (reverse entry-vars
))
528 (arg-vars (make-lambda-var :%source-name
(leaf-source-name var
)
529 :type
(leaf-type var
)
530 :where-from
(leaf-where-from var
))))
532 (let* ((n-context (gensym "N-CONTEXT-"))
533 (context-temp (make-lambda-var :%source-name n-context
))
534 (n-count (gensym "N-COUNT-"))
535 (count-temp (make-lambda-var :%source-name n-count
536 :type
(specifier-type 'index
))))
538 (arg-vars context-temp count-temp
)
541 (arg-vals `(%listify-rest-args
542 ,n-context
,n-count
)))
547 ;; The reason for all the noise with
548 ;; STACK-GROWS-DOWNWARD-NOT-UPWARD is to enable generation of
549 ;; slightly more efficient code on x86oid processors. (We can
550 ;; hoist the negation of the index outside the main parsing loop
551 ;; and take advantage of the base+index+displacement addressing
553 (when (optional-dispatch-keyp res
)
554 (let ((n-index (gensym "N-INDEX-"))
555 (n-key (gensym "N-KEY-"))
556 (n-value-temp (gensym "N-VALUE-TEMP-"))
557 (n-allowp (gensym "N-ALLOWP-"))
558 (n-losep (gensym "N-LOSEP-"))
559 (allowp (or (optional-dispatch-allowp res
)
560 (policy *lexenv
* (zerop safety
))))
563 (temps #!-stack-grows-downward-not-upward
564 `(,n-index
(1- ,n-count
))
565 #!+stack-grows-downward-not-upward
566 `(,n-index
(- (1- ,n-count
)))
567 #!-stack-grows-downward-not-upward n-value-temp
568 #!-stack-grows-downward-not-upward n-key
)
569 (body `(declare (fixnum ,n-index
)
570 #!-stack-grows-downward-not-upward
571 (ignorable ,n-value-temp
,n-key
)))
575 (let* ((info (lambda-var-arg-info key
))
576 (default (arg-info-default info
))
577 (keyword (arg-info-key info
))
578 (supplied-p (arg-info-supplied-p info
))
579 (n-value (gensym "N-VALUE-"))
580 (clause (cond (supplied-p
581 (let ((n-supplied (gensym "N-SUPPLIED-")))
583 (arg-vals n-value n-supplied
)
584 `((eq ,n-key
',keyword
)
586 (setq ,n-value
,n-value-temp
))))
589 `((eq ,n-key
',keyword
)
590 (setq ,n-value
,n-value-temp
))))))
591 (when (and (not allowp
) (eq keyword
:allow-other-keys
))
592 (setq found-allow-p t
)
594 (append clause
`((setq ,n-allowp
,n-value-temp
)))))
596 (temps `(,n-value
,default
))
600 (temps n-allowp n-losep
)
601 (unless found-allow-p
602 (tests `((eq ,n-key
:allow-other-keys
)
603 (setq ,n-allowp
,n-value-temp
))))
605 (setq ,n-losep
(list ,n-key
)))))
608 `(when (oddp ,n-count
)
609 (%odd-key-args-error
)))
612 #!-stack-grows-downward-not-upward
614 (declare (optimize (safety 0)))
616 (when (minusp ,n-index
) (return))
617 (setf ,n-value-temp
(%more-arg
,n-context
,n-index
))
619 (setq ,n-key
(%more-arg
,n-context
,n-index
))
622 #!+stack-grows-downward-not-upward
623 `(locally (declare (optimize (safety 0)))
625 (when (plusp ,n-index
) (return))
626 (multiple-value-bind (,n-value-temp
,n-key
)
627 (%more-kw-arg
,n-context
,n-index
)
628 (declare (ignorable ,n-value-temp
,n-key
))
633 (body `(when (and ,n-losep
(not ,n-allowp
))
634 (%unknown-key-arg-error
(car ,n-losep
))))))))
636 (let ((ep (ir1-convert-lambda-body
639 (%funcall
,(optional-dispatch-main-entry res
)
642 :debug-name
(debug-name '&more-processor name
)
643 :note-lexical-bindings nil
645 (setf (optional-dispatch-more-entry res
)
646 (register-entry-point ep res
)))))
650 ;;; This is called by IR1-CONVERT-HAIRY-ARGS when we run into a &REST
651 ;;; or &KEY arg. The arguments are similar to that function, but we
652 ;;; split off any &REST arg and pass it in separately. REST is the
653 ;;; &REST arg var, or NIL if there is no &REST arg. KEYS is a list of
654 ;;; the &KEY argument vars.
656 ;;; When there are &KEY arguments, we introduce temporary gensym
657 ;;; variables to hold the values while keyword defaulting is in
658 ;;; progress to get the required sequential binding semantics.
660 ;;; This gets interesting mainly when there are &KEY arguments with
661 ;;; supplied-p vars or non-constant defaults. In either case, pass in
662 ;;; a supplied-p var. If the default is non-constant, we introduce an
663 ;;; IF in the main entry that tests the supplied-p var and decides
664 ;;; whether to evaluate the default or not. In this case, the real
665 ;;; incoming value is NIL, so we must union NULL with the declared
666 ;;; type when computing the type for the main entry's argument.
667 (defun ir1-convert-more (res default-vars default-vals entry-vars entry-vals
668 rest more-context more-count keys supplied-p-p
669 body aux-vars aux-vals source-name debug-name
670 post-binding-lexenv system-lambda
)
671 (declare (type optional-dispatch res
)
672 (list default-vars default-vals entry-vars entry-vals keys body
674 (collect ((main-vars (reverse default-vars
))
675 (main-vals default-vals cons
)
682 (main-vars more-context
)
684 (main-vars more-count
)
688 (let* ((info (lambda-var-arg-info key
))
689 (default (arg-info-default info
))
690 (hairy-default (not (sb!xc
:constantp default
)))
691 (supplied-p (arg-info-supplied-p info
))
692 (n-val (make-symbol (format nil
694 (leaf-source-name key
))))
695 (val-temp (make-lambda-var :%source-name n-val
)))
698 (cond ((or hairy-default supplied-p
)
699 (let* ((n-supplied (gensym "N-SUPPLIED-"))
700 (supplied-temp (make-lambda-var
701 :%source-name n-supplied
)))
703 (setf (arg-info-supplied-p info
) supplied-temp
))
705 (setf (arg-info-default info
) nil
))
706 (main-vars supplied-temp
)
709 (bind-vals `(if ,n-supplied
,n-val
,default
)))
711 (main-vals default nil
)
714 (bind-vars supplied-p
)
715 (bind-vals n-supplied
))))
717 (main-vals (arg-info-default info
))
718 (bind-vals n-val
)))))
720 (let* ((name (or debug-name source-name
))
721 (main-entry (ir1-convert-lambda-body
723 :aux-vars
(append (bind-vars) aux-vars
)
724 :aux-vals
(append (bind-vals) aux-vals
)
725 :post-binding-lexenv post-binding-lexenv
726 :debug-name
(debug-name 'varargs-entry name
)
727 :system-lambda system-lambda
))
728 (last-entry (convert-optional-entry main-entry default-vars
729 (main-vals) () name
)))
730 (setf (optional-dispatch-main-entry res
)
731 (register-entry-point main-entry res
))
732 (convert-more-entry res entry-vars entry-vals rest more-context keys
735 (push (register-entry-point
737 (convert-optional-entry last-entry entry-vars entry-vals
741 (optional-dispatch-entry-points res
))
744 ;;; This function generates the entry point functions for the
745 ;;; OPTIONAL-DISPATCH RES. We accomplish this by recursion on the list
746 ;;; of arguments, analyzing the arglist on the way down and generating
747 ;;; entry points on the way up.
749 ;;; DEFAULT-VARS is a reversed list of all the argument vars processed
750 ;;; so far, including supplied-p vars. DEFAULT-VALS is a list of the
751 ;;; names of the DEFAULT-VARS.
753 ;;; ENTRY-VARS is a reversed list of processed argument vars,
754 ;;; excluding supplied-p vars. ENTRY-VALS is a list things that can be
755 ;;; evaluated to get the values for all the vars from the ENTRY-VARS.
756 ;;; It has the var name for each required or optional arg, and has T
757 ;;; for each supplied-p arg.
759 ;;; VARS is a list of the LAMBDA-VAR structures for arguments that
760 ;;; haven't been processed yet. SUPPLIED-P-P is true if a supplied-p
761 ;;; argument has already been processed; only in this case are the
762 ;;; DEFAULT-XXX and ENTRY-XXX different.
764 ;;; The result at each point is a lambda which should be called by the
765 ;;; above level to default the remaining arguments and evaluate the
766 ;;; body. We cause the body to be evaluated by converting it and
767 ;;; returning it as the result when the recursion bottoms out.
769 ;;; Each level in the recursion also adds its entry point function to
770 ;;; the result OPTIONAL-DISPATCH. For most arguments, the defaulting
771 ;;; function and the entry point function will be the same, but when
772 ;;; SUPPLIED-P args are present they may be different.
774 ;;; When we run into a &REST or &KEY arg, we punt out to
775 ;;; IR1-CONVERT-MORE, which finishes for us in this case.
776 (defun ir1-convert-hairy-args (res default-vars default-vals
777 entry-vars entry-vals
778 vars supplied-p-p body aux-vars
780 source-name debug-name
781 force post-binding-lexenv
783 (declare (type optional-dispatch res
)
784 (list default-vars default-vals entry-vars entry-vals vars body
786 (aver (or debug-name
(neq '.anonymous. source-name
)))
788 (if (optional-dispatch-keyp res
)
789 ;; Handle &KEY with no keys...
790 (ir1-convert-more res default-vars default-vals
791 entry-vars entry-vals
792 nil nil nil vars supplied-p-p body aux-vars
793 aux-vals source-name debug-name
794 post-binding-lexenv system-lambda
)
795 (let* ((name (or debug-name source-name
))
796 (fun (ir1-convert-lambda-body
797 body
(reverse default-vars
)
800 :post-binding-lexenv post-binding-lexenv
801 :debug-name
(debug-name 'hairy-arg-processor name
)
802 :system-lambda system-lambda
)))
804 (setf (optional-dispatch-main-entry res
) fun
)
805 (register-entry-point fun res
)
806 (push (if supplied-p-p
807 (register-entry-point
808 (convert-optional-entry fun entry-vars entry-vals
()
812 (optional-dispatch-entry-points res
))
814 ((not (lambda-var-arg-info (first vars
)))
815 (let* ((arg (first vars
))
816 (nvars (cons arg default-vars
))
817 (nvals (cons (leaf-source-name arg
) default-vals
)))
818 (ir1-convert-hairy-args res nvars nvals nvars nvals
819 (rest vars
) nil body aux-vars aux-vals
820 source-name debug-name
821 nil post-binding-lexenv system-lambda
)))
823 (let* ((arg (first vars
))
824 (info (lambda-var-arg-info arg
))
825 (kind (arg-info-kind info
)))
828 (let ((ep (generate-optional-default-entry
829 res default-vars default-vals
830 entry-vars entry-vals vars supplied-p-p body
832 source-name debug-name
833 force post-binding-lexenv
835 ;; See GENERATE-OPTIONAL-DEFAULT-ENTRY.
836 (push (if (lambda-p ep
)
837 (register-entry-point
839 (convert-optional-entry
840 ep entry-vars entry-vals nil
841 (or debug-name source-name
))
844 (progn (aver (not supplied-p-p
))
846 (optional-dispatch-entry-points res
))
849 (ir1-convert-more res default-vars default-vals
850 entry-vars entry-vals
851 arg nil nil
(rest vars
) supplied-p-p body
853 source-name debug-name
854 post-binding-lexenv system-lambda
))
856 (ir1-convert-more res default-vars default-vals
857 entry-vars entry-vals
858 nil arg
(second vars
) (cddr vars
) supplied-p-p
859 body aux-vars aux-vals
860 source-name debug-name
861 post-binding-lexenv system-lambda
))
863 (ir1-convert-more res default-vars default-vals
864 entry-vars entry-vals
865 nil nil nil vars supplied-p-p body aux-vars
866 aux-vals source-name debug-name
867 post-binding-lexenv system-lambda
)))))))
869 ;;; This function deals with the case where we have to make an
870 ;;; OPTIONAL-DISPATCH to represent a LAMBDA. We cons up the result and
871 ;;; call IR1-CONVERT-HAIRY-ARGS to do the work. When it is done, we
872 ;;; figure out the MIN-ARGS and MAX-ARGS.
873 (defun ir1-convert-hairy-lambda (body vars keyp allowp aux-vars aux-vals
874 &key post-binding-lexenv
875 (source-name '.anonymous.
)
876 debug-name system-lambda
)
877 (declare (list body vars aux-vars aux-vals
))
878 (aver (or debug-name
(neq '.anonymous. source-name
)))
879 (let ((res (make-optional-dispatch :arglist vars
882 :%source-name source-name
883 :%debug-name debug-name
884 :plist
`(:ir1-environment
887 (min (or (position-if #'lambda-var-arg-info vars
) (length vars
))))
888 (aver-live-component *current-component
*)
889 (push res
(component-new-functionals *current-component
*))
890 (ir1-convert-hairy-args res
() () () () vars nil body aux-vars aux-vals
891 source-name debug-name nil post-binding-lexenv
893 (setf (optional-dispatch-min-args res
) min
)
894 (setf (optional-dispatch-max-args res
)
895 (+ (1- (length (optional-dispatch-entry-points res
))) min
))
899 ;;; Convert a LAMBDA form into a LAMBDA leaf or an OPTIONAL-DISPATCH leaf.
900 (defun ir1-convert-lambda (form &key
(source-name '.anonymous.
)
901 debug-name maybe-add-debug-catch
904 (compiler-error "A ~S was found when expecting a lambda expression:~% ~S"
907 (unless (eq (car form
) 'lambda
)
908 (compiler-error "~S was expected but ~S was found:~% ~S"
912 (unless (and (consp (cdr form
)) (listp (cadr form
)))
914 "The lambda expression has a missing or non-list lambda list:~% ~S"
916 (when (and system-lambda maybe-add-debug-catch
)
917 (bug "Both SYSTEM-LAMBDA and MAYBE-ADD-DEBUG-CATCH specified"))
918 (unless (or debug-name
(neq '.anonymous. source-name
))
919 (setf debug-name
(name-lambdalike form
)))
920 (multiple-value-bind (vars keyp allow-other-keys aux-vars aux-vals
)
921 (make-lambda-vars (cadr form
))
922 (multiple-value-bind (forms decls doc
) (parse-body (cddr form
))
923 (binding* (((*lexenv
* result-type post-binding-lexenv
)
924 (process-decls decls
(append aux-vars vars
) nil
926 (debug-catch-p (and maybe-add-debug-catch
927 *allow-instrumenting
*
929 (>= insert-debug-catch
2))))
930 (forms (if debug-catch-p
931 (wrap-forms-in-debug-catch forms
)
933 (forms (if (eq result-type
*wild-type
*)
935 `((the ,result-type
(progn ,@forms
)))))
936 (*allow-instrumenting
* (and (not system-lambda
) *allow-instrumenting
*))
937 (res (cond ((or (find-if #'lambda-var-arg-info vars
) keyp
)
938 (ir1-convert-hairy-lambda forms vars keyp
941 :post-binding-lexenv post-binding-lexenv
942 :source-name source-name
943 :debug-name debug-name
944 :system-lambda system-lambda
))
946 (ir1-convert-lambda-body forms vars
949 :post-binding-lexenv post-binding-lexenv
950 :source-name source-name
951 :debug-name debug-name
952 :system-lambda system-lambda
)))))
953 (setf (functional-inline-expansion res
) form
)
954 (setf (functional-arg-documentation res
) (cadr form
))
955 (setf (functional-documentation res
) doc
)
956 (when (boundp '*lambda-conversions
*)
957 ;; KLUDGE: Not counting TL-XEPs is a lie, of course, but
958 ;; keeps things less confusing to users of TIME, where this
960 (unless (and (consp debug-name
) (eq 'tl-xep
(car debug-name
)))
961 (incf *lambda-conversions
*)))
964 (defun wrap-forms-in-debug-catch (forms)
965 #!+unwind-to-frame-and-call-vop
966 `((multiple-value-prog1
969 ;; Just ensure that there won't be any tail-calls, IR2 magic will
972 #!-unwind-to-frame-and-call-vop
973 `( ;; Normally, we'll return from this block with the below RETURN-FROM.
976 ;; If DEBUG-CATCH-TAG is thrown (with a thunk as the value) the
977 ;; RETURN-FROM is elided and we funcall the thunk instead. That
978 ;; thunk might either return a value (for a RETURN-FROM-FRAME)
979 ;; or call this same function again (for a RESTART-FRAME).
980 ;; -- JES, 2007-01-09
983 ;; Use a constant catch tag instead of consing a new one for every
984 ;; entry to this block. The uniquencess of the catch tags is
985 ;; ensured when the tag is throw by the debugger. It'll allocate a
986 ;; new tag, and modify the reference this tag in the proper
987 ;; catch-block structure to refer to that new tag. This
988 ;; significantly decreases the runtime cost of high debug levels.
989 ;; -- JES, 2007-01-09
990 (catch 'debug-catch-tag
991 (return-from return-value-tag
995 ;;; helper for LAMBDA-like things, to massage them into a form
996 ;;; suitable for IR1-CONVERT-LAMBDA.
997 (defun ir1-convert-lambdalike (thing
999 (source-name '.anonymous.
)
1001 (when (and (not debug-name
) (eq '.anonymous. source-name
))
1002 (setf debug-name
(name-lambdalike thing
)))
1005 (ir1-convert-lambda thing
1006 :maybe-add-debug-catch t
1007 :source-name source-name
1008 :debug-name debug-name
))
1010 (deprecation-warning 'instance-lambda
'lambda
)
1011 (ir1-convert-lambda `(lambda ,@(cdr thing
))
1012 :source-name source-name
1013 :debug-name debug-name
))
1015 (let ((name (cadr thing
))
1016 (lambda-expression `(lambda ,@(cddr thing
))))
1017 (if (and name
(legal-fun-name-p name
))
1018 (let ((defined-fun-res (get-defined-fun name
))
1019 (res (ir1-convert-lambda lambda-expression
1020 :maybe-add-debug-catch t
1021 :source-name name
)))
1022 (assert-global-function-definition-type name res
)
1023 (push res
(defined-fun-functionals defined-fun-res
))
1024 (unless (eq (defined-fun-inlinep defined-fun-res
) :notinline
)
1027 (policy ref
(> recognize-self-calls
0)))
1028 res defined-fun-res
))
1030 (ir1-convert-lambda lambda-expression
1031 :maybe-add-debug-catch t
1033 (or name
(name-lambdalike thing
))))))
1034 ((lambda-with-lexenv)
1035 (ir1-convert-inline-lambda thing
1036 :source-name source-name
1037 :debug-name debug-name
))))
1039 ;;;; defining global functions
1041 ;;; Convert FUN as a lambda in the null environment, but use the
1042 ;;; current compilation policy. Note that FUN may be a
1043 ;;; LAMBDA-WITH-LEXENV, so we may have to augment the environment to
1044 ;;; reflect the state at the definition site.
1045 (defun ir1-convert-inline-lambda (fun
1047 (source-name '.anonymous.
)
1050 (when (and (not debug-name
) (eq '.anonymous. source-name
))
1051 (setf debug-name
(name-lambdalike fun
)))
1052 (destructuring-bind (decls macros symbol-macros
&rest body
)
1053 (if (eq (car fun
) 'lambda-with-lexenv
)
1055 `(() () () .
,(cdr fun
)))
1056 (let* ((*lexenv
* (make-lexenv
1057 :default
(process-decls decls nil nil
1058 :lexenv
(make-null-lexenv))
1059 :vars
(copy-list symbol-macros
)
1060 :funs
(mapcar (lambda (x)
1062 (macro .
,(coerce (cdr x
) 'function
))))
1064 ;; Inherit MUFFLE-CONDITIONS from the call-site lexenv
1065 ;; rather than the definition-site lexenv, since it seems
1066 ;; like a much more common case.
1067 :handled-conditions
(lexenv-handled-conditions *lexenv
*)
1068 :policy
(lexenv-policy *lexenv
*)))
1069 (clambda (ir1-convert-lambda `(lambda ,@body
)
1070 :source-name source-name
1071 :debug-name debug-name
1072 :system-lambda system-lambda
)))
1073 (setf (functional-inline-expanded clambda
) t
)
1076 ;;; Get a DEFINED-FUN object for a function we are about to define. If
1077 ;;; the function has been forward referenced, then substitute for the
1078 ;;; previous references.
1079 (defun get-defined-fun (name)
1080 (proclaim-as-fun-name name
)
1081 (let ((found (find-free-fun name
"shouldn't happen! (defined-fun)")))
1082 (note-name-defined name
:function
)
1083 (cond ((not (defined-fun-p found
))
1084 (aver (not (info :function
:inlinep name
)))
1085 (let* ((where-from (leaf-where-from found
))
1086 (res (make-defined-fun
1088 :where-from
(if (eq where-from
:declared
)
1090 :type
(leaf-type found
))))
1091 (substitute-leaf res found
)
1092 (setf (gethash name
*free-funs
*) res
)))
1093 ;; If *FREE-FUNS* has a previously converted definition
1094 ;; for this name, then blow it away and try again.
1095 ((defined-fun-functionals found
)
1096 (remhash name
*free-funs
*)
1097 (get-defined-fun name
))
1100 ;;; Check a new global function definition for consistency with
1101 ;;; previous declaration or definition, and assert argument/result
1102 ;;; types if appropriate. This assertion is suppressed by the
1103 ;;; EXPLICIT-CHECK attribute, which is specified on functions that
1104 ;;; check their argument types as a consequence of type dispatching.
1105 ;;; This avoids redundant checks such as NUMBERP on the args to +, etc.
1106 (defun assert-new-definition (var fun
)
1107 (let ((type (leaf-type var
))
1108 (for-real (eq (leaf-where-from var
) :declared
))
1109 (info (info :function
:info
(leaf-source-name var
))))
1110 (assert-definition-type
1112 ;; KLUDGE: Common Lisp is such a dynamic language that in general
1113 ;; all we can do here in general is issue a STYLE-WARNING. It
1114 ;; would be nice to issue a full WARNING in the special case of
1115 ;; of type mismatches within a compilation unit (as in section
1116 ;; 3.2.2.3 of the spec) but at least as of sbcl-0.6.11, we don't
1117 ;; keep track of whether the mismatched data came from the same
1118 ;; compilation unit, so we can't do that. -- WHN 2001-02-11
1119 :lossage-fun
#'compiler-style-warn
1120 :unwinnage-fun
(cond (info #'compiler-style-warn
)
1121 (for-real #'compiler-notify
)
1126 (ir1-attributep (fun-info-attributes info
)
1129 "previous declaration"
1130 "previous definition"))))
1132 ;;; Used for global inline expansion. Earlier something like this was
1133 ;;; used by %DEFUN too. FIXME: And now it's probably worth rethinking
1134 ;;; whether this function is a good idea at all.
1135 (defun ir1-convert-inline-expansion (name expansion var inlinep info
)
1136 ;; Unless a :INLINE function, we temporarily clobber the inline
1137 ;; expansion. This prevents recursive inline expansion of
1138 ;; opportunistic pseudo-inlines.
1139 (unless (eq inlinep
:inline
)
1140 (setf (defined-fun-inline-expansion var
) nil
))
1141 (let ((fun (ir1-convert-inline-lambda expansion
1143 ;; prevent instrumentation of
1144 ;; known function expansions
1145 :system-lambda
(and info t
))))
1146 (setf (functional-inlinep fun
) inlinep
)
1147 (assert-new-definition var fun
)
1148 (setf (defined-fun-inline-expansion var
) expansion
)
1149 ;; substitute for any old references
1150 (unless (or (not *block-compile
*)
1152 (or (fun-info-transforms info
)
1153 (fun-info-templates info
)
1154 (fun-info-ir2-convert info
))))
1155 (substitute-leaf fun var
))
1158 ;;; the even-at-compile-time part of DEFUN
1160 ;;; The INLINE-EXPANSION is a LAMBDA-WITH-LEXENV, or NIL if there is
1161 ;;; no inline expansion.
1162 (defun %compiler-defun
(name lambda-with-lexenv compile-toplevel
)
1163 (let ((defined-fun nil
)) ; will be set below if we're in the compiler
1164 (when compile-toplevel
1165 ;; better be in the compiler
1166 (aver (boundp '*lexenv
*))
1167 (remhash name
*free-funs
*)
1168 (setf defined-fun
(get-defined-fun name
))
1169 (aver (fasl-output-p *compile-object
*))
1170 (if (member name
*fun-names-in-this-file
* :test
#'equal
)
1171 (warn 'duplicate-definition
:name name
)
1172 (push name
*fun-names-in-this-file
*)))
1174 (become-defined-fun-name name
)
1176 (cond (lambda-with-lexenv
1177 (setf (info :function
:inline-expansion-designator name
)
1180 (setf (defined-fun-inline-expansion defined-fun
)
1181 lambda-with-lexenv
)))
1183 (clear-info :function
:inline-expansion-designator name
)))
1185 ;; old CMU CL comment:
1186 ;; If there is a type from a previous definition, blast it,
1187 ;; since it is obsolete.
1188 (when (and defined-fun
1189 (eq (leaf-where-from defined-fun
) :defined
))
1190 (setf (leaf-type defined-fun
)
1191 ;; FIXME: If this is a block compilation thing, shouldn't
1192 ;; we be setting the type to the full derived type for the
1193 ;; definition, instead of this most general function type?
1194 (specifier-type 'function
))))
1199 ;;; Entry point utilities
1201 ;;; Return a function for the Nth entry point.
1202 (defun optional-dispatch-entry-point-fun (dispatcher n
)
1203 (declare (type optional-dispatch dispatcher
)
1204 (type unsigned-byte n
))
1205 (let* ((env (getf (optional-dispatch-plist dispatcher
) :ir1-environment
))
1206 (*lexenv
* (first env
))
1207 (*current-path
* (second env
)))
1208 (force (nth n
(optional-dispatch-entry-points dispatcher
)))))