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)
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)))
54 (defmacro-mundanely multiple-value-list (value-form)
55 `(multiple-value-call #'list ,value-form))
57 ;;;; various conditional constructs
59 ;;; COND defined in terms of IF
60 (defmacro-mundanely cond (&rest clauses)
63 (let ((clause (first clauses)))
65 (error "COND clause is not a list: ~S" clause)
66 (let ((test (first clause))
67 (forms (rest clause)))
69 (let ((n-result (gensym)))
70 `(let ((,n-result ,test))
73 (cond ,@(rest clauses)))))
76 (cond ,@(rest clauses)))))))))
78 ;;; other things defined in terms of COND
79 (defmacro-mundanely when (test &body forms)
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)
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))
96 (defmacro-mundanely or (&rest forms)
97 (cond ((endp forms) nil)
98 ((endp (rest forms)) (first forms))
100 (let ((n-result (gensym)))
101 `(let ((,n-result ,(first forms)))
104 (or ,@(rest forms))))))))
106 ;;;; various sequencing constructs
108 (defmacro-mundanely prog (varlist &body body-decls)
109 (multiple-value-bind (body decls) (parse-body body-decls nil)
115 (defmacro-mundanely prog* (varlist &body body-decls)
116 (multiple-value-bind (body decls) (parse-body body-decls nil)
122 (defmacro-mundanely prog1 (result &body body)
123 (let ((n-result (gensym)))
124 `(let ((,n-result ,result))
128 (defmacro-mundanely prog2 (form1 result &body body)
129 `(prog1 (progn ,form1 ,result) ,@body))
133 ;;; Should we save the inline expansion of the function named NAME?
134 (defun inline-fun-name-p (name)
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))
142 ;; (DECLAIM (NOTINLINE FOO))
143 ;; has been used, and then we later do another
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)))
152 (defmacro-mundanely defun (&environment env name args &body body)
153 "Define a function at top level."
155 (unless (symbol-package (fun-name-block-name name))
156 (warn "DEFUN of uninterned symbol ~S (tricky for GENESIS)" name))
157 (multiple-value-bind (forms decls doc) (parse-body body)
158 (let* (;; stuff shared between LAMBDA and INLINE-LAMBDA and NAMED-LAMBDA
161 (block ,(fun-name-block-name name)
163 (lambda `(lambda ,@lambda-guts))
165 (named-lambda `(named-lambda ,name ,@lambda-guts))
167 (when (inline-fun-name-p name)
168 ;; we want to attempt to inline, so complain if we can't
169 (or (sb!c:maybe-inline-syntactic-closure lambda env)
172 #-sb-xc-host sb!c:maybe-compiler-note
173 "lexical environment too hairy, can't inline DEFUN ~S"
178 ;; In cross-compilation of toplevel DEFUNs, we arrange
179 ;; for the LAMBDA to be statically linked by GENESIS.
181 ;; It may seem strangely inconsistent not to use NAMED-LAMBDA
182 ;; here instead of LAMBDA. The reason is historical:
183 ;; COLD-FSET was written before NAMED-LAMBDA, and has special
184 ;; logic of its own to notify the compiler about NAME.
186 (cold-fset ,name ,lambda)
188 (eval-when (:compile-toplevel :load-toplevel :execute)
189 (sb!c:%compiler-defun ',name ',inline-lambda))
192 ;; In normal compilation (not for cold load) this is
193 ;; where the compiled LAMBDA first appears. In
194 ;; cross-compilation, we manipulate the
195 ;; previously-statically-linked LAMBDA here.
196 #-sb-xc-host ,named-lambda
197 #+sb-xc-host (fdefinition ',name)
200 (defun %defun (name def doc)
201 (declare (type function def))
202 (declare (type (or null simple-string doc)))
203 (aver (legal-fun-name-p name)) ; should've been checked by DEFMACRO DEFUN
205 (/show0 "redefining NAME in %DEFUN")
206 (style-warn "redefining ~S in DEFUN" name))
207 (setf (sb!xc:fdefinition name) def)
209 ;; FIXME: I want to do this here (and fix bug 137), but until the
210 ;; breathtaking CMU CL function name architecture is converted into
211 ;; something sane, (1) doing so doesn't really fix the bug, and
212 ;; (2) doing probably isn't even really safe.
213 #+nil (setf (%fun-name def) name)
216 (setf (fdocumentation name 'function) doc))
219 ;;;; DEFVAR and DEFPARAMETER
221 (defmacro-mundanely defvar (var &optional (val nil valp) (doc nil docp))
223 "Define a global variable at top level. Declare the variable
224 SPECIAL and, optionally, initialize it. If the variable already has a
225 value, the old value is not clobbered. The third argument is an optional
226 documentation string for the variable."
228 (declaim (special ,var))
230 `((unless (boundp ',var)
233 `((setf (fdocumentation ',var 'variable) ',doc )))
236 (defmacro-mundanely defparameter (var val &optional (doc nil docp))
238 "Define a parameter that is not normally changed by the program,
239 but that may be changed without causing an error. Declare the
240 variable special and sets its value to VAL, overwriting any
241 previous value. The third argument is an optional documentation
242 string for the parameter."
244 (declaim (special ,var))
247 `((setf (fdocumentation ',var 'variable) ',doc)))
250 ;;;; iteration constructs
252 ;;; (These macros are defined in terms of a function FROB-DO-BODY which
253 ;;; is also used by SB!INT:DO-ANONYMOUS. Since these macros should not
254 ;;; be loaded on the cross-compilation host, but SB!INT:DO-ANONYMOUS
255 ;;; and FROB-DO-BODY should be, these macros can't conveniently be in
256 ;;; the same file as FROB-DO-BODY.)
257 (defmacro-mundanely do (varlist endlist &body body)
259 "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
260 Iteration construct. Each Var is initialized in parallel to the value of the
261 specified Init form. On subsequent iterations, the Vars are assigned the
262 value of the Step form (if any) in parallel. The Test is evaluated before
263 each evaluation of the body Forms. When the Test is true, the Exit-Forms
264 are evaluated as a PROGN, with the result being the value of the DO. A block
265 named NIL is established around the entire expansion, allowing RETURN to be
266 used as an alternate exit mechanism."
267 (frob-do-body varlist endlist body 'let 'psetq 'do nil))
268 (defmacro-mundanely do* (varlist endlist &body body)
270 "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
271 Iteration construct. Each Var is initialized sequentially (like LET*) to the
272 value of the specified Init form. On subsequent iterations, the Vars are
273 sequentially assigned the value of the Step form (if any). The Test is
274 evaluated before each evaluation of the body Forms. When the Test is true,
275 the Exit-Forms are evaluated as a PROGN, with the result being the value
276 of the DO. A block named NIL is established around the entire expansion,
277 allowing RETURN to be used as an laternate exit mechanism."
278 (frob-do-body varlist endlist body 'let* 'setq 'do* nil))
280 ;;; DOTIMES and DOLIST could be defined more concisely using
281 ;;; destructuring macro lambda lists or DESTRUCTURING-BIND, but then
282 ;;; it'd be tricky to use them before those things were defined.
283 ;;; They're used enough times before destructuring mechanisms are
284 ;;; defined that it looks as though it's worth just implementing them
285 ;;; ASAP, at the cost of being unable to use the standard
286 ;;; destructuring mechanisms.
287 (defmacro-mundanely dotimes ((var count &optional (result nil)) &body body)
288 (cond ((numberp count)
289 `(do ((,var 0 (1+ ,var)))
290 ((>= ,var ,count) ,result)
291 (declare (type unsigned-byte ,var))
293 (t (let ((v1 (gensym)))
294 `(do ((,var 0 (1+ ,var)) (,v1 ,count))
295 ((>= ,var ,v1) ,result)
296 (declare (type unsigned-byte ,var))
299 (defmacro-mundanely dolist ((var list &optional (result nil)) &body body)
300 ;; We repeatedly bind the var instead of setting it so that we never
301 ;; have to give the var an arbitrary value such as NIL (which might
302 ;; conflict with a declaration). If there is a result form, we
303 ;; introduce a gratuitous binding of the variable to NIL without the
304 ;; declarations, then evaluate the result form in that
305 ;; environment. We spuriously reference the gratuitous variable,
306 ;; since we don't want to use IGNORABLE on what might be a special
308 (multiple-value-bind (forms decls) (parse-body body nil)
309 (let ((n-list (gensym)))
310 `(do* ((,n-list ,list (cdr ,n-list)))
317 (let ((,var (car ,n-list)))
324 (defmacro-mundanely return (&optional (value nil))
325 `(return-from nil ,value))
327 (defmacro-mundanely psetq (&rest pairs)
330 Set the variables to the values, like SETQ, except that assignments
331 happen in parallel, i.e. no assignments take place until all the
332 forms have been evaluated."
333 ;; Given the possibility of symbol-macros, we delegate to PSETF
334 ;; which knows how to deal with them, after checking that syntax is
335 ;; compatible with PSETQ.
336 (do ((pair pairs (cddr pair)))
337 ((endp pair) `(psetf ,@pairs))
338 (unless (symbolp (car pair))
339 (error 'simple-program-error
340 :format-control "variable ~S in PSETQ is not a SYMBOL"
341 :format-arguments (list (car pair))))))
343 (defmacro-mundanely lambda (&whole whole args &body body)
344 (declare (ignore args body))
347 (defmacro-mundanely named-lambda (&whole whole name args &body body)
348 (declare (ignore name args body))
351 (defmacro-mundanely lambda-with-lexenv (&whole whole
352 declarations macros symbol-macros
354 (declare (ignore declarations macros symbol-macros body))