1.0.20.8: ATOMIC-INCF implementation
[sbcl/pkhuong.git] / src / code / target-misc.lisp
blob0f65c71acc8690b70627a28a1c0fa6693a589261
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")
17 ;;;; function names and documentation
19 ;;;; the ANSI interface to function names (and to other stuff too)
20 ;;; Note: this function gets called by the compiler (as of 1.0.17.x,
21 ;;; in MAYBE-INLINE-SYNTACTIC-CLOSURE), and so although ANSI says
22 ;;; we're allowed to return NIL here freely, it seems plausible that
23 ;;; small changes to the circumstances under which this function
24 ;;; returns non-NIL might have subtle consequences on the compiler.
25 ;;; So it might be desirable to have the compiler not rely on this
26 ;;; function, eventually.
27 (defun function-lambda-expression (fun)
28 "Return (VALUES DEFINING-LAMBDA-EXPRESSION CLOSURE-P NAME), where
29 DEFINING-LAMBDA-EXPRESSION is NIL if unknown, or a suitable argument
30 to COMPILE otherwise, CLOSURE-P is non-NIL if the function's definition
31 might have been enclosed in some non-null lexical environment, and
32 NAME is some name (for debugging only) or NIL if there is no name."
33 (declare (type function fun))
34 (etypecase fun
35 #!+sb-eval
36 (sb!eval:interpreted-function
37 (let ((name (sb!eval:interpreted-function-name fun))
38 (lambda-list (sb!eval:interpreted-function-lambda-list fun))
39 (body (sb!eval:interpreted-function-body fun)))
40 (values `(lambda ,lambda-list ,@body)
41 t name)))
42 (function
43 (let* ((fun (%simple-fun-self (%fun-fun fun)))
44 (name (%fun-name fun))
45 (code (sb!di::fun-code-header fun))
46 (info (sb!kernel:%code-debug-info code)))
47 (if info
48 (let ((source (sb!c::debug-info-source info)))
49 (cond ((and (sb!c::debug-source-form source)
50 (eq (sb!c::debug-source-function source) fun))
51 (values (sb!c::debug-source-form source)
52 nil
53 name))
54 ((legal-fun-name-p name)
55 (let ((exp (fun-name-inline-expansion name)))
56 (values exp (not exp) name)))
58 (values nil t name))))
59 (values nil t name))))))
61 (defun closurep (object)
62 (= sb!vm:closure-header-widetag (widetag-of object)))
64 (defun %fun-fun (function)
65 (declare (function function))
66 (case (widetag-of function)
67 (#.sb!vm:simple-fun-header-widetag
68 function)
69 (#.sb!vm:closure-header-widetag
70 (%closure-fun function))
71 (#.sb!vm:funcallable-instance-header-widetag
72 (%fun-fun (funcallable-instance-fun function)))))
74 (defun %closure-values (object)
75 (declare (function object))
76 (loop for index from 0
77 below (- (get-closure-length object) (1- sb!vm:closure-info-offset))
78 collect (%closure-index-ref object index)))
80 (defun %fun-lambda-list (object)
81 (%simple-fun-arglist (%fun-fun object)))
83 ;;; a SETFable function to return the associated debug name for FUN
84 ;;; (i.e., the third value returned from CL:FUNCTION-LAMBDA-EXPRESSION),
85 ;;; or NIL if there's none
86 (defun %fun-name (function)
87 (%simple-fun-name (%fun-fun function)))
89 (defun %fun-type (function)
90 (%simple-fun-type (%fun-fun function)))
92 (defun (setf %fun-name) (new-name fun)
93 (aver nil) ; since this is unsafe 'til bug 137 is fixed
94 (let ((widetag (widetag-of fun)))
95 (case widetag
96 (#.sb!vm:simple-fun-header-widetag
97 ;; KLUDGE: The pun that %SIMPLE-FUN-NAME is used for closure
98 ;; functions is left over from CMU CL (modulo various renaming
99 ;; that's gone on since the fork).
100 (setf (%simple-fun-name fun) new-name))
101 (#.sb!vm:closure-header-widetag
102 ;; FIXME: It'd be nice to be able to set %FUN-NAME here on
103 ;; per-closure basis. Instead, we are still using the CMU CL
104 ;; approach of closures being named after their closure
105 ;; function, which doesn't work right e.g. for structure
106 ;; accessors, and might not be quite right for DEFUN
107 ;; in a non-null lexical environment either.
108 ;; When/if weak hash tables become supported
109 ;; again, it'll become easy to fix this, but for now there
110 ;; seems to be no easy way (short of the ugly way of adding a
111 ;; slot to every single closure header), so we don't.
113 ;; Meanwhile, users might encounter this problem by doing DEFUN
114 ;; in a non-null lexical environment, so we try to give a
115 ;; reasonably meaningful user-level "error" message (but only
116 ;; as a warning because this is optional debugging
117 ;; functionality anyway, not some hard ANSI requirement).
118 (warn "can't set name for closure, leaving name unchanged"))
120 ;; The other function subtype names are also un-settable
121 ;; but this problem seems less likely to be tickled by
122 ;; user-level code, so we can give a implementor-level
123 ;; "error" (warning) message.
124 (warn "can't set function name ((~S function)=~S), leaving it unchanged"
125 'widetag-of widetag))))
126 new-name)
128 (defun %fun-doc (x)
129 ;; FIXME: This business of going through %FUN-NAME and then globaldb
130 ;; is the way CMU CL did it, but it doesn't really seem right.
131 ;; When/if weak hash tables become supported again, using a weak
132 ;; hash table to maintain the object/documentation association would
133 ;; probably be better.
134 (let ((name (%fun-name x)))
135 (when (and name (typep name '(or symbol cons)))
136 (values (info :function :documentation name)))))
138 ;;; various environment inquiries
140 (defvar *features* '#.sb-cold:*shebang-features*
141 #!+sb-doc
142 "a list of symbols that describe features provided by the
143 implementation")
145 (defun machine-instance ()
146 #!+sb-doc
147 "Return a string giving the name of the local machine."
148 #!+win32 (sb!win32::get-computer-name)
149 #!-win32 (sb!unix:unix-gethostname))
151 (defvar *machine-version*)
153 (defun machine-version ()
154 #!+sb-doc
155 "Return a string describing the version of the computer hardware we
156 are running on, or NIL if we can't find any useful information."
157 (unless (boundp '*machine-version*)
158 (setf *machine-version* (get-machine-version)))
159 *machine-version*)
161 ;;; FIXME: Don't forget to set these in a sample site-init file.
162 ;;; FIXME: Perhaps the functions could be SETFable instead of having the
163 ;;; interface be through special variables? As far as I can tell
164 ;;; from ANSI 11.1.2.1.1 "Constraints on the COMMON-LISP Package
165 ;;; for Conforming Implementations" it is kosher to add a SETF function for
166 ;;; a symbol in COMMON-LISP..
167 (defvar *short-site-name* nil
168 #!+sb-doc
169 "The value of SHORT-SITE-NAME.")
170 (defvar *long-site-name* nil
171 #!+sb-doc "the value of LONG-SITE-NAME")
172 (defun short-site-name ()
173 #!+sb-doc
174 "Return a string with the abbreviated site name, or NIL if not known."
175 *short-site-name*)
176 (defun long-site-name ()
177 #!+sb-doc
178 "Return a string with the long form of the site name, or NIL if not known."
179 *long-site-name*)
181 ;;;; ED
182 (defvar *ed-functions* nil
183 "See function documentation for ED.")
185 (defun ed (&optional x)
186 "Starts the editor (on a file or a function if named). Functions
187 from the list *ED-FUNCTIONS* are called in order with X as an argument
188 until one of them returns non-NIL; these functions are responsible for
189 signalling a FILE-ERROR to indicate failure to perform an operation on
190 the file system."
191 (dolist (fun *ed-functions*
192 (error 'extension-failure
193 :format-control "Don't know how to ~S ~A"
194 :format-arguments (list 'ed x)
195 :references (list '(:sbcl :variable *ed-functions*))))
196 (when (funcall fun x)
197 (return t))))
199 ;;;; dribble stuff
201 ;;; Each time we start dribbling to a new stream, we put it in
202 ;;; *DRIBBLE-STREAM*, and push a list of *DRIBBLE-STREAM*, *STANDARD-INPUT*,
203 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* in *PREVIOUS-DRIBBLE-STREAMS*.
204 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* is changed to a broadcast stream that
205 ;;; broadcasts to *DRIBBLE-STREAM* and to the old values of the variables.
206 ;;; *STANDARD-INPUT* is changed to an echo stream that echos input from the old
207 ;;; value of standard input to *DRIBBLE-STREAM*.
209 ;;; When dribble is called with no arguments, *DRIBBLE-STREAM* is closed,
210 ;;; and the values of *DRIBBLE-STREAM*, *STANDARD-INPUT*, and
211 ;;; *STANDARD-OUTPUT* are popped from *PREVIOUS-DRIBBLE-STREAMS*.
213 (defvar *previous-dribble-streams* nil)
214 (defvar *dribble-stream* nil)
216 (defun dribble (&optional pathname &key (if-exists :append))
217 #!+sb-doc
218 "With a file name as an argument, dribble opens the file and sends a
219 record of further I/O to that file. Without an argument, it closes
220 the dribble file, and quits logging."
221 (cond (pathname
222 (let* ((new-dribble-stream
223 (open pathname
224 :direction :output
225 :if-exists if-exists
226 :if-does-not-exist :create))
227 (new-standard-output
228 (make-broadcast-stream *standard-output* new-dribble-stream))
229 (new-error-output
230 (make-broadcast-stream *error-output* new-dribble-stream))
231 (new-standard-input
232 (make-echo-stream *standard-input* new-dribble-stream)))
233 (push (list *dribble-stream* *standard-input* *standard-output*
234 *error-output*)
235 *previous-dribble-streams*)
236 (setf *dribble-stream* new-dribble-stream)
237 (setf *standard-input* new-standard-input)
238 (setf *standard-output* new-standard-output)
239 (setf *error-output* new-error-output)))
240 ((null *dribble-stream*)
241 (error "not currently dribbling"))
243 (let ((old-streams (pop *previous-dribble-streams*)))
244 (close *dribble-stream*)
245 (setf *dribble-stream* (first old-streams))
246 (setf *standard-input* (second old-streams))
247 (setf *standard-output* (third old-streams))
248 (setf *error-output* (fourth old-streams)))))
249 (values))
251 (defun %byte-blt (src src-start dst dst-start dst-end)
252 (%byte-blt src src-start dst dst-start dst-end))
254 ;;;; some *LOAD-FOO* variables
256 (defvar *load-print* nil
257 #!+sb-doc
258 "the default for the :PRINT argument to LOAD")
260 (defvar *load-verbose* nil
261 ;; Note that CMU CL's default for this was T, and ANSI says it's
262 ;; implementation-dependent. We choose NIL on the theory that it's
263 ;; a nicer default behavior for Unix programs.
264 #!+sb-doc
265 "the default for the :VERBOSE argument to LOAD")