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")
25 (defmacro-mundanely in-package (package-designator)
26 `(eval-when (:compile-toplevel :load-toplevel :execute)
27 (setq *package* (find-undeleted-package-or-lose ',package-designator))))
29 ;;;; MULTIPLE-VALUE-FOO
31 (defun list-of-symbols-p (x)
35 (defmacro-mundanely multiple-value-bind (vars value-form &body body)
36 (if (list-of-symbols-p vars)
37 ;; It's unclear why it would be important to special-case the LENGTH=1 case
38 ;; at this level, but the CMU CL code did it, so.. -- WHN 19990411
39 (if (= (length vars) 1)
40 `(let ((,(car vars) ,value-form))
42 (let ((ignore (gensym)))
43 `(multiple-value-call #'(lambda (&optional ,@(mapcar #'list vars)
45 (declare (ignore ,ignore))
48 (error "Vars is not a list of symbols: ~S" vars)))
50 (defmacro-mundanely multiple-value-setq (vars value-form)
51 (unless (list-of-symbols-p vars)
52 (error "Vars is not a list of symbols: ~S" vars))
53 ;; MULTIPLE-VALUE-SETQ is required to always return just the primary
54 ;; value of the value-from, even if there are no vars. (SETF VALUES)
55 ;; in turn is required to return as many values as there are
56 ;; value-places, hence this:
58 `(values (setf (values ,@vars) ,value-form))
59 `(values ,value-form)))
61 (defmacro-mundanely multiple-value-list (value-form)
62 `(multiple-value-call #'list ,value-form))
64 ;;;; various conditional constructs
66 ;;; COND defined in terms of IF
67 (defmacro-mundanely cond (&rest clauses)
70 (let ((clause (first clauses)))
72 (error "COND clause is not a list: ~S" clause)
73 (let ((test (first clause))
74 (forms (rest clause)))
76 (let ((n-result (gensym)))
77 `(let ((,n-result ,test))
80 (cond ,@(rest clauses)))))
83 (cond ,@(rest clauses)))))))))
85 ;;; other things defined in terms of COND
86 (defmacro-mundanely when (test &body forms)
88 "If the first argument is true, the rest of the forms are
89 evaluated as a PROGN."
90 `(cond (,test nil ,@forms)))
91 (defmacro-mundanely unless (test &body forms)
93 "If the first argument is not true, the rest of the forms are
94 evaluated as a PROGN."
95 `(cond ((not ,test) nil ,@forms)))
96 (defmacro-mundanely and (&rest forms)
97 (cond ((endp forms) t)
98 ((endp (rest forms)) (first forms))
103 (defmacro-mundanely or (&rest forms)
104 (cond ((endp forms) nil)
105 ((endp (rest forms)) (first forms))
107 (let ((n-result (gensym)))
108 `(let ((,n-result ,(first forms)))
111 (or ,@(rest forms))))))))
113 ;;;; various sequencing constructs
115 (flet ((prog-expansion-from-let (varlist body-decls let)
116 (multiple-value-bind (body decls)
117 (parse-body body-decls :doc-string-allowed nil)
121 (tagbody ,@body))))))
122 (defmacro-mundanely prog (varlist &body body-decls)
123 (prog-expansion-from-let varlist body-decls 'let))
124 (defmacro-mundanely prog* (varlist &body body-decls)
125 (prog-expansion-from-let varlist body-decls 'let*)))
127 (defmacro-mundanely prog1 (result &body body)
128 (let ((n-result (gensym)))
129 `(let ((,n-result ,result))
133 (defmacro-mundanely prog2 (form1 result &body body)
134 `(prog1 (progn ,form1 ,result) ,@body))
138 ;;; Should we save the inline expansion of the function named NAME?
139 (defun inline-fun-name-p (name)
141 ;; the normal reason for saving the inline expansion
142 (info :function :inlinep name)
143 ;; another reason for saving the inline expansion: If the
144 ;; ANSI-recommended idiom
145 ;; (DECLAIM (INLINE FOO))
147 ;; (DECLAIM (NOTINLINE FOO))
148 ;; has been used, and then we later do another
150 ;; without a preceding
151 ;; (DECLAIM (INLINE FOO))
152 ;; what should we do with the old inline expansion when we see the
153 ;; new DEFUN? Overwriting it with the new definition seems like
154 ;; the only unsurprising choice.
155 (info :function :inline-expansion-designator name)))
157 (defmacro-mundanely defun (&environment env name args &body body)
158 "Define a function at top level."
160 (unless (symbol-package (fun-name-block-name name))
161 (warn "DEFUN of uninterned function name ~S (tricky for GENESIS)" name))
162 (multiple-value-bind (forms decls doc) (parse-body body)
163 (let* (;; stuff shared between LAMBDA and INLINE-LAMBDA and NAMED-LAMBDA
166 (block ,(fun-name-block-name name)
168 (lambda `(lambda ,@lambda-guts))
170 (named-lambda `(named-lambda ,name ,@lambda-guts))
172 (when (inline-fun-name-p name)
173 ;; we want to attempt to inline, so complain if we can't
174 (or (sb!c:maybe-inline-syntactic-closure lambda env)
177 #-sb-xc-host sb!c:maybe-compiler-notify
178 "lexical environment too hairy, can't inline DEFUN ~S"
182 ;; In cross-compilation of toplevel DEFUNs, we arrange for
183 ;; the LAMBDA to be statically linked by GENESIS.
185 ;; It may seem strangely inconsistent not to use NAMED-LAMBDA
186 ;; here instead of LAMBDA. The reason is historical:
187 ;; COLD-FSET was written before NAMED-LAMBDA, and has special
188 ;; logic of its own to notify the compiler about NAME.
190 (cold-fset ,name ,lambda)
192 (eval-when (:compile-toplevel)
193 (sb!c:%compiler-defun ',name ',inline-lambda t))
194 (eval-when (:load-toplevel :execute)
196 ;; In normal compilation (not for cold load) this is
197 ;; where the compiled LAMBDA first appears. In
198 ;; cross-compilation, we manipulate the
199 ;; previously-statically-linked LAMBDA here.
200 #-sb-xc-host ,named-lambda
201 #+sb-xc-host (fdefinition ',name)
203 ',inline-lambda))))))
206 (defun %defun (name def doc inline-lambda)
207 (declare (type function def))
208 (declare (type (or null simple-string) doc))
209 (aver (legal-fun-name-p name)) ; should've been checked by DEFMACRO DEFUN
210 (sb!c:%compiler-defun name inline-lambda nil)
212 (/show0 "redefining NAME in %DEFUN")
213 (style-warn "redefining ~S in DEFUN" name))
214 (setf (sb!xc:fdefinition name) def)
216 ;; FIXME: I want to do this here (and fix bug 137), but until the
217 ;; breathtaking CMU CL function name architecture is converted into
218 ;; something sane, (1) doing so doesn't really fix the bug, and
219 ;; (2) doing probably isn't even really safe.
220 #+nil (setf (%fun-name def) name)
223 (setf (fdocumentation name 'function) doc))
226 ;;;; DEFVAR and DEFPARAMETER
228 (defmacro-mundanely defvar (var &optional (val nil valp) (doc nil docp))
230 "Define a global variable at top level. Declare the variable
231 SPECIAL and, optionally, initialize it. If the variable already has a
232 value, the old value is not clobbered. The third argument is an optional
233 documentation string for the variable."
235 (eval-when (:compile-toplevel)
236 (%compiler-defvar ',var))
237 (eval-when (:load-toplevel :execute)
238 (%defvar ',var (unless (boundp ',var) ,val) ',valp ,doc ',docp))))
240 (defmacro-mundanely defparameter (var val &optional (doc nil docp))
242 "Define a parameter that is not normally changed by the program,
243 but that may be changed without causing an error. Declare the
244 variable special and sets its value to VAL, overwriting any
245 previous value. The third argument is an optional documentation
246 string for the parameter."
248 (eval-when (:compile-toplevel)
249 (%compiler-defvar ',var))
250 (eval-when (:load-toplevel :execute)
251 (%defparameter ',var ,val ,doc ',docp))))
253 (defun %compiler-defvar (var)
254 (sb!xc:proclaim `(special ,var)))
257 (defun %defvar (var val valp doc docp)
258 (%compiler-defvar var)
263 (setf (fdocumentation var 'variable) doc))
267 (defun %defparameter (var val doc docp)
268 (%compiler-defvar var)
271 (setf (fdocumentation var 'variable) doc))
274 ;;;; iteration constructs
276 ;;; (These macros are defined in terms of a function FROB-DO-BODY which
277 ;;; is also used by SB!INT:DO-ANONYMOUS. Since these macros should not
278 ;;; be loaded on the cross-compilation host, but SB!INT:DO-ANONYMOUS
279 ;;; and FROB-DO-BODY should be, these macros can't conveniently be in
280 ;;; the same file as FROB-DO-BODY.)
281 (defmacro-mundanely do (varlist endlist &body body)
283 "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
284 Iteration construct. Each Var is initialized in parallel to the value of the
285 specified Init form. On subsequent iterations, the Vars are assigned the
286 value of the Step form (if any) in parallel. The Test is evaluated before
287 each evaluation of the body Forms. When the Test is true, the Exit-Forms
288 are evaluated as a PROGN, with the result being the value of the DO. A block
289 named NIL is established around the entire expansion, allowing RETURN to be
290 used as an alternate exit mechanism."
291 (frob-do-body varlist endlist body 'let 'psetq 'do nil))
292 (defmacro-mundanely do* (varlist endlist &body body)
294 "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
295 Iteration construct. Each Var is initialized sequentially (like LET*) to the
296 value of the specified Init form. On subsequent iterations, the Vars are
297 sequentially assigned the value of the Step form (if any). The Test is
298 evaluated before each evaluation of the body Forms. When the Test is true,
299 the Exit-Forms are evaluated as a PROGN, with the result being the value
300 of the DO. A block named NIL is established around the entire expansion,
301 allowing RETURN to be used as an laternate exit mechanism."
302 (frob-do-body varlist endlist body 'let* 'setq 'do* nil))
304 ;;; DOTIMES and DOLIST could be defined more concisely using
305 ;;; destructuring macro lambda lists or DESTRUCTURING-BIND, but then
306 ;;; it'd be tricky to use them before those things were defined.
307 ;;; They're used enough times before destructuring mechanisms are
308 ;;; defined that it looks as though it's worth just implementing them
309 ;;; ASAP, at the cost of being unable to use the standard
310 ;;; destructuring mechanisms.
311 (defmacro-mundanely dotimes ((var count &optional (result nil)) &body body)
312 (cond ((numberp count)
313 `(do ((,var 0 (1+ ,var)))
314 ((>= ,var ,count) ,result)
315 (declare (type unsigned-byte ,var))
317 (t (let ((v1 (gensym)))
318 `(do ((,var 0 (1+ ,var)) (,v1 ,count))
319 ((>= ,var ,v1) ,result)
320 (declare (type unsigned-byte ,var))
323 (defun filter-dolist-declarations (decls)
324 (mapcar (lambda (decl)
325 `(declare ,@(remove-if
328 (or (eq (car clause) 'type)
329 (eq (car clause) 'ignore))))
333 (defmacro-mundanely dolist ((var list &optional (result nil)) &body body)
334 ;; We repeatedly bind the var instead of setting it so that we never
335 ;; have to give the var an arbitrary value such as NIL (which might
336 ;; conflict with a declaration). If there is a result form, we
337 ;; introduce a gratuitous binding of the variable to NIL without the
338 ;; declarations, then evaluate the result form in that
339 ;; environment. We spuriously reference the gratuitous variable,
340 ;; since we don't want to use IGNORABLE on what might be a special
342 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
343 (let ((n-list (gensym "N-LIST"))
344 (start (gensym "START")))
346 (let ((,n-list ,list))
349 (unless (endp ,n-list)
350 (let ((,var (car ,n-list)))
352 (setq ,n-list (cdr ,n-list))
357 ;; Filter out TYPE declarations (VAR gets bound to NIL,
358 ;; and might have a conflicting type declaration) and
359 ;; IGNORE (VAR might be ignored in the loop body, but
360 ;; it's used in the result form).
361 ,@(filter-dolist-declarations decls)
366 ;;;; conditions, handlers, restarts
368 ;;; KLUDGE: we PROCLAIM these special here so that we can use restart
369 ;;; macros in the compiler before the DEFVARs are compiled.
371 '(special *handler-clusters* *restart-clusters* *condition-restarts*))
373 (defmacro-mundanely with-condition-restarts
374 (condition-form restarts-form &body body)
376 "Evaluates the BODY in a dynamic environment where the restarts in the list
377 RESTARTS-FORM are associated with the condition returned by CONDITION-FORM.
378 This allows FIND-RESTART, etc., to recognize restarts that are not related
379 to the error currently being debugged. See also RESTART-CASE."
380 (let ((n-cond (gensym)))
381 `(let ((*condition-restarts*
382 (cons (let ((,n-cond ,condition-form))
384 (append ,restarts-form
385 (cdr (assoc ,n-cond *condition-restarts*)))))
386 *condition-restarts*)))
389 (defmacro-mundanely restart-bind (bindings &body forms)
391 "Executes forms in a dynamic context where the given restart bindings are
392 in effect. Users probably want to use RESTART-CASE. When clauses contain
393 the same restart name, FIND-RESTART will find the first such clause."
394 `(let ((*restart-clusters*
396 ,@(mapcar (lambda (binding)
397 (unless (or (car binding)
398 (member :report-function
401 (warn "Unnamed restart does not have a ~
404 `(make-restart :name ',(car binding)
405 :function ,(cadr binding)
408 *restart-clusters*)))
411 ;;; Wrap the RESTART-CASE expression in a WITH-CONDITION-RESTARTS if
412 ;;; appropriate. Gross, but it's what the book seems to say...
413 (defun munge-restart-case-expression (expression env)
414 (let ((exp (sb!xc:macroexpand expression env)))
416 (let* ((name (car exp))
417 (args (if (eq name 'cerror) (cddr exp) (cdr exp))))
418 (if (member name '(signal error cerror warn))
419 (once-only ((n-cond `(coerce-to-condition
423 (warn 'simple-warning)
424 (signal 'simple-condition)
427 `(with-condition-restarts
429 (car *restart-clusters*)
430 ,(if (eq name 'cerror)
431 `(cerror ,(second exp) ,n-cond)
436 ;;; FIXME: I did a fair amount of rearrangement of this code in order to
437 ;;; get WITH-KEYWORD-PAIRS to work cleanly. This code should be tested..
438 (defmacro-mundanely restart-case (expression &body clauses &environment env)
441 {(case-name arg-list {keyword value}* body)}*)
442 The form is evaluated in a dynamic context where the clauses have special
443 meanings as points to which control may be transferred (see INVOKE-RESTART).
444 When clauses contain the same case-name, FIND-RESTART will find the first
445 such clause. If Expression is a call to SIGNAL, ERROR, CERROR or WARN (or
446 macroexpands into such) then the signalled condition will be associated with
448 (flet ((transform-keywords (&key report interactive test)
451 (setq result (list* (if (stringp report)
453 (write-string ,report stream))
458 (setq result (list* `#',interactive
459 :interactive-function
462 (setq result (list* `#',test :test-function result)))
464 (parse-keyword-pairs (list keys)
465 (do ((l list (cddr l))
466 (k '() (list* (cadr l) (car l) k)))
467 ((or (null l) (not (member (car l) keys)))
468 (values (nreverse k) l)))))
469 (let ((block-tag (gensym))
472 (macrolet (;; KLUDGE: This started as an old DEFMACRO
473 ;; WITH-KEYWORD-PAIRS general utility, which was used
474 ;; only in this one place in the code. It was translated
475 ;; literally into this MACROLET in order to avoid some
476 ;; cross-compilation bootstrap problems. It would almost
477 ;; certainly be clearer, and it would certainly be more
478 ;; concise, to do a more idiomatic translation, merging
479 ;; this with the TRANSFORM-KEYWORDS logic above.
481 (with-keyword-pairs ((names expression) &body forms)
482 (let ((temp (member '&rest names)))
483 (unless (= (length temp) 2)
484 (error "&REST keyword is ~:[missing~;misplaced~]."
486 (let* ((key-vars (ldiff names temp))
487 (keywords (mapcar #'keywordicate key-vars))
489 (rest-var (cadr temp)))
490 `(multiple-value-bind (,key-var ,rest-var)
491 (parse-keyword-pairs ,expression ',keywords)
492 (let ,(mapcar (lambda (var keyword)
493 `(,var (getf ,key-var
497 (mapcar (lambda (clause)
498 (with-keyword-pairs ((report interactive test
501 (list (car clause) ;name=0
503 (transform-keywords :report report ;keywords=2
504 :interactive interactive
510 (let ((,temp-var nil))
513 ,(mapcar (lambda (datum)
514 (let ((name (nth 0 datum))
516 (keys (nth 2 datum)))
517 `(,name #'(lambda (&rest temp)
518 (setq ,temp-var temp)
522 (return-from ,block-tag
523 ,(munge-restart-case-expression expression env)))
524 ,@(mapcan (lambda (datum)
525 (let ((tag (nth 1 datum))
527 (body (nth 4 datum)))
529 `(return-from ,block-tag
530 (apply (lambda ,bvl ,@body)
534 (defmacro-mundanely with-simple-restart ((restart-name format-string
535 &rest format-arguments)
538 "(WITH-SIMPLE-RESTART (restart-name format-string format-arguments)
540 If restart-name is not invoked, then all values returned by forms are
541 returned. If control is transferred to this restart, it immediately
542 returns the values NIL and T."
544 ;; If there's just one body form, then don't use PROGN. This allows
545 ;; RESTART-CASE to "see" calls to ERROR, etc.
546 ,(if (= (length forms) 1) (car forms) `(progn ,@forms))
548 :report (lambda (stream)
549 (format stream ,format-string ,@format-arguments))
552 (defmacro-mundanely handler-bind (bindings &body forms)
554 "(HANDLER-BIND ( {(type handler)}* ) body)
555 Executes body in a dynamic context where the given handler bindings are
556 in effect. Each handler must take the condition being signalled as an
557 argument. The bindings are searched first to last in the event of a
558 signalled condition."
559 (let ((member-if (member-if (lambda (x)
560 (not (proper-list-of-length-p x 2)))
563 (error "ill-formed handler binding: ~S" (first member-if))))
564 `(let ((*handler-clusters*
565 (cons (list ,@(mapcar (lambda (x) `(cons ',(car x) ,(cadr x)))
567 *handler-clusters*)))
568 (multiple-value-prog1
571 ;; Wait for any float exceptions.
572 #!+x86 (float-wait))))
574 (defmacro-mundanely handler-case (form &rest cases)
576 { (type ([var]) body) }* )
577 Execute FORM in a context with handlers established for the condition
578 types. A peculiar property allows type to be :NO-ERROR. If such a clause
579 occurs, and form returns normally, all its values are passed to this clause
580 as if by MULTIPLE-VALUE-CALL. The :NO-ERROR clause accepts more than one
582 ;; FIXME: Replacing CADR, CDDDR and friends with DESTRUCTURING-BIND
583 ;; and names for the subexpressions would make it easier to
584 ;; understand the code below.
585 (let ((no-error-clause (assoc ':no-error cases)))
587 (let ((normal-return (make-symbol "normal-return"))
588 (error-return (make-symbol "error-return")))
589 `(block ,error-return
590 (multiple-value-call (lambda ,@(cdr no-error-clause))
591 (block ,normal-return
592 (return-from ,error-return
593 (handler-case (return-from ,normal-return ,form)
594 ,@(remove no-error-clause cases)))))))
597 (annotated-cases (mapcar (lambda (case) (cons (gensym) case))
601 (declare (ignorable ,var))
604 ,(mapcar (lambda (annotated-case)
605 (list (cadr annotated-case)
607 ,(if (caddr annotated-case)
609 '(declare (ignore temp)))
610 (go ,(car annotated-case)))))
614 #!+x86 (multiple-value-prog1 ,form
615 ;; Need to catch FP errors here!
618 (lambda (annotated-case)
619 (list (car annotated-case)
620 (let ((body (cdddr annotated-case)))
623 ,(cond ((caddr annotated-case)
624 `(let ((,(caaddr annotated-case)
628 `(locally ,@body)))))))
629 annotated-cases))))))))
633 (defmacro-mundanely return (&optional (value nil))
634 `(return-from nil ,value))
636 (defmacro-mundanely psetq (&rest pairs)
639 Set the variables to the values, like SETQ, except that assignments
640 happen in parallel, i.e. no assignments take place until all the
641 forms have been evaluated."
642 ;; Given the possibility of symbol-macros, we delegate to PSETF
643 ;; which knows how to deal with them, after checking that syntax is
644 ;; compatible with PSETQ.
645 (do ((pair pairs (cddr pair)))
646 ((endp pair) `(psetf ,@pairs))
647 (unless (symbolp (car pair))
648 (error 'simple-program-error
649 :format-control "variable ~S in PSETQ is not a SYMBOL"
650 :format-arguments (list (car pair))))))
652 (defmacro-mundanely lambda (&whole whole args &body body)
653 (declare (ignore args body))
656 (defmacro-mundanely named-lambda (&whole whole name args &body body)
657 (declare (ignore name args body))
660 (defmacro-mundanely lambda-with-lexenv (&whole whole
661 declarations macros symbol-macros
663 (declare (ignore declarations macros symbol-macros body))
666 ;;; this eliminates a whole bundle of unknown function STYLE-WARNINGs
667 ;;; when cross-compiling. It's not critical for behaviour, but is
668 ;;; aesthetically pleasing, except inasmuch as there's this list of
669 ;;; magic functions here. -- CSR, 2003-04-01
671 (sb!xc:proclaim '(ftype (function * *)
672 ;; functions appearing in fundamental defining
681 sb!c::%define-symbol-macro
683 sb!c::%define-compiler-macro
685 sb!kernel::%compiler-defstruct
686 sb!kernel::%compiler-define-condition
687 sb!kernel::%defstruct
688 sb!kernel::%define-condition
689 ;; miscellaneous functions commonly appearing
690 ;; as a result of macro expansions or compiler
692 sb!int:find-undeleted-package-or-lose ; IN-PACKAGE
693 sb!kernel::arg-count-error ; PARSE-DEFMACRO