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))
146 ;;; Should we save the inline expansion of the function named NAME?
147 (defun inline-fun-name-p (name)
149 ;; the normal reason for saving the inline expansion
150 (info :function :inlinep name)
151 ;; another reason for saving the inline expansion: If the
152 ;; ANSI-recommended idiom
153 ;; (DECLAIM (INLINE FOO))
155 ;; (DECLAIM (NOTINLINE FOO))
156 ;; has been used, and then we later do another
158 ;; without a preceding
159 ;; (DECLAIM (INLINE FOO))
160 ;; what should we do with the old inline expansion when we see the
161 ;; new DEFUN? Overwriting it with the new definition seems like
162 ;; the only unsurprising choice.
163 (info :function :inline-expansion-designator name)))
165 ;;; Now that we have the definition of MULTIPLE-VALUE-BIND, we can
166 ;;; make a reasonably readable definition of DEFUN.
167 (defmacro-mundanely defun (&environment env name args &body body)
168 "Define a function at top level."
170 (unless (symbol-package (fun-name-block-name name))
171 (warn "DEFUN of uninterned symbol ~S (tricky for GENESIS)" name))
172 (multiple-value-bind (forms decls doc) (parse-body body)
173 (let* (;; stuff shared between LAMBDA and INLINE-LAMBDA and NAMED-LAMBDA
176 (block ,(fun-name-block-name name)
178 (lambda `(lambda ,@lambda-guts))
180 (named-lambda `(named-lambda ,name ,@lambda-guts))
182 (cond (;; Does the user not even want to inline?
183 (not (inline-fun-name-p name))
185 (;; Does inlining look too hairy to handle?
186 (not (sb!c:lambda-independent-of-lexenv-p lambda env))
187 (sb!c:maybe-compiler-note
188 "lexical environment too hairy, can't inline DEFUN ~S"
192 ;; FIXME: The only reason that we return
193 ;; LAMBDA-WITH-LEXENV instead of returning bare
194 ;; LAMBDA is to avoid modifying downstream code
195 ;; which expects LAMBDA-WITH-LEXENV. But the code
196 ;; here is the only code which feeds into the
197 ;; downstream code, and the generality of the
198 ;; interface is no longer used, so it'd make sense
199 ;; to simplify the interface instead of using the
200 ;; old general LAMBDA-WITH-LEXENV interface in this
202 `(sb!c:lambda-with-lexenv
203 nil nil nil ; i.e. no DECLS, no MACROS, no SYMMACS
207 ;; In cross-compilation of toplevel DEFUNs, we arrange
208 ;; for the LAMBDA to be statically linked by GENESIS.
210 ;; It may seem strangely inconsistent not to use NAMED-LAMBDA
211 ;; here instead of LAMBDA. The reason is historical:
212 ;; COLD-FSET was written before NAMED-LAMBDA, and has special
213 ;; logic of its own to notify the compiler about NAME.
215 (cold-fset ,name ,lambda)
217 (eval-when (:compile-toplevel :load-toplevel :execute)
218 (sb!c:%compiler-defun ',name ',inline-lambda))
221 ;; In normal compilation (not for cold load) this is
222 ;; where the compiled LAMBDA first appears. In
223 ;; cross-compilation, we manipulate the
224 ;; previously-statically-linked LAMBDA here.
225 #-sb-xc-host ,named-lambda
226 #+sb-xc-host (fdefinition ',name)
229 (defun %defun (name def doc)
230 (declare (type function def))
231 (declare (type (or null simple-string doc)))
232 (aver (legal-fun-name-p name))
234 (/show0 "redefining NAME in %DEFUN")
235 (style-warn "redefining ~S in DEFUN" name))
236 (setf (sb!xc:fdefinition name) def)
238 ;; FIXME: I want to do this here (and fix bug 137), but until the
239 ;; breathtaking CMU CL function name architecture is converted into
240 ;; something sane, (1) doing so doesn't really fix the bug, and
241 ;; (2) doing probably isn't even really safe.
242 #+nil (setf (%fun-name def) name)
245 ;; FIXME: This should use shared SETF-name-parsing logic.
246 (if (and (consp name) (eq (first name) 'setf))
247 (setf (fdocumentation (second name) 'setf) doc)
248 (setf (fdocumentation (the symbol name) 'function) doc)))
251 ;;;; DEFVAR and DEFPARAMETER
253 (defmacro-mundanely defvar (var &optional (val nil valp) (doc nil docp))
255 "Define a global variable at top level. Declare the variable
256 SPECIAL and, optionally, initialize it. If the variable already has a
257 value, the old value is not clobbered. The third argument is an optional
258 documentation string for the variable."
260 (declaim (special ,var))
262 `((unless (boundp ',var)
265 `((funcall #'(setf fdocumentation) ',doc ',var 'variable)))
268 (defmacro-mundanely defparameter (var val &optional (doc nil docp))
270 "Define a parameter that is not normally changed by the program,
271 but that may be changed without causing an error. Declare the
272 variable special and sets its value to VAL, overwriting any
273 previous value. The third argument is an optional documentation
274 string for the parameter."
276 (declaim (special ,var))
279 ;; FIXME: The various FUNCALL #'(SETF FDOCUMENTATION) and
280 ;; other FUNCALL #'(SETF FOO) forms in the code should
281 ;; unbogobootstrapized back to ordinary SETF forms.
282 `((funcall #'(setf fdocumentation) ',doc ',var 'variable)))
285 ;;;; iteration constructs
287 ;;; (These macros are defined in terms of a function FROB-DO-BODY which
288 ;;; is also used by SB!INT:DO-ANONYMOUS. Since these macros should not
289 ;;; be loaded on the cross-compilation host, but SB!INT:DO-ANONYMOUS
290 ;;; and FROB-DO-BODY should be, these macros can't conveniently be in
291 ;;; the same file as FROB-DO-BODY.)
292 (defmacro-mundanely do (varlist endlist &body body)
294 "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
295 Iteration construct. Each Var is initialized in parallel to the value of the
296 specified Init form. On subsequent iterations, the Vars are assigned the
297 value of the Step form (if any) in parallel. The Test is evaluated before
298 each evaluation of the body Forms. When the Test is true, the Exit-Forms
299 are evaluated as a PROGN, with the result being the value of the DO. A block
300 named NIL is established around the entire expansion, allowing RETURN to be
301 used as an alternate exit mechanism."
302 (frob-do-body varlist endlist body 'let 'psetq 'do nil))
303 (defmacro-mundanely do* (varlist endlist &body body)
305 "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
306 Iteration construct. Each Var is initialized sequentially (like LET*) to the
307 value of the specified Init form. On subsequent iterations, the Vars are
308 sequentially assigned the value of the Step form (if any). The Test is
309 evaluated before each evaluation of the body Forms. When the Test is true,
310 the Exit-Forms are evaluated as a PROGN, with the result being the value
311 of the DO. A block named NIL is established around the entire expansion,
312 allowing RETURN to be used as an laternate exit mechanism."
313 (frob-do-body varlist endlist body 'let* 'setq 'do* nil))
315 ;;; DOTIMES and DOLIST could be defined more concisely using
316 ;;; destructuring macro lambda lists or DESTRUCTURING-BIND, but then
317 ;;; it'd be tricky to use them before those things were defined.
318 ;;; They're used enough times before destructuring mechanisms are
319 ;;; defined that it looks as though it's worth just implementing them
320 ;;; ASAP, at the cost of being unable to use the standard
321 ;;; destructuring mechanisms.
322 (defmacro-mundanely dotimes (var-count-result &body body)
323 (multiple-value-bind ; to roll our own destructuring
325 (apply (lambda (var count &optional (result nil))
326 (values var count result))
328 (cond ((numberp count)
329 `(do ((,var 0 (1+ ,var)))
330 ((>= ,var ,count) ,result)
331 (declare (type unsigned-byte ,var))
333 (t (let ((v1 (gensym)))
334 `(do ((,var 0 (1+ ,var)) (,v1 ,count))
335 ((>= ,var ,v1) ,result)
336 (declare (type unsigned-byte ,var))
338 (defmacro-mundanely dolist (var-list-result &body body)
339 (multiple-value-bind ; to roll our own destructuring
341 (apply (lambda (var list &optional (result nil))
342 (values var list result))
344 ;; We repeatedly bind the var instead of setting it so that we
345 ;; never have to give the var an arbitrary value such as NIL
346 ;; (which might conflict with a declaration). If there is a result
347 ;; form, we introduce a gratuitous binding of the variable to NIL
348 ;; without the declarations, then evaluate the result form in that
349 ;; environment. We spuriously reference the gratuitous variable,
350 ;; since since we don't want to use IGNORABLE on what might be a
352 (let ((n-list (gensym)))
353 `(do ((,n-list ,list (cdr ,n-list)))
360 (let ((,var (car ,n-list)))
365 (defmacro-mundanely return (&optional (value nil))
366 `(return-from nil ,value))
368 (defmacro-mundanely psetq (&rest pairs)
371 Set the variables to the values, like SETQ, except that assignments
372 happen in parallel, i.e. no assignments take place until all the
373 forms have been evaluated."
374 ;; (This macro is used in the definition of DO, so we can't use DO in the
375 ;; definition of this macro without getting into confusing bootstrap issues.)
380 (when (atom (cdr pairs))
381 (return `(let ,(nreverse lets)
382 (setq ,@(nreverse setqs))
384 (let ((gen (gensym)))
385 (setq lets (cons `(,gen ,(cadr pairs)) lets)
386 setqs (list* gen (car pairs) setqs)
390 (defmacro-mundanely lambda (&whole whole args &body body)
391 (declare (ignore args body))