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 ((classoid-cell-typep #.
(find-classoid-cell 'standard-generic-function
)
148 (return-from %fun-name
149 (sb!mop
:generic-function-name function
))))))
150 (#.sb
!vm
:closure-header-widetag
151 (multiple-value-bind (name namedp
) (closure-name function
)
153 (return-from %fun-name name
)))))
154 (%simple-fun-name
(%fun-fun function
)))
156 (defun (setf %fun-name
) (new-value function
)
159 (sb!eval
:interpreted-function
160 (setf (sb!eval
:interpreted-function-debug-name function
) new-value
))
162 (sb!interpreter
:interpreted-function
163 (setf (sb!interpreter
:proto-fn-name
(sb!interpreter
:fun-proto-fn function
))
166 ;; STANDARD-GENERIC-FUNCTION definitely has a NAME,
167 ;; but other subtypes of GENERIC-FUNCTION could as well.
168 (when (slot-exists-p function
'sb
!pcl
::name
)
169 (setf (slot-value function
'sb
!pcl
::name
) new-value
)))
170 ;; This does not set the name of an un-named closure because doing so
171 ;; is not a side-effecting operation that it ought to be.
172 ;; In contrast, SB-PCL::SET-FUN-NAME specifically says that only if the
173 ;; argument fun is a funcallable instance must it retain its identity.
174 ;; That function *is* allowed to cons a new closure to name it.
175 ((or simple-fun closure
)
176 (if (and (closurep function
) (nth-value 1 (closure-name function
)))
177 (set-closure-name function new-value
)
178 (setf (%simple-fun-name
(%fun-fun function
)) new-value
))))
181 (defun %fun-doc
(function)
184 (sb!interpreter
:interpreted-function
185 (sb!interpreter
:proto-fn-docstring
(sb!interpreter
:fun-proto-fn function
)))
187 (sb!eval
:interpreted-function
188 (sb!eval
:interpreted-function-documentation function
))
190 (when (closurep function
)
191 (multiple-value-bind (name namedp
) (closure-name function
)
193 (return-from %fun-doc
(random-documentation name
'function
)))))
194 (%simple-fun-doc
(%fun-fun function
)))))
196 (defun (setf %fun-doc
) (new-value function
)
197 (declare (type (or null string
) new-value
))
200 (sb!interpreter
:interpreted-function
201 (setf (sb!interpreter
:proto-fn-docstring
202 (sb!interpreter
:fun-proto-fn function
)) new-value
))
204 (sb!eval
:interpreted-function
205 (setf (sb!eval
:interpreted-function-documentation function
) new-value
))
206 ((or simple-fun closure
)
207 (when (closurep function
)
208 (multiple-value-bind (name namedp
) (closure-name function
)
210 (return-from %fun-doc
211 (setf (random-documentation name
'function
) new-value
)))))
212 (setf (%simple-fun-doc
(%fun-fun function
)) new-value
)))
215 (defun code-n-unboxed-data-words (code-obj)
216 ;; If the number of boxed words (from the header) is not the same as
217 ;; the displacement backwards from the first simple-fun to the header,
218 ;; then there are unboxed constants between the end of the boxed constants
219 ;; and the first simple-fun.
220 (let ((f (%code-entry-points code-obj
)))
222 (let ((from (code-header-words code-obj
))
223 (to (ash (with-pinned-objects (f)
224 (sap-ref-word (int-sap (get-lisp-obj-address f
))
225 (- sb
!vm
:fun-pointer-lowtag
)))
226 (- sb
!vm
:n-widetag-bits
))))
227 (and (< from to
) (- to from
))))
230 ;;; various environment inquiries
233 '#.
(sort (copy-list sb-cold
:*shebang-features
*) #'string
<)
235 "a list of symbols that describe features provided by the
238 (defun machine-instance ()
240 "Return a string giving the name of the local machine."
241 #!+win32
(sb!win32
::get-computer-name
)
242 #!-win32
(truly-the simple-string
(sb!unix
:unix-gethostname
)))
244 (declaim (type (or null string
) *machine-version
*))
245 (defvar *machine-version
*)
247 (defun machine-version ()
249 "Return a string describing the version of the computer hardware we
250 are running on, or NIL if we can't find any useful information."
251 (unless (boundp '*machine-version
*)
252 (setf *machine-version
* (get-machine-version)))
255 ;;; FIXME: Don't forget to set these in a sample site-init file.
256 ;;; FIXME: Perhaps the functions could be SETFable instead of having the
257 ;;; interface be through special variables? As far as I can tell
258 ;;; from ANSI 11.1.2.1.1 "Constraints on the COMMON-LISP Package
259 ;;; for Conforming Implementations" it is kosher to add a SETF function for
260 ;;; a symbol in COMMON-LISP..
261 (declaim (type (or null string
) *short-site-name
* *long-site-name
*))
262 (defvar *short-site-name
* nil
264 "The value of SHORT-SITE-NAME.")
265 (defvar *long-site-name
* nil
267 "The value of LONG-SITE-NAME.")
268 (defun short-site-name ()
270 "Return a string with the abbreviated site name, or NIL if not known."
272 (defun long-site-name ()
274 "Return a string with the long form of the site name, or NIL if not known."
278 (declaim (type list
*ed-functions
*))
279 (defvar *ed-functions
* '()
281 "See function documentation for ED.")
283 (defun ed (&optional x
)
285 "Starts the editor (on a file or a function if named). Functions
286 from the list *ED-FUNCTIONS* are called in order with X as an argument
287 until one of them returns non-NIL; these functions are responsible for
288 signalling a FILE-ERROR to indicate failure to perform an operation on
290 (dolist (fun *ed-functions
*
291 (error 'extension-failure
292 :format-control
"Don't know how to ~S ~A"
293 :format-arguments
(list 'ed x
)
294 :references
(list '(:sbcl
:variable
*ed-functions
*))))
295 (when (funcall fun x
)
300 ;;; Each time we start dribbling to a new stream, we put it in
301 ;;; *DRIBBLE-STREAM*, and push a list of *DRIBBLE-STREAM*, *STANDARD-INPUT*,
302 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* in *PREVIOUS-DRIBBLE-STREAMS*.
303 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* is changed to a broadcast stream that
304 ;;; broadcasts to *DRIBBLE-STREAM* and to the old values of the variables.
305 ;;; *STANDARD-INPUT* is changed to an echo stream that echos input from the old
306 ;;; value of standard input to *DRIBBLE-STREAM*.
308 ;;; When dribble is called with no arguments, *DRIBBLE-STREAM* is closed,
309 ;;; and the values of *DRIBBLE-STREAM*, *STANDARD-INPUT*, and
310 ;;; *STANDARD-OUTPUT* are popped from *PREVIOUS-DRIBBLE-STREAMS*.
312 (defvar *previous-dribble-streams
* '())
313 (defvar *dribble-stream
* nil
)
315 (defun dribble (&optional pathname
&key
(if-exists :append
))
317 "With a file name as an argument, dribble opens the file and sends a
318 record of further I/O to that file. Without an argument, it closes
319 the dribble file, and quits logging."
320 (flet ((install-streams (dribble input output error
)
321 (setf *dribble-stream
* dribble
322 *standard-input
* input
323 *standard-output
* output
324 *error-output
* error
)))
326 (push (list *dribble-stream
* *standard-input
* *standard-output
*
328 *previous-dribble-streams
*)
329 (let ((new-dribble (open pathname
332 :if-does-not-exist
:create
)))
335 (make-echo-stream *standard-input
* new-dribble
)
336 (make-broadcast-stream *standard-output
* new-dribble
)
337 (make-broadcast-stream *error-output
* new-dribble
))))
338 ((null *dribble-stream
*)
339 (error "not currently dribbling"))
341 (close *dribble-stream
*)
342 (apply #'install-streams
(pop *previous-dribble-streams
*)))))
345 (defun %byte-blt
(src src-start dst dst-start dst-end
)
346 (%byte-blt src src-start dst dst-start dst-end
))
348 ;;;; some *LOAD-FOO* variables
350 (defvar *load-print
* nil
352 "the default for the :PRINT argument to LOAD")
354 (defvar *load-verbose
* nil
355 ;; Note that CMU CL's default for this was T, and ANSI says it's
356 ;; implementation-dependent. We choose NIL on the theory that it's
357 ;; a nicer default behavior for Unix programs.
359 "the default for the :VERBOSE argument to LOAD")