Remove remnants of non-interleavable raw slot spport.
[sbcl.git] / src / code / target-misc.lisp
blob23f62ff77b936656d2eac00384eff391a0d27998
1 ;;;; Environment query functions, DOCUMENTATION and DRIBBLE.
2 ;;;;
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
7 ;;;; more information.
8 ;;;;
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
24 function)
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)
34 (typecase function
35 #!+sb-fasteval
36 (sb!interpreter:interpreted-function
37 (sb!interpreter:fun-pretty-arglist function))
38 #!+sb-eval
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)
45 (typecase function
46 #!+sb-fasteval
47 (sb!interpreter:interpreted-function
48 (sb!interpreter:set-fun-pretty-arglist function new-value))
49 #!+sb-eval
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)))
55 new-value)
57 (defun %fun-type (function)
58 (typecase function
59 #!+sb-fasteval
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)))
69 (if (and (>= len 4)
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)
77 (values nil nil))))
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*)))
103 copy))
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)
122 sb!vm:word-shift)))
123 new-name))
124 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.
128 (defun fun-name (x)
129 (if (typep x 'standard-generic-function)
130 (sb!mop:generic-function-name x)
131 (%fun-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)
137 (typecase function
138 #!+sb-eval
139 (sb!eval:interpreted-function
140 (sb!eval:interpreted-function-debug-name function))
141 #!+sb-fasteval
142 (sb!interpreter:interpreted-function (sb!interpreter:fun-name function))
144 (let (name namedp)
145 (if (and (closurep function)
146 (progn
147 (multiple-value-setq (name namedp) (closure-name function))
148 namedp))
149 name
150 (%simple-fun-name (%fun-fun function)))))))
152 (defun (setf %fun-name) (new-value function)
153 (typecase function
154 #!+sb-eval
155 (sb!eval:interpreted-function
156 (setf (sb!eval:interpreted-function-debug-name function) new-value))
157 #!+sb-fasteval
158 (sb!interpreter:interpreted-function
159 (sb!interpreter:set-fun-name function new-value))
160 (generic-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))))
174 new-value)
176 (defun %fun-doc (function)
177 (typecase function
178 #!+sb-fasteval
179 (sb!interpreter:interpreted-function
180 (sb!interpreter:fun-docstring function))
181 #!+sb-eval
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)
187 (when namedp
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))
193 (typecase function
194 #!+sb-fasteval
195 (sb!interpreter:interpreted-function
196 (sb!interpreter:set-fun-docstring function new-value))
197 #!+sb-eval
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)
203 (when namedp
204 (return-from %fun-doc
205 (setf (random-documentation name 'function) new-value)))))
206 (setf (%simple-fun-doc (%fun-fun function)) new-value)))
207 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)))
215 (or (and f
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))))
222 0)))
224 ;;; various environment inquiries
226 (defvar *features*
227 '#.(sort (copy-list sb-cold:*shebang-features*) #'string<)
228 #!+sb-doc
229 "a list of symbols that describe features provided by the
230 implementation")
232 (defun machine-instance ()
233 #!+sb-doc
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 ()
241 #!+sb-doc
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)))
246 *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
255 #!+sb-doc
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 ()
260 #!+sb-doc
261 "Return a string with the abbreviated site name, or NIL if not known."
262 *short-site-name*)
263 (defun long-site-name ()
264 #!+sb-doc
265 "Return a string with the long form of the site name, or NIL if not known."
266 *long-site-name*)
268 ;;;; ED
269 (defvar *ed-functions* nil
270 #!+sb-doc
271 "See function documentation for ED.")
273 (defun ed (&optional x)
274 #!+sb-doc
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
279 the file system."
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)
286 (return t))))
288 ;;;; dribble stuff
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))
306 #!+sb-doc
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."
310 (cond (pathname
311 (let* ((new-dribble-stream
312 (open pathname
313 :direction :output
314 :if-exists if-exists
315 :if-does-not-exist :create))
316 (new-standard-output
317 (make-broadcast-stream *standard-output* new-dribble-stream))
318 (new-error-output
319 (make-broadcast-stream *error-output* new-dribble-stream))
320 (new-standard-input
321 (make-echo-stream *standard-input* new-dribble-stream)))
322 (push (list *dribble-stream* *standard-input* *standard-output*
323 *error-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)))))
338 (values))
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
346 #!+sb-doc
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.
353 #!+sb-doc
354 "the default for the :VERBOSE argument to LOAD")