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 (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 "COND clause is not a list: ~S" clause)
76 (let ((test (first clause))
77 (forms (rest clause)))
79 (let ((n-result (gensym)))
80 `(let ((,n-result ,test))
88 ,(when more `(cond ,@more))))))))))
90 (defmacro-mundanely when (test &body forms)
92 "If the first argument is true, the rest of the forms are
93 evaluated as a PROGN."
94 `(if ,test (progn ,@forms) nil))
96 (defmacro-mundanely unless (test &body forms)
98 "If the first argument is not true, the rest of the forms are
99 evaluated as a PROGN."
100 `(if ,test nil (progn ,@forms)))
102 (defmacro-mundanely and (&rest forms)
103 (cond ((endp forms) t)
104 ((endp (rest forms)) (first forms))
110 (defmacro-mundanely or (&rest forms)
111 (cond ((endp forms) nil)
112 ((endp (rest forms)) (first forms))
114 (let ((n-result (gensym)))
115 `(let ((,n-result ,(first forms)))
118 (or ,@(rest forms))))))))
120 ;;;; various sequencing constructs
122 (flet ((prog-expansion-from-let (varlist body-decls let)
123 (multiple-value-bind (body decls)
124 (parse-body body-decls :doc-string-allowed nil)
128 (tagbody ,@body))))))
129 (defmacro-mundanely prog (varlist &body body-decls)
130 (prog-expansion-from-let varlist body-decls 'let))
131 (defmacro-mundanely prog* (varlist &body body-decls)
132 (prog-expansion-from-let varlist body-decls 'let*)))
134 (defmacro-mundanely prog1 (result &body body)
135 (let ((n-result (gensym)))
136 `(let ((,n-result ,result))
140 (defmacro-mundanely prog2 (form1 result &body body)
141 `(prog1 (progn ,form1 ,result) ,@body))
145 ;;; Should we save the inline expansion of the function named NAME?
146 (defun inline-fun-name-p (name)
148 ;; the normal reason for saving the inline expansion
149 (info :function :inlinep name)
150 ;; another reason for saving the inline expansion: If the
151 ;; ANSI-recommended idiom
152 ;; (DECLAIM (INLINE FOO))
154 ;; (DECLAIM (NOTINLINE FOO))
155 ;; has been used, and then we later do another
157 ;; without a preceding
158 ;; (DECLAIM (INLINE FOO))
159 ;; what should we do with the old inline expansion when we see the
160 ;; new DEFUN? Overwriting it with the new definition seems like
161 ;; the only unsurprising choice.
162 (info :function :inline-expansion-designator name)))
164 (defmacro-mundanely defun (&environment env name args &body body)
165 "Define a function at top level."
167 (unless (symbol-package (fun-name-block-name name))
168 (warn "DEFUN of uninterned function name ~S (tricky for GENESIS)" name))
169 (multiple-value-bind (forms decls doc) (parse-body body)
170 (let* (;; stuff shared between LAMBDA and INLINE-LAMBDA and NAMED-LAMBDA
173 (block ,(fun-name-block-name name)
175 (lambda `(lambda ,@lambda-guts))
177 (named-lambda `(named-lambda ,name ,@lambda-guts))
179 (when (inline-fun-name-p name)
180 ;; we want to attempt to inline, so complain if we can't
181 (or (sb!c:maybe-inline-syntactic-closure lambda env)
184 #-sb-xc-host sb!c:maybe-compiler-notify
185 "lexical environment too hairy, can't inline DEFUN ~S"
189 ;; In cross-compilation of toplevel DEFUNs, we arrange for
190 ;; the LAMBDA to be statically linked by GENESIS.
192 ;; It may seem strangely inconsistent not to use NAMED-LAMBDA
193 ;; here instead of LAMBDA. The reason is historical:
194 ;; COLD-FSET was written before NAMED-LAMBDA, and has special
195 ;; logic of its own to notify the compiler about NAME.
197 (cold-fset ,name ,lambda)
199 (eval-when (:compile-toplevel)
200 (sb!c:%compiler-defun ',name ',inline-lambda t))
201 (eval-when (:load-toplevel :execute)
203 ;; In normal compilation (not for cold load) this is
204 ;; where the compiled LAMBDA first appears. In
205 ;; cross-compilation, we manipulate the
206 ;; previously-statically-linked LAMBDA here.
207 #-sb-xc-host ,named-lambda
208 #+sb-xc-host (fdefinition ',name)
211 (sb!c:source-location)))))))
214 (defun %defun (name def doc inline-lambda source-location)
215 (declare (type function def))
216 (declare (type (or null simple-string) doc))
217 (aver (legal-fun-name-p name)) ; should've been checked by DEFMACRO DEFUN
218 (sb!c:%compiler-defun name inline-lambda nil)
220 (/show0 "redefining NAME in %DEFUN")
221 (style-warn 'sb!kernel::redefinition-with-defun :name name
222 :old (fdefinition name) :new def
223 :new-location source-location))
224 (setf (sb!xc:fdefinition name) def)
226 (sb!c::note-name-defined name :function)
228 ;; FIXME: I want to do this here (and fix bug 137), but until the
229 ;; breathtaking CMU CL function name architecture is converted into
230 ;; something sane, (1) doing so doesn't really fix the bug, and
231 ;; (2) doing probably isn't even really safe.
232 #+nil (setf (%fun-name def) name)
235 (setf (fdocumentation name 'function) doc)
237 (when (typep def 'sb!eval:interpreted-function)
238 (setf (sb!eval:interpreted-function-documentation def)
242 ;;;; DEFVAR and DEFPARAMETER
244 (defmacro-mundanely defvar (var &optional (val nil valp) (doc nil docp))
246 "Define a global variable at top level. Declare the variable
247 SPECIAL and, optionally, initialize it. If the variable already has a
248 value, the old value is not clobbered. The third argument is an optional
249 documentation string for the variable."
251 (eval-when (:compile-toplevel)
252 (%compiler-defvar ',var))
253 (eval-when (:load-toplevel :execute)
254 (%defvar ',var (unless (boundp ',var) ,val)
256 (sb!c:source-location)))))
258 (defmacro-mundanely defparameter (var val &optional (doc nil docp))
260 "Define a parameter that is not normally changed by the program,
261 but that may be changed without causing an error. Declare the
262 variable special and sets its value to VAL, overwriting any
263 previous value. The third argument is an optional documentation
264 string for the parameter."
266 (eval-when (:compile-toplevel)
267 (%compiler-defvar ',var))
268 (eval-when (:load-toplevel :execute)
269 (%defparameter ',var ,val ,doc ',docp (sb!c:source-location)))))
271 (defun %compiler-defvar (var)
272 (sb!xc:proclaim `(special ,var)))
275 (defun %defvar (var val valp doc docp source-location)
276 (%compiler-defvar var)
281 (setf (fdocumentation var 'variable) doc))
282 (sb!c:with-source-location (source-location)
283 (setf (info :source-location :variable var) source-location))
287 (defun %defparameter (var val doc docp source-location)
288 (%compiler-defvar var)
291 (setf (fdocumentation var 'variable) doc))
292 (sb!c:with-source-location (source-location)
293 (setf (info :source-location :variable var) source-location))
296 ;;;; iteration constructs
298 ;;; (These macros are defined in terms of a function FROB-DO-BODY which
299 ;;; is also used by SB!INT:DO-ANONYMOUS. Since these macros should not
300 ;;; be loaded on the cross-compilation host, but SB!INT:DO-ANONYMOUS
301 ;;; and FROB-DO-BODY should be, these macros can't conveniently be in
302 ;;; the same file as FROB-DO-BODY.)
303 (defmacro-mundanely do (varlist endlist &body body)
305 "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
306 Iteration construct. Each Var is initialized in parallel to the value of the
307 specified Init form. On subsequent iterations, the Vars are assigned the
308 value of the Step form (if any) in parallel. The Test is evaluated before
309 each evaluation of the body Forms. When the Test is true, the Exit-Forms
310 are evaluated as a PROGN, with the result being the value of the DO. A block
311 named NIL is established around the entire expansion, allowing RETURN to be
312 used as an alternate exit mechanism."
313 (frob-do-body varlist endlist body 'let 'psetq 'do nil))
314 (defmacro-mundanely do* (varlist endlist &body body)
316 "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
317 Iteration construct. Each Var is initialized sequentially (like LET*) to the
318 value of the specified Init form. On subsequent iterations, the Vars are
319 sequentially assigned the value of the Step form (if any). The Test is
320 evaluated before each evaluation of the body Forms. When the Test is true,
321 the Exit-Forms are evaluated as a PROGN, with the result being the value
322 of the DO. A block named NIL is established around the entire expansion,
323 allowing RETURN to be used as an laternate exit mechanism."
324 (frob-do-body varlist endlist body 'let* 'setq 'do* nil))
326 ;;; DOTIMES and DOLIST could be defined more concisely using
327 ;;; destructuring macro lambda lists or DESTRUCTURING-BIND, but then
328 ;;; it'd be tricky to use them before those things were defined.
329 ;;; They're used enough times before destructuring mechanisms are
330 ;;; defined that it looks as though it's worth just implementing them
331 ;;; ASAP, at the cost of being unable to use the standard
332 ;;; destructuring mechanisms.
333 (defmacro-mundanely dotimes ((var count &optional (result nil)) &body body)
334 (cond ((numberp count)
335 `(do ((,var 0 (1+ ,var)))
336 ((>= ,var ,count) ,result)
337 (declare (type unsigned-byte ,var))
340 (let ((c (gensym "COUNT")))
341 `(do ((,var 0 (1+ ,var))
343 ((>= ,var ,c) ,result)
344 (declare (type unsigned-byte ,var)
348 (defmacro-mundanely dolist ((var list &optional (result nil)) &body body)
349 ;; We repeatedly bind the var instead of setting it so that we never
350 ;; have to give the var an arbitrary value such as NIL (which might
351 ;; conflict with a declaration). If there is a result form, we
352 ;; introduce a gratuitous binding of the variable to NIL without the
353 ;; declarations, then evaluate the result form in that
354 ;; environment. We spuriously reference the gratuitous variable,
355 ;; since we don't want to use IGNORABLE on what might be a special
357 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
358 (let ((n-list (gensym "N-LIST"))
359 (start (gensym "START")))
361 (let ((,n-list ,list))
364 (unless (endp ,n-list)
365 (let ((,var (car ,n-list)))
367 (setq ,n-list (cdr ,n-list))
372 ;; Filter out TYPE declarations (VAR gets bound to NIL,
373 ;; and might have a conflicting type declaration) and
374 ;; IGNORE (VAR might be ignored in the loop body, but
375 ;; it's used in the result form).
376 ,@(filter-dolist-declarations decls)
381 ;;;; conditions, handlers, restarts
383 ;;; KLUDGE: we PROCLAIM these special here so that we can use restart
384 ;;; macros in the compiler before the DEFVARs are compiled.
386 '(special *handler-clusters* *restart-clusters* *condition-restarts*))
388 (defmacro-mundanely with-condition-restarts
389 (condition-form restarts-form &body body)
391 "Evaluates the BODY in a dynamic environment where the restarts in the list
392 RESTARTS-FORM are associated with the condition returned by CONDITION-FORM.
393 This allows FIND-RESTART, etc., to recognize restarts that are not related
394 to the error currently being debugged. See also RESTART-CASE."
395 (let ((n-cond (gensym)))
396 `(let ((*condition-restarts*
397 (cons (let ((,n-cond ,condition-form))
399 (append ,restarts-form
400 (cdr (assoc ,n-cond *condition-restarts*)))))
401 *condition-restarts*)))
404 (defmacro-mundanely restart-bind (bindings &body forms)
406 "Executes forms in a dynamic context where the given restart bindings are
407 in effect. Users probably want to use RESTART-CASE. When clauses contain
408 the same restart name, FIND-RESTART will find the first such clause."
409 `(let ((*restart-clusters*
411 ,@(mapcar (lambda (binding)
412 (unless (or (car binding)
413 (member :report-function
416 (warn "Unnamed restart does not have a ~
419 `(make-restart :name ',(car binding)
420 :function ,(cadr binding)
423 *restart-clusters*)))
426 ;;; Wrap the RESTART-CASE expression in a WITH-CONDITION-RESTARTS if
427 ;;; appropriate. Gross, but it's what the book seems to say...
428 (defun munge-restart-case-expression (expression env)
429 (let ((exp (sb!xc:macroexpand expression env)))
431 (let* ((name (car exp))
432 (args (if (eq name 'cerror) (cddr exp) (cdr exp))))
433 (if (member name '(signal error cerror warn))
434 (once-only ((n-cond `(coerce-to-condition
438 (warn 'simple-warning)
439 (signal 'simple-condition)
442 `(with-condition-restarts
444 (car *restart-clusters*)
445 ,(if (eq name 'cerror)
446 `(cerror ,(second exp) ,n-cond)
451 ;;; FIXME: I did a fair amount of rearrangement of this code in order to
452 ;;; get WITH-KEYWORD-PAIRS to work cleanly. This code should be tested..
453 (defmacro-mundanely restart-case (expression &body clauses &environment env)
456 {(case-name arg-list {keyword value}* body)}*)
457 The form is evaluated in a dynamic context where the clauses have special
458 meanings as points to which control may be transferred (see INVOKE-RESTART).
459 When clauses contain the same case-name, FIND-RESTART will find the first
460 such clause. If Expression is a call to SIGNAL, ERROR, CERROR or WARN (or
461 macroexpands into such) then the signalled condition will be associated with
463 (flet ((transform-keywords (&key report interactive test)
466 (setq result (list* (if (stringp report)
468 (write-string ,report stream))
473 (setq result (list* `#',interactive
474 :interactive-function
477 (setq result (list* `#',test :test-function result)))
479 (parse-keyword-pairs (list keys)
480 (do ((l list (cddr l))
481 (k '() (list* (cadr l) (car l) k)))
482 ((or (null l) (not (member (car l) keys)))
483 (values (nreverse k) l)))))
484 (let ((block-tag (gensym))
487 (macrolet (;; KLUDGE: This started as an old DEFMACRO
488 ;; WITH-KEYWORD-PAIRS general utility, which was used
489 ;; only in this one place in the code. It was translated
490 ;; literally into this MACROLET in order to avoid some
491 ;; cross-compilation bootstrap problems. It would almost
492 ;; certainly be clearer, and it would certainly be more
493 ;; concise, to do a more idiomatic translation, merging
494 ;; this with the TRANSFORM-KEYWORDS logic above.
496 (with-keyword-pairs ((names expression) &body forms)
497 (let ((temp (member '&rest names)))
498 (unless (= (length temp) 2)
499 (error "&REST keyword is ~:[missing~;misplaced~]."
501 (let* ((key-vars (ldiff names temp))
502 (keywords (mapcar #'keywordicate key-vars))
504 (rest-var (cadr temp)))
505 `(multiple-value-bind (,key-var ,rest-var)
506 (parse-keyword-pairs ,expression ',keywords)
507 (let ,(mapcar (lambda (var keyword)
508 `(,var (getf ,key-var
512 (mapcar (lambda (clause)
513 (with-keyword-pairs ((report interactive test
516 (list (car clause) ;name=0
518 (transform-keywords :report report ;keywords=2
519 :interactive interactive
525 (let ((,temp-var nil))
528 ,(mapcar (lambda (datum)
529 (let ((name (nth 0 datum))
531 (keys (nth 2 datum)))
532 `(,name #'(lambda (&rest temp)
533 (setq ,temp-var temp)
537 (return-from ,block-tag
538 ,(munge-restart-case-expression expression env)))
539 ,@(mapcan (lambda (datum)
540 (let ((tag (nth 1 datum))
542 (body (nth 4 datum)))
544 `(return-from ,block-tag
545 (apply (lambda ,bvl ,@body)
549 (defmacro-mundanely with-simple-restart ((restart-name format-string
550 &rest format-arguments)
553 "(WITH-SIMPLE-RESTART (restart-name format-string format-arguments)
555 If restart-name is not invoked, then all values returned by forms are
556 returned. If control is transferred to this restart, it immediately
557 returns the values NIL and T."
559 ;; If there's just one body form, then don't use PROGN. This allows
560 ;; RESTART-CASE to "see" calls to ERROR, etc.
561 ,(if (= (length forms) 1) (car forms) `(progn ,@forms))
563 :report (lambda (stream)
564 (format stream ,format-string ,@format-arguments))
567 (defmacro-mundanely %handler-bind (bindings form)
568 (let ((member-if (member-if (lambda (x)
569 (not (proper-list-of-length-p x 2)))
572 (error "ill-formed handler binding: ~S" (first member-if))))
573 (let* ((local-funs nil)
574 (mapped-bindings (mapcar (lambda (binding)
575 (destructuring-bind (type handler) binding
576 (let ((lambda-form handler))
577 (if (and (consp handler)
578 (or (eq 'lambda (car handler))
579 (and (eq 'function (car handler))
580 (consp (cdr handler))
581 (let ((x (second handler)))
584 (setf lambda-form x))))))
585 (let ((name (gensym "LAMBDA")))
586 (push `(,name ,@(cdr lambda-form)) local-funs)
587 (list type `(function ,name)))
590 `(dx-flet (,@(reverse local-funs))
591 (let ((*handler-clusters*
592 (cons (list ,@(mapcar (lambda (x) `(cons ',(car x) ,(cadr x)))
594 *handler-clusters*)))
595 (declare (truly-dynamic-extent *handler-clusters*))
598 (defmacro-mundanely handler-bind (bindings &body forms)
600 "(HANDLER-BIND ( {(type handler)}* ) body)
602 Executes body in a dynamic context where the given handler bindings are in
603 effect. Each handler must take the condition being signalled as an argument.
604 The bindings are searched first to last in the event of a signalled
606 `(%handler-bind ,bindings
607 #!-x86 (progn ,@forms)
608 ;; Need to catch FP errors here!
609 #!+x86 (multiple-value-prog1 (progn ,@forms) (float-wait))))
611 (defmacro-mundanely handler-case (form &rest cases)
612 "(HANDLER-CASE form { (type ([var]) body) }* )
614 Execute FORM in a context with handlers established for the condition types. A
615 peculiar property allows type to be :NO-ERROR. If such a clause occurs, and
616 form returns normally, all its values are passed to this clause as if by
617 MULTIPLE-VALUE-CALL. The :NO-ERROR clause accepts more than one var
619 (let ((no-error-clause (assoc ':no-error cases)))
621 (let ((normal-return (make-symbol "normal-return"))
622 (error-return (make-symbol "error-return")))
623 `(block ,error-return
624 (multiple-value-call (lambda ,@(cdr no-error-clause))
625 (block ,normal-return
626 (return-from ,error-return
627 (handler-case (return-from ,normal-return ,form)
628 ,@(remove no-error-clause cases)))))))
629 (let* ((local-funs nil)
630 (annotated-cases (mapcar (lambda (case)
631 (let ((tag (gensym "TAG"))
632 (fun (gensym "FUN")))
633 (destructuring-bind (type ll &body body) case
634 (push `(,fun ,ll ,@body) local-funs)
635 (list tag type ll fun))))
637 (with-unique-names (block var form-fun)
638 `(dx-flet ((,form-fun ()
640 ;; Need to catch FP errors here!
641 #!+x86 (multiple-value-prog1 ,form (float-wait)))
642 ,@(reverse local-funs))
643 (declare (optimize (sb!c::check-tag-existence 0)))
646 (declare (ignorable ,var))
649 ,(mapcar (lambda (annotated-case)
650 (destructuring-bind (tag type ll fun-name) annotated-case
651 (declare (ignore fun-name))
656 '(declare (ignore temp)))
659 (return-from ,block (,form-fun)))
661 (lambda (annotated-case)
662 (destructuring-bind (tag type ll fun-name) annotated-case
663 (declare (ignore type))
669 annotated-cases))))))))))
673 (defmacro-mundanely return (&optional (value nil))
674 `(return-from nil ,value))
676 (defmacro-mundanely psetq (&rest pairs)
679 Set the variables to the values, like SETQ, except that assignments
680 happen in parallel, i.e. no assignments take place until all the
681 forms have been evaluated."
682 ;; Given the possibility of symbol-macros, we delegate to PSETF
683 ;; which knows how to deal with them, after checking that syntax is
684 ;; compatible with PSETQ.
685 (do ((pair pairs (cddr pair)))
686 ((endp pair) `(psetf ,@pairs))
687 (unless (symbolp (car pair))
688 (error 'simple-program-error
689 :format-control "variable ~S in PSETQ is not a SYMBOL"
690 :format-arguments (list (car pair))))))
692 (defmacro-mundanely lambda (&whole whole args &body body)
693 (declare (ignore args body))
696 (defmacro-mundanely named-lambda (&whole whole name args &body body)
697 (declare (ignore name args body))
700 (defmacro-mundanely lambda-with-lexenv (&whole whole
701 declarations macros symbol-macros
703 (declare (ignore declarations macros symbol-macros body))
706 ;;; this eliminates a whole bundle of unknown function STYLE-WARNINGs
707 ;;; when cross-compiling. It's not critical for behaviour, but is
708 ;;; aesthetically pleasing, except inasmuch as there's this list of
709 ;;; magic functions here. -- CSR, 2003-04-01
711 (sb!xc:proclaim '(ftype (function * *)
712 ;; functions appearing in fundamental defining
721 sb!c::%define-symbol-macro
723 sb!c::%define-compiler-macro
725 sb!kernel::%compiler-defstruct
726 sb!kernel::%compiler-define-condition
727 sb!kernel::%defstruct
728 sb!kernel::%define-condition
729 ;; miscellaneous functions commonly appearing
730 ;; as a result of macro expansions or compiler
732 sb!int:find-undeleted-package-or-lose ; IN-PACKAGE
733 sb!kernel::arg-count-error ; PARSE-DEFMACRO