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