Fix build with CLL and CLisp hosts
[sbcl.git] / src / code / early-extensions.lisp
blobf656f54ad8a80003f3d51618b9eda2ae39e200ef
1 ;;;; various extensions (including SB-INT "internal extensions")
2 ;;;; available both in the cross-compilation host Lisp and in the
3 ;;;; target SBCL
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
14 (in-package "SB!IMPL")
16 (defvar *core-pathname* nil
17 #!+sb-doc
18 "The absolute pathname of the running SBCL core.")
20 (defvar *runtime-pathname* nil
21 #!+sb-doc
22 "The absolute pathname of the running SBCL runtime.")
24 ;;; something not EQ to anything we might legitimately READ
25 (defglobal *eof-object* (make-symbol "EOF-OBJECT"))
27 (eval-when (:compile-toplevel :load-toplevel :execute)
28 (defconstant max-hash sb!xc:most-positive-fixnum))
30 (def!type hash ()
31 `(integer 0 ,max-hash))
33 ;;; a type used for indexing into sequences, and for related
34 ;;; quantities like lengths of lists and other sequences.
35 ;;;
36 ;;; A more correct value for the exclusive upper bound for indexing
37 ;;; would be (1- ARRAY-DIMENSION-LIMIT) since ARRAY-DIMENSION-LIMIT is
38 ;;; the exclusive maximum *size* of one array dimension (As specified
39 ;;; in CLHS entries for MAKE-ARRAY and "valid array dimensions"). The
40 ;;; current value is maintained to avoid breaking existing code that
41 ;;; also uses that type for upper bounds on indices (e.g. sequence
42 ;;; length).
43 ;;;
44 ;;; In SBCL, ARRAY-DIMENSION-LIMIT is arranged to be a little smaller
45 ;;; than MOST-POSITIVE-FIXNUM, for implementation (see comment above
46 ;;; ARRAY-DIMENSION-LIMIT) and efficiency reasons: staying below
47 ;;; MOST-POSITIVE-FIXNUM lets the system know it can increment a value
48 ;;; of type INDEX without having to worry about using a bignum to
49 ;;; represent the result.
50 (def!type index () `(integer 0 (,sb!xc:array-dimension-limit)))
52 ;;; like INDEX, but only up to half the maximum. Used by hash-table
53 ;;; code that does plenty to (aref v (* 2 i)) and (aref v (1+ (* 2 i))).
54 (def!type index/2 () `(integer 0 (,(floor sb!xc:array-dimension-limit 2))))
56 ;;; like INDEX, but augmented with -1 (useful when using the index
57 ;;; to count downwards to 0, e.g. LOOP FOR I FROM N DOWNTO 0, with
58 ;;; an implementation which terminates the loop by testing for the
59 ;;; index leaving the loop range)
60 (def!type index-or-minus-1 () `(integer -1 (,sb!xc:array-dimension-limit)))
62 ;;; A couple of VM-related types that are currently used only on the
63 ;;; alpha platform. -- CSR, 2002-06-24
64 (def!type unsigned-byte-with-a-bite-out (s bite)
65 (cond ((eq s '*) 'integer)
66 ((and (integerp s) (> s 0))
67 (let ((bound (ash 1 s)))
68 `(integer 0 ,(- bound bite 1))))
70 (error "Bad size specified for UNSIGNED-BYTE type specifier: ~S." s))))
72 ;;; Motivated by the mips port. -- CSR, 2002-08-22
73 (def!type signed-byte-with-a-bite-out (s bite)
74 (cond ((eq s '*) 'integer)
75 ((and (integerp s) (> s 1))
76 (let ((bound (ash 1 (1- s))))
77 `(integer ,(- bound) ,(- bound bite 1))))
79 (error "Bad size specified for SIGNED-BYTE type specifier: ~S." s))))
81 (def!type load/store-index (scale lowtag min-offset
82 &optional (max-offset min-offset))
83 `(integer ,(- (truncate (+ (ash 1 16)
84 (* min-offset sb!vm:n-word-bytes)
85 (- lowtag))
86 scale))
87 ,(truncate (- (+ (1- (ash 1 16)) lowtag)
88 (* max-offset sb!vm:n-word-bytes))
89 scale)))
91 #!+(or x86 x86-64)
92 (defun displacement-bounds (lowtag element-size data-offset)
93 (let* ((adjustment (- (* data-offset sb!vm:n-word-bytes) lowtag))
94 (bytes-per-element (ceiling element-size sb!vm:n-byte-bits))
95 (min (truncate (+ sb!vm::minimum-immediate-offset adjustment)
96 bytes-per-element))
97 (max (truncate (+ sb!vm::maximum-immediate-offset adjustment)
98 bytes-per-element)))
99 (values min max)))
101 #!+(or x86 x86-64)
102 (def!type constant-displacement (lowtag element-size data-offset)
103 (flet ((integerify (x)
104 (etypecase x
105 (integer x)
106 (symbol (symbol-value x)))))
107 (let ((lowtag (integerify lowtag))
108 (element-size (integerify element-size))
109 (data-offset (integerify data-offset)))
110 (multiple-value-bind (min max) (displacement-bounds lowtag
111 element-size
112 data-offset)
113 `(integer ,min ,max)))))
115 ;;; Similar to FUNCTION, but the result type is "exactly" specified:
116 ;;; if it is an object type, then the function returns exactly one
117 ;;; value, if it is a short form of VALUES, then this short form
118 ;;; specifies the exact number of values.
119 (def!type sfunction (args &optional result)
120 (let ((result (cond ((eq result '*) '*)
121 ((or (atom result)
122 (not (eq (car result) 'values)))
123 `(values ,result &optional))
124 ((intersection (cdr result) sb!xc:lambda-list-keywords)
125 result)
126 (t `(values ,@(cdr result) &optional)))))
127 `(function ,args ,result)))
129 ;;; a type specifier
131 ;;; FIXME: The SB!KERNEL:INSTANCE here really means CL:CLASS.
132 ;;; However, the CL:CLASS type is only defined once PCL is loaded,
133 ;;; which is before this is evaluated. Once PCL is moved into cold
134 ;;; init, this might be fixable.
135 (def!type type-specifier () '(or list symbol instance))
137 ;;; the default value used for initializing character data. The ANSI
138 ;;; spec says this is arbitrary, so we use the value that falls
139 ;;; through when we just let the low-level consing code initialize
140 ;;; all newly-allocated memory to zero.
142 ;;; KLUDGE: It might be nice to use something which is a
143 ;;; STANDARD-CHAR, both to reduce user surprise a little and, probably
144 ;;; more significantly, to help SBCL's cross-compiler (which knows how
145 ;;; to dump STANDARD-CHARs). Unfortunately, the old CMU CL code is
146 ;;; shot through with implicit assumptions that it's #\NULL, and code
147 ;;; in several places (notably both DEFUN MAKE-ARRAY and DEFTRANSFORM
148 ;;; MAKE-ARRAY) would have to be rewritten. -- WHN 2001-10-04
149 (eval-when (:compile-toplevel :load-toplevel :execute)
150 ;; an expression we can use to construct a DEFAULT-INIT-CHAR value
151 ;; at load time (so that we don't need to teach the cross-compiler
152 ;; how to represent and dump non-STANDARD-CHARs like #\NULL)
153 (defparameter *default-init-char-form* '(code-char 0)))
155 ;;; CHAR-CODE values for ASCII characters which we care about but
156 ;;; which aren't defined in section "2.1.3 Standard Characters" of the
157 ;;; ANSI specification for Lisp
159 ;;; KLUDGE: These are typically used in the idiom (CODE-CHAR
160 ;;; FOO-CHAR-CODE). I suspect that the current implementation is
161 ;;; expanding this idiom into a full call to CODE-CHAR, which is an
162 ;;; annoying overhead. I should check whether this is happening, and
163 ;;; if so, perhaps implement a DEFTRANSFORM or something to stop it.
164 ;;; (or just find a nicer way of expressing characters portably?) --
165 ;;; WHN 19990713
166 (def!constant bell-char-code 7)
167 (def!constant backspace-char-code 8)
168 (def!constant tab-char-code 9)
169 (def!constant line-feed-char-code 10)
170 (def!constant form-feed-char-code 12)
171 (def!constant return-char-code 13)
172 (def!constant escape-char-code 27)
173 (def!constant rubout-char-code 127)
175 ;;;; type-ish predicates
177 ;;; X may contain cycles -- a conservative approximation. This
178 ;;; occupies a somewhat uncomfortable niche between being fast for
179 ;;; common cases (we don't want to allocate a hash-table), and not
180 ;;; falling down to exponential behaviour for large trees (so we set
181 ;;; an arbitrady depth limit beyond which we punt).
182 (defun maybe-cyclic-p (x &optional (depth-limit 12))
183 (and (listp x)
184 (labels ((safe-cddr (cons)
185 (let ((cdr (cdr cons)))
186 (when (consp cdr)
187 (cdr cdr))))
188 (check-cycle (object seen depth)
189 (when (and (consp object)
190 (or (> depth depth-limit)
191 (member object seen)
192 (circularp object seen depth)))
193 (return-from maybe-cyclic-p t)))
194 (circularp (list seen depth)
195 ;; Almost regular circular list detection, with a twist:
196 ;; we also check each element of the list for upward
197 ;; references using CHECK-CYCLE.
198 (do ((fast (cons (car list) (cdr list)) (safe-cddr fast))
199 (slow list (cdr slow)))
200 ((not (consp fast))
201 ;; Not CDR-circular, need to check remaining CARs yet
202 (do ((tail slow (and (cdr tail))))
203 ((not (consp tail))
204 nil)
205 (check-cycle (car tail) (cons tail seen) (1+ depth))))
206 (check-cycle (car slow) (cons slow seen) (1+ depth))
207 (when (eq fast slow)
208 (return t)))))
209 (circularp x (list x) 0))))
211 ;;; Is X a (possibly-improper) list of at least N elements?
212 (declaim (ftype (function (t index)) list-of-length-at-least-p))
213 (defun list-of-length-at-least-p (x n)
214 (or (zerop n) ; since anything can be considered an improper list of length 0
215 (and (consp x)
216 (list-of-length-at-least-p (cdr x) (1- n)))))
218 ;;; Is X is a positive prime integer?
219 (defun positive-primep (x)
220 ;; This happens to be called only from one place in sbcl-0.7.0, and
221 ;; only for fixnums, we can limit it to fixnums for efficiency. (And
222 ;; if we didn't limit it to fixnums, we should use a cleverer
223 ;; algorithm, since this one scales pretty badly for huge X.)
224 (declare (fixnum x))
225 (if (<= x 5)
226 (and (>= x 2) (/= x 4))
227 (and (not (evenp x))
228 (not (zerop (rem x 3)))
229 (do ((q 6)
230 (r 1)
231 (inc 2 (logxor inc 6)) ;; 2,4,2,4...
232 (d 5 (+ d inc)))
233 ((or (= r 0) (> d q)) (/= r 0))
234 (declare (fixnum inc))
235 (multiple-value-setq (q r) (truncate x d))))))
237 ;;; Could this object contain other objects? (This is important to
238 ;;; the implementation of things like *PRINT-CIRCLE* and the dumper.)
239 (defun compound-object-p (x)
240 (or (consp x)
241 (%instancep x)
242 (typep x '(array t *))))
244 ;;;; the COLLECT macro
245 ;;;;
246 ;;;; comment from CMU CL: "the ultimate collection macro..."
248 ;;; helper functions for COLLECT, which become the expanders of the
249 ;;; MACROLET definitions created by COLLECT
251 ;;; COLLECT-NORMAL-EXPANDER handles normal collection macros.
253 ;;; COLLECT-LIST-EXPANDER handles the list collection case. N-TAIL
254 ;;; is the pointer to the current tail of the list, or NIL if the list
255 ;;; is empty.
256 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
257 (defun collect-normal-expander (n-value fun forms)
258 `(progn
259 ,@(mapcar (lambda (form) `(setq ,n-value (,fun ,form ,n-value))) forms)
260 ,n-value))
261 (defun collect-list-expander (n-value n-tail forms)
262 (let ((n-res (gensym)))
263 `(progn
264 ,@(mapcar (lambda (form)
265 `(let ((,n-res (cons ,form nil)))
266 (cond (,n-tail
267 (setf (cdr ,n-tail) ,n-res)
268 (setq ,n-tail ,n-res))
270 (setq ,n-tail ,n-res ,n-value ,n-res)))))
271 forms)
272 ,n-value))))
274 ;;; Collect some values somehow. Each of the collections specifies a
275 ;;; bunch of things which collected during the evaluation of the body
276 ;;; of the form. The name of the collection is used to define a local
277 ;;; macro, a la MACROLET. Within the body, this macro will evaluate
278 ;;; each of its arguments and collect the result, returning the
279 ;;; current value after the collection is done. The body is evaluated
280 ;;; as a PROGN; to get the final values when you are done, just call
281 ;;; the collection macro with no arguments.
283 ;;; INITIAL-VALUE is the value that the collection starts out with,
284 ;;; which defaults to NIL. FUNCTION is the function which does the
285 ;;; collection. It is a function which will accept two arguments: the
286 ;;; value to be collected and the current collection. The result of
287 ;;; the function is made the new value for the collection. As a
288 ;;; totally magical special-case, FUNCTION may be COLLECT, which tells
289 ;;; us to build a list in forward order; this is the default. If an
290 ;;; INITIAL-VALUE is supplied for COLLECT, the stuff will be RPLACD'd
291 ;;; onto the end. Note that FUNCTION may be anything that can appear
292 ;;; in the functional position, including macros and lambdas.
293 (defmacro collect (collections &body body)
294 (let ((macros ())
295 (binds ()))
296 (dolist (spec collections)
297 (unless (proper-list-of-length-p spec 1 3)
298 (error "malformed collection specifier: ~S" spec))
299 (let* ((name (first spec))
300 (default (second spec))
301 (kind (or (third spec) 'collect))
302 (n-value (gensym (concatenate 'string
303 (symbol-name name)
304 "-N-VALUE-"))))
305 (push `(,n-value ,default) binds)
306 (if (eq kind 'collect)
307 (let ((n-tail (gensym (concatenate 'string
308 (symbol-name name)
309 "-N-TAIL-"))))
310 (if default
311 (push `(,n-tail (last ,n-value)) binds)
312 (push n-tail binds))
313 (push `(,name (&rest args)
314 (collect-list-expander ',n-value ',n-tail args))
315 macros))
316 (push `(,name (&rest args)
317 (collect-normal-expander ',n-value ',kind args))
318 macros))))
319 `(macrolet ,macros (let* ,(nreverse binds) ,@body))))
321 ;;;; some old-fashioned functions. (They're not just for old-fashioned
322 ;;;; code, they're also used as optimized forms of the corresponding
323 ;;;; general functions when the compiler can prove that they're
324 ;;;; equivalent.)
326 ;;; like (MEMBER ITEM LIST :TEST #'EQ)
327 (defun memq (item list)
328 #!+sb-doc
329 "Return tail of LIST beginning with first element EQ to ITEM."
330 ;; KLUDGE: These could be and probably should be defined as
331 ;; (MEMBER ITEM LIST :TEST #'EQ)),
332 ;; but when I try to cross-compile that, I get an error from
333 ;; LTN-ANALYZE-KNOWN-CALL, "Recursive known function definition". The
334 ;; comments for that error say it "is probably a botched interpreter stub".
335 ;; Rather than try to figure that out, I just rewrote this function from
336 ;; scratch. -- WHN 19990512
337 (do ((i list (cdr i)))
338 ((null i))
339 (when (eq (car i) item)
340 (return i))))
342 ;;; like (ASSOC ITEM ALIST :TEST #'EQ):
343 ;;; Return the first pair of ALIST where ITEM is EQ to the key of
344 ;;; the pair.
345 (defun assq (item alist)
346 ;; KLUDGE: CMU CL defined this with
347 ;; (DECLARE (INLINE ASSOC))
348 ;; (ASSOC ITEM ALIST :TEST #'EQ))
349 ;; which is pretty, but which would have required adding awkward
350 ;; build order constraints on SBCL (or figuring out some way to make
351 ;; inline definitions installable at build-the-cross-compiler time,
352 ;; which was too ambitious for now). Rather than mess with that, we
353 ;; just define ASSQ explicitly in terms of more primitive
354 ;; operations:
355 (dolist (pair alist)
356 ;; though it may look more natural to write this as
357 ;; (AND PAIR (EQ (CAR PAIR) ITEM))
358 ;; the temptation to do so should be resisted, as pointed out by PFD
359 ;; sbcl-devel 2003-08-16, as NIL elements are rare in association
360 ;; lists. -- CSR, 2003-08-16
361 (when (and (eq (car pair) item) (not (null pair)))
362 (return pair))))
364 ;;; like (DELETE .. :TEST #'EQ):
365 ;;; Delete all LIST entries EQ to ITEM (destructively modifying
366 ;;; LIST), and return the modified LIST.
367 (defun delq (item list)
368 (let ((list list))
369 (do ((x list (cdr x))
370 (splice '()))
371 ((endp x) list)
372 (cond ((eq item (car x))
373 (if (null splice)
374 (setq list (cdr x))
375 (rplacd splice (cdr x))))
376 (t (setq splice x)))))) ; Move splice along to include element.
379 ;;; like (POSITION .. :TEST #'EQ):
380 ;;; Return the position of the first element EQ to ITEM.
381 (defun posq (item list)
382 (do ((i list (cdr i))
383 (j 0 (1+ j)))
384 ((null i))
385 (when (eq (car i) item)
386 (return j))))
388 (declaim (inline neq))
389 (defun neq (x y)
390 (not (eq x y)))
392 ;;; not really an old-fashioned function, but what the calling
393 ;;; convention should've been: like NTH, but with the same argument
394 ;;; order as in all the other indexed dereferencing functions, with
395 ;;; the collection first and the index second
396 (declaim (inline nth-but-with-sane-arg-order))
397 (declaim (ftype (function (list index) t) nth-but-with-sane-arg-order))
398 (defun nth-but-with-sane-arg-order (list index)
399 (nth index list))
401 (defun adjust-list (list length initial-element)
402 (let ((old-length (length list)))
403 (cond ((< old-length length)
404 (append list (make-list (- length old-length)
405 :initial-element initial-element)))
406 ((> old-length length)
407 (subseq list 0 length))
408 (t list))))
410 ;;;; miscellaneous iteration extensions
412 ;;; like Scheme's named LET
414 ;;; (CMU CL called this ITERATE, and commented it as "the ultimate
415 ;;; iteration macro...". I (WHN) found the old name insufficiently
416 ;;; specific to remind me what the macro means, so I renamed it.)
417 (defmacro named-let (name binds &body body)
418 (dolist (x binds)
419 (unless (proper-list-of-length-p x 2)
420 (error "malformed NAMED-LET variable spec: ~S" x)))
421 `(labels ((,name ,(mapcar #'first binds) ,@body))
422 (,name ,@(mapcar #'second binds))))
424 (defun filter-dolist-declarations (decls)
425 (mapcar (lambda (decl)
426 `(declare ,@(remove-if
427 (lambda (clause)
428 (and (consp clause)
429 (or (eq (car clause) 'type)
430 (eq (car clause) 'ignore))))
431 (cdr decl))))
432 decls))
433 ;;; just like DOLIST, but with one-dimensional arrays
434 (defmacro dovector ((elt vector &optional result) &body body)
435 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
436 (with-unique-names (index length vec)
437 `(let ((,vec ,vector))
438 (declare (type vector ,vec))
439 (do ((,index 0 (1+ ,index))
440 (,length (length ,vec)))
441 ((>= ,index ,length) (let ((,elt nil))
442 ,@(filter-dolist-declarations decls)
443 ,elt
444 ,result))
445 (let ((,elt (aref ,vec ,index)))
446 ,@decls
447 (tagbody
448 ,@forms)))))))
450 ;;; Iterate over the entries in a HASH-TABLE, first obtaining the lock
451 ;;; if the table is a synchronized table.
452 (defmacro dohash (((key-var value-var) table &key result locked) &body body)
453 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
454 (with-unique-names (gen n-more n-table)
455 (let ((iter-form `(with-hash-table-iterator (,gen ,n-table)
456 (loop
457 (multiple-value-bind (,n-more ,key-var ,value-var) (,gen)
458 ,@decls
459 (unless ,n-more (return ,result))
460 ,@forms)))))
461 `(let ((,n-table ,table))
462 ,(if locked
463 `(with-locked-system-table (,n-table)
464 ,iter-form)
465 iter-form))))))
467 ;;; Executes BODY for all entries of PLIST with KEY and VALUE bound to
468 ;;; the respective keys and values.
469 (defmacro doplist ((key val) plist &body body)
470 (with-unique-names (tail)
471 `(let ((,tail ,plist) ,key ,val)
472 (loop (when (null ,tail) (return nil))
473 (setq ,key (pop ,tail))
474 (when (null ,tail)
475 (error "malformed plist, odd number of elements"))
476 (setq ,val (pop ,tail))
477 (progn ,@body)))))
480 ;;;; hash cache utility
482 (eval-when (:compile-toplevel :load-toplevel :execute)
483 (defvar *profile-hash-cache* nil))
485 ;;; Define a hash cache that associates some number of argument values
486 ;;; with a result value. The TEST-FUNCTION paired with each ARG-NAME
487 ;;; is used to compare the value for that arg in a cache entry with a
488 ;;; supplied arg. The TEST-FUNCTION must not error when passed NIL as
489 ;;; its first arg, but need not return any particular value.
490 ;;; TEST-FUNCTION may be any thing that can be placed in CAR position.
492 ;;; This code used to store all the arguments / return values directly
493 ;;; in the cache vector. This was both interrupt- and thread-unsafe, since
494 ;;; it was possible that *-CACHE-ENTER would scribble over a region of the
495 ;;; cache vector which *-CACHE-LOOKUP had only partially processed. Instead
496 ;;; we now store the contents of each cache bucket as a separate array, which
497 ;;; is stored in the appropriate cell in the cache vector. A new bucket array
498 ;;; is created every time *-CACHE-ENTER is called, and the old ones are never
499 ;;; modified. This means that *-CACHE-LOOKUP will always work with a set
500 ;;; of consistent data. The overhead caused by consing new buckets seems to
501 ;;; be insignificant on the grand scale of things. -- JES, 2006-11-02
503 ;;; NAME is used to define these functions:
504 ;;; <name>-CACHE-LOOKUP Arg*
505 ;;; See whether there is an entry for the specified ARGs in the
506 ;;; cache. If not present, the :DEFAULT keyword (default NIL)
507 ;;; determines the result(s).
508 ;;; <name>-CACHE-ENTER Arg* Value*
509 ;;; Encache the association of the specified args with VALUE.
510 ;;; <name>-CACHE-CLEAR
511 ;;; Reinitialize the cache, invalidating all entries and allowing
512 ;;; the arguments and result values to be GC'd.
514 ;;; These other keywords are defined:
515 ;;; :HASH-BITS <n>
516 ;;; The size of the cache as a power of 2.
517 ;;; :HASH-FUNCTION function
518 ;;; Some thing that can be placed in CAR position which will compute
519 ;;; a fixnum with at least (* 2 <hash-bits>) of information in it.
520 ;;; :VALUES <n>
521 ;;; the number of return values cached for each function call
522 (defvar *cache-vector-symbols* nil)
524 (defun drop-all-hash-caches ()
525 (dolist (name *cache-vector-symbols*)
526 (set name nil)))
528 ;; Make a new hash-cache and optionally create the statistics vector.
529 (defun alloc-hash-cache (size symbol)
530 (let (cache)
531 ;; It took me a while to figure out why infinite recursion could occur
532 ;; in VALUES-SPECIFIER-TYPE. It's because SET calls VALUES-SPECIFIER-TYPE.
533 (macrolet ((set! (symbol value)
534 `(#+sb-xc-host set
535 #-sb-xc-host sb!kernel:%set-symbol-global-value
536 ,symbol ,value))
537 (reset-stats ()
538 ;; If statistics gathering is not not compiled-in,
539 ;; no sense in setting a symbol that is never used.
540 ;; While this uses SYMBOLICATE at runtime,
541 ;; it is inconsequential to performance.
542 (if *profile-hash-cache*
543 `(let ((statistics
544 (let ((*package* (symbol-package symbol)))
545 (symbolicate symbol "STATISTICS"))))
546 (unless (boundp statistics)
547 (set! statistics
548 (make-array 3 :element-type 'fixnum
549 :initial-contents '(1 0 0))))))))
550 ;; It would be bad if another thread sees MAKE-ARRAY's result in the
551 ;; global variable before the vector's header+length have been set.
552 ;; This is theoretically possible if the architecture allows out-of-order
553 ;; memory writes. A barrier will prevent that, but a meta-bug prevents
554 ;; using SB!THREAD:BARRIER here. The macro isn't defined yet? (FIXME)
555 ;; Note that this bug already existed and I'm just documenting it.
556 ;; Most likely all caches are made before ever starting multiple threads.
557 (progn ; sb!thread:barrier (:write)
558 (reset-stats)
559 (setq cache (make-array size :initial-element 0)))
560 (set! symbol cache))))
562 ;; At present we make a new vector every time a line is re-written,
563 ;; to make it thread-safe and interrupt-safe. A multi-word compare-and-swap
564 ;; is tricky to code and stronger than we need. It is possible instead
565 ;; to provide multi-word reads that can detect failure of atomicity,
566 ;; and on x86 it's possible to have atomic double-wide read/write,
567 ;; so a 1-arg/1-result cache line needn't cons at all except once
568 ;; (and maybe not even that if we make the cache into pairs of cells).
569 ;; But this way is easier to understand, for now anyway.
570 (macrolet ((def (n)
571 (let* ((ftype `(sfunction ,(make-list n :initial-element t) t))
572 (fn (symbolicate "ALLOC-HASH-CACHE-LINE/"
573 (write-to-string n)))
574 (args (loop for i from 1 to n
575 collect (make-symbol (write-to-string i)))))
576 `(progn
577 (declaim (ftype ,ftype ,fn))
578 (defun ,fn ,args
579 (declare (optimize (safety 0)))
580 ,(if (<= n 3)
581 `(list* ,@args)
582 ;; FIXME: (VECTOR ,@args) should emit exactly the
583 ;; same code as this, except it is worse.
584 `(let ((a (make-array ,n)))
585 ,@(loop for i from 0 for arg in args
586 collect `(setf (svref a ,i) ,arg))
587 a)))))))
588 (def 2)
589 (def 3)
590 (def 4)
591 (def 5)
592 (def 6))
594 ;; Should this be !DEFINE-HASH-CACHE ?
595 (defmacro define-hash-cache (name args
596 &key hash-function hash-bits memoizer
597 (values 1))
598 (declare (ignore memoizer))
599 (dolist (arg args)
600 (unless (= (length arg) 2)
601 (error "bad argument spec: ~S" arg)))
602 (assert (typep hash-bits '(integer 5 14))) ; reasonable bounds
603 (let* ((fun-name (symbolicate name "-MEMO-WRAPPER"))
604 (var-name (symbolicate "**" name "-CACHE-VECTOR**"))
605 (statistics-name
606 (when *profile-hash-cache*
607 (symbolicate var-name "STATISTICS")))
608 (nargs (length args))
609 (size (ash 1 hash-bits))
610 (hashval (make-symbol "HASH"))
611 (cache (make-symbol "CACHE"))
612 (entry (make-symbol "LINE"))
613 (thunk (make-symbol "THUNK"))
614 (arg-vars (mapcar #'first args))
615 (result-temps (loop for i from 1 to values
616 collect (make-symbol (format nil "RES~D" i))))
617 (temps (append (mapcar (lambda (x) (make-symbol (string x)))
618 arg-vars)
619 result-temps))
620 (tests (mapcar (lambda (arg temp) ; -> (EQx ARG #:ARG)
621 `(,(cadr arg) ,(car arg) ,temp))
622 args temps))
623 (cache-type `(simple-vector ,size))
624 (line-type (let ((n (+ nargs values)))
625 (if (<= n 3) 'cons `(simple-vector ,n))))
626 (binds
627 (case (length temps)
628 (2 `((,(first temps) (car ,entry))
629 (,(second temps) (cdr ,entry))))
630 (3 (let ((arg-temp (sb!xc:gensym "ARGS")))
631 `((,arg-temp (cdr ,entry))
632 (,(first temps) (car ,entry))
633 (,(second temps) (car (truly-the cons ,arg-temp)))
634 (,(third temps) (cdr ,arg-temp)))))
635 (t (loop for i from 0 for x in temps
636 collect `(,x (svref ,entry ,i))))))
637 (fun
638 `(defun ,fun-name (,thunk ,@arg-vars)
639 ,@(when *profile-hash-cache* ; count seeks
640 `((when (boundp ',statistics-name)
641 (incf (aref ,statistics-name 0)))))
642 (let ((,hashval (the (signed-byte #.sb!vm:n-fixnum-bits)
643 (funcall ,hash-function ,@arg-vars)))
644 (,cache ,var-name))
645 (when ,cache
646 (let ((,hashval ,hashval))
647 (declare (type (signed-byte #.sb!vm:n-fixnum-bits) ,hashval))
648 (loop repeat 2 do
649 (let ((,entry (svref (truly-the ,cache-type ,cache)
650 (ldb (byte ,hash-bits 0) ,hashval))))
651 (unless (eql ,entry 0)
652 (locally (declare (type ,line-type ,entry))
653 (let* ,binds
654 (when (and ,@tests)
655 (return-from ,fun-name
656 (values ,@result-temps))))))
657 (setq ,hashval (ash ,hashval ,(- hash-bits)))))))
658 (multiple-value-bind ,result-temps (funcall ,thunk)
659 (let ((,entry
660 (,(let ((*package* (symbol-package 'alloc-hash-cache)))
661 (symbolicate "ALLOC-HASH-CACHE-LINE/"
662 (write-to-string (+ nargs values))))
663 ,@arg-vars ,@result-temps))
664 (,cache
665 (truly-the ,cache-type
666 (or ,cache (alloc-hash-cache ,size ',var-name))))
667 (idx1 (ldb (byte ,hash-bits 0) ,hashval))
668 (idx2 (ldb (byte ,hash-bits ,hash-bits) ,hashval)))
669 ,@(when *profile-hash-cache*
670 `((incf (aref ,statistics-name 1)))) ; count misses
671 (cond ((eql (svref ,cache idx1) 0)
672 (setf (svref ,cache idx1) ,entry))
673 ((eql (svref ,cache idx2) 0)
674 (setf (svref ,cache idx2) ,entry))
676 ,@(when *profile-hash-cache* ; count evictions
677 `((incf (aref ,statistics-name 2))))
678 (setf (svref ,cache idx1) ,entry))))
679 (values ,@result-temps))))))
680 `(progn
681 (pushnew ',var-name *cache-vector-symbols*)
682 (defglobal ,var-name nil)
683 ,@(when *profile-hash-cache*
684 `((declaim (type (simple-array fixnum (3)) ,statistics-name))
685 (defvar ,statistics-name)))
686 (declaim (type (or null ,cache-type) ,var-name))
687 (defun ,(symbolicate name "-CACHE-CLEAR") () (setq ,var-name nil))
688 (declaim (inline ,fun-name))
689 ,fun)))
691 ;;; some syntactic sugar for defining a function whose values are
692 ;;; cached by DEFINE-HASH-CACHE
693 ;;; These keywords are mostly defined at DEFINE-HASH-CACHE.
694 ;;; Additional options:
695 ;;; :MEMOIZER <name>
696 ;;; If provided, it is the name of a local macro that must be called
697 ;;; within the body forms to perform cache lookup/insertion.
698 ;;; If not provided, then the function's behavior is to automatically
699 ;;; attempt cache lookup, and on miss, execute the body code and
700 ;;; insert into the cache.
701 ;;; Manual control over memoization is useful if there are cases for
702 ;;; which computing the result is simpler than cache lookup.
704 (defmacro defun-cached ((name &rest options &key
705 (memoizer (make-symbol "MEMOIZE")
706 memoizer-supplied-p)
707 &allow-other-keys)
708 args &body body-decls-doc)
709 (let ((arg-names (mapcar #'car args)))
710 ;; What I wouldn't give to be able to use BINDING*, right?
711 (multiple-value-bind (forms decls doc) (parse-body body-decls-doc)
712 `(progn
713 (define-hash-cache ,name ,args ,@options)
714 (defun ,name ,arg-names
715 ,@decls
716 ,@(if doc (list doc))
717 (macrolet ((,memoizer (&body body)
718 ;; We don't need (DX-FLET ((,thunk () ,@body)) ...)
719 ;; This lambda is a single-use local call within
720 ;; the inline memoizing wrapper.
721 `(,',(symbolicate name "-MEMO-WRAPPER")
722 (lambda () ,@body) ,@',arg-names)))
723 ,@(if memoizer-supplied-p
724 forms
725 `((,memoizer ,@forms)))))))))
727 ;;; FIXME: maybe not the best place
729 ;;; FIXME: think of a better name -- not only does this not have the
730 ;;; CAR recursion of EQUAL, it also doesn't have the special treatment
731 ;;; of pathnames, bit-vectors and strings.
733 ;;; KLUDGE: This means that we will no longer cache specifiers of the
734 ;;; form '(INTEGER (0) 4). This is probably not a disaster.
736 ;;; A helper function for the type system, which is the main user of
737 ;;; these caches: we must be more conservative than EQUAL for some of
738 ;;; our equality tests, because MEMBER and friends refer to EQLity.
739 ;;; So:
740 (defun equal-but-no-car-recursion (x y)
741 (do () (())
742 (cond ((eql x y) (return t))
743 ((and (consp x)
744 (consp y)
745 (eql (pop x) (pop y))))
747 (return)))))
749 ;;;; package idioms
751 ;;; Note: Almost always you want to use FIND-UNDELETED-PACKAGE-OR-LOSE
752 ;;; instead of this function. (The distinction only actually matters when
753 ;;; PACKAGE-DESIGNATOR is actually a deleted package, and in that case
754 ;;; you generally do want to signal an error instead of proceeding.)
755 (defun %find-package-or-lose (package-designator)
756 (or (find-package package-designator)
757 (error 'simple-package-error
758 :package package-designator
759 :format-control "The name ~S does not designate any package."
760 :format-arguments (list package-designator))))
762 ;;; ANSI specifies (in the section for FIND-PACKAGE) that the
763 ;;; consequences of most operations on deleted packages are
764 ;;; unspecified. We try to signal errors in such cases.
765 (defun find-undeleted-package-or-lose (package-designator)
766 (let ((maybe-result (%find-package-or-lose package-designator)))
767 (if (package-name maybe-result) ; if not deleted
768 maybe-result
769 (error 'simple-package-error
770 :package maybe-result
771 :format-control "The package ~S has been deleted."
772 :format-arguments (list maybe-result)))))
774 ;;;; various operations on names
776 ;;; Is NAME a legal function name?
777 (declaim (inline legal-fun-name-p))
778 (defun legal-fun-name-p (name)
779 (values (valid-function-name-p name)))
781 (deftype function-name () '(satisfies legal-fun-name-p))
783 ;;; Signal an error unless NAME is a legal function name.
784 (defun legal-fun-name-or-type-error (name)
785 (unless (legal-fun-name-p name)
786 (error 'simple-type-error
787 :datum name
788 :expected-type 'function-name
789 :format-control "invalid function name: ~S"
790 :format-arguments (list name))))
792 ;;; Given a function name, return the symbol embedded in it.
794 ;;; The ordinary use for this operator (and the motivation for the
795 ;;; name of this operator) is to convert from a function name to the
796 ;;; name of the BLOCK which encloses its body.
798 ;;; Occasionally the operator is useful elsewhere, where the operator
799 ;;; name is less mnemonic. (Maybe it should be changed?)
800 (declaim (ftype (function ((or symbol cons)) symbol) fun-name-block-name))
801 (defun fun-name-block-name (fun-name)
802 (if (symbolp fun-name)
803 fun-name
804 (multiple-value-bind (legalp block-name)
805 (valid-function-name-p fun-name)
806 (if legalp
807 block-name
808 (error "not legal as a function name: ~S" fun-name)))))
810 (defun looks-like-name-of-special-var-p (x)
811 (and (symbolp x)
812 (symbol-package x)
813 (let ((name (symbol-name x)))
814 (and (> (length name) 2) ; to exclude '* and '**
815 (char= #\* (aref name 0))
816 (char= #\* (aref name (1- (length name))))))))
818 ;;;; ONCE-ONLY
819 ;;;;
820 ;;;; "The macro ONCE-ONLY has been around for a long time on various
821 ;;;; systems [..] if you can understand how to write and when to use
822 ;;;; ONCE-ONLY, then you truly understand macro." -- Peter Norvig,
823 ;;;; _Paradigms of Artificial Intelligence Programming: Case Studies
824 ;;;; in Common Lisp_, p. 853
826 ;;; ONCE-ONLY is a utility useful in writing source transforms and
827 ;;; macros. It provides a concise way to wrap a LET around some code
828 ;;; to ensure that some forms are only evaluated once.
830 ;;; Create a LET* which evaluates each value expression, binding a
831 ;;; temporary variable to the result, and wrapping the LET* around the
832 ;;; result of the evaluation of BODY. Within the body, each VAR is
833 ;;; bound to the corresponding temporary variable.
834 (defmacro once-only (specs &body body)
835 (named-let frob ((specs specs)
836 (body body))
837 (if (null specs)
838 `(progn ,@body)
839 (let ((spec (first specs)))
840 ;; FIXME: should just be DESTRUCTURING-BIND of SPEC
841 (unless (proper-list-of-length-p spec 2)
842 (error "malformed ONCE-ONLY binding spec: ~S" spec))
843 (let* ((name (first spec))
844 (exp-temp (gensym "ONCE-ONLY")))
845 `(let ((,exp-temp ,(second spec))
846 (,name (sb!xc:gensym ,(symbol-name name))))
847 `(let ((,,name ,,exp-temp))
848 ,,(frob (rest specs) body))))))))
850 ;;;; various error-checking utilities
852 ;;; This function can be used as the default value for keyword
853 ;;; arguments that must be always be supplied. Since it is known by
854 ;;; the compiler to never return, it will avoid any compile-time type
855 ;;; warnings that would result from a default value inconsistent with
856 ;;; the declared type. When this function is called, it signals an
857 ;;; error indicating that a required &KEY argument was not supplied.
858 ;;; This function is also useful for DEFSTRUCT slot defaults
859 ;;; corresponding to required arguments.
860 (declaim (ftype (function () nil) missing-arg))
861 (defun missing-arg ()
862 #!+sb-doc
863 (/show0 "entering MISSING-ARG")
864 (error "A required &KEY or &OPTIONAL argument was not supplied."))
866 ;;; like CL:ASSERT and CL:CHECK-TYPE, but lighter-weight
868 ;;; (As of sbcl-0.6.11.20, we were using some 400 calls to CL:ASSERT.
869 ;;; The CL:ASSERT restarts and whatnot expand into a significant
870 ;;; amount of code when you multiply them by 400, so replacing them
871 ;;; with this should reduce the size of the system by enough to be
872 ;;; worthwhile. ENFORCE-TYPE is much less common, but might still be
873 ;;; worthwhile, and since I don't really like CERROR stuff deep in the
874 ;;; guts of complex systems anyway, I replaced it too.)
875 (defmacro aver (expr)
876 `(unless ,expr
877 (%failed-aver ',expr)))
879 (defun %failed-aver (expr)
880 ;; hackish way to tell we're in a cold sbcl and output the
881 ;; message before signalling error, as it may be this is too
882 ;; early in the cold init.
883 (when (find-package "SB!C")
884 (fresh-line)
885 (write-line "failed AVER:")
886 (write expr)
887 (terpri))
888 (bug "~@<failed AVER: ~2I~_~A~:>" expr))
890 (defun bug (format-control &rest format-arguments)
891 (error 'bug
892 :format-control format-control
893 :format-arguments format-arguments))
895 (defmacro enforce-type (value type)
896 (once-only ((value value))
897 `(unless (typep ,value ',type)
898 (%failed-enforce-type ,value ',type))))
900 (defun %failed-enforce-type (value type)
901 ;; maybe should be TYPE-BUG, subclass of BUG? If it is changed,
902 ;; check uses of it in user-facing code (e.g. WARN)
903 (error 'simple-type-error
904 :datum value
905 :expected-type type
906 :format-control "~@<~S ~_is not a ~_~S~:>"
907 :format-arguments (list value type)))
909 ;;; Return a function like FUN, but expecting its (two) arguments in
910 ;;; the opposite order that FUN does.
911 (declaim (inline swapped-args-fun))
912 (defun swapped-args-fun (fun)
913 (declare (type function fun))
914 (lambda (x y)
915 (funcall fun y x)))
917 ;;; Return the numeric value of a type bound, i.e. an interval bound
918 ;;; more or less in the format of bounds in ANSI's type specifiers,
919 ;;; where a bare numeric value is a closed bound and a list of a
920 ;;; single numeric value is an open bound.
922 ;;; The "more or less" bit is that the no-bound-at-all case is
923 ;;; represented by NIL (not by * as in ANSI type specifiers); and in
924 ;;; this case we return NIL.
925 (defun type-bound-number (x)
926 (if (consp x)
927 (destructuring-bind (result) x result)
930 ;;; some commonly-occuring CONSTANTLY forms
931 (macrolet ((def-constantly-fun (name constant-expr)
932 `(setf (symbol-function ',name)
933 (constantly ,constant-expr))))
934 (def-constantly-fun constantly-t t)
935 (def-constantly-fun constantly-nil nil)
936 (def-constantly-fun constantly-0 0))
938 ;;; If X is a symbol, see whether it is present in *FEATURES*. Also
939 ;;; handle arbitrary combinations of atoms using NOT, AND, OR.
940 (defun featurep (x)
941 (typecase x
942 (cons
943 (case (car x)
944 ((:not not)
945 (cond
946 ((cddr x)
947 (error "too many subexpressions in feature expression: ~S" x))
948 ((null (cdr x))
949 (error "too few subexpressions in feature expression: ~S" x))
950 (t (not (featurep (cadr x))))))
951 ((:and and) (every #'featurep (cdr x)))
952 ((:or or) (some #'featurep (cdr x)))
954 (error "unknown operator in feature expression: ~S." x))))
955 (symbol (not (null (memq x *features*))))
957 (error "invalid feature expression: ~S" x))))
960 ;;;; utilities for two-VALUES predicates
962 (defmacro not/type (x)
963 (let ((val (gensym "VAL"))
964 (win (gensym "WIN")))
965 `(multiple-value-bind (,val ,win)
967 (if ,win
968 (values (not ,val) t)
969 (values nil nil)))))
971 (defmacro and/type (x y)
972 `(multiple-value-bind (val1 win1) ,x
973 (if (and (not val1) win1)
974 (values nil t)
975 (multiple-value-bind (val2 win2) ,y
976 (if (and val1 val2)
977 (values t t)
978 (values nil (and win2 (not val2))))))))
980 ;;; sort of like ANY and EVERY, except:
981 ;;; * We handle two-VALUES predicate functions, as SUBTYPEP does.
982 ;;; (And if the result is uncertain, then we return (VALUES NIL NIL),
983 ;;; as SUBTYPEP does.)
984 ;;; * THING is just an atom, and we apply OP (an arity-2 function)
985 ;;; successively to THING and each element of LIST.
986 (defun any/type (op thing list)
987 (declare (type function op))
988 (let ((certain? t))
989 (dolist (i list (values nil certain?))
990 (multiple-value-bind (sub-value sub-certain?) (funcall op thing i)
991 (if sub-certain?
992 (when sub-value (return (values t t)))
993 (setf certain? nil))))))
994 (defun every/type (op thing list)
995 (declare (type function op))
996 (let ((certain? t))
997 (dolist (i list (if certain? (values t t) (values nil nil)))
998 (multiple-value-bind (sub-value sub-certain?) (funcall op thing i)
999 (if sub-certain?
1000 (unless sub-value (return (values nil t)))
1001 (setf certain? nil))))))
1003 ;;;; DEFPRINTER
1005 ;;; These functions are called by the expansion of the DEFPRINTER
1006 ;;; macro to do the actual printing.
1007 (declaim (ftype (function (symbol t stream) (values))
1008 defprinter-prin1 defprinter-princ))
1009 (defun defprinter-prin1 (name value stream)
1010 (defprinter-prinx #'prin1 name value stream))
1011 (defun defprinter-princ (name value stream)
1012 (defprinter-prinx #'princ name value stream))
1013 (defun defprinter-prinx (prinx name value stream)
1014 (declare (type function prinx))
1015 (when *print-pretty*
1016 (pprint-newline :linear stream))
1017 (format stream ":~A " name)
1018 (funcall prinx value stream)
1019 (values))
1020 (defun defprinter-print-space (stream)
1021 (write-char #\space stream))
1023 ;;; Define some kind of reasonable PRINT-OBJECT method for a
1024 ;;; STRUCTURE-OBJECT class.
1026 ;;; NAME is the name of the structure class, and CONC-NAME is the same
1027 ;;; as in DEFSTRUCT.
1029 ;;; The SLOT-DESCS describe how each slot should be printed. Each
1030 ;;; SLOT-DESC can be a slot name, indicating that the slot should
1031 ;;; simply be printed. A SLOT-DESC may also be a list of a slot name
1032 ;;; and other stuff. The other stuff is composed of keywords followed
1033 ;;; by expressions. The expressions are evaluated with the variable
1034 ;;; which is the slot name bound to the value of the slot. These
1035 ;;; keywords are defined:
1037 ;;; :PRIN1 Print the value of the expression instead of the slot value.
1038 ;;; :PRINC Like :PRIN1, only PRINC the value
1039 ;;; :TEST Only print something if the test is true.
1041 ;;; If no printing thing is specified then the slot value is printed
1042 ;;; as if by PRIN1.
1044 ;;; The structure being printed is bound to STRUCTURE and the stream
1045 ;;; is bound to STREAM.
1046 (defmacro defprinter ((name
1047 &key
1048 (conc-name (concatenate 'simple-string
1049 (symbol-name name)
1050 "-"))
1051 identity)
1052 &rest slot-descs)
1053 (let ((first? t)
1054 maybe-print-space
1055 (reversed-prints nil)
1056 (stream (sb!xc:gensym "STREAM")))
1057 (flet ((sref (slot-name)
1058 `(,(symbolicate conc-name slot-name) structure)))
1059 (dolist (slot-desc slot-descs)
1060 (if first?
1061 (setf maybe-print-space nil
1062 first? nil)
1063 (setf maybe-print-space `(defprinter-print-space ,stream)))
1064 (cond ((atom slot-desc)
1065 (push maybe-print-space reversed-prints)
1066 (push `(defprinter-prin1 ',slot-desc ,(sref slot-desc) ,stream)
1067 reversed-prints))
1069 (let ((sname (first slot-desc))
1070 (test t))
1071 (collect ((stuff))
1072 (do ((option (rest slot-desc) (cddr option)))
1073 ((null option)
1074 (push `(let ((,sname ,(sref sname)))
1075 (when ,test
1076 ,maybe-print-space
1077 ,@(or (stuff)
1078 `((defprinter-prin1
1079 ',sname ,sname ,stream)))))
1080 reversed-prints))
1081 (case (first option)
1082 (:prin1
1083 (stuff `(defprinter-prin1
1084 ',sname ,(second option) ,stream)))
1085 (:princ
1086 (stuff `(defprinter-princ
1087 ',sname ,(second option) ,stream)))
1088 (:test (setq test (second option)))
1090 (error "bad option: ~S" (first option)))))))))))
1091 `(sb!xc:defmethod print-object ((structure ,name) ,stream)
1092 (pprint-logical-block (,stream nil)
1093 (print-unreadable-object (structure
1094 ,stream
1095 :type t
1096 :identity ,identity)
1097 ,@(nreverse reversed-prints))))))
1099 (defun print-symbol-with-prefix (stream symbol colon at)
1100 #!+sb-doc
1101 "For use with ~/: Write SYMBOL to STREAM as if it is not accessible from
1102 the current package."
1103 (declare (ignore colon at))
1104 ;; Only keywords should be accessible from the keyword package, and
1105 ;; keywords are always printed with colons, so this guarantees that the
1106 ;; symbol will not be printed without a prefix.
1107 (let ((*package* *keyword-package*))
1108 (write symbol :stream stream :escape t)))
1110 ;;;; etc.
1112 ;;; Given a pathname, return a corresponding physical pathname.
1113 (defun physicalize-pathname (possibly-logical-pathname)
1114 (if (typep possibly-logical-pathname 'logical-pathname)
1115 (translate-logical-pathname possibly-logical-pathname)
1116 possibly-logical-pathname))
1118 ;;;; Deprecating stuff
1120 (defun normalize-deprecation-replacements (replacements)
1121 (if (or (not (listp replacements))
1122 (eq 'setf (car replacements)))
1123 (list replacements)
1124 replacements))
1126 (defun deprecation-error (since name replacements)
1127 (error 'deprecation-error
1128 :name name
1129 :replacements (normalize-deprecation-replacements replacements)
1130 :since since))
1132 (defun deprecation-warning (state since name replacements
1133 &key (runtime-error (neq :early state)))
1134 (warn (ecase state
1135 (:early 'early-deprecation-warning)
1136 (:late 'late-deprecation-warning)
1137 (:final 'final-deprecation-warning))
1138 :name name
1139 :replacements (normalize-deprecation-replacements replacements)
1140 :since since
1141 :runtime-error runtime-error))
1143 (defun deprecated-function (since name replacements)
1144 (lambda (&rest deprecated-function-args)
1145 (declare (ignore deprecated-function-args))
1146 (deprecation-error since name replacements)))
1148 (defun deprecation-compiler-macro (state since name replacements)
1149 (lambda (form env)
1150 (declare (ignore env))
1151 (deprecation-warning state since name replacements)
1152 form))
1154 ;;; STATE is one of
1156 ;;; :EARLY, for a compile-time style-warning.
1157 ;;; :LATE, for a compile-time full warning.
1158 ;;; :FINAL, for a compile-time full warning and runtime error.
1160 ;;; Suggested duration of each stage is one year, but some things can move faster,
1161 ;;; and some widely used legacy APIs might need to move slower. Internals we don't
1162 ;;; usually add deprecation notes for, but sometimes an internal API actually has
1163 ;;; several external users, in which case we try to be nice about it.
1165 ;;; When you deprecate something, note it here till it is fully gone: makes it
1166 ;;; easier to keep things progressing orderly. Also add the relevant section
1167 ;;; (or update it when deprecation proceeds) in the manual, in
1168 ;;; deprecated.texinfo.
1170 ;;; EARLY:
1172 ;;; LATE:
1173 ;;; - SB-THREAD::GET-MUTEX, since 1.2.10 (02/2015) -> Final: 08/2015
1174 ;;; - SB-THREAD::SPINLOCK (type), since 1.2.10 (02/2015) -> Final: 08/2015
1175 ;;; - SB-THREAD::MAKE-SPINLOCK, since 1.2.10 (02/2015) -> Final: 08/2015
1176 ;;; - SB-THREAD::WITH-SPINLOCK, since 1.2.10 (02/2015) -> Final: 08/2015
1177 ;;; - SB-THREAD::WITH-RECURSIVE-SPINLOCK, since 1.2.10 (02/2015) -> Final: 08/2015
1178 ;;; - SB-THREAD::GET-SPINLOCK, since 1.2.10 (02/2015) -> Final: 08/2015
1179 ;;; - SB-THREAD::RELEASE-SPINLOCK, since 1.2.10 (02/2015) -> Final: 08/2015
1180 ;;; - SB-THREAD::SPINLOCK-VALUE, since 1.2.10 (02/2015) -> Final: 08/2015
1181 ;;; - SB-THREAD::SPINLOCK-NAME, since 1.2.10 (02/2015) -> Final: 08/2015
1182 ;;; - SETF SB-THREAD::SPINLOCK-NAME, since 1.2.10 (02/2015) -> Final: 08/2015
1183 ;;; - SB-C::MERGE-TAIL-CALLS (policy), since 1.2.10 (02/2015) -> Final: 08/2015
1184 ;;; - SB-EXT:QUIT, since 1.2.10 (02/2015) -> Final: 08/2015
1185 ;;; - SB-UNIX:UNIX-EXIT, since 1.2.10 (02/2015) -> Final: 08/2015
1186 ;;; - SB-DEBUG:*SHOW-ENTRY-POINT-DETAILS*, since 1.2.10 (02/2015) -> Final: 08/2015
1188 ;;; FINAL:
1189 ;;; - SB-SYS:OUTPUT-RAW-BYTES, since 1.2.10 (02/2015) -> Remove: 04/2015
1190 ;;; - SB-C::STACK-ALLOCATE-DYNAMIC-EXTENT (policy), since 1.2.10 (02/2015) -> Remove: 04/2015
1191 ;;; - SB-C::STACK-ALLOCATE-VECTOR (policy), since 1.2.10 (02/2015) -> Remove: 04/2015
1192 ;;; - SB-C::STACK-ALLOCATE-VALUE-CELLS (policy), since 1.2.10 (02/2015) -> Remove: 04/2015
1193 ;;; - SB-INTROSPECT:FUNCTION-ARGLIST, since 1.2.10 (02/2015 -> Remove: 04/2015
1194 ;;; - SB-THREAD:JOIN-THREAD-ERROR-THREAD, since 1.2.10 (02/2015) -> Remove: 04/2015
1195 ;;; - SB-THREAD:INTERRUPT-THREAD-ERROR-THREAD since 1.2.10 (02/2015) -> Remove: 04/2015
1197 (deftype deprecation-state ()
1198 '(member :early :late :final))
1200 (defun print-deprecation-message (name since &optional replacements stream)
1201 (apply #'format stream
1202 #+sb-xc-host
1203 "~/sb!impl:print-symbol-with-prefix/ has been ~
1204 deprecated as of SBCL ~A.~
1205 ~#[~;~
1206 ~2%Use ~/sb!impl:print-symbol-with-prefix/ instead.~;~
1207 ~2%Use ~/sb!impl:print-symbol-with-prefix/ or ~
1208 /sb!impl:print-symbol-with-prefix/ instead.~:;~
1209 ~2%Use~@{~#[~; or~] ~
1210 ~/sb!impl:print-symbol-with-prefix/~^,~} instead.~
1212 #-sb-xc-host
1213 "~/sb-impl:print-symbol-with-prefix/ has been ~
1214 deprecated as of SBCL ~A.~
1215 ~#[~;~
1216 ~2%Use ~/sb-impl:print-symbol-with-prefix/ instead.~;~
1217 ~2%Use ~/sb-impl:print-symbol-with-prefix/ or ~
1218 /sb-impl:print-symbol-with-prefix/ instead.~:;~
1219 ~2%Use~@{~#[~; or~] ~
1220 ~/sb-impl:print-symbol-with-prefix/~^,~} instead.~
1222 name since replacements))
1224 (defmacro define-deprecated-function (state since name replacements lambda-list
1225 &body body)
1226 (declare (type deprecation-state state)
1227 (type string since)
1228 (type function-name name)
1229 (type (or function-name list) replacements)
1230 (type list lambda-list))
1231 (let* ((replacements (normalize-deprecation-replacements replacements))
1232 (doc (print-deprecation-message name since replacements)))
1233 `(prog1
1234 ,(ecase state
1235 ((:early :late)
1236 `(defun ,name ,lambda-list
1237 #!+sb-doc ,doc
1238 ,@body))
1239 ((:final)
1240 `(progn
1241 (declaim (ftype (function * nil) ,name))
1242 (setf (fdefinition ',name)
1243 (deprecated-function ,since ',name ',replacements))
1244 #!+sb-doc
1245 (setf (fdocumentation ',name 'function) ,doc)
1246 ',name)))
1247 (setf (compiler-macro-function ',name)
1248 (deprecation-compiler-macro ,state ,since ',name ',replacements)))))
1250 (defun check-deprecated-variable (name)
1251 (let ((info (info :variable :deprecated name)))
1252 (when info
1253 (deprecation-warning (first info) (second info) name (third info))
1254 (values-list info))))
1256 (defmacro define-deprecated-variable (state since name
1257 &key (value nil valuep) replacement)
1258 (declare (ignorable replacement)
1259 (type deprecation-state state)
1260 (type string since)
1261 (type symbol name))
1262 `(prog2
1263 (setf (info :variable :deprecated ',name)
1264 '(,state ,since ,(when replacement `(,replacement))))
1265 ,(if (member state '(:early :late))
1266 `(defvar ,name ,@(when valuep (list value)))
1267 `',name)
1268 #!+sb-doc
1269 (setf (fdocumentation ',name 'variable)
1270 ,(print-deprecation-message name since (list replacement)))))
1272 ;;; Anaphoric macros
1273 (defmacro awhen (test &body body)
1274 `(let ((it ,test))
1275 (when it ,@body)))
1277 (defmacro acond (&rest clauses)
1278 (if (null clauses)
1280 (destructuring-bind ((test &body body) &rest rest) clauses
1281 (once-only ((test test))
1282 `(if ,test
1283 (let ((it ,test)) (declare (ignorable it)),@body)
1284 (acond ,@rest))))))
1286 ;; Given DECLS as returned by from parse-body, and SYMBOLS to be bound
1287 ;; (with LET, MULTIPLE-VALUE-BIND, etc) return two sets of declarations:
1288 ;; those which pertain to the variables and those which don't.
1289 (defun extract-var-decls (decls symbols)
1290 (labels ((applies-to-variables (decl)
1291 (let ((id (car decl)))
1292 (remove-if (lambda (x) (not (memq x symbols)))
1293 (cond ((eq id 'type)
1294 (cddr decl))
1295 ((or (listp id) ; must be a type-specifier
1296 (memq id '(special ignorable ignore
1297 dynamic-extent
1298 truly-dynamic-extent))
1299 (info :type :kind id))
1300 (cdr decl))))))
1301 (partition (spec)
1302 (let ((variables (applies-to-variables spec)))
1303 (cond ((not variables)
1304 (values nil spec))
1305 ((eq (car spec) 'type)
1306 (let ((more (set-difference (cddr spec) variables)))
1307 (values `(type ,(cadr spec) ,@variables)
1308 (if more `(type ,(cadr spec) ,@more)))))
1310 (let ((more (set-difference (cdr spec) variables)))
1311 (values `(,(car spec) ,@variables)
1312 (if more `(,(car spec) ,@more)))))))))
1313 ;; This loop is less inefficient than theoretically possible,
1314 ;; reconstructing the tree even if no need,
1315 ;; but it's just a macroexpander, so... fine.
1316 (collect ((binding-decls))
1317 (let ((filtered
1318 (mapcar (lambda (decl-expr) ; a list headed by DECLARE
1319 (mapcan (lambda (spec)
1320 (multiple-value-bind (binding other)
1321 (partition spec)
1322 (when binding
1323 (binding-decls binding))
1324 (if other (list other))))
1325 (cdr decl-expr)))
1326 decls)))
1327 (values (awhen (binding-decls) `(declare ,@it))
1328 (mapcan (lambda (x) (if x (list `(declare ,@x)))) filtered))))))
1330 ;;; (binding* ({(names initial-value [flag])}*) body)
1331 ;;; FLAG may be NIL or :EXIT-IF-NULL
1333 ;;; This form unites LET*, MULTIPLE-VALUE-BIND and AWHEN.
1334 ;;; Any name in a list of names may be NIL to ignore the respective value.
1335 ;;; If NAMES itself is nil, the initial-value form is evaluated only for effect.
1337 ;;; Clauses with no flags and one binding per clause are equivalent to LET*.
1338 ;;; We reduce to LET* when possible so that the body can contain declarations
1339 ;;; without having to split out declarations which affect variables and insert
1340 ;;; them into the appropriate places. This qualifies as an extreme KLUDGE,
1341 ;;; but has desirable behavior of allowing declarations in the innermost form.
1343 ;;; Caution: don't use declarations of the form (<non-builtin-type-id> <var>)
1344 ;;; before the INFO database is set up in building the cross-compiler,
1345 ;;; or you will probably lose.
1346 ;;; Of course, since some other host Lisps don't seem to think that's
1347 ;;; acceptable syntax anyway, you're pretty much prevented from writing it.
1349 (defmacro binding* ((&rest bindings) &body body)
1350 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
1351 (labels
1352 ((recurse (bindings decls &aux ignores)
1353 (cond
1354 ((some (lambda (x)
1355 (destructuring-bind (names value-form &optional flag) x
1356 (declare (ignore value-form))
1357 (or flag (not (symbolp names)))))
1358 bindings)
1359 (destructuring-bind (names value-form &optional flag) (car bindings)
1360 (etypecase names
1361 ;; () for names is esoteric. Does anyone really need that?
1362 (null (setq names (list (gensym)) ignores names))
1363 (symbol (setq names (list names)))
1364 (list
1365 (setq names (mapcar (lambda (name)
1366 (or name (car (push (gensym) ignores))))
1367 names))))
1368 (multiple-value-bind (binding-decls rest-decls)
1369 ;; If no more bindings, and no (WHEN ...) before the FORMS,
1370 ;; then don't bother parsing decls.
1371 (if (or (cdr bindings) flag)
1372 (extract-var-decls decls
1373 (filter-names names (cdr bindings)))
1374 (values nil decls))
1375 (let ((continue (acond ((cdr bindings) (recurse it rest-decls))
1376 (t (append decls forms)))))
1377 `((multiple-value-bind ,names ,value-form
1378 ,@(decl-expr binding-decls ignores)
1379 ,@(ecase flag
1380 ((nil) continue)
1381 ((:exit-if-null)
1382 `((when ,(first names) ,@continue))))))))))
1384 ;; This case is not strictly necessary now that declarations that
1385 ;; affect variables are correctly inserted into the M-V-BIND,
1386 ;; but it makes the expansion more legible/concise when applicable.
1387 `((let* ,(mapcar (lambda (binding)
1388 (if (car binding)
1389 binding
1390 (let ((var (gensym)))
1391 (push var ignores)
1392 (cons var (cdr binding)))))
1393 bindings)
1394 ,@(decl-expr nil ignores)
1395 ,@body)))))
1396 (filter-names (names more-bindings)
1397 ;; Return the subset of SYMBOLs that does not intersect any
1398 ;; symbol in MORE-BINDINGS. This makes declarations apply only
1399 ;; to the final occurrence of a repeated name, as is the custom.
1400 (remove-if (lambda (x) (subsequently-bound-p x more-bindings)) names))
1401 (subsequently-bound-p (name more-bindings)
1402 (member-if (lambda (binding)
1403 (let ((names (car binding)))
1404 (if (listp names) (memq name names) (eq name names))))
1405 more-bindings))
1406 (decl-expr (binding-decls ignores)
1407 (nconc (if binding-decls (list binding-decls))
1408 ;; IGNORABLE, not IGNORE, just in case :EXIT-IF-NULL reads a gensym
1409 (if ignores `((declare (ignorable ,@ignores)))))))
1410 ;; Zero bindings have to be special-cased. RECURSE returns a list of forms
1411 ;; because we musn't wrap BODY in a PROGN if it contains declarations,
1412 ;; so we unwrap once here, but if the body was returned as the base case
1413 ;; of recursion then (CAR (RECURSE)) would be wrong.
1414 (if bindings (car (recurse bindings decls)) `(locally ,@body)))))
1416 ;;; Delayed evaluation
1417 (defmacro delay (form)
1418 `(cons nil (lambda () ,form)))
1420 (defun force (promise)
1421 (cond ((not (consp promise)) promise)
1422 ((car promise) (cdr promise))
1423 (t (setf (car promise) t
1424 (cdr promise) (funcall (cdr promise))))))
1426 (defun promise-ready-p (promise)
1427 (or (not (consp promise))
1428 (car promise)))
1430 ;;; toplevel helper
1431 (defmacro with-rebound-io-syntax (&body body)
1432 `(%with-rebound-io-syntax (lambda () ,@body)))
1434 (defun %with-rebound-io-syntax (function)
1435 (declare (type function function))
1436 (let ((*package* *package*)
1437 (*print-array* *print-array*)
1438 (*print-base* *print-base*)
1439 (*print-case* *print-case*)
1440 (*print-circle* *print-circle*)
1441 (*print-escape* *print-escape*)
1442 (*print-gensym* *print-gensym*)
1443 (*print-length* *print-length*)
1444 (*print-level* *print-level*)
1445 (*print-lines* *print-lines*)
1446 (*print-miser-width* *print-miser-width*)
1447 (*print-pretty* *print-pretty*)
1448 (*print-radix* *print-radix*)
1449 (*print-readably* *print-readably*)
1450 (*print-right-margin* *print-right-margin*)
1451 (*read-base* *read-base*)
1452 (*read-default-float-format* *read-default-float-format*)
1453 (*read-eval* *read-eval*)
1454 (*read-suppress* *read-suppress*)
1455 (*readtable* *readtable*))
1456 (funcall function)))
1458 ;;; Bind a few "potentially dangerous" printer control variables to
1459 ;;; safe values, respecting current values if possible.
1460 (defmacro with-sane-io-syntax (&body forms)
1461 `(call-with-sane-io-syntax (lambda () ,@forms)))
1463 (defun call-with-sane-io-syntax (function)
1464 (declare (type function function))
1465 (macrolet ((true (sym)
1466 `(and (boundp ',sym) ,sym)))
1467 (let ((*print-readably* nil)
1468 (*print-level* (or (true *print-level*) 6))
1469 (*print-length* (or (true *print-length*) 12)))
1470 (funcall function))))
1472 ;;; Returns a list of members of LIST. Useful for dealing with circular lists.
1473 ;;; For a dotted list returns a secondary value of T -- in which case the
1474 ;;; primary return value does not include the dotted tail.
1475 ;;; If the maximum length is reached, return a secondary value of :MAYBE.
1476 (defun list-members (list &key max-length)
1477 (when list
1478 (do ((tail (cdr list) (cdr tail))
1479 (members (list (car list)) (cons (car tail) members))
1480 (count 0 (1+ count)))
1481 ((or (not (consp tail)) (eq tail list)
1482 (and max-length (>= count max-length)))
1483 (values members (or (not (listp tail))
1484 (and (>= count max-length) :maybe)))))))
1486 ;;; Default evaluator mode (interpeter / compiler)
1488 (declaim (type (member :compile #!+sb-eval :interpret) *evaluator-mode*))
1489 (!defparameter *evaluator-mode* :compile
1490 #!+sb-doc
1491 "Toggle between different evaluator implementations. If set to :COMPILE,
1492 an implementation of EVAL that calls the compiler will be used. If set
1493 to :INTERPRET, an interpreter will be used.")
1495 ;;; Helper for making the DX closure allocation in macros expanding
1496 ;;; to CALL-WITH-FOO less ugly.
1497 (defmacro dx-flet (functions &body forms)
1498 `(flet ,functions
1499 (declare (#+sb-xc-host dynamic-extent #-sb-xc-host truly-dynamic-extent
1500 ,@(mapcar (lambda (func) `(function ,(car func))) functions)))
1501 ,@forms))
1503 ;;; Another similar one.
1504 (defmacro dx-let (bindings &body forms)
1505 `(let ,bindings
1506 (declare (#+sb-xc-host dynamic-extent #-sb-xc-host truly-dynamic-extent
1507 ,@(mapcar (lambda (bind) (if (consp bind) (car bind) bind))
1508 bindings)))
1509 ,@forms))
1511 ;; This is not my preferred name for this function, but chosen for harmony
1512 ;; with everything else that refers to these as 'hash-caches'.
1513 ;; Hashing is just one particular way of memoizing, and it would have been
1514 ;; slightly more abstract and yet at the same time more concrete to say
1515 ;; "memoized-function-caches". "hash-caches" is pretty nonspecific.
1516 #.(if *profile-hash-cache*
1517 '(defun show-hash-cache-statistics ()
1518 (flet ((cache-stats (symbol)
1519 (let* ((name (string symbol))
1520 (statistics (let ((*package* (symbol-package symbol)))
1521 (symbolicate symbol "STATISTICS")))
1522 (prefix
1523 (subseq name 0 (- (length name) (length "VECTOR**")))))
1524 (values (if (boundp statistics)
1525 (symbol-value statistics)
1526 (make-array 3 :element-type 'fixnum))
1527 (subseq prefix 2 (1- (length prefix)))))))
1528 (format t "~%Type function memoization:~% Seek Hit (%)~:
1529 Evict (%) Size full~%")
1530 ;; Sort by descending seek count to rank by likely relative importance
1531 (dolist (symbol (sort (copy-list *cache-vector-symbols*) #'>
1532 :key (lambda (x) (aref (cache-stats x) 0))))
1533 ;; Sadly we can't use BINDING* within this file
1534 (multiple-value-bind (stats short-name) (cache-stats symbol)
1535 (let* ((seek (aref stats 0))
1536 (miss (aref stats 1))
1537 (hit (- seek miss))
1538 (evict (aref stats 2))
1539 (cache (symbol-value symbol)))
1540 (format t "~9d ~9d (~5,1f%) ~8d (~5,1f%) ~4d ~6,1f% ~A~%"
1541 seek hit
1542 (if (plusp seek) (* 100 (/ hit seek)))
1543 evict
1544 (if (plusp seek) (* 100 (/ evict seek)))
1545 (length cache)
1546 (if (plusp (length cache))
1547 (* 100 (/ (count-if-not #'fixnump cache)
1548 (length cache))))
1549 short-name)))))))
1551 (in-package "SB!KERNEL")
1553 (defun fp-zero-p (x)
1554 (typecase x
1555 (single-float (zerop x))
1556 (double-float (zerop x))
1557 #!+long-float
1558 (long-float (zerop x))
1559 (t nil)))
1561 (defun neg-fp-zero (x)
1562 (etypecase x
1563 (single-float
1564 (if (eql x 0.0f0)
1565 (make-unportable-float :single-float-negative-zero)
1566 0.0f0))
1567 (double-float
1568 (if (eql x 0.0d0)
1569 (make-unportable-float :double-float-negative-zero)
1570 0.0d0))
1571 #!+long-float
1572 (long-float
1573 (if (eql x 0.0l0)
1574 (make-unportable-float :long-float-negative-zero)
1575 0.0l0))))
1577 ;;; Signalling an error when trying to print an error condition is
1578 ;;; generally a PITA, so whatever the failure encountered when
1579 ;;; wondering about FILE-POSITION within a condition printer, 'tis
1580 ;;; better silently to give up than to try to complain.
1581 (defun file-position-or-nil-for-error (stream &optional (pos nil posp))
1582 ;; Arguably FILE-POSITION shouldn't be signalling errors at all; but
1583 ;; "NIL if this cannot be determined" in the ANSI spec doesn't seem
1584 ;; absolutely unambiguously to prohibit errors when, e.g., STREAM
1585 ;; has been closed so that FILE-POSITION is a nonsense question. So
1586 ;; my (WHN) impression is that the conservative approach is to
1587 ;; IGNORE-ERRORS. (I encountered this failure from within a homebrew
1588 ;; defsystemish operation where the ERROR-STREAM had been CL:CLOSEd,
1589 ;; I think by nonlocally exiting through a WITH-OPEN-FILE, by the
1590 ;; time an error was reported.)
1591 (ignore-errors
1592 (if posp
1593 (file-position stream pos)
1594 (file-position stream))))
1596 (defun stream-error-position-info (stream &optional position)
1597 ;; Give up early for interactive streams and non-character stream.
1598 (when (or (ignore-errors (interactive-stream-p stream))
1599 (not (subtypep (ignore-errors (stream-element-type stream))
1600 'character)))
1601 (return-from stream-error-position-info))
1603 (flet ((read-content (old-position position)
1604 "Read the content of STREAM into a buffer in order to count
1605 lines and columns."
1606 (unless (and old-position position
1607 (< position sb!xc:array-dimension-limit))
1608 (return-from read-content))
1609 (let ((content
1610 (make-string position :element-type (stream-element-type stream))))
1611 (when (and (file-position-or-nil-for-error stream :start)
1612 (eql position (ignore-errors (read-sequence content stream))))
1613 (file-position-or-nil-for-error stream old-position)
1614 content)))
1615 ;; Lines count from 1, columns from 0. It's stupid and
1616 ;; traditional.
1617 (line (string)
1618 (1+ (count #\Newline string)))
1619 (column (string position)
1620 (- position (or (position #\Newline string :from-end t) 0))))
1621 (let* ((stream-position (file-position-or-nil-for-error stream))
1622 (position (or position
1623 ;; FILE-POSITION is the next character --
1624 ;; error is at the previous one.
1625 (and stream-position (plusp stream-position)
1626 (1- stream-position))))
1627 (content (read-content stream-position position)))
1628 `(,@(when content `((:line ,(line content))
1629 (:column ,(column content position))))
1630 ,@(when position `((:file-position ,position)))))))
1632 (declaim (inline schwartzian-stable-sort-list))
1633 (defun schwartzian-stable-sort-list (list comparator &key key)
1634 (if (null key)
1635 (stable-sort (copy-list list) comparator)
1636 (let* ((key (if (functionp key)
1638 (symbol-function key)))
1639 (wrapped (mapcar (lambda (x)
1640 (cons x (funcall key x)))
1641 list))
1642 (sorted (stable-sort wrapped comparator :key #'cdr)))
1643 (map-into sorted #'car sorted))))
1645 ;;; Just like WITH-OUTPUT-TO-STRING but doesn't close the stream,
1646 ;;; producing more compact code.
1647 (defmacro with-simple-output-to-string
1648 ((var &optional string)
1649 &body body)
1650 (multiple-value-bind (forms decls)
1651 (parse-body body :doc-string-allowed nil)
1652 (if string
1653 `(let ((,var (sb!impl::make-fill-pointer-output-stream ,string)))
1654 ,@decls
1655 ,@forms)
1656 `(let ((,var (make-string-output-stream)))
1657 ,@decls
1658 ,@forms
1659 (get-output-stream-string ,var)))))