Declaim types of %%data-vector-...%%.
[sbcl.git] / src / pcl / low.lisp
blobf7b2f6acecc952788db05d1a8c0ec3c29498313f
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)
15 ;;;; This software is part of the SBCL system. See the README file for more
16 ;;;; information.
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.
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.
38 (in-package "SB!PCL")
40 (eval-when (:compile-toplevel :load-toplevel :execute)
41 (defvar *optimize-speed*
42 '(optimize (speed 3) (safety 0) (sb!ext:inhibit-warnings 3)))
43 ) ; EVAL-WHEN
45 (defmacro dotimes-fixnum ((var count &optional (result nil)) &body body)
46 `(dotimes (,var (the fixnum ,count) ,result)
47 (declare (fixnum ,var))
48 ,@body))
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))
65 (lambda ,lambda-list
66 (when (logbitp (the unsigned-byte (decf ,drop-pos)) ,drops)
67 (locally ,@body))
68 (when (zerop ,drop-pos)
69 (setf ,drops (random-fixnum)
70 ,drop-pos sb!vm:n-positive-fixnum-bits))))))
72 (import 'sb!kernel:funcallable-instance-p) ; why?
74 (defun set-funcallable-instance-function (fin new-value)
75 (declare (type function new-value)
76 ;; KLUDGE: it might be nice to restrict
77 ;; SB-MOP:SET-FUNCALLABLE-INSTANCE-FUNCTION to operate only
78 ;; on generalized instances of
79 ;; SB-MOP:FUNCALLABLE-STANDARD-OBJECT; at present, even
80 ;; PCL's internal use of SET-FUNCALLABLE-INSTANCE-FUNCTION
81 ;; doesn't obey this restriction.
82 (type funcallable-instance fin))
83 (setf (funcallable-instance-fun fin) new-value))
85 ;;; FIXME: these macros should just go away. It's not clear whether
86 ;;; the inline functions defined by
87 ;;; !DEFSTRUCT-WITH-ALTERNATE-METACLASS are as efficient as they could
88 ;;; be; ordinary defstruct accessors are defined as source transforms.
89 (defun fsc-instance-p (fin)
90 (funcallable-instance-p fin))
91 (define-compiler-macro fsc-instance-p (fin)
92 `(funcallable-instance-p ,fin))
93 (defmacro fsc-instance-wrapper (fin)
94 `(%funcallable-instance-layout ,fin))
95 (defmacro fsc-instance-slots (fin)
96 `(%funcallable-instance-info ,fin 1))
98 (declaim (inline clos-slots-ref (setf clos-slots-ref)))
99 (declaim (ftype (function (simple-vector index) t) clos-slots-ref))
100 (defun clos-slots-ref (slots index)
101 (svref slots index))
102 (declaim (ftype (function (t simple-vector index) t) (setf clos-slots-ref)))
103 (defun (setf clos-slots-ref) (new-value slots index)
104 (setf (svref slots index) new-value))
106 ;;; Note on implementation under CMU CL >=17 and SBCL: STD-INSTANCE-P
107 ;;; is only used to discriminate between functions (including FINs)
108 ;;; and normal instances, so we can return true on structures also. A
109 ;;; few uses of (OR STD-INSTANCE-P FSC-INSTANCE-P) are changed to
110 ;;; PCL-INSTANCE-P.
111 (defun std-instance-p (x)
112 (%instancep x))
113 (define-compiler-macro std-instance-p (x)
114 `(%instancep ,x))
116 ;; a temporary definition used for debugging the bootstrap
117 #!+sb-show
118 (defun print-std-instance (instance stream depth)
119 (declare (ignore depth))
120 (print-unreadable-object (instance stream :type t :identity t)
121 (let ((class (class-of instance)))
122 (when (or (eq class (find-class 'standard-class nil))
123 (eq class (find-class 'funcallable-standard-class nil))
124 (eq class (find-class 'system-class nil))
125 (eq class (find-class 'built-in-class nil)))
126 (princ (early-class-name instance) stream)))))
128 (defmacro std-instance-class (instance)
129 `(wrapper-class* (std-instance-wrapper ,instance)))
131 ;;; When given a funcallable instance, SET-FUN-NAME *must* side-effect
132 ;;; that FIN to give it the name. When given any other kind of
133 ;;; function SET-FUN-NAME is allowed to return a new function which is
134 ;;; "the same" except that it has the name.
136 ;;; In all cases, SET-FUN-NAME must return the new (or same)
137 ;;; function. (Unlike other functions to set stuff, it does not return
138 ;;; the new value.)
139 ;; This is an absolutely terrible name for a function which both assigns
140 ;; the name slot of a function, and _sometimes_ binds a name to a function.
141 (defun set-fun-name (fun new-name)
142 #!+sb-doc
143 "Set the name of a compiled function object. Return the function."
144 (when (valid-function-name-p fun)
145 (setq fun (fdefinition fun)))
146 (typecase fun
147 (%method-function (setf (%method-function-name fun) new-name))
148 ;; a closure potentially becomes a different closure
149 (closure (setq fun (sb!impl::set-closure-name fun new-name)))
150 (t (setf (%fun-name fun) new-name)))
151 ;; Fixup name-to-function mappings in cases where the function
152 ;; hasn't been defined by DEFUN. (FIXME: is this right? This logic
153 ;; comes from CMUCL). -- CSR, 2004-12-31
155 ;; Now, given this logic is somewhat suspect to begin with, and is the final
156 ;; remaining contributor to the immortalization of EQL-specialized methods,
157 ;; I'm going to say that we don't create an fdefn for anything
158 ;; whose specializers are not symbols.
159 ;; Otherwise, adding+removing N methods named
160 ;; (SLOW-METHOD BLAH ((EQL <HAIRY-LIST-OBJECT>)))
161 ;; makes them all permanent because FDEFNs are compared by name EQUALity,
162 ;; so each gets its own FDEFN. This is bad, and pretty much useless anyway.
163 (when (and (consp new-name)
164 (or (eq (car new-name) 'slot-accessor)
165 (and (member (car new-name) '(slow-method fast-method))
166 ;; name is: ({SLOW|FAST}-METHOD root <qual>* spec+)
167 (every #'symbolp (car (last new-name))))))
168 (setf (fdefinition new-name) fun))
169 fun)
171 ;;; FIXME: probably no longer needed after init
172 (defmacro precompile-random-code-segments (&optional system)
173 `(progn
174 (eval-when (:compile-toplevel)
175 (update-dispatch-dfuns))
176 (precompile-function-generators ,system)
177 (precompile-dfun-constructors ,system)
178 (precompile-ctors)))
180 ;;; Return true of any object which is either a funcallable-instance,
181 ;;; or an ordinary instance that is not a structure-object.
182 ;;; This used to be implemented as (LAYOUT-FOR-STD-CLASS-P (LAYOUT-OF x))
183 ;;; but LAYOUT-OF is more general than need be here. So this bails out
184 ;;; after the first two clauses of the equivalent COND in LAYOUT-OF
185 ;;; because nothing else could possibly return T.
186 (declaim (inline %pcl-instance-p))
187 (defun %pcl-instance-p (x)
188 (layout-for-std-class-p
189 (cond ((%instancep x) (%instance-layout x))
190 ((funcallable-instance-p x) (%funcallable-instance-layout x))
191 (t (return-from %pcl-instance-p nil)))))
193 ;;; This definition is for interpreted code.
194 (defun pcl-instance-p (x) (declare (explicit-check)) (%pcl-instance-p x))
196 ;;; Both of these operations "work" on structures, which allows the above
197 ;;; weakening of STD-INSTANCE-P.
198 ;;; FIXME: what does the preceding comment mean? You can't use instance-slots
199 ;;; on a structure. (Consider especially a structure of 0 slots.)
200 (defmacro std-instance-slots (x) `(%instance-ref ,x 1))
201 (defmacro std-instance-wrapper (x) `(%instance-layout ,x))
203 ;;; FIXME: These functions are called every place we do a
204 ;;; CALL-NEXT-METHOD, and probably other places too. It's likely worth
205 ;;; selectively optimizing them with DEFTRANSFORMs and stuff, rather
206 ;;; than just indiscriminately expanding them inline everywhere.
207 (declaim (inline get-slots get-slots-or-nil))
208 (declaim (ftype (function (t) simple-vector) get-slots))
209 (declaim (ftype (function (t) (or simple-vector null)) get-slots-or-nil))
210 (defun get-slots (instance)
211 (if (std-instance-p instance)
212 (std-instance-slots instance)
213 (fsc-instance-slots instance)))
214 (defun get-slots-or-nil (instance)
215 ;; Suppress a code-deletion note. FIXME: doing the FIXME above,
216 ;; integrating PCL more with the compiler, would remove the need for
217 ;; this icky stuff.
218 (declare (optimize (inhibit-warnings 3)))
219 (when (pcl-instance-p instance)
220 (get-slots instance)))
222 ;; This macro is used only by %CHANGE-CLASS. Can we just do this there?
223 ;; [The code in 'fsc.lisp' which looks like it needs it is commented out]
224 (defmacro get-wrapper (inst)
225 (once-only ((wrapper `(layout-of ,inst)))
226 `(progn
227 (aver (layout-for-std-class-p ,wrapper))
228 ,wrapper)))
230 ;;;; structure-instance stuff
231 ;;;;
232 ;;;; FIXME: Now that the code is SBCL-only, this extra layer of
233 ;;;; abstraction around our native structure representation doesn't
234 ;;;; seem to add anything useful, and could probably go away.
236 ;;; The definition of STRUCTURE-TYPE-P was moved to early-low.lisp.
238 (defun structure-type-slot-description-list (type)
239 (let* ((dd (find-defstruct-description type))
240 (include (dd-include dd))
241 (all-slots (dd-slots dd)))
242 (multiple-value-bind (super slot-overrides)
243 (if (consp include)
244 (values (car include) (mapcar #'car (cdr include)))
245 (values include nil))
246 (let ((included-slots
247 (when super
248 (dd-slots (find-defstruct-description super)))))
249 (loop for slot = (pop all-slots)
250 for included-slot = (pop included-slots)
251 while slot
252 when (or (not included-slot)
253 (member (dsd-name included-slot) slot-overrides :test #'eq))
254 collect slot)))))
256 (defun uninitialized-accessor-function (type slotd)
257 (lambda (&rest args)
258 (declare (ignore args))
259 (error "~:(~A~) function~@[ for ~S ~] not yet initialized."
260 type slotd)))
262 (defun structure-slotd-name (slotd)
263 (dsd-name slotd))
265 (defun structure-slotd-accessor-symbol (slotd)
266 (dsd-accessor-name slotd))
268 (defun structure-slotd-reader-function (slotd)
269 (let ((name (dsd-accessor-name slotd)))
270 (if (fboundp name)
271 (fdefinition name)
272 (uninitialized-accessor-function :reader slotd))))
274 ;;; Return a function to write the slot identified by SLOTD.
275 ;;; This is easy for read/write slots - we just return the accessor
276 ;;; that was already set up - but it requires work for read-only slots.
277 ;;; Basically we get the slotter-setter-lambda-form and compile it.
278 ;;; Using (COERCE lambda-form 'FUNCTION) as used to be done might produce
279 ;;; an interpreted function. I'm not sure whether that's right or wrong,
280 ;;; because if the DEFSTRUCT itself were evaluated, then the ordinary
281 ;;; accessors would indeed be interpreted. However if the DEFSTRUCT were
282 ;;; compiled, and the fasl loaded in a Lisp with *EVALUATOR-MODE* = :INTERPRET,
283 ;;; arguably this is against the expectation that all things got compiled.
284 ;;; But can people really expect that manipulating read-only slots
285 ;;; via (SETF SLOT-VALUE) should be fast?
287 ;;; Damned-if-you-do / damned-if-you don't - the best thing would be to
288 ;;; compile all accessors at "really" compile-time but not store the writer
289 ;;; for a reaadonly slot under the #<fdefn> for #'(SETF slot-name).
291 (defun structure-slotd-writer-function (type slotd)
292 ;; TYPE is not used, because the DD is taken from runtime data.
293 (declare (ignore type))
294 (if (dsd-read-only slotd)
295 ;; We'd like to compile the writer just-in-time and store it
296 ;; back into the STRUCTURE-DIRECT-SLOT-DEFINITION and also
297 ;; the LAYOUT for the class, but we don't have a handle on
298 ;; any of the containing objects. So this has to be a closure.
299 (let ((setter 0))
300 (lambda (newval instance)
301 (if (eql setter 0)
302 (let* ((dd (layout-info (%instance-layout instance)))
303 (f (compile nil (slot-setter-lambda-form dd slotd))))
304 (if (functionp f)
305 (funcall (setq setter f) newval instance)
306 (uninitialized-accessor-function :writer slotd)))
307 (funcall (truly-the function setter) newval instance))))
308 (let ((name `(setf ,(dsd-accessor-name slotd))))
309 (if (fboundp name)
310 (fdefinition name)
311 (uninitialized-accessor-function :writer slotd)))))
313 (defun structure-slotd-type (slotd)
314 (dsd-type slotd))
316 (defun structure-slotd-init-form (slotd)
317 (dsd-default slotd))