0.pre8.85
[sbcl.git] / src / code / sysmacs.lisp
1 ;;;; miscellaneous system hacking macros
2
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.
11
12 (in-package "SB!IMPL")
13
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
19
20 (defmacro atomic-incf/symbol (symbol-name &optional (delta 1))
21   #!-sb-thread
22   `(incf ,symbol-name ,delta)
23   #!+sb-thread
24   `(locally
25     (declare (optimize (safety 0) (speed 3)))
26     (sb!vm::locked-symbol-global-value-add ',symbol-name ,delta)))
27
28 (defmacro without-gcing (&rest body)
29   #!+sb-doc
30   "Executes the forms in the body without doing a garbage collection."
31   `(unwind-protect
32     (progn
33       (atomic-incf/symbol *gc-inhibit*)
34       ,@body)
35     (atomic-incf/symbol *gc-inhibit* -1)
36     (when (and *need-to-collect-garbage* (zerop *gc-inhibit*))
37       (sub-gc))))
38
39 \f
40 ;;; EOF-OR-LOSE is a useful macro that handles EOF.
41 (defmacro eof-or-lose (stream eof-error-p eof-value)
42   `(if ,eof-error-p
43        (error 'end-of-file :stream ,stream)
44        ,eof-value))
45
46 ;;; These macros handle the special cases of T and NIL for input and
47 ;;; output streams.
48 ;;;
49 ;;; FIXME: Shouldn't these be functions instead of macros?
50 (defmacro in-synonym-of (stream &optional check-type)
51   (let ((svar (gensym)))
52     `(let ((,svar ,stream))
53        (cond ((null ,svar) *standard-input*)
54              ((eq ,svar t) *terminal-io*)
55              (T ,@(when check-type `((enforce-type ,svar ,check-type)))
56                 #!+high-security
57                 (unless (input-stream-p ,svar)
58                   (error 'simple-type-error
59                          :datum ,svar
60                          :expected-type '(satisfies input-stream-p)
61                          :format-control "~S isn't an input stream"
62                          :format-arguments ,(list  svar)))              
63                 ,svar)))))
64 (defmacro out-synonym-of (stream &optional check-type)
65   (let ((svar (gensym)))
66     `(let ((,svar ,stream))
67        (cond ((null ,svar) *standard-output*)
68              ((eq ,svar t) *terminal-io*)
69              (T ,@(when check-type `((check-type ,svar ,check-type)))
70                 #!+high-security
71                 (unless (output-stream-p ,svar)
72                   (error 'simple-type-error
73                          :datum ,svar
74                          :expected-type '(satisfies output-stream-p)
75                          :format-control "~S isn't an output stream."
76                          :format-arguments ,(list  svar)))
77                 ,svar)))))
78
79 ;;; WITH-mumble-STREAM calls the function in the given SLOT of the
80 ;;; STREAM with the ARGS for ANSI-STREAMs, or the FUNCTION with the
81 ;;; ARGS for FUNDAMENTAL-STREAMs.
82 (defmacro with-in-stream (stream (slot &rest args) &optional stream-dispatch)
83   `(let ((stream (in-synonym-of ,stream)))
84     ,(if stream-dispatch
85          `(if (ansi-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))))
91
92 (defmacro with-out-stream (stream (slot &rest args) &optional stream-dispatch)
93   `(let ((stream (out-synonym-of ,stream)))
94     ,(if stream-dispatch
95          `(if (ansi-stream-p stream)
96               (funcall (,slot stream) stream ,@args)
97               ,@(when stream-dispatch
98                   `(,(destructuring-bind (function &rest args) stream-dispatch
99                                          `(,function stream ,@args)))))
100          `(funcall (,slot stream) stream ,@args))))
101 \f
102 ;;;; These are hacks to make the reader win.
103
104 ;;; This macro sets up some local vars for use by the
105 ;;; FAST-READ-CHAR macro within the enclosed lexical scope. The stream
106 ;;; is assumed to be a ANSI-STREAM.
107 (defmacro prepare-for-fast-read-char (stream &body forms)
108   `(let* ((%frc-stream% ,stream)
109           (%frc-method% (ansi-stream-in %frc-stream%))
110           (%frc-buffer% (ansi-stream-in-buffer %frc-stream%))
111           (%frc-index% (ansi-stream-in-index %frc-stream%)))
112      (declare (type index %frc-index%)
113               (type ansi-stream %frc-stream%))
114      ,@forms))
115
116 ;;; This macro must be called after one is done with FAST-READ-CHAR
117 ;;; inside its scope to decache the ANSI-STREAM-IN-INDEX.
118 (defmacro done-with-fast-read-char ()
119   `(setf (ansi-stream-in-index %frc-stream%) %frc-index%))
120
121 ;;; a macro with the same calling convention as READ-CHAR, to be used
122 ;;; within the scope of a PREPARE-FOR-FAST-READ-CHAR
123 (defmacro fast-read-char (&optional (eof-error-p t) (eof-value ()))
124   `(cond
125     ((not %frc-buffer%)
126      (funcall %frc-method% %frc-stream% ,eof-error-p ,eof-value))
127     ((= %frc-index% +ansi-stream-in-buffer-length+)
128      (prog1 (fast-read-char-refill %frc-stream% ,eof-error-p ,eof-value)
129             (setq %frc-index% (ansi-stream-in-index %frc-stream%))))
130     (t
131      (prog1 (code-char (aref %frc-buffer% %frc-index%))
132             (incf %frc-index%)))))
133
134 ;;;; And these for the fasloader...
135
136 ;;; Just like PREPARE-FOR-FAST-READ-CHAR except that we get the BIN
137 ;;; method. The stream is assumed to be a ANSI-STREAM.
138 ;;;
139 ;;; KLUDGE: It seems weird to have to remember to explicitly call
140 ;;; DONE-WITH-FAST-READ-BYTE at the end of this, given that we're
141 ;;; already wrapping the stuff inside in a block. Why not rename this
142 ;;; macro to WITH-FAST-READ-BYTE, do the DONE-WITH-FAST-READ-BYTE stuff
143 ;;; automatically at the end of the block, and eliminate
144 ;;; DONE-WITH-FAST-READ-BYTE as a separate entity? (and similarly
145 ;;; for the FAST-READ-CHAR stuff) -- WHN 19990825
146 (defmacro prepare-for-fast-read-byte (stream &body forms)
147   `(let* ((%frc-stream% ,stream)
148           (%frc-method% (ansi-stream-bin %frc-stream%))
149           (%frc-buffer% (ansi-stream-in-buffer %frc-stream%))
150           (%frc-index% (ansi-stream-in-index %frc-stream%)))
151      (declare (type index %frc-index%)
152               (type ansi-stream %frc-stream%))
153      ,@forms))
154
155 ;;; Similar to fast-read-char, but we use a different refill routine & don't
156 ;;; convert to characters. If ANY-TYPE is true, then this can be used on any
157 ;;; integer streams, and we don't assert the result type.
158 (defmacro fast-read-byte (&optional (eof-error-p t) (eof-value ()) any-type)
159   ;; KLUDGE: should use ONCE-ONLY on EOF-ERROR-P and EOF-VALUE -- WHN 19990825
160   `(truly-the
161     ,(if (and (eq eof-error-p t) (not any-type)) '(unsigned-byte 8) t)
162     (cond
163      ((not %frc-buffer%)
164       (funcall %frc-method% %frc-stream% ,eof-error-p ,eof-value))
165      ((= %frc-index% +ansi-stream-in-buffer-length+)
166       (prog1 (fast-read-byte-refill %frc-stream% ,eof-error-p ,eof-value)
167         (setq %frc-index% (ansi-stream-in-index %frc-stream%))))
168      (t
169       (prog1 (aref %frc-buffer% %frc-index%)
170         (incf %frc-index%))))))
171 (defmacro done-with-fast-read-byte ()
172   `(done-with-fast-read-char))