2f739c0a8a8ae2dc01c897c962688753320ff7b8
[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.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.)
37 ;;; -- WHN 2001-02-28
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 macroexpansion for WITH-HASH-TABLE-ITERATOR will work
46 ;;;     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
52 ;;;     into FASL files.
53 ;;; -- WHN 20000622
54 \f
55 ;;;; DO-related stuff which needs to be visible on the cross-compilation host
56
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
61            (label-1 (gensym))
62            (label-2 (gensym)))
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.
67       (dolist (v varlist)
68         (flet (;; (We avoid using CL:PUSH here so that CL:PUSH can be defined
69                ;; in terms of CL:SETF, and CL:SETF can be defined in terms of
70                ;; CL:DO, and CL:DO can be defined in terms of the current
71                ;; function.)
72                (push-on-r-inits (x)
73                  (setq r-inits (cons x r-inits)))
74                ;; common error-handling
75                (illegal-varlist ()
76                  (error "~S is an illegal form for a ~S varlist." v name)))
77           (cond ((symbolp v) (push-on-r-inits v))
78                 ((listp v)
79                  (unless (symbolp (first v))
80                    (error "~S step variable is not a symbol: ~S"
81                           name
82                           (first v)))
83                  (let ((lv (length v)))
84                    ;; (We avoid using CL:CASE here so that CL:CASE can be
85                    ;; defined in terms of CL:SETF, and CL:SETF can be defined
86                    ;; in terms of CL:DO, and CL:DO can be defined in terms of
87                    ;; the current function.)
88                    (cond ((= lv 1)
89                           (push-on-r-inits (first v)))
90                          ((= lv 2)
91                           (push-on-r-inits v))
92                          ((= lv 3)
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)
99         `(block ,block
100            (,bind ,(nreverse r-inits)
101                   ,@decls
102                   (tagbody
103                    (go ,label-2)
104                    ,label-1
105                    ,@code
106                    (,step ,@(nreverse r-steps))
107                    ,label-2
108                    (unless ,(first endlist) (go ,label-1))
109                    (return-from ,block (progn ,@(rest endlist))))))))))
110
111 ;;; DO-ANONYMOUS ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
112 ;;;
113 ;;; This is like DO, except it has no implicit NIL block. Each VAR is
114 ;;; initialized in parallel to the value of the specified INIT form.
115 ;;; On subsequent iterations, the VARS are assigned the value of the
116 ;;; STEP form (if any) in parallel. The TEST is evaluated before each
117 ;;; evaluation of the body FORMS. When the TEST is true, the
118 ;;; EXIT-FORMS are evaluated as a PROGN, with the result being the
119 ;;; value of the DO.
120 (defmacro do-anonymous (varlist endlist &rest body)
121   (do-do-body varlist endlist body 'let 'psetq 'do-anonymous (gensym)))
122 \f
123 ;;;; miscellany
124
125 ;;; Concatenate together the names of some strings and symbols,
126 ;;; producing a symbol in the current package.
127 (eval-when (:compile-toplevel :load-toplevel :execute)
128   (defun symbolicate (&rest things)
129     (values (intern (apply #'concatenate
130                            'string
131                            (mapcar #'string things))))))
132
133 ;;; like SYMBOLICATE, but producing keywords
134 (defun keywordicate (&rest things)
135   (let ((*package* *keyword-package*))
136     (apply #'symbolicate things)))
137
138 ;;; Access *PACKAGE* in a way which lets us recover when someone has
139 ;;; done something silly like (SETF *PACKAGE* :CL-USER). (Such an
140 ;;; assignment is undefined behavior, so it's sort of reasonable for
141 ;;; it to cause the system to go totally insane afterwards, but it's a
142 ;;; fairly easy mistake to make, so let's try to recover gracefully
143 ;;; instead.)
144 (defun sane-package ()
145   (let ((maybe-package *package*))
146     (cond ((and (packagep maybe-package)
147                 ;; For good measure, we also catch the problem of
148                 ;; *PACKAGE* being bound to a deleted package.
149                 ;; Technically, this is not undefined behavior in itself,
150                 ;; but it will immediately lead to undefined to behavior,
151                 ;; since almost any operation on a deleted package is
152                 ;; undefined.
153                 (package-name maybe-package))
154            maybe-package)
155           (t
156            ;; We're in the undefined behavior zone. First, munge the
157            ;; system back into a defined state.
158            (let ((really-package (find-package :cl-user)))
159              (setf *package* really-package)
160              ;; Then complain.
161              (error 'simple-type-error
162                     :datum maybe-package
163                     :expected-type 'package
164                     :format-control
165                     "~@<~S can't be a ~S: ~2I~_~S has been reset to ~S.~:>"
166                     :format-arguments (list '*package* (type-of maybe-package)
167                                             '*package* really-package)))))))
168
169 ;;; Give names to elements of a numeric sequence.
170 (defmacro defenum ((&key (prefix "") (suffix "") (start 0) (step 1))
171                    &rest identifiers)
172   (let ((results nil)
173         (index 0)
174         (start (eval start))
175         (step (eval step)))
176     (dolist (id identifiers)
177       (when id
178         (multiple-value-bind (root docs)
179             (if (consp id)
180                 (values (car id) (cdr id))
181                 (values id nil))
182           (push `(defconstant ,(symbolicate prefix root suffix)
183                    ,(+ start (* step index))
184                    ,@docs)
185                 results)))
186       (incf index))
187     `(progn
188        ,@(nreverse results))))
189
190 ;;; generalization of DEFCONSTANT to values which are the same not
191 ;;; under EQL but under e.g. EQUAL or EQUALP
192 ;;;
193 ;;; DEFCONSTANT-EQX is to be used instead of DEFCONSTANT for values
194 ;;; which are appropriately compared using the function given by the
195 ;;; EQX argument instead of EQL.
196 ;;;
197 ;;; Note: Be careful when using this macro, since it's easy to
198 ;;; unintentionally pessimize your code. A good time to use this macro
199 ;;; is when the values defined will be fed into optimization
200 ;;; transforms and never actually appear in the generated code; this
201 ;;; is especially common when defining BYTE expressions. Unintentional
202 ;;; pessimization can result when the values defined by this macro are
203 ;;; actually used in generated code: because of the way that the
204 ;;; dump/load system works, you'll typically get one copy of consed
205 ;;; structure for each object file which contains code referring to
206 ;;; the value, plus perhaps one more copy bound to the SYMBOL-VALUE of
207 ;;; the constant. If you don't want that to happen, you should
208 ;;; probably use DEFPARAMETER instead.
209 (defmacro defconstant-eqx (symbol expr eqx &optional doc)
210   (let ((expr-tmp (gensym "EXPR-TMP-")))
211     `(progn
212        ;; When we're building the cross-compiler, and in most
213        ;; situations even when we're running the cross-compiler,
214        ;; all we need is a nice portable definition in terms of the
215        ;; ANSI Common Lisp operations.
216        (eval-when (:compile-toplevel :load-toplevel :execute)
217          (let ((,expr-tmp ,expr))
218            (cond ((boundp ',symbol)
219                   (unless (and (constantp ',symbol)
220                                (funcall ,eqx
221                                         (symbol-value ',symbol)
222                                         ,expr-tmp))
223                     (error "already bound differently: ~S")))
224                  (t
225                   (defconstant ,symbol
226                     ;; KLUDGE: This is a very ugly hack, to be able to
227                     ;; build SBCL with CMU CL (2.4.19), because there
228                     ;; seems to be some confusion in CMU CL about
229                     ;; ,EXPR-TEMP at EVAL-WHEN time ... -- MNA 2000-02-23
230                     #-cmu ,expr-tmp
231                     #+cmu ,expr
232                     ,@(when doc `(,doc)))))))
233        ;; The #+SB-XC :COMPILE-TOPLEVEL situation is special, since we
234        ;; want to define the symbol not just in the cross-compilation
235        ;; host Lisp (which was handled above) but also in the
236        ;; cross-compiler (which we will handle now).
237        ;;
238        ;; KLUDGE: It would probably be possible to do this fairly
239        ;; cleanly, in a way parallel to the code above, if we had
240        ;; SB!XC:FOO versions of all the primitives CL:FOO used above
241        ;; (e.g. SB!XC:BOUNDP, SB!XC:SYMBOL-VALUE, and
242        ;; SB!XC:DEFCONSTANT), and took care to call them. But right
243        ;; now we just hack around in the guts of the cross-compiler
244        ;; instead. -- WHN 2000-11-03
245        #+sb-xc
246        (eval-when (:compile-toplevel)
247          (let ((,expr-tmp ,symbol))
248            (unless (and (eql (info :variable :kind ',symbol) :constant)
249                         (funcall ,eqx
250                                  (info :variable :constant-value ',symbol)
251                                  ,expr-tmp))
252              (sb!c::%defconstant ',symbol ,expr-tmp ,doc)))))))