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)
51 ;; The ANSI spec says that the primary value of VALUE-FORM must be
52 ;; returned. The general-case-handling code below doesn't do this
53 ;; correctly in the special case when there are no vars bound, so we
54 ;; handle this special case separately here.
56 `(multiple-value-bind (,g) ,value-form
58 ((list-of-symbols-p vars)
59 (let ((temps (make-gensym-list (length vars))))
60 `(multiple-value-bind ,temps ,value-form
61 ,@(mapcar #'(lambda (var temp)
65 (t (error "Vars is not a list of symbols: ~S" vars))))
67 (defmacro-mundanely multiple-value-list (value-form)
68 `(multiple-value-call #'list ,value-form))
70 ;;;; various conditional constructs
72 ;;; COND defined in terms of IF
73 (defmacro-mundanely cond (&rest clauses)
76 (let ((clause (first clauses)))
78 (error "Cond clause is not a list: ~S" clause)
79 (let ((test (first clause))
80 (forms (rest clause)))
82 (let ((n-result (gensym)))
83 `(let ((,n-result ,test))
86 (cond ,@(rest clauses)))))
89 (cond ,@(rest clauses)))))))))
91 ;;; other things defined in terms of COND
92 (defmacro-mundanely when (test &body forms)
94 "If the first argument is true, the rest of the forms are
95 evaluated as a PROGN."
96 `(cond (,test nil ,@forms)))
97 (defmacro-mundanely unless (test &body forms)
99 "If the first argument is not true, the rest of the forms are
100 evaluated as a PROGN."
101 `(cond ((not ,test) nil ,@forms)))
102 (defmacro-mundanely and (&rest forms)
103 (cond ((endp forms) t)
104 ((endp (rest forms)) (first forms))
109 (defmacro-mundanely or (&rest forms)
110 (cond ((endp forms) nil)
111 ((endp (rest forms)) (first forms))
113 (let ((n-result (gensym)))
114 `(let ((,n-result ,(first forms)))
117 (or ,@(rest forms))))))))
119 ;;;; various sequencing constructs
121 (defmacro-mundanely prog (varlist &body body-decls)
122 (multiple-value-bind (body decls) (parse-body body-decls nil)
128 (defmacro-mundanely prog* (varlist &body body-decls)
129 (multiple-value-bind (body decls) (parse-body body-decls nil)
135 (defmacro-mundanely prog1 (result &body body)
136 (let ((n-result (gensym)))
137 `(let ((,n-result ,result))
141 (defmacro-mundanely prog2 (form1 result &body body)
142 `(prog1 (progn ,form1 ,result) ,@body))
144 ;;; Now that we have the definition of MULTIPLE-VALUE-BIND, we can make a
145 ;;; reasonably readable definition of DEFUN.
147 ;;; DEFUN expands into %DEFUN which is a function that is treated
148 ;;; magically by the compiler (through an IR1 transform) in order to
149 ;;; handle stuff like inlining. After the compiler has gotten the
150 ;;; information it wants out of macro definition, it compiles a call
151 ;;; to %%DEFUN which happens at load time.
152 (defmacro-mundanely defun (&whole whole name args &body body)
153 (multiple-value-bind (forms decls doc) (parse-body body)
154 (let ((def `(lambda ,args
156 (block ,(function-name-block-name name)
158 `(sb!c::%defun ',name #',def ,doc ',whole))))
159 #+sb-xc-host (/show "before PROCLAIM" (sb!c::info :function :kind 'sb!c::%%defun))
160 #+sb-xc-host (sb!xc:proclaim '(ftype function sb!c::%%defun)) ; to avoid
161 ; undefined function warnings
162 #+sb-xc-host (/show "after PROCLAIM" (sb!c::info :function :kind 'sb!c::%%defun))
163 (defun sb!c::%%defun (name def doc &optional inline-expansion)
164 ;; When we're built as a cross-compiler, the DEF is a function
165 ;; implemented by the cross-compilation host, which is opaque to us.
166 ;; Similarly, other things like FDEFINITION or DOCUMENTATION either
167 ;; aren't ours to mess with or are meaningless to mess with. Thus,
169 #+sb-xc-host (declare (ignore def))
173 (style-warn "redefining ~S in DEFUN" name))
174 (setf (sb!xc:fdefinition name) def)
176 ;; FIXME: This should use shared SETF-name-parsing logic.
177 (if (and (consp name) (eq (first name) 'setf))
178 (setf (fdocumentation (second name) 'setf) doc)
179 (setf (fdocumentation name 'function) doc))))
180 ;; Other stuff remains meaningful whether we're cross-compiling or
182 (become-defined-function-name name)
183 (when (or inline-expansion
184 (info :function :inline-expansion name))
185 (setf (info :function :inline-expansion name)
189 ;;; FIXME: Now that the IR1 interpreter is going away and EVAL-WHEN is
190 ;;; becoming ANSI-compliant, it should be possible to merge this and
191 ;;; DEF-IR1-TRANSLATOR %DEFUN into a single DEFUN. (And does %%DEFUN
192 ;;; merge into that too? dunno..)
193 (defun sb!c::%defun (name def doc source)
194 (declare (ignore source))
197 (setf (sb!eval:interpreted-function-name def) name))
198 (flet ((set-type-info-from-def ()
199 (setf (info :function :type name)
200 #-sb-xc-host (extract-function-type def)
201 ;; When we're built as a cross-compiler, the DEF is
202 ;; a function implemented by the cross-compilation
203 ;; host, which is opaque to us, so we have to punt here.
204 #+sb-xc-host *universal-function-type*)))
205 (ecase (info :function :where-from name)
207 (setf (info :function :where-from name) :defined)
208 (set-type-info-from-def)
209 (when (info :function :assumed-type name)
210 (setf (info :function :assumed-type name) nil)))
213 (set-type-info-from-def)
214 ;; We shouldn't need to clear this here because it should be
215 ;; clear already (having been cleared when the last definition
217 (aver (null (info :function :assumed-type name))))))
218 (sb!c::%%defun name def doc))
220 ;;;; DEFVAR and DEFPARAMETER
222 (defmacro-mundanely defvar (var &optional (val nil valp) (doc nil docp))
224 "Define a global variable at top level. Declare the variable
225 SPECIAL and, optionally, initialize it. If the variable already has a
226 value, the old value is not clobbered. The third argument is an optional
227 documentation string for the variable."
229 (declaim (special ,var))
231 `((unless (boundp ',var)
234 `((funcall #'(setf fdocumentation) ',doc ',var 'variable)))
237 (defmacro-mundanely defparameter (var val &optional (doc nil docp))
239 "Define a parameter that is not normally changed by the program,
240 but that may be changed without causing an error. Declare the
241 variable special and sets its value to VAL, overwriting any
242 previous value. The third argument is an optional documentation
243 string for the parameter."
245 (declaim (special ,var))
248 ;; FIXME: The various FUNCALL #'(SETF FDOCUMENTATION) and
249 ;; other FUNCALL #'(SETF FOO) forms in the code should
250 ;; unbogobootstrapized back to ordinary SETF forms.
251 `((funcall #'(setf fdocumentation) ',doc ',var 'variable)))
254 ;;;; iteration constructs
256 ;;; (These macros are defined in terms of a function DO-DO-BODY which
257 ;;; is also used by SB!INT:DO-ANONYMOUS. Since these macros should not
258 ;;; be loaded on the cross-compilation host, but SB!INT:DO-ANONYMOUS
259 ;;; and DO-DO-BODY should be, these macros can't conveniently be in
260 ;;; the same file as DO-DO-BODY.)
261 (defmacro-mundanely do (varlist endlist &body body)
263 "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
264 Iteration construct. Each Var is initialized in parallel to the value of the
265 specified Init form. On subsequent iterations, the Vars are assigned the
266 value of the Step form (if any) in parallel. The Test is evaluated before
267 each evaluation of the body Forms. When the Test is true, the Exit-Forms
268 are evaluated as a PROGN, with the result being the value of the DO. A block
269 named NIL is established around the entire expansion, allowing RETURN to be
270 used as an alternate exit mechanism."
271 (do-do-body varlist endlist body 'let 'psetq 'do nil))
272 (defmacro-mundanely do* (varlist endlist &body body)
274 "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
275 Iteration construct. Each Var is initialized sequentially (like LET*) to the
276 value of the specified Init form. On subsequent iterations, the Vars are
277 sequentially assigned the value of the Step form (if any). The Test is
278 evaluated before each evaluation of the body Forms. When the Test is true,
279 the Exit-Forms are evaluated as a PROGN, with the result being the value
280 of the DO. A block named NIL is established around the entire expansion,
281 allowing RETURN to be used as an laternate exit mechanism."
282 (do-do-body varlist endlist body 'let* 'setq 'do* nil))
284 ;;; DOTIMES and DOLIST could be defined more concisely using
285 ;;; destructuring macro lambda lists or DESTRUCTURING-BIND, but then
286 ;;; it'd be tricky to use them before those things were defined.
287 ;;; They're used enough times before destructuring mechanisms are
288 ;;; defined that it looks as though it's worth just implementing them
289 ;;; ASAP, at the cost of being unable to use the standard
290 ;;; destructuring mechanisms.
291 (defmacro-mundanely dotimes (var-count-result &body body)
292 (multiple-value-bind ; to roll our own destructuring
294 (apply (lambda (var count &optional (result nil))
295 (values var count result))
297 (cond ((numberp count)
298 `(do ((,var 0 (1+ ,var)))
299 ((>= ,var ,count) ,result)
300 (declare (type unsigned-byte ,var))
302 (t (let ((v1 (gensym)))
303 `(do ((,var 0 (1+ ,var)) (,v1 ,count))
304 ((>= ,var ,v1) ,result)
305 (declare (type unsigned-byte ,var))
307 (defmacro-mundanely dolist (var-list-result &body body)
308 (multiple-value-bind ; to roll our own destructuring
310 (apply (lambda (var list &optional (result nil))
311 (values var list result))
313 ;; We repeatedly bind the var instead of setting it so that we
314 ;; never have to give the var an arbitrary value such as NIL
315 ;; (which might conflict with a declaration). If there is a result
316 ;; form, we introduce a gratuitous binding of the variable to NIL
317 ;; without the declarations, then evaluate the result form in that
318 ;; environment. We spuriously reference the gratuitous variable,
319 ;; since we don't want to use IGNORABLE on what might be a special
321 (let ((n-list (gensym)))
322 `(do ((,n-list ,list (cdr ,n-list)))
329 (let ((,var (car ,n-list)))
334 (defmacro-mundanely return (&optional (value nil))
335 `(return-from nil ,value))
337 (defmacro-mundanely psetq (&rest pairs)
340 Set the variables to the values, like SETQ, except that assignments
341 happen in parallel, i.e. no assignments take place until all the
342 forms have been evaluated."
343 ;; (This macro is used in the definition of DO, so we can't use DO in the
344 ;; definition of this macro without getting into confusing bootstrap issues.)
349 (when (atom (cdr pairs))
350 (return `(let ,(nreverse lets)
351 (setq ,@(nreverse setqs))
353 (let ((gen (gensym)))
354 (setq lets (cons `(,gen ,(cadr pairs)) lets)
355 setqs (list* gen (car pairs) setqs)
359 (defmacro-mundanely lambda (&whole whole args &body body)
360 (declare (ignore args body))