1 ;;;; bootstrapping fundamental machinery (e.g. DEFUN, DEFCONSTANT,
2 ;;;; DEFVAR) from special forms and primitive functions
4 ;;;; KLUDGE: The bootstrapping aspect of this is now obsolete. It was
5 ;;;; originally intended that this file file would be loaded into a
6 ;;;; Lisp image which had Common Lisp primitives defined, and DEFMACRO
7 ;;;; defined, and little else. Since then that approach has been
8 ;;;; dropped and this file has been modified somewhat to make it work
9 ;;;; more cleanly when used to predefine macros at
10 ;;;; build-the-cross-compiler time.
12 ;;;; This software is part of the SBCL system. See the README file for
13 ;;;; more information.
15 ;;;; This software is derived from the CMU CL system, which was
16 ;;;; written at Carnegie Mellon University and released into the
17 ;;;; public domain. The software is in the public domain and is
18 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
19 ;;;; files for more information.
21 (in-package "SB!IMPL")
26 (defmacro-mundanely in-package
(string-designator)
27 (let ((string (string string-designator
)))
28 `(eval-when (:compile-toplevel
:load-toplevel
:execute
)
29 (setq *package
* (find-undeleted-package-or-lose ,string
)))))
31 ;;;; MULTIPLE-VALUE-FOO
33 (defun list-of-symbols-p (x)
37 (defmacro-mundanely multiple-value-bind
(vars value-form
&body body
)
38 (if (list-of-symbols-p vars
)
39 ;; It's unclear why it would be important to special-case the LENGTH=1 case
40 ;; at this level, but the CMU CL code did it, so.. -- WHN 19990411
41 (if (= (length vars
) 1)
42 `(let ((,(car vars
) ,value-form
))
44 (let ((ignore (sb!xc
:gensym
)))
45 `(multiple-value-call #'(lambda (&optional
,@(mapcar #'list vars
)
47 (declare (ignore ,ignore
))
50 (error "Vars is not a list of symbols: ~S" vars
)))
52 (defmacro-mundanely multiple-value-setq
(vars value-form
)
53 (unless (list-of-symbols-p vars
)
54 (error "Vars is not a list of symbols: ~S" vars
))
55 ;; MULTIPLE-VALUE-SETQ is required to always return just the primary
56 ;; value of the value-from, even if there are no vars. (SETF VALUES)
57 ;; in turn is required to return as many values as there are
58 ;; value-places, hence this:
60 `(values (setf (values ,@vars
) ,value-form
))
61 `(values ,value-form
)))
63 (defmacro-mundanely multiple-value-list
(value-form)
64 `(multiple-value-call #'list
,value-form
))
66 ;;;; various conditional constructs
68 ;;; COND defined in terms of IF
69 (defmacro-mundanely cond
(&rest clauses
)
72 (let ((clause (first clauses
))
73 (more (rest clauses
)))
75 (error 'simple-type-error
76 :format-control
"COND clause is not a ~S: ~S"
77 :format-arguments
(list 'cons clause
)
80 (let ((test (first clause
))
81 (forms (rest clause
)))
83 (let ((n-result (gensym)))
84 `(let ((,n-result
,test
))
90 ;; THE to preserve non-toplevelness for FOO in
92 `(the t
(progn ,@forms
))
95 ,(when more
`(cond ,@more
))))))))))
97 (flet ((prognify (forms)
98 (cond ((singleton-p forms
) (car forms
))
100 (t `(progn ,@forms
)))))
101 (defmacro-mundanely when
(test &body forms
)
103 "If the first argument is true, the rest of the forms are
104 evaluated as a PROGN."
105 `(if ,test
,(prognify forms
)))
107 (defmacro-mundanely unless
(test &body forms
)
109 "If the first argument is not true, the rest of the forms are
110 evaluated as a PROGN."
111 `(if ,test nil
,(prognify forms
))))
113 (defmacro-mundanely and
(&rest forms
)
114 (cond ((endp forms
) t
)
116 ;; Preserve non-toplevelness of the form!
117 `(the t
,(first forms
)))
123 (defmacro-mundanely or
(&rest forms
)
124 (cond ((endp forms
) nil
)
126 ;; Preserve non-toplevelness of the form!
127 `(the t
,(first forms
)))
129 (let ((n-result (gensym)))
130 `(let ((,n-result
,(first forms
)))
133 (or ,@(rest forms
))))))))
135 ;;;; various sequencing constructs
137 (flet ((prog-expansion-from-let (varlist body-decls let
)
138 (multiple-value-bind (body decls
) (parse-body body-decls nil
)
142 (tagbody ,@body
))))))
143 (defmacro-mundanely prog
(varlist &body body-decls
)
144 (prog-expansion-from-let varlist body-decls
'let
))
145 (defmacro-mundanely prog
* (varlist &body body-decls
)
146 (prog-expansion-from-let varlist body-decls
'let
*)))
148 (defmacro-mundanely prog1
(result &body body
)
149 (let ((n-result (gensym)))
150 `(let ((,n-result
,result
))
154 (defmacro-mundanely prog2
(form1 result
&body body
)
155 `(prog1 (progn ,form1
,result
) ,@body
))
159 ;;; Should we save the inline expansion of the function named NAME?
160 (defun inline-fun-name-p (name)
162 ;; the normal reason for saving the inline expansion
163 (let ((inlinep (info :function
:inlinep name
)))
164 (member inlinep
'(:inline
:maybe-inline
)))
165 ;; another reason for saving the inline expansion: If the
166 ;; ANSI-recommended idiom
167 ;; (DECLAIM (INLINE FOO))
169 ;; (DECLAIM (NOTINLINE FOO))
170 ;; has been used, and then we later do another
172 ;; without a preceding
173 ;; (DECLAIM (INLINE FOO))
174 ;; what should we do with the old inline expansion when we see the
175 ;; new DEFUN? Overwriting it with the new definition seems like
176 ;; the only unsurprising choice.
177 (info :function
:inline-expansion-designator name
)))
179 (defmacro-mundanely defun
(&environment env name lambda-list
&body body
)
181 "Define a function at top level."
183 (unless (symbol-package (fun-name-block-name name
))
184 (warn "DEFUN of uninterned function name ~S (tricky for GENESIS)" name
))
185 (multiple-value-bind (forms decls doc
) (parse-body body t
)
186 (let* (;; stuff shared between LAMBDA and INLINE-LAMBDA and NAMED-LAMBDA
187 (lambda-guts `(,lambda-list
188 ,@(when doc
(list doc
))
190 (block ,(fun-name-block-name name
)
192 (lambda `(lambda ,@lambda-guts
))
193 (named-lambda `(named-lambda ,name
,@lambda-guts
))
195 (or (sb!kernel
::defstruct-generated-defn-p name lambda-list body
)
196 (when (inline-fun-name-p name
)
197 ;; we want to attempt to inline, so complain if we can't
198 (acond ((sb!c
:maybe-inline-syntactic-closure lambda env
)
202 #-sb-xc-host sb
!c
:maybe-compiler-notify
203 "lexical environment too hairy, can't inline DEFUN ~S"
207 (eval-when (:compile-toplevel
)
208 (sb!c
:%compiler-defun
',name
,inline-thing t
))
209 (%defun
',name
,named-lambda
(sb!c
:source-location
)
210 ,@(and inline-thing
(list inline-thing
)))
211 ;; This warning, if produced, comes after the DEFUN happens.
212 ;; When compiling, there's no real difference, but when interpreting,
213 ;; if there is a handler for style-warning that nonlocally exits,
214 ;; it's wrong to have skipped the DEFUN itself, since if there is no
215 ;; function, then the warning ought not to have been issued at all.
216 ,@(when (typep name
'(cons (eql setf
)))
217 `((eval-when (:compile-toplevel
:execute
)
218 (sb!c
::warn-if-setf-macro
',name
))
223 (defun %defun
(name def source-location
&optional inline-lambda
)
224 (declare (type function def
))
225 ;; should've been checked by DEFMACRO DEFUN
226 (aver (legal-fun-name-p name
))
227 (sb!c
:%compiler-defun
name inline-lambda nil
)
229 (warn 'redefinition-with-defun
230 :name name
:new-function def
:new-location source-location
))
231 (setf (sb!xc
:fdefinition name
) def
)
232 ;; %COMPILER-DEFUN doesn't do this except at compile-time, when it
233 ;; also checks package locks. By doing this here we let (SETF
234 ;; FDEFINITION) do the load-time package lock checking before
235 ;; we frob any existing inline expansions.
236 (sb!c
::%set-inline-expansion name nil inline-lambda
)
237 (sb!c
::note-name-defined name
:function
)
239 ;; During cold-init we don't touch the fdefinition.
240 (defun !%quietly-defun
(name inline-lambda
)
241 (sb!c
:%compiler-defun
name nil nil
) ; makes :WHERE-FROM = :DEFINED
242 (sb!c
::%set-inline-expansion name nil inline-lambda
)
243 ;; and no need to call NOTE-NAME-DEFINED. It would do nothing.
246 ;;;; DEFVAR and DEFPARAMETER
248 (defmacro-mundanely defvar
(var &optional
(val nil valp
) (doc nil docp
))
250 "Define a special variable at top level. Declare the variable
251 SPECIAL and, optionally, initialize it. If the variable already has a
252 value, the old value is not clobbered. The third argument is an optional
253 documentation string for the variable."
255 (eval-when (:compile-toplevel
)
256 (%compiler-defvar
',var
))
258 (sb!c
:source-location
)
260 `((unless (boundp ',var
) ,val
)))
264 (defmacro-mundanely defparameter
(var val
&optional
(doc nil docp
))
266 "Define a parameter that is not normally changed by the program,
267 but that may be changed without causing an error. Declare the
268 variable special and sets its value to VAL, overwriting any
269 previous value. The third argument is an optional documentation
270 string for the parameter."
272 (eval-when (:compile-toplevel
)
273 (%compiler-defvar
',var
))
274 (%defparameter
',var
,val
(sb!c
:source-location
)
278 (defun %compiler-defvar
(var)
279 (sb!xc
:proclaim
`(special ,var
)))
282 (defun %defvar
(var source-location
&optional
(val nil valp
) (doc nil docp
))
283 (%compiler-defvar var
)
288 (setf (fdocumentation var
'variable
) doc
))
289 (when source-location
290 (setf (info :source-location
:variable var
) source-location
))
294 (defun %defparameter
(var val source-location
&optional
(doc nil docp
))
295 (%compiler-defvar var
)
298 (setf (fdocumentation var
'variable
) doc
))
299 (when source-location
300 (setf (info :source-location
:variable var
) source-location
))
303 ;;;; iteration constructs
306 ((frob-do-body (varlist endlist decls-and-code bind step name block
)
307 ;; Check for illegal old-style DO.
308 (when (or (not (listp varlist
)) (atom endlist
))
309 (error "ill-formed ~S -- possibly illegal old style DO?" name
))
312 (mapcar (lambda (var)
313 (or (cond ((symbolp var
) var
)
315 (unless (symbolp (first var
))
316 (error "~S step variable is not a symbol: ~S"
320 (3 (steps (first var
) (third var
))
321 (list (first var
) (second var
))))))
322 (error "~S is an illegal form for a ~S varlist."
325 (multiple-value-bind (code decls
) (parse-body decls-and-code nil
)
326 (let ((label-1 (sb!xc
:gensym
)) (label-2 (sb!xc
:gensym
)))
336 (unless ,(first endlist
) (go ,label-1
))
337 (return-from ,block
(progn ,@(rest endlist
))))))))))))
339 ;; This is like DO, except it has no implicit NIL block.
340 (defmacro-mundanely do-anonymous
(varlist endlist
&rest body
)
341 (frob-do-body varlist endlist body
'let
'psetq
'do-anonymous
(sb!xc
:gensym
)))
343 (defmacro-mundanely do
(varlist endlist
&body body
)
345 "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
346 Iteration construct. Each Var is initialized in parallel to the value of the
347 specified Init form. On subsequent iterations, the Vars are assigned the
348 value of the Step form (if any) in parallel. The Test is evaluated before
349 each evaluation of the body Forms. When the Test is true, the Exit-Forms
350 are evaluated as a PROGN, with the result being the value of the DO. A block
351 named NIL is established around the entire expansion, allowing RETURN to be
352 used as an alternate exit mechanism."
353 (frob-do-body varlist endlist body
'let
'psetq
'do nil
))
355 (defmacro-mundanely do
* (varlist endlist
&body body
)
357 "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
358 Iteration construct. Each Var is initialized sequentially (like LET*) to the
359 value of the specified Init form. On subsequent iterations, the Vars are
360 sequentially assigned the value of the Step form (if any). The Test is
361 evaluated before each evaluation of the body Forms. When the Test is true,
362 the Exit-Forms are evaluated as a PROGN, with the result being the value
363 of the DO. A block named NIL is established around the entire expansion,
364 allowing RETURN to be used as an alternate exit mechanism."
365 (frob-do-body varlist endlist body
'let
* 'setq
'do
* nil
)))
367 ;;; DOTIMES and DOLIST could be defined more concisely using
368 ;;; destructuring macro lambda lists or DESTRUCTURING-BIND, but then
369 ;;; it'd be tricky to use them before those things were defined.
370 ;;; They're used enough times before destructuring mechanisms are
371 ;;; defined that it looks as though it's worth just implementing them
372 ;;; ASAP, at the cost of being unable to use the standard
373 ;;; destructuring mechanisms.
374 (defmacro-mundanely dotimes
((var count
&optional
(result nil
)) &body body
)
375 (cond ((integerp count
)
376 `(do ((,var
0 (1+ ,var
)))
377 ((>= ,var
,count
) ,result
)
378 (declare (type unsigned-byte
,var
))
381 (let ((c (gensym "COUNT")))
382 `(do ((,var
0 (1+ ,var
))
384 ((>= ,var
,c
) ,result
)
385 (declare (type unsigned-byte
,var
)
389 (defmacro-mundanely dolist
((var list
&optional
(result nil
)) &body body
&environment env
)
390 ;; We repeatedly bind the var instead of setting it so that we never
391 ;; have to give the var an arbitrary value such as NIL (which might
392 ;; conflict with a declaration). If there is a result form, we
393 ;; introduce a gratuitous binding of the variable to NIL without the
394 ;; declarations, then evaluate the result form in that
395 ;; environment. We spuriously reference the gratuitous variable,
396 ;; since we don't want to use IGNORABLE on what might be a special
398 (multiple-value-bind (forms decls
) (parse-body body nil
)
399 (let* ((n-list (gensym "N-LIST"))
400 (start (gensym "START")))
401 (multiple-value-bind (clist members clist-ok
)
402 (cond ((sb!xc
:constantp list env
)
403 (let ((value (constant-form-value list env
)))
404 (multiple-value-bind (all dot
) (list-members value
:max-length
20)
406 ;; Full warning is too much: the user may terminate the loop
407 ;; early enough. Contents are still right, though.
408 (style-warn "Dotted list ~S in DOLIST." value
))
410 (values value nil nil
)
411 (values value all t
)))))
412 ((and (consp list
) (eq 'list
(car list
))
413 (every (lambda (arg) (sb!xc
:constantp arg env
)) (cdr list
)))
414 (let ((values (mapcar (lambda (arg) (constant-form-value arg env
)) (cdr list
))))
415 (values values values t
)))
417 (values nil nil nil
)))
419 (let ((,n-list
,(if clist-ok
(list 'quote clist
) list
)))
422 (unless (endp ,n-list
)
423 (let ((,var
,(if clist-ok
424 `(truly-the (member ,@members
) (car ,n-list
))
427 (setq ,n-list
(cdr ,n-list
))
432 ;; Filter out TYPE declarations (VAR gets bound to NIL,
433 ;; and might have a conflicting type declaration) and
434 ;; IGNORE (VAR might be ignored in the loop body, but
435 ;; it's used in the result form).
436 ,@(filter-dolist-declarations decls
)
441 ;;;; conditions, handlers, restarts
443 ;;; KLUDGE: we PROCLAIM these special here so that we can use restart
444 ;;; macros in the compiler before the DEFVARs are compiled.
446 ;;; For an explanation of these data structures, see DEFVARs in
447 ;;; target-error.lisp.
448 (sb!xc
:proclaim
'(special *handler-clusters
* *restart-clusters
*))
450 ;;; Generated code need not check for unbound-marker in *HANDLER-CLUSTERS*
451 ;;; (resp *RESTART-). To elicit this we must poke at the info db.
452 ;;; SB!XC:PROCLAIM SPECIAL doesn't advise the host Lisp that *HANDLER-CLUSTERS*
453 ;;; is special and so it rightfully complains about a SETQ of the variable.
454 ;;; But I must SETQ if proclaming ALWAYS-BOUND because the xc asks the host
455 ;;; whether it's currently bound.
456 ;;; But the DEFVARs are in target-error. So it's one hack or another.
457 (setf (info :variable
:always-bound
'*handler-clusters
*)
458 #+sb-xc
:always-bound
#-sb-xc
:eventually
)
459 (setf (info :variable
:always-bound
'*restart-clusters
*)
460 #+sb-xc
:always-bound
#-sb-xc
:eventually
)
462 (defmacro-mundanely with-condition-restarts
463 (condition-form restarts-form
&body body
)
465 "Evaluates the BODY in a dynamic environment where the restarts in the list
466 RESTARTS-FORM are associated with the condition returned by CONDITION-FORM.
467 This allows FIND-RESTART, etc., to recognize restarts that are not related
468 to the error currently being debugged. See also RESTART-CASE."
469 (once-only ((condition-form condition-form
)
470 (restarts restarts-form
))
471 (with-unique-names (restart)
472 ;; FIXME: check the need for interrupt-safety.
475 (dolist (,restart
,restarts
)
476 (push ,condition-form
477 (restart-associated-conditions ,restart
)))
479 (dolist (,restart
,restarts
)
480 (pop (restart-associated-conditions ,restart
)))))))
482 (defmacro-mundanely restart-bind
(bindings &body forms
)
484 "(RESTART-BIND ({(case-name function {keyword value}*)}*) forms)
485 Executes forms in a dynamic context where the given bindings are in
486 effect. Users probably want to use RESTART-CASE. A case-name of NIL
487 indicates an anonymous restart. When bindings contain the same
488 restart name, FIND-RESTART will find the first such binding."
489 (flet ((parse-binding (binding)
490 (unless (>= (length binding
) 2)
491 (error "ill-formed restart binding: ~S" binding
))
492 (destructuring-bind (name function
493 &key interactive-function
497 (unless (or name report-function
)
498 (warn "Unnamed restart does not have a report function: ~
500 `(make-restart ',name
,function
501 ,@(and (or report-function
505 ,@(and (or interactive-function
507 `(,interactive-function
))
509 `(,test-function
))))))
510 `(let ((*restart-clusters
*
511 (cons (list ,@(mapcar #'parse-binding bindings
))
512 *restart-clusters
*)))
513 (declare (truly-dynamic-extent *restart-clusters
*))
516 ;;; Wrap the RESTART-CASE expression in a WITH-CONDITION-RESTARTS if
517 ;;; appropriate. Gross, but it's what the book seems to say...
518 (defun munge-restart-case-expression (expression env
)
519 (let ((exp (%macroexpand expression env
)))
521 (let* ((name (car exp
))
522 (args (if (eq name
'cerror
) (cddr exp
) (cdr exp
))))
523 (if (member name
'(signal error cerror warn
))
524 (once-only ((n-cond `(coerce-to-condition
528 (warn 'simple-warning
)
529 (signal 'simple-condition
)
532 `(with-condition-restarts
534 (car *restart-clusters
*)
535 ,(if (eq name
'cerror
)
536 `(cerror ,(second exp
) ,n-cond
)
541 (defmacro-mundanely restart-case
(expression &body clauses
&environment env
)
543 "(RESTART-CASE form {(case-name arg-list {keyword value}* body)}*)
544 The form is evaluated in a dynamic context where the clauses have
545 special meanings as points to which control may be transferred (see
546 INVOKE-RESTART). When clauses contain the same case-name,
547 FIND-RESTART will find the first such clause. If form is a call to
548 SIGNAL, ERROR, CERROR or WARN (or macroexpands into such) then the
549 signalled condition will be associated with the new restarts."
550 ;; PARSE-CLAUSE (which uses PARSE-KEYWORDS-AND-BODY) is used to
551 ;; parse all clauses into lists of the form
553 ;; (NAME TAG KEYWORDS LAMBDA-LIST BODY)
555 ;; where KEYWORDS are suitable keywords for use in HANDLER-BIND
556 ;; bindings. These lists are then passed to
557 ;; * MAKE-BINDING which generates bindings for the respective NAME
559 ;; * MAKE-APPLY-AND-RETURN which generates TAGBODY entries executing
560 ;; the respective BODY.
561 (let ((block-tag (sb!xc
:gensym
"BLOCK"))
563 (labels ((parse-keywords-and-body (keywords-and-body)
564 (do ((form keywords-and-body
(cddr form
))
566 (destructuring-bind (&optional key
(arg nil argp
) &rest rest
)
568 (declare (ignore rest
))
572 ((and (eq key
:report
) argp
)
573 (list :report-function
576 (write-string ,arg stream
))
578 ((and (eq key
:interactive
) argp
)
579 (list :interactive-function
`#',arg
))
580 ((and (eq key
:test
) argp
)
581 (list :test-function
`#',arg
))
583 (return (values result form
))))
585 (parse-clause (clause)
586 (unless (and (listp clause
) (>= (length clause
) 2)
587 (listp (second clause
)))
588 (error "ill-formed ~S clause, no lambda-list:~% ~S"
589 'restart-case clause
))
590 (destructuring-bind (name lambda-list
&body body
) clause
591 (multiple-value-bind (keywords body
)
592 (parse-keywords-and-body body
)
593 (list name
(sb!xc
:gensym
"TAG") keywords lambda-list body
))))
594 (make-binding (clause-data)
595 (destructuring-bind (name tag keywords lambda-list body
) clause-data
596 (declare (ignore body
))
598 (lambda ,(cond ((null lambda-list
)
600 ((and (null (cdr lambda-list
))
601 (not (member (car lambda-list
)
602 '(&optional
&key
&aux
))))
606 ,@(when lambda-list
`((setq ,temp-var temp
)))
607 (locally (declare (optimize (safety 0)))
610 (make-apply-and-return (clause-data)
611 (destructuring-bind (name tag keywords lambda-list body
) clause-data
612 (declare (ignore name keywords
))
613 `(,tag
(return-from ,block-tag
614 ,(cond ((null lambda-list
)
616 ((and (null (cdr lambda-list
))
617 (not (member (car lambda-list
)
618 '(&optional
&key
&aux
))))
619 `(funcall (lambda ,lambda-list
,@body
) ,temp-var
))
621 `(apply (lambda ,lambda-list
,@body
) ,temp-var
))))))))
622 (let ((clauses-data (mapcar #'parse-clause clauses
)))
624 (let ((,temp-var nil
))
625 (declare (ignorable ,temp-var
))
628 ,(mapcar #'make-binding clauses-data
)
629 (return-from ,block-tag
630 ,(munge-restart-case-expression expression env
)))
631 ,@(mapcan #'make-apply-and-return clauses-data
))))))))
633 (defmacro-mundanely with-simple-restart
((restart-name format-string
634 &rest format-arguments
)
637 "(WITH-SIMPLE-RESTART (restart-name format-string format-arguments)
639 If restart-name is not invoked, then all values returned by forms are
640 returned. If control is transferred to this restart, it immediately
641 returns the values NIL and T."
642 (let ((stream (sb!xc
:gensym
"STREAM")))
644 ;; If there's just one body form, then don't use PROGN. This allows
645 ;; RESTART-CASE to "see" calls to ERROR, etc.
646 ,(if (= (length forms
) 1) (car forms
) `(progn ,@forms
))
648 :report
(lambda (,stream
)
649 (declare (type stream
,stream
))
650 (format ,stream
,format-string
,@format-arguments
))
653 (defmacro-mundanely %handler-bind
(bindings form
&environment env
)
655 (return-from %handler-bind form
))
656 ;; As an optimization, this looks at the handler parts of BINDINGS
657 ;; and turns handlers of the forms (lambda ...) and (function
658 ;; (lambda ...)) into local, dynamic-extent functions.
660 ;; Type specifiers in BINDINGS which name classoids are parsed
661 ;; into the classoid, otherwise are translated local TYPEP wrappers.
663 ;; As a further optimization, it is possible to eliminate some runtime
664 ;; consing (which is a speed win if not a space win, since it's dx already)
665 ;; in special cases such as (HANDLER-BIND ((WARNING #'MUFFLE-WARNING)) ...).
666 ;; If all bindings are optimizable, then the runtime cost of making them
667 ;; is one dx cons cell for the whole cluster.
668 ;; Otherwise it takes 1+2N cons cells where N is the number of bindings.
670 (collect ((local-functions) (cluster-entries) (dummy-forms))
671 (flet ((const-cons (test handler
)
672 ;; If possible, render HANDLER as a load-time constant so that
673 ;; consing the test and handler is also load-time constant.
674 (let ((name (when (typep handler
675 '(cons (member function quote
)
678 (cond ((or (not name
)
679 (assq name
(local-functions))
680 (and (eq (car handler
) 'function
)
681 (sb!c
::fun-locally-defined-p name env
)))
682 `(cons ,(case (car test
)
683 ((named-lambda function
) test
)
684 (t `(load-time-value ,test t
)))
685 ,(if (typep handler
'(cons (eql function
)))
687 ;; Regardless of lexical policy, never allow
688 ;; a non-callable into handler-clusters.
690 (declare (optimize (safety 3)))
692 ((info :function
:info name
) ; known
693 ;; This takes care of CONTINUE,ABORT,MUFFLE-WARNING.
694 ;; #' will be evaluated in the null environment.
695 `(load-time-value (cons ,test
#',name
) t
))
697 ;; For each handler specified as #'F we must verify
698 ;; that F is fboundp upon entering the binding scope.
699 ;; Referencing #'F is enough to ensure a warning if the
700 ;; function isn't defined at compile-time, but the
701 ;; compiler considers it elidable unless something forces
702 ;; an apparent use of the form at runtime,
703 ;; so instead use SAFE-FDEFN-FUN on the fdefn.
704 (when (eq (car handler
) 'function
)
705 (dummy-forms `(sb!c
:safe-fdefn-fun
707 (find-or-create-fdefn ',name
) t
))))
708 ;; Resolve to an fdefn at load-time.
710 (cons ,test
(find-or-create-fdefn ',name
))
714 ;; If the resultant list is (LIST (L-T-V ...) (L-T-V ...) ...)
715 ;; then pull the L-T-V outside.
716 (if (every (lambda (x) (typep x
'(cons (eql load-time-value
))))
718 `(load-time-value (list ,@(mapcar #'second items
)) t
)
721 (dolist (binding bindings
)
722 (unless (proper-list-of-length-p binding
2)
723 (error "ill-formed handler binding: ~S" binding
))
724 (destructuring-bind (type handler
) binding
725 (setq type
(typexpand type env
))
726 ;; Simplify a singleton AND or OR.
727 (when (typep type
'(cons (member and or
) (cons t null
)))
728 (setf type
(second type
)))
731 ;; Compute the test expression
732 (cond ((member type
'(t condition
))
733 ;; Every signal is necesarily a CONDITION, so whether you
734 ;; wrote T or CONDITION, this is always an eligible handler.
736 ((typep type
'(cons (eql satisfies
) (cons t null
)))
737 ;; (SATISFIES F) => #'F but never a local definition of F.
738 ;; The predicate is used only if needed - it's not an error
739 ;; if not fboundp (though dangerously stupid) - so just
740 ;; reference #'F for the compiler to see the use of the name.
741 ;; But (KLUDGE): since the ref is to force a compile-time
742 ;; effect, the interpreter should not see that form,
743 ;; because there is no way for it to perform an unsafe ref,
744 ;; (and it wouldn't signal a style-warning anyway),
745 ;; and so it would actually fail immediately if predicate
747 (let ((name (second type
)))
748 (when (typep env
'lexenv
)
749 (dummy-forms `#',name
))
750 `(find-or-create-fdefn ',name
)))
752 (condition-classoid-p (find-classoid type nil
)))
753 ;; It's debatable whether we need to go through a
754 ;; classoid-cell instead of just using load-time-value
755 ;; on FIND-CLASS, but the extra indirection is
756 ;; safer, and no slower than what TYPEP does.
757 `(find-classoid-cell ',type
:create t
))
758 (t ; No runtime consing here- this is not a closure.
759 `(named-lambda (%handler-bind
,type
) (c)
760 (declare (optimize (sb!c
::verify-arg-count
0)))
762 ;; Compute the handler expression
763 (let ((lexpr (typecase handler
764 ((cons (eql lambda
)) handler
)
765 ((cons (eql function
)
766 (cons (cons (eql lambda
)) null
))
769 (let ((name (let ((sb!xc
:*gensym-counter
*
770 (length (cluster-entries))))
771 (sb!xc
:gensym
"H"))))
772 (local-functions `(,name
,@(rest lexpr
)))
776 `(dx-flet ,(local-functions)
778 (dx-let ((*handler-clusters
*
779 (cons ,(const-list (cluster-entries))
780 *handler-clusters
*)))
783 (defmacro-mundanely handler-bind
(bindings &body forms
)
785 "(HANDLER-BIND ( {(type handler)}* ) body)
787 Executes body in a dynamic context where the given handler bindings are in
788 effect. Each handler must take the condition being signalled as an argument.
789 The bindings are searched first to last in the event of a signalled
791 ;; Bindings which meet specific criteria can be established with
792 ;; slightly less runtime overhead than in general.
793 ;; To allow the optimization, TYPE must be either be (SATISFIES P)
794 ;; or a symbol naming a condition class at compile time,
795 ;; and HANDLER must be a global function specified as either 'F or #'F.
796 `(%handler-bind
,bindings
797 #!-x86
(progn ,@forms
)
798 ;; Need to catch FP errors here!
799 #!+x86
(multiple-value-prog1 (progn ,@forms
) (float-wait))))
801 (defmacro-mundanely handler-case
(form &rest cases
)
803 "(HANDLER-CASE form { (type ([var]) body) }* )
805 Execute FORM in a context with handlers established for the condition types. A
806 peculiar property allows type to be :NO-ERROR. If such a clause occurs, and
807 form returns normally, all its values are passed to this clause as if by
808 MULTIPLE-VALUE-CALL. The :NO-ERROR clause accepts more than one var
810 (let ((no-error-clause (assoc ':no-error cases
)))
812 (let ((normal-return (make-symbol "normal-return"))
813 (error-return (make-symbol "error-return")))
814 `(block ,error-return
815 (multiple-value-call (lambda ,@(cdr no-error-clause
))
816 (block ,normal-return
817 (return-from ,error-return
818 (handler-case (return-from ,normal-return
,form
)
819 ,@(remove no-error-clause cases
)))))))
820 (let* ((local-funs nil
)
822 (mapcar (lambda (case)
823 (with-unique-names (tag fun
)
824 (destructuring-bind (type ll
&body body
) case
825 (push `(,fun
,ll
,@body
) local-funs
)
826 (list tag type ll fun
))))
828 (with-unique-names (block cell form-fun
)
829 `(dx-flet ((,form-fun
()
831 ;; Need to catch FP errors here!
832 #!+x86
(multiple-value-prog1 ,form
(float-wait)))
833 ,@(reverse local-funs
))
834 (declare (optimize (sb!c
::check-tag-existence
0)))
836 ;; KLUDGE: We use a dx CONS cell instead of just assigning to
837 ;; the variable directly, so that we can stack allocate
838 ;; robustly: dx value cells don't work quite right, and it is
839 ;; possible to construct user code that should loop
840 ;; indefinitely, but instead eats up some stack each time
842 (dx-let ((,cell
(cons :condition nil
)))
843 (declare (ignorable ,cell
))
846 ,(mapcar (lambda (annotated-case)
847 (destructuring-bind (tag type ll fun-name
) annotated-case
848 (declare (ignore fun-name
))
852 `(setf (cdr ,cell
) temp
)
853 '(declare (ignore temp
)))
856 (return-from ,block
(,form-fun
)))
858 (lambda (annotated-case)
859 (destructuring-bind (tag type ll fun-name
) annotated-case
860 (declare (ignore type
))
864 `(,fun-name
(cdr ,cell
))
866 annotated-cases
))))))))))
870 (defmacro-mundanely return
(&optional
(value nil
))
871 `(return-from nil
,value
))
873 (defmacro-mundanely lambda
(&whole whole args
&body body
)
874 (declare (ignore args body
))
877 (defmacro-mundanely named-lambda
(&whole whole name args
&body body
)
878 (declare (ignore name args body
))
881 (defmacro-mundanely lambda-with-lexenv
(&whole whole
882 declarations macros symbol-macros
884 (declare (ignore declarations macros symbol-macros body
))