0.7.9.16:
[sbcl.git] / src / code / defboot.lisp
1 ;;;; bootstrapping fundamental machinery (e.g. DEFUN, DEFCONSTANT,
2 ;;;; DEFVAR) from special forms and primitive functions
3 ;;;;
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.
11
12 ;;;; This software is part of the SBCL system. See the README file for
13 ;;;; more information.
14 ;;;;
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.
20
21 (in-package "SB!IMPL")
22 \f
23 ;;;; IN-PACKAGE
24
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))))
28 \f
29 ;;;; MULTIPLE-VALUE-FOO
30
31 (defun list-of-symbols-p (x)
32   (and (listp x)
33        (every #'symbolp x)))
34
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))
41          ,@body)
42       (let ((ignore (gensym)))
43         `(multiple-value-call #'(lambda (&optional ,@vars &rest ,ignore)
44                                   (declare (ignore ,ignore))
45                                   ,@body)
46                               ,value-form)))
47     (error "Vars is not a list of symbols: ~S" vars)))
48
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)))
53
54 (defmacro-mundanely multiple-value-list (value-form)
55   `(multiple-value-call #'list ,value-form))
56 \f
57 ;;;; various conditional constructs
58
59 ;;; COND defined in terms of IF
60 (defmacro-mundanely cond (&rest clauses)
61   (if (endp clauses)
62       nil
63       (let ((clause (first clauses)))
64         (if (atom clause)
65             (error "COND clause is not a list: ~S" clause)
66             (let ((test (first clause))
67                   (forms (rest clause)))
68               (if (endp forms)
69                   (let ((n-result (gensym)))
70                     `(let ((,n-result ,test))
71                        (if ,n-result
72                            ,n-result
73                            (cond ,@(rest clauses)))))
74                   `(if ,test
75                        (progn ,@forms)
76                        (cond ,@(rest clauses)))))))))
77
78 ;;; other things defined in terms of COND
79 (defmacro-mundanely when (test &body forms)
80   #!+sb-doc
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)
85   #!+sb-doc
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))
92         (t
93          `(if ,(first forms)
94               (and ,@(rest forms))
95               nil))))
96 (defmacro-mundanely or (&rest forms)
97   (cond ((endp forms) nil)
98         ((endp (rest forms)) (first forms))
99         (t
100          (let ((n-result (gensym)))
101            `(let ((,n-result ,(first forms)))
102               (if ,n-result
103                   ,n-result
104                   (or ,@(rest forms))))))))
105 \f
106 ;;;; various sequencing constructs
107
108 (defmacro-mundanely prog (varlist &body body-decls)
109   (multiple-value-bind (body decls) (parse-body body-decls nil)
110     `(block nil
111        (let ,varlist
112          ,@decls
113          (tagbody ,@body)))))
114
115 (defmacro-mundanely prog* (varlist &body body-decls)
116   (multiple-value-bind (body decls) (parse-body body-decls nil)
117     `(block nil
118        (let* ,varlist
119          ,@decls
120          (tagbody ,@body)))))
121
122 (defmacro-mundanely prog1 (result &body body)
123   (let ((n-result (gensym)))
124     `(let ((,n-result ,result))
125        ,@body
126        ,n-result)))
127
128 (defmacro-mundanely prog2 (form1 result &body body)
129   `(prog1 (progn ,form1 ,result) ,@body))
130 \f
131 ;;;; DEFUN
132
133 ;;; Should we save the inline expansion of the function named NAME?
134 (defun inline-fun-name-p (name)
135   (or
136    ;; the normal reason for saving the inline expansion
137    (info :function :inlinep name)
138    ;; another reason for saving the inline expansion: If the
139    ;; ANSI-recommended idiom
140    ;;   (DECLAIM (INLINE FOO))
141    ;;   (DEFUN FOO ..)
142    ;;   (DECLAIM (NOTINLINE FOO))
143    ;; has been used, and then we later do another
144    ;;   (DEFUN FOO ..)
145    ;; without a preceding
146    ;;   (DECLAIM (INLINE FOO))
147    ;; what should we do with the old inline expansion when we see the
148    ;; new DEFUN? Overwriting it with the new definition seems like
149    ;; the only unsurprising choice.
150    (info :function :inline-expansion-designator name)))
151
152 ;;; Now that we have the definition of MULTIPLE-VALUE-BIND, we can
153 ;;; make a reasonably readable definition of DEFUN.
154 (defmacro-mundanely defun (&environment env name args &body body)
155   "Define a function at top level."
156   #+sb-xc-host
157   (unless (symbol-package (fun-name-block-name name))
158     (warn "DEFUN of uninterned symbol ~S (tricky for GENESIS)" name))
159   (multiple-value-bind (forms decls doc) (parse-body body)
160     (let* (;; stuff shared between LAMBDA and INLINE-LAMBDA and NAMED-LAMBDA
161            (lambda-guts `(,args
162                           ,@decls
163                           (block ,(fun-name-block-name name)
164                             ,@forms)))
165            (lambda `(lambda ,@lambda-guts))
166            #-sb-xc-host
167            (named-lambda `(named-lambda ,name ,@lambda-guts))
168            (inline-lambda
169             (cond (;; Does the user not even want to inline?
170                    (not (inline-fun-name-p name))
171                    nil)
172                   (;; Does inlining look too hairy to handle?
173                    (not (sb!c:lambda-independent-of-lexenv-p lambda env))
174                    (sb!c:maybe-compiler-note
175                     "lexical environment too hairy, can't inline DEFUN ~S"
176                     name)
177                    nil)
178                   (t
179                    ;; FIXME: The only reason that we return
180                    ;; LAMBDA-WITH-LEXENV instead of returning bare
181                    ;; LAMBDA is to avoid modifying downstream code
182                    ;; which expects LAMBDA-WITH-LEXENV. But the code
183                    ;; here is the only code which feeds into the
184                    ;; downstream code, and the generality of the
185                    ;; interface is no longer used, so it'd make sense
186                    ;; to simplify the interface instead of using the
187                    ;; old general LAMBDA-WITH-LEXENV interface in this
188                    ;; simplified way.
189                    `(sb!c:lambda-with-lexenv
190                      nil nil nil ; i.e. no DECLS, no MACROS, no SYMMACS
191                      ,@lambda-guts)))))
192       `(progn
193
194          ;; In cross-compilation of toplevel DEFUNs, we arrange
195          ;; for the LAMBDA to be statically linked by GENESIS.
196          ;;
197          ;; It may seem strangely inconsistent not to use NAMED-LAMBDA
198          ;; here instead of LAMBDA. The reason is historical:
199          ;; COLD-FSET was written before NAMED-LAMBDA, and has special
200          ;; logic of its own to notify the compiler about NAME.
201          #+sb-xc-host
202          (cold-fset ,name ,lambda)
203
204          (eval-when (:compile-toplevel :load-toplevel :execute)
205            (sb!c:%compiler-defun ',name ',inline-lambda))
206
207          (%defun ',name
208                  ;; In normal compilation (not for cold load) this is
209                  ;; where the compiled LAMBDA first appears. In
210                  ;; cross-compilation, we manipulate the
211                  ;; previously-statically-linked LAMBDA here.
212                  #-sb-xc-host ,named-lambda
213                  #+sb-xc-host (fdefinition ',name)
214                  ,doc)))))
215 #-sb-xc-host
216 (defun %defun (name def doc)
217   (declare (type function def))
218   (declare (type (or null simple-string doc)))
219   (aver (legal-fun-name-p name)) ; should've been checked by DEFMACRO DEFUN
220   (when (fboundp name)
221     (/show0 "redefining NAME in %DEFUN")
222     (style-warn "redefining ~S in DEFUN" name))
223   (setf (sb!xc:fdefinition name) def)
224   
225   ;; FIXME: I want to do this here (and fix bug 137), but until the
226   ;; breathtaking CMU CL function name architecture is converted into
227   ;; something sane, (1) doing so doesn't really fix the bug, and 
228   ;; (2) doing probably isn't even really safe.
229   #+nil (setf (%fun-name def) name)
230
231   (when doc
232     ;; FIXME: This should use shared SETF-name-parsing logic.
233     (if (and (consp name) (eq (first name) 'setf))
234         (setf (fdocumentation (second name) 'setf) doc)
235         (setf (fdocumentation (the symbol name) 'function) doc)))
236   name)
237 \f
238 ;;;; DEFVAR and DEFPARAMETER
239
240 (defmacro-mundanely defvar (var &optional (val nil valp) (doc nil docp))
241   #!+sb-doc
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."
246   `(progn
247      (declaim (special ,var))
248      ,@(when valp
249          `((unless (boundp ',var)
250              (setq ,var ,val))))
251      ,@(when docp
252          `((funcall #'(setf fdocumentation) ',doc ',var 'variable)))
253      ',var))
254
255 (defmacro-mundanely defparameter (var val &optional (doc nil docp))
256   #!+sb-doc
257   "Define a parameter that is not normally changed by the program,
258   but that may be changed without causing an error. Declare the
259   variable special and sets its value to VAL, overwriting any
260   previous value. The third argument is an optional documentation
261   string for the parameter."
262   `(progn
263      (declaim (special ,var))
264      (setq ,var ,val)
265      ,@(when docp
266          ;; FIXME: The various FUNCALL #'(SETF FDOCUMENTATION) and
267          ;; other FUNCALL #'(SETF FOO) forms in the code should
268          ;; unbogobootstrapized back to ordinary SETF forms.
269          `((funcall #'(setf fdocumentation) ',doc ',var 'variable)))
270      ',var))
271 \f
272 ;;;; iteration constructs
273
274 ;;; (These macros are defined in terms of a function FROB-DO-BODY which
275 ;;; is also used by SB!INT:DO-ANONYMOUS. Since these macros should not
276 ;;; be loaded on the cross-compilation host, but SB!INT:DO-ANONYMOUS
277 ;;; and FROB-DO-BODY should be, these macros can't conveniently be in
278 ;;; the same file as FROB-DO-BODY.)
279 (defmacro-mundanely do (varlist endlist &body body)
280   #!+sb-doc
281   "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
282   Iteration construct. Each Var is initialized in parallel to the value of the
283   specified Init form. On subsequent iterations, the Vars are assigned the
284   value of the Step form (if any) in parallel. The Test is evaluated before
285   each evaluation of the body Forms. When the Test is true, the Exit-Forms
286   are evaluated as a PROGN, with the result being the value of the DO. A block
287   named NIL is established around the entire expansion, allowing RETURN to be
288   used as an alternate exit mechanism."
289   (frob-do-body varlist endlist body 'let 'psetq 'do nil))
290 (defmacro-mundanely do* (varlist endlist &body body)
291   #!+sb-doc
292   "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
293   Iteration construct. Each Var is initialized sequentially (like LET*) to the
294   value of the specified Init form. On subsequent iterations, the Vars are
295   sequentially assigned the value of the Step form (if any). The Test is
296   evaluated before each evaluation of the body Forms. When the Test is true,
297   the Exit-Forms are evaluated as a PROGN, with the result being the value
298   of the DO. A block named NIL is established around the entire expansion,
299   allowing RETURN to be used as an laternate exit mechanism."
300   (frob-do-body varlist endlist body 'let* 'setq 'do* nil))
301
302 ;;; DOTIMES and DOLIST could be defined more concisely using
303 ;;; destructuring macro lambda lists or DESTRUCTURING-BIND, but then
304 ;;; it'd be tricky to use them before those things were defined.
305 ;;; They're used enough times before destructuring mechanisms are
306 ;;; defined that it looks as though it's worth just implementing them
307 ;;; ASAP, at the cost of being unable to use the standard
308 ;;; destructuring mechanisms.
309 (defmacro-mundanely dotimes (var-count-result &body body)
310   (multiple-value-bind ; to roll our own destructuring
311       (var count result)
312       (apply (lambda (var count &optional (result nil))
313                (values var count result))
314              var-count-result)
315     (cond ((numberp count)
316            `(do ((,var 0 (1+ ,var)))
317                 ((>= ,var ,count) ,result)
318               (declare (type unsigned-byte ,var))
319               ,@body))
320           (t (let ((v1 (gensym)))
321                `(do ((,var 0 (1+ ,var)) (,v1 ,count))
322                     ((>= ,var ,v1) ,result)
323                   (declare (type unsigned-byte ,var))
324                   ,@body))))))
325 (defmacro-mundanely dolist (var-list-result &body body)
326   (multiple-value-bind                 ; to roll our own destructuring
327         (var list result)
328       (apply (lambda (var list &optional (result nil))
329                (values var list result))
330              var-list-result)
331     ;; We repeatedly bind the var instead of setting it so that we
332     ;; never have to give the var an arbitrary value such as NIL
333     ;; (which might conflict with a declaration). If there is a result
334     ;; form, we introduce a gratuitous binding of the variable to NIL
335     ;; without the declarations, then evaluate the result form in that
336     ;; environment. We spuriously reference the gratuitous variable,
337     ;; since we don't want to use IGNORABLE on what might be a special
338     ;; var.
339     (multiple-value-bind (forms decls) (parse-body body nil)
340       (let ((n-list (gensym)))
341         `(do* ((,n-list ,list (cdr ,n-list)))
342               ((endp ,n-list)
343                ,@(if result
344                      `((let ((,var nil))
345                          ,var
346                          ,result))
347                      '(nil)))
348            (let ((,var (car ,n-list)))
349              ,@decls
350              (tagbody
351                 ,@forms)))))))
352 \f
353 ;;;; miscellaneous
354
355 (defmacro-mundanely return (&optional (value nil))
356   `(return-from nil ,value))
357
358 (defmacro-mundanely psetq (&rest pairs)
359   #!+sb-doc
360   "PSETQ {var value}*
361    Set the variables to the values, like SETQ, except that assignments
362    happen in parallel, i.e. no assignments take place until all the
363    forms have been evaluated."
364   ;; (This macro is used in the definition of DO, so we can't use DO in the
365   ;; definition of this macro without getting into confusing bootstrap issues.)
366   (prog ((lets nil)
367          (setqs nil)
368          (pairs pairs))
369     :again
370     (when (atom (cdr pairs))
371       (return `(let ,(nreverse lets)
372                  (setq ,@(nreverse setqs))
373                  nil)))
374     (let ((gen (gensym)))
375       (setq lets (cons `(,gen ,(cadr pairs)) lets)
376             setqs (list* gen (car pairs) setqs)
377             pairs (cddr pairs)))
378     (go :again)))
379
380 (defmacro-mundanely lambda (&whole whole args &body body)
381   (declare (ignore args body))
382   `#',whole)