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 ;;; FIXME Not the most sensible way to do this: we could just use
15 ;;; LOCK ADD, given that we don't need the old version. This will
16 ;;; do until we get around to writing new VOPs
17 ;;; FIXME in fact we're not SMP-safe without LOCK anyway, but
18 ;;; this will do us for UP systems
20 (defmacro atomic-incf/symbol (symbol-name &optional (delta 1))
22 `(incf ,symbol-name ,delta)
25 (declare (optimize (safety 0) (speed 3)))
26 (sb!vm::fast-symbol-global-value-xadd ',symbol-name ,delta)
29 (defmacro without-gcing (&rest body)
31 "Executes the forms in the body without doing a garbage collection."
34 (atomic-incf/symbol *gc-inhibit*)
36 (atomic-incf/symbol *gc-inhibit* -1)
37 (when (and *need-to-collect-garbage* (zerop *gc-inhibit*))
41 ;;; EOF-OR-LOSE is a useful macro that handles EOF.
42 (defmacro eof-or-lose (stream eof-error-p eof-value)
44 (error 'end-of-file :stream ,stream)
47 ;;; These macros handle the special cases of T and NIL for input and
50 ;;; FIXME: Shouldn't these be functions instead of macros?
51 (defmacro in-synonym-of (stream &optional check-type)
52 (let ((svar (gensym)))
53 `(let ((,svar ,stream))
54 (cond ((null ,svar) *standard-input*)
55 ((eq ,svar t) *terminal-io*)
56 (T ,@(when check-type `((enforce-type ,svar ,check-type)))
58 (unless (input-stream-p ,svar)
59 (error 'simple-type-error
61 :expected-type '(satisfies input-stream-p)
62 :format-control "~S isn't an input stream"
63 :format-arguments ,(list svar)))
65 (defmacro out-synonym-of (stream &optional check-type)
66 (let ((svar (gensym)))
67 `(let ((,svar ,stream))
68 (cond ((null ,svar) *standard-output*)
69 ((eq ,svar t) *terminal-io*)
70 (T ,@(when check-type `((check-type ,svar ,check-type)))
72 (unless (output-stream-p ,svar)
73 (error 'simple-type-error
75 :expected-type '(satisfies output-stream-p)
76 :format-control "~S isn't an output stream."
77 :format-arguments ,(list svar)))
80 ;;; WITH-mumble-STREAM calls the function in the given SLOT of the
81 ;;; STREAM with the ARGS for ANSI-STREAMs, or the FUNCTION with the
82 ;;; ARGS for FUNDAMENTAL-STREAMs.
83 (defmacro with-in-stream (stream (slot &rest args) &optional stream-dispatch)
84 `(let ((stream (in-synonym-of ,stream)))
86 `(if (ansi-stream-p stream)
87 (funcall (,slot stream) stream ,@args)
88 ,@(when stream-dispatch
89 `(,(destructuring-bind (function &rest args) stream-dispatch
90 `(,function stream ,@args)))))
91 `(funcall (,slot stream) stream ,@args))))
93 (defmacro with-out-stream (stream (slot &rest args) &optional stream-dispatch)
94 `(let ((stream (out-synonym-of ,stream)))
96 `(if (ansi-stream-p stream)
97 (funcall (,slot stream) stream ,@args)
98 ,@(when stream-dispatch
99 `(,(destructuring-bind (function &rest args) stream-dispatch
100 `(,function stream ,@args)))))
101 `(funcall (,slot stream) stream ,@args))))
103 ;;;; These are hacks to make the reader win.
105 ;;; This macro sets up some local vars for use by the
106 ;;; FAST-READ-CHAR macro within the enclosed lexical scope. The stream
107 ;;; is assumed to be a ANSI-STREAM.
108 (defmacro prepare-for-fast-read-char (stream &body forms)
109 `(let* ((%frc-stream% ,stream)
110 (%frc-method% (ansi-stream-in %frc-stream%))
111 (%frc-buffer% (ansi-stream-in-buffer %frc-stream%))
112 (%frc-index% (ansi-stream-in-index %frc-stream%)))
113 (declare (type index %frc-index%)
114 (type ansi-stream %frc-stream%))
117 ;;; This macro must be called after one is done with FAST-READ-CHAR
118 ;;; inside its scope to decache the ANSI-STREAM-IN-INDEX.
119 (defmacro done-with-fast-read-char ()
120 `(setf (ansi-stream-in-index %frc-stream%) %frc-index%))
122 ;;; a macro with the same calling convention as READ-CHAR, to be used
123 ;;; within the scope of a PREPARE-FOR-FAST-READ-CHAR
124 (defmacro fast-read-char (&optional (eof-error-p t) (eof-value ()))
127 (funcall %frc-method% %frc-stream% ,eof-error-p ,eof-value))
128 ((= %frc-index% +ansi-stream-in-buffer-length+)
129 (prog1 (fast-read-char-refill %frc-stream% ,eof-error-p ,eof-value)
130 (setq %frc-index% (ansi-stream-in-index %frc-stream%))))
132 (prog1 (code-char (aref %frc-buffer% %frc-index%))
133 (incf %frc-index%)))))
135 ;;;; And these for the fasloader...
137 ;;; Just like PREPARE-FOR-FAST-READ-CHAR except that we get the BIN
138 ;;; method. The stream is assumed to be a ANSI-STREAM.
140 ;;; KLUDGE: It seems weird to have to remember to explicitly call
141 ;;; DONE-WITH-FAST-READ-BYTE at the end of this, given that we're
142 ;;; already wrapping the stuff inside in a block. Why not rename this
143 ;;; macro to WITH-FAST-READ-BYTE, do the DONE-WITH-FAST-READ-BYTE stuff
144 ;;; automatically at the end of the block, and eliminate
145 ;;; DONE-WITH-FAST-READ-BYTE as a separate entity? (and similarly
146 ;;; for the FAST-READ-CHAR stuff) -- WHN 19990825
147 (defmacro prepare-for-fast-read-byte (stream &body forms)
148 `(let* ((%frc-stream% ,stream)
149 (%frc-method% (ansi-stream-bin %frc-stream%))
150 (%frc-buffer% (ansi-stream-in-buffer %frc-stream%))
151 (%frc-index% (ansi-stream-in-index %frc-stream%)))
152 (declare (type index %frc-index%)
153 (type ansi-stream %frc-stream%))
156 ;;; Similar to fast-read-char, but we use a different refill routine & don't
157 ;;; convert to characters. If ANY-TYPE is true, then this can be used on any
158 ;;; integer streams, and we don't assert the result type.
159 (defmacro fast-read-byte (&optional (eof-error-p t) (eof-value ()) any-type)
160 ;; KLUDGE: should use ONCE-ONLY on EOF-ERROR-P and EOF-VALUE -- WHN 19990825
162 ,(if (and (eq eof-error-p t) (not any-type)) '(unsigned-byte 8) t)
165 (funcall %frc-method% %frc-stream% ,eof-error-p ,eof-value))
166 ((= %frc-index% +ansi-stream-in-buffer-length+)
167 (prog1 (fast-read-byte-refill %frc-stream% ,eof-error-p ,eof-value)
168 (setq %frc-index% (ansi-stream-in-index %frc-stream%))))
170 (prog1 (aref %frc-buffer% %frc-index%)
171 (incf %frc-index%))))))
172 (defmacro done-with-fast-read-byte ()
173 `(done-with-fast-read-char))