1 ;;;; miscellaneous system hacking macros
3 ;;;; This software is part of the SBCL system. See the README file for
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 (defmacro without-gcing (&rest body)
16 "Executes the forms in the body without doing a garbage collection."
18 (let ((*gc-inhibit* t))
20 (when (and *need-to-collect-garbage* (not *gc-inhibit*))
23 ;;; EOF-OR-LOSE is a useful macro that handles EOF.
24 (defmacro eof-or-lose (stream eof-error-p eof-value)
26 (error 'end-of-file :stream ,stream)
29 ;;; These macros handle the special cases of T and NIL for input and
32 ;;; FIXME: Shouldn't these be functions instead of macros?
33 (defmacro in-synonym-of (stream &optional check-type)
34 (let ((svar (gensym)))
35 `(let ((,svar ,stream))
36 (cond ((null ,svar) *standard-input*)
37 ((eq ,svar t) *terminal-io*)
38 (T ,@(when check-type `((enforce-type ,svar ,check-type)))
40 (unless (input-stream-p ,svar)
41 (error 'simple-type-error
43 :expected-type '(satisfies input-stream-p)
44 :format-control "~S isn't an input stream"
45 :format-arguments ,(list svar)))
47 (defmacro out-synonym-of (stream &optional check-type)
48 (let ((svar (gensym)))
49 `(let ((,svar ,stream))
50 (cond ((null ,svar) *standard-output*)
51 ((eq ,svar t) *terminal-io*)
52 (T ,@(when check-type `((check-type ,svar ,check-type)))
54 (unless (output-stream-p ,svar)
55 (error 'simple-type-error
57 :expected-type '(satisfies output-stream-p)
58 :format-control "~S isn't an output stream."
59 :format-arguments ,(list svar)))
62 ;;; WITH-mumble-STREAM calls the function in the given SLOT of the
63 ;;; STREAM with the ARGS for ANSI-STREAMs, or the FUNCTION with the
64 ;;; ARGS for FUNDAMENTAL-STREAMs.
65 (defmacro with-in-stream (stream (slot &rest args) &optional stream-dispatch)
66 `(let ((stream (in-synonym-of ,stream)))
68 `(if (ansi-stream-p stream)
69 (funcall (,slot stream) stream ,@args)
70 ,@(when stream-dispatch
71 `(,(destructuring-bind (function &rest args) stream-dispatch
72 `(,function stream ,@args)))))
73 `(funcall (,slot stream) stream ,@args))))
75 (defmacro with-out-stream (stream (slot &rest args) &optional stream-dispatch)
76 `(let ((stream (out-synonym-of ,stream)))
78 `(if (ansi-stream-p stream)
79 (funcall (,slot stream) stream ,@args)
80 ,@(when stream-dispatch
81 `(,(destructuring-bind (function &rest args) stream-dispatch
82 `(,function stream ,@args)))))
83 `(funcall (,slot stream) stream ,@args))))
85 ;;;; These are hacks to make the reader win.
87 ;;; This macro sets up some local vars for use by the
88 ;;; FAST-READ-CHAR macro within the enclosed lexical scope. The stream
89 ;;; is assumed to be a ANSI-STREAM.
90 (defmacro prepare-for-fast-read-char (stream &body forms)
91 `(let* ((%frc-stream% ,stream)
92 (%frc-method% (ansi-stream-in %frc-stream%))
93 (%frc-buffer% (ansi-stream-in-buffer %frc-stream%))
94 (%frc-index% (ansi-stream-in-index %frc-stream%)))
95 (declare (type index %frc-index%)
96 (type ansi-stream %frc-stream%))
99 ;;; This macro must be called after one is done with FAST-READ-CHAR
100 ;;; inside its scope to decache the ANSI-STREAM-IN-INDEX.
101 (defmacro done-with-fast-read-char ()
102 `(setf (ansi-stream-in-index %frc-stream%) %frc-index%))
104 ;;; a macro with the same calling convention as READ-CHAR, to be used
105 ;;; within the scope of a PREPARE-FOR-FAST-READ-CHAR
106 (defmacro fast-read-char (&optional (eof-error-p t) (eof-value ()))
109 (funcall %frc-method% %frc-stream% ,eof-error-p ,eof-value))
110 ((= %frc-index% +ansi-stream-in-buffer-length+)
111 (prog1 (fast-read-char-refill %frc-stream% ,eof-error-p ,eof-value)
112 (setq %frc-index% (ansi-stream-in-index %frc-stream%))))
114 (prog1 (code-char (aref %frc-buffer% %frc-index%))
115 (incf %frc-index%)))))
117 ;;;; And these for the fasloader...
119 ;;; Just like PREPARE-FOR-FAST-READ-CHAR except that we get the BIN
120 ;;; method. The stream is assumed to be a ANSI-STREAM.
122 ;;; KLUDGE: It seems weird to have to remember to explicitly call
123 ;;; DONE-WITH-FAST-READ-BYTE at the end of this, given that we're
124 ;;; already wrapping the stuff inside in a block. Why not rename this
125 ;;; macro to WITH-FAST-READ-BYTE, do the DONE-WITH-FAST-READ-BYTE stuff
126 ;;; automatically at the end of the block, and eliminate
127 ;;; DONE-WITH-FAST-READ-BYTE as a separate entity? (and similarly
128 ;;; for the FAST-READ-CHAR stuff) -- WHN 19990825
129 (defmacro prepare-for-fast-read-byte (stream &body forms)
130 `(let* ((%frc-stream% ,stream)
131 (%frc-method% (ansi-stream-bin %frc-stream%))
132 (%frc-buffer% (ansi-stream-in-buffer %frc-stream%))
133 (%frc-index% (ansi-stream-in-index %frc-stream%)))
134 (declare (type index %frc-index%)
135 (type ansi-stream %frc-stream%))
138 ;;; Similar to fast-read-char, but we use a different refill routine & don't
139 ;;; convert to characters. If ANY-TYPE is true, then this can be used on any
140 ;;; integer streams, and we don't assert the result type.
141 (defmacro fast-read-byte (&optional (eof-error-p t) (eof-value ()) any-type)
142 ;; KLUDGE: should use ONCE-ONLY on EOF-ERROR-P and EOF-VALUE -- WHN 19990825
144 ,(if (and (eq eof-error-p t) (not any-type)) '(unsigned-byte 8) t)
147 (funcall %frc-method% %frc-stream% ,eof-error-p ,eof-value))
148 ((= %frc-index% +ansi-stream-in-buffer-length+)
149 (prog1 (fast-read-byte-refill %frc-stream% ,eof-error-p ,eof-value)
150 (setq %frc-index% (ansi-stream-in-index %frc-stream%))))
152 (prog1 (aref %frc-buffer% %frc-index%)
153 (incf %frc-index%))))))
154 (defmacro done-with-fast-read-byte ()
155 `(done-with-fast-read-char))