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. Spinlock 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 *big-compiler-lock*.
62 (sb-thread::make-spinlock :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-spinlock (*pv-lock*)
81 (%intern-pv-table (mapcar #'intern-slot-names slot-name-lists)))))
83 (defun optimize-slot-value-by-class-p (class slot-name type)
84 (or (not (eq *boot-state* 'complete))
85 (let ((slotd (find-slot-definition class slot-name)))
87 (slot-accessor-std-p slotd type)))))
89 (defun compute-slot-location-for-pv (slot-name wrapper class)
90 (when (optimize-slot-value-by-class-p class slot-name 'all)
91 (car (find-slot-cell wrapper slot-name))))
93 (defun compute-slot-typecheckfun-for-pv (slot-name wrapper class)
94 (when (optimize-slot-value-by-class-p class slot-name 'all)
95 (cadr (find-slot-cell wrapper slot-name))))
97 (defun compute-pv (slot-name-lists wrappers)
98 (unless (listp wrappers)
99 (setq wrappers (list wrappers)))
101 (dolist (slot-names slot-name-lists)
103 (let* ((wrapper (pop wrappers))
104 (std-p (typep wrapper 'wrapper))
105 (class (wrapper-class* wrapper)))
106 (dolist (slot-name (cdr slot-names))
108 (compute-slot-location-for-pv slot-name wrapper class))
111 (compute-slot-typecheckfun-for-pv slot-name wrapper class))
113 (let* ((n (length elements))
115 (loop for i from (1- n) downto 0
116 do (setf (svref pv i) (pop elements)))
119 (defun pv-table-lookup (pv-table pv-wrappers)
120 (let* ((slot-name-lists (pv-table-slot-name-lists pv-table))
121 (cache (or (pv-table-cache pv-table)
122 (setf (pv-table-cache pv-table)
123 (make-cache :key-count (- (length slot-name-lists)
124 (count nil slot-name-lists))
127 (multiple-value-bind (hitp value) (probe-cache cache pv-wrappers)
130 (let* ((pv (compute-pv slot-name-lists pv-wrappers))
131 (new-cache (fill-cache cache pv-wrappers pv)))
132 ;; This is safe: if another thread races us here the loser just
133 ;; misses the next time as well.
134 (unless (eq new-cache cache)
135 (setf (pv-table-cache pv-table) new-cache))
138 (defun make-pv-type-declaration (var)
139 `(type simple-vector ,var))
141 (defun can-optimize-access (form required-parameters env)
142 (destructuring-bind (op var-form slot-name-form &optional new-value) form
143 (let ((type (ecase op
145 (set-slot-value 'writer)
146 (slot-boundp 'boundp)))
147 (var (extract-the var-form))
148 (slot-name (constant-form-value slot-name-form env)))
150 (let* ((rebound? (caddr (var-declaration '%variable-rebinding var env)))
151 (parameter-or-nil (car (memq (or rebound? var)
152 required-parameters))))
153 (when parameter-or-nil
154 (let* ((class-name (caddr (var-declaration '%class
157 (class (find-class class-name nil)))
158 (when (or (not (eq *boot-state* 'complete))
159 (and class (not (class-finalized-p class))))
161 (when (and class-name (not (eq class-name t)))
162 (when (or (null type)
164 (memq *the-class-structure-object*
165 (class-precedence-list class))))
166 (optimize-slot-value-by-class-p class slot-name type))
167 (values (cons parameter-or-nil (or class class-name))
171 ;;; Check whether the binding of the named variable is modified in the
173 (defun parameter-modified-p (parameter-name env)
174 (let ((modified-variables (macroexpand '%parameter-binding-modified env)))
175 (memq parameter-name modified-variables)))
177 (defun optimize-slot-value (form slots required-parameters env)
178 (multiple-value-bind (sparameter slot-name)
179 (can-optimize-access form required-parameters env)
181 (let ((optimized-form
182 (optimize-instance-access slots :read sparameter
184 ;; We don't return the optimized form directly, since there's
185 ;; still a chance that we'll find out later on that the
186 ;; optimization should not have been done, for example due to
187 ;; the walker encountering a SETQ on SPARAMETER later on in
188 ;; the body [ see for example clos.impure.lisp test with :name
189 ;; ((:setq :method-parameter) slot-value)) ]. Instead we defer
190 ;; the decision until the compiler macroexpands
191 ;; OPTIMIZED-SLOT-VALUE.
193 ;; Note that we must still call OPTIMIZE-INSTANCE-ACCESS at
194 ;; this point (instead of when expanding
195 ;; OPTIMIZED-SLOT-VALUE), since it mutates the structure of
196 ;; SLOTS. If that mutation isn't done during the walking,
197 ;; MAKE-METHOD-LAMBDA-INTERNAL won't wrap a correct PV-BINDING
198 ;; form around the body, and compilation will fail. -- JES,
200 `(optimized-slot-value ,form ,(car sparameter) ,optimized-form))
201 `(accessor-slot-value ,@(cdr form)))))
203 (defmacro optimized-slot-value (form parameter-name optimized-form
205 ;; Either use OPTIMIZED-FORM or fall back to the safe
206 ;; ACCESSOR-SLOT-VALUE.
207 (if (parameter-modified-p parameter-name env)
208 `(accessor-slot-value ,@(cdr form))
211 (defun optimize-set-slot-value (form slots required-parameters env)
212 (multiple-value-bind (sparameter slot-name new-value)
213 (can-optimize-access form required-parameters env)
215 (let ((optimized-form
216 (optimize-instance-access slots :write sparameter
217 slot-name new-value (safe-code-p env))))
218 ;; See OPTIMIZE-SLOT-VALUE
219 `(optimized-set-slot-value ,form ,(car sparameter) ,optimized-form))
220 `(accessor-set-slot-value ,@(cdr form)))))
222 (defmacro optimized-set-slot-value (form parameter-name optimized-form
224 (cond ((parameter-modified-p parameter-name env)
225 ;; ACCESSOR-SET-SLOT-VALUE doesn't do type-checking,
226 ;; so we need to use SAFE-SET-SLOT-VALUE.
227 (if (safe-code-p env)
228 `(safe-set-slot-value ,@(cdr form)))
229 `(accessor-set-slot-value ,@(cdr form)))
233 (defun optimize-slot-boundp (form slots required-parameters env)
234 (multiple-value-bind (sparameter slot-name)
235 (can-optimize-access form required-parameters env)
237 (let ((optimized-form
238 (optimize-instance-access slots :boundp sparameter
240 ;; See OPTIMIZE-SLOT-VALUE
241 `(optimized-slot-boundp ,form ,(car sparameter) ,optimized-form))
242 `(accessor-slot-boundp ,@(cdr form)))))
244 (defmacro optimized-slot-boundp (form parameter-name optimized-form
246 (if (parameter-modified-p parameter-name env)
247 `(accessor-slot-boundp ,@(cdr form))
250 ;;; The SLOTS argument is an alist, the CAR of each entry is the name
251 ;;; of a required parameter to the function. The alist is in order, so
252 ;;; the position of an entry in the alist corresponds to the
253 ;;; argument's position in the lambda list.
254 (defun optimize-instance-access (slots read/write sparameter slot-name
255 new-value &optional safep)
256 (let ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
257 (parameter (if (consp sparameter) (car sparameter) sparameter)))
258 (if (and (eq *boot-state* 'complete)
260 (memq *the-class-structure-object* (class-precedence-list class)))
261 (let ((slotd (find-slot-definition class slot-name)))
264 `(,(slot-definition-defstruct-accessor-symbol slotd) ,parameter))
266 `(setf (,(slot-definition-defstruct-accessor-symbol slotd)
271 (let* ((parameter-entry (assq parameter slots))
272 (slot-entry (assq slot-name (cdr parameter-entry)))
273 (position (posq parameter-entry slots))
274 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
275 (unless parameter-entry
276 (bug "slot optimization bewilderment: O-I-A"))
278 (setq slot-entry (list slot-name))
279 (push slot-entry (cdr parameter-entry)))
280 (push pv-offset-form (cdr slot-entry))
283 `(instance-read ,pv-offset-form ,parameter ,position
284 ',slot-name ',class))
286 `(let ((.new-value. ,new-value))
287 (instance-write ,pv-offset-form ,parameter ,position
288 ',slot-name ',class .new-value. ,safep)))
290 `(instance-boundp ,pv-offset-form ,parameter ,position
291 ',slot-name ',class)))))))
293 (define-walker-template pv-offset) ; These forms get munged by mutate slots.
294 (defmacro pv-offset (arg) arg)
295 (define-walker-template instance-accessor-parameter)
296 (defmacro instance-accessor-parameter (x) x)
298 ;;; It is safe for these two functions to be wrong. They just try to
299 ;;; guess what the most likely case will be.
300 (defun generate-fast-class-slot-access-p (class-form slot-name-form)
301 (let ((class (and (constantp class-form) (constant-form-value class-form)))
302 (slot-name (and (constantp slot-name-form)
303 (constant-form-value slot-name-form))))
304 (and (eq *boot-state* 'complete)
305 (standard-class-p class)
306 (not (eq class *the-class-t*)) ; shouldn't happen, though.
307 (let ((slotd (find-slot-definition class slot-name)))
308 (and slotd (eq :class (slot-definition-allocation slotd)))))))
310 (defun skip-fast-slot-access-p (class-form slot-name-form type)
311 (let ((class (and (constantp class-form) (constant-form-value class-form)))
312 (slot-name (and (constantp slot-name-form)
313 (constant-form-value slot-name-form))))
314 (and (eq *boot-state* 'complete)
315 (standard-class-p class)
316 (not (eq class *the-class-t*)) ; shouldn't happen, though.
317 ;; FIXME: Is this really right? "Don't skip if there is
318 ;; no slot definition."
319 (let ((slotd (find-slot-definition class slot-name)))
321 (not (slot-accessor-std-p slotd type)))))))
323 (defmacro instance-read-internal (pv slots pv-offset default &optional kind)
324 (unless (member kind '(nil :instance :class))
325 (error "illegal kind argument to ~S: ~S" 'instance-read-internal kind))
326 (let* ((index (gensym))
328 `(locally (declare #.*optimize-speed*)
329 (let ((,index (svref ,pv ,pv-offset)))
330 (setq ,value (typecase ,index
331 ;; FIXME: the line marked by KLUDGE below (and
332 ;; the analogous spot in
333 ;; INSTANCE-WRITE-INTERNAL) is there purely to
334 ;; suppress a type mismatch warning that
335 ;; propagates through to user code.
336 ;; Presumably SLOTS at this point can never
337 ;; actually be NIL, but the compiler seems to
338 ;; think it could, so we put this here to shut
339 ;; it up. (see also mail Rudi Schlatte
340 ;; sbcl-devel 2003-09-21) -- CSR, 2003-11-30
341 ,@(when (or (null kind) (eq kind :instance))
344 (clos-slots-ref ,slots ,index)))))
345 ,@(when (or (null kind) (eq kind :class))
346 `((cons (cdr ,index))))
348 (if (eq ,value +slot-unbound+)
352 (defmacro instance-read (pv-offset parameter position slot-name class)
353 (if (skip-fast-slot-access-p class slot-name 'reader)
354 `(accessor-slot-value ,parameter ,slot-name)
355 `(instance-read-internal .pv. ,(slot-vector-symbol position)
356 ,pv-offset (accessor-slot-value ,parameter ,slot-name)
357 ,(if (generate-fast-class-slot-access-p class slot-name)
360 (defmacro instance-write-internal (pv slots pv-offset new-value default
361 &optional kind safep)
362 (unless (member kind '(nil :instance :class))
363 (error "illegal kind argument to ~S: ~S" 'instance-write-internal kind))
364 (let* ((index (gensym))
367 `(let ((.typecheckfun. (svref ,pv (1+ ,pv-offset))))
368 (declare (type (or function null) .typecheckfun.))
370 (funcall .typecheckfun. ,new-value)
373 `(locally (declare #.*optimize-speed*)
374 (let ((.good-new-value. ,new-value-form)
375 (,index (svref ,pv ,pv-offset)))
377 ,@(when (or (null kind) (eq kind :instance))
378 `((fixnum (and ,slots
379 (setf (clos-slots-ref ,slots ,index)
380 .good-new-value.)))))
381 ,@(when (or (null kind) (eq kind :class))
382 `((cons (setf (cdr ,index) .good-new-value.))))
385 (defmacro instance-write (pv-offset parameter position slot-name class new-value
386 &optional check-type-p)
387 (if (skip-fast-slot-access-p class slot-name 'writer)
389 ;; FIXME: We don't want this here. If it's _possible_ the fast path
390 ;; is applicable, we wan to use it as well.
391 `(safe-set-slot-value ,parameter ,slot-name ,new-value)
392 `(accessor-set-slot-value ,parameter ,slot-name ,new-value))
393 `(instance-write-internal
394 .pv. ,(slot-vector-symbol position)
395 ,pv-offset ,new-value
396 ;; KLUDGE: .GOOD-NEW-VALUE. is type-checked by the time this form
397 ;; is executed (if it is executed).
398 (accessor-set-slot-value ,parameter ,slot-name .good-new-value.)
399 ,(if (generate-fast-class-slot-access-p class slot-name)
403 (defmacro instance-boundp-internal (pv slots pv-offset default
405 (unless (member kind '(nil :instance :class))
406 (error "illegal kind argument to ~S: ~S" 'instance-boundp-internal kind))
407 (let* ((index (gensym)))
408 `(locally (declare #.*optimize-speed*)
409 (let ((,index (svref ,pv ,pv-offset)))
411 ,@(when (or (null kind) (eq kind :instance))
412 `((fixnum (not (and ,slots
413 (eq (clos-slots-ref ,slots ,index)
415 ,@(when (or (null kind) (eq kind :class))
416 `((cons (not (eq (cdr ,index) +slot-unbound+)))))
419 (defmacro instance-boundp (pv-offset parameter position slot-name class)
420 (if (skip-fast-slot-access-p class slot-name 'boundp)
421 `(accessor-slot-boundp ,parameter ,slot-name)
422 `(instance-boundp-internal .pv. ,(slot-vector-symbol position)
423 ,pv-offset (accessor-slot-boundp ,parameter ,slot-name)
424 ,(if (generate-fast-class-slot-access-p class slot-name)
427 ;;; This magic function has quite a job to do indeed.
429 ;;; The careful reader will recall that <slots> contains all of the
430 ;;; optimized slot access forms produced by OPTIMIZE-INSTANCE-ACCESS.
431 ;;; Each of these is a call to either INSTANCE-READ or INSTANCE-WRITE.
433 ;;; At the time these calls were produced, the first argument was
434 ;;; specified as the symbol .PV-OFFSET.; what we have to do now is
435 ;;; convert those pv-offset arguments into the actual number that is
436 ;;; the correct offset into the pv.
438 ;;; But first, oh but first, we sort <slots> a bit so that for each
439 ;;; argument we have the slots in alphabetical order. This
440 ;;; canonicalizes the PV-TABLE's a bit and will hopefully lead to
441 ;;; having fewer PV's floating around. Even if the gain is only
442 ;;; modest, it costs nothing.
443 (defun slot-name-lists-from-slots (slots)
444 (let ((slots (mutate-slots slots)))
445 (let* ((slot-name-lists
446 (mapcar (lambda (parameter-entry)
447 (cons nil (mapcar #'car (cdr parameter-entry))))
449 (mapcar (lambda (r+snl)
450 (when (or (car r+snl) (cdr r+snl))
454 (defun mutate-slots (slots)
455 (let ((sorted-slots (sort-slots slots))
457 (dolist (parameter-entry sorted-slots)
458 (dolist (slot-entry (cdr parameter-entry))
460 (dolist (form (cdr slot-entry))
461 (setf (cadr form) pv-offset))
462 ;; Count one more for the slot we use for typecheckfun.
466 (defun symbol-pkg-name (sym)
467 (let ((pkg (symbol-package sym)))
468 (if pkg (package-name pkg) "")))
470 ;;; FIXME: Because of the existence of UNINTERN and RENAME-PACKAGE,
471 ;;; the part of this ordering which is based on SYMBOL-PKG-NAME is not
472 ;;; stable. This ordering is only used in to
473 ;;; SLOT-NAME-LISTS-FROM-SLOTS, where it serves to "canonicalize the
474 ;;; PV-TABLE's a bit and will hopefully lead to having fewer PV's
475 ;;; floating around", so it sounds as though the instability won't
476 ;;; actually lead to bugs, just small inefficiency. But still, it
477 ;;; would be better to reimplement this function as a comparison based
479 ;;; * stable comparison
480 ;;; * smaller code (here, and in being able to discard SYMBOL-PKG-NAME)
482 (defun symbol-lessp (a b)
483 (if (eq (symbol-package a)
485 (string-lessp (symbol-name a)
487 (string-lessp (symbol-pkg-name a)
488 (symbol-pkg-name b))))
490 (defun symbol-or-cons-lessp (a b)
493 (symbol (symbol-lessp a b))
497 (cons (if (eq (car a) (car b))
498 (symbol-or-cons-lessp (cdr a) (cdr b))
499 (symbol-or-cons-lessp (car a) (car b))))))))
501 (defun sort-slots (slots)
502 (mapcar (lambda (parameter-entry)
503 (cons (car parameter-entry)
504 (sort (cdr parameter-entry) ;slot entries
505 #'symbol-or-cons-lessp
510 ;;;; This needs to work in terms of metatypes and also needs to work
511 ;;;; for automatically generated reader and writer functions.
512 ;;;; Automatically generated reader and writer functions use this
515 (defmacro pv-binding ((required-parameters slot-name-lists pv-table-form)
517 (let (slot-vars pv-parameters)
518 (loop for slots in slot-name-lists
519 for required-parameter in required-parameters
522 (push required-parameter pv-parameters)
523 (push (slot-vector-symbol i) slot-vars)))
524 `(pv-binding1 (,pv-table-form
525 ,(nreverse pv-parameters) ,(nreverse slot-vars))
528 (defmacro pv-binding1 ((pv-table-form pv-parameters slot-vars)
530 `(pv-env (,pv-table-form ,pv-parameters)
531 (let (,@(mapcar (lambda (slot-var p) `(,slot-var (get-slots-or-nil ,p)))
532 slot-vars pv-parameters))
533 (declare (ignorable ,@(mapcar #'identity slot-vars)))
536 ;;; This will only be visible in PV-ENV when the default MAKE-METHOD-LAMBDA is
538 (define-symbol-macro pv-env-environment overridden)
540 (defmacro pv-env (&environment env
541 (pv-table-form pv-parameters)
543 ;; Decide which expansion to use based on the state of the PV-ENV-ENVIRONMENT
545 (if (eq (macroexpand 'pv-env-environment env) 'default)
546 `(locally (declare (simple-vector .pv.))
548 `(let* ((.pv-table. ,pv-table-form)
549 (.pv. (pv-table-lookup-pv-args .pv-table. ,@pv-parameters)))
550 (declare ,(make-pv-type-declaration '.pv.))
553 (defvar *non-var-declarations*
554 ;; FIXME: VALUES was in this list, conditionalized with #+CMU, but I
555 ;; don't *think* CMU CL had, or SBCL has, VALUES declarations. If
556 ;; SBCL doesn't have 'em, VALUES should probably be removed from
567 (defvar *var-declarations-with-arg*
571 (defvar *var-declarations-without-arg*
573 ignorable special dynamic-extent
574 ;; FIXME: Possibly this entire list and variable could go away.
575 ;; If not, certainly we should remove all these built-in typenames
576 ;; from the list, and replace them with a test for "is it a type
577 ;; name?" (CLTL1 allowed only built-in type names as declarations,
578 ;; but ANSI CL allows any type name as a declaration.)
579 array atom base-char bignum bit bit-vector character compiled-function
580 complex cons double-float extended-char
581 fixnum float function hash-table integer
582 keyword list long-float nil null number package pathname random-state ratio
583 rational readtable sequence short-float signed-byte simple-array
584 simple-bit-vector simple-string simple-vector single-float standard-char
585 stream string symbol t unsigned-byte vector))
587 (defun split-declarations (body args maybe-reads-params-p)
588 (let ((inner-decls nil)
591 (loop (when (null body) (return nil))
592 (setq decl (car body))
593 (unless (and (consp decl)
594 (eq (car decl) 'declare))
596 (dolist (form (cdr decl))
598 (let ((declaration-name (car form)))
599 (if (member declaration-name *non-var-declarations*)
600 (push `(declare ,form) outer-decls)
602 (member declaration-name
603 *var-declarations-with-arg*))
605 (member declaration-name
606 *var-declarations-without-arg*))
607 (dname (list (pop form)))
608 (inners nil) (outers nil))
609 (unless (or arg-p non-arg-p)
610 ;; FIXME: This warning, and perhaps the
611 ;; various *VAR-DECLARATIONS-FOO* and/or
612 ;; *NON-VAR-DECLARATIONS* variables,
613 ;; could probably go away now that we're not
614 ;; trying to be portable between different
615 ;; CLTL1 hosts the way PCL was. (Note that to
616 ;; do this right, we need to be able to handle
617 ;; user-defined (DECLAIM (DECLARATION FOO))
619 (warn "The declaration ~S is not understood by ~S.~@
620 Please put ~S on one of the lists ~S,~%~S, or~%~S.~@
621 (Assuming it is a variable declaration without argument)."
622 declaration-name 'split-declarations
624 '*non-var-declarations*
625 '*var-declarations-with-arg*
626 '*var-declarations-without-arg*)
627 (push declaration-name *var-declarations-without-arg*))
629 (setq dname (append dname (list (pop form)))))
631 (%class (push `(declare (,@dname ,@form)) inner-decls))
634 (if (member var args)
635 ;; Quietly remove IGNORE declarations
636 ;; on args when a next-method is
637 ;; involved, to prevent compiler
638 ;; warnings about ignored args being
640 (unless (and maybe-reads-params-p
641 (eq (car dname) 'ignore))
645 (push `(declare (,@dname ,@outers)) outer-decls))
648 `(declare (,@dname ,@inners))
650 (setq body (cdr body)))
651 (values outer-decls inner-decls body)))
653 ;;; Pull a name out of the %METHOD-NAME declaration in the function
654 ;;; body given, or return NIL if no %METHOD-NAME declaration is found.
655 (defun body-method-name (body)
656 (multiple-value-bind (real-body declarations documentation)
658 (declare (ignore real-body documentation))
659 (let ((name-decl (get-declaration '%method-name declarations)))
661 (destructuring-bind (name) name-decl
664 ;;; Convert a lambda expression containing a SB-PCL::%METHOD-NAME
665 ;;; declaration (which is a naming style internal to PCL) into an
666 ;;; SB-INT:NAMED-LAMBDA expression (which is a naming style used
667 ;;; throughout SBCL, understood by the main compiler); or if there's
668 ;;; no SB-PCL::%METHOD-NAME declaration, then just return the original
669 ;;; lambda expression.
670 (defun name-method-lambda (method-lambda)
671 (let ((method-name (body-method-name (cddr method-lambda))))
673 `(named-lambda (slow-method ,method-name) ,(rest method-lambda))
676 (defun make-method-initargs-form-internal (method-lambda initargs env)
677 (declare (ignore env))
678 (let (method-lambda-args
679 lmf ; becomes body of function
681 (if (not (and (= 3 (length method-lambda))
682 (= 2 (length (setq method-lambda-args (cadr method-lambda))))
683 (consp (setq lmf (third method-lambda)))
684 (eq 'simple-lexical-method-functions (car lmf))
685 (eq (car method-lambda-args)
686 (cadr (setq lmf-params (cadr lmf))))
687 (eq (cadr method-lambda-args)
688 (caddr lmf-params))))
689 `(list* :function ,(name-method-lambda method-lambda)
691 (let* ((lambda-list (car lmf-params))
695 (dolist (arg lambda-list)
696 (when (member arg '(&optional &rest &key))
703 (setq args (nreverse args))
704 (setf (getf (getf initargs 'plist) :arg-info) (cons nreq restp))
705 (make-method-initargs-form-internal1
706 initargs (cddr lmf) args lmf-params restp)))))
708 (defun lambda-list-parameter-names (lambda-list)
709 ;; Given a valid lambda list, extract the parameter names.
710 (loop for x in lambda-list
712 do (unless (member x lambda-list-keywords)
714 (let ((name (car x)))
716 ;; ... ((:BAR FOO) 1)
717 (push (second name) res)
721 (let ((name-p (cddr x)))
723 (push (car name-p) res))))
726 finally (return res)))
728 (defun make-method-initargs-form-internal1
729 (initargs body req-args lmf-params restp)
730 (let* (;; The lambda-list of the method, minus specifiers
731 (lambda-list (car lmf-params))
732 ;; Names of the parameters that will be in the outermost lambda-list
733 ;; (and whose bound declarations thus need to be in OUTER-DECLS).
734 (outer-parameters req-args)
735 ;; The lambda-list used by BIND-ARGS
736 (bind-list lambda-list)
737 (setq-p (getf (cdr lmf-params) :setq-p))
738 (auxp (member '&aux bind-list))
739 (call-next-method-p (getf (cdr lmf-params) :call-next-method-p)))
740 ;; Try to use the normal function call machinery instead of BIND-ARGS
741 ;; binding the arguments, unless:
742 (unless (or ;; If all arguments are required, BIND-ARGS will be a no-op
744 (and (not restp) (not auxp))
745 ;; CALL-NEXT-METHOD wants to use BIND-ARGS, and needs a
746 ;; list of all non-required arguments.
748 (setf ;; We don't want a binding for .REST-ARG.
750 ;; Get all the parameters for declaration parsing
751 outer-parameters (lambda-list-parameter-names lambda-list)
752 ;; Ensure that BIND-ARGS won't do anything (since
753 ;; BIND-LIST won't contain any non-required parameters,
754 ;; and REQ-ARGS will be of an equal length). We still want
755 ;; to pass BIND-LIST to FAST-LEXICAL-METHOD-FUNCTIONS so
756 ;; that BIND-FAST-LEXICAL-METHOD-FUNCTIONS can take care
757 ;; of rebinding SETQd required arguments around the method
760 (multiple-value-bind (outer-decls inner-decls body-sans-decls)
762 body outer-parameters (or call-next-method-p setq-p))
763 (let* ((rest-arg (when restp
765 (fmf-lambda-list (if rest-arg
766 (append req-args (list '&rest rest-arg))
767 (if call-next-method-p
772 (let* ((fmf (,(if (body-method-name body) 'named-lambda 'lambda)
773 ,@(when (body-method-name body)
775 (list (cons 'fast-method (body-method-name body))))
776 ;; The lambda-list of the FMF
777 (.pv. .next-method-call. ,@fmf-lambda-list)
778 ;; body of the function
779 (declare (ignorable .pv. .next-method-call.)
780 (disable-package-locks pv-env-environment))
782 (symbol-macrolet ((pv-env-environment default))
783 (fast-lexical-method-functions
784 (,bind-list .next-method-call. ,req-args ,rest-arg
785 ,@(cdddr lmf-params))
787 ,@body-sans-decls))))
788 (mf (%make-method-function fmf nil)))
789 (set-funcallable-instance-function
790 mf (method-function-from-fast-function fmf ',(getf initargs 'plist)))
794 ;;; Use arrays and hash tables and the fngen stuff to make this much
795 ;;; better. It doesn't really matter, though, because a function
796 ;;; returned by this will get called only when the user explicitly
797 ;;; funcalls a result of method-function. BUT, this is needed to make
798 ;;; early methods work.
799 (defun method-function-from-fast-function (fmf plist)
800 (declare (type function fmf))
801 (let* ((method-function nil)
802 (snl (getf plist :slot-name-lists))
804 (intern-pv-table :slot-name-lists snl)))
805 (arg-info (getf plist :arg-info))
806 (nreq (car arg-info))
807 (restp (cdr arg-info)))
808 (setq method-function
809 (lambda (method-args next-methods)
810 (let* ((pv (when pv-table
811 (get-pv method-args pv-table)))
812 (nm (car next-methods))
813 (nms (cdr next-methods))
816 :function (if (std-instance-p nm)
819 :call-method-args (list nms)))))
820 (apply fmf pv nmc method-args))))
821 ;; FIXME: this looks dangerous.
822 (let* ((fname (%fun-name fmf)))
823 (when (and fname (eq (car fname) 'fast-method))
824 (set-fun-name method-function (cons 'slow-method (cdr fname)))))
827 ;;; this is similar to the above, only not quite. Only called when
828 ;;; the MOP is heavily involved. Not quite parallel to
829 ;;; METHOD-FUNCTION-FROM-FAST-METHOD-FUNCTION, because we can close
830 ;;; over the actual PV-CELL in this case.
831 (defun method-function-from-fast-method-call (fmc)
832 (let* ((fmf (fast-method-call-function fmc))
833 (pv (fast-method-call-pv fmc))
834 (arg-info (fast-method-call-arg-info fmc))
835 (nreq (car arg-info))
836 (restp (cdr arg-info)))
837 (lambda (method-args next-methods)
838 (let* ((nm (car next-methods))
839 (nms (cdr next-methods))
842 :function (if (std-instance-p nm)
845 :call-method-args (list nms)))))
846 (apply fmf pv nmc method-args)))))
848 (defun get-pv (method-args pv-table)
849 (let ((pv-wrappers (pv-wrappers-from-all-args pv-table method-args)))
851 (pv-table-lookup pv-table pv-wrappers))))
853 (defun pv-table-lookup-pv-args (pv-table &rest pv-parameters)
854 (pv-table-lookup pv-table (pv-wrappers-from-pv-args pv-parameters)))
856 (defun pv-wrappers-from-pv-args (&rest args)
857 (loop for arg in args
858 collect (valid-wrapper-of arg)))
860 (defun pv-wrappers-from-all-args (pv-table args)
861 (loop for snl in (pv-table-slot-name-lists pv-table)
864 collect (valid-wrapper-of arg)))
866 ;;; Return the subset of WRAPPERS which is used in the cache
868 (defun pv-wrappers-from-all-wrappers (pv-table wrappers)
869 (loop for snl in (pv-table-slot-name-lists pv-table) and w in wrappers