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 (destructuring-bind (op var-form slot-name-form &optional new-value) form
308 (let ((type (ecase op
310 (set-slot-value 'writer)
311 (slot-boundp 'boundp)))
312 (var (extract-the var-form))
313 (slot-name (constant-form-value slot-name-form env)))
315 (let* ((rebound? (caddr (var-declaration '%variable-rebinding var env)))
316 (parameter-or-nil (car (memq (or rebound? var)
317 required-parameters))))
318 (when parameter-or-nil
319 (let* ((class-name (caddr (var-declaration '%class
322 (class (find-class class-name nil)))
323 (when (or (not (eq *boot-state* 'complete))
324 (and class (not (class-finalized-p class))))
326 (when (and class-name (not (eq class-name t)))
327 (when (or (null type)
329 (memq *the-class-structure-object*
330 (class-precedence-list class))))
331 (optimize-slot-value-by-class-p class slot-name type))
332 (values (cons parameter-or-nil (or class class-name))
336 ;;; Check whether the binding of the named variable is modified in the
338 (defun parameter-modified-p (parameter-name env)
339 (let ((modified-variables (macroexpand '%parameter-binding-modified env)))
340 (memq parameter-name modified-variables)))
342 (defun optimize-slot-value (form slots required-parameters env)
343 (multiple-value-bind (sparameter slot-name)
344 (can-optimize-access form required-parameters env)
346 (let ((optimized-form
347 (optimize-instance-access slots :read sparameter
349 ;; We don't return the optimized form directly, since there's
350 ;; still a chance that we'll find out later on that the
351 ;; optimization should not have been done, for example due to
352 ;; the walker encountering a SETQ on SPARAMETER later on in
353 ;; the body [ see for example clos.impure.lisp test with :name
354 ;; ((:setq :method-parameter) slot-value)) ]. Instead we defer
355 ;; the decision until the compiler macroexpands
356 ;; OPTIMIZED-SLOT-VALUE.
358 ;; Note that we must still call OPTIMIZE-INSTANCE-ACCESS at
359 ;; this point (instead of when expanding
360 ;; OPTIMIZED-SLOT-VALUE), since it mutates the structure of
361 ;; SLOTS. If that mutation isn't done during the walking,
362 ;; MAKE-METHOD-LAMBDA-INTERNAL won't wrap a correct PV-BINDING
363 ;; form around the body, and compilation will fail. -- JES,
365 `(optimized-slot-value ,form ,(car sparameter) ,optimized-form))
366 `(accessor-slot-value ,@(cdr form)))))
368 (defmacro optimized-slot-value (form parameter-name optimized-form
370 ;; Either use OPTIMIZED-FORM or fall back to the safe
371 ;; ACCESSOR-SLOT-VALUE.
372 (if (parameter-modified-p parameter-name env)
373 `(accessor-slot-value ,@(cdr form))
376 (defun optimize-set-slot-value (form slots required-parameters env)
377 (multiple-value-bind (sparameter slot-name new-value)
378 (can-optimize-access form required-parameters env)
380 (let ((optimized-form
381 (optimize-instance-access slots :write sparameter
382 slot-name new-value)))
383 ;; See OPTIMIZE-SLOT-VALUE
384 `(optimized-set-slot-value ,form ,(car sparameter) ,optimized-form))
385 `(accessor-set-slot-value ,@(cdr form)))))
387 (defmacro optimized-set-slot-value (form parameter-name optimized-form
389 (cond ((safe-code-p env)
390 ;; Don't optimize slot value setting in safe code, since the
391 ;; optimized version will fail to catch some type errors
392 ;; (for example when a subclass declares a tighter type for
393 ;; the slot than a superclass).
394 `(safe-set-slot-value ,@(cdr form)))
395 ((parameter-modified-p parameter-name env)
396 `(accessor-set-slot-value ,@(cdr form)))
400 (defun optimize-slot-boundp (form slots required-parameters env)
401 (multiple-value-bind (sparameter slot-name)
402 (can-optimize-access form required-parameters env)
404 (let ((optimized-form
405 (optimize-instance-access slots :boundp sparameter
407 ;; See OPTIMIZE-SLOT-VALUE
408 `(optimized-slot-boundp ,form ,(car sparameter) ,optimized-form))
409 `(accessor-slot-boundp ,@(cdr form)))))
411 (defmacro optimized-slot-boundp (form parameter-name optimized-form
413 (if (parameter-modified-p parameter-name env)
414 `(accessor-slot-boundp ,@(cdr form))
417 ;;; The SLOTS argument is an alist, the CAR of each entry is the name
418 ;;; of a required parameter to the function. The alist is in order, so
419 ;;; the position of an entry in the alist corresponds to the
420 ;;; argument's position in the lambda list.
421 (defun optimize-instance-access (slots
426 (let ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
427 (parameter (if (consp sparameter) (car sparameter) sparameter)))
428 (if (and (eq *boot-state* 'complete)
430 (memq *the-class-structure-object* (class-precedence-list class)))
431 (let ((slotd (find-slot-definition class slot-name)))
434 `(,(slot-definition-defstruct-accessor-symbol slotd) ,parameter))
436 `(setf (,(slot-definition-defstruct-accessor-symbol slotd)
441 (let* ((parameter-entry (assq parameter slots))
442 (slot-entry (assq slot-name (cdr parameter-entry)))
443 (position (posq parameter-entry slots))
444 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
445 (unless parameter-entry
446 (bug "slot optimization bewilderment: O-I-A"))
448 (setq slot-entry (list slot-name))
449 (push slot-entry (cdr parameter-entry)))
450 (push pv-offset-form (cdr slot-entry))
453 `(instance-read ,pv-offset-form ,parameter ,position
454 ',slot-name ',class))
456 `(let ((.new-value. ,new-value))
457 (instance-write ,pv-offset-form ,parameter ,position
458 ',slot-name ',class .new-value.)))
460 `(instance-boundp ,pv-offset-form ,parameter ,position
461 ',slot-name ',class)))))))
463 (define-walker-template pv-offset) ; These forms get munged by mutate slots.
464 (defmacro pv-offset (arg) arg)
465 (define-walker-template instance-accessor-parameter)
466 (defmacro instance-accessor-parameter (x) x)
468 ;;; It is safe for these two functions to be wrong. They just try to
469 ;;; guess what the most likely case will be.
470 (defun generate-fast-class-slot-access-p (class-form slot-name-form)
471 (let ((class (and (constantp class-form) (constant-form-value class-form)))
472 (slot-name (and (constantp slot-name-form)
473 (constant-form-value slot-name-form))))
474 (and (eq *boot-state* 'complete)
475 (standard-class-p class)
476 (not (eq class *the-class-t*)) ; shouldn't happen, though.
477 (let ((slotd (find-slot-definition class slot-name)))
478 (and slotd (eq :class (slot-definition-allocation slotd)))))))
480 (defun skip-fast-slot-access-p (class-form slot-name-form type)
481 (let ((class (and (constantp class-form) (constant-form-value class-form)))
482 (slot-name (and (constantp slot-name-form)
483 (constant-form-value slot-name-form))))
484 (and (eq *boot-state* 'complete)
485 (standard-class-p class)
486 (not (eq class *the-class-t*)) ; shouldn't happen, though.
487 (let ((slotd (find-slot-definition class slot-name)))
488 (and slotd (skip-optimize-slot-value-by-class-p class
492 (defun skip-optimize-slot-value-by-class-p (class slot-name type)
493 (let ((slotd (find-slot-definition class slot-name)))
495 (eq *boot-state* 'complete)
496 (not (slot-accessor-std-p slotd type)))))
498 (defmacro instance-read-internal (pv slots pv-offset default &optional kind)
499 (unless (member kind '(nil :instance :class :default))
500 (error "illegal kind argument to ~S: ~S" 'instance-read-internal kind))
501 (if (eq kind :default)
503 (let* ((index (gensym))
505 `(locally (declare #.*optimize-speed*)
506 (let ((,index (svref ,pv ,pv-offset)))
507 (setq ,value (typecase ,index
508 ;; FIXME: the line marked by KLUDGE below
509 ;; (and the analogous spot in
510 ;; INSTANCE-WRITE-INTERNAL) is there purely
511 ;; to suppress a type mismatch warning that
512 ;; propagates through to user code.
513 ;; Presumably SLOTS at this point can never
514 ;; actually be NIL, but the compiler seems
515 ;; to think it could, so we put this here
516 ;; to shut it up. (see also mail Rudi
517 ;; Schlatte sbcl-devel 2003-09-21) -- CSR,
519 ,@(when (or (null kind) (eq kind :instance))
522 (clos-slots-ref ,slots ,index)))))
523 ,@(when (or (null kind) (eq kind :class))
524 `((cons (cdr ,index))))
526 (if (eq ,value +slot-unbound+)
530 (defmacro instance-read (pv-offset parameter position slot-name class)
531 (if (skip-fast-slot-access-p class slot-name 'reader)
532 `(accessor-slot-value ,parameter ,slot-name)
533 `(instance-read-internal .pv. ,(slot-vector-symbol position)
534 ,pv-offset (accessor-slot-value ,parameter ,slot-name)
535 ,(if (generate-fast-class-slot-access-p class slot-name)
538 (defmacro instance-write-internal (pv slots pv-offset new-value default
540 (unless (member kind '(nil :instance :class :default))
541 (error "illegal kind argument to ~S: ~S" 'instance-write-internal kind))
542 (if (eq kind :default)
544 (let* ((index (gensym)))
545 `(locally (declare #.*optimize-speed*)
546 (let ((,index (svref ,pv ,pv-offset)))
548 ,@(when (or (null kind) (eq kind :instance))
549 `((fixnum (and ,slots
550 (setf (clos-slots-ref ,slots ,index)
552 ,@(when (or (null kind) (eq kind :class))
553 `((cons (setf (cdr ,index) ,new-value))))
556 (defmacro instance-write (pv-offset
562 (if (skip-fast-slot-access-p class slot-name 'writer)
563 `(accessor-set-slot-value ,parameter ,slot-name ,new-value)
564 `(instance-write-internal .pv. ,(slot-vector-symbol position)
565 ,pv-offset ,new-value
566 (accessor-set-slot-value ,parameter ,slot-name ,new-value)
567 ,(if (generate-fast-class-slot-access-p class slot-name)
570 (defmacro instance-boundp-internal (pv slots pv-offset default
572 (unless (member kind '(nil :instance :class :default))
573 (error "illegal kind argument to ~S: ~S" 'instance-boundp-internal kind))
574 (if (eq kind :default)
576 (let* ((index (gensym)))
577 `(locally (declare #.*optimize-speed*)
578 (let ((,index (svref ,pv ,pv-offset)))
580 ,@(when (or (null kind) (eq kind :instance))
581 `((fixnum (not (and ,slots
582 (eq (clos-slots-ref ,slots ,index)
584 ,@(when (or (null kind) (eq kind :class))
585 `((cons (not (eq (cdr ,index) +slot-unbound+)))))
588 (defmacro instance-boundp (pv-offset parameter position slot-name class)
589 (if (skip-fast-slot-access-p class slot-name 'boundp)
590 `(accessor-slot-boundp ,parameter ,slot-name)
591 `(instance-boundp-internal .pv. ,(slot-vector-symbol position)
592 ,pv-offset (accessor-slot-boundp ,parameter ,slot-name)
593 ,(if (generate-fast-class-slot-access-p class slot-name)
596 ;;; This magic function has quite a job to do indeed.
598 ;;; The careful reader will recall that <slots> contains all of the
599 ;;; optimized slot access forms produced by OPTIMIZE-INSTANCE-ACCESS.
600 ;;; Each of these is a call to either INSTANCE-READ or INSTANCE-WRITE.
602 ;;; At the time these calls were produced, the first argument was
603 ;;; specified as the symbol .PV-OFFSET.; what we have to do now is
604 ;;; convert those pv-offset arguments into the actual number that is
605 ;;; the correct offset into the pv.
607 ;;; But first, oh but first, we sort <slots> a bit so that for each
608 ;;; argument we have the slots in alphabetical order. This
609 ;;; canonicalizes the PV-TABLE's a bit and will hopefully lead to
610 ;;; having fewer PV's floating around. Even if the gain is only
611 ;;; modest, it costs nothing.
612 (defun slot-name-lists-from-slots (slots calls)
613 (multiple-value-bind (slots calls) (mutate-slots-and-calls slots calls)
614 (let* ((slot-name-lists
615 (mapcar (lambda (parameter-entry)
616 (cons nil (mapcar #'car (cdr parameter-entry))))
619 (mapcar #'car calls)))
620 (dolist (call call-list)
621 (dolist (arg (cdr call))
623 (setf (car (nth arg slot-name-lists)) t))))
624 (setq slot-name-lists (mapcar (lambda (r+snl)
625 (when (or (car r+snl) (cdr r+snl))
628 (let ((cvt (apply #'vector
630 (mapcar (lambda (r+snl)
631 (when r+snl (incf i)))
633 (setq call-list (mapcar (lambda (call)
635 (mapcar (lambda (arg)
641 (values slot-name-lists call-list))))
643 (defun mutate-slots-and-calls (slots calls)
644 (let ((sorted-slots (sort-slots slots))
645 (sorted-calls (sort-calls (cdr calls)))
647 (dolist (parameter-entry sorted-slots)
648 (dolist (slot-entry (cdr parameter-entry))
650 (dolist (form (cdr slot-entry))
651 (setf (cadr form) pv-offset))))
652 (dolist (call-entry sorted-calls)
654 (dolist (form (cdr call-entry))
655 (setf (cadr form) pv-offset)))
656 (values sorted-slots sorted-calls)))
658 (defun symbol-pkg-name (sym)
659 (let ((pkg (symbol-package sym)))
660 (if pkg (package-name pkg) "")))
662 ;;; FIXME: Because of the existence of UNINTERN and RENAME-PACKAGE,
663 ;;; the part of this ordering which is based on SYMBOL-PKG-NAME is not
664 ;;; stable. This ordering is only used in to
665 ;;; SLOT-NAME-LISTS-FROM-SLOTS, where it serves to "canonicalize the
666 ;;; PV-TABLE's a bit and will hopefully lead to having fewer PV's
667 ;;; floating around", so it sounds as though the instability won't
668 ;;; actually lead to bugs, just small inefficiency. But still, it
669 ;;; would be better to reimplement this function as a comparison based
671 ;;; * stable comparison
672 ;;; * smaller code (here, and in being able to discard SYMBOL-PKG-NAME)
674 (defun symbol-lessp (a b)
675 (if (eq (symbol-package a)
677 (string-lessp (symbol-name a)
679 (string-lessp (symbol-pkg-name a)
680 (symbol-pkg-name b))))
682 (defun symbol-or-cons-lessp (a b)
685 (symbol (symbol-lessp a b))
689 (cons (if (eq (car a) (car b))
690 (symbol-or-cons-lessp (cdr a) (cdr b))
691 (symbol-or-cons-lessp (car a) (car b))))))))
693 (defun sort-slots (slots)
694 (mapcar (lambda (parameter-entry)
695 (cons (car parameter-entry)
696 (sort (cdr parameter-entry) ;slot entries
697 #'symbol-or-cons-lessp
701 (defun sort-calls (calls)
702 (sort calls #'symbol-or-cons-lessp :key #'car))
704 ;;;; This needs to work in terms of metatypes and also needs to work
705 ;;;; for automatically generated reader and writer functions.
706 ;;;; Automatically generated reader and writer functions use this
709 (defmacro pv-binding ((required-parameters slot-name-lists pv-table-form)
711 (let (slot-vars pv-parameters)
712 (loop for slots in slot-name-lists
713 for required-parameter in required-parameters
716 (push required-parameter pv-parameters)
717 (push (slot-vector-symbol i) slot-vars)))
718 `(pv-binding1 (.pv. .calls. ,pv-table-form
719 ,(nreverse pv-parameters) ,(nreverse slot-vars))
722 (defmacro pv-binding1 ((pv calls pv-table-form pv-parameters slot-vars)
724 `(pv-env (,pv ,calls ,pv-table-form ,pv-parameters)
725 (let (,@(mapcar (lambda (slot-var p) `(,slot-var (get-slots-or-nil ,p)))
726 slot-vars pv-parameters))
727 (declare (ignorable ,@(mapcar #'identity slot-vars)))
730 ;;; This will only be visible in PV-ENV when the default MAKE-METHOD-LAMBDA is
732 (define-symbol-macro pv-env-environment overridden)
734 (defmacro pv-env (&environment env
735 (pv calls pv-table-form pv-parameters)
737 ;; Decide which expansion to use based on the state of the PV-ENV-ENVIRONMENT
739 (if (eq (macroexpand 'pv-env-environment env) 'default)
740 `(let ((,pv (car .pv-cell.))
741 (,calls (cdr .pv-cell.)))
742 (declare ,(make-pv-type-declaration pv)
743 ,(make-calls-type-declaration calls))
746 `(let* ((.pv-table. ,pv-table-form)
747 (.pv-cell. (pv-table-lookup-pv-args .pv-table. ,@pv-parameters))
748 (,pv (car .pv-cell.))
749 (,calls (cdr .pv-cell.)))
750 (declare ,(make-pv-type-declaration pv))
751 (declare ,(make-calls-type-declaration calls))
755 (defvar *non-var-declarations*
756 ;; FIXME: VALUES was in this list, conditionalized with #+CMU, but I
757 ;; don't *think* CMU CL had, or SBCL has, VALUES declarations. If
758 ;; SBCL doesn't have 'em, VALUES should probably be removed from
769 (defvar *var-declarations-with-arg*
773 (defvar *var-declarations-without-arg*
775 ignorable special dynamic-extent
776 ;; FIXME: Possibly this entire list and variable could go away.
777 ;; If not, certainly we should remove all these built-in typenames
778 ;; from the list, and replace them with a test for "is it a type
779 ;; name?" (CLTL1 allowed only built-in type names as declarations,
780 ;; but ANSI CL allows any type name as a declaration.)
781 array atom base-char bignum bit bit-vector character compiled-function
782 complex cons double-float extended-char
783 fixnum float function hash-table integer
784 keyword list long-float nil null number package pathname random-state ratio
785 rational readtable sequence short-float signed-byte simple-array
786 simple-bit-vector simple-string simple-vector single-float standard-char
787 stream string symbol t unsigned-byte vector))
789 (defun split-declarations (body args maybe-reads-params-p)
790 (let ((inner-decls nil)
793 (loop (when (null body) (return nil))
794 (setq decl (car body))
795 (unless (and (consp decl)
796 (eq (car decl) 'declare))
798 (dolist (form (cdr decl))
800 (let ((declaration-name (car form)))
801 (if (member declaration-name *non-var-declarations*)
802 (push `(declare ,form) outer-decls)
804 (member declaration-name
805 *var-declarations-with-arg*))
807 (member declaration-name
808 *var-declarations-without-arg*))
809 (dname (list (pop form)))
810 (inners nil) (outers nil))
811 (unless (or arg-p non-arg-p)
812 ;; FIXME: This warning, and perhaps the
813 ;; various *VAR-DECLARATIONS-FOO* and/or
814 ;; *NON-VAR-DECLARATIONS* variables,
815 ;; could probably go away now that we're not
816 ;; trying to be portable between different
817 ;; CLTL1 hosts the way PCL was. (Note that to
818 ;; do this right, we need to be able to handle
819 ;; user-defined (DECLAIM (DECLARATION FOO))
821 (warn "The declaration ~S is not understood by ~S.~@
822 Please put ~S on one of the lists ~S,~%~S, or~%~S.~@
823 (Assuming it is a variable declaration without argument)."
824 declaration-name 'split-declarations
826 '*non-var-declarations*
827 '*var-declarations-with-arg*
828 '*var-declarations-without-arg*)
829 (push declaration-name *var-declarations-without-arg*))
831 (setq dname (append dname (list (pop form)))))
833 (%class (push `(declare (,@dname ,@form)) inner-decls))
836 (if (member var args)
837 ;; Quietly remove IGNORE declarations
838 ;; on args when a next-method is
839 ;; involved, to prevent compiler
840 ;; warnings about ignored args being
842 (unless (and maybe-reads-params-p
843 (eq (car dname) 'ignore))
847 (push `(declare (,@dname ,@outers)) outer-decls))
850 `(declare (,@dname ,@inners))
852 (setq body (cdr body)))
853 (values outer-decls inner-decls body)))
855 ;;; Pull a name out of the %METHOD-NAME declaration in the function
856 ;;; body given, or return NIL if no %METHOD-NAME declaration is found.
857 (defun body-method-name (body)
858 (multiple-value-bind (real-body declarations documentation)
860 (declare (ignore real-body documentation))
861 (let ((name-decl (get-declaration '%method-name declarations)))
863 (destructuring-bind (name) name-decl
866 ;;; Convert a lambda expression containing a SB-PCL::%METHOD-NAME
867 ;;; declaration (which is a naming style internal to PCL) into an
868 ;;; SB-INT:NAMED-LAMBDA expression (which is a naming style used
869 ;;; throughout SBCL, understood by the main compiler); or if there's
870 ;;; no SB-PCL::%METHOD-NAME declaration, then just return the original
871 ;;; lambda expression.
872 (defun name-method-lambda (method-lambda)
873 (let ((method-name (body-method-name (cddr method-lambda))))
875 `(named-lambda (slow-method ,method-name) ,(rest method-lambda))
878 (defun make-method-initargs-form-internal (method-lambda initargs env)
879 (declare (ignore env))
880 (let (method-lambda-args
881 lmf ; becomes body of function
883 (if (not (and (= 3 (length method-lambda))
884 (= 2 (length (setq method-lambda-args (cadr method-lambda))))
885 (consp (setq lmf (third method-lambda)))
886 (eq 'simple-lexical-method-functions (car lmf))
887 (eq (car method-lambda-args)
888 (cadr (setq lmf-params (cadr lmf))))
889 (eq (cadr method-lambda-args)
890 (caddr lmf-params))))
891 `(list* :function ,(name-method-lambda method-lambda)
893 (let* ((lambda-list (car lmf-params))
897 (dolist (arg lambda-list)
898 (when (member arg '(&optional &rest &key))
905 (setq args (nreverse args))
906 (setf (getf (getf initargs 'plist) :arg-info) (cons nreq restp))
907 (make-method-initargs-form-internal1
908 initargs (cddr lmf) args lmf-params restp)))))
910 (defun lambda-list-parameter-names (lambda-list)
911 ;; Given a valid lambda list, extract the parameter names.
912 (loop for x in lambda-list
914 do (unless (member x lambda-list-keywords)
916 (let ((name (car x)))
918 ;; ... ((:BAR FOO) 1)
919 (push (second name) res)
923 (let ((name-p (cddr x)))
925 (push (car name-p) res))))
928 finally (return res)))
930 (defun make-method-initargs-form-internal1
931 (initargs body req-args lmf-params restp)
932 (let* (;; The lambda-list of the method, minus specifiers
933 (lambda-list (car lmf-params))
934 ;; Names of the parameters that will be in the outermost lambda-list
935 ;; (and whose bound declarations thus need to be in OUTER-DECLS).
936 (outer-parameters req-args)
937 ;; The lambda-list used by BIND-ARGS
938 (bind-list lambda-list)
939 (setq-p (getf (cdr lmf-params) :setq-p))
940 (auxp (member '&aux bind-list))
941 (call-next-method-p (getf (cdr lmf-params) :call-next-method-p)))
942 ;; Try to use the normal function call machinery instead of BIND-ARGS
943 ;; binding the arguments, unless:
944 (unless (or ;; If all arguments are required, BIND-ARGS will be a no-op
946 (and (not restp) (not auxp))
947 ;; CALL-NEXT-METHOD wants to use BIND-ARGS, and needs a
948 ;; list of all non-required arguments.
950 (setf ;; We don't want a binding for .REST-ARG.
952 ;; Get all the parameters for declaration parsing
953 outer-parameters (lambda-list-parameter-names lambda-list)
954 ;; Ensure that BIND-ARGS won't do anything (since
955 ;; BIND-LIST won't contain any non-required parameters,
956 ;; and REQ-ARGS will be of an equal length). We still want
957 ;; to pass BIND-LIST to FAST-LEXICAL-METHOD-FUNCTIONS so
958 ;; that BIND-FAST-LEXICAL-METHOD-FUNCTIONS can take care
959 ;; of rebinding SETQd required arguments around the method
962 (multiple-value-bind (outer-decls inner-decls body-sans-decls)
964 body outer-parameters (or call-next-method-p setq-p))
965 (let* ((rest-arg (when restp
967 (fmf-lambda-list (if rest-arg
968 (append req-args (list '&rest rest-arg))
969 (if call-next-method-p
974 (let* ((fmf (,(if (body-method-name body) 'named-lambda 'lambda)
975 ,@(when (body-method-name body)
977 (list (cons 'fast-method (body-method-name body))))
978 ;; The lambda-list of the FMF
979 (.pv-cell. .next-method-call. ,@fmf-lambda-list)
980 ;; body of the function
981 (declare (ignorable .pv-cell. .next-method-call.)
982 (disable-package-locks pv-env-environment))
984 (symbol-macrolet ((pv-env-environment default))
985 (fast-lexical-method-functions
986 (,bind-list .next-method-call. ,req-args ,rest-arg
987 ,@(cdddr lmf-params))
989 ,@body-sans-decls))))
990 (mf (%make-method-function fmf nil)))
991 (set-funcallable-instance-function
992 mf (method-function-from-fast-function fmf ',(getf initargs 'plist)))
996 ;;; Use arrays and hash tables and the fngen stuff to make this much
997 ;;; better. It doesn't really matter, though, because a function
998 ;;; returned by this will get called only when the user explicitly
999 ;;; funcalls a result of method-function. BUT, this is needed to make
1000 ;;; early methods work.
1001 (defun method-function-from-fast-function (fmf plist)
1002 (declare (type function fmf))
1003 (let* ((method-function nil)
1004 (calls (getf plist :call-list))
1005 (snl (getf plist :slot-name-lists))
1006 (pv-table (when (or calls snl)
1007 (intern-pv-table :call-list calls :slot-name-lists snl)))
1008 (arg-info (getf plist :arg-info))
1009 (nreq (car arg-info))
1010 (restp (cdr arg-info)))
1011 (setq method-function
1012 (lambda (method-args next-methods)
1013 (let* ((pv-cell (when pv-table
1014 (get-pv-cell method-args pv-table)))
1015 (nm (car next-methods))
1016 (nms (cdr next-methods))
1019 :function (if (std-instance-p nm)
1020 (method-function nm)
1022 :call-method-args (list nms)))))
1023 (apply fmf pv-cell nmc method-args))))
1024 ;; FIXME: this looks dangerous.
1025 (let* ((fname (%fun-name fmf)))
1026 (when (and fname (eq (car fname) 'fast-method))
1027 (set-fun-name method-function (cons 'slow-method (cdr fname)))))
1030 ;;; this is similar to the above, only not quite. Only called when
1031 ;;; the MOP is heavily involved. Not quite parallel to
1032 ;;; METHOD-FUNCTION-FROM-FAST-METHOD-FUNCTION, because we can close
1033 ;;; over the actual PV-CELL in this case.
1034 (defun method-function-from-fast-method-call (fmc)
1035 (let* ((fmf (fast-method-call-function fmc))
1036 (pv-cell (fast-method-call-pv-cell fmc))
1037 (arg-info (fast-method-call-arg-info fmc))
1038 (nreq (car arg-info))
1039 (restp (cdr arg-info)))
1040 (lambda (method-args next-methods)
1041 (let* ((nm (car next-methods))
1042 (nms (cdr next-methods))
1045 :function (if (std-instance-p nm)
1046 (method-function nm)
1048 :call-method-args (list nms)))))
1049 (apply fmf pv-cell nmc method-args)))))
1051 (defun get-pv-cell (method-args pv-table)
1052 (let ((pv-wrappers (pv-wrappers-from-all-args pv-table method-args)))
1054 (pv-table-lookup pv-table pv-wrappers))))
1056 (defun pv-table-lookup-pv-args (pv-table &rest pv-parameters)
1057 (pv-table-lookup pv-table (pv-wrappers-from-pv-args pv-parameters)))
1059 (defun pv-wrappers-from-pv-args (&rest args)
1061 (dolist (arg args (if (cdr wrappers) (nreverse wrappers) (car wrappers)))
1062 (let ((wrapper (wrapper-of arg)))
1063 (push (if (invalid-wrapper-p wrapper)
1064 (check-wrapper-validity wrapper)
1068 (defun pv-wrappers-from-all-args (pv-table args)
1069 (loop for snl in (pv-table-slot-name-lists pv-table) and arg in args
1071 collect (wrapper-of arg) into wrappers
1072 finally (return (if (cdr wrappers) wrappers (car wrappers)))))
1074 ;;; Return the subset of WRAPPERS which is used in the cache
1076 (defun pv-wrappers-from-all-wrappers (pv-table wrappers)
1077 (loop for snl in (pv-table-slot-name-lists pv-table) and w in wrappers
1079 collect w into result
1080 finally (return (if (cdr result) result (car result)))))