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
))
89 ;; THE to preserve non-toplevelness for FOO in
91 ;; FIXME: this hides all other possible stylistic issues,
92 ;; not the least of which is a code deletion note,
93 ;; if there are forms following the one whose head is T.
94 ;; This is not usually the SBCL preferred way.
95 `(the t
(progn ,@forms
))
98 ,(when more
`(cond ,@more
))))))))))
100 (defmacro-mundanely when
(test &body forms
)
102 "If the first argument is true, the rest of the forms are
103 evaluated as a PROGN."
104 `(if ,test
(progn ,@forms
) nil
))
106 (defmacro-mundanely unless
(test &body forms
)
108 "If the first argument is not true, the rest of the forms are
109 evaluated as a PROGN."
110 `(if ,test nil
(progn ,@forms
)))
112 (defmacro-mundanely and
(&rest forms
)
113 (cond ((endp forms
) t
)
115 ;; Preserve non-toplevelness of the form!
116 `(the t
,(first forms
)))
122 (defmacro-mundanely or
(&rest forms
)
123 (cond ((endp forms
) nil
)
125 ;; Preserve non-toplevelness of the form!
126 `(the t
,(first forms
)))
128 (let ((n-result (gensym)))
129 `(let ((,n-result
,(first forms
)))
132 (or ,@(rest forms
))))))))
134 ;;;; various sequencing constructs
136 (flet ((prog-expansion-from-let (varlist body-decls let
)
137 (multiple-value-bind (body decls
)
138 (parse-body body-decls
:doc-string-allowed 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 args
&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
)
186 (let* (;; stuff shared between LAMBDA and INLINE-LAMBDA and NAMED-LAMBDA
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 (when (inline-fun-name-p name
)
196 ;; we want to attempt to inline, so complain if we can't
197 (or (sb!c
:maybe-inline-syntactic-closure lambda env
)
200 #-sb-xc-host sb
!c
:maybe-compiler-notify
201 "lexical environment too hairy, can't inline DEFUN ~S"
205 (eval-when (:compile-toplevel
)
206 (sb!c
:%compiler-defun
',name
',inline-lambda t
))
207 ,@(when (typep name
'(cons (eql setf
)))
208 `((eval-when (:compile-toplevel
:execute
)
209 (sb!c
::warn-if-setf-macro
',name
))))
210 (%defun
',name
,named-lambda
(sb!c
:source-location
)
212 `(',inline-lambda
)))))))
215 (progn (defun %defun
(name def source-location
&optional inline-lambda
)
216 (declare (type function def
))
217 ;; should've been checked by DEFMACRO DEFUN
218 (aver (legal-fun-name-p name
))
219 (sb!c
:%compiler-defun
name inline-lambda nil
)
221 (warn 'redefinition-with-defun
222 :name name
:new-function def
:new-location source-location
))
223 (setf (sb!xc
:fdefinition name
) def
)
224 ;; %COMPILER-DEFUN doesn't do this except at compile-time, when it
225 ;; also checks package locks. By doing this here we let (SETF
226 ;; FDEFINITION) do the load-time package lock checking before
227 ;; we frob any existing inline expansions.
228 (sb!c
::%set-inline-expansion name nil inline-lambda
)
229 (sb!c
::note-name-defined name
:function
)
231 ;; During cold-init we don't touch the fdefinition.
232 (defun !%quietly-defun
(name inline-lambda
)
233 (sb!c
:%compiler-defun
name nil nil
) ; makes :WHERE-FROM = :DEFINED
234 (sb!c
::%set-inline-expansion name nil inline-lambda
)
235 ;; and no need to call NOTE-NAME-DEFINED. It would do nothing.
238 ;;;; DEFVAR and DEFPARAMETER
240 (defmacro-mundanely defvar
(var &optional
(val nil valp
) (doc nil docp
))
242 "Define a special variable at top level. Declare the variable
243 SPECIAL and, optionally, initialize it. If the variable already has a
244 value, the old value is not clobbered. The third argument is an optional
245 documentation string for the variable."
247 (eval-when (:compile-toplevel
)
248 (%compiler-defvar
',var
))
250 (sb!c
:source-location
)
252 `((unless (boundp ',var
) ,val
)))
256 (defmacro-mundanely defparameter
(var val
&optional
(doc nil docp
))
258 "Define a parameter that is not normally changed by the program,
259 but that may be changed without causing an error. Declare the
260 variable special and sets its value to VAL, overwriting any
261 previous value. The third argument is an optional documentation
262 string for the parameter."
264 (eval-when (:compile-toplevel
)
265 (%compiler-defvar
',var
))
266 (%defparameter
',var
,val
(sb!c
:source-location
)
270 (defun %compiler-defvar
(var)
271 (sb!xc
:proclaim
`(special ,var
)))
274 (defun %defvar
(var source-location
&optional
(val nil valp
) (doc nil docp
))
275 (%compiler-defvar var
)
280 (setf (fdocumentation var
'variable
) doc
))
281 (sb!c
:with-source-location
(source-location)
282 (setf (info :source-location
:variable var
) source-location
))
286 (defun %defparameter
(var val source-location
&optional
(doc nil docp
))
287 (%compiler-defvar var
)
290 (setf (fdocumentation var
'variable
) doc
))
291 (sb!c
:with-source-location
(source-location)
292 (setf (info :source-location
:variable var
) source-location
))
295 ;;;; iteration constructs
297 ;;; (These macros are defined in terms of a function FROB-DO-BODY which
298 ;;; is also used by SB!INT:DO-ANONYMOUS. Since these macros should not
299 ;;; be loaded on the cross-compilation host, but SB!INT:DO-ANONYMOUS
300 ;;; and FROB-DO-BODY should be, these macros can't conveniently be in
301 ;;; the same file as FROB-DO-BODY.)
302 (defmacro-mundanely do
(varlist endlist
&body body
)
304 "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
305 Iteration construct. Each Var is initialized in parallel to the value of the
306 specified Init form. On subsequent iterations, the Vars are assigned the
307 value of the Step form (if any) in parallel. The Test is evaluated before
308 each evaluation of the body Forms. When the Test is true, the Exit-Forms
309 are evaluated as a PROGN, with the result being the value of the DO. A block
310 named NIL is established around the entire expansion, allowing RETURN to be
311 used as an alternate exit mechanism."
312 (frob-do-body varlist endlist body
'let
'psetq
'do nil
))
313 (defmacro-mundanely do
* (varlist endlist
&body body
)
315 "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
316 Iteration construct. Each Var is initialized sequentially (like LET*) to the
317 value of the specified Init form. On subsequent iterations, the Vars are
318 sequentially assigned the value of the Step form (if any). The Test is
319 evaluated before each evaluation of the body Forms. When the Test is true,
320 the Exit-Forms are evaluated as a PROGN, with the result being the value
321 of the DO. A block named NIL is established around the entire expansion,
322 allowing RETURN to be used as an alternate exit mechanism."
323 (frob-do-body varlist endlist body
'let
* 'setq
'do
* nil
))
325 ;;; DOTIMES and DOLIST could be defined more concisely using
326 ;;; destructuring macro lambda lists or DESTRUCTURING-BIND, but then
327 ;;; it'd be tricky to use them before those things were defined.
328 ;;; They're used enough times before destructuring mechanisms are
329 ;;; defined that it looks as though it's worth just implementing them
330 ;;; ASAP, at the cost of being unable to use the standard
331 ;;; destructuring mechanisms.
332 (defmacro-mundanely dotimes
((var count
&optional
(result nil
)) &body body
)
333 (cond ((integerp count
)
334 `(do ((,var
0 (1+ ,var
)))
335 ((>= ,var
,count
) ,result
)
336 (declare (type unsigned-byte
,var
))
339 (let ((c (gensym "COUNT")))
340 `(do ((,var
0 (1+ ,var
))
342 ((>= ,var
,c
) ,result
)
343 (declare (type unsigned-byte
,var
)
347 (defmacro-mundanely dolist
((var list
&optional
(result nil
)) &body body
&environment env
)
348 ;; We repeatedly bind the var instead of setting it so that we never
349 ;; have to give the var an arbitrary value such as NIL (which might
350 ;; conflict with a declaration). If there is a result form, we
351 ;; introduce a gratuitous binding of the variable to NIL without the
352 ;; declarations, then evaluate the result form in that
353 ;; environment. We spuriously reference the gratuitous variable,
354 ;; since we don't want to use IGNORABLE on what might be a special
356 (multiple-value-bind (forms decls
) (parse-body body
:doc-string-allowed nil
)
357 (let* ((n-list (gensym "N-LIST"))
358 (start (gensym "START")))
359 (multiple-value-bind (clist members clist-ok
)
360 (cond ((sb!xc
:constantp list env
)
361 (let ((value (constant-form-value list env
)))
362 (multiple-value-bind (all dot
) (list-members value
:max-length
20)
364 ;; Full warning is too much: the user may terminate the loop
365 ;; early enough. Contents are still right, though.
366 (style-warn "Dotted list ~S in DOLIST." value
))
368 (values value nil nil
)
369 (values value all t
)))))
370 ((and (consp list
) (eq 'list
(car list
))
371 (every (lambda (arg) (sb!xc
:constantp arg env
)) (cdr list
)))
372 (let ((values (mapcar (lambda (arg) (constant-form-value arg env
)) (cdr list
))))
373 (values values values t
)))
375 (values nil nil nil
)))
377 (let ((,n-list
,(if clist-ok
(list 'quote clist
) list
)))
380 (unless (endp ,n-list
)
381 (let ((,var
,(if clist-ok
382 `(truly-the (member ,@members
) (car ,n-list
))
385 (setq ,n-list
(cdr ,n-list
))
390 ;; Filter out TYPE declarations (VAR gets bound to NIL,
391 ;; and might have a conflicting type declaration) and
392 ;; IGNORE (VAR might be ignored in the loop body, but
393 ;; it's used in the result form).
394 ,@(filter-dolist-declarations decls
)
399 ;;;; conditions, handlers, restarts
401 ;;; KLUDGE: we PROCLAIM these special here so that we can use restart
402 ;;; macros in the compiler before the DEFVARs are compiled.
404 ;;; For an explanation of these data structures, see DEFVARs in
405 ;;; target-error.lisp.
406 (sb!xc
:proclaim
'(special *handler-clusters
* *restart-clusters
*))
408 ;;; Generated code need not check for unbound-marker in *HANDLER-CLUSTERS*
409 ;;; (resp *RESTART-). To elicit this we must poke at the info db.
410 ;;; SB!XC:PROCLAIM SPECIAL doesn't advise the host Lisp that *HANDLER-CLUSTERS*
411 ;;; is special and so it rightfully complains about a SETQ of the variable.
412 ;;; But I must SETQ if proclaming ALWAYS-BOUND because the xc asks the host
413 ;;; whether it's currently bound.
414 ;;; But the DEFVARs are in target-error. So it's one hack or another.
415 (setf (info :variable
:always-bound
'*handler-clusters
*)
416 #+sb-xc
:always-bound
#-sb-xc
:eventually
)
417 (setf (info :variable
:always-bound
'*restart-clusters
*)
418 #+sb-xc
:always-bound
#-sb-xc
:eventually
)
420 (defmacro-mundanely with-condition-restarts
421 (condition-form restarts-form
&body body
)
423 "Evaluates the BODY in a dynamic environment where the restarts in the list
424 RESTARTS-FORM are associated with the condition returned by CONDITION-FORM.
425 This allows FIND-RESTART, etc., to recognize restarts that are not related
426 to the error currently being debugged. See also RESTART-CASE."
427 (once-only ((restarts restarts-form
))
428 (with-unique-names (restart)
429 ;; FIXME: check the need for interrupt-safety.
432 (dolist (,restart
,restarts
)
433 (push ,condition-form
434 (restart-associated-conditions ,restart
)))
436 (dolist (,restart
,restarts
)
437 (pop (restart-associated-conditions ,restart
)))))))
439 (defmacro-mundanely restart-bind
(bindings &body forms
)
441 "(RESTART-BIND ({(case-name function {keyword value}*)}*) forms)
442 Executes forms in a dynamic context where the given bindings are in
443 effect. Users probably want to use RESTART-CASE. A case-name of NIL
444 indicates an anonymous restart. When bindings contain the same
445 restart name, FIND-RESTART will find the first such binding."
446 (flet ((parse-binding (binding)
447 (unless (>= (length binding
) 2)
448 (error "ill-formed restart binding: ~S" binding
))
449 (destructuring-bind (name function
450 &key interactive-function
454 (unless (or name report-function
)
455 (warn "Unnamed restart does not have a report function: ~
457 `(make-restart ',name
,function
458 ,@(and (or report-function
462 ,@(and (or interactive-function
464 `(,interactive-function
))
466 `(,test-function
))))))
467 `(let ((*restart-clusters
*
468 (cons (list ,@(mapcar #'parse-binding bindings
))
469 *restart-clusters
*)))
470 (declare (truly-dynamic-extent *restart-clusters
*))
473 ;;; Wrap the RESTART-CASE expression in a WITH-CONDITION-RESTARTS if
474 ;;; appropriate. Gross, but it's what the book seems to say...
475 (defun munge-restart-case-expression (expression env
)
476 (let ((exp (%macroexpand expression env
)))
478 (let* ((name (car exp
))
479 (args (if (eq name
'cerror
) (cddr exp
) (cdr exp
))))
480 (if (member name
'(signal error cerror warn
))
481 (once-only ((n-cond `(coerce-to-condition
485 (warn 'simple-warning
)
486 (signal 'simple-condition
)
489 `(with-condition-restarts
491 (car *restart-clusters
*)
492 ,(if (eq name
'cerror
)
493 `(cerror ,(second exp
) ,n-cond
)
498 (defmacro-mundanely restart-case
(expression &body clauses
&environment env
)
500 "(RESTART-CASE form {(case-name arg-list {keyword value}* body)}*)
501 The form is evaluated in a dynamic context where the clauses have
502 special meanings as points to which control may be transferred (see
503 INVOKE-RESTART). When clauses contain the same case-name,
504 FIND-RESTART will find the first such clause. If form is a call to
505 SIGNAL, ERROR, CERROR or WARN (or macroexpands into such) then the
506 signalled condition will be associated with the new restarts."
507 ;; PARSE-CLAUSE (which uses PARSE-KEYWORDS-AND-BODY) is used to
508 ;; parse all clauses into lists of the form
510 ;; (NAME TAG KEYWORDS LAMBDA-LIST BODY)
512 ;; where KEYWORDS are suitable keywords for use in HANDLER-BIND
513 ;; bindings. These lists are then passed to
514 ;; * MAKE-BINDING which generates bindings for the respective NAME
516 ;; * MAKE-APPLY-AND-RETURN which generates TAGBODY entries executing
517 ;; the respective BODY.
518 (let ((block-tag (sb!xc
:gensym
"BLOCK"))
520 (labels ((parse-keywords-and-body (keywords-and-body)
521 (do ((form keywords-and-body
(cddr form
))
523 (destructuring-bind (&optional key
(arg nil argp
) &rest rest
)
525 (declare (ignore rest
))
529 ((and (eq key
:report
) argp
)
530 (list :report-function
533 (write-string ,arg stream
))
535 ((and (eq key
:interactive
) argp
)
536 (list :interactive-function
`#',arg
))
537 ((and (eq key
:test
) argp
)
538 (list :test-function
`#',arg
))
540 (return (values result form
))))
542 (parse-clause (clause)
543 (unless (and (listp clause
) (>= (length clause
) 2)
544 (listp (second clause
)))
545 (error "ill-formed ~S clause, no lambda-list:~% ~S"
546 'restart-case clause
))
547 (destructuring-bind (name lambda-list
&body body
) clause
548 (multiple-value-bind (keywords body
)
549 (parse-keywords-and-body body
)
550 (list name
(sb!xc
:gensym
"TAG") keywords lambda-list body
))))
551 (make-binding (clause-data)
552 (destructuring-bind (name tag keywords lambda-list body
) clause-data
553 (declare (ignore body
))
555 (lambda ,(cond ((null lambda-list
)
557 ((and (null (cdr lambda-list
))
558 (not (member (car lambda-list
)
559 '(&optional
&key
&aux
))))
563 ,@(when lambda-list
`((setq ,temp-var temp
)))
564 (locally (declare (optimize (safety 0)))
567 (make-apply-and-return (clause-data)
568 (destructuring-bind (name tag keywords lambda-list body
) clause-data
569 (declare (ignore name keywords
))
570 `(,tag
(return-from ,block-tag
571 ,(cond ((null lambda-list
)
573 ((and (null (cdr lambda-list
))
574 (not (member (car lambda-list
)
575 '(&optional
&key
&aux
))))
576 `(funcall (lambda ,lambda-list
,@body
) ,temp-var
))
578 `(apply (lambda ,lambda-list
,@body
) ,temp-var
))))))))
579 (let ((clauses-data (mapcar #'parse-clause clauses
)))
581 (let ((,temp-var nil
))
582 (declare (ignorable ,temp-var
))
585 ,(mapcar #'make-binding clauses-data
)
586 (return-from ,block-tag
587 ,(munge-restart-case-expression expression env
)))
588 ,@(mapcan #'make-apply-and-return clauses-data
))))))))
590 (defmacro-mundanely with-simple-restart
((restart-name format-string
591 &rest format-arguments
)
594 "(WITH-SIMPLE-RESTART (restart-name format-string format-arguments)
596 If restart-name is not invoked, then all values returned by forms are
597 returned. If control is transferred to this restart, it immediately
598 returns the values NIL and T."
599 (let ((stream (sb!xc
:gensym
"STREAM")))
601 ;; If there's just one body form, then don't use PROGN. This allows
602 ;; RESTART-CASE to "see" calls to ERROR, etc.
603 ,(if (= (length forms
) 1) (car forms
) `(progn ,@forms
))
605 :report
(lambda (,stream
)
606 (declare (type stream
,stream
))
607 (format ,stream
,format-string
,@format-arguments
))
610 (defmacro-mundanely %handler-bind
(bindings form
)
611 ;; As an optimization, this looks at the handler parts of BINDINGS
612 ;; and turns handlers of the forms (lambda ...) and (function
613 ;; (lambda ...)) into local, dynamic-extent functions.
615 ;; Type specifiers in BINDINGS are translated into local,
616 ;; dynamic-extent functions to allow TYPEP optimizations.
617 (let ((local-functions '())
618 (cluster-entries '()))
619 (labels ((local-function (lambda-form &optional name
)
620 (let ((name (sb!xc
:gensym name
)))
621 (push `(,name
,@(rest lambda-form
)) local-functions
)
623 (entry-form (type handler
)
624 (let ((name (local-function
626 (typep condition
',type
))
628 `(cons (function ,name
) ,handler
)))
629 (local-function-handler (type lambda-form
)
630 (let ((name (local-function lambda-form
"HANDLER")))
631 (push (entry-form type
`(function ,name
)) cluster-entries
)))
632 (process-binding (binding)
633 (unless (proper-list-of-length-p binding
2)
634 (error "ill-formed handler binding: ~S" binding
))
635 (destructuring-bind (type handler
) binding
637 ((cons (eql lambda
) t
)
638 (local-function-handler type handler
))
639 ((cons (eql function
) (cons (cons (eql lambda
)) null
))
640 (local-function-handler type
(second handler
)))
642 (push (apply #'entry-form binding
) cluster-entries
))))))
647 (mapc #'process-binding bindings
)
648 `(dx-flet (,@(reverse local-functions
))
649 (let ((*handler-clusters
*
650 (list* (list ,@(nreverse cluster-entries
)) *handler-clusters
*)))
651 #!+stack-allocatable-fixed-objects
652 (declare (truly-dynamic-extent *handler-clusters
*))
655 (defmacro-mundanely handler-bind
(bindings &body forms
)
657 "(HANDLER-BIND ( {(type handler)}* ) body)
659 Executes body in a dynamic context where the given handler bindings are in
660 effect. Each handler must take the condition being signalled as an argument.
661 The bindings are searched first to last in the event of a signalled
663 `(%handler-bind
,bindings
664 #!-x86
(progn ,@forms
)
665 ;; Need to catch FP errors here!
666 #!+x86
(multiple-value-prog1 (progn ,@forms
) (float-wait))))
668 (defmacro-mundanely handler-case
(form &rest cases
)
670 "(HANDLER-CASE form { (type ([var]) body) }* )
672 Execute FORM in a context with handlers established for the condition types. A
673 peculiar property allows type to be :NO-ERROR. If such a clause occurs, and
674 form returns normally, all its values are passed to this clause as if by
675 MULTIPLE-VALUE-CALL. The :NO-ERROR clause accepts more than one var
677 (let ((no-error-clause (assoc ':no-error cases
)))
679 (let ((normal-return (make-symbol "normal-return"))
680 (error-return (make-symbol "error-return")))
681 `(block ,error-return
682 (multiple-value-call (lambda ,@(cdr no-error-clause
))
683 (block ,normal-return
684 (return-from ,error-return
685 (handler-case (return-from ,normal-return
,form
)
686 ,@(remove no-error-clause cases
)))))))
687 (let* ((local-funs nil
)
689 (mapcar (lambda (case)
690 (with-unique-names (tag fun
)
691 (destructuring-bind (type ll
&body body
) case
692 (push `(,fun
,ll
,@body
) local-funs
)
693 (list tag type ll fun
))))
695 (with-unique-names (block cell form-fun
)
696 `(dx-flet ((,form-fun
()
698 ;; Need to catch FP errors here!
699 #!+x86
(multiple-value-prog1 ,form
(float-wait)))
700 ,@(reverse local-funs
))
701 (declare (optimize (sb!c
::check-tag-existence
0)))
703 ;; KLUDGE: We use a dx CONS cell instead of just assigning to
704 ;; the variable directly, so that we can stack allocate
705 ;; robustly: dx value cells don't work quite right, and it is
706 ;; possible to construct user code that should loop
707 ;; indefinitely, but instead eats up some stack each time
709 (dx-let ((,cell
(cons :condition nil
)))
710 (declare (ignorable ,cell
))
713 ,(mapcar (lambda (annotated-case)
714 (destructuring-bind (tag type ll fun-name
) annotated-case
715 (declare (ignore fun-name
))
719 `(setf (cdr ,cell
) temp
)
720 '(declare (ignore temp
)))
723 (return-from ,block
(,form-fun
)))
725 (lambda (annotated-case)
726 (destructuring-bind (tag type ll fun-name
) annotated-case
727 (declare (ignore type
))
731 `(,fun-name
(cdr ,cell
))
733 annotated-cases
))))))))))
737 (defmacro-mundanely return
(&optional
(value nil
))
738 `(return-from nil
,value
))
740 (defmacro-mundanely lambda
(&whole whole args
&body body
)
741 (declare (ignore args body
))
744 (defmacro-mundanely named-lambda
(&whole whole name args
&body body
)
745 (declare (ignore name args body
))
748 (defmacro-mundanely lambda-with-lexenv
(&whole whole
749 declarations macros symbol-macros
751 (declare (ignore declarations macros symbol-macros body
))