1.0.0.28: more PCL cleanups
[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*
42   '(optimize (speed 3) (safety 0)))
43 ) ; EVAL-WHEN
44
45 (defmacro dotimes-fixnum ((var count &optional (result nil)) &body body)
46   `(dotimes (,var (the fixnum ,count) ,result)
47      (declare (fixnum ,var))
48      ,@body))
49 \f
50 ;;;; early definition of WRAPPER
51 ;;;;
52 ;;;; Most WRAPPER stuff is defined later, but the DEFSTRUCT itself
53 ;;;; is here early so that things like (TYPEP .. 'WRAPPER) can be
54 ;;;; compiled efficiently.
55
56 ;;; Note that for SBCL, as for CMU CL, the WRAPPER of a built-in or
57 ;;; structure class will be some other kind of SB-KERNEL:LAYOUT, but
58 ;;; this shouldn't matter, since the only two slots that WRAPPER adds
59 ;;; are meaningless in those cases.
60 (defstruct (wrapper
61             (:include layout
62                       ;; KLUDGE: In CMU CL, the initialization default
63                       ;; for LAYOUT-INVALID was NIL. In SBCL, that has
64                       ;; changed to :UNINITIALIZED, but PCL code might
65                       ;; still expect NIL for the initialization
66                       ;; default of WRAPPER-INVALID. Instead of trying
67                       ;; to find out, I just overrode the LAYOUT
68                       ;; default here. -- WHN 19991204
69                       (invalid nil))
70             (:constructor make-wrapper-internal)
71             (:copier nil))
72   (instance-slots-layout nil :type list)
73   (class-slots nil :type list))
74 #-sb-fluid (declaim (sb-ext:freeze-type wrapper))
75 \f
76 ;;;; PCL's view of funcallable instances
77
78 (!defstruct-with-alternate-metaclass standard-funcallable-instance
79   ;; KLUDGE: Note that neither of these slots is ever accessed by its
80   ;; accessor name as of sbcl-0.pre7.63. Presumably everything works
81   ;; by puns based on absolute locations. Fun fun fun.. -- WHN 2001-10-30
82   :slot-names (clos-slots name hash-code)
83   :boa-constructor %make-standard-funcallable-instance
84   :superclass-name function
85   :metaclass-name standard-classoid
86   :metaclass-constructor make-standard-classoid
87   :dd-type funcallable-structure
88   ;; Only internal implementation code will access these, and these
89   ;; accesses (slot readers in particular) could easily be a
90   ;; bottleneck, so it seems reasonable to suppress runtime type
91   ;; checks.
92   ;;
93   ;; (Except note KLUDGE above that these accessors aren't used at all
94   ;; (!) as of sbcl-0.pre7.63, so for now it's academic.)
95   :runtime-type-checks-p nil)
96
97 (import 'sb-kernel:funcallable-instance-p)
98
99 (defun set-funcallable-instance-function (fin new-value)
100   (declare (type function new-value))
101   (aver (funcallable-instance-p fin))
102   (setf (funcallable-instance-fun fin) new-value))
103 ;;; FIXME: these macros should just go away.  It's not clear whether
104 ;;; the inline functions defined by
105 ;;; !DEFSTRUCT-WITH-ALTERNATE-METACLASS are as efficient as they could
106 ;;; be; ordinary defstruct accessors are defined as source transforms.
107 (defmacro fsc-instance-p (fin)
108   `(funcallable-instance-p ,fin))
109 (defmacro fsc-instance-wrapper (fin)
110   `(%funcallable-instance-layout ,fin))
111 (defmacro fsc-instance-slots (fin)
112   `(%funcallable-instance-info ,fin 1))
113 (defmacro fsc-instance-hash (fin)
114   `(%funcallable-instance-info ,fin 3))
115 \f
116 (declaim (inline clos-slots-ref (setf clos-slots-ref)))
117 (declaim (ftype (function (simple-vector index) t) clos-slots-ref))
118 (defun clos-slots-ref (slots index)
119   (svref slots index))
120 (declaim (ftype (function (t simple-vector index) t) (setf clos-slots-ref)))
121 (defun (setf clos-slots-ref) (new-value slots index)
122   (setf (svref slots index) new-value))
123
124 ;;; Note on implementation under CMU CL >=17 and SBCL: STD-INSTANCE-P
125 ;;; is only used to discriminate between functions (including FINs)
126 ;;; and normal instances, so we can return true on structures also. A
127 ;;; few uses of (OR STD-INSTANCE-P FSC-INSTANCE-P) are changed to
128 ;;; PCL-INSTANCE-P.
129 (defmacro std-instance-p (x)
130   `(%instancep ,x))
131
132 ;; a temporary definition used for debugging the bootstrap
133 #+sb-show
134 (defun print-std-instance (instance stream depth)
135   (declare (ignore depth))
136   (print-unreadable-object (instance stream :type t :identity t)
137     (let ((class (class-of instance)))
138       (when (or (eq class (find-class 'standard-class nil))
139                 (eq class (find-class 'funcallable-standard-class nil))
140                 (eq class (find-class 'built-in-class nil)))
141         (princ (early-class-name instance) stream)))))
142
143 ;;; This is the value that we stick into a slot to tell us that it is
144 ;;; unbound. It may seem gross, but for performance reasons, we make
145 ;;; this an interned symbol. That means that the fast check to see
146 ;;; whether a slot is unbound is to say (EQ <val> '..SLOT-UNBOUND..).
147 ;;; That is considerably faster than looking at the value of a special
148 ;;; variable. Be careful, there are places in the code which actually
149 ;;; use ..SLOT-UNBOUND.. rather than this variable. So much for
150 ;;; modularity..
151 ;;;
152 ;;; FIXME: Now that we're tightly integrated into SBCL, we could use
153 ;;; the SBCL built-in unbound value token instead. Perhaps if we did
154 ;;; so it would be a good idea to define collections of CLOS slots as
155 ;;; a new type of heap object, instead of using bare SIMPLE-VECTOR, in
156 ;;; order to avoid problems (in the debugger if nowhere else) with
157 ;;; SIMPLE-VECTORs some of whose elements are unbound tokens.
158 (defconstant +slot-unbound+ '..slot-unbound..)
159
160 (defmacro %allocate-static-slot-storage--class (no-of-slots)
161   `(make-array ,no-of-slots :initial-element +slot-unbound+))
162
163 (defmacro std-instance-class (instance)
164   `(wrapper-class* (std-instance-wrapper ,instance)))
165 \f
166 ;;; When given a funcallable instance, SET-FUN-NAME *must* side-effect
167 ;;; that FIN to give it the name. When given any other kind of
168 ;;; function SET-FUN-NAME is allowed to return a new function which is
169 ;;; "the same" except that it has the name.
170 ;;;
171 ;;; In all cases, SET-FUN-NAME must return the new (or same)
172 ;;; function. (Unlike other functions to set stuff, it does not return
173 ;;; the new value.)
174 (defun set-fun-name (fun new-name)
175   #+sb-doc
176   "Set the name of a compiled function object. Return the function."
177   (declare (special *boot-state* *the-class-standard-generic-function*))
178   (when (valid-function-name-p fun)
179     (setq fun (fdefinition fun)))
180   (typecase fun
181     (%method-function (setf (%method-function-name fun) new-name))
182     #+sb-eval
183     (sb-eval:interpreted-function
184      (setf (sb-eval:interpreted-function-name fun) new-name))
185     (funcallable-instance ;; KLUDGE: probably a generic function...
186      (cond ((if (eq *boot-state* 'complete)
187                 (typep fun 'generic-function)
188                 (eq (class-of fun) *the-class-standard-generic-function*))
189             (setf (%funcallable-instance-info fun 2) new-name))
190            (t
191             (bug "unanticipated function type")))))
192   ;; Fixup name-to-function mappings in cases where the function
193   ;; hasn't been defined by DEFUN.  (FIXME: is this right?  This logic
194   ;; comes from CMUCL).  -- CSR, 2004-12-31
195   (when (and (consp new-name)
196              (member (car new-name) '(slow-method fast-method slot-accessor)))
197     (setf (fdefinition new-name) fun))
198   fun)
199 \f
200 ;;; FIXME: probably no longer needed after init
201 (defmacro precompile-random-code-segments (&optional system)
202   `(progn
203      (eval-when (:compile-toplevel)
204        (update-dispatch-dfuns))
205      (precompile-function-generators ,system)
206      (precompile-dfun-constructors ,system)
207      (precompile-ctors)))
208 \f
209 ;;; This definition is for interpreted code.
210 (defun pcl-instance-p (x)
211   (typep (layout-of x) 'wrapper))
212
213 ;;; CMU CL comment:
214 ;;;   We define this as STANDARD-INSTANCE, since we're going to
215 ;;;   clobber the layout with some standard-instance layout as soon as
216 ;;;   we make it, and we want the accessor to still be type-correct.
217 #|
218 (defstruct (standard-instance
219             (:predicate nil)
220             (:constructor %%allocate-instance--class ())
221             (:copier nil)
222             (:alternate-metaclass instance
223                                   cl:standard-class
224                                   make-standard-class))
225   (slots nil))
226 |#
227 (!defstruct-with-alternate-metaclass standard-instance
228   :slot-names (slots hash-code)
229   :boa-constructor %make-standard-instance
230   :superclass-name t
231   :metaclass-name standard-classoid
232   :metaclass-constructor make-standard-classoid
233   :dd-type structure
234   :runtime-type-checks-p nil)
235
236 ;;; Both of these operations "work" on structures, which allows the above
237 ;;; weakening of STD-INSTANCE-P.
238 (defmacro std-instance-slots (x) `(%instance-ref ,x 1))
239 (defmacro std-instance-wrapper (x) `(%instance-layout ,x))
240 ;;; KLUDGE: This one doesn't "work" on structures.  However, we
241 ;;; ensure, in SXHASH and friends, never to call it on structures.
242 (defmacro std-instance-hash (x) `(%instance-ref ,x 2))
243
244 ;;; FIXME: These functions are called every place we do a
245 ;;; CALL-NEXT-METHOD, and probably other places too. It's likely worth
246 ;;; selectively optimizing them with DEFTRANSFORMs and stuff, rather
247 ;;; than just indiscriminately expanding them inline everywhere.
248 (declaim (inline get-slots get-slots-or-nil))
249 (declaim (ftype (function (t) simple-vector) get-slots))
250 (declaim (ftype (function (t) (or simple-vector null)) get-slots-or-nil))
251 (defun get-slots (instance)
252   (if (std-instance-p instance)
253       (std-instance-slots instance)
254       (fsc-instance-slots instance)))
255 (defun get-slots-or-nil (instance)
256   ;; Suppress a code-deletion note.  FIXME: doing the FIXME above,
257   ;; integrating PCL more with the compiler, would remove the need for
258   ;; this icky stuff.
259   (declare (optimize (inhibit-warnings 3)))
260   (when (pcl-instance-p instance)
261     (get-slots instance)))
262
263 (defmacro built-in-or-structure-wrapper (x) `(layout-of ,x))
264
265 (defmacro get-wrapper (inst)
266   (once-only ((wrapper `(wrapper-of ,inst)))
267     `(progn
268        (aver (typep ,wrapper 'wrapper))
269        ,wrapper)))
270
271 ;;; FIXME: could be an inline function or ordinary function (like many
272 ;;; other things around here)
273 (defmacro get-instance-wrapper-or-nil (inst)
274   (once-only ((wrapper `(wrapper-of ,inst)))
275     `(if (typep ,wrapper 'wrapper)
276          ,wrapper
277          nil)))
278 \f
279 ;;;; support for useful hashing of PCL instances
280
281 (defvar *instance-hash-code-random-state* (make-random-state))
282 (defun get-instance-hash-code ()
283   ;; ANSI SXHASH wants us to make a good-faith effort to produce
284   ;; hash-codes that are well distributed within the range of
285   ;; non-negative fixnums, and this RANDOM operation does that, unlike
286   ;; the sbcl<=0.8.16 implementation of this operation as
287   ;; (INCF COUNTER).
288   ;;
289   ;; Hopefully there was no virtue to the old counter implementation
290   ;; that I am insufficiently insightful to insee. -- WHN 2004-10-28
291   (random most-positive-fixnum
292           *instance-hash-code-random-state*))
293
294 (defun sb-impl::sxhash-instance (x)
295   (cond
296     ((std-instance-p x) (std-instance-hash x))
297     ((fsc-instance-p x) (fsc-instance-hash x))
298     (t (bug "SXHASH-INSTANCE called on some weird thing: ~S" x))))
299 \f
300 ;;;; structure-instance stuff
301 ;;;;
302 ;;;; FIXME: Now that the code is SBCL-only, this extra layer of
303 ;;;; abstraction around our native structure representation doesn't
304 ;;;; seem to add anything useful, and could probably go away.
305
306 ;;; The definition of STRUCTURE-TYPE-P was moved to early-low.lisp.
307
308 (defun get-structure-dd (type)
309   (layout-info (classoid-layout (find-classoid type))))
310
311 (defun structure-type-included-type-name (type)
312   (let ((include (dd-include (get-structure-dd type))))
313     (if (consp include)
314         (car include)
315         include)))
316
317 (defun structure-type-slot-description-list (type)
318   (nthcdr (length (let ((include (structure-type-included-type-name type)))
319                     (and include
320                          (dd-slots (get-structure-dd include)))))
321           (dd-slots (get-structure-dd type))))
322
323 (defun structure-slotd-name (slotd)
324   (dsd-name slotd))
325
326 (defun structure-slotd-accessor-symbol (slotd)
327   (dsd-accessor-name slotd))
328
329 (defun structure-slotd-reader-function (slotd)
330   (fdefinition (dsd-accessor-name slotd)))
331
332 (defun structure-slotd-writer-function (type slotd)
333   (if (dsd-read-only slotd)
334       (let ((dd (get-structure-dd type)))
335         (coerce (slot-setter-lambda-form dd slotd) 'function))
336       (fdefinition `(setf ,(dsd-accessor-name slotd)))))
337
338 (defun structure-slotd-type (slotd)
339   (dsd-type slotd))
340
341 (defun structure-slotd-init-form (slotd)
342   (dsd-default slotd))
343 \f
344 ;;; method function stuff.
345 ;;;
346 ;;; PCL historically included a so-called method-fast-function, which
347 ;;; is essentially a method function but with (a) a precomputed
348 ;;; continuation for CALL-NEXT-METHOD and (b) a permutation vector for
349 ;;; slot access.  [ FIXME: see if we can understand these two
350 ;;; optimizations before commit. ]  However, the presence of the
351 ;;; fast-function meant that we violated AMOP and the effect of the
352 ;;; :FUNCTION initarg, and furthermore got to potentially confusing
353 ;;; situations where the function and the fast-function got out of
354 ;;; sync, so that calling (method-function method) with the defined
355 ;;; protocol would do different things from (call-method method) in
356 ;;; method combination.
357 ;;;
358 ;;; So we define this internal method function structure, which we use
359 ;;; when we create a method function ourselves.  This means that we
360 ;;; can hang the various bits of information that we want off the
361 ;;; method function itself, and also that if a user overrides method
362 ;;; function creation there is no danger of having the system get
363 ;;; confused.
364 (!defstruct-with-alternate-metaclass %method-function
365   :slot-names (fast-function name)
366   :boa-constructor %make-method-function
367   :superclass-name function
368   :metaclass-name static-classoid
369   :metaclass-constructor make-static-classoid
370   :dd-type funcallable-structure)
371 \f
372 ;;; WITH-PCL-LOCK is used around some forms that were previously
373 ;;; protected by WITHOUT-INTERRUPTS, but in a threaded SBCL we don't
374 ;;; have a useful WITHOUT-INTERRUPTS.  In an unthreaded SBCL I'm not
375 ;;; sure what the desired effect is anyway: should we be protecting
376 ;;; against the possibility of recursive calls into these functions
377 ;;; or are we using WITHOUT-INTERRUPTS as WITHOUT-SCHEDULING?
378 ;;;
379 ;;; Users: FORCE-CACHE-FLUSHES, MAKE-INSTANCES-OBSOLETE.  Note that
380 ;;; it's not all certain this is sufficent for threadsafety: do we
381 ;;; just have to protect against simultaneous calls to these mutators,
382 ;;; or actually to stop normal slot access etc at the same time as one
383 ;;; of them runs
384
385 #+sb-thread
386 (progn
387   (defvar *pcl-lock* (sb-thread::make-spinlock))
388
389   (defmacro with-pcl-lock (&body body)
390     `(sb-thread::with-spinlock (*pcl-lock*)
391       ,@body)))
392
393 #-sb-thread
394 (defmacro with-pcl-lock (&body body)
395   `(progn ,@body))