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)))
74 (error "COND clause is not a list: ~S" clause)
75 (let ((test (first clause))
76 (forms (rest clause)))
78 (let ((n-result (gensym)))
79 `(let ((,n-result ,test))
82 (cond ,@(rest clauses)))))
85 (cond ,@(rest clauses)))))))))
87 (defmacro-mundanely when (test &body forms)
89 "If the first argument is true, the rest of the forms are
90 evaluated as a PROGN."
91 `(if ,test (progn ,@forms) nil))
93 (defmacro-mundanely unless (test &body forms)
95 "If the first argument is not true, the rest of the forms are
96 evaluated as a PROGN."
97 `(if ,test nil (progn ,@forms)))
99 (defmacro-mundanely and (&rest forms)
100 (cond ((endp forms) t)
101 ((endp (rest forms)) (first forms))
107 (defmacro-mundanely or (&rest forms)
108 (cond ((endp forms) nil)
109 ((endp (rest forms)) (first forms))
111 (let ((n-result (gensym)))
112 `(let ((,n-result ,(first forms)))
115 (or ,@(rest forms))))))))
117 ;;;; various sequencing constructs
119 (flet ((prog-expansion-from-let (varlist body-decls let)
120 (multiple-value-bind (body decls)
121 (parse-body body-decls :doc-string-allowed nil)
125 (tagbody ,@body))))))
126 (defmacro-mundanely prog (varlist &body body-decls)
127 (prog-expansion-from-let varlist body-decls 'let))
128 (defmacro-mundanely prog* (varlist &body body-decls)
129 (prog-expansion-from-let varlist body-decls 'let*)))
131 (defmacro-mundanely prog1 (result &body body)
132 (let ((n-result (gensym)))
133 `(let ((,n-result ,result))
137 (defmacro-mundanely prog2 (form1 result &body body)
138 `(prog1 (progn ,form1 ,result) ,@body))
142 ;;; Should we save the inline expansion of the function named NAME?
143 (defun inline-fun-name-p (name)
145 ;; the normal reason for saving the inline expansion
146 (info :function :inlinep name)
147 ;; another reason for saving the inline expansion: If the
148 ;; ANSI-recommended idiom
149 ;; (DECLAIM (INLINE FOO))
151 ;; (DECLAIM (NOTINLINE FOO))
152 ;; has been used, and then we later do another
154 ;; without a preceding
155 ;; (DECLAIM (INLINE FOO))
156 ;; what should we do with the old inline expansion when we see the
157 ;; new DEFUN? Overwriting it with the new definition seems like
158 ;; the only unsurprising choice.
159 (info :function :inline-expansion-designator name)))
161 (defmacro-mundanely defun (&environment env name args &body body)
162 "Define a function at top level."
164 (unless (symbol-package (fun-name-block-name name))
165 (warn "DEFUN of uninterned function name ~S (tricky for GENESIS)" name))
166 (multiple-value-bind (forms decls doc) (parse-body body)
167 (let* (;; stuff shared between LAMBDA and INLINE-LAMBDA and NAMED-LAMBDA
170 (block ,(fun-name-block-name name)
172 (lambda `(lambda ,@lambda-guts))
174 (named-lambda `(named-lambda ,name ,@lambda-guts))
176 (when (inline-fun-name-p name)
177 ;; we want to attempt to inline, so complain if we can't
178 (or (sb!c:maybe-inline-syntactic-closure lambda env)
181 #-sb-xc-host sb!c:maybe-compiler-notify
182 "lexical environment too hairy, can't inline DEFUN ~S"
186 ;; In cross-compilation of toplevel DEFUNs, we arrange for
187 ;; the LAMBDA to be statically linked by GENESIS.
189 ;; It may seem strangely inconsistent not to use NAMED-LAMBDA
190 ;; here instead of LAMBDA. The reason is historical:
191 ;; COLD-FSET was written before NAMED-LAMBDA, and has special
192 ;; logic of its own to notify the compiler about NAME.
194 (cold-fset ,name ,lambda)
196 (eval-when (:compile-toplevel)
197 (sb!c:%compiler-defun ',name ',inline-lambda t))
198 (eval-when (:load-toplevel :execute)
200 ;; In normal compilation (not for cold load) this is
201 ;; where the compiled LAMBDA first appears. In
202 ;; cross-compilation, we manipulate the
203 ;; previously-statically-linked LAMBDA here.
204 #-sb-xc-host ,named-lambda
205 #+sb-xc-host (fdefinition ',name)
208 (sb!c:source-location)))))))
211 (defun %defun (name def doc inline-lambda source-location)
212 (declare (ignore source-location))
213 (declare (type function def))
214 (declare (type (or null simple-string) doc))
215 (aver (legal-fun-name-p name)) ; should've been checked by DEFMACRO DEFUN
216 (sb!c:%compiler-defun name inline-lambda nil)
218 (/show0 "redefining NAME in %DEFUN")
219 (style-warn "redefining ~S in DEFUN" name))
220 (setf (sb!xc:fdefinition name) def)
222 (sb!c::note-name-defined name :function)
224 ;; FIXME: I want to do this here (and fix bug 137), but until the
225 ;; breathtaking CMU CL function name architecture is converted into
226 ;; something sane, (1) doing so doesn't really fix the bug, and
227 ;; (2) doing probably isn't even really safe.
228 #+nil (setf (%fun-name def) name)
231 (setf (fdocumentation name 'function) doc)
233 (when (typep def 'sb!eval:interpreted-function)
234 (setf (sb!eval:interpreted-function-documentation def)
238 ;;;; DEFVAR and DEFPARAMETER
240 (defmacro-mundanely defvar (var &optional (val nil valp) (doc nil docp))
242 "Define a global 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))
249 (eval-when (:load-toplevel :execute)
250 (%defvar ',var (unless (boundp ',var) ,val)
252 (sb!c:source-location)))))
254 (defmacro-mundanely defparameter (var val &optional (doc nil docp))
256 "Define a parameter that is not normally changed by the program,
257 but that may be changed without causing an error. Declare the
258 variable special and sets its value to VAL, overwriting any
259 previous value. The third argument is an optional documentation
260 string for the parameter."
262 (eval-when (:compile-toplevel)
263 (%compiler-defvar ',var))
264 (eval-when (:load-toplevel :execute)
265 (%defparameter ',var ,val ,doc ',docp (sb!c:source-location)))))
267 (defun %compiler-defvar (var)
268 (sb!xc:proclaim `(special ,var)))
271 (defun %defvar (var val valp doc docp source-location)
272 (%compiler-defvar var)
277 (setf (fdocumentation var 'variable) doc))
278 (sb!c:with-source-location (source-location)
279 (setf (info :source-location :variable var) source-location))
283 (defun %defparameter (var val doc docp source-location)
284 (%compiler-defvar var)
287 (setf (fdocumentation var 'variable) doc))
288 (sb!c:with-source-location (source-location)
289 (setf (info :source-location :variable var) source-location))
292 ;;;; iteration constructs
294 ;;; (These macros are defined in terms of a function FROB-DO-BODY which
295 ;;; is also used by SB!INT:DO-ANONYMOUS. Since these macros should not
296 ;;; be loaded on the cross-compilation host, but SB!INT:DO-ANONYMOUS
297 ;;; and FROB-DO-BODY should be, these macros can't conveniently be in
298 ;;; the same file as FROB-DO-BODY.)
299 (defmacro-mundanely do (varlist endlist &body body)
301 "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
302 Iteration construct. Each Var is initialized in parallel to the value of the
303 specified Init form. On subsequent iterations, the Vars are assigned the
304 value of the Step form (if any) in parallel. The Test is evaluated before
305 each evaluation of the body Forms. When the Test is true, the Exit-Forms
306 are evaluated as a PROGN, with the result being the value of the DO. A block
307 named NIL is established around the entire expansion, allowing RETURN to be
308 used as an alternate exit mechanism."
309 (frob-do-body varlist endlist body 'let 'psetq 'do nil))
310 (defmacro-mundanely do* (varlist endlist &body body)
312 "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
313 Iteration construct. Each Var is initialized sequentially (like LET*) to the
314 value of the specified Init form. On subsequent iterations, the Vars are
315 sequentially assigned the value of the Step form (if any). The Test is
316 evaluated before each evaluation of the body Forms. When the Test is true,
317 the Exit-Forms are evaluated as a PROGN, with the result being the value
318 of the DO. A block named NIL is established around the entire expansion,
319 allowing RETURN to be used as an laternate exit mechanism."
320 (frob-do-body varlist endlist body 'let* 'setq 'do* nil))
322 ;;; DOTIMES and DOLIST could be defined more concisely using
323 ;;; destructuring macro lambda lists or DESTRUCTURING-BIND, but then
324 ;;; it'd be tricky to use them before those things were defined.
325 ;;; They're used enough times before destructuring mechanisms are
326 ;;; defined that it looks as though it's worth just implementing them
327 ;;; ASAP, at the cost of being unable to use the standard
328 ;;; destructuring mechanisms.
329 (defmacro-mundanely dotimes ((var count &optional (result nil)) &body body)
330 (cond ((numberp count)
331 `(do ((,var 0 (1+ ,var)))
332 ((>= ,var ,count) ,result)
333 (declare (type unsigned-byte ,var))
336 (let ((c (gensym "COUNT")))
337 `(do ((,var 0 (1+ ,var))
339 ((>= ,var ,c) ,result)
340 (declare (type unsigned-byte ,var)
344 (defmacro-mundanely dolist ((var list &optional (result nil)) &body body)
345 ;; We repeatedly bind the var instead of setting it so that we never
346 ;; have to give the var an arbitrary value such as NIL (which might
347 ;; conflict with a declaration). If there is a result form, we
348 ;; introduce a gratuitous binding of the variable to NIL without the
349 ;; declarations, then evaluate the result form in that
350 ;; environment. We spuriously reference the gratuitous variable,
351 ;; since we don't want to use IGNORABLE on what might be a special
353 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
354 (let ((n-list (gensym "N-LIST"))
355 (start (gensym "START")))
357 (let ((,n-list ,list))
360 (unless (endp ,n-list)
361 (let ((,var (car ,n-list)))
363 (setq ,n-list (cdr ,n-list))
368 ;; Filter out TYPE declarations (VAR gets bound to NIL,
369 ;; and might have a conflicting type declaration) and
370 ;; IGNORE (VAR might be ignored in the loop body, but
371 ;; it's used in the result form).
372 ,@(filter-dolist-declarations decls)
377 ;;;; conditions, handlers, restarts
379 ;;; KLUDGE: we PROCLAIM these special here so that we can use restart
380 ;;; macros in the compiler before the DEFVARs are compiled.
382 '(special *handler-clusters* *restart-clusters* *condition-restarts*))
384 (defmacro-mundanely with-condition-restarts
385 (condition-form restarts-form &body body)
387 "Evaluates the BODY in a dynamic environment where the restarts in the list
388 RESTARTS-FORM are associated with the condition returned by CONDITION-FORM.
389 This allows FIND-RESTART, etc., to recognize restarts that are not related
390 to the error currently being debugged. See also RESTART-CASE."
391 (let ((n-cond (gensym)))
392 `(let ((*condition-restarts*
393 (cons (let ((,n-cond ,condition-form))
395 (append ,restarts-form
396 (cdr (assoc ,n-cond *condition-restarts*)))))
397 *condition-restarts*)))
400 (defmacro-mundanely restart-bind (bindings &body forms)
402 "Executes forms in a dynamic context where the given restart bindings are
403 in effect. Users probably want to use RESTART-CASE. When clauses contain
404 the same restart name, FIND-RESTART will find the first such clause."
405 `(let ((*restart-clusters*
407 ,@(mapcar (lambda (binding)
408 (unless (or (car binding)
409 (member :report-function
412 (warn "Unnamed restart does not have a ~
415 `(make-restart :name ',(car binding)
416 :function ,(cadr binding)
419 *restart-clusters*)))
422 ;;; Wrap the RESTART-CASE expression in a WITH-CONDITION-RESTARTS if
423 ;;; appropriate. Gross, but it's what the book seems to say...
424 (defun munge-restart-case-expression (expression env)
425 (let ((exp (sb!xc:macroexpand expression env)))
427 (let* ((name (car exp))
428 (args (if (eq name 'cerror) (cddr exp) (cdr exp))))
429 (if (member name '(signal error cerror warn))
430 (once-only ((n-cond `(coerce-to-condition
434 (warn 'simple-warning)
435 (signal 'simple-condition)
438 `(with-condition-restarts
440 (car *restart-clusters*)
441 ,(if (eq name 'cerror)
442 `(cerror ,(second exp) ,n-cond)
447 ;;; FIXME: I did a fair amount of rearrangement of this code in order to
448 ;;; get WITH-KEYWORD-PAIRS to work cleanly. This code should be tested..
449 (defmacro-mundanely restart-case (expression &body clauses &environment env)
452 {(case-name arg-list {keyword value}* body)}*)
453 The form is evaluated in a dynamic context where the clauses have special
454 meanings as points to which control may be transferred (see INVOKE-RESTART).
455 When clauses contain the same case-name, FIND-RESTART will find the first
456 such clause. If Expression is a call to SIGNAL, ERROR, CERROR or WARN (or
457 macroexpands into such) then the signalled condition will be associated with
459 (flet ((transform-keywords (&key report interactive test)
462 (setq result (list* (if (stringp report)
464 (write-string ,report stream))
469 (setq result (list* `#',interactive
470 :interactive-function
473 (setq result (list* `#',test :test-function result)))
475 (parse-keyword-pairs (list keys)
476 (do ((l list (cddr l))
477 (k '() (list* (cadr l) (car l) k)))
478 ((or (null l) (not (member (car l) keys)))
479 (values (nreverse k) l)))))
480 (let ((block-tag (gensym))
483 (macrolet (;; KLUDGE: This started as an old DEFMACRO
484 ;; WITH-KEYWORD-PAIRS general utility, which was used
485 ;; only in this one place in the code. It was translated
486 ;; literally into this MACROLET in order to avoid some
487 ;; cross-compilation bootstrap problems. It would almost
488 ;; certainly be clearer, and it would certainly be more
489 ;; concise, to do a more idiomatic translation, merging
490 ;; this with the TRANSFORM-KEYWORDS logic above.
492 (with-keyword-pairs ((names expression) &body forms)
493 (let ((temp (member '&rest names)))
494 (unless (= (length temp) 2)
495 (error "&REST keyword is ~:[missing~;misplaced~]."
497 (let* ((key-vars (ldiff names temp))
498 (keywords (mapcar #'keywordicate key-vars))
500 (rest-var (cadr temp)))
501 `(multiple-value-bind (,key-var ,rest-var)
502 (parse-keyword-pairs ,expression ',keywords)
503 (let ,(mapcar (lambda (var keyword)
504 `(,var (getf ,key-var
508 (mapcar (lambda (clause)
509 (with-keyword-pairs ((report interactive test
512 (list (car clause) ;name=0
514 (transform-keywords :report report ;keywords=2
515 :interactive interactive
521 (let ((,temp-var nil))
524 ,(mapcar (lambda (datum)
525 (let ((name (nth 0 datum))
527 (keys (nth 2 datum)))
528 `(,name #'(lambda (&rest temp)
529 (setq ,temp-var temp)
533 (return-from ,block-tag
534 ,(munge-restart-case-expression expression env)))
535 ,@(mapcan (lambda (datum)
536 (let ((tag (nth 1 datum))
538 (body (nth 4 datum)))
540 `(return-from ,block-tag
541 (apply (lambda ,bvl ,@body)
545 (defmacro-mundanely with-simple-restart ((restart-name format-string
546 &rest format-arguments)
549 "(WITH-SIMPLE-RESTART (restart-name format-string format-arguments)
551 If restart-name is not invoked, then all values returned by forms are
552 returned. If control is transferred to this restart, it immediately
553 returns the values NIL and T."
555 ;; If there's just one body form, then don't use PROGN. This allows
556 ;; RESTART-CASE to "see" calls to ERROR, etc.
557 ,(if (= (length forms) 1) (car forms) `(progn ,@forms))
559 :report (lambda (stream)
560 (format stream ,format-string ,@format-arguments))
563 (defmacro-mundanely handler-bind (bindings &body forms)
565 "(HANDLER-BIND ( {(type handler)}* ) body)
566 Executes body in a dynamic context where the given handler bindings are
567 in effect. Each handler must take the condition being signalled as an
568 argument. The bindings are searched first to last in the event of a
569 signalled condition."
570 (let ((member-if (member-if (lambda (x)
571 (not (proper-list-of-length-p x 2)))
574 (error "ill-formed handler binding: ~S" (first member-if))))
575 `(let ((*handler-clusters*
576 (cons (list ,@(mapcar (lambda (x) `(cons ',(car x) ,(cadr x)))
578 *handler-clusters*)))
579 (multiple-value-prog1
582 ;; Wait for any float exceptions.
583 #!+x86 (float-wait))))
585 (defmacro-mundanely handler-case (form &rest cases)
587 { (type ([var]) body) }* )
588 Execute FORM in a context with handlers established for the condition
589 types. A peculiar property allows type to be :NO-ERROR. If such a clause
590 occurs, and form returns normally, all its values are passed to this clause
591 as if by MULTIPLE-VALUE-CALL. The :NO-ERROR clause accepts more than one
593 ;; FIXME: Replacing CADR, CDDDR and friends with DESTRUCTURING-BIND
594 ;; and names for the subexpressions would make it easier to
595 ;; understand the code below.
596 (let ((no-error-clause (assoc ':no-error cases)))
598 (let ((normal-return (make-symbol "normal-return"))
599 (error-return (make-symbol "error-return")))
600 `(block ,error-return
601 (multiple-value-call (lambda ,@(cdr no-error-clause))
602 (block ,normal-return
603 (return-from ,error-return
604 (handler-case (return-from ,normal-return ,form)
605 ,@(remove no-error-clause cases)))))))
608 (annotated-cases (mapcar (lambda (case) (cons (gensym) case))
612 (declare (ignorable ,var))
615 ,(mapcar (lambda (annotated-case)
616 (list (cadr annotated-case)
618 ,(if (caddr annotated-case)
620 '(declare (ignore temp)))
621 (go ,(car annotated-case)))))
625 #!+x86 (multiple-value-prog1 ,form
626 ;; Need to catch FP errors here!
629 (lambda (annotated-case)
630 (list (car annotated-case)
631 (let ((body (cdddr annotated-case)))
634 ,(cond ((caddr annotated-case)
635 `(let ((,(caaddr annotated-case)
639 `(locally ,@body)))))))
640 annotated-cases))))))))
644 (defmacro-mundanely return (&optional (value nil))
645 `(return-from nil ,value))
647 (defmacro-mundanely psetq (&rest pairs)
650 Set the variables to the values, like SETQ, except that assignments
651 happen in parallel, i.e. no assignments take place until all the
652 forms have been evaluated."
653 ;; Given the possibility of symbol-macros, we delegate to PSETF
654 ;; which knows how to deal with them, after checking that syntax is
655 ;; compatible with PSETQ.
656 (do ((pair pairs (cddr pair)))
657 ((endp pair) `(psetf ,@pairs))
658 (unless (symbolp (car pair))
659 (error 'simple-program-error
660 :format-control "variable ~S in PSETQ is not a SYMBOL"
661 :format-arguments (list (car pair))))))
663 (defmacro-mundanely lambda (&whole whole args &body body)
664 (declare (ignore args body))
667 (defmacro-mundanely named-lambda (&whole whole name args &body body)
668 (declare (ignore name args body))
671 (defmacro-mundanely lambda-with-lexenv (&whole whole
672 declarations macros symbol-macros
674 (declare (ignore declarations macros symbol-macros body))
677 ;;; this eliminates a whole bundle of unknown function STYLE-WARNINGs
678 ;;; when cross-compiling. It's not critical for behaviour, but is
679 ;;; aesthetically pleasing, except inasmuch as there's this list of
680 ;;; magic functions here. -- CSR, 2003-04-01
682 (sb!xc:proclaim '(ftype (function * *)
683 ;; functions appearing in fundamental defining
692 sb!c::%define-symbol-macro
694 sb!c::%define-compiler-macro
696 sb!kernel::%compiler-defstruct
697 sb!kernel::%compiler-define-condition
698 sb!kernel::%defstruct
699 sb!kernel::%define-condition
700 ;; miscellaneous functions commonly appearing
701 ;; as a result of macro expansions or compiler
703 sb!int:find-undeleted-package-or-lose ; IN-PACKAGE
704 sb!kernel::arg-count-error ; PARSE-DEFMACRO