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 (defmacro instance-slot-index (wrapper slot-name)
42 (declare (fixnum pos))
44 (dolist (sn (wrapper-instance-slots-layout ,wrapper))
45 (when (eq ,slot-name sn) (return-from loop pos))
48 (defstruct (pv-table (:predicate pv-tablep)
49 (:constructor make-pv-table-internal
52 (cache nil :type (or cache null))
53 (pv-size 0 :type fixnum)
54 (slot-name-lists nil :type list))
56 #-sb-fluid (declaim (sb-ext:freeze-type pv-table))
58 ;;; FIXME: The comment below seem to indicate that this was intended
59 ;;; to be actually used, however, it isn't anymore, and was commented
60 ;;; out at 0.9.13.47. Also removed was code in MAKE-PV-TABLE that
61 ;;; pushed each new PV-TABLE onto this list. --NS 2006-06-18
63 ;;; help new slot-value-using-class methods affect fast iv access
65 ;;; (defvar *all-pv-table-list* nil)
67 (declaim (inline make-pv-table))
68 (defun make-pv-table (&key slot-name-lists)
69 (make-pv-table-internal slot-name-lists))
71 (defun make-pv-table-type-declaration (var)
72 `(type pv-table ,var))
74 (defvar *slot-name-lists-inner* (make-hash-table :test 'equal))
75 (defvar *slot-name-lists-outer* (make-hash-table :test 'equal))
77 ;;; Entries in this are lists of (table . pv-offset-list).
78 (defvar *pv-key-to-pv-table-table* (make-hash-table :test 'equal))
80 (defun intern-pv-table (&key slot-name-lists)
82 (flet ((inner (slot-names)
83 (or (gethash slot-names *slot-name-lists-inner*)
84 (setf (gethash slot-names *slot-name-lists-inner*) slot-names)))
86 (or (gethash snl *slot-name-lists-outer*)
87 (setf (gethash snl *slot-name-lists-outer*)
90 (make-pv-table :slot-name-lists snl))))))
91 (let ((pv-table (outer (mapcar #'inner slot-name-lists))))
94 (dolist (slot-name-list slot-name-lists)
95 (dolist (slot-name (cdr slot-name-list))
96 (note-pv-table-reference slot-name pv-index pv-table)
98 (setf (pv-table-pv-size pv-table) pv-index)))
101 (defun note-pv-table-reference (ref pv-offset pv-table)
102 (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
104 (let ((table-entry (assq pv-table entry)))
105 (when (and (null table-entry)
106 (> (length entry) 8))
107 (let ((new-table-table (make-hash-table :size 16 :test 'eq)))
108 (dolist (table-entry entry)
109 (setf (gethash (car table-entry) new-table-table)
111 (setf (gethash ref *pv-key-to-pv-table-table*) new-table-table)))
113 (if (null table-entry)
114 (let ((new (cons pv-table pv-offset)))
116 (push new (cdr entry))
117 (setf (gethash ref *pv-key-to-pv-table-table*)
119 (push pv-offset (cdr table-entry)))
120 (return-from note-pv-table-reference nil))))
121 (let ((list (gethash pv-table entry)))
123 (push pv-offset (cdr list))
124 (setf (gethash pv-table entry) (list pv-offset)))))
127 (defun map-pv-table-references-of (ref function)
128 (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
130 (dolist (table+pv-offset-list entry)
132 (car table+pv-offset-list)
133 (cdr table+pv-offset-list)))
134 (maphash function entry)))
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)))
141 (slot-accessor-std-p slotd type)))))
143 (defun compute-pv-slot (slot-name wrapper class class-slots)
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 (assq slot-name class-slots)))
148 (when (consp slot-name)
149 (case (first slot-name)
151 (when (eq *boot-state* 'complete)
152 (let ((gf (gdefinition (second slot-name))))
153 (when (generic-function-p gf)
154 (accessor-values1 gf (first slot-name) class)))))
155 (t (bug "Don't know how to deal with ~S in ~S"
156 slot-name 'compute-pv-slots))))))
158 (defun compute-pv (slot-name-lists wrappers)
159 (unless (listp wrappers)
160 (setq wrappers (list wrappers)))
162 (dolist (slot-names slot-name-lists
163 (make-permutation-vector (nreverse elements)))
165 (let* ((wrapper (pop wrappers))
166 (std-p (typep wrapper 'wrapper))
167 (class (wrapper-class* wrapper))
168 (class-slots (and std-p (wrapper-class-slots wrapper))))
169 (dolist (slot-name (cdr slot-names))
171 (compute-pv-slot slot-name wrapper class class-slots)
175 (defun make-permutation-vector (indexes)
176 (make-array (length indexes) :initial-contents indexes))
178 (defun pv-table-lookup (pv-table pv-wrappers)
179 (let* ((slot-name-lists (pv-table-slot-name-lists pv-table))
180 (cache (or (pv-table-cache pv-table)
181 (setf (pv-table-cache pv-table)
182 (make-cache :key-count (- (length slot-name-lists)
183 (count nil slot-name-lists))
186 (multiple-value-bind (hitp value) (probe-cache cache pv-wrappers)
189 (let* ((pv (compute-pv slot-name-lists pv-wrappers))
190 (pv-cell (cons pv nil))
191 (new-cache (fill-cache cache pv-wrappers pv-cell)))
192 ;; This is safe: if another thread races us here the loser just
193 ;; misses the next time as well.
194 (unless (eq new-cache cache)
195 (setf (pv-table-cache pv-table) new-cache))
198 (defun make-pv-type-declaration (var)
199 `(type simple-vector ,var))
201 (defvar *pv-table-cache-update-info* nil)
203 (defun update-pv-table-cache-info (class)
204 (let ((slot-names-for-pv-table-update nil)
206 (dolist (icu *pv-table-cache-update-info*)
207 (if (eq (car icu) class)
208 (pushnew (cdr icu) slot-names-for-pv-table-update)
209 (push icu new-icui)))
210 (setq *pv-table-cache-update-info* new-icui)
211 (when slot-names-for-pv-table-update
212 (update-all-pv-table-caches class slot-names-for-pv-table-update))))
214 (defun update-all-pv-table-caches (class slot-names)
215 (let* ((cwrapper (class-wrapper class))
216 (std-p (typep cwrapper 'wrapper))
217 (class-slots (and std-p (wrapper-class-slots cwrapper)))
223 (compute-pv-slot slot-name cwrapper class class-slots)
227 (dolist (slot-name slot-names)
228 (map-pv-table-references-of
230 (lambda (pv-table pv-offset-list)
231 (declare (ignore pv-offset-list))
232 (pushnew pv-table pv-tables))))
233 (dolist (pv-table pv-tables)
234 (let* ((cache (pv-table-cache pv-table))
235 (slot-name-lists (pv-table-slot-name-lists pv-table))
236 (pv-size (pv-table-pv-size pv-table))
237 (pv-map (make-array pv-size :initial-element nil)))
238 (let ((map-index 0) (param-index 0))
239 (dolist (slot-name-list slot-name-lists)
240 (dolist (slot-name (cdr slot-name-list))
241 (let ((a (assoc slot-name new-values)))
242 (setf (svref pv-map map-index)
243 (and a (cons param-index (cdr a)))))
247 (map-cache (lambda (wrappers pv-cell)
248 (update-slots-in-pv wrappers (car pv-cell)
249 cwrapper pv-size pv-map))
252 (defun update-slots-in-pv (wrappers pv cwrapper pv-size pv-map)
254 (when (eq cwrapper wrappers)
255 (dotimes-fixnum (i pv-size)
256 (let ((map (svref pv-map i)))
258 (aver (= (car map) 0))
259 (setf (svref pv i) (cdr map))))))
260 (when (memq cwrapper wrappers)
262 (dolist (wrapper wrappers)
263 (when (eq wrapper cwrapper)
264 (dotimes-fixnum (i pv-size)
265 (let ((map (svref pv-map i)))
266 (when (and map (= (car map) param))
267 (setf (svref pv i) (cdr map))))))
270 (defun can-optimize-access (form required-parameters env)
271 (destructuring-bind (op var-form slot-name-form &optional new-value) form
272 (let ((type (ecase op
274 (set-slot-value 'writer)
275 (slot-boundp 'boundp)))
276 (var (extract-the var-form))
277 (slot-name (constant-form-value slot-name-form env)))
279 (let* ((rebound? (caddr (var-declaration '%variable-rebinding var env)))
280 (parameter-or-nil (car (memq (or rebound? var)
281 required-parameters))))
282 (when parameter-or-nil
283 (let* ((class-name (caddr (var-declaration '%class
286 (class (find-class class-name nil)))
287 (when (or (not (eq *boot-state* 'complete))
288 (and class (not (class-finalized-p class))))
290 (when (and class-name (not (eq class-name t)))
291 (when (or (null type)
293 (memq *the-class-structure-object*
294 (class-precedence-list class))))
295 (optimize-slot-value-by-class-p class slot-name type))
296 (values (cons parameter-or-nil (or class class-name))
300 ;;; Check whether the binding of the named variable is modified in the
302 (defun parameter-modified-p (parameter-name env)
303 (let ((modified-variables (macroexpand '%parameter-binding-modified env)))
304 (memq parameter-name modified-variables)))
306 (defun optimize-slot-value (form slots required-parameters env)
307 (multiple-value-bind (sparameter slot-name)
308 (can-optimize-access form required-parameters env)
310 (let ((optimized-form
311 (optimize-instance-access slots :read sparameter
313 ;; We don't return the optimized form directly, since there's
314 ;; still a chance that we'll find out later on that the
315 ;; optimization should not have been done, for example due to
316 ;; the walker encountering a SETQ on SPARAMETER later on in
317 ;; the body [ see for example clos.impure.lisp test with :name
318 ;; ((:setq :method-parameter) slot-value)) ]. Instead we defer
319 ;; the decision until the compiler macroexpands
320 ;; OPTIMIZED-SLOT-VALUE.
322 ;; Note that we must still call OPTIMIZE-INSTANCE-ACCESS at
323 ;; this point (instead of when expanding
324 ;; OPTIMIZED-SLOT-VALUE), since it mutates the structure of
325 ;; SLOTS. If that mutation isn't done during the walking,
326 ;; MAKE-METHOD-LAMBDA-INTERNAL won't wrap a correct PV-BINDING
327 ;; form around the body, and compilation will fail. -- JES,
329 `(optimized-slot-value ,form ,(car sparameter) ,optimized-form))
330 `(accessor-slot-value ,@(cdr form)))))
332 (defmacro optimized-slot-value (form parameter-name optimized-form
334 ;; Either use OPTIMIZED-FORM or fall back to the safe
335 ;; ACCESSOR-SLOT-VALUE.
336 (if (parameter-modified-p parameter-name env)
337 `(accessor-slot-value ,@(cdr form))
340 (defun optimize-set-slot-value (form slots required-parameters env)
341 (multiple-value-bind (sparameter slot-name new-value)
342 (can-optimize-access form required-parameters env)
344 (let ((optimized-form
345 (optimize-instance-access slots :write sparameter
346 slot-name new-value)))
347 ;; See OPTIMIZE-SLOT-VALUE
348 `(optimized-set-slot-value ,form ,(car sparameter) ,optimized-form))
349 `(accessor-set-slot-value ,@(cdr form)))))
351 (defmacro optimized-set-slot-value (form parameter-name optimized-form
353 (cond ((safe-code-p env)
354 ;; Don't optimize slot value setting in safe code, since the
355 ;; optimized version will fail to catch some type errors
356 ;; (for example when a subclass declares a tighter type for
357 ;; the slot than a superclass).
358 `(safe-set-slot-value ,@(cdr form)))
359 ((parameter-modified-p parameter-name env)
360 `(accessor-set-slot-value ,@(cdr form)))
364 (defun optimize-slot-boundp (form slots required-parameters env)
365 (multiple-value-bind (sparameter slot-name)
366 (can-optimize-access form required-parameters env)
368 (let ((optimized-form
369 (optimize-instance-access slots :boundp sparameter
371 ;; See OPTIMIZE-SLOT-VALUE
372 `(optimized-slot-boundp ,form ,(car sparameter) ,optimized-form))
373 `(accessor-slot-boundp ,@(cdr form)))))
375 (defmacro optimized-slot-boundp (form parameter-name optimized-form
377 (if (parameter-modified-p parameter-name env)
378 `(accessor-slot-boundp ,@(cdr form))
381 ;;; The SLOTS argument is an alist, the CAR of each entry is the name
382 ;;; of a required parameter to the function. The alist is in order, so
383 ;;; the position of an entry in the alist corresponds to the
384 ;;; argument's position in the lambda list.
385 (defun optimize-instance-access (slots
390 (let ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
391 (parameter (if (consp sparameter) (car sparameter) sparameter)))
392 (if (and (eq *boot-state* 'complete)
394 (memq *the-class-structure-object* (class-precedence-list class)))
395 (let ((slotd (find-slot-definition class slot-name)))
398 `(,(slot-definition-defstruct-accessor-symbol slotd) ,parameter))
400 `(setf (,(slot-definition-defstruct-accessor-symbol slotd)
405 (let* ((parameter-entry (assq parameter slots))
406 (slot-entry (assq slot-name (cdr parameter-entry)))
407 (position (posq parameter-entry slots))
408 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
409 (unless parameter-entry
410 (bug "slot optimization bewilderment: O-I-A"))
412 (setq slot-entry (list slot-name))
413 (push slot-entry (cdr parameter-entry)))
414 (push pv-offset-form (cdr slot-entry))
417 `(instance-read ,pv-offset-form ,parameter ,position
418 ',slot-name ',class))
420 `(let ((.new-value. ,new-value))
421 (instance-write ,pv-offset-form ,parameter ,position
422 ',slot-name ',class .new-value.)))
424 `(instance-boundp ,pv-offset-form ,parameter ,position
425 ',slot-name ',class)))))))
427 (define-walker-template pv-offset) ; These forms get munged by mutate slots.
428 (defmacro pv-offset (arg) arg)
429 (define-walker-template instance-accessor-parameter)
430 (defmacro instance-accessor-parameter (x) x)
432 ;;; It is safe for these two functions to be wrong. They just try to
433 ;;; guess what the most likely case will be.
434 (defun generate-fast-class-slot-access-p (class-form slot-name-form)
435 (let ((class (and (constantp class-form) (constant-form-value class-form)))
436 (slot-name (and (constantp slot-name-form)
437 (constant-form-value slot-name-form))))
438 (and (eq *boot-state* 'complete)
439 (standard-class-p class)
440 (not (eq class *the-class-t*)) ; shouldn't happen, though.
441 (let ((slotd (find-slot-definition class slot-name)))
442 (and slotd (eq :class (slot-definition-allocation slotd)))))))
444 (defun skip-fast-slot-access-p (class-form slot-name-form type)
445 (let ((class (and (constantp class-form) (constant-form-value class-form)))
446 (slot-name (and (constantp slot-name-form)
447 (constant-form-value slot-name-form))))
448 (and (eq *boot-state* 'complete)
449 (standard-class-p class)
450 (not (eq class *the-class-t*)) ; shouldn't happen, though.
451 ;; FIXME: Is this really right? "Don't skip if there is
452 ;; no slot definition."
453 (let ((slotd (find-slot-definition class slot-name)))
455 (not (slot-accessor-std-p slotd type)))))))
457 (defmacro instance-read-internal (pv slots pv-offset default &optional kind)
458 (unless (member kind '(nil :instance :class :default))
459 (error "illegal kind argument to ~S: ~S" 'instance-read-internal kind))
460 (if (eq kind :default)
462 (let* ((index (gensym))
464 `(locally (declare #.*optimize-speed*)
465 (let ((,index (svref ,pv ,pv-offset)))
466 (setq ,value (typecase ,index
467 ;; FIXME: the line marked by KLUDGE below
468 ;; (and the analogous spot in
469 ;; INSTANCE-WRITE-INTERNAL) is there purely
470 ;; to suppress a type mismatch warning that
471 ;; propagates through to user code.
472 ;; Presumably SLOTS at this point can never
473 ;; actually be NIL, but the compiler seems
474 ;; to think it could, so we put this here
475 ;; to shut it up. (see also mail Rudi
476 ;; Schlatte sbcl-devel 2003-09-21) -- CSR,
478 ,@(when (or (null kind) (eq kind :instance))
481 (clos-slots-ref ,slots ,index)))))
482 ,@(when (or (null kind) (eq kind :class))
483 `((cons (cdr ,index))))
485 (if (eq ,value +slot-unbound+)
489 (defmacro instance-read (pv-offset parameter position slot-name class)
490 (if (skip-fast-slot-access-p class slot-name 'reader)
491 `(accessor-slot-value ,parameter ,slot-name)
492 `(instance-read-internal .pv. ,(slot-vector-symbol position)
493 ,pv-offset (accessor-slot-value ,parameter ,slot-name)
494 ,(if (generate-fast-class-slot-access-p class slot-name)
497 (defmacro instance-write-internal (pv slots pv-offset new-value default
499 (unless (member kind '(nil :instance :class :default))
500 (error "illegal kind argument to ~S: ~S" 'instance-write-internal kind))
501 (if (eq kind :default)
503 (let* ((index (gensym)))
504 `(locally (declare #.*optimize-speed*)
505 (let ((,index (svref ,pv ,pv-offset)))
507 ,@(when (or (null kind) (eq kind :instance))
508 `((fixnum (and ,slots
509 (setf (clos-slots-ref ,slots ,index)
511 ,@(when (or (null kind) (eq kind :class))
512 `((cons (setf (cdr ,index) ,new-value))))
515 (defmacro instance-write (pv-offset
521 (if (skip-fast-slot-access-p class slot-name 'writer)
522 `(accessor-set-slot-value ,parameter ,slot-name ,new-value)
523 `(instance-write-internal .pv. ,(slot-vector-symbol position)
524 ,pv-offset ,new-value
525 (accessor-set-slot-value ,parameter ,slot-name ,new-value)
526 ,(if (generate-fast-class-slot-access-p class slot-name)
529 (defmacro instance-boundp-internal (pv slots pv-offset default
531 (unless (member kind '(nil :instance :class :default))
532 (error "illegal kind argument to ~S: ~S" 'instance-boundp-internal kind))
533 (if (eq kind :default)
535 (let* ((index (gensym)))
536 `(locally (declare #.*optimize-speed*)
537 (let ((,index (svref ,pv ,pv-offset)))
539 ,@(when (or (null kind) (eq kind :instance))
540 `((fixnum (not (and ,slots
541 (eq (clos-slots-ref ,slots ,index)
543 ,@(when (or (null kind) (eq kind :class))
544 `((cons (not (eq (cdr ,index) +slot-unbound+)))))
547 (defmacro instance-boundp (pv-offset parameter position slot-name class)
548 (if (skip-fast-slot-access-p class slot-name 'boundp)
549 `(accessor-slot-boundp ,parameter ,slot-name)
550 `(instance-boundp-internal .pv. ,(slot-vector-symbol position)
551 ,pv-offset (accessor-slot-boundp ,parameter ,slot-name)
552 ,(if (generate-fast-class-slot-access-p class slot-name)
555 ;;; This magic function has quite a job to do indeed.
557 ;;; The careful reader will recall that <slots> contains all of the
558 ;;; optimized slot access forms produced by OPTIMIZE-INSTANCE-ACCESS.
559 ;;; Each of these is a call to either INSTANCE-READ or INSTANCE-WRITE.
561 ;;; At the time these calls were produced, the first argument was
562 ;;; specified as the symbol .PV-OFFSET.; what we have to do now is
563 ;;; convert those pv-offset arguments into the actual number that is
564 ;;; the correct offset into the pv.
566 ;;; But first, oh but first, we sort <slots> a bit so that for each
567 ;;; argument we have the slots in alphabetical order. This
568 ;;; canonicalizes the PV-TABLE's a bit and will hopefully lead to
569 ;;; having fewer PV's floating around. Even if the gain is only
570 ;;; modest, it costs nothing.
571 (defun slot-name-lists-from-slots (slots)
572 (let ((slots (mutate-slots slots)))
573 (let* ((slot-name-lists
574 (mapcar (lambda (parameter-entry)
575 (cons nil (mapcar #'car (cdr parameter-entry))))
577 (mapcar (lambda (r+snl)
578 (when (or (car r+snl) (cdr r+snl))
582 (defun mutate-slots (slots)
583 (let ((sorted-slots (sort-slots slots))
585 (dolist (parameter-entry sorted-slots)
586 (dolist (slot-entry (cdr parameter-entry))
588 (dolist (form (cdr slot-entry))
589 (setf (cadr form) pv-offset))))
592 (defun symbol-pkg-name (sym)
593 (let ((pkg (symbol-package sym)))
594 (if pkg (package-name pkg) "")))
596 ;;; FIXME: Because of the existence of UNINTERN and RENAME-PACKAGE,
597 ;;; the part of this ordering which is based on SYMBOL-PKG-NAME is not
598 ;;; stable. This ordering is only used in to
599 ;;; SLOT-NAME-LISTS-FROM-SLOTS, where it serves to "canonicalize the
600 ;;; PV-TABLE's a bit and will hopefully lead to having fewer PV's
601 ;;; floating around", so it sounds as though the instability won't
602 ;;; actually lead to bugs, just small inefficiency. But still, it
603 ;;; would be better to reimplement this function as a comparison based
605 ;;; * stable comparison
606 ;;; * smaller code (here, and in being able to discard SYMBOL-PKG-NAME)
608 (defun symbol-lessp (a b)
609 (if (eq (symbol-package a)
611 (string-lessp (symbol-name a)
613 (string-lessp (symbol-pkg-name a)
614 (symbol-pkg-name b))))
616 (defun symbol-or-cons-lessp (a b)
619 (symbol (symbol-lessp a b))
623 (cons (if (eq (car a) (car b))
624 (symbol-or-cons-lessp (cdr a) (cdr b))
625 (symbol-or-cons-lessp (car a) (car b))))))))
627 (defun sort-slots (slots)
628 (mapcar (lambda (parameter-entry)
629 (cons (car parameter-entry)
630 (sort (cdr parameter-entry) ;slot entries
631 #'symbol-or-cons-lessp
636 ;;;; This needs to work in terms of metatypes and also needs to work
637 ;;;; for automatically generated reader and writer functions.
638 ;;;; Automatically generated reader and writer functions use this
641 (defmacro pv-binding ((required-parameters slot-name-lists pv-table-form)
643 (let (slot-vars pv-parameters)
644 (loop for slots in slot-name-lists
645 for required-parameter in required-parameters
648 (push required-parameter pv-parameters)
649 (push (slot-vector-symbol i) slot-vars)))
650 `(pv-binding1 (.pv. ,pv-table-form
651 ,(nreverse pv-parameters) ,(nreverse slot-vars))
654 (defmacro pv-binding1 ((pv pv-table-form pv-parameters slot-vars)
656 `(pv-env (,pv ,pv-table-form ,pv-parameters)
657 (let (,@(mapcar (lambda (slot-var p) `(,slot-var (get-slots-or-nil ,p)))
658 slot-vars pv-parameters))
659 (declare (ignorable ,@(mapcar #'identity slot-vars)))
662 ;;; This will only be visible in PV-ENV when the default MAKE-METHOD-LAMBDA is
664 (define-symbol-macro pv-env-environment overridden)
666 (defmacro pv-env (&environment env
667 (pv pv-table-form pv-parameters)
669 ;; Decide which expansion to use based on the state of the PV-ENV-ENVIRONMENT
671 (if (eq (macroexpand 'pv-env-environment env) 'default)
672 `(let ((,pv (car .pv-cell.)))
673 (declare ,(make-pv-type-declaration pv))
675 `(let* ((.pv-table. ,pv-table-form)
676 (.pv-cell. (pv-table-lookup-pv-args .pv-table. ,@pv-parameters))
677 (,pv (car .pv-cell.)))
678 (declare ,(make-pv-type-declaration pv))
681 (defvar *non-var-declarations*
682 ;; FIXME: VALUES was in this list, conditionalized with #+CMU, but I
683 ;; don't *think* CMU CL had, or SBCL has, VALUES declarations. If
684 ;; SBCL doesn't have 'em, VALUES should probably be removed from
695 (defvar *var-declarations-with-arg*
699 (defvar *var-declarations-without-arg*
701 ignorable special dynamic-extent
702 ;; FIXME: Possibly this entire list and variable could go away.
703 ;; If not, certainly we should remove all these built-in typenames
704 ;; from the list, and replace them with a test for "is it a type
705 ;; name?" (CLTL1 allowed only built-in type names as declarations,
706 ;; but ANSI CL allows any type name as a declaration.)
707 array atom base-char bignum bit bit-vector character compiled-function
708 complex cons double-float extended-char
709 fixnum float function hash-table integer
710 keyword list long-float nil null number package pathname random-state ratio
711 rational readtable sequence short-float signed-byte simple-array
712 simple-bit-vector simple-string simple-vector single-float standard-char
713 stream string symbol t unsigned-byte vector))
715 (defun split-declarations (body args maybe-reads-params-p)
716 (let ((inner-decls nil)
719 (loop (when (null body) (return nil))
720 (setq decl (car body))
721 (unless (and (consp decl)
722 (eq (car decl) 'declare))
724 (dolist (form (cdr decl))
726 (let ((declaration-name (car form)))
727 (if (member declaration-name *non-var-declarations*)
728 (push `(declare ,form) outer-decls)
730 (member declaration-name
731 *var-declarations-with-arg*))
733 (member declaration-name
734 *var-declarations-without-arg*))
735 (dname (list (pop form)))
736 (inners nil) (outers nil))
737 (unless (or arg-p non-arg-p)
738 ;; FIXME: This warning, and perhaps the
739 ;; various *VAR-DECLARATIONS-FOO* and/or
740 ;; *NON-VAR-DECLARATIONS* variables,
741 ;; could probably go away now that we're not
742 ;; trying to be portable between different
743 ;; CLTL1 hosts the way PCL was. (Note that to
744 ;; do this right, we need to be able to handle
745 ;; user-defined (DECLAIM (DECLARATION FOO))
747 (warn "The declaration ~S is not understood by ~S.~@
748 Please put ~S on one of the lists ~S,~%~S, or~%~S.~@
749 (Assuming it is a variable declaration without argument)."
750 declaration-name 'split-declarations
752 '*non-var-declarations*
753 '*var-declarations-with-arg*
754 '*var-declarations-without-arg*)
755 (push declaration-name *var-declarations-without-arg*))
757 (setq dname (append dname (list (pop form)))))
759 (%class (push `(declare (,@dname ,@form)) inner-decls))
762 (if (member var args)
763 ;; Quietly remove IGNORE declarations
764 ;; on args when a next-method is
765 ;; involved, to prevent compiler
766 ;; warnings about ignored args being
768 (unless (and maybe-reads-params-p
769 (eq (car dname) 'ignore))
773 (push `(declare (,@dname ,@outers)) outer-decls))
776 `(declare (,@dname ,@inners))
778 (setq body (cdr body)))
779 (values outer-decls inner-decls body)))
781 ;;; Pull a name out of the %METHOD-NAME declaration in the function
782 ;;; body given, or return NIL if no %METHOD-NAME declaration is found.
783 (defun body-method-name (body)
784 (multiple-value-bind (real-body declarations documentation)
786 (declare (ignore real-body documentation))
787 (let ((name-decl (get-declaration '%method-name declarations)))
789 (destructuring-bind (name) name-decl
792 ;;; Convert a lambda expression containing a SB-PCL::%METHOD-NAME
793 ;;; declaration (which is a naming style internal to PCL) into an
794 ;;; SB-INT:NAMED-LAMBDA expression (which is a naming style used
795 ;;; throughout SBCL, understood by the main compiler); or if there's
796 ;;; no SB-PCL::%METHOD-NAME declaration, then just return the original
797 ;;; lambda expression.
798 (defun name-method-lambda (method-lambda)
799 (let ((method-name (body-method-name (cddr method-lambda))))
801 `(named-lambda (slow-method ,method-name) ,(rest method-lambda))
804 (defun make-method-initargs-form-internal (method-lambda initargs env)
805 (declare (ignore env))
806 (let (method-lambda-args
807 lmf ; becomes body of function
809 (if (not (and (= 3 (length method-lambda))
810 (= 2 (length (setq method-lambda-args (cadr method-lambda))))
811 (consp (setq lmf (third method-lambda)))
812 (eq 'simple-lexical-method-functions (car lmf))
813 (eq (car method-lambda-args)
814 (cadr (setq lmf-params (cadr lmf))))
815 (eq (cadr method-lambda-args)
816 (caddr lmf-params))))
817 `(list* :function ,(name-method-lambda method-lambda)
819 (let* ((lambda-list (car lmf-params))
823 (dolist (arg lambda-list)
824 (when (member arg '(&optional &rest &key))
831 (setq args (nreverse args))
832 (setf (getf (getf initargs 'plist) :arg-info) (cons nreq restp))
833 (make-method-initargs-form-internal1
834 initargs (cddr lmf) args lmf-params restp)))))
836 (defun lambda-list-parameter-names (lambda-list)
837 ;; Given a valid lambda list, extract the parameter names.
838 (loop for x in lambda-list
840 do (unless (member x lambda-list-keywords)
842 (let ((name (car x)))
844 ;; ... ((:BAR FOO) 1)
845 (push (second name) res)
849 (let ((name-p (cddr x)))
851 (push (car name-p) res))))
854 finally (return res)))
856 (defun make-method-initargs-form-internal1
857 (initargs body req-args lmf-params restp)
858 (let* (;; The lambda-list of the method, minus specifiers
859 (lambda-list (car lmf-params))
860 ;; Names of the parameters that will be in the outermost lambda-list
861 ;; (and whose bound declarations thus need to be in OUTER-DECLS).
862 (outer-parameters req-args)
863 ;; The lambda-list used by BIND-ARGS
864 (bind-list lambda-list)
865 (setq-p (getf (cdr lmf-params) :setq-p))
866 (auxp (member '&aux bind-list))
867 (call-next-method-p (getf (cdr lmf-params) :call-next-method-p)))
868 ;; Try to use the normal function call machinery instead of BIND-ARGS
869 ;; binding the arguments, unless:
870 (unless (or ;; If all arguments are required, BIND-ARGS will be a no-op
872 (and (not restp) (not auxp))
873 ;; CALL-NEXT-METHOD wants to use BIND-ARGS, and needs a
874 ;; list of all non-required arguments.
876 (setf ;; We don't want a binding for .REST-ARG.
878 ;; Get all the parameters for declaration parsing
879 outer-parameters (lambda-list-parameter-names lambda-list)
880 ;; Ensure that BIND-ARGS won't do anything (since
881 ;; BIND-LIST won't contain any non-required parameters,
882 ;; and REQ-ARGS will be of an equal length). We still want
883 ;; to pass BIND-LIST to FAST-LEXICAL-METHOD-FUNCTIONS so
884 ;; that BIND-FAST-LEXICAL-METHOD-FUNCTIONS can take care
885 ;; of rebinding SETQd required arguments around the method
888 (multiple-value-bind (outer-decls inner-decls body-sans-decls)
890 body outer-parameters (or call-next-method-p setq-p))
891 (let* ((rest-arg (when restp
893 (fmf-lambda-list (if rest-arg
894 (append req-args (list '&rest rest-arg))
895 (if call-next-method-p
900 (let* ((fmf (,(if (body-method-name body) 'named-lambda 'lambda)
901 ,@(when (body-method-name body)
903 (list (cons 'fast-method (body-method-name body))))
904 ;; The lambda-list of the FMF
905 (.pv-cell. .next-method-call. ,@fmf-lambda-list)
906 ;; body of the function
907 (declare (ignorable .pv-cell. .next-method-call.)
908 (disable-package-locks pv-env-environment))
910 (symbol-macrolet ((pv-env-environment default))
911 (fast-lexical-method-functions
912 (,bind-list .next-method-call. ,req-args ,rest-arg
913 ,@(cdddr lmf-params))
915 ,@body-sans-decls))))
916 (mf (%make-method-function fmf nil)))
917 (set-funcallable-instance-function
918 mf (method-function-from-fast-function fmf ',(getf initargs 'plist)))
922 ;;; Use arrays and hash tables and the fngen stuff to make this much
923 ;;; better. It doesn't really matter, though, because a function
924 ;;; returned by this will get called only when the user explicitly
925 ;;; funcalls a result of method-function. BUT, this is needed to make
926 ;;; early methods work.
927 (defun method-function-from-fast-function (fmf plist)
928 (declare (type function fmf))
929 (let* ((method-function nil)
930 (snl (getf plist :slot-name-lists))
932 (intern-pv-table :slot-name-lists snl)))
933 (arg-info (getf plist :arg-info))
934 (nreq (car arg-info))
935 (restp (cdr arg-info)))
936 (setq method-function
937 (lambda (method-args next-methods)
938 (let* ((pv-cell (when pv-table
939 (get-pv-cell method-args pv-table)))
940 (nm (car next-methods))
941 (nms (cdr next-methods))
944 :function (if (std-instance-p nm)
947 :call-method-args (list nms)))))
948 (apply fmf pv-cell nmc method-args))))
949 ;; FIXME: this looks dangerous.
950 (let* ((fname (%fun-name fmf)))
951 (when (and fname (eq (car fname) 'fast-method))
952 (set-fun-name method-function (cons 'slow-method (cdr fname)))))
955 ;;; this is similar to the above, only not quite. Only called when
956 ;;; the MOP is heavily involved. Not quite parallel to
957 ;;; METHOD-FUNCTION-FROM-FAST-METHOD-FUNCTION, because we can close
958 ;;; over the actual PV-CELL in this case.
959 (defun method-function-from-fast-method-call (fmc)
960 (let* ((fmf (fast-method-call-function fmc))
961 (pv-cell (fast-method-call-pv-cell fmc))
962 (arg-info (fast-method-call-arg-info fmc))
963 (nreq (car arg-info))
964 (restp (cdr arg-info)))
965 (lambda (method-args next-methods)
966 (let* ((nm (car next-methods))
967 (nms (cdr next-methods))
970 :function (if (std-instance-p nm)
973 :call-method-args (list nms)))))
974 (apply fmf pv-cell nmc method-args)))))
976 (defun get-pv-cell (method-args pv-table)
977 (let ((pv-wrappers (pv-wrappers-from-all-args pv-table method-args)))
979 (pv-table-lookup pv-table pv-wrappers))))
981 (defun pv-table-lookup-pv-args (pv-table &rest pv-parameters)
982 (pv-table-lookup pv-table (pv-wrappers-from-pv-args pv-parameters)))
984 (defun pv-wrappers-from-pv-args (&rest args)
986 (dolist (arg args (if (cdr wrappers) (nreverse wrappers) (car wrappers)))
987 (let ((wrapper (wrapper-of arg)))
988 (push (if (invalid-wrapper-p wrapper)
989 (check-wrapper-validity wrapper)
993 (defun pv-wrappers-from-all-args (pv-table args)
994 (loop for snl in (pv-table-slot-name-lists pv-table) and arg in args
996 collect (wrapper-of arg) into wrappers
997 finally (return (if (cdr wrappers) wrappers (car wrappers)))))
999 ;;; Return the subset of WRAPPERS which is used in the cache
1001 (defun pv-wrappers-from-all-wrappers (pv-table wrappers)
1002 (loop for snl in (pv-table-slot-name-lists pv-table) and w in wrappers
1004 collect w into result
1005 finally (return (if (cdr result) result (car result)))))