1 ;;;; permutation vectors
3 ;;;; This software is part of the SBCL system. See the README file for
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
12 ;;;; copyright information from original PCL sources:
14 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
15 ;;;; All rights reserved.
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
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
28 ;;;; Up to 1.0.9.24 SBCL used to have a sketched out implementation
29 ;;;; for optimizing GF calls inside method bodies using a PV approach,
30 ;;;; inherited from the original PCL. This was never completed, and
31 ;;;; was removed at that point to make the code easier to understand
34 ;;;; FIXME: It would be possible to optimize GF calls inside method
35 ;;;; bodies using permutation vectors: if all the arguments to the
36 ;;;; GF are specializers parameters, we can assign a permutation index
37 ;;;; to each such (GF . ARGS) tuple inside a method body, and use this
38 ;;;; to cache effective method functions.
40 (declaim (inline make-pv-table))
41 (defstruct (pv-table (:predicate pv-tablep)
43 (cache nil :type (or cache null))
44 (pv-size 0 :type fixnum)
45 (slot-name-lists nil :type list))
47 (defun make-pv-table-type-declaration (var)
48 `(type pv-table ,var))
50 ;;; Used for interning parts of SLOT-NAME-LISTS, as part of
51 ;;; PV-TABLE interning -- just to save space.
52 (defvar *slot-name-lists* (make-hash-table :test 'equal))
54 ;;; Used for interning PV-TABLES, keyed by the SLOT-NAME-LISTS
56 (defvar *pv-tables* (make-hash-table :test 'equal))
58 ;;; ...and one lock to rule them. Lock because for certain (rare)
59 ;;; cases this lock might be grabbed in the course of method dispatch
60 ;;; -- and mostly this is already under the *world-lock*
62 (sb-thread:make-mutex :name "pv table index lock"))
64 (defun intern-pv-table (&key slot-name-lists)
65 (flet ((intern-slot-names (slot-names)
66 ;; FIXME: NIL at the head of the list is a remnant from
67 ;; old purged code, that hasn't been quite cleaned up yet.
68 ;; ...but as long as we assume it is there, we may as well
70 (aver (not (car slot-names)))
71 (or (gethash slot-names *slot-name-lists*)
72 (setf (gethash slot-names *slot-name-lists*) slot-names)))
73 (%intern-pv-table (snl)
74 (or (gethash snl *pv-tables*)
75 (setf (gethash snl *pv-tables*)
76 (make-pv-table :slot-name-lists snl
77 :pv-size (* 2 (reduce #'+ snl
79 (length (cdr slots))))))))))
80 (sb-thread:with-mutex (*pv-lock*)
81 (%intern-pv-table (mapcar #'intern-slot-names slot-name-lists)))))
83 (defun use-standard-slot-access-p (class slot-name type)
84 (or (not (eq **boot-state** 'complete))
85 (and (standard-class-p class)
86 (let ((slotd (find-slot-definition class slot-name)))
88 (slot-accessor-std-p slotd type))))))
90 (defun slot-missing-info (class slot-name)
91 (flet ((missing (operation)
93 (slot-missing class object slot-name operation))))
95 :reader (missing 'slot-value)
96 :boundp (missing 'slot-boundp)
97 :writer (lambda (new-value object)
98 (slot-missing class object slot-name 'setf new-value)))))
100 (defun compute-pv (slot-name-lists wrappers)
101 (unless (listp wrappers)
102 (setq wrappers (list wrappers)))
104 (dolist (slot-names slot-name-lists)
106 (let* ((wrapper (pop wrappers))
107 (std-p (typep wrapper 'wrapper))
108 (class (wrapper-class* wrapper)))
109 (dolist (slot-name (cdr slot-names))
111 (or (find-slot-cell wrapper slot-name)
112 (cons nil (slot-missing-info class slot-name)))))
113 (push (when (and std-p (use-standard-slot-access-p class slot-name 'all))
117 (bug "No SLOT-INFO for ~S in ~S" slot-name class))
119 (let* ((n (length elements))
121 (loop for i from (1- n) downto 0
122 do (setf (svref pv i) (pop elements)))
125 (defun pv-table-lookup (pv-table pv-wrappers)
126 (let* ((slot-name-lists (pv-table-slot-name-lists pv-table))
127 (cache (or (pv-table-cache pv-table)
128 (setf (pv-table-cache pv-table)
129 (make-cache :key-count (- (length slot-name-lists)
130 (count nil slot-name-lists))
133 (multiple-value-bind (hitp value) (probe-cache cache pv-wrappers)
136 (let* ((pv (compute-pv slot-name-lists pv-wrappers))
137 (new-cache (fill-cache cache pv-wrappers pv)))
138 ;; This is safe: if another thread races us here the loser just
139 ;; misses the next time as well.
140 (unless (eq new-cache cache)
141 (setf (pv-table-cache pv-table) new-cache))
144 (defun make-pv-type-declaration (var)
145 `(type simple-vector ,var))
147 ;;; Sometimes we want to finalize if we can, but it's OK if
149 (defun try-finalize-inheritance (class)
150 (unless (typep class 'forward-referenced-class)
151 (when (every (lambda (super)
153 (class-finalized-p super)
154 (try-finalize-inheritance super)))
155 (class-direct-superclasses class))
156 (finalize-inheritance class)
159 (defun can-optimize-access (form required-parameters env)
160 (destructuring-bind (op var-form slot-name-form &optional new-value) form
161 (let ((type (ecase op
163 (set-slot-value 'writer)
164 (slot-boundp 'boundp)))
165 (var (extract-the var-form))
166 (slot-name (constant-form-value slot-name-form env)))
167 (when (and (symbolp var) (not (var-special-p var env)))
168 (let* ((rebound? (caddr (var-declaration '%variable-rebinding var env)))
169 (parameter-or-nil (car (memq (or rebound? var)
170 required-parameters))))
171 (when parameter-or-nil
172 (let* ((class-name (caddr (var-declaration '%class
175 (class (find-class class-name nil)))
176 (cond ((not (eq **boot-state** 'complete))
178 ((and class (not (class-finalized-p class)))
179 ;; The class itself is never forward-referenced
180 ;; here, but its superclasses may be.
181 (unless (try-finalize-inheritance class)
182 (when (boundp 'sb-c:*lexenv*)
183 (sb-c:compiler-notify
184 "~@<Cannot optimize slot access, inheritance of ~S is not ~
185 yet finaliable due to forward-referenced superclasses:~
189 (when (and class-name (not (eq class-name t)))
190 (when (not (and class
191 (memq *the-class-structure-object*
192 (class-precedence-list class))))
194 (values (cons parameter-or-nil (or class class-name))
198 ;;; Check whether the binding of the named variable is modified in the
200 (defun parameter-modified-p (parameter-name env)
201 (let ((modified-variables (%macroexpand '%parameter-binding-modified env)))
202 (memq parameter-name modified-variables)))
204 (defun optimize-slot-value (form slots required-parameters env)
205 (multiple-value-bind (sparameter slot-name)
206 (can-optimize-access form required-parameters env)
208 (let ((optimized-form
209 (optimize-instance-access slots :read sparameter
211 ;; We don't return the optimized form directly, since there's
212 ;; still a chance that we'll find out later on that the
213 ;; optimization should not have been done, for example due to
214 ;; the walker encountering a SETQ on SPARAMETER later on in
215 ;; the body [ see for example clos.impure.lisp test with :name
216 ;; ((:setq :method-parameter) slot-value)) ]. Instead we defer
217 ;; the decision until the compiler macroexpands
218 ;; OPTIMIZED-SLOT-VALUE.
220 ;; Note that we must still call OPTIMIZE-INSTANCE-ACCESS at
221 ;; this point (instead of when expanding
222 ;; OPTIMIZED-SLOT-VALUE), since it mutates the structure of
223 ;; SLOTS. If that mutation isn't done during the walking,
224 ;; MAKE-METHOD-LAMBDA-INTERNAL won't wrap a correct PV-BINDING
225 ;; form around the body, and compilation will fail. -- JES,
227 `(optimized-slot-value ,form ,(car sparameter) ,optimized-form))
228 `(accessor-slot-value ,@(cdr form)))))
230 (defmacro optimized-slot-value (form parameter-name optimized-form
232 ;; Either use OPTIMIZED-FORM or fall back to the safe
233 ;; ACCESSOR-SLOT-VALUE.
234 (if (parameter-modified-p parameter-name env)
235 `(accessor-slot-value ,@(cdr form))
238 (defun optimize-set-slot-value (form slots required-parameters env)
239 (multiple-value-bind (sparameter slot-name new-value)
240 (can-optimize-access form required-parameters env)
242 (let ((optimized-form
243 (optimize-instance-access slots :write sparameter
244 slot-name new-value (safe-code-p env))))
245 ;; See OPTIMIZE-SLOT-VALUE
246 `(optimized-set-slot-value ,form ,(car sparameter) ,optimized-form))
247 `(accessor-set-slot-value ,@(cdr form)))))
249 (defmacro optimized-set-slot-value (form parameter-name optimized-form
251 (cond ((parameter-modified-p parameter-name env)
252 ;; ACCESSOR-SET-SLOT-VALUE doesn't do type-checking,
253 ;; so we need to use SAFE-SET-SLOT-VALUE.
254 (if (safe-code-p env)
255 `(safe-set-slot-value ,@(cdr form)))
256 `(accessor-set-slot-value ,@(cdr form)))
260 (defun optimize-slot-boundp (form slots required-parameters env)
261 (multiple-value-bind (sparameter slot-name)
262 (can-optimize-access form required-parameters env)
264 (let ((optimized-form
265 (optimize-instance-access slots :boundp sparameter
267 ;; See OPTIMIZE-SLOT-VALUE
268 `(optimized-slot-boundp ,form ,(car sparameter) ,optimized-form))
269 `(accessor-slot-boundp ,@(cdr form)))))
271 (defmacro optimized-slot-boundp (form parameter-name optimized-form
273 (if (parameter-modified-p parameter-name env)
274 `(accessor-slot-boundp ,@(cdr form))
277 ;;; The SLOTS argument is an alist, the CAR of each entry is the name
278 ;;; of a required parameter to the function. The alist is in order, so
279 ;;; the position of an entry in the alist corresponds to the
280 ;;; argument's position in the lambda list.
281 (defun optimize-instance-access (slots read/write sparameter slot-name
282 new-value &optional safep)
283 (let ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
284 (parameter (if (consp sparameter) (car sparameter) sparameter)))
285 (if (and (eq **boot-state** 'complete)
287 (memq *the-class-structure-object* (class-precedence-list class)))
288 (let ((slotd (find-slot-definition class slot-name)))
291 `(,(slot-definition-defstruct-accessor-symbol slotd) ,parameter))
293 `(setf (,(slot-definition-defstruct-accessor-symbol slotd)
298 (let* ((parameter-entry (assq parameter slots))
299 (slot-entry (assq slot-name (cdr parameter-entry)))
300 (position (posq parameter-entry slots))
301 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
302 (unless parameter-entry
303 (bug "slot optimization bewilderment: O-I-A"))
305 (setq slot-entry (list slot-name))
306 (push slot-entry (cdr parameter-entry)))
307 (push pv-offset-form (cdr slot-entry))
310 `(instance-read ,pv-offset-form ,parameter ,position
311 ',slot-name ',class))
313 `(let ((.new-value. ,new-value))
314 (instance-write ,pv-offset-form ,parameter ,position
315 ',slot-name ',class .new-value. ,safep)))
317 `(instance-boundp ,pv-offset-form ,parameter ,position
318 ',slot-name ',class)))))))
320 (define-walker-template pv-offset) ; These forms get munged by mutate slots.
321 (defmacro pv-offset (arg) arg)
322 (define-walker-template instance-accessor-parameter)
323 (defmacro instance-accessor-parameter (x) x)
325 ;;; It is safe for these two functions to be wrong. They just try to
326 ;;; guess what the most likely case will be.
327 (defun generate-fast-class-slot-access-p (class-form slot-name-form)
328 (let ((class (and (constantp class-form) (constant-form-value class-form)))
329 (slot-name (and (constantp slot-name-form)
330 (constant-form-value slot-name-form))))
331 (and (eq **boot-state** 'complete)
332 (standard-class-p class)
333 (not (eq class *the-class-t*)) ; shouldn't happen, though.
334 (let ((slotd (find-slot-definition class slot-name)))
335 (and slotd (eq :class (slot-definition-allocation slotd)))))))
337 (defun constant-value-or-nil (form)
338 (and (constantp form) (constant-form-value form)))
340 (defun slot-access-strategy (class slot-name type &optional conservative)
341 ;; CONSERVATIVE means we should assume custom access pattern even if
342 ;; there are no custom accessors defined if the metaclass is non-standard.
344 ;; This is needed because DEFCLASS generates accessor methods before possible
345 ;; SLOT-VALUE-USING-CLASS methods are defined, which causes them to take
346 ;; the slow path unless we make the conservative assumption here.
347 (if (eq **boot-state** 'complete)
350 ;; Conditions, structures, and classes for which FIND-CLASS
351 ;; doesn't return them yet.
352 ;; FIXME: surely we can get faster accesses for structures?
353 (not (standard-class-p class))
354 ;; Should not happen... (FIXME: assert instead?)
355 (eq class *the-class-t*)
356 (not (class-finalized-p class))
358 (not (setf slotd (find-slot-definition class slot-name))))
360 ((and (slot-accessor-std-p slotd type)
361 (or (not conservative) (eq *the-class-standard-class* (class-of class))))
370 (defmacro instance-read (pv-offset parameter position slot-name class)
371 (ecase (slot-access-strategy (constant-value-or-nil class)
372 (constant-value-or-nil slot-name)
375 `(instance-read-standard
376 .pv. ,(slot-vector-symbol position)
377 ,pv-offset (accessor-slot-value ,parameter ,slot-name)
378 ,(if (generate-fast-class-slot-access-p class slot-name)
381 `(instance-read-custom .pv. ,pv-offset ,parameter))
383 `(accessor-slot-value ,parameter ,slot-name))))
385 (defmacro instance-read-standard (pv slots pv-offset default &optional kind)
386 (unless (member kind '(nil :instance :class))
387 (error "illegal kind argument to ~S: ~S" 'instance-read-standard kind))
388 (let* ((index (gensym))
390 `(locally (declare #.*optimize-speed*)
391 (let ((,index (svref ,pv ,pv-offset))
392 (,slots (truly-the simple-vector ,slots)))
393 (setq ,value (typecase ,index
394 ;; FIXME: the line marked by KLUDGE below (and
395 ;; the analogous spot in
396 ;; INSTANCE-WRITE-STANDARD) is there purely to
397 ;; suppress a type mismatch warning that
398 ;; propagates through to user code.
399 ;; Presumably SLOTS at this point can never
400 ;; actually be NIL, but the compiler seems to
401 ;; think it could, so we put this here to shut
402 ;; it up. (see also mail Rudi Schlatte
403 ;; sbcl-devel 2003-09-21) -- CSR, 2003-11-30
404 ,@(when (or (null kind) (eq kind :instance))
406 (clos-slots-ref ,slots ,index))))
407 ,@(when (or (null kind) (eq kind :class))
408 `((cons (cdr ,index))))
411 (if (eq ,value +slot-unbound+)
415 (defmacro instance-read-custom (pv pv-offset parameter)
416 `(locally (declare #.*optimize-speed*)
417 (funcall (slot-info-reader (svref ,pv (1+ ,pv-offset))) ,parameter)))
419 ;;;; (SETF SLOT-VALUE)
421 (defmacro instance-write (pv-offset parameter position slot-name class new-value
422 &optional check-type-p)
423 (ecase (slot-access-strategy (constant-value-or-nil class)
424 (constant-value-or-nil slot-name)
427 `(instance-write-standard
428 .pv. ,(slot-vector-symbol position)
429 ,pv-offset ,new-value
430 ;; KLUDGE: .GOOD-NEW-VALUE. is type-checked by the time this form
431 ;; is executed (if it is executed).
432 (accessor-set-slot-value ,parameter ,slot-name .good-new-value.)
433 ,(if (generate-fast-class-slot-access-p class slot-name)
437 `(instance-write-custom .pv. ,pv-offset ,parameter ,new-value))
440 ;; FIXME: We don't want this here. If it's _possible_ the fast path
441 ;; is applicable, we want to use it as well.
442 `(safe-set-slot-value ,parameter ,slot-name ,new-value)
443 `(accessor-set-slot-value ,parameter ,slot-name ,new-value)))))
445 (defmacro instance-write-standard (pv slots pv-offset new-value default
446 &optional kind safep)
447 (unless (member kind '(nil :instance :class))
448 (error "illegal kind argument to ~S: ~S" 'instance-write-standard kind))
449 (let* ((index (gensym))
452 `(let ((.typecheckfun. (slot-info-typecheck (svref ,pv (1+ ,pv-offset)))))
453 (declare (type (or function null) .typecheckfun.))
455 (funcall .typecheckfun. ,new-value)
458 `(locally (declare #.*optimize-speed*)
459 (let ((.good-new-value. ,new-value-form)
460 (,index (svref ,pv ,pv-offset)))
462 ,@(when (or (null kind) (eq kind :instance))
463 `((fixnum (and ,slots
464 (setf (clos-slots-ref ,slots ,index)
465 .good-new-value.)))))
466 ,@(when (or (null kind) (eq kind :class))
467 `((cons (setf (cdr ,index) .good-new-value.))))
470 (defmacro instance-write-custom (pv pv-offset parameter new-value)
471 `(locally (declare #.*optimize-speed*)
472 (funcall (slot-info-writer (svref ,pv (1+ ,pv-offset))) ,new-value ,parameter)))
476 (defmacro instance-boundp (pv-offset parameter position slot-name class)
477 (ecase (slot-access-strategy (constant-value-or-nil class)
478 (constant-value-or-nil slot-name)
481 `(instance-boundp-standard
482 .pv. ,(slot-vector-symbol position)
483 ,pv-offset (accessor-slot-boundp ,parameter ,slot-name)
484 ,(if (generate-fast-class-slot-access-p class slot-name)
487 `(instance-boundp-custom .pv. ,pv-offset ,parameter))
489 `(accessor-slot-boundp ,parameter ,slot-name))))
491 (defmacro instance-boundp-standard (pv slots pv-offset default
493 (unless (member kind '(nil :instance :class))
494 (error "illegal kind argument to ~S: ~S" 'instance-boundp-standard kind))
495 (let* ((index (gensym)))
496 `(locally (declare #.*optimize-speed*)
497 (let ((,index (svref ,pv ,pv-offset)))
499 ,@(when (or (null kind) (eq kind :instance))
500 `((fixnum (not (and ,slots
501 (eq (clos-slots-ref ,slots ,index)
503 ,@(when (or (null kind) (eq kind :class))
504 `((cons (not (eq (cdr ,index) +slot-unbound+)))))
507 (defmacro instance-boundp-custom (pv pv-offset parameter)
508 `(locally (declare #.*optimize-speed*)
509 (funcall (slot-info-boundp (svref ,pv (1+ ,pv-offset))) ,parameter)))
511 ;;; This magic function has quite a job to do indeed.
513 ;;; The careful reader will recall that <slots> contains all of the
514 ;;; optimized slot access forms produced by OPTIMIZE-INSTANCE-ACCESS.
515 ;;; Each of these is a call to either INSTANCE-READ or INSTANCE-WRITE.
517 ;;; At the time these calls were produced, the first argument was
518 ;;; specified as the symbol .PV-OFFSET.; what we have to do now is
519 ;;; convert those pv-offset arguments into the actual number that is
520 ;;; the correct offset into the pv.
522 ;;; But first, oh but first, we sort <slots> a bit so that for each
523 ;;; argument we have the slots in alphabetical order. This
524 ;;; canonicalizes the PV-TABLE's a bit and will hopefully lead to
525 ;;; having fewer PV's floating around. Even if the gain is only
526 ;;; modest, it costs nothing.
527 (defun slot-name-lists-from-slots (slots)
528 (let ((slots (mutate-slots slots)))
529 (let* ((slot-name-lists
530 (mapcar (lambda (parameter-entry)
531 (cons nil (mapcar #'car (cdr parameter-entry))))
533 (mapcar (lambda (r+snl)
534 (when (or (car r+snl) (cdr r+snl))
538 (defun mutate-slots (slots)
539 (let ((sorted-slots (sort-slots slots))
541 (dolist (parameter-entry sorted-slots)
542 (dolist (slot-entry (cdr parameter-entry))
544 (dolist (form (cdr slot-entry))
545 (setf (cadr form) pv-offset))
546 ;; Count one more for the slot we use for SLOT-INFO.
550 (defun symbol-pkg-name (sym)
551 (let ((pkg (symbol-package sym)))
552 (if pkg (package-name pkg) "")))
554 ;;; FIXME: Because of the existence of UNINTERN and RENAME-PACKAGE,
555 ;;; the part of this ordering which is based on SYMBOL-PKG-NAME is not
556 ;;; stable. This ordering is only used in to
557 ;;; SLOT-NAME-LISTS-FROM-SLOTS, where it serves to "canonicalize the
558 ;;; PV-TABLE's a bit and will hopefully lead to having fewer PV's
559 ;;; floating around", so it sounds as though the instability won't
560 ;;; actually lead to bugs, just small inefficiency. But still, it
561 ;;; would be better to reimplement this function as a comparison based
563 ;;; * stable comparison
564 ;;; * smaller code (here, and in being able to discard SYMBOL-PKG-NAME)
566 (defun symbol-lessp (a b)
567 (if (eq (symbol-package a)
569 (string-lessp (symbol-name a)
571 (string-lessp (symbol-pkg-name a)
572 (symbol-pkg-name b))))
574 (defun symbol-or-cons-lessp (a b)
577 (symbol (symbol-lessp a b))
581 (cons (if (eq (car a) (car b))
582 (symbol-or-cons-lessp (cdr a) (cdr b))
583 (symbol-or-cons-lessp (car a) (car b))))))))
585 (defun sort-slots (slots)
586 (mapcar (lambda (parameter-entry)
587 (cons (car parameter-entry)
588 (sort (cdr parameter-entry) ;slot entries
589 #'symbol-or-cons-lessp
594 ;;;; This needs to work in terms of metatypes and also needs to work
595 ;;;; for automatically generated reader and writer functions.
596 ;;;; Automatically generated reader and writer functions use this
599 (defmacro pv-binding ((required-parameters slot-name-lists pv-table-form)
601 (let (slot-vars pv-parameters)
602 (loop for slots in slot-name-lists
603 for required-parameter in required-parameters
606 (push required-parameter pv-parameters)
607 (push (slot-vector-symbol i) slot-vars)))
608 `(pv-binding1 (,pv-table-form
609 ,(nreverse pv-parameters) ,(nreverse slot-vars))
612 (defmacro pv-binding1 ((pv-table-form pv-parameters slot-vars)
614 `(pv-env (,pv-table-form ,pv-parameters)
615 (let (,@(mapcar (lambda (slot-var p) `(,slot-var (get-slots-or-nil ,p)))
616 slot-vars pv-parameters))
617 (declare (ignorable ,@(mapcar #'identity slot-vars)))
620 ;;; This will only be visible in PV-ENV when the default MAKE-METHOD-LAMBDA is
622 (define-symbol-macro pv-env-environment overridden)
624 (defmacro pv-env (&environment env
625 (pv-table-form pv-parameters)
627 ;; Decide which expansion to use based on the state of the PV-ENV-ENVIRONMENT
629 (if (eq (macroexpand 'pv-env-environment env) 'default)
630 `(locally (declare (simple-vector .pv.))
632 `(let* ((.pv-table. ,pv-table-form)
633 (.pv. (pv-table-lookup-pv-args .pv-table. ,@pv-parameters)))
634 (declare ,(make-pv-type-declaration '.pv.))
637 (defun split-declarations (body args req-args cnm-p parameters-setqd)
638 (let ((inner-decls nil)
644 (setq decl (car body))
645 (unless (and (consp decl) (eq (car decl) 'declare))
647 (dolist (form (cdr decl))
649 (let* ((name (car form)))
650 (cond ((eq '%class name)
651 (push `(declare ,form) inner-decls))
652 ((or (member name '(ignore ignorable special dynamic-extent type))
653 (info :type :kind name))
657 (head (if (eq 'type name)
658 (list name (pop tail))
661 (if (member var args :test #'eq)
662 ;; Quietly remove IGNORE declarations on
663 ;; args when a next-method is involved, to
664 ;; prevent compiler warnings about ignored
666 (unless (and (eq 'ignore name)
667 (member var req-args :test #'eq)
668 (or cnm-p (member var parameters-setqd)))
672 (push `(declare (,@head ,@outers)) outer-decls))
674 (push `(declare (,@head ,@inners)) inner-decls))))
676 ;; All other declarations are not variable declarations,
677 ;; so they become outer declarations.
678 (push `(declare ,form) outer-decls))))))
679 (setq body (cdr body)))
680 (values outer-decls inner-decls body)))
682 ;;; Convert a lambda expression containing a SB-PCL::%METHOD-NAME
683 ;;; declaration (which is a naming style internal to PCL) into an
684 ;;; SB-INT:NAMED-LAMBDA expression (which is a naming style used
685 ;;; throughout SBCL, understood by the main compiler); or if there's
686 ;;; no SB-PCL::%METHOD-NAME declaration, then just return the original
687 ;;; lambda expression.
688 (defun name-method-lambda (method-lambda)
689 (let ((method-name *method-name*))
691 `(named-lambda (slow-method ,@method-name) ,@(rest method-lambda))
694 (defun make-method-initargs-form-internal (method-lambda initargs env)
695 (declare (ignore env))
696 (let (method-lambda-args
697 lmf ; becomes body of function
699 (if (not (and (= 3 (length method-lambda))
700 (= 2 (length (setq method-lambda-args (cadr method-lambda))))
701 (consp (setq lmf (third method-lambda)))
702 (eq 'simple-lexical-method-functions (car lmf))
703 (eq (car method-lambda-args)
704 (cadr (setq lmf-params (cadr lmf))))
705 (eq (cadr method-lambda-args)
706 (caddr lmf-params))))
707 `(list* :function ,(name-method-lambda method-lambda)
709 (let* ((lambda-list (car lmf-params))
713 (dolist (arg lambda-list)
714 (when (member arg '(&optional &rest &key))
721 (setq args (nreverse args))
722 (setf (getf (getf initargs 'plist) :arg-info) (cons nreq restp))
723 (make-method-initargs-form-internal1
724 initargs (cddr lmf) args lmf-params restp)))))
726 (defun lambda-list-parameter-names (lambda-list)
727 ;; Given a valid lambda list, extract the parameter names.
728 (loop for x in lambda-list
730 do (unless (member x lambda-list-keywords :test #'eq)
732 (let ((name (car x)))
734 ;; ... ((:BAR FOO) 1)
735 (push (second name) res)
739 (let ((name-p (cddr x)))
741 (push (car name-p) res))))
744 finally (return res)))
746 (defun make-method-initargs-form-internal1
747 (initargs body req-args lmf-params restp)
748 (let* (;; The lambda-list of the method, minus specifiers
749 (lambda-list (car lmf-params))
750 ;; Names of the parameters that will be in the outermost lambda-list
751 ;; (and whose bound declarations thus need to be in OUTER-DECLS).
752 (outer-parameters req-args)
753 ;; The lambda-list used by BIND-ARGS
754 (bind-list lambda-list)
755 (parameters-setqd (getf (cdr lmf-params) :parameters-setqd))
756 (auxp (member '&aux bind-list))
757 (call-next-method-p (getf (cdr lmf-params) :call-next-method-p)))
758 ;; Try to use the normal function call machinery instead of BIND-ARGS
759 ;; binding the arguments, unless:
760 (unless (or ;; If all arguments are required, BIND-ARGS will be a no-op
762 (and (not restp) (not auxp))
763 ;; CALL-NEXT-METHOD wants to use BIND-ARGS, and needs a
764 ;; list of all non-required arguments.
766 (setf ;; We don't want a binding for .REST-ARG.
768 ;; Get all the parameters for declaration parsing
769 outer-parameters (lambda-list-parameter-names lambda-list)
770 ;; Ensure that BIND-ARGS won't do anything (since
771 ;; BIND-LIST won't contain any non-required parameters,
772 ;; and REQ-ARGS will be of an equal length). We still want
773 ;; to pass BIND-LIST to FAST-LEXICAL-METHOD-FUNCTIONS so
774 ;; that BIND-FAST-LEXICAL-METHOD-FUNCTIONS can take care
775 ;; of rebinding SETQd required arguments around the method
778 (multiple-value-bind (outer-decls inner-decls body-sans-decls)
780 body outer-parameters req-args call-next-method-p parameters-setqd)
781 (let* ((rest-arg (when restp
783 (fmf-lambda-list (if rest-arg
784 (append req-args (list '&rest rest-arg))
785 (if call-next-method-p
790 (let* ((fmf (,(if *method-name* 'named-lambda 'lambda)
791 ,@(when *method-name*
793 (list `(fast-method ,@*method-name*)))
794 ;; The lambda-list of the FMF
795 (.pv. .next-method-call. ,@fmf-lambda-list)
796 ;; body of the function
797 (declare (ignorable .pv. .next-method-call.)
798 (disable-package-locks pv-env-environment))
800 (symbol-macrolet ((pv-env-environment default))
801 (fast-lexical-method-functions
802 (,bind-list .next-method-call. ,req-args ,rest-arg
803 ,@(cdddr lmf-params))
805 ,@body-sans-decls))))
806 (mf (%make-method-function fmf nil)))
807 (set-funcallable-instance-function
808 mf (method-function-from-fast-function fmf ',(getf initargs 'plist)))
812 ;;; Use arrays and hash tables and the fngen stuff to make this much
813 ;;; better. It doesn't really matter, though, because a function
814 ;;; returned by this will get called only when the user explicitly
815 ;;; funcalls a result of method-function. BUT, this is needed to make
816 ;;; early methods work.
817 (defun method-function-from-fast-function (fmf plist)
818 (declare (type function fmf))
819 (let* ((method-function nil)
820 (snl (getf plist :slot-name-lists))
822 (intern-pv-table :slot-name-lists snl)))
823 (arg-info (getf plist :arg-info))
824 (nreq (car arg-info))
825 (restp (cdr arg-info)))
826 (setq method-function
827 (lambda (method-args next-methods)
828 (let* ((pv (when pv-table
829 (get-pv method-args pv-table)))
830 (nm (car next-methods))
831 (nms (cdr next-methods))
834 :function (if (std-instance-p nm)
837 :call-method-args (list nms)))))
838 (apply fmf pv nmc method-args))))
839 ;; FIXME: this looks dangerous.
840 (let* ((fname (%fun-name fmf)))
841 (when (and fname (eq (car fname) 'fast-method))
842 (set-fun-name method-function (cons 'slow-method (cdr fname)))))
845 ;;; this is similar to the above, only not quite. Only called when
846 ;;; the MOP is heavily involved. Not quite parallel to
847 ;;; METHOD-FUNCTION-FROM-FAST-METHOD-FUNCTION, because we can close
848 ;;; over the actual PV-CELL in this case.
849 (defun method-function-from-fast-method-call (fmc)
850 (let* ((fmf (fast-method-call-function fmc))
851 (pv (fast-method-call-pv fmc))
852 (arg-info (fast-method-call-arg-info fmc))
853 (nreq (car arg-info))
854 (restp (cdr arg-info)))
855 (lambda (method-args next-methods)
856 (let* ((nm (car next-methods))
857 (nms (cdr next-methods))
860 :function (if (std-instance-p nm)
863 :call-method-args (list nms)))))
864 (apply fmf pv nmc method-args)))))
866 (defun get-pv (method-args pv-table)
867 (let ((pv-wrappers (pv-wrappers-from-all-args pv-table method-args)))
869 (pv-table-lookup pv-table pv-wrappers))))
871 (defun pv-table-lookup-pv-args (pv-table &rest pv-parameters)
872 (pv-table-lookup pv-table (pv-wrappers-from-pv-args pv-parameters)))
874 (defun pv-wrappers-from-pv-args (&rest args)
875 (loop for arg in args
876 collect (valid-wrapper-of arg)))
878 (defun pv-wrappers-from-all-args (pv-table args)
879 (loop for snl in (pv-table-slot-name-lists pv-table)
882 collect (valid-wrapper-of arg)))
884 ;;; Return the subset of WRAPPERS which is used in the cache
886 (defun pv-wrappers-from-all-wrappers (pv-table wrappers)
887 (loop for snl in (pv-table-slot-name-lists pv-table) and w in wrappers