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
)
92 :reader
(lambda (object)
93 (values (slot-missing class object slot-name
'slot-value
)))
94 :boundp
(lambda (object)
95 (and (slot-missing class object slot-name
'slot-boundp
) t
))
96 :writer
(lambda (new-value object
)
97 (slot-missing class object slot-name
'setf new-value
)
100 (defun compute-pv (slot-name-lists wrappers
)
101 (let ((wrappers (ensure-list wrappers
))
103 (dolist (slot-names slot-name-lists
)
105 (let* ((wrapper (pop wrappers
))
106 (std-p (layout-for-std-class-p wrapper
))
107 (class (wrapper-class* wrapper
)))
108 (dolist (slot-name (cdr slot-names
))
110 (or (find-slot-cell wrapper slot-name
)
111 (cons nil
(slot-missing-info class slot-name
)))))
112 (push (when (and std-p
(use-standard-slot-access-p class slot-name
'all
))
116 (bug "No SLOT-INFO for ~S in ~S" slot-name class
))
118 (let* ((n (length elements
))
120 (loop for i from
(1- n
) downto
0
121 do
(setf (svref pv i
) (pop elements
)))
124 (defun pv-table-lookup (pv-table pv-wrappers
)
125 (let* ((slot-name-lists (pv-table-slot-name-lists pv-table
))
126 (cache (or (pv-table-cache pv-table
)
127 (setf (pv-table-cache pv-table
)
128 (make-cache :key-count
(- (length slot-name-lists
)
129 (count nil slot-name-lists
))
132 (multiple-value-bind (hitp value
) (probe-cache cache pv-wrappers
)
135 (let* ((pv (compute-pv slot-name-lists pv-wrappers
))
136 (new-cache (fill-cache cache pv-wrappers pv
)))
137 ;; This is safe: if another thread races us here the loser just
138 ;; misses the next time as well.
139 (unless (eq new-cache cache
)
140 (setf (pv-table-cache pv-table
) new-cache
))
143 (defun make-pv-type-declaration (var)
144 `(type simple-vector
,var
))
146 ;;; Sometimes we want to finalize if we can, but it's OK if
148 (defun try-finalize-inheritance (class)
149 (unless (typep class
'forward-referenced-class
)
150 (when (every (lambda (super)
152 (class-finalized-p super
)
153 (try-finalize-inheritance super
)))
154 (class-direct-superclasses class
))
155 (finalize-inheritance class
)
158 (declaim (ftype (sfunction (class) class
) ensure-class-finalized
))
159 (defun ensure-class-finalized (class)
160 (unless (class-finalized-p class
)
161 (finalize-inheritance class
))
164 (defun can-optimize-access (form required-parameters env
)
165 (destructuring-bind (op var-form slot-name-form
&optional new-value
) form
166 (let ((type (ecase op
168 (set-slot-value 'writer
)
169 (slot-boundp 'boundp
)))
170 (var (extract-the var-form
))
171 (slot-name (constant-form-value slot-name-form env
)))
172 (when (and (symbolp var
) (not (var-special-p var env
)))
173 (let* ((rebound?
(caddr (var-declaration '%variable-rebinding var env
)))
174 (parameter-or-nil (car (memq (or rebound? var
)
175 required-parameters
))))
176 (when parameter-or-nil
177 (let* ((class-name (caddr (var-declaration '%class
180 (class (find-class class-name nil
)))
181 (cond ((not (eq **boot-state
** 'complete
))
183 ((and class
(not (class-finalized-p class
)))
184 ;; The class itself is never forward-referenced
185 ;; here, but its superclasses may be.
186 (unless (try-finalize-inheritance class
)
187 (when (boundp 'sb-c
:*lexenv
*)
188 (sb-c:compiler-notify
189 "~@<Cannot optimize slot access, inheritance of ~S is not ~
190 yet finalizable due to forward-referenced superclasses:~
194 (when (and class-name
(not (eq class-name t
)))
195 (when (not (and class
196 (memq *the-class-structure-object
*
197 (class-precedence-list class
))))
199 (values (cons parameter-or-nil
(or class class-name
))
203 ;;; Check whether the binding of the named variable is modified in the
205 (defun parameter-modified-p (parameter-name env
)
206 (let ((modified-variables (%macroexpand
'%parameter-binding-modified env
)))
207 (memq parameter-name modified-variables
)))
209 (defun optimize-slot-value (form slots required-parameters env
)
210 (multiple-value-bind (sparameter slot-name
)
211 (can-optimize-access form required-parameters env
)
213 (let ((optimized-form
214 (optimize-instance-access slots
:read sparameter
216 ;; We don't return the optimized form directly, since there's
217 ;; still a chance that we'll find out later on that the
218 ;; optimization should not have been done, for example due to
219 ;; the walker encountering a SETQ on SPARAMETER later on in
220 ;; the body [ see for example clos.impure.lisp test with :name
221 ;; ((:setq :method-parameter) slot-value)) ]. Instead we defer
222 ;; the decision until the compiler macroexpands
223 ;; OPTIMIZED-SLOT-VALUE.
225 ;; Note that we must still call OPTIMIZE-INSTANCE-ACCESS at
226 ;; this point (instead of when expanding
227 ;; OPTIMIZED-SLOT-VALUE), since it mutates the structure of
228 ;; SLOTS. If that mutation isn't done during the walking,
229 ;; MAKE-METHOD-LAMBDA-INTERNAL won't wrap a correct PV-BINDING
230 ;; form around the body, and compilation will fail. -- JES,
232 `(optimized-slot-value ,form
,(car sparameter
) ,optimized-form
))
233 `(accessor-slot-value ,@(cdr form
)))))
235 (defmacro optimized-slot-value
(form parameter-name optimized-form
237 ;; Either use OPTIMIZED-FORM or fall back to the safe
238 ;; ACCESSOR-SLOT-VALUE.
239 (if (parameter-modified-p parameter-name env
)
240 `(accessor-slot-value ,@(cdr form
))
243 (defun optimize-set-slot-value (form slots required-parameters env
)
244 (multiple-value-bind (sparameter slot-name new-value
)
245 (can-optimize-access form required-parameters env
)
247 (let ((optimized-form
248 (optimize-instance-access slots
:write sparameter
249 slot-name new-value
(safe-code-p env
))))
250 ;; See OPTIMIZE-SLOT-VALUE
251 `(optimized-set-slot-value ,form
,(car sparameter
) ,optimized-form
))
252 `(accessor-set-slot-value ,@(cdr form
)))))
254 (defmacro optimized-set-slot-value
(form parameter-name optimized-form
256 (cond ((parameter-modified-p parameter-name env
)
257 ;; ACCESSOR-SET-SLOT-VALUE doesn't do type-checking,
258 ;; so we need to use SAFE-SET-SLOT-VALUE.
259 (if (safe-code-p env
)
260 `(safe-set-slot-value ,@(cdr form
)))
261 `(accessor-set-slot-value ,@(cdr form
)))
265 (defun optimize-slot-boundp (form slots required-parameters env
)
266 (multiple-value-bind (sparameter slot-name
)
267 (can-optimize-access form required-parameters env
)
269 (let ((optimized-form
270 (optimize-instance-access slots
:boundp sparameter
272 ;; See OPTIMIZE-SLOT-VALUE
273 `(optimized-slot-boundp ,form
,(car sparameter
) ,optimized-form
))
274 `(accessor-slot-boundp ,@(cdr form
)))))
276 (defmacro optimized-slot-boundp
(form parameter-name optimized-form
278 (if (parameter-modified-p parameter-name env
)
279 `(accessor-slot-boundp ,@(cdr form
))
282 ;;; The SLOTS argument is an alist, the CAR of each entry is the name
283 ;;; of a required parameter to the function. The alist is in order, so
284 ;;; the position of an entry in the alist corresponds to the
285 ;;; argument's position in the lambda list.
286 (defun optimize-instance-access (slots read
/write sparameter slot-name
287 new-value
&optional safep
)
288 (let ((class (if (consp sparameter
) (cdr sparameter
) *the-class-t
*))
289 (parameter (if (consp sparameter
) (car sparameter
) sparameter
)))
290 (if (and (eq **boot-state
** 'complete
)
292 (memq *the-class-structure-object
* (class-precedence-list class
)))
293 (let ((slotd (find-slot-definition class slot-name
)))
296 `(,(slot-definition-defstruct-accessor-symbol slotd
) ,parameter
))
298 `(setf (,(slot-definition-defstruct-accessor-symbol slotd
)
303 (let* ((parameter-entry (assq parameter slots
))
304 (slot-entry (assq slot-name
(cdr parameter-entry
)))
305 (position (posq parameter-entry slots
))
306 (pv-offset-form (list 'pv-offset
''.PV-OFFSET.
)))
307 (unless parameter-entry
308 (bug "slot optimization bewilderment: O-I-A"))
310 (setq slot-entry
(list slot-name
))
311 (push slot-entry
(cdr parameter-entry
)))
312 (push pv-offset-form
(cdr slot-entry
))
315 `(instance-read ,pv-offset-form
,parameter
,position
316 ',slot-name
',class
))
318 `(let ((.new-value.
,new-value
))
319 (instance-write ,pv-offset-form
,parameter
,position
320 ',slot-name
',class .new-value.
,safep
)))
322 `(instance-boundp ,pv-offset-form
,parameter
,position
323 ',slot-name
',class
)))))))
325 (define-walker-template pv-offset
) ; These forms get munged by mutate slots.
326 (defmacro pv-offset
(arg) arg
)
327 (define-walker-template instance-accessor-parameter
)
328 (defmacro instance-accessor-parameter
(x) x
)
330 ;;; It is safe for these two functions to be wrong. They just try to
331 ;;; guess what the most likely case will be.
332 (defun generate-fast-class-slot-access-p (class-form slot-name-form
)
333 (let ((class (and (constantp class-form
) (constant-form-value class-form
)))
334 (slot-name (and (constantp slot-name-form
)
335 (constant-form-value slot-name-form
))))
336 (and (eq **boot-state
** 'complete
)
337 (standard-class-p class
)
338 (not (eq class
*the-class-t
*)) ; shouldn't happen, though.
339 (let ((slotd (find-slot-definition class slot-name
)))
340 (and slotd
(eq :class
(slot-definition-allocation slotd
)))))))
342 (defun constant-value-or-nil (form)
343 (and (constantp form
) (constant-form-value form
)))
345 (defun slot-access-strategy (class slot-name type
&optional conservative
)
346 ;; CONSERVATIVE means we should assume custom access pattern even if
347 ;; there are no custom accessors defined if the metaclass is non-standard.
349 ;; This is needed because DEFCLASS generates accessor methods before possible
350 ;; SLOT-VALUE-USING-CLASS methods are defined, which causes them to take
351 ;; the slow path unless we make the conservative assumption here.
352 (if (eq **boot-state
** 'complete
)
355 ;; Conditions, structures, and classes for which FIND-CLASS
356 ;; doesn't return them yet.
357 ;; FIXME: surely we can get faster accesses for structures?
358 (not (standard-class-p class
))
359 ;; Should not happen... (FIXME: assert instead?)
360 (eq class
*the-class-t
*)
361 (not (class-finalized-p class
))
363 (not (setf slotd
(find-slot-definition class slot-name
))))
365 ((and (slot-accessor-std-p slotd type
)
366 (or (not conservative
) (eq *the-class-standard-class
* (class-of class
))))
375 (defmacro instance-read
(pv-offset parameter position slot-name class
)
376 (ecase (slot-access-strategy (constant-value-or-nil class
)
377 (constant-value-or-nil slot-name
)
380 `(instance-read-standard
381 .pv.
,(slot-vector-symbol position
)
382 ,pv-offset
(accessor-slot-value ,parameter
,slot-name
)
383 ,(if (generate-fast-class-slot-access-p class slot-name
)
386 `(instance-read-custom .pv.
,pv-offset
,parameter
))
388 `(accessor-slot-value ,parameter
,slot-name
))))
390 (defmacro instance-read-standard
(pv slots pv-offset default
&optional kind
)
391 (unless (member kind
'(nil :instance
:class
))
392 (error "illegal kind argument to ~S: ~S" 'instance-read-standard kind
))
393 (let* ((index (gensym))
395 `(locally (declare #.
*optimize-speed
*)
396 (let ((,index
(svref ,pv
,pv-offset
))
397 (,slots
(truly-the simple-vector
,slots
)))
398 (setq ,value
(typecase ,index
399 ;; FIXME: the line marked by KLUDGE below (and
400 ;; the analogous spot in
401 ;; INSTANCE-WRITE-STANDARD) is there purely to
402 ;; suppress a type mismatch warning that
403 ;; propagates through to user code.
404 ;; Presumably SLOTS at this point can never
405 ;; actually be NIL, but the compiler seems to
406 ;; think it could, so we put this here to shut
407 ;; it up. (see also mail Rudi Schlatte
408 ;; sbcl-devel 2003-09-21) -- CSR, 2003-11-30
409 ,@(when (or (null kind
) (eq kind
:instance
))
411 (clos-slots-ref ,slots
,index
))))
412 ,@(when (or (null kind
) (eq kind
:class
))
413 `((cons (cdr ,index
))))
416 (if (eq ,value
+slot-unbound
+)
420 (defmacro instance-read-custom
(pv pv-offset parameter
)
421 `(locally (declare #.
*optimize-speed
*)
422 (funcall (slot-info-reader (svref ,pv
(1+ ,pv-offset
))) ,parameter
)))
424 ;;;; (SETF SLOT-VALUE)
426 (defmacro instance-write
(pv-offset parameter position slot-name class new-value
427 &optional check-type-p
)
428 (ecase (slot-access-strategy (constant-value-or-nil class
)
429 (constant-value-or-nil slot-name
)
432 `(instance-write-standard
433 .pv.
,(slot-vector-symbol position
)
434 ,pv-offset
,new-value
435 ;; KLUDGE: .GOOD-NEW-VALUE. is type-checked by the time this form
436 ;; is executed (if it is executed).
437 (accessor-set-slot-value ,parameter
,slot-name .good-new-value.
)
438 ,(if (generate-fast-class-slot-access-p class slot-name
)
442 `(instance-write-custom .pv.
,pv-offset
,parameter
,new-value
))
445 ;; FIXME: We don't want this here. If it's _possible_ the fast path
446 ;; is applicable, we want to use it as well.
447 `(safe-set-slot-value ,parameter
,slot-name
,new-value
)
448 `(accessor-set-slot-value ,parameter
,slot-name
,new-value
)))))
450 (defmacro instance-write-standard
(pv slots pv-offset new-value default
451 &optional kind safep
)
452 (unless (member kind
'(nil :instance
:class
))
453 (error "illegal kind argument to ~S: ~S" 'instance-write-standard kind
))
454 (let* ((index (gensym))
457 `(let ((.typecheckfun.
(slot-info-typecheck (svref ,pv
(1+ ,pv-offset
)))))
458 (declare (type (or function null
) .typecheckfun.
))
460 (funcall .typecheckfun.
,new-value
)
463 `(locally (declare #.
*optimize-speed
*)
464 (let ((.good-new-value.
,new-value-form
)
465 (,index
(svref ,pv
,pv-offset
)))
467 ,@(when (or (null kind
) (eq kind
:instance
))
468 `((fixnum (and ,slots
469 (setf (clos-slots-ref ,slots
,index
)
470 .good-new-value.
)))))
471 ,@(when (or (null kind
) (eq kind
:class
))
472 `((cons (setf (cdr ,index
) .good-new-value.
))))
475 (defmacro instance-write-custom
(pv pv-offset parameter new-value
)
476 `(locally (declare #.
*optimize-speed
*)
477 (funcall (slot-info-writer (svref ,pv
(1+ ,pv-offset
))) ,new-value
,parameter
)))
481 (defmacro instance-boundp
(pv-offset parameter position slot-name class
)
482 (ecase (slot-access-strategy (constant-value-or-nil class
)
483 (constant-value-or-nil slot-name
)
486 `(instance-boundp-standard
487 .pv.
,(slot-vector-symbol position
)
488 ,pv-offset
(accessor-slot-boundp ,parameter
,slot-name
)
489 ,(if (generate-fast-class-slot-access-p class slot-name
)
492 `(instance-boundp-custom .pv.
,pv-offset
,parameter
))
494 `(accessor-slot-boundp ,parameter
,slot-name
))))
496 (defmacro instance-boundp-standard
(pv slots pv-offset default
498 (unless (member kind
'(nil :instance
:class
))
499 (error "illegal kind argument to ~S: ~S" 'instance-boundp-standard kind
))
500 (let* ((index (gensym)))
501 `(locally (declare #.
*optimize-speed
*)
502 (let ((,index
(svref ,pv
,pv-offset
)))
504 ,@(when (or (null kind
) (eq kind
:instance
))
505 `((fixnum (not (and ,slots
506 (eq (clos-slots-ref ,slots
,index
)
508 ,@(when (or (null kind
) (eq kind
:class
))
509 `((cons (not (eq (cdr ,index
) +slot-unbound
+)))))
512 (defmacro instance-boundp-custom
(pv pv-offset parameter
)
513 `(locally (declare #.
*optimize-speed
*)
514 (funcall (slot-info-boundp (svref ,pv
(1+ ,pv-offset
))) ,parameter
)))
516 ;;; This magic function has quite a job to do indeed.
518 ;;; The careful reader will recall that <slots> contains all of the
519 ;;; optimized slot access forms produced by OPTIMIZE-INSTANCE-ACCESS.
520 ;;; Each of these is a call to either INSTANCE-READ or INSTANCE-WRITE.
522 ;;; At the time these calls were produced, the first argument was
523 ;;; specified as the symbol .PV-OFFSET.; what we have to do now is
524 ;;; convert those pv-offset arguments into the actual number that is
525 ;;; the correct offset into the pv.
527 ;;; But first, oh but first, we sort <slots> a bit so that for each
528 ;;; argument we have the slots in alphabetical order. This
529 ;;; canonicalizes the PV-TABLE's a bit and will hopefully lead to
530 ;;; having fewer PV's floating around. Even if the gain is only
531 ;;; modest, it costs nothing.
532 (defun slot-name-lists-from-slots (slots)
533 (let ((slots (mutate-slots slots
)))
534 (let* ((slot-name-lists
535 (mapcar (lambda (parameter-entry)
536 (cons nil
(mapcar #'car
(cdr parameter-entry
))))
538 (mapcar (lambda (r+snl
)
539 (when (or (car r
+snl
) (cdr r
+snl
))
543 (defun mutate-slots (slots)
544 (let ((sorted-slots (sort-slots slots
))
546 (dolist (parameter-entry sorted-slots
)
547 (dolist (slot-entry (cdr parameter-entry
))
549 (dolist (form (cdr slot-entry
))
550 (setf (cadr form
) pv-offset
))
551 ;; Count one more for the slot we use for SLOT-INFO.
555 (defun symbol-pkg-name (sym)
556 (let ((pkg (symbol-package sym
)))
557 (if pkg
(package-name pkg
) "")))
559 ;;; FIXME: Because of the existence of UNINTERN and RENAME-PACKAGE,
560 ;;; the part of this ordering which is based on SYMBOL-PKG-NAME is not
561 ;;; stable. This ordering is only used in to
562 ;;; SLOT-NAME-LISTS-FROM-SLOTS, where it serves to "canonicalize the
563 ;;; PV-TABLE's a bit and will hopefully lead to having fewer PV's
564 ;;; floating around", so it sounds as though the instability won't
565 ;;; actually lead to bugs, just small inefficiency. But still, it
566 ;;; would be better to reimplement this function as a comparison based
568 ;;; * stable comparison
569 ;;; * smaller code (here, and in being able to discard SYMBOL-PKG-NAME)
571 (defun symbol-lessp (a b
)
572 (if (eq (symbol-package a
)
574 (string-lessp (symbol-name a
)
576 (string-lessp (symbol-pkg-name a
)
577 (symbol-pkg-name b
))))
579 (defun symbol-or-cons-lessp (a b
)
582 (symbol (symbol-lessp a b
))
586 (cons (if (eq (car a
) (car b
))
587 (symbol-or-cons-lessp (cdr a
) (cdr b
))
588 (symbol-or-cons-lessp (car a
) (car b
))))))))
590 (defun sort-slots (slots)
591 (mapcar (lambda (parameter-entry)
592 (cons (car parameter-entry
)
593 (sort (cdr parameter-entry
) ;slot entries
594 #'symbol-or-cons-lessp
599 ;;;; This needs to work in terms of metatypes and also needs to work
600 ;;;; for automatically generated reader and writer functions.
601 ;;;; Automatically generated reader and writer functions use this
604 (defmacro pv-binding
((required-parameters slot-name-lists pv-table-form
)
606 (let (slot-vars pv-parameters
)
607 (loop for slots in slot-name-lists
608 for required-parameter in required-parameters
611 (push required-parameter pv-parameters
)
612 (push (slot-vector-symbol i
) slot-vars
)))
613 `(pv-binding1 (,pv-table-form
614 ,(nreverse pv-parameters
) ,(nreverse slot-vars
))
617 (defmacro pv-binding1
((pv-table-form pv-parameters slot-vars
)
619 `(pv-env (,pv-table-form
,pv-parameters
)
620 (let (,@(mapcar (lambda (slot-var p
) `(,slot-var
(get-slots-or-nil ,p
)))
621 slot-vars pv-parameters
))
622 (declare (ignorable ,@(mapcar #'identity slot-vars
)))
625 ;;; This will only be visible in PV-ENV when the default MAKE-METHOD-LAMBDA is
627 (define-symbol-macro pv-env-environment overridden
)
629 (defmacro pv-env
(&environment env
630 (pv-table-form pv-parameters
)
632 ;; Decide which expansion to use based on the state of the PV-ENV-ENVIRONMENT
634 (if (eq (macroexpand 'pv-env-environment env
) 'default
)
635 `(locally (declare (simple-vector .pv.
))
637 `(let* ((.pv-table.
,pv-table-form
)
638 (.pv.
(pv-table-lookup-pv-args .pv-table.
,@pv-parameters
)))
639 (declare ,(make-pv-type-declaration '.pv.
))
642 (defun split-declarations (body args req-args cnm-p parameters-setqd
)
643 (let ((inner-decls nil
)
649 (setq decl
(car body
))
650 (unless (and (consp decl
) (eq (car decl
) 'declare
))
652 (dolist (form (cdr decl
))
654 (let* ((name (car form
)))
655 (cond ((eq '%class name
)
656 (push `(declare ,form
) inner-decls
))
657 ((or (member name
'(ignore ignorable special dynamic-extent type
))
658 (info :type
:kind name
))
662 (head (if (eq 'type name
)
663 (list name
(pop tail
))
666 (if (member var args
:test
#'eq
)
667 ;; Quietly remove IGNORE declarations on
668 ;; args when a next-method is involved, to
669 ;; prevent compiler warnings about ignored
671 (unless (and (eq 'ignore name
)
672 (member var req-args
:test
#'eq
)
673 (or cnm-p
(member var parameters-setqd
)))
677 (push `(declare (,@head
,@outers
)) outer-decls
))
679 (push `(declare (,@head
,@inners
)) inner-decls
))))
681 ;; All other declarations are not variable declarations,
682 ;; so they become outer declarations.
683 (push `(declare ,form
) outer-decls
))))))
684 (setq body
(cdr body
)))
685 (values outer-decls inner-decls body
)))
687 ;;; Convert a lambda expression containing a SB-PCL::%METHOD-NAME
688 ;;; declaration (which is a naming style internal to PCL) into an
689 ;;; SB-INT:NAMED-LAMBDA expression (which is a naming style used
690 ;;; throughout SBCL, understood by the main compiler); or if there's
691 ;;; no SB-PCL::%METHOD-NAME declaration, then just return the original
692 ;;; lambda expression.
693 (defun name-method-lambda (method-lambda)
694 (let ((method-name *method-name
*))
696 `(named-lambda (slow-method ,@method-name
) ,@(rest method-lambda
))
699 (defun make-method-initargs-form-internal (method-lambda initargs env
)
700 (declare (ignore env
))
701 (let (method-lambda-args
702 lmf
; becomes body of function
704 (if (not (and (= 3 (length method-lambda
))
705 (= 2 (length (setq method-lambda-args
(cadr method-lambda
))))
706 (consp (setq lmf
(third method-lambda
)))
707 (eq 'simple-lexical-method-functions
(car lmf
))
708 (eq (car method-lambda-args
)
709 (cadr (setq lmf-params
(cadr lmf
))))
710 (eq (cadr method-lambda-args
)
711 (caddr lmf-params
))))
712 `(list* :function
,(name-method-lambda method-lambda
)
714 (let* ((lambda-list (car lmf-params
))
718 (dolist (arg lambda-list
)
719 (when (member arg
'(&optional
&rest
&key
))
726 (setq args
(nreverse args
))
727 (setf (getf (getf initargs
'plist
) :arg-info
) (cons nreq restp
))
728 (make-method-initargs-form-internal1
729 initargs
(cddr lmf
) args lmf-params restp
)))))
731 (defun lambda-list-parameter-names (lambda-list)
732 ;; Given a valid lambda list, extract the parameter names.
733 (loop for x in lambda-list
735 do
(unless (member x lambda-list-keywords
:test
#'eq
)
737 (let ((name (car x
)))
739 ;; ... ((:BAR FOO) 1)
740 (push (second name
) res
)
744 (let ((name-p (cddr x
)))
746 (push (car name-p
) res
))))
749 finally
(return res
)))
751 (defun make-method-initargs-form-internal1
752 (initargs body req-args lmf-params restp
)
753 (let* (;; The lambda-list of the method, minus specifiers
754 (lambda-list (car lmf-params
))
755 ;; Names of the parameters that will be in the outermost lambda-list
756 ;; (and whose bound declarations thus need to be in OUTER-DECLS).
757 (outer-parameters req-args
)
758 ;; The lambda-list used by BIND-ARGS
759 (bind-list lambda-list
)
760 (parameters-setqd (getf (cdr lmf-params
) :parameters-setqd
))
761 (auxp (member '&aux bind-list
))
762 (call-next-method-p (getf (cdr lmf-params
) :call-next-method-p
)))
763 ;; Try to use the normal function call machinery instead of BIND-ARGS
764 ;; binding the arguments, unless:
765 (unless (or ;; If all arguments are required, BIND-ARGS will be a no-op
767 (and (not restp
) (not auxp
))
768 ;; CALL-NEXT-METHOD wants to use BIND-ARGS, and needs a
769 ;; list of all non-required arguments.
771 (setf ;; We don't want a binding for .REST-ARG.
773 ;; Get all the parameters for declaration parsing
774 outer-parameters
(lambda-list-parameter-names lambda-list
)
775 ;; Ensure that BIND-ARGS won't do anything (since
776 ;; BIND-LIST won't contain any non-required parameters,
777 ;; and REQ-ARGS will be of an equal length). We still want
778 ;; to pass BIND-LIST to FAST-LEXICAL-METHOD-FUNCTIONS so
779 ;; that BIND-FAST-LEXICAL-METHOD-FUNCTIONS can take care
780 ;; of rebinding SETQd required arguments around the method
783 (multiple-value-bind (outer-decls inner-decls body-sans-decls
)
785 body outer-parameters req-args call-next-method-p parameters-setqd
)
786 (let* ((rest-arg (when restp
788 (fmf-lambda-list (if rest-arg
789 (append req-args
(list '&rest rest-arg
))
790 (if call-next-method-p
795 (let* ((fmf (,(if *method-name
* 'named-lambda
'lambda
)
796 ,@(when *method-name
*
798 (list `(fast-method ,@*method-name
*)))
799 ;; The lambda-list of the FMF
800 (.pv. .next-method-call.
,@fmf-lambda-list
)
801 ;; body of the function
802 (declare (ignorable .pv. .next-method-call.
)
803 (disable-package-locks pv-env-environment
))
805 (symbol-macrolet ((pv-env-environment default
))
806 (fast-lexical-method-functions
807 (,bind-list .next-method-call.
,req-args
,rest-arg
808 ,@(cdddr lmf-params
))
810 ,@body-sans-decls
))))
811 (mf (%make-method-function fmf nil
)))
812 (set-funcallable-instance-function
813 mf
(method-function-from-fast-function fmf
',(getf initargs
'plist
)))
817 ;;; Use arrays and hash tables and the fngen stuff to make this much
818 ;;; better. It doesn't really matter, though, because a function
819 ;;; returned by this will get called only when the user explicitly
820 ;;; funcalls a result of method-function. BUT, this is needed to make
821 ;;; early methods work.
822 (defun method-function-from-fast-function (fmf plist
)
823 (declare (type function fmf
))
824 (let* ((method-function nil
)
825 (snl (getf plist
:slot-name-lists
))
827 (intern-pv-table :slot-name-lists snl
))))
828 (setq method-function
829 (lambda (method-args next-methods
)
830 (let* ((pv (when pv-table
831 (get-pv method-args pv-table
)))
832 (nm (car next-methods
))
833 (nms (cdr next-methods
))
836 :function
(if (std-instance-p nm
)
839 :call-method-args
(list nms
)))))
840 (apply fmf pv nmc method-args
))))
841 ;; FIXME: this looks dangerous.
842 (let* ((fname (%fun-name fmf
)))
843 (when (and fname
(eq (car fname
) 'fast-method
))
844 (set-fun-name method-function
(cons 'slow-method
(cdr fname
)))))
847 ;;; this is similar to the above, only not quite. Only called when
848 ;;; the MOP is heavily involved. Not quite parallel to
849 ;;; METHOD-FUNCTION-FROM-FAST-METHOD-FUNCTION, because we can close
850 ;;; over the actual PV-CELL in this case.
851 (defun method-function-from-fast-method-call (fmc)
852 (let* ((fmf (fast-method-call-function fmc
))
853 (pv (fast-method-call-pv fmc
)))
854 (lambda (method-args next-methods
)
855 (let* ((nm (car next-methods
))
856 (nms (cdr next-methods
))
859 :function
(if (std-instance-p nm
)
862 :call-method-args
(list nms
)))))
863 (apply fmf pv nmc method-args
)))))
865 (defun get-pv (method-args pv-table
)
866 (let ((pv-wrappers (pv-wrappers-from-all-args pv-table method-args
)))
868 (pv-table-lookup pv-table pv-wrappers
))))
870 (defun pv-table-lookup-pv-args (pv-table &rest pv-parameters
)
871 (pv-table-lookup pv-table
(pv-wrappers-from-pv-args pv-parameters
)))
873 (defun pv-wrappers-from-pv-args (&rest args
)
874 (loop for arg in args
875 collect
(valid-wrapper-of arg
)))
877 (defun pv-wrappers-from-all-args (pv-table args
)
878 (loop for snl in
(pv-table-slot-name-lists pv-table
)
881 collect
(valid-wrapper-of arg
)))
883 ;;; Return the subset of WRAPPERS which is used in the cache
885 (defun pv-wrappers-from-all-wrappers (pv-table wrappers
)
886 (loop for snl in
(pv-table-slot-name-lists pv-table
) and w in wrappers