(belated 0.6.11.2 checkin notes):
[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 ;;;; DO-related stuff which needs to be visible on the cross-compilation host
16
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
21            (label-1 (gensym))
22            (label-2 (gensym)))
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.
27       (dolist (v varlist)
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
31                ;; function.)
32                (push-on-r-inits (x)
33                  (setq r-inits (cons x r-inits)))
34                ;; common error-handling
35                (illegal-varlist ()
36                  (error "~S is an illegal form for a ~S varlist." v name)))
37           (cond ((symbolp v) (push-on-r-inits v))
38                 ((listp v)
39                  (unless (symbolp (first v))
40                    (error "~S step variable is not a symbol: ~S"
41                           name
42                           (first v)))
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.)
48                    (cond ((= lv 1)
49                           (push-on-r-inits (first v)))
50                          ((= lv 2)
51                           (push-on-r-inits v))
52                          ((= lv 3)
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)
59         `(block ,block
60            (,bind ,(nreverse r-inits)
61                   ,@decls
62                   (tagbody
63                    (go ,label-2)
64                    ,label-1
65                    ,@code
66                    (,step ,@(nreverse r-steps))
67                    ,label-2
68                    (unless ,(first endlist) (go ,label-1))
69                    (return-from ,block (progn ,@(rest endlist))))))))))
70
71 ;;; DO-ANONYMOUS ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
72 ;;;
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
79 ;;; value of the DO.
80 (defmacro do-anonymous (varlist endlist &rest body)
81   (do-do-body varlist endlist body 'let 'psetq 'do-anonymous (gensym)))
82 \f
83 ;;;; miscellany
84
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
90                            'string
91                            (mapcar #'string things))))))
92
93 ;;; like SYMBOLICATE, but producing keywords
94 (defun keywordicate (&rest things)
95   (let ((*package* *keyword-package*))
96     (apply #'symbolicate things)))
97
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
103 ;;; instead.)
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
112                 ;; undefined.
113                 (package-name maybe-package))
114            maybe-package)
115           (t
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)
120              ;; Then complain.
121              (error 'simple-type-error
122                     :datum maybe-package
123                     :expected-type 'package
124                     :format-control
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)))))))
128
129 ;;; Give names to elements of a numeric sequence.
130 (defmacro defenum ((&key (prefix "") (suffix "") (start 0) (step 1))
131                    &rest identifiers)
132   (let ((results nil)
133         (index 0)
134         (start (eval start))
135         (step (eval step)))
136     (dolist (id identifiers)
137       (when id
138         (multiple-value-bind (root docs)
139             (if (consp id)
140                 (values (car id) (cdr id))
141                 (values id nil))
142           (push `(defconstant ,(symbolicate prefix root suffix)
143                    ,(+ start (* step index))
144                    ,@docs)
145                 results)))
146       (incf index))
147     `(progn
148        ,@(nreverse results))))
149
150 ;;; generalization of DEFCONSTANT to values which are the same not
151 ;;; under EQL but under e.g. EQUAL or EQUALP
152 ;;;
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.
156 ;;;
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-")))
171     `(progn
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)
180                                (funcall ,eqx
181                                         (symbol-value ',symbol)
182                                         ,expr-tmp))
183                     (error "already bound differently: ~S")))
184                  (t
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).
190        ;;
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
198        #+sb-xc
199        (eval-when (:compile-toplevel)
200          (let ((,expr-tmp ,symbol))
201            (unless (and (eql (info :variable :kind ',symbol) :constant)
202                         (funcall ,eqx
203                                  (info :variable :constant-value ',symbol)
204                                  ,expr-tmp))
205              (sb!c::%defconstant ',symbol ,expr-tmp ,doc)))))))