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 ,@vars &rest ,ignore)
44 (declare (ignore ,ignore))
47 (error "Vars is not a list of symbols: ~S" vars)))
49 (defmacro-mundanely multiple-value-setq (vars value-form)
50 (unless (list-of-symbols-p vars)
51 (error "Vars is not a list of symbols: ~S" vars))
52 `(values (setf (values ,@vars) ,value-form)))
54 (defmacro-mundanely multiple-value-list (value-form)
55 `(multiple-value-call #'list ,value-form))
57 ;;;; various conditional constructs
59 ;;; COND defined in terms of IF
60 (defmacro-mundanely cond (&rest clauses)
63 (let ((clause (first clauses)))
65 (error "COND clause is not a list: ~S" clause)
66 (let ((test (first clause))
67 (forms (rest clause)))
69 (let ((n-result (gensym)))
70 `(let ((,n-result ,test))
73 (cond ,@(rest clauses)))))
76 (cond ,@(rest clauses)))))))))
78 ;;; other things defined in terms of COND
79 (defmacro-mundanely when (test &body forms)
81 "If the first argument is true, the rest of the forms are
82 evaluated as a PROGN."
83 `(cond (,test nil ,@forms)))
84 (defmacro-mundanely unless (test &body forms)
86 "If the first argument is not true, the rest of the forms are
87 evaluated as a PROGN."
88 `(cond ((not ,test) nil ,@forms)))
89 (defmacro-mundanely and (&rest forms)
90 (cond ((endp forms) t)
91 ((endp (rest forms)) (first forms))
96 (defmacro-mundanely or (&rest forms)
97 (cond ((endp forms) nil)
98 ((endp (rest forms)) (first forms))
100 (let ((n-result (gensym)))
101 `(let ((,n-result ,(first forms)))
104 (or ,@(rest forms))))))))
106 ;;;; various sequencing constructs
108 (flet ((prog-expansion-from-let (varlist body-decls let)
109 (multiple-value-bind (body decls)
110 (parse-body body-decls :doc-string-allowed nil)
114 (tagbody ,@body))))))
115 (defmacro-mundanely prog (varlist &body body-decls)
116 (prog-expansion-from-let varlist body-decls 'let))
117 (defmacro-mundanely prog* (varlist &body body-decls)
118 (prog-expansion-from-let varlist body-decls 'let*)))
120 (defmacro-mundanely prog1 (result &body body)
121 (let ((n-result (gensym)))
122 `(let ((,n-result ,result))
126 (defmacro-mundanely prog2 (form1 result &body body)
127 `(prog1 (progn ,form1 ,result) ,@body))
131 ;;; Should we save the inline expansion of the function named NAME?
132 (defun inline-fun-name-p (name)
134 ;; the normal reason for saving the inline expansion
135 (info :function :inlinep name)
136 ;; another reason for saving the inline expansion: If the
137 ;; ANSI-recommended idiom
138 ;; (DECLAIM (INLINE FOO))
140 ;; (DECLAIM (NOTINLINE FOO))
141 ;; has been used, and then we later do another
143 ;; without a preceding
144 ;; (DECLAIM (INLINE FOO))
145 ;; what should we do with the old inline expansion when we see the
146 ;; new DEFUN? Overwriting it with the new definition seems like
147 ;; the only unsurprising choice.
148 (info :function :inline-expansion-designator name)))
150 (defmacro-mundanely defun (&environment env name args &body body)
151 "Define a function at top level."
153 (unless (symbol-package (fun-name-block-name name))
154 (warn "DEFUN of uninterned symbol ~S (tricky for GENESIS)" name))
155 (multiple-value-bind (forms decls doc) (parse-body body)
156 (let* (;; stuff shared between LAMBDA and INLINE-LAMBDA and NAMED-LAMBDA
159 (block ,(fun-name-block-name name)
161 (lambda `(lambda ,@lambda-guts))
163 (named-lambda `(named-lambda ,name ,@lambda-guts))
165 (when (inline-fun-name-p name)
166 ;; we want to attempt to inline, so complain if we can't
167 (or (sb!c:maybe-inline-syntactic-closure lambda env)
170 #-sb-xc-host sb!c:maybe-compiler-notify
171 "lexical environment too hairy, can't inline DEFUN ~S"
176 ;; In cross-compilation of toplevel DEFUNs, we arrange
177 ;; for 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 :load-toplevel :execute)
187 (sb!c:%compiler-defun ',name ',inline-lambda))
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)
198 (defun %defun (name def doc)
199 (declare (type function def))
200 (declare (type (or null simple-string) doc))
201 (aver (legal-fun-name-p name)) ; should've been checked by DEFMACRO DEFUN
203 (/show0 "redefining NAME in %DEFUN")
204 (style-warn "redefining ~S in DEFUN" name))
205 (setf (sb!xc:fdefinition name) def)
207 ;; FIXME: I want to do this here (and fix bug 137), but until the
208 ;; breathtaking CMU CL function name architecture is converted into
209 ;; something sane, (1) doing so doesn't really fix the bug, and
210 ;; (2) doing probably isn't even really safe.
211 #+nil (setf (%fun-name def) name)
214 (setf (fdocumentation name 'function) doc))
217 ;;;; DEFVAR and DEFPARAMETER
219 (defmacro-mundanely defvar (var &optional (val nil valp) (doc nil docp))
221 "Define a global variable at top level. Declare the variable
222 SPECIAL and, optionally, initialize it. If the variable already has a
223 value, the old value is not clobbered. The third argument is an optional
224 documentation string for the variable."
226 (declaim (special ,var))
228 `((unless (boundp ',var)
231 `((setf (fdocumentation ',var 'variable) ',doc )))
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 (declaim (special ,var))
245 `((setf (fdocumentation ',var 'variable) ',doc)))
248 ;;;; iteration constructs
250 ;;; (These macros are defined in terms of a function FROB-DO-BODY which
251 ;;; is also used by SB!INT:DO-ANONYMOUS. Since these macros should not
252 ;;; be loaded on the cross-compilation host, but SB!INT:DO-ANONYMOUS
253 ;;; and FROB-DO-BODY should be, these macros can't conveniently be in
254 ;;; the same file as FROB-DO-BODY.)
255 (defmacro-mundanely do (varlist endlist &body body)
257 "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
258 Iteration construct. Each Var is initialized in parallel to the value of the
259 specified Init form. On subsequent iterations, the Vars are assigned the
260 value of the Step form (if any) in parallel. The Test is evaluated before
261 each evaluation of the body Forms. When the Test is true, the Exit-Forms
262 are evaluated as a PROGN, with the result being the value of the DO. A block
263 named NIL is established around the entire expansion, allowing RETURN to be
264 used as an alternate exit mechanism."
265 (frob-do-body varlist endlist body 'let 'psetq 'do nil))
266 (defmacro-mundanely do* (varlist endlist &body body)
268 "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
269 Iteration construct. Each Var is initialized sequentially (like LET*) to the
270 value of the specified Init form. On subsequent iterations, the Vars are
271 sequentially assigned the value of the Step form (if any). The Test is
272 evaluated before each evaluation of the body Forms. When the Test is true,
273 the Exit-Forms are evaluated as a PROGN, with the result being the value
274 of the DO. A block named NIL is established around the entire expansion,
275 allowing RETURN to be used as an laternate exit mechanism."
276 (frob-do-body varlist endlist body 'let* 'setq 'do* nil))
278 ;;; DOTIMES and DOLIST could be defined more concisely using
279 ;;; destructuring macro lambda lists or DESTRUCTURING-BIND, but then
280 ;;; it'd be tricky to use them before those things were defined.
281 ;;; They're used enough times before destructuring mechanisms are
282 ;;; defined that it looks as though it's worth just implementing them
283 ;;; ASAP, at the cost of being unable to use the standard
284 ;;; destructuring mechanisms.
285 (defmacro-mundanely dotimes ((var count &optional (result nil)) &body body)
286 (cond ((numberp count)
287 `(do ((,var 0 (1+ ,var)))
288 ((>= ,var ,count) ,result)
289 (declare (type unsigned-byte ,var))
291 (t (let ((v1 (gensym)))
292 `(do ((,var 0 (1+ ,var)) (,v1 ,count))
293 ((>= ,var ,v1) ,result)
294 (declare (type unsigned-byte ,var))
297 (defmacro-mundanely dolist ((var list &optional (result nil)) &body body)
298 ;; We repeatedly bind the var instead of setting it so that we never
299 ;; have to give the var an arbitrary value such as NIL (which might
300 ;; conflict with a declaration). If there is a result form, we
301 ;; introduce a gratuitous binding of the variable to NIL without the
302 ;; declarations, then evaluate the result form in that
303 ;; environment. We spuriously reference the gratuitous variable,
304 ;; since we don't want to use IGNORABLE on what might be a special
306 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
307 (let ((n-list (gensym)))
308 `(do* ((,n-list ,list (cdr ,n-list)))
315 (let ((,var (car ,n-list)))
320 ;;;; conditions, handlers, restarts
322 ;;; KLUDGE: we PROCLAIM these special here so that we can use restart
323 ;;; macros in the compiler before the DEFVARs are compiled.
325 '(special *handler-clusters* *restart-clusters* *condition-restarts*))
327 (defmacro-mundanely with-condition-restarts
328 (condition-form restarts-form &body body)
330 "WITH-CONDITION-RESTARTS Condition-Form Restarts-Form Form*
331 Evaluates the Forms in a dynamic environment where the restarts in the list
332 Restarts-Form are associated with the condition returned by Condition-Form.
333 This allows FIND-RESTART, etc., to recognize restarts that are not related
334 to the error currently being debugged. See also RESTART-CASE."
335 (let ((n-cond (gensym)))
336 `(let ((*condition-restarts*
337 (cons (let ((,n-cond ,condition-form))
339 (append ,restarts-form
340 (cdr (assoc ,n-cond *condition-restarts*)))))
341 *condition-restarts*)))
344 (defmacro-mundanely restart-bind (bindings &body forms)
346 "Executes forms in a dynamic context where the given restart bindings are
347 in effect. Users probably want to use RESTART-CASE. When clauses contain
348 the same restart name, FIND-RESTART will find the first such clause."
349 `(let ((*restart-clusters*
351 ,@(mapcar (lambda (binding)
352 (unless (or (car binding)
353 (member :report-function
356 (warn "Unnamed restart does not have a ~
359 `(make-restart :name ',(car binding)
360 :function ,(cadr binding)
363 *restart-clusters*)))
366 ;;; Wrap the RESTART-CASE expression in a WITH-CONDITION-RESTARTS if
367 ;;; appropriate. Gross, but it's what the book seems to say...
368 (defun munge-restart-case-expression (expression env)
369 (let ((exp (sb!xc:macroexpand expression env)))
371 (let* ((name (car exp))
372 (args (if (eq name 'cerror) (cddr exp) (cdr exp))))
373 (if (member name '(signal error cerror warn))
374 (once-only ((n-cond `(coerce-to-condition
378 (warn 'simple-warning)
379 (signal 'simple-condition)
382 `(with-condition-restarts
384 (car *restart-clusters*)
385 ,(if (eq name 'cerror)
386 `(cerror ,(second exp) ,n-cond)
391 ;;; FIXME: I did a fair amount of rearrangement of this code in order to
392 ;;; get WITH-KEYWORD-PAIRS to work cleanly. This code should be tested..
393 (defmacro-mundanely restart-case (expression &body clauses &environment env)
396 {(case-name arg-list {keyword value}* body)}*)
397 The form is evaluated in a dynamic context where the clauses have special
398 meanings as points to which control may be transferred (see INVOKE-RESTART).
399 When clauses contain the same case-name, FIND-RESTART will find the first
400 such clause. If Expression is a call to SIGNAL, ERROR, CERROR or WARN (or
401 macroexpands into such) then the signalled condition will be associated with
403 (flet ((transform-keywords (&key report interactive test)
406 (setq result (list* (if (stringp report)
408 (write-string ,report stream))
413 (setq result (list* `#',interactive
414 :interactive-function
417 (setq result (list* `#',test :test-function result)))
419 (parse-keyword-pairs (list keys)
420 (do ((l list (cddr l))
421 (k '() (list* (cadr l) (car l) k)))
422 ((or (null l) (not (member (car l) keys)))
423 (values (nreverse k) l)))))
424 (let ((block-tag (gensym))
427 (macrolet (;; KLUDGE: This started as an old DEFMACRO
428 ;; WITH-KEYWORD-PAIRS general utility, which was used
429 ;; only in this one place in the code. It was translated
430 ;; literally into this MACROLET in order to avoid some
431 ;; cross-compilation bootstrap problems. It would almost
432 ;; certainly be clearer, and it would certainly be more
433 ;; concise, to do a more idiomatic translation, merging
434 ;; this with the TRANSFORM-KEYWORDS logic above.
436 (with-keyword-pairs ((names expression) &body forms)
437 (let ((temp (member '&rest names)))
438 (unless (= (length temp) 2)
439 (error "&REST keyword is ~:[missing~;misplaced~]."
441 (let* ((key-vars (ldiff names temp))
442 (keywords (mapcar #'keywordicate key-vars))
444 (rest-var (cadr temp)))
445 `(multiple-value-bind (,key-var ,rest-var)
446 (parse-keyword-pairs ,expression ',keywords)
447 (let ,(mapcar (lambda (var keyword)
448 `(,var (getf ,key-var
452 (mapcar (lambda (clause)
453 (with-keyword-pairs ((report interactive test
456 (list (car clause) ;name=0
458 (transform-keywords :report report ;keywords=2
459 :interactive interactive
465 (let ((,temp-var nil))
468 ,(mapcar (lambda (datum)
469 (let ((name (nth 0 datum))
471 (keys (nth 2 datum)))
472 `(,name #'(lambda (&rest temp)
473 (setq ,temp-var temp)
477 (return-from ,block-tag
478 ,(munge-restart-case-expression expression env)))
479 ,@(mapcan (lambda (datum)
480 (let ((tag (nth 1 datum))
482 (body (nth 4 datum)))
484 `(return-from ,block-tag
485 (apply (lambda ,bvl ,@body)
489 (defmacro-mundanely with-simple-restart ((restart-name format-string
490 &rest format-arguments)
493 "(WITH-SIMPLE-RESTART (restart-name format-string format-arguments)
495 If restart-name is not invoked, then all values returned by forms are
496 returned. If control is transferred to this restart, it immediately
497 returns the values NIL and T."
499 ;; If there's just one body form, then don't use PROGN. This allows
500 ;; RESTART-CASE to "see" calls to ERROR, etc.
501 ,(if (= (length forms) 1) (car forms) `(progn ,@forms))
503 :report (lambda (stream)
504 (format stream ,format-string ,@format-arguments))
507 (defmacro-mundanely handler-bind (bindings &body forms)
509 "(HANDLER-BIND ( {(type handler)}* ) body)
510 Executes body in a dynamic context where the given handler bindings are
511 in effect. Each handler must take the condition being signalled as an
512 argument. The bindings are searched first to last in the event of a
513 signalled condition."
514 (let ((member-if (member-if (lambda (x)
515 (not (proper-list-of-length-p x 2)))
518 (error "ill-formed handler binding: ~S" (first member-if))))
519 `(let ((*handler-clusters*
520 (cons (list ,@(mapcar (lambda (x) `(cons ',(car x) ,(cadr x)))
522 *handler-clusters*)))
523 (multiple-value-prog1
526 ;; Wait for any float exceptions.
527 #!+x86 (float-wait))))
529 (defmacro-mundanely handler-case (form &rest cases)
531 { (type ([var]) body) }* )
532 Execute FORM in a context with handlers established for the condition
533 types. A peculiar property allows type to be :NO-ERROR. If such a clause
534 occurs, and form returns normally, all its values are passed to this clause
535 as if by MULTIPLE-VALUE-CALL. The :NO-ERROR clause accepts more than one
537 ;; FIXME: Replacing CADR, CDDDR and friends with DESTRUCTURING-BIND
538 ;; and names for the subexpressions would make it easier to
539 ;; understand the code below.
540 (let ((no-error-clause (assoc ':no-error cases)))
542 (let ((normal-return (make-symbol "normal-return"))
543 (error-return (make-symbol "error-return")))
544 `(block ,error-return
545 (multiple-value-call (lambda ,@(cdr no-error-clause))
546 (block ,normal-return
547 (return-from ,error-return
548 (handler-case (return-from ,normal-return ,form)
549 ,@(remove no-error-clause cases)))))))
552 (annotated-cases (mapcar (lambda (case) (cons (gensym) case))
556 (declare (ignorable ,var))
559 ,(mapcar (lambda (annotated-case)
560 (list (cadr annotated-case)
562 ,(if (caddr annotated-case)
564 '(declare (ignore temp)))
565 (go ,(car annotated-case)))))
569 #!+x86 (multiple-value-prog1 ,form
570 ;; Need to catch FP errors here!
573 (lambda (annotated-case)
574 (list (car annotated-case)
575 (let ((body (cdddr annotated-case)))
578 ,(cond ((caddr annotated-case)
579 `(let ((,(caaddr annotated-case)
585 `(progn ,@body)))))))
586 annotated-cases))))))))
590 (defmacro-mundanely return (&optional (value nil))
591 `(return-from nil ,value))
593 (defmacro-mundanely psetq (&rest pairs)
596 Set the variables to the values, like SETQ, except that assignments
597 happen in parallel, i.e. no assignments take place until all the
598 forms have been evaluated."
599 ;; Given the possibility of symbol-macros, we delegate to PSETF
600 ;; which knows how to deal with them, after checking that syntax is
601 ;; compatible with PSETQ.
602 (do ((pair pairs (cddr pair)))
603 ((endp pair) `(psetf ,@pairs))
604 (unless (symbolp (car pair))
605 (error 'simple-program-error
606 :format-control "variable ~S in PSETQ is not a SYMBOL"
607 :format-arguments (list (car pair))))))
609 (defmacro-mundanely lambda (&whole whole args &body body)
610 (declare (ignore args body))
613 (defmacro-mundanely named-lambda (&whole whole name args &body body)
614 (declare (ignore name args body))
617 (defmacro-mundanely lambda-with-lexenv (&whole whole
618 declarations macros symbol-macros
620 (declare (ignore declarations macros symbol-macros body))
623 ;;; this eliminates a whole bundle of unknown function STYLE-WARNINGs
624 ;;; when cross-compiling. It's not critical for behaviour, but is
625 ;;; aesthetically pleasing, except inasmuch as there's this list of
626 ;;; magic functions here. -- CSR, 2003-04-01
628 (sb!xc:proclaim '(ftype (function * *)
629 ;; functions appearing in fundamental defining
635 sb!c::%define-symbol-macro
637 sb!c::%define-compiler-macro
639 sb!kernel::%compiler-defstruct
640 sb!kernel::%compiler-define-condition
641 sb!kernel::%defstruct
642 sb!kernel::%define-condition
643 ;; miscellaneous functions commonly appearing
644 ;; as a result of macro expansions or compiler
646 sb!int:find-undeleted-package-or-lose ; IN-PACKAGE
647 sb!kernel::arg-count-error ; PARSE-DEFMACRO