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