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