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 (defmacro instance-slot-index (wrapper slot-name)
30 (declare (fixnum pos))
32 (dolist (sn (wrapper-instance-slots-layout ,wrapper))
33 (when (eq ,slot-name sn) (return-from loop pos))
36 (defstruct (pv-table (:predicate pv-tablep)
37 (:constructor make-pv-table-internal
38 (slot-name-lists call-list))
40 (cache nil :type (or cache null))
41 (pv-size 0 :type fixnum)
42 (slot-name-lists nil :type list)
43 (call-list nil :type list))
45 #-sb-fluid (declaim (sb-ext:freeze-type pv-table))
47 ;;; FIXME: The comment below seem to indicate that this was intended
48 ;;; to be actually used, however, it isn't anymore, and was commented
49 ;;; out at 0.9.13.47. Also removed was code in MAKE-PV-TABLE that
50 ;;; pushed each new PV-TABLE onto this list. --NS 2006-06-18
52 ;;; help new slot-value-using-class methods affect fast iv access
54 ;;; (defvar *all-pv-table-list* nil)
56 (declaim (inline make-pv-table))
57 (defun make-pv-table (&key slot-name-lists call-list)
58 (make-pv-table-internal slot-name-lists call-list))
60 (defun make-pv-table-type-declaration (var)
61 `(type pv-table ,var))
63 (defvar *slot-name-lists-inner* (make-hash-table :test 'equal))
64 (defvar *slot-name-lists-outer* (make-hash-table :test 'equal))
66 ;;; Entries in this are lists of (table . pv-offset-list).
67 (defvar *pv-key-to-pv-table-table* (make-hash-table :test 'equal))
69 (defun intern-pv-table (&key slot-name-lists call-list)
72 (or (gethash x *slot-name-lists-inner*)
73 (setf (gethash x *slot-name-lists-inner*) (copy-list x))))
75 (or (gethash x *slot-name-lists-outer*)
76 (setf (gethash x *slot-name-lists-outer*)
77 (let ((snl (copy-list (cdr x)))
80 (make-pv-table :slot-name-lists snl
83 (outer (mapcar #'inner (cons call-list slot-name-lists)))))
86 (dolist (slot-name-list slot-name-lists)
87 (dolist (slot-name (cdr slot-name-list))
88 (note-pv-table-reference slot-name pv-index pv-table)
90 (dolist (gf-call call-list)
91 (note-pv-table-reference gf-call pv-index pv-table)
93 (setf (pv-table-pv-size pv-table) pv-index)))
96 (defun note-pv-table-reference (ref pv-offset pv-table)
97 (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
99 (let ((table-entry (assq pv-table entry)))
100 (when (and (null table-entry)
101 (> (length entry) 8))
102 (let ((new-table-table (make-hash-table :size 16 :test 'eq)))
103 (dolist (table-entry entry)
104 (setf (gethash (car table-entry) new-table-table)
106 (setf (gethash ref *pv-key-to-pv-table-table*) new-table-table)))
108 (if (null table-entry)
109 (let ((new (cons pv-table pv-offset)))
111 (push new (cdr entry))
112 (setf (gethash ref *pv-key-to-pv-table-table*)
114 (push pv-offset (cdr table-entry)))
115 (return-from note-pv-table-reference nil))))
116 (let ((list (gethash pv-table entry)))
118 (push pv-offset (cdr list))
119 (setf (gethash pv-table entry) (list pv-offset)))))
122 (defun map-pv-table-references-of (ref function)
123 (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
125 (dolist (table+pv-offset-list entry)
127 (car table+pv-offset-list)
128 (cdr table+pv-offset-list)))
129 (maphash function entry)))
132 (defun optimize-slot-value-by-class-p (class slot-name type)
133 (or (not (eq *boot-state* 'complete))
134 (let ((slotd (find-slot-definition class slot-name)))
136 (slot-accessor-std-p slotd type)))))
138 (defun compute-pv-slot (slot-name wrapper class class-slots)
139 (if (symbolp slot-name)
140 (when (optimize-slot-value-by-class-p class slot-name 'all)
141 (or (instance-slot-index wrapper slot-name)
142 (assq slot-name class-slots)))
143 (when (consp slot-name)
144 (case (first slot-name)
146 (when (eq *boot-state* 'complete)
147 (let ((gf (gdefinition (second slot-name))))
148 (when (generic-function-p gf)
149 (accessor-values1 gf (first slot-name) class)))))
150 (t (bug "Don't know how to deal with ~S in ~S"
151 slot-name 'compute-pv-slots))))))
153 (defun compute-pv (slot-name-lists wrappers)
154 (unless (listp wrappers)
155 (setq wrappers (list wrappers)))
157 (dolist (slot-names slot-name-lists
158 (make-permutation-vector (nreverse elements)))
160 (let* ((wrapper (pop wrappers))
161 (std-p (typep wrapper 'wrapper))
162 (class (wrapper-class* wrapper))
163 (class-slots (and std-p (wrapper-class-slots wrapper))))
164 (dolist (slot-name (cdr slot-names))
166 (compute-pv-slot slot-name wrapper class class-slots)
170 (defun compute-calls (call-list wrappers)
171 (declare (ignore call-list wrappers))
175 (compute-emf-from-wrappers call wrappers))
180 #|| ; Need to finish this, then write the maintenance functions.
181 (defun compute-emf-from-wrappers (call wrappers)
183 (destructuring-bind (gf-name nreq restp arg-info) call
184 (if (eq gf-name 'make-instance)
185 (error "should not get here") ; there is another mechanism for this.
187 (if (not (eq *boot-state* 'complete))
188 (apply (gdefinition gf-name) args)
189 (let* ((gf (gdefinition gf-name))
190 (arg-info (arg-info-reader gf))
193 (emf (cache-miss-values-internal gf arg-info
194 wrappers classes types
196 (update-all-pv-tables call wrappers emf)
197 (invoke-emf emf args))))))))
200 (defun make-permutation-vector (indexes)
201 (make-array (length indexes) :initial-contents indexes))
203 (defun pv-table-lookup (pv-table pv-wrappers)
204 (let* ((slot-name-lists (pv-table-slot-name-lists pv-table))
205 (call-list (pv-table-call-list pv-table))
206 (cache (or (pv-table-cache pv-table)
207 (setf (pv-table-cache pv-table)
208 (make-cache :key-count (- (length slot-name-lists)
209 (count nil slot-name-lists))
212 (multiple-value-bind (hitp value) (probe-cache cache pv-wrappers)
215 (let* ((pv (compute-pv slot-name-lists pv-wrappers))
216 (calls (compute-calls call-list pv-wrappers))
217 (pv-cell (cons pv calls))
218 (new-cache (fill-cache cache pv-wrappers pv-cell)))
219 ;; This is safe: if another thread races us here the loser just
220 ;; misses the next time as well.
221 (unless (eq new-cache cache)
222 (setf (pv-table-cache pv-table) new-cache))
225 (defun make-pv-type-declaration (var)
226 `(type simple-vector ,var))
228 (defmacro copy-pv (pv)
231 (defun make-calls-type-declaration (var)
232 `(type simple-vector ,var))
234 (defmacro callsref (calls index)
235 `(svref ,calls ,index))
237 (defvar *pv-table-cache-update-info* nil)
239 (defun update-pv-table-cache-info (class)
240 (let ((slot-names-for-pv-table-update nil)
242 (dolist (icu *pv-table-cache-update-info*)
243 (if (eq (car icu) class)
244 (pushnew (cdr icu) slot-names-for-pv-table-update)
245 (push icu new-icui)))
246 (setq *pv-table-cache-update-info* new-icui)
247 (when slot-names-for-pv-table-update
248 (update-all-pv-table-caches class slot-names-for-pv-table-update))))
250 (defun update-all-pv-table-caches (class slot-names)
251 (let* ((cwrapper (class-wrapper class))
252 (std-p (typep cwrapper 'wrapper))
253 (class-slots (and std-p (wrapper-class-slots cwrapper)))
259 (compute-pv-slot slot-name cwrapper class class-slots)
263 (dolist (slot-name slot-names)
264 (map-pv-table-references-of
266 (lambda (pv-table pv-offset-list)
267 (declare (ignore pv-offset-list))
268 (pushnew pv-table pv-tables))))
269 (dolist (pv-table pv-tables)
270 (let* ((cache (pv-table-cache pv-table))
271 (slot-name-lists (pv-table-slot-name-lists pv-table))
272 (pv-size (pv-table-pv-size pv-table))
273 (pv-map (make-array pv-size :initial-element nil)))
274 (let ((map-index 0) (param-index 0))
275 (dolist (slot-name-list slot-name-lists)
276 (dolist (slot-name (cdr slot-name-list))
277 (let ((a (assoc slot-name new-values)))
278 (setf (svref pv-map map-index)
279 (and a (cons param-index (cdr a)))))
283 (map-cache (lambda (wrappers pv-cell)
284 (update-slots-in-pv wrappers (car pv-cell)
285 cwrapper pv-size pv-map))
288 (defun update-slots-in-pv (wrappers pv cwrapper pv-size pv-map)
290 (when (eq cwrapper wrappers)
291 (dotimes-fixnum (i pv-size)
292 (let ((map (svref pv-map i)))
294 (aver (= (car map) 0))
295 (setf (svref pv i) (cdr map))))))
296 (when (memq cwrapper wrappers)
298 (dolist (wrapper wrappers)
299 (when (eq wrapper cwrapper)
300 (dotimes-fixnum (i pv-size)
301 (let ((map (svref pv-map i)))
302 (when (and map (= (car map) param))
303 (setf (svref pv i) (cdr map))))))
306 (defun can-optimize-access (form required-parameters env)
307 (let ((type (ecase (car form)
309 (set-slot-value 'writer)
310 (slot-boundp 'boundp)))
312 (slot-name (eval (caddr form)))) ; known to be constant
313 (when (and (consp var) (eq 'the (car var)))
314 ;; FIXME: We should assert list of length 3 here. Or maybe we
315 ;; should just define EXTRACT-THE, replace the whole (WHEN ..)
316 ;; form with (AWHEN (EXTRACT-THE VAR) (SETF VAR IT)) and then
317 ;; use EXTRACT-THE similarly to clean up the other tests against
318 ;; 'THE scattered through the PCL code.
319 (setq var (caddr var)))
321 (let* ((rebound? (caddr (var-declaration '%variable-rebinding var env)))
322 (parameter-or-nil (car (memq (or rebound? var)
323 required-parameters))))
324 (when parameter-or-nil
325 (let* ((class-name (caddr (var-declaration '%class
328 (class (find-class class-name nil)))
329 (when (or (not (eq *boot-state* 'complete))
330 (and class (not (class-finalized-p class))))
332 (when (and class-name (not (eq class-name t)))
333 (when (or (null type)
335 (memq *the-class-structure-object*
336 (class-precedence-list class))))
337 (optimize-slot-value-by-class-p class slot-name type))
338 (cons parameter-or-nil (or class class-name))))))))))
340 ;;; Check whether the binding of the named variable is modified in the
342 (defun parameter-modified-p (parameter-name env)
343 (let ((modified-variables (macroexpand '%parameter-binding-modified env)))
344 (memq parameter-name modified-variables)))
346 (defun optimize-slot-value (slots sparameter form)
348 (let ((optimized-form
349 (destructuring-bind (ignore1 ignore2 slot-name-form) form
350 (declare (ignore ignore1 ignore2))
351 (let ((slot-name (eval slot-name-form)))
352 (optimize-instance-access slots :read sparameter
354 ;; We don't return the optimized form directly, since there's
355 ;; still a chance that we'll find out later on that the
356 ;; optimization should not have been done, for example due to
357 ;; the walker encountering a SETQ on SPARAMETER later on in
358 ;; the body [ see for example clos.impure.lisp test with :name
359 ;; ((:setq :method-parameter) slot-value)) ]. Instead we defer
360 ;; the decision until the compiler macroexpands
361 ;; OPTIMIZED-SLOT-VALUE.
363 ;; Note that we must still call OPTIMIZE-INSTANCE-ACCESS at
364 ;; this point (instead of when expanding
365 ;; OPTIMIZED-SLOT-VALUE), since it mutates the structure of
366 ;; SLOTS. If that mutation isn't done during the walking,
367 ;; MAKE-METHOD-LAMBDA-INTERNAL won't wrap a correct PV-BINDING
368 ;; form around the body, and compilation will fail. -- JES,
370 `(optimized-slot-value ,form ,(car sparameter) ,optimized-form))
371 `(accessor-slot-value ,@(cdr form))))
373 (defmacro optimized-slot-value (form parameter-name optimized-form
375 ;; Either use OPTIMIZED-FORM or fall back to the safe
376 ;; ACCESSOR-SLOT-VALUE.
377 (if (parameter-modified-p parameter-name env)
378 `(accessor-slot-value ,@(cdr form))
381 (defun optimize-set-slot-value (slots sparameter form)
383 (let ((optimized-form
384 (destructuring-bind (ignore1 ignore2 slot-name-form new-value) form
385 (declare (ignore ignore1 ignore2))
386 (let ((slot-name (eval slot-name-form)))
387 (optimize-instance-access slots
392 ;; See OPTIMIZE-SLOT-VALUE
393 `(optimized-set-slot-value ,form ,(car sparameter) ,optimized-form))
394 `(accessor-set-slot-value ,@(cdr form))))
396 (defmacro optimized-set-slot-value (form parameter-name optimized-form
398 (cond ((safe-code-p env)
399 ;; Don't optimize slot value setting in safe code, since the
400 ;; optimized version will fail to catch some type errors
401 ;; (for example when a subclass declares a tighter type for
402 ;; the slot than a superclass).
403 `(safe-set-slot-value ,@(cdr form)))
404 ((parameter-modified-p parameter-name env)
405 `(accessor-set-slot-value ,@(cdr form)))
409 (defun optimize-slot-boundp (slots sparameter form)
411 (let ((optimized-form
413 ;; FIXME: In CMU CL ca. 19991205, this binding list
414 ;; had a fourth element in it, NEW-VALUE. It's hard
415 ;; to see how that could possibly be right, since
416 ;; SLOT-BOUNDP has no NEW-VALUE. Since it was
417 ;; causing a failure in building PCL for SBCL, so I
418 ;; changed it to match the definition of
419 ;; SLOT-BOUNDP (and also to match the list used in
420 ;; the similar OPTIMIZE-SLOT-VALUE,
421 ;; above). However, I'm weirded out by this, since
422 ;; this is old code which has worked for ages to
423 ;; build PCL for CMU CL, so it's hard to see why it
424 ;; should need a patch like this in order to build
425 ;; PCL for SBCL. I'd like to return to this and
426 ;; find a test case which exercises this function
427 ;; both in CMU CL, to see whether it's really a
428 ;; previously-unexercised bug or whether I've
429 ;; misunderstood something (and, presumably,
430 ;; patched it wrong).
431 (slot-boundp-symbol instance slot-name-form)
433 (declare (ignore slot-boundp-symbol instance))
434 (let ((slot-name (eval slot-name-form)))
435 (optimize-instance-access slots
440 ;; See OPTIMIZE-SLOT-VALUE
441 `(optimized-slot-boundp ,form ,(car sparameter) ,optimized-form))
442 `(accessor-slot-boundp ,@(cdr form))))
444 (defmacro optimized-slot-boundp (form parameter-name optimized-form
446 (if (parameter-modified-p parameter-name env)
447 `(accessor-slot-boundp ,@(cdr form))
450 ;;; The SLOTS argument is an alist, the CAR of each entry is the name
451 ;;; of a required parameter to the function. The alist is in order, so
452 ;;; the position of an entry in the alist corresponds to the
453 ;;; argument's position in the lambda list.
454 (defun optimize-instance-access (slots
459 (let ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
460 (parameter (if (consp sparameter) (car sparameter) sparameter)))
461 (if (and (eq *boot-state* 'complete)
463 (memq *the-class-structure-object* (class-precedence-list class)))
464 (let ((slotd (find-slot-definition class slot-name)))
467 `(,(slot-definition-defstruct-accessor-symbol slotd) ,parameter))
469 `(setf (,(slot-definition-defstruct-accessor-symbol slotd)
474 (let* ((parameter-entry (assq parameter slots))
475 (slot-entry (assq slot-name (cdr parameter-entry)))
476 (position (posq parameter-entry slots))
477 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
478 (unless parameter-entry
479 (bug "slot optimization bewilderment: O-I-A"))
481 (setq slot-entry (list slot-name))
482 (push slot-entry (cdr parameter-entry)))
483 (push pv-offset-form (cdr slot-entry))
486 `(instance-read ,pv-offset-form ,parameter ,position
487 ',slot-name ',class))
489 `(let ((.new-value. ,new-value))
490 (instance-write ,pv-offset-form ,parameter ,position
491 ',slot-name ',class .new-value.)))
493 `(instance-boundp ,pv-offset-form ,parameter ,position
494 ',slot-name ',class)))))))
496 (define-walker-template pv-offset) ; These forms get munged by mutate slots.
497 (defmacro pv-offset (arg) arg)
498 (define-walker-template instance-accessor-parameter)
499 (defmacro instance-accessor-parameter (x) x)
501 ;;; It is safe for these two functions to be wrong. They just try to
502 ;;; guess what the most likely case will be.
503 (defun generate-fast-class-slot-access-p (class-form slot-name-form)
504 (let ((class (and (constantp class-form) (constant-form-value class-form)))
505 (slot-name (and (constantp slot-name-form)
506 (constant-form-value slot-name-form))))
507 (and (eq *boot-state* 'complete)
508 (standard-class-p class)
509 (not (eq class *the-class-t*)) ; shouldn't happen, though.
510 (let ((slotd (find-slot-definition class slot-name)))
511 (and slotd (eq :class (slot-definition-allocation slotd)))))))
513 (defun skip-fast-slot-access-p (class-form slot-name-form type)
514 (let ((class (and (constantp class-form) (constant-form-value class-form)))
515 (slot-name (and (constantp slot-name-form)
516 (constant-form-value slot-name-form))))
517 (and (eq *boot-state* 'complete)
518 (standard-class-p class)
519 (not (eq class *the-class-t*)) ; shouldn't happen, though.
520 (let ((slotd (find-slot-definition class slot-name)))
521 (and slotd (skip-optimize-slot-value-by-class-p class
525 (defun skip-optimize-slot-value-by-class-p (class slot-name type)
526 (let ((slotd (find-slot-definition class slot-name)))
528 (eq *boot-state* 'complete)
529 (not (slot-accessor-std-p slotd type)))))
531 (defmacro instance-read-internal (pv slots pv-offset default &optional kind)
532 (unless (member kind '(nil :instance :class :default))
533 (error "illegal kind argument to ~S: ~S" 'instance-read-internal kind))
534 (if (eq kind :default)
536 (let* ((index (gensym))
538 `(locally (declare #.*optimize-speed*)
539 (let ((,index (svref ,pv ,pv-offset)))
540 (setq ,value (typecase ,index
541 ;; FIXME: the line marked by KLUDGE below
542 ;; (and the analogous spot in
543 ;; INSTANCE-WRITE-INTERNAL) is there purely
544 ;; to suppress a type mismatch warning that
545 ;; propagates through to user code.
546 ;; Presumably SLOTS at this point can never
547 ;; actually be NIL, but the compiler seems
548 ;; to think it could, so we put this here
549 ;; to shut it up. (see also mail Rudi
550 ;; Schlatte sbcl-devel 2003-09-21) -- CSR,
552 ,@(when (or (null kind) (eq kind :instance))
555 (clos-slots-ref ,slots ,index)))))
556 ,@(when (or (null kind) (eq kind :class))
557 `((cons (cdr ,index))))
559 (if (eq ,value +slot-unbound+)
563 (defmacro instance-read (pv-offset parameter position slot-name class)
564 (if (skip-fast-slot-access-p class slot-name 'reader)
565 `(accessor-slot-value ,parameter ,slot-name)
566 `(instance-read-internal .pv. ,(slot-vector-symbol position)
567 ,pv-offset (accessor-slot-value ,parameter ,slot-name)
568 ,(if (generate-fast-class-slot-access-p class slot-name)
571 (defmacro instance-write-internal (pv slots pv-offset new-value default
573 (unless (member kind '(nil :instance :class :default))
574 (error "illegal kind argument to ~S: ~S" 'instance-write-internal kind))
575 (if (eq kind :default)
577 (let* ((index (gensym)))
578 `(locally (declare #.*optimize-speed*)
579 (let ((,index (svref ,pv ,pv-offset)))
581 ,@(when (or (null kind) (eq kind :instance))
582 `((fixnum (and ,slots
583 (setf (clos-slots-ref ,slots ,index)
585 ,@(when (or (null kind) (eq kind :class))
586 `((cons (setf (cdr ,index) ,new-value))))
589 (defmacro instance-write (pv-offset
595 (if (skip-fast-slot-access-p class slot-name 'writer)
596 `(accessor-set-slot-value ,parameter ,slot-name ,new-value)
597 `(instance-write-internal .pv. ,(slot-vector-symbol position)
598 ,pv-offset ,new-value
599 (accessor-set-slot-value ,parameter ,slot-name ,new-value)
600 ,(if (generate-fast-class-slot-access-p class slot-name)
603 (defmacro instance-boundp-internal (pv slots pv-offset default
605 (unless (member kind '(nil :instance :class :default))
606 (error "illegal kind argument to ~S: ~S" 'instance-boundp-internal kind))
607 (if (eq kind :default)
609 (let* ((index (gensym)))
610 `(locally (declare #.*optimize-speed*)
611 (let ((,index (svref ,pv ,pv-offset)))
613 ,@(when (or (null kind) (eq kind :instance))
614 `((fixnum (not (and ,slots
615 (eq (clos-slots-ref ,slots ,index)
617 ,@(when (or (null kind) (eq kind :class))
618 `((cons (not (eq (cdr ,index) +slot-unbound+)))))
621 (defmacro instance-boundp (pv-offset parameter position slot-name class)
622 (if (skip-fast-slot-access-p class slot-name 'boundp)
623 `(accessor-slot-boundp ,parameter ,slot-name)
624 `(instance-boundp-internal .pv. ,(slot-vector-symbol position)
625 ,pv-offset (accessor-slot-boundp ,parameter ,slot-name)
626 ,(if (generate-fast-class-slot-access-p class slot-name)
629 ;;; This magic function has quite a job to do indeed.
631 ;;; The careful reader will recall that <slots> contains all of the
632 ;;; optimized slot access forms produced by OPTIMIZE-INSTANCE-ACCESS.
633 ;;; Each of these is a call to either INSTANCE-READ or INSTANCE-WRITE.
635 ;;; At the time these calls were produced, the first argument was
636 ;;; specified as the symbol .PV-OFFSET.; what we have to do now is
637 ;;; convert those pv-offset arguments into the actual number that is
638 ;;; the correct offset into the pv.
640 ;;; But first, oh but first, we sort <slots> a bit so that for each
641 ;;; argument we have the slots in alphabetical order. This
642 ;;; canonicalizes the PV-TABLE's a bit and will hopefully lead to
643 ;;; having fewer PV's floating around. Even if the gain is only
644 ;;; modest, it costs nothing.
645 (defun slot-name-lists-from-slots (slots calls)
646 (multiple-value-bind (slots calls) (mutate-slots-and-calls slots calls)
647 (let* ((slot-name-lists
648 (mapcar (lambda (parameter-entry)
649 (cons nil (mapcar #'car (cdr parameter-entry))))
652 (mapcar #'car calls)))
653 (dolist (call call-list)
654 (dolist (arg (cdr call))
656 (setf (car (nth arg slot-name-lists)) t))))
657 (setq slot-name-lists (mapcar (lambda (r+snl)
658 (when (or (car r+snl) (cdr r+snl))
661 (let ((cvt (apply #'vector
663 (mapcar (lambda (r+snl)
664 (when r+snl (incf i)))
666 (setq call-list (mapcar (lambda (call)
668 (mapcar (lambda (arg)
674 (values slot-name-lists call-list))))
676 (defun mutate-slots-and-calls (slots calls)
677 (let ((sorted-slots (sort-slots slots))
678 (sorted-calls (sort-calls (cdr calls)))
680 (dolist (parameter-entry sorted-slots)
681 (dolist (slot-entry (cdr parameter-entry))
683 (dolist (form (cdr slot-entry))
684 (setf (cadr form) pv-offset))))
685 (dolist (call-entry sorted-calls)
687 (dolist (form (cdr call-entry))
688 (setf (cadr form) pv-offset)))
689 (values sorted-slots sorted-calls)))
691 (defun symbol-pkg-name (sym)
692 (let ((pkg (symbol-package sym)))
693 (if pkg (package-name pkg) "")))
695 ;;; FIXME: Because of the existence of UNINTERN and RENAME-PACKAGE,
696 ;;; the part of this ordering which is based on SYMBOL-PKG-NAME is not
697 ;;; stable. This ordering is only used in to
698 ;;; SLOT-NAME-LISTS-FROM-SLOTS, where it serves to "canonicalize the
699 ;;; PV-TABLE's a bit and will hopefully lead to having fewer PV's
700 ;;; floating around", so it sounds as though the instability won't
701 ;;; actually lead to bugs, just small inefficiency. But still, it
702 ;;; would be better to reimplement this function as a comparison based
704 ;;; * stable comparison
705 ;;; * smaller code (here, and in being able to discard SYMBOL-PKG-NAME)
707 (defun symbol-lessp (a b)
708 (if (eq (symbol-package a)
710 (string-lessp (symbol-name a)
712 (string-lessp (symbol-pkg-name a)
713 (symbol-pkg-name b))))
715 (defun symbol-or-cons-lessp (a b)
718 (symbol (symbol-lessp a b))
722 (cons (if (eq (car a) (car b))
723 (symbol-or-cons-lessp (cdr a) (cdr b))
724 (symbol-or-cons-lessp (car a) (car b))))))))
726 (defun sort-slots (slots)
727 (mapcar (lambda (parameter-entry)
728 (cons (car parameter-entry)
729 (sort (cdr parameter-entry) ;slot entries
730 #'symbol-or-cons-lessp
734 (defun sort-calls (calls)
735 (sort calls #'symbol-or-cons-lessp :key #'car))
737 ;;;; This needs to work in terms of metatypes and also needs to work
738 ;;;; for automatically generated reader and writer functions.
739 ;;;; Automatically generated reader and writer functions use this
742 (defmacro pv-binding ((required-parameters slot-name-lists pv-table-form)
744 (let (slot-vars pv-parameters)
745 (loop for slots in slot-name-lists
746 for required-parameter in required-parameters
749 (push required-parameter pv-parameters)
750 (push (slot-vector-symbol i) slot-vars)))
751 `(pv-binding1 (.pv. .calls. ,pv-table-form
752 ,(nreverse pv-parameters) ,(nreverse slot-vars))
755 (defmacro pv-binding1 ((pv calls pv-table-form pv-parameters slot-vars)
757 `(pv-env (,pv ,calls ,pv-table-form ,pv-parameters)
758 (let (,@(mapcar (lambda (slot-var p) `(,slot-var (get-slots-or-nil ,p)))
759 slot-vars pv-parameters))
760 (declare (ignorable ,@(mapcar #'identity slot-vars)))
763 ;;; This will only be visible in PV-ENV when the default MAKE-METHOD-LAMBDA is
765 (define-symbol-macro pv-env-environment overridden)
767 (defmacro pv-env (&environment env
768 (pv calls pv-table-form pv-parameters)
770 ;; Decide which expansion to use based on the state of the PV-ENV-ENVIRONMENT
772 (if (eq (macroexpand 'pv-env-environment env) 'default)
773 `(let ((,pv (car .pv-cell.))
774 (,calls (cdr .pv-cell.)))
775 (declare ,(make-pv-type-declaration pv)
776 ,(make-calls-type-declaration calls))
779 `(let* ((.pv-table. ,pv-table-form)
780 (.pv-cell. (pv-table-lookup-pv-args .pv-table. ,@pv-parameters))
781 (,pv (car .pv-cell.))
782 (,calls (cdr .pv-cell.)))
783 (declare ,(make-pv-type-declaration pv))
784 (declare ,(make-calls-type-declaration calls))
788 (defvar *non-var-declarations*
789 ;; FIXME: VALUES was in this list, conditionalized with #+CMU, but I
790 ;; don't *think* CMU CL had, or SBCL has, VALUES declarations. If
791 ;; SBCL doesn't have 'em, VALUES should probably be removed from
802 (defvar *var-declarations-with-arg*
806 (defvar *var-declarations-without-arg*
808 ignorable special dynamic-extent
809 ;; FIXME: Possibly this entire list and variable could go away.
810 ;; If not, certainly we should remove all these built-in typenames
811 ;; from the list, and replace them with a test for "is it a type
812 ;; name?" (CLTL1 allowed only built-in type names as declarations,
813 ;; but ANSI CL allows any type name as a declaration.)
814 array atom base-char bignum bit bit-vector character compiled-function
815 complex cons double-float extended-char
816 fixnum float function hash-table integer
817 keyword list long-float nil null number package pathname random-state ratio
818 rational readtable sequence short-float signed-byte simple-array
819 simple-bit-vector simple-string simple-vector single-float standard-char
820 stream string symbol t unsigned-byte vector))
822 (defun split-declarations (body args maybe-reads-params-p)
823 (let ((inner-decls nil)
826 (loop (when (null body) (return nil))
827 (setq decl (car body))
828 (unless (and (consp decl)
829 (eq (car decl) 'declare))
831 (dolist (form (cdr decl))
833 (let ((declaration-name (car form)))
834 (if (member declaration-name *non-var-declarations*)
835 (push `(declare ,form) outer-decls)
837 (member declaration-name
838 *var-declarations-with-arg*))
840 (member declaration-name
841 *var-declarations-without-arg*))
842 (dname (list (pop form)))
843 (inners nil) (outers nil))
844 (unless (or arg-p non-arg-p)
845 ;; FIXME: This warning, and perhaps the
846 ;; various *VAR-DECLARATIONS-FOO* and/or
847 ;; *NON-VAR-DECLARATIONS* variables,
848 ;; could probably go away now that we're not
849 ;; trying to be portable between different
850 ;; CLTL1 hosts the way PCL was. (Note that to
851 ;; do this right, we need to be able to handle
852 ;; user-defined (DECLAIM (DECLARATION FOO))
854 (warn "The declaration ~S is not understood by ~S.~@
855 Please put ~S on one of the lists ~S,~%~S, or~%~S.~@
856 (Assuming it is a variable declaration without argument)."
857 declaration-name 'split-declarations
859 '*non-var-declarations*
860 '*var-declarations-with-arg*
861 '*var-declarations-without-arg*)
862 (push declaration-name *var-declarations-without-arg*))
864 (setq dname (append dname (list (pop form)))))
866 (%class (push `(declare (,@dname ,@form)) inner-decls))
869 (if (member var args)
870 ;; Quietly remove IGNORE declarations
871 ;; on args when a next-method is
872 ;; involved, to prevent compiler
873 ;; warnings about ignored args being
875 (unless (and maybe-reads-params-p
876 (eq (car dname) 'ignore))
880 (push `(declare (,@dname ,@outers)) outer-decls))
883 `(declare (,@dname ,@inners))
885 (setq body (cdr body)))
886 (values outer-decls inner-decls body)))
888 ;;; Pull a name out of the %METHOD-NAME declaration in the function
889 ;;; body given, or return NIL if no %METHOD-NAME declaration is found.
890 (defun body-method-name (body)
891 (multiple-value-bind (real-body declarations documentation)
893 (declare (ignore real-body documentation))
894 (let ((name-decl (get-declaration '%method-name declarations)))
896 (destructuring-bind (name) name-decl
899 ;;; Convert a lambda expression containing a SB-PCL::%METHOD-NAME
900 ;;; declaration (which is a naming style internal to PCL) into an
901 ;;; SB-INT:NAMED-LAMBDA expression (which is a naming style used
902 ;;; throughout SBCL, understood by the main compiler); or if there's
903 ;;; no SB-PCL::%METHOD-NAME declaration, then just return the original
904 ;;; lambda expression.
905 (defun name-method-lambda (method-lambda)
906 (let ((method-name (body-method-name (cddr method-lambda))))
908 `(named-lambda (slow-method ,method-name) ,(rest method-lambda))
911 (defun make-method-initargs-form-internal (method-lambda initargs env)
912 (declare (ignore env))
913 (let (method-lambda-args
914 lmf ; becomes body of function
916 (if (not (and (= 3 (length method-lambda))
917 (= 2 (length (setq method-lambda-args (cadr method-lambda))))
918 (consp (setq lmf (third method-lambda)))
919 (eq 'simple-lexical-method-functions (car lmf))
920 (eq (car method-lambda-args)
921 (cadr (setq lmf-params (cadr lmf))))
922 (eq (cadr method-lambda-args)
923 (caddr lmf-params))))
924 `(list* :function ,(name-method-lambda method-lambda)
926 (let* ((lambda-list (car lmf-params))
930 (dolist (arg lambda-list)
931 (when (member arg '(&optional &rest &key))
938 (setq args (nreverse args))
939 (setf (getf (getf initargs 'plist) :arg-info) (cons nreq restp))
940 (make-method-initargs-form-internal1
941 initargs (cddr lmf) args lmf-params restp)))))
943 (defun lambda-list-parameter-names (lambda-list)
944 ;; Given a valid lambda list, extract the parameter names.
945 (loop for x in lambda-list
947 do (unless (member x lambda-list-keywords)
949 (let ((name (car x)))
951 ;; ... ((:BAR FOO) 1)
952 (push (second name) res)
956 (let ((name-p (cddr x)))
958 (push (car name-p) res))))
961 finally (return res)))
963 (defun make-method-initargs-form-internal1
964 (initargs body req-args lmf-params restp)
965 (let* (;; The lambda-list of the method, minus specifiers
966 (lambda-list (car lmf-params))
967 ;; Names of the parameters that will be in the outermost lambda-list
968 ;; (and whose bound declarations thus need to be in OUTER-DECLS).
969 (outer-parameters req-args)
970 ;; The lambda-list used by BIND-ARGS
971 (bind-list lambda-list)
972 (setq-p (getf (cdr lmf-params) :setq-p))
973 (auxp (member '&aux bind-list))
974 (call-next-method-p (getf (cdr lmf-params) :call-next-method-p)))
975 ;; Try to use the normal function call machinery instead of BIND-ARGS
976 ;; binding the arguments, unless:
977 (unless (or ;; If all arguments are required, BIND-ARGS will be a no-op
979 (and (not restp) (not auxp))
980 ;; CALL-NEXT-METHOD wants to use BIND-ARGS, and needs a
981 ;; list of all non-required arguments.
983 (setf ;; We don't want a binding for .REST-ARG.
985 ;; Get all the parameters for declaration parsing
986 outer-parameters (lambda-list-parameter-names lambda-list)
987 ;; Ensure that BIND-ARGS won't do anything (since
988 ;; BIND-LIST won't contain any non-required parameters,
989 ;; and REQ-ARGS will be of an equal length). We still want
990 ;; to pass BIND-LIST to FAST-LEXICAL-METHOD-FUNCTIONS so
991 ;; that BIND-FAST-LEXICAL-METHOD-FUNCTIONS can take care
992 ;; of rebinding SETQd required arguments around the method
995 (multiple-value-bind (outer-decls inner-decls body-sans-decls)
997 body outer-parameters (or call-next-method-p setq-p))
998 (let* ((rest-arg (when restp
1000 (fmf-lambda-list (if rest-arg
1001 (append req-args (list '&rest rest-arg))
1002 (if call-next-method-p
1007 (let* ((fmf (,(if (body-method-name body) 'named-lambda 'lambda)
1008 ,@(when (body-method-name body)
1010 (list (cons 'fast-method (body-method-name body))))
1011 ;; The lambda-list of the FMF
1012 (.pv-cell. .next-method-call. ,@fmf-lambda-list)
1013 ;; body of the function
1014 (declare (ignorable .pv-cell. .next-method-call.)
1015 (disable-package-locks pv-env-environment))
1017 (symbol-macrolet ((pv-env-environment default))
1018 (fast-lexical-method-functions
1019 (,bind-list .next-method-call. ,req-args ,rest-arg
1020 ,@(cdddr lmf-params))
1022 ,@body-sans-decls))))
1023 (mf (%make-method-function fmf nil)))
1024 (set-funcallable-instance-function
1025 mf (method-function-from-fast-function fmf ',(getf initargs 'plist)))
1029 ;;; Use arrays and hash tables and the fngen stuff to make this much
1030 ;;; better. It doesn't really matter, though, because a function
1031 ;;; returned by this will get called only when the user explicitly
1032 ;;; funcalls a result of method-function. BUT, this is needed to make
1033 ;;; early methods work.
1034 (defun method-function-from-fast-function (fmf plist)
1035 (declare (type function fmf))
1036 (let* ((method-function nil)
1037 (calls (getf plist :call-list))
1038 (snl (getf plist :slot-name-lists))
1039 (pv-table (when (or calls snl)
1040 (intern-pv-table :call-list calls :slot-name-lists snl)))
1041 (arg-info (getf plist :arg-info))
1042 (nreq (car arg-info))
1043 (restp (cdr arg-info)))
1044 (setq method-function
1045 (lambda (method-args next-methods)
1046 (let* ((pv-cell (when pv-table
1047 (get-pv-cell method-args pv-table)))
1048 (nm (car next-methods))
1049 (nms (cdr next-methods))
1052 :function (if (std-instance-p nm)
1053 (method-function nm)
1055 :call-method-args (list nms)))))
1056 (apply fmf pv-cell nmc method-args))))
1057 ;; FIXME: this looks dangerous.
1058 (let* ((fname (%fun-name fmf)))
1059 (when (and fname (eq (car fname) 'fast-method))
1060 (set-fun-name method-function (cons 'slow-method (cdr fname)))))
1063 ;;; this is similar to the above, only not quite. Only called when
1064 ;;; the MOP is heavily involved. Not quite parallel to
1065 ;;; METHOD-FUNCTION-FROM-FAST-METHOD-FUNCTION, because we can close
1066 ;;; over the actual PV-CELL in this case.
1067 (defun method-function-from-fast-method-call (fmc)
1068 (let* ((fmf (fast-method-call-function fmc))
1069 (pv-cell (fast-method-call-pv-cell fmc))
1070 (arg-info (fast-method-call-arg-info fmc))
1071 (nreq (car arg-info))
1072 (restp (cdr arg-info)))
1073 (lambda (method-args next-methods)
1074 (let* ((nm (car next-methods))
1075 (nms (cdr next-methods))
1078 :function (if (std-instance-p nm)
1079 (method-function nm)
1081 :call-method-args (list nms)))))
1082 (apply fmf pv-cell nmc method-args)))))
1084 (defun get-pv-cell (method-args pv-table)
1085 (let ((pv-wrappers (pv-wrappers-from-all-args pv-table method-args)))
1087 (pv-table-lookup pv-table pv-wrappers))))
1089 (defun pv-table-lookup-pv-args (pv-table &rest pv-parameters)
1090 (pv-table-lookup pv-table (pv-wrappers-from-pv-args pv-parameters)))
1092 (defun pv-wrappers-from-pv-args (&rest args)
1094 (dolist (arg args (if (cdr wrappers) (nreverse wrappers) (car wrappers)))
1095 (let ((wrapper (wrapper-of arg)))
1096 (push (if (invalid-wrapper-p wrapper)
1097 (check-wrapper-validity wrapper)
1101 (defun pv-wrappers-from-all-args (pv-table args)
1102 (loop for snl in (pv-table-slot-name-lists pv-table) and arg in args
1104 collect (wrapper-of arg) into wrappers
1105 finally (return (if (cdr wrappers) wrappers (car wrappers)))))
1107 ;;; Return the subset of WRAPPERS which is used in the cache
1109 (defun pv-wrappers-from-all-wrappers (pv-table wrappers)
1110 (loop for snl in (pv-table-slot-name-lists pv-table) and w in wrappers
1112 collect w into result
1113 finally (return (if (cdr result) result (car result)))))