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