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 (defun pv-cache-limit-fn (nlines)
37 (default-limit-fn nlines))
39 (defstruct (pv-table (:predicate pv-tablep)
40 (:constructor make-pv-table-internal
41 (slot-name-lists call-list))
43 (cache nil :type (or cache null))
44 (pv-size 0 :type fixnum)
45 (slot-name-lists nil :type list)
46 (call-list nil :type list))
48 #-sb-fluid (declaim (sb-ext:freeze-type pv-table))
50 (defvar *initial-pv-table* (make-pv-table-internal nil nil))
52 ; help new slot-value-using-class methods affect fast iv access
53 (defvar *all-pv-table-list* nil)
55 (defun make-pv-table (&key slot-name-lists call-list)
56 (let ((pv-table (make-pv-table-internal slot-name-lists call-list)))
57 (push pv-table *all-pv-table-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
82 (let ((pv-table (outer (mapcar #'inner (cons call-list slot-name-lists)))))
85 (dolist (slot-name-list slot-name-lists)
86 (dolist (slot-name (cdr slot-name-list))
87 (note-pv-table-reference slot-name pv-index pv-table)
89 (dolist (gf-call call-list)
90 (note-pv-table-reference gf-call pv-index pv-table)
92 (setf (pv-table-pv-size pv-table) pv-index)))
95 (defun note-pv-table-reference (ref pv-offset pv-table)
96 (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
98 (let ((table-entry (assq pv-table entry)))
99 (when (and (null table-entry)
100 (> (length entry) 8))
101 (let ((new-table-table (make-hash-table :size 16 :test 'eq)))
102 (dolist (table-entry entry)
103 (setf (gethash (car table-entry) new-table-table)
105 (setf (gethash ref *pv-key-to-pv-table-table*) new-table-table)))
107 (if (null table-entry)
108 (let ((new (cons pv-table pv-offset)))
110 (push new (cdr entry))
111 (setf (gethash ref *pv-key-to-pv-table-table*)
113 (push pv-offset (cdr table-entry)))
114 (return-from note-pv-table-reference nil))))
115 (let ((list (gethash pv-table entry)))
117 (push pv-offset (cdr list))
118 (setf (gethash pv-table entry) (list pv-offset)))))
121 (defun map-pv-table-references-of (ref function)
122 (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
124 (dolist (table+pv-offset-list entry)
126 (car table+pv-offset-list)
127 (cdr table+pv-offset-list)))
128 (maphash function entry)))
131 (defvar *pvs* (make-hash-table :test 'equal))
133 (defun optimize-slot-value-by-class-p (class slot-name type)
134 (or (not (eq *boot-state* 'complete))
135 (let ((slotd (find-slot-definition class slot-name)))
137 (slot-accessor-std-p slotd type)))))
139 (defun compute-pv-slot (slot-name wrapper class class-slots class-slot-p-cell)
140 (if (symbolp slot-name)
141 (when (optimize-slot-value-by-class-p class slot-name 'all)
142 (or (instance-slot-index wrapper slot-name)
143 (let ((cell (assq slot-name class-slots)))
145 (setf (car class-slot-p-cell) t)
147 (when (consp slot-name)
148 (dolist (type '(reader writer) nil)
149 (when (eq (car slot-name) type)
151 (let* ((gf-name (cadr slot-name))
152 (gf (gdefinition gf-name))
153 (location (when (eq *boot-state* 'complete)
154 (accessor-values1 gf type class))))
155 (when (consp location)
156 (setf (car class-slot-p-cell) t))
159 (defun compute-pv (slot-name-lists wrappers)
160 (unless (listp wrappers) (setq wrappers (list wrappers)))
161 (let* ((not-simple-p-cell (list nil))
163 (let ((elements nil))
164 (dolist (slot-names slot-name-lists)
166 (let* ((wrapper (pop wrappers))
167 (std-p (typep wrapper 'wrapper))
168 (class (wrapper-class* wrapper))
169 (class-slots (and std-p (wrapper-class-slots wrapper))))
170 (dolist (slot-name (cdr slot-names))
171 ;; Original PCL code had this idiom. why not:
176 (compute-pv-slot slot-name wrapper class
177 class-slots not-simple-p-cell))
179 (nreverse elements))))
180 (if (car not-simple-p-cell)
181 (make-permutation-vector (cons t elements))
182 (or (gethash elements *pvs*)
183 (setf (gethash elements *pvs*)
184 (make-permutation-vector (cons nil elements)))))))
186 (defun compute-calls (call-list wrappers)
187 (declare (ignore call-list wrappers))
191 (compute-emf-from-wrappers call wrappers))
196 #|| ; Need to finish this, then write the maintenance functions.
197 (defun compute-emf-from-wrappers (call wrappers)
199 (destructuring-bind (gf-name nreq restp arg-info) call
200 (if (eq gf-name 'make-instance)
201 (error "should not get here") ; there is another mechanism for this.
203 (if (not (eq *boot-state* 'complete))
204 (apply (gdefinition gf-name) args)
205 (let* ((gf (gdefinition gf-name))
206 (arg-info (arg-info-reader gf))
209 (emf (cache-miss-values-internal gf arg-info
210 wrappers classes types
212 (update-all-pv-tables call wrappers emf)
213 (invoke-emf emf args))))))))
216 (defun make-permutation-vector (indexes)
217 (make-array (length indexes) :initial-contents indexes))
219 (defun pv-table-lookup (pv-table pv-wrappers)
220 (let* ((slot-name-lists (pv-table-slot-name-lists pv-table))
221 (call-list (pv-table-call-list pv-table))
222 (cache (or (pv-table-cache pv-table)
223 (setf (pv-table-cache pv-table)
224 (get-cache (- (length slot-name-lists)
225 (count nil slot-name-lists))
229 (or (probe-cache cache pv-wrappers)
230 (let* ((pv (compute-pv slot-name-lists pv-wrappers))
231 (calls (compute-calls call-list pv-wrappers))
232 (pv-cell (cons pv calls))
233 (new-cache (fill-cache cache pv-wrappers pv-cell)))
234 (unless (eq new-cache cache)
235 (setf (pv-table-cache pv-table) new-cache)
239 (defun make-pv-type-declaration (var)
240 `(type simple-vector ,var))
242 (defvar *empty-pv* #())
244 (defmacro pvref (pv index)
247 (defmacro copy-pv (pv)
250 (defun make-calls-type-declaration (var)
251 `(type simple-vector ,var))
253 (defmacro callsref (calls index)
254 `(svref ,calls ,index))
256 (defvar *pv-table-cache-update-info* nil)
258 (defun update-pv-table-cache-info (class)
259 (let ((slot-names-for-pv-table-update nil)
261 (dolist (icu *pv-table-cache-update-info*)
262 (if (eq (car icu) class)
263 (pushnew (cdr icu) slot-names-for-pv-table-update)
264 (push icu new-icui)))
265 (setq *pv-table-cache-update-info* new-icui)
266 (when slot-names-for-pv-table-update
267 (update-all-pv-table-caches class slot-names-for-pv-table-update))))
269 (defun update-all-pv-table-caches (class slot-names)
270 (let* ((cwrapper (class-wrapper class))
271 (std-p (typep cwrapper 'wrapper))
272 (class-slots (and std-p (wrapper-class-slots cwrapper)))
273 (class-slot-p-cell (list nil))
274 (new-values (mapcar (lambda (slot-name)
278 slot-name cwrapper class
279 class-slots class-slot-p-cell))))
282 (dolist (slot-name slot-names)
283 (map-pv-table-references-of
285 (lambda (pv-table pv-offset-list)
286 (declare (ignore pv-offset-list))
287 (pushnew pv-table pv-tables))))
288 (dolist (pv-table pv-tables)
289 (let* ((cache (pv-table-cache pv-table))
290 (slot-name-lists (pv-table-slot-name-lists pv-table))
291 (pv-size (pv-table-pv-size pv-table))
292 (pv-map (make-array pv-size :initial-element nil)))
293 (let ((map-index 1) (param-index 0))
294 (dolist (slot-name-list slot-name-lists)
295 (dolist (slot-name (cdr slot-name-list))
296 (let ((a (assoc slot-name new-values)))
297 (setf (svref pv-map map-index)
298 (and a (cons param-index (cdr a)))))
302 (map-cache (lambda (wrappers pv-cell)
304 (update-slots-in-pv wrappers (car pv-cell)
305 cwrapper pv-size pv-map)))
308 (defun update-slots-in-pv (wrappers pv cwrapper pv-size pv-map)
309 (if (not (if (atom wrappers)
310 (eq cwrapper wrappers)
311 (dolist (wrapper wrappers nil)
312 (when (eq wrapper cwrapper)
315 (let* ((old-intern-p (listp (pvref pv 0)))
316 (new-pv (if old-intern-p
321 (dotimes-fixnum (i pv-size)
322 (when (consp (let ((map (svref pv-map i)))
324 (setf (pvref new-pv i) (cdr map))
326 (setq new-intern-p nil)))
328 (dolist (wrapper wrappers)
329 (when (eq wrapper cwrapper)
330 (dotimes-fixnum (i pv-size)
331 (when (consp (let ((map (svref pv-map i)))
332 (if (and map (= (car map) param))
333 (setf (pvref new-pv i) (cdr map))
335 (setq new-intern-p nil))))
338 (setq new-pv (let ((list-pv (coerce pv 'list)))
339 (or (gethash (cdr list-pv) *pvs*)
340 (setf (gethash (cdr list-pv) *pvs*)
343 (make-permutation-vector list-pv)))))))
346 (defun maybe-expand-accessor-form (form required-parameters slots env)
347 (let* ((fname (car form))
348 #||(len (length form))||#
349 (gf (if (symbolp fname)
350 (unencapsulated-fdefinition fname)
351 (gdefinition fname))))
352 (macrolet ((maybe-optimize-reader ()
354 (can-optimize-access1 (cadr form)
355 required-parameters env)))
357 (optimize-reader slots parameter gf-name form))))
358 (maybe-optimize-writer ()
360 (can-optimize-access1 (caddr form)
361 required-parameters env)))
363 (optimize-writer slots parameter gf-name form)))))
364 (unless (and (consp (cadr form))
365 (eq 'instance-accessor-parameter (caadr form)))
366 (when (and (eq *boot-state* 'complete)
367 (generic-function-p gf))
368 (let ((methods (generic-function-methods gf)))
370 (let* ((gf-name (generic-function-name gf))
371 (arg-info (gf-arg-info gf))
372 (metatypes (arg-info-metatypes arg-info))
373 (nreq (length metatypes))
374 (applyp (arg-info-applyp arg-info)))
377 (when (some #'standard-reader-method-p methods)
378 (maybe-optimize-reader)))
381 (eq (car gf-name) 'setf))
382 (when (some #'standard-writer-method-p methods)
383 (maybe-optimize-writer)))))))))))))
385 (defun optimize-generic-function-call (form
390 (declare (ignore required-parameters env slots calls))
391 (or (and (eq (car form) 'make-instance)
392 (expand-make-instance-form form))
395 (defun can-optimize-access (form required-parameters env)
396 (let ((type (ecase (car form)
398 (set-slot-value 'writer)
399 (slot-boundp 'boundp)))
401 (slot-name (eval (caddr form)))) ; known to be constant
402 (can-optimize-access1 var required-parameters env type slot-name)))
404 ;;; FIXME: This looks like an internal helper function for
405 ;;; CAN-OPTIMIZE-ACCESS, and it is used that way, but it's also called
406 ;;; bare from several places in the code. Perhaps the two functions
407 ;;; should be renamed CAN-OPTIMIZE-ACCESS-FOR-FORM and
408 ;;; CAN-OPTIMIZE-ACCESS-FOR-VAR. If so, I'd just as soon use keyword
409 ;;; args instead of optional ones, too.
410 (defun can-optimize-access1 (var required-parameters env
411 &optional type slot-name)
412 (when (and (consp var) (eq 'the (car var)))
413 ;; FIXME: We should assert list of length 3 here. Or maybe we
414 ;; should just define EXTRACT-THE, replace the whole
417 ;; (AWHEN (EXTRACT-THE VAR)
419 ;; and then use EXTRACT-THE similarly to clean up the other tests
420 ;; against 'THE scattered through the PCL code.
421 (setq var (caddr var)))
423 (let* ((rebound? (caddr (var-declaration '%variable-rebinding var env)))
424 (parameter-or-nil (car (memq (or rebound? var)
425 required-parameters))))
426 (when parameter-or-nil
427 (let* ((class-name (caddr (var-declaration '%class
430 (class (find-class class-name nil)))
431 (when (or (not (eq *boot-state* 'complete))
432 (and class (not (class-finalized-p class))))
434 (when (and class-name (not (eq class-name t)))
435 (when (or (null type)
437 (memq *the-class-structure-object*
438 (class-precedence-list class))))
439 (optimize-slot-value-by-class-p class slot-name type))
440 (cons parameter-or-nil (or class class-name)))))))))
442 (defun optimize-slot-value (slots sparameter form)
444 (destructuring-bind (ignore1 ignore2 slot-name-form) form
445 (declare (ignore ignore1 ignore2))
446 (let ((slot-name (eval slot-name-form)))
447 (optimize-instance-access slots :read sparameter slot-name nil)))
448 `(accessor-slot-value ,@(cdr form))))
450 (defun optimize-set-slot-value (slots sparameter form)
452 (destructuring-bind (ignore1 ignore2 slot-name-form new-value) form
453 (declare (ignore ignore1 ignore2))
454 (let ((slot-name (eval slot-name-form)))
455 (optimize-instance-access slots
460 `(accessor-set-slot-value ,@(cdr form))))
462 (defun optimize-slot-boundp (slots sparameter form)
465 ;; FIXME: In CMU CL ca. 19991205, this binding list had a
466 ;; fourth element in it, NEW-VALUE. It's hard to see how
467 ;; that could possibly be right, since SLOT-BOUNDP has no
468 ;; NEW-VALUE. Since it was causing a failure in building PCL
469 ;; for SBCL, so I changed it to match the definition of
470 ;; SLOT-BOUNDP (and also to match the list used in the
471 ;; similar OPTIMIZE-SLOT-VALUE, above). However, I'm weirded
472 ;; out by this, since this is old code which has worked for
473 ;; ages to build PCL for CMU CL, so it's hard to see why it
474 ;; should need a patch like this in order to build PCL for
475 ;; SBCL. I'd like to return to this and find a test case
476 ;; which exercises this function both in CMU CL, to see
477 ;; whether it's really a previously-unexercised bug or
478 ;; whether I've misunderstood something (and, presumably,
479 ;; patched it wrong).
480 (slot-boundp-symbol instance slot-name-form)
482 (declare (ignore slot-boundp-symbol instance))
483 (let ((slot-name (eval slot-name-form)))
484 (optimize-instance-access slots
489 `(accessor-slot-boundp ,@(cdr form))))
491 (defun optimize-reader (slots sparameter gf-name form)
493 (optimize-accessor-call slots :read sparameter gf-name nil)
496 (defun optimize-writer (slots sparameter gf-name form)
498 (destructuring-bind (ignore1 ignore2 new-value) form
499 (declare (ignore ignore1 ignore2))
500 (optimize-accessor-call slots :write sparameter gf-name new-value))
503 ;;; The SLOTS argument is an alist, the CAR of each entry is the name
504 ;;; of a required parameter to the function. The alist is in order, so
505 ;;; the position of an entry in the alist corresponds to the
506 ;;; argument's position in the lambda list.
507 (defun optimize-instance-access (slots
512 (let ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
513 (parameter (if (consp sparameter) (car sparameter) sparameter)))
514 (if (and (eq *boot-state* 'complete)
516 (memq *the-class-structure-object* (class-precedence-list class)))
517 (let ((slotd (find-slot-definition class slot-name)))
520 `(,(slot-definition-defstruct-accessor-symbol slotd) ,parameter))
522 `(setf (,(slot-definition-defstruct-accessor-symbol slotd)
527 (let* ((parameter-entry (assq parameter slots))
528 (slot-entry (assq slot-name (cdr parameter-entry)))
529 (position (posq parameter-entry slots))
530 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
531 (unless parameter-entry
532 (bug "slot optimization bewilderment: O-I-A"))
534 (setq slot-entry (list slot-name))
535 (push slot-entry (cdr parameter-entry)))
536 (push pv-offset-form (cdr slot-entry))
539 `(instance-read ,pv-offset-form ,parameter ,position
540 ',slot-name ',class))
542 `(let ((.new-value. ,new-value))
543 (instance-write ,pv-offset-form ,parameter ,position
544 ',slot-name ',class .new-value.)))
546 `(instance-boundp ,pv-offset-form ,parameter ,position
547 ',slot-name ',class)))))))
549 (defun optimize-accessor-call (slots read/write sparameter gf-name new-value)
550 (let* ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
551 (parameter (if (consp sparameter) (car sparameter) sparameter))
552 (parameter-entry (assq parameter slots))
553 (name (case read/write
554 (:read `(reader ,gf-name))
555 (:write `(writer ,gf-name))))
556 (slot-entry (assoc name (cdr parameter-entry) :test #'equal))
557 (position (posq parameter-entry slots))
558 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
559 (unless parameter-entry
560 (error "slot optimization bewilderment: O-A-C"))
562 (setq slot-entry (list name))
563 (push slot-entry (cdr parameter-entry)))
564 (push pv-offset-form (cdr slot-entry))
567 `(instance-reader ,pv-offset-form ,parameter ,position ,gf-name ',class))
569 `(let ((.new-value. ,new-value))
570 (instance-writer ,pv-offset-form ,parameter ,position ,gf-name ',class
573 (defvar *unspecific-arg* '..unspecific-arg..)
575 (defun optimize-gf-call-internal (form slots env)
576 (when (and (consp form)
577 (eq (car form) 'the))
578 (setq form (caddr form)))
579 (or (and (symbolp form)
580 (let* ((rebound? (caddr (var-declaration '%variable-rebinding
583 (parameter-or-nil (car (assq (or rebound? form) slots))))
584 (when parameter-or-nil
585 (let* ((class-name (caddr (var-declaration 'class
588 (when (and class-name (not (eq class-name t)))
589 (position parameter-or-nil slots :key #'car))))))
591 (let ((form (eval form)))
597 (defun optimize-gf-call (slots calls gf-call-form nreq restp env)
598 (unless (eq (car gf-call-form) 'make-instance) ; XXX needs more work
599 (let* ((args (cdr gf-call-form))
600 (all-args-p (eq (car gf-call-form) 'make-instance))
601 (non-required-args (nthcdr nreq args))
602 (required-args (ldiff args non-required-args))
603 (call-spec (list (car gf-call-form) nreq restp
604 (mapcar (lambda (form)
605 (optimize-gf-call-internal form slots env))
609 (call-entry (assoc call-spec calls :test #'equal))
610 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
611 (unless (some #'integerp
612 (let ((spec-args (cdr call-spec)))
614 (ldiff spec-args (nthcdr nreq spec-args))
616 (return-from optimize-gf-call nil))
618 (setq call-entry (list call-spec))
619 (push call-entry (cdr calls)))
620 (push pv-offset-form (cdr call-entry))
621 (if (eq (car call-spec) 'make-instance)
622 `(funcall (pv-ref .pv. ,pv-offset-form) ,@(cdr gf-call-form))
623 `(let ((.emf. (pv-ref .pv. ,pv-offset-form)))
624 (invoke-effective-method-function .emf. ,restp
625 ,@required-args ,@(when restp `((list ,@non-required-args)))))))))
627 (define-walker-template pv-offset) ; These forms get munged by mutate slots.
628 (defmacro pv-offset (arg) arg)
629 (define-walker-template instance-accessor-parameter)
630 (defmacro instance-accessor-parameter (x) x)
632 ;;; It is safe for these two functions to be wrong. They just try to
633 ;;; guess what the most likely case will be.
634 (defun generate-fast-class-slot-access-p (class-form slot-name-form)
635 (let ((class (and (constantp class-form) (eval class-form)))
636 (slot-name (and (constantp slot-name-form) (eval slot-name-form))))
637 (and (eq *boot-state* 'complete)
638 (standard-class-p class)
639 (not (eq class *the-class-t*)) ; shouldn't happen, though.
640 (let ((slotd (find-slot-definition class slot-name)))
641 (and slotd (classp (slot-definition-allocation slotd)))))))
643 (defun skip-fast-slot-access-p (class-form slot-name-form type)
644 (let ((class (and (constantp class-form) (eval class-form)))
645 (slot-name (and (constantp slot-name-form) (eval slot-name-form))))
646 (and (eq *boot-state* 'complete)
647 (standard-class-p class)
648 (not (eq class *the-class-t*)) ; shouldn't happen, though.
649 (let ((slotd (find-slot-definition class slot-name)))
650 (and slotd (skip-optimize-slot-value-by-class-p class
654 (defun skip-optimize-slot-value-by-class-p (class slot-name type)
655 (let ((slotd (find-slot-definition class slot-name)))
657 (eq *boot-state* 'complete)
658 (not (slot-accessor-std-p slotd type)))))
660 (defmacro instance-read-internal (pv slots pv-offset default &optional type)
661 (unless (member type '(nil :instance :class :default))
662 (error "illegal type argument to ~S: ~S" 'instance-read-internal type))
663 (if (eq type :default)
665 (let* ((index (gensym))
667 `(locally (declare #.*optimize-speed*)
668 (let ((,index (pvref ,pv ,pv-offset)))
669 (setq ,value (typecase ,index
670 ,@(when (or (null type) (eq type :instance))
671 `((fixnum (clos-slots-ref ,slots ,index))))
672 ,@(when (or (null type) (eq type :class))
673 `((cons (cdr ,index))))
675 (if (eq ,value +slot-unbound+)
679 (defmacro instance-read (pv-offset parameter position slot-name class)
680 (if (skip-fast-slot-access-p class slot-name 'reader)
681 `(accessor-slot-value ,parameter ,slot-name)
682 `(instance-read-internal .pv. ,(slot-vector-symbol position)
683 ,pv-offset (accessor-slot-value ,parameter ,slot-name)
684 ,(if (generate-fast-class-slot-access-p class slot-name)
687 (defmacro instance-reader (pv-offset parameter position gf-name class)
688 (declare (ignore class))
689 `(instance-read-internal .pv. ,(slot-vector-symbol position)
691 (,gf-name (instance-accessor-parameter ,parameter))
694 (defmacro instance-write-internal (pv slots pv-offset new-value default
696 (unless (member type '(nil :instance :class :default))
697 (error "illegal type argument to ~S: ~S" 'instance-write-internal type))
698 (if (eq type :default)
700 (let* ((index (gensym)))
701 `(locally (declare #.*optimize-speed*)
702 (let ((,index (pvref ,pv ,pv-offset)))
704 ,@(when (or (null type) (eq type :instance))
705 `((fixnum (setf (clos-slots-ref ,slots ,index)
707 ,@(when (or (null type) (eq type :class))
708 `((cons (setf (cdr ,index) ,new-value))))
711 (defmacro instance-write (pv-offset
717 (if (skip-fast-slot-access-p class slot-name 'writer)
718 `(accessor-set-slot-value ,parameter ,slot-name ,new-value)
719 `(instance-write-internal .pv. ,(slot-vector-symbol position)
720 ,pv-offset ,new-value
721 (accessor-set-slot-value ,parameter ,slot-name ,new-value)
722 ,(if (generate-fast-class-slot-access-p class slot-name)
725 (defmacro instance-writer (pv-offset
731 (declare (ignore class))
732 `(instance-write-internal .pv. ,(slot-vector-symbol position)
733 ,pv-offset ,new-value
734 (,(if (consp gf-name)
735 (get-setf-fun-name gf-name)
737 (instance-accessor-parameter ,parameter)
741 (defmacro instance-boundp-internal (pv slots pv-offset default
743 (unless (member type '(nil :instance :class :default))
744 (error "illegal type argument to ~S: ~S" 'instance-boundp-internal type))
745 (if (eq type :default)
747 (let* ((index (gensym)))
748 `(locally (declare #.*optimize-speed*)
749 (let ((,index (pvref ,pv ,pv-offset)))
751 ,@(when (or (null type) (eq type :instance))
752 `((fixnum (not (and ,slots
753 (eq (clos-slots-ref ,slots ,index)
755 ,@(when (or (null type) (eq type :class))
756 `((cons (not (eq (cdr ,index) +slot-unbound+)))))
759 (defmacro instance-boundp (pv-offset parameter position slot-name class)
760 (if (skip-fast-slot-access-p class slot-name 'boundp)
761 `(accessor-slot-boundp ,parameter ,slot-name)
762 `(instance-boundp-internal .pv. ,(slot-vector-symbol position)
763 ,pv-offset (accessor-slot-boundp ,parameter ,slot-name)
764 ,(if (generate-fast-class-slot-access-p class slot-name)
767 ;;; This magic function has quite a job to do indeed.
769 ;;; The careful reader will recall that <slots> contains all of the
770 ;;; optimized slot access forms produced by OPTIMIZE-INSTANCE-ACCESS.
771 ;;; Each of these is a call to either INSTANCE-READ or INSTANCE-WRITE.
773 ;;; At the time these calls were produced, the first argument was
774 ;;; specified as the symbol .PV-OFFSET.; what we have to do now is
775 ;;; convert those pv-offset arguments into the actual number that is
776 ;;; the correct offset into the pv.
778 ;;; But first, oh but first, we sort <slots> a bit so that for each
779 ;;; argument we have the slots in alphabetical order. This
780 ;;; canonicalizes the PV-TABLE's a bit and will hopefully lead to
781 ;;; having fewer PV's floating around. Even if the gain is only
782 ;;; modest, it costs nothing.
783 (defun slot-name-lists-from-slots (slots calls)
784 (multiple-value-bind (slots calls) (mutate-slots-and-calls slots calls)
785 (let* ((slot-name-lists
786 (mapcar (lambda (parameter-entry)
787 (cons nil (mapcar #'car (cdr parameter-entry))))
790 (mapcar #'car calls)))
791 (dolist (call call-list)
792 (dolist (arg (cdr call))
794 (setf (car (nth arg slot-name-lists)) t))))
795 (setq slot-name-lists (mapcar (lambda (r+snl)
796 (when (or (car r+snl) (cdr r+snl))
799 (let ((cvt (apply #'vector
801 (mapcar (lambda (r+snl)
802 (when r+snl (incf i)))
804 (setq call-list (mapcar (lambda (call)
806 (mapcar (lambda (arg)
812 (values slot-name-lists call-list))))
814 (defun mutate-slots-and-calls (slots calls)
815 (let ((sorted-slots (sort-slots slots))
816 (sorted-calls (sort-calls (cdr calls)))
817 (pv-offset 0)) ; index 0 is for info
818 (dolist (parameter-entry sorted-slots)
819 (dolist (slot-entry (cdr parameter-entry))
821 (dolist (form (cdr slot-entry))
822 (setf (cadr form) pv-offset))))
823 (dolist (call-entry sorted-calls)
825 (dolist (form (cdr call-entry))
826 (setf (cadr form) pv-offset)))
827 (values sorted-slots sorted-calls)))
829 (defun symbol-pkg-name (sym)
830 (let ((pkg (symbol-package sym)))
831 (if pkg (package-name pkg) "")))
833 ;;; FIXME: Because of the existence of UNINTERN and RENAME-PACKAGE,
834 ;;; the part of this ordering which is based on SYMBOL-PKG-NAME is not
835 ;;; stable. This ordering is only used in to
836 ;;; SLOT-NAME-LISTS-FROM-SLOTS, where it serves to "canonicalize the
837 ;;; PV-TABLE's a bit and will hopefully lead to having fewer PV's
838 ;;; floating around", so it sounds as though the instability won't
839 ;;; actually lead to bugs, just small inefficiency. But still, it
840 ;;; would be better to reimplement this function as a comparison based
842 ;;; * stable comparison
843 ;;; * smaller code (here, and in being able to discard SYMBOL-PKG-NAME)
845 (defun symbol-lessp (a b)
846 (if (eq (symbol-package a)
848 (string-lessp (symbol-name a)
850 (string-lessp (symbol-pkg-name a)
851 (symbol-pkg-name b))))
853 (defun symbol-or-cons-lessp (a b)
856 (symbol (symbol-lessp a b))
860 (cons (if (eq (car a) (car b))
861 (symbol-or-cons-lessp (cdr a) (cdr b))
862 (symbol-or-cons-lessp (car a) (car b))))))))
864 (defun sort-slots (slots)
865 (mapcar (lambda (parameter-entry)
866 (cons (car parameter-entry)
867 (sort (cdr parameter-entry) ;slot entries
868 #'symbol-or-cons-lessp
872 (defun sort-calls (calls)
873 (sort calls #'symbol-or-cons-lessp :key #'car))
875 ;;;; This needs to work in terms of metatypes and also needs to work
876 ;;;; for automatically generated reader and writer functions.
877 ;;;; Automatically generated reader and writer functions use this
880 (defmacro pv-binding ((required-parameters slot-name-lists pv-table-symbol)
882 (let (slot-vars pv-parameters)
883 (loop for slots in slot-name-lists
884 for required-parameter in required-parameters
887 (push required-parameter pv-parameters)
888 (push (slot-vector-symbol i) slot-vars)))
889 `(pv-binding1 (.pv. .calls. ,pv-table-symbol
890 ,(nreverse pv-parameters) ,(nreverse slot-vars))
893 (defmacro pv-binding1 ((pv calls pv-table-symbol pv-parameters slot-vars)
895 `(pv-env (,pv ,calls ,pv-table-symbol ,pv-parameters)
896 (let (,@(mapcar (lambda (slot-var p) `(,slot-var (get-slots-or-nil ,p)))
897 slot-vars pv-parameters))
900 ;;; This gets used only when the default MAKE-METHOD-LAMBDA is
902 (defmacro pv-env ((pv calls pv-table-symbol pv-parameters)
904 `(let* ((.pv-table. ,pv-table-symbol)
905 (.pv-cell. (pv-table-lookup-pv-args .pv-table. ,@pv-parameters))
906 (,pv (car .pv-cell.))
907 (,calls (cdr .pv-cell.)))
908 (declare ,(make-pv-type-declaration pv))
909 (declare ,(make-calls-type-declaration calls))
910 ,@(when (symbolp pv-table-symbol)
911 `((declare (special ,pv-table-symbol))))
915 (defvar *non-var-declarations*
916 ;; FIXME: VALUES was in this list, conditionalized with #+CMU, but I
917 ;; don't *think* CMU CL had, or SBCL has, VALUES declarations. If
918 ;; SBCL doesn't have 'em, VALUES should probably be removed from
928 (defvar *var-declarations-with-arg*
932 (defvar *var-declarations-without-arg*
934 ignorable special dynamic-extent
935 ;; FIXME: Possibly this entire list and variable could go away.
936 ;; If not, certainly we should remove all these built-in typenames
937 ;; from the list, and replace them with a test for "is it a type
938 ;; name?" (CLTL1 allowed only built-in type names as declarations,
939 ;; but ANSI CL allows any type name as a declaration.)
940 array atom base-char bignum bit bit-vector character compiled-function
941 complex cons double-float extended-char
942 fixnum float function hash-table integer
943 keyword list long-float nil null number package pathname random-state ratio
944 rational readtable sequence short-float signed-byte simple-array
945 simple-bit-vector simple-string simple-vector single-float standard-char
946 stream string symbol t unsigned-byte vector))
948 (defun split-declarations (body args calls-next-method-p)
949 (let ((inner-decls nil)
952 (loop (when (null body) (return nil))
953 (setq decl (car body))
954 (unless (and (consp decl)
955 (eq (car decl) 'declare))
957 (dolist (form (cdr decl))
959 (let ((declaration-name (car form)))
960 (if (member declaration-name *non-var-declarations*)
961 (push `(declare ,form) outer-decls)
963 (member declaration-name
964 *var-declarations-with-arg*))
966 (member declaration-name
967 *var-declarations-without-arg*))
968 (dname (list (pop form)))
969 (inners nil) (outers nil))
970 (unless (or arg-p non-arg-p)
971 ;; FIXME: This warning, and perhaps the
972 ;; various *VAR-DECLARATIONS-FOO* and/or
973 ;; *NON-VAR-DECLARATIONS* variables,
974 ;; could probably go away now that we're not
975 ;; trying to be portable between different
976 ;; CLTL1 hosts the way PCL was. (Note that to
977 ;; do this right, we need to be able to handle
978 ;; user-defined (DECLAIM (DECLARATION FOO))
980 (warn "The declaration ~S is not understood by ~S.~@
981 Please put ~S on one of the lists ~S,~%~S, or~%~S.~@
982 (Assuming it is a variable declaration without argument)."
983 declaration-name 'split-declarations
985 '*non-var-declarations*
986 '*var-declarations-with-arg*
987 '*var-declarations-without-arg*)
988 (push declaration-name *var-declarations-without-arg*))
990 (setq dname (append dname (list (pop form)))))
992 (if (member var args)
993 ;; Quietly remove IGNORE declarations on
994 ;; args when a next-method is involved, to
995 ;; prevent compiler warnings about ignored
997 (unless (and calls-next-method-p
998 (eq (car dname) 'ignore))
1002 (push `(declare (,@dname ,@outers)) outer-decls))
1004 (push `(declare (,@dname ,@inners)) inner-decls)))))))
1005 (setq body (cdr body)))
1006 (values outer-decls inner-decls body)))
1008 ;;; Pull a name out of the %METHOD-NAME declaration in the function
1009 ;;; body given, or return NIL if no %METHOD-NAME declaration is found.
1010 (defun body-method-name (body)
1011 (multiple-value-bind (real-body declarations documentation)
1012 (parse-body body nil)
1013 (declare (ignore documentation real-body))
1014 (let ((name-decl (get-declaration '%method-name declarations)))
1016 (destructuring-bind (name) name-decl
1019 ;;; Convert a lambda expression containing a SB-PCL::%METHOD-NAME
1020 ;;; declaration (which is a naming style internal to PCL) into an
1021 ;;; SB-INT:NAMED-LAMBDA expression (which is a naming style used
1022 ;;; throughout SBCL, understood by the main compiler); or if there's
1023 ;;; no SB-PCL::%METHOD-NAME declaration, then just return the original
1024 ;;; lambda expression.
1025 (defun name-method-lambda (method-lambda)
1026 (let ((method-name (body-method-name (cddr method-lambda))))
1028 `(named-lambda ,method-name ,(rest method-lambda))
1031 (defun make-method-initargs-form-internal (method-lambda initargs env)
1032 (declare (ignore env))
1033 (let (method-lambda-args
1034 lmf ; becomes body of function
1036 (if (not (and (= 3 (length method-lambda))
1037 (= 2 (length (setq method-lambda-args (cadr method-lambda))))
1038 (consp (setq lmf (third method-lambda)))
1039 (eq 'simple-lexical-method-functions (car lmf))
1040 (eq (car method-lambda-args)
1041 (cadr (setq lmf-params (cadr lmf))))
1042 (eq (cadr method-lambda-args)
1043 (caddr lmf-params))))
1044 `(list* :function ,(name-method-lambda method-lambda)
1046 (let* ((lambda-list (car lmf-params))
1050 (dolist (arg lambda-list)
1051 (when (member arg '(&optional &rest &key))
1054 (when (eq arg '&aux)
1058 (setq args (nreverse args))
1059 (setf (getf (getf initargs :plist) :arg-info) (cons nreq restp))
1060 (make-method-initargs-form-internal1
1061 initargs (cddr lmf) args lmf-params restp)))))
1063 (defun make-method-initargs-form-internal1
1064 (initargs body req-args lmf-params restp)
1065 (multiple-value-bind (outer-decls inner-decls body-sans-decls)
1067 body req-args (getf (cdr lmf-params) :call-next-method-p))
1068 (let* ((rest-arg (when restp '.rest-arg.))
1069 (args+rest-arg (if restp
1070 (append req-args (list rest-arg))
1075 ,(or (body-method-name body) '.method.) ; function name
1076 (.pv-cell. .next-method-call. ,@args+rest-arg) ; function args
1077 ;; body of the function
1078 (declare (ignorable .pv-cell. .next-method-call.))
1080 (macrolet ((pv-env ((pv calls pv-table-symbol pv-parameters)
1082 (declare (ignore pv-table-symbol
1084 `(let ((,pv (car .pv-cell.))
1085 (,calls (cdr .pv-cell.)))
1086 (declare ,(make-pv-type-declaration pv)
1087 ,(make-calls-type-declaration calls))
1090 (fast-lexical-method-functions
1091 (,(car lmf-params) .next-method-call. ,req-args ,rest-arg
1092 ,@(cdddr lmf-params))
1094 ,@body-sans-decls)))
1097 ;;; Use arrays and hash tables and the fngen stuff to make this much
1098 ;;; better. It doesn't really matter, though, because a function
1099 ;;; returned by this will get called only when the user explicitly
1100 ;;; funcalls a result of method-function. BUT, this is needed to make
1101 ;;; early methods work.
1102 (defun method-function-from-fast-function (fmf)
1103 (declare (type function fmf))
1104 (let* ((method-function nil) (pv-table nil)
1105 (arg-info (method-function-get fmf :arg-info))
1106 (nreq (car arg-info))
1107 (restp (cdr arg-info)))
1108 (setq method-function
1109 (lambda (method-args next-methods)
1111 (setq pv-table (method-function-pv-table fmf)))
1112 (let* ((pv-cell (when pv-table
1113 (get-method-function-pv-cell
1114 method-function method-args pv-table)))
1115 (nm (car next-methods))
1116 (nms (cdr next-methods))
1119 :function (if (std-instance-p nm)
1120 (method-function nm)
1122 :call-method-args (list nms)))))
1124 (let* ((rest (nthcdr nreq method-args))
1125 (args (ldiff method-args rest)))
1126 (apply fmf pv-cell nmc (nconc args (list rest))))
1127 (apply fmf pv-cell nmc method-args)))))
1128 (let* ((fname (method-function-get fmf :name))
1129 (name `(,(or (get (car fname) 'method-sym)
1130 (setf (get (car fname) 'method-sym)
1131 (let ((str (symbol-name (car fname))))
1132 (if (string= "FAST-" str :end2 5)
1133 (intern (subseq str 5) *pcl-package*)
1136 (set-fun-name method-function name))
1137 (setf (method-function-get method-function :fast-function) fmf)
1140 (defun get-method-function-pv-cell (method-function
1143 (let ((pv-table (or pv-table (method-function-pv-table method-function))))
1145 (let ((pv-wrappers (pv-wrappers-from-all-args pv-table method-args)))
1147 (pv-table-lookup pv-table pv-wrappers))))))
1149 (defun pv-table-lookup-pv-args (pv-table &rest pv-parameters)
1150 (pv-table-lookup pv-table (pv-wrappers-from-pv-args pv-parameters)))
1152 (defun pv-wrappers-from-pv-args (&rest args)
1153 (let* ((nkeys (length args))
1154 (pv-wrappers (make-list nkeys))
1158 (setq w (wrapper-of arg))
1159 (when (invalid-wrapper-p w)
1160 (setq w (check-wrapper-validity arg)))
1162 (setq w-t (cdr w-t))
1163 (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))
1166 (defun pv-wrappers-from-all-args (pv-table args)
1168 (slot-name-lists (pv-table-slot-name-lists pv-table)))
1169 (dolist (sn slot-name-lists)
1170 (when sn (incf nkeys)))
1171 (let* ((pv-wrappers (make-list nkeys))
1172 (pv-w-t pv-wrappers))
1173 (dolist (sn slot-name-lists)
1175 (let* ((arg (car args))
1176 (w (wrapper-of arg)))
1177 (unless w ; CAN-OPTIMIZE-ACCESS prevents this from happening.
1178 (error "error in PV-WRAPPERS-FROM-ALL-ARGS"))
1179 (setf (car pv-w-t) w)
1180 (setq pv-w-t (cdr pv-w-t))))
1181 (setq args (cdr args)))
1182 (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))
1185 (defun pv-wrappers-from-all-wrappers (pv-table wrappers)
1187 (slot-name-lists (pv-table-slot-name-lists pv-table)))
1188 (dolist (sn slot-name-lists)
1189 (when sn (incf nkeys)))
1190 (let* ((pv-wrappers (make-list nkeys))
1191 (pv-w-t pv-wrappers))
1192 (dolist (sn slot-name-lists)
1194 (let ((w (car wrappers)))
1195 (unless w ; CAN-OPTIMIZE-ACCESS prevents this from happening.
1196 (error "error in PV-WRAPPERS-FROM-ALL-WRAPPERS"))
1197 (setf (car pv-w-t) w)
1198 (setq pv-w-t (cdr pv-w-t))))
1199 (setq wrappers (cdr wrappers)))
1200 (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))