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 ;;; This checks to see whether the array is simple and the start and
15 ;;; end are in bounds. If so, it proceeds with those values.
16 ;;; Otherwise, it calls %WITH-ARRAY-DATA. Note that %WITH-ARRAY-DATA
17 ;;; may be further optimized.
19 ;;; Given any ARRAY, bind DATA-VAR to the array's data vector and
20 ;;; START-VAR and END-VAR to the start and end of the designated
21 ;;; portion of the data vector. SVALUE and EVALUE are any start and
22 ;;; end specified to the original operation, and are factored into the
23 ;;; bindings of START-VAR and END-VAR. OFFSET-VAR is the cumulative
24 ;;; offset of all displacements encountered, and does not include
26 (defmacro with-array-data (((data-var array &key (offset-var (gensym)))
27 (start-var &optional (svalue 0))
28 (end-var &optional (evalue nil)))
30 (once-only ((n-array array)
31 (n-svalue `(the index ,svalue))
32 (n-evalue `(the (or index null) ,evalue)))
33 `(multiple-value-bind (,data-var ,start-var ,end-var ,offset-var)
34 (if (not (array-header-p ,n-array))
35 (let ((,n-array ,n-array))
36 (declare (type (simple-array * (*)) ,n-array))
37 ,(once-only ((n-len `(length ,n-array))
38 (n-end `(or ,n-evalue ,n-len)))
39 `(if (<= ,n-svalue ,n-end ,n-len)
40 (values ,n-array ,n-svalue ,n-end 0)
41 (%with-array-data ,n-array ,n-svalue ,n-evalue))))
42 (%with-array-data ,n-array ,n-svalue ,n-evalue))
43 (declare (ignorable ,offset-var))
47 (defmacro without-gcing (&rest body)
49 "Executes the forms in the body without doing a garbage collection."
51 (let ((*gc-inhibit* t))
53 (when (and *need-to-collect-garbage* (not *gc-inhibit*))
57 (defmacro without-gcing (&rest body)
59 "Executes the forms in the body without doing a garbage collection."
60 `(without-interrupts ,@body))
62 ;;; Eof-Or-Lose is a useful macro that handles EOF.
63 (defmacro eof-or-lose (stream eof-error-p eof-value)
65 (error 'end-of-file :stream ,stream)
68 ;;; These macros handle the special cases of t and nil for input and
71 ;;; FIXME: Shouldn't these be functions instead of macros?
72 (defmacro in-synonym-of (stream &optional check-type)
73 (let ((svar (gensym)))
74 `(let ((,svar ,stream))
75 (cond ((null ,svar) *standard-input*)
76 ((eq ,svar t) *terminal-io*)
77 (T ,@(if check-type `((check-type ,svar ,check-type)))
79 (unless (input-stream-p ,svar)
80 (error 'simple-type-error
82 :expected-type '(satisfies input-stream-p)
83 :format-control "~S isn't an input stream"
84 :format-arguments ,(list svar)))
86 (defmacro out-synonym-of (stream &optional check-type)
87 (let ((svar (gensym)))
88 `(let ((,svar ,stream))
89 (cond ((null ,svar) *standard-output*)
90 ((eq ,svar t) *terminal-io*)
91 (T ,@(if check-type `((check-type ,svar ,check-type)))
93 (unless (output-stream-p ,svar)
94 (error 'simple-type-error
96 :expected-type '(satisfies output-stream-p)
97 :format-control "~S isn't an output stream."
98 :format-arguments ,(list svar)))
101 ;;; With-Mumble-Stream calls the function in the given Slot of the
102 ;;; Stream with the Args for lisp-streams, or the Function with the
103 ;;; Args for fundamental-streams.
104 (defmacro with-in-stream (stream (slot &rest args) &optional stream-dispatch)
105 `(let ((stream (in-synonym-of ,stream)))
107 `(if (lisp-stream-p stream)
108 (funcall (,slot stream) stream ,@args)
109 ,@(when stream-dispatch
110 `(,(destructuring-bind (function &rest args) stream-dispatch
111 `(,function stream ,@args)))))
112 `(funcall (,slot stream) stream ,@args))))
114 (defmacro with-out-stream (stream (slot &rest args) &optional stream-dispatch)
115 `(let ((stream (out-synonym-of ,stream)))
117 `(if (lisp-stream-p stream)
118 (funcall (,slot stream) stream ,@args)
119 ,@(when stream-dispatch
120 `(,(destructuring-bind (function &rest args) stream-dispatch
121 `(,function stream ,@args)))))
122 `(funcall (,slot stream) stream ,@args))))
124 ;;;; These are hacks to make the reader win.
126 ;;; This macro sets up some local vars for use by the
127 ;;; Fast-Read-Char macro within the enclosed lexical scope. The stream
128 ;;; is assumed to be a lisp-stream.
129 (defmacro prepare-for-fast-read-char (stream &body forms)
130 `(let* ((%frc-stream% ,stream)
131 (%frc-method% (lisp-stream-in %frc-stream%))
132 (%frc-buffer% (lisp-stream-in-buffer %frc-stream%))
133 (%frc-index% (lisp-stream-in-index %frc-stream%)))
134 (declare (type index %frc-index%)
135 (type lisp-stream %frc-stream%))
138 ;;; This macro must be called after one is done with fast-read-char
139 ;;; inside its scope to decache the lisp-stream-in-index.
140 (defmacro done-with-fast-read-char ()
141 `(setf (lisp-stream-in-index %frc-stream%) %frc-index%))
143 ;;; a macro with the same calling convention as READ-CHAR, to be
144 ;;; used within the scope of a PREPARE-FOR-FAST-READ-CHAR
145 (defmacro fast-read-char (&optional (eof-error-p t) (eof-value ()))
148 (funcall %frc-method% %frc-stream% ,eof-error-p ,eof-value))
149 ((= %frc-index% in-buffer-length)
150 (prog1 (fast-read-char-refill %frc-stream% ,eof-error-p ,eof-value)
151 (setq %frc-index% (lisp-stream-in-index %frc-stream%))))
153 (prog1 (code-char (aref %frc-buffer% %frc-index%))
154 (incf %frc-index%)))))
156 ;;;; And these for the fasloader...
158 ;;; Just like Prepare-For-Fast-Read-Char except that we get the Bin
159 ;;; method. The stream is assumed to be a lisp-stream.
161 ;;; KLUDGE: It seems weird to have to remember to explicitly call
162 ;;; DONE-WITH-FAST-READ-BYTE at the end of this, given that we're
163 ;;; already wrapping the stuff inside in a block. Why not rename this
164 ;;; macro to WITH-FAST-READ-BYTE, do the DONE-WITH-FAST-READ-BYTE stuff
165 ;;; automatically at the end of the block, and eliminate
166 ;;; DONE-WITH-FAST-READ-BYTE as a separate entity? (and similarly
167 ;;; for the FAST-READ-CHAR stuff) -- WHN 19990825
168 (defmacro prepare-for-fast-read-byte (stream &body forms)
169 `(let* ((%frc-stream% ,stream)
170 (%frc-method% (lisp-stream-bin %frc-stream%))
171 (%frc-buffer% (lisp-stream-in-buffer %frc-stream%))
172 (%frc-index% (lisp-stream-in-index %frc-stream%)))
173 (declare (type index %frc-index%)
174 (type lisp-stream %frc-stream%))
177 ;;; Similar to fast-read-char, but we use a different refill routine & don't
178 ;;; convert to characters. If ANY-TYPE is true, then this can be used on any
179 ;;; integer streams, and we don't assert the result type.
180 (defmacro fast-read-byte (&optional (eof-error-p t) (eof-value ()) any-type)
181 ;; KLUDGE: should use ONCE-ONLY on EOF-ERROR-P and EOF-VALUE -- WHN 19990825
183 ,(if (and (eq eof-error-p 't) (not any-type)) '(unsigned-byte 8) 't)
186 (funcall %frc-method% %frc-stream% ,eof-error-p ,eof-value))
187 ((= %frc-index% in-buffer-length)
188 (prog1 (fast-read-byte-refill %frc-stream% ,eof-error-p ,eof-value)
189 (setq %frc-index% (lisp-stream-in-index %frc-stream%))))
191 (prog1 (aref %frc-buffer% %frc-index%)
192 (incf %frc-index%))))))
193 (defmacro done-with-fast-read-byte ()
194 `(done-with-fast-read-char))