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 (gathering1 (collecting)
164 (iterate ((slot-names (list-elements 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))
173 (compute-pv-slot slot-name wrapper class
174 class-slots not-simple-p-cell))))))))))
175 (if (car not-simple-p-cell)
176 (make-permutation-vector (cons t elements))
177 (or (gethash elements *pvs*)
178 (setf (gethash elements *pvs*)
179 (make-permutation-vector (cons nil elements)))))))
181 (defun compute-calls (call-list wrappers)
182 (declare (ignore call-list wrappers))
186 (compute-emf-from-wrappers call wrappers))
191 #|| ; Need to finish this, then write the maintenance functions.
192 (defun compute-emf-from-wrappers (call wrappers)
194 (destructuring-bind (gf-name nreq restp arg-info) call
195 (if (eq gf-name 'make-instance)
196 (error "should not get here") ; there is another mechanism for this.
197 #'(lambda (&rest args)
198 (if (not (eq *boot-state* 'complete))
199 (apply (gdefinition gf-name) args)
200 (let* ((gf (gdefinition gf-name))
201 (arg-info (arg-info-reader gf))
204 (emf (cache-miss-values-internal gf arg-info
205 wrappers classes types
207 (update-all-pv-tables call wrappers emf)
208 (invoke-emf emf args))))))))
211 (defun make-permutation-vector (indexes)
212 (make-array (length indexes) :initial-contents indexes))
214 (defun pv-table-lookup (pv-table pv-wrappers)
215 (let* ((slot-name-lists (pv-table-slot-name-lists pv-table))
216 (call-list (pv-table-call-list pv-table))
217 (cache (or (pv-table-cache pv-table)
218 (setf (pv-table-cache pv-table)
219 (get-cache (- (length slot-name-lists)
220 (count nil slot-name-lists))
224 (or (probe-cache cache pv-wrappers)
225 (let* ((pv (compute-pv slot-name-lists pv-wrappers))
226 (calls (compute-calls call-list pv-wrappers))
227 (pv-cell (cons pv calls))
228 (new-cache (fill-cache cache pv-wrappers pv-cell)))
229 (unless (eq new-cache cache)
230 (setf (pv-table-cache pv-table) new-cache)
234 (defun make-pv-type-declaration (var)
235 `(type simple-vector ,var))
237 (defvar *empty-pv* #())
239 (defmacro pvref (pv index)
242 (defmacro copy-pv (pv)
245 (defun make-calls-type-declaration (var)
246 `(type simple-vector ,var))
248 (defmacro callsref (calls index)
249 `(svref ,calls ,index))
251 (defvar *pv-table-cache-update-info* nil)
253 (defun update-pv-table-cache-info (class)
254 (let ((slot-names-for-pv-table-update nil)
256 (dolist (icu *pv-table-cache-update-info*)
257 (if (eq (car icu) class)
258 (pushnew (cdr icu) slot-names-for-pv-table-update)
259 (push icu new-icui)))
260 (setq *pv-table-cache-update-info* new-icui)
261 (when slot-names-for-pv-table-update
262 (update-all-pv-table-caches class slot-names-for-pv-table-update))))
264 (defun update-all-pv-table-caches (class slot-names)
265 (let* ((cwrapper (class-wrapper class))
266 (std-p (typep cwrapper 'wrapper))
267 (class-slots (and std-p (wrapper-class-slots cwrapper)))
268 (class-slot-p-cell (list nil))
269 (new-values (mapcar #'(lambda (slot-name)
273 slot-name cwrapper class
274 class-slots class-slot-p-cell))))
277 (dolist (slot-name slot-names)
278 (map-pv-table-references-of
280 #'(lambda (pv-table pv-offset-list)
281 (declare (ignore pv-offset-list))
282 (pushnew pv-table pv-tables))))
283 (dolist (pv-table pv-tables)
284 (let* ((cache (pv-table-cache pv-table))
285 (slot-name-lists (pv-table-slot-name-lists pv-table))
286 (pv-size (pv-table-pv-size pv-table))
287 (pv-map (make-array pv-size :initial-element nil)))
288 (let ((map-index 1)(param-index 0))
289 (dolist (slot-name-list slot-name-lists)
290 (dolist (slot-name (cdr slot-name-list))
291 (let ((a (assoc slot-name new-values)))
292 (setf (svref pv-map map-index)
293 (and a (cons param-index (cdr a)))))
297 (map-cache #'(lambda (wrappers pv-cell)
299 (update-slots-in-pv wrappers (car pv-cell)
300 cwrapper pv-size pv-map)))
303 (defun update-slots-in-pv (wrappers pv cwrapper pv-size pv-map)
304 (if (not (if (atom wrappers)
305 (eq cwrapper wrappers)
306 (dolist (wrapper wrappers nil)
307 (when (eq wrapper cwrapper)
310 (let* ((old-intern-p (listp (pvref pv 0)))
311 (new-pv (if old-intern-p
316 (dotimes-fixnum (i pv-size)
317 (when (consp (let ((map (svref pv-map i)))
319 (setf (pvref new-pv i) (cdr map))
321 (setq new-intern-p nil)))
323 (dolist (wrapper wrappers)
324 (when (eq wrapper cwrapper)
325 (dotimes-fixnum (i pv-size)
326 (when (consp (let ((map (svref pv-map i)))
327 (if (and map (= (car map) param))
328 (setf (pvref new-pv i) (cdr map))
330 (setq new-intern-p nil))))
333 (setq new-pv (let ((list-pv (coerce pv 'list)))
334 (or (gethash (cdr list-pv) *pvs*)
335 (setf (gethash (cdr list-pv) *pvs*)
338 (make-permutation-vector list-pv)))))))
341 (defun maybe-expand-accessor-form (form required-parameters slots env)
342 (let* ((fname (car form))
343 #||(len (length form))||#
344 (gf (if (symbolp fname)
345 (unencapsulated-fdefinition fname)
346 (gdefinition fname))))
347 (macrolet ((maybe-optimize-reader ()
349 (can-optimize-access1 (cadr form)
350 required-parameters env)))
352 (optimize-reader slots parameter gf-name form))))
353 (maybe-optimize-writer ()
355 (can-optimize-access1 (caddr form)
356 required-parameters env)))
358 (optimize-writer slots parameter gf-name form)))))
359 (unless (and (consp (cadr form))
360 (eq 'instance-accessor-parameter (caadr form)))
361 (when (and (eq *boot-state* 'complete)
362 (generic-function-p gf))
363 (let ((methods (generic-function-methods gf)))
365 (let* ((gf-name (generic-function-name gf))
366 (arg-info (gf-arg-info gf))
367 (metatypes (arg-info-metatypes arg-info))
368 (nreq (length metatypes))
369 (applyp (arg-info-applyp arg-info)))
372 (when (some #'standard-reader-method-p methods)
373 (maybe-optimize-reader)))
376 (eq (car gf-name) 'setf))
377 (when (some #'standard-writer-method-p methods)
378 (maybe-optimize-writer)))))))))))))
380 (defun optimize-generic-function-call (form
385 (declare (ignore required-parameters env slots calls))
386 (or (and (eq (car form) 'make-instance)
387 (expand-make-instance-form form))
390 (defun can-optimize-access (form required-parameters env)
391 (let ((type (ecase (car form)
393 (set-slot-value 'writer)
394 (slot-boundp 'boundp)))
396 (slot-name (eval (caddr form)))) ; known to be constant
397 (can-optimize-access1 var required-parameters env type slot-name)))
399 ;;; FIXME: This looks like an internal helper function for
400 ;;; CAN-OPTIMIZE-ACCESS, and it is used that way, but it's also called
401 ;;; bare from several places in the code. Perhaps the two functions
402 ;;; should be renamed CAN-OPTIMIZE-ACCESS-FOR-FORM and
403 ;;; CAN-OPTIMIZE-ACCESS-FOR-VAR. If so, I'd just as soon use keyword
404 ;;; args instead of optional ones, too.
405 (defun can-optimize-access1 (var required-parameters env
406 &optional type slot-name)
407 (when (and (consp var) (eq 'the (car var)))
408 ;; FIXME: We should assert list of length 3 here. Or maybe we
409 ;; should just define EXTRACT-THE, replace the whole
412 ;; (AWHEN (EXTRACT-THE VAR)
414 ;; and then use EXTRACT-THE similarly to clean up the other tests
415 ;; against 'THE scattered through the PCL code.
416 (setq var (caddr var)))
418 (let* ((rebound? (caddr (variable-declaration '%variable-rebinding
421 (parameter-or-nil (car (memq (or rebound? var)
422 required-parameters))))
423 (when parameter-or-nil
424 (let* ((class-name (caddr (variable-declaration '%class
427 (class (find-class class-name nil)))
428 (when (or (not (eq *boot-state* 'complete))
429 (and class (not (class-finalized-p class))))
431 (when (and class-name (not (eq class-name t)))
432 (when (or (null type)
434 (memq *the-class-structure-object*
435 (class-precedence-list class))))
436 (optimize-slot-value-by-class-p class slot-name type))
437 (cons parameter-or-nil (or class class-name)))))))))
439 (defun optimize-slot-value (slots sparameter form)
441 (destructuring-bind (ignore1 ignore2 slot-name-form) form
442 (declare (ignore ignore1 ignore2))
443 (let ((slot-name (eval slot-name-form)))
444 (optimize-instance-access slots :read sparameter slot-name nil)))
445 `(accessor-slot-value ,@(cdr form))))
447 (defun optimize-set-slot-value (slots sparameter form)
449 (destructuring-bind (ignore1 ignore2 slot-name-form new-value) form
450 (declare (ignore ignore1 ignore2))
451 (let ((slot-name (eval slot-name-form)))
452 (optimize-instance-access slots
457 `(accessor-set-slot-value ,@(cdr form))))
459 (defun optimize-slot-boundp (slots sparameter form)
462 ;; FIXME: In CMU CL ca. 19991205, this binding list had a
463 ;; fourth element in it, NEW-VALUE. It's hard to see how
464 ;; that could possibly be right, since SLOT-BOUNDP has no
465 ;; NEW-VALUE. Since it was causing a failure in building PCL
466 ;; for SBCL, so I changed it to match the definition of
467 ;; SLOT-BOUNDP (and also to match the list used in the
468 ;; similar OPTIMIZE-SLOT-VALUE, above). However, I'm weirded
469 ;; out by this, since this is old code which has worked for
470 ;; ages to build PCL for CMU CL, so it's hard to see why it
471 ;; should need a patch like this in order to build PCL for
472 ;; SBCL. I'd like to return to this and find a test case
473 ;; which exercises this function both in CMU CL, to see
474 ;; whether it's really a previously-unexercised bug or
475 ;; whether I've misunderstood something (and, presumably,
476 ;; patched it wrong).
477 (slot-boundp-symbol instance slot-name-form)
479 (declare (ignore slot-boundp-symbol instance))
480 (let ((slot-name (eval slot-name-form)))
481 (optimize-instance-access slots
486 `(accessor-slot-boundp ,@(cdr form))))
488 (defun optimize-reader (slots sparameter gf-name form)
490 (optimize-accessor-call slots :read sparameter gf-name nil)
493 (defun optimize-writer (slots sparameter gf-name form)
495 (destructuring-bind (ignore1 ignore2 new-value) form
496 (declare (ignore ignore1 ignore2))
497 (optimize-accessor-call slots :write sparameter gf-name new-value))
500 ;;; The SLOTS argument is an alist, the CAR of each entry is the name
501 ;;; of a required parameter to the function. The alist is in order, so
502 ;;; the position of an entry in the alist corresponds to the
503 ;;; argument's position in the lambda list.
504 (defun optimize-instance-access (slots
509 (let ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
510 (parameter (if (consp sparameter) (car sparameter) sparameter)))
511 (if (and (eq *boot-state* 'complete)
513 (memq *the-class-structure-object* (class-precedence-list class)))
514 (let ((slotd (find-slot-definition class slot-name)))
517 `(,(slot-definition-defstruct-accessor-symbol slotd) ,parameter))
519 `(setf (,(slot-definition-defstruct-accessor-symbol slotd)
524 (let* ((parameter-entry (assq parameter slots))
525 (slot-entry (assq slot-name (cdr parameter-entry)))
526 (position (posq parameter-entry slots))
527 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
528 (unless parameter-entry
529 (error "internal error in slot optimization"))
531 (setq slot-entry (list slot-name))
532 (push slot-entry (cdr parameter-entry)))
533 (push pv-offset-form (cdr slot-entry))
536 `(instance-read ,pv-offset-form ,parameter ,position
537 ',slot-name ',class))
539 `(let ((.new-value. ,new-value))
540 (instance-write ,pv-offset-form ,parameter ,position
541 ',slot-name ',class .new-value.)))
543 `(instance-boundp ,pv-offset-form ,parameter ,position
544 ',slot-name ',class)))))))
546 (defun optimize-accessor-call (slots read/write sparameter gf-name new-value)
547 (let* ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
548 (parameter (if (consp sparameter) (car sparameter) sparameter))
549 (parameter-entry (assq parameter slots))
550 (name (case read/write
551 (:read `(reader ,gf-name))
552 (:write `(writer ,gf-name))))
553 (slot-entry (assoc name (cdr parameter-entry) :test #'equal))
554 (position (posq parameter-entry slots))
555 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
556 (unless parameter-entry
557 (error "internal error in slot optimization"))
559 (setq slot-entry (list name))
560 (push slot-entry (cdr parameter-entry)))
561 (push pv-offset-form (cdr slot-entry))
564 `(instance-reader ,pv-offset-form ,parameter ,position ,gf-name ',class))
566 `(let ((.new-value. ,new-value))
567 (instance-writer ,pv-offset-form ,parameter ,position ,gf-name ',class
570 (defvar *unspecific-arg* '..unspecific-arg..)
572 (defun optimize-gf-call-internal (form slots env)
573 (when (and (consp form)
574 (eq (car form) 'the))
575 (setq form (caddr form)))
576 (or (and (symbolp form)
577 (let* ((rebound? (caddr (variable-declaration '%variable-rebinding
579 (parameter-or-nil (car (assq (or rebound? form) slots))))
580 (when parameter-or-nil
581 (let* ((class-name (caddr (variable-declaration
582 'class parameter-or-nil env))))
583 (when (and class-name (not (eq class-name t)))
584 (position parameter-or-nil slots :key #'car))))))
586 (let ((form (eval form)))
592 (defun optimize-gf-call (slots calls gf-call-form nreq restp env)
593 (unless (eq (car gf-call-form) 'make-instance) ; XXX needs more work
594 (let* ((args (cdr gf-call-form))
595 (all-args-p (eq (car gf-call-form) 'make-instance))
596 (non-required-args (nthcdr nreq args))
597 (required-args (ldiff args non-required-args))
598 (call-spec (list (car gf-call-form) nreq restp
599 (mapcar #'(lambda (form)
600 (optimize-gf-call-internal form slots env))
604 (call-entry (assoc call-spec calls :test #'equal))
605 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
606 (unless (some #'integerp
607 (let ((spec-args (cdr call-spec)))
609 (ldiff spec-args (nthcdr nreq spec-args))
611 (return-from optimize-gf-call nil))
613 (setq call-entry (list call-spec))
614 (push call-entry (cdr calls)))
615 (push pv-offset-form (cdr call-entry))
616 (if (eq (car call-spec) 'make-instance)
617 `(funcall (pv-ref .pv. ,pv-offset-form) ,@(cdr gf-call-form))
618 `(let ((.emf. (pv-ref .pv. ,pv-offset-form)))
619 (invoke-effective-method-function .emf. ,restp
620 ,@required-args ,@(when restp `((list ,@non-required-args)))))))))
622 (define-walker-template pv-offset) ; These forms get munged by mutate slots.
623 (defmacro pv-offset (arg) arg)
624 (define-walker-template instance-accessor-parameter)
625 (defmacro instance-accessor-parameter (x) x)
627 ;;; It is safe for these two functions to be wrong. They just try to
628 ;;; guess what the most likely case will be.
629 (defun generate-fast-class-slot-access-p (class-form slot-name-form)
630 (let ((class (and (constantp class-form) (eval class-form)))
631 (slot-name (and (constantp slot-name-form) (eval slot-name-form))))
632 (and (eq *boot-state* 'complete)
633 (standard-class-p class)
634 (not (eq class *the-class-t*)) ; shouldn't happen, though.
635 (let ((slotd (find-slot-definition class slot-name)))
636 (and slotd (classp (slot-definition-allocation slotd)))))))
638 (defun skip-fast-slot-access-p (class-form slot-name-form type)
639 (let ((class (and (constantp class-form) (eval class-form)))
640 (slot-name (and (constantp slot-name-form) (eval slot-name-form))))
641 (and (eq *boot-state* 'complete)
642 (standard-class-p class)
643 (not (eq class *the-class-t*)) ; shouldn't happen, though.
644 (let ((slotd (find-slot-definition class slot-name)))
645 (and slotd (skip-optimize-slot-value-by-class-p class
649 (defun skip-optimize-slot-value-by-class-p (class slot-name type)
650 (let ((slotd (find-slot-definition class slot-name)))
652 (eq *boot-state* 'complete)
653 (not (slot-accessor-std-p slotd type)))))
655 (defmacro instance-read-internal (pv slots pv-offset default &optional type)
656 (unless (member type '(nil :instance :class :default))
657 (error "illegal type argument to ~S: ~S" 'instance-read-internal type))
658 (if (eq type ':default)
660 (let* ((index (gensym))
662 `(locally (declare #.*optimize-speed*)
663 (let ((,index (pvref ,pv ,pv-offset)))
664 (setq ,value (typecase ,index
665 ,@(when (or (null type) (eq type ':instance))
666 `((fixnum (clos-slots-ref ,slots ,index))))
667 ,@(when (or (null type) (eq type ':class))
668 `((cons (cdr ,index))))
670 (if (eq ,value +slot-unbound+)
674 (defmacro instance-read (pv-offset parameter position slot-name class)
675 (if (skip-fast-slot-access-p class slot-name 'reader)
676 `(accessor-slot-value ,parameter ,slot-name)
677 `(instance-read-internal .pv. ,(slot-vector-symbol position)
678 ,pv-offset (accessor-slot-value ,parameter ,slot-name)
679 ,(if (generate-fast-class-slot-access-p class slot-name)
680 ':class ':instance))))
682 (defmacro instance-reader (pv-offset parameter position gf-name class)
683 (declare (ignore class))
684 `(instance-read-internal .pv. ,(slot-vector-symbol position)
686 (,gf-name (instance-accessor-parameter ,parameter))
689 (defmacro instance-write-internal (pv slots pv-offset new-value default
691 (unless (member type '(nil :instance :class :default))
692 (error "illegal type argument to ~S: ~S" 'instance-write-internal type))
693 (if (eq type ':default)
695 (let* ((index (gensym)))
696 `(locally (declare #.*optimize-speed*)
697 (let ((,index (pvref ,pv ,pv-offset)))
699 ,@(when (or (null type) (eq type ':instance))
700 `((fixnum (setf (clos-slots-ref ,slots ,index)
702 ,@(when (or (null type) (eq type ':class))
703 `((cons (setf (cdr ,index) ,new-value))))
706 (defmacro instance-write (pv-offset
712 (if (skip-fast-slot-access-p class slot-name 'writer)
713 `(accessor-set-slot-value ,parameter ,slot-name ,new-value)
714 `(instance-write-internal .pv. ,(slot-vector-symbol position)
715 ,pv-offset ,new-value
716 (accessor-set-slot-value ,parameter ,slot-name ,new-value)
717 ,(if (generate-fast-class-slot-access-p class slot-name)
718 ':class ':instance))))
720 (defmacro instance-writer (pv-offset
726 (declare (ignore class))
727 `(instance-write-internal .pv. ,(slot-vector-symbol position)
728 ,pv-offset ,new-value
729 (,(if (consp gf-name)
730 (get-setf-function-name gf-name)
732 (instance-accessor-parameter ,parameter)
736 (defmacro instance-boundp-internal (pv slots pv-offset default
738 (unless (member type '(nil :instance :class :default))
739 (error "illegal type argument to ~S: ~S" 'instance-boundp-internal type))
740 (if (eq type ':default)
742 (let* ((index (gensym)))
743 `(locally (declare #.*optimize-speed*)
744 (let ((,index (pvref ,pv ,pv-offset)))
746 ,@(when (or (null type) (eq type ':instance))
747 `((fixnum (not (and ,slots
748 (eq (clos-slots-ref ,slots ,index)
750 ,@(when (or (null type) (eq type ':class))
751 `((cons (not (eq (cdr ,index) +slot-unbound+)))))
754 (defmacro instance-boundp (pv-offset parameter position slot-name class)
755 (if (skip-fast-slot-access-p class slot-name 'boundp)
756 `(accessor-slot-boundp ,parameter ,slot-name)
757 `(instance-boundp-internal .pv. ,(slot-vector-symbol position)
758 ,pv-offset (accessor-slot-boundp ,parameter ,slot-name)
759 ,(if (generate-fast-class-slot-access-p class slot-name)
760 ':class ':instance))))
762 ;;; This magic function has quite a job to do indeed.
764 ;;; The careful reader will recall that <slots> contains all of the
765 ;;; optimized slot access forms produced by OPTIMIZE-INSTANCE-ACCESS.
766 ;;; Each of these is a call to either INSTANCE-READ or INSTANCE-WRITE.
768 ;;; At the time these calls were produced, the first argument was
769 ;;; specified as the symbol .PV-OFFSET.; what we have to do now is
770 ;;; convert those pv-offset arguments into the actual number that is
771 ;;; the correct offset into the pv.
773 ;;; But first, oh but first, we sort <slots> a bit so that for each
774 ;;; argument we have the slots in alphabetical order. This
775 ;;; canonicalizes the PV-TABLE's a bit and will hopefully lead to
776 ;;; having fewer PV's floating around. Even if the gain is only
777 ;;; modest, it costs nothing.
778 (defun slot-name-lists-from-slots (slots calls)
779 (multiple-value-bind (slots calls) (mutate-slots-and-calls slots calls)
780 (let* ((slot-name-lists
781 (mapcar #'(lambda (parameter-entry)
782 (cons nil (mapcar #'car (cdr parameter-entry))))
785 (mapcar #'car calls)))
786 (dolist (call call-list)
787 (dolist (arg (cdr call))
789 (setf (car (nth arg slot-name-lists)) t))))
790 (setq slot-name-lists (mapcar #'(lambda (r+snl)
791 (when (or (car r+snl) (cdr r+snl))
794 (let ((cvt (apply #'vector
796 (mapcar #'(lambda (r+snl)
797 (when r+snl (incf i)))
799 (setq call-list (mapcar #'(lambda (call)
801 (mapcar #'(lambda (arg)
807 (values slot-name-lists call-list))))
809 (defun mutate-slots-and-calls (slots calls)
810 (let ((sorted-slots (sort-slots slots))
811 (sorted-calls (sort-calls (cdr calls)))
812 (pv-offset 0)) ; index 0 is for info
813 (dolist (parameter-entry sorted-slots)
814 (dolist (slot-entry (cdr parameter-entry))
816 (dolist (form (cdr slot-entry))
817 (setf (cadr form) pv-offset))))
818 (dolist (call-entry sorted-calls)
820 (dolist (form (cdr call-entry))
821 (setf (cadr form) pv-offset)))
822 (values sorted-slots sorted-calls)))
824 (defun symbol-pkg-name (sym)
825 (let ((pkg (symbol-package sym)))
826 (if pkg (package-name pkg) "")))
828 ;;; FIXME: Because of the existence of UNINTERN and RENAME-PACKAGE,
829 ;;; the part of this ordering which is based on SYMBOL-PKG-NAME is not
830 ;;; stable. This ordering is only used in to
831 ;;; SLOT-NAME-LISTS-FROM-SLOTS, where it serves to "canonicalize the
832 ;;; PV-TABLE's a bit and will hopefully lead to having fewer PV's
833 ;;; floating around", so it sounds as though the instability won't
834 ;;; actually lead to bugs, just small inefficiency. But still, it
835 ;;; would be better to reimplement this function as a comparison based
837 ;;; * stable comparison
838 ;;; * smaller code (here, and in being able to discard SYMBOL-PKG-NAME)
840 (defun symbol-lessp (a b)
841 (if (eq (symbol-package a)
843 (string-lessp (symbol-name a)
845 (string-lessp (symbol-pkg-name a)
846 (symbol-pkg-name b))))
848 (defun symbol-or-cons-lessp (a b)
851 (symbol (symbol-lessp a b))
855 (cons (if (eq (car a) (car b))
856 (symbol-or-cons-lessp (cdr a) (cdr b))
857 (symbol-or-cons-lessp (car a) (car b))))))))
859 (defun sort-slots (slots)
860 (mapcar (lambda (parameter-entry)
861 (cons (car parameter-entry)
862 (sort (cdr parameter-entry) ;slot entries
863 #'symbol-or-cons-lessp
867 (defun sort-calls (calls)
868 (sort calls #'symbol-or-cons-lessp :key #'car))
870 ;;;; This needs to work in terms of metatypes and also needs to work
871 ;;;; for automatically generated reader and writer functions.
872 ;;;; Automatically generated reader and writer functions use this
875 (defmacro pv-binding ((required-parameters slot-name-lists pv-table-symbol)
877 (with-gathering ((slot-vars (collecting))
878 (pv-parameters (collecting)))
879 (iterate ((slots (list-elements slot-name-lists))
880 (required-parameter (list-elements required-parameters))
881 (i (interval :from 0)))
883 (gather required-parameter pv-parameters)
884 (gather (slot-vector-symbol i) slot-vars)))
885 `(pv-binding1 (.pv. .calls. ,pv-table-symbol ,pv-parameters ,slot-vars)
888 (defmacro pv-binding1 ((pv calls pv-table-symbol pv-parameters slot-vars)
890 `(pv-env (,pv ,calls ,pv-table-symbol ,pv-parameters)
891 (let (,@(mapcar #'(lambda (slot-var p) `(,slot-var (get-slots-or-nil ,p)))
892 slot-vars pv-parameters))
895 ;;; This gets used only when the default MAKE-METHOD-LAMBDA is overridden.
896 (defmacro pv-env ((pv calls pv-table-symbol pv-parameters)
898 `(let* ((.pv-table. ,pv-table-symbol)
899 (.pv-cell. (pv-table-lookup-pv-args .pv-table. ,@pv-parameters))
900 (,pv (car .pv-cell.))
901 (,calls (cdr .pv-cell.)))
902 (declare ,(make-pv-type-declaration pv))
903 (declare ,(make-calls-type-declaration calls))
904 ,@(when (symbolp pv-table-symbol)
905 `((declare (special ,pv-table-symbol))))
909 (defvar *non-variable-declarations*
910 ;; FIXME: VALUES was in this list, conditionalized with #+CMU, but I
911 ;; don't *think* CMU CL had, or SBCL has, VALUES declarations. If
912 ;; SBCL doesn't have 'em, VALUES should probably be removed from
914 '(values %method-name %method-lambda-list
915 optimize ftype inline notinline))
917 (defvar *variable-declarations-with-argument*
921 (defvar *variable-declarations-without-argument*
923 ignorable special dynamic-extent
924 ;; FIXME: Possibly this entire list and variable could go away.
925 ;; If not, certainly we should remove all these built-in typenames
926 ;; from the list, and replace them with a test for "is it a type
927 ;; name?" (CLTL1 allowed only built-in type names as declarations,
928 ;; but ANSI CL allows any type name as a declaration.)
929 array atom base-char bignum bit bit-vector character compiled-function
930 complex cons double-float extended-char
931 fixnum float function hash-table integer
932 keyword list long-float nil null number package pathname random-state ratio
933 rational readtable sequence short-float signed-byte simple-array
934 simple-bit-vector simple-string simple-vector single-float standard-char
935 stream string symbol t unsigned-byte vector))
937 (defun split-declarations (body args calls-next-method-p)
938 (let ((inner-decls nil)
941 (loop (when (null body) (return nil))
942 (setq decl (car body))
943 (unless (and (consp decl)
944 (eq (car decl) 'declare))
946 (dolist (form (cdr decl))
948 (let ((declaration-name (car form)))
949 (if (member declaration-name *non-variable-declarations*)
950 (push `(declare ,form) outer-decls)
952 (member declaration-name
953 *variable-declarations-with-argument*))
955 (member declaration-name
956 *variable-declarations-without-argument*))
957 (dname (list (pop form)))
958 (inners nil) (outers nil))
959 (unless (or arg-p non-arg-p)
960 ;; FIXME: This warning, and perhaps the
961 ;; various *VARIABLE-DECLARATIONS-FOO* and/or
962 ;; *NON-VARIABLE-DECLARATIONS* variables,
963 ;; could probably go away now that we're not
964 ;; trying to be portable between different
965 ;; CLTL1 hosts the way PCL was. (Note that to
966 ;; do this right, we need to be able to handle
967 ;; user-defined (DECLAIM (DECLARATION FOO))
969 (warn "The declaration ~S is not understood by ~S.~@
970 Please put ~S on one of the lists ~S,~%~S, or~%~S.~@
971 (Assuming it is a variable declaration without argument)."
972 declaration-name 'split-declarations
974 '*non-variable-declarations*
975 '*variable-declarations-with-argument*
976 '*variable-declarations-without-argument*)
977 (push declaration-name
978 *variable-declarations-without-argument*))
980 (setq dname (append dname (list (pop form)))))
982 (if (member var args)
983 ;; Quietly remove IGNORE declarations on
984 ;; args when a next-method is involved, to
985 ;; prevent compiler warns about ignored
987 (unless (and calls-next-method-p
988 (eq (car dname) 'ignore))
992 (push `(declare (,@dname ,@outers)) outer-decls))
994 (push `(declare (,@dname ,@inners)) inner-decls)))))))
995 (setq body (cdr body)))
996 (values outer-decls inner-decls body)))
998 (defun make-method-initargs-form-internal (method-lambda initargs env)
999 (declare (ignore env))
1000 (let (method-lambda-args lmf lmf-params)
1001 (if (not (and (= 3 (length method-lambda))
1002 (= 2 (length (setq method-lambda-args (cadr method-lambda))))
1003 (consp (setq lmf (third method-lambda)))
1004 (eq 'simple-lexical-method-functions (car lmf))
1005 (eq (car method-lambda-args)
1006 (cadr (setq lmf-params (cadr lmf))))
1007 (eq (cadr method-lambda-args)
1008 (caddr lmf-params))))
1009 `(list* :function #',method-lambda
1011 (let* ((lambda-list (car lmf-params))
1012 (nreq 0)(restp nil)(args nil))
1013 (dolist (arg lambda-list)
1014 (when (member arg '(&optional &rest &key))
1015 (setq restp t)(return nil))
1016 (when (eq arg '&aux) (return nil))
1017 (incf nreq)(push arg args))
1018 (setq args (nreverse args))
1019 (setf (getf (getf initargs ':plist) ':arg-info) (cons nreq restp))
1020 (make-method-initargs-form-internal1
1021 initargs (cddr lmf) args lmf-params restp)))))
1023 (defun make-method-initargs-form-internal1
1024 (initargs body req-args lmf-params restp)
1025 (multiple-value-bind (outer-decls inner-decls body)
1027 body req-args (getf (cdr lmf-params) :call-next-method-p))
1028 (let* ((rest-arg (when restp '.rest-arg.))
1029 (args+rest-arg (if restp
1030 (append req-args (list rest-arg))
1032 `(list* :fast-function
1033 (lambda (.pv-cell. .next-method-call. ,@args+rest-arg)
1034 (declare (ignorable .pv-cell. .next-method-call.))
1036 (macrolet ((pv-env ((pv calls pv-table-symbol pv-parameters)
1038 (declare (ignore pv-table-symbol pv-parameters))
1039 `(let ((,pv (car .pv-cell.))
1040 (,calls (cdr .pv-cell.)))
1041 (declare ,(make-pv-type-declaration pv)
1042 ,(make-calls-type-declaration calls))
1045 (fast-lexical-method-functions
1046 (,(car lmf-params) .next-method-call. ,req-args ,rest-arg
1047 ,@(cdddr lmf-params))
1052 ;;; Use arrays and hash tables and the fngen stuff to make this much
1053 ;;; better. It doesn't really matter, though, because a function
1054 ;;; returned by this will get called only when the user explicitly
1055 ;;; funcalls a result of method-function. BUT, this is needed to make
1056 ;;; early methods work.
1057 (defun method-function-from-fast-function (fmf)
1058 (declare (type function fmf))
1059 (let* ((method-function nil) (pv-table nil)
1060 (arg-info (method-function-get fmf ':arg-info))
1061 (nreq (car arg-info))
1062 (restp (cdr arg-info)))
1063 (setq method-function
1064 #'(lambda (method-args next-methods)
1066 (setq pv-table (method-function-pv-table fmf)))
1067 (let* ((pv-cell (when pv-table
1068 (get-method-function-pv-cell
1069 method-function method-args pv-table)))
1070 (nm (car next-methods))
1071 (nms (cdr next-methods))
1074 :function (if (std-instance-p nm)
1075 (method-function nm)
1077 :call-method-args (list nms)))))
1079 (let* ((rest (nthcdr nreq method-args))
1080 (args (ldiff method-args rest)))
1081 (apply fmf pv-cell nmc (nconc args (list rest))))
1082 (apply fmf pv-cell nmc method-args)))))
1083 (let* ((fname (method-function-get fmf :name))
1084 (name `(,(or (get (car fname) 'method-sym)
1085 (setf (get (car fname) 'method-sym)
1086 (let ((str (symbol-name (car fname))))
1087 (if (string= "FAST-" str :end2 5)
1088 (intern (subseq str 5) *pcl-package*)
1091 (set-function-name method-function name))
1092 (setf (method-function-get method-function :fast-function) fmf)
1095 (defun get-method-function-pv-cell (method-function
1098 (let ((pv-table (or pv-table (method-function-pv-table method-function))))
1100 (let ((pv-wrappers (pv-wrappers-from-all-args pv-table method-args)))
1102 (pv-table-lookup pv-table pv-wrappers))))))
1104 (defun pv-table-lookup-pv-args (pv-table &rest pv-parameters)
1105 (pv-table-lookup pv-table (pv-wrappers-from-pv-args pv-parameters)))
1107 (defun pv-wrappers-from-pv-args (&rest args)
1108 (let* ((nkeys (length args))
1109 (pv-wrappers (make-list nkeys))
1113 (setq w (wrapper-of arg))
1114 (unless (eq t (wrapper-state w)) ; FIXME: should be INVALID-WRAPPER-P
1115 (setq w (check-wrapper-validity arg)))
1117 (setq w-t (cdr w-t))
1118 (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))
1121 (defun pv-wrappers-from-all-args (pv-table args)
1123 (slot-name-lists (pv-table-slot-name-lists pv-table)))
1124 (dolist (sn slot-name-lists)
1125 (when sn (incf nkeys)))
1126 (let* ((pv-wrappers (make-list nkeys))
1127 (pv-w-t pv-wrappers))
1128 (dolist (sn slot-name-lists)
1130 (let* ((arg (car args))
1131 (w (wrapper-of arg)))
1132 (unless w ; CAN-OPTIMIZE-ACCESS prevents this from happening.
1133 (error "error in PV-WRAPPERS-FROM-ALL-ARGS"))
1134 (setf (car pv-w-t) w)
1135 (setq pv-w-t (cdr pv-w-t))))
1136 (setq args (cdr args)))
1137 (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))
1140 (defun pv-wrappers-from-all-wrappers (pv-table wrappers)
1142 (slot-name-lists (pv-table-slot-name-lists pv-table)))
1143 (dolist (sn slot-name-lists)
1144 (when sn (incf nkeys)))
1145 (let* ((pv-wrappers (make-list nkeys))
1146 (pv-w-t pv-wrappers))
1147 (dolist (sn slot-name-lists)
1149 (let ((w (car wrappers)))
1150 (unless w ; CAN-OPTIMIZE-ACCESS prevents this from happening.
1151 (error "error in PV-WRAPPERS-FROM-ALL-WRAPPERS"))
1152 (setf (car pv-w-t) w)
1153 (setq pv-w-t (cdr pv-w-t))))
1154 (setq wrappers (cdr wrappers)))
1155 (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))