Move poorly-named NWORDS function near its call site
[sbcl.git] / src / code / target-misc.lisp
blob2f2fc063c191a2818d6da6ba0b34bb372d3fce90
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: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)))
30 (t function)))
32 (defun %fun-lambda-list (function)
33 (typecase function
34 #!+sb-fasteval
35 (sb!interpreter:interpreted-function
36 (sb!interpreter:proto-fn-pretty-arglist
37 (sb!interpreter:fun-proto-fn 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 (setf (sb!interpreter:proto-fn-pretty-arglist
49 (sb!interpreter:fun-proto-fn function)) new-value))
50 #!+sb-eval
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)))
56 new-value)
58 (defun %fun-type (function)
59 (typecase function
60 #!+sb-fasteval
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)
76 (values nil nil)))
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))
89 #!+(or x86 x86-64)
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)
99 by sb!vm:n-word-bytes
100 do (setf (sap-ref-lispobj sap ofs) (%closure-index-ref closure i)))
101 (setf (closure-header-word copy) ; Update the header
102 (logior (closure-header-word copy) +closure-header-namedp+)))
103 copy))
105 ;; Rename a closure. Doing so changes its identity unless it was already named.
106 (defun set-closure-name (closure new-name)
107 (declare (closure closure))
108 (unless (logtest (with-pinned-objects (closure) (closure-header-word closure))
109 +closure-header-namedp+)
110 (setq closure (nameify-closure closure)))
111 ;; There are no closure slot setters, and in fact SLOT-SET
112 ;; does not exist in a variant that takes a non-constant index.
113 (with-pinned-objects (closure)
114 (setf (sap-ref-lispobj (int-sap (get-lisp-obj-address closure))
115 (- (ash (get-closure-length closure) sb!vm:word-shift)
116 sb!vm:fun-pointer-lowtag)) new-name))
117 closure))
119 ;;; a SETFable function to return the associated debug name for FUN
120 ;;; (i.e., the third value returned from CL:FUNCTION-LAMBDA-EXPRESSION),
121 ;;; or NIL if there's none
122 (defun %fun-name (function)
123 (case (fun-subtype function)
124 (#.sb!vm:funcallable-instance-header-widetag
125 (let (#!+(or sb-eval sb-fasteval)
126 (layout (%funcallable-instance-layout function)))
127 ;; We know that funcallable-instance-p is true,
128 ;; and so testing via TYPEP would be wasteful.
129 (cond #!+sb-eval
130 ((eq layout #.(find-layout 'sb!eval:interpreted-function))
131 (return-from %fun-name
132 (sb!eval:interpreted-function-debug-name function)))
133 #!+sb-fasteval
134 ((eq layout #.(find-layout 'sb!interpreter:interpreted-function))
135 (return-from %fun-name
136 (sb!interpreter:proto-fn-name
137 (sb!interpreter:fun-proto-fn
138 (truly-the sb!interpreter:interpreted-function function)))))
139 ((classoid-cell-typep #.(find-classoid-cell 'standard-generic-function)
140 function)
141 (return-from %fun-name
142 (sb!mop:generic-function-name function))))))
143 (#.sb!vm:closure-header-widetag
144 (multiple-value-bind (name namedp) (closure-name function)
145 (when namedp
146 (return-from %fun-name name)))))
147 (%simple-fun-name (%fun-fun function)))
149 (defun (setf %fun-name) (new-value function)
150 (typecase function
151 #!+sb-eval
152 (sb!eval:interpreted-function
153 (setf (sb!eval:interpreted-function-debug-name function) new-value))
154 #!+sb-fasteval
155 (sb!interpreter:interpreted-function
156 (setf (sb!interpreter:proto-fn-name (sb!interpreter:fun-proto-fn function))
157 new-value))
158 (generic-function
159 ;; STANDARD-GENERIC-FUNCTION definitely has a NAME,
160 ;; but other subtypes of GENERIC-FUNCTION could as well.
161 (when (slot-exists-p function 'sb!pcl::name)
162 (setf (slot-value function 'sb!pcl::name) new-value)))
163 ;; This does not set the name of an un-named closure because doing so
164 ;; is not a side-effecting operation that it ought to be.
165 ;; In contrast, SB-PCL::SET-FUN-NAME specifically says that only if the
166 ;; argument fun is a funcallable instance must it retain its identity.
167 ;; That function *is* allowed to cons a new closure to name it.
168 ((or simple-fun closure)
169 (if (and (closurep function) (nth-value 1 (closure-name function)))
170 (set-closure-name function new-value)
171 (setf (%simple-fun-name (%fun-fun function)) new-value))))
172 new-value)
174 (defun %fun-doc (function)
175 (typecase function
176 #!+sb-fasteval
177 (sb!interpreter:interpreted-function
178 (sb!interpreter:proto-fn-docstring (sb!interpreter:fun-proto-fn function)))
179 #!+sb-eval
180 (sb!eval:interpreted-function
181 (sb!eval:interpreted-function-documentation function))
183 (when (closurep function)
184 (multiple-value-bind (name namedp) (closure-name function)
185 (when namedp
186 (return-from %fun-doc (random-documentation name 'function)))))
187 (%simple-fun-doc (%fun-fun function)))))
189 (defun (setf %fun-doc) (new-value function)
190 (declare (type (or null string) new-value))
191 (typecase function
192 #!+sb-fasteval
193 (sb!interpreter:interpreted-function
194 (setf (sb!interpreter:proto-fn-docstring
195 (sb!interpreter:fun-proto-fn function)) new-value))
196 #!+sb-eval
197 (sb!eval:interpreted-function
198 (setf (sb!eval:interpreted-function-documentation function) new-value))
199 ((or simple-fun closure)
200 (when (closurep function)
201 (multiple-value-bind (name namedp) (closure-name function)
202 (when namedp
203 (return-from %fun-doc
204 (setf (random-documentation name 'function) new-value)))))
205 (setf (%simple-fun-doc (%fun-fun function)) new-value)))
206 new-value)
208 (defun code-n-entries (code-obj)
209 ;; The internal %n-entries slot is a fixnum storing the number
210 ;; of simple-funs in the low 14 bits (16 bits of the machine word),
211 ;; and the first function's offset in the high 16 bits.
212 #!-64-bit (ldb (byte 14 0) (sb!vm::%code-n-entries code-obj))
213 ;; The header stores the count.
214 #!+64-bit (ldb (byte 16 24) (get-header-data code-obj)))
216 (defun %code-entry-point (code-obj fun-index)
217 (declare (type (unsigned-byte 16) fun-index))
218 (if (>= fun-index (code-n-entries code-obj))
220 (%primitive sb!c:compute-fun
221 code-obj
222 (cond ((zerop fun-index) ; special case for the first simple-fun
223 #!-64-bit (ldb (byte 16 14) (sb!vm::%code-n-entries code-obj))
224 #!+64-bit (ldb (byte 16 40) (get-header-data code-obj)))
226 (let ((i (+ (- sb!vm:other-pointer-lowtag)
227 (ash (code-header-words code-obj)
228 sb!vm:word-shift)
229 (ash (1- fun-index) 2))))
230 (with-pinned-objects (code-obj)
231 (sap-ref-32 (int-sap (get-lisp-obj-address code-obj))
232 i))))))))
234 (defun code-entry-points (code-obj)
235 (let ((a (make-array (code-n-entries code-obj))))
236 (dotimes (i (length a) a)
237 (setf (aref a i) (%code-entry-point code-obj i)))))
239 (defun code-n-unboxed-data-words (code-obj)
240 ;; If the number of boxed words (from the header) is not the same as
241 ;; the displacement backwards from the first simple-fun to the header,
242 ;; then there are unboxed constants between the end of the boxed constants
243 ;; and the first simple-fun.
244 (let ((f (%code-entry-point code-obj 0)))
245 (or (and f
246 (let ((from (code-header-words code-obj))
247 (to (ash (with-pinned-objects (f)
248 (sap-ref-word (int-sap (get-lisp-obj-address f))
249 (- sb!vm:fun-pointer-lowtag)))
250 (- sb!vm:n-widetag-bits))))
251 (and (< from to) (- to from))))
252 0)))
254 ;;; various environment inquiries
256 (defvar *features*
257 '#.(sort (copy-list sb-cold:*shebang-features*) #'string<)
258 "a list of symbols that describe features provided by the
259 implementation")
261 (defun machine-instance ()
262 "Return a string giving the name of the local machine."
263 #!+win32 (sb!win32::get-computer-name)
264 #!-win32 (truly-the simple-string (sb!unix:unix-gethostname)))
266 (declaim (type (or null string) *machine-version*))
267 (defvar *machine-version*)
269 (defun machine-version ()
270 "Return a string describing the version of the computer hardware we
271 are running on, or NIL if we can't find any useful information."
272 (unless (boundp '*machine-version*)
273 (setf *machine-version* (get-machine-version)))
274 *machine-version*)
276 ;;; FIXME: Don't forget to set these in a sample site-init file.
277 ;;; FIXME: Perhaps the functions could be SETFable instead of having the
278 ;;; interface be through special variables? As far as I can tell
279 ;;; from ANSI 11.1.2.1.1 "Constraints on the COMMON-LISP Package
280 ;;; for Conforming Implementations" it is kosher to add a SETF function for
281 ;;; a symbol in COMMON-LISP..
282 (declaim (type (or null string) *short-site-name* *long-site-name*))
283 (defvar *short-site-name* nil
284 "The value of SHORT-SITE-NAME.")
285 (defvar *long-site-name* nil
286 "The value of LONG-SITE-NAME.")
287 (defun short-site-name ()
288 "Return a string with the abbreviated site name, or NIL if not known."
289 *short-site-name*)
290 (defun long-site-name ()
291 "Return a string with the long form of the site name, or NIL if not known."
292 *long-site-name*)
294 ;;;; ED
295 (declaim (type list *ed-functions*))
296 (defvar *ed-functions* '()
297 "See function documentation for ED.")
299 (defun ed (&optional x)
300 "Starts the editor (on a file or a function if named). Functions
301 from the list *ED-FUNCTIONS* are called in order with X as an argument
302 until one of them returns non-NIL; these functions are responsible for
303 signalling a FILE-ERROR to indicate failure to perform an operation on
304 the file system."
305 (dolist (fun *ed-functions*
306 (error 'extension-failure
307 :format-control "Don't know how to ~S ~A"
308 :format-arguments (list 'ed x)
309 :references (list '(:sbcl :variable *ed-functions*))))
310 (when (funcall fun x)
311 (return t))))
313 ;;;; dribble stuff
315 ;;; Each time we start dribbling to a new stream, we put it in
316 ;;; *DRIBBLE-STREAM*, and push a list of *DRIBBLE-STREAM*, *STANDARD-INPUT*,
317 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* in *PREVIOUS-DRIBBLE-STREAMS*.
318 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* is changed to a broadcast stream that
319 ;;; broadcasts to *DRIBBLE-STREAM* and to the old values of the variables.
320 ;;; *STANDARD-INPUT* is changed to an echo stream that echos input from the old
321 ;;; value of standard input to *DRIBBLE-STREAM*.
323 ;;; When dribble is called with no arguments, *DRIBBLE-STREAM* is closed,
324 ;;; and the values of *DRIBBLE-STREAM*, *STANDARD-INPUT*, and
325 ;;; *STANDARD-OUTPUT* are popped from *PREVIOUS-DRIBBLE-STREAMS*.
327 (defvar *previous-dribble-streams* '())
328 (defvar *dribble-stream* nil)
330 (defun dribble (&optional pathname &key (if-exists :append))
331 "With a file name as an argument, dribble opens the file and sends a
332 record of further I/O to that file. Without an argument, it closes
333 the dribble file, and quits logging."
334 (flet ((install-streams (dribble input output error)
335 (setf *dribble-stream* dribble
336 *standard-input* input
337 *standard-output* output
338 *error-output* error)))
339 (cond (pathname
340 (push (list *dribble-stream* *standard-input* *standard-output*
341 *error-output*)
342 *previous-dribble-streams*)
343 (let ((new-dribble (open pathname
344 :direction :output
345 :if-exists if-exists
346 :if-does-not-exist :create)))
347 (install-streams
348 new-dribble
349 (make-echo-stream *standard-input* new-dribble)
350 (make-broadcast-stream *standard-output* new-dribble)
351 (make-broadcast-stream *error-output* new-dribble))))
352 ((null *dribble-stream*)
353 (error "not currently dribbling"))
355 (close *dribble-stream*)
356 (apply #'install-streams (pop *previous-dribble-streams*)))))
357 (values))
359 (defun %byte-blt (src src-start dst dst-start dst-end)
360 (%byte-blt src src-start dst dst-start dst-end))
362 ;;;; some *LOAD-FOO* variables
364 (defvar *load-print* nil
365 "the default for the :PRINT argument to LOAD")
367 (defvar *load-verbose* nil
368 ;; Note that CMU CL's default for this was T, and ANSI says it's
369 ;; implementation-dependent. We choose NIL on the theory that it's
370 ;; a nicer default behavior for Unix programs.
371 "the default for the :VERBOSE argument to LOAD")