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 ;;; This formerly punted with slightly greater than 50% probability,
57 ;;; and there was a periodicity to the nonrandomess.
58 ;;; If that was intentional, it should have been commented to that effect.
59 (defmacro randomly-punting-lambda
(lambda-list &body body
)
60 (with-unique-names (drops drop-pos
)
61 `(let ((,drops
(random-fixnum)) ; means a POSITIVE fixnum
62 (,drop-pos sb-vm
:n-positive-fixnum-bits
))
63 (declare (fixnum ,drops
)
64 (type (mod #.sb-vm
:n-fixnum-bits
) ,drop-pos
))
66 (when (logbitp (the unsigned-byte
(decf ,drop-pos
)) ,drops
)
68 (when (zerop ,drop-pos
)
69 (setf ,drops
(random-fixnum)
70 ,drop-pos sb-vm
:n-positive-fixnum-bits
))))))
72 ;;;; PCL's view of funcallable instances
74 (!defstruct-with-alternate-metaclass standard-funcallable-instance
75 ;; KLUDGE: Note that neither of these slots is ever accessed by its
76 ;; accessor name as of sbcl-0.pre7.63. Presumably everything works
77 ;; by puns based on absolute locations. Fun fun fun.. -- WHN 2001-10-30
78 :slot-names
(clos-slots name hash-code
)
79 :boa-constructor %make-standard-funcallable-instance
80 :superclass-name function
81 :metaclass-name standard-classoid
82 :metaclass-constructor make-standard-classoid
83 :dd-type funcallable-structure
84 ;; Only internal implementation code will access these, and these
85 ;; accesses (slot readers in particular) could easily be a
86 ;; bottleneck, so it seems reasonable to suppress runtime type
89 ;; (Except note KLUDGE above that these accessors aren't used at all
90 ;; (!) as of sbcl-0.pre7.63, so for now it's academic.)
91 :runtime-type-checks-p nil
)
93 (import 'sb-kernel
:funcallable-instance-p
)
95 (defun set-funcallable-instance-function (fin new-value
)
96 (declare (type function new-value
)
97 ;; KLUDGE: it might be nice to restrict
98 ;; SB-MOP:SET-FUNCALLABLE-INSTANCE-FUNCTION to operate only
99 ;; on generalized instances of
100 ;; SB-MOP:FUNCALLABLE-STANDARD-OBJECT; at present, even
101 ;; PCL's internal use of SET-FUNCALLABLE-INSTANCE-FUNCTION
102 ;; doesn't obey this restriction.
103 (type funcallable-instance fin
))
104 (setf (funcallable-instance-fun fin
) new-value
))
106 ;;; FIXME: these macros should just go away. It's not clear whether
107 ;;; the inline functions defined by
108 ;;; !DEFSTRUCT-WITH-ALTERNATE-METACLASS are as efficient as they could
109 ;;; be; ordinary defstruct accessors are defined as source transforms.
110 (defun fsc-instance-p (fin)
111 (funcallable-instance-p fin
))
112 (define-compiler-macro fsc-instance-p
(fin)
113 `(funcallable-instance-p ,fin
))
114 (defmacro fsc-instance-wrapper
(fin)
115 `(%funcallable-instance-layout
,fin
))
116 (defmacro fsc-instance-slots
(fin)
117 `(%funcallable-instance-info
,fin
1))
118 (defmacro fsc-instance-hash
(fin)
119 `(%funcallable-instance-info
,fin
3))
121 (declaim (inline clos-slots-ref
(setf clos-slots-ref
)))
122 (declaim (ftype (function (simple-vector index
) t
) clos-slots-ref
))
123 (defun clos-slots-ref (slots index
)
125 (declaim (ftype (function (t simple-vector index
) t
) (setf clos-slots-ref
)))
126 (defun (setf clos-slots-ref
) (new-value slots index
)
127 (setf (svref slots index
) new-value
))
129 ;;; Note on implementation under CMU CL >=17 and SBCL: STD-INSTANCE-P
130 ;;; is only used to discriminate between functions (including FINs)
131 ;;; and normal instances, so we can return true on structures also. A
132 ;;; few uses of (OR STD-INSTANCE-P FSC-INSTANCE-P) are changed to
134 (defun std-instance-p (x)
136 (define-compiler-macro std-instance-p
(x)
139 ;; a temporary definition used for debugging the bootstrap
141 (defun print-std-instance (instance stream depth
)
142 (declare (ignore depth
))
143 (print-unreadable-object (instance stream
:type t
:identity t
)
144 (let ((class (class-of instance
)))
145 (when (or (eq class
(find-class 'standard-class nil
))
146 (eq class
(find-class 'funcallable-standard-class nil
))
147 (eq class
(find-class 'system-class nil
))
148 (eq class
(find-class 'built-in-class nil
)))
149 (princ (early-class-name instance
) stream
)))))
151 ;;; This is the value that we stick into a slot to tell us that it is
152 ;;; unbound. It may seem gross, but for performance reasons, we make
153 ;;; this an interned symbol. That means that the fast check to see
154 ;;; whether a slot is unbound is to say (EQ <val> '..SLOT-UNBOUND..).
155 ;;; That is considerably faster than looking at the value of a special
158 ;;; It seems only reasonable to also export this for users, since
159 ;;; otherwise dealing with STANDARD-INSTANCE-ACCESS becomes harder
160 ;;; -- and slower -- than it needs to be.
161 (defconstant +slot-unbound
+ '..slot-unbound..
163 "SBCL specific extensions to MOP: if this value is read from an
164 instance using STANDARD-INSTANCE-ACCESS, the slot is unbound.
165 Similarly, an :INSTANCE allocated slot can be made unbound by
166 assigning this to it using (SETF STANDARD-INSTANCE-ACCESS).
168 Value of +SLOT-UNBOUND+ is unspecified, and should not be relied to be
169 of any particular type, but it is guaranteed to be suitable for EQ
172 (defmacro std-instance-class
(instance)
173 `(wrapper-class* (std-instance-wrapper ,instance
)))
175 ;;; When given a funcallable instance, SET-FUN-NAME *must* side-effect
176 ;;; that FIN to give it the name. When given any other kind of
177 ;;; function SET-FUN-NAME is allowed to return a new function which is
178 ;;; "the same" except that it has the name.
180 ;;; In all cases, SET-FUN-NAME must return the new (or same)
181 ;;; function. (Unlike other functions to set stuff, it does not return
183 (declaim (ftype function class-of
))
184 (defun set-fun-name (fun new-name
)
186 "Set the name of a compiled function object. Return the function."
187 (when (valid-function-name-p fun
)
188 (setq fun
(fdefinition fun
)))
190 (%method-function
(setf (%method-function-name fun
) new-name
))
192 (sb-eval:interpreted-function
193 (setf (sb-eval:interpreted-function-name fun
) new-name
))
195 (setq fun
(sb-impl::set-closure-name fun new-name
)))
196 (funcallable-instance ;; KLUDGE: probably a generic function...
197 (cond ((if (eq **boot-state
** 'complete
)
198 (typep fun
'generic-function
) ; FIXME: inefficient forward-ref
199 (eq (class-of fun
) *the-class-standard-generic-function
*))
200 (setf (%funcallable-instance-info fun
2) new-name
))
202 (bug "unanticipated function type")))))
203 ;; Fixup name-to-function mappings in cases where the function
204 ;; hasn't been defined by DEFUN. (FIXME: is this right? This logic
205 ;; comes from CMUCL). -- CSR, 2004-12-31
206 (when (and (consp new-name
)
207 (member (car new-name
) '(slow-method fast-method slot-accessor
)))
208 (setf (fdefinition new-name
) fun
))
211 ;;; FIXME: probably no longer needed after init
212 (defmacro precompile-random-code-segments
(&optional system
)
214 (eval-when (:compile-toplevel
)
215 (update-dispatch-dfuns))
216 (precompile-function-generators ,system
)
217 (precompile-dfun-constructors ,system
)
220 ;;; Return true of any object which is either a funcallable-instance,
221 ;;; or an ordinary instance that is not a structure-object.
222 ;;; This used to be implemented as (LAYOUT-FOR-STD-CLASS-P (LAYOUT-OF x))
223 ;;; but LAYOUT-OF is more general than need be here. So this bails out
224 ;;; after the first two clauses of the equivalent COND in LAYOUT-OF
225 ;;; because nothing else could possibly return T.
226 (declaim (inline %pcl-instance-p
))
227 (defun %pcl-instance-p
(x)
228 (layout-for-std-class-p
229 (cond ((%instancep x
) (%instance-layout x
))
230 ((funcallable-instance-p x
) (%funcallable-instance-layout x
))
231 (t (return-from %pcl-instance-p nil
)))))
233 ;;; This definition is for interpreted code.
234 (defun pcl-instance-p (x) (%pcl-instance-p x
))
237 ;;; We define this as STANDARD-INSTANCE, since we're going to
238 ;;; clobber the layout with some standard-instance layout as soon as
239 ;;; we make it, and we want the accessor to still be type-correct.
241 (defstruct (standard-instance
243 (:constructor %%allocate-instance--class
())
245 (:alternate-metaclass instance
247 make-standard-class
))
250 (!defstruct-with-alternate-metaclass standard-instance
251 :slot-names
(slots hash-code
)
252 :boa-constructor %make-standard-instance
254 :metaclass-name standard-classoid
255 :metaclass-constructor make-standard-classoid
257 :runtime-type-checks-p nil
)
259 ;;; Both of these operations "work" on structures, which allows the above
260 ;;; weakening of STD-INSTANCE-P.
261 (defmacro std-instance-slots
(x) `(%instance-ref
,x
1))
262 (defmacro std-instance-wrapper
(x) `(%instance-layout
,x
))
263 ;;; KLUDGE: This one doesn't "work" on structures. However, we
264 ;;; ensure, in SXHASH and friends, never to call it on structures.
265 (defmacro std-instance-hash
(x) `(%instance-ref
,x
2))
267 ;;; FIXME: These functions are called every place we do a
268 ;;; CALL-NEXT-METHOD, and probably other places too. It's likely worth
269 ;;; selectively optimizing them with DEFTRANSFORMs and stuff, rather
270 ;;; than just indiscriminately expanding them inline everywhere.
271 (declaim (inline get-slots get-slots-or-nil
))
272 (declaim (ftype (function (t) simple-vector
) get-slots
))
273 (declaim (ftype (function (t) (or simple-vector null
)) get-slots-or-nil
))
274 (defun get-slots (instance)
275 (if (std-instance-p instance
)
276 (std-instance-slots instance
)
277 (fsc-instance-slots instance
)))
278 (defun get-slots-or-nil (instance)
279 ;; Suppress a code-deletion note. FIXME: doing the FIXME above,
280 ;; integrating PCL more with the compiler, would remove the need for
282 (declare (optimize (inhibit-warnings 3)))
283 (when (pcl-instance-p instance
)
284 (get-slots instance
)))
286 ;; This macro is used only by %CHANGE-CLASS. Can we just do this there?
287 ;; [The code in 'fsc.lisp' which looks like it needs it is commented out]
288 (defmacro get-wrapper
(inst)
289 (once-only ((wrapper `(layout-of ,inst
)))
291 (aver (layout-for-std-class-p ,wrapper
))
294 ;;;; support for useful hashing of PCL instances
296 (defvar *instance-hash-code-random-state
* (make-random-state))
297 (defun get-instance-hash-code ()
298 ;; ANSI SXHASH wants us to make a good-faith effort to produce
299 ;; hash-codes that are well distributed within the range of
300 ;; non-negative fixnums, and this RANDOM operation does that, unlike
301 ;; the sbcl<=0.8.16 implementation of this operation as
304 ;; Hopefully there was no virtue to the old counter implementation
305 ;; that I am insufficiently insightful to insee. -- WHN 2004-10-28
306 (random most-positive-fixnum
307 *instance-hash-code-random-state
*))
309 (defun sb-impl::sxhash-instance
(x)
311 ((std-instance-p x
) (std-instance-hash x
))
312 ((fsc-instance-p x
) (fsc-instance-hash x
))
313 (t (bug "SXHASH-INSTANCE called on some weird thing: ~S" x
))))
315 ;;;; structure-instance stuff
317 ;;;; FIXME: Now that the code is SBCL-only, this extra layer of
318 ;;;; abstraction around our native structure representation doesn't
319 ;;;; seem to add anything useful, and could probably go away.
321 ;;; The definition of STRUCTURE-TYPE-P was moved to early-low.lisp.
323 (defun structure-type-slot-description-list (type)
324 (let* ((dd (find-defstruct-description type
))
325 (include (dd-include dd
))
326 (all-slots (dd-slots dd
)))
327 (multiple-value-bind (super slot-overrides
)
329 (values (car include
) (mapcar #'car
(cdr include
)))
330 (values include nil
))
331 (let ((included-slots
333 (dd-slots (find-defstruct-description super
)))))
334 (loop for slot
= (pop all-slots
)
335 for included-slot
= (pop included-slots
)
337 when
(or (not included-slot
)
338 (member (dsd-name included-slot
) slot-overrides
:test
#'eq
))
341 (defun uninitialized-accessor-function (type slotd
)
343 (declare (ignore args
))
344 (error "~:(~A~) function~@[ for ~S ~] not yet initialized."
347 (defun structure-slotd-name (slotd)
350 (defun structure-slotd-accessor-symbol (slotd)
351 (dsd-accessor-name slotd
))
353 (defun structure-slotd-reader-function (slotd)
354 (let ((name (dsd-accessor-name slotd
)))
357 (uninitialized-accessor-function :reader slotd
))))
359 (defun structure-slotd-writer-function (type slotd
)
360 (if (dsd-read-only slotd
)
361 (let ((dd (find-defstruct-description type
)))
362 (coerce (slot-setter-lambda-form dd slotd
) 'function
))
363 (let ((name `(setf ,(dsd-accessor-name slotd
))))
366 (uninitialized-accessor-function :writer slotd
)))))
368 (defun structure-slotd-type (slotd)
371 (defun structure-slotd-init-form (slotd)
374 ;;; method function stuff.
376 ;;; PCL historically included a so-called method-fast-function, which
377 ;;; is essentially a method function but with (a) a precomputed
378 ;;; continuation for CALL-NEXT-METHOD and (b) a permutation vector for
379 ;;; slot access. [ FIXME: see if we can understand these two
380 ;;; optimizations before commit. ] However, the presence of the
381 ;;; fast-function meant that we violated AMOP and the effect of the
382 ;;; :FUNCTION initarg, and furthermore got to potentially confusing
383 ;;; situations where the function and the fast-function got out of
384 ;;; sync, so that calling (method-function method) with the defined
385 ;;; protocol would do different things from (call-method method) in
386 ;;; method combination.
388 ;;; So we define this internal method function structure, which we use
389 ;;; when we create a method function ourselves. This means that we
390 ;;; can hang the various bits of information that we want off the
391 ;;; method function itself, and also that if a user overrides method
392 ;;; function creation there is no danger of having the system get
394 (!defstruct-with-alternate-metaclass %method-function
395 :slot-names
(fast-function name
)
396 :boa-constructor %make-method-function
397 :superclass-name function
398 :metaclass-name static-classoid
399 :metaclass-constructor make-static-classoid
400 :dd-type funcallable-structure
)