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