1.0.8.20: replace PVREF by SVREF
[sbcl.git] / src / pcl / vector.lisp
blobfb0d53713d125a1705077e78cbe560420c0ba4fc
1 ;;;; permutation vectors
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
6 ;;;; This software is derived from software originally released by Xerox
7 ;;;; Corporation. Copyright and release statements follow. Later modifications
8 ;;;; to the software are in the public domain and are provided with
9 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
10 ;;;; information.
12 ;;;; copyright information from original PCL sources:
13 ;;;;
14 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
15 ;;;; All rights reserved.
16 ;;;;
17 ;;;; Use and copying of this software and preparation of derivative works based
18 ;;;; upon this software are permitted. Any distribution of this software or
19 ;;;; derivative works must comply with all applicable United States export
20 ;;;; control laws.
21 ;;;;
22 ;;;; This software is made available AS IS, and Xerox Corporation makes no
23 ;;;; warranty about the software, its performance or its conformity to any
24 ;;;; specification.
26 (in-package "SB-PCL")
28 (defmacro instance-slot-index (wrapper slot-name)
29 `(let ((pos 0))
30 (declare (fixnum pos))
31 (block loop
32 (dolist (sn (wrapper-instance-slots-layout ,wrapper))
33 (when (eq ,slot-name sn) (return-from loop pos))
34 (incf pos)))))
36 (defstruct (pv-table (:predicate pv-tablep)
37 (:constructor make-pv-table-internal
38 (slot-name-lists call-list))
39 (:copier nil))
40 (cache nil :type (or cache null))
41 (pv-size 0 :type fixnum)
42 (slot-name-lists nil :type list)
43 (call-list nil :type list))
45 #-sb-fluid (declaim (sb-ext:freeze-type pv-table))
47 ;;; FIXME: The comment below seem to indicate that this was intended
48 ;;; to be actually used, however, it isn't anymore, and was commented
49 ;;; out at 0.9.13.47. Also removed was code in MAKE-PV-TABLE that
50 ;;; pushed each new PV-TABLE onto this list. --NS 2006-06-18
51 ;;;
52 ;;; help new slot-value-using-class methods affect fast iv access
53 ;;;
54 ;;; (defvar *all-pv-table-list* nil)
56 (declaim (inline make-pv-table))
57 (defun make-pv-table (&key slot-name-lists call-list)
58 (make-pv-table-internal slot-name-lists call-list))
60 (defun make-pv-table-type-declaration (var)
61 `(type pv-table ,var))
63 (defvar *slot-name-lists-inner* (make-hash-table :test 'equal))
64 (defvar *slot-name-lists-outer* (make-hash-table :test 'equal))
66 ;;; Entries in this are lists of (table . pv-offset-list).
67 (defvar *pv-key-to-pv-table-table* (make-hash-table :test 'equal))
69 (defun intern-pv-table (&key slot-name-lists call-list)
70 (let ((new-p nil))
71 (flet ((inner (x)
72 (or (gethash x *slot-name-lists-inner*)
73 (setf (gethash x *slot-name-lists-inner*) (copy-list x))))
74 (outer (x)
75 (or (gethash x *slot-name-lists-outer*)
76 (setf (gethash x *slot-name-lists-outer*)
77 (let ((snl (copy-list (cdr x)))
78 (cl (car x)))
79 (setq new-p t)
80 (make-pv-table :slot-name-lists snl
81 :call-list cl))))))
82 (let ((pv-table
83 (outer (mapcar #'inner (cons call-list slot-name-lists)))))
84 (when new-p
85 (let ((pv-index 0))
86 (dolist (slot-name-list slot-name-lists)
87 (dolist (slot-name (cdr slot-name-list))
88 (note-pv-table-reference slot-name pv-index pv-table)
89 (incf pv-index)))
90 (dolist (gf-call call-list)
91 (note-pv-table-reference gf-call pv-index pv-table)
92 (incf pv-index))
93 (setf (pv-table-pv-size pv-table) pv-index)))
94 pv-table))))
96 (defun note-pv-table-reference (ref pv-offset pv-table)
97 (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
98 (when (listp entry)
99 (let ((table-entry (assq pv-table entry)))
100 (when (and (null table-entry)
101 (> (length entry) 8))
102 (let ((new-table-table (make-hash-table :size 16 :test 'eq)))
103 (dolist (table-entry entry)
104 (setf (gethash (car table-entry) new-table-table)
105 (cdr table-entry)))
106 (setf (gethash ref *pv-key-to-pv-table-table*) new-table-table)))
107 (when (listp entry)
108 (if (null table-entry)
109 (let ((new (cons pv-table pv-offset)))
110 (if (consp entry)
111 (push new (cdr entry))
112 (setf (gethash ref *pv-key-to-pv-table-table*)
113 (list new))))
114 (push pv-offset (cdr table-entry)))
115 (return-from note-pv-table-reference nil))))
116 (let ((list (gethash pv-table entry)))
117 (if (consp list)
118 (push pv-offset (cdr list))
119 (setf (gethash pv-table entry) (list pv-offset)))))
120 nil)
122 (defun map-pv-table-references-of (ref function)
123 (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
124 (if (listp entry)
125 (dolist (table+pv-offset-list entry)
126 (funcall function
127 (car table+pv-offset-list)
128 (cdr table+pv-offset-list)))
129 (maphash function entry)))
130 ref)
132 (defun optimize-slot-value-by-class-p (class slot-name type)
133 (or (not (eq *boot-state* 'complete))
134 (let ((slotd (find-slot-definition class slot-name)))
135 (and slotd
136 (slot-accessor-std-p slotd type)))))
138 (defun compute-pv-slot (slot-name wrapper class class-slots)
139 (if (symbolp slot-name)
140 (when (optimize-slot-value-by-class-p class slot-name 'all)
141 (or (instance-slot-index wrapper slot-name)
142 (assq slot-name class-slots)))
143 (when (consp slot-name)
144 (case (first slot-name)
145 ((reader writer)
146 (when (eq *boot-state* 'complete)
147 (let ((gf (gdefinition (second slot-name))))
148 (when (generic-function-p gf)
149 (accessor-values1 gf (first slot-name) class)))))
150 (t (bug "Don't know how to deal with ~S in ~S"
151 slot-name 'compute-pv-slots))))))
153 (defun compute-pv (slot-name-lists wrappers)
154 (unless (listp wrappers)
155 (setq wrappers (list wrappers)))
156 (let (elements)
157 (dolist (slot-names slot-name-lists
158 (make-permutation-vector (nreverse elements)))
159 (when slot-names
160 (let* ((wrapper (pop wrappers))
161 (std-p (typep wrapper 'wrapper))
162 (class (wrapper-class* wrapper))
163 (class-slots (and std-p (wrapper-class-slots wrapper))))
164 (dolist (slot-name (cdr slot-names))
165 (push (if std-p
166 (compute-pv-slot slot-name wrapper class class-slots)
167 nil)
168 elements)))))))
170 (defun compute-calls (call-list wrappers)
171 (declare (ignore call-list wrappers))
173 (map 'vector
174 (lambda (call)
175 (compute-emf-from-wrappers call wrappers))
176 call-list)
178 '#())
180 #|| ; Need to finish this, then write the maintenance functions.
181 (defun compute-emf-from-wrappers (call wrappers)
182 (when call
183 (destructuring-bind (gf-name nreq restp arg-info) call
184 (if (eq gf-name 'make-instance)
185 (error "should not get here") ; there is another mechanism for this.
186 (lambda (&rest args)
187 (if (not (eq *boot-state* 'complete))
188 (apply (gdefinition gf-name) args)
189 (let* ((gf (gdefinition gf-name))
190 (arg-info (arg-info-reader gf))
191 (classes '?)
192 (types '?)
193 (emf (cache-miss-values-internal gf arg-info
194 wrappers classes types
195 'caching)))
196 (update-all-pv-tables call wrappers emf)
197 (invoke-emf emf args))))))))
200 (defun make-permutation-vector (indexes)
201 (make-array (length indexes) :initial-contents indexes))
203 (defun pv-table-lookup (pv-table pv-wrappers)
204 (let* ((slot-name-lists (pv-table-slot-name-lists pv-table))
205 (call-list (pv-table-call-list pv-table))
206 (cache (or (pv-table-cache pv-table)
207 (setf (pv-table-cache pv-table)
208 (make-cache :key-count (- (length slot-name-lists)
209 (count nil slot-name-lists))
210 :value t
211 :size 2)))))
212 (multiple-value-bind (hitp value) (probe-cache cache pv-wrappers)
213 (if hitp
214 value
215 (let* ((pv (compute-pv slot-name-lists pv-wrappers))
216 (calls (compute-calls call-list pv-wrappers))
217 (pv-cell (cons pv calls))
218 (new-cache (fill-cache cache pv-wrappers pv-cell)))
219 ;; This is safe: if another thread races us here the loser just
220 ;; misses the next time as well.
221 (unless (eq new-cache cache)
222 (setf (pv-table-cache pv-table) new-cache))
223 pv-cell)))))
225 (defun make-pv-type-declaration (var)
226 `(type simple-vector ,var))
228 (defmacro copy-pv (pv)
229 `(copy-seq ,pv))
231 (defun make-calls-type-declaration (var)
232 `(type simple-vector ,var))
234 (defmacro callsref (calls index)
235 `(svref ,calls ,index))
237 (defvar *pv-table-cache-update-info* nil)
239 (defun update-pv-table-cache-info (class)
240 (let ((slot-names-for-pv-table-update nil)
241 (new-icui nil))
242 (dolist (icu *pv-table-cache-update-info*)
243 (if (eq (car icu) class)
244 (pushnew (cdr icu) slot-names-for-pv-table-update)
245 (push icu new-icui)))
246 (setq *pv-table-cache-update-info* new-icui)
247 (when slot-names-for-pv-table-update
248 (update-all-pv-table-caches class slot-names-for-pv-table-update))))
250 (defun update-all-pv-table-caches (class slot-names)
251 (let* ((cwrapper (class-wrapper class))
252 (std-p (typep cwrapper 'wrapper))
253 (class-slots (and std-p (wrapper-class-slots cwrapper)))
254 (new-values
255 (mapcar
256 (lambda (slot-name)
257 (cons slot-name
258 (if std-p
259 (compute-pv-slot slot-name cwrapper class class-slots)
260 nil)))
261 slot-names))
262 (pv-tables nil))
263 (dolist (slot-name slot-names)
264 (map-pv-table-references-of
265 slot-name
266 (lambda (pv-table pv-offset-list)
267 (declare (ignore pv-offset-list))
268 (pushnew pv-table pv-tables))))
269 (dolist (pv-table pv-tables)
270 (let* ((cache (pv-table-cache pv-table))
271 (slot-name-lists (pv-table-slot-name-lists pv-table))
272 (pv-size (pv-table-pv-size pv-table))
273 (pv-map (make-array pv-size :initial-element nil)))
274 (let ((map-index 0) (param-index 0))
275 (dolist (slot-name-list slot-name-lists)
276 (dolist (slot-name (cdr slot-name-list))
277 (let ((a (assoc slot-name new-values)))
278 (setf (svref pv-map map-index)
279 (and a (cons param-index (cdr a)))))
280 (incf map-index))
281 (incf param-index)))
282 (when cache
283 (map-cache (lambda (wrappers pv-cell)
284 (update-slots-in-pv wrappers (car pv-cell)
285 cwrapper pv-size pv-map))
286 cache))))))
288 (defun update-slots-in-pv (wrappers pv cwrapper pv-size pv-map)
289 (if (atom wrappers)
290 (when (eq cwrapper wrappers)
291 (dotimes-fixnum (i pv-size)
292 (let ((map (svref pv-map i)))
293 (when map
294 (aver (= (car map) 0))
295 (setf (svref pv i) (cdr map))))))
296 (when (memq cwrapper wrappers)
297 (let ((param 0))
298 (dolist (wrapper wrappers)
299 (when (eq wrapper cwrapper)
300 (dotimes-fixnum (i pv-size)
301 (let ((map (svref pv-map i)))
302 (when (and map (= (car map) param))
303 (setf (svref pv i) (cdr map))))))
304 (incf param))))))
306 (defun can-optimize-access (form required-parameters env)
307 (let ((type (ecase (car form)
308 (slot-value 'reader)
309 (set-slot-value 'writer)
310 (slot-boundp 'boundp)))
311 (var (cadr form))
312 (slot-name (eval (caddr form)))) ; known to be constant
313 (can-optimize-access1 var required-parameters env type slot-name)))
315 ;;; FIXME: This looks like an internal helper function for
316 ;;; CAN-OPTIMIZE-ACCESS, and it is used that way, but it's also called
317 ;;; bare from several places in the code. Perhaps the two functions
318 ;;; should be renamed CAN-OPTIMIZE-ACCESS-FOR-FORM and
319 ;;; CAN-OPTIMIZE-ACCESS-FOR-VAR. If so, I'd just as soon use keyword
320 ;;; args instead of optional ones, too.
321 (defun can-optimize-access1 (var required-parameters env
322 &optional type slot-name)
323 (when (and (consp var) (eq 'the (car var)))
324 ;; FIXME: We should assert list of length 3 here. Or maybe we
325 ;; should just define EXTRACT-THE, replace the whole
326 ;; (WHEN ..)
327 ;; form with
328 ;; (AWHEN (EXTRACT-THE VAR)
329 ;; (SETF VAR IT))
330 ;; and then use EXTRACT-THE similarly to clean up the other tests
331 ;; against 'THE scattered through the PCL code.
332 (setq var (caddr var)))
333 (when (symbolp var)
334 (let* ((rebound? (caddr (var-declaration '%variable-rebinding var env)))
335 (parameter-or-nil (car (memq (or rebound? var)
336 required-parameters))))
337 (when parameter-or-nil
338 (let* ((class-name (caddr (var-declaration '%class
339 parameter-or-nil
340 env)))
341 (class (find-class class-name nil)))
342 (when (or (not (eq *boot-state* 'complete))
343 (and class (not (class-finalized-p class))))
344 (setq class nil))
345 (when (and class-name (not (eq class-name t)))
346 (when (or (null type)
347 (not (and class
348 (memq *the-class-structure-object*
349 (class-precedence-list class))))
350 (optimize-slot-value-by-class-p class slot-name type))
351 (cons parameter-or-nil (or class class-name)))))))))
353 ;;; Check whether the binding of the named variable is modified in the
354 ;;; method body.
355 (defun parameter-modified-p (parameter-name env)
356 (let ((modified-variables (macroexpand '%parameter-binding-modified env)))
357 (memq parameter-name modified-variables)))
359 (defun optimize-slot-value (slots sparameter form)
360 (if sparameter
361 (let ((optimized-form
362 (destructuring-bind (ignore1 ignore2 slot-name-form) form
363 (declare (ignore ignore1 ignore2))
364 (let ((slot-name (eval slot-name-form)))
365 (optimize-instance-access slots :read sparameter
366 slot-name nil)))))
367 ;; We don't return the optimized form directly, since there's
368 ;; still a chance that we'll find out later on that the
369 ;; optimization should not have been done, for example due to
370 ;; the walker encountering a SETQ on SPARAMETER later on in
371 ;; the body [ see for example clos.impure.lisp test with :name
372 ;; ((:setq :method-parameter) slot-value)) ]. Instead we defer
373 ;; the decision until the compiler macroexpands
374 ;; OPTIMIZED-SLOT-VALUE.
376 ;; Note that we must still call OPTIMIZE-INSTANCE-ACCESS at
377 ;; this point (instead of when expanding
378 ;; OPTIMIZED-SLOT-VALUE), since it mutates the structure of
379 ;; SLOTS. If that mutation isn't done during the walking,
380 ;; MAKE-METHOD-LAMBDA-INTERNAL won't wrap a correct PV-BINDING
381 ;; form around the body, and compilation will fail. -- JES,
382 ;; 2006-09-18
383 `(optimized-slot-value ,form ,(car sparameter) ,optimized-form))
384 `(accessor-slot-value ,@(cdr form))))
386 (defmacro optimized-slot-value (form parameter-name optimized-form
387 &environment env)
388 ;; Either use OPTIMIZED-FORM or fall back to the safe
389 ;; ACCESSOR-SLOT-VALUE.
390 (if (parameter-modified-p parameter-name env)
391 `(accessor-slot-value ,@(cdr form))
392 optimized-form))
394 (defun optimize-set-slot-value (slots sparameter form)
395 (if sparameter
396 (let ((optimized-form
397 (destructuring-bind (ignore1 ignore2 slot-name-form new-value) form
398 (declare (ignore ignore1 ignore2))
399 (let ((slot-name (eval slot-name-form)))
400 (optimize-instance-access slots
401 :write
402 sparameter
403 slot-name
404 new-value)))))
405 ;; See OPTIMIZE-SLOT-VALUE
406 `(optimized-set-slot-value ,form ,(car sparameter) ,optimized-form))
407 `(accessor-set-slot-value ,@(cdr form))))
409 (defmacro optimized-set-slot-value (form parameter-name optimized-form
410 &environment env)
411 (cond ((safe-code-p env)
412 ;; Don't optimize slot value setting in safe code, since the
413 ;; optimized version will fail to catch some type errors
414 ;; (for example when a subclass declares a tighter type for
415 ;; the slot than a superclass).
416 `(safe-set-slot-value ,@(cdr form)))
417 ((parameter-modified-p parameter-name env)
418 `(accessor-set-slot-value ,@(cdr form)))
420 optimized-form)))
422 (defun optimize-slot-boundp (slots sparameter form)
423 (if sparameter
424 (let ((optimized-form
425 (destructuring-bind
426 ;; FIXME: In CMU CL ca. 19991205, this binding list
427 ;; had a fourth element in it, NEW-VALUE. It's hard
428 ;; to see how that could possibly be right, since
429 ;; SLOT-BOUNDP has no NEW-VALUE. Since it was
430 ;; causing a failure in building PCL for SBCL, so I
431 ;; changed it to match the definition of
432 ;; SLOT-BOUNDP (and also to match the list used in
433 ;; the similar OPTIMIZE-SLOT-VALUE,
434 ;; above). However, I'm weirded out by this, since
435 ;; this is old code which has worked for ages to
436 ;; build PCL for CMU CL, so it's hard to see why it
437 ;; should need a patch like this in order to build
438 ;; PCL for SBCL. I'd like to return to this and
439 ;; find a test case which exercises this function
440 ;; both in CMU CL, to see whether it's really a
441 ;; previously-unexercised bug or whether I've
442 ;; misunderstood something (and, presumably,
443 ;; patched it wrong).
444 (slot-boundp-symbol instance slot-name-form)
445 form
446 (declare (ignore slot-boundp-symbol instance))
447 (let ((slot-name (eval slot-name-form)))
448 (optimize-instance-access slots
449 :boundp
450 sparameter
451 slot-name
452 nil)))))
453 ;; See OPTIMIZE-SLOT-VALUE
454 `(optimized-slot-boundp ,form ,(car sparameter) ,optimized-form))
455 `(accessor-slot-boundp ,@(cdr form))))
457 (defmacro optimized-slot-boundp (form parameter-name optimized-form
458 &environment env)
459 (if (parameter-modified-p parameter-name env)
460 `(accessor-slot-boundp ,@(cdr form))
461 optimized-form))
463 ;;; The SLOTS argument is an alist, the CAR of each entry is the name
464 ;;; of a required parameter to the function. The alist is in order, so
465 ;;; the position of an entry in the alist corresponds to the
466 ;;; argument's position in the lambda list.
467 (defun optimize-instance-access (slots
468 read/write
469 sparameter
470 slot-name
471 new-value)
472 (let ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
473 (parameter (if (consp sparameter) (car sparameter) sparameter)))
474 (if (and (eq *boot-state* 'complete)
475 (classp class)
476 (memq *the-class-structure-object* (class-precedence-list class)))
477 (let ((slotd (find-slot-definition class slot-name)))
478 (ecase read/write
479 (:read
480 `(,(slot-definition-defstruct-accessor-symbol slotd) ,parameter))
481 (:write
482 `(setf (,(slot-definition-defstruct-accessor-symbol slotd)
483 ,parameter)
484 ,new-value))
485 (:boundp
486 t)))
487 (let* ((parameter-entry (assq parameter slots))
488 (slot-entry (assq slot-name (cdr parameter-entry)))
489 (position (posq parameter-entry slots))
490 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
491 (unless parameter-entry
492 (bug "slot optimization bewilderment: O-I-A"))
493 (unless slot-entry
494 (setq slot-entry (list slot-name))
495 (push slot-entry (cdr parameter-entry)))
496 (push pv-offset-form (cdr slot-entry))
497 (ecase read/write
498 (:read
499 `(instance-read ,pv-offset-form ,parameter ,position
500 ',slot-name ',class))
501 (:write
502 `(let ((.new-value. ,new-value))
503 (instance-write ,pv-offset-form ,parameter ,position
504 ',slot-name ',class .new-value.)))
505 (:boundp
506 `(instance-boundp ,pv-offset-form ,parameter ,position
507 ',slot-name ',class)))))))
509 (define-walker-template pv-offset) ; These forms get munged by mutate slots.
510 (defmacro pv-offset (arg) arg)
511 (define-walker-template instance-accessor-parameter)
512 (defmacro instance-accessor-parameter (x) x)
514 ;;; It is safe for these two functions to be wrong. They just try to
515 ;;; guess what the most likely case will be.
516 (defun generate-fast-class-slot-access-p (class-form slot-name-form)
517 (let ((class (and (constantp class-form) (constant-form-value class-form)))
518 (slot-name (and (constantp slot-name-form)
519 (constant-form-value slot-name-form))))
520 (and (eq *boot-state* 'complete)
521 (standard-class-p class)
522 (not (eq class *the-class-t*)) ; shouldn't happen, though.
523 (let ((slotd (find-slot-definition class slot-name)))
524 (and slotd (eq :class (slot-definition-allocation slotd)))))))
526 (defun skip-fast-slot-access-p (class-form slot-name-form type)
527 (let ((class (and (constantp class-form) (constant-form-value class-form)))
528 (slot-name (and (constantp slot-name-form)
529 (constant-form-value slot-name-form))))
530 (and (eq *boot-state* 'complete)
531 (standard-class-p class)
532 (not (eq class *the-class-t*)) ; shouldn't happen, though.
533 (let ((slotd (find-slot-definition class slot-name)))
534 (and slotd (skip-optimize-slot-value-by-class-p class
535 slot-name
536 type))))))
538 (defun skip-optimize-slot-value-by-class-p (class slot-name type)
539 (let ((slotd (find-slot-definition class slot-name)))
540 (and slotd
541 (eq *boot-state* 'complete)
542 (not (slot-accessor-std-p slotd type)))))
544 (defmacro instance-read-internal (pv slots pv-offset default &optional kind)
545 (unless (member kind '(nil :instance :class :default))
546 (error "illegal kind argument to ~S: ~S" 'instance-read-internal kind))
547 (if (eq kind :default)
548 default
549 (let* ((index (gensym))
550 (value index))
551 `(locally (declare #.*optimize-speed*)
552 (let ((,index (svref ,pv ,pv-offset)))
553 (setq ,value (typecase ,index
554 ;; FIXME: the line marked by KLUDGE below
555 ;; (and the analogous spot in
556 ;; INSTANCE-WRITE-INTERNAL) is there purely
557 ;; to suppress a type mismatch warning that
558 ;; propagates through to user code.
559 ;; Presumably SLOTS at this point can never
560 ;; actually be NIL, but the compiler seems
561 ;; to think it could, so we put this here
562 ;; to shut it up. (see also mail Rudi
563 ;; Schlatte sbcl-devel 2003-09-21) -- CSR,
564 ;; 2003-11-30
565 ,@(when (or (null kind) (eq kind :instance))
566 `((fixnum
567 (and ,slots ; KLUDGE
568 (clos-slots-ref ,slots ,index)))))
569 ,@(when (or (null kind) (eq kind :class))
570 `((cons (cdr ,index))))
571 (t +slot-unbound+)))
572 (if (eq ,value +slot-unbound+)
573 ,default
574 ,value))))))
576 (defmacro instance-read (pv-offset parameter position slot-name class)
577 (if (skip-fast-slot-access-p class slot-name 'reader)
578 `(accessor-slot-value ,parameter ,slot-name)
579 `(instance-read-internal .pv. ,(slot-vector-symbol position)
580 ,pv-offset (accessor-slot-value ,parameter ,slot-name)
581 ,(if (generate-fast-class-slot-access-p class slot-name)
582 :class :instance))))
584 (defmacro instance-write-internal (pv slots pv-offset new-value default
585 &optional kind)
586 (unless (member kind '(nil :instance :class :default))
587 (error "illegal kind argument to ~S: ~S" 'instance-write-internal kind))
588 (if (eq kind :default)
589 default
590 (let* ((index (gensym)))
591 `(locally (declare #.*optimize-speed*)
592 (let ((,index (svref ,pv ,pv-offset)))
593 (typecase ,index
594 ,@(when (or (null kind) (eq kind :instance))
595 `((fixnum (and ,slots
596 (setf (clos-slots-ref ,slots ,index)
597 ,new-value)))))
598 ,@(when (or (null kind) (eq kind :class))
599 `((cons (setf (cdr ,index) ,new-value))))
600 (t ,default)))))))
602 (defmacro instance-write (pv-offset
603 parameter
604 position
605 slot-name
606 class
607 new-value)
608 (if (skip-fast-slot-access-p class slot-name 'writer)
609 `(accessor-set-slot-value ,parameter ,slot-name ,new-value)
610 `(instance-write-internal .pv. ,(slot-vector-symbol position)
611 ,pv-offset ,new-value
612 (accessor-set-slot-value ,parameter ,slot-name ,new-value)
613 ,(if (generate-fast-class-slot-access-p class slot-name)
614 :class :instance))))
616 (defmacro instance-boundp-internal (pv slots pv-offset default
617 &optional kind)
618 (unless (member kind '(nil :instance :class :default))
619 (error "illegal kind argument to ~S: ~S" 'instance-boundp-internal kind))
620 (if (eq kind :default)
621 default
622 (let* ((index (gensym)))
623 `(locally (declare #.*optimize-speed*)
624 (let ((,index (svref ,pv ,pv-offset)))
625 (typecase ,index
626 ,@(when (or (null kind) (eq kind :instance))
627 `((fixnum (not (and ,slots
628 (eq (clos-slots-ref ,slots ,index)
629 +slot-unbound+))))))
630 ,@(when (or (null kind) (eq kind :class))
631 `((cons (not (eq (cdr ,index) +slot-unbound+)))))
632 (t ,default)))))))
634 (defmacro instance-boundp (pv-offset parameter position slot-name class)
635 (if (skip-fast-slot-access-p class slot-name 'boundp)
636 `(accessor-slot-boundp ,parameter ,slot-name)
637 `(instance-boundp-internal .pv. ,(slot-vector-symbol position)
638 ,pv-offset (accessor-slot-boundp ,parameter ,slot-name)
639 ,(if (generate-fast-class-slot-access-p class slot-name)
640 :class :instance))))
642 ;;; This magic function has quite a job to do indeed.
644 ;;; The careful reader will recall that <slots> contains all of the
645 ;;; optimized slot access forms produced by OPTIMIZE-INSTANCE-ACCESS.
646 ;;; Each of these is a call to either INSTANCE-READ or INSTANCE-WRITE.
648 ;;; At the time these calls were produced, the first argument was
649 ;;; specified as the symbol .PV-OFFSET.; what we have to do now is
650 ;;; convert those pv-offset arguments into the actual number that is
651 ;;; the correct offset into the pv.
653 ;;; But first, oh but first, we sort <slots> a bit so that for each
654 ;;; argument we have the slots in alphabetical order. This
655 ;;; canonicalizes the PV-TABLE's a bit and will hopefully lead to
656 ;;; having fewer PV's floating around. Even if the gain is only
657 ;;; modest, it costs nothing.
658 (defun slot-name-lists-from-slots (slots calls)
659 (multiple-value-bind (slots calls) (mutate-slots-and-calls slots calls)
660 (let* ((slot-name-lists
661 (mapcar (lambda (parameter-entry)
662 (cons nil (mapcar #'car (cdr parameter-entry))))
663 slots))
664 (call-list
665 (mapcar #'car calls)))
666 (dolist (call call-list)
667 (dolist (arg (cdr call))
668 (when (integerp arg)
669 (setf (car (nth arg slot-name-lists)) t))))
670 (setq slot-name-lists (mapcar (lambda (r+snl)
671 (when (or (car r+snl) (cdr r+snl))
672 r+snl))
673 slot-name-lists))
674 (let ((cvt (apply #'vector
675 (let ((i -1))
676 (mapcar (lambda (r+snl)
677 (when r+snl (incf i)))
678 slot-name-lists)))))
679 (setq call-list (mapcar (lambda (call)
680 (cons (car call)
681 (mapcar (lambda (arg)
682 (if (integerp arg)
683 (svref cvt arg)
684 arg))
685 (cdr call))))
686 call-list)))
687 (values slot-name-lists call-list))))
689 (defun mutate-slots-and-calls (slots calls)
690 (let ((sorted-slots (sort-slots slots))
691 (sorted-calls (sort-calls (cdr calls)))
692 (pv-offset -1))
693 (dolist (parameter-entry sorted-slots)
694 (dolist (slot-entry (cdr parameter-entry))
695 (incf pv-offset)
696 (dolist (form (cdr slot-entry))
697 (setf (cadr form) pv-offset))))
698 (dolist (call-entry sorted-calls)
699 (incf pv-offset)
700 (dolist (form (cdr call-entry))
701 (setf (cadr form) pv-offset)))
702 (values sorted-slots sorted-calls)))
704 (defun symbol-pkg-name (sym)
705 (let ((pkg (symbol-package sym)))
706 (if pkg (package-name pkg) "")))
708 ;;; FIXME: Because of the existence of UNINTERN and RENAME-PACKAGE,
709 ;;; the part of this ordering which is based on SYMBOL-PKG-NAME is not
710 ;;; stable. This ordering is only used in to
711 ;;; SLOT-NAME-LISTS-FROM-SLOTS, where it serves to "canonicalize the
712 ;;; PV-TABLE's a bit and will hopefully lead to having fewer PV's
713 ;;; floating around", so it sounds as though the instability won't
714 ;;; actually lead to bugs, just small inefficiency. But still, it
715 ;;; would be better to reimplement this function as a comparison based
716 ;;; on SYMBOL-HASH:
717 ;;; * stable comparison
718 ;;; * smaller code (here, and in being able to discard SYMBOL-PKG-NAME)
719 ;;; * faster code.
720 (defun symbol-lessp (a b)
721 (if (eq (symbol-package a)
722 (symbol-package b))
723 (string-lessp (symbol-name a)
724 (symbol-name b))
725 (string-lessp (symbol-pkg-name a)
726 (symbol-pkg-name b))))
728 (defun symbol-or-cons-lessp (a b)
729 (etypecase a
730 (symbol (etypecase b
731 (symbol (symbol-lessp a b))
732 (cons t)))
733 (cons (etypecase b
734 (symbol nil)
735 (cons (if (eq (car a) (car b))
736 (symbol-or-cons-lessp (cdr a) (cdr b))
737 (symbol-or-cons-lessp (car a) (car b))))))))
739 (defun sort-slots (slots)
740 (mapcar (lambda (parameter-entry)
741 (cons (car parameter-entry)
742 (sort (cdr parameter-entry) ;slot entries
743 #'symbol-or-cons-lessp
744 :key #'car)))
745 slots))
747 (defun sort-calls (calls)
748 (sort calls #'symbol-or-cons-lessp :key #'car))
750 ;;;; This needs to work in terms of metatypes and also needs to work
751 ;;;; for automatically generated reader and writer functions.
752 ;;;; Automatically generated reader and writer functions use this
753 ;;;; stuff too.
755 (defmacro pv-binding ((required-parameters slot-name-lists pv-table-form)
756 &body body)
757 (let (slot-vars pv-parameters)
758 (loop for slots in slot-name-lists
759 for required-parameter in required-parameters
760 for i from 0
761 do (when slots
762 (push required-parameter pv-parameters)
763 (push (slot-vector-symbol i) slot-vars)))
764 `(pv-binding1 (.pv. .calls. ,pv-table-form
765 ,(nreverse pv-parameters) ,(nreverse slot-vars))
766 ,@body)))
768 (defmacro pv-binding1 ((pv calls pv-table-form pv-parameters slot-vars)
769 &body body)
770 `(pv-env (,pv ,calls ,pv-table-form ,pv-parameters)
771 (let (,@(mapcar (lambda (slot-var p) `(,slot-var (get-slots-or-nil ,p)))
772 slot-vars pv-parameters))
773 (declare (ignorable ,@(mapcar #'identity slot-vars)))
774 ,@body)))
776 ;;; This will only be visible in PV-ENV when the default MAKE-METHOD-LAMBDA is
777 ;;; overridden.
778 (define-symbol-macro pv-env-environment overridden)
780 (defmacro pv-env (&environment env
781 (pv calls pv-table-form pv-parameters)
782 &rest forms)
783 ;; Decide which expansion to use based on the state of the PV-ENV-ENVIRONMENT
784 ;; symbol-macrolet.
785 (if (eq (macroexpand 'pv-env-environment env) 'default)
786 `(let ((,pv (car .pv-cell.))
787 (,calls (cdr .pv-cell.)))
788 (declare ,(make-pv-type-declaration pv)
789 ,(make-calls-type-declaration calls))
790 ,pv ,calls
791 ,@forms)
792 `(let* ((.pv-table. ,pv-table-form)
793 (.pv-cell. (pv-table-lookup-pv-args .pv-table. ,@pv-parameters))
794 (,pv (car .pv-cell.))
795 (,calls (cdr .pv-cell.)))
796 (declare ,(make-pv-type-declaration pv))
797 (declare ,(make-calls-type-declaration calls))
798 ,pv ,calls
799 ,@forms)))
801 (defvar *non-var-declarations*
802 ;; FIXME: VALUES was in this list, conditionalized with #+CMU, but I
803 ;; don't *think* CMU CL had, or SBCL has, VALUES declarations. If
804 ;; SBCL doesn't have 'em, VALUES should probably be removed from
805 ;; this list.
806 '(values
807 %method-name
808 %method-lambda-list
809 optimize
810 ftype
811 muffle-conditions
812 inline
813 notinline))
815 (defvar *var-declarations-with-arg*
816 '(%class
817 type))
819 (defvar *var-declarations-without-arg*
820 '(ignore
821 ignorable special dynamic-extent
822 ;; FIXME: Possibly this entire list and variable could go away.
823 ;; If not, certainly we should remove all these built-in typenames
824 ;; from the list, and replace them with a test for "is it a type
825 ;; name?" (CLTL1 allowed only built-in type names as declarations,
826 ;; but ANSI CL allows any type name as a declaration.)
827 array atom base-char bignum bit bit-vector character compiled-function
828 complex cons double-float extended-char
829 fixnum float function hash-table integer
830 keyword list long-float nil null number package pathname random-state ratio
831 rational readtable sequence short-float signed-byte simple-array
832 simple-bit-vector simple-string simple-vector single-float standard-char
833 stream string symbol t unsigned-byte vector))
835 (defun split-declarations (body args maybe-reads-params-p)
836 (let ((inner-decls nil)
837 (outer-decls nil)
838 decl)
839 (loop (when (null body) (return nil))
840 (setq decl (car body))
841 (unless (and (consp decl)
842 (eq (car decl) 'declare))
843 (return nil))
844 (dolist (form (cdr decl))
845 (when (consp form)
846 (let ((declaration-name (car form)))
847 (if (member declaration-name *non-var-declarations*)
848 (push `(declare ,form) outer-decls)
849 (let ((arg-p
850 (member declaration-name
851 *var-declarations-with-arg*))
852 (non-arg-p
853 (member declaration-name
854 *var-declarations-without-arg*))
855 (dname (list (pop form)))
856 (inners nil) (outers nil))
857 (unless (or arg-p non-arg-p)
858 ;; FIXME: This warning, and perhaps the
859 ;; various *VAR-DECLARATIONS-FOO* and/or
860 ;; *NON-VAR-DECLARATIONS* variables,
861 ;; could probably go away now that we're not
862 ;; trying to be portable between different
863 ;; CLTL1 hosts the way PCL was. (Note that to
864 ;; do this right, we need to be able to handle
865 ;; user-defined (DECLAIM (DECLARATION FOO))
866 ;; stuff.)
867 (warn "The declaration ~S is not understood by ~S.~@
868 Please put ~S on one of the lists ~S,~%~S, or~%~S.~@
869 (Assuming it is a variable declaration without argument)."
870 declaration-name 'split-declarations
871 declaration-name
872 '*non-var-declarations*
873 '*var-declarations-with-arg*
874 '*var-declarations-without-arg*)
875 (push declaration-name *var-declarations-without-arg*))
876 (when arg-p
877 (setq dname (append dname (list (pop form)))))
878 (case (car dname)
879 (%class (push `(declare (,@dname ,@form)) inner-decls))
881 (dolist (var form)
882 (if (member var args)
883 ;; Quietly remove IGNORE declarations
884 ;; on args when a next-method is
885 ;; involved, to prevent compiler
886 ;; warnings about ignored args being
887 ;; read.
888 (unless (and maybe-reads-params-p
889 (eq (car dname) 'ignore))
890 (push var outers))
891 (push var inners)))
892 (when outers
893 (push `(declare (,@dname ,@outers)) outer-decls))
894 (when inners
895 (push
896 `(declare (,@dname ,@inners))
897 inner-decls)))))))))
898 (setq body (cdr body)))
899 (values outer-decls inner-decls body)))
901 ;;; Pull a name out of the %METHOD-NAME declaration in the function
902 ;;; body given, or return NIL if no %METHOD-NAME declaration is found.
903 (defun body-method-name (body)
904 (multiple-value-bind (real-body declarations documentation)
905 (parse-body body)
906 (declare (ignore real-body documentation))
907 (let ((name-decl (get-declaration '%method-name declarations)))
908 (and name-decl
909 (destructuring-bind (name) name-decl
910 name)))))
912 ;;; Convert a lambda expression containing a SB-PCL::%METHOD-NAME
913 ;;; declaration (which is a naming style internal to PCL) into an
914 ;;; SB-INT:NAMED-LAMBDA expression (which is a naming style used
915 ;;; throughout SBCL, understood by the main compiler); or if there's
916 ;;; no SB-PCL::%METHOD-NAME declaration, then just return the original
917 ;;; lambda expression.
918 (defun name-method-lambda (method-lambda)
919 (let ((method-name (body-method-name (cddr method-lambda))))
920 (if method-name
921 `(named-lambda (slow-method ,method-name) ,(rest method-lambda))
922 method-lambda)))
924 (defun make-method-initargs-form-internal (method-lambda initargs env)
925 (declare (ignore env))
926 (let (method-lambda-args
927 lmf ; becomes body of function
928 lmf-params)
929 (if (not (and (= 3 (length method-lambda))
930 (= 2 (length (setq method-lambda-args (cadr method-lambda))))
931 (consp (setq lmf (third method-lambda)))
932 (eq 'simple-lexical-method-functions (car lmf))
933 (eq (car method-lambda-args)
934 (cadr (setq lmf-params (cadr lmf))))
935 (eq (cadr method-lambda-args)
936 (caddr lmf-params))))
937 `(list* :function ,(name-method-lambda method-lambda)
938 ',initargs)
939 (let* ((lambda-list (car lmf-params))
940 (nreq 0)
941 (restp nil)
942 (args nil))
943 (dolist (arg lambda-list)
944 (when (member arg '(&optional &rest &key))
945 (setq restp t)
946 (return nil))
947 (when (eq arg '&aux)
948 (return nil))
949 (incf nreq)
950 (push arg args))
951 (setq args (nreverse args))
952 (setf (getf (getf initargs 'plist) :arg-info) (cons nreq restp))
953 (make-method-initargs-form-internal1
954 initargs (cddr lmf) args lmf-params restp)))))
956 (defun lambda-list-parameter-names (lambda-list)
957 ;; Given a valid lambda list, extract the parameter names.
958 (loop for x in lambda-list
959 with res = nil
960 do (unless (member x lambda-list-keywords)
961 (if (consp x)
962 (let ((name (car x)))
963 (if (consp name)
964 ;; ... ((:BAR FOO) 1)
965 (push (second name) res)
966 ;; ... (FOO 1)
967 (push name res))
968 ;; ... (... 1 FOO-P)
969 (let ((name-p (cddr x)))
970 (when name-p
971 (push (car name-p) res))))
972 ;; ... FOO
973 (push x res)))
974 finally (return res)))
976 (defun make-method-initargs-form-internal1
977 (initargs body req-args lmf-params restp)
978 (let* (;; The lambda-list of the method, minus specifiers
979 (lambda-list (car lmf-params))
980 ;; Names of the parameters that will be in the outermost lambda-list
981 ;; (and whose bound declarations thus need to be in OUTER-DECLS).
982 (outer-parameters req-args)
983 ;; The lambda-list used by BIND-ARGS
984 (bind-list lambda-list)
985 (setq-p (getf (cdr lmf-params) :setq-p))
986 (auxp (member '&aux bind-list))
987 (call-next-method-p (getf (cdr lmf-params) :call-next-method-p)))
988 ;; Try to use the normal function call machinery instead of BIND-ARGS
989 ;; binding the arguments, unless:
990 (unless (or ;; If all arguments are required, BIND-ARGS will be a no-op
991 ;; in any case.
992 (and (not restp) (not auxp))
993 ;; CALL-NEXT-METHOD wants to use BIND-ARGS, and needs a
994 ;; list of all non-required arguments.
995 call-next-method-p)
996 (setf ;; We don't want a binding for .REST-ARG.
997 restp nil
998 ;; Get all the parameters for declaration parsing
999 outer-parameters (lambda-list-parameter-names lambda-list)
1000 ;; Ensure that BIND-ARGS won't do anything (since
1001 ;; BIND-LIST won't contain any non-required parameters,
1002 ;; and REQ-ARGS will be of an equal length). We still want
1003 ;; to pass BIND-LIST to FAST-LEXICAL-METHOD-FUNCTIONS so
1004 ;; that BIND-FAST-LEXICAL-METHOD-FUNCTIONS can take care
1005 ;; of rebinding SETQd required arguments around the method
1006 ;; body.
1007 bind-list req-args))
1008 (multiple-value-bind (outer-decls inner-decls body-sans-decls)
1009 (split-declarations
1010 body outer-parameters (or call-next-method-p setq-p))
1011 (let* ((rest-arg (when restp
1012 '.rest-arg.))
1013 (fmf-lambda-list (if rest-arg
1014 (append req-args (list '&rest rest-arg))
1015 (if call-next-method-p
1016 req-args
1017 lambda-list))))
1018 `(list*
1019 :function
1020 (let* ((fmf (,(if (body-method-name body) 'named-lambda 'lambda)
1021 ,@(when (body-method-name body)
1022 ;; function name
1023 (list (cons 'fast-method (body-method-name body))))
1024 ;; The lambda-list of the FMF
1025 (.pv-cell. .next-method-call. ,@fmf-lambda-list)
1026 ;; body of the function
1027 (declare (ignorable .pv-cell. .next-method-call.)
1028 (disable-package-locks pv-env-environment))
1029 ,@outer-decls
1030 (symbol-macrolet ((pv-env-environment default))
1031 (fast-lexical-method-functions
1032 (,bind-list .next-method-call. ,req-args ,rest-arg
1033 ,@(cdddr lmf-params))
1034 ,@inner-decls
1035 ,@body-sans-decls))))
1036 (mf (%make-method-function fmf nil)))
1037 (set-funcallable-instance-function
1038 mf (method-function-from-fast-function fmf ',(getf initargs 'plist)))
1040 ',initargs)))))
1042 ;;; Use arrays and hash tables and the fngen stuff to make this much
1043 ;;; better. It doesn't really matter, though, because a function
1044 ;;; returned by this will get called only when the user explicitly
1045 ;;; funcalls a result of method-function. BUT, this is needed to make
1046 ;;; early methods work.
1047 (defun method-function-from-fast-function (fmf plist)
1048 (declare (type function fmf))
1049 (let* ((method-function nil)
1050 (calls (getf plist :call-list))
1051 (snl (getf plist :slot-name-lists))
1052 (pv-table (when (or calls snl)
1053 (intern-pv-table :call-list calls :slot-name-lists snl)))
1054 (arg-info (getf plist :arg-info))
1055 (nreq (car arg-info))
1056 (restp (cdr arg-info)))
1057 (setq method-function
1058 (lambda (method-args next-methods)
1059 (let* ((pv-cell (when pv-table
1060 (get-pv-cell method-args pv-table)))
1061 (nm (car next-methods))
1062 (nms (cdr next-methods))
1063 (nmc (when nm
1064 (make-method-call
1065 :function (if (std-instance-p nm)
1066 (method-function nm)
1068 :call-method-args (list nms)))))
1069 (apply fmf pv-cell nmc method-args))))
1070 ;; FIXME: this looks dangerous.
1071 (let* ((fname (%fun-name fmf)))
1072 (when (and fname (eq (car fname) 'fast-method))
1073 (set-fun-name method-function (cons 'slow-method (cdr fname)))))
1074 method-function))
1076 ;;; this is similar to the above, only not quite. Only called when
1077 ;;; the MOP is heavily involved. Not quite parallel to
1078 ;;; METHOD-FUNCTION-FROM-FAST-METHOD-FUNCTION, because we can close
1079 ;;; over the actual PV-CELL in this case.
1080 (defun method-function-from-fast-method-call (fmc)
1081 (let* ((fmf (fast-method-call-function fmc))
1082 (pv-cell (fast-method-call-pv-cell fmc))
1083 (arg-info (fast-method-call-arg-info fmc))
1084 (nreq (car arg-info))
1085 (restp (cdr arg-info)))
1086 (lambda (method-args next-methods)
1087 (let* ((nm (car next-methods))
1088 (nms (cdr next-methods))
1089 (nmc (when nm
1090 (make-method-call
1091 :function (if (std-instance-p nm)
1092 (method-function nm)
1094 :call-method-args (list nms)))))
1095 (apply fmf pv-cell nmc method-args)))))
1097 (defun get-pv-cell (method-args pv-table)
1098 (let ((pv-wrappers (pv-wrappers-from-all-args pv-table method-args)))
1099 (when pv-wrappers
1100 (pv-table-lookup pv-table pv-wrappers))))
1102 (defun pv-table-lookup-pv-args (pv-table &rest pv-parameters)
1103 (pv-table-lookup pv-table (pv-wrappers-from-pv-args pv-parameters)))
1105 (defun pv-wrappers-from-pv-args (&rest args)
1106 (let (wrappers)
1107 (dolist (arg args (if (cdr wrappers) (nreverse wrappers) (car wrappers)))
1108 (let ((wrapper (wrapper-of arg)))
1109 (push (if (invalid-wrapper-p wrapper)
1110 (check-wrapper-validity wrapper)
1111 wrapper)
1112 wrappers)))))
1114 (defun pv-wrappers-from-all-args (pv-table args)
1115 (loop for snl in (pv-table-slot-name-lists pv-table) and arg in args
1116 when snl
1117 collect (wrapper-of arg) into wrappers
1118 finally (return (if (cdr wrappers) wrappers (car wrappers)))))
1120 ;;; Return the subset of WRAPPERS which is used in the cache
1121 ;;; of PV-TABLE.
1122 (defun pv-wrappers-from-all-wrappers (pv-table wrappers)
1123 (loop for snl in (pv-table-slot-name-lists pv-table) and w in wrappers
1124 when snl
1125 collect w into result
1126 finally (return (if (cdr result) result (car result)))))