37226a73a3d4f6b194d0ee9373393c48a1c256d0
[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 (var-declaration '%variable-rebinding var env)))
419            (parameter-or-nil (car (memq (or rebound? var)
420                                         required-parameters))))
421       (when parameter-or-nil
422         (let* ((class-name (caddr (var-declaration '%class
423                                                    parameter-or-nil
424                                                    env)))
425                (class (find-class class-name nil)))
426           (when (or (not (eq *boot-state* 'complete))
427                     (and class (not (class-finalized-p class))))
428             (setq class nil))
429           (when (and class-name (not (eq class-name t)))
430             (when (or (null type)
431                       (not (and class
432                                 (memq *the-class-structure-object*
433                                       (class-precedence-list class))))
434                       (optimize-slot-value-by-class-p class slot-name type))
435               (cons parameter-or-nil (or class class-name)))))))))
436
437 (defun optimize-slot-value (slots sparameter form)
438   (if sparameter
439       (destructuring-bind (ignore1 ignore2 slot-name-form) form
440         (declare (ignore ignore1 ignore2))
441         (let ((slot-name (eval slot-name-form)))
442           (optimize-instance-access slots :read sparameter slot-name nil)))
443       `(accessor-slot-value ,@(cdr form))))
444
445 (defun optimize-set-slot-value (slots sparameter form)
446   (if sparameter
447       (destructuring-bind (ignore1 ignore2 slot-name-form new-value) form
448         (declare (ignore ignore1 ignore2))
449         (let ((slot-name (eval slot-name-form)))
450           (optimize-instance-access slots
451                                     :write
452                                     sparameter
453                                     slot-name
454                                     new-value)))
455       `(accessor-set-slot-value ,@(cdr form))))
456
457 (defun optimize-slot-boundp (slots sparameter form)
458   (if sparameter
459       (destructuring-bind
460           ;; FIXME: In CMU CL ca. 19991205, this binding list had a
461           ;; fourth element in it, NEW-VALUE. It's hard to see how
462           ;; that could possibly be right, since SLOT-BOUNDP has no
463           ;; NEW-VALUE. Since it was causing a failure in building PCL
464           ;; for SBCL, so I changed it to match the definition of
465           ;; SLOT-BOUNDP (and also to match the list used in the
466           ;; similar OPTIMIZE-SLOT-VALUE, above). However, I'm weirded
467           ;; out by this, since this is old code which has worked for
468           ;; ages to build PCL for CMU CL, so it's hard to see why it
469           ;; should need a patch like this in order to build PCL for
470           ;; SBCL. I'd like to return to this and find a test case
471           ;; which exercises this function both in CMU CL, to see
472           ;; whether it's really a previously-unexercised bug or
473           ;; whether I've misunderstood something (and, presumably,
474           ;; patched it wrong).
475           (slot-boundp-symbol instance slot-name-form)
476           form
477         (declare (ignore slot-boundp-symbol instance))
478         (let ((slot-name (eval slot-name-form)))
479           (optimize-instance-access slots
480                                     :boundp
481                                     sparameter
482                                     slot-name
483                                     nil)))
484       `(accessor-slot-boundp ,@(cdr form))))
485
486 (defun optimize-reader (slots sparameter gf-name form)
487   (if sparameter
488       (optimize-accessor-call slots :read sparameter gf-name nil)
489       form))
490
491 (defun optimize-writer (slots sparameter gf-name form)
492   (if sparameter
493       (destructuring-bind (ignore1 ignore2 new-value) form
494         (declare (ignore ignore1 ignore2))
495         (optimize-accessor-call slots :write sparameter gf-name new-value))
496       form))
497
498 ;;; The SLOTS argument is an alist, the CAR of each entry is the name
499 ;;; of a required parameter to the function. The alist is in order, so
500 ;;; the position of an entry in the alist corresponds to the
501 ;;; argument's position in the lambda list.
502 (defun optimize-instance-access (slots
503                                  read/write
504                                  sparameter
505                                  slot-name
506                                  new-value)
507   (let ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
508         (parameter (if (consp sparameter) (car sparameter) sparameter)))
509     (if (and (eq *boot-state* 'complete)
510              (classp class)
511              (memq *the-class-structure-object* (class-precedence-list class)))
512         (let ((slotd (find-slot-definition class slot-name)))
513           (ecase read/write
514             (:read
515              `(,(slot-definition-defstruct-accessor-symbol slotd) ,parameter))
516             (:write
517              `(setf (,(slot-definition-defstruct-accessor-symbol slotd)
518                      ,parameter)
519                     ,new-value))
520             (:boundp
521              t)))
522         (let* ((parameter-entry (assq parameter slots))
523                (slot-entry      (assq slot-name (cdr parameter-entry)))
524                (position (posq parameter-entry slots))
525                (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
526           (unless parameter-entry
527             (error "internal error in slot optimization"))
528           (unless slot-entry
529             (setq slot-entry (list slot-name))
530             (push slot-entry (cdr parameter-entry)))
531           (push pv-offset-form (cdr slot-entry))
532           (ecase read/write
533             (:read
534              `(instance-read ,pv-offset-form ,parameter ,position
535                              ',slot-name ',class))
536             (:write
537              `(let ((.new-value. ,new-value))
538                 (instance-write ,pv-offset-form ,parameter ,position
539                                 ',slot-name ',class .new-value.)))
540             (:boundp
541              `(instance-boundp ,pv-offset-form ,parameter ,position
542                                ',slot-name ',class)))))))
543
544 (defun optimize-accessor-call (slots read/write sparameter gf-name new-value)
545   (let* ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
546          (parameter (if (consp sparameter) (car sparameter) sparameter))
547          (parameter-entry (assq parameter slots))
548          (name (case read/write
549                  (:read `(reader ,gf-name))
550                  (:write `(writer ,gf-name))))
551          (slot-entry      (assoc name (cdr parameter-entry) :test #'equal))
552          (position (posq parameter-entry slots))
553          (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
554     (unless parameter-entry
555       (error "internal error in slot optimization"))
556     (unless slot-entry
557       (setq slot-entry (list name))
558       (push slot-entry (cdr parameter-entry)))
559     (push pv-offset-form (cdr slot-entry))
560     (ecase read/write
561       (:read
562        `(instance-reader ,pv-offset-form ,parameter ,position ,gf-name ',class))
563       (:write
564        `(let ((.new-value. ,new-value))
565           (instance-writer ,pv-offset-form ,parameter ,position ,gf-name ',class
566                            .new-value.))))))
567
568 (defvar *unspecific-arg* '..unspecific-arg..)
569
570 (defun optimize-gf-call-internal (form slots env)
571   (when (and (consp form)
572              (eq (car form) 'the))
573     (setq form (caddr form)))
574   (or (and (symbolp form)
575            (let* ((rebound? (caddr (var-declaration '%variable-rebinding
576                                                     form
577                                                     env)))
578                   (parameter-or-nil (car (assq (or rebound? form) slots))))
579              (when parameter-or-nil
580                (let* ((class-name (caddr (var-declaration 'class
581                                                           parameter-or-nil
582                                                           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-fun-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
896 ;;; overridden.
897 (defmacro pv-env ((pv calls pv-table-symbol pv-parameters)
898                   &rest forms)
899   `(let* ((.pv-table. ,pv-table-symbol)
900           (.pv-cell. (pv-table-lookup-pv-args .pv-table. ,@pv-parameters))
901           (,pv (car .pv-cell.))
902           (,calls (cdr .pv-cell.)))
903      (declare ,(make-pv-type-declaration pv))
904      (declare ,(make-calls-type-declaration calls))
905      ,@(when (symbolp pv-table-symbol)
906          `((declare (special ,pv-table-symbol))))
907      ,pv ,calls
908      ,@forms))
909
910 (defvar *non-var-declarations*
911   ;; FIXME: VALUES was in this list, conditionalized with #+CMU, but I
912   ;; don't *think* CMU CL had, or SBCL has, VALUES declarations. If
913   ;; SBCL doesn't have 'em, VALUES should probably be removed from
914   ;; this list.
915   '(values %method-name %method-lambda-list
916     optimize ftype inline notinline))
917
918 (defvar *var-declarations-with-argument*
919   '(%class
920     type))
921
922 (defvar *var-declarations-without-argument*
923   '(ignore
924     ignorable special dynamic-extent
925     ;; FIXME: Possibly this entire list and variable could go away.
926     ;; If not, certainly we should remove all these built-in typenames
927     ;; from the list, and replace them with a test for "is it a type
928     ;; name?" (CLTL1 allowed only built-in type names as declarations,
929     ;; but ANSI CL allows any type name as a declaration.)
930     array atom base-char bignum bit bit-vector character compiled-function
931     complex cons double-float extended-char
932     fixnum float function hash-table integer
933     keyword list long-float nil null number package pathname random-state ratio
934     rational readtable sequence short-float signed-byte simple-array
935     simple-bit-vector simple-string simple-vector single-float standard-char
936     stream string symbol t unsigned-byte vector))
937
938 (defun split-declarations (body args calls-next-method-p)
939   (let ((inner-decls nil)
940         (outer-decls nil)
941         decl)
942     (loop (when (null body) (return nil))
943           (setq decl (car body))
944           (unless (and (consp decl)
945                        (eq (car decl) 'declare))
946             (return nil))
947           (dolist (form (cdr decl))
948             (when (consp form)
949               (let ((declaration-name (car form)))
950                 (if (member declaration-name *non-var-declarations*)
951                     (push `(declare ,form) outer-decls)
952                     (let ((arg-p
953                            (member declaration-name
954                                    *var-declarations-with-argument*))
955                           (non-arg-p
956                            (member declaration-name
957                                    *var-declarations-without-argument*))
958                           (dname (list (pop form)))
959                           (inners nil) (outers nil))
960                       (unless (or arg-p non-arg-p)
961                         ;; FIXME: This warning, and perhaps the
962                         ;; various *VAR-DECLARATIONS-FOO* and/or
963                         ;; *NON-VAR-DECLARATIONS* variables,
964                         ;; could probably go away now that we're not
965                         ;; trying to be portable between different
966                         ;; CLTL1 hosts the way PCL was. (Note that to
967                         ;; do this right, we need to be able to handle
968                         ;; user-defined (DECLAIM (DECLARATION FOO))
969                         ;; stuff.)
970                         (warn "The declaration ~S is not understood by ~S.~@
971                                Please put ~S on one of the lists ~S,~%~S, or~%~S.~@
972                         (Assuming it is a variable declaration without argument)."
973                               declaration-name 'split-declarations
974                               declaration-name
975                               '*non-var-declarations*
976                               '*var-declarations-with-argument*
977                               '*var-declarations-without-argument*)
978                         (push declaration-name
979                               *var-declarations-without-argument*))
980                       (when arg-p
981                         (setq dname (append dname (list (pop form)))))
982                       (dolist (var form)
983                         (if (member var args)
984                             ;; Quietly remove IGNORE declarations on
985                             ;; args when a next-method is involved, to
986                             ;; prevent compiler warns about ignored
987                             ;; args being read.
988                             (unless (and calls-next-method-p
989                                          (eq (car dname) 'ignore))
990                                 (push var outers))
991                             (push var inners)))
992                       (when outers
993                         (push `(declare (,@dname ,@outers)) outer-decls))
994                       (when inners
995                         (push `(declare (,@dname ,@inners)) inner-decls)))))))
996           (setq body (cdr body)))
997     (values outer-decls inner-decls body)))
998
999 (defun make-method-initargs-form-internal (method-lambda initargs env)
1000   (declare (ignore env))
1001   (let (method-lambda-args lmf lmf-params)
1002     (if (not (and (= 3 (length method-lambda))
1003                   (= 2 (length (setq method-lambda-args (cadr method-lambda))))
1004                   (consp (setq lmf (third method-lambda)))
1005                   (eq 'simple-lexical-method-functions (car lmf))
1006                   (eq (car method-lambda-args)
1007                       (cadr (setq lmf-params (cadr lmf))))
1008                   (eq (cadr method-lambda-args)
1009                       (caddr lmf-params))))
1010         `(list* :function #',method-lambda
1011                 ',initargs)
1012         (let* ((lambda-list (car lmf-params))
1013                (nreq 0)(restp nil)(args nil))
1014           (dolist (arg lambda-list)
1015             (when (member arg '(&optional &rest &key))
1016               (setq restp t)(return nil))
1017             (when (eq arg '&aux) (return nil))
1018             (incf nreq)(push arg args))
1019           (setq args (nreverse args))
1020           (setf (getf (getf initargs ':plist) ':arg-info) (cons nreq restp))
1021           (make-method-initargs-form-internal1
1022            initargs (cddr lmf) args lmf-params restp)))))
1023
1024 (defun make-method-initargs-form-internal1
1025     (initargs body req-args lmf-params restp)
1026   (multiple-value-bind (outer-decls inner-decls body)
1027       (split-declarations
1028        body req-args (getf (cdr lmf-params) :call-next-method-p))
1029     (let* ((rest-arg (when restp '.rest-arg.))
1030            (args+rest-arg (if restp
1031                               (append req-args (list rest-arg))
1032                               req-args)))
1033       `(list* :fast-function
1034         (lambda (.pv-cell. .next-method-call. ,@args+rest-arg)
1035           (declare (ignorable .pv-cell. .next-method-call.))
1036           ,@outer-decls
1037           (macrolet ((pv-env ((pv calls pv-table-symbol pv-parameters)
1038                               &rest forms)
1039                        (declare (ignore pv-table-symbol pv-parameters))
1040                        `(let ((,pv (car .pv-cell.))
1041                               (,calls (cdr .pv-cell.)))
1042                           (declare ,(make-pv-type-declaration pv)
1043                                    ,(make-calls-type-declaration calls))
1044                           ,pv ,calls
1045                           ,@forms)))
1046             (fast-lexical-method-functions
1047              (,(car lmf-params) .next-method-call. ,req-args ,rest-arg
1048               ,@(cdddr lmf-params))
1049              ,@inner-decls
1050              ,@body)))
1051         ',initargs))))
1052
1053 ;;; Use arrays and hash tables and the fngen stuff to make this much
1054 ;;; better. It doesn't really matter, though, because a function
1055 ;;; returned by this will get called only when the user explicitly
1056 ;;; funcalls a result of method-function. BUT, this is needed to make
1057 ;;; early methods work.
1058 (defun method-function-from-fast-function (fmf)
1059   (declare (type function fmf))
1060   (let* ((method-function nil) (pv-table nil)
1061          (arg-info (method-function-get fmf ':arg-info))
1062          (nreq (car arg-info))
1063          (restp (cdr arg-info)))
1064     (setq method-function
1065           #'(lambda (method-args next-methods)
1066               (unless pv-table
1067                 (setq pv-table (method-function-pv-table fmf)))
1068               (let* ((pv-cell (when pv-table
1069                                 (get-method-function-pv-cell
1070                                  method-function method-args pv-table)))
1071                      (nm (car next-methods))
1072                      (nms (cdr next-methods))
1073                      (nmc (when nm
1074                             (make-method-call
1075                              :function (if (std-instance-p nm)
1076                                            (method-function nm)
1077                                            nm)
1078                              :call-method-args (list nms)))))
1079                 (if restp
1080                     (let* ((rest (nthcdr nreq method-args))
1081                            (args (ldiff method-args rest)))
1082                       (apply fmf pv-cell nmc (nconc args (list rest))))
1083                     (apply fmf pv-cell nmc method-args)))))
1084     (let* ((fname (method-function-get fmf :name))
1085            (name `(,(or (get (car fname) 'method-sym)
1086                         (setf (get (car fname) 'method-sym)
1087                               (let ((str (symbol-name (car fname))))
1088                                 (if (string= "FAST-" str :end2 5)
1089                                     (intern (subseq str 5) *pcl-package*)
1090                                     (car fname)))))
1091                     ,@(cdr fname))))
1092       (set-fun-name method-function name))
1093     (setf (method-function-get method-function :fast-function) fmf)
1094     method-function))
1095
1096 (defun get-method-function-pv-cell (method-function
1097                                     method-args
1098                                     &optional pv-table)
1099   (let ((pv-table (or pv-table (method-function-pv-table method-function))))
1100     (when pv-table
1101       (let ((pv-wrappers (pv-wrappers-from-all-args pv-table method-args)))
1102         (when pv-wrappers
1103           (pv-table-lookup pv-table pv-wrappers))))))
1104
1105 (defun pv-table-lookup-pv-args (pv-table &rest pv-parameters)
1106   (pv-table-lookup pv-table (pv-wrappers-from-pv-args pv-parameters)))
1107
1108 (defun pv-wrappers-from-pv-args (&rest args)
1109   (let* ((nkeys (length args))
1110          (pv-wrappers (make-list nkeys))
1111          w
1112          (w-t pv-wrappers))
1113     (dolist (arg args)
1114       (setq w (wrapper-of arg))
1115       (unless (eq t (wrapper-state w)) ; FIXME: should be INVALID-WRAPPER-P
1116         (setq w (check-wrapper-validity arg)))
1117       (setf (car w-t) w))
1118       (setq w-t (cdr w-t))
1119       (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))
1120       pv-wrappers))
1121
1122 (defun pv-wrappers-from-all-args (pv-table args)
1123   (let ((nkeys 0)
1124         (slot-name-lists (pv-table-slot-name-lists pv-table)))
1125     (dolist (sn slot-name-lists)
1126       (when sn (incf nkeys)))
1127     (let* ((pv-wrappers (make-list nkeys))
1128            (pv-w-t pv-wrappers))
1129       (dolist (sn slot-name-lists)
1130         (when sn
1131           (let* ((arg (car args))
1132                  (w (wrapper-of arg)))
1133             (unless w ; CAN-OPTIMIZE-ACCESS prevents this from happening.
1134               (error "error in PV-WRAPPERS-FROM-ALL-ARGS"))
1135             (setf (car pv-w-t) w)
1136             (setq pv-w-t (cdr pv-w-t))))
1137         (setq args (cdr args)))
1138       (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))
1139       pv-wrappers)))
1140
1141 (defun pv-wrappers-from-all-wrappers (pv-table wrappers)
1142   (let ((nkeys 0)
1143         (slot-name-lists (pv-table-slot-name-lists pv-table)))
1144     (dolist (sn slot-name-lists)
1145       (when sn (incf nkeys)))
1146     (let* ((pv-wrappers (make-list nkeys))
1147            (pv-w-t pv-wrappers))
1148       (dolist (sn slot-name-lists)
1149         (when sn
1150           (let ((w (car wrappers)))
1151             (unless w ; CAN-OPTIMIZE-ACCESS prevents this from happening.
1152               (error "error in PV-WRAPPERS-FROM-ALL-WRAPPERS"))
1153             (setf (car pv-w-t) w)
1154             (setq pv-w-t (cdr pv-w-t))))
1155         (setq wrappers (cdr wrappers)))
1156       (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))
1157       pv-wrappers)))