0.pre7.14.flaky4:
[sbcl.git] / src / code / primordial-extensions.lisp
1 ;;;; various user-level definitions which need to be done particularly
2 ;;;; early
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
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.
12
13 (in-package "SB!INT")
14 \f
15 ;;;; target constants which need to appear as early as possible
16
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
20 ;;;
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!
27 ;;;
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.
33 ;;;
34 ;;; KLUDGE: Note that as of version 0.pre7 there's a dependence in the
35 ;;; gencgc.c code on this value being a symbol. (This is only one of
36 ;;; several nasty dependencies between that code and this, alas.)
37 ;;; -- WHN 2001-08-17
38 ;;;
39 ;;; FIXME: We end up doing two DEFCONSTANT forms because (1) LispWorks
40 ;;; needs EVAL-WHEN wrapped around DEFCONSTANT, and (2) SBCL's
41 ;;; DEFCONSTANT expansion doesn't seem to behave properly inside
42 ;;; EVAL-WHEN, so that without this, the +EMPTY-HT-SLOT+ references in
43 ;;; e.g. DOHASH macroexpansions don't end up being replaced by
44 ;;; constant values, so that the system dies at cold init because
45 ;;; '+EMPTY-HT-SLOT+ isn't bound yet. It's hard to fix this properly
46 ;;; until SBCL's EVAL-WHEN is fixed, which is waiting for the IR1
47 ;;; interpreter to go away, which is waiting for sbcl-0.7.x..
48 (eval-when (:compile-toplevel :load-toplevel :execute)
49   (defconstant +empty-ht-slot+ '%empty-ht-slot%))
50 (defconstant +empty-ht-slot+ '#.+empty-ht-slot+) ; egads.. See FIXME above.
51 ;;; KLUDGE: Using a private symbol still leaves us vulnerable to users
52 ;;; getting nonconforming behavior by messing around with
53 ;;; DO-ALL-SYMBOLS. That seems like a fairly obscure problem, so for
54 ;;; now we just don't worry about it. If for some reason it becomes
55 ;;; worrisome and the magic value needs replacement:
56 ;;;   * The replacement value needs to be LOADable with EQL preserved,
57 ;;;     so that the macroexpansion for WITH-HASH-TABLE-ITERATOR will
58 ;;;     work when compiled into a file and loaded back into SBCL.
59 ;;;     (Thus, just uninterning %EMPTY-HT-SLOT% doesn't work.)
60 ;;;   * The replacement value needs to be acceptable to the
61 ;;;     low-level gencgc.lisp hash table scavenging code. 
62 ;;;   * The change will break binary compatibility, since comparisons
63 ;;;     against the value used at the time of compilation are wired
64 ;;;     into FASL files.
65 ;;; -- WHN 20000622
66 \f
67 ;;;; DO-related stuff which needs to be visible on the cross-compilation host
68
69 (eval-when (:compile-toplevel :load-toplevel :execute)
70   (defun do-do-body (varlist endlist decls-and-code bind step name block)
71     (let* ((r-inits nil) ; accumulator for reversed list
72            (r-steps nil) ; accumulator for reversed list
73            (label-1 (gensym))
74            (label-2 (gensym)))
75       ;; Check for illegal old-style DO.
76       (when (or (not (listp varlist)) (atom endlist))
77         (error "ill-formed ~S -- possibly illegal old style DO?" name))
78       ;; Parse VARLIST to get R-INITS and R-STEPS.
79       (dolist (v varlist)
80         (flet (;; (We avoid using CL:PUSH here so that CL:PUSH can be
81                ;; defined in terms of CL:SETF, and CL:SETF can be
82                ;; defined in terms of CL:DO, and CL:DO can be defined
83                ;; in terms of the current function.)
84                (push-on-r-inits (x)
85                  (setq r-inits (cons x r-inits)))
86                ;; common error-handling
87                (illegal-varlist ()
88                  (error "~S is an illegal form for a ~S varlist." v name)))
89           (cond ((symbolp v) (push-on-r-inits v))
90                 ((listp v)
91                  (unless (symbolp (first v))
92                    (error "~S step variable is not a symbol: ~S"
93                           name
94                           (first v)))
95                  (let ((lv (length v)))
96                    ;; (We avoid using CL:CASE here so that CL:CASE can
97                    ;; be defined in terms of CL:SETF, and CL:SETF can
98                    ;; be defined in terms of CL:DO, and CL:DO can be
99                    ;; defined in terms of the current function.)
100                    (cond ((= lv 1)
101                           (push-on-r-inits (first v)))
102                          ((= lv 2)
103                           (push-on-r-inits v))
104                          ((= lv 3)
105                           (push-on-r-inits (list (first v) (second v)))
106                           (setq r-steps (list* (third v) (first v) r-steps)))
107                          (t (illegal-varlist)))))
108                 (t (illegal-varlist)))))
109       ;; Construct the new form.
110       (multiple-value-bind (code decls) (parse-body decls-and-code nil)
111         `(block ,block
112            (,bind ,(nreverse r-inits)
113                   ,@decls
114                   (tagbody
115                    (go ,label-2)
116                    ,label-1
117                    ,@code
118                    (,step ,@(nreverse r-steps))
119                    ,label-2
120                    (unless ,(first endlist) (go ,label-1))
121                    (return-from ,block (progn ,@(rest endlist))))))))))
122
123 ;;; This is like DO, except it has no implicit NIL block. Each VAR is
124 ;;; initialized in parallel to the value of the specified INIT form.
125 ;;; On subsequent iterations, the VARS are assigned the value of the
126 ;;; STEP form (if any) in parallel. The TEST is evaluated before each
127 ;;; evaluation of the body FORMS. When the TEST is true, the
128 ;;; EXIT-FORMS are evaluated as a PROGN, with the result being the
129 ;;; value of the DO.
130 (defmacro do-anonymous (varlist endlist &rest body)
131   (do-do-body varlist endlist body 'let 'psetq 'do-anonymous (gensym)))
132 \f
133 ;;;; miscellany
134
135 ;;; Concatenate together the names of some strings and symbols,
136 ;;; producing a symbol in the current package.
137 (eval-when (:compile-toplevel :load-toplevel :execute)
138   (defun symbolicate (&rest things)
139     (values (intern (apply #'concatenate
140                            'string
141                            (mapcar #'string things))))))
142
143 ;;; like SYMBOLICATE, but producing keywords
144 (defun keywordicate (&rest things)
145   (let ((*package* *keyword-package*))
146     (apply #'symbolicate things)))
147
148 ;;; Access *PACKAGE* in a way which lets us recover when someone has
149 ;;; done something silly like (SETF *PACKAGE* :CL-USER). (Such an
150 ;;; assignment is undefined behavior, so it's sort of reasonable for
151 ;;; it to cause the system to go totally insane afterwards, but it's a
152 ;;; fairly easy mistake to make, so let's try to recover gracefully
153 ;;; instead.)
154 (defun sane-package ()
155   (let ((maybe-package *package*))
156     (cond ((and (packagep maybe-package)
157                 ;; For good measure, we also catch the problem of
158                 ;; *PACKAGE* being bound to a deleted package.
159                 ;; Technically, this is not undefined behavior in itself,
160                 ;; but it will immediately lead to undefined to behavior,
161                 ;; since almost any operation on a deleted package is
162                 ;; undefined.
163                 (package-name maybe-package))
164            maybe-package)
165           (t
166            ;; We're in the undefined behavior zone. First, munge the
167            ;; system back into a defined state.
168            (let ((really-package (find-package :cl-user)))
169              (setf *package* really-package)
170              ;; Then complain.
171              (error 'simple-type-error
172                     :datum maybe-package
173                     :expected-type '(and package (satisfies package-name))
174                     :format-control
175                     "~@<~S can't be a ~A: ~2I~_~S has been reset to ~S.~:>"
176                     :format-arguments (list '*package*
177                                             (if (packagep maybe-package)
178                                                 "deleted package"
179                                                 (type-of maybe-package))
180                                             '*package* really-package)))))))
181
182 ;;; Access *DEFAULT-PATHNAME-DEFAULTS*, issuing a warning if its value
183 ;;; is silly. (Unlike the vaguely-analogous SANE-PACKAGE, we don't
184 ;;; actually need to reset the variable when it's silly, since even
185 ;;; crazy values of *DEFAULT-PATHNAME-DEFAULTS* don't leave the system
186 ;;; in a state where it's hard to recover interactively.)
187 (defun sane-default-pathname-defaults ()
188   (let* ((dfd *default-pathname-defaults*)
189          (dfd-dir (pathname-directory dfd)))
190     ;; It's generally not good to use a relative pathname for
191     ;; *DEFAULT-PATHNAME-DEFAULTS*, since relative pathnames
192     ;; are defined by merging into a default pathname (which is,
193     ;; by default, *DEFAULT-PATHNAME-DEFAULTS*).
194     (when (and (consp dfd-dir)
195                (eql (first dfd-dir) :relative))
196       (warn
197        "~@<~S is a relative pathname. (But we'll try using it anyway.)~@:>"
198        '*default-pathname-defaults*))
199     dfd))
200
201 ;;; Give names to elements of a numeric sequence.
202 (defmacro defenum ((&key (prefix "") (suffix "") (start 0) (step 1))
203                    &rest identifiers)
204   (let ((results nil)
205         (index 0)
206         (start (eval start))
207         (step (eval step)))
208     (dolist (id identifiers)
209       (when id
210         (multiple-value-bind (root docs)
211             (if (consp id)
212                 (values (car id) (cdr id))
213                 (values id nil))
214           (push `(defconstant ,(symbolicate prefix root suffix)
215                    ,(+ start (* step index))
216                    ,@docs)
217                 results)))
218       (incf index))
219     `(progn
220        ,@(nreverse results))))
221
222 ;;; generalization of DEFCONSTANT to values which are the same not
223 ;;; under EQL but under e.g. EQUAL or EQUALP
224 ;;;
225 ;;; DEFCONSTANT-EQX is to be used instead of DEFCONSTANT for values
226 ;;; which are appropriately compared using the function given by the
227 ;;; EQX argument instead of EQL.
228 ;;;
229 ;;; Note: Be careful when using this macro, since it's easy to
230 ;;; unintentionally pessimize your code. A good time to use this macro
231 ;;; is when the values defined will be fed into optimization
232 ;;; transforms and never actually appear in the generated code; this
233 ;;; is especially common when defining BYTE expressions. Unintentional
234 ;;; pessimization can result when the values defined by this macro are
235 ;;; actually used in generated code: because of the way that the
236 ;;; dump/load system works, you'll typically get one copy of consed
237 ;;; structure for each object file which contains code referring to
238 ;;; the value, plus perhaps one more copy bound to the SYMBOL-VALUE of
239 ;;; the constant. If you don't want that to happen, you should
240 ;;; probably use DEFPARAMETER instead; or if you truly desperately
241 ;;; need to avoid runtime indirection through a symbol, you might be
242 ;;; able to do something with LOAD-TIME-VALUE or MAKE-LOAD-FORM.
243 (defmacro defconstant-eqx (symbol expr eqx &optional doc)
244   `(defconstant ,symbol
245      (%defconstant-eqx-value ',symbol ,expr ,eqx)
246      ,@(when doc (list doc))))
247 (defun %defconstant-eqx-value (symbol expr eqx)
248   (flet ((bummer (explanation)
249            (error "~@<bad DEFCONSTANT-EQX ~S: ~2I~_~A~:>" symbol explanation)))
250     (cond ((not (boundp symbol))
251            expr)
252           ((not (constantp symbol))
253            (bummer "already bound as a non-constant"))
254           ((not (funcall eqx (symbol-value symbol) expr))
255            (bummer "already bound as a different constant value"))
256           (t
257            (symbol-value symbol)))))