Eliminate style-warning about undefined type GLOBAL-VAR
[sbcl.git] / src / code / target-misc.lisp
blobaae550966ff938ddf8abde5f406ea4a141d771f4
1 ;;;; Environment query functions, DOCUMENTATION and DRIBBLE.
2 ;;;;
3 ;;;; FIXME: If there are exactly three things in here, it could be
4 ;;;; exactly three files named e.g. equery.lisp, doc.lisp, and dribble.lisp.
6 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; more information.
8 ;;;;
9 ;;;; This software is derived from the CMU CL system, which was
10 ;;;; written at Carnegie Mellon University and released into the
11 ;;;; public domain. The software is in the public domain and is
12 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
13 ;;;; files for more information.
15 (in-package "SB!IMPL")
16 ;;;; Generalizing over SIMPLE-FUN, CLOSURE, and FUNCALLABLE-INSTANCEs
18 ;;; Underlying SIMPLE-FUN
19 (defun %fun-fun (function)
20 (declare (function function))
21 ;; It's too bad that TYPECASE isn't able to generate equivalent code.
22 (case (fun-subtype function)
23 (#.sb!vm:simple-fun-header-widetag
24 function)
25 (#.sb!vm:closure-header-widetag
26 (%closure-fun function))
27 (#.sb!vm:funcallable-instance-header-widetag
28 ;; %FUNCALLABLE-INSTANCE-FUNCTION is not known to return a FUNCTION.
29 ;; Is that right? Shouldn't we always initialize to something
30 ;; that is a function, such as an error-signaling trampoline?
31 (%fun-fun (%funcallable-instance-function function)))))
33 (defun %fun-lambda-list (function)
34 (typecase function
35 #!+sb-eval
36 (sb!eval:interpreted-function
37 (sb!eval:interpreted-function-debug-lambda-list function))
39 (%simple-fun-arglist (%fun-fun function)))))
41 (defun (setf %fun-lambda-list) (new-value function)
42 (typecase function
43 #!+sb-eval
44 (sb!eval:interpreted-function
45 (setf (sb!eval:interpreted-function-debug-lambda-list function) new-value))
46 ;; FIXME: Eliding general funcallable-instances for now.
47 ((or simple-fun closure)
48 (setf (%simple-fun-arglist (%fun-fun function)) new-value)))
49 new-value)
51 (defun %fun-type (function)
52 (%simple-fun-type (%fun-fun function)))
54 (!defglobal *closure-name-marker* (make-symbol ".CLOSURE-NAME."))
55 (defun closure-name (closure)
56 (declare (closure closure))
57 (let ((len (get-closure-length closure)))
58 (if (and (>= len 4)
59 ;; The number of closure-values is 1- the len.
60 ;; The index of the last value is 1- that.
61 ;; The index of the name-marker is 1- that.
62 ;; (closure index 0 is the first closed-over value)
63 (eq (%closure-index-ref closure (- len 3))
64 (load-time-value *closure-name-marker* t)))
65 (values (%closure-index-ref closure (- len 2)) t)
66 (values nil nil))))
68 ;; Add 2 "slots" to the payload of a closure, one for the magic symbol
69 ;; signifying that there is a name, and one for the name itself.
70 (defun nameify-closure (closure)
71 (declare (closure closure))
72 (let* ((physical-len (get-closure-length closure)) ; excluding header
73 ;; subtract 1 because physical-len includes the trampoline word.
74 (new-n-closure-vals (+ 2 (1- physical-len)))
75 ;; Closures and funcallable-instances are pretty much the same to GC.
76 ;; They're both varying-length boxed-payload objects.
77 ;; But funcallable-instance has <tramp, function, info>
78 ;; where closure has <tramp, info> so subtract 1 more word.
79 (copy (%make-funcallable-instance (1- new-n-closure-vals))))
80 (with-pinned-objects (closure copy)
81 ;; change the widetag from funcallable-instance to closure.
82 (setf (sap-ref-word (int-sap (get-lisp-obj-address copy))
83 (- sb!vm:fun-pointer-lowtag))
84 (logior (ash (+ physical-len 2) 8) sb!vm:closure-header-widetag))
85 (macrolet ((word (obj index)
86 `(sap-ref-lispobj (int-sap (get-lisp-obj-address ,obj))
87 (+ (- sb!vm:fun-pointer-lowtag)
88 (ash ,index sb!vm:word-shift)))))
89 (loop for i from 1 to physical-len
90 do (setf (word copy i) (word closure i)))
91 (setf (word copy (1+ physical-len)) *closure-name-marker*)))
92 copy))
94 ;; Rename a closure. Doing so changes its identity unless it was already named.
95 ;; To do this without allocating a new closure, we'd need an interface that
96 ;; requests a placeholder from the outset. One possibility is that
97 ;; (NAMED-LAMBDA NIL (x) ...) would allocate the name, initially stored as nil.
98 ;; In that case, the simple-fun's debug-info could also contain a bit that
99 ;; indicates that all closures over it are named, eliminating the storage
100 ;; and check for *closure-name-marker* in the closure values.
101 (defun set-closure-name (closure new-name)
102 (declare (closure closure))
103 (unless (nth-value 1 (closure-name closure))
104 (setq closure (nameify-closure closure)))
105 ;; There are no closure slot setters, and in fact SLOT-SET
106 ;; does not exist in a variant that takes a non-constant index.
107 (with-pinned-objects (closure)
108 (setf (sap-ref-lispobj (int-sap (get-lisp-obj-address closure))
109 (+ (- sb!vm:fun-pointer-lowtag)
110 (ash (get-closure-length closure)
111 sb!vm:word-shift)))
112 new-name))
113 closure)
115 ;;; a SETFable function to return the associated debug name for FUN
116 ;;; (i.e., the third value returned from CL:FUNCTION-LAMBDA-EXPRESSION),
117 ;;; or NIL if there's none
118 (defun %fun-name (function)
119 (typecase function
120 #!+sb-eval
121 (sb!eval:interpreted-function
122 (sb!eval:interpreted-function-debug-name function))
124 (let (name namedp)
125 (if (and (closurep function)
126 (progn
127 (multiple-value-setq (name namedp) (closure-name function))
128 namedp))
129 name
130 (%simple-fun-name (%fun-fun function)))))))
132 (defun (setf %fun-name) (new-value function)
133 (typecase function
134 #!+sb-eval
135 (sb!eval:interpreted-function
136 (setf (sb!eval:interpreted-function-debug-name function) new-value))
137 (generic-function
138 ;; STANDARD-GENERIC-FUNCTION definitely has a NAME,
139 ;; but other subtypes of GENERIC-FUNCTION could as well.
140 (when (slot-exists-p function 'sb!pcl::name)
141 (setf (slot-value function 'sb!pcl::name) new-value)))
142 ;; This does not set the name of an un-named closure because doing so
143 ;; is not a side-effecting operation that it ought to be.
144 ;; In contrast, SB-PCL::SET-FUN-NAME specifically says that only if the
145 ;; argument fun is a funcallable instance must it retain its identity.
146 ;; That function *is* allowed to cons a new closure to name it.
147 ((or simple-fun closure)
148 (if (and (closurep function) (nth-value 1 (closure-name function)))
149 (set-closure-name function new-value)
150 (setf (%simple-fun-name (%fun-fun function)) new-value))))
151 new-value)
153 (defun %fun-doc (function)
154 (typecase function
155 #!+sb-eval
156 (sb!eval:interpreted-function
157 (sb!eval:interpreted-function-documentation function))
159 (when (closurep function)
160 (multiple-value-bind (name namedp) (closure-name function)
161 (when namedp
162 (return-from %fun-doc (random-documentation name 'function)))))
163 (%simple-fun-doc (%fun-fun function)))))
165 (defun (setf %fun-doc) (new-value function)
166 (declare (type (or null string) new-value))
167 (typecase function
168 #!+sb-eval
169 (sb!eval:interpreted-function
170 (setf (sb!eval:interpreted-function-documentation function) new-value))
171 ((or simple-fun closure)
172 (when (closurep function)
173 (multiple-value-bind (name namedp) (closure-name function)
174 (when namedp
175 (return-from %fun-doc
176 (setf (random-documentation name 'function) new-value)))))
177 (setf (%simple-fun-doc (%fun-fun function)) new-value)))
178 new-value)
180 (defun code-n-unboxed-data-words (code-obj)
181 ;; If the number of boxed words (from the header) is not the same as
182 ;; the displacement backwards from the first simple-fun to the header,
183 ;; then there are unboxed constants between the end of the boxed constants
184 ;; and the first simple-fun.
185 (let ((f (%code-entry-points code-obj)))
186 (or (and f
187 (let ((from (get-header-data code-obj))
188 (to (ash (with-pinned-objects (f)
189 (sap-ref-word (int-sap (get-lisp-obj-address f))
190 (- sb!vm:fun-pointer-lowtag)))
191 (- sb!vm:n-widetag-bits))))
192 (and (< from to) (- to from))))
193 0)))
195 ;;; various environment inquiries
197 (defvar *features*
198 '#.(sort (copy-list sb-cold:*shebang-features*) #'string<)
199 #!+sb-doc
200 "a list of symbols that describe features provided by the
201 implementation")
203 (defun machine-instance ()
204 #!+sb-doc
205 "Return a string giving the name of the local machine."
206 #!+win32 (sb!win32::get-computer-name)
207 #!-win32 (sb!unix:unix-gethostname))
209 (defvar *machine-version*)
211 (defun machine-version ()
212 #!+sb-doc
213 "Return a string describing the version of the computer hardware we
214 are running on, or NIL if we can't find any useful information."
215 (unless (boundp '*machine-version*)
216 (setf *machine-version* (get-machine-version)))
217 *machine-version*)
219 ;;; FIXME: Don't forget to set these in a sample site-init file.
220 ;;; FIXME: Perhaps the functions could be SETFable instead of having the
221 ;;; interface be through special variables? As far as I can tell
222 ;;; from ANSI 11.1.2.1.1 "Constraints on the COMMON-LISP Package
223 ;;; for Conforming Implementations" it is kosher to add a SETF function for
224 ;;; a symbol in COMMON-LISP..
225 (defvar *short-site-name* nil
226 #!+sb-doc
227 "The value of SHORT-SITE-NAME.")
228 (defvar *long-site-name* nil
229 #!+sb-doc "the value of LONG-SITE-NAME")
230 (defun short-site-name ()
231 #!+sb-doc
232 "Return a string with the abbreviated site name, or NIL if not known."
233 *short-site-name*)
234 (defun long-site-name ()
235 #!+sb-doc
236 "Return a string with the long form of the site name, or NIL if not known."
237 *long-site-name*)
239 ;;;; ED
240 (defvar *ed-functions* nil
241 #!+sb-doc
242 "See function documentation for ED.")
244 (defun ed (&optional x)
245 #!+sb-doc
246 "Starts the editor (on a file or a function if named). Functions
247 from the list *ED-FUNCTIONS* are called in order with X as an argument
248 until one of them returns non-NIL; these functions are responsible for
249 signalling a FILE-ERROR to indicate failure to perform an operation on
250 the file system."
251 (dolist (fun *ed-functions*
252 (error 'extension-failure
253 :format-control "Don't know how to ~S ~A"
254 :format-arguments (list 'ed x)
255 :references (list '(:sbcl :variable *ed-functions*))))
256 (when (funcall fun x)
257 (return t))))
259 ;;;; dribble stuff
261 ;;; Each time we start dribbling to a new stream, we put it in
262 ;;; *DRIBBLE-STREAM*, and push a list of *DRIBBLE-STREAM*, *STANDARD-INPUT*,
263 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* in *PREVIOUS-DRIBBLE-STREAMS*.
264 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* is changed to a broadcast stream that
265 ;;; broadcasts to *DRIBBLE-STREAM* and to the old values of the variables.
266 ;;; *STANDARD-INPUT* is changed to an echo stream that echos input from the old
267 ;;; value of standard input to *DRIBBLE-STREAM*.
269 ;;; When dribble is called with no arguments, *DRIBBLE-STREAM* is closed,
270 ;;; and the values of *DRIBBLE-STREAM*, *STANDARD-INPUT*, and
271 ;;; *STANDARD-OUTPUT* are popped from *PREVIOUS-DRIBBLE-STREAMS*.
273 (defvar *previous-dribble-streams* nil)
274 (defvar *dribble-stream* nil)
276 (defun dribble (&optional pathname &key (if-exists :append))
277 #!+sb-doc
278 "With a file name as an argument, dribble opens the file and sends a
279 record of further I/O to that file. Without an argument, it closes
280 the dribble file, and quits logging."
281 (cond (pathname
282 (let* ((new-dribble-stream
283 (open pathname
284 :direction :output
285 :if-exists if-exists
286 :if-does-not-exist :create))
287 (new-standard-output
288 (make-broadcast-stream *standard-output* new-dribble-stream))
289 (new-error-output
290 (make-broadcast-stream *error-output* new-dribble-stream))
291 (new-standard-input
292 (make-echo-stream *standard-input* new-dribble-stream)))
293 (push (list *dribble-stream* *standard-input* *standard-output*
294 *error-output*)
295 *previous-dribble-streams*)
296 (setf *dribble-stream* new-dribble-stream)
297 (setf *standard-input* new-standard-input)
298 (setf *standard-output* new-standard-output)
299 (setf *error-output* new-error-output)))
300 ((null *dribble-stream*)
301 (error "not currently dribbling"))
303 (let ((old-streams (pop *previous-dribble-streams*)))
304 (close *dribble-stream*)
305 (setf *dribble-stream* (first old-streams))
306 (setf *standard-input* (second old-streams))
307 (setf *standard-output* (third old-streams))
308 (setf *error-output* (fourth old-streams)))))
309 (values))
311 (defun %byte-blt (src src-start dst dst-start dst-end)
312 (%byte-blt src src-start dst dst-start dst-end))
314 ;;;; some *LOAD-FOO* variables
316 (defvar *load-print* nil
317 #!+sb-doc
318 "the default for the :PRINT argument to LOAD")
320 (defvar *load-verbose* nil
321 ;; Note that CMU CL's default for this was T, and ANSI says it's
322 ;; implementation-dependent. We choose NIL on the theory that it's
323 ;; a nicer default behavior for Unix programs.
324 #!+sb-doc
325 "the default for the :VERBOSE argument to LOAD")