1 ;;;; various user-level definitions which need to be done particularly
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
15 ;;;; target constants which need to appear as early as possible
17 ;;; an internal tag for marking empty slots, which needs to be defined
18 ;;; as early as possible because it appears in macroexpansions for
19 ;;; iteration over hash tables
21 ;;; CMU CL 18b used :EMPTY for this purpose, which was somewhat nasty
22 ;;; since it's easily accessible to the user, so that e.g.
23 ;;; (DEFVAR *HT* (MAKE-HASH-TABLE))
24 ;;; (SETF (GETHASH :EMPTY *HT*) :EMPTY)
25 ;;; (MAPHASH (LAMBDA (K V) (FORMAT T "~&~S ~S~%" K V)))
26 ;;; gives no output -- oops!
28 ;;; FIXME: It'd probably be good to use the unbound marker for this.
29 ;;; However, there might be some gotchas involving assumptions by
30 ;;; e.g. AREF that they're not going to return the unbound marker,
31 ;;; and there's also the noted-below problem that the C-level code
32 ;;; contains implicit assumptions about this marker.
34 ;;; KLUDGE: Note that as of version 0.6.6 there's a dependence in the
35 ;;; gencgc.c code on this value being a symbol. (This is only one of
36 ;;; many nasty dependencies between that code and this, alas.)
38 (defconstant +empty-ht-slot+ '%empty-ht-slot%)
39 ;;; KLUDGE: Using a private symbol still leaves us vulnerable to users
40 ;;; getting nonconforming behavior by messing around with
41 ;;; DO-ALL-SYMBOLS. That seems like a fairly obscure problem, so for
42 ;;; now we just don't worry about it. If for some reason it becomes
43 ;;; worrisome and the magic value needs replacement:
44 ;;; * The replacement value needs to be LOADable with EQL preserved,
45 ;;; so that the macroexpansion for WITH-HASH-TABLE-ITERATOR will
46 ;;; work when compiled into a file and loaded back into SBCL.
47 ;;; (Thus, just uninterning %EMPTY-HT-SLOT% doesn't work.)
48 ;;; * The replacement value needs to be acceptable to the
49 ;;; low-level gencgc.lisp hash table scavenging code.
50 ;;; * The change will break binary compatibility, since comparisons
51 ;;; against the value used at the time of compilation are wired
55 ;;;; DO-related stuff which needs to be visible on the cross-compilation host
57 (eval-when (:compile-toplevel :load-toplevel :execute)
58 (defun do-do-body (varlist endlist decls-and-code bind step name block)
59 (let* ((r-inits nil) ; accumulator for reversed list
60 (r-steps nil) ; accumulator for reversed list
63 ;; Check for illegal old-style DO.
64 (when (or (not (listp varlist)) (atom endlist))
65 (error "ill-formed ~S -- possibly illegal old style DO?" name))
66 ;; Parse VARLIST to get R-INITS and R-STEPS.
68 (flet (;; (We avoid using CL:PUSH here so that CL:PUSH can be
69 ;; defined in terms of CL:SETF, and CL:SETF can be
70 ;; defined in terms of CL:DO, and CL:DO can be defined
71 ;; in terms of the current function.)
73 (setq r-inits (cons x r-inits)))
74 ;; common error-handling
76 (error "~S is an illegal form for a ~S varlist." v name)))
77 (cond ((symbolp v) (push-on-r-inits v))
79 (unless (symbolp (first v))
80 (error "~S step variable is not a symbol: ~S"
83 (let ((lv (length v)))
84 ;; (We avoid using CL:CASE here so that CL:CASE can
85 ;; be defined in terms of CL:SETF, and CL:SETF can
86 ;; be defined in terms of CL:DO, and CL:DO can be
87 ;; defined in terms of the current function.)
89 (push-on-r-inits (first v)))
93 (push-on-r-inits (list (first v) (second v)))
94 (setq r-steps (list* (third v) (first v) r-steps)))
95 (t (illegal-varlist)))))
96 (t (illegal-varlist)))))
97 ;; Construct the new form.
98 (multiple-value-bind (code decls) (parse-body decls-and-code nil)
100 (,bind ,(nreverse r-inits)
106 (,step ,@(nreverse r-steps))
108 (unless ,(first endlist) (go ,label-1))
109 (return-from ,block (progn ,@(rest endlist))))))))))
111 ;;; This is like DO, except it has no implicit NIL block. Each VAR is
112 ;;; initialized in parallel to the value of the specified INIT form.
113 ;;; On subsequent iterations, the VARS are assigned the value of the
114 ;;; STEP form (if any) in parallel. The TEST is evaluated before each
115 ;;; evaluation of the body FORMS. When the TEST is true, the
116 ;;; EXIT-FORMS are evaluated as a PROGN, with the result being the
118 (defmacro do-anonymous (varlist endlist &rest body)
119 (do-do-body varlist endlist body 'let 'psetq 'do-anonymous (gensym)))
123 ;;; Concatenate together the names of some strings and symbols,
124 ;;; producing a symbol in the current package.
125 (eval-when (:compile-toplevel :load-toplevel :execute)
126 (defun symbolicate (&rest things)
127 (values (intern (apply #'concatenate
129 (mapcar #'string things))))))
131 ;;; like SYMBOLICATE, but producing keywords
132 (defun keywordicate (&rest things)
133 (let ((*package* *keyword-package*))
134 (apply #'symbolicate things)))
136 ;;; Access *PACKAGE* in a way which lets us recover when someone has
137 ;;; done something silly like (SETF *PACKAGE* :CL-USER). (Such an
138 ;;; assignment is undefined behavior, so it's sort of reasonable for
139 ;;; it to cause the system to go totally insane afterwards, but it's a
140 ;;; fairly easy mistake to make, so let's try to recover gracefully
142 (defun sane-package ()
143 (let ((maybe-package *package*))
144 (cond ((and (packagep maybe-package)
145 ;; For good measure, we also catch the problem of
146 ;; *PACKAGE* being bound to a deleted package.
147 ;; Technically, this is not undefined behavior in itself,
148 ;; but it will immediately lead to undefined to behavior,
149 ;; since almost any operation on a deleted package is
151 (package-name maybe-package))
154 ;; We're in the undefined behavior zone. First, munge the
155 ;; system back into a defined state.
156 (let ((really-package (find-package :cl-user)))
157 (setf *package* really-package)
159 (error 'simple-type-error
161 :expected-type '(and package (satisfies package-name))
163 "~@<~S can't be a ~A: ~2I~_~S has been reset to ~S.~:>"
164 :format-arguments (list '*package*
165 (if (packagep maybe-package)
167 (type-of maybe-package))
168 '*package* really-package)))))))
170 ;;; Give names to elements of a numeric sequence.
171 (defmacro defenum ((&key (prefix "") (suffix "") (start 0) (step 1))
177 (dolist (id identifiers)
179 (multiple-value-bind (root docs)
181 (values (car id) (cdr id))
183 (push `(defconstant ,(symbolicate prefix root suffix)
184 ,(+ start (* step index))
189 ,@(nreverse results))))
191 ;;; generalization of DEFCONSTANT to values which are the same not
192 ;;; under EQL but under e.g. EQUAL or EQUALP
194 ;;; DEFCONSTANT-EQX is to be used instead of DEFCONSTANT for values
195 ;;; which are appropriately compared using the function given by the
196 ;;; EQX argument instead of EQL.
198 ;;; Note: Be careful when using this macro, since it's easy to
199 ;;; unintentionally pessimize your code. A good time to use this macro
200 ;;; is when the values defined will be fed into optimization
201 ;;; transforms and never actually appear in the generated code; this
202 ;;; is especially common when defining BYTE expressions. Unintentional
203 ;;; pessimization can result when the values defined by this macro are
204 ;;; actually used in generated code: because of the way that the
205 ;;; dump/load system works, you'll typically get one copy of consed
206 ;;; structure for each object file which contains code referring to
207 ;;; the value, plus perhaps one more copy bound to the SYMBOL-VALUE of
208 ;;; the constant. If you don't want that to happen, you should
209 ;;; probably use DEFPARAMETER instead.
210 (defmacro defconstant-eqx (symbol expr eqx &optional doc)
211 (let ((expr-tmp (gensym "EXPR-TMP-")))
213 ;; When we're building the cross-compiler, and in most
214 ;; situations even when we're running the cross-compiler,
215 ;; all we need is a nice portable definition in terms of the
216 ;; ANSI Common Lisp operations.
217 (eval-when (:compile-toplevel :load-toplevel :execute)
218 (let ((,expr-tmp ,expr))
219 (cond ((boundp ',symbol)
220 (unless (and (constantp ',symbol)
222 (symbol-value ',symbol)
224 (error "already bound differently: ~S")))
227 ;; KLUDGE: This is a very ugly hack, to be able to
228 ;; build SBCL with CMU CL (2.4.19), because there
229 ;; seems to be some confusion in CMU CL about
230 ;; ,EXPR-TEMP at EVAL-WHEN time ... -- MNA 2000-02-23
233 ,@(when doc `(,doc)))))))
234 ;; The #+SB-XC :COMPILE-TOPLEVEL situation is special, since we
235 ;; want to define the symbol not just in the cross-compilation
236 ;; host Lisp (which was handled above) but also in the
237 ;; cross-compiler (which we will handle now).
239 ;; KLUDGE: It would probably be possible to do this fairly
240 ;; cleanly, in a way parallel to the code above, if we had
241 ;; SB!XC:FOO versions of all the primitives CL:FOO used above
242 ;; (e.g. SB!XC:BOUNDP, SB!XC:SYMBOL-VALUE, and
243 ;; SB!XC:DEFCONSTANT), and took care to call them. But right
244 ;; now we just hack around in the guts of the cross-compiler
245 ;; instead. -- WHN 2000-11-03
247 (eval-when (:compile-toplevel)
248 (let ((,expr-tmp ,symbol))
249 (unless (and (eql (info :variable :kind ',symbol) :constant)
251 (info :variable :constant-value ',symbol)
253 (sb!c::%defconstant ',symbol ,expr-tmp ,doc)))))))