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")
15 (defmacro without-gcing (&rest body)
17 "Executes the forms in the body without doing a garbage collection."
19 (let ((*gc-inhibit* t))
21 (when (and *need-to-collect-garbage* (not *gc-inhibit*))
25 (defmacro without-gcing (&rest body)
27 "Executes the forms in the body without doing a garbage collection."
28 `(without-interrupts ,@body))
30 ;;; EOF-OR-LOSE is a useful macro that handles EOF.
31 (defmacro eof-or-lose (stream eof-error-p eof-value)
33 (error 'end-of-file :stream ,stream)
36 ;;; These macros handle the special cases of T and NIL for input and
39 ;;; FIXME: Shouldn't these be functions instead of macros?
40 (defmacro in-synonym-of (stream &optional check-type)
41 (let ((svar (gensym)))
42 `(let ((,svar ,stream))
43 (cond ((null ,svar) *standard-input*)
44 ((eq ,svar t) *terminal-io*)
45 (T ,@(when check-type `((enforce-type ,svar ,check-type)))
47 (unless (input-stream-p ,svar)
48 (error 'simple-type-error
50 :expected-type '(satisfies input-stream-p)
51 :format-control "~S isn't an input stream"
52 :format-arguments ,(list svar)))
54 (defmacro out-synonym-of (stream &optional check-type)
55 (let ((svar (gensym)))
56 `(let ((,svar ,stream))
57 (cond ((null ,svar) *standard-output*)
58 ((eq ,svar t) *terminal-io*)
59 (T ,@(when check-type `((check-type ,svar ,check-type)))
61 (unless (output-stream-p ,svar)
62 (error 'simple-type-error
64 :expected-type '(satisfies output-stream-p)
65 :format-control "~S isn't an output stream."
66 :format-arguments ,(list svar)))
69 ;;; WITH-mumble-STREAM calls the function in the given SLOT of the
70 ;;; STREAM with the ARGS for LISP-STREAMs, or the FUNCTION with the
71 ;;; ARGS for FUNDAMENTAL-STREAMs.
72 (defmacro with-in-stream (stream (slot &rest args) &optional stream-dispatch)
73 `(let ((stream (in-synonym-of ,stream)))
75 `(if (lisp-stream-p stream)
76 (funcall (,slot stream) stream ,@args)
77 ,@(when stream-dispatch
78 `(,(destructuring-bind (function &rest args) stream-dispatch
79 `(,function stream ,@args)))))
80 `(funcall (,slot stream) stream ,@args))))
82 (defmacro with-out-stream (stream (slot &rest args) &optional stream-dispatch)
83 `(let ((stream (out-synonym-of ,stream)))
85 `(if (lisp-stream-p stream)
86 (funcall (,slot stream) stream ,@args)
87 ,@(when stream-dispatch
88 `(,(destructuring-bind (function &rest args) stream-dispatch
89 `(,function stream ,@args)))))
90 `(funcall (,slot stream) stream ,@args))))
92 ;;;; These are hacks to make the reader win.
94 ;;; This macro sets up some local vars for use by the
95 ;;; FAST-READ-CHAR macro within the enclosed lexical scope. The stream
96 ;;; is assumed to be a LISP-STREAM.
97 (defmacro prepare-for-fast-read-char (stream &body forms)
98 `(let* ((%frc-stream% ,stream)
99 (%frc-method% (lisp-stream-in %frc-stream%))
100 (%frc-buffer% (lisp-stream-in-buffer %frc-stream%))
101 (%frc-index% (lisp-stream-in-index %frc-stream%)))
102 (declare (type index %frc-index%)
103 (type lisp-stream %frc-stream%))
106 ;;; This macro must be called after one is done with FAST-READ-CHAR
107 ;;; inside its scope to decache the lisp-stream-in-index.
108 (defmacro done-with-fast-read-char ()
109 `(setf (lisp-stream-in-index %frc-stream%) %frc-index%))
111 ;;; a macro with the same calling convention as READ-CHAR, to be used
112 ;;; within the scope of a PREPARE-FOR-FAST-READ-CHAR
113 (defmacro fast-read-char (&optional (eof-error-p t) (eof-value ()))
116 (funcall %frc-method% %frc-stream% ,eof-error-p ,eof-value))
117 ((= %frc-index% +in-buffer-length+)
118 (prog1 (fast-read-char-refill %frc-stream% ,eof-error-p ,eof-value)
119 (setq %frc-index% (lisp-stream-in-index %frc-stream%))))
121 (prog1 (code-char (aref %frc-buffer% %frc-index%))
122 (incf %frc-index%)))))
124 ;;;; And these for the fasloader...
126 ;;; Just like PREPARE-FOR-FAST-READ-CHAR except that we get the BIN
127 ;;; method. The stream is assumed to be a LISP-STREAM.
129 ;;; KLUDGE: It seems weird to have to remember to explicitly call
130 ;;; DONE-WITH-FAST-READ-BYTE at the end of this, given that we're
131 ;;; already wrapping the stuff inside in a block. Why not rename this
132 ;;; macro to WITH-FAST-READ-BYTE, do the DONE-WITH-FAST-READ-BYTE stuff
133 ;;; automatically at the end of the block, and eliminate
134 ;;; DONE-WITH-FAST-READ-BYTE as a separate entity? (and similarly
135 ;;; for the FAST-READ-CHAR stuff) -- WHN 19990825
136 (defmacro prepare-for-fast-read-byte (stream &body forms)
137 `(let* ((%frc-stream% ,stream)
138 (%frc-method% (lisp-stream-bin %frc-stream%))
139 (%frc-buffer% (lisp-stream-in-buffer %frc-stream%))
140 (%frc-index% (lisp-stream-in-index %frc-stream%)))
141 (declare (type index %frc-index%)
142 (type lisp-stream %frc-stream%))
145 ;;; Similar to fast-read-char, but we use a different refill routine & don't
146 ;;; convert to characters. If ANY-TYPE is true, then this can be used on any
147 ;;; integer streams, and we don't assert the result type.
148 (defmacro fast-read-byte (&optional (eof-error-p t) (eof-value ()) any-type)
149 ;; KLUDGE: should use ONCE-ONLY on EOF-ERROR-P and EOF-VALUE -- WHN 19990825
151 ,(if (and (eq eof-error-p t) (not any-type)) '(unsigned-byte 8) t)
154 (funcall %frc-method% %frc-stream% ,eof-error-p ,eof-value))
155 ((= %frc-index% +in-buffer-length+)
156 (prog1 (fast-read-byte-refill %frc-stream% ,eof-error-p ,eof-value)
157 (setq %frc-index% (lisp-stream-in-index %frc-stream%))))
159 (prog1 (aref %frc-buffer% %frc-index%)
160 (incf %frc-index%))))))
161 (defmacro done-with-fast-read-byte ()
162 `(done-with-fast-read-char))