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