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 ;;;; DO-related stuff which needs to be visible on the cross-compilation host
17 (eval-when (:compile-toplevel :load-toplevel :execute)
18 (defun do-do-body (varlist endlist decls-and-code bind step name block)
19 (let* ((r-inits nil) ; accumulator for reversed list
20 (r-steps nil) ; accumulator for reversed list
23 ;; Check for illegal old-style DO.
24 (when (or (not (listp varlist)) (atom endlist))
25 (error "Ill-formed ~S -- possibly illegal old style DO?" name))
26 ;; Parse VARLIST to get R-INITS and R-STEPS.
28 (flet (;; (We avoid using CL:PUSH here so that CL:PUSH can be defined
29 ;; in terms of CL:SETF, and CL:SETF can be defined in terms of
30 ;; CL:DO, and CL:DO can be defined in terms of the current
33 (setq r-inits (cons x r-inits)))
34 ;; common error-handling
36 (error "~S is an illegal form for a ~S varlist." v name)))
37 (cond ((symbolp v) (push-on-r-inits v))
39 (unless (symbolp (first v))
40 (error "~S step variable is not a symbol: ~S"
43 (let ((lv (length v)))
44 ;; (We avoid using CL:CASE here so that CL:CASE can be
45 ;; defined in terms of CL:SETF, and CL:SETF can be defined
46 ;; in terms of CL:DO, and CL:DO can be defined in terms of
47 ;; the current function.)
49 (push-on-r-inits (first v)))
53 (push-on-r-inits (list (first v) (second v)))
54 (setq r-steps (list* (third v) (first v) r-steps)))
55 (t (illegal-varlist)))))
56 (t (illegal-varlist)))))
57 ;; Construct the new form.
58 (multiple-value-bind (code decls) (parse-body decls-and-code nil)
60 (,bind ,(nreverse r-inits)
66 (,step ,@(nreverse r-steps))
68 (unless ,(first endlist) (go ,label-1))
69 (return-from ,block (progn ,@(rest endlist))))))))))
71 ;;; DO-ANONYMOUS ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
73 ;;; This is like DO, except it has no implicit NIL block. Each VAR is
74 ;;; initialized in parallel to the value of the specified INIT form.
75 ;;; On subsequent iterations, the VARS are assigned the value of the
76 ;;; STEP form (if any) in parallel. The TEST is evaluated before each
77 ;;; evaluation of the body FORMS. When the TEST is true, the
78 ;;; EXIT-FORMS are evaluated as a PROGN, with the result being the
80 (defmacro do-anonymous (varlist endlist &rest body)
81 (do-do-body varlist endlist body 'let 'psetq 'do-anonymous (gensym)))
85 ;;; Concatenate together the names of some strings and symbols,
86 ;;; producing a symbol in the current package.
87 (eval-when (:compile-toplevel :load-toplevel :execute)
88 (defun symbolicate (&rest things)
89 (values (intern (apply #'concatenate
91 (mapcar #'string things))))))
93 ;;; like SYMBOLICATE, but producing keywords
94 (defun keywordicate (&rest things)
95 (let ((*package* *keyword-package*))
96 (apply #'symbolicate things)))
98 ;;; Access *PACKAGE* in a way which lets us recover when someone has
99 ;;; done something silly like (SETF *PACKAGE* :CL-USER). (Such an
100 ;;; assignment is undefined behavior, so it's sort of reasonable for
101 ;;; it to cause the system to go totally insane afterwards, but it's a
102 ;;; fairly easy mistake to make, so let's try to recover gracefully
104 (defun sane-package ()
105 (let ((maybe-package *package*))
106 (cond ((and (packagep maybe-package)
107 ;; For good measure, we also catch the problem of
108 ;; *PACKAGE* being bound to a deleted package.
109 ;; Technically, this is not undefined behavior in itself,
110 ;; but it will immediately lead to undefined to behavior,
111 ;; since almost any operation on a deleted package is
113 (package-name maybe-package))
116 ;; We're in the undefined behavior zone. First, munge the
117 ;; system back into a defined state.
118 (let ((really-package (find-package :cl-user)))
119 (setf *package* really-package)
121 (error 'simple-type-error
123 :expected-type 'package
125 "~@<~S can't be a ~S: ~2I~_~S has been reset to ~S.~:>"
126 :format-arguments (list '*package* (type-of maybe-package)
127 '*package* really-package)))))))
129 ;;; Give names to elements of a numeric sequence.
130 (defmacro defenum ((&key (prefix "") (suffix "") (start 0) (step 1))
136 (dolist (id identifiers)
138 (multiple-value-bind (root docs)
140 (values (car id) (cdr id))
142 (push `(defconstant ,(symbolicate prefix root suffix)
143 ,(+ start (* step index))
148 ,@(nreverse results))))
150 ;;; generalization of DEFCONSTANT to values which are the same not
151 ;;; under EQL but under e.g. EQUAL or EQUALP
153 ;;; DEFCONSTANT-EQX is to be used instead of DEFCONSTANT for values
154 ;;; which are appropriately compared using the function given by the
155 ;;; EQX argument instead of EQL.
157 ;;; Note: Be careful when using this macro, since it's easy to
158 ;;; unintentionally pessimize your code. A good time to use this macro
159 ;;; is when the values defined will be fed into optimization
160 ;;; transforms and never actually appear in the generated code; this
161 ;;; is especially common when defining BYTE expressions. Unintentional
162 ;;; pessimization can result when the values defined by this macro are
163 ;;; actually used in generated code: because of the way that the
164 ;;; dump/load system works, you'll typically get one copy of consed
165 ;;; structure for each object file which contains code referring to
166 ;;; the value, plus perhaps one more copy bound to the SYMBOL-VALUE of
167 ;;; the constant. If you don't want that to happen, you should
168 ;;; probably use DEFPARAMETER instead.
169 (defmacro defconstant-eqx (symbol expr eqx &optional doc)
170 (let ((expr-tmp (gensym "EXPR-TMP-")))
172 ;; When we're building the cross-compiler, and in most
173 ;; situations even when we're running the cross-compiler,
174 ;; all we need is a nice portable definition in terms of the
175 ;; ANSI Common Lisp operations.
176 (eval-when (:compile-toplevel :load-toplevel :execute)
177 (let ((,expr-tmp ,expr))
178 (cond ((boundp ',symbol)
179 (unless (and (constantp ',symbol)
181 (symbol-value ',symbol)
183 (error "already bound differently: ~S")))
185 (defconstant ,symbol ,expr-tmp ,@(when doc `(,doc)))))))
186 ;; The #+SB-XC :COMPILE-TOPLEVEL situation is special, since we
187 ;; want to define the symbol not just in the cross-compilation
188 ;; host Lisp (which was handled above) but also in the
189 ;; cross-compiler (which we will handle now).
191 ;; KLUDGE: It would probably be possible to do this fairly
192 ;; cleanly, in a way parallel to the code above, if we had
193 ;; SB!XC:FOO versions of all the primitives CL:FOO used above
194 ;; (e.g. SB!XC:BOUNDP, SB!XC:SYMBOL-VALUE, and
195 ;; SB!XC:DEFCONSTANT), and took care to call them. But right
196 ;; now we just hack around in the guts of the cross-compiler
197 ;; instead. -- WHN 2000-11-03
199 (eval-when (:compile-toplevel)
200 (let ((,expr-tmp ,symbol))
201 (unless (and (eql (info :variable :kind ',symbol) :constant)
203 (info :variable :constant-value ',symbol)
205 (sb!c::%defconstant ',symbol ,expr-tmp ,doc)))))))