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
:closure-header-widetag
24 (%closure-fun function
))
25 (#.sb
!vm
:funcallable-instance-header-widetag
26 ;; %FUNCALLABLE-INSTANCE-FUNCTION is not known to return a FUNCTION.
27 ;; Is that right? Shouldn't we always initialize to something
28 ;; that is a function, such as an error-signaling trampoline?
29 (%fun-fun
(%funcallable-instance-function function
)))
32 (defun %fun-lambda-list
(function)
35 (sb!interpreter
:interpreted-function
36 (sb!interpreter
:proto-fn-pretty-arglist
37 (sb!interpreter
:fun-proto-fn 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 (setf (sb!interpreter
:proto-fn-pretty-arglist
49 (sb!interpreter
:fun-proto-fn function
)) new-value
))
51 (sb!eval
:interpreted-function
52 (setf (sb!eval
:interpreted-function-debug-lambda-list function
) new-value
))
53 ;; FIXME: Eliding general funcallable-instances for now.
54 ((or simple-fun closure
)
55 (setf (%simple-fun-arglist
(%fun-fun function
)) new-value
)))
58 (defun %fun-type
(function)
61 ;; Obtain a list of the right shape, usually with T for each
62 ;; arg type, but respecting local declarations if any.
63 (sb!interpreter
:interpreted-function
(sb!interpreter
:%fun-type function
))
64 (t (%simple-fun-type
(%fun-fun function
)))))
66 (!defglobal
*closure-name-marker
* (make-symbol ".CLOSURE-NAME."))
67 (defun closure-name (closure)
68 (declare (closure closure
))
69 (let ((len (get-closure-length closure
)))
71 ;; The number of closure-values is 1- the len.
72 ;; The index of the last value is 1- that.
73 ;; The index of the name-marker is 1- that.
74 ;; (closure index 0 is the first closed-over value)
75 (eq (%closure-index-ref closure
(- len
3))
76 (load-time-value *closure-name-marker
* t
)))
77 (values (%closure-index-ref closure
(- len
2)) t
)
80 ;; Add 2 "slots" to the payload of a closure, one for the magic symbol
81 ;; signifying that there is a name, and one for the name itself.
82 (defun nameify-closure (closure)
83 (declare (closure closure
))
84 (let* ((physical-len (get-closure-length closure
)) ; excluding header
85 ;; subtract 1 because physical-len includes the trampoline word.
86 (new-n-closure-vals (+ 2 (1- physical-len
)))
87 ;; Closures and funcallable-instances are pretty much the same to GC.
88 ;; They're both varying-length boxed-payload objects.
89 ;; But funcallable-instance has <tramp, function, info>
90 ;; where closure has <tramp, info> so subtract 1 more word.
91 (copy (%make-funcallable-instance
(1- new-n-closure-vals
))))
92 (with-pinned-objects (closure copy
)
93 ;; change the widetag from funcallable-instance to closure.
94 (setf (sap-ref-word (int-sap (get-lisp-obj-address copy
))
95 (- sb
!vm
:fun-pointer-lowtag
))
96 (logior (ash (+ physical-len
2) 8) sb
!vm
:closure-header-widetag
))
97 (macrolet ((word (obj index
)
98 `(sap-ref-lispobj (int-sap (get-lisp-obj-address ,obj
))
99 (+ (- sb
!vm
:fun-pointer-lowtag
)
100 (ash ,index sb
!vm
:word-shift
)))))
101 (loop for i from
1 to physical-len
102 do
(setf (word copy i
) (word closure i
)))
103 (setf (word copy
(1+ physical-len
)) *closure-name-marker
*)))
106 ;; Rename a closure. Doing so changes its identity unless it was already named.
107 ;; To do this without allocating a new closure, we'd need an interface that
108 ;; requests a placeholder from the outset. One possibility is that
109 ;; (NAMED-LAMBDA NIL (x) ...) would allocate the name, initially stored as nil.
110 ;; In that case, the simple-fun's debug-info could also contain a bit that
111 ;; indicates that all closures over it are named, eliminating the storage
112 ;; and check for *closure-name-marker* in the closure values.
113 (defun set-closure-name (closure new-name
)
114 (declare (closure closure
))
115 (unless (nth-value 1 (closure-name closure
))
116 (setq closure
(nameify-closure closure
)))
117 ;; There are no closure slot setters, and in fact SLOT-SET
118 ;; does not exist in a variant that takes a non-constant index.
119 (with-pinned-objects (closure)
120 (setf (sap-ref-lispobj (int-sap (get-lisp-obj-address closure
))
121 (+ (- sb
!vm
:fun-pointer-lowtag
)
122 (ash (get-closure-length closure
)
127 ;;; a SETFable function to return the associated debug name for FUN
128 ;;; (i.e., the third value returned from CL:FUNCTION-LAMBDA-EXPRESSION),
129 ;;; or NIL if there's none
130 (defun %fun-name
(function)
131 (case (fun-subtype function
)
132 (#.sb
!vm
:funcallable-instance-header-widetag
133 (let ((layout (%funcallable-instance-layout function
)))
134 ;; We know that funcallable-instance-p is true,
135 ;; and so testing via TYPEP would be wasteful.
137 ((eq layout
#.
(find-layout 'sb
!eval
:interpreted-function
))
138 (return-from %fun-name
139 (sb!eval
:interpreted-function-debug-name function
)))
141 ((eq layout
#.
(find-layout 'sb
!interpreter
:interpreted-function
))
142 (return-from %fun-name
143 (sb!interpreter
:proto-fn-name
144 (sb!interpreter
:fun-proto-fn
145 (truly-the sb
!interpreter
:interpreted-function function
)))))
146 ;; Avoid fetching the layout again since we already have it.
147 ((classoid-cell-typep layout
148 #.
(find-classoid-cell 'standard-generic-function
)
150 (return-from %fun-name
151 (sb!mop
:generic-function-name function
))))))
152 (#.sb
!vm
:closure-header-widetag
153 (multiple-value-bind (name namedp
) (closure-name function
)
155 (return-from %fun-name name
)))))
156 (%simple-fun-name
(%fun-fun function
)))
158 (defun (setf %fun-name
) (new-value function
)
161 (sb!eval
:interpreted-function
162 (setf (sb!eval
:interpreted-function-debug-name function
) new-value
))
164 (sb!interpreter
:interpreted-function
165 (setf (sb!interpreter
:proto-fn-name
(sb!interpreter
:fun-proto-fn function
))
168 ;; STANDARD-GENERIC-FUNCTION definitely has a NAME,
169 ;; but other subtypes of GENERIC-FUNCTION could as well.
170 (when (slot-exists-p function
'sb
!pcl
::name
)
171 (setf (slot-value function
'sb
!pcl
::name
) new-value
)))
172 ;; This does not set the name of an un-named closure because doing so
173 ;; is not a side-effecting operation that it ought to be.
174 ;; In contrast, SB-PCL::SET-FUN-NAME specifically says that only if the
175 ;; argument fun is a funcallable instance must it retain its identity.
176 ;; That function *is* allowed to cons a new closure to name it.
177 ((or simple-fun closure
)
178 (if (and (closurep function
) (nth-value 1 (closure-name function
)))
179 (set-closure-name function new-value
)
180 (setf (%simple-fun-name
(%fun-fun function
)) new-value
))))
183 (defun %fun-doc
(function)
186 (sb!interpreter
:interpreted-function
187 (sb!interpreter
:proto-fn-docstring
(sb!interpreter
:fun-proto-fn function
)))
189 (sb!eval
:interpreted-function
190 (sb!eval
:interpreted-function-documentation function
))
192 (when (closurep function
)
193 (multiple-value-bind (name namedp
) (closure-name function
)
195 (return-from %fun-doc
(random-documentation name
'function
)))))
196 (%simple-fun-doc
(%fun-fun function
)))))
198 (defun (setf %fun-doc
) (new-value function
)
199 (declare (type (or null string
) new-value
))
202 (sb!interpreter
:interpreted-function
203 (setf (sb!interpreter
:proto-fn-docstring
204 (sb!interpreter
:fun-proto-fn function
)) new-value
))
206 (sb!eval
:interpreted-function
207 (setf (sb!eval
:interpreted-function-documentation function
) new-value
))
208 ((or simple-fun closure
)
209 (when (closurep function
)
210 (multiple-value-bind (name namedp
) (closure-name function
)
212 (return-from %fun-doc
213 (setf (random-documentation name
'function
) new-value
)))))
214 (setf (%simple-fun-doc
(%fun-fun function
)) new-value
)))
217 (defun code-n-unboxed-data-words (code-obj)
218 ;; If the number of boxed words (from the header) is not the same as
219 ;; the displacement backwards from the first simple-fun to the header,
220 ;; then there are unboxed constants between the end of the boxed constants
221 ;; and the first simple-fun.
222 (let ((f (%code-entry-points code-obj
)))
224 (let ((from (get-header-data code-obj
))
225 (to (ash (with-pinned-objects (f)
226 (sap-ref-word (int-sap (get-lisp-obj-address f
))
227 (- sb
!vm
:fun-pointer-lowtag
)))
228 (- sb
!vm
:n-widetag-bits
))))
229 (and (< from to
) (- to from
))))
232 ;;; various environment inquiries
235 '#.
(sort (copy-list sb-cold
:*shebang-features
*) #'string
<)
237 "a list of symbols that describe features provided by the
240 (defun machine-instance ()
242 "Return a string giving the name of the local machine."
243 #!+win32
(sb!win32
::get-computer-name
)
244 #!-win32
(truly-the simple-string
(sb!unix
:unix-gethostname
)))
246 (defvar *machine-version
*)
248 (defun machine-version ()
250 "Return a string describing the version of the computer hardware we
251 are running on, or NIL if we can't find any useful information."
252 (unless (boundp '*machine-version
*)
253 (setf *machine-version
* (get-machine-version)))
256 ;;; FIXME: Don't forget to set these in a sample site-init file.
257 ;;; FIXME: Perhaps the functions could be SETFable instead of having the
258 ;;; interface be through special variables? As far as I can tell
259 ;;; from ANSI 11.1.2.1.1 "Constraints on the COMMON-LISP Package
260 ;;; for Conforming Implementations" it is kosher to add a SETF function for
261 ;;; a symbol in COMMON-LISP..
262 (defvar *short-site-name
* nil
264 "The value of SHORT-SITE-NAME.")
265 (defvar *long-site-name
* nil
266 #!+sb-doc
"the value of LONG-SITE-NAME")
267 (defun short-site-name ()
269 "Return a string with the abbreviated site name, or NIL if not known."
271 (defun long-site-name ()
273 "Return a string with the long form of the site name, or NIL if not known."
277 (defvar *ed-functions
* nil
279 "See function documentation for ED.")
281 (defun ed (&optional x
)
283 "Starts the editor (on a file or a function if named). Functions
284 from the list *ED-FUNCTIONS* are called in order with X as an argument
285 until one of them returns non-NIL; these functions are responsible for
286 signalling a FILE-ERROR to indicate failure to perform an operation on
288 (dolist (fun *ed-functions
*
289 (error 'extension-failure
290 :format-control
"Don't know how to ~S ~A"
291 :format-arguments
(list 'ed x
)
292 :references
(list '(:sbcl
:variable
*ed-functions
*))))
293 (when (funcall fun x
)
298 ;;; Each time we start dribbling to a new stream, we put it in
299 ;;; *DRIBBLE-STREAM*, and push a list of *DRIBBLE-STREAM*, *STANDARD-INPUT*,
300 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* in *PREVIOUS-DRIBBLE-STREAMS*.
301 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* is changed to a broadcast stream that
302 ;;; broadcasts to *DRIBBLE-STREAM* and to the old values of the variables.
303 ;;; *STANDARD-INPUT* is changed to an echo stream that echos input from the old
304 ;;; value of standard input to *DRIBBLE-STREAM*.
306 ;;; When dribble is called with no arguments, *DRIBBLE-STREAM* is closed,
307 ;;; and the values of *DRIBBLE-STREAM*, *STANDARD-INPUT*, and
308 ;;; *STANDARD-OUTPUT* are popped from *PREVIOUS-DRIBBLE-STREAMS*.
310 (defvar *previous-dribble-streams
* nil
)
311 (defvar *dribble-stream
* nil
)
313 (defun dribble (&optional pathname
&key
(if-exists :append
))
315 "With a file name as an argument, dribble opens the file and sends a
316 record of further I/O to that file. Without an argument, it closes
317 the dribble file, and quits logging."
319 (let* ((new-dribble-stream
323 :if-does-not-exist
:create
))
325 (make-broadcast-stream *standard-output
* new-dribble-stream
))
327 (make-broadcast-stream *error-output
* new-dribble-stream
))
329 (make-echo-stream *standard-input
* new-dribble-stream
)))
330 (push (list *dribble-stream
* *standard-input
* *standard-output
*
332 *previous-dribble-streams
*)
333 (setf *dribble-stream
* new-dribble-stream
)
334 (setf *standard-input
* new-standard-input
)
335 (setf *standard-output
* new-standard-output
)
336 (setf *error-output
* new-error-output
)))
337 ((null *dribble-stream
*)
338 (error "not currently dribbling"))
340 (let ((old-streams (pop *previous-dribble-streams
*)))
341 (close *dribble-stream
*)
342 (setf *dribble-stream
* (first old-streams
))
343 (setf *standard-input
* (second old-streams
))
344 (setf *standard-output
* (third old-streams
))
345 (setf *error-output
* (fourth old-streams
)))))
348 (defun %byte-blt
(src src-start dst dst-start dst-end
)
349 (%byte-blt src src-start dst dst-start dst-end
))
351 ;;;; some *LOAD-FOO* variables
353 (defvar *load-print
* nil
355 "the default for the :PRINT argument to LOAD")
357 (defvar *load-verbose
* nil
358 ;; Note that CMU CL's default for this was T, and ANSI says it's
359 ;; implementation-dependent. We choose NIL on the theory that it's
360 ;; a nicer default behavior for Unix programs.
362 "the default for the :VERBOSE argument to LOAD")