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-widetag
24 (%closure-fun function
))
25 (#.sb
!vm
:funcallable-instance-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 (defconstant +closure-header-namedp
+ #x800000
)
67 (macrolet ((closure-header-word (closure)
68 `(sap-ref-word (int-sap (get-lisp-obj-address ,closure
))
69 (- sb
!vm
:fun-pointer-lowtag
))))
70 (defun closure-name (closure)
71 (declare (closure closure
))
72 (if (logtest (with-pinned-objects (closure) (closure-header-word closure
))
73 +closure-header-namedp
+)
74 ;; GET-CLOSURE-LENGTH counts the 'fun' slot
75 (values (%closure-index-ref closure
(- (get-closure-length closure
) 2)) t
)
78 ;; Return a new object that has 1 more slot than CLOSURE,
79 ;; and frob its header bit signifying that it is named.
80 (defun nameify-closure (closure)
81 (declare (closure closure
))
82 (let* ((n-words (get-closure-length closure
)) ; excluding header
83 ;; N-WORDS includes the trampoline, so the number of slots we would
84 ;; pass to %COPY-CLOSURE is 1 less than that, were it not for
85 ;; the fact that we actually want to create 1 additional slot.
86 ;; So in effect, asking for N-WORDS does exactly the right thing.
87 (copy #!-
(or x86 x86-64
)
88 (sb!vm
::%copy-closure n-words
(%closure-fun closure
))
90 (with-pinned-objects ((%closure-fun closure
))
91 ;; %CLOSURE-CALLEE manifests as a fixnum which remains
92 ;; valid across GC due to %CLOSURE-FUN being pinned
93 ;; until after the new closure is made.
94 (sb!vm
::%copy-closure n-words
(sb!vm
::%closure-callee closure
)))))
95 (with-pinned-objects (copy)
96 (loop with sap
= (int-sap (get-lisp-obj-address copy
))
97 for i from
0 below
(1- n-words
)
98 for ofs from
(- (ash 2 sb
!vm
:word-shift
) sb
!vm
:fun-pointer-lowtag
)
100 do
(setf (sap-ref-lispobj sap ofs
) (%closure-index-ref closure i
)))
101 (setf (closure-header-word copy
) ; Update the header
102 (logior #!+immobile-space
(ash sb
!vm
:function-layout
32)
103 +closure-header-namedp
+
104 (closure-header-word copy
))))
107 ;; Rename a closure. Doing so changes its identity unless it was already named.
108 (defun set-closure-name (closure new-name
)
109 (declare (closure closure
))
110 (unless (logtest (with-pinned-objects (closure) (closure-header-word closure
))
111 +closure-header-namedp
+)
112 (setq closure
(nameify-closure closure
)))
113 ;; There are no closure slot setters, and in fact SLOT-SET
114 ;; does not exist in a variant that takes a non-constant index.
115 (with-pinned-objects (closure)
116 (setf (sap-ref-lispobj (int-sap (get-lisp-obj-address closure
))
117 (- (ash (get-closure-length closure
) sb
!vm
:word-shift
)
118 sb
!vm
:fun-pointer-lowtag
)) new-name
))
121 ;;; a SETFable function to return the associated debug name for FUN
122 ;;; (i.e., the third value returned from CL:FUNCTION-LAMBDA-EXPRESSION),
123 ;;; or NIL if there's none
124 (defun %fun-name
(function)
125 (case (fun-subtype function
)
126 (#.sb
!vm
:funcallable-instance-widetag
127 (let (#!+(or sb-eval sb-fasteval
)
128 (layout (%funcallable-instance-layout function
)))
129 ;; We know that funcallable-instance-p is true,
130 ;; and so testing via TYPEP would be wasteful.
132 ((eq layout
#.
(find-layout 'sb
!eval
:interpreted-function
))
133 (return-from %fun-name
134 (sb!eval
:interpreted-function-debug-name function
)))
136 ((eq layout
#.
(find-layout 'sb
!interpreter
:interpreted-function
))
137 (return-from %fun-name
138 (sb!interpreter
:proto-fn-name
139 (sb!interpreter
:fun-proto-fn
140 (truly-the sb
!interpreter
:interpreted-function function
)))))
141 ((classoid-cell-typep #.
(find-classoid-cell 'standard-generic-function
)
143 (return-from %fun-name
144 (sb!mop
:generic-function-name function
))))))
145 (#.sb
!vm
:closure-widetag
146 (multiple-value-bind (name namedp
) (closure-name function
)
148 (return-from %fun-name name
)))))
149 (%simple-fun-name
(%fun-fun function
)))
151 (defun (setf %fun-name
) (new-value function
)
154 (sb!eval
:interpreted-function
155 (setf (sb!eval
:interpreted-function-debug-name function
) new-value
))
157 (sb!interpreter
:interpreted-function
158 (setf (sb!interpreter
:proto-fn-name
(sb!interpreter
:fun-proto-fn function
))
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
:proto-fn-docstring
(sb!interpreter
:fun-proto-fn 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 (setf (sb!interpreter
:proto-fn-docstring
197 (sb!interpreter
:fun-proto-fn function
)) new-value
))
199 (sb!eval
:interpreted-function
200 (setf (sb!eval
:interpreted-function-documentation function
) new-value
))
201 ((or simple-fun closure
)
202 (when (closurep function
)
203 (multiple-value-bind (name namedp
) (closure-name function
)
205 (return-from %fun-doc
206 (setf (random-documentation name
'function
) new-value
)))))
207 (setf (%simple-fun-doc
(%fun-fun function
)) new-value
)))
210 (defun code-n-entries (code-obj)
211 ;; The internal %n-entries slot is a fixnum storing the number
212 ;; of simple-funs in the low 14 bits (16 bits of the machine word),
213 ;; and the first function's offset in the high 16 bits.
214 #!-
64-bit
(ldb (byte 14 0) (sb!vm
::%code-n-entries code-obj
))
215 ;; The header stores the count.
216 #!+64-bit
(ldb (byte 16 24) (get-header-data code-obj
)))
218 (defun %code-entry-point
(code-obj fun-index
)
219 (declare (type (unsigned-byte 16) fun-index
))
220 (if (>= fun-index
(code-n-entries code-obj
))
222 (%primitive sb
!c
:compute-fun
224 (cond ((zerop fun-index
) ; special case for the first simple-fun
225 #!-
64-bit
(ldb (byte 16 14) (sb!vm
::%code-n-entries code-obj
))
226 #!+64-bit
(ldb (byte 16 40) (get-header-data code-obj
)))
228 (let ((i (+ (- sb
!vm
:other-pointer-lowtag
)
229 (ash (code-header-words code-obj
)
231 (ash (1- fun-index
) 2))))
232 (with-pinned-objects (code-obj)
233 (sap-ref-32 (int-sap (get-lisp-obj-address code-obj
))
236 (defun code-entry-points (code-obj)
237 (let ((a (make-array (code-n-entries code-obj
))))
238 (dotimes (i (length a
) a
)
239 (setf (aref a i
) (%code-entry-point code-obj i
)))))
241 (defun code-n-unboxed-data-words (code-obj)
242 ;; If the number of boxed words (from the header) is not the same as
243 ;; the displacement backwards from the first simple-fun to the header,
244 ;; then there are unboxed constants between the end of the boxed constants
245 ;; and the first simple-fun.
246 (let ((f (%code-entry-point code-obj
0)))
248 (let ((from (code-header-words code-obj
))
249 ;; Ignore the layout pointer (if present) in the upper bits
250 ;; of the function header.
251 (to (ldb (byte 24 sb
!vm
:n-widetag-bits
)
252 (with-pinned-objects (f)
253 (sap-ref-word (int-sap (get-lisp-obj-address f
))
254 (- sb
!vm
:fun-pointer-lowtag
))))))
255 (and (< from to
) (- to from
))))
258 ;;; various environment inquiries
261 '#.
(sort (copy-list sb-cold
:*shebang-features
*) #'string
<)
262 "a list of symbols that describe features provided by the
265 (defun machine-instance ()
266 "Return a string giving the name of the local machine."
267 #!+win32
(sb!win32
::get-computer-name
)
268 #!-win32
(truly-the simple-string
(sb!unix
:unix-gethostname
)))
270 (declaim (type (or null string
) *machine-version
*))
271 (defvar *machine-version
*)
273 (defun machine-version ()
274 "Return a string describing the version of the computer hardware we
275 are running on, or NIL if we can't find any useful information."
276 (unless (boundp '*machine-version
*)
277 (setf *machine-version
* (get-machine-version)))
280 ;;; FIXME: Don't forget to set these in a sample site-init file.
281 ;;; FIXME: Perhaps the functions could be SETFable instead of having the
282 ;;; interface be through special variables? As far as I can tell
283 ;;; from ANSI 11.1.2.1.1 "Constraints on the COMMON-LISP Package
284 ;;; for Conforming Implementations" it is kosher to add a SETF function for
285 ;;; a symbol in COMMON-LISP..
286 (declaim (type (or null string
) *short-site-name
* *long-site-name
*))
287 (defvar *short-site-name
* nil
288 "The value of SHORT-SITE-NAME.")
289 (defvar *long-site-name
* nil
290 "The value of LONG-SITE-NAME.")
291 (defun short-site-name ()
292 "Return a string with the abbreviated site name, or NIL if not known."
294 (defun long-site-name ()
295 "Return a string with the long form of the site name, or NIL if not known."
299 (declaim (type list
*ed-functions
*))
300 (defvar *ed-functions
* '()
301 "See function documentation for ED.")
303 (defun ed (&optional x
)
304 "Starts the editor (on a file or a function if named). Functions
305 from the list *ED-FUNCTIONS* are called in order with X as an argument
306 until one of them returns non-NIL; these functions are responsible for
307 signalling a FILE-ERROR to indicate failure to perform an operation on
309 (dolist (fun *ed-functions
*
310 (error 'extension-failure
311 :format-control
"Don't know how to ~S ~A"
312 :format-arguments
(list 'ed x
)
313 :references
(list '(:sbcl
:variable
*ed-functions
*))))
314 (when (funcall fun x
)
319 ;;; Each time we start dribbling to a new stream, we put it in
320 ;;; *DRIBBLE-STREAM*, and push a list of *DRIBBLE-STREAM*, *STANDARD-INPUT*,
321 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* in *PREVIOUS-DRIBBLE-STREAMS*.
322 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* is changed to a broadcast stream that
323 ;;; broadcasts to *DRIBBLE-STREAM* and to the old values of the variables.
324 ;;; *STANDARD-INPUT* is changed to an echo stream that echos input from the old
325 ;;; value of standard input to *DRIBBLE-STREAM*.
327 ;;; When dribble is called with no arguments, *DRIBBLE-STREAM* is closed,
328 ;;; and the values of *DRIBBLE-STREAM*, *STANDARD-INPUT*, and
329 ;;; *STANDARD-OUTPUT* are popped from *PREVIOUS-DRIBBLE-STREAMS*.
331 (defvar *previous-dribble-streams
* '())
332 (defvar *dribble-stream
* nil
)
334 (defun dribble (&optional pathname
&key
(if-exists :append
))
335 "With a file name as an argument, dribble opens the file and sends a
336 record of further I/O to that file. Without an argument, it closes
337 the dribble file, and quits logging."
338 (flet ((install-streams (dribble input output error
)
339 (setf *dribble-stream
* dribble
340 *standard-input
* input
341 *standard-output
* output
342 *error-output
* error
)))
344 (push (list *dribble-stream
* *standard-input
* *standard-output
*
346 *previous-dribble-streams
*)
347 (let ((new-dribble (open pathname
350 :if-does-not-exist
:create
)))
353 (make-echo-stream *standard-input
* new-dribble
)
354 (make-broadcast-stream *standard-output
* new-dribble
)
355 (make-broadcast-stream *error-output
* new-dribble
))))
356 ((null *dribble-stream
*)
357 (error "not currently dribbling"))
359 (close *dribble-stream
*)
360 (apply #'install-streams
(pop *previous-dribble-streams
*)))))
363 (defun %byte-blt
(src src-start dst dst-start dst-end
)
364 (%byte-blt src src-start dst dst-start dst-end
))
366 ;;;; some *LOAD-FOO* variables
368 (defvar *load-print
* nil
369 "the default for the :PRINT argument to LOAD")
371 (defvar *load-verbose
* nil
372 ;; Note that CMU CL's default for this was T, and ANSI says it's
373 ;; implementation-dependent. We choose NIL on the theory that it's
374 ;; a nicer default behavior for Unix programs.
375 "the default for the :VERBOSE argument to LOAD")