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