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")
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))))
32 ;;; MULTIPLE-VALUE-FOO
34 (defun list-of-symbols-p (x)
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))
45 (let ((ignore (gensym)))
46 `(multiple-value-call #'(lambda (&optional ,@vars &rest ,ignore)
47 (declare (ignore ,ignore))
50 (error "Vars is not a list of symbols: ~S" vars)))
52 (defmacro-mundanely multiple-value-setq (vars value-form)
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.
59 `(multiple-value-bind (,g) ,value-form
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)
68 (t (error "Vars is not a list of symbols: ~S" vars))))
70 (defmacro-mundanely multiple-value-list (value-form)
71 `(multiple-value-call #'list ,value-form))
73 ;;;; various conditional constructs
75 ;;; COND defined in terms of IF
76 (defmacro-mundanely cond (&rest clauses)
79 (let ((clause (first clauses)))
81 (error "Cond clause is not a list: ~S" clause)
82 (let ((test (first clause))
83 (forms (rest clause)))
85 (let ((n-result (gensym)))
86 `(let ((,n-result ,test))
89 (cond ,@(rest clauses)))))
92 (cond ,@(rest clauses)))))))))
94 ;;; other things defined in terms of COND
95 (defmacro-mundanely when (test &body forms)
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)
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))
112 (defmacro-mundanely or (&rest forms)
113 (cond ((endp forms) nil)
114 ((endp (rest forms)) (first forms))
116 (let ((n-result (gensym)))
117 `(let ((,n-result ,(first forms)))
120 (or ,@(rest forms))))))))
122 ;;;; various sequencing constructs
124 (defmacro-mundanely prog (varlist &body body-decls)
125 (multiple-value-bind (body decls) (parse-body body-decls nil)
131 (defmacro-mundanely prog* (varlist &body body-decls)
132 (multiple-value-bind (body decls) (parse-body body-decls nil)
138 (defmacro-mundanely prog1 (result &body body)
139 (let ((n-result (gensym)))
140 `(let ((,n-result ,result))
144 (defmacro-mundanely prog2 (form1 result &body body)
145 `(prog1 (progn ,form1 ,result) ,@body))
147 ;;; Now that we have the definition of MULTIPLE-VALUE-BIND, we can make a
148 ;;; reasonably readable definition of DEFUN.
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
159 (block ,(function-name-block-name name)
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)
168 (style-warn "redefining ~S in DEFUN" name))
169 (setf (sb!xc:fdefinition name) def)
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)
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)
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))
198 ;;;; DEFVAR and DEFPARAMETER
200 (defmacro-mundanely defvar (var &optional (val nil valp) (doc nil docp))
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."
207 (declaim (special ,var))
209 `((unless (boundp ',var)
212 `((funcall #'(setf fdocumentation) ',doc ',var 'variable)))
215 (defmacro-mundanely defparameter (var val &optional (doc nil docp))
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."
222 (declaim (special ,var))
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)))
231 ;;;; iteration constructs
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
238 (defmacro-mundanely do (varlist endlist &body body)
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)
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))
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
270 (apply (lambda (var count &optional (result nil))
271 (values var count result))
273 (cond ((numberp count)
274 `(do ((,var 0 (1+ ,var)))
275 ((>= ,var ,count) ,result)
276 (declare (type unsigned-byte ,var))
278 (t (let ((v1 (gensym)))
279 `(do ((,var 0 (1+ ,var)) (,v1 ,count))
280 ((>= ,var ,v1) ,result)
281 (declare (type unsigned-byte ,var))
283 (defmacro-mundanely dolist (var-list-result &body body)
284 (multiple-value-bind ; to roll our own destructuring
286 (apply (lambda (var list &optional (result nil))
287 (values 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)))
304 (let ((,var (car ,n-list)))
309 (defmacro-mundanely return (&optional (value nil))
310 `(return-from nil ,value))
312 (defmacro-mundanely psetq (&rest pairs)
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.)
324 (when (atom (cdr pairs))
325 (return `(let ,(nreverse lets)
326 (setq ,@(nreverse setqs))
328 (let ((gen (gensym)))
329 (setq lets (cons `(,gen ,(cadr pairs)) lets)
330 setqs (list* gen (car pairs) setqs)
334 (defmacro-mundanely lambda (&whole whole args &body body)
335 (declare (ignore args body))