Fix FORMAT compilation on non-simple strings.
[sbcl.git] / src / code / sysmacs.lisp
blobbaa5a65ff83ba42c4b7a15b12c29c1d33ca1bcc3
1 ;;;; miscellaneous system hacking macros
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 ;;;; these are initialized by create_thread_struct()
16 (defvar *in-without-gcing*)
17 (defvar *gc-inhibit*)
19 ;;; When the dynamic usage increases beyond this amount, the system
20 ;;; notes that a garbage collection needs to occur by setting
21 ;;; *GC-PENDING* to T. It starts out as NIL meaning nobody has figured
22 ;;; out what it should be yet.
23 (defvar *gc-pending*)
25 #!+sb-thread
26 (defvar *stop-for-gc-pending*)
28 (eval-when (:compile-toplevel :load-toplevel :execute)
29 (dolist (symbol '(*gc-inhibit* *in-without-gcing*
30 *gc-pending* *stop-for-gc-pending*))
31 (setf (info :variable :always-bound symbol) :always-bound)))
33 ;;; This one is initialized by the runtime, at thread creation. On
34 ;;; non-x86oid gencgc targets, this is a per-thread list of objects
35 ;;; which must not be moved during GC. It is frobbed by the code for
36 ;;; with-pinned-objects in src/compiler/target/macros.lisp.
37 #!+(and gencgc (not (or x86 x86-64)))
38 (defvar sb!vm::*pinned-objects*)
40 (defmacro without-gcing (&body body)
41 "Executes the forms in the body without doing a garbage collection. It
42 inhibits both automatically and explicitly triggered collections. Finally,
43 upon leaving the BODY if gc is not inhibited it runs the pending gc.
44 Similarly, if gc is triggered in another thread then it waits until gc is
45 enabled in this thread.
47 Implies SB-SYS:WITHOUT-INTERRUPTS for BODY, and causes any nested
48 SB-SYS:WITH-INTERRUPTS to signal a warning during execution of the BODY.
50 Should be used with great care, and not at all in multithreaded application
51 code: Any locks that are ever acquired while GC is inhibited need to be always
52 held with GC inhibited to prevent deadlocks: if T1 holds the lock and is
53 stopped for GC while T2 is waiting for the lock inside WITHOUT-GCING the
54 system will be deadlocked. Since SBCL does not currently document its internal
55 locks, application code can never be certain that this invariant is
56 maintained."
57 (with-unique-names (without-gcing-body)
58 `(dx-flet ((,without-gcing-body ()
59 ,@body))
60 (if *gc-inhibit*
61 (,without-gcing-body)
62 ;; We need to disable interrupts before disabling GC, so
63 ;; that signal handlers using locks don't accidentally try
64 ;; to grab them with GC inhibited.
65 (let ((*in-without-gcing* t))
66 (unwind-protect
67 (let* ((*allow-with-interrupts* nil)
68 (*interrupts-enabled* nil)
69 (*gc-inhibit* t))
70 (,without-gcing-body))
71 ;; This is not racy becuase maybe_defer_handler
72 ;; defers signals if *GC-INHIBIT* is NIL but there
73 ;; is a pending gc or stop-for-gc.
74 (when (or *interrupt-pending*
75 *gc-pending*
76 #!+sb-thread *stop-for-gc-pending*)
77 (sb!unix::receive-pending-interrupt))))))))
79 ;;; EOF-OR-LOSE is a useful macro that handles EOF.
80 (defmacro eof-or-lose (stream eof-error-p eof-value)
81 `(if ,eof-error-p
82 (error 'end-of-file :stream ,stream)
83 ,eof-value))
85 ;;; These macros handle the special cases of T and NIL for input and
86 ;;; output streams.
87 ;;; FIXME: should we kill the high-security feature? Or, if enabled,
88 ;;; ensure that the designated stream has the right directionality?
89 ;;; (Nothing prevents *TERMINAL-IO* from being bound to an output-only stream, e.g.)
90 ;;;
91 ;;; FIXME: Shouldn't these be functions instead of macros?
92 (defmacro in-stream-from-designator (stream)
93 (let ((svar (gensym)))
94 `(let ((,svar ,stream))
95 (cond ((null ,svar) *standard-input*)
96 ((eq ,svar t) *terminal-io*)
98 #!+high-security
99 (unless (input-stream-p ,svar)
100 (error 'simple-type-error
101 :datum ,svar
102 :expected-type '(satisfies input-stream-p)
103 :format-control "~S isn't an input stream"
104 :format-arguments (list ,svar)))
105 ,svar)))))
106 ;; As noted above, this code is a tad wasteful for probably not a huge
107 ;; performance gain. On the other hand, where STREAM is known to be of type
108 ;; STREAM, it produces shorter code. But we could shorten the general case:
110 (lambda (x)
111 (block nil
112 (symbol-value
113 (case x ((nil) '*standard-output*)
114 ((t) '*terminal-io*)
115 (t (return x))))))
117 (defmacro out-stream-from-designator (stream)
118 (let ((svar (gensym)))
119 `(let ((,svar ,stream))
120 (cond ((null ,svar) *standard-output*)
121 ((eq ,svar t) *terminal-io*)
123 #!+high-security
124 (unless (output-stream-p ,svar)
125 (error 'simple-type-error
126 :datum ,svar
127 :expected-type '(satisfies output-stream-p)
128 :format-control "~S isn't an output stream."
129 :format-arguments (list ,svar)))
130 ,svar)))))
132 ;;; WITH-mumble-STREAM calls the function in the given SLOT of the
133 ;;; STREAM with the ARGS for ANSI-STREAMs, or the FUNCTION with the
134 ;;; ARGS for FUNDAMENTAL-STREAMs.
135 (defmacro with-in-stream (stream (slot &rest args) &optional stream-dispatch)
136 `(let ((stream (in-stream-from-designator ,stream)))
137 ,(if stream-dispatch
138 `(if (ansi-stream-p stream)
139 (funcall (,slot stream) stream ,@args)
140 ,@(when stream-dispatch
141 `(,(destructuring-bind (function &rest args) stream-dispatch
142 `(,function stream ,@args)))))
143 `(funcall (,slot stream) stream ,@args))))
145 (defmacro %with-out-stream (stream (slot &rest args) &optional stream-dispatch)
146 `(let ((stream ,stream))
147 ,(if stream-dispatch
148 `(if (ansi-stream-p stream)
149 (funcall (,slot stream) stream ,@args)
150 ,@(when stream-dispatch
151 `(,(destructuring-bind (function &rest args) stream-dispatch
152 `(,function stream ,@args)))))
153 `(funcall (,slot stream) stream ,@args))))
155 (defmacro with-out-stream (stream (slot &rest args) &optional stream-dispatch)
156 `(%with-out-stream (out-stream-from-designator ,stream)
157 (,slot ,@args)
158 ,stream-dispatch))
161 ;;;; These are hacks to make the reader win.
163 ;;; This macro sets up some local vars for use by the
164 ;;; FAST-READ-CHAR macro within the enclosed lexical scope. The stream
165 ;;; is assumed to be a ANSI-STREAM.
167 ;;; KLUDGE: Some functions (e.g. ANSI-STREAM-READ-LINE) use these variables
168 ;;; directly, instead of indirecting through FAST-READ-CHAR.
169 ;;; When ANSI-STREAM-INPUT-CHAR-POS is non-null, we take care to update it,
170 ;;; but not for each character of input.
171 (defmacro prepare-for-fast-read-char (stream &body forms)
172 `(let* ((%frc-stream% ,stream)
173 (%frc-method% (ansi-stream-in %frc-stream%))
174 (%frc-buffer% (ansi-stream-cin-buffer %frc-stream%))
175 (%frc-index% (ansi-stream-in-index %frc-stream%)))
176 (declare (type (mod ,(1+ +ansi-stream-in-buffer-length+)) %frc-index%)
177 (type ansi-stream %frc-stream%))
178 ,@forms))
180 ;;; This macro must be called after one is done with FAST-READ-CHAR
181 ;;; inside its scope to decache the ANSI-STREAM-IN-INDEX.
182 ;;; To keep the amount of code injected by FAST-READ-CHAR as small as possible,
183 ;;; we avoid bumping the absolute stream position counter at each character.
184 ;;; When finished looping, one extra function call takes care of that.
185 ;;; If buffer refills occurred within FAST-READ-CHAR, the refill logic
186 ;;; similarly scans the cin-buffer before placing anything new into it.
187 (defmacro done-with-fast-read-char ()
188 `(progn
189 (when (ansi-stream-input-char-pos %frc-stream%)
190 (update-input-char-pos %frc-stream% %frc-index%))
191 (setf (ansi-stream-in-index %frc-stream%) %frc-index%)))
193 ;;; a macro with the same calling convention as READ-CHAR, to be used
194 ;;; within the scope of a PREPARE-FOR-FAST-READ-CHAR.
195 ;;; If EOF-ERROR-P is statically T (not any random expression evaluating
196 ;;; to T) then wrap the whole thing in (TRULY-THE CHARACTER ...)
197 ;;; because it's either going to yield a character or signal EOF.
198 (defmacro fast-read-char (&optional (eof-error-p t) (eof-value ()))
199 (let ((result
200 `(if (not %frc-buffer%)
201 (funcall %frc-method% %frc-stream% ,eof-error-p ,eof-value)
202 (block nil
203 (when (= %frc-index% +ansi-stream-in-buffer-length+)
204 (let ((index-or-nil
205 (fast-read-char-refill %frc-stream% ,eof-error-p)))
206 ,@(unless (eq eof-error-p 't)
207 `((when (null index-or-nil)
208 (return ,eof-value))))
209 (setq %frc-index%
210 (truly-the (mod ,+ansi-stream-in-buffer-length+)
211 index-or-nil))))
212 (prog1 (aref %frc-buffer%
213 (truly-the (mod ,+ansi-stream-in-buffer-length+)
214 %frc-index%))
215 (incf %frc-index%))))))
216 (cond ((eq eof-error-p 't)
217 `(truly-the character ,result))
218 ((and (symbolp eof-value) (constantp eof-value)
219 ;; use an EQL specifier only if the const is EQL-comparable
220 (typep (symbol-value eof-value) '(or symbol fixnum)))
221 `(truly-the (or (eql ,(symbol-value eof-value)) character) ,result))
223 result))))
225 ;;;; And these for the fasloader...
227 ;;; Just like PREPARE-FOR-FAST-READ-CHAR except that we get the BIN
228 ;;; method. The stream is assumed to be a ANSI-STREAM.
230 ;;; FIXME: Refactor PREPARE-FOR-FAST-READ-CHAR into similar shape.
231 (defmacro with-fast-read-byte ((type stream &optional (eof-error-p t) eof-value)
232 &body body)
233 (aver (or (eq t eof-error-p) (eq t type)))
234 (with-unique-names (f-stream f-method f-buffer f-index eof-p eof-val)
235 `(let* ((,f-stream ,stream)
236 (,eof-p ,eof-error-p)
237 (,eof-val ,eof-value)
238 (,f-method (ansi-stream-bin ,f-stream))
239 (,f-buffer (ansi-stream-in-buffer ,f-stream))
240 (,f-index (ansi-stream-in-index ,f-stream)))
241 (declare (type ansi-stream ,f-stream)
242 (type index ,f-index))
243 (declare (disable-package-locks fast-read-byte))
244 (flet ((fast-read-byte ()
245 (,@(cond ((equal '(unsigned-byte 8) type)
246 ;; KLUDGE: For some reason I haven't tracked down
247 ;; this makes a difference even in given the TRULY-THE.
248 `(logand #xff))
250 `(identity)))
251 (truly-the ,type
252 (cond
253 ((not ,f-buffer)
254 (funcall ,f-method ,f-stream ,eof-p ,eof-val))
255 ((= ,f-index +ansi-stream-in-buffer-length+)
256 (prog1 (fast-read-byte-refill ,f-stream ,eof-p ,eof-val)
257 (setq ,f-index (ansi-stream-in-index ,f-stream))))
259 (prog1 (aref ,f-buffer ,f-index)
260 (incf ,f-index))))))))
261 (declare (inline fast-read-byte))
262 (declare (enable-package-locks fast-read-byte))
263 (unwind-protect
264 (locally ,@body)
265 (setf (ansi-stream-in-index ,f-stream) ,f-index))))))
267 ;; This is an internal-use-only macro.
268 (defmacro do-rest-arg (((var &optional index-var) rest-var
269 &optional (start 0) result)
270 &body body)
271 ;; If the &REST arg never needs to be reified, this is slightly quicker
272 ;; than using a DX list.
273 (let ((index (sb!xc:gensym "INDEX")))
274 `(let ((,index ,start))
275 (loop
276 (cond ((< (truly-the index ,index) (length ,rest-var))
277 (let ((,var (fast-&rest-nth ,index ,rest-var))
278 ,@(if index-var `((,index-var ,index))))
279 ,@body)
280 (incf ,index))
282 (return ,result)))))))