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-entries (code-obj)
216 ;; The internal %n-entries slot is a fixnum storing the number
217 ;; of simple-funs in the low 14 bits (16 bits of the machine word),
218 ;; and the first function's offset in the high 16 bits.
219 #!-
64-bit
(ldb (byte 14 0) (sb!vm
::%code-n-entries code-obj
))
220 ;; The header stores the count.
221 #!+64-bit
(ldb (byte 16 24) (get-header-data code-obj
)))
223 (defun %code-entry-point
(code-obj fun-index
)
224 (declare (type (unsigned-byte 16) fun-index
))
225 (if (>= fun-index
(code-n-entries code-obj
))
227 (%primitive sb
!c
:compute-fun
229 (cond ((zerop fun-index
) ; special case for the first simple-fun
230 #!-
64-bit
(ldb (byte 16 14) (sb!vm
::%code-n-entries code-obj
))
231 #!+64-bit
(ldb (byte 16 40) (get-header-data code-obj
)))
233 (let ((i (+ (- sb
!vm
:other-pointer-lowtag
)
234 (ash (code-header-words code-obj
)
236 (ash (1- fun-index
) 2))))
237 (with-pinned-objects (code-obj)
238 (sap-ref-32 (int-sap (get-lisp-obj-address code-obj
))
241 (defun code-entry-points (code-obj)
242 (let ((a (make-array (code-n-entries code-obj
))))
243 (dotimes (i (length a
) a
)
244 (setf (aref a i
) (%code-entry-point code-obj i
)))))
246 (defun code-n-unboxed-data-words (code-obj)
247 ;; If the number of boxed words (from the header) is not the same as
248 ;; the displacement backwards from the first simple-fun to the header,
249 ;; then there are unboxed constants between the end of the boxed constants
250 ;; and the first simple-fun.
251 (let ((f (%code-entry-point code-obj
0)))
253 (let ((from (code-header-words code-obj
))
254 (to (ash (with-pinned-objects (f)
255 (sap-ref-word (int-sap (get-lisp-obj-address f
))
256 (- sb
!vm
:fun-pointer-lowtag
)))
257 (- sb
!vm
:n-widetag-bits
))))
258 (and (< from to
) (- to from
))))
261 ;;; various environment inquiries
264 '#.
(sort (copy-list sb-cold
:*shebang-features
*) #'string
<)
266 "a list of symbols that describe features provided by the
269 (defun machine-instance ()
271 "Return a string giving the name of the local machine."
272 #!+win32
(sb!win32
::get-computer-name
)
273 #!-win32
(truly-the simple-string
(sb!unix
:unix-gethostname
)))
275 (declaim (type (or null string
) *machine-version
*))
276 (defvar *machine-version
*)
278 (defun machine-version ()
280 "Return a string describing the version of the computer hardware we
281 are running on, or NIL if we can't find any useful information."
282 (unless (boundp '*machine-version
*)
283 (setf *machine-version
* (get-machine-version)))
286 ;;; FIXME: Don't forget to set these in a sample site-init file.
287 ;;; FIXME: Perhaps the functions could be SETFable instead of having the
288 ;;; interface be through special variables? As far as I can tell
289 ;;; from ANSI 11.1.2.1.1 "Constraints on the COMMON-LISP Package
290 ;;; for Conforming Implementations" it is kosher to add a SETF function for
291 ;;; a symbol in COMMON-LISP..
292 (declaim (type (or null string
) *short-site-name
* *long-site-name
*))
293 (defvar *short-site-name
* nil
295 "The value of SHORT-SITE-NAME.")
296 (defvar *long-site-name
* nil
298 "The value of LONG-SITE-NAME.")
299 (defun short-site-name ()
301 "Return a string with the abbreviated site name, or NIL if not known."
303 (defun long-site-name ()
305 "Return a string with the long form of the site name, or NIL if not known."
309 (declaim (type list
*ed-functions
*))
310 (defvar *ed-functions
* '()
312 "See function documentation for ED.")
314 (defun ed (&optional x
)
316 "Starts the editor (on a file or a function if named). Functions
317 from the list *ED-FUNCTIONS* are called in order with X as an argument
318 until one of them returns non-NIL; these functions are responsible for
319 signalling a FILE-ERROR to indicate failure to perform an operation on
321 (dolist (fun *ed-functions
*
322 (error 'extension-failure
323 :format-control
"Don't know how to ~S ~A"
324 :format-arguments
(list 'ed x
)
325 :references
(list '(:sbcl
:variable
*ed-functions
*))))
326 (when (funcall fun x
)
331 ;;; Each time we start dribbling to a new stream, we put it in
332 ;;; *DRIBBLE-STREAM*, and push a list of *DRIBBLE-STREAM*, *STANDARD-INPUT*,
333 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* in *PREVIOUS-DRIBBLE-STREAMS*.
334 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* is changed to a broadcast stream that
335 ;;; broadcasts to *DRIBBLE-STREAM* and to the old values of the variables.
336 ;;; *STANDARD-INPUT* is changed to an echo stream that echos input from the old
337 ;;; value of standard input to *DRIBBLE-STREAM*.
339 ;;; When dribble is called with no arguments, *DRIBBLE-STREAM* is closed,
340 ;;; and the values of *DRIBBLE-STREAM*, *STANDARD-INPUT*, and
341 ;;; *STANDARD-OUTPUT* are popped from *PREVIOUS-DRIBBLE-STREAMS*.
343 (defvar *previous-dribble-streams
* '())
344 (defvar *dribble-stream
* nil
)
346 (defun dribble (&optional pathname
&key
(if-exists :append
))
348 "With a file name as an argument, dribble opens the file and sends a
349 record of further I/O to that file. Without an argument, it closes
350 the dribble file, and quits logging."
351 (flet ((install-streams (dribble input output error
)
352 (setf *dribble-stream
* dribble
353 *standard-input
* input
354 *standard-output
* output
355 *error-output
* error
)))
357 (push (list *dribble-stream
* *standard-input
* *standard-output
*
359 *previous-dribble-streams
*)
360 (let ((new-dribble (open pathname
363 :if-does-not-exist
:create
)))
366 (make-echo-stream *standard-input
* new-dribble
)
367 (make-broadcast-stream *standard-output
* new-dribble
)
368 (make-broadcast-stream *error-output
* new-dribble
))))
369 ((null *dribble-stream
*)
370 (error "not currently dribbling"))
372 (close *dribble-stream
*)
373 (apply #'install-streams
(pop *previous-dribble-streams
*)))))
376 (defun %byte-blt
(src src-start dst dst-start dst-end
)
377 (%byte-blt src src-start dst dst-start dst-end
))
379 ;;;; some *LOAD-FOO* variables
381 (defvar *load-print
* nil
383 "the default for the :PRINT argument to LOAD")
385 (defvar *load-verbose
* nil
386 ;; Note that CMU CL's default for this was T, and ANSI says it's
387 ;; implementation-dependent. We choose NIL on the theory that it's
388 ;; a nicer default behavior for Unix programs.
390 "the default for the :VERBOSE argument to LOAD")