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) (sb-ext:inhibit-warnings 3)))
45 (defmacro dotimes-fixnum ((var count &optional (result nil)) &body body)
46 `(dotimes (,var (the fixnum ,count) ,result)
47 (declare (fixnum ,var))
50 (declaim (inline random-fixnum))
51 (defun random-fixnum ()
52 (random (1+ most-positive-fixnum)))
54 ;;; Lambda which executes its body (or not) randomly. Used to drop
55 ;;; random cache entries.
56 (defmacro randomly-punting-lambda (lambda-list &body body)
57 (with-unique-names (drops drop-pos)
58 `(let ((,drops (random-fixnum))
59 (,drop-pos sb-vm:n-fixnum-bits))
60 (declare (fixnum ,drops)
61 (type (integer 0 #.sb-vm:n-fixnum-bits) ,drop-pos))
63 (when (logbitp (the unsigned-byte (decf ,drop-pos)) ,drops)
65 (when (zerop ,drop-pos)
66 (setf ,drops (random-fixnum)
67 ,drop-pos sb-vm:n-fixnum-bits))))))
69 ;;;; early definition of WRAPPER
71 ;;;; Most WRAPPER stuff is defined later, but the DEFSTRUCT itself
72 ;;;; is here early so that things like (TYPEP .. 'WRAPPER) can be
73 ;;;; compiled efficiently.
75 ;;; Note that for SBCL, as for CMU CL, the WRAPPER of a built-in or
76 ;;; structure class will be some other kind of SB-KERNEL:LAYOUT, but
77 ;;; this shouldn't matter, since the only two slots that WRAPPER adds
78 ;;; are meaningless in those cases.
81 ;; KLUDGE: In CMU CL, the initialization default
82 ;; for LAYOUT-INVALID was NIL. In SBCL, that has
83 ;; changed to :UNINITIALIZED, but PCL code might
84 ;; still expect NIL for the initialization
85 ;; default of WRAPPER-INVALID. Instead of trying
86 ;; to find out, I just overrode the LAYOUT
87 ;; default here. -- WHN 19991204
89 ;; This allows quick testing of wrapperness.
91 (:constructor make-wrapper-internal)
93 (slots () :type list))
94 #-sb-fluid (declaim (sb-ext:freeze-type wrapper))
96 ;;;; PCL's view of funcallable instances
98 (!defstruct-with-alternate-metaclass standard-funcallable-instance
99 ;; KLUDGE: Note that neither of these slots is ever accessed by its
100 ;; accessor name as of sbcl-0.pre7.63. Presumably everything works
101 ;; by puns based on absolute locations. Fun fun fun.. -- WHN 2001-10-30
102 :slot-names (clos-slots name hash-code)
103 :boa-constructor %make-standard-funcallable-instance
104 :superclass-name function
105 :metaclass-name standard-classoid
106 :metaclass-constructor make-standard-classoid
107 :dd-type funcallable-structure
108 ;; Only internal implementation code will access these, and these
109 ;; accesses (slot readers in particular) could easily be a
110 ;; bottleneck, so it seems reasonable to suppress runtime type
113 ;; (Except note KLUDGE above that these accessors aren't used at all
114 ;; (!) as of sbcl-0.pre7.63, so for now it's academic.)
115 :runtime-type-checks-p nil)
117 (import 'sb-kernel:funcallable-instance-p)
119 (defun set-funcallable-instance-function (fin new-value)
120 (declare (type function new-value)
121 ;; KLUDGE: it might be nice to restrict
122 ;; SB-MOP:SET-FUNCALLABLE-INSTANCE-FUNCTION to operate only
123 ;; on generalized instances of
124 ;; SB-MOP:FUNCALLABLE-STANDARD-OBJECT; at present, even
125 ;; PCL's internal use of SET-FUNCALLABLE-INSTANCE-FUNCTION
126 ;; doesn't obey this restriction.
127 (type funcallable-instance fin))
128 (setf (funcallable-instance-fun fin) new-value))
130 ;;; FIXME: these macros should just go away. It's not clear whether
131 ;;; the inline functions defined by
132 ;;; !DEFSTRUCT-WITH-ALTERNATE-METACLASS are as efficient as they could
133 ;;; be; ordinary defstruct accessors are defined as source transforms.
134 (defun fsc-instance-p (fin)
135 (funcallable-instance-p fin))
136 (define-compiler-macro fsc-instance-p (fin)
137 `(funcallable-instance-p ,fin))
138 (defmacro fsc-instance-wrapper (fin)
139 `(%funcallable-instance-layout ,fin))
140 (defmacro fsc-instance-slots (fin)
141 `(%funcallable-instance-info ,fin 1))
142 (defmacro fsc-instance-hash (fin)
143 `(%funcallable-instance-info ,fin 3))
145 (declaim (inline clos-slots-ref (setf clos-slots-ref)))
146 (declaim (ftype (function (simple-vector index) t) clos-slots-ref))
147 (defun clos-slots-ref (slots index)
149 (declaim (ftype (function (t simple-vector index) t) (setf clos-slots-ref)))
150 (defun (setf clos-slots-ref) (new-value slots index)
151 (setf (svref slots index) new-value))
153 ;;; Note on implementation under CMU CL >=17 and SBCL: STD-INSTANCE-P
154 ;;; is only used to discriminate between functions (including FINs)
155 ;;; and normal instances, so we can return true on structures also. A
156 ;;; few uses of (OR STD-INSTANCE-P FSC-INSTANCE-P) are changed to
158 (defun std-instance-p (x)
160 (define-compiler-macro std-instance-p (x)
163 ;; a temporary definition used for debugging the bootstrap
165 (defun print-std-instance (instance stream depth)
166 (declare (ignore depth))
167 (print-unreadable-object (instance stream :type t :identity t)
168 (let ((class (class-of instance)))
169 (when (or (eq class (find-class 'standard-class nil))
170 (eq class (find-class 'funcallable-standard-class nil))
171 (eq class (find-class 'built-in-class nil)))
172 (princ (early-class-name instance) stream)))))
174 ;;; This is the value that we stick into a slot to tell us that it is
175 ;;; unbound. It may seem gross, but for performance reasons, we make
176 ;;; this an interned symbol. That means that the fast check to see
177 ;;; whether a slot is unbound is to say (EQ <val> '..SLOT-UNBOUND..).
178 ;;; That is considerably faster than looking at the value of a special
181 ;;; It seems only reasonable to also export this for users, since
182 ;;; otherwise dealing with STANDARD-INSTANCE-ACCESS becomes harder
183 ;;; -- and slower -- than it needs to be.
184 (defconstant +slot-unbound+ '..slot-unbound..
185 "SBCL specific extentions to MOP: if this value is read from an
186 instance using STANDARD-INSTANCE-ACCESS, the slot is unbound.
187 Similarly, an :INSTANCE allocated slot can be made unbound by
188 assigning this to it using (SETF STANDARD-INSTANCE-ACCESS).
190 Value of +SLOT-UNBOUND+ is unspecified, and should not be relied to be
191 of any particular type, but it is guaranteed to be suitable for EQ
194 (defmacro %allocate-static-slot-storage--class (no-of-slots)
195 `(make-array ,no-of-slots :initial-element +slot-unbound+))
197 (defmacro std-instance-class (instance)
198 `(wrapper-class* (std-instance-wrapper ,instance)))
200 ;;; When given a funcallable instance, SET-FUN-NAME *must* side-effect
201 ;;; that FIN to give it the name. When given any other kind of
202 ;;; function SET-FUN-NAME is allowed to return a new function which is
203 ;;; "the same" except that it has the name.
205 ;;; In all cases, SET-FUN-NAME must return the new (or same)
206 ;;; function. (Unlike other functions to set stuff, it does not return
208 (defun set-fun-name (fun new-name)
210 "Set the name of a compiled function object. Return the function."
211 (when (valid-function-name-p fun)
212 (setq fun (fdefinition fun)))
214 (%method-function (setf (%method-function-name fun) new-name))
216 (sb-eval:interpreted-function
217 (setf (sb-eval:interpreted-function-name fun) new-name))
218 (funcallable-instance ;; KLUDGE: probably a generic function...
219 (cond ((if (eq **boot-state** 'complete)
220 (typep fun 'generic-function)
221 (eq (class-of fun) *the-class-standard-generic-function*))
222 (setf (%funcallable-instance-info fun 2) new-name))
224 (bug "unanticipated function type")))))
225 ;; Fixup name-to-function mappings in cases where the function
226 ;; hasn't been defined by DEFUN. (FIXME: is this right? This logic
227 ;; comes from CMUCL). -- CSR, 2004-12-31
228 (when (and (consp new-name)
229 (member (car new-name) '(slow-method fast-method slot-accessor)))
230 (setf (fdefinition new-name) fun))
233 ;;; FIXME: probably no longer needed after init
234 (defmacro precompile-random-code-segments (&optional system)
236 (eval-when (:compile-toplevel)
237 (update-dispatch-dfuns))
238 (precompile-function-generators ,system)
239 (precompile-dfun-constructors ,system)
242 ;;; This definition is for interpreted code.
243 (defun pcl-instance-p (x)
244 (typep (layout-of x) 'wrapper))
247 ;;; We define this as STANDARD-INSTANCE, since we're going to
248 ;;; clobber the layout with some standard-instance layout as soon as
249 ;;; we make it, and we want the accessor to still be type-correct.
251 (defstruct (standard-instance
253 (:constructor %%allocate-instance--class ())
255 (:alternate-metaclass instance
257 make-standard-class))
260 (!defstruct-with-alternate-metaclass standard-instance
261 :slot-names (slots hash-code)
262 :boa-constructor %make-standard-instance
264 :metaclass-name standard-classoid
265 :metaclass-constructor make-standard-classoid
267 :runtime-type-checks-p nil)
269 ;;; Both of these operations "work" on structures, which allows the above
270 ;;; weakening of STD-INSTANCE-P.
271 (defmacro std-instance-slots (x) `(%instance-ref ,x 1))
272 (defmacro std-instance-wrapper (x) `(%instance-layout ,x))
273 ;;; KLUDGE: This one doesn't "work" on structures. However, we
274 ;;; ensure, in SXHASH and friends, never to call it on structures.
275 (defmacro std-instance-hash (x) `(%instance-ref ,x 2))
277 ;;; FIXME: These functions are called every place we do a
278 ;;; CALL-NEXT-METHOD, and probably other places too. It's likely worth
279 ;;; selectively optimizing them with DEFTRANSFORMs and stuff, rather
280 ;;; than just indiscriminately expanding them inline everywhere.
281 (declaim (inline get-slots get-slots-or-nil))
282 (declaim (ftype (function (t) simple-vector) get-slots))
283 (declaim (ftype (function (t) (or simple-vector null)) get-slots-or-nil))
284 (defun get-slots (instance)
285 (if (std-instance-p instance)
286 (std-instance-slots instance)
287 (fsc-instance-slots instance)))
288 (defun get-slots-or-nil (instance)
289 ;; Suppress a code-deletion note. FIXME: doing the FIXME above,
290 ;; integrating PCL more with the compiler, would remove the need for
292 (declare (optimize (inhibit-warnings 3)))
293 (when (pcl-instance-p instance)
294 (get-slots instance)))
296 (defmacro get-wrapper (inst)
297 (once-only ((wrapper `(wrapper-of ,inst)))
299 (aver (typep ,wrapper 'wrapper))
302 ;;; FIXME: could be an inline function or ordinary function (like many
303 ;;; other things around here)
304 (defmacro get-instance-wrapper-or-nil (inst)
305 (once-only ((wrapper `(wrapper-of ,inst)))
306 `(if (typep ,wrapper 'wrapper)
310 ;;;; support for useful hashing of PCL instances
312 (defvar *instance-hash-code-random-state* (make-random-state))
313 (defun get-instance-hash-code ()
314 ;; ANSI SXHASH wants us to make a good-faith effort to produce
315 ;; hash-codes that are well distributed within the range of
316 ;; non-negative fixnums, and this RANDOM operation does that, unlike
317 ;; the sbcl<=0.8.16 implementation of this operation as
320 ;; Hopefully there was no virtue to the old counter implementation
321 ;; that I am insufficiently insightful to insee. -- WHN 2004-10-28
322 (random most-positive-fixnum
323 *instance-hash-code-random-state*))
325 (defun sb-impl::sxhash-instance (x)
327 ((std-instance-p x) (std-instance-hash x))
328 ((fsc-instance-p x) (fsc-instance-hash x))
329 (t (bug "SXHASH-INSTANCE called on some weird thing: ~S" x))))
331 ;;;; structure-instance stuff
333 ;;;; FIXME: Now that the code is SBCL-only, this extra layer of
334 ;;;; abstraction around our native structure representation doesn't
335 ;;;; seem to add anything useful, and could probably go away.
337 ;;; The definition of STRUCTURE-TYPE-P was moved to early-low.lisp.
339 (defun structure-type-slot-description-list (type)
340 (let* ((dd (find-defstruct-description type))
341 (include (dd-include dd))
342 (all-slots (dd-slots dd)))
343 (multiple-value-bind (super slot-overrides)
345 (values (car include) (mapcar #'car (cdr include)))
346 (values include nil))
347 (let ((included-slots
349 (dd-slots (find-defstruct-description super)))))
350 (loop for slot = (pop all-slots)
351 for included-slot = (pop included-slots)
353 when (or (not included-slot)
354 (member (dsd-name included-slot) slot-overrides :test #'eq))
357 (defun uninitialized-accessor-function (type slotd)
359 (declare (ignore args))
360 (error "~:(~A~) function~@[ for ~S ~] not yet initialized."
363 (defun structure-slotd-name (slotd)
366 (defun structure-slotd-accessor-symbol (slotd)
367 (dsd-accessor-name slotd))
369 (defun structure-slotd-reader-function (slotd)
370 (let ((name (dsd-accessor-name slotd)))
373 (uninitialized-accessor-function :reader slotd))))
375 (defun structure-slotd-writer-function (type slotd)
376 (if (dsd-read-only slotd)
377 (let ((dd (find-defstruct-description type)))
378 (coerce (slot-setter-lambda-form dd slotd) 'function))
379 (let ((name `(setf ,(dsd-accessor-name slotd))))
382 (uninitialized-accessor-function :writer slotd)))))
384 (defun structure-slotd-type (slotd)
387 (defun structure-slotd-init-form (slotd)
390 ;;; method function stuff.
392 ;;; PCL historically included a so-called method-fast-function, which
393 ;;; is essentially a method function but with (a) a precomputed
394 ;;; continuation for CALL-NEXT-METHOD and (b) a permutation vector for
395 ;;; slot access. [ FIXME: see if we can understand these two
396 ;;; optimizations before commit. ] However, the presence of the
397 ;;; fast-function meant that we violated AMOP and the effect of the
398 ;;; :FUNCTION initarg, and furthermore got to potentially confusing
399 ;;; situations where the function and the fast-function got out of
400 ;;; sync, so that calling (method-function method) with the defined
401 ;;; protocol would do different things from (call-method method) in
402 ;;; method combination.
404 ;;; So we define this internal method function structure, which we use
405 ;;; when we create a method function ourselves. This means that we
406 ;;; can hang the various bits of information that we want off the
407 ;;; method function itself, and also that if a user overrides method
408 ;;; function creation there is no danger of having the system get
410 (!defstruct-with-alternate-metaclass %method-function
411 :slot-names (fast-function name)
412 :boa-constructor %make-method-function
413 :superclass-name function
414 :metaclass-name static-classoid
415 :metaclass-constructor make-static-classoid
416 :dd-type funcallable-structure)