1 ;;;; permutation vectors
3 ;;;; This software is part of the SBCL system. See the README file for
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
12 ;;;; copyright information from original PCL sources:
14 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
15 ;;;; All rights reserved.
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
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
31 (defmacro instance-slot-index (wrapper slot-name)
33 (declare (fixnum pos))
35 (dolist (sn (wrapper-instance-slots-layout ,wrapper))
36 (when (eq ,slot-name sn) (return-from loop pos))
39 (defun pv-cache-limit-fn (nlines)
40 (default-limit-fn nlines))
43 (:predicate pv-tablep)
44 (:constructor make-pv-table-internal
45 (slot-name-lists call-list)))
46 (cache nil :type (or cache null))
47 (pv-size 0 :type fixnum)
48 (slot-name-lists nil :type list)
49 (call-list nil :type list))
51 #-sb-fluid (declaim (sb-ext:freeze-type pv-table))
53 (defvar *initial-pv-table* (make-pv-table-internal nil nil))
55 ; help new slot-value-using-class methods affect fast iv access
56 (defvar *all-pv-table-list* nil)
58 (defun make-pv-table (&key slot-name-lists call-list)
59 (let ((pv-table (make-pv-table-internal slot-name-lists call-list)))
60 (push pv-table *all-pv-table-list*)
63 (defun make-pv-table-type-declaration (var)
64 `(type pv-table ,var))
66 (defvar *slot-name-lists-inner* (make-hash-table :test 'equal))
67 (defvar *slot-name-lists-outer* (make-hash-table :test 'equal))
69 ;entries in this are lists of (table . pv-offset-list)
70 (defvar *pv-key-to-pv-table-table* (make-hash-table :test 'equal))
72 (defun intern-pv-table (&key slot-name-lists call-list)
75 (or (gethash x *slot-name-lists-inner*)
76 (setf (gethash x *slot-name-lists-inner*) (copy-list x))))
78 (or (gethash x *slot-name-lists-outer*)
79 (setf (gethash x *slot-name-lists-outer*)
80 (let ((snl (copy-list (cdr x)))
83 (make-pv-table :slot-name-lists snl
85 (let ((pv-table (outer (mapcar #'inner (cons call-list slot-name-lists)))))
88 (dolist (slot-name-list slot-name-lists)
89 (dolist (slot-name (cdr slot-name-list))
90 (note-pv-table-reference slot-name pv-index pv-table)
92 (dolist (gf-call call-list)
93 (note-pv-table-reference gf-call pv-index pv-table)
95 (setf (pv-table-pv-size pv-table) pv-index)))
98 (defun note-pv-table-reference (ref pv-offset pv-table)
99 (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
101 (let ((table-entry (assq pv-table entry)))
102 (when (and (null table-entry)
103 (> (length entry) 8))
104 (let ((new-table-table (make-hash-table :size 16 :test 'eq)))
105 (dolist (table-entry entry)
106 (setf (gethash (car table-entry) new-table-table)
108 (setf (gethash ref *pv-key-to-pv-table-table*) new-table-table)))
110 (if (null table-entry)
111 (let ((new (cons pv-table pv-offset)))
113 (push new (cdr entry))
114 (setf (gethash ref *pv-key-to-pv-table-table*)
116 (push pv-offset (cdr table-entry)))
117 (return-from note-pv-table-reference nil))))
118 (let ((list (gethash pv-table entry)))
120 (push pv-offset (cdr list))
121 (setf (gethash pv-table entry) (list pv-offset)))))
124 (defun map-pv-table-references-of (ref function)
125 (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
127 (dolist (table+pv-offset-list entry)
129 (car table+pv-offset-list)
130 (cdr table+pv-offset-list)))
131 (maphash function entry)))
134 (defvar *pvs* (make-hash-table :test 'equal))
136 (defun optimize-slot-value-by-class-p (class slot-name type)
137 (or (not (eq *boot-state* 'complete))
138 (let ((slotd (find-slot-definition class slot-name)))
140 (slot-accessor-std-p slotd type)))))
142 (defun compute-pv-slot (slot-name wrapper class class-slots class-slot-p-cell)
143 (if (symbolp slot-name)
144 (when (optimize-slot-value-by-class-p class slot-name 'all)
145 (or (instance-slot-index wrapper slot-name)
146 (let ((cell (assq slot-name class-slots)))
148 (setf (car class-slot-p-cell) t)
150 (when (consp slot-name)
151 (dolist (type '(reader writer) nil)
152 (when (eq (car slot-name) type)
154 (let* ((gf-name (cadr slot-name))
155 (gf (gdefinition gf-name))
156 (location (when (eq *boot-state* 'complete)
157 (accessor-values1 gf type class))))
158 (when (consp location)
159 (setf (car class-slot-p-cell) t))
162 (defun compute-pv (slot-name-lists wrappers)
163 (unless (listp wrappers) (setq wrappers (list wrappers)))
164 (let* ((not-simple-p-cell (list nil))
166 (gathering1 (collecting)
167 (iterate ((slot-names (list-elements slot-name-lists)))
169 (let* ((wrapper (pop wrappers))
170 (std-p (typep wrapper 'wrapper))
171 (class (wrapper-class* wrapper))
172 (class-slots (and std-p (wrapper-class-slots wrapper))))
173 (dolist (slot-name (cdr slot-names))
176 (compute-pv-slot slot-name wrapper class
177 class-slots not-simple-p-cell))))))))))
178 (if (car not-simple-p-cell)
179 (make-permutation-vector (cons t elements))
180 (or (gethash elements *pvs*)
181 (setf (gethash elements *pvs*)
182 (make-permutation-vector (cons nil elements)))))))
184 (defun compute-calls (call-list wrappers)
185 (declare (ignore call-list wrappers))
189 (compute-emf-from-wrappers call wrappers))
194 #|| ; Need to finish this, then write the maintenance functions.
195 (defun compute-emf-from-wrappers (call wrappers)
197 (destructuring-bind (gf-name nreq restp arg-info) call
198 (if (eq gf-name 'make-instance)
199 (error "should not get here") ; there is another mechanism for this.
200 #'(lambda (&rest args)
201 (if (not (eq *boot-state* 'complete))
202 (apply (gdefinition gf-name) args)
203 (let* ((gf (gdefinition gf-name))
204 (arg-info (arg-info-reader gf))
207 (emf (cache-miss-values-internal gf arg-info
208 wrappers classes types
210 (update-all-pv-tables call wrappers emf)
211 (invoke-emf emf args))))))))
214 (defun make-permutation-vector (indexes)
215 (make-array (length indexes) :initial-contents indexes))
217 (defun pv-table-lookup (pv-table pv-wrappers)
218 (let* ((slot-name-lists (pv-table-slot-name-lists pv-table))
219 (call-list (pv-table-call-list pv-table))
220 (cache (or (pv-table-cache pv-table)
221 (setf (pv-table-cache pv-table)
222 (get-cache (- (length slot-name-lists)
223 (count nil slot-name-lists))
227 (or (probe-cache cache pv-wrappers)
228 (let* ((pv (compute-pv slot-name-lists pv-wrappers))
229 (calls (compute-calls call-list pv-wrappers))
230 (pv-cell (cons pv calls))
231 (new-cache (fill-cache cache pv-wrappers pv-cell)))
232 (unless (eq new-cache cache)
233 (setf (pv-table-cache pv-table) new-cache)
237 (defun make-pv-type-declaration (var)
238 `(type simple-vector ,var))
240 (defvar *empty-pv* #())
242 (defmacro pvref (pv index)
245 (defmacro copy-pv (pv)
248 (defun make-calls-type-declaration (var)
249 `(type simple-vector ,var))
251 (defmacro callsref (calls index)
252 `(svref ,calls ,index))
254 (defvar *pv-table-cache-update-info* nil)
257 ;(method shared-initialize :after (structure-class t))
259 (defun update-pv-table-cache-info (class)
260 (let ((slot-names-for-pv-table-update nil)
262 (dolist (icu *pv-table-cache-update-info*)
263 (if (eq (car icu) class)
264 (pushnew (cdr icu) slot-names-for-pv-table-update)
265 (push icu new-icui)))
266 (setq *pv-table-cache-update-info* new-icui)
267 (when slot-names-for-pv-table-update
268 (update-all-pv-table-caches class slot-names-for-pv-table-update))))
270 (defun update-all-pv-table-caches (class slot-names)
271 (let* ((cwrapper (class-wrapper class))
272 (std-p (typep cwrapper 'wrapper))
273 (class-slots (and std-p (wrapper-class-slots cwrapper)))
274 (class-slot-p-cell (list nil))
275 (new-values (mapcar #'(lambda (slot-name)
279 slot-name cwrapper class
280 class-slots class-slot-p-cell))))
283 (dolist (slot-name slot-names)
284 (map-pv-table-references-of
286 #'(lambda (pv-table pv-offset-list)
287 (declare (ignore pv-offset-list))
288 (pushnew pv-table pv-tables))))
289 (dolist (pv-table pv-tables)
290 (let* ((cache (pv-table-cache pv-table))
291 (slot-name-lists (pv-table-slot-name-lists pv-table))
292 (pv-size (pv-table-pv-size pv-table))
293 (pv-map (make-array pv-size :initial-element nil)))
294 (let ((map-index 1)(param-index 0))
295 (dolist (slot-name-list slot-name-lists)
296 (dolist (slot-name (cdr slot-name-list))
297 (let ((a (assoc slot-name new-values)))
298 (setf (svref pv-map map-index)
299 (and a (cons param-index (cdr a)))))
303 (map-cache #'(lambda (wrappers pv-cell)
305 (update-slots-in-pv wrappers (car pv-cell)
306 cwrapper pv-size pv-map)))
309 (defun update-slots-in-pv (wrappers pv cwrapper pv-size pv-map)
310 (if (not (if (atom wrappers)
311 (eq cwrapper wrappers)
312 (dolist (wrapper wrappers nil)
313 (when (eq wrapper cwrapper)
316 (let* ((old-intern-p (listp (pvref pv 0)))
317 (new-pv (if old-intern-p
322 (dotimes-fixnum (i pv-size)
323 (when (consp (let ((map (svref pv-map i)))
325 (setf (pvref new-pv i) (cdr map))
327 (setq new-intern-p nil)))
329 (dolist (wrapper wrappers)
330 (when (eq wrapper cwrapper)
331 (dotimes-fixnum (i pv-size)
332 (when (consp (let ((map (svref pv-map i)))
333 (if (and map (= (car map) param))
334 (setf (pvref new-pv i) (cdr map))
336 (setq new-intern-p nil))))
339 (setq new-pv (let ((list-pv (coerce pv 'list)))
340 (or (gethash (cdr list-pv) *pvs*)
341 (setf (gethash (cdr list-pv) *pvs*)
344 (make-permutation-vector list-pv)))))))
347 (defun maybe-expand-accessor-form (form required-parameters slots env)
348 (let* ((fname (car form))
349 #||(len (length form))||#
350 (gf (if (symbolp fname)
351 (unencapsulated-fdefinition fname)
352 (gdefinition fname))))
353 (macrolet ((maybe-optimize-reader ()
355 (can-optimize-access1 (cadr form)
356 required-parameters env)))
358 (optimize-reader slots parameter gf-name form))))
359 (maybe-optimize-writer ()
361 (can-optimize-access1 (caddr form)
362 required-parameters env)))
364 (optimize-writer slots parameter gf-name form)))))
365 (unless (and (consp (cadr form))
366 (eq 'instance-accessor-parameter (caadr form)))
368 (cond ((and (= len 2) (symbolp fname))
369 (let ((gf-name (gethash fname *gf-declared-reader-table*)))
371 (maybe-optimize-reader))))
373 (let ((gf-name (gethash fname *gf-declared-writer-table*)))
375 (maybe-optimize-writer)))))
377 (when (and (eq *boot-state* 'complete)
378 (generic-function-p gf))
379 (let ((methods (generic-function-methods gf)))
381 (let* ((gf-name (generic-function-name gf))
382 (arg-info (gf-arg-info gf))
383 (metatypes (arg-info-metatypes arg-info))
384 (nreq (length metatypes))
385 (applyp (arg-info-applyp arg-info)))
388 (when (some #'standard-reader-method-p methods)
389 (maybe-optimize-reader)))
392 (eq (car gf-name) 'setf))
393 (when (some #'standard-writer-method-p methods)
394 (maybe-optimize-writer))))))))))))))
396 (defun optimize-generic-function-call (form
401 (declare (ignore required-parameters env slots calls))
402 (or (and (eq (car form) 'make-instance)
403 (expand-make-instance-form form))
405 (maybe-expand-accessor-form form required-parameters slots env)
406 (let* ((fname (car form))
408 (gf (if (symbolp fname)
410 (unencapsulated-fdefinition fname))
412 (gdefinition fname))))
413 (gf-name (and (fsc-instance-p gf)
416 (generic-function-name gf)))))
418 (multiple-value-bind (nreq restp)
419 (get-generic-function-info gf)
420 (optimize-gf-call slots calls form nreq restp env))))
424 (defun can-optimize-access (form required-parameters env)
425 (let ((type (ecase (car form)
427 (set-slot-value 'writer)
428 (slot-boundp 'boundp)))
430 (slot-name (eval (caddr form)))) ; known to be constant
431 (can-optimize-access1 var required-parameters env type slot-name)))
433 ;;; FIXME: This looks like an internal helper function for CAN-OPTIMIZE-ACCESS,
434 ;;; and it is used that way, but
435 ;;; it's also called bare from several places in the code. Perhaps
436 ;;; the two functions should be renamed fo CAN-OPTIMIZE-ACCESS-FOR-FORM
437 ;;; and CAN-OPTIMIZE-ACCESS-FOR-VAR. If so, I'd just as soon use keyword
438 ;;; args instead of optional ones, too.
439 (defun can-optimize-access1 (var required-parameters env
440 &optional type slot-name)
441 (when (and (consp var) (eq 'the (car var)))
442 ;; FIXME: We should assert list of length 3 here. Or maybe we should just
443 ;; define EXTRACT-THE, replace the whole
446 ;; (AWHEN (EXTRACT-THE VAR)
448 ;; and then use EXTRACT-THE similarly to clean up the other tests against
449 ;; 'THE scattered through the PCL code.
450 (setq var (caddr var)))
452 (let* ((rebound? (caddr (variable-declaration 'variable-rebinding
455 (parameter-or-nil (car (memq (or rebound? var)
456 required-parameters))))
457 (when parameter-or-nil
458 (let* ((class-name (caddr (variable-declaration 'class
461 (class (find-class class-name nil)))
462 (when (or (not (eq *boot-state* 'complete))
463 (and class (not (class-finalized-p class))))
465 (when (and class-name (not (eq class-name 't)))
466 (when (or (null type)
468 (memq *the-class-structure-object*
469 (class-precedence-list class))))
470 (optimize-slot-value-by-class-p class slot-name type))
471 (cons parameter-or-nil (or class class-name)))))))))
473 (defun optimize-slot-value (slots sparameter form)
475 (destructuring-bind (ignore1 ignore2 slot-name-form) form
476 (declare (ignore ignore1 ignore2))
477 (let ((slot-name (eval slot-name-form)))
478 (optimize-instance-access slots :read sparameter slot-name nil)))
479 `(accessor-slot-value ,@(cdr form))))
481 (defun optimize-set-slot-value (slots sparameter form)
483 (destructuring-bind (ignore1 ignore2 slot-name-form new-value) form
484 (declare (ignore ignore1 ignore2))
485 (let ((slot-name (eval slot-name-form)))
486 (optimize-instance-access slots
491 `(accessor-set-slot-value ,@(cdr form))))
493 (defun optimize-slot-boundp (slots sparameter form)
496 ;; FIXME: In CMU CL ca. 19991205, this binding list had a fourth
497 ;; element in it, NEW-VALUE. It's hard to see how that could possibly
498 ;; be right, since SLOT-BOUNDP has no NEW-VALUE. Since it was causing
499 ;; a failure in building PCL for SBCL, so I changed it to match the
500 ;; definition of SLOT-BOUNDP (and also to match the list used in the
501 ;; similar OPTIMIZE-SLOT-VALUE, above). However, I'm weirded out by
502 ;; this, since this is old code which has worked for ages to build
503 ;; PCL for CMU CL, so it's hard to see why it should need a patch
504 ;; like this in order to build PCL for SBCL. I'd like to return to
505 ;; this and find a test case which exercises this function both in
506 ;; CMU CL, to see whether it's really a previously-unexercised bug or
507 ;; whether I've misunderstood something (and, presumably, patched it
509 (slot-boundp-symbol instance slot-name-form)
511 (declare (ignore slot-boundp-symbol instance))
512 (let ((slot-name (eval slot-name-form)))
513 (optimize-instance-access slots
518 `(accessor-slot-boundp ,@(cdr form))))
520 (defun optimize-reader (slots sparameter gf-name form)
522 (optimize-accessor-call slots :read sparameter gf-name nil)
525 (defun optimize-writer (slots sparameter gf-name form)
527 (destructuring-bind (ignore1 ignore2 new-value) form
528 (declare (ignore ignore1 ignore2))
529 (optimize-accessor-call slots :write sparameter gf-name new-value))
532 ;;; The SLOTS argument is an alist, the CAR of each entry is the name of
533 ;;; a required parameter to the function. The alist is in order, so the
534 ;;; position of an entry in the alist corresponds to the argument's position
535 ;;; in the lambda list.
536 (defun optimize-instance-access (slots
541 (let ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
542 (parameter (if (consp sparameter) (car sparameter) sparameter)))
543 (if (and (eq *boot-state* 'complete)
545 (memq *the-class-structure-object* (class-precedence-list class)))
546 (let ((slotd (find-slot-definition class slot-name)))
549 `(,(slot-definition-defstruct-accessor-symbol slotd) ,parameter))
551 `(setf (,(slot-definition-defstruct-accessor-symbol slotd)
556 (let* ((parameter-entry (assq parameter slots))
557 (slot-entry (assq slot-name (cdr parameter-entry)))
558 (position (posq parameter-entry slots))
559 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
560 (unless parameter-entry
561 (error "internal error in slot optimization"))
563 (setq slot-entry (list slot-name))
564 (push slot-entry (cdr parameter-entry)))
565 (push pv-offset-form (cdr slot-entry))
568 `(instance-read ,pv-offset-form ,parameter ,position
569 ',slot-name ',class))
571 `(let ((.new-value. ,new-value))
572 (instance-write ,pv-offset-form ,parameter ,position
573 ',slot-name ',class .new-value.)))
575 `(instance-boundp ,pv-offset-form ,parameter ,position
576 ',slot-name ',class)))))))
578 (defun optimize-accessor-call (slots read/write sparameter gf-name new-value)
579 (let* ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
580 (parameter (if (consp sparameter) (car sparameter) sparameter))
581 (parameter-entry (assq parameter slots))
582 (name (case read/write
583 (:read `(reader ,gf-name))
584 (:write `(writer ,gf-name))))
585 (slot-entry (assoc name (cdr parameter-entry) :test #'equal))
586 (position (posq parameter-entry slots))
587 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
588 (unless parameter-entry
589 (error "internal error in slot optimization"))
591 (setq slot-entry (list name))
592 (push slot-entry (cdr parameter-entry)))
593 (push pv-offset-form (cdr slot-entry))
596 `(instance-reader ,pv-offset-form ,parameter ,position ,gf-name ',class))
598 `(let ((.new-value. ,new-value))
599 (instance-writer ,pv-offset-form ,parameter ,position ,gf-name ',class
602 (defvar *unspecific-arg* '..unspecific-arg..)
604 (defun optimize-gf-call-internal (form slots env)
605 (when (and (consp form)
606 (eq (car form) 'the))
607 (setq form (caddr form)))
608 (or (and (symbolp form)
609 (let* ((rebound? (caddr (variable-declaration 'variable-rebinding
611 (parameter-or-nil (car (assq (or rebound? form) slots))))
612 (when parameter-or-nil
613 (let* ((class-name (caddr (variable-declaration
614 'class parameter-or-nil env))))
615 (when (and class-name (not (eq class-name 't)))
616 (position parameter-or-nil slots :key #'car))))))
618 (let ((form (eval form)))
624 (defun optimize-gf-call (slots calls gf-call-form nreq restp env)
625 (unless (eq (car gf-call-form) 'make-instance) ; needs more work
626 (let* ((args (cdr gf-call-form))
627 (all-args-p (eq (car gf-call-form) 'make-instance))
628 (non-required-args (nthcdr nreq args))
629 (required-args (ldiff args non-required-args))
630 (call-spec (list (car gf-call-form) nreq restp
631 (mapcar #'(lambda (form)
632 (optimize-gf-call-internal form slots env))
636 (call-entry (assoc call-spec calls :test #'equal))
637 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
638 (unless (some #'integerp
639 (let ((spec-args (cdr call-spec)))
641 (ldiff spec-args (nthcdr nreq spec-args))
643 (return-from optimize-gf-call nil))
645 (setq call-entry (list call-spec))
646 (push call-entry (cdr calls)))
647 (push pv-offset-form (cdr call-entry))
648 (if (eq (car call-spec) 'make-instance)
649 `(funcall (pv-ref .pv. ,pv-offset-form) ,@(cdr gf-call-form))
650 `(let ((.emf. (pv-ref .pv. ,pv-offset-form)))
651 (invoke-effective-method-function .emf. ,restp
652 ,@required-args ,@(when restp `((list ,@non-required-args)))))))))
654 (define-walker-template pv-offset) ; These forms get munged by mutate slots.
655 (defmacro pv-offset (arg) arg)
656 (define-walker-template instance-accessor-parameter)
657 (defmacro instance-accessor-parameter (x) x)
659 ;; It is safe for these two functions to be wrong.
660 ;; They just try to guess what the most likely case will be.
661 (defun generate-fast-class-slot-access-p (class-form slot-name-form)
662 (let ((class (and (constantp class-form) (eval class-form)))
663 (slot-name (and (constantp slot-name-form) (eval slot-name-form))))
664 (and (eq *boot-state* 'complete)
665 (standard-class-p class)
666 (not (eq class *the-class-t*)) ; shouldn't happen, though.
667 (let ((slotd (find-slot-definition class slot-name)))
668 (and slotd (classp (slot-definition-allocation slotd)))))))
670 (defun skip-fast-slot-access-p (class-form slot-name-form type)
671 (let ((class (and (constantp class-form) (eval class-form)))
672 (slot-name (and (constantp slot-name-form) (eval slot-name-form))))
673 (and (eq *boot-state* 'complete)
674 (standard-class-p class)
675 (not (eq class *the-class-t*)) ; shouldn't happen, though.
676 (let ((slotd (find-slot-definition class slot-name)))
677 (and slotd (skip-optimize-slot-value-by-class-p class slot-name type))))))
679 (defun skip-optimize-slot-value-by-class-p (class slot-name type)
680 (let ((slotd (find-slot-definition class slot-name)))
682 (eq *boot-state* 'complete)
683 (not (slot-accessor-std-p slotd type)))))
685 (defmacro instance-read-internal (pv slots pv-offset default &optional type)
686 (unless (member type '(nil :instance :class :default))
687 (error "illegal type argument to ~S: ~S" 'instance-read-internal type))
688 (if (eq type ':default)
690 (let* ((index (gensym))
692 `(locally (declare #.*optimize-speed*)
693 (let ((,index (pvref ,pv ,pv-offset)))
694 (setq ,value (typecase ,index
695 ,@(when (or (null type) (eq type ':instance))
696 `((fixnum (%instance-ref ,slots ,index))))
697 ,@(when (or (null type) (eq type ':class))
698 `((cons (cdr ,index))))
699 (t ',*slot-unbound*)))
700 (if (eq ,value ',*slot-unbound*)
704 (defmacro instance-read (pv-offset parameter position slot-name class)
705 (if (skip-fast-slot-access-p class slot-name 'reader)
706 `(accessor-slot-value ,parameter ,slot-name)
707 `(instance-read-internal .pv. ,(slot-vector-symbol position)
708 ,pv-offset (accessor-slot-value ,parameter ,slot-name)
709 ,(if (generate-fast-class-slot-access-p class slot-name)
710 ':class ':instance))))
712 (defmacro instance-reader (pv-offset parameter position gf-name class)
713 (declare (ignore class))
714 `(instance-read-internal .pv. ,(slot-vector-symbol position)
716 (,gf-name (instance-accessor-parameter ,parameter))
719 (defmacro instance-write-internal (pv slots pv-offset new-value default
721 (unless (member type '(nil :instance :class :default))
722 (error "illegal type argument to ~S: ~S" 'instance-write-internal type))
723 (if (eq type ':default)
725 (let* ((index (gensym)))
726 `(locally (declare #.*optimize-speed*)
727 (let ((,index (pvref ,pv ,pv-offset)))
729 ,@(when (or (null type) (eq type ':instance))
730 `((fixnum (setf (%instance-ref ,slots ,index) ,new-value))))
731 ,@(when (or (null type) (eq type ':class))
732 `((cons (setf (cdr ,index) ,new-value))))
735 (defmacro instance-write (pv-offset
741 (if (skip-fast-slot-access-p class slot-name 'writer)
742 `(accessor-set-slot-value ,parameter ,slot-name ,new-value)
743 `(instance-write-internal .pv. ,(slot-vector-symbol position)
744 ,pv-offset ,new-value
745 (accessor-set-slot-value ,parameter ,slot-name ,new-value)
746 ,(if (generate-fast-class-slot-access-p class slot-name)
747 ':class ':instance))))
749 (defmacro instance-writer (pv-offset
755 (declare (ignore class))
756 `(instance-write-internal .pv. ,(slot-vector-symbol position)
757 ,pv-offset ,new-value
758 (,(if (consp gf-name)
759 (get-setf-function-name gf-name)
761 (instance-accessor-parameter ,parameter)
765 (defmacro instance-boundp-internal (pv slots pv-offset default
767 (unless (member type '(nil :instance :class :default))
768 (error "illegal type argument to ~S: ~S" 'instance-boundp-internal type))
769 (if (eq type ':default)
771 (let* ((index (gensym)))
772 `(locally (declare #.*optimize-speed*)
773 (let ((,index (pvref ,pv ,pv-offset)))
775 ,@(when (or (null type) (eq type ':instance))
776 `((fixnum (not (eq (%instance-ref ,slots ,index)
777 ',*slot-unbound*)))))
778 ,@(when (or (null type) (eq type ':class))
779 `((cons (not (eq (cdr ,index) ',*slot-unbound*)))))
782 (defmacro instance-boundp (pv-offset parameter position slot-name class)
783 (if (skip-fast-slot-access-p class slot-name 'boundp)
784 `(accessor-slot-boundp ,parameter ,slot-name)
785 `(instance-boundp-internal .pv. ,(slot-vector-symbol position)
786 ,pv-offset (accessor-slot-boundp ,parameter ,slot-name)
787 ,(if (generate-fast-class-slot-access-p class slot-name)
788 ':class ':instance))))
790 ;;; This magic function has quite a job to do indeed.
792 ;;; The careful reader will recall that <slots> contains all of the optimized
793 ;;; slot access forms produced by OPTIMIZE-INSTANCE-ACCESS. Each of these is
794 ;;; a call to either INSTANCE-READ or INSTANCE-WRITE.
796 ;;; At the time these calls were produced, the first argument was specified as
797 ;;; the symbol .PV-OFFSET.; what we have to do now is convert those pv-offset
798 ;;; arguments into the actual number that is the correct offset into the pv.
800 ;;; But first, oh but first, we sort <slots> a bit so that for each argument we
801 ;;; have the slots in alphabetical order. This canonicalizes the PV-TABLE's a
802 ;;; bit and will hopefully lead to having fewer PV's floating around. Even if
803 ;;; the gain is only modest, it costs nothing.
804 (defun slot-name-lists-from-slots (slots calls)
805 (multiple-value-bind (slots calls) (mutate-slots-and-calls slots calls)
806 (let* ((slot-name-lists
807 (mapcar #'(lambda (parameter-entry)
808 (cons nil (mapcar #'car (cdr parameter-entry))))
811 (mapcar #'car calls)))
812 (dolist (call call-list)
813 (dolist (arg (cdr call))
815 (setf (car (nth arg slot-name-lists)) t))))
816 (setq slot-name-lists (mapcar #'(lambda (r+snl)
817 (when (or (car r+snl) (cdr r+snl))
820 (let ((cvt (apply #'vector
822 (mapcar #'(lambda (r+snl)
823 (when r+snl (incf i)))
825 (setq call-list (mapcar #'(lambda (call)
827 (mapcar #'(lambda (arg)
833 (values slot-name-lists call-list))))
835 (defun mutate-slots-and-calls (slots calls)
836 (let ((sorted-slots (sort-slots slots))
837 (sorted-calls (sort-calls (cdr calls)))
838 (pv-offset 0)) ; index 0 is for info
839 (dolist (parameter-entry sorted-slots)
840 (dolist (slot-entry (cdr parameter-entry))
842 (dolist (form (cdr slot-entry))
843 (setf (cadr form) pv-offset))))
844 (dolist (call-entry sorted-calls)
846 (dolist (form (cdr call-entry))
847 (setf (cadr form) pv-offset)))
848 (values sorted-slots sorted-calls)))
850 (defun symbol-pkg-name (sym)
851 (let ((pkg (symbol-package sym)))
852 (if pkg (package-name pkg) "")))
854 ;;; FIXME: Because of the existence of UNINTERN and RENAME-PACKAGE,
855 ;;; the part of this ordering which is based on SYMBOL-PKG-NAME is not
856 ;;; stable. This ordering is only used in to
857 ;;; SLOT-NAME-LISTS-FROM-SLOTS, where it serves to "canonicalize the
858 ;;; PV-TABLE's a bit and will hopefully lead to having fewer PV's
859 ;;; floating around", so it sounds as though the instability won't
860 ;;; actually lead to bugs, just small inefficiency. But still, it
861 ;;; would be better to reimplement this function as a comparison based
863 ;;; * stable comparison
864 ;;; * smaller code (here, and in being able to discard SYMBOL-PKG-NAME)
866 (defun symbol-lessp (a b)
867 (if (eq (symbol-package a)
869 (string-lessp (symbol-name a)
871 (string-lessp (symbol-pkg-name a)
872 (symbol-pkg-name b))))
874 (defun symbol-or-cons-lessp (a b)
877 (symbol (symbol-lessp a b))
881 (cons (if (eq (car a) (car b))
882 (symbol-or-cons-lessp (cdr a) (cdr b))
883 (symbol-or-cons-lessp (car a) (car b))))))))
885 (defun sort-slots (slots)
886 (mapcar #'(lambda (parameter-entry)
887 (cons (car parameter-entry)
888 (sort (cdr parameter-entry) ;slot entries
889 #'symbol-or-cons-lessp
893 (defun sort-calls (calls)
894 (sort calls #'symbol-or-cons-lessp :key #'car))
896 ;;; This needs to work in terms of metatypes and also needs to work for
897 ;;; automatically generated reader and writer functions.
898 ;;; -- Automatically generated reader and writer functions use this stuff too.
900 (defmacro pv-binding ((required-parameters slot-name-lists pv-table-symbol)
902 (with-gathering ((slot-vars (collecting))
903 (pv-parameters (collecting)))
904 (iterate ((slots (list-elements slot-name-lists))
905 (required-parameter (list-elements required-parameters))
906 (i (interval :from 0)))
908 (gather required-parameter pv-parameters)
909 (gather (slot-vector-symbol i) slot-vars)))
910 `(pv-binding1 (.pv. .calls. ,pv-table-symbol ,pv-parameters ,slot-vars)
913 (defmacro pv-binding1 ((pv calls pv-table-symbol pv-parameters slot-vars)
915 `(pv-env (,pv ,calls ,pv-table-symbol ,pv-parameters)
916 (let (,@(mapcar #'(lambda (slot-var p) `(,slot-var (get-slots-or-nil ,p)))
917 slot-vars pv-parameters))
920 ;This gets used only when the default make-method-lambda is overriden.
921 (defmacro pv-env ((pv calls pv-table-symbol pv-parameters)
923 `(let* ((.pv-table. ,pv-table-symbol)
924 (.pv-cell. (pv-table-lookup-pv-args .pv-table. ,@pv-parameters))
925 (,pv (car .pv-cell.))
926 (,calls (cdr .pv-cell.)))
927 (declare ,(make-pv-type-declaration pv))
928 (declare ,(make-calls-type-declaration calls))
929 ,@(when (symbolp pv-table-symbol)
930 `((declare (special ,pv-table-symbol))))
934 (defvar *non-variable-declarations*
935 ;; FIXME: VALUES was in this list, conditionalized with #+CMU, but
936 ;; I don't *think* CMU CL had, or SBCL has, VALUES declarations. If
937 ;; SBCL doesn't have 'em, VALUES should probably be removed from this list.
938 '(values method-name method-lambda-list
939 optimize ftype inline notinline))
941 (defvar *variable-declarations-with-argument*
945 (defvar *variable-declarations-without-argument*
946 '(ignore ignorable special dynamic-extent
947 array atom base-char bignum bit bit-vector character compiled-function
948 complex cons double-float extended-char
949 fixnum float function hash-table integer
950 keyword list long-float nil null number package pathname random-state ratio
951 rational readtable sequence short-float signed-byte simple-array
952 simple-bit-vector simple-string simple-vector single-float standard-char
953 stream string symbol t unsigned-byte vector))
955 (defun split-declarations (body args calls-next-method-p)
956 (let ((inner-decls nil) (outer-decls nil) decl)
957 (loop (when (null body) (return nil))
958 (setq decl (car body))
959 (unless (and (consp decl)
960 (eq (car decl) 'declare))
962 (dolist (form (cdr decl))
964 (let ((declaration-name (car form)))
965 (if (member declaration-name *non-variable-declarations*)
966 (push `(declare ,form) outer-decls)
968 (member declaration-name
969 *variable-declarations-with-argument*))
971 (member declaration-name
972 *variable-declarations-without-argument*))
973 (dname (list (pop form)))
974 (inners nil) (outers nil))
975 (unless (or arg-p non-arg-p)
976 ;; FIXME: This warning should probably go away now
977 ;; that we're not trying to be portable between
978 ;; different CLTL1 hosts the way PCL was.
979 (warn "The declaration ~S is not understood by ~S.~@
980 Please put ~S on one of the lists ~S,~%~S, or~%~S.~@
981 (Assuming it is a variable declaration without argument)."
982 declaration-name 'split-declarations
984 '*non-variable-declarations*
985 '*variable-declarations-with-argument*
986 '*variable-declarations-without-argument*)
987 (push declaration-name
988 *variable-declarations-without-argument*))
990 (setq dname (append dname (list (pop form)))))
992 (if (member var args)
993 ;; Quietly remove IGNORE declarations on args when
994 ;; a next-method is involved, to prevent compiler
995 ;; warns about ignored args being read.
996 (unless (and calls-next-method-p
997 (eq (car dname) 'ignore))
1001 (push `(declare (,@dname ,@outers)) outer-decls))
1003 (push `(declare (,@dname ,@inners)) inner-decls)))))))
1004 (setq body (cdr body)))
1005 (values outer-decls inner-decls body)))
1007 (defun make-method-initargs-form-internal (method-lambda initargs env)
1008 (declare (ignore env))
1009 (let (method-lambda-args lmf lmf-params)
1010 (if (not (and (= 3 (length method-lambda))
1011 (= 2 (length (setq method-lambda-args (cadr method-lambda))))
1012 (consp (setq lmf (third method-lambda)))
1013 (eq 'simple-lexical-method-functions (car lmf))
1014 (eq (car method-lambda-args)
1015 (cadr (setq lmf-params (cadr lmf))))
1016 (eq (cadr method-lambda-args)
1017 (caddr lmf-params))))
1018 `(list* :function #',method-lambda
1020 (let* ((lambda-list (car lmf-params))
1021 (nreq 0)(restp nil)(args nil))
1022 (dolist (arg lambda-list)
1023 (when (member arg '(&optional &rest &key))
1024 (setq restp t)(return nil))
1025 (when (eq arg '&aux) (return nil))
1026 (incf nreq)(push arg args))
1027 (setq args (nreverse args))
1028 (setf (getf (getf initargs ':plist) ':arg-info) (cons nreq restp))
1029 (make-method-initargs-form-internal1
1030 initargs (cddr lmf) args lmf-params restp)))))
1032 (defun make-method-initargs-form-internal1
1033 (initargs body req-args lmf-params restp)
1034 (multiple-value-bind (outer-decls inner-decls body)
1036 body req-args (getf (cdr lmf-params) :call-next-method-p))
1037 (let* ((rest-arg (when restp '.rest-arg.))
1038 (args+rest-arg (if restp
1039 (append req-args (list rest-arg))
1041 `(list* :fast-function
1042 #'(lambda (.pv-cell. .next-method-call. ,@args+rest-arg)
1044 .pv-cell. .next-method-call.
1045 (macrolet ((pv-env ((pv calls pv-table-symbol pv-parameters)
1047 (declare (ignore pv-table-symbol pv-parameters))
1048 `(let ((,pv (car .pv-cell.))
1049 (,calls (cdr .pv-cell.)))
1050 (declare ,(make-pv-type-declaration pv)
1051 ,(make-calls-type-declaration calls))
1054 (fast-lexical-method-functions
1055 (,(car lmf-params) .next-method-call. ,req-args ,rest-arg
1056 ,@(cdddr lmf-params))
1061 ;;; Use arrays and hash tables and the fngen stuff to make this much better. It
1062 ;;; doesn't really matter, though, because a function returned by this will get
1063 ;;; called only when the user explicitly funcalls a result of method-function.
1064 ;;; BUT, this is needed to make early methods work.
1065 (defun method-function-from-fast-function (fmf)
1066 (declare (type function fmf))
1067 (let* ((method-function nil) (pv-table nil)
1068 (arg-info (method-function-get fmf ':arg-info))
1069 (nreq (car arg-info))
1070 (restp (cdr arg-info)))
1071 (setq method-function
1072 #'(lambda (method-args next-methods)
1074 (setq pv-table (method-function-pv-table fmf)))
1075 (let* ((pv-cell (when pv-table
1076 (get-method-function-pv-cell
1077 method-function method-args pv-table)))
1078 (nm (car next-methods))
1079 (nms (cdr next-methods))
1081 (make-method-call :function (if (std-instance-p nm)
1082 (method-function nm)
1084 :call-method-args (list nms)))))
1086 (let* ((rest (nthcdr nreq method-args))
1087 (args (ldiff method-args rest)))
1088 (apply fmf pv-cell nmc (nconc args (list rest))))
1089 (apply fmf pv-cell nmc method-args)))))
1090 (let* ((fname (method-function-get fmf :name))
1091 (name `(,(or (get (car fname) 'method-sym)
1092 (setf (get (car fname) 'method-sym)
1093 (let ((str (symbol-name (car fname))))
1094 (if (string= "FAST-" str :end2 5)
1095 (intern (subseq str 5) *pcl-package*)
1098 (set-function-name method-function name))
1099 (setf (method-function-get method-function :fast-function) fmf)
1102 (defun get-method-function-pv-cell (method-function
1105 (let ((pv-table (or pv-table (method-function-pv-table method-function))))
1107 (let ((pv-wrappers (pv-wrappers-from-all-args pv-table method-args)))
1109 (pv-table-lookup pv-table pv-wrappers))))))
1111 (defun pv-table-lookup-pv-args (pv-table &rest pv-parameters)
1112 (pv-table-lookup pv-table (pv-wrappers-from-pv-args pv-parameters)))
1114 (defun pv-wrappers-from-pv-args (&rest args)
1115 (let* ((nkeys (length args))
1116 (pv-wrappers (make-list nkeys))
1120 (setq w (wrapper-of arg))
1121 (unless (eq 't (wrapper-state w)) ; FIXME: should be INVALID-WRAPPER-P
1122 (setq w (check-wrapper-validity arg)))
1124 (setq w-t (cdr w-t))
1125 (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))
1128 (defun pv-wrappers-from-all-args (pv-table args)
1130 (slot-name-lists (pv-table-slot-name-lists pv-table)))
1131 (dolist (sn slot-name-lists)
1132 (when sn (incf nkeys)))
1133 (let* ((pv-wrappers (make-list nkeys))
1134 (pv-w-t pv-wrappers))
1135 (dolist (sn slot-name-lists)
1137 (let* ((arg (car args))
1138 (w (wrapper-of arg)))
1139 (unless w ; CAN-OPTIMIZE-ACCESS prevents this from happening.
1140 (error "error in PV-WRAPPERS-FROM-ALL-ARGS"))
1141 (setf (car pv-w-t) w)
1142 (setq pv-w-t (cdr pv-w-t))))
1143 (setq args (cdr args)))
1144 (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))
1147 (defun pv-wrappers-from-all-wrappers (pv-table wrappers)
1149 (slot-name-lists (pv-table-slot-name-lists pv-table)))
1150 (dolist (sn slot-name-lists)
1151 (when sn (incf nkeys)))
1152 (let* ((pv-wrappers (make-list nkeys))
1153 (pv-w-t pv-wrappers))
1154 (dolist (sn slot-name-lists)
1156 (let ((w (car wrappers)))
1157 (unless w ; CAN-OPTIMIZE-ACCESS prevents this from happening.
1158 (error "error in PV-WRAPPERS-FROM-ALL-WRAPPERS"))
1159 (setf (car pv-w-t) w)
1160 (setq pv-w-t (cdr pv-w-t))))
1161 (setq wrappers (cdr wrappers)))
1162 (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))