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.
13 (in-package "SB!IMPL")
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.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.)
38 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
39 (def!constant
+empty-ht-slot
+ '%empty-ht-slot%
))
40 ;;; We shouldn't need this mess now that EVAL-WHEN works.
42 ;;; KLUDGE: Using a private symbol still leaves us vulnerable to users
43 ;;; getting nonconforming behavior by messing around with
44 ;;; DO-ALL-SYMBOLS. That seems like a fairly obscure problem, so for
45 ;;; now we just don't worry about it. If for some reason it becomes
46 ;;; worrisome and the magic value needs replacement:
47 ;;; * The replacement value needs to be LOADable with EQL preserved,
48 ;;; so that the macroexpansion for WITH-HASH-TABLE-ITERATOR will
49 ;;; work when compiled into a file and loaded back into SBCL.
50 ;;; (Thus, just uninterning %EMPTY-HT-SLOT% doesn't work.)
51 ;;; * The replacement value needs to be acceptable to the
52 ;;; low-level gencgc.lisp hash table scavenging code.
53 ;;; * The change will break binary compatibility, since comparisons
54 ;;; against the value used at the time of compilation are wired
58 ;;;; DO-related stuff which needs to be visible on the cross-compilation host
60 (eval-when (#-sb-xc
:compile-toplevel
:load-toplevel
:execute
)
61 (defun frob-do-body (varlist endlist decls-and-code bind step name block
)
62 (let* ((r-inits nil
) ; accumulator for reversed list
63 (r-steps nil
) ; accumulator for reversed list
66 ;; Check for illegal old-style DO.
67 (when (or (not (listp varlist
)) (atom endlist
))
68 (error "ill-formed ~S -- possibly illegal old style DO?" name
))
69 ;; Parse VARLIST to get R-INITS and R-STEPS.
71 (flet (;; (We avoid using CL:PUSH here so that CL:PUSH can be
72 ;; defined in terms of CL:SETF, and CL:SETF can be
73 ;; defined in terms of CL:DO, and CL:DO can be defined
74 ;; in terms of the current function.)
76 (setq r-inits
(cons x r-inits
)))
77 ;; common error-handling
79 (error "~S is an illegal form for a ~S varlist." v name
)))
80 (cond ((symbolp v
) (push-on-r-inits v
))
82 (unless (symbolp (first v
))
83 (error "~S step variable is not a symbol: ~S"
86 (let ((lv (length v
)))
87 ;; (We avoid using CL:CASE here so that CL:CASE can
88 ;; be defined in terms of CL:SETF, and CL:SETF can
89 ;; be defined in terms of CL:DO, and CL:DO can be
90 ;; defined in terms of the current function.)
92 (push-on-r-inits (first v
)))
96 (push-on-r-inits (list (first v
) (second v
)))
97 (setq r-steps
(list* (third v
) (first v
) r-steps
)))
98 (t (illegal-varlist)))))
99 (t (illegal-varlist)))))
100 ;; Construct the new form.
101 (multiple-value-bind (code decls
)
102 (parse-body decls-and-code
:doc-string-allowed nil
)
104 (,bind
,(nreverse r-inits
)
110 (,step
,@(nreverse r-steps
))
112 (unless ,(first endlist
) (go ,label-1
))
113 (return-from ,block
(progn ,@(rest endlist
))))))))))
117 ;;; Automate an idiom often found in macros:
118 ;;; (LET ((FOO (GENSYM "FOO"))
119 ;;; (MAX-INDEX (GENSYM "MAX-INDEX-")))
122 ;;; "Good notation eliminates thought." -- Eric Siggia
124 ;;; Incidentally, this is essentially the same operator which
125 ;;; _On Lisp_ calls WITH-GENSYMS.
126 (defmacro with-unique-names
(symbols &body body
)
127 `(let ,(mapcar (lambda (symbol)
128 (let* ((symbol-name (symbol-name symbol
))
129 (stem (if (every #'alpha-char-p symbol-name
)
131 (concatenate 'string symbol-name
"-"))))
132 `(,symbol
(sb!xc
:gensym
,stem
))))
136 ;;; Return a list of N gensyms. (This is a common suboperation in
137 ;;; macros and other code-manipulating code.)
138 (declaim (ftype (function (index &optional t
) (values list
&optional
))
140 (defun make-gensym-list (n &optional name
)
144 (loop repeat n collect
(sb!xc
:gensym
(string name
)))
145 (loop repeat n collect
(sb!xc
:gensym
))))
149 ;;; Lots of code wants to get to the KEYWORD package or the
150 ;;; COMMON-LISP package without a lot of fuss, so we cache them in
151 ;;; variables. TO DO: How much does this actually buy us? It sounds
152 ;;; sensible, but I don't know for sure that it saves space or time..
155 ;;; (The initialization forms here only matter on the cross-compilation
156 ;;; host; In the target SBCL, these variables are set in cold init.)
157 (declaim (type package
*cl-package
* *keyword-package
*))
158 (defglobal *cl-package
* (find-package "COMMON-LISP"))
159 (defglobal *keyword-package
* (find-package "KEYWORD"))
161 ;;; Concatenate together the names of some strings and symbols,
162 ;;; producing a symbol in the current package.
163 (eval-when (#-sb-xc
:compile-toplevel
:load-toplevel
:execute
)
164 (defun symbolicate (&rest things
)
165 (declare (dynamic-extent things
))
168 (if (singleton-p things
)
169 (string (first things
))
170 (let* ((length (reduce #'+ things
171 :key
(lambda (x) (length (string x
)))))
172 (name (make-array length
:element-type
'character
))
174 (dolist (thing things name
)
175 (let ((x (string thing
)))
176 (replace name x
:start1 index
)
177 (incf index
(length x
))))))))))
181 (sb!xc
:gensym
(symbol-name x
))
184 ;;; like SYMBOLICATE, but producing keywords
185 (defun keywordicate (&rest things
)
186 (let ((*package
* *keyword-package
*))
187 (apply #'symbolicate things
)))
189 ;;; Access *PACKAGE* in a way which lets us recover when someone has
190 ;;; done something silly like (SETF *PACKAGE* :CL-USER). (Such an
191 ;;; assignment is undefined behavior, so it's sort of reasonable for
192 ;;; it to cause the system to go totally insane afterwards, but it's a
193 ;;; fairly easy mistake to make, so let's try to recover gracefully
195 ;;; This function is called while compiling this file because DO-ANONYMOUS
196 ;;; is a delayed-def!macro, the constructor for which calls SANE-PACKAGE.
197 (eval-when (:load-toplevel
:execute
#+sb-xc-host
:compile-toplevel
)
198 (defun sane-package ()
199 (let ((maybe-package *package
*))
200 (cond ((and (packagep maybe-package
)
201 ;; For good measure, we also catch the problem of
202 ;; *PACKAGE* being bound to a deleted package.
203 ;; Technically, this is not undefined behavior in itself,
204 ;; but it will immediately lead to undefined to behavior,
205 ;; since almost any operation on a deleted package is
208 (package-%name maybe-package
))
211 ;; We're in the undefined behavior zone. First, munge the
212 ;; system back into a defined state.
213 (let ((really-package (find-package :cl-user
)))
214 (setf *package
* really-package
)
216 (error 'simple-type-error
218 :expected-type
'(and package
(satisfies package-name
))
220 "~@<~S can't be a ~A: ~2I~_~S has been reset to ~S.~:>"
221 :format-arguments
(list '*package
*
222 (if (packagep maybe-package
)
224 (type-of maybe-package
))
225 '*package
* really-package
))))))))
227 ;;; Access *DEFAULT-PATHNAME-DEFAULTS*, issuing a warning if its value
228 ;;; is silly. (Unlike the vaguely-analogous SANE-PACKAGE, we don't
229 ;;; actually need to reset the variable when it's silly, since even
230 ;;; crazy values of *DEFAULT-PATHNAME-DEFAULTS* don't leave the system
231 ;;; in a state where it's hard to recover interactively.)
232 (defun sane-default-pathname-defaults ()
233 (let* ((dfd *default-pathname-defaults
*)
234 (dfd-dir (pathname-directory dfd
)))
235 ;; It's generally not good to use a relative pathname for
236 ;; *DEFAULT-PATHNAME-DEFAULTS*, since relative pathnames
237 ;; are defined by merging into a default pathname (which is,
238 ;; by default, *DEFAULT-PATHNAME-DEFAULTS*).
239 (when (and (consp dfd-dir
)
240 (eql (first dfd-dir
) :relative
))
242 "~@<~S is a relative pathname. (But we'll try using it anyway.)~@:>"
243 '*default-pathname-defaults
*))
246 ;;; Compile a version of BODY for all TYPES, and dispatch to the
247 ;;; correct one based on the value of VAR. This was originally used
248 ;;; only for strings, hence the name. Renaming it to something more
249 ;;; generic might not be a bad idea.
250 (def!macro string-dispatch
((&rest types
) var
&body body
)
251 (let ((fun (sb!xc
:gensym
"STRING-DISPATCH-FUN")))
254 (declare (inline ,fun
))
256 ,@(loop for type in types
257 ;; TRULY-THE allows transforms to take advantage of the type
258 ;; information without need for constraint propagation.
259 collect
`(,type
(,fun
(truly-the ,type
,var
))))))))
261 ;;; Give names to elements of a numeric sequence.
262 (defmacro defenum
((&key
(start 0) (step 1))
268 (dolist (id identifiers
)
270 (multiple-value-bind (sym docs
)
272 (values (car id
) (cdr id
))
274 (push `(def!constant
,sym
275 ,(+ start
(* step index
))
280 ,@(nreverse results
))))
282 ;;; generalization of DEFCONSTANT to values which are the same not
283 ;;; under EQL but under e.g. EQUAL or EQUALP
285 ;;; DEFCONSTANT-EQX is to be used instead of DEFCONSTANT for values
286 ;;; which are appropriately compared using the function given by the
287 ;;; EQX argument instead of EQL.
289 (defmacro defconstant-eqx
(symbol expr eqx
&optional doc
)
290 `(def!constant
,symbol
291 (%defconstant-eqx-value
',symbol
,expr
,eqx
)
292 ,@(when doc
(list doc
))))
293 (defun %defconstant-eqx-value
(symbol expr eqx
)
294 (declare (type function eqx
))
295 (flet ((bummer (explanation)
296 (error "~@<bad DEFCONSTANT-EQX ~S ~2I~_~S: ~2I~_~A ~S~:>"
300 (symbol-value symbol
))))
301 (cond ((not (boundp symbol
))
303 ((not (constantp symbol
))
304 (bummer "already bound as a non-constant"))
305 ((not (funcall eqx
(symbol-value symbol
) expr
))
306 (bummer "already bound as a different constant value"))
308 (symbol-value symbol
)))))
310 ;;; a helper function for various macros which expect clauses of a
311 ;;; given length, etc.
313 ;;; Return true if X is a proper list whose length is between MIN and
315 (defun proper-list-of-length-p (x min
&optional
(max min
))
316 ;; FIXME: This implementation will hang on circular list
317 ;; structure. Since this is an error-checking utility, i.e. its
318 ;; job is to deal with screwed-up input, it'd be good style to fix
319 ;; it so that it can deal with circular list structure.
320 (cond ((minusp max
) nil
)
321 ((null x
) (zerop min
))
324 (proper-list-of-length-p (cdr x
)
331 (defun proper-list-p (x)
333 (return-from proper-list-p
(null x
)))
334 (let ((rabbit (cdr x
))
336 (flet ((pop-rabbit ()
337 (when (eql rabbit turtle
) ; circular
338 (return-from proper-list-p nil
))
340 (return-from proper-list-p
(null rabbit
)))
346 ;;; Helpers for defining error-signalling NOP's for "not supported
347 ;;; here" operations.
348 (defmacro define-unsupported-fun
(name &optional
349 (doc "Unsupported on this platform.")
351 "~S is unsupported on this platform ~
352 (OS, CPU, whatever)."
355 (declare (ignorable doc
))
356 `(defun ,name
(&rest args
)
359 (declare (ignore args
))
360 (error 'unsupported-operator
361 :format-control
,control
362 :format-arguments
(if ,controlp
',arguments
(list ',name
)))))
364 ;;; This is like DO, except it has no implicit NIL block.
365 (def!macro do-anonymous
(varlist endlist
&rest body
)
366 (frob-do-body varlist endlist body
'let
'psetq
'do-anonymous
(gensym)))
368 ;;;; Theoretically is 'early-globaldb' but it's not worth creating a file for.
369 ;;;; At run time, we represent the type of a piece of INFO in the globaldb
370 ;;;; by a small integer between 1 and 63. [0 is reserved for internal use.]
371 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
372 (def!constant info-number-bits
6))
373 (deftype info-number
() `(unsigned-byte ,info-number-bits
))