ef53775fb3bb0f510b8920ca494b38f2545ba3c5
[sbcl.git] / src / pcl / vector.lisp
1 ;;;; permutation vectors
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5
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.
11
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.
25
26 (in-package "SB-PCL")
27 \f
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)))))
35 \f
36 (defun pv-cache-limit-fn (nlines)
37   (default-limit-fn nlines))
38
39 (defstruct (pv-table (:predicate pv-tablep)
40                      (:constructor make-pv-table-internal
41                                    (slot-name-lists call-list))
42                      (:copier nil))
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))
47
48 #-sb-fluid (declaim (sb-ext:freeze-type pv-table))
49
50 (defvar *initial-pv-table* (make-pv-table-internal nil nil))
51
52 ; help new slot-value-using-class methods affect fast iv access
53 (defvar *all-pv-table-list* nil)
54
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*)
58     pv-table))
59
60 (defun make-pv-table-type-declaration (var)
61   `(type pv-table ,var))
62
63 (defvar *slot-name-lists-inner* (make-hash-table :test 'equal))
64 (defvar *slot-name-lists-outer* (make-hash-table :test 'equal))
65
66 ;;; Entries in this are lists of (table . pv-offset-list).
67 (defvar *pv-key-to-pv-table-table* (make-hash-table :test 'equal))
68
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 (outer (mapcar #'inner (cons call-list slot-name-lists)))))
83       (when new-p
84         (let ((pv-index 1))
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)
88               (incf pv-index)))
89           (dolist (gf-call call-list)
90             (note-pv-table-reference gf-call pv-index pv-table)
91             (incf pv-index))
92           (setf (pv-table-pv-size pv-table) pv-index)))
93       pv-table))))
94
95 (defun note-pv-table-reference (ref pv-offset pv-table)
96   (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
97     (when (listp entry)
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)
104                     (cdr table-entry)))
105             (setf (gethash ref *pv-key-to-pv-table-table*) new-table-table)))
106         (when (listp entry)
107           (if (null table-entry)
108               (let ((new (cons pv-table pv-offset)))
109                 (if (consp entry)
110                     (push new (cdr entry))
111                     (setf (gethash ref *pv-key-to-pv-table-table*)
112                           (list new))))
113               (push pv-offset (cdr table-entry)))
114           (return-from note-pv-table-reference nil))))
115     (let ((list (gethash pv-table entry)))
116       (if (consp list)
117           (push pv-offset (cdr list))
118           (setf (gethash pv-table entry) (list pv-offset)))))
119   nil)
120
121 (defun map-pv-table-references-of (ref function)
122   (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
123     (if (listp entry)
124         (dolist (table+pv-offset-list entry)
125           (funcall function
126                    (car table+pv-offset-list)
127                    (cdr table+pv-offset-list)))
128         (maphash function entry)))
129   ref)
130 \f
131 (defvar *pvs* (make-hash-table :test 'equal))
132
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)))
136         (and slotd
137              (slot-accessor-std-p slotd type)))))
138
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)))
144               (when cell
145                 (setf (car class-slot-p-cell) t)
146                 cell))))
147       (when (consp slot-name)
148         (dolist (type '(reader writer) nil)
149           (when (eq (car slot-name) type)
150             (return
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))
157                 location)))))))
158
159 (defun compute-pv (slot-name-lists wrappers)
160   (unless (listp wrappers) (setq wrappers (list wrappers)))
161   (let* ((not-simple-p-cell (list nil))
162          (elements
163           (let ((elements nil))
164             (dolist (slot-names slot-name-lists)
165               (when slot-names
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))
171                     ;; Original PCL code had this idiom.  why not:
172                     ;;
173                     ;; (WHEN STD-P
174                     ;;   (PUSH ...)) ?
175                     (push (when std-p
176                             (compute-pv-slot slot-name wrapper class
177                                              class-slots not-simple-p-cell))
178                           elements)))))
179             (nreverse elements))))
180     (if (car not-simple-p-cell)
181         (make-permutation-vector (cons t elements))
182         (or (gethash elements *pvs*)
183             (setf (gethash elements *pvs*)
184                   (make-permutation-vector (cons nil elements)))))))
185
186 (defun compute-calls (call-list wrappers)
187   (declare (ignore call-list wrappers))
188   #||
189   (map 'vector
190        (lambda (call)
191          (compute-emf-from-wrappers call wrappers))
192        call-list)
193   ||#
194   '#())
195
196 #|| ; Need to finish this, then write the maintenance functions.
197 (defun compute-emf-from-wrappers (call wrappers)
198   (when call
199     (destructuring-bind (gf-name nreq restp arg-info) call
200       (if (eq gf-name 'make-instance)
201           (error "should not get here") ; there is another mechanism for this.
202           (lambda (&rest args)
203             (if (not (eq *boot-state* 'complete))
204                 (apply (gdefinition gf-name) args)
205                 (let* ((gf (gdefinition gf-name))
206                        (arg-info (arg-info-reader gf))
207                        (classes '?)
208                        (types '?)
209                        (emf (cache-miss-values-internal gf arg-info
210                                                         wrappers classes types
211                                                         'caching)))
212                   (update-all-pv-tables call wrappers emf)
213                   (invoke-emf emf args))))))))
214 ||#
215
216 (defun make-permutation-vector (indexes)
217   (make-array (length indexes) :initial-contents indexes))
218
219 (defun pv-table-lookup (pv-table pv-wrappers)
220   (let* ((slot-name-lists (pv-table-slot-name-lists pv-table))
221          (call-list (pv-table-call-list pv-table))
222          (cache (or (pv-table-cache pv-table)
223                     (setf (pv-table-cache pv-table)
224                           (get-cache (- (length slot-name-lists)
225                                         (count nil slot-name-lists))
226                                      t
227                                      #'pv-cache-limit-fn
228                                      2)))))
229     (or (probe-cache cache pv-wrappers)
230         (let* ((pv (compute-pv slot-name-lists pv-wrappers))
231                (calls (compute-calls call-list pv-wrappers))
232                (pv-cell (cons pv calls))
233                (new-cache (fill-cache cache pv-wrappers pv-cell)))
234           (unless (eq new-cache cache)
235             (setf (pv-table-cache pv-table) new-cache)
236             (free-cache cache))
237           pv-cell))))
238
239 (defun make-pv-type-declaration (var)
240   `(type simple-vector ,var))
241
242 (defvar *empty-pv* #())
243
244 (defmacro pvref (pv index)
245   `(svref ,pv ,index))
246
247 (defmacro copy-pv (pv)
248   `(copy-seq ,pv))
249
250 (defun make-calls-type-declaration (var)
251   `(type simple-vector ,var))
252
253 (defmacro callsref (calls index)
254   `(svref ,calls ,index))
255
256 (defvar *pv-table-cache-update-info* nil)
257
258 (defun update-pv-table-cache-info (class)
259   (let ((slot-names-for-pv-table-update nil)
260         (new-icui nil))
261     (dolist (icu *pv-table-cache-update-info*)
262       (if (eq (car icu) class)
263           (pushnew (cdr icu) slot-names-for-pv-table-update)
264           (push icu new-icui)))
265     (setq *pv-table-cache-update-info* new-icui)
266     (when slot-names-for-pv-table-update
267       (update-all-pv-table-caches class slot-names-for-pv-table-update))))
268
269 (defun update-all-pv-table-caches (class slot-names)
270   (let* ((cwrapper (class-wrapper class))
271          (std-p (typep cwrapper 'wrapper))
272          (class-slots (and std-p (wrapper-class-slots cwrapper)))
273          (class-slot-p-cell (list nil))
274          (new-values (mapcar (lambda (slot-name)
275                                (cons slot-name
276                                      (when std-p
277                                        (compute-pv-slot
278                                         slot-name cwrapper class
279                                         class-slots class-slot-p-cell))))
280                              slot-names))
281          (pv-tables nil))
282     (dolist (slot-name slot-names)
283       (map-pv-table-references-of
284        slot-name
285        (lambda (pv-table pv-offset-list)
286          (declare (ignore pv-offset-list))
287          (pushnew pv-table pv-tables))))
288     (dolist (pv-table pv-tables)
289       (let* ((cache (pv-table-cache pv-table))
290              (slot-name-lists (pv-table-slot-name-lists pv-table))
291              (pv-size (pv-table-pv-size pv-table))
292              (pv-map (make-array pv-size :initial-element nil)))
293         (let ((map-index 1)(param-index 0))
294           (dolist (slot-name-list slot-name-lists)
295             (dolist (slot-name (cdr slot-name-list))
296               (let ((a (assoc slot-name new-values)))
297                 (setf (svref pv-map map-index)
298                       (and a (cons param-index (cdr a)))))
299               (incf map-index))
300             (incf param-index)))
301         (when cache
302           (map-cache (lambda (wrappers pv-cell)
303                        (setf (car pv-cell)
304                              (update-slots-in-pv wrappers (car pv-cell)
305                                                  cwrapper pv-size pv-map)))
306                      cache))))))
307
308 (defun update-slots-in-pv (wrappers pv cwrapper pv-size pv-map)
309   (if (not (if (atom wrappers)
310                (eq cwrapper wrappers)
311                (dolist (wrapper wrappers nil)
312                  (when (eq wrapper cwrapper)
313                    (return t)))))
314       pv
315       (let* ((old-intern-p (listp (pvref pv 0)))
316              (new-pv (if old-intern-p
317                          (copy-pv pv)
318                          pv))
319              (new-intern-p t))
320         (if (atom wrappers)
321             (dotimes-fixnum (i pv-size)
322               (when (consp (let ((map (svref pv-map i)))
323                              (if map
324                                  (setf (pvref new-pv i) (cdr map))
325                                  (pvref new-pv i))))
326                 (setq new-intern-p nil)))
327             (let ((param 0))
328               (dolist (wrapper wrappers)
329                 (when (eq wrapper cwrapper)
330                   (dotimes-fixnum (i pv-size)
331                     (when (consp (let ((map (svref pv-map i)))
332                                    (if (and map (= (car map) param))
333                                        (setf (pvref new-pv i) (cdr map))
334                                        (pvref new-pv i))))
335                       (setq new-intern-p nil))))
336                 (incf param))))
337         (when new-intern-p
338           (setq new-pv (let ((list-pv (coerce pv 'list)))
339                          (or (gethash (cdr list-pv) *pvs*)
340                              (setf (gethash (cdr list-pv) *pvs*)
341                                    (if old-intern-p
342                                        new-pv
343                                        (make-permutation-vector list-pv)))))))
344         new-pv)))
345 \f
346 (defun maybe-expand-accessor-form (form required-parameters slots env)
347   (let* ((fname (car form))
348          #||(len (length form))||#
349          (gf (if (symbolp fname)
350                  (unencapsulated-fdefinition fname)
351                  (gdefinition fname))))
352     (macrolet ((maybe-optimize-reader ()
353                  `(let ((parameter
354                          (can-optimize-access1 (cadr form)
355                                                required-parameters env)))
356                    (when parameter
357                      (optimize-reader slots parameter gf-name form))))
358                (maybe-optimize-writer ()
359                  `(let ((parameter
360                          (can-optimize-access1 (caddr form)
361                                                required-parameters env)))
362                    (when parameter
363                      (optimize-writer slots parameter gf-name form)))))
364       (unless (and (consp (cadr form))
365                    (eq 'instance-accessor-parameter (caadr form)))
366         (when (and (eq *boot-state* 'complete)
367                    (generic-function-p gf))
368           (let ((methods (generic-function-methods gf)))
369             (when methods
370               (let* ((gf-name (generic-function-name gf))
371                      (arg-info (gf-arg-info gf))
372                      (metatypes (arg-info-metatypes arg-info))
373                      (nreq (length metatypes))
374                      (applyp (arg-info-applyp arg-info)))
375                 (when (null applyp)
376                   (cond ((= nreq 1)
377                          (when (some #'standard-reader-method-p methods)
378                            (maybe-optimize-reader)))
379                         ((and (= nreq 2)
380                               (consp gf-name)
381                               (eq (car gf-name) 'setf))
382                          (when (some #'standard-writer-method-p methods)
383                            (maybe-optimize-writer)))))))))))))
384
385 (defun optimize-generic-function-call (form
386                                        required-parameters
387                                        env
388                                        slots
389                                        calls)
390   (declare (ignore required-parameters env slots calls))
391   (or (and (eq (car form) 'make-instance)
392            (expand-make-instance-form form))
393       form))
394 \f
395 (defun can-optimize-access (form required-parameters env)
396   (let ((type (ecase (car form)
397                 (slot-value 'reader)
398                 (set-slot-value 'writer)
399                 (slot-boundp 'boundp)))
400         (var (cadr form))
401         (slot-name (eval (caddr form)))) ; known to be constant
402     (can-optimize-access1 var required-parameters env type slot-name)))
403
404 ;;; FIXME: This looks like an internal helper function for
405 ;;; CAN-OPTIMIZE-ACCESS, and it is used that way, but it's also called
406 ;;; bare from several places in the code. Perhaps the two functions
407 ;;; should be renamed CAN-OPTIMIZE-ACCESS-FOR-FORM and
408 ;;; CAN-OPTIMIZE-ACCESS-FOR-VAR. If so, I'd just as soon use keyword
409 ;;; args instead of optional ones, too.
410 (defun can-optimize-access1 (var required-parameters env
411                              &optional type slot-name)
412   (when (and (consp var) (eq 'the (car var)))
413     ;; FIXME: We should assert list of length 3 here. Or maybe we
414     ;; should just define EXTRACT-THE, replace the whole
415     ;;   (WHEN ..)
416     ;; form with
417     ;;   (AWHEN (EXTRACT-THE VAR)
418     ;;     (SETF VAR IT))
419     ;; and then use EXTRACT-THE similarly to clean up the other tests
420     ;; against 'THE scattered through the PCL code.
421     (setq var (caddr var)))
422   (when (symbolp var)
423     (let* ((rebound? (caddr (var-declaration '%variable-rebinding var env)))
424            (parameter-or-nil (car (memq (or rebound? var)
425                                         required-parameters))))
426       (when parameter-or-nil
427         (let* ((class-name (caddr (var-declaration '%class
428                                                    parameter-or-nil
429                                                    env)))
430                (class (find-class class-name nil)))
431           (when (or (not (eq *boot-state* 'complete))
432                     (and class (not (class-finalized-p class))))
433             (setq class nil))
434           (when (and class-name (not (eq class-name t)))
435             (when (or (null type)
436                       (not (and class
437                                 (memq *the-class-structure-object*
438                                       (class-precedence-list class))))
439                       (optimize-slot-value-by-class-p class slot-name type))
440               (cons parameter-or-nil (or class class-name)))))))))
441
442 (defun optimize-slot-value (slots sparameter form)
443   (if sparameter
444       (destructuring-bind (ignore1 ignore2 slot-name-form) form
445         (declare (ignore ignore1 ignore2))
446         (let ((slot-name (eval slot-name-form)))
447           (optimize-instance-access slots :read sparameter slot-name nil)))
448       `(accessor-slot-value ,@(cdr form))))
449
450 (defun optimize-set-slot-value (slots sparameter form)
451   (if sparameter
452       (destructuring-bind (ignore1 ignore2 slot-name-form new-value) form
453         (declare (ignore ignore1 ignore2))
454         (let ((slot-name (eval slot-name-form)))
455           (optimize-instance-access slots
456                                     :write
457                                     sparameter
458                                     slot-name
459                                     new-value)))
460       `(accessor-set-slot-value ,@(cdr form))))
461
462 (defun optimize-slot-boundp (slots sparameter form)
463   (if sparameter
464       (destructuring-bind
465           ;; FIXME: In CMU CL ca. 19991205, this binding list had a
466           ;; fourth element in it, NEW-VALUE. It's hard to see how
467           ;; that could possibly be right, since SLOT-BOUNDP has no
468           ;; NEW-VALUE. Since it was causing a failure in building PCL
469           ;; for SBCL, so I changed it to match the definition of
470           ;; SLOT-BOUNDP (and also to match the list used in the
471           ;; similar OPTIMIZE-SLOT-VALUE, above). However, I'm weirded
472           ;; out by this, since this is old code which has worked for
473           ;; ages to build PCL for CMU CL, so it's hard to see why it
474           ;; should need a patch like this in order to build PCL for
475           ;; SBCL. I'd like to return to this and find a test case
476           ;; which exercises this function both in CMU CL, to see
477           ;; whether it's really a previously-unexercised bug or
478           ;; whether I've misunderstood something (and, presumably,
479           ;; patched it wrong).
480           (slot-boundp-symbol instance slot-name-form)
481           form
482         (declare (ignore slot-boundp-symbol instance))
483         (let ((slot-name (eval slot-name-form)))
484           (optimize-instance-access slots
485                                     :boundp
486                                     sparameter
487                                     slot-name
488                                     nil)))
489       `(accessor-slot-boundp ,@(cdr form))))
490
491 (defun optimize-reader (slots sparameter gf-name form)
492   (if sparameter
493       (optimize-accessor-call slots :read sparameter gf-name nil)
494       form))
495
496 (defun optimize-writer (slots sparameter gf-name form)
497   (if sparameter
498       (destructuring-bind (ignore1 ignore2 new-value) form
499         (declare (ignore ignore1 ignore2))
500         (optimize-accessor-call slots :write sparameter gf-name new-value))
501       form))
502
503 ;;; The SLOTS argument is an alist, the CAR of each entry is the name
504 ;;; of a required parameter to the function. The alist is in order, so
505 ;;; the position of an entry in the alist corresponds to the
506 ;;; argument's position in the lambda list.
507 (defun optimize-instance-access (slots
508                                  read/write
509                                  sparameter
510                                  slot-name
511                                  new-value)
512   (let ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
513         (parameter (if (consp sparameter) (car sparameter) sparameter)))
514     (if (and (eq *boot-state* 'complete)
515              (classp class)
516              (memq *the-class-structure-object* (class-precedence-list class)))
517         (let ((slotd (find-slot-definition class slot-name)))
518           (ecase read/write
519             (:read
520              `(,(slot-definition-defstruct-accessor-symbol slotd) ,parameter))
521             (:write
522              `(setf (,(slot-definition-defstruct-accessor-symbol slotd)
523                      ,parameter)
524                     ,new-value))
525             (:boundp
526              t)))
527         (let* ((parameter-entry (assq parameter slots))
528                (slot-entry      (assq slot-name (cdr parameter-entry)))
529                (position (posq parameter-entry slots))
530                (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
531           (unless parameter-entry
532             (error "internal error in slot optimization"))
533           (unless slot-entry
534             (setq slot-entry (list slot-name))
535             (push slot-entry (cdr parameter-entry)))
536           (push pv-offset-form (cdr slot-entry))
537           (ecase read/write
538             (:read
539              `(instance-read ,pv-offset-form ,parameter ,position
540                              ',slot-name ',class))
541             (:write
542              `(let ((.new-value. ,new-value))
543                 (instance-write ,pv-offset-form ,parameter ,position
544                                 ',slot-name ',class .new-value.)))
545             (:boundp
546              `(instance-boundp ,pv-offset-form ,parameter ,position
547                                ',slot-name ',class)))))))
548
549 (defun optimize-accessor-call (slots read/write sparameter gf-name new-value)
550   (let* ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
551          (parameter (if (consp sparameter) (car sparameter) sparameter))
552          (parameter-entry (assq parameter slots))
553          (name (case read/write
554                  (:read `(reader ,gf-name))
555                  (:write `(writer ,gf-name))))
556          (slot-entry      (assoc name (cdr parameter-entry) :test #'equal))
557          (position (posq parameter-entry slots))
558          (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
559     (unless parameter-entry
560       (error "internal error in slot optimization"))
561     (unless slot-entry
562       (setq slot-entry (list name))
563       (push slot-entry (cdr parameter-entry)))
564     (push pv-offset-form (cdr slot-entry))
565     (ecase read/write
566       (:read
567        `(instance-reader ,pv-offset-form ,parameter ,position ,gf-name ',class))
568       (:write
569        `(let ((.new-value. ,new-value))
570           (instance-writer ,pv-offset-form ,parameter ,position ,gf-name ',class
571                            .new-value.))))))
572
573 (defvar *unspecific-arg* '..unspecific-arg..)
574
575 (defun optimize-gf-call-internal (form slots env)
576   (when (and (consp form)
577              (eq (car form) 'the))
578     (setq form (caddr form)))
579   (or (and (symbolp form)
580            (let* ((rebound? (caddr (var-declaration '%variable-rebinding
581                                                     form
582                                                     env)))
583                   (parameter-or-nil (car (assq (or rebound? form) slots))))
584              (when parameter-or-nil
585                (let* ((class-name (caddr (var-declaration 'class
586                                                           parameter-or-nil
587                                                           env))))
588                  (when (and class-name (not (eq class-name t)))
589                    (position parameter-or-nil slots :key #'car))))))
590       (if (constantp form)
591           (let ((form (eval form)))
592             (if (symbolp form)
593                 form
594                 *unspecific-arg*))
595           *unspecific-arg*)))
596
597 (defun optimize-gf-call (slots calls gf-call-form nreq restp env)
598   (unless (eq (car gf-call-form) 'make-instance) ; XXX needs more work
599     (let* ((args (cdr gf-call-form))
600            (all-args-p (eq (car gf-call-form) 'make-instance))
601            (non-required-args (nthcdr nreq args))
602            (required-args (ldiff args non-required-args))
603            (call-spec (list (car gf-call-form) nreq restp
604                             (mapcar (lambda (form)
605                                       (optimize-gf-call-internal form slots env))
606                                     (if all-args-p
607                                         args
608                                         required-args))))
609            (call-entry (assoc call-spec calls :test #'equal))
610            (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
611       (unless (some #'integerp
612                     (let ((spec-args (cdr call-spec)))
613                       (if all-args-p
614                           (ldiff spec-args (nthcdr nreq spec-args))
615                           spec-args)))
616         (return-from optimize-gf-call nil))
617       (unless call-entry
618         (setq call-entry (list call-spec))
619         (push call-entry (cdr calls)))
620       (push pv-offset-form (cdr call-entry))
621       (if (eq (car call-spec) 'make-instance)
622           `(funcall (pv-ref .pv. ,pv-offset-form) ,@(cdr gf-call-form))
623           `(let ((.emf. (pv-ref .pv. ,pv-offset-form)))
624             (invoke-effective-method-function .emf. ,restp
625              ,@required-args ,@(when restp `((list ,@non-required-args)))))))))
626
627 (define-walker-template pv-offset) ; These forms get munged by mutate slots.
628 (defmacro pv-offset (arg) arg)
629 (define-walker-template instance-accessor-parameter)
630 (defmacro instance-accessor-parameter (x) x)
631
632 ;;; It is safe for these two functions to be wrong. They just try to
633 ;;; guess what the most likely case will be.
634 (defun generate-fast-class-slot-access-p (class-form slot-name-form)
635   (let ((class (and (constantp class-form) (eval class-form)))
636         (slot-name (and (constantp slot-name-form) (eval slot-name-form))))
637     (and (eq *boot-state* 'complete)
638          (standard-class-p class)
639          (not (eq class *the-class-t*)) ; shouldn't happen, though.
640          (let ((slotd (find-slot-definition class slot-name)))
641            (and slotd (classp (slot-definition-allocation slotd)))))))
642
643 (defun skip-fast-slot-access-p (class-form slot-name-form type)
644   (let ((class (and (constantp class-form) (eval class-form)))
645         (slot-name (and (constantp slot-name-form) (eval slot-name-form))))
646     (and (eq *boot-state* 'complete)
647          (standard-class-p class)
648          (not (eq class *the-class-t*)) ; shouldn't happen, though.
649          (let ((slotd (find-slot-definition class slot-name)))
650            (and slotd (skip-optimize-slot-value-by-class-p class
651                                                            slot-name
652                                                            type))))))
653
654 (defun skip-optimize-slot-value-by-class-p (class slot-name type)
655   (let ((slotd (find-slot-definition class slot-name)))
656     (and slotd
657          (eq *boot-state* 'complete)
658          (not (slot-accessor-std-p slotd type)))))
659
660 (defmacro instance-read-internal (pv slots pv-offset default &optional type)
661   (unless (member type '(nil :instance :class :default))
662     (error "illegal type argument to ~S: ~S" 'instance-read-internal type))
663   (if (eq type :default)
664       default
665       (let* ((index (gensym))
666              (value index))
667         `(locally (declare #.*optimize-speed*)
668           (let ((,index (pvref ,pv ,pv-offset)))
669             (setq ,value (typecase ,index
670                            ,@(when (or (null type) (eq type :instance))
671                                `((fixnum (clos-slots-ref ,slots ,index))))
672                            ,@(when (or (null type) (eq type :class))
673                                `((cons (cdr ,index))))
674                            (t +slot-unbound+)))
675             (if (eq ,value +slot-unbound+)
676                 ,default
677                 ,value))))))
678
679 (defmacro instance-read (pv-offset parameter position slot-name class)
680   (if (skip-fast-slot-access-p class slot-name 'reader)
681       `(accessor-slot-value ,parameter ,slot-name)
682       `(instance-read-internal .pv. ,(slot-vector-symbol position)
683         ,pv-offset (accessor-slot-value ,parameter ,slot-name)
684         ,(if (generate-fast-class-slot-access-p class slot-name)
685              :class :instance))))
686
687 (defmacro instance-reader (pv-offset parameter position gf-name class)
688   (declare (ignore class))
689   `(instance-read-internal .pv. ,(slot-vector-symbol position)
690     ,pv-offset
691     (,gf-name (instance-accessor-parameter ,parameter))
692     :instance))
693
694 (defmacro instance-write-internal (pv slots pv-offset new-value default
695                                       &optional type)
696   (unless (member type '(nil :instance :class :default))
697     (error "illegal type argument to ~S: ~S" 'instance-write-internal type))
698   (if (eq type :default)
699       default
700       (let* ((index (gensym)))
701         `(locally (declare #.*optimize-speed*)
702           (let ((,index (pvref ,pv ,pv-offset)))
703             (typecase ,index
704               ,@(when (or (null type) (eq type :instance))
705                       `((fixnum (setf (clos-slots-ref ,slots ,index)
706                                       ,new-value))))
707               ,@(when (or (null type) (eq type :class))
708                   `((cons (setf (cdr ,index) ,new-value))))
709               (t ,default)))))))
710
711 (defmacro instance-write (pv-offset
712                           parameter
713                           position
714                           slot-name
715                           class
716                           new-value)
717   (if (skip-fast-slot-access-p class slot-name 'writer)
718       `(accessor-set-slot-value ,parameter ,slot-name ,new-value)
719       `(instance-write-internal .pv. ,(slot-vector-symbol position)
720         ,pv-offset ,new-value
721         (accessor-set-slot-value ,parameter ,slot-name ,new-value)
722         ,(if (generate-fast-class-slot-access-p class slot-name)
723              :class :instance))))
724
725 (defmacro instance-writer (pv-offset
726                            parameter
727                            position
728                            gf-name
729                            class
730                            new-value)
731   (declare (ignore class))
732   `(instance-write-internal .pv. ,(slot-vector-symbol position)
733     ,pv-offset ,new-value
734     (,(if (consp gf-name)
735           (get-setf-fun-name gf-name)
736           gf-name)
737      (instance-accessor-parameter ,parameter)
738      ,new-value)
739     :instance))
740
741 (defmacro instance-boundp-internal (pv slots pv-offset default
742                                        &optional type)
743   (unless (member type '(nil :instance :class :default))
744     (error "illegal type argument to ~S: ~S" 'instance-boundp-internal type))
745   (if (eq type :default)
746       default
747       (let* ((index (gensym)))
748         `(locally (declare #.*optimize-speed*)
749           (let ((,index (pvref ,pv ,pv-offset)))
750             (typecase ,index
751               ,@(when (or (null type) (eq type :instance))
752                   `((fixnum (not (and ,slots
753                                       (eq (clos-slots-ref ,slots ,index)
754                                           +slot-unbound+))))))
755               ,@(when (or (null type) (eq type :class))
756                   `((cons (not (eq (cdr ,index) +slot-unbound+)))))
757               (t ,default)))))))
758
759 (defmacro instance-boundp (pv-offset parameter position slot-name class)
760   (if (skip-fast-slot-access-p class slot-name 'boundp)
761       `(accessor-slot-boundp ,parameter ,slot-name)
762       `(instance-boundp-internal .pv. ,(slot-vector-symbol position)
763         ,pv-offset (accessor-slot-boundp ,parameter ,slot-name)
764         ,(if (generate-fast-class-slot-access-p class slot-name)
765              :class :instance))))
766
767 ;;; This magic function has quite a job to do indeed.
768 ;;;
769 ;;; The careful reader will recall that <slots> contains all of the
770 ;;; optimized slot access forms produced by OPTIMIZE-INSTANCE-ACCESS.
771 ;;; Each of these is a call to either INSTANCE-READ or INSTANCE-WRITE.
772 ;;;
773 ;;; At the time these calls were produced, the first argument was
774 ;;; specified as the symbol .PV-OFFSET.; what we have to do now is
775 ;;; convert those pv-offset arguments into the actual number that is
776 ;;; the correct offset into the pv.
777 ;;;
778 ;;; But first, oh but first, we sort <slots> a bit so that for each
779 ;;; argument we have the slots in alphabetical order. This
780 ;;; canonicalizes the PV-TABLE's a bit and will hopefully lead to
781 ;;; having fewer PV's floating around. Even if the gain is only
782 ;;; modest, it costs nothing.
783 (defun slot-name-lists-from-slots (slots calls)
784   (multiple-value-bind (slots calls) (mutate-slots-and-calls slots calls)
785     (let* ((slot-name-lists
786             (mapcar (lambda (parameter-entry)
787                       (cons nil (mapcar #'car (cdr parameter-entry))))
788                     slots))
789            (call-list
790             (mapcar #'car calls)))
791       (dolist (call call-list)
792         (dolist (arg (cdr call))
793           (when (integerp arg)
794             (setf (car (nth arg slot-name-lists)) t))))
795       (setq slot-name-lists (mapcar (lambda (r+snl)
796                                       (when (or (car r+snl) (cdr r+snl))
797                                         r+snl))
798                                     slot-name-lists))
799       (let ((cvt (apply #'vector
800                         (let ((i -1))
801                           (mapcar (lambda (r+snl)
802                                     (when r+snl (incf i)))
803                                   slot-name-lists)))))
804         (setq call-list (mapcar (lambda (call)
805                                   (cons (car call)
806                                         (mapcar (lambda (arg)
807                                                   (if (integerp arg)
808                                                       (svref cvt arg)
809                                                       arg))
810                                                 (cdr call))))
811                                 call-list)))
812       (values slot-name-lists call-list))))
813
814 (defun mutate-slots-and-calls (slots calls)
815   (let ((sorted-slots (sort-slots slots))
816         (sorted-calls (sort-calls (cdr calls)))
817         (pv-offset 0))  ; index 0 is for info
818     (dolist (parameter-entry sorted-slots)
819       (dolist (slot-entry (cdr parameter-entry))
820         (incf pv-offset)        
821         (dolist (form (cdr slot-entry))
822           (setf (cadr form) pv-offset))))
823     (dolist (call-entry sorted-calls)
824       (incf pv-offset)
825       (dolist (form (cdr call-entry))
826         (setf (cadr form) pv-offset)))
827     (values sorted-slots sorted-calls)))
828
829 (defun symbol-pkg-name (sym)
830   (let ((pkg (symbol-package sym)))
831     (if pkg (package-name pkg) "")))
832
833 ;;; FIXME: Because of the existence of UNINTERN and RENAME-PACKAGE,
834 ;;; the part of this ordering which is based on SYMBOL-PKG-NAME is not
835 ;;; stable. This ordering is only used in to
836 ;;; SLOT-NAME-LISTS-FROM-SLOTS, where it serves to "canonicalize the
837 ;;; PV-TABLE's a bit and will hopefully lead to having fewer PV's
838 ;;; floating around", so it sounds as though the instability won't
839 ;;; actually lead to bugs, just small inefficiency. But still, it
840 ;;; would be better to reimplement this function as a comparison based
841 ;;; on SYMBOL-HASH:
842 ;;;   * stable comparison
843 ;;;   * smaller code (here, and in being able to discard SYMBOL-PKG-NAME)
844 ;;;   * faster code.
845 (defun symbol-lessp (a b)
846   (if (eq (symbol-package a)
847           (symbol-package b))
848       (string-lessp (symbol-name a)
849                     (symbol-name b))
850       (string-lessp (symbol-pkg-name a)
851                     (symbol-pkg-name b))))
852
853 (defun symbol-or-cons-lessp (a b)
854   (etypecase a
855     (symbol (etypecase b
856               (symbol (symbol-lessp a b))
857               (cons t)))
858     (cons   (etypecase b
859               (symbol nil)
860               (cons (if (eq (car a) (car b))
861                         (symbol-or-cons-lessp (cdr a) (cdr b))
862                         (symbol-or-cons-lessp (car a) (car b))))))))
863
864 (defun sort-slots (slots)
865   (mapcar (lambda (parameter-entry)
866             (cons (car parameter-entry)
867                   (sort (cdr parameter-entry)   ;slot entries
868                         #'symbol-or-cons-lessp
869                         :key #'car)))
870           slots))
871
872 (defun sort-calls (calls)
873   (sort calls #'symbol-or-cons-lessp :key #'car))
874 \f
875 ;;;; This needs to work in terms of metatypes and also needs to work
876 ;;;; for automatically generated reader and writer functions.
877 ;;;; Automatically generated reader and writer functions use this
878 ;;;; stuff too.
879
880 (defmacro pv-binding ((required-parameters slot-name-lists pv-table-symbol)
881                       &body body)
882   (let (slot-vars pv-parameters)
883     (loop for slots in slot-name-lists
884           for required-parameter in required-parameters
885           for i from 0
886           do (when slots
887                (push required-parameter pv-parameters)
888                (push (slot-vector-symbol i) slot-vars)))
889     `(pv-binding1 (.pv. .calls. ,pv-table-symbol
890                    ,(nreverse pv-parameters) ,(nreverse slot-vars))
891        ,@body)))
892
893 (defmacro pv-binding1 ((pv calls pv-table-symbol pv-parameters slot-vars)
894                        &body body)
895   `(pv-env (,pv ,calls ,pv-table-symbol ,pv-parameters)
896      (let (,@(mapcar (lambda (slot-var p) `(,slot-var (get-slots-or-nil ,p)))
897                      slot-vars pv-parameters))
898         ,@body)))
899
900 ;;; This gets used only when the default MAKE-METHOD-LAMBDA is
901 ;;; overridden.
902 (defmacro pv-env ((pv calls pv-table-symbol pv-parameters)
903                   &rest forms)
904   `(let* ((.pv-table. ,pv-table-symbol)
905           (.pv-cell. (pv-table-lookup-pv-args .pv-table. ,@pv-parameters))
906           (,pv (car .pv-cell.))
907           (,calls (cdr .pv-cell.)))
908      (declare ,(make-pv-type-declaration pv))
909      (declare ,(make-calls-type-declaration calls))
910      ,@(when (symbolp pv-table-symbol)
911          `((declare (special ,pv-table-symbol))))
912      ,pv ,calls
913      ,@forms))
914
915 (defvar *non-var-declarations*
916   ;; FIXME: VALUES was in this list, conditionalized with #+CMU, but I
917   ;; don't *think* CMU CL had, or SBCL has, VALUES declarations. If
918   ;; SBCL doesn't have 'em, VALUES should probably be removed from
919   ;; this list.
920   '(values %method-name %method-lambda-list
921     optimize ftype inline notinline))
922
923 (defvar *var-declarations-with-arg*
924   '(%class
925     type))
926
927 (defvar *var-declarations-without-arg*
928   '(ignore
929     ignorable special dynamic-extent
930     ;; FIXME: Possibly this entire list and variable could go away.
931     ;; If not, certainly we should remove all these built-in typenames
932     ;; from the list, and replace them with a test for "is it a type
933     ;; name?" (CLTL1 allowed only built-in type names as declarations,
934     ;; but ANSI CL allows any type name as a declaration.)
935     array atom base-char bignum bit bit-vector character compiled-function
936     complex cons double-float extended-char
937     fixnum float function hash-table integer
938     keyword list long-float nil null number package pathname random-state ratio
939     rational readtable sequence short-float signed-byte simple-array
940     simple-bit-vector simple-string simple-vector single-float standard-char
941     stream string symbol t unsigned-byte vector))
942
943 (defun split-declarations (body args calls-next-method-p)
944   (let ((inner-decls nil)
945         (outer-decls nil)
946         decl)
947     (loop (when (null body) (return nil))
948           (setq decl (car body))
949           (unless (and (consp decl)
950                        (eq (car decl) 'declare))
951             (return nil))
952           (dolist (form (cdr decl))
953             (when (consp form)
954               (let ((declaration-name (car form)))
955                 (if (member declaration-name *non-var-declarations*)
956                     (push `(declare ,form) outer-decls)
957                     (let ((arg-p
958                            (member declaration-name
959                                    *var-declarations-with-arg*))
960                           (non-arg-p
961                            (member declaration-name
962                                    *var-declarations-without-arg*))
963                           (dname (list (pop form)))
964                           (inners nil) (outers nil))
965                       (unless (or arg-p non-arg-p)
966                         ;; FIXME: This warning, and perhaps the
967                         ;; various *VAR-DECLARATIONS-FOO* and/or
968                         ;; *NON-VAR-DECLARATIONS* variables,
969                         ;; could probably go away now that we're not
970                         ;; trying to be portable between different
971                         ;; CLTL1 hosts the way PCL was. (Note that to
972                         ;; do this right, we need to be able to handle
973                         ;; user-defined (DECLAIM (DECLARATION FOO))
974                         ;; stuff.)
975                         (warn "The declaration ~S is not understood by ~S.~@
976                                Please put ~S on one of the lists ~S,~%~S, or~%~S.~@
977                         (Assuming it is a variable declaration without argument)."
978                               declaration-name 'split-declarations
979                               declaration-name
980                               '*non-var-declarations*
981                               '*var-declarations-with-arg*
982                               '*var-declarations-without-arg*)
983                         (push declaration-name *var-declarations-without-arg*))
984                       (when arg-p
985                         (setq dname (append dname (list (pop form)))))
986                       (dolist (var form)
987                         (if (member var args)
988                             ;; Quietly remove IGNORE declarations on
989                             ;; args when a next-method is involved, to
990                             ;; prevent compiler warns about ignored
991                             ;; args being read.
992                             (unless (and calls-next-method-p
993                                          (eq (car dname) 'ignore))
994                                 (push var outers))
995                             (push var inners)))
996                       (when outers
997                         (push `(declare (,@dname ,@outers)) outer-decls))
998                       (when inners
999                         (push `(declare (,@dname ,@inners)) inner-decls)))))))
1000           (setq body (cdr body)))
1001     (values outer-decls inner-decls body)))
1002
1003 (defun make-method-initargs-form-internal (method-lambda initargs env)
1004   (declare (ignore env))
1005   (let (method-lambda-args lmf lmf-params)
1006     (if (not (and (= 3 (length method-lambda))
1007                   (= 2 (length (setq method-lambda-args (cadr method-lambda))))
1008                   (consp (setq lmf (third method-lambda)))
1009                   (eq 'simple-lexical-method-functions (car lmf))
1010                   (eq (car method-lambda-args)
1011                       (cadr (setq lmf-params (cadr lmf))))
1012                   (eq (cadr method-lambda-args)
1013                       (caddr lmf-params))))
1014         `(list* :function #',method-lambda
1015                 ',initargs)
1016         (let* ((lambda-list (car lmf-params))
1017                (nreq 0)(restp nil)(args nil))
1018           (dolist (arg lambda-list)
1019             (when (member arg '(&optional &rest &key))
1020               (setq restp t)(return nil))
1021             (when (eq arg '&aux) (return nil))
1022             (incf nreq)(push arg args))
1023           (setq args (nreverse args))
1024           (setf (getf (getf initargs :plist) :arg-info) (cons nreq restp))
1025           (make-method-initargs-form-internal1
1026            initargs (cddr lmf) args lmf-params restp)))))
1027
1028 (defun make-method-initargs-form-internal1
1029     (initargs body req-args lmf-params restp)
1030   (multiple-value-bind (outer-decls inner-decls body)
1031       (split-declarations
1032        body req-args (getf (cdr lmf-params) :call-next-method-p))
1033     (let* ((rest-arg (when restp '.rest-arg.))
1034            (args+rest-arg (if restp
1035                               (append req-args (list rest-arg))
1036                               req-args)))
1037       `(list* :fast-function
1038         (lambda (.pv-cell. .next-method-call. ,@args+rest-arg)
1039           (declare (ignorable .pv-cell. .next-method-call.))
1040           ,@outer-decls
1041           (macrolet ((pv-env ((pv calls pv-table-symbol pv-parameters)
1042                               &rest forms)
1043                        (declare (ignore pv-table-symbol pv-parameters))
1044                        `(let ((,pv (car .pv-cell.))
1045                               (,calls (cdr .pv-cell.)))
1046                           (declare ,(make-pv-type-declaration pv)
1047                                    ,(make-calls-type-declaration calls))
1048                           ,pv ,calls
1049                           ,@forms)))
1050             (fast-lexical-method-functions
1051              (,(car lmf-params) .next-method-call. ,req-args ,rest-arg
1052               ,@(cdddr lmf-params))
1053              ,@inner-decls
1054              ,@body)))
1055         ',initargs))))
1056
1057 ;;; Use arrays and hash tables and the fngen stuff to make this much
1058 ;;; better. It doesn't really matter, though, because a function
1059 ;;; returned by this will get called only when the user explicitly
1060 ;;; funcalls a result of method-function. BUT, this is needed to make
1061 ;;; 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)
1070             (unless pv-table
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))
1077                    (nmc (when nm
1078                           (make-method-call
1079                            :function (if (std-instance-p nm)
1080                                          (method-function nm)
1081                                          nm)
1082                            :call-method-args (list nms)))))
1083               (if restp
1084                   (let* ((rest (nthcdr nreq method-args))
1085                          (args (ldiff method-args rest)))
1086                     (apply fmf pv-cell nmc (nconc args (list rest))))
1087                   (apply fmf pv-cell nmc method-args)))))
1088     (let* ((fname (method-function-get fmf :name))
1089            (name `(,(or (get (car fname) 'method-sym)
1090                         (setf (get (car fname) 'method-sym)
1091                               (let ((str (symbol-name (car fname))))
1092                                 (if (string= "FAST-" str :end2 5)
1093                                     (intern (subseq str 5) *pcl-package*)
1094                                     (car fname)))))
1095                     ,@(cdr fname))))
1096       (set-fun-name method-function name))
1097     (setf (method-function-get method-function :fast-function) fmf)
1098     method-function))
1099
1100 (defun get-method-function-pv-cell (method-function
1101                                     method-args
1102                                     &optional pv-table)
1103   (let ((pv-table (or pv-table (method-function-pv-table method-function))))
1104     (when pv-table
1105       (let ((pv-wrappers (pv-wrappers-from-all-args pv-table method-args)))
1106         (when pv-wrappers
1107           (pv-table-lookup pv-table pv-wrappers))))))
1108
1109 (defun pv-table-lookup-pv-args (pv-table &rest pv-parameters)
1110   (pv-table-lookup pv-table (pv-wrappers-from-pv-args pv-parameters)))
1111
1112 (defun pv-wrappers-from-pv-args (&rest args)
1113   (let* ((nkeys (length args))
1114          (pv-wrappers (make-list nkeys))
1115          w
1116          (w-t pv-wrappers))
1117     (dolist (arg args)
1118       (setq w (wrapper-of arg))
1119       (unless (eq t (wrapper-state w)) ; FIXME: should be INVALID-WRAPPER-P
1120         (setq w (check-wrapper-validity arg)))
1121       (setf (car w-t) w))
1122       (setq w-t (cdr w-t))
1123       (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))
1124       pv-wrappers))
1125
1126 (defun pv-wrappers-from-all-args (pv-table args)
1127   (let ((nkeys 0)
1128         (slot-name-lists (pv-table-slot-name-lists pv-table)))
1129     (dolist (sn slot-name-lists)
1130       (when sn (incf nkeys)))
1131     (let* ((pv-wrappers (make-list nkeys))
1132            (pv-w-t pv-wrappers))
1133       (dolist (sn slot-name-lists)
1134         (when sn
1135           (let* ((arg (car args))
1136                  (w (wrapper-of arg)))
1137             (unless w ; CAN-OPTIMIZE-ACCESS prevents this from happening.
1138               (error "error in PV-WRAPPERS-FROM-ALL-ARGS"))
1139             (setf (car pv-w-t) w)
1140             (setq pv-w-t (cdr pv-w-t))))
1141         (setq args (cdr args)))
1142       (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))
1143       pv-wrappers)))
1144
1145 (defun pv-wrappers-from-all-wrappers (pv-table wrappers)
1146   (let ((nkeys 0)
1147         (slot-name-lists (pv-table-slot-name-lists pv-table)))
1148     (dolist (sn slot-name-lists)
1149       (when sn (incf nkeys)))
1150     (let* ((pv-wrappers (make-list nkeys))
1151            (pv-w-t pv-wrappers))
1152       (dolist (sn slot-name-lists)
1153         (when sn
1154           (let ((w (car wrappers)))
1155             (unless w ; CAN-OPTIMIZE-ACCESS prevents this from happening.
1156               (error "error in PV-WRAPPERS-FROM-ALL-WRAPPERS"))
1157             (setf (car pv-w-t) w)
1158             (setq pv-w-t (cdr pv-w-t))))
1159         (setq wrappers (cdr wrappers)))
1160       (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))
1161       pv-wrappers)))