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
28 (defmacro instance-slot-index (wrapper slot-name)
30 (declare (fixnum pos))
32 (dolist (sn (wrapper-instance-slots-layout ,wrapper))
33 (when (eq ,slot-name sn) (return-from loop pos))
36 (defun pv-cache-limit-fn (nlines)
37 (default-limit-fn nlines))
40 (:predicate pv-tablep)
41 (:constructor make-pv-table-internal
42 (slot-name-lists call-list)))
43 (cache nil :type (or cache null))
44 (pv-size 0 :type fixnum)
45 (slot-name-lists nil :type list)
46 (call-list nil :type list))
48 #-sb-fluid (declaim (sb-ext:freeze-type pv-table))
50 (defvar *initial-pv-table* (make-pv-table-internal nil nil))
52 ; help new slot-value-using-class methods affect fast iv access
53 (defvar *all-pv-table-list* nil)
55 (defun make-pv-table (&key slot-name-lists call-list)
56 (let ((pv-table (make-pv-table-internal slot-name-lists call-list)))
57 (push pv-table *all-pv-table-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)
72 (or (gethash x *slot-name-lists-inner*)
73 (setf (gethash x *slot-name-lists-inner*) (copy-list x))))
75 (or (gethash x *slot-name-lists-outer*)
76 (setf (gethash x *slot-name-lists-outer*)
77 (let ((snl (copy-list (cdr x)))
80 (make-pv-table :slot-name-lists snl
82 (let ((pv-table (outer (mapcar #'inner (cons call-list slot-name-lists)))))
85 (dolist (slot-name-list slot-name-lists)
86 (dolist (slot-name (cdr slot-name-list))
87 (note-pv-table-reference slot-name pv-index pv-table)
89 (dolist (gf-call call-list)
90 (note-pv-table-reference gf-call pv-index pv-table)
92 (setf (pv-table-pv-size pv-table) pv-index)))
95 (defun note-pv-table-reference (ref pv-offset pv-table)
96 (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
98 (let ((table-entry (assq pv-table entry)))
99 (when (and (null table-entry)
100 (> (length entry) 8))
101 (let ((new-table-table (make-hash-table :size 16 :test 'eq)))
102 (dolist (table-entry entry)
103 (setf (gethash (car table-entry) new-table-table)
105 (setf (gethash ref *pv-key-to-pv-table-table*) new-table-table)))
107 (if (null table-entry)
108 (let ((new (cons pv-table pv-offset)))
110 (push new (cdr entry))
111 (setf (gethash ref *pv-key-to-pv-table-table*)
113 (push pv-offset (cdr table-entry)))
114 (return-from note-pv-table-reference nil))))
115 (let ((list (gethash pv-table entry)))
117 (push pv-offset (cdr list))
118 (setf (gethash pv-table entry) (list pv-offset)))))
121 (defun map-pv-table-references-of (ref function)
122 (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
124 (dolist (table+pv-offset-list entry)
126 (car table+pv-offset-list)
127 (cdr table+pv-offset-list)))
128 (maphash function entry)))
131 (defvar *pvs* (make-hash-table :test 'equal))
133 (defun optimize-slot-value-by-class-p (class slot-name type)
134 (or (not (eq *boot-state* 'complete))
135 (let ((slotd (find-slot-definition class slot-name)))
137 (slot-accessor-std-p slotd type)))))
139 (defun compute-pv-slot (slot-name wrapper class class-slots class-slot-p-cell)
140 (if (symbolp slot-name)
141 (when (optimize-slot-value-by-class-p class slot-name 'all)
142 (or (instance-slot-index wrapper slot-name)
143 (let ((cell (assq slot-name class-slots)))
145 (setf (car class-slot-p-cell) t)
147 (when (consp slot-name)
148 (dolist (type '(reader writer) nil)
149 (when (eq (car slot-name) type)
151 (let* ((gf-name (cadr slot-name))
152 (gf (gdefinition gf-name))
153 (location (when (eq *boot-state* 'complete)
154 (accessor-values1 gf type class))))
155 (when (consp location)
156 (setf (car class-slot-p-cell) t))
159 (defun compute-pv (slot-name-lists wrappers)
160 (unless (listp wrappers) (setq wrappers (list wrappers)))
161 (let* ((not-simple-p-cell (list nil))
163 (gathering1 (collecting)
164 (iterate ((slot-names (list-elements slot-name-lists)))
166 (let* ((wrapper (pop wrappers))
167 (std-p (typep wrapper 'wrapper))
168 (class (wrapper-class* wrapper))
169 (class-slots (and std-p (wrapper-class-slots wrapper))))
170 (dolist (slot-name (cdr slot-names))
173 (compute-pv-slot slot-name wrapper class
174 class-slots not-simple-p-cell))))))))))
175 (if (car not-simple-p-cell)
176 (make-permutation-vector (cons t elements))
177 (or (gethash elements *pvs*)
178 (setf (gethash elements *pvs*)
179 (make-permutation-vector (cons nil elements)))))))
181 (defun compute-calls (call-list wrappers)
182 (declare (ignore call-list wrappers))
186 (compute-emf-from-wrappers call wrappers))
191 #|| ; Need to finish this, then write the maintenance functions.
192 (defun compute-emf-from-wrappers (call wrappers)
194 (destructuring-bind (gf-name nreq restp arg-info) call
195 (if (eq gf-name 'make-instance)
196 (error "should not get here") ; there is another mechanism for this.
197 #'(lambda (&rest args)
198 (if (not (eq *boot-state* 'complete))
199 (apply (gdefinition gf-name) args)
200 (let* ((gf (gdefinition gf-name))
201 (arg-info (arg-info-reader gf))
204 (emf (cache-miss-values-internal gf arg-info
205 wrappers classes types
207 (update-all-pv-tables call wrappers emf)
208 (invoke-emf emf args))))))))
211 (defun make-permutation-vector (indexes)
212 (make-array (length indexes) :initial-contents indexes))
214 (defun pv-table-lookup (pv-table pv-wrappers)
215 (let* ((slot-name-lists (pv-table-slot-name-lists pv-table))
216 (call-list (pv-table-call-list pv-table))
217 (cache (or (pv-table-cache pv-table)
218 (setf (pv-table-cache pv-table)
219 (get-cache (- (length slot-name-lists)
220 (count nil slot-name-lists))
224 (or (probe-cache cache pv-wrappers)
225 (let* ((pv (compute-pv slot-name-lists pv-wrappers))
226 (calls (compute-calls call-list pv-wrappers))
227 (pv-cell (cons pv calls))
228 (new-cache (fill-cache cache pv-wrappers pv-cell)))
229 (unless (eq new-cache cache)
230 (setf (pv-table-cache pv-table) new-cache)
234 (defun make-pv-type-declaration (var)
235 `(type simple-vector ,var))
237 (defvar *empty-pv* #())
239 (defmacro pvref (pv index)
242 (defmacro copy-pv (pv)
245 (defun make-calls-type-declaration (var)
246 `(type simple-vector ,var))
248 (defmacro callsref (calls index)
249 `(svref ,calls ,index))
251 (defvar *pv-table-cache-update-info* nil)
254 ;(method shared-initialize :after (structure-class t))
256 (defun update-pv-table-cache-info (class)
257 (let ((slot-names-for-pv-table-update nil)
259 (dolist (icu *pv-table-cache-update-info*)
260 (if (eq (car icu) class)
261 (pushnew (cdr icu) slot-names-for-pv-table-update)
262 (push icu new-icui)))
263 (setq *pv-table-cache-update-info* new-icui)
264 (when slot-names-for-pv-table-update
265 (update-all-pv-table-caches class slot-names-for-pv-table-update))))
267 (defun update-all-pv-table-caches (class slot-names)
268 (let* ((cwrapper (class-wrapper class))
269 (std-p (typep cwrapper 'wrapper))
270 (class-slots (and std-p (wrapper-class-slots cwrapper)))
271 (class-slot-p-cell (list nil))
272 (new-values (mapcar #'(lambda (slot-name)
276 slot-name cwrapper class
277 class-slots class-slot-p-cell))))
280 (dolist (slot-name slot-names)
281 (map-pv-table-references-of
283 #'(lambda (pv-table pv-offset-list)
284 (declare (ignore pv-offset-list))
285 (pushnew pv-table pv-tables))))
286 (dolist (pv-table pv-tables)
287 (let* ((cache (pv-table-cache pv-table))
288 (slot-name-lists (pv-table-slot-name-lists pv-table))
289 (pv-size (pv-table-pv-size pv-table))
290 (pv-map (make-array pv-size :initial-element nil)))
291 (let ((map-index 1)(param-index 0))
292 (dolist (slot-name-list slot-name-lists)
293 (dolist (slot-name (cdr slot-name-list))
294 (let ((a (assoc slot-name new-values)))
295 (setf (svref pv-map map-index)
296 (and a (cons param-index (cdr a)))))
300 (map-cache #'(lambda (wrappers pv-cell)
302 (update-slots-in-pv wrappers (car pv-cell)
303 cwrapper pv-size pv-map)))
306 (defun update-slots-in-pv (wrappers pv cwrapper pv-size pv-map)
307 (if (not (if (atom wrappers)
308 (eq cwrapper wrappers)
309 (dolist (wrapper wrappers nil)
310 (when (eq wrapper cwrapper)
313 (let* ((old-intern-p (listp (pvref pv 0)))
314 (new-pv (if old-intern-p
319 (dotimes-fixnum (i pv-size)
320 (when (consp (let ((map (svref pv-map i)))
322 (setf (pvref new-pv i) (cdr map))
324 (setq new-intern-p nil)))
326 (dolist (wrapper wrappers)
327 (when (eq wrapper cwrapper)
328 (dotimes-fixnum (i pv-size)
329 (when (consp (let ((map (svref pv-map i)))
330 (if (and map (= (car map) param))
331 (setf (pvref new-pv i) (cdr map))
333 (setq new-intern-p nil))))
336 (setq new-pv (let ((list-pv (coerce pv 'list)))
337 (or (gethash (cdr list-pv) *pvs*)
338 (setf (gethash (cdr list-pv) *pvs*)
341 (make-permutation-vector list-pv)))))))
344 (defun maybe-expand-accessor-form (form required-parameters slots env)
345 (let* ((fname (car form))
346 #||(len (length form))||#
347 (gf (if (symbolp fname)
348 (unencapsulated-fdefinition fname)
349 (gdefinition fname))))
350 (macrolet ((maybe-optimize-reader ()
352 (can-optimize-access1 (cadr form)
353 required-parameters env)))
355 (optimize-reader slots parameter gf-name form))))
356 (maybe-optimize-writer ()
358 (can-optimize-access1 (caddr form)
359 required-parameters env)))
361 (optimize-writer slots parameter gf-name form)))))
362 (unless (and (consp (cadr form))
363 (eq 'instance-accessor-parameter (caadr form)))
365 (cond ((and (= len 2) (symbolp fname))
366 (let ((gf-name (gethash fname *gf-declared-reader-table*)))
368 (maybe-optimize-reader))))
370 (let ((gf-name (gethash fname *gf-declared-writer-table*)))
372 (maybe-optimize-writer)))))
374 (when (and (eq *boot-state* 'complete)
375 (generic-function-p gf))
376 (let ((methods (generic-function-methods gf)))
378 (let* ((gf-name (generic-function-name gf))
379 (arg-info (gf-arg-info gf))
380 (metatypes (arg-info-metatypes arg-info))
381 (nreq (length metatypes))
382 (applyp (arg-info-applyp arg-info)))
385 (when (some #'standard-reader-method-p methods)
386 (maybe-optimize-reader)))
389 (eq (car gf-name) 'setf))
390 (when (some #'standard-writer-method-p methods)
391 (maybe-optimize-writer))))))))))))))
393 (defun optimize-generic-function-call (form
398 (declare (ignore required-parameters env slots calls))
399 (or (and (eq (car form) 'make-instance)
400 (expand-make-instance-form form))
402 (maybe-expand-accessor-form form required-parameters slots env)
403 (let* ((fname (car form))
405 (gf (if (symbolp fname)
407 (unencapsulated-fdefinition fname))
409 (gdefinition fname))))
410 (gf-name (and (fsc-instance-p gf)
413 (generic-function-name gf)))))
415 (multiple-value-bind (nreq restp)
416 (get-generic-function-info gf)
417 (optimize-gf-call slots calls form nreq restp env))))
421 (defun can-optimize-access (form required-parameters env)
422 (let ((type (ecase (car form)
424 (set-slot-value 'writer)
425 (slot-boundp 'boundp)))
427 (slot-name (eval (caddr form)))) ; known to be constant
428 (can-optimize-access1 var required-parameters env type slot-name)))
430 ;;; FIXME: This looks like an internal helper function for CAN-OPTIMIZE-ACCESS,
431 ;;; and it is used that way, but
432 ;;; it's also called bare from several places in the code. Perhaps
433 ;;; the two functions should be renamed fo CAN-OPTIMIZE-ACCESS-FOR-FORM
434 ;;; and CAN-OPTIMIZE-ACCESS-FOR-VAR. If so, I'd just as soon use keyword
435 ;;; args instead of optional ones, too.
436 (defun can-optimize-access1 (var required-parameters env
437 &optional type slot-name)
438 (when (and (consp var) (eq 'the (car var)))
439 ;; FIXME: We should assert list of length 3 here. Or maybe we should just
440 ;; define EXTRACT-THE, replace the whole
443 ;; (AWHEN (EXTRACT-THE VAR)
445 ;; and then use EXTRACT-THE similarly to clean up the other tests against
446 ;; 'THE scattered through the PCL code.
447 (setq var (caddr var)))
449 (let* ((rebound? (caddr (variable-declaration 'variable-rebinding
452 (parameter-or-nil (car (memq (or rebound? var)
453 required-parameters))))
454 (when parameter-or-nil
455 (let* ((class-name (caddr (variable-declaration 'class
458 (class (find-class class-name nil)))
459 (when (or (not (eq *boot-state* 'complete))
460 (and class (not (class-finalized-p class))))
462 (when (and class-name (not (eq class-name 't)))
463 (when (or (null type)
465 (memq *the-class-structure-object*
466 (class-precedence-list class))))
467 (optimize-slot-value-by-class-p class slot-name type))
468 (cons parameter-or-nil (or class class-name)))))))))
470 (defun optimize-slot-value (slots sparameter form)
472 (destructuring-bind (ignore1 ignore2 slot-name-form) form
473 (declare (ignore ignore1 ignore2))
474 (let ((slot-name (eval slot-name-form)))
475 (optimize-instance-access slots :read sparameter slot-name nil)))
476 `(accessor-slot-value ,@(cdr form))))
478 (defun optimize-set-slot-value (slots sparameter form)
480 (destructuring-bind (ignore1 ignore2 slot-name-form new-value) form
481 (declare (ignore ignore1 ignore2))
482 (let ((slot-name (eval slot-name-form)))
483 (optimize-instance-access slots
488 `(accessor-set-slot-value ,@(cdr form))))
490 (defun optimize-slot-boundp (slots sparameter form)
493 ;; FIXME: In CMU CL ca. 19991205, this binding list had a fourth
494 ;; element in it, NEW-VALUE. It's hard to see how that could possibly
495 ;; be right, since SLOT-BOUNDP has no NEW-VALUE. Since it was causing
496 ;; a failure in building PCL for SBCL, so I changed it to match the
497 ;; definition of SLOT-BOUNDP (and also to match the list used in the
498 ;; similar OPTIMIZE-SLOT-VALUE, above). However, I'm weirded out by
499 ;; this, since this is old code which has worked for ages to build
500 ;; PCL for CMU CL, so it's hard to see why it should need a patch
501 ;; like this in order to build PCL for SBCL. I'd like to return to
502 ;; this and find a test case which exercises this function both in
503 ;; CMU CL, to see whether it's really a previously-unexercised bug or
504 ;; whether I've misunderstood something (and, presumably, patched it
506 (slot-boundp-symbol instance slot-name-form)
508 (declare (ignore slot-boundp-symbol instance))
509 (let ((slot-name (eval slot-name-form)))
510 (optimize-instance-access slots
515 `(accessor-slot-boundp ,@(cdr form))))
517 (defun optimize-reader (slots sparameter gf-name form)
519 (optimize-accessor-call slots :read sparameter gf-name nil)
522 (defun optimize-writer (slots sparameter gf-name form)
524 (destructuring-bind (ignore1 ignore2 new-value) form
525 (declare (ignore ignore1 ignore2))
526 (optimize-accessor-call slots :write sparameter gf-name new-value))
529 ;;; The SLOTS argument is an alist, the CAR of each entry is the name of
530 ;;; a required parameter to the function. The alist is in order, so the
531 ;;; position of an entry in the alist corresponds to the argument's position
532 ;;; in the lambda list.
533 (defun optimize-instance-access (slots
538 (let ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
539 (parameter (if (consp sparameter) (car sparameter) sparameter)))
540 (if (and (eq *boot-state* 'complete)
542 (memq *the-class-structure-object* (class-precedence-list class)))
543 (let ((slotd (find-slot-definition class slot-name)))
546 `(,(slot-definition-defstruct-accessor-symbol slotd) ,parameter))
548 `(setf (,(slot-definition-defstruct-accessor-symbol slotd)
553 (let* ((parameter-entry (assq parameter slots))
554 (slot-entry (assq slot-name (cdr parameter-entry)))
555 (position (posq parameter-entry slots))
556 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
557 (unless parameter-entry
558 (error "internal error in slot optimization"))
560 (setq slot-entry (list slot-name))
561 (push slot-entry (cdr parameter-entry)))
562 (push pv-offset-form (cdr slot-entry))
565 `(instance-read ,pv-offset-form ,parameter ,position
566 ',slot-name ',class))
568 `(let ((.new-value. ,new-value))
569 (instance-write ,pv-offset-form ,parameter ,position
570 ',slot-name ',class .new-value.)))
572 `(instance-boundp ,pv-offset-form ,parameter ,position
573 ',slot-name ',class)))))))
575 (defun optimize-accessor-call (slots read/write sparameter gf-name new-value)
576 (let* ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
577 (parameter (if (consp sparameter) (car sparameter) sparameter))
578 (parameter-entry (assq parameter slots))
579 (name (case read/write
580 (:read `(reader ,gf-name))
581 (:write `(writer ,gf-name))))
582 (slot-entry (assoc name (cdr parameter-entry) :test #'equal))
583 (position (posq parameter-entry slots))
584 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
585 (unless parameter-entry
586 (error "internal error in slot optimization"))
588 (setq slot-entry (list name))
589 (push slot-entry (cdr parameter-entry)))
590 (push pv-offset-form (cdr slot-entry))
593 `(instance-reader ,pv-offset-form ,parameter ,position ,gf-name ',class))
595 `(let ((.new-value. ,new-value))
596 (instance-writer ,pv-offset-form ,parameter ,position ,gf-name ',class
599 (defvar *unspecific-arg* '..unspecific-arg..)
601 (defun optimize-gf-call-internal (form slots env)
602 (when (and (consp form)
603 (eq (car form) 'the))
604 (setq form (caddr form)))
605 (or (and (symbolp form)
606 (let* ((rebound? (caddr (variable-declaration 'variable-rebinding
608 (parameter-or-nil (car (assq (or rebound? form) slots))))
609 (when parameter-or-nil
610 (let* ((class-name (caddr (variable-declaration
611 'class parameter-or-nil env))))
612 (when (and class-name (not (eq class-name 't)))
613 (position parameter-or-nil slots :key #'car))))))
615 (let ((form (eval form)))
621 (defun optimize-gf-call (slots calls gf-call-form nreq restp env)
622 (unless (eq (car gf-call-form) 'make-instance) ; needs more work
623 (let* ((args (cdr gf-call-form))
624 (all-args-p (eq (car gf-call-form) 'make-instance))
625 (non-required-args (nthcdr nreq args))
626 (required-args (ldiff args non-required-args))
627 (call-spec (list (car gf-call-form) nreq restp
628 (mapcar #'(lambda (form)
629 (optimize-gf-call-internal form slots env))
633 (call-entry (assoc call-spec calls :test #'equal))
634 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
635 (unless (some #'integerp
636 (let ((spec-args (cdr call-spec)))
638 (ldiff spec-args (nthcdr nreq spec-args))
640 (return-from optimize-gf-call nil))
642 (setq call-entry (list call-spec))
643 (push call-entry (cdr calls)))
644 (push pv-offset-form (cdr call-entry))
645 (if (eq (car call-spec) 'make-instance)
646 `(funcall (pv-ref .pv. ,pv-offset-form) ,@(cdr gf-call-form))
647 `(let ((.emf. (pv-ref .pv. ,pv-offset-form)))
648 (invoke-effective-method-function .emf. ,restp
649 ,@required-args ,@(when restp `((list ,@non-required-args)))))))))
651 (define-walker-template pv-offset) ; These forms get munged by mutate slots.
652 (defmacro pv-offset (arg) arg)
653 (define-walker-template instance-accessor-parameter)
654 (defmacro instance-accessor-parameter (x) x)
656 ;; It is safe for these two functions to be wrong.
657 ;; They just try to guess what the most likely case will be.
658 (defun generate-fast-class-slot-access-p (class-form slot-name-form)
659 (let ((class (and (constantp class-form) (eval class-form)))
660 (slot-name (and (constantp slot-name-form) (eval slot-name-form))))
661 (and (eq *boot-state* 'complete)
662 (standard-class-p class)
663 (not (eq class *the-class-t*)) ; shouldn't happen, though.
664 (let ((slotd (find-slot-definition class slot-name)))
665 (and slotd (classp (slot-definition-allocation slotd)))))))
667 (defun skip-fast-slot-access-p (class-form slot-name-form type)
668 (let ((class (and (constantp class-form) (eval class-form)))
669 (slot-name (and (constantp slot-name-form) (eval slot-name-form))))
670 (and (eq *boot-state* 'complete)
671 (standard-class-p class)
672 (not (eq class *the-class-t*)) ; shouldn't happen, though.
673 (let ((slotd (find-slot-definition class slot-name)))
674 (and slotd (skip-optimize-slot-value-by-class-p class slot-name type))))))
676 (defun skip-optimize-slot-value-by-class-p (class slot-name type)
677 (let ((slotd (find-slot-definition class slot-name)))
679 (eq *boot-state* 'complete)
680 (not (slot-accessor-std-p slotd type)))))
682 (defmacro instance-read-internal (pv slots pv-offset default &optional type)
683 (unless (member type '(nil :instance :class :default))
684 (error "illegal type argument to ~S: ~S" 'instance-read-internal type))
685 (if (eq type ':default)
687 (let* ((index (gensym))
689 `(locally (declare #.*optimize-speed*)
690 (let ((,index (pvref ,pv ,pv-offset)))
691 (setq ,value (typecase ,index
692 ,@(when (or (null type) (eq type ':instance))
693 `((fixnum (%instance-ref ,slots ,index))))
694 ,@(when (or (null type) (eq type ':class))
695 `((cons (cdr ,index))))
696 (t ',*slot-unbound*)))
697 (if (eq ,value ',*slot-unbound*)
701 (defmacro instance-read (pv-offset parameter position slot-name class)
702 (if (skip-fast-slot-access-p class slot-name 'reader)
703 `(accessor-slot-value ,parameter ,slot-name)
704 `(instance-read-internal .pv. ,(slot-vector-symbol position)
705 ,pv-offset (accessor-slot-value ,parameter ,slot-name)
706 ,(if (generate-fast-class-slot-access-p class slot-name)
707 ':class ':instance))))
709 (defmacro instance-reader (pv-offset parameter position gf-name class)
710 (declare (ignore class))
711 `(instance-read-internal .pv. ,(slot-vector-symbol position)
713 (,gf-name (instance-accessor-parameter ,parameter))
716 (defmacro instance-write-internal (pv slots pv-offset new-value default
718 (unless (member type '(nil :instance :class :default))
719 (error "illegal type argument to ~S: ~S" 'instance-write-internal type))
720 (if (eq type ':default)
722 (let* ((index (gensym)))
723 `(locally (declare #.*optimize-speed*)
724 (let ((,index (pvref ,pv ,pv-offset)))
726 ,@(when (or (null type) (eq type ':instance))
727 `((fixnum (setf (%instance-ref ,slots ,index) ,new-value))))
728 ,@(when (or (null type) (eq type ':class))
729 `((cons (setf (cdr ,index) ,new-value))))
732 (defmacro instance-write (pv-offset
738 (if (skip-fast-slot-access-p class slot-name 'writer)
739 `(accessor-set-slot-value ,parameter ,slot-name ,new-value)
740 `(instance-write-internal .pv. ,(slot-vector-symbol position)
741 ,pv-offset ,new-value
742 (accessor-set-slot-value ,parameter ,slot-name ,new-value)
743 ,(if (generate-fast-class-slot-access-p class slot-name)
744 ':class ':instance))))
746 (defmacro instance-writer (pv-offset
752 (declare (ignore class))
753 `(instance-write-internal .pv. ,(slot-vector-symbol position)
754 ,pv-offset ,new-value
755 (,(if (consp gf-name)
756 (get-setf-function-name gf-name)
758 (instance-accessor-parameter ,parameter)
762 (defmacro instance-boundp-internal (pv slots pv-offset default
764 (unless (member type '(nil :instance :class :default))
765 (error "illegal type argument to ~S: ~S" 'instance-boundp-internal type))
766 (if (eq type ':default)
768 (let* ((index (gensym)))
769 `(locally (declare #.*optimize-speed*)
770 (let ((,index (pvref ,pv ,pv-offset)))
772 ,@(when (or (null type) (eq type ':instance))
773 `((fixnum (not (eq (%instance-ref ,slots ,index)
774 ',*slot-unbound*)))))
775 ,@(when (or (null type) (eq type ':class))
776 `((cons (not (eq (cdr ,index) ',*slot-unbound*)))))
779 (defmacro instance-boundp (pv-offset parameter position slot-name class)
780 (if (skip-fast-slot-access-p class slot-name 'boundp)
781 `(accessor-slot-boundp ,parameter ,slot-name)
782 `(instance-boundp-internal .pv. ,(slot-vector-symbol position)
783 ,pv-offset (accessor-slot-boundp ,parameter ,slot-name)
784 ,(if (generate-fast-class-slot-access-p class slot-name)
785 ':class ':instance))))
787 ;;; This magic function has quite a job to do indeed.
789 ;;; The careful reader will recall that <slots> contains all of the optimized
790 ;;; slot access forms produced by OPTIMIZE-INSTANCE-ACCESS. Each of these is
791 ;;; a call to either INSTANCE-READ or INSTANCE-WRITE.
793 ;;; At the time these calls were produced, the first argument was specified as
794 ;;; the symbol .PV-OFFSET.; what we have to do now is convert those pv-offset
795 ;;; arguments into the actual number that is the correct offset into the pv.
797 ;;; But first, oh but first, we sort <slots> a bit so that for each argument we
798 ;;; have the slots in alphabetical order. This canonicalizes the PV-TABLE's a
799 ;;; bit and will hopefully lead to having fewer PV's floating around. Even if
800 ;;; the gain is only modest, it costs nothing.
801 (defun slot-name-lists-from-slots (slots calls)
802 (multiple-value-bind (slots calls) (mutate-slots-and-calls slots calls)
803 (let* ((slot-name-lists
804 (mapcar #'(lambda (parameter-entry)
805 (cons nil (mapcar #'car (cdr parameter-entry))))
808 (mapcar #'car calls)))
809 (dolist (call call-list)
810 (dolist (arg (cdr call))
812 (setf (car (nth arg slot-name-lists)) t))))
813 (setq slot-name-lists (mapcar #'(lambda (r+snl)
814 (when (or (car r+snl) (cdr r+snl))
817 (let ((cvt (apply #'vector
819 (mapcar #'(lambda (r+snl)
820 (when r+snl (incf i)))
822 (setq call-list (mapcar #'(lambda (call)
824 (mapcar #'(lambda (arg)
830 (values slot-name-lists call-list))))
832 (defun mutate-slots-and-calls (slots calls)
833 (let ((sorted-slots (sort-slots slots))
834 (sorted-calls (sort-calls (cdr calls)))
835 (pv-offset 0)) ; index 0 is for info
836 (dolist (parameter-entry sorted-slots)
837 (dolist (slot-entry (cdr parameter-entry))
839 (dolist (form (cdr slot-entry))
840 (setf (cadr form) pv-offset))))
841 (dolist (call-entry sorted-calls)
843 (dolist (form (cdr call-entry))
844 (setf (cadr form) pv-offset)))
845 (values sorted-slots sorted-calls)))
847 (defun symbol-pkg-name (sym)
848 (let ((pkg (symbol-package sym)))
849 (if pkg (package-name pkg) "")))
851 ;;; FIXME: Because of the existence of UNINTERN and RENAME-PACKAGE,
852 ;;; the part of this ordering which is based on SYMBOL-PKG-NAME is not
853 ;;; stable. This ordering is only used in to
854 ;;; SLOT-NAME-LISTS-FROM-SLOTS, where it serves to "canonicalize the
855 ;;; PV-TABLE's a bit and will hopefully lead to having fewer PV's
856 ;;; floating around", so it sounds as though the instability won't
857 ;;; actually lead to bugs, just small inefficiency. But still, it
858 ;;; would be better to reimplement this function as a comparison based
860 ;;; * stable comparison
861 ;;; * smaller code (here, and in being able to discard SYMBOL-PKG-NAME)
863 (defun symbol-lessp (a b)
864 (if (eq (symbol-package a)
866 (string-lessp (symbol-name a)
868 (string-lessp (symbol-pkg-name a)
869 (symbol-pkg-name b))))
871 (defun symbol-or-cons-lessp (a b)
874 (symbol (symbol-lessp a b))
878 (cons (if (eq (car a) (car b))
879 (symbol-or-cons-lessp (cdr a) (cdr b))
880 (symbol-or-cons-lessp (car a) (car b))))))))
882 (defun sort-slots (slots)
883 (mapcar #'(lambda (parameter-entry)
884 (cons (car parameter-entry)
885 (sort (cdr parameter-entry) ;slot entries
886 #'symbol-or-cons-lessp
890 (defun sort-calls (calls)
891 (sort calls #'symbol-or-cons-lessp :key #'car))
893 ;;; This needs to work in terms of metatypes and also needs to work for
894 ;;; automatically generated reader and writer functions.
895 ;;; -- Automatically generated reader and writer functions use this stuff too.
897 (defmacro pv-binding ((required-parameters slot-name-lists pv-table-symbol)
899 (with-gathering ((slot-vars (collecting))
900 (pv-parameters (collecting)))
901 (iterate ((slots (list-elements slot-name-lists))
902 (required-parameter (list-elements required-parameters))
903 (i (interval :from 0)))
905 (gather required-parameter pv-parameters)
906 (gather (slot-vector-symbol i) slot-vars)))
907 `(pv-binding1 (.pv. .calls. ,pv-table-symbol ,pv-parameters ,slot-vars)
910 (defmacro pv-binding1 ((pv calls pv-table-symbol pv-parameters slot-vars)
912 `(pv-env (,pv ,calls ,pv-table-symbol ,pv-parameters)
913 (let (,@(mapcar #'(lambda (slot-var p) `(,slot-var (get-slots-or-nil ,p)))
914 slot-vars pv-parameters))
917 ;This gets used only when the default make-method-lambda is overriden.
918 (defmacro pv-env ((pv calls pv-table-symbol pv-parameters)
920 `(let* ((.pv-table. ,pv-table-symbol)
921 (.pv-cell. (pv-table-lookup-pv-args .pv-table. ,@pv-parameters))
922 (,pv (car .pv-cell.))
923 (,calls (cdr .pv-cell.)))
924 (declare ,(make-pv-type-declaration pv))
925 (declare ,(make-calls-type-declaration calls))
926 ,@(when (symbolp pv-table-symbol)
927 `((declare (special ,pv-table-symbol))))
931 (defvar *non-variable-declarations*
932 ;; FIXME: VALUES was in this list, conditionalized with #+CMU, but
933 ;; I don't *think* CMU CL had, or SBCL has, VALUES declarations. If
934 ;; SBCL doesn't have 'em, VALUES should probably be removed from this list.
935 '(values method-name method-lambda-list
936 optimize ftype inline notinline))
938 (defvar *variable-declarations-with-argument*
942 (defvar *variable-declarations-without-argument*
943 '(ignore ignorable special dynamic-extent
944 array atom base-char bignum bit bit-vector character compiled-function
945 complex cons double-float extended-char
946 fixnum float function hash-table integer
947 keyword list long-float nil null number package pathname random-state ratio
948 rational readtable sequence short-float signed-byte simple-array
949 simple-bit-vector simple-string simple-vector single-float standard-char
950 stream string symbol t unsigned-byte vector))
952 (defun split-declarations (body args calls-next-method-p)
953 (let ((inner-decls nil) (outer-decls nil) decl)
954 (loop (when (null body) (return nil))
955 (setq decl (car body))
956 (unless (and (consp decl)
957 (eq (car decl) 'declare))
959 (dolist (form (cdr decl))
961 (let ((declaration-name (car form)))
962 (if (member declaration-name *non-variable-declarations*)
963 (push `(declare ,form) outer-decls)
965 (member declaration-name
966 *variable-declarations-with-argument*))
968 (member declaration-name
969 *variable-declarations-without-argument*))
970 (dname (list (pop form)))
971 (inners nil) (outers nil))
972 (unless (or arg-p non-arg-p)
973 ;; FIXME: This warning should probably go away now
974 ;; that we're not trying to be portable between
975 ;; different CLTL1 hosts the way PCL was.
976 (warn "The declaration ~S is not understood by ~S.~@
977 Please put ~S on one of the lists ~S,~%~S, or~%~S.~@
978 (Assuming it is a variable declaration without argument)."
979 declaration-name 'split-declarations
981 '*non-variable-declarations*
982 '*variable-declarations-with-argument*
983 '*variable-declarations-without-argument*)
984 (push declaration-name
985 *variable-declarations-without-argument*))
987 (setq dname (append dname (list (pop form)))))
989 (if (member var args)
990 ;; Quietly remove IGNORE declarations on args when
991 ;; a next-method is involved, to prevent compiler
992 ;; warns about ignored args being read.
993 (unless (and calls-next-method-p
994 (eq (car dname) 'ignore))
998 (push `(declare (,@dname ,@outers)) outer-decls))
1000 (push `(declare (,@dname ,@inners)) inner-decls)))))))
1001 (setq body (cdr body)))
1002 (values outer-decls inner-decls body)))
1004 (defun make-method-initargs-form-internal (method-lambda initargs env)
1005 (declare (ignore env))
1006 (let (method-lambda-args lmf lmf-params)
1007 (if (not (and (= 3 (length method-lambda))
1008 (= 2 (length (setq method-lambda-args (cadr method-lambda))))
1009 (consp (setq lmf (third method-lambda)))
1010 (eq 'simple-lexical-method-functions (car lmf))
1011 (eq (car method-lambda-args)
1012 (cadr (setq lmf-params (cadr lmf))))
1013 (eq (cadr method-lambda-args)
1014 (caddr lmf-params))))
1015 `(list* :function #',method-lambda
1017 (let* ((lambda-list (car lmf-params))
1018 (nreq 0)(restp nil)(args nil))
1019 (dolist (arg lambda-list)
1020 (when (member arg '(&optional &rest &key))
1021 (setq restp t)(return nil))
1022 (when (eq arg '&aux) (return nil))
1023 (incf nreq)(push arg args))
1024 (setq args (nreverse args))
1025 (setf (getf (getf initargs ':plist) ':arg-info) (cons nreq restp))
1026 (make-method-initargs-form-internal1
1027 initargs (cddr lmf) args lmf-params restp)))))
1029 (defun make-method-initargs-form-internal1
1030 (initargs body req-args lmf-params restp)
1031 (multiple-value-bind (outer-decls inner-decls body)
1033 body req-args (getf (cdr lmf-params) :call-next-method-p))
1034 (let* ((rest-arg (when restp '.rest-arg.))
1035 (args+rest-arg (if restp
1036 (append req-args (list rest-arg))
1038 `(list* :fast-function
1039 #'(lambda (.pv-cell. .next-method-call. ,@args+rest-arg)
1041 .pv-cell. .next-method-call.
1042 (macrolet ((pv-env ((pv calls pv-table-symbol pv-parameters)
1044 (declare (ignore pv-table-symbol pv-parameters))
1045 `(let ((,pv (car .pv-cell.))
1046 (,calls (cdr .pv-cell.)))
1047 (declare ,(make-pv-type-declaration pv)
1048 ,(make-calls-type-declaration calls))
1051 (fast-lexical-method-functions
1052 (,(car lmf-params) .next-method-call. ,req-args ,rest-arg
1053 ,@(cdddr lmf-params))
1058 ;;; Use arrays and hash tables and the fngen stuff to make this much better. It
1059 ;;; doesn't really matter, though, because a function returned by this will get
1060 ;;; called only when the user explicitly funcalls a result of method-function.
1061 ;;; BUT, this is needed to make early methods work.
1062 (defun method-function-from-fast-function (fmf)
1063 (declare (type function fmf))
1064 (let* ((method-function nil) (pv-table nil)
1065 (arg-info (method-function-get fmf ':arg-info))
1066 (nreq (car arg-info))
1067 (restp (cdr arg-info)))
1068 (setq method-function
1069 #'(lambda (method-args next-methods)
1071 (setq pv-table (method-function-pv-table fmf)))
1072 (let* ((pv-cell (when pv-table
1073 (get-method-function-pv-cell
1074 method-function method-args pv-table)))
1075 (nm (car next-methods))
1076 (nms (cdr next-methods))
1078 (make-method-call :function (if (std-instance-p nm)
1079 (method-function nm)
1081 :call-method-args (list nms)))))
1083 (let* ((rest (nthcdr nreq method-args))
1084 (args (ldiff method-args rest)))
1085 (apply fmf pv-cell nmc (nconc args (list rest))))
1086 (apply fmf pv-cell nmc method-args)))))
1087 (let* ((fname (method-function-get fmf :name))
1088 (name `(,(or (get (car fname) 'method-sym)
1089 (setf (get (car fname) 'method-sym)
1090 (let ((str (symbol-name (car fname))))
1091 (if (string= "FAST-" str :end2 5)
1092 (intern (subseq str 5) *pcl-package*)
1095 (set-function-name method-function name))
1096 (setf (method-function-get method-function :fast-function) fmf)
1099 (defun get-method-function-pv-cell (method-function
1102 (let ((pv-table (or pv-table (method-function-pv-table method-function))))
1104 (let ((pv-wrappers (pv-wrappers-from-all-args pv-table method-args)))
1106 (pv-table-lookup pv-table pv-wrappers))))))
1108 (defun pv-table-lookup-pv-args (pv-table &rest pv-parameters)
1109 (pv-table-lookup pv-table (pv-wrappers-from-pv-args pv-parameters)))
1111 (defun pv-wrappers-from-pv-args (&rest args)
1112 (let* ((nkeys (length args))
1113 (pv-wrappers (make-list nkeys))
1117 (setq w (wrapper-of arg))
1118 (unless (eq 't (wrapper-state w)) ; FIXME: should be INVALID-WRAPPER-P
1119 (setq w (check-wrapper-validity arg)))
1121 (setq w-t (cdr w-t))
1122 (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))
1125 (defun pv-wrappers-from-all-args (pv-table args)
1127 (slot-name-lists (pv-table-slot-name-lists pv-table)))
1128 (dolist (sn slot-name-lists)
1129 (when sn (incf nkeys)))
1130 (let* ((pv-wrappers (make-list nkeys))
1131 (pv-w-t pv-wrappers))
1132 (dolist (sn slot-name-lists)
1134 (let* ((arg (car args))
1135 (w (wrapper-of arg)))
1136 (unless w ; CAN-OPTIMIZE-ACCESS prevents this from happening.
1137 (error "error in PV-WRAPPERS-FROM-ALL-ARGS"))
1138 (setf (car pv-w-t) w)
1139 (setq pv-w-t (cdr pv-w-t))))
1140 (setq args (cdr args)))
1141 (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))
1144 (defun pv-wrappers-from-all-wrappers (pv-table wrappers)
1146 (slot-name-lists (pv-table-slot-name-lists pv-table)))
1147 (dolist (sn slot-name-lists)
1148 (when sn (incf nkeys)))
1149 (let* ((pv-wrappers (make-list nkeys))
1150 (pv-w-t pv-wrappers))
1151 (dolist (sn slot-name-lists)
1153 (let ((w (car wrappers)))
1154 (unless w ; CAN-OPTIMIZE-ACCESS prevents this from happening.
1155 (error "error in PV-WRAPPERS-FROM-ALL-WRAPPERS"))
1156 (setf (car pv-w-t) w)
1157 (setq pv-w-t (cdr pv-w-t))))
1158 (setq wrappers (cdr wrappers)))
1159 (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))