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 ;;; other things defined in terms of COND
88 (defmacro-mundanely when
(test &body forms
)
90 "If the first argument is true, the rest of the forms are
91 evaluated as a PROGN."
92 `(cond (,test nil
,@forms
)))
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 `(cond ((not ,test
) nil
,@forms
)))
98 (defmacro-mundanely and
(&rest forms
)
99 (cond ((endp forms
) t
)
100 ((endp (rest forms
)) (first forms
))
105 (defmacro-mundanely or
(&rest forms
)
106 (cond ((endp forms
) nil
)
107 ((endp (rest forms
)) (first forms
))
109 (let ((n-result (gensym)))
110 `(let ((,n-result
,(first forms
)))
113 (or ,@(rest forms
))))))))
115 ;;;; various sequencing constructs
117 (flet ((prog-expansion-from-let (varlist body-decls let
)
118 (multiple-value-bind (body decls
)
119 (parse-body body-decls
:doc-string-allowed nil
)
123 (tagbody ,@body
))))))
124 (defmacro-mundanely prog
(varlist &body body-decls
)
125 (prog-expansion-from-let varlist body-decls
'let
))
126 (defmacro-mundanely prog
* (varlist &body body-decls
)
127 (prog-expansion-from-let varlist body-decls
'let
*)))
129 (defmacro-mundanely prog1
(result &body body
)
130 (let ((n-result (gensym)))
131 `(let ((,n-result
,result
))
135 (defmacro-mundanely prog2
(form1 result
&body body
)
136 `(prog1 (progn ,form1
,result
) ,@body
))
140 ;;; Should we save the inline expansion of the function named NAME?
141 (defun inline-fun-name-p (name)
143 ;; the normal reason for saving the inline expansion
144 (info :function
:inlinep name
)
145 ;; another reason for saving the inline expansion: If the
146 ;; ANSI-recommended idiom
147 ;; (DECLAIM (INLINE FOO))
149 ;; (DECLAIM (NOTINLINE FOO))
150 ;; has been used, and then we later do another
152 ;; without a preceding
153 ;; (DECLAIM (INLINE FOO))
154 ;; what should we do with the old inline expansion when we see the
155 ;; new DEFUN? Overwriting it with the new definition seems like
156 ;; the only unsurprising choice.
157 (info :function
:inline-expansion-designator name
)))
159 (defmacro-mundanely defun
(&environment env name args
&body body
)
160 "Define a function at top level."
162 (unless (symbol-package (fun-name-block-name name
))
163 (warn "DEFUN of uninterned function name ~S (tricky for GENESIS)" name
))
164 (multiple-value-bind (forms decls doc
) (parse-body body
)
165 (let* (;; stuff shared between LAMBDA and INLINE-LAMBDA and NAMED-LAMBDA
168 (block ,(fun-name-block-name name
)
170 (lambda `(lambda ,@lambda-guts
))
172 (named-lambda `(named-lambda ,name
,@lambda-guts
))
174 (when (inline-fun-name-p name
)
175 ;; we want to attempt to inline, so complain if we can't
176 (or (sb!c
:maybe-inline-syntactic-closure lambda env
)
179 #-sb-xc-host sb
!c
:maybe-compiler-notify
180 "lexical environment too hairy, can't inline DEFUN ~S"
184 ;; In cross-compilation of toplevel DEFUNs, we arrange for
185 ;; the LAMBDA to be statically linked by GENESIS.
187 ;; It may seem strangely inconsistent not to use NAMED-LAMBDA
188 ;; here instead of LAMBDA. The reason is historical:
189 ;; COLD-FSET was written before NAMED-LAMBDA, and has special
190 ;; logic of its own to notify the compiler about NAME.
192 (cold-fset ,name
,lambda
)
194 (eval-when (:compile-toplevel
)
195 (sb!c
:%compiler-defun
',name
',inline-lambda t
))
196 (eval-when (:load-toplevel
:execute
)
198 ;; In normal compilation (not for cold load) this is
199 ;; where the compiled LAMBDA first appears. In
200 ;; cross-compilation, we manipulate the
201 ;; previously-statically-linked LAMBDA here.
202 #-sb-xc-host
,named-lambda
203 #+sb-xc-host
(fdefinition ',name
)
206 (sb!c
:source-location
)))))))
209 (defun %defun
(name def doc inline-lambda source-location
)
210 (declare (ignore source-location
))
211 (declare (type function def
))
212 (declare (type (or null simple-string
) doc
))
213 (aver (legal-fun-name-p name
)) ; should've been checked by DEFMACRO DEFUN
214 (sb!c
:%compiler-defun
name inline-lambda nil
)
216 (/show0
"redefining NAME in %DEFUN")
217 (style-warn "redefining ~S in DEFUN" name
))
218 (setf (sb!xc
:fdefinition name
) def
)
220 (sb!c
::note-name-defined name
:function
)
222 ;; FIXME: I want to do this here (and fix bug 137), but until the
223 ;; breathtaking CMU CL function name architecture is converted into
224 ;; something sane, (1) doing so doesn't really fix the bug, and
225 ;; (2) doing probably isn't even really safe.
226 #+nil
(setf (%fun-name def
) name
)
229 (setf (fdocumentation name
'function
) doc
)
231 (when (typep def
'sb
!eval
:interpreted-function
)
232 (setf (sb!eval
:interpreted-function-documentation def
)
236 ;;;; DEFVAR and DEFPARAMETER
238 (defmacro-mundanely defvar
(var &optional
(val nil valp
) (doc nil docp
))
240 "Define a global variable at top level. Declare the variable
241 SPECIAL and, optionally, initialize it. If the variable already has a
242 value, the old value is not clobbered. The third argument is an optional
243 documentation string for the variable."
245 (eval-when (:compile-toplevel
)
246 (%compiler-defvar
',var
))
247 (eval-when (:load-toplevel
:execute
)
248 (%defvar
',var
(unless (boundp ',var
) ,val
)
250 (sb!c
:source-location
)))))
252 (defmacro-mundanely defparameter
(var val
&optional
(doc nil docp
))
254 "Define a parameter that is not normally changed by the program,
255 but that may be changed without causing an error. Declare the
256 variable special and sets its value to VAL, overwriting any
257 previous value. The third argument is an optional documentation
258 string for the parameter."
260 (eval-when (:compile-toplevel
)
261 (%compiler-defvar
',var
))
262 (eval-when (:load-toplevel
:execute
)
263 (%defparameter
',var
,val
,doc
',docp
(sb!c
:source-location
)))))
265 (defun %compiler-defvar
(var)
266 (sb!xc
:proclaim
`(special ,var
)))
269 (defun %defvar
(var val valp doc docp source-location
)
270 (%compiler-defvar var
)
275 (setf (fdocumentation var
'variable
) doc
))
276 (sb!c
:with-source-location
(source-location)
277 (setf (info :source-location
:variable var
) source-location
))
281 (defun %defparameter
(var val doc docp source-location
)
282 (%compiler-defvar var
)
285 (setf (fdocumentation var
'variable
) doc
))
286 (sb!c
:with-source-location
(source-location)
287 (setf (info :source-location
:variable var
) source-location
))
290 ;;;; iteration constructs
292 ;;; (These macros are defined in terms of a function FROB-DO-BODY which
293 ;;; is also used by SB!INT:DO-ANONYMOUS. Since these macros should not
294 ;;; be loaded on the cross-compilation host, but SB!INT:DO-ANONYMOUS
295 ;;; and FROB-DO-BODY should be, these macros can't conveniently be in
296 ;;; the same file as FROB-DO-BODY.)
297 (defmacro-mundanely do
(varlist endlist
&body body
)
299 "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
300 Iteration construct. Each Var is initialized in parallel to the value of the
301 specified Init form. On subsequent iterations, the Vars are assigned the
302 value of the Step form (if any) in parallel. The Test is evaluated before
303 each evaluation of the body Forms. When the Test is true, the Exit-Forms
304 are evaluated as a PROGN, with the result being the value of the DO. A block
305 named NIL is established around the entire expansion, allowing RETURN to be
306 used as an alternate exit mechanism."
307 (frob-do-body varlist endlist body
'let
'psetq
'do nil
))
308 (defmacro-mundanely do
* (varlist endlist
&body body
)
310 "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
311 Iteration construct. Each Var is initialized sequentially (like LET*) to the
312 value of the specified Init form. On subsequent iterations, the Vars are
313 sequentially assigned the value of the Step form (if any). The Test is
314 evaluated before each evaluation of the body Forms. When the Test is true,
315 the Exit-Forms are evaluated as a PROGN, with the result being the value
316 of the DO. A block named NIL is established around the entire expansion,
317 allowing RETURN to be used as an laternate exit mechanism."
318 (frob-do-body varlist endlist body
'let
* 'setq
'do
* nil
))
320 ;;; DOTIMES and DOLIST could be defined more concisely using
321 ;;; destructuring macro lambda lists or DESTRUCTURING-BIND, but then
322 ;;; it'd be tricky to use them before those things were defined.
323 ;;; They're used enough times before destructuring mechanisms are
324 ;;; defined that it looks as though it's worth just implementing them
325 ;;; ASAP, at the cost of being unable to use the standard
326 ;;; destructuring mechanisms.
327 (defmacro-mundanely dotimes
((var count
&optional
(result nil
)) &body body
)
328 (cond ((numberp count
)
329 `(do ((,var
0 (1+ ,var
)))
330 ((>= ,var
,count
) ,result
)
331 (declare (type unsigned-byte
,var
))
334 (let ((c (gensym "COUNT")))
335 `(do ((,var
0 (1+ ,var
))
337 ((>= ,var
,c
) ,result
)
338 (declare (type unsigned-byte
,var
)
342 (defmacro-mundanely dolist
((var list
&optional
(result nil
)) &body body
)
343 ;; We repeatedly bind the var instead of setting it so that we never
344 ;; have to give the var an arbitrary value such as NIL (which might
345 ;; conflict with a declaration). If there is a result form, we
346 ;; introduce a gratuitous binding of the variable to NIL without the
347 ;; declarations, then evaluate the result form in that
348 ;; environment. We spuriously reference the gratuitous variable,
349 ;; since we don't want to use IGNORABLE on what might be a special
351 (multiple-value-bind (forms decls
) (parse-body body
:doc-string-allowed nil
)
352 (let ((n-list (gensym "N-LIST"))
353 (start (gensym "START")))
355 (let ((,n-list
,list
))
358 (unless (endp ,n-list
)
359 (let ((,var
(car ,n-list
)))
361 (setq ,n-list
(cdr ,n-list
))
366 ;; Filter out TYPE declarations (VAR gets bound to NIL,
367 ;; and might have a conflicting type declaration) and
368 ;; IGNORE (VAR might be ignored in the loop body, but
369 ;; it's used in the result form).
370 ,@(filter-dolist-declarations decls
)
375 ;;;; conditions, handlers, restarts
377 ;;; KLUDGE: we PROCLAIM these special here so that we can use restart
378 ;;; macros in the compiler before the DEFVARs are compiled.
380 '(special *handler-clusters
* *restart-clusters
* *condition-restarts
*))
382 (defmacro-mundanely with-condition-restarts
383 (condition-form restarts-form
&body body
)
385 "Evaluates the BODY in a dynamic environment where the restarts in the list
386 RESTARTS-FORM are associated with the condition returned by CONDITION-FORM.
387 This allows FIND-RESTART, etc., to recognize restarts that are not related
388 to the error currently being debugged. See also RESTART-CASE."
389 (let ((n-cond (gensym)))
390 `(let ((*condition-restarts
*
391 (cons (let ((,n-cond
,condition-form
))
393 (append ,restarts-form
394 (cdr (assoc ,n-cond
*condition-restarts
*)))))
395 *condition-restarts
*)))
398 (defmacro-mundanely restart-bind
(bindings &body forms
)
400 "Executes forms in a dynamic context where the given restart bindings are
401 in effect. Users probably want to use RESTART-CASE. When clauses contain
402 the same restart name, FIND-RESTART will find the first such clause."
403 `(let ((*restart-clusters
*
405 ,@(mapcar (lambda (binding)
406 (unless (or (car binding
)
407 (member :report-function
410 (warn "Unnamed restart does not have a ~
413 `(make-restart :name
',(car binding
)
414 :function
,(cadr binding
)
417 *restart-clusters
*)))
420 ;;; Wrap the RESTART-CASE expression in a WITH-CONDITION-RESTARTS if
421 ;;; appropriate. Gross, but it's what the book seems to say...
422 (defun munge-restart-case-expression (expression env
)
423 (let ((exp (sb!xc
:macroexpand expression env
)))
425 (let* ((name (car exp
))
426 (args (if (eq name
'cerror
) (cddr exp
) (cdr exp
))))
427 (if (member name
'(signal error cerror warn
))
428 (once-only ((n-cond `(coerce-to-condition
432 (warn 'simple-warning
)
433 (signal 'simple-condition
)
436 `(with-condition-restarts
438 (car *restart-clusters
*)
439 ,(if (eq name
'cerror
)
440 `(cerror ,(second exp
) ,n-cond
)
445 ;;; FIXME: I did a fair amount of rearrangement of this code in order to
446 ;;; get WITH-KEYWORD-PAIRS to work cleanly. This code should be tested..
447 (defmacro-mundanely restart-case
(expression &body clauses
&environment env
)
450 {(case-name arg-list {keyword value}* body)}*)
451 The form is evaluated in a dynamic context where the clauses have special
452 meanings as points to which control may be transferred (see INVOKE-RESTART).
453 When clauses contain the same case-name, FIND-RESTART will find the first
454 such clause. If Expression is a call to SIGNAL, ERROR, CERROR or WARN (or
455 macroexpands into such) then the signalled condition will be associated with
457 (flet ((transform-keywords (&key report interactive test
)
460 (setq result
(list* (if (stringp report
)
462 (write-string ,report stream
))
467 (setq result
(list* `#',interactive
468 :interactive-function
471 (setq result
(list* `#',test
:test-function result
)))
473 (parse-keyword-pairs (list keys
)
474 (do ((l list
(cddr l
))
475 (k '() (list* (cadr l
) (car l
) k
)))
476 ((or (null l
) (not (member (car l
) keys
)))
477 (values (nreverse k
) l
)))))
478 (let ((block-tag (gensym))
481 (macrolet (;; KLUDGE: This started as an old DEFMACRO
482 ;; WITH-KEYWORD-PAIRS general utility, which was used
483 ;; only in this one place in the code. It was translated
484 ;; literally into this MACROLET in order to avoid some
485 ;; cross-compilation bootstrap problems. It would almost
486 ;; certainly be clearer, and it would certainly be more
487 ;; concise, to do a more idiomatic translation, merging
488 ;; this with the TRANSFORM-KEYWORDS logic above.
490 (with-keyword-pairs ((names expression
) &body forms
)
491 (let ((temp (member '&rest names
)))
492 (unless (= (length temp
) 2)
493 (error "&REST keyword is ~:[missing~;misplaced~]."
495 (let* ((key-vars (ldiff names temp
))
496 (keywords (mapcar #'keywordicate key-vars
))
498 (rest-var (cadr temp
)))
499 `(multiple-value-bind (,key-var
,rest-var
)
500 (parse-keyword-pairs ,expression
',keywords
)
501 (let ,(mapcar (lambda (var keyword
)
502 `(,var
(getf ,key-var
506 (mapcar (lambda (clause)
507 (with-keyword-pairs ((report interactive test
510 (list (car clause
) ;name=0
512 (transform-keywords :report report
;keywords=2
513 :interactive interactive
519 (let ((,temp-var nil
))
522 ,(mapcar (lambda (datum)
523 (let ((name (nth 0 datum
))
525 (keys (nth 2 datum
)))
526 `(,name
#'(lambda (&rest temp
)
527 (setq ,temp-var temp
)
531 (return-from ,block-tag
532 ,(munge-restart-case-expression expression env
)))
533 ,@(mapcan (lambda (datum)
534 (let ((tag (nth 1 datum
))
536 (body (nth 4 datum
)))
538 `(return-from ,block-tag
539 (apply (lambda ,bvl
,@body
)
543 (defmacro-mundanely with-simple-restart
((restart-name format-string
544 &rest format-arguments
)
547 "(WITH-SIMPLE-RESTART (restart-name format-string format-arguments)
549 If restart-name is not invoked, then all values returned by forms are
550 returned. If control is transferred to this restart, it immediately
551 returns the values NIL and T."
553 ;; If there's just one body form, then don't use PROGN. This allows
554 ;; RESTART-CASE to "see" calls to ERROR, etc.
555 ,(if (= (length forms
) 1) (car forms
) `(progn ,@forms
))
557 :report
(lambda (stream)
558 (format stream
,format-string
,@format-arguments
))
561 (defmacro-mundanely handler-bind
(bindings &body forms
)
563 "(HANDLER-BIND ( {(type handler)}* ) body)
564 Executes body in a dynamic context where the given handler bindings are
565 in effect. Each handler must take the condition being signalled as an
566 argument. The bindings are searched first to last in the event of a
567 signalled condition."
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 ((*handler-clusters
*
574 (cons (list ,@(mapcar (lambda (x) `(cons ',(car x
) ,(cadr x
)))
576 *handler-clusters
*)))
577 (multiple-value-prog1
580 ;; Wait for any float exceptions.
581 #!+x86
(float-wait))))
583 (defmacro-mundanely handler-case
(form &rest cases
)
585 { (type ([var]) body) }* )
586 Execute FORM in a context with handlers established for the condition
587 types. A peculiar property allows type to be :NO-ERROR. If such a clause
588 occurs, and form returns normally, all its values are passed to this clause
589 as if by MULTIPLE-VALUE-CALL. The :NO-ERROR clause accepts more than one
591 ;; FIXME: Replacing CADR, CDDDR and friends with DESTRUCTURING-BIND
592 ;; and names for the subexpressions would make it easier to
593 ;; understand the code below.
594 (let ((no-error-clause (assoc ':no-error cases
)))
596 (let ((normal-return (make-symbol "normal-return"))
597 (error-return (make-symbol "error-return")))
598 `(block ,error-return
599 (multiple-value-call (lambda ,@(cdr no-error-clause
))
600 (block ,normal-return
601 (return-from ,error-return
602 (handler-case (return-from ,normal-return
,form
)
603 ,@(remove no-error-clause cases
)))))))
606 (annotated-cases (mapcar (lambda (case) (cons (gensym) case
))
610 (declare (ignorable ,var
))
613 ,(mapcar (lambda (annotated-case)
614 (list (cadr annotated-case
)
616 ,(if (caddr annotated-case
)
618 '(declare (ignore temp
)))
619 (go ,(car annotated-case
)))))
623 #!+x86
(multiple-value-prog1 ,form
624 ;; Need to catch FP errors here!
627 (lambda (annotated-case)
628 (list (car annotated-case
)
629 (let ((body (cdddr annotated-case
)))
632 ,(cond ((caddr annotated-case
)
633 `(let ((,(caaddr annotated-case
)
637 `(locally ,@body
)))))))
638 annotated-cases
))))))))
642 (defmacro-mundanely return
(&optional
(value nil
))
643 `(return-from nil
,value
))
645 (defmacro-mundanely psetq
(&rest pairs
)
648 Set the variables to the values, like SETQ, except that assignments
649 happen in parallel, i.e. no assignments take place until all the
650 forms have been evaluated."
651 ;; Given the possibility of symbol-macros, we delegate to PSETF
652 ;; which knows how to deal with them, after checking that syntax is
653 ;; compatible with PSETQ.
654 (do ((pair pairs
(cddr pair
)))
655 ((endp pair
) `(psetf ,@pairs
))
656 (unless (symbolp (car pair
))
657 (error 'simple-program-error
658 :format-control
"variable ~S in PSETQ is not a SYMBOL"
659 :format-arguments
(list (car pair
))))))
661 (defmacro-mundanely lambda
(&whole whole args
&body body
)
662 (declare (ignore args body
))
665 (defmacro-mundanely named-lambda
(&whole whole name args
&body body
)
666 (declare (ignore name args body
))
669 (defmacro-mundanely lambda-with-lexenv
(&whole whole
670 declarations macros symbol-macros
672 (declare (ignore declarations macros symbol-macros body
))
675 ;;; this eliminates a whole bundle of unknown function STYLE-WARNINGs
676 ;;; when cross-compiling. It's not critical for behaviour, but is
677 ;;; aesthetically pleasing, except inasmuch as there's this list of
678 ;;; magic functions here. -- CSR, 2003-04-01
680 (sb!xc
:proclaim
'(ftype (function * *)
681 ;; functions appearing in fundamental defining
690 sb
!c
::%define-symbol-macro
692 sb
!c
::%define-compiler-macro
694 sb
!kernel
::%compiler-defstruct
695 sb
!kernel
::%compiler-define-condition
696 sb
!kernel
::%defstruct
697 sb
!kernel
::%define-condition
698 ;; miscellaneous functions commonly appearing
699 ;; as a result of macro expansions or compiler
701 sb
!int
:find-undeleted-package-or-lose
; IN-PACKAGE
702 sb
!kernel
::arg-count-error
; PARSE-DEFMACRO