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.
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
15 ;;;; This software is part of the SBCL system. See the README file for more
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
24 ;;;; copyright information from original PCL sources:
26 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
27 ;;;; All rights reserved.
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
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
40 (eval-when (:compile-toplevel :load-toplevel :execute)
41 (defvar *optimize-speed*
42 '(optimize (speed 3) (safety 0)))
45 (defmacro dotimes-fixnum ((var count &optional (result nil)) &body body)
46 `(dotimes (,var (the fixnum ,count) ,result)
47 (declare (fixnum ,var))
50 ;;;; early definition of WRAPPER
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.
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.
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
70 (:conc-name %wrapper-)
71 (:constructor make-wrapper-internal)
73 (instance-slots-layout nil :type list)
74 (class-slots nil :type list))
75 #-sb-fluid (declaim (sb-ext:freeze-type wrapper))
77 ;;;; PCL's view of funcallable instances
79 (!defstruct-with-alternate-metaclass pcl-funcallable-instance
80 ;; KLUDGE: Note that neither of these slots is ever accessed by its
81 ;; accessor name as of sbcl-0.pre7.63. Presumably everything works
82 ;; by puns based on absolute locations. Fun fun fun.. -- WHN 2001-10-30
83 :slot-names (clos-slots name hash-code)
84 :boa-constructor %make-pcl-funcallable-instance
85 :superclass-name function
86 :metaclass-name standard-classoid
87 :metaclass-constructor make-standard-classoid
88 :dd-type funcallable-structure
89 ;; Only internal implementation code will access these, and these
90 ;; accesses (slot readers in particular) could easily be a
91 ;; bottleneck, so it seems reasonable to suppress runtime type
94 ;; (Except note KLUDGE above that these accessors aren't used at all
95 ;; (!) as of sbcl-0.pre7.63, so for now it's academic.)
96 :runtime-type-checks-p nil)
98 (import 'sb-kernel:funcallable-instance-p)
100 (defun set-funcallable-instance-function (fin new-value)
101 (declare (type function new-value))
102 (aver (funcallable-instance-p fin))
103 (setf (funcallable-instance-fun fin) new-value))
104 ;;; FIXME: these macros should just go away. It's not clear whether
105 ;;; the inline functions defined by
106 ;;; !DEFSTRUCT-WITH-ALTERNATE-METACLASS are as efficient as they could
107 ;;; be; ordinary defstruct accessors are defined as source transforms.
108 (defmacro fsc-instance-p (fin)
109 `(funcallable-instance-p ,fin))
110 (defmacro fsc-instance-wrapper (fin)
111 `(%funcallable-instance-layout ,fin))
112 (defmacro fsc-instance-slots (fin)
113 `(%funcallable-instance-info ,fin 1))
114 (defmacro fsc-instance-hash (fin)
115 `(%funcallable-instance-info ,fin 3))
117 (declaim (inline clos-slots-ref (setf clos-slots-ref)))
118 (declaim (ftype (function (simple-vector index) t) clos-slots-ref))
119 (defun clos-slots-ref (slots index)
121 (declaim (ftype (function (t simple-vector index) t) (setf clos-slots-ref)))
122 (defun (setf clos-slots-ref) (new-value slots index)
123 (setf (svref slots index) new-value))
125 ;;; Note on implementation under CMU CL >=17 and SBCL: STD-INSTANCE-P
126 ;;; is only used to discriminate between functions (including FINs)
127 ;;; and normal instances, so we can return true on structures also. A
128 ;;; few uses of (OR STD-INSTANCE-P FSC-INSTANCE-P) are changed to
130 (defmacro std-instance-p (x)
133 ;; a temporary definition used for debugging the bootstrap
135 (defun print-std-instance (instance stream depth)
136 (declare (ignore depth))
137 (print-unreadable-object (instance stream :type t :identity t)
138 (let ((class (class-of instance)))
139 (when (or (eq class (find-class 'standard-class nil))
140 (eq class (find-class 'funcallable-standard-class nil))
141 (eq class (find-class 'built-in-class nil)))
142 (princ (early-class-name instance) stream)))))
144 ;;; This is the value that we stick into a slot to tell us that it is
145 ;;; unbound. It may seem gross, but for performance reasons, we make
146 ;;; this an interned symbol. That means that the fast check to see
147 ;;; whether a slot is unbound is to say (EQ <val> '..SLOT-UNBOUND..).
148 ;;; That is considerably faster than looking at the value of a special
149 ;;; variable. Be careful, there are places in the code which actually
150 ;;; use ..SLOT-UNBOUND.. rather than this variable. So much for
153 ;;; FIXME: Now that we're tightly integrated into SBCL, we could use
154 ;;; the SBCL built-in unbound value token instead. Perhaps if we did
155 ;;; so it would be a good idea to define collections of CLOS slots as
156 ;;; a new type of heap object, instead of using bare SIMPLE-VECTOR, in
157 ;;; order to avoid problems (in the debugger if nowhere else) with
158 ;;; SIMPLE-VECTORs some of whose elements are unbound tokens.
159 (defconstant +slot-unbound+ '..slot-unbound..)
161 (defmacro %allocate-static-slot-storage--class (no-of-slots)
162 `(make-array ,no-of-slots :initial-element +slot-unbound+))
164 (defmacro std-instance-class (instance)
165 `(wrapper-class* (std-instance-wrapper ,instance)))
167 ;;; When given a funcallable instance, SET-FUN-NAME *must* side-effect
168 ;;; that FIN to give it the name. When given any other kind of
169 ;;; function SET-FUN-NAME is allowed to return a new function which is
170 ;;; "the same" except that it has the name.
172 ;;; In all cases, SET-FUN-NAME must return the new (or same)
173 ;;; function. (Unlike other functions to set stuff, it does not return
175 (defun set-fun-name (fun new-name)
177 "Set the name of a compiled function object. Return the function."
178 (declare (special *boot-state* *the-class-standard-generic-function*))
179 (when (valid-function-name-p fun)
180 (setq fun (fdefinition fun)))
181 (when (funcallable-instance-p fun)
182 (if (if (eq *boot-state* 'complete)
183 (typep fun 'generic-function)
184 (eq (class-of fun) *the-class-standard-generic-function*))
185 (setf (%funcallable-instance-info fun 2) new-name)
186 (bug "unanticipated function type")))
187 ;; Fixup name-to-function mappings in cases where the function
188 ;; hasn't been defined by DEFUN. (FIXME: is this right? This logic
189 ;; comes from CMUCL). -- CSR, 2004-12-31
190 (when (and (consp new-name)
191 (member (car new-name) '(slow-method fast-method slot-accessor)))
192 (setf (fdefinition new-name) fun))
195 ;;; FIXME: probably no longer needed after init
196 (defmacro precompile-random-code-segments (&optional system)
198 (eval-when (:compile-toplevel)
199 (update-dispatch-dfuns))
200 (precompile-function-generators ,system)
201 (precompile-dfun-constructors ,system)
204 ;;; This definition is for interpreted code.
205 (defun pcl-instance-p (x)
206 (typep (layout-of x) 'wrapper))
209 ;;; We define this as STANDARD-INSTANCE, since we're going to
210 ;;; clobber the layout with some standard-instance layout as soon as
211 ;;; we make it, and we want the accessor to still be type-correct.
213 (defstruct (standard-instance
215 (:constructor %%allocate-instance--class ())
217 (:alternate-metaclass instance
219 make-standard-class))
222 (!defstruct-with-alternate-metaclass standard-instance
223 :slot-names (slots hash-code)
224 :boa-constructor %make-standard-instance
226 :metaclass-name standard-classoid
227 :metaclass-constructor make-standard-classoid
229 :runtime-type-checks-p nil)
231 ;;; Both of these operations "work" on structures, which allows the above
232 ;;; weakening of STD-INSTANCE-P.
233 (defmacro std-instance-slots (x) `(%instance-ref ,x 1))
234 (defmacro std-instance-wrapper (x) `(%instance-layout ,x))
235 ;;; KLUDGE: This one doesn't "work" on structures. However, we
236 ;;; ensure, in SXHASH and friends, never to call it on structures.
237 (defmacro std-instance-hash (x) `(%instance-ref ,x 2))
239 ;;; FIXME: These functions are called every place we do a
240 ;;; CALL-NEXT-METHOD, and probably other places too. It's likely worth
241 ;;; selectively optimizing them with DEFTRANSFORMs and stuff, rather
242 ;;; than just indiscriminately expanding them inline everywhere.
243 (declaim (inline get-slots get-slots-or-nil))
244 (declaim (ftype (function (t) simple-vector) get-slots))
245 (declaim (ftype (function (t) (or simple-vector null)) get-slots-or-nil))
246 (defun get-slots (instance)
247 (if (std-instance-p instance)
248 (std-instance-slots instance)
249 (fsc-instance-slots instance)))
250 (defun get-slots-or-nil (instance)
251 ;; Suppress a code-deletion note. FIXME: doing the FIXME above,
252 ;; integrating PCL more with the compiler, would remove the need for
254 (declare (optimize (inhibit-warnings 3)))
255 (when (pcl-instance-p instance)
256 (get-slots instance)))
258 (defmacro built-in-or-structure-wrapper (x) `(layout-of ,x))
260 (defmacro get-wrapper (inst)
261 (once-only ((wrapper `(wrapper-of ,inst)))
263 (aver (typep ,wrapper 'wrapper))
266 ;;; FIXME: could be an inline function or ordinary function (like many
267 ;;; other things around here)
268 (defmacro get-instance-wrapper-or-nil (inst)
269 (once-only ((wrapper `(wrapper-of ,inst)))
270 `(if (typep ,wrapper 'wrapper)
274 ;;;; support for useful hashing of PCL instances
276 (defvar *instance-hash-code-random-state* (make-random-state))
277 (defun get-instance-hash-code ()
278 ;; ANSI SXHASH wants us to make a good-faith effort to produce
279 ;; hash-codes that are well distributed within the range of
280 ;; non-negative fixnums, and this RANDOM operation does that, unlike
281 ;; the sbcl<=0.8.16 implementation of this operation as
284 ;; Hopefully there was no virtue to the old counter implementation
285 ;; that I am insufficiently insightful to insee. -- WHN 2004-10-28
286 (random most-positive-fixnum
287 *instance-hash-code-random-state*))
289 (defun sb-impl::sxhash-instance (x)
291 ((std-instance-p x) (std-instance-hash x))
292 ((fsc-instance-p x) (fsc-instance-hash x))
293 (t (bug "SXHASH-INSTANCE called on some weird thing: ~S" x))))
295 ;;;; structure-instance stuff
297 ;;;; FIXME: Now that the code is SBCL-only, this extra layer of
298 ;;;; abstraction around our native structure representation doesn't
299 ;;;; seem to add anything useful, and could probably go away.
301 ;;; The definition of STRUCTURE-TYPE-P was moved to early-low.lisp.
303 (defun get-structure-dd (type)
304 (layout-info (classoid-layout (find-classoid type))))
306 (defun structure-type-included-type-name (type)
307 (let ((include (dd-include (get-structure-dd type))))
312 (defun structure-type-slot-description-list (type)
313 (nthcdr (length (let ((include (structure-type-included-type-name type)))
315 (dd-slots (get-structure-dd include)))))
316 (dd-slots (get-structure-dd type))))
318 (defun structure-slotd-name (slotd)
321 (defun structure-slotd-accessor-symbol (slotd)
322 (dsd-accessor-name slotd))
324 (defun structure-slotd-reader-function (slotd)
325 (fdefinition (dsd-accessor-name slotd)))
327 (defun structure-slotd-writer-function (type slotd)
328 (if (dsd-read-only slotd)
329 (let ((dd (get-structure-dd type)))
330 (coerce (slot-setter-lambda-form dd slotd) 'function))
331 (fdefinition `(setf ,(dsd-accessor-name slotd)))))
333 (defun structure-slotd-type (slotd)
336 (defun structure-slotd-init-form (slotd)
339 ;;; WITH-PCL-LOCK is used around some forms that were previously
340 ;;; protected by WITHOUT-INTERRUPTS, but in a threaded SBCL we don't
341 ;;; have a useful WITHOUT-INTERRUPTS. In an unthreaded SBCL I'm not
342 ;;; sure what the desired effect is anyway: should we be protecting
343 ;;; against the possibility of recursive calls into these functions
344 ;;; or are we using WITHOUT-INTERRUPTS as WITHOUT-SCHEDULING?
346 ;;; Users: FORCE-CACHE-FLUSHES, MAKE-INSTANCES-OBSOLETE. Note that
347 ;;; it's not all certain this is sufficent for threadsafety: do we
348 ;;; just have to protect against simultaneous calls to these mutators,
349 ;;; or actually to stop normal slot access etc at the same time as one
354 (defvar *pcl-lock* (sb-thread::make-spinlock))
356 (defmacro with-pcl-lock (&body body)
357 `(sb-thread::with-spinlock (*pcl-lock*)
361 (defmacro with-pcl-lock (&body body)