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