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