ab967520c956b268de93909bdf89d4b71e877412
[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 \f
14 #!-gengc
15 (defmacro without-gcing (&rest body)
16   #!+sb-doc
17   "Executes the forms in the body without doing a garbage collection."
18   `(unwind-protect
19        (let ((*gc-inhibit* t))
20          ,@body)
21      (when (and *need-to-collect-garbage* (not *gc-inhibit*))
22        (maybe-gc nil))))
23
24 #!+gengc
25 (defmacro without-gcing (&rest body)
26   #!+sb-doc
27   "Executes the forms in the body without doing a garbage collection."
28   `(without-interrupts ,@body))
29 \f
30 ;;; EOF-OR-LOSE is a useful macro that handles EOF.
31 (defmacro eof-or-lose (stream eof-error-p eof-value)
32   `(if ,eof-error-p
33        (error 'end-of-file :stream ,stream)
34        ,eof-value))
35
36 ;;; These macros handle the special cases of T and NIL for input and
37 ;;; output streams.
38 ;;;
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)))
46                 #!+high-security
47                 (unless (input-stream-p ,svar)
48                   (error 'simple-type-error
49                          :datum ,svar
50                          :expected-type '(satisfies input-stream-p)
51                          :format-control "~S isn't an input stream"
52                          :format-arguments ,(list  svar)))              
53                 ,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)))
60                 #!+high-security
61                 (unless (output-stream-p ,svar)
62                   (error 'simple-type-error
63                          :datum ,svar
64                          :expected-type '(satisfies output-stream-p)
65                          :format-control "~S isn't an output stream."
66                          :format-arguments ,(list  svar)))
67                 ,svar)))))
68
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)))
74     ,(if stream-dispatch
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))))
81
82 (defmacro with-out-stream (stream (slot &rest args) &optional stream-dispatch)
83   `(let ((stream (out-synonym-of ,stream)))
84     ,(if stream-dispatch
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))))
91 \f
92 ;;;; These are hacks to make the reader win.
93
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%))
104      ,@forms))
105
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%))
110
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 ()))
114   `(cond
115     ((not %frc-buffer%)
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%))))
120     (t
121      (prog1 (code-char (aref %frc-buffer% %frc-index%))
122             (incf %frc-index%)))))
123
124 ;;;; And these for the fasloader...
125
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.
128 ;;;
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%))
143      ,@forms))
144
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
150   `(truly-the
151     ,(if (and (eq eof-error-p t) (not any-type)) '(unsigned-byte 8) t)
152     (cond
153      ((not %frc-buffer%)
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%))))
158      (t
159       (prog1 (aref %frc-buffer% %frc-index%)
160         (incf %frc-index%))))))
161 (defmacro done-with-fast-read-byte ()
162   `(done-with-fast-read-char))