Remove some test noise. A drop in the ocean unfortunately.
[sbcl.git] / src / code / target-misc.lisp
blobcec5b06725af27acfcbaadbd74bc75e34ddc20ec
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 ;; FIXME: Eliding general funcallable-instances for now.
138 ;; This does not set the name of an un-named closure because doing so
139 ;; is not a side-effecting operation that it ought to be.
140 ;; In contrast, SB-PCL::SET-FUN-NAME specifically says that only if the
141 ;; argument fun is a funcallable instance must it retain its identity.
142 ;; That function *is* allowed to cons a new closure to name it.
143 ((or simple-fun closure)
144 (if (and (closurep function) (nth-value 1 (closure-name function)))
145 (set-closure-name function new-value)
146 (setf (%simple-fun-name (%fun-fun function)) new-value))))
147 new-value)
149 (defun %fun-doc (function)
150 (typecase function
151 #!+sb-eval
152 (sb!eval:interpreted-function
153 (sb!eval:interpreted-function-documentation function))
155 (when (closurep function)
156 (multiple-value-bind (name namedp) (closure-name function)
157 (when namedp
158 (return-from %fun-doc (random-documentation name 'function)))))
159 (%simple-fun-doc (%fun-fun function)))))
161 (defun (setf %fun-doc) (new-value function)
162 (declare (type (or null string) new-value))
163 (typecase function
164 #!+sb-eval
165 (sb!eval:interpreted-function
166 (setf (sb!eval:interpreted-function-documentation function) new-value))
167 ((or simple-fun closure)
168 (when (closurep function)
169 (multiple-value-bind (name namedp) (closure-name function)
170 (when namedp
171 (return-from %fun-doc
172 (setf (random-documentation name 'function) new-value)))))
173 (setf (%simple-fun-doc (%fun-fun function)) new-value)))
174 new-value)
176 (defun code-n-unboxed-data-words (code-obj)
177 ;; If the number of boxed words (from the header) is not the same as
178 ;; the displacement backwards from the first simple-fun to the header,
179 ;; then there are unboxed constants between the end of the boxed constants
180 ;; and the first simple-fun.
181 (let ((f (%code-entry-points code-obj)))
182 (or (and f
183 (let ((from (get-header-data code-obj))
184 (to (ash (with-pinned-objects (f)
185 (sap-ref-word (int-sap (get-lisp-obj-address f))
186 (- sb!vm:fun-pointer-lowtag)))
187 (- sb!vm:n-widetag-bits))))
188 (and (< from to) (- to from))))
189 0)))
191 ;;; various environment inquiries
193 (defvar *features*
194 '#.(sort (copy-list sb-cold:*shebang-features*) #'string<)
195 #!+sb-doc
196 "a list of symbols that describe features provided by the
197 implementation")
199 (defun machine-instance ()
200 #!+sb-doc
201 "Return a string giving the name of the local machine."
202 #!+win32 (sb!win32::get-computer-name)
203 #!-win32 (sb!unix:unix-gethostname))
205 (defvar *machine-version*)
207 (defun machine-version ()
208 #!+sb-doc
209 "Return a string describing the version of the computer hardware we
210 are running on, or NIL if we can't find any useful information."
211 (unless (boundp '*machine-version*)
212 (setf *machine-version* (get-machine-version)))
213 *machine-version*)
215 ;;; FIXME: Don't forget to set these in a sample site-init file.
216 ;;; FIXME: Perhaps the functions could be SETFable instead of having the
217 ;;; interface be through special variables? As far as I can tell
218 ;;; from ANSI 11.1.2.1.1 "Constraints on the COMMON-LISP Package
219 ;;; for Conforming Implementations" it is kosher to add a SETF function for
220 ;;; a symbol in COMMON-LISP..
221 (defvar *short-site-name* nil
222 #!+sb-doc
223 "The value of SHORT-SITE-NAME.")
224 (defvar *long-site-name* nil
225 #!+sb-doc "the value of LONG-SITE-NAME")
226 (defun short-site-name ()
227 #!+sb-doc
228 "Return a string with the abbreviated site name, or NIL if not known."
229 *short-site-name*)
230 (defun long-site-name ()
231 #!+sb-doc
232 "Return a string with the long form of the site name, or NIL if not known."
233 *long-site-name*)
235 ;;;; ED
236 (defvar *ed-functions* nil
237 #!+sb-doc
238 "See function documentation for ED.")
240 (defun ed (&optional x)
241 #!+sb-doc
242 "Starts the editor (on a file or a function if named). Functions
243 from the list *ED-FUNCTIONS* are called in order with X as an argument
244 until one of them returns non-NIL; these functions are responsible for
245 signalling a FILE-ERROR to indicate failure to perform an operation on
246 the file system."
247 (dolist (fun *ed-functions*
248 (error 'extension-failure
249 :format-control "Don't know how to ~S ~A"
250 :format-arguments (list 'ed x)
251 :references (list '(:sbcl :variable *ed-functions*))))
252 (when (funcall fun x)
253 (return t))))
255 ;;;; dribble stuff
257 ;;; Each time we start dribbling to a new stream, we put it in
258 ;;; *DRIBBLE-STREAM*, and push a list of *DRIBBLE-STREAM*, *STANDARD-INPUT*,
259 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* in *PREVIOUS-DRIBBLE-STREAMS*.
260 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* is changed to a broadcast stream that
261 ;;; broadcasts to *DRIBBLE-STREAM* and to the old values of the variables.
262 ;;; *STANDARD-INPUT* is changed to an echo stream that echos input from the old
263 ;;; value of standard input to *DRIBBLE-STREAM*.
265 ;;; When dribble is called with no arguments, *DRIBBLE-STREAM* is closed,
266 ;;; and the values of *DRIBBLE-STREAM*, *STANDARD-INPUT*, and
267 ;;; *STANDARD-OUTPUT* are popped from *PREVIOUS-DRIBBLE-STREAMS*.
269 (defvar *previous-dribble-streams* nil)
270 (defvar *dribble-stream* nil)
272 (defun dribble (&optional pathname &key (if-exists :append))
273 #!+sb-doc
274 "With a file name as an argument, dribble opens the file and sends a
275 record of further I/O to that file. Without an argument, it closes
276 the dribble file, and quits logging."
277 (cond (pathname
278 (let* ((new-dribble-stream
279 (open pathname
280 :direction :output
281 :if-exists if-exists
282 :if-does-not-exist :create))
283 (new-standard-output
284 (make-broadcast-stream *standard-output* new-dribble-stream))
285 (new-error-output
286 (make-broadcast-stream *error-output* new-dribble-stream))
287 (new-standard-input
288 (make-echo-stream *standard-input* new-dribble-stream)))
289 (push (list *dribble-stream* *standard-input* *standard-output*
290 *error-output*)
291 *previous-dribble-streams*)
292 (setf *dribble-stream* new-dribble-stream)
293 (setf *standard-input* new-standard-input)
294 (setf *standard-output* new-standard-output)
295 (setf *error-output* new-error-output)))
296 ((null *dribble-stream*)
297 (error "not currently dribbling"))
299 (let ((old-streams (pop *previous-dribble-streams*)))
300 (close *dribble-stream*)
301 (setf *dribble-stream* (first old-streams))
302 (setf *standard-input* (second old-streams))
303 (setf *standard-output* (third old-streams))
304 (setf *error-output* (fourth old-streams)))))
305 (values))
307 (defun %byte-blt (src src-start dst dst-start dst-end)
308 (%byte-blt src src-start dst dst-start dst-end))
310 ;;;; some *LOAD-FOO* variables
312 (defvar *load-print* nil
313 #!+sb-doc
314 "the default for the :PRINT argument to LOAD")
316 (defvar *load-verbose* nil
317 ;; Note that CMU CL's default for this was T, and ANSI says it's
318 ;; implementation-dependent. We choose NIL on the theory that it's
319 ;; a nicer default behavior for Unix programs.
320 #!+sb-doc
321 "the default for the :VERBOSE argument to LOAD")