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