primarily intending to integrate Colin Walter's O(N) map code and
[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
23 (file-comment
24   "$Header$")
25 \f
26 ;;;; IN-PACKAGE
27
28 (defmacro-mundanely in-package (package-designator)
29   `(eval-when (:compile-toplevel :load-toplevel :execute)
30      (setq *package* (find-undeleted-package-or-lose ',package-designator))))
31 \f
32 ;;; MULTIPLE-VALUE-FOO
33
34 (defun list-of-symbols-p (x)
35   (and (listp x)
36        (every #'symbolp x)))
37
38 (defmacro-mundanely multiple-value-bind (vars value-form &body body)
39   (if (list-of-symbols-p vars)
40     ;; It's unclear why it would be important to special-case the LENGTH=1 case
41     ;; at this level, but the CMU CL code did it, so.. -- WHN 19990411
42     (if (= (length vars) 1)
43       `(let ((,(car vars) ,value-form))
44          ,@body)
45       (let ((ignore (gensym)))
46         `(multiple-value-call #'(lambda (&optional ,@vars &rest ,ignore)
47                                   (declare (ignore ,ignore))
48                                   ,@body)
49                               ,value-form)))
50     (error "Vars is not a list of symbols: ~S" vars)))
51
52 (defmacro-mundanely multiple-value-setq (vars value-form)
53   (cond ((null vars)
54          ;; The ANSI spec says that the primary value of VALUE-FORM must be
55          ;; returned. The general-case-handling code below doesn't do this
56          ;; correctly in the special case when there are no vars bound, so we
57          ;; handle this special case separately here.
58          (let ((g (gensym)))
59            `(multiple-value-bind (,g) ,value-form
60               ,g)))
61         ((list-of-symbols-p vars)
62          (let ((temps (make-gensym-list (length vars))))
63            `(multiple-value-bind ,temps ,value-form
64               ,@(mapcar #'(lambda (var temp)
65                             `(setq ,var ,temp))
66                         vars temps)
67               ,(car temps))))
68         (t (error "Vars is not a list of symbols: ~S" vars))))
69
70 (defmacro-mundanely multiple-value-list (value-form)
71   `(multiple-value-call #'list ,value-form))
72 \f
73 ;;;; various conditional constructs
74
75 ;;; COND defined in terms of IF
76 (defmacro-mundanely cond (&rest clauses)
77   (if (endp clauses)
78     nil
79     (let ((clause (first clauses)))
80       (if (atom clause)
81         (error "Cond clause is not a list: ~S" clause)
82         (let ((test (first clause))
83               (forms (rest clause)))
84           (if (endp forms)
85             (let ((n-result (gensym)))
86               `(let ((,n-result ,test))
87                  (if ,n-result
88                    ,n-result
89                    (cond ,@(rest clauses)))))
90             `(if ,test
91                (progn ,@forms)
92                (cond ,@(rest clauses)))))))))
93
94 ;;; other things defined in terms of COND
95 (defmacro-mundanely when (test &body forms)
96   #!+sb-doc
97   "First arg is a predicate. If it is non-null, the rest of the forms are
98   evaluated as a PROGN."
99   `(cond (,test nil ,@forms)))
100 (defmacro-mundanely unless (test &body forms)
101   #!+sb-doc
102   "First arg is a predicate. If it is null, the rest of the forms are
103   evaluated as a PROGN."
104   `(cond ((not ,test) nil ,@forms)))
105 (defmacro-mundanely and (&rest forms)
106   (cond ((endp forms) t)
107         ((endp (rest forms)) (first forms))
108         (t
109          `(if ,(first forms)
110               (and ,@(rest forms))
111               nil))))
112 (defmacro-mundanely or (&rest forms)
113   (cond ((endp forms) nil)
114         ((endp (rest forms)) (first forms))
115         (t
116          (let ((n-result (gensym)))
117            `(let ((,n-result ,(first forms)))
118               (if ,n-result
119                   ,n-result
120                   (or ,@(rest forms))))))))
121 \f
122 ;;;; various sequencing constructs
123
124 (defmacro-mundanely prog (varlist &body body-decls)
125   (multiple-value-bind (body decls) (parse-body body-decls nil)
126     `(block nil
127        (let ,varlist
128          ,@decls
129          (tagbody ,@body)))))
130
131 (defmacro-mundanely prog* (varlist &body body-decls)
132   (multiple-value-bind (body decls) (parse-body body-decls nil)
133     `(block nil
134        (let* ,varlist
135          ,@decls
136          (tagbody ,@body)))))
137
138 (defmacro-mundanely prog1 (result &body body)
139   (let ((n-result (gensym)))
140     `(let ((,n-result ,result))
141        ,@body
142        ,n-result)))
143
144 (defmacro-mundanely prog2 (form1 result &body body)
145   `(prog1 (progn ,form1 ,result) ,@body))
146 \f
147 ;;; Now that we have the definition of MULTIPLE-VALUE-BIND, we can make a
148 ;;; reasonably readable definition of DEFUN.
149 ;;;
150 ;;; DEFUN expands into %DEFUN which is a function that is treated
151 ;;; magically by the compiler (through an IR1 transform) in order to
152 ;;; handle stuff like inlining. After the compiler has gotten the
153 ;;; information it wants out of macro definition, it compiles a call
154 ;;; to %%DEFUN which happens at load time.
155 (defmacro-mundanely defun (&whole whole name args &body body)
156   (multiple-value-bind (forms decls doc) (parse-body body)
157     (let ((def `(lambda ,args
158                   ,@decls
159                   (block ,(function-name-block-name name)
160                     ,@forms))))
161       `(sb!c::%defun ',name #',def ,doc ',whole))))
162 #+sb-xc-host (/show "before PROCLAIM" (sb!c::info :function :kind 'sb!c::%%defun))
163 #+sb-xc-host (sb!xc:proclaim '(ftype function sb!c::%%defun)) ; to avoid
164                                         ; undefined function warnings
165 #+sb-xc-host (/show "after PROCLAIM" (sb!c::info :function :kind 'sb!c::%%defun))
166 (defun sb!c::%%defun (name def doc &optional inline-expansion)
167   (when (fboundp name)
168     (style-warn "redefining ~S in DEFUN" name))
169   (setf (sb!xc:fdefinition name) def)
170   (when doc
171     ;; FIXME: This should use shared SETF-name parsing logic.
172     (if (and (consp name) (eq (first name) 'setf))
173         (setf (fdocumentation (second name) 'setf) doc)
174         (setf (fdocumentation name 'function) doc)))
175   (sb!c::proclaim-as-function-name name)
176   (if (eq (info :function :where-from name) :assumed)
177       (progn
178         (setf (info :function :where-from name) :defined)
179         (if (info :function :assumed-type name)
180             (setf (info :function :assumed-type name) nil))))
181   (when (or inline-expansion
182             (info :function :inline-expansion name))
183     (setf (info :function :inline-expansion name)
184           inline-expansion))
185   name)
186 ;;; Ordinarily this definition of SB!C:%DEFUN as an ordinary function is not
187 ;;; used: the parallel (but different) definition as an IR1 transform takes
188 ;;; precedence. However, it's still good to define this in order to keep the
189 ;;; interpreter happy. We define it here (instead of alongside the parallel
190 ;;; IR1 transform) because while the IR1 transform is needed and appropriate
191 ;;; in the cross-compiler running in the host Common Lisp, this parallel
192 ;;; ordinary function definition is only appropriate in the target Lisp.
193 (defun sb!c::%defun (name def doc source)
194   (declare (ignore source))
195   (setf (sb!eval:interpreted-function-name def) name)
196   (sb!c::%%defun name def doc))
197 \f
198 ;;;; DEFVAR and DEFPARAMETER
199
200 (defmacro-mundanely defvar (var &optional (val nil valp) (doc nil docp))
201   #!+sb-doc
202   "For defining global variables at top level. Declares the variable
203   SPECIAL and, optionally, initializes it. If the variable already has a
204   value, the old value is not clobbered. The third argument is an optional
205   documentation string for the variable."
206   `(progn
207      (declaim (special ,var))
208      ,@(when valp
209          `((unless (boundp ',var)
210              (setq ,var ,val))))
211      ,@(when docp
212          `((funcall #'(setf fdocumentation) ',doc ',var 'variable)))
213      ',var))
214
215 (defmacro-mundanely defparameter (var val &optional (doc nil docp))
216   #!+sb-doc
217   "Defines a parameter that is not normally changed by the program,
218   but that may be changed without causing an error. Declares the
219   variable special and sets its value to VAL. The third argument is
220   an optional documentation string for the parameter."
221   `(progn
222      (declaim (special ,var))
223      (setq ,var ,val)
224      ,@(when docp
225          ;; FIXME: The various FUNCALL #'(SETF FDOCUMENTATION) and
226          ;; other FUNCALL #'(SETF FOO) forms in the code should
227          ;; unbogobootstrapized back to ordinary SETF forms.
228          `((funcall #'(setf fdocumentation) ',doc ',var 'variable)))
229      ',var))
230 \f
231 ;;;; iteration constructs
232
233 ;;; (These macros are defined in terms of a function DO-DO-BODY which is also
234 ;;; used by SB!INT:DO-ANONYMOUS. Since these macros should not be loaded
235 ;;; on the cross-compilation host, but SB!INT:DO-ANONYMOUS and DO-DO-BODY
236 ;;; should be, these macros can't conveniently be in the same file as
237 ;;; DO-DO-BODY.)
238 (defmacro-mundanely do (varlist endlist &body body)
239   #!+sb-doc
240   "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
241   Iteration construct. Each Var is initialized in parallel to the value of the
242   specified Init form. On subsequent iterations, the Vars are assigned the
243   value of the Step form (if any) in parallel. The Test is evaluated before
244   each evaluation of the body Forms. When the Test is true, the Exit-Forms
245   are evaluated as a PROGN, with the result being the value of the DO. A block
246   named NIL is established around the entire expansion, allowing RETURN to be
247   used as an alternate exit mechanism."
248   (do-do-body varlist endlist body 'let 'psetq 'do nil))
249 (defmacro-mundanely do* (varlist endlist &body body)
250   #!+sb-doc
251   "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
252   Iteration construct. Each Var is initialized sequentially (like LET*) to the
253   value of the specified Init form. On subsequent iterations, the Vars are
254   sequentially assigned the value of the Step form (if any). The Test is
255   evaluated before each evaluation of the body Forms. When the Test is true,
256   the Exit-Forms are evaluated as a PROGN, with the result being the value
257   of the DO. A block named NIL is established around the entire expansion,
258   allowing RETURN to be used as an laternate exit mechanism."
259   (do-do-body varlist endlist body 'let* 'setq 'do* nil))
260
261 ;;; DOTIMES and DOLIST could be defined more concisely using destructuring
262 ;;; macro lambda lists or DESTRUCTURING-BIND, but then it'd be tricky to use
263 ;;; them before those things were defined. They're used enough times before
264 ;;; destructuring mechanisms are defined that it looks as though it's worth
265 ;;; just implementing them ASAP, at the cost of being unable to use the
266 ;;; standard destructuring mechanisms.
267 (defmacro-mundanely dotimes (var-count-result &body body)
268   (multiple-value-bind ; to roll our own destructuring
269       (var count result)
270       (apply (lambda (var count &optional (result nil))
271                (values var count result))
272              var-count-result)
273     (cond ((numberp count)
274            `(do ((,var 0 (1+ ,var)))
275                 ((>= ,var ,count) ,result)
276               (declare (type unsigned-byte ,var))
277               ,@body))
278           (t (let ((v1 (gensym)))
279                `(do ((,var 0 (1+ ,var)) (,v1 ,count))
280                     ((>= ,var ,v1) ,result)
281                   (declare (type unsigned-byte ,var))
282                   ,@body))))))
283 (defmacro-mundanely dolist (var-list-result &body body)
284   (multiple-value-bind ; to roll our own destructuring
285       (var list result)
286       (apply (lambda (var list &optional (result nil))
287                (values var list result))
288              var-list-result)
289     ;; We repeatedly bind the var instead of setting it so that we never have
290     ;; to give the var an arbitrary value such as NIL (which might conflict
291     ;; with a declaration). If there is a result form, we introduce a
292     ;; gratuitous binding of the variable to NIL w/o the declarations, then
293     ;; evaluate the result form in that environment. We spuriously reference
294     ;; the gratuitous variable, since we don't want to use IGNORABLE on what
295     ;; might be a special var.
296     (let ((n-list (gensym)))
297       `(do ((,n-list ,list (cdr ,n-list)))
298            ((endp ,n-list)
299             ,@(if result
300                 `((let ((,var nil))
301                     ,var
302                     ,result))
303                 '(nil)))
304          (let ((,var (car ,n-list)))
305            ,@body)))))
306 \f
307 ;;;; miscellaneous
308
309 (defmacro-mundanely return (&optional (value nil))
310   `(return-from nil ,value))
311
312 (defmacro-mundanely psetq (&rest pairs)
313   #!+sb-doc
314   "SETQ {var value}*
315    Set the variables to the values, like SETQ, except that assignments
316    happen in parallel, i.e. no assignments take place until all the
317    forms have been evaluated."
318   ;; (This macro is used in the definition of DO, so we can't use DO in the
319   ;; definition of this macro without getting into confusing bootstrap issues.)
320   (prog ((lets nil)
321          (setqs nil)
322          (pairs pairs))
323     :again
324     (when (atom (cdr pairs))
325       (return `(let ,(nreverse lets)
326                  (setq ,@(nreverse setqs))
327                  nil)))
328     (let ((gen (gensym)))
329       (setq lets (cons `(,gen ,(cadr pairs)) lets)
330             setqs (list* gen (car pairs) setqs)
331             pairs (cddr pairs)))
332     (go :again)))
333
334 (defmacro-mundanely lambda (&whole whole args &body body)
335   (declare (ignore args body))
336   `#',whole)