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