0.8.3.3:
[sbcl.git] / src / code / target-misc.lisp
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.
5
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.
14
15 (in-package "SB!IMPL")
16 \f
17 ;;;; function names and documentation
18
19 ;;;; the ANSI interface to function names (and to other stuff too)
20 (defun function-lambda-expression (fun)
21   "Return (VALUES DEFINING-LAMBDA-EXPRESSION CLOSURE-P NAME), where
22   DEFINING-LAMBDA-EXPRESSION is NIL if unknown, or a suitable argument
23   to COMPILE otherwise, CLOSURE-P is non-NIL if the function's definition
24   might have been enclosed in some non-null lexical environment, and
25   NAME is some name (for debugging only) or NIL if there is no name."
26     (declare (type function fun))
27     (let* ((fun (%simple-fun-self fun))
28            (name (%fun-name fun))
29            (code (sb!di::fun-code-header fun))
30            (info (sb!kernel:%code-debug-info code)))
31       (if info
32         (let ((source (first (sb!c::compiled-debug-info-source info))))
33           (cond ((and (eq (sb!c::debug-source-from source) :lisp)
34                       (eq (sb!c::debug-source-info source) fun))
35                  (values (svref (sb!c::debug-source-name source) 0)
36                          nil
37                          name))
38                 ;; FIXME: shouldn't these two clauses be the other way
39                 ;; round?  Using VALID-FUNCTION-NAME-P to see if we
40                 ;; want to find an inline-expansion?
41                 ((stringp name)
42                  (values nil t name))
43                 (t
44                  (let ((exp (fun-name-inline-expansion name)))
45                    (if exp
46                        (values exp nil name)
47                        (values nil t name))))))
48         (values nil t name))))
49
50 ;;; a SETFable function to return the associated debug name for FUN
51 ;;; (i.e., the third value returned from CL:FUNCTION-LAMBDA-EXPRESSION),
52 ;;; or NIL if there's none
53 (defun %fun-name (fun)
54   (case (widetag-of fun)
55     (#.sb!vm:closure-header-widetag
56      (%simple-fun-name (%closure-fun fun)))
57     ((#.sb!vm:simple-fun-header-widetag
58       #.sb!vm:closure-fun-header-widetag)
59      ;; KLUDGE: The pun that %SIMPLE-FUN-NAME is used for closure
60      ;; functions is left over from CMU CL (modulo various renaming
61      ;; that's gone on since the fork).
62      (%simple-fun-name fun))
63     (#.sb!vm:funcallable-instance-header-widetag
64      (%simple-fun-name
65       (funcallable-instance-fun fun)))))
66
67 (defun (setf %fun-name) (new-name fun)
68   (aver nil) ; since this is unsafe 'til bug 137 is fixed
69   (let ((widetag (widetag-of fun)))
70     (case widetag
71       ((#.sb!vm:simple-fun-header-widetag
72         #.sb!vm:closure-fun-header-widetag)
73        ;; KLUDGE: The pun that %SIMPLE-FUN-NAME is used for closure
74        ;; functions is left over from CMU CL (modulo various renaming
75        ;; that's gone on since the fork).
76        (setf (%simple-fun-name fun) new-name))
77       (#.sb!vm:closure-header-widetag
78        ;; FIXME: It'd be nice to be able to set %FUN-NAME here on
79        ;; per-closure basis. Instead, we are still using the CMU CL
80        ;; approach of closures being named after their closure
81        ;; function, which doesn't work right e.g. for structure
82        ;; accessors, and might not be quite right for DEFUN
83        ;; in a non-null lexical environment either.
84        ;; When/if weak hash tables become supported
85        ;; again, it'll become easy to fix this, but for now there
86        ;; seems to be no easy way (short of the ugly way of adding a
87        ;; slot to every single closure header), so we don't. 
88        ;;
89        ;; Meanwhile, users might encounter this problem by doing DEFUN
90        ;; in a non-null lexical environment, so we try to give a
91        ;; reasonably meaningful user-level "error" message (but only
92        ;; as a warning because this is optional debugging
93        ;; functionality anyway, not some hard ANSI requirement).
94        (warn "can't set name for closure, leaving name unchanged"))
95       (t
96        ;; The other function subtype names are also un-settable
97        ;; but this problem seems less likely to be tickled by
98        ;; user-level code, so we can give a implementor-level
99        ;; "error" (warning) message.
100        (warn "can't set function name ((~S function)=~S), leaving it unchanged"
101              'widetag-of widetag))))
102   new-name)
103
104 (defun %fun-doc (x)
105   ;; FIXME: This business of going through %FUN-NAME and then globaldb
106   ;; is the way CMU CL did it, but it doesn't really seem right.
107   ;; When/if weak hash tables become supported again, using a weak
108   ;; hash table to maintain the object/documentation association would
109   ;; probably be better.
110   (let ((name (%fun-name x)))
111     (when (and name (typep name '(or symbol cons)))
112       (values (info :function :documentation name)))))
113 \f
114 ;;; various environment inquiries
115
116 (defvar *features* '#.sb-cold:*shebang-features*
117   #!+sb-doc
118   "a list of symbols that describe features provided by the
119    implementation")
120
121 (defun machine-instance ()
122   #!+sb-doc
123   "Return a string giving the name of the local machine."
124   (sb!unix:unix-gethostname))
125
126 (defvar *machine-version*)
127
128 (defun machine-version ()
129   #!+sb-doc
130   "Return a string describing the version of the computer hardware we
131 are running on, or NIL if we can't find any useful information."
132   (unless (boundp '*machine-version*)
133     (setf *machine-version* (get-machine-version)))
134   *machine-version*)
135   
136 ;;; FIXME: Don't forget to set these in a sample site-init file.
137 ;;; FIXME: Perhaps the functions could be SETFable instead of having the
138 ;;; interface be through special variables? As far as I can tell
139 ;;; from ANSI 11.1.2.1.1 "Constraints on the COMMON-LISP Package
140 ;;; for Conforming Implementations" it is kosher to add a SETF function for
141 ;;; a symbol in COMMON-LISP..
142 (defvar *short-site-name* nil
143   #!+sb-doc
144   "The value of SHORT-SITE-NAME.")
145 (defvar *long-site-name* nil
146   #!+sb-doc "the value of LONG-SITE-NAME")
147 (defun short-site-name ()
148   #!+sb-doc
149   "Return a string with the abbreviated site name, or NIL if not known."
150   *short-site-name*)
151 (defun long-site-name ()
152   #!+sb-doc
153   "Return a string with the long form of the site name, or NIL if not known."
154   *long-site-name*)
155 \f
156 ;;;; dribble stuff
157
158 ;;; Each time we start dribbling to a new stream, we put it in
159 ;;; *DRIBBLE-STREAM*, and push a list of *DRIBBLE-STREAM*, *STANDARD-INPUT*,
160 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* in *PREVIOUS-DRIBBLE-STREAMS*.
161 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* is changed to a broadcast stream that
162 ;;; broadcasts to *DRIBBLE-STREAM* and to the old values of the variables.
163 ;;; *STANDARD-INPUT* is changed to an echo stream that echos input from the old
164 ;;; value of standard input to *DRIBBLE-STREAM*.
165 ;;;
166 ;;; When dribble is called with no arguments, *DRIBBLE-STREAM* is closed,
167 ;;; and the values of *DRIBBLE-STREAM*, *STANDARD-INPUT*, and
168 ;;; *STANDARD-OUTPUT* are popped from *PREVIOUS-DRIBBLE-STREAMS*.
169
170 (defvar *previous-dribble-streams* nil)
171 (defvar *dribble-stream* nil)
172
173 (defun dribble (&optional pathname &key (if-exists :append))
174   #!+sb-doc
175   "With a file name as an argument, dribble opens the file and sends a
176   record of further I/O to that file. Without an argument, it closes
177   the dribble file, and quits logging."
178   (cond (pathname
179          (let* ((new-dribble-stream
180                  (open pathname
181                        :direction :output
182                        :if-exists if-exists
183                        :if-does-not-exist :create))
184                 (new-standard-output
185                  (make-broadcast-stream *standard-output* new-dribble-stream))
186                 (new-error-output
187                  (make-broadcast-stream *error-output* new-dribble-stream))
188                 (new-standard-input
189                  (make-echo-stream *standard-input* new-dribble-stream)))
190            (push (list *dribble-stream* *standard-input* *standard-output*
191                        *error-output*)
192                  *previous-dribble-streams*)
193            (setf *dribble-stream* new-dribble-stream)
194            (setf *standard-input* new-standard-input)
195            (setf *standard-output* new-standard-output)
196            (setf *error-output* new-error-output)))
197         ((null *dribble-stream*)
198          (error "not currently dribbling"))
199         (t
200          (let ((old-streams (pop *previous-dribble-streams*)))
201            (close *dribble-stream*)
202            (setf *dribble-stream* (first old-streams))
203            (setf *standard-input* (second old-streams))
204            (setf *standard-output* (third old-streams))
205            (setf *error-output* (fourth old-streams)))))
206   (values))
207
208 (defun %byte-blt (src src-start dst dst-start dst-end)
209   (%byte-blt src src-start dst dst-start dst-end))