0.6.7.22: removed CVS dollar-Header-dollar tags from sources
[sbcl.git] / src / pcl / low.lisp
1 ;;;; This file contains portable versions of low-level functions and macros
2 ;;;; which are ripe for implementation specific customization. None of the code
3 ;;;; in this file *has* to be customized for a particular Common Lisp
4 ;;;; implementation. Moreover, in some implementations it may not make any
5 ;;;; sense to customize some of this code.
6 ;;;;
7 ;;;; The original version was intended to support portable customization to
8 ;;;; lotso different Lisp implementations. This functionality is gone in the
9 ;;;; current version, and it now runs only under SBCL. (Now that ANSI Common
10 ;;;; Lisp has mixed CLOS into the insides of the system (e.g. error handling
11 ;;;; and printing) so deeply that it's not very meaningful to bootstrap Common
12 ;;;; Lisp without CLOS, the old functionality is of dubious use. -- WHN
13 ;;;; 19981108)
14
15 ;;;; This software is part of the SBCL system. See the README file for more
16 ;;;; information.
17
18 ;;;; This software is derived from software originally released by Xerox
19 ;;;; Corporation. Copyright and release statements follow. Later modifications
20 ;;;; to the software are in the public domain and are provided with
21 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
22 ;;;; information.
23
24 ;;;; copyright information from original PCL sources:
25 ;;;;
26 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
27 ;;;; All rights reserved.
28 ;;;;
29 ;;;; Use and copying of this software and preparation of derivative works based
30 ;;;; upon this software are permitted. Any distribution of this software or
31 ;;;; derivative works must comply with all applicable United States export
32 ;;;; control laws.
33 ;;;;
34 ;;;; This software is made available AS IS, and Xerox Corporation makes no
35 ;;;; warranty about the software, its performance or its conformity to any
36 ;;;; specification.
37
38 (in-package "SB-PCL")
39 \f
40 (eval-when (:compile-toplevel :load-toplevel :execute)
41 (defvar *optimize-speed* '(optimize (speed 3) (safety 0)))
42 ) ; EVAL-WHEN
43
44 ;;; FIXME: Do these definitions actually increase speed significantly?
45 ;;; Could we just use SVREF instead, possibly with a few extra
46 ;;; OPTIMIZE declarations added here and ther?
47 (defmacro %svref (vector index)
48   `(locally (declare #.*optimize-speed*
49                      (inline svref))
50             (svref (the simple-vector ,vector) (the fixnum ,index))))
51 (defsetf %svref %set-svref)
52 (defmacro %set-svref (vector index new-value)
53   `(locally (declare #.*optimize-speed*
54                      (inline svref))
55      (setf (svref (the simple-vector ,vector) (the fixnum ,index))
56            ,new-value)))
57
58 ;;; I want the body to be evaluated in such a way that no other code that is
59 ;;; running PCL can be run during that evaluation. I agree that the body
60 ;;; won't take *long* to evaluate. That is to say that I will only use
61 ;;; WITHOUT-INTERRUPTS around relatively small computations.
62 ;;;
63 ;;; FIXME: We can get rid of this macro definitionand either USE package %SYS
64 ;;; or add an explicit SB-SYS: prefix to each reference to WITHOUT-INTERRUPTS.
65 (defmacro without-interrupts (&rest stuff)
66   `(sb-sys:without-interrupts ,@stuff))
67
68 (defmacro dotimes-fixnum ((var count &optional (result nil)) &body body)
69   `(dotimes (,var (the fixnum ,count) ,result)
70      (declare (fixnum ,var))
71      ,@body))
72 \f
73 ;;;; very low-level representation of instances with meta-class
74 ;;;; STANDARD-CLASS
75
76 ;;; FIXME: more than one IN-PACKAGE in a source file, ick
77 (in-package "SB-C")
78
79 (defknown sb-pcl::pcl-instance-p (t) boolean
80   (movable foldable flushable explicit-check))
81
82 (deftransform sb-pcl::pcl-instance-p ((object))
83   (let* ((otype (continuation-type object))
84          (std-obj (specifier-type 'sb-pcl::std-object)))
85     (cond
86       ;; Flush tests whose result is known at compile time.
87       ((csubtypep otype std-obj) 't)
88       ((not (types-intersect otype std-obj)) 'nil)
89       (t
90        `(typep (sb-kernel:layout-of object) 'sb-pcl::wrapper)))))
91
92 (in-package "SB-PCL")
93
94 ;;; FIXME: What do these do? Could we use SB-KERNEL:INSTANCE-REF instead?
95 (defmacro %instance-ref (slots index)
96   `(%svref ,slots ,index))
97 (defmacro instance-ref (slots index)
98   `(svref ,slots ,index))
99
100 ;;; Note on implementation under CMU CL >=17 and SBCL: STD-INSTANCE-P is
101 ;;; only used to discriminate between functions (including FINs) and
102 ;;; normal instances, so we can return true on structures also. A few
103 ;;; uses of (or std-instance-p fsc-instance-p) are changed to
104 ;;; pcl-instance-p.
105 (defmacro std-instance-p (x)
106   `(sb-kernel:%instancep ,x))
107
108 (defmacro get-slots (inst)
109   `(cond ((std-instance-p ,inst) (std-instance-slots ,inst))
110          ((fsc-instance-p ,inst) (fsc-instance-slots ,inst))
111          (t (error "What kind of instance is this?"))))
112
113 ;; a temporary definition used for debugging the bootstrap
114 #+sb-show
115 (defun print-std-instance (instance stream depth)
116   (declare (ignore depth))      
117   (print-unreadable-object (instance stream :type t :identity t)
118     (let ((class (class-of instance)))
119       (when (or (eq class (find-class 'standard-class nil))
120                 (eq class (find-class 'funcallable-standard-class nil))
121                 (eq class (find-class 'built-in-class nil)))
122         (princ (early-class-name instance) stream)))))
123
124 ;;; This is the value that we stick into a slot to tell us that it is unbound.
125 ;;; It may seem gross, but for performance reasons, we make this an interned
126 ;;; symbol. That means that the fast check to see whether a slot is unbound is
127 ;;; to say (EQ <val> '..SLOT-UNBOUND..). That is considerably faster than
128 ;;; looking at the value of a special variable. Be careful, there are places in
129 ;;; the code which actually use ..slot-unbound.. rather than this variable. So
130 ;;; much for modularity..
131 ;;;
132 ;;; FIXME: Now that we're tightly integrated into SBCL, we could use the
133 ;;; SBCL built-in unbound value token instead.
134 (defconstant *slot-unbound* '..slot-unbound..)
135
136 (defmacro %allocate-static-slot-storage--class (no-of-slots)
137   `(make-array ,no-of-slots :initial-element *slot-unbound*))
138
139 (defmacro std-instance-class (instance)
140   `(wrapper-class* (std-instance-wrapper ,instance)))
141 \f
142 ;;;; FUNCTION-ARGLIST
143
144 ;;; FIXME: Does FUNCTION-PRETTY-ARGLIST need to be settable at all?
145 (defsetf function-pretty-arglist set-function-pretty-arglist)
146 (defun set-function-pretty-arglist (function new-value)
147   (declare (ignore function))
148   new-value)
149
150 ;;; SET-FUNCTION-NAME
151 ;;;
152 ;;; When given a function should give this function the name <new-name>.
153 ;;; Note that <new-name> is sometimes a list. Some lisps get the upset
154 ;;; in the tummy when they start thinking about functions which have
155 ;;; lists as names. To deal with that there is set-function-name-intern
156 ;;; which takes a list spec for a function name and turns it into a symbol
157 ;;; if need be.
158 ;;;
159 ;;; When given a funcallable instance, set-function-name MUST side-effect
160 ;;; that FIN to give it the name. When given any other kind of function
161 ;;; set-function-name is allowed to return new function which is the 'same'
162 ;;; except that it has the name.
163 ;;;
164 ;;; In all cases, set-function-name must return the new (or same) function.
165 ;;; (Unlike other functions to set stuff, it does not return the new value.)
166 (defun set-function-name (fcn new-name)
167   #+sb-doc
168   "Set the name of a compiled function object. Return the function."
169   (declare (special *boot-state* *the-class-standard-generic-function*))
170   (cond ((symbolp fcn)
171          (set-function-name (symbol-function fcn) new-name))
172         ((funcallable-instance-p fcn)
173          (if (if (eq *boot-state* 'complete)
174                  (typep fcn 'generic-function)
175                  (eq (class-of fcn) *the-class-standard-generic-function*))
176              (setf (sb-kernel:%funcallable-instance-info fcn 1) new-name)
177              (typecase fcn
178                (sb-kernel:byte-closure
179                 (set-function-name (sb-kernel:byte-closure-function fcn)
180                                    new-name))
181                (sb-kernel:byte-function
182                 (setf (sb-kernel:byte-function-name fcn) new-name))
183                (sb-eval:interpreted-function
184                 (setf (sb-eval:interpreted-function-name fcn) new-name))))
185          fcn)
186         (t
187          ;; pw-- This seems wrong and causes trouble. Tests show
188          ;; that loading CL-HTTP resulted in ~5400 closures being
189          ;; passed through this code of which ~4000 of them pointed
190          ;; to but 16 closure-functions, including 1015 each of
191          ;; DEFUN MAKE-OPTIMIZED-STD-WRITER-METHOD-FUNCTION
192          ;; DEFUN MAKE-OPTIMIZED-STD-READER-METHOD-FUNCTION
193          ;; DEFUN MAKE-OPTIMIZED-STD-BOUNDP-METHOD-FUNCTION.
194          ;; Since the actual functions have been moved by PURIFY
195          ;; to memory not seen by GC, changing a pointer there
196          ;; not only clobbers the last change but leaves a dangling
197          ;; pointer invalid  after the next GC. Comments in low.lisp
198          ;; indicate this code need do nothing. Setting the
199          ;; function-name to NIL loses some info, and not changing
200          ;; it loses some info of potential hacking value. So,
201          ;; lets not do this...
202          #+nil
203          (let ((header (sb-kernel:%closure-function fcn)))
204            (setf (sb-c::%function-name header) new-name))
205
206          ;; Maybe add better scheme here someday.
207          fcn)))
208
209 (defun intern-function-name (name)
210   (cond ((symbolp name) name)
211         ((listp name)
212          (intern (let ((*package* *pcl-package*)
213                        (*print-case* :upcase)
214                        (*print-pretty* nil)
215                        (*print-gensym* 't))
216                    (format nil "~S" name))
217                  *pcl-package*))))
218 \f
219 ;;;; COMPILE-LAMBDA
220
221 ;;; This is like the Common Lisp function COMPILE. In fact, that is what it
222 ;;; ends up calling. The difference is that it deals with things like not
223 ;;; calling the compiler in certain cases.
224 ;;;
225 ;;; FIXME: I suspect that in SBCL, we should always call the compiler. (PCL
226 ;;; was originally designed to run even on systems with dog-slow call-out-to-C
227 ;;; compilers, and I suspect that this code is needed only for that.)
228 (defun compile-lambda (lambda &optional (desirability :fast))
229   (cond ((eq desirability :fast)
230          (compile nil lambda))
231         (t
232          (compile-lambda-uncompiled lambda))))
233
234 (defun compile-lambda-uncompiled (uncompiled)
235   #'(lambda (&rest args) (apply (coerce uncompiled 'function) args)))
236
237 (defun compile-lambda-deferred (uncompiled)
238   (let ((function (coerce uncompiled 'function))
239         (compiled nil))
240     (declare (type (or function null) compiled))
241     #'(lambda (&rest args)
242         (if compiled
243             (apply compiled args)
244             (if (in-the-compiler-p)
245                 (apply function args)
246                 (progn (setq compiled (compile nil uncompiled))
247                        (apply compiled args)))))))
248
249 ;;; FIXME: probably no longer needed after init
250 (defmacro precompile-random-code-segments (&optional system)
251   `(progn
252      (eval-when (:compile-toplevel)
253        (update-dispatch-dfuns)
254        (compile-iis-functions nil))
255      (precompile-function-generators ,system)
256      (precompile-dfun-constructors ,system)
257      (precompile-iis-functions ,system)
258      (eval-when (:load-toplevel)
259        (compile-iis-functions t))))
260 \f
261 (defun record-definition (type spec &rest args)
262   (declare (ignore type spec args))
263   ())
264
265 (defun doctor-dfun-for-the-debugger (gf dfun) (declare (ignore gf)) dfun)
266 \f
267 ;;;; low level functions for structures I: functions on arbitrary objects
268
269 ;;; FIXME: Maybe we don't need this given the SBCL-specific
270 ;;; versions of the functions which would otherwise use it?
271 (defvar *structure-table* (make-hash-table :test 'eq))
272
273 (defun declare-structure (name included-name slot-description-list)
274   (setf (gethash name *structure-table*)
275         (cons included-name slot-description-list)))
276
277 (unless (fboundp 'structure-functions-exist-p)
278   (setf (symbol-function 'structure-functions-exist-p)
279         #'(lambda () nil)))
280
281 ;;; FIXME: should probably be INLINE
282 ;;; FIXME: should probably be moved to package SB-INT along with
283 ;;; other nonstandard type predicates, or removed entirely
284 (defun structurep (x)
285   (typep x 'cl:structure-object))
286 \f
287 ;;; This definition is for interpreted code.
288 (defun pcl-instance-p (x)
289   (typep (sb-kernel:layout-of x) 'wrapper))
290
291 ;;; We define this as STANDARD-INSTANCE, since we're going to clobber the
292 ;;; layout with some standard-instance layout as soon as we make it, and we
293 ;;; want the accessor to still be type-correct.
294 (defstruct (standard-instance
295             (:predicate nil)
296             (:constructor %%allocate-instance--class ())
297             (:copier nil)
298             (:alternate-metaclass sb-kernel:instance cl:standard-class
299                                   sb-kernel:make-standard-class))
300   (slots nil))
301
302 ;;; Both of these operations "work" on structures, which allows the above
303 ;;; weakening of std-instance-p.
304 (defmacro std-instance-slots (x) `(sb-kernel:%instance-ref ,x 1))
305 (defmacro std-instance-wrapper (x) `(sb-kernel:%instance-layout ,x))
306
307 (defmacro built-in-or-structure-wrapper (x) `(sb-kernel:layout-of ,x))
308
309 (defmacro get-wrapper (inst)
310   (sb-int:once-only ((wrapper `(wrapper-of ,inst)))
311     `(progn
312        (assert (typep ,wrapper 'wrapper) () "What kind of instance is this?")
313        ,wrapper)))
314
315 ;;; FIXME: could be an inline function (like many other things around
316 ;;; here)
317 (defmacro get-instance-wrapper-or-nil (inst)
318   (sb-int:once-only ((wrapper `(wrapper-of ,inst)))
319     `(if (typep ,wrapper 'wrapper)
320          ,wrapper
321          nil)))
322
323 (defmacro get-slots-or-nil (inst)
324   (sb-int:once-only ((n-inst inst))
325     `(when (pcl-instance-p ,n-inst)
326        (if (std-instance-p ,n-inst)
327            (std-instance-slots ,n-inst)
328            (fsc-instance-slots ,n-inst)))))
329 \f
330 ;;;; structure-instance stuff
331
332 ;;; FIXME: This can be removed by hardwiring uses of it to T.
333 (defun structure-functions-exist-p ()
334   t)
335
336 ;;; The definition of STRUCTURE-TYPE-P was moved to early-low.lisp.
337
338 (defun get-structure-dd (type)
339   (sb-kernel:layout-info (sb-kernel:class-layout (cl:find-class type))))
340
341 (defun structure-type-included-type-name (type)
342   (let ((include (sb-kernel::dd-include (get-structure-dd type))))
343     (if (consp include)
344         (car include)
345         include)))
346
347 (defun structure-type-slot-description-list (type)
348   (nthcdr (length (let ((include (structure-type-included-type-name type)))
349                     (and include
350                          (sb-kernel:dd-slots (get-structure-dd include)))))
351           (sb-kernel:dd-slots (get-structure-dd type))))
352
353 (defun structure-slotd-name (slotd)
354   (sb-kernel:dsd-name slotd))
355
356 (defun structure-slotd-accessor-symbol (slotd)
357   (sb-kernel:dsd-accessor slotd))
358
359 (defun structure-slotd-reader-function (slotd)
360   (fdefinition (sb-kernel:dsd-accessor slotd)))
361
362 (defun structure-slotd-writer-function (slotd)
363   (unless (sb-kernel:dsd-read-only slotd)
364     (fdefinition `(setf ,(sb-kernel:dsd-accessor slotd)))))
365
366 (defun structure-slotd-type (slotd)
367   (sb-kernel:dsd-type slotd))
368
369 (defun structure-slotd-init-form (slotd)
370   (sb-kernel::dsd-default slotd))
371
372 ;;; FIXME: more than one IN-PACKAGE in a source file, ick
373 (in-package "SB-C")
374
375 (def-source-context defmethod (name &rest stuff)
376   (let ((arg-pos (position-if #'listp stuff)))
377     (if arg-pos
378         `(defmethod ,name ,@(subseq stuff 0 arg-pos)
379            ,(nth-value 2 (sb-pcl::parse-specialized-lambda-list
380                           (elt stuff arg-pos))))
381         `(defmethod ,name "<illegal syntax>"))))