1 ;;;; Environment query functions, DOCUMENTATION and DRIBBLE.
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
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
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)
36 (sb!interpreter
:interpreted-function
37 (sb!interpreter
:fun-pretty-arglist function
))
39 (sb!eval
:interpreted-function
40 (sb!eval
:interpreted-function-debug-lambda-list function
))
42 (%simple-fun-arglist
(%fun-fun function
)))))
44 (defun (setf %fun-lambda-list
) (new-value function
)
47 (sb!interpreter
:interpreted-function
48 (sb!interpreter
:set-fun-pretty-arglist function new-value
))
50 (sb!eval
:interpreted-function
51 (setf (sb!eval
:interpreted-function-debug-lambda-list function
) new-value
))
52 ;; FIXME: Eliding general funcallable-instances for now.
53 ((or simple-fun closure
)
54 (setf (%simple-fun-arglist
(%fun-fun function
)) new-value
)))
57 (defun %fun-type
(function)
60 ;; Obtain a list of the right shape, usually with T for each
61 ;; arg type, but respecting local declarations if any.
62 (sb!interpreter
:interpreted-function
(sb!interpreter
:%fun-type function
))
63 (t (%simple-fun-type
(%fun-fun function
)))))
65 (!defglobal
*closure-name-marker
* (make-symbol ".CLOSURE-NAME."))
66 (defun closure-name (closure)
67 (declare (closure closure
))
68 (let ((len (get-closure-length closure
)))
70 ;; The number of closure-values is 1- the len.
71 ;; The index of the last value is 1- that.
72 ;; The index of the name-marker is 1- that.
73 ;; (closure index 0 is the first closed-over value)
74 (eq (%closure-index-ref closure
(- len
3))
75 (load-time-value *closure-name-marker
* t
)))
76 (values (%closure-index-ref closure
(- len
2)) t
)
79 ;; Add 2 "slots" to the payload of a closure, one for the magic symbol
80 ;; signifying that there is a name, and one for the name itself.
81 (defun nameify-closure (closure)
82 (declare (closure closure
))
83 (let* ((physical-len (get-closure-length closure
)) ; excluding header
84 ;; subtract 1 because physical-len includes the trampoline word.
85 (new-n-closure-vals (+ 2 (1- physical-len
)))
86 ;; Closures and funcallable-instances are pretty much the same to GC.
87 ;; They're both varying-length boxed-payload objects.
88 ;; But funcallable-instance has <tramp, function, info>
89 ;; where closure has <tramp, info> so subtract 1 more word.
90 (copy (%make-funcallable-instance
(1- new-n-closure-vals
))))
91 (with-pinned-objects (closure copy
)
92 ;; change the widetag from funcallable-instance to closure.
93 (setf (sap-ref-word (int-sap (get-lisp-obj-address copy
))
94 (- sb
!vm
:fun-pointer-lowtag
))
95 (logior (ash (+ physical-len
2) 8) sb
!vm
:closure-header-widetag
))
96 (macrolet ((word (obj index
)
97 `(sap-ref-lispobj (int-sap (get-lisp-obj-address ,obj
))
98 (+ (- sb
!vm
:fun-pointer-lowtag
)
99 (ash ,index sb
!vm
:word-shift
)))))
100 (loop for i from
1 to physical-len
101 do
(setf (word copy i
) (word closure i
)))
102 (setf (word copy
(1+ physical-len
)) *closure-name-marker
*)))
105 ;; Rename a closure. Doing so changes its identity unless it was already named.
106 ;; To do this without allocating a new closure, we'd need an interface that
107 ;; requests a placeholder from the outset. One possibility is that
108 ;; (NAMED-LAMBDA NIL (x) ...) would allocate the name, initially stored as nil.
109 ;; In that case, the simple-fun's debug-info could also contain a bit that
110 ;; indicates that all closures over it are named, eliminating the storage
111 ;; and check for *closure-name-marker* in the closure values.
112 (defun set-closure-name (closure new-name
)
113 (declare (closure closure
))
114 (unless (nth-value 1 (closure-name closure
))
115 (setq closure
(nameify-closure closure
)))
116 ;; There are no closure slot setters, and in fact SLOT-SET
117 ;; does not exist in a variant that takes a non-constant index.
118 (with-pinned-objects (closure)
119 (setf (sap-ref-lispobj (int-sap (get-lisp-obj-address closure
))
120 (+ (- sb
!vm
:fun-pointer-lowtag
)
121 (ash (get-closure-length closure
)
126 ;;; FIXME: there is no reason to expose two FUN-NAME readers,
127 ;;; one that works for everything except generic, and one that always works.
129 (if (typep x
'standard-generic-function
)
130 (sb!mop
:generic-function-name x
)
133 ;;; a SETFable function to return the associated debug name for FUN
134 ;;; (i.e., the third value returned from CL:FUNCTION-LAMBDA-EXPRESSION),
135 ;;; or NIL if there's none
136 (defun %fun-name
(function)
139 (sb!eval
:interpreted-function
140 (sb!eval
:interpreted-function-debug-name function
))
142 (sb!interpreter
:interpreted-function
(sb!interpreter
:fun-name function
))
145 (if (and (closurep function
)
147 (multiple-value-setq (name namedp
) (closure-name function
))
150 (%simple-fun-name
(%fun-fun function
)))))))
152 (defun (setf %fun-name
) (new-value function
)
155 (sb!eval
:interpreted-function
156 (setf (sb!eval
:interpreted-function-debug-name function
) new-value
))
158 (sb!interpreter
:interpreted-function
159 (sb!interpreter
:set-fun-name function new-value
))
161 ;; STANDARD-GENERIC-FUNCTION definitely has a NAME,
162 ;; but other subtypes of GENERIC-FUNCTION could as well.
163 (when (slot-exists-p function
'sb
!pcl
::name
)
164 (setf (slot-value function
'sb
!pcl
::name
) new-value
)))
165 ;; This does not set the name of an un-named closure because doing so
166 ;; is not a side-effecting operation that it ought to be.
167 ;; In contrast, SB-PCL::SET-FUN-NAME specifically says that only if the
168 ;; argument fun is a funcallable instance must it retain its identity.
169 ;; That function *is* allowed to cons a new closure to name it.
170 ((or simple-fun closure
)
171 (if (and (closurep function
) (nth-value 1 (closure-name function
)))
172 (set-closure-name function new-value
)
173 (setf (%simple-fun-name
(%fun-fun function
)) new-value
))))
176 (defun %fun-doc
(function)
179 (sb!interpreter
:interpreted-function
180 (sb!interpreter
:fun-docstring function
))
182 (sb!eval
:interpreted-function
183 (sb!eval
:interpreted-function-documentation function
))
185 (when (closurep function
)
186 (multiple-value-bind (name namedp
) (closure-name function
)
188 (return-from %fun-doc
(random-documentation name
'function
)))))
189 (%simple-fun-doc
(%fun-fun function
)))))
191 (defun (setf %fun-doc
) (new-value function
)
192 (declare (type (or null string
) new-value
))
195 (sb!interpreter
:interpreted-function
196 (sb!interpreter
:set-fun-docstring function new-value
))
198 (sb!eval
:interpreted-function
199 (setf (sb!eval
:interpreted-function-documentation function
) new-value
))
200 ((or simple-fun closure
)
201 (when (closurep function
)
202 (multiple-value-bind (name namedp
) (closure-name function
)
204 (return-from %fun-doc
205 (setf (random-documentation name
'function
) new-value
)))))
206 (setf (%simple-fun-doc
(%fun-fun function
)) new-value
)))
209 (defun code-n-unboxed-data-words (code-obj)
210 ;; If the number of boxed words (from the header) is not the same as
211 ;; the displacement backwards from the first simple-fun to the header,
212 ;; then there are unboxed constants between the end of the boxed constants
213 ;; and the first simple-fun.
214 (let ((f (%code-entry-points code-obj
)))
216 (let ((from (get-header-data code-obj
))
217 (to (ash (with-pinned-objects (f)
218 (sap-ref-word (int-sap (get-lisp-obj-address f
))
219 (- sb
!vm
:fun-pointer-lowtag
)))
220 (- sb
!vm
:n-widetag-bits
))))
221 (and (< from to
) (- to from
))))
224 ;;; various environment inquiries
227 '#.
(sort (copy-list sb-cold
:*shebang-features
*) #'string
<)
229 "a list of symbols that describe features provided by the
232 (defun machine-instance ()
234 "Return a string giving the name of the local machine."
235 #!+win32
(sb!win32
::get-computer-name
)
236 #!-win32
(truly-the simple-string
(sb!unix
:unix-gethostname
)))
238 (defvar *machine-version
*)
240 (defun machine-version ()
242 "Return a string describing the version of the computer hardware we
243 are running on, or NIL if we can't find any useful information."
244 (unless (boundp '*machine-version
*)
245 (setf *machine-version
* (get-machine-version)))
248 ;;; FIXME: Don't forget to set these in a sample site-init file.
249 ;;; FIXME: Perhaps the functions could be SETFable instead of having the
250 ;;; interface be through special variables? As far as I can tell
251 ;;; from ANSI 11.1.2.1.1 "Constraints on the COMMON-LISP Package
252 ;;; for Conforming Implementations" it is kosher to add a SETF function for
253 ;;; a symbol in COMMON-LISP..
254 (defvar *short-site-name
* nil
256 "The value of SHORT-SITE-NAME.")
257 (defvar *long-site-name
* nil
258 #!+sb-doc
"the value of LONG-SITE-NAME")
259 (defun short-site-name ()
261 "Return a string with the abbreviated site name, or NIL if not known."
263 (defun long-site-name ()
265 "Return a string with the long form of the site name, or NIL if not known."
269 (defvar *ed-functions
* nil
271 "See function documentation for ED.")
273 (defun ed (&optional x
)
275 "Starts the editor (on a file or a function if named). Functions
276 from the list *ED-FUNCTIONS* are called in order with X as an argument
277 until one of them returns non-NIL; these functions are responsible for
278 signalling a FILE-ERROR to indicate failure to perform an operation on
280 (dolist (fun *ed-functions
*
281 (error 'extension-failure
282 :format-control
"Don't know how to ~S ~A"
283 :format-arguments
(list 'ed x
)
284 :references
(list '(:sbcl
:variable
*ed-functions
*))))
285 (when (funcall fun x
)
290 ;;; Each time we start dribbling to a new stream, we put it in
291 ;;; *DRIBBLE-STREAM*, and push a list of *DRIBBLE-STREAM*, *STANDARD-INPUT*,
292 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* in *PREVIOUS-DRIBBLE-STREAMS*.
293 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* is changed to a broadcast stream that
294 ;;; broadcasts to *DRIBBLE-STREAM* and to the old values of the variables.
295 ;;; *STANDARD-INPUT* is changed to an echo stream that echos input from the old
296 ;;; value of standard input to *DRIBBLE-STREAM*.
298 ;;; When dribble is called with no arguments, *DRIBBLE-STREAM* is closed,
299 ;;; and the values of *DRIBBLE-STREAM*, *STANDARD-INPUT*, and
300 ;;; *STANDARD-OUTPUT* are popped from *PREVIOUS-DRIBBLE-STREAMS*.
302 (defvar *previous-dribble-streams
* nil
)
303 (defvar *dribble-stream
* nil
)
305 (defun dribble (&optional pathname
&key
(if-exists :append
))
307 "With a file name as an argument, dribble opens the file and sends a
308 record of further I/O to that file. Without an argument, it closes
309 the dribble file, and quits logging."
311 (let* ((new-dribble-stream
315 :if-does-not-exist
:create
))
317 (make-broadcast-stream *standard-output
* new-dribble-stream
))
319 (make-broadcast-stream *error-output
* new-dribble-stream
))
321 (make-echo-stream *standard-input
* new-dribble-stream
)))
322 (push (list *dribble-stream
* *standard-input
* *standard-output
*
324 *previous-dribble-streams
*)
325 (setf *dribble-stream
* new-dribble-stream
)
326 (setf *standard-input
* new-standard-input
)
327 (setf *standard-output
* new-standard-output
)
328 (setf *error-output
* new-error-output
)))
329 ((null *dribble-stream
*)
330 (error "not currently dribbling"))
332 (let ((old-streams (pop *previous-dribble-streams
*)))
333 (close *dribble-stream
*)
334 (setf *dribble-stream
* (first old-streams
))
335 (setf *standard-input
* (second old-streams
))
336 (setf *standard-output
* (third old-streams
))
337 (setf *error-output
* (fourth old-streams
)))))
340 (defun %byte-blt
(src src-start dst dst-start dst-end
)
341 (%byte-blt src src-start dst dst-start dst-end
))
343 ;;;; some *LOAD-FOO* variables
345 (defvar *load-print
* nil
347 "the default for the :PRINT argument to LOAD")
349 (defvar *load-verbose
* nil
350 ;; Note that CMU CL's default for this was T, and ANSI says it's
351 ;; implementation-dependent. We choose NIL on the theory that it's
352 ;; a nicer default behavior for Unix programs.
354 "the default for the :VERBOSE argument to LOAD")