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