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 `(values (setf (values ,@vars) ,value-form)))
55 (defmacro-mundanely multiple-value-list (value-form)
56 `(multiple-value-call #'list ,value-form))
58 ;;;; various conditional constructs
60 ;;; COND defined in terms of IF
61 (defmacro-mundanely cond (&rest clauses)
64 (let ((clause (first clauses)))
66 (error "COND clause is not a list: ~S" clause)
67 (let ((test (first clause))
68 (forms (rest clause)))
70 (let ((n-result (gensym)))
71 `(let ((,n-result ,test))
74 (cond ,@(rest clauses)))))
77 (cond ,@(rest clauses)))))))))
79 ;;; other things defined in terms of COND
80 (defmacro-mundanely when (test &body forms)
82 "If the first argument is true, the rest of the forms are
83 evaluated as a PROGN."
84 `(cond (,test nil ,@forms)))
85 (defmacro-mundanely unless (test &body forms)
87 "If the first argument is not true, the rest of the forms are
88 evaluated as a PROGN."
89 `(cond ((not ,test) nil ,@forms)))
90 (defmacro-mundanely and (&rest forms)
91 (cond ((endp forms) t)
92 ((endp (rest forms)) (first forms))
97 (defmacro-mundanely or (&rest forms)
98 (cond ((endp forms) nil)
99 ((endp (rest forms)) (first forms))
101 (let ((n-result (gensym)))
102 `(let ((,n-result ,(first forms)))
105 (or ,@(rest forms))))))))
107 ;;;; various sequencing constructs
109 (flet ((prog-expansion-from-let (varlist body-decls let)
110 (multiple-value-bind (body decls)
111 (parse-body body-decls :doc-string-allowed nil)
115 (tagbody ,@body))))))
116 (defmacro-mundanely prog (varlist &body body-decls)
117 (prog-expansion-from-let varlist body-decls 'let))
118 (defmacro-mundanely prog* (varlist &body body-decls)
119 (prog-expansion-from-let varlist body-decls 'let*)))
121 (defmacro-mundanely prog1 (result &body body)
122 (let ((n-result (gensym)))
123 `(let ((,n-result ,result))
127 (defmacro-mundanely prog2 (form1 result &body body)
128 `(prog1 (progn ,form1 ,result) ,@body))
132 ;;; Should we save the inline expansion of the function named NAME?
133 (defun inline-fun-name-p (name)
135 ;; the normal reason for saving the inline expansion
136 (info :function :inlinep name)
137 ;; another reason for saving the inline expansion: If the
138 ;; ANSI-recommended idiom
139 ;; (DECLAIM (INLINE FOO))
141 ;; (DECLAIM (NOTINLINE FOO))
142 ;; has been used, and then we later do another
144 ;; without a preceding
145 ;; (DECLAIM (INLINE FOO))
146 ;; what should we do with the old inline expansion when we see the
147 ;; new DEFUN? Overwriting it with the new definition seems like
148 ;; the only unsurprising choice.
149 (info :function :inline-expansion-designator name)))
151 (defmacro-mundanely defun (&environment env name args &body body)
152 "Define a function at top level."
154 (unless (symbol-package (fun-name-block-name name))
155 (warn "DEFUN of uninterned function name ~S (tricky for GENESIS)" name))
156 (multiple-value-bind (forms decls doc) (parse-body body)
157 (let* (;; stuff shared between LAMBDA and INLINE-LAMBDA and NAMED-LAMBDA
160 (block ,(fun-name-block-name name)
162 (lambda `(lambda ,@lambda-guts))
164 (named-lambda `(named-lambda ,name ,@lambda-guts))
166 (when (inline-fun-name-p name)
167 ;; we want to attempt to inline, so complain if we can't
168 (or (sb!c:maybe-inline-syntactic-closure lambda env)
171 #-sb-xc-host sb!c:maybe-compiler-notify
172 "lexical environment too hairy, can't inline DEFUN ~S"
176 ;; In cross-compilation of toplevel DEFUNs, we arrange for
177 ;; the LAMBDA to be statically linked by GENESIS.
179 ;; It may seem strangely inconsistent not to use NAMED-LAMBDA
180 ;; here instead of LAMBDA. The reason is historical:
181 ;; COLD-FSET was written before NAMED-LAMBDA, and has special
182 ;; logic of its own to notify the compiler about NAME.
184 (cold-fset ,name ,lambda)
186 (eval-when (:compile-toplevel)
187 (sb!c:%compiler-defun ',name ',inline-lambda t))
188 (eval-when (:load-toplevel :execute)
190 ;; In normal compilation (not for cold load) this is
191 ;; where the compiled LAMBDA first appears. In
192 ;; cross-compilation, we manipulate the
193 ;; previously-statically-linked LAMBDA here.
194 #-sb-xc-host ,named-lambda
195 #+sb-xc-host (fdefinition ',name)
197 ',inline-lambda))))))
200 (defun %defun (name def doc inline-lambda)
201 (declare (type function def))
202 (declare (type (or null simple-string) doc))
203 (aver (legal-fun-name-p name)) ; should've been checked by DEFMACRO DEFUN
204 (sb!c:%compiler-defun name inline-lambda nil)
206 (/show0 "redefining NAME in %DEFUN")
207 (style-warn "redefining ~S in DEFUN" name))
208 (setf (sb!xc:fdefinition name) def)
210 ;; FIXME: I want to do this here (and fix bug 137), but until the
211 ;; breathtaking CMU CL function name architecture is converted into
212 ;; something sane, (1) doing so doesn't really fix the bug, and
213 ;; (2) doing probably isn't even really safe.
214 #+nil (setf (%fun-name def) name)
217 (setf (fdocumentation name 'function) doc))
220 ;;;; DEFVAR and DEFPARAMETER
222 (defmacro-mundanely defvar (var &optional (val nil valp) (doc nil docp))
224 "Define a global variable at top level. Declare the variable
225 SPECIAL and, optionally, initialize it. If the variable already has a
226 value, the old value is not clobbered. The third argument is an optional
227 documentation string for the variable."
229 (eval-when (:compile-toplevel)
230 (%compiler-defvar ',var))
231 (eval-when (:load-toplevel :execute)
232 (%defvar ',var (unless (boundp ',var) ,val) ',valp ,doc ',docp))))
234 (defmacro-mundanely defparameter (var val &optional (doc nil docp))
236 "Define a parameter that is not normally changed by the program,
237 but that may be changed without causing an error. Declare the
238 variable special and sets its value to VAL, overwriting any
239 previous value. The third argument is an optional documentation
240 string for the parameter."
242 (eval-when (:compile-toplevel)
243 (%compiler-defvar ',var))
244 (eval-when (:load-toplevel :execute)
245 (%defparameter ',var ,val ,doc ',docp))))
247 (defun %compiler-defvar (var)
248 (sb!xc:proclaim `(special ,var)))
251 (defun %defvar (var val valp doc docp)
252 (%compiler-defvar var)
257 (setf (fdocumentation var 'variable) doc))
261 (defun %defparameter (var val doc docp)
262 (%compiler-defvar var)
265 (setf (fdocumentation var 'variable) doc))
268 ;;;; iteration constructs
270 ;;; (These macros are defined in terms of a function FROB-DO-BODY which
271 ;;; is also used by SB!INT:DO-ANONYMOUS. Since these macros should not
272 ;;; be loaded on the cross-compilation host, but SB!INT:DO-ANONYMOUS
273 ;;; and FROB-DO-BODY should be, these macros can't conveniently be in
274 ;;; the same file as FROB-DO-BODY.)
275 (defmacro-mundanely do (varlist endlist &body body)
277 "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
278 Iteration construct. Each Var is initialized in parallel to the value of the
279 specified Init form. On subsequent iterations, the Vars are assigned the
280 value of the Step form (if any) in parallel. The Test is evaluated before
281 each evaluation of the body Forms. When the Test is true, the Exit-Forms
282 are evaluated as a PROGN, with the result being the value of the DO. A block
283 named NIL is established around the entire expansion, allowing RETURN to be
284 used as an alternate exit mechanism."
285 (frob-do-body varlist endlist body 'let 'psetq 'do nil))
286 (defmacro-mundanely do* (varlist endlist &body body)
288 "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
289 Iteration construct. Each Var is initialized sequentially (like LET*) to the
290 value of the specified Init form. On subsequent iterations, the Vars are
291 sequentially assigned the value of the Step form (if any). The Test is
292 evaluated before each evaluation of the body Forms. When the Test is true,
293 the Exit-Forms are evaluated as a PROGN, with the result being the value
294 of the DO. A block named NIL is established around the entire expansion,
295 allowing RETURN to be used as an laternate exit mechanism."
296 (frob-do-body varlist endlist body 'let* 'setq 'do* nil))
298 ;;; DOTIMES and DOLIST could be defined more concisely using
299 ;;; destructuring macro lambda lists or DESTRUCTURING-BIND, but then
300 ;;; it'd be tricky to use them before those things were defined.
301 ;;; They're used enough times before destructuring mechanisms are
302 ;;; defined that it looks as though it's worth just implementing them
303 ;;; ASAP, at the cost of being unable to use the standard
304 ;;; destructuring mechanisms.
305 (defmacro-mundanely dotimes ((var count &optional (result nil)) &body body)
306 (cond ((numberp count)
307 `(do ((,var 0 (1+ ,var)))
308 ((>= ,var ,count) ,result)
309 (declare (type unsigned-byte ,var))
311 (t (let ((v1 (gensym)))
312 `(do ((,var 0 (1+ ,var)) (,v1 ,count))
313 ((>= ,var ,v1) ,result)
314 (declare (type unsigned-byte ,var))
317 (defmacro-mundanely dolist ((var list &optional (result nil)) &body body)
318 ;; We repeatedly bind the var instead of setting it so that we never
319 ;; have to give the var an arbitrary value such as NIL (which might
320 ;; conflict with a declaration). If there is a result form, we
321 ;; introduce a gratuitous binding of the variable to NIL without the
322 ;; declarations, then evaluate the result form in that
323 ;; environment. We spuriously reference the gratuitous variable,
324 ;; since we don't want to use IGNORABLE on what might be a special
326 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
327 (let ((n-list (gensym "N-LIST"))
328 (start (gensym "START")))
330 (let ((,n-list ,list))
333 (unless (endp ,n-list)
334 (let ((,var (car ,n-list)))
336 (setq ,n-list (cdr ,n-list))
345 ;;;; conditions, handlers, restarts
347 ;;; KLUDGE: we PROCLAIM these special here so that we can use restart
348 ;;; macros in the compiler before the DEFVARs are compiled.
350 '(special *handler-clusters* *restart-clusters* *condition-restarts*))
352 (defmacro-mundanely with-condition-restarts
353 (condition-form restarts-form &body body)
355 "WITH-CONDITION-RESTARTS Condition-Form Restarts-Form Form*
356 Evaluates the Forms in a dynamic environment where the restarts in the list
357 Restarts-Form are associated with the condition returned by Condition-Form.
358 This allows FIND-RESTART, etc., to recognize restarts that are not related
359 to the error currently being debugged. See also RESTART-CASE."
360 (let ((n-cond (gensym)))
361 `(let ((*condition-restarts*
362 (cons (let ((,n-cond ,condition-form))
364 (append ,restarts-form
365 (cdr (assoc ,n-cond *condition-restarts*)))))
366 *condition-restarts*)))
369 (defmacro-mundanely restart-bind (bindings &body forms)
371 "Executes forms in a dynamic context where the given restart bindings are
372 in effect. Users probably want to use RESTART-CASE. When clauses contain
373 the same restart name, FIND-RESTART will find the first such clause."
374 `(let ((*restart-clusters*
376 ,@(mapcar (lambda (binding)
377 (unless (or (car binding)
378 (member :report-function
381 (warn "Unnamed restart does not have a ~
384 `(make-restart :name ',(car binding)
385 :function ,(cadr binding)
388 *restart-clusters*)))
391 ;;; Wrap the RESTART-CASE expression in a WITH-CONDITION-RESTARTS if
392 ;;; appropriate. Gross, but it's what the book seems to say...
393 (defun munge-restart-case-expression (expression env)
394 (let ((exp (sb!xc:macroexpand expression env)))
396 (let* ((name (car exp))
397 (args (if (eq name 'cerror) (cddr exp) (cdr exp))))
398 (if (member name '(signal error cerror warn))
399 (once-only ((n-cond `(coerce-to-condition
403 (warn 'simple-warning)
404 (signal 'simple-condition)
407 `(with-condition-restarts
409 (car *restart-clusters*)
410 ,(if (eq name 'cerror)
411 `(cerror ,(second exp) ,n-cond)
416 ;;; FIXME: I did a fair amount of rearrangement of this code in order to
417 ;;; get WITH-KEYWORD-PAIRS to work cleanly. This code should be tested..
418 (defmacro-mundanely restart-case (expression &body clauses &environment env)
421 {(case-name arg-list {keyword value}* body)}*)
422 The form is evaluated in a dynamic context where the clauses have special
423 meanings as points to which control may be transferred (see INVOKE-RESTART).
424 When clauses contain the same case-name, FIND-RESTART will find the first
425 such clause. If Expression is a call to SIGNAL, ERROR, CERROR or WARN (or
426 macroexpands into such) then the signalled condition will be associated with
428 (flet ((transform-keywords (&key report interactive test)
431 (setq result (list* (if (stringp report)
433 (write-string ,report stream))
438 (setq result (list* `#',interactive
439 :interactive-function
442 (setq result (list* `#',test :test-function result)))
444 (parse-keyword-pairs (list keys)
445 (do ((l list (cddr l))
446 (k '() (list* (cadr l) (car l) k)))
447 ((or (null l) (not (member (car l) keys)))
448 (values (nreverse k) l)))))
449 (let ((block-tag (gensym))
452 (macrolet (;; KLUDGE: This started as an old DEFMACRO
453 ;; WITH-KEYWORD-PAIRS general utility, which was used
454 ;; only in this one place in the code. It was translated
455 ;; literally into this MACROLET in order to avoid some
456 ;; cross-compilation bootstrap problems. It would almost
457 ;; certainly be clearer, and it would certainly be more
458 ;; concise, to do a more idiomatic translation, merging
459 ;; this with the TRANSFORM-KEYWORDS logic above.
461 (with-keyword-pairs ((names expression) &body forms)
462 (let ((temp (member '&rest names)))
463 (unless (= (length temp) 2)
464 (error "&REST keyword is ~:[missing~;misplaced~]."
466 (let* ((key-vars (ldiff names temp))
467 (keywords (mapcar #'keywordicate key-vars))
469 (rest-var (cadr temp)))
470 `(multiple-value-bind (,key-var ,rest-var)
471 (parse-keyword-pairs ,expression ',keywords)
472 (let ,(mapcar (lambda (var keyword)
473 `(,var (getf ,key-var
477 (mapcar (lambda (clause)
478 (with-keyword-pairs ((report interactive test
481 (list (car clause) ;name=0
483 (transform-keywords :report report ;keywords=2
484 :interactive interactive
490 (let ((,temp-var nil))
493 ,(mapcar (lambda (datum)
494 (let ((name (nth 0 datum))
496 (keys (nth 2 datum)))
497 `(,name #'(lambda (&rest temp)
498 (setq ,temp-var temp)
502 (return-from ,block-tag
503 ,(munge-restart-case-expression expression env)))
504 ,@(mapcan (lambda (datum)
505 (let ((tag (nth 1 datum))
507 (body (nth 4 datum)))
509 `(return-from ,block-tag
510 (apply (lambda ,bvl ,@body)
514 (defmacro-mundanely with-simple-restart ((restart-name format-string
515 &rest format-arguments)
518 "(WITH-SIMPLE-RESTART (restart-name format-string format-arguments)
520 If restart-name is not invoked, then all values returned by forms are
521 returned. If control is transferred to this restart, it immediately
522 returns the values NIL and T."
524 ;; If there's just one body form, then don't use PROGN. This allows
525 ;; RESTART-CASE to "see" calls to ERROR, etc.
526 ,(if (= (length forms) 1) (car forms) `(progn ,@forms))
528 :report (lambda (stream)
529 (format stream ,format-string ,@format-arguments))
532 (defmacro-mundanely handler-bind (bindings &body forms)
534 "(HANDLER-BIND ( {(type handler)}* ) body)
535 Executes body in a dynamic context where the given handler bindings are
536 in effect. Each handler must take the condition being signalled as an
537 argument. The bindings are searched first to last in the event of a
538 signalled condition."
539 (let ((member-if (member-if (lambda (x)
540 (not (proper-list-of-length-p x 2)))
543 (error "ill-formed handler binding: ~S" (first member-if))))
544 `(let ((*handler-clusters*
545 (cons (list ,@(mapcar (lambda (x) `(cons ',(car x) ,(cadr x)))
547 *handler-clusters*)))
548 (multiple-value-prog1
551 ;; Wait for any float exceptions.
552 #!+x86 (float-wait))))
554 (defmacro-mundanely handler-case (form &rest cases)
556 { (type ([var]) body) }* )
557 Execute FORM in a context with handlers established for the condition
558 types. A peculiar property allows type to be :NO-ERROR. If such a clause
559 occurs, and form returns normally, all its values are passed to this clause
560 as if by MULTIPLE-VALUE-CALL. The :NO-ERROR clause accepts more than one
562 ;; FIXME: Replacing CADR, CDDDR and friends with DESTRUCTURING-BIND
563 ;; and names for the subexpressions would make it easier to
564 ;; understand the code below.
565 (let ((no-error-clause (assoc ':no-error cases)))
567 (let ((normal-return (make-symbol "normal-return"))
568 (error-return (make-symbol "error-return")))
569 `(block ,error-return
570 (multiple-value-call (lambda ,@(cdr no-error-clause))
571 (block ,normal-return
572 (return-from ,error-return
573 (handler-case (return-from ,normal-return ,form)
574 ,@(remove no-error-clause cases)))))))
577 (annotated-cases (mapcar (lambda (case) (cons (gensym) case))
581 (declare (ignorable ,var))
584 ,(mapcar (lambda (annotated-case)
585 (list (cadr annotated-case)
587 ,(if (caddr annotated-case)
589 '(declare (ignore temp)))
590 (go ,(car annotated-case)))))
594 #!+x86 (multiple-value-prog1 ,form
595 ;; Need to catch FP errors here!
598 (lambda (annotated-case)
599 (list (car annotated-case)
600 (let ((body (cdddr annotated-case)))
603 ,(cond ((caddr annotated-case)
604 `(let ((,(caaddr annotated-case)
608 `(locally ,@body)))))))
609 annotated-cases))))))))
613 (defmacro-mundanely return (&optional (value nil))
614 `(return-from nil ,value))
616 (defmacro-mundanely psetq (&rest pairs)
619 Set the variables to the values, like SETQ, except that assignments
620 happen in parallel, i.e. no assignments take place until all the
621 forms have been evaluated."
622 ;; Given the possibility of symbol-macros, we delegate to PSETF
623 ;; which knows how to deal with them, after checking that syntax is
624 ;; compatible with PSETQ.
625 (do ((pair pairs (cddr pair)))
626 ((endp pair) `(psetf ,@pairs))
627 (unless (symbolp (car pair))
628 (error 'simple-program-error
629 :format-control "variable ~S in PSETQ is not a SYMBOL"
630 :format-arguments (list (car pair))))))
632 (defmacro-mundanely lambda (&whole whole args &body body)
633 (declare (ignore args body))
636 (defmacro-mundanely named-lambda (&whole whole name args &body body)
637 (declare (ignore name args body))
640 (defmacro-mundanely lambda-with-lexenv (&whole whole
641 declarations macros symbol-macros
643 (declare (ignore declarations macros symbol-macros body))
646 ;;; this eliminates a whole bundle of unknown function STYLE-WARNINGs
647 ;;; when cross-compiling. It's not critical for behaviour, but is
648 ;;; aesthetically pleasing, except inasmuch as there's this list of
649 ;;; magic functions here. -- CSR, 2003-04-01
651 (sb!xc:proclaim '(ftype (function * *)
652 ;; functions appearing in fundamental defining
661 sb!c::%define-symbol-macro
663 sb!c::%define-compiler-macro
665 sb!kernel::%compiler-defstruct
666 sb!kernel::%compiler-define-condition
667 sb!kernel::%defstruct
668 sb!kernel::%define-condition
669 ;; miscellaneous functions commonly appearing
670 ;; as a result of macro expansions or compiler
672 sb!int:find-undeleted-package-or-lose ; IN-PACKAGE
673 sb!kernel::arg-count-error ; PARSE-DEFMACRO