0.7.12.17:
[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 ;;; FIXME: Don't forget to set these in a sample site-init file.
127 ;;; FIXME: Perhaps the functions could be SETFable instead of having the
128 ;;; interface be through special variables? As far as I can tell
129 ;;; from ANSI 11.1.2.1.1 "Constraints on the COMMON-LISP Package
130 ;;; for Conforming Implementations" it is kosher to add a SETF function for
131 ;;; a symbol in COMMON-LISP..
132 (defvar *short-site-name* nil
133   #!+sb-doc
134   "The value of SHORT-SITE-NAME.")
135 (defvar *long-site-name* nil
136   #!+sb-doc "the value of LONG-SITE-NAME")
137 (defun short-site-name ()
138   #!+sb-doc
139   "Return a string with the abbreviated site name, or NIL if not known."
140   *short-site-name*)
141 (defun long-site-name ()
142   #!+sb-doc
143   "Return a string with the long form of the site name, or NIL if not known."
144   *long-site-name*)
145 \f
146 ;;;; dribble stuff
147
148 ;;; Each time we start dribbling to a new stream, we put it in
149 ;;; *DRIBBLE-STREAM*, and push a list of *DRIBBLE-STREAM*, *STANDARD-INPUT*,
150 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* in *PREVIOUS-DRIBBLE-STREAMS*.
151 ;;; *STANDARD-OUTPUT* and *ERROR-OUTPUT* is changed to a broadcast stream that
152 ;;; broadcasts to *DRIBBLE-STREAM* and to the old values of the variables.
153 ;;; *STANDARD-INPUT* is changed to an echo stream that echos input from the old
154 ;;; value of standard input to *DRIBBLE-STREAM*.
155 ;;;
156 ;;; When dribble is called with no arguments, *DRIBBLE-STREAM* is closed,
157 ;;; and the values of *DRIBBLE-STREAM*, *STANDARD-INPUT*, and
158 ;;; *STANDARD-OUTPUT* are popped from *PREVIOUS-DRIBBLE-STREAMS*.
159
160 (defvar *previous-dribble-streams* nil)
161 (defvar *dribble-stream* nil)
162
163 (defun dribble (&optional pathname &key (if-exists :append))
164   #!+sb-doc
165   "With a file name as an argument, dribble opens the file and sends a
166   record of further I/O to that file. Without an argument, it closes
167   the dribble file, and quits logging."
168   (cond (pathname
169          (let* ((new-dribble-stream
170                  (open pathname
171                        :direction :output
172                        :if-exists if-exists
173                        :if-does-not-exist :create))
174                 (new-standard-output
175                  (make-broadcast-stream *standard-output* new-dribble-stream))
176                 (new-error-output
177                  (make-broadcast-stream *error-output* new-dribble-stream))
178                 (new-standard-input
179                  (make-echo-stream *standard-input* new-dribble-stream)))
180            (push (list *dribble-stream* *standard-input* *standard-output*
181                        *error-output*)
182                  *previous-dribble-streams*)
183            (setf *dribble-stream* new-dribble-stream)
184            (setf *standard-input* new-standard-input)
185            (setf *standard-output* new-standard-output)
186            (setf *error-output* new-error-output)))
187         ((null *dribble-stream*)
188          (error "not currently dribbling"))
189         (t
190          (let ((old-streams (pop *previous-dribble-streams*)))
191            (close *dribble-stream*)
192            (setf *dribble-stream* (first old-streams))
193            (setf *standard-input* (second old-streams))
194            (setf *standard-output* (third old-streams))
195            (setf *error-output* (fourth old-streams)))))
196   (values))
197
198 (defun %byte-blt (src src-start dst dst-start dst-end)
199   (%byte-blt src src-start dst dst-start dst-end))