Part 1 of 2-part fix for lp#309102 "Explicit-check defknowns"
[sbcl.git] / src / code / print.lisp
blob295ee97a508e3c63d5b31a2a51f32c1d77593622
1 ;;;; the printer
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 ;;;; exported printer control variables
16 (!defvar *print-readably* nil
17 #!+sb-doc
18 "If true, all objects will be printed readably. If readable printing
19 is impossible, an error will be signalled. This overrides the value of
20 *PRINT-ESCAPE*.")
21 (!defvar *print-escape* t
22 #!+sb-doc
23 "Should we print in a reasonably machine-readable way? (possibly
24 overridden by *PRINT-READABLY*)")
25 (!defvar *print-pretty* nil ; (set later when pretty-printer is initialized)
26 #!+sb-doc
27 "Should pretty printing be used?")
28 (!defvar *print-base* 10.
29 #!+sb-doc
30 "The output base for RATIONALs (including integers).")
31 (!defvar *print-radix* nil
32 #!+sb-doc
33 "Should base be verified when printing RATIONALs?")
34 (!defvar *print-level* nil
35 #!+sb-doc
36 "How many levels should be printed before abbreviating with \"#\"?")
37 (!defvar *print-length* nil
38 #!+sb-doc
39 "How many elements at any level should be printed before abbreviating
40 with \"...\"?")
41 (!defvar *print-circle* nil
42 #!+sb-doc
43 "Should we use #n= and #n# notation to preserve uniqueness in general (and
44 circularity in particular) when printing?")
45 (!defvar *print-case* :upcase
46 #!+sb-doc
47 "What case should the printer should use default?")
48 (!defvar *print-array* t
49 #!+sb-doc
50 "Should the contents of arrays be printed?")
51 (!defvar *print-gensym* t
52 #!+sb-doc
53 "Should #: prefixes be used when printing symbols with null SYMBOL-PACKAGE?")
54 (!defvar *print-lines* nil
55 #!+sb-doc
56 "The maximum number of lines to print per object.")
57 (!defvar *print-right-margin* nil
58 #!+sb-doc
59 "The position of the right margin in ems (for pretty-printing).")
60 (!defvar *print-miser-width* nil
61 #!+sb-doc
62 "If the remaining space between the current column and the right margin
63 is less than this, then print using ``miser-style'' output. Miser
64 style conditional newlines are turned on, and all indentations are
65 turned off. If NIL, never use miser mode.")
66 (defvar *print-pprint-dispatch*
67 (sb!pretty::make-pprint-dispatch-table) ; for type-correctness
68 #!+sb-doc
69 "The pprint-dispatch-table that controls how to pretty-print objects.")
70 (!defvar *suppress-print-errors* nil
71 #!+sb-doc
72 "Suppress printer errors when the condition is of the type designated by this
73 variable: an unreadable object representing the error is printed instead.")
75 (defmacro with-standard-io-syntax (&body body)
76 #!+sb-doc
77 "Bind the reader and printer control variables to values that enable READ
78 to reliably read the results of PRINT. These values are:
80 *PACKAGE* the COMMON-LISP-USER package
81 *PRINT-ARRAY* T
82 *PRINT-BASE* 10
83 *PRINT-CASE* :UPCASE
84 *PRINT-CIRCLE* NIL
85 *PRINT-ESCAPE* T
86 *PRINT-GENSYM* T
87 *PRINT-LENGTH* NIL
88 *PRINT-LEVEL* NIL
89 *PRINT-LINES* NIL
90 *PRINT-MISER-WIDTH* NIL
91 *PRINT-PPRINT-DISPATCH* the standard pprint dispatch table
92 *PRINT-PRETTY* NIL
93 *PRINT-RADIX* NIL
94 *PRINT-READABLY* T
95 *PRINT-RIGHT-MARGIN* NIL
96 *READ-BASE* 10
97 *READ-DEFAULT-FLOAT-FORMAT* SINGLE-FLOAT
98 *READ-EVAL* T
99 *READ-SUPPRESS* NIL
100 *READTABLE* the standard readtable
101 SB-EXT:*SUPPRESS-PRINT-ERRORS* NIL
103 `(%with-standard-io-syntax (lambda () ,@body)))
105 (defglobal sb!pretty::*standard-pprint-dispatch-table* nil)
106 ;; duplicate defglobal because this file is compiled before "reader"
107 (defglobal *standard-readtable* nil)
109 (defun %with-standard-io-syntax (function)
110 (declare (type function function))
111 (let ((*package* (find-package "COMMON-LISP-USER"))
112 (*print-array* t)
113 (*print-base* 10)
114 (*print-case* :upcase)
115 (*print-circle* nil)
116 (*print-escape* t)
117 (*print-gensym* t)
118 (*print-length* nil)
119 (*print-level* nil)
120 (*print-lines* nil)
121 (*print-miser-width* nil)
122 (*print-pprint-dispatch* sb!pretty::*standard-pprint-dispatch-table*)
123 (*print-pretty* nil)
124 (*print-radix* nil)
125 (*print-readably* t)
126 (*print-right-margin* nil)
127 (*read-base* 10)
128 (*read-default-float-format* 'single-float)
129 (*read-eval* t)
130 (*read-suppress* nil)
131 (*readtable* *standard-readtable*)
132 (*suppress-print-errors* nil))
133 (funcall function)))
135 ;;;; routines to print objects
137 (macrolet ((def (fn doc &rest forms)
138 (declare (ignorable doc))
139 `(defun ,fn
140 (object
141 &key
142 ,@(if (eq fn 'write) '(stream))
143 ((:escape *print-escape*) *print-escape*)
144 ((:radix *print-radix*) *print-radix*)
145 ((:base *print-base*) *print-base*)
146 ((:circle *print-circle*) *print-circle*)
147 ((:pretty *print-pretty*) *print-pretty*)
148 ((:level *print-level*) *print-level*)
149 ((:length *print-length*) *print-length*)
150 ((:case *print-case*) *print-case*)
151 ((:array *print-array*) *print-array*)
152 ((:gensym *print-gensym*) *print-gensym*)
153 ((:readably *print-readably*) *print-readably*)
154 ((:right-margin *print-right-margin*)
155 *print-right-margin*)
156 ((:miser-width *print-miser-width*)
157 *print-miser-width*)
158 ((:lines *print-lines*) *print-lines*)
159 ((:pprint-dispatch *print-pprint-dispatch*)
160 *print-pprint-dispatch*)
161 ((:suppress-errors *suppress-print-errors*)
162 *suppress-print-errors*))
163 #!+sb-doc ,doc
164 (declare (explicit-check))
165 ,@forms)))
166 (def write
167 "Output OBJECT to the specified stream, defaulting to *STANDARD-OUTPUT*."
168 (output-object object (out-synonym-of stream))
169 object)
170 (def write-to-string
171 "Return the printed representation of OBJECT as a string."
172 (stringify-object object)))
174 ;;; Same as a call to (WRITE OBJECT :STREAM STREAM), but returning OBJECT.
175 (defun %write (object stream)
176 (declare (explicit-check))
177 (output-object object (out-synonym-of stream))
178 object)
180 (defun prin1 (object &optional stream)
181 #!+sb-doc
182 "Output a mostly READable printed representation of OBJECT on the specified
183 STREAM."
184 (declare (explicit-check))
185 (let ((*print-escape* t))
186 (output-object object (out-synonym-of stream)))
187 object)
189 (defun princ (object &optional stream)
190 #!+sb-doc
191 "Output an aesthetic but not necessarily READable printed representation
192 of OBJECT on the specified STREAM."
193 (declare (explicit-check))
194 (let ((*print-escape* nil)
195 (*print-readably* nil))
196 (output-object object (out-synonym-of stream)))
197 object)
199 (defun print (object &optional stream)
200 #!+sb-doc
201 "Output a newline, the mostly READable printed representation of OBJECT, and
202 space to the specified STREAM."
203 (declare (explicit-check))
204 (let ((stream (out-synonym-of stream)))
205 (terpri stream)
206 (prin1 object stream)
207 (write-char #\space stream)
208 object))
210 (defun pprint (object &optional stream)
211 #!+sb-doc
212 "Prettily output OBJECT preceded by a newline."
213 (declare (explicit-check))
214 (let ((*print-pretty* t)
215 (*print-escape* t)
216 (stream (out-synonym-of stream)))
217 (terpri stream)
218 (output-object object stream))
219 (values))
221 (defun prin1-to-string (object)
222 #!+sb-doc
223 "Return the printed representation of OBJECT as a string with
224 slashification on."
225 (let ((*print-escape* t))
226 (stringify-object object)))
228 (defun princ-to-string (object)
229 #!+sb-doc
230 "Return the printed representation of OBJECT as a string with
231 slashification off."
232 (let ((*print-escape* nil)
233 (*print-readably* nil))
234 (stringify-object object)))
236 ;;; This produces the printed representation of an object as a string.
237 ;;; The few ...-TO-STRING functions above call this.
238 (defun stringify-object (object)
239 (let ((stream (make-string-output-stream)))
240 (setup-printer-state)
241 (output-object object stream)
242 (get-output-stream-string stream)))
244 ;;;; support for the PRINT-UNREADABLE-OBJECT macro
246 (defun print-not-readable-error (object stream)
247 (restart-case
248 (error 'print-not-readable :object object)
249 (print-unreadably ()
250 :report "Print unreadably."
251 (let ((*print-readably* nil))
252 (output-object object stream)
253 object))
254 (use-value (o)
255 :report "Supply an object to be printed instead."
256 :interactive
257 (lambda ()
258 (read-evaluated-form "~@<Enter an object (evaluated): ~@:>"))
259 (output-object o stream)
260 o)))
262 ;;; guts of PRINT-UNREADABLE-OBJECT
263 (defun %print-unreadable-object (object stream type identity &optional body)
264 (declare (type (or null function) body))
265 (if *print-readably*
266 (print-not-readable-error object stream)
267 (flet ((print-description ()
268 (when type
269 (write (type-of object) :stream stream :circle nil
270 :level nil :length nil)
271 (write-char #\space stream)
272 (pprint-newline :fill stream))
273 (when body
274 (funcall body))
275 (when identity
276 (when (or body (not type))
277 (write-char #\space stream))
278 (pprint-newline :fill stream)
279 (write-char #\{ stream)
280 (write (get-lisp-obj-address object) :stream stream
281 :radix nil :base 16)
282 (write-char #\} stream))))
283 (cond ((print-pretty-on-stream-p stream)
284 ;; Since we're printing prettily on STREAM, format the
285 ;; object within a logical block. PPRINT-LOGICAL-BLOCK does
286 ;; not rebind the stream when it is already a pretty stream,
287 ;; so output from the body will go to the same stream.
288 (pprint-logical-block (stream nil :prefix "#<" :suffix ">")
289 (print-description)))
291 (write-string "#<" stream)
292 (print-description)
293 (write-char #\> stream)))))
294 nil)
296 ;;;; OUTPUT-OBJECT -- the main entry point
298 ;;; Objects whose print representation identifies them EQLly don't
299 ;;; need to be checked for circularity.
300 (defun uniquely-identified-by-print-p (x)
301 (or (numberp x)
302 (characterp x)
303 (and (symbolp x)
304 (symbol-package x))))
306 (defvar *in-print-error* nil)
308 ;;; Output OBJECT to STREAM observing all printer control variables.
309 (defun output-object (object stream)
310 ;; FIXME: this function is declared EXPLICIT-CHECK, so it allows STREAM
311 ;; to be T or NIL (a stream-designator), which is not really right
312 ;; if eventually the call will be to a PRINT-OBJECT method,
313 ;; since the generic function should always receive a stream.
314 (declare (explicit-check))
315 (labels ((print-it (stream)
316 (if *print-pretty*
317 (sb!pretty:output-pretty-object object stream)
318 (output-ugly-object object stream)))
319 (handle-it (stream)
320 (if *suppress-print-errors*
321 (handler-bind ((condition
322 (lambda (condition) nil
323 (when (typep condition *suppress-print-errors*)
324 (cond (*in-print-error*
325 (write-string "(error printing " stream)
326 (write-string *in-print-error* stream)
327 (write-string ")" stream))
329 ;; Give outer handlers a chance.
330 (with-simple-restart
331 (continue "Suppress the error.")
332 (signal condition))
333 (let ((*print-readably* nil)
334 (*print-escape* t))
335 (write-string
336 "#<error printing a " stream)
337 (let ((*in-print-error* "type"))
338 (output-object (type-of object) stream))
339 (write-string ": " stream)
340 (let ((*in-print-error* "condition"))
341 (output-object condition stream))
342 (write-string ">" stream))))
343 (return-from handle-it object)))))
344 (print-it stream))
345 (print-it stream)))
346 (check-it (stream)
347 (multiple-value-bind (marker initiate)
348 (check-for-circularity object t)
349 (if (eq initiate :initiate)
350 (let ((*circularity-hash-table*
351 (make-hash-table :test 'eq)))
352 (check-it (make-broadcast-stream))
353 (let ((*circularity-counter* 0))
354 (check-it stream)))
355 ;; otherwise
356 (if marker
357 (when (handle-circularity marker stream)
358 (handle-it stream))
359 (handle-it stream))))))
360 (cond (;; Maybe we don't need to bother with circularity detection.
361 (or (not *print-circle*)
362 (uniquely-identified-by-print-p object))
363 (handle-it stream))
364 (;; If we have already started circularity detection, this
365 ;; object might be a shared reference. If we have not, then
366 ;; if it is a compound object it might contain a circular
367 ;; reference to itself or multiple shared references.
368 (or *circularity-hash-table*
369 (compound-object-p object))
370 (check-it stream))
372 (handle-it stream)))))
374 ;;; a hack to work around recurring gotchas with printing while
375 ;;; DEFGENERIC PRINT-OBJECT is being built
377 ;;; (hopefully will go away naturally when CLOS moves into cold init)
378 (defvar *print-object-is-disabled-p*)
380 ;;; Output OBJECT to STREAM observing all printer control variables
381 ;;; except for *PRINT-PRETTY*. Note: if *PRINT-PRETTY* is non-NIL,
382 ;;; then the pretty printer will be used for any components of OBJECT,
383 ;;; just not for OBJECT itself.
384 (defun output-ugly-object (object stream)
385 (typecase object
386 ;; KLUDGE: The TYPECASE approach here is non-ANSI; the ANSI definition of
387 ;; PRINT-OBJECT says it provides printing and we're supposed to provide
388 ;; PRINT-OBJECT methods covering all classes. We deviate from this
389 ;; by using PRINT-OBJECT only when we print instance values. However,
390 ;; ANSI makes it hard to tell that we're deviating from this:
391 ;; (1) ANSI specifies that the user isn't supposed to call PRINT-OBJECT
392 ;; directly.
393 ;; (2) ANSI (section 11.1.2.1.2) says it's undefined to define
394 ;; a method on an external symbol in the CL package which is
395 ;; applicable to arg lists containing only direct instances of
396 ;; standardized classes.
397 ;; Thus, in order for the user to detect our sleaziness in conforming
398 ;; code, he has to do something relatively obscure like
399 ;; (1) actually use tools like FIND-METHOD to look for PRINT-OBJECT
400 ;; methods, or
401 ;; (2) define a PRINT-OBJECT method which is specialized on the stream
402 ;; value (e.g. a Gray stream object).
403 ;; As long as no one comes up with a non-obscure way of detecting this
404 ;; sleaziness, fixing this nonconformity will probably have a low
405 ;; priority. -- WHN 2001-11-25
406 (list
407 (if (null object)
408 (output-symbol object stream)
409 (output-list object stream)))
410 (instance
411 ;; The first case takes the above idea one step further: If an instance
412 ;; isn't a citizen yet, it has no right to a print-object method.
413 ;; Additionally, if the object is an obsolete CONDITION, don't crash.
414 ;; (There is no update-instance protocol for conditions)
415 (let* ((layout (layout-of object))
416 (classoid (layout-classoid layout)))
417 (cond ((or (sb!kernel::undefined-classoid-p classoid)
418 (and (layout-invalid layout) (condition-classoid-p classoid)))
419 ;; not only is this unreadable, it's unprintable too.
420 (print-unreadable-object (object stream :identity t)
421 (format stream "UNPRINTABLE instance of ~W" classoid)))
422 ((not (and (boundp '*print-object-is-disabled-p*)
423 *print-object-is-disabled-p*))
424 (print-object object stream))
425 ((typep object 'structure-object)
426 (default-structure-print object stream *current-level-in-print*))
428 (write-string "#<INSTANCE but not STRUCTURE-OBJECT>" stream)))))
429 (funcallable-instance
430 (cond
431 ((not (and (boundp '*print-object-is-disabled-p*)
432 *print-object-is-disabled-p*))
433 (print-object object stream))
434 (t (output-fun object stream))))
435 (function
436 (output-fun object stream))
437 (symbol
438 (output-symbol object stream))
439 (number
440 (etypecase object
441 (integer
442 (output-integer object stream))
443 (float
444 (output-float object stream))
445 (ratio
446 (output-ratio object stream))
447 (complex
448 (output-complex object stream))))
449 (character
450 (output-character object stream))
451 (vector
452 (output-vector object stream))
453 (array
454 (output-array object stream))
455 (system-area-pointer
456 (output-sap object stream))
457 (weak-pointer
458 (output-weak-pointer object stream))
459 (lra
460 (output-lra object stream))
461 (code-component
462 (output-code-component object stream))
463 (fdefn
464 (output-fdefn object stream))
465 #!+sb-simd-pack
466 (simd-pack
467 (output-simd-pack object stream))
469 (output-random object stream))))
471 ;;;; symbols
473 ;;; values of *PRINT-CASE* and (READTABLE-CASE *READTABLE*) the last
474 ;;; time the printer was called
475 (defvar *previous-case* nil)
476 (defvar *previous-readtable-case* nil)
478 ;;; This variable contains the current definition of one of three
479 ;;; symbol printers. SETUP-PRINTER-STATE sets this variable.
480 (defvar *internal-symbol-output-fun* nil)
481 (declaim (function *internal-symbol-output-fun*))
483 ;;; Output PNAME (a symbol-name or package-name) surrounded with |'s,
484 ;;; and with any embedded |'s or \'s escaped.
485 (defun output-quoted-symbol-name (pname stream)
486 (declare (string pname))
487 (write-char #\| stream)
488 (dotimes (index (length pname))
489 (let ((char (schar pname index)))
490 (when (or (char= char #\\) (char= char #\|))
491 (write-char #\\ stream))
492 (write-char char stream)))
493 (write-char #\| stream))
495 (defun output-symbol (object stream)
496 (declare (symbol object))
497 (if (or *print-escape* *print-readably*)
498 (let ((package (symbol-package object))
499 (name (symbol-name object))
500 (current (sane-package)))
501 (cond
502 ;; The ANSI spec "22.1.3.3.1 Package Prefixes for Symbols"
503 ;; requires that keywords be printed with preceding colons
504 ;; always, regardless of the value of *PACKAGE*.
505 ((eq package *keyword-package*)
506 (write-char #\: stream))
507 ;; Otherwise, if the symbol's home package is the current
508 ;; one, then a prefix is never necessary.
509 ((eq package current))
510 ;; Uninterned symbols print with a leading #:.
511 ((null package)
512 (when (or *print-gensym* *print-readably*)
513 (write-string "#:" stream)))
515 (multiple-value-bind (symbol accessible)
516 (find-symbol name current)
517 ;; If we can find the symbol by looking it up, it need not
518 ;; be qualified. This can happen if the symbol has been
519 ;; inherited from a package other than its home package.
521 ;; To preserve print-read consistency, use the local nickname if
522 ;; one exists.
523 (unless (and accessible (eq symbol object))
524 (let ((prefix (or (car (rassoc package (package-%local-nicknames current)))
525 (package-name package))))
526 (output-symbol-name prefix stream))
527 (if (nth-value 1 (find-external-symbol name package))
528 (write-char #\: stream)
529 (write-string "::" stream))))))
530 (output-symbol-name name stream))
531 (output-symbol-name (symbol-name object) stream nil)))
533 ;;; Output the string NAME as if it were a symbol name. In other
534 ;;; words, diddle its case according to *PRINT-CASE* and
535 ;;; READTABLE-CASE.
536 (defun output-symbol-name (name stream &optional (maybe-quote t))
537 (declare (type simple-string name))
538 (let ((*readtable* (if *print-readably* *standard-readtable* *readtable*)))
539 (setup-printer-state)
540 (if (and maybe-quote (or
541 (and (readtable-normalization *readtable*)
542 (not (sb!unicode:normalized-p name :nfkc)))
543 (symbol-quotep name)))
544 (output-quoted-symbol-name name stream)
545 (funcall *internal-symbol-output-fun* name stream))))
547 ;;;; escaping symbols
549 ;;; When we print symbols we have to figure out if they need to be
550 ;;; printed with escape characters. This isn't a whole lot easier than
551 ;;; reading symbols in the first place.
553 ;;; For each character, the value of the corresponding element is a
554 ;;; fixnum with bits set corresponding to attributes that the
555 ;;; character has. At characters have at least one bit set, so we can
556 ;;; search for any character with a positive test.
557 (defvar *character-attributes*
558 (make-array 160 ; FIXME
559 :element-type '(unsigned-byte 16)
560 :initial-element 0))
561 (declaim (type (simple-array (unsigned-byte 16) (#.160)) ; FIXME
562 *character-attributes*))
564 ;;; constants which are a bit-mask for each interesting character attribute
565 (defconstant other-attribute (ash 1 0)) ; Anything else legal.
566 (defconstant number-attribute (ash 1 1)) ; A numeric digit.
567 (defconstant uppercase-attribute (ash 1 2)) ; An uppercase letter.
568 (defconstant lowercase-attribute (ash 1 3)) ; A lowercase letter.
569 (defconstant sign-attribute (ash 1 4)) ; +-
570 (defconstant extension-attribute (ash 1 5)) ; ^_
571 (defconstant dot-attribute (ash 1 6)) ; .
572 (defconstant slash-attribute (ash 1 7)) ; /
573 (defconstant funny-attribute (ash 1 8)) ; Anything illegal.
575 (eval-when (:compile-toplevel :load-toplevel :execute)
577 ;;; LETTER-ATTRIBUTE is a local of SYMBOL-QUOTEP. It matches letters
578 ;;; that don't need to be escaped (according to READTABLE-CASE.)
579 (defparameter *attribute-names*
580 `((number . number-attribute) (lowercase . lowercase-attribute)
581 (uppercase . uppercase-attribute) (letter . letter-attribute)
582 (sign . sign-attribute) (extension . extension-attribute)
583 (dot . dot-attribute) (slash . slash-attribute)
584 (other . other-attribute) (funny . funny-attribute)))
586 ) ; EVAL-WHEN
588 ;;; For each character, the value of the corresponding element is the
589 ;;; lowest base in which that character is a digit.
590 (declaim (type (simple-array (unsigned-byte 8) (128)) ; FIXME: range?
591 *digit-bases*))
592 (defvar *digit-bases*
593 (make-array 128 ; FIXME
594 :element-type '(unsigned-byte 8)))
596 (defun !printer-cold-init ()
597 ;; The dispatch table will be changed later, so this doesn't really matter
598 ;; except if a full call to WRITE wants to read the current binding.
599 (setq *print-pprint-dispatch* (sb!pretty::make-pprint-dispatch-table))
600 (setq *digit-bases* (make-array 128 ; FIXME
601 :element-type '(unsigned-byte 8)
602 :initial-element 36)
603 *character-attributes* (make-array 160 ; FIXME
604 :element-type '(unsigned-byte 16)
605 :initial-element 0))
606 (dotimes (i 36)
607 (let ((char (digit-char i 36)))
608 (setf (aref *digit-bases* (char-code char)) i)))
610 (flet ((set-bit (char bit)
611 (let ((code (char-code char)))
612 (setf (aref *character-attributes* code)
613 (logior bit (aref *character-attributes* code))))))
615 (dolist (char '(#\! #\@ #\$ #\% #\& #\* #\= #\~ #\[ #\] #\{ #\}
616 #\? #\< #\>))
617 (set-bit char other-attribute))
619 (dotimes (i 10)
620 (set-bit (digit-char i) number-attribute))
622 (do ((code (char-code #\A) (1+ code))
623 (end (char-code #\Z)))
624 ((> code end))
625 (declare (fixnum code end))
626 (set-bit (code-char code) uppercase-attribute)
627 (set-bit (char-downcase (code-char code)) lowercase-attribute))
629 (set-bit #\- sign-attribute)
630 (set-bit #\+ sign-attribute)
631 (set-bit #\^ extension-attribute)
632 (set-bit #\_ extension-attribute)
633 (set-bit #\. dot-attribute)
634 (set-bit #\/ slash-attribute)
636 ;; Mark anything not explicitly allowed as funny.
637 (dotimes (i 160) ; FIXME
638 (when (zerop (aref *character-attributes* i))
639 (setf (aref *character-attributes* i) funny-attribute))))
640 ) ; end !COLD-PRINT-INIT
642 ;;; A FSM-like thingie that determines whether a symbol is a potential
643 ;;; number or has evil characters in it.
644 (defun symbol-quotep (name)
645 (declare (simple-string name))
646 (macrolet ((advance (tag &optional (at-end t))
647 `(progn
648 (when (= index len)
649 ,(if at-end '(go TEST-SIGN) '(return nil)))
650 (setq current (schar name index)
651 code (char-code current)
652 bits (cond ; FIXME
653 ((< code 160) (aref attributes code))
654 ((upper-case-p current) uppercase-attribute)
655 ((lower-case-p current) lowercase-attribute)
656 (t other-attribute)))
657 (incf index)
658 (go ,tag)))
659 (test (&rest attributes)
660 `(not (zerop
661 (the fixnum
662 (logand
663 (logior ,@(mapcar
664 (lambda (x)
665 (or (cdr (assoc x
666 *attribute-names*))
667 (error "Blast!")))
668 attributes))
669 bits)))))
670 (digitp ()
671 `(and (< code 128) ; FIXME
672 (< (the fixnum (aref bases code)) base))))
674 (prog ((len (length name))
675 (attributes *character-attributes*)
676 (bases *digit-bases*)
677 (base *print-base*)
678 (letter-attribute
679 (case (%readtable-case *readtable*)
680 (:upcase uppercase-attribute)
681 (:downcase lowercase-attribute)
682 (t (logior lowercase-attribute uppercase-attribute))))
683 (index 0)
684 (bits 0)
685 (code 0)
686 current)
687 (declare (fixnum len base index bits code))
688 (advance START t)
690 TEST-SIGN ; At end, see whether it is a sign...
691 (return (not (test sign)))
693 OTHER ; not potential number, see whether funny chars...
694 (let ((mask (logxor (logior lowercase-attribute uppercase-attribute
695 funny-attribute)
696 letter-attribute)))
697 (do ((i (1- index) (1+ i)))
698 ((= i len) (return-from symbol-quotep nil))
699 (unless (zerop (logand (let* ((char (schar name i))
700 (code (char-code char)))
701 (cond
702 ((< code 160) (aref attributes code))
703 ((upper-case-p char) uppercase-attribute)
704 ((lower-case-p char) lowercase-attribute)
705 (t other-attribute)))
706 mask))
707 (return-from symbol-quotep t))))
709 START
710 (when (digitp)
711 (if (test letter)
712 (advance LAST-DIGIT-ALPHA)
713 (advance DIGIT)))
714 (when (test letter number other slash) (advance OTHER nil))
715 (when (char= current #\.) (advance DOT-FOUND))
716 (when (test sign extension) (advance START-STUFF nil))
717 (return t)
719 DOT-FOUND ; leading dots...
720 (when (test letter) (advance START-DOT-MARKER nil))
721 (when (digitp) (advance DOT-DIGIT))
722 (when (test number other) (advance OTHER nil))
723 (when (test extension slash sign) (advance START-DOT-STUFF nil))
724 (when (char= current #\.) (advance DOT-FOUND))
725 (return t)
727 START-STUFF ; leading stuff before any dot or digit
728 (when (digitp)
729 (if (test letter)
730 (advance LAST-DIGIT-ALPHA)
731 (advance DIGIT)))
732 (when (test number other) (advance OTHER nil))
733 (when (test letter) (advance START-MARKER nil))
734 (when (char= current #\.) (advance START-DOT-STUFF nil))
735 (when (test sign extension slash) (advance START-STUFF nil))
736 (return t)
738 START-MARKER ; number marker in leading stuff...
739 (when (test letter) (advance OTHER nil))
740 (go START-STUFF)
742 START-DOT-STUFF ; leading stuff containing dot without digit...
743 (when (test letter) (advance START-DOT-STUFF nil))
744 (when (digitp) (advance DOT-DIGIT))
745 (when (test sign extension dot slash) (advance START-DOT-STUFF nil))
746 (when (test number other) (advance OTHER nil))
747 (return t)
749 START-DOT-MARKER ; number marker in leading stuff with dot..
750 ;; leading stuff containing dot without digit followed by letter...
751 (when (test letter) (advance OTHER nil))
752 (go START-DOT-STUFF)
754 DOT-DIGIT ; in a thing with dots...
755 (when (test letter) (advance DOT-MARKER))
756 (when (digitp) (advance DOT-DIGIT))
757 (when (test number other) (advance OTHER nil))
758 (when (test sign extension dot slash) (advance DOT-DIGIT))
759 (return t)
761 DOT-MARKER ; number marker in number with dot...
762 (when (test letter) (advance OTHER nil))
763 (go DOT-DIGIT)
765 LAST-DIGIT-ALPHA ; previous char is a letter digit...
766 (when (or (digitp) (test sign slash))
767 (advance ALPHA-DIGIT))
768 (when (test letter number other dot) (advance OTHER nil))
769 (return t)
771 ALPHA-DIGIT ; seen a digit which is a letter...
772 (when (or (digitp) (test sign slash))
773 (if (test letter)
774 (advance LAST-DIGIT-ALPHA)
775 (advance ALPHA-DIGIT)))
776 (when (test letter) (advance ALPHA-MARKER))
777 (when (test number other dot) (advance OTHER nil))
778 (return t)
780 ALPHA-MARKER ; number marker in number with alpha digit...
781 (when (test letter) (advance OTHER nil))
782 (go ALPHA-DIGIT)
784 DIGIT ; seen only ordinary (non-alphabetic) numeric digits...
785 (when (digitp)
786 (if (test letter)
787 (advance ALPHA-DIGIT)
788 (advance DIGIT)))
789 (when (test number other) (advance OTHER nil))
790 (when (test letter) (advance MARKER))
791 (when (test extension slash sign) (advance DIGIT))
792 (when (char= current #\.) (advance DOT-DIGIT))
793 (return t)
795 MARKER ; number marker in a numeric number...
796 ;; ("What," you may ask, "is a 'number marker'?" It's something
797 ;; that a conforming implementation might use in number syntax.
798 ;; See ANSI 2.3.1.1 "Potential Numbers as Tokens".)
799 (when (test letter) (advance OTHER nil))
800 (go DIGIT))))
802 ;;;; *INTERNAL-SYMBOL-OUTPUT-FUN*
803 ;;;;
804 ;;;; case hackery: These functions are stored in
805 ;;;; *INTERNAL-SYMBOL-OUTPUT-FUN* according to the values of
806 ;;;; *PRINT-CASE* and READTABLE-CASE.
808 ;;; called when:
809 ;;; READTABLE-CASE *PRINT-CASE*
810 ;;; :UPCASE :UPCASE
811 ;;; :DOWNCASE :DOWNCASE
812 ;;; :PRESERVE any
813 (defun output-preserve-symbol (pname stream)
814 (declare (simple-string pname))
815 (write-string pname stream))
817 ;;; called when:
818 ;;; READTABLE-CASE *PRINT-CASE*
819 ;;; :UPCASE :DOWNCASE
820 (defun output-lowercase-symbol (pname stream)
821 (declare (simple-string pname))
822 (dotimes (index (length pname))
823 (let ((char (schar pname index)))
824 (write-char (char-downcase char) stream))))
826 ;;; called when:
827 ;;; READTABLE-CASE *PRINT-CASE*
828 ;;; :DOWNCASE :UPCASE
829 (defun output-uppercase-symbol (pname stream)
830 (declare (simple-string pname))
831 (dotimes (index (length pname))
832 (let ((char (schar pname index)))
833 (write-char (char-upcase char) stream))))
835 ;;; called when:
836 ;;; READTABLE-CASE *PRINT-CASE*
837 ;;; :UPCASE :CAPITALIZE
838 ;;; :DOWNCASE :CAPITALIZE
839 (defun output-capitalize-symbol (pname stream)
840 (declare (simple-string pname))
841 (let ((prev-not-alphanum t)
842 (up (eq (%readtable-case *readtable*) :upcase)))
843 (dotimes (i (length pname))
844 (let ((char (char pname i)))
845 (write-char (if up
846 (if (or prev-not-alphanum (lower-case-p char))
847 char
848 (char-downcase char))
849 (if prev-not-alphanum
850 (char-upcase char)
851 char))
852 stream)
853 (setq prev-not-alphanum (not (alphanumericp char)))))))
855 ;;; called when:
856 ;;; READTABLE-CASE *PRINT-CASE*
857 ;;; :INVERT any
858 (defun output-invert-symbol (pname stream)
859 (declare (simple-string pname))
860 (let ((all-upper t)
861 (all-lower t))
862 (dotimes (i (length pname))
863 (let ((ch (schar pname i)))
864 (when (both-case-p ch)
865 (if (upper-case-p ch)
866 (setq all-lower nil)
867 (setq all-upper nil)))))
868 (cond (all-upper (output-lowercase-symbol pname stream))
869 (all-lower (output-uppercase-symbol pname stream))
871 (write-string pname stream)))))
873 ;;; Set the internal global symbol *INTERNAL-SYMBOL-OUTPUT-FUN*
874 ;;; to the right function depending on the values of *PRINT-CASE*
875 ;;; and (%READTABLE-CASE *READTABLE*).
876 (defun setup-printer-state ()
877 (let ((readtable-case (%readtable-case *readtable*))
878 (print-case *print-case*))
879 (unless (and (eq print-case *previous-case*)
880 (eq readtable-case *previous-readtable-case*))
881 (setq *previous-case* print-case)
882 (setq *previous-readtable-case* readtable-case)
883 (setq *internal-symbol-output-fun*
884 ;; a morally equivalent reformulation of FOP-KNOWN-FUN
885 (macrolet ((load-time-fn (name) `(load-time-value #',name t)))
886 (case readtable-case
887 (:upcase
888 (case print-case
889 (:upcase (load-time-fn output-preserve-symbol))
890 (:downcase (load-time-fn output-lowercase-symbol))
891 (:capitalize (load-time-fn output-capitalize-symbol))))
892 (:downcase
893 (case print-case
894 (:upcase (load-time-fn output-uppercase-symbol))
895 (:downcase (load-time-fn output-preserve-symbol))
896 (:capitalize (load-time-fn output-capitalize-symbol))))
897 (:preserve (load-time-fn output-preserve-symbol))
898 (:invert (load-time-fn output-invert-symbol))))))))
901 (defun test1 ()
902 (let ((*readtable* (copy-readtable nil)))
903 (format t "READTABLE-CASE Input Symbol-name~@
904 ----------------------------------~%")
905 (dolist (readtable-case '(:upcase :downcase :preserve :invert))
906 (setf (readtable-case *readtable*) readtable-case)
907 (dolist (input '("ZEBRA" "Zebra" "zebra"))
908 (format t "~&:~A~16T~A~24T~A"
909 (string-upcase readtable-case)
910 input
911 (symbol-name (read-from-string input)))))))
913 (defun test2 ()
914 (let ((*readtable* (copy-readtable nil)))
915 (format t "READTABLE-CASE *PRINT-CASE* Symbol-name Output Princ~@
916 --------------------------------------------------------~%")
917 (dolist (readtable-case '(:upcase :downcase :preserve :invert))
918 (setf (readtable-case *readtable*) readtable-case)
919 (dolist (*print-case* '(:upcase :downcase :capitalize))
920 (dolist (symbol '(|ZEBRA| |Zebra| |zebra|))
921 (format t "~&:~A~15T:~A~29T~A~42T~A~50T~A"
922 (string-upcase readtable-case)
923 (string-upcase *print-case*)
924 (symbol-name symbol)
925 (prin1-to-string symbol)
926 (princ-to-string symbol)))))))
929 ;;;; recursive objects
931 (defun output-list (list stream)
932 (descend-into (stream)
933 (write-char #\( stream)
934 (let ((length 0)
935 (list list))
936 (loop
937 (punt-print-if-too-long length stream)
938 (output-object (pop list) stream)
939 (unless list
940 (return))
941 (when (or (atom list)
942 (check-for-circularity list))
943 (write-string " . " stream)
944 (output-object list stream)
945 (return))
946 (write-char #\space stream)
947 (incf length)))
948 (write-char #\) stream)))
950 (defun output-unreadable-vector-readably (vector stream)
951 (declare (vector vector))
952 (write-string "#." stream)
953 (write `(coerce ,(coerce vector '(vector t))
954 '(simple-array ,(array-element-type vector) (*)))
955 :stream stream))
957 (defun output-vector (vector stream)
958 (declare (vector vector))
959 (cond ((stringp vector)
960 (cond ((and *print-readably*
961 (not (eq (array-element-type vector)
962 (load-time-value
963 (array-element-type
964 (make-array 0 :element-type 'character))))))
965 (print-not-readable-error vector stream))
966 ((or *print-escape* *print-readably*)
967 (write-char #\" stream)
968 (quote-string vector stream)
969 (write-char #\" stream))
971 (write-string vector stream))))
972 ((not (or *print-array* *print-readably*))
973 (output-terse-array vector stream))
974 ((bit-vector-p vector)
975 (write-string "#*" stream)
976 (dovector (bit vector)
977 ;; (Don't use OUTPUT-OBJECT here, since this code
978 ;; has to work for all possible *PRINT-BASE* values.)
979 (write-char (if (zerop bit) #\0 #\1) stream)))
980 ((or (not *print-readably*)
981 (array-readably-printable-p vector))
982 (descend-into (stream)
983 (write-string "#(" stream)
984 (dotimes (i (length vector))
985 (unless (zerop i)
986 (write-char #\space stream))
987 (punt-print-if-too-long i stream)
988 (output-object (aref vector i) stream))
989 (write-string ")" stream)))
990 (*read-eval*
991 (output-unreadable-vector-readably vector stream))
993 (print-not-readable-error vector stream))))
995 ;;; This function outputs a string quoting characters sufficiently
996 ;;; so that someone can read it in again. Basically, put a slash in
997 ;;; front of an character satisfying NEEDS-SLASH-P.
998 (defun quote-string (string stream)
999 (macrolet ((needs-slash-p (char)
1000 ;; KLUDGE: We probably should look at the readtable, but just do
1001 ;; this for now. [noted by anonymous long ago] -- WHN 19991130
1002 `(or (char= ,char #\\)
1003 (char= ,char #\"))))
1004 (with-array-data ((data string) (start) (end)
1005 :check-fill-pointer t)
1006 (do ((index start (1+ index)))
1007 ((>= index end))
1008 (let ((char (schar data index)))
1009 (when (needs-slash-p char) (write-char #\\ stream))
1010 (write-char char stream))))))
1012 (defun array-readably-printable-p (array)
1013 (and (eq (array-element-type array) t)
1014 (let ((zero (position 0 (array-dimensions array)))
1015 (number (position 0 (array-dimensions array)
1016 :test (complement #'eql)
1017 :from-end t)))
1018 (or (null zero) (null number) (> zero number)))))
1020 ;;; Output the printed representation of any array in either the #< or #A
1021 ;;; form.
1022 (defun output-array (array stream)
1023 (if (or *print-array* *print-readably*)
1024 (output-array-guts array stream)
1025 (output-terse-array array stream)))
1027 ;;; Output the abbreviated #< form of an array.
1028 (defun output-terse-array (array stream)
1029 (let ((*print-level* nil)
1030 (*print-length* nil))
1031 (print-unreadable-object (array stream :type t :identity t))))
1033 ;;; Convert an array into a list that can be used with MAKE-ARRAY's
1034 ;;; :INITIAL-CONTENTS keyword argument.
1035 (defun listify-array (array)
1036 (with-array-data ((data array) (start) (end))
1037 (declare (ignore end))
1038 (labels ((listify (dimensions index)
1039 (if (null dimensions)
1040 (aref data index)
1041 (let* ((dimension (car dimensions))
1042 (dimensions (cdr dimensions))
1043 (count (reduce #'* dimensions)))
1044 (loop for i below dimension
1045 collect (listify dimensions index)
1046 do (incf index count))))))
1047 (listify (array-dimensions array) start))))
1049 (defun output-unreadable-array-readably (array stream)
1050 (write-string "#." stream)
1051 (write `(make-array ',(array-dimensions array)
1052 :element-type ',(array-element-type array)
1053 :initial-contents ',(listify-array array))
1054 :stream stream))
1056 ;;; Output the readable #A form of an array.
1057 (defun output-array-guts (array stream)
1058 (cond ((or (not *print-readably*)
1059 (array-readably-printable-p array))
1060 (write-char #\# stream)
1061 (let ((*print-base* 10)
1062 (*print-radix* nil))
1063 (output-integer (array-rank array) stream))
1064 (write-char #\A stream)
1065 (with-array-data ((data array) (start) (end))
1066 (declare (ignore end))
1067 (sub-output-array-guts data (array-dimensions array) stream start)))
1068 (*read-eval*
1069 (output-unreadable-array-readably array stream))
1071 (print-not-readable-error array stream))))
1073 (defun sub-output-array-guts (array dimensions stream index)
1074 (declare (type (simple-array * (*)) array) (fixnum index))
1075 (cond ((null dimensions)
1076 (output-object (aref array index) stream))
1078 (descend-into (stream)
1079 (write-char #\( stream)
1080 (let* ((dimension (car dimensions))
1081 (dimensions (cdr dimensions))
1082 (count (reduce #'* dimensions)))
1083 (dotimes (i dimension)
1084 (unless (zerop i)
1085 (write-char #\space stream))
1086 (punt-print-if-too-long i stream)
1087 (sub-output-array-guts array dimensions stream index)
1088 (incf index count)))
1089 (write-char #\) stream)))))
1091 ;;; a trivial non-generic-function placeholder for PRINT-OBJECT, for
1092 ;;; use until CLOS is set up (at which time it will be replaced with
1093 ;;; the real generic function implementation)
1094 (defun print-object (instance stream)
1095 (default-structure-print instance stream *current-level-in-print*))
1097 ;;;; integer, ratio, and complex printing (i.e. everything but floats)
1099 (defun %output-radix (base stream)
1100 (write-char #\# stream)
1101 (write-char (case base
1102 (2 #\b)
1103 (8 #\o)
1104 (16 #\x)
1105 (t (%output-reasonable-integer-in-base base 10 stream)
1106 #\r))
1107 stream))
1109 (defun %output-reasonable-integer-in-base (n base stream)
1110 (multiple-value-bind (q r)
1111 (truncate n base)
1112 ;; Recurse until you have all the digits pushed on
1113 ;; the stack.
1114 (unless (zerop q)
1115 (%output-reasonable-integer-in-base q base stream))
1116 ;; Then as each recursive call unwinds, turn the
1117 ;; digit (in remainder) into a character and output
1118 ;; the character.
1119 (write-char
1120 (schar "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" r)
1121 stream)))
1123 ;;; *POWER-CACHE* is an alist mapping bases to power-vectors. It is
1124 ;;; filled and probed by POWERS-FOR-BASE. SCRUB-POWER-CACHE is called
1125 ;;; always prior a GC to drop overly large bignums from the cache.
1127 ;;; It doesn't need a lock, but if you work on SCRUB-POWER-CACHE or
1128 ;;; POWERS-FOR-BASE, see that you don't break the assumptions!
1129 (defvar *power-cache* nil)
1131 (defconstant +power-cache-integer-length-limit+ 2048)
1133 (defun scrub-power-cache ()
1134 (let ((cache *power-cache*))
1135 (dolist (cell cache)
1136 (let ((powers (cdr cell)))
1137 (declare (simple-vector powers))
1138 (let ((too-big (position-if
1139 (lambda (x)
1140 (>= (integer-length x)
1141 +power-cache-integer-length-limit+))
1142 powers)))
1143 (when too-big
1144 (setf (cdr cell) (subseq powers 0 too-big))))))
1145 ;; Since base 10 is overwhelmingly common, make sure it's at head.
1146 ;; Try to keep other bases in a hopefully sensible order as well.
1147 (if (eql 10 (caar cache))
1148 (setf *power-cache* cache)
1149 ;; If we modify the list destructively we need to copy it, otherwise
1150 ;; an alist lookup in progress might be screwed.
1151 (setf *power-cache* (sort (copy-list cache)
1152 (lambda (a b)
1153 (declare (fixnum a b))
1154 (cond ((= 10 a) t)
1155 ((= 10 b) nil)
1156 ((= 16 a) t)
1157 ((= 16 b) nil)
1158 ((= 2 a) t)
1159 ((= 2 b) nil)
1160 (t (< a b))))
1161 :key #'car)))))
1163 ;;; Compute (and cache) a power vector for a BASE and LIMIT:
1164 ;;; the vector holds integers for which
1165 ;;; (aref powers k) == (expt base (expt 2 k))
1166 ;;; holds.
1167 (defun powers-for-base (base limit)
1168 (flet ((compute-powers (from)
1169 (let (powers)
1170 (do ((p from (* p p)))
1171 ((> p limit)
1172 ;; We don't actually need this, but we also
1173 ;; prefer not to cons it up a second time...
1174 (push p powers))
1175 (push p powers))
1176 (nreverse powers))))
1177 ;; Grab a local reference so that we won't stuff consed at the
1178 ;; head by other threads -- or sorting by SCRUB-POWER-CACHE.
1179 (let ((cache *power-cache*))
1180 (let ((cell (assoc base cache)))
1181 (if cell
1182 (let* ((powers (cdr cell))
1183 (len (length powers))
1184 (max (svref powers (1- len))))
1185 (if (> max limit)
1186 powers
1187 (let ((new
1188 (concatenate 'vector powers
1189 (compute-powers (* max max)))))
1190 (setf (cdr cell) new)
1191 new)))
1192 (let ((powers (coerce (compute-powers base) 'vector)))
1193 ;; Add new base to head: SCRUB-POWER-CACHE will later
1194 ;; put it to a better place.
1195 (setf *power-cache* (acons base powers cache))
1196 powers))))))
1198 ;; Algorithm by Harald Hanche-Olsen, sbcl-devel 2005-02-05
1199 (defun %output-huge-integer-in-base (n base stream)
1200 (declare (type bignum n) (type fixnum base))
1201 ;; POWER is a vector for which the following holds:
1202 ;; (aref power k) == (expt base (expt 2 k))
1203 (let* ((power (powers-for-base base n))
1204 (k-start (or (position-if (lambda (x) (> x n)) power)
1205 (bug "power-vector too short"))))
1206 (labels ((bisect (n k exactp)
1207 (declare (fixnum k))
1208 ;; N is the number to bisect
1209 ;; K on initial entry BASE^(2^K) > N
1210 ;; EXACTP is true if 2^K is the exact number of digits
1211 (cond ((zerop n)
1212 (when exactp
1213 (loop repeat (ash 1 k) do (write-char #\0 stream))))
1214 ((zerop k)
1215 (write-char
1216 (schar "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" n)
1217 stream))
1219 (setf k (1- k))
1220 (multiple-value-bind (q r) (truncate n (aref power k))
1221 ;; EXACTP is NIL only at the head of the
1222 ;; initial number, as we don't know the number
1223 ;; of digits there, but we do know that it
1224 ;; doesn't get any leading zeros.
1225 (bisect q k exactp)
1226 (bisect r k (or exactp (plusp q))))))))
1227 (bisect n k-start nil))))
1229 (defun %output-integer-in-base (integer base stream)
1230 (when (minusp integer)
1231 (write-char #\- stream)
1232 (setf integer (- integer)))
1233 ;; The ideal cutoff point between these two algorithms is almost
1234 ;; certainly quite platform dependent: this gives 87 for 32 bit
1235 ;; SBCL, which is about right at least for x86/Darwin.
1236 (if (or (fixnump integer)
1237 (< (integer-length integer) (* 3 sb!vm:n-positive-fixnum-bits)))
1238 (%output-reasonable-integer-in-base integer base stream)
1239 (%output-huge-integer-in-base integer base stream)))
1241 (defun output-integer (integer stream)
1242 (let ((base *print-base*))
1243 (when (and (/= base 10) *print-radix*)
1244 (%output-radix base stream))
1245 (%output-integer-in-base integer base stream)
1246 (when (and *print-radix* (= base 10))
1247 (write-char #\. stream))))
1249 (defun output-ratio (ratio stream)
1250 (let ((base *print-base*))
1251 (when *print-radix*
1252 (%output-radix base stream))
1253 (%output-integer-in-base (numerator ratio) base stream)
1254 (write-char #\/ stream)
1255 (%output-integer-in-base (denominator ratio) base stream)))
1257 (defun output-complex (complex stream)
1258 (write-string "#C(" stream)
1259 ;; FIXME: Could this just be OUTPUT-NUMBER?
1260 (output-object (realpart complex) stream)
1261 (write-char #\space stream)
1262 (output-object (imagpart complex) stream)
1263 (write-char #\) stream))
1265 ;;;; float printing
1267 ;;; FLONUM-TO-STRING (and its subsidiary function FLOAT-STRING) does
1268 ;;; most of the work for all printing of floating point numbers in
1269 ;;; FORMAT. It converts a floating point number to a string in a free
1270 ;;; or fixed format with no exponent. The interpretation of the
1271 ;;; arguments is as follows:
1273 ;;; X - The floating point number to convert, which must not be
1274 ;;; negative.
1275 ;;; WIDTH - The preferred field width, used to determine the number
1276 ;;; of fraction digits to produce if the FDIGITS parameter
1277 ;;; is unspecified or NIL. If the non-fraction digits and the
1278 ;;; decimal point alone exceed this width, no fraction digits
1279 ;;; will be produced unless a non-NIL value of FDIGITS has been
1280 ;;; specified. Field overflow is not considerd an error at this
1281 ;;; level.
1282 ;;; FDIGITS - The number of fractional digits to produce. Insignificant
1283 ;;; trailing zeroes may be introduced as needed. May be
1284 ;;; unspecified or NIL, in which case as many digits as possible
1285 ;;; are generated, subject to the constraint that there are no
1286 ;;; trailing zeroes.
1287 ;;; SCALE - If this parameter is specified or non-NIL, then the number
1288 ;;; printed is (* x (expt 10 scale)). This scaling is exact,
1289 ;;; and cannot lose precision.
1290 ;;; FMIN - This parameter, if specified or non-NIL, is the minimum
1291 ;;; number of fraction digits which will be produced, regardless
1292 ;;; of the value of WIDTH or FDIGITS. This feature is used by
1293 ;;; the ~E format directive to prevent complete loss of
1294 ;;; significance in the printed value due to a bogus choice of
1295 ;;; scale factor.
1297 ;;; Returns:
1298 ;;; (VALUES DIGIT-STRING DIGIT-LENGTH LEADING-POINT TRAILING-POINT DECPNT)
1299 ;;; where the results have the following interpretation:
1301 ;;; DIGIT-STRING - The decimal representation of X, with decimal point.
1302 ;;; DIGIT-LENGTH - The length of the string DIGIT-STRING.
1303 ;;; LEADING-POINT - True if the first character of DIGIT-STRING is the
1304 ;;; decimal point.
1305 ;;; TRAILING-POINT - True if the last character of DIGIT-STRING is the
1306 ;;; decimal point.
1307 ;;; POINT-POS - The position of the digit preceding the decimal
1308 ;;; point. Zero indicates point before first digit.
1310 ;;; NOTE: FLONUM-TO-STRING goes to a lot of trouble to guarantee
1311 ;;; accuracy. Specifically, the decimal number printed is the closest
1312 ;;; possible approximation to the true value of the binary number to
1313 ;;; be printed from among all decimal representations with the same
1314 ;;; number of digits. In free-format output, i.e. with the number of
1315 ;;; digits unconstrained, it is guaranteed that all the information is
1316 ;;; preserved, so that a properly- rounding reader can reconstruct the
1317 ;;; original binary number, bit-for-bit, from its printed decimal
1318 ;;; representation. Furthermore, only as many digits as necessary to
1319 ;;; satisfy this condition will be printed.
1321 ;;; FLOAT-DIGITS actually generates the digits for positive numbers;
1322 ;;; see below for comments.
1324 (defun flonum-to-string (x &optional width fdigits scale fmin)
1325 (declare (type float x))
1326 ;; FIXME: I think only FORMAT-DOLLARS calls FLONUM-TO-STRING with
1327 ;; possibly-negative X.
1328 (setf x (abs x))
1329 (multiple-value-bind (e string)
1330 (if fdigits
1331 (flonum-to-digits x (min (- (+ fdigits (or scale 0)))
1332 (- (or fmin 0))))
1333 (if (and width (> width 1))
1334 (let ((w (multiple-value-list
1335 (flonum-to-digits x
1336 (max 1
1337 (+ (1- width)
1338 (if (and scale (minusp scale))
1339 scale 0)))
1340 t)))
1341 (f (multiple-value-list
1342 (flonum-to-digits x (- (+ (or fmin 0)
1343 (if scale scale 0)))))))
1344 (cond
1345 ((>= (length (cadr w)) (length (cadr f)))
1346 (values-list w))
1347 (t (values-list f))))
1348 (flonum-to-digits x)))
1349 (let ((e (if (zerop x)
1351 (+ e (or scale 0))))
1352 (stream (make-string-output-stream)))
1353 (if (plusp e)
1354 (progn
1355 (write-string string stream :end (min (length string) e))
1356 (dotimes (i (- e (length string)))
1357 (write-char #\0 stream))
1358 (write-char #\. stream)
1359 (write-string string stream :start (min (length string) e))
1360 (when fdigits
1361 (dotimes (i (- fdigits
1362 (- (length string)
1363 (min (length string) e))))
1364 (write-char #\0 stream))))
1365 (progn
1366 (write-string "." stream)
1367 (dotimes (i (- e))
1368 (write-char #\0 stream))
1369 (write-string string stream :end (when fdigits
1370 (min (length string)
1371 (max (or fmin 0)
1372 (+ fdigits e)))))
1373 (when fdigits
1374 (dotimes (i (+ fdigits e (- (length string))))
1375 (write-char #\0 stream)))))
1376 (let ((string (get-output-stream-string stream)))
1377 (values string (length string)
1378 (char= (char string 0) #\.)
1379 (char= (char string (1- (length string))) #\.)
1380 (position #\. string))))))
1382 ;;; implementation of figure 1 from Burger and Dybvig, 1996. It is
1383 ;;; extended in order to handle rounding.
1385 ;;; As the implementation of the Dragon from Classic CMUCL (and
1386 ;;; previously in SBCL above FLONUM-TO-STRING) says: "DO NOT EVEN
1387 ;;; THINK OF ATTEMPTING TO UNDERSTAND THIS CODE WITHOUT READING THE
1388 ;;; PAPER!", and in this case we have to add that even reading the
1389 ;;; paper might not bring immediate illumination as CSR has attempted
1390 ;;; to turn idiomatic Scheme into idiomatic Lisp.
1392 ;;; FIXME: figure 1 from Burger and Dybvig is the unoptimized
1393 ;;; algorithm, noticeably slow at finding the exponent. Figure 2 has
1394 ;;; an improved algorithm, but CSR ran out of energy.
1396 ;;; possible extension for the enthusiastic: printing floats in bases
1397 ;;; other than base 10.
1398 (defconstant single-float-min-e
1399 (- 2 sb!vm:single-float-bias sb!vm:single-float-digits))
1400 (defconstant double-float-min-e
1401 (- 2 sb!vm:double-float-bias sb!vm:double-float-digits))
1402 #!+long-float
1403 (defconstant long-float-min-e
1404 (nth-value 1 (decode-float least-positive-long-float)))
1406 (defun flonum-to-digits (v &optional position relativep)
1407 (let ((print-base 10) ; B
1408 (float-radix 2) ; b
1409 (float-digits (float-digits v)) ; p
1410 (digit-characters "0123456789")
1411 (min-e
1412 (etypecase v
1413 (single-float single-float-min-e)
1414 (double-float double-float-min-e)
1415 #!+long-float
1416 (long-float long-float-min-e))))
1417 (multiple-value-bind (f e)
1418 (integer-decode-float v)
1419 (let (;; FIXME: these even tests assume normal IEEE rounding
1420 ;; mode. I wonder if we should cater for non-normal?
1421 (high-ok (evenp f))
1422 (low-ok (evenp f)))
1423 (with-push-char (:element-type base-char)
1424 (labels ((scale (r s m+ m-)
1425 (do ((k 0 (1+ k))
1426 (s s (* s print-base)))
1427 ((not (or (> (+ r m+) s)
1428 (and high-ok (= (+ r m+) s))))
1429 (do ((k k (1- k))
1430 (r r (* r print-base))
1431 (m+ m+ (* m+ print-base))
1432 (m- m- (* m- print-base)))
1433 ((not (and (plusp (- r m-)) ; Extension to handle zero
1434 (or (< (* (+ r m+) print-base) s)
1435 (and (not high-ok)
1436 (= (* (+ r m+) print-base) s)))))
1437 (values k (generate r s m+ m-)))))))
1438 (generate (r s m+ m-)
1439 (let (d tc1 tc2)
1440 (tagbody
1441 loop
1442 (setf (values d r) (truncate (* r print-base) s))
1443 (setf m+ (* m+ print-base))
1444 (setf m- (* m- print-base))
1445 (setf tc1 (or (< r m-) (and low-ok (= r m-))))
1446 (setf tc2 (or (> (+ r m+) s)
1447 (and high-ok (= (+ r m+) s))))
1448 (when (or tc1 tc2)
1449 (go end))
1450 (push-char (char digit-characters d))
1451 (go loop)
1453 (let ((d (cond
1454 ((and (not tc1) tc2) (1+ d))
1455 ((and tc1 (not tc2)) d)
1456 (t ; (and tc1 tc2)
1457 (if (< (* r 2) s) d (1+ d))))))
1458 (push-char (char digit-characters d))
1459 (return-from generate (get-pushed-string))))))
1460 (initialize ()
1461 (let (r s m+ m-)
1462 (if (>= e 0)
1463 (let* ((be (expt float-radix e))
1464 (be1 (* be float-radix)))
1465 (if (/= f (expt float-radix (1- float-digits)))
1466 (setf r (* f be 2)
1468 m+ be
1469 m- be)
1470 (setf r (* f be1 2)
1471 s (* float-radix 2)
1472 m+ be1
1473 m- be)))
1474 (if (or (= e min-e)
1475 (/= f (expt float-radix (1- float-digits))))
1476 (setf r (* f 2)
1477 s (* (expt float-radix (- e)) 2)
1478 m+ 1
1479 m- 1)
1480 (setf r (* f float-radix 2)
1481 s (* (expt float-radix (- 1 e)) 2)
1482 m+ float-radix
1483 m- 1)))
1484 (when position
1485 (when relativep
1486 (aver (> position 0))
1487 (do ((k 0 (1+ k))
1488 ;; running out of letters here
1489 (l 1 (* l print-base)))
1490 ((>= (* s l) (+ r m+))
1491 ;; k is now \hat{k}
1492 (if (< (+ r (* s (/ (expt print-base (- k position)) 2)))
1493 (* s (expt print-base k)))
1494 (setf position (- k position))
1495 (setf position (- k position 1))))))
1496 (let ((low (max m- (/ (* s (expt print-base position)) 2)))
1497 (high (max m+ (/ (* s (expt print-base position)) 2))))
1498 (when (<= m- low)
1499 (setf m- low)
1500 (setf low-ok t))
1501 (when (<= m+ high)
1502 (setf m+ high)
1503 (setf high-ok t))))
1504 (values r s m+ m-))))
1505 (multiple-value-bind (r s m+ m-) (initialize)
1506 (scale r s m+ m-))))))))
1508 ;;; Given a non-negative floating point number, SCALE-EXPONENT returns
1509 ;;; a new floating point number Z in the range (0.1, 1.0] and an
1510 ;;; exponent E such that Z * 10^E is (approximately) equal to the
1511 ;;; original number. There may be some loss of precision due the
1512 ;;; floating point representation. The scaling is always done with
1513 ;;; long float arithmetic, which helps printing of lesser precisions
1514 ;;; as well as avoiding generic arithmetic.
1516 ;;; When computing our initial scale factor using EXPT, we pull out
1517 ;;; part of the computation to avoid over/under flow. When
1518 ;;; denormalized, we must pull out a large factor, since there is more
1519 ;;; negative exponent range than positive range.
1521 (eval-when (:compile-toplevel :execute)
1522 (setf *read-default-float-format*
1523 #!+long-float 'long-float #!-long-float 'double-float))
1524 (defun scale-exponent (original-x)
1525 (let* ((x (coerce original-x 'long-float)))
1526 (multiple-value-bind (sig exponent) (decode-float x)
1527 (declare (ignore sig))
1528 (if (= x 0.0e0)
1529 (values (float 0.0e0 original-x) 1)
1530 (let* ((ex (locally (declare (optimize (safety 0)))
1531 (the fixnum
1532 (round (* exponent
1533 ;; this is the closest double float
1534 ;; to (log 2 10), but expressed so
1535 ;; that we're not vulnerable to the
1536 ;; host lisp's interpretation of
1537 ;; arithmetic. (FIXME: it turns
1538 ;; out that sbcl itself is off by 1
1539 ;; ulp in this value, which is a
1540 ;; little unfortunate.)
1541 (load-time-value
1542 #!-long-float
1543 (make-double-float 1070810131 1352628735)
1544 #!+long-float
1545 (error "(log 2 10) not computed")))))))
1546 (x (if (minusp ex)
1547 (if (float-denormalized-p x)
1548 #!-long-float
1549 (* x 1.0e16 (expt 10.0e0 (- (- ex) 16)))
1550 #!+long-float
1551 (* x 1.0e18 (expt 10.0e0 (- (- ex) 18)))
1552 (* x 10.0e0 (expt 10.0e0 (- (- ex) 1))))
1553 (/ x 10.0e0 (expt 10.0e0 (1- ex))))))
1554 (do ((d 10.0e0 (* d 10.0e0))
1555 (y x (/ x d))
1556 (ex ex (1+ ex)))
1557 ((< y 1.0e0)
1558 (do ((m 10.0e0 (* m 10.0e0))
1559 (z y (* y m))
1560 (ex ex (1- ex)))
1561 ((>= z 0.1e0)
1562 (values (float z original-x) ex))
1563 (declare (long-float m) (integer ex))))
1564 (declare (long-float d))))))))
1565 (eval-when (:compile-toplevel :execute)
1566 (setf *read-default-float-format* 'single-float))
1568 ;;;; entry point for the float printer
1570 ;;; the float printer as called by PRINT, PRIN1, PRINC, etc. The
1571 ;;; argument is printed free-format, in either exponential or
1572 ;;; non-exponential notation, depending on its magnitude.
1574 ;;; NOTE: When a number is to be printed in exponential format, it is
1575 ;;; scaled in floating point. Since precision may be lost in this
1576 ;;; process, the guaranteed accuracy properties of FLONUM-TO-STRING
1577 ;;; are lost. The difficulty is that FLONUM-TO-STRING performs
1578 ;;; extensive computations with integers of similar magnitude to that
1579 ;;; of the number being printed. For large exponents, the bignums
1580 ;;; really get out of hand. If bignum arithmetic becomes reasonably
1581 ;;; fast and the exponent range is not too large, then it might become
1582 ;;; attractive to handle exponential notation with the same accuracy
1583 ;;; as non-exponential notation, using the method described in the
1584 ;;; Steele and White paper.
1586 ;;; NOTE II: this has been bypassed slightly by implementing Burger
1587 ;;; and Dybvig, 1996. When someone has time (KLUDGE) they can
1588 ;;; probably (a) implement the optimizations suggested by Burger and
1589 ;;; Dyvbig, and (b) remove all vestiges of Dragon4, including from
1590 ;;; fixed-format printing.
1592 ;;; Print the appropriate exponent marker for X and the specified exponent.
1593 (defun print-float-exponent (x exp stream)
1594 (declare (type float x) (type integer exp) (type stream stream))
1595 (let ((*print-radix* nil))
1596 (if (typep x *read-default-float-format*)
1597 (unless (eql exp 0)
1598 (format stream "e~D" exp))
1599 (format stream "~C~D"
1600 (etypecase x
1601 (single-float #\f)
1602 (double-float #\d)
1603 (short-float #\s)
1604 (long-float #\L))
1605 exp))))
1607 (defun output-float-infinity (x stream)
1608 (declare (float x) (stream stream))
1609 (cond (*read-eval*
1610 (write-string "#." stream))
1611 (*print-readably*
1612 (return-from output-float-infinity
1613 (print-not-readable-error x stream)))
1615 (write-string "#<" stream)))
1616 (write-string "SB-EXT:" stream)
1617 (write-string (symbol-name (float-format-name x)) stream)
1618 (write-string (if (plusp x) "-POSITIVE-" "-NEGATIVE-")
1619 stream)
1620 (write-string "INFINITY" stream)
1621 (unless *read-eval*
1622 (write-string ">" stream)))
1624 (defun output-float-nan (x stream)
1625 (print-unreadable-object (x stream)
1626 (princ (float-format-name x) stream)
1627 (write-string (if (float-trapping-nan-p x) " trapping" " quiet") stream)
1628 (write-string " NaN" stream)))
1630 ;;; the function called by OUTPUT-OBJECT to handle floats
1631 (defun output-float (x stream)
1632 (cond
1633 ((float-infinity-p x)
1634 (output-float-infinity x stream))
1635 ((float-nan-p x)
1636 (output-float-nan x stream))
1638 (let ((x (cond ((minusp (float-sign x))
1639 (write-char #\- stream)
1640 (- x))
1642 x))))
1643 (cond
1644 ((zerop x)
1645 (write-string "0.0" stream)
1646 (print-float-exponent x 0 stream))
1648 (output-float-aux x stream -3 8)))))))
1650 (defun output-float-aux (x stream e-min e-max)
1651 (multiple-value-bind (e string)
1652 (flonum-to-digits x)
1653 (cond
1654 ((< e-min e e-max)
1655 (if (plusp e)
1656 (progn
1657 (write-string string stream :end (min (length string) e))
1658 (dotimes (i (- e (length string)))
1659 (write-char #\0 stream))
1660 (write-char #\. stream)
1661 (write-string string stream :start (min (length string) e))
1662 (when (<= (length string) e)
1663 (write-char #\0 stream))
1664 (print-float-exponent x 0 stream))
1665 (progn
1666 (write-string "0." stream)
1667 (dotimes (i (- e))
1668 (write-char #\0 stream))
1669 (write-string string stream)
1670 (print-float-exponent x 0 stream))))
1671 (t (write-string string stream :end 1)
1672 (write-char #\. stream)
1673 (write-string string stream :start 1)
1674 (print-float-exponent x (1- e) stream)))))
1676 ;;;; other leaf objects
1678 ;;; If *PRINT-ESCAPE* is false, just do a WRITE-CHAR, otherwise output
1679 ;;; the character name or the character in the #\char format.
1680 (defun output-character (char stream)
1681 (if (or *print-escape* *print-readably*)
1682 (let ((graphicp (and (graphic-char-p char)
1683 (standard-char-p char)))
1684 (name (char-name char)))
1685 (write-string "#\\" stream)
1686 (if (and name (or (not graphicp) *print-readably*))
1687 (quote-string name stream)
1688 (write-char char stream)))
1689 (write-char char stream)))
1691 (defun output-sap (sap stream)
1692 (declare (type system-area-pointer sap))
1693 (cond (*read-eval*
1694 (format stream "#.(~S #X~8,'0X)" 'int-sap (sap-int sap)))
1696 (print-unreadable-object (sap stream)
1697 (format stream "system area pointer: #X~8,'0X" (sap-int sap))))))
1699 (defun output-weak-pointer (weak-pointer stream)
1700 (declare (type weak-pointer weak-pointer))
1701 (print-unreadable-object (weak-pointer stream)
1702 (multiple-value-bind (value validp) (weak-pointer-value weak-pointer)
1703 (cond (validp
1704 (write-string "weak pointer: " stream)
1705 (write value :stream stream))
1707 (write-string "broken weak pointer" stream))))))
1709 (defun output-code-component (component stream)
1710 (print-unreadable-object (component stream :identity t)
1711 (let ((dinfo (%code-debug-info component)))
1712 (cond ((eq dinfo :bogus-lra)
1713 (write-string "bogus code object" stream))
1715 (write-string "code object" stream)
1716 (when dinfo
1717 (write-char #\space stream)
1718 (output-object (sb!c::debug-info-name dinfo) stream)))))))
1720 (defun output-lra (lra stream)
1721 (print-unreadable-object (lra stream :identity t)
1722 (write-string "return PC object" stream)))
1724 (defun output-fdefn (fdefn stream)
1725 (print-unreadable-object (fdefn stream)
1726 (write-string "FDEFINITION for " stream)
1727 ;; It's somewhat unhelpful to print as <FDEFINITION for (SETF #)>
1728 ;; Generalized function names are indivisible.
1729 (let ((name (fdefn-name fdefn)))
1730 (if (atom name)
1731 (output-object name stream)
1732 ;; This needn't protect against improper lists.
1733 ;; (You'd get crashes in INTERNAL-NAME-P and other places)
1734 (format stream "(~{~S~^ ~})" name)))))
1736 #!+sb-simd-pack
1737 (defun output-simd-pack (pack stream)
1738 (declare (type simd-pack pack))
1739 (cond ((and *print-readably* *read-eval*)
1740 (etypecase pack
1741 ((simd-pack double-float)
1742 (multiple-value-call #'format stream
1743 "#.(~S ~S ~S)"
1744 '%make-simd-pack-double
1745 (%simd-pack-doubles pack)))
1746 ((simd-pack single-float)
1747 (multiple-value-call #'format stream
1748 "#.(~S ~S ~S ~S ~S)"
1749 '%make-simd-pack-single
1750 (%simd-pack-singles pack)))
1752 (multiple-value-call #'format stream
1753 "#.(~S #X~16,'0X #X~16,'0X)"
1754 '%make-simd-pack-ub64
1755 (%simd-pack-ub64s pack)))))
1757 (print-unreadable-object (pack stream)
1758 (flet ((all-ones-p (value start end &aux (mask (- (ash 1 end) (ash 1 start))))
1759 (= (logand value mask) mask))
1760 (split-num (value start)
1761 (loop
1762 for i from 0 to 3
1763 and v = (ash value (- start)) then (ash v -8)
1764 collect (logand v #xFF))))
1765 (multiple-value-bind (low high)
1766 (%simd-pack-ub64s pack)
1767 (etypecase pack
1768 ((simd-pack double-float)
1769 (multiple-value-bind (v0 v1) (%simd-pack-doubles pack)
1770 (format stream "~S~@{ ~:[~,13E~;~*TRUE~]~}"
1771 'simd-pack
1772 (all-ones-p low 0 64) v0
1773 (all-ones-p high 0 64) v1)))
1774 ((simd-pack single-float)
1775 (multiple-value-bind (v0 v1 v2 v3) (%simd-pack-singles pack)
1776 (format stream "~S~@{ ~:[~,7E~;~*TRUE~]~}"
1777 'simd-pack
1778 (all-ones-p low 0 32) v0
1779 (all-ones-p low 32 64) v1
1780 (all-ones-p high 0 32) v2
1781 (all-ones-p high 32 64) v3)))
1783 (format stream "~S~@{ ~{ ~2,'0X~}~}"
1784 'simd-pack
1785 (split-num low 0) (split-num low 32)
1786 (split-num high 0) (split-num high 32))))))))))
1788 ;;;; functions
1790 ;;; Output OBJECT as using PRINT-OBJECT if it's a
1791 ;;; FUNCALLABLE-STANDARD-CLASS, or return NIL otherwise.
1793 ;;; The definition here is a simple temporary placeholder. It will be
1794 ;;; overwritten by a smarter version (capable of calling generic
1795 ;;; PRINT-OBJECT when appropriate) when CLOS is installed.
1796 (defun printed-as-funcallable-standard-class (object stream)
1797 (declare (ignore object stream))
1798 nil)
1800 (defun output-fun (object stream)
1801 (let* ((name (%fun-name object))
1802 (proper-name-p (and (legal-fun-name-p name) (fboundp name)
1803 (eq (fdefinition name) object))))
1804 (print-unreadable-object (object stream :identity (not proper-name-p))
1805 (format stream "~:[FUNCTION~;CLOSURE~]~@[ ~S~]"
1806 (closurep object)
1807 name))))
1809 ;;;; catch-all for unknown things
1811 (defun output-random (object stream)
1812 (print-unreadable-object (object stream :identity t)
1813 (let ((lowtag (lowtag-of object)))
1814 (case lowtag
1815 (#.sb!vm:other-pointer-lowtag
1816 (let ((widetag (widetag-of object)))
1817 (case widetag
1818 (#.sb!vm:value-cell-header-widetag
1819 (write-string "value cell " stream)
1820 (output-object (value-cell-ref object) stream))
1822 (write-string "unknown pointer object, widetag=" stream)
1823 (let ((*print-base* 16) (*print-radix* t))
1824 (output-integer widetag stream))))))
1825 ((#.sb!vm:fun-pointer-lowtag
1826 #.sb!vm:instance-pointer-lowtag
1827 #.sb!vm:list-pointer-lowtag)
1828 (write-string "unknown pointer object, lowtag=" stream)
1829 (let ((*print-base* 16) (*print-radix* t))
1830 (output-integer lowtag stream)))
1832 (case (widetag-of object)
1833 (#.sb!vm:unbound-marker-widetag
1834 (write-string "unbound marker" stream))
1836 (write-string "unknown immediate object, lowtag=" stream)
1837 (let ((*print-base* 2) (*print-radix* t))
1838 (output-integer lowtag stream))
1839 (write-string ", widetag=" stream)
1840 (let ((*print-base* 16) (*print-radix* t))
1841 (output-integer (widetag-of object) stream)))))))))