cross-host-friendly version of uninterned symbol dumping
[sbcl.git] / src / compiler / ir1tran-lambda.lisp
blobe86893943159288d619cdbd6cea9c871171004cc
1 ;;;; This file contains code which does the translation of lambda
2 ;;;; forms from Lisp code to the first intermediate representation
3 ;;;; (IR1).
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
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.
14 (in-package "SB!C")
16 ;;;; LAMBDA hackery
18 ;;;; FIXME: where is that file?
19 ;;;; Note: Take a look at the compiler-overview.tex section on "Hairy
20 ;;;; function representation" before you seriously mess with this
21 ;;;; stuff.
23 ;;; Verify that the NAME is a legal name for a variable and return a
24 ;;; VAR structure for it, filling in info if it is globally special.
25 ;;; If it is losing, we punt with a COMPILER-ERROR. NAMES-SO-FAR is a
26 ;;; list of names which have previously been bound. If the NAME is in
27 ;;; this list, then we error out.
28 (declaim (ftype (sfunction (t list &optional t) lambda-var) varify-lambda-arg))
29 (defun varify-lambda-arg (name names-so-far &optional (context "lambda list"))
30 (declare (inline member))
31 (unless (symbolp name)
32 (compiler-error "~S is not a symbol, and cannot be used as a variable." name))
33 (when (member name names-so-far :test #'eq)
34 (compiler-error "The variable ~S occurs more than once in the ~A."
35 name
36 context))
37 (let ((kind (info :variable :kind name)))
38 (cond ((keywordp name)
39 (compiler-error "~S is a keyword, and cannot be used as a local variable."
40 name))
41 ((eq kind :constant)
42 (compiler-error "~@<~S names a defined constant, and cannot be used as a ~
43 local variable.~:@>"
44 name))
45 ((eq :global kind)
46 (compiler-error "~@<~S names a global lexical variable, and cannot be used ~
47 as a local variable.~:@>"
48 name))
49 ((eq kind :special)
50 (let ((specvar (find-free-var name)))
51 (make-lambda-var :%source-name name
52 :type (leaf-type specvar)
53 :where-from (leaf-where-from specvar)
54 :specvar specvar)))
56 (make-lambda-var :%source-name name)))))
58 ;;; Make the default keyword for a &KEY arg, checking that the keyword
59 ;;; isn't already used by one of the VARS.
60 (declaim (ftype (sfunction (symbol list t) symbol) make-keyword-for-arg))
61 (defun make-keyword-for-arg (symbol vars keywordify)
62 (let ((key (if (and keywordify (not (keywordp symbol)))
63 (keywordicate symbol)
64 symbol)))
65 (dolist (var vars)
66 (let ((info (lambda-var-arg-info var)))
67 (when (and info
68 (eq (arg-info-kind info) :keyword)
69 (eq (arg-info-key info) key))
70 (compiler-error
71 "The keyword ~S appears more than once in the lambda list."
72 key))))
73 key))
75 ;;; Parse a lambda list into a list of VAR structures, stripping off
76 ;;; any &AUX bindings. Each arg name is checked for legality, and
77 ;;; duplicate names are checked for. If an arg is globally special,
78 ;;; the var is marked as :SPECIAL instead of :LEXICAL. &KEY,
79 ;;; &OPTIONAL and &REST args are annotated with an ARG-INFO structure
80 ;;; which contains the extra information. If we hit something losing,
81 ;;; we bug out with COMPILER-ERROR. These values are returned:
82 ;;; 1. a list of the var structures for each top level argument;
83 ;;; 2. a flag indicating whether &KEY was specified;
84 ;;; 3. a flag indicating whether other &KEY args are allowed;
85 ;;; 4. a list of the &AUX variables; and
86 ;;; 5. a list of the &AUX values.
87 (declaim (ftype (sfunction (list) (values list boolean boolean list list))
88 make-lambda-vars))
89 (defun make-lambda-vars (list)
90 (multiple-value-bind (required optional restp rest keyp keys allowp auxp aux
91 morep more-context more-count)
92 (parse-lambda-list list)
93 (declare (ignore auxp)) ; since we just iterate over AUX regardless
94 (collect ((vars)
95 (names-so-far)
96 (aux-vars)
97 (aux-vals))
98 (flet (;; PARSE-DEFAULT deals with defaults and supplied-p args
99 ;; for optionals and keywords args.
100 (parse-default (spec info)
101 (when (consp (cdr spec))
102 (setf (arg-info-default info) (second spec))
103 (when (consp (cddr spec))
104 (let* ((supplied-p (third spec))
105 (supplied-var (varify-lambda-arg supplied-p
106 (names-so-far))))
107 (setf (arg-info-supplied-p info) supplied-var)
108 (names-so-far supplied-p)
109 (when (> (length (the list spec)) 3)
110 (compiler-error
111 "The list ~S is too long to be an arg specifier."
112 spec)))))))
114 (dolist (name required)
115 (let ((var (varify-lambda-arg name (names-so-far))))
116 (vars var)
117 (names-so-far name)))
119 (dolist (spec optional)
120 (if (atom spec)
121 (let ((var (varify-lambda-arg spec (names-so-far))))
122 (setf (lambda-var-arg-info var)
123 (make-arg-info :kind :optional))
124 (vars var)
125 (names-so-far spec))
126 (let* ((name (first spec))
127 (var (varify-lambda-arg name (names-so-far)))
128 (info (make-arg-info :kind :optional)))
129 (setf (lambda-var-arg-info var) info)
130 (vars var)
131 (names-so-far name)
132 (parse-default spec info))))
134 (when restp
135 (let ((var (varify-lambda-arg rest (names-so-far))))
136 (setf (lambda-var-arg-info var) (make-arg-info :kind :rest))
137 (vars var)
138 (names-so-far rest)))
140 (when morep
141 (let ((var (varify-lambda-arg more-context (names-so-far))))
142 (setf (lambda-var-arg-info var)
143 (make-arg-info :kind :more-context))
144 (vars var)
145 (names-so-far more-context))
146 (let ((var (varify-lambda-arg more-count (names-so-far))))
147 (setf (lambda-var-arg-info var)
148 (make-arg-info :kind :more-count))
149 (vars var)
150 (names-so-far more-count)))
152 (dolist (spec keys)
153 (cond
154 ((atom spec)
155 (let ((var (varify-lambda-arg spec (names-so-far))))
156 (setf (lambda-var-arg-info var)
157 (make-arg-info :kind :keyword
158 :key (make-keyword-for-arg spec
159 (vars)
160 t)))
161 (vars var)
162 (names-so-far spec)))
163 ((atom (first spec))
164 (let* ((name (first spec))
165 (var (varify-lambda-arg name (names-so-far)))
166 (info (make-arg-info
167 :kind :keyword
168 :key (make-keyword-for-arg name (vars) t))))
169 (setf (lambda-var-arg-info var) info)
170 (vars var)
171 (names-so-far name)
172 (parse-default spec info)))
174 (let ((head (first spec)))
175 (unless (proper-list-of-length-p head 2)
176 (error "malformed &KEY argument specifier: ~S" spec))
177 (let* ((name (second head))
178 (var (varify-lambda-arg name (names-so-far)))
179 (info (make-arg-info
180 :kind :keyword
181 :key (make-keyword-for-arg (first head)
182 (vars)
183 nil))))
184 (setf (lambda-var-arg-info var) info)
185 (vars var)
186 (names-so-far name)
187 (parse-default spec info))))))
189 (dolist (spec aux)
190 (cond ((atom spec)
191 (let ((var (varify-lambda-arg spec nil)))
192 (aux-vars var)
193 (aux-vals nil)
194 (names-so-far spec)))
196 (unless (proper-list-of-length-p spec 1 2)
197 (compiler-error "malformed &AUX binding specifier: ~S"
198 spec))
199 (let* ((name (first spec))
200 (var (varify-lambda-arg name nil)))
201 (aux-vars var)
202 (aux-vals (second spec))
203 (names-so-far name)))))
205 (values (vars) keyp allowp (aux-vars) (aux-vals))))))
207 ;;; This is similar to IR1-CONVERT-PROGN-BODY except that we
208 ;;; sequentially bind each AUX-VAR to the corresponding AUX-VAL before
209 ;;; converting the body. If there are no bindings, just convert the
210 ;;; body, otherwise do one binding and recurse on the rest.
212 ;;; FIXME: This could and probably should be converted to use
213 ;;; SOURCE-NAME and DEBUG-NAME. But I (WHN) don't use &AUX bindings,
214 ;;; so I'm not motivated. Patches will be accepted...
215 (defun ir1-convert-aux-bindings (start next result body aux-vars aux-vals
216 post-binding-lexenv)
217 (declare (type ctran start next) (type (or lvar null) result)
218 (list body aux-vars aux-vals))
219 (if (null aux-vars)
220 (let ((*lexenv* (make-lexenv :vars (copy-list post-binding-lexenv))))
221 (ir1-convert-progn-body start next result body))
222 (let ((ctran (make-ctran))
223 (fun-lvar (make-lvar))
224 (fun (ir1-convert-lambda-body body
225 (list (first aux-vars))
226 :aux-vars (rest aux-vars)
227 :aux-vals (rest aux-vals)
228 :post-binding-lexenv post-binding-lexenv
229 :debug-name (debug-name
230 '&aux-bindings
231 aux-vars))))
232 (reference-leaf start ctran fun-lvar fun)
233 (ir1-convert-combination-args fun-lvar ctran next result
234 (list (first aux-vals)))))
235 (values))
237 ;;; This is similar to IR1-CONVERT-PROGN-BODY except that code to bind
238 ;;; the SPECVAR for each SVAR to the value of the variable is wrapped
239 ;;; around the body. If there are no special bindings, we just convert
240 ;;; the body, otherwise we do one special binding and recurse on the
241 ;;; rest.
243 ;;; We make a cleanup and introduce it into the lexical
244 ;;; environment. If there are multiple special bindings, the cleanup
245 ;;; for the blocks will end up being the innermost one. We force NEXT
246 ;;; to start a block outside of this cleanup, causing cleanup code to
247 ;;; be emitted when the scope is exited.
248 (defun ir1-convert-special-bindings
249 (start next result body aux-vars aux-vals svars post-binding-lexenv)
250 (declare (type ctran start next) (type (or lvar null) result)
251 (list body aux-vars aux-vals svars))
252 (cond
253 ((null svars)
254 (ir1-convert-aux-bindings start next result body aux-vars aux-vals
255 post-binding-lexenv))
257 (ctran-starts-block next)
258 (let ((cleanup (make-cleanup :kind :special-bind))
259 (var (first svars))
260 (bind-ctran (make-ctran))
261 (cleanup-ctran (make-ctran)))
262 (ir1-convert start bind-ctran nil
263 `(%special-bind ',(lambda-var-specvar var) ,var))
264 (setf (cleanup-mess-up cleanup) (ctran-use bind-ctran))
265 (let ((*lexenv* (make-lexenv :cleanup cleanup)))
266 (ir1-convert bind-ctran cleanup-ctran nil '(%cleanup-point))
267 (ir1-convert-special-bindings cleanup-ctran next result
268 body aux-vars aux-vals
269 (rest svars)
270 post-binding-lexenv)))))
271 (values))
273 ;;; Create a lambda node out of some code, returning the result. The
274 ;;; bindings are specified by the list of VAR structures VARS. We deal
275 ;;; with adding the names to the LEXENV-VARS for the conversion. The
276 ;;; result is added to the NEW-FUNCTIONALS in the *CURRENT-COMPONENT*
277 ;;; and linked to the component head and tail.
279 ;;; We detect special bindings here, replacing the original VAR in the
280 ;;; lambda list with a temporary variable. We then pass a list of the
281 ;;; special vars to IR1-CONVERT-SPECIAL-BINDINGS, which actually emits
282 ;;; the special binding code.
284 ;;; We ignore any ARG-INFO in the VARS, trusting that someone else is
285 ;;; dealing with &NONSENSE, except for &REST vars with DYNAMIC-EXTENT.
287 ;;; AUX-VARS is a list of VAR structures for variables that are to be
288 ;;; sequentially bound. Each AUX-VAL is a form that is to be evaluated
289 ;;; to get the initial value for the corresponding AUX-VAR.
290 (defun ir1-convert-lambda-body (body
291 vars
292 &key
293 aux-vars
294 aux-vals
295 (source-name '.anonymous.)
296 debug-name
297 (note-lexical-bindings t)
298 post-binding-lexenv
299 system-lambda)
300 (declare (list body vars aux-vars aux-vals))
302 ;; We're about to try to put new blocks into *CURRENT-COMPONENT*.
303 (aver-live-component *current-component*)
305 (let* ((bind (make-bind))
306 (lambda (make-lambda :vars vars
307 :bind bind
308 :%source-name source-name
309 :%debug-name debug-name
310 :system-lambda-p system-lambda))
311 (result-ctran (make-ctran))
312 (result-lvar (make-lvar)))
314 (awhen (lexenv-lambda *lexenv*)
315 (push lambda (lambda-children it))
316 (setf (lambda-parent lambda) it))
318 ;; just to check: This function should fail internal assertions if
319 ;; we didn't set up a valid debug name above.
321 ;; (In SBCL we try to make everything have a debug name, since we
322 ;; lack the omniscient perspective the original implementors used
323 ;; to decide which things didn't need one.)
324 (functional-debug-name lambda)
326 (setf (lambda-home lambda) lambda)
327 (collect ((svars)
328 (new-venv nil cons))
330 (dolist (var vars)
331 ;; As far as I can see, LAMBDA-VAR-HOME should never have
332 ;; been set before. Let's make sure. -- WHN 2001-09-29
333 (aver (not (lambda-var-home var)))
334 (setf (lambda-var-home var) lambda)
335 (let ((specvar (lambda-var-specvar var)))
336 (cond (specvar
337 (svars var)
338 (new-venv (cons (leaf-source-name specvar) specvar)))
340 (when note-lexical-bindings
341 (note-lexical-binding (leaf-source-name var)))
342 (new-venv (cons (leaf-source-name var) var))))))
344 (let ((*lexenv* (make-lexenv :vars (new-venv)
345 :lambda lambda
346 :cleanup nil)))
347 (setf (bind-lambda bind) lambda)
348 (setf (node-lexenv bind) *lexenv*)
350 (let ((block (ctran-starts-block result-ctran)))
351 (let ((return (make-return :result result-lvar :lambda lambda))
352 (tail-set (make-tail-set :funs (list lambda))))
353 (setf (lambda-tail-set lambda) tail-set)
354 (setf (lambda-return lambda) return)
355 (setf (lvar-dest result-lvar) return)
356 (link-node-to-previous-ctran return result-ctran)
357 (setf (block-last block) return))
358 (link-blocks block (component-tail *current-component*)))
360 (with-component-last-block (*current-component*
361 (ctran-block result-ctran))
362 (let ((prebind-ctran (make-ctran))
363 (postbind-ctran (make-ctran)))
364 (ctran-starts-block prebind-ctran)
365 (link-node-to-previous-ctran bind prebind-ctran)
366 (use-ctran bind postbind-ctran)
367 (ir1-convert-special-bindings postbind-ctran result-ctran
368 result-lvar body
369 aux-vars aux-vals (svars)
370 post-binding-lexenv)))))
372 (link-blocks (component-head *current-component*) (node-block bind))
373 (push lambda (component-new-functionals *current-component*))
375 lambda))
377 ;;; Entry point CLAMBDAs have a special kind
378 (defun register-entry-point (entry dispatcher)
379 (declare (type clambda entry)
380 (type optional-dispatch dispatcher))
381 (setf (functional-kind entry) :optional)
382 (setf (leaf-ever-used entry) t)
383 (setf (lambda-optional-dispatch entry) dispatcher)
384 entry)
386 ;;; Create the actual entry-point function for an optional entry
387 ;;; point. The lambda binds copies of each of the VARS, then calls FUN
388 ;;; with the argument VALS and the DEFAULTS. Presumably the VALS refer
389 ;;; to the VARS by name. The VALS are passed in the reverse order.
391 ;;; If any of the copies of the vars are referenced more than once,
392 ;;; then we mark the corresponding var as EVER-USED to inhibit
393 ;;; "defined but not read" warnings for arguments that are only used
394 ;;; by default forms.
395 (defun convert-optional-entry (fun vars vals defaults name)
396 (declare (type clambda fun) (list vars vals defaults))
397 (let* ((fvars (reverse vars))
398 (arg-vars (mapcar (lambda (var)
399 (make-lambda-var
400 :%source-name (leaf-source-name var)
401 :type (leaf-type var)
402 :where-from (leaf-where-from var)
403 :specvar (lambda-var-specvar var)))
404 fvars))
405 (fun (collect ((default-bindings)
406 (default-vals))
407 (dolist (default defaults)
408 (if (sb!xc:constantp default)
409 (default-vals default)
410 (let ((var (sb!xc:gensym)))
411 (default-bindings `(,var ,default))
412 (default-vals var))))
413 (let ((bindings (default-bindings))
414 (call
415 `(locally
416 ;; See lengthy comment at top of 'seqtran'
417 ;; as to why muffling is not done during xc.
418 #-sb-xc-host
419 (declare (muffle-conditions code-deletion-note))
420 (%funcall ,fun ,@(reverse vals) ,@(default-vals)))))
421 (ir1-convert-lambda-body (if bindings
422 `((let (,@bindings) ,call))
423 `(,call))
424 arg-vars
425 ;; FIXME: Would be nice to
426 ;; share these names instead
427 ;; of consing up several
428 ;; identical ones. Oh well.
429 :debug-name (debug-name
430 '&optional-processor
431 name)
432 :note-lexical-bindings nil
433 :system-lambda t)))))
434 (mapc (lambda (var arg-var)
435 (when (cdr (leaf-refs arg-var))
436 (setf (leaf-ever-used var) t)))
437 fvars arg-vars)
438 fun))
440 ;;; This function deals with supplied-p vars in optional arguments. If
441 ;;; there is no supplied-p arg, then we just call
442 ;;; IR1-CONVERT-HAIRY-ARGS on the remaining arguments, and generate a
443 ;;; optional entry that calls the result. If there is a supplied-p
444 ;;; var, then we add it into the default vars and throw a T into the
445 ;;; entry values. The resulting entry point function is returned.
446 (defun generate-optional-default-entry (res default-vars default-vals
447 entry-vars entry-vals
448 vars supplied-p-p body
449 aux-vars aux-vals
450 source-name debug-name
451 force post-binding-lexenv
452 system-lambda)
453 (declare (type optional-dispatch res)
454 (list default-vars default-vals entry-vars entry-vals vars body
455 aux-vars aux-vals))
456 (let* ((arg (first vars))
457 (arg-name (leaf-source-name arg))
458 (info (lambda-var-arg-info arg))
459 (default (arg-info-default info))
460 (supplied-p (arg-info-supplied-p info))
461 (force (or force
462 (not (sb!xc:constantp (arg-info-default info)))))
463 (ep (if supplied-p
464 (ir1-convert-hairy-args
466 (list* supplied-p arg default-vars)
467 (list* (leaf-source-name supplied-p) arg-name default-vals)
468 (cons arg entry-vars)
469 (list* t arg-name entry-vals)
470 (rest vars) t body aux-vars aux-vals
471 source-name debug-name
472 force post-binding-lexenv system-lambda)
473 (ir1-convert-hairy-args
475 (cons arg default-vars)
476 (cons arg-name default-vals)
477 (cons arg entry-vars)
478 (cons arg-name entry-vals)
479 (rest vars) supplied-p-p body aux-vars aux-vals
480 source-name debug-name
481 force post-binding-lexenv system-lambda))))
483 ;; We want to delay converting the entry, but there exist
484 ;; problems: hidden references should not be established to
485 ;; lambdas of kind NIL should not have (otherwise the compiler
486 ;; might let-convert or delete them) and to variables.
487 (let ((name (or debug-name source-name)))
488 (if (or force
489 supplied-p-p ; this entry will be of kind NIL
490 (and (lambda-p ep) (eq (lambda-kind ep) nil)))
491 (convert-optional-entry ep
492 default-vars default-vals
493 (if supplied-p (list default nil) (list default))
494 name)
495 (let* ((default `',(constant-form-value default))
496 (defaults (if supplied-p (list default nil) (list default))))
497 ;; DEFAULT can contain a reference to a
498 ;; to-be-optimized-away function/block/tag, so better to
499 ;; reduce code now (but we possibly lose syntax checking
500 ;; in an unreachable code).
501 (delay
502 (register-entry-point
503 (convert-optional-entry (force ep)
504 default-vars default-vals
505 defaults
506 name)
507 res)))))))
509 ;;; Create the MORE-ENTRY function for the OPTIONAL-DISPATCH RES.
510 ;;; ENTRY-VARS and ENTRY-VALS describe the fixed arguments. REST is
511 ;;; the var for any &REST arg. KEYS is a list of the &KEY arg vars.
513 ;;; The most interesting thing that we do is parse keywords. We create
514 ;;; a bunch of temporary variables to hold the result of the parse,
515 ;;; and then loop over the supplied arguments, setting the appropriate
516 ;;; temps for the supplied keyword. Note that it is significant that
517 ;;; we iterate over the keywords in reverse order --- this implements
518 ;;; the CL requirement that (when a keyword appears more than once)
519 ;;; the first value is used.
521 ;;; If there is no supplied-p var, then we initialize the temp to the
522 ;;; default and just pass the temp into the main entry. Since
523 ;;; non-constant &KEY args are forcibly given a supplied-p var, we
524 ;;; know that the default is constant, and thus safe to evaluate out
525 ;;; of order.
527 ;;; If there is a supplied-p var, then we create temps for both the
528 ;;; value and the supplied-p, and pass them into the main entry,
529 ;;; letting it worry about defaulting.
531 ;;; We deal with :ALLOW-OTHER-KEYS by delaying unknown keyword errors
532 ;;; until we have scanned all the keywords.
533 (defun convert-more-entry (res entry-vars entry-vals rest morep keys name)
534 (declare (type optional-dispatch res) (list entry-vars entry-vals keys))
535 (collect ((arg-vars)
536 (arg-vals (reverse entry-vals))
537 (temps)
538 (body))
540 (dolist (var (reverse entry-vars))
541 (arg-vars (make-lambda-var :%source-name (leaf-source-name var)
542 :type (leaf-type var)
543 :where-from (leaf-where-from var))))
545 (let* ((n-context (sb!xc:gensym "N-CONTEXT-"))
546 (context-temp (make-lambda-var :%source-name n-context))
547 (n-count (sb!xc:gensym "N-COUNT-"))
548 (count-temp (make-lambda-var :%source-name n-count
549 :type (specifier-type 'index))))
551 (arg-vars context-temp count-temp)
553 (when rest
554 (arg-vals `(%listify-rest-args ,n-context ,n-count)))
555 (when morep
556 (arg-vals n-context)
557 (arg-vals n-count))
559 ;; The reason for all the noise with
560 ;; STACK-GROWS-DOWNWARD-NOT-UPWARD is to enable generation of
561 ;; slightly more efficient code on x86oid processors. (We can
562 ;; hoist the negation of the index outside the main parsing loop
563 ;; and take advantage of the base+index+displacement addressing
564 ;; mode on x86oids.)
565 (when (optional-dispatch-keyp res)
566 (let ((n-index (sb!xc:gensym "N-INDEX-"))
567 (n-key (sb!xc:gensym "N-KEY-"))
568 (n-value-temp (sb!xc:gensym "N-VALUE-TEMP-"))
569 (n-allowp (sb!xc:gensym "N-ALLOWP-"))
570 (n-lose (sb!xc:gensym "N-LOSE-"))
571 (n-losep (sb!xc:gensym "N-LOSEP-"))
572 (allowp (or (optional-dispatch-allowp res)
573 (policy *lexenv* (zerop safety))))
574 (found-allow-p nil))
576 (temps #!-stack-grows-downward-not-upward
577 `(,n-index (1- ,n-count))
578 #!+stack-grows-downward-not-upward
579 `(,n-index (- (1- ,n-count)))
580 #!-stack-grows-downward-not-upward n-value-temp
581 #!-stack-grows-downward-not-upward n-key)
582 (body `(declare (fixnum ,n-index)
583 #!-stack-grows-downward-not-upward
584 (ignorable ,n-value-temp ,n-key)))
586 (collect ((tests))
587 (dolist (key keys)
588 (let* ((info (lambda-var-arg-info key))
589 (default (arg-info-default info))
590 (keyword (arg-info-key info))
591 (supplied-p (arg-info-supplied-p info))
592 (supplied-used-p (arg-info-supplied-used-p info))
593 (n-value (sb!xc:gensym "N-VALUE-"))
594 (clause (cond (supplied-p
595 (let ((n-supplied (sb!xc:gensym "N-SUPPLIED-")))
596 (temps (list n-supplied
597 (if supplied-used-p
599 0)))
600 (arg-vals n-value n-supplied)
601 `((eq ,n-key ',keyword)
602 (setq ,n-supplied ,(if supplied-used-p
605 (setq ,n-value ,n-value-temp))))
607 (arg-vals n-value)
608 `((eq ,n-key ',keyword)
609 (setq ,n-value ,n-value-temp))))))
610 (when (and (not allowp) (eq keyword :allow-other-keys))
611 (setq found-allow-p t)
612 (setq clause
613 (append clause `((setq ,n-allowp ,n-value-temp)))))
615 (temps `(,n-value ,default))
616 (tests clause)))
618 (unless allowp
619 (temps n-allowp
620 (list n-lose 0)
621 (list n-losep 0))
622 (unless found-allow-p
623 (tests `((eq ,n-key :allow-other-keys)
624 (setq ,n-allowp ,n-value-temp))))
625 (tests `(t
626 (setq ,n-lose ,n-key
627 ,n-losep 1))))
629 (body
630 `(when (oddp ,n-count)
631 (%odd-key-args-error)))
633 (body
634 #!-stack-grows-downward-not-upward
635 `(locally
636 (declare (optimize (safety 0)))
637 (loop
638 (when (minusp ,n-index) (return))
639 (setf ,n-value-temp (%more-arg ,n-context ,n-index))
640 (decf ,n-index)
641 (setq ,n-key (%more-arg ,n-context ,n-index))
642 (decf ,n-index)
643 (cond ,@(tests))))
644 #!+stack-grows-downward-not-upward
645 `(locally (declare (optimize (safety 0)))
646 (loop
647 (when (plusp ,n-index) (return))
648 (multiple-value-bind (,n-value-temp ,n-key)
649 (%more-kw-arg ,n-context ,n-index)
650 (declare (ignorable ,n-value-temp ,n-key))
651 (incf ,n-index 2)
652 (cond ,@(tests))))))
654 (unless allowp
655 (body `(when (and (/= ,n-losep 0) (not ,n-allowp))
656 (%unknown-key-arg-error ,n-lose)))))))
658 (let ((ep (ir1-convert-lambda-body
659 `((let ,(temps)
660 ,@(body)
661 (%funcall ,(optional-dispatch-main-entry res)
662 ,@(arg-vals))))
663 (arg-vars)
664 :debug-name (debug-name '&more-processor name)
665 :note-lexical-bindings nil
666 :system-lambda t)))
667 (setf (optional-dispatch-more-entry res)
668 (register-entry-point ep res)))))
670 (values))
672 ;;; This is called by IR1-CONVERT-HAIRY-ARGS when we run into a &REST
673 ;;; or &KEY arg. The arguments are similar to that function, but we
674 ;;; split off any &REST arg and pass it in separately. REST is the
675 ;;; &REST arg var, or NIL if there is no &REST arg. KEYS is a list of
676 ;;; the &KEY argument vars.
678 ;;; When there are &KEY arguments, we introduce temporary gensym
679 ;;; variables to hold the values while keyword defaulting is in
680 ;;; progress to get the required sequential binding semantics.
682 ;;; This gets interesting mainly when there are &KEY arguments with
683 ;;; supplied-p vars or non-constant defaults. In either case, pass in
684 ;;; a supplied-p var. If the default is non-constant, we introduce an
685 ;;; IF in the main entry that tests the supplied-p var and decides
686 ;;; whether to evaluate the default or not. In this case, the real
687 ;;; incoming value is NIL, so we must union NULL with the declared
688 ;;; type when computing the type for the main entry's argument.
689 (defun ir1-convert-more (res default-vars default-vals entry-vars entry-vals
690 rest more-context more-count keys supplied-p-p
691 body aux-vars aux-vals source-name debug-name
692 post-binding-lexenv system-lambda)
693 (declare (type optional-dispatch res)
694 (list default-vars default-vals entry-vars entry-vals keys body
695 aux-vars aux-vals))
696 (collect ((main-vars (reverse default-vars))
697 (main-vals default-vals cons)
698 (bind-vars)
699 (bind-vals))
700 (when rest
701 (main-vars rest)
702 (main-vals '())
703 (unless (lambda-var-ignorep rest)
704 ;; Make up two extra variables, and squirrel them away in
705 ;; ARG-INFO-DEFAULT for transforming (VALUES-LIST REST) into
706 ;; (%MORE-ARG-VALUES CONTEXT 0 COUNT) when possible.
707 (let* ((context-name (sb!xc:gensym "REST-CONTEXT-"))
708 (context (make-lambda-var :%source-name context-name
709 :arg-info (make-arg-info :kind :more-context)))
710 (count-name (sb!xc:gensym "REST-COUNT-"))
711 (count (make-lambda-var :%source-name count-name
712 :arg-info (make-arg-info :kind :more-count)
713 :type (specifier-type 'index))))
714 (setf (arg-info-default (lambda-var-arg-info rest)) (list context count)
715 (lambda-var-ever-used context) t
716 (lambda-var-ever-used count) t)
717 (setf more-context context
718 more-count count))))
719 (when more-context
720 (main-vars more-context)
721 (main-vals nil)
722 (main-vars more-count)
723 (main-vals 0))
725 (dolist (key keys)
726 (let* ((info (lambda-var-arg-info key))
727 (default (arg-info-default info))
728 (hairy-default (not (sb!xc:constantp default)))
729 (supplied-p (arg-info-supplied-p info))
730 ;; was: (format nil "~A-DEFAULTING-TEMP" (leaf-source-name key))
731 (n-val (make-symbol ".DEFAULTING-TEMP."))
732 (val-temp (make-lambda-var :%source-name n-val)))
733 (main-vars val-temp)
734 (bind-vars key)
735 (cond ((or hairy-default supplied-p)
736 (let* ((n-supplied (sb!xc:gensym "N-SUPPLIED-"))
737 (supplied-temp (make-lambda-var
738 :%source-name n-supplied)))
739 (unless supplied-p
740 (setf (arg-info-supplied-p info) supplied-temp))
741 (when hairy-default
742 (setf (arg-info-default info) nil)
743 (unless supplied-p
744 (setf (arg-info-supplied-used-p info) nil)))
745 (main-vars supplied-temp)
746 (cond (hairy-default
747 (main-vals nil
748 (if supplied-p
751 (bind-vals
752 (if supplied-p
753 `(if ,n-supplied ,n-val ,default)
754 `(if (eq ,n-supplied 0) ,default ,n-val))))
756 (main-vals default nil)
757 (bind-vals n-val)))
758 (when supplied-p
759 (bind-vars supplied-p)
760 (bind-vals n-supplied))))
762 (main-vals (arg-info-default info))
763 (bind-vals n-val)))))
765 (let* ((main-entry (ir1-convert-lambda-body
766 body (main-vars)
767 :aux-vars (append (bind-vars) aux-vars)
768 :aux-vals (append (bind-vals) aux-vals)
769 :post-binding-lexenv post-binding-lexenv
770 :source-name source-name
771 :debug-name debug-name
772 :system-lambda system-lambda))
773 (name (or debug-name source-name))
774 (last-entry (convert-optional-entry main-entry default-vars
775 (main-vals) () name)))
776 (setf (optional-dispatch-main-entry res)
777 (register-entry-point main-entry res))
778 (convert-more-entry res entry-vars entry-vals rest more-context keys
779 name)
781 (push (register-entry-point
782 (if supplied-p-p
783 (convert-optional-entry last-entry entry-vars entry-vals
784 () name)
785 last-entry)
786 res)
787 (optional-dispatch-entry-points res))
788 last-entry)))
790 ;;; This function generates the entry point functions for the
791 ;;; OPTIONAL-DISPATCH RES. We accomplish this by recursion on the list
792 ;;; of arguments, analyzing the arglist on the way down and generating
793 ;;; entry points on the way up.
795 ;;; DEFAULT-VARS is a reversed list of all the argument vars processed
796 ;;; so far, including supplied-p vars. DEFAULT-VALS is a list of the
797 ;;; names of the DEFAULT-VARS.
799 ;;; ENTRY-VARS is a reversed list of processed argument vars,
800 ;;; excluding supplied-p vars. ENTRY-VALS is a list things that can be
801 ;;; evaluated to get the values for all the vars from the ENTRY-VARS.
802 ;;; It has the var name for each required or optional arg, and has T
803 ;;; for each supplied-p arg.
805 ;;; VARS is a list of the LAMBDA-VAR structures for arguments that
806 ;;; haven't been processed yet. SUPPLIED-P-P is true if a supplied-p
807 ;;; argument has already been processed; only in this case are the
808 ;;; DEFAULT-XXX and ENTRY-XXX different.
810 ;;; The result at each point is a lambda which should be called by the
811 ;;; above level to default the remaining arguments and evaluate the
812 ;;; body. We cause the body to be evaluated by converting it and
813 ;;; returning it as the result when the recursion bottoms out.
815 ;;; Each level in the recursion also adds its entry point function to
816 ;;; the result OPTIONAL-DISPATCH. For most arguments, the defaulting
817 ;;; function and the entry point function will be the same, but when
818 ;;; SUPPLIED-P args are present they may be different.
820 ;;; When we run into a &REST or &KEY arg, we punt out to
821 ;;; IR1-CONVERT-MORE, which finishes for us in this case.
822 (defun ir1-convert-hairy-args (res default-vars default-vals
823 entry-vars entry-vals
824 vars supplied-p-p body aux-vars
825 aux-vals
826 source-name debug-name
827 force post-binding-lexenv
828 system-lambda)
829 (declare (type optional-dispatch res)
830 (list default-vars default-vals entry-vars entry-vals vars body
831 aux-vars aux-vals))
832 (aver (or debug-name (neq '.anonymous. source-name)))
833 (cond ((not vars)
834 (if (optional-dispatch-keyp res)
835 ;; Handle &KEY with no keys...
836 (ir1-convert-more res default-vars default-vals
837 entry-vars entry-vals
838 nil nil nil vars supplied-p-p body aux-vars
839 aux-vals source-name debug-name
840 post-binding-lexenv system-lambda)
841 (let* ((name (or debug-name source-name))
842 (fun (ir1-convert-lambda-body
843 body (reverse default-vars)
844 :aux-vars aux-vars
845 :aux-vals aux-vals
846 :post-binding-lexenv post-binding-lexenv
847 :source-name source-name
848 :debug-name debug-name
849 :system-lambda system-lambda)))
851 (setf (optional-dispatch-main-entry res) fun)
852 (register-entry-point fun res)
853 (push (if supplied-p-p
854 (register-entry-point
855 (convert-optional-entry fun entry-vars entry-vals ()
856 name)
857 res)
858 fun)
859 (optional-dispatch-entry-points res))
860 fun)))
861 ((not (lambda-var-arg-info (first vars)))
862 (let* ((arg (first vars))
863 (nvars (cons arg default-vars))
864 (nvals (cons (leaf-source-name arg) default-vals)))
865 (ir1-convert-hairy-args res nvars nvals nvars nvals
866 (rest vars) nil body aux-vars aux-vals
867 source-name debug-name
868 nil post-binding-lexenv system-lambda)))
870 (let* ((arg (first vars))
871 (info (lambda-var-arg-info arg))
872 (kind (arg-info-kind info)))
873 (ecase kind
874 (:optional
875 (let ((ep (generate-optional-default-entry
876 res default-vars default-vals
877 entry-vars entry-vals vars supplied-p-p body
878 aux-vars aux-vals
879 source-name debug-name
880 force post-binding-lexenv
881 system-lambda)))
882 ;; See GENERATE-OPTIONAL-DEFAULT-ENTRY.
883 (push (if (lambda-p ep)
884 (register-entry-point
885 (if supplied-p-p
886 (convert-optional-entry
887 ep entry-vars entry-vals nil
888 (or debug-name source-name))
890 res)
891 (progn (aver (not supplied-p-p))
892 ep))
893 (optional-dispatch-entry-points res))
894 ep))
895 (:rest
896 (ir1-convert-more res default-vars default-vals
897 entry-vars entry-vals
898 arg nil nil (rest vars) supplied-p-p body
899 aux-vars aux-vals
900 source-name debug-name
901 post-binding-lexenv system-lambda))
902 (:more-context
903 (ir1-convert-more res default-vars default-vals
904 entry-vars entry-vals
905 nil arg (second vars) (cddr vars) supplied-p-p
906 body aux-vars aux-vals
907 source-name debug-name
908 post-binding-lexenv system-lambda))
909 (:keyword
910 (ir1-convert-more res default-vars default-vals
911 entry-vars entry-vals
912 nil nil nil vars supplied-p-p body aux-vars
913 aux-vals source-name debug-name
914 post-binding-lexenv system-lambda)))))))
916 ;;; This function deals with the case where we have to make an
917 ;;; OPTIONAL-DISPATCH to represent a LAMBDA. We cons up the result and
918 ;;; call IR1-CONVERT-HAIRY-ARGS to do the work. When it is done, we
919 ;;; figure out the MIN-ARGS and MAX-ARGS.
920 (defun ir1-convert-hairy-lambda (body vars keyp allowp aux-vars aux-vals
921 &key post-binding-lexenv
922 (source-name '.anonymous.)
923 debug-name system-lambda)
924 (declare (list body vars aux-vars aux-vals))
925 (aver (or debug-name (neq '.anonymous. source-name)))
926 (let ((res (make-optional-dispatch :arglist vars
927 :allowp allowp
928 :keyp keyp
929 :%source-name source-name
930 :%debug-name debug-name
931 :plist `(:ir1-environment
932 (,*lexenv*
933 ,*current-path*))))
934 (min (or (position-if #'lambda-var-arg-info vars) (length vars))))
935 (aver-live-component *current-component*)
936 (ir1-convert-hairy-args res () () () () vars nil body aux-vars aux-vals
937 source-name debug-name nil post-binding-lexenv
938 system-lambda)
939 ;; ir1-convert-hairy-args can throw 'locall-already-let-converted
940 ;; push optional-dispatch into the current component only after it
941 ;; normally returned
942 (push res (component-new-functionals *current-component*))
943 (setf (optional-dispatch-min-args res) min)
944 (setf (optional-dispatch-max-args res)
945 (+ (1- (length (optional-dispatch-entry-points res))) min))
947 res))
949 ;;; Convert a LAMBDA form into a LAMBDA leaf or an OPTIONAL-DISPATCH leaf.
950 (defun ir1-convert-lambda (form &key (source-name '.anonymous.)
951 debug-name maybe-add-debug-catch
952 system-lambda)
953 (unless (consp form)
954 (compiler-error "A ~S was found when expecting a lambda expression:~% ~S"
955 (type-of form)
956 form))
957 (unless (eq (car form) 'lambda)
958 (compiler-error "~S was expected but ~S was found:~% ~S"
959 'lambda
960 (car form)
961 form))
962 (unless (and (consp (cdr form)) (listp (cadr form)))
963 (compiler-error
964 "The lambda expression has a missing or non-list lambda list:~% ~S"
965 form))
966 (when (and system-lambda maybe-add-debug-catch)
967 (bug "Both SYSTEM-LAMBDA and MAYBE-ADD-DEBUG-CATCH specified"))
968 (unless (or debug-name (neq '.anonymous. source-name))
969 (setf debug-name (name-lambdalike form)))
970 (multiple-value-bind (vars keyp allow-other-keys aux-vars aux-vals)
971 (make-lambda-vars (cadr form))
972 (multiple-value-bind (forms decls doc) (parse-body (cddr form))
973 (binding* (((*lexenv* result-type post-binding-lexenv)
974 (process-decls decls (append aux-vars vars) nil
975 :binding-form-p t))
976 (debug-catch-p (and maybe-add-debug-catch
977 *allow-instrumenting*
978 (policy *lexenv*
979 (>= insert-debug-catch 2))))
980 (forms (if debug-catch-p
981 (wrap-forms-in-debug-catch forms)
982 forms))
983 (forms (if (eq result-type *wild-type*)
984 forms
985 `((the ,(type-specifier result-type) (progn ,@forms)))))
986 (*allow-instrumenting* (and (not system-lambda) *allow-instrumenting*))
987 (res (cond ((or (find-if #'lambda-var-arg-info vars) keyp)
988 (ir1-convert-hairy-lambda forms vars keyp
989 allow-other-keys
990 aux-vars aux-vals
991 :post-binding-lexenv post-binding-lexenv
992 :source-name source-name
993 :debug-name debug-name
994 :system-lambda system-lambda))
996 (ir1-convert-lambda-body forms vars
997 :aux-vars aux-vars
998 :aux-vals aux-vals
999 :post-binding-lexenv post-binding-lexenv
1000 :source-name source-name
1001 :debug-name debug-name
1002 :system-lambda system-lambda)))))
1003 (setf (functional-inline-expansion res) form)
1004 (setf (functional-arg-documentation res) (cadr form))
1005 (setf (functional-documentation res) doc)
1006 (when (boundp '*lambda-conversions*)
1007 ;; KLUDGE: Not counting TL-XEPs is a lie, of course, but
1008 ;; keeps things less confusing to users of TIME, where this
1009 ;; count gets used.
1010 (unless (and (consp debug-name) (eq 'tl-xep (car debug-name)))
1011 (incf *lambda-conversions*)))
1012 res))))
1014 (defun wrap-forms-in-debug-catch (forms)
1015 #!+unwind-to-frame-and-call-vop
1016 `((multiple-value-prog1
1017 (progn
1018 ,@forms)
1019 ;; Just ensure that there won't be any tail-calls, IR2 magic will
1020 ;; handle the rest.
1021 (values)))
1022 #!-unwind-to-frame-and-call-vop
1023 `( ;; Normally, we'll return from this block with the below RETURN-FROM.
1024 (block
1025 return-value-tag
1026 ;; If DEBUG-CATCH-TAG is thrown (with a thunk as the value) the
1027 ;; RETURN-FROM is elided and we funcall the thunk instead. That
1028 ;; thunk might either return a value (for a RETURN-FROM-FRAME)
1029 ;; or call this same function again (for a RESTART-FRAME).
1030 ;; -- JES, 2007-01-09
1031 (funcall
1032 (the function
1033 ;; Use a constant catch tag instead of consing a new one for every
1034 ;; entry to this block. The uniquencess of the catch tags is
1035 ;; ensured when the tag is throw by the debugger. It'll allocate a
1036 ;; new tag, and modify the reference this tag in the proper
1037 ;; catch-block structure to refer to that new tag. This
1038 ;; significantly decreases the runtime cost of high debug levels.
1039 ;; -- JES, 2007-01-09
1040 (catch 'debug-catch-tag
1041 (return-from return-value-tag
1042 (progn
1043 ,@forms))))))))
1045 ;;; helper for LAMBDA-like things, to massage them into a form
1046 ;;; suitable for IR1-CONVERT-LAMBDA.
1047 (defun ir1-convert-lambdalike (thing
1048 &key
1049 (source-name '.anonymous.)
1050 debug-name)
1051 (when (and (not debug-name) (eq '.anonymous. source-name))
1052 (setf debug-name (name-lambdalike thing)))
1053 (ecase (car thing)
1054 ((lambda)
1055 (ir1-convert-lambda thing
1056 :maybe-add-debug-catch t
1057 :source-name source-name
1058 :debug-name debug-name))
1059 ((named-lambda)
1060 (let ((name (cadr thing))
1061 (lambda-expression `(lambda ,@(cddr thing))))
1062 (if (and name (legal-fun-name-p name))
1063 (let ((defined-fun-res (get-defined-fun name (second lambda-expression)))
1064 (res (ir1-convert-lambda lambda-expression
1065 :maybe-add-debug-catch t
1066 :source-name name))
1067 (info (info :function :info name)))
1068 (assert-global-function-definition-type name res)
1069 (push res (defined-fun-functionals defined-fun-res))
1070 (unless (or
1071 (eq (defined-fun-inlinep defined-fun-res) :notinline)
1072 ;; Don't treat recursive stubs like CAR as self-calls
1073 ;; Maybe just use the fact that it is a known function?
1074 ;; Though a known function may be used
1075 ;; because of some other attributues but
1076 ;; still wants to get optimized self calls
1077 (and info
1078 (or (fun-info-templates info)
1079 (fun-info-transforms info)
1080 (fun-info-ltn-annotate info)
1081 (fun-info-ir2-convert info)
1082 (fun-info-optimizer info))))
1083 (substitute-leaf-if
1084 (lambda (ref)
1085 (policy ref (> recognize-self-calls 0)))
1086 res defined-fun-res))
1087 res)
1088 (ir1-convert-lambda lambda-expression
1089 :maybe-add-debug-catch t
1090 :debug-name
1091 (or name (name-lambdalike thing))))))
1092 ((lambda-with-lexenv)
1093 (ir1-convert-inline-lambda thing
1094 :source-name source-name
1095 :debug-name debug-name))))
1097 ;;;; defining global functions
1099 ;;; Convert FUN as a lambda in the null environment, but use the
1100 ;;; current compilation policy. Note that FUN may be a
1101 ;;; LAMBDA-WITH-LEXENV, so we may have to augment the environment to
1102 ;;; reflect the state at the definition site.
1103 (defun ir1-convert-inline-lambda (fun
1104 &key
1105 (source-name '.anonymous.)
1106 debug-name
1107 system-lambda)
1108 (when (and (not debug-name) (eq '.anonymous. source-name))
1109 (setf debug-name (name-lambdalike fun)))
1110 (destructuring-bind (decls macros symbol-macros &rest body)
1111 (if (eq (car fun) 'lambda-with-lexenv)
1112 (cdr fun)
1113 `(() () () . ,(cdr fun)))
1114 (let* ((*lexenv* (make-lexenv
1115 :default (process-decls decls nil nil
1116 :lexenv (make-null-lexenv))
1117 :vars (copy-list symbol-macros)
1118 :funs (mapcar (lambda (x)
1119 `(,(car x) .
1120 (macro . ,(coerce (cdr x) 'function))))
1121 macros)
1122 ;; Inherit MUFFLE-CONDITIONS from the call-site lexenv
1123 ;; rather than the definition-site lexenv, since it seems
1124 ;; like a much more common case.
1125 :handled-conditions (lexenv-handled-conditions *lexenv*)
1126 :policy (lexenv-policy *lexenv*)))
1127 (clambda (ir1-convert-lambda `(lambda ,@body)
1128 :source-name source-name
1129 :debug-name debug-name
1130 :system-lambda system-lambda)))
1131 (setf (functional-inline-expanded clambda) t)
1132 clambda)))
1134 ;;; Given a lambda-list, return a FUN-TYPE object representing the signature:
1135 ;;; return type is *, and each individual arguments type is T -- but we get
1136 ;;; the argument counts and keywords.
1137 (defun ftype-from-lambda-list (lambda-list)
1138 (multiple-value-bind (req opt restp rest-name keyp key-list allowp morep)
1139 (parse-lambda-list lambda-list)
1140 (declare (ignore rest-name))
1141 (flet ((t (list)
1142 (mapcar (constantly t) list)))
1143 (let ((reqs (t req))
1144 (opts (when opt (cons '&optional (t opt))))
1145 ;; When it comes to building a type, &REST means pretty much the
1146 ;; same thing as &MORE.
1147 (rest (when (or morep restp) (list '&rest t)))
1148 (keys (when keyp
1149 (cons '&key (mapcar (lambda (spec)
1150 (let ((key/var (if (consp spec)
1151 (car spec)
1152 spec)))
1153 (list (if (consp key/var)
1154 (car key/var)
1155 (keywordicate key/var))
1156 t)))
1157 key-list))))
1158 (allow (when allowp (list '&allow-other-keys))))
1159 (specifier-type `(function (,@reqs ,@opts ,@rest ,@keys ,@allow) *))))))
1161 ;;; Get a DEFINED-FUN object for a function we are about to define. If
1162 ;;; the function has been forward referenced, then substitute for the
1163 ;;; previous references.
1164 (defun get-defined-fun (name &optional (lambda-list nil lp))
1165 (proclaim-as-fun-name name)
1166 (when (boundp '*free-funs*)
1167 (let ((found (find-free-fun name "shouldn't happen! (defined-fun)")))
1168 (note-name-defined name :function)
1169 (cond ((not (defined-fun-p found))
1170 (aver (not (info :function :inlinep name)))
1171 (let* ((where-from (leaf-where-from found))
1172 (res (make-defined-fun
1173 :%source-name name
1174 :where-from (if (eq where-from :declared)
1175 :declared
1176 :defined-here)
1177 :type (if (eq :declared where-from)
1178 (leaf-type found)
1179 (if lp
1180 (ftype-from-lambda-list lambda-list)
1181 (specifier-type 'function))))))
1182 (substitute-leaf res found)
1183 (setf (gethash name *free-funs*) res)))
1184 ;; If *FREE-FUNS* has a previously converted definition
1185 ;; for this name, then blow it away and try again.
1186 ((defined-fun-functionals found)
1187 (remhash name *free-funs*)
1188 (get-defined-fun name lambda-list))
1189 (t found)))))
1191 ;;; Check a new global function definition for consistency with
1192 ;;; previous declaration or definition, and assert argument/result
1193 ;;; types if appropriate. This assertion is suppressed by the
1194 ;;; EXPLICIT-CHECK attribute, which is specified on functions that
1195 ;;; check their argument types as a consequence of type dispatching.
1196 ;;; This avoids redundant checks such as NUMBERP on the args to +, etc.
1197 (defun assert-new-definition (var fun)
1198 (let ((type (leaf-type var))
1199 (for-real (eq (leaf-where-from var) :declared))
1200 (info (info :function :info (leaf-source-name var))))
1201 (assert-definition-type
1202 fun type
1203 ;; KLUDGE: Common Lisp is such a dynamic language that in general
1204 ;; all we can do here is issue a STYLE-WARNING. It would be nice
1205 ;; to issue a full WARNING in the special case of type mismatches
1206 ;; within a compilation unit (as in section 3.2.2.3 of the spec)
1207 ;; but at least as of sbcl-0.6.11, we don't keep track of whether
1208 ;; the mismatched data came from the same compilation unit, so we
1209 ;; can't do that. -- WHN 2001-02-11
1210 :lossage-fun #'compiler-style-warn
1211 :unwinnage-fun (cond (info #'compiler-style-warn)
1212 (for-real #'compiler-notify)
1213 (t nil))
1214 :really-assert
1215 (and for-real
1216 (not (and info
1217 (ir1-attributep (fun-info-attributes info)
1218 explicit-check))))
1219 :where (if for-real
1220 "previous declaration"
1221 "previous definition"))))
1223 ;;; Used for global inline expansion. Earlier something like this was
1224 ;;; used by %DEFUN too. FIXME: And now it's probably worth rethinking
1225 ;;; whether this function is a good idea at all.
1226 (defun ir1-convert-inline-expansion (name expansion var inlinep info)
1227 ;; Unless a :INLINE function, we temporarily clobber the inline
1228 ;; expansion. This prevents recursive inline expansion of
1229 ;; opportunistic pseudo-inlines.
1230 (unless (eq inlinep :inline)
1231 (setf (defined-fun-inline-expansion var) nil))
1232 (let ((fun (ir1-convert-inline-lambda expansion
1233 :source-name name
1234 ;; prevent instrumentation of
1235 ;; known function expansions
1236 :system-lambda (and info t))))
1237 (setf (functional-inlinep fun) inlinep)
1238 (assert-new-definition var fun)
1239 (setf (defined-fun-inline-expansion var) expansion)
1240 ;; Associate VAR with the FUN -- and in case of an optional dispatch
1241 ;; with the various entry-points. This allows XREF to know where the
1242 ;; inline CLAMBDA comes from.
1243 (flet ((note-inlining (f)
1244 (typecase f
1245 (functional
1246 (setf (functional-inline-expanded f) var))
1247 (cons
1248 ;; Delayed entry-point.
1249 (if (car f)
1250 (setf (functional-inline-expanded (cdr f)) var)
1251 (let ((old-thunk (cdr f)))
1252 (setf (cdr f) (lambda ()
1253 (let ((g (funcall old-thunk)))
1254 (setf (functional-inline-expanded g) var)
1255 g)))))))))
1256 (note-inlining fun)
1257 (when (optional-dispatch-p fun)
1258 (note-inlining (optional-dispatch-main-entry fun))
1259 (note-inlining (optional-dispatch-more-entry fun))
1260 (mapc #'note-inlining (optional-dispatch-entry-points fun))))
1261 ;; substitute for any old references
1262 (unless (or (not *block-compile*)
1263 (and info
1264 (or (fun-info-transforms info)
1265 (fun-info-templates info)
1266 (fun-info-ir2-convert info))))
1267 (substitute-leaf fun var))
1268 fun))
1270 (defun %set-inline-expansion (name defined-fun inline-lambda)
1271 (cond (inline-lambda
1272 (setf (info :function :inline-expansion-designator name)
1273 inline-lambda)
1274 (when defined-fun
1275 (setf (defined-fun-inline-expansion defined-fun)
1276 inline-lambda)))
1278 (clear-info :function :inline-expansion-designator name))))
1280 ;;; the even-at-compile-time part of DEFUN
1282 ;;; The INLINE-LAMBDA is a LAMBDA-WITH-LEXENV, or NIL if there is no
1283 ;;; inline expansion.
1284 (defun %compiler-defun (name inline-lambda compile-toplevel)
1285 (let ((defined-fun nil)) ; will be set below if we're in the compiler
1286 (when compile-toplevel
1287 (with-single-package-locked-error
1288 (:symbol name "defining ~S as a function")
1289 (setf defined-fun
1290 (if inline-lambda
1291 (get-defined-fun name (fifth inline-lambda))
1292 (get-defined-fun name))))
1293 (when (boundp '*lexenv*)
1294 (aver (fasl-output-p *compile-object*))
1295 (if (member name *fun-names-in-this-file* :test #'equal)
1296 (warn 'duplicate-definition :name name)
1297 (push name *fun-names-in-this-file*)))
1298 (%set-inline-expansion name defined-fun inline-lambda))
1300 (become-defined-fun-name name)
1302 ;; old CMU CL comment:
1303 ;; If there is a type from a previous definition, blast it,
1304 ;; since it is obsolete.
1305 (when (and defined-fun (neq :declared (leaf-where-from defined-fun)))
1306 (setf (leaf-type defined-fun)
1307 ;; FIXME: If this is a block compilation thing, shouldn't
1308 ;; we be setting the type to the full derived type for the
1309 ;; definition, instead of this most general function type?
1310 (specifier-type 'function))))
1312 (values))
1314 ;; Similar to above, detect duplicate definitions within a file,
1315 ;; but the package lock check is unnecessary - it's handled elsewhere.
1317 ;; Additionally, this is a STYLE-WARNING, not a WARNING, because there is
1318 ;; meaningful behavior that can be ascribed to some redefinitions, e.g.
1319 ;; (defmacro foo () first-definition)
1320 ;; (defun f () (use-it (foo )))
1321 ;; (defmacro foo () other-definition)
1322 ;; will use the first definition when compiling F, but make the second available
1323 ;; in the loaded fasl. In this usage it would have made sense to wrap the
1324 ;; respective definitions with EVAL-WHEN for different situations,
1325 ;; but as long as the compile-time behavior is deterministic, it's just bad style
1326 ;; and not flat-out wrong, though there is indeed some waste in the fasl.
1328 ;; KIND is the globaldb KIND of this NAME
1329 (defun %compiler-defmacro (kind name compile-toplevel)
1330 (when compile-toplevel
1331 (let ((name-key `(,kind ,name)))
1332 (when (boundp '*lexenv*)
1333 (aver (fasl-output-p *compile-object*))
1334 (if (member name-key *fun-names-in-this-file* :test #'equal)
1335 (compiler-style-warn 'same-file-redefinition-warning :name name)
1336 (push name-key *fun-names-in-this-file*))))))
1339 ;;; Entry point utilities
1341 ;;; Return a function for the Nth entry point.
1342 (defun optional-dispatch-entry-point-fun (dispatcher n)
1343 (declare (type optional-dispatch dispatcher)
1344 (type unsigned-byte n))
1345 (let* ((env (getf (optional-dispatch-plist dispatcher) :ir1-environment))
1346 (*lexenv* (first env))
1347 (*current-path* (second env)))
1348 (force (nth n (optional-dispatch-entry-points dispatcher)))))