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 ;;; FIXME: The comment below seem to indicate that this was intended
51 ;;; to be actually used, however, it isn't anymore, and was commented
52 ;;; out at 0.9.13.47. Also removed was code in MAKE-PV-TABLE that
53 ;;; pushed each new PV-TABLE onto this list. --NS 2006-06-18
55 ;;; help new slot-value-using-class methods affect fast iv access
57 ;;; (defvar *all-pv-table-list* nil)
59 (declaim (inline make-pv-table))
60 (defun make-pv-table (&key slot-name-lists call-list)
61 (make-pv-table-internal slot-name-lists call-list))
63 (defun make-pv-table-type-declaration (var)
64 `(type pv-table ,var))
66 (defvar *slot-name-lists-inner* (make-hash-table :test 'equal))
67 (defvar *slot-name-lists-outer* (make-hash-table :test 'equal))
69 ;;; Entries in this are lists of (table . pv-offset-list).
70 (defvar *pv-key-to-pv-table-table* (make-hash-table :test 'equal))
72 (defun intern-pv-table (&key slot-name-lists call-list)
75 (or (gethash x *slot-name-lists-inner*)
76 (setf (gethash x *slot-name-lists-inner*) (copy-list x))))
78 (or (gethash x *slot-name-lists-outer*)
79 (setf (gethash x *slot-name-lists-outer*)
80 (let ((snl (copy-list (cdr x)))
83 (make-pv-table :slot-name-lists snl
86 (outer (mapcar #'inner (cons call-list slot-name-lists)))))
89 (dolist (slot-name-list slot-name-lists)
90 (dolist (slot-name (cdr slot-name-list))
91 (note-pv-table-reference slot-name pv-index pv-table)
93 (dolist (gf-call call-list)
94 (note-pv-table-reference gf-call pv-index pv-table)
96 (setf (pv-table-pv-size pv-table) pv-index)))
99 (defun note-pv-table-reference (ref pv-offset pv-table)
100 (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
102 (let ((table-entry (assq pv-table entry)))
103 (when (and (null table-entry)
104 (> (length entry) 8))
105 (let ((new-table-table (make-hash-table :size 16 :test 'eq)))
106 (dolist (table-entry entry)
107 (setf (gethash (car table-entry) new-table-table)
109 (setf (gethash ref *pv-key-to-pv-table-table*) new-table-table)))
111 (if (null table-entry)
112 (let ((new (cons pv-table pv-offset)))
114 (push new (cdr entry))
115 (setf (gethash ref *pv-key-to-pv-table-table*)
117 (push pv-offset (cdr table-entry)))
118 (return-from note-pv-table-reference nil))))
119 (let ((list (gethash pv-table entry)))
121 (push pv-offset (cdr list))
122 (setf (gethash pv-table entry) (list pv-offset)))))
125 (defun map-pv-table-references-of (ref function)
126 (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
128 (dolist (table+pv-offset-list entry)
130 (car table+pv-offset-list)
131 (cdr table+pv-offset-list)))
132 (maphash function entry)))
135 (defun optimize-slot-value-by-class-p (class slot-name type)
136 (or (not (eq *boot-state* 'complete))
137 (let ((slotd (find-slot-definition class slot-name)))
139 (slot-accessor-std-p slotd type)))))
141 (defun compute-pv-slot (slot-name wrapper class class-slots)
142 (if (symbolp slot-name)
143 (when (optimize-slot-value-by-class-p class slot-name 'all)
144 (or (instance-slot-index wrapper slot-name)
145 (assq slot-name class-slots)))
146 (when (consp slot-name)
147 (case (first slot-name)
149 (when (eq *boot-state* 'complete)
150 (let ((gf (gdefinition (second slot-name))))
151 (when (generic-function-p gf)
152 (accessor-values1 gf (first slot-name) class)))))
153 (t (bug "Don't know how to deal with ~S in ~S"
154 slot-name 'compute-pv-slots))))))
156 (defun compute-pv (slot-name-lists wrappers)
157 (unless (listp wrappers)
158 (setq wrappers (list wrappers)))
160 (dolist (slot-names slot-name-lists
161 (make-permutation-vector (nreverse elements)))
163 (let* ((wrapper (pop wrappers))
164 (std-p (typep wrapper 'wrapper))
165 (class (wrapper-class* wrapper))
166 (class-slots (and std-p (wrapper-class-slots wrapper))))
167 (dolist (slot-name (cdr slot-names))
169 (compute-pv-slot slot-name wrapper class class-slots)
173 (defun compute-calls (call-list wrappers)
174 (declare (ignore call-list wrappers))
178 (compute-emf-from-wrappers call wrappers))
183 #|| ; Need to finish this, then write the maintenance functions.
184 (defun compute-emf-from-wrappers (call wrappers)
186 (destructuring-bind (gf-name nreq restp arg-info) call
187 (if (eq gf-name 'make-instance)
188 (error "should not get here") ; there is another mechanism for this.
190 (if (not (eq *boot-state* 'complete))
191 (apply (gdefinition gf-name) args)
192 (let* ((gf (gdefinition gf-name))
193 (arg-info (arg-info-reader gf))
196 (emf (cache-miss-values-internal gf arg-info
197 wrappers classes types
199 (update-all-pv-tables call wrappers emf)
200 (invoke-emf emf args))))))))
203 (defun make-permutation-vector (indexes)
204 (make-array (length indexes) :initial-contents indexes))
206 (defun pv-table-lookup (pv-table pv-wrappers)
207 (let* ((slot-name-lists (pv-table-slot-name-lists pv-table))
208 (call-list (pv-table-call-list pv-table))
209 (cache (or (pv-table-cache pv-table)
210 (setf (pv-table-cache pv-table)
211 (get-cache (- (length slot-name-lists)
212 (count nil slot-name-lists))
216 (or (probe-cache cache pv-wrappers)
217 (let* ((pv (compute-pv slot-name-lists pv-wrappers))
218 (calls (compute-calls call-list pv-wrappers))
219 (pv-cell (cons pv calls))
220 (new-cache (fill-cache cache pv-wrappers pv-cell)))
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 pvref (pv index)
231 (defmacro copy-pv (pv)
234 (defun make-calls-type-declaration (var)
235 `(type simple-vector ,var))
237 (defmacro callsref (calls index)
238 `(svref ,calls ,index))
240 (defvar *pv-table-cache-update-info* nil)
242 (defun update-pv-table-cache-info (class)
243 (let ((slot-names-for-pv-table-update nil)
245 (dolist (icu *pv-table-cache-update-info*)
246 (if (eq (car icu) class)
247 (pushnew (cdr icu) slot-names-for-pv-table-update)
248 (push icu new-icui)))
249 (setq *pv-table-cache-update-info* new-icui)
250 (when slot-names-for-pv-table-update
251 (update-all-pv-table-caches class slot-names-for-pv-table-update))))
253 (defun update-all-pv-table-caches (class slot-names)
254 (let* ((cwrapper (class-wrapper class))
255 (std-p (typep cwrapper 'wrapper))
256 (class-slots (and std-p (wrapper-class-slots cwrapper)))
262 (compute-pv-slot slot-name cwrapper class class-slots)
266 (dolist (slot-name slot-names)
267 (map-pv-table-references-of
269 (lambda (pv-table pv-offset-list)
270 (declare (ignore pv-offset-list))
271 (pushnew pv-table pv-tables))))
272 (dolist (pv-table pv-tables)
273 (let* ((cache (pv-table-cache pv-table))
274 (slot-name-lists (pv-table-slot-name-lists pv-table))
275 (pv-size (pv-table-pv-size pv-table))
276 (pv-map (make-array pv-size :initial-element nil)))
277 (let ((map-index 0) (param-index 0))
278 (dolist (slot-name-list slot-name-lists)
279 (dolist (slot-name (cdr slot-name-list))
280 (let ((a (assoc slot-name new-values)))
281 (setf (svref pv-map map-index)
282 (and a (cons param-index (cdr a)))))
286 (map-cache (lambda (wrappers pv-cell)
287 (update-slots-in-pv wrappers (car pv-cell)
288 cwrapper pv-size pv-map))
291 (defun update-slots-in-pv (wrappers pv cwrapper pv-size pv-map)
293 (when (eq cwrapper wrappers)
294 (dotimes-fixnum (i pv-size)
295 (let ((map (svref pv-map i)))
297 (aver (= (car map) 0))
298 (setf (pvref pv i) (cdr map))))))
299 (when (memq cwrapper wrappers)
301 (dolist (wrapper wrappers)
302 (when (eq wrapper cwrapper)
303 (dotimes-fixnum (i pv-size)
304 (let ((map (svref pv-map i)))
305 (when (and map (= (car map) param))
306 (setf (pvref pv i) (cdr map))))))
309 (defun maybe-expand-accessor-form (form required-parameters slots env)
310 (let* ((fname (car form))
311 #||(len (length form))||#
312 (gf (if (symbolp fname)
313 (unencapsulated-fdefinition fname)
314 (gdefinition fname))))
315 (macrolet ((maybe-optimize-reader ()
317 (can-optimize-access1 (cadr form)
318 required-parameters env)))
320 (optimize-reader slots parameter gf-name form))))
321 (maybe-optimize-writer ()
323 (can-optimize-access1 (caddr form)
324 required-parameters env)))
326 (optimize-writer slots parameter gf-name form)))))
327 (unless (and (consp (cadr form))
328 (eq 'instance-accessor-parameter (caadr form)))
329 (when (and (eq *boot-state* 'complete)
330 (generic-function-p gf))
331 (let ((methods (generic-function-methods gf)))
333 (let* ((gf-name (generic-function-name gf))
334 (arg-info (gf-arg-info gf))
335 (metatypes (arg-info-metatypes arg-info))
336 (nreq (length metatypes))
337 (applyp (arg-info-applyp arg-info)))
340 (when (some #'standard-reader-method-p methods)
341 (maybe-optimize-reader)))
344 (eq (car gf-name) 'setf))
345 (when (some #'standard-writer-method-p methods)
346 (maybe-optimize-writer)))))))))))))
348 (defun optimize-generic-function-call (form
353 (declare (ignore required-parameters env slots calls))
354 (or ; (optimize-reader ...)?
357 (defun can-optimize-access (form required-parameters env)
358 (let ((type (ecase (car form)
360 (set-slot-value 'writer)
361 (slot-boundp 'boundp)))
363 (slot-name (eval (caddr form)))) ; known to be constant
364 (can-optimize-access1 var required-parameters env type slot-name)))
366 ;;; FIXME: This looks like an internal helper function for
367 ;;; CAN-OPTIMIZE-ACCESS, and it is used that way, but it's also called
368 ;;; bare from several places in the code. Perhaps the two functions
369 ;;; should be renamed CAN-OPTIMIZE-ACCESS-FOR-FORM and
370 ;;; CAN-OPTIMIZE-ACCESS-FOR-VAR. If so, I'd just as soon use keyword
371 ;;; args instead of optional ones, too.
372 (defun can-optimize-access1 (var required-parameters env
373 &optional type slot-name)
374 (when (and (consp var) (eq 'the (car var)))
375 ;; FIXME: We should assert list of length 3 here. Or maybe we
376 ;; should just define EXTRACT-THE, replace the whole
379 ;; (AWHEN (EXTRACT-THE VAR)
381 ;; and then use EXTRACT-THE similarly to clean up the other tests
382 ;; against 'THE scattered through the PCL code.
383 (setq var (caddr var)))
385 (let* ((rebound? (caddr (var-declaration '%variable-rebinding var env)))
386 (parameter-or-nil (car (memq (or rebound? var)
387 required-parameters))))
388 (when parameter-or-nil
389 (let* ((class-name (caddr (var-declaration '%class
392 (class (find-class class-name nil)))
393 (when (or (not (eq *boot-state* 'complete))
394 (and class (not (class-finalized-p class))))
396 (when (and class-name (not (eq class-name t)))
397 (when (or (null type)
399 (memq *the-class-structure-object*
400 (class-precedence-list class))))
401 (optimize-slot-value-by-class-p class slot-name type))
402 (cons parameter-or-nil (or class class-name)))))))))
404 ;;; Check whether the binding of the named variable is modified in the
406 (defun parameter-modified-p (parameter-name env)
407 (let ((modified-variables (macroexpand '%parameter-binding-modified env)))
408 (memq parameter-name modified-variables)))
410 (defun optimize-slot-value (slots sparameter form)
412 (let ((optimized-form
413 (destructuring-bind (ignore1 ignore2 slot-name-form) form
414 (declare (ignore ignore1 ignore2))
415 (let ((slot-name (eval slot-name-form)))
416 (optimize-instance-access slots :read sparameter
418 ;; We don't return the optimized form directly, since there's
419 ;; still a chance that we'll find out later on that the
420 ;; optimization should not have been done, for example due to
421 ;; the walker encountering a SETQ on SPARAMETER later on in
422 ;; the body [ see for example clos.impure.lisp test with :name
423 ;; ((:setq :method-parameter) slot-value)) ]. Instead we defer
424 ;; the decision until the compiler macroexpands
425 ;; OPTIMIZED-SLOT-VALUE.
427 ;; Note that we must still call OPTIMIZE-INSTANCE-ACCESS at
428 ;; this point (instead of when expanding
429 ;; OPTIMIZED-SLOT-VALUE), since it mutates the structure of
430 ;; SLOTS. If that mutation isn't done while during the
431 ;; walking, MAKE-METHOD-LAMBDA-INTERNAL won't wrap a correct
432 ;; PV-BINDING form around the body, and compilation will fail.
433 ;; -- JES, 2006-09-18
434 `(optimized-slot-value ,form ,(car sparameter) ,optimized-form))
435 `(accessor-slot-value ,@(cdr form))))
437 (defmacro optimized-slot-value (form parameter-name optimized-form
439 ;; Either use OPTIMIZED-FORM or fall back to the safe
440 ;; ACCESSOR-SLOT-VALUE.
441 (if (parameter-modified-p parameter-name env)
442 `(accessor-slot-value ,@(cdr form))
445 (defun optimize-set-slot-value (slots sparameter form)
447 (let ((optimized-form
448 (destructuring-bind (ignore1 ignore2 slot-name-form new-value) form
449 (declare (ignore ignore1 ignore2))
450 (let ((slot-name (eval slot-name-form)))
451 (optimize-instance-access slots
456 ;; See OPTIMIZE-SLOT-VALUE
457 `(optimized-set-slot-value ,form ,(car sparameter) ,optimized-form))
458 `(accessor-set-slot-value ,@(cdr form))))
460 (defmacro optimized-set-slot-value (form parameter-name optimized-form
462 (if (parameter-modified-p parameter-name env)
463 `(accessor-set-slot-value ,@(cdr form))
466 (defun optimize-slot-boundp (slots sparameter form)
468 (let ((optimized-form
470 ;; FIXME: In CMU CL ca. 19991205, this binding list
471 ;; had a fourth element in it, NEW-VALUE. It's hard
472 ;; to see how that could possibly be right, since
473 ;; SLOT-BOUNDP has no NEW-VALUE. Since it was
474 ;; causing a failure in building PCL for SBCL, so I
475 ;; changed it to match the definition of
476 ;; SLOT-BOUNDP (and also to match the list used in
477 ;; the similar OPTIMIZE-SLOT-VALUE,
478 ;; above). However, I'm weirded out by this, since
479 ;; this is old code which has worked for ages to
480 ;; build PCL for CMU CL, so it's hard to see why it
481 ;; should need a patch like this in order to build
482 ;; PCL for SBCL. I'd like to return to this and
483 ;; find a test case which exercises this function
484 ;; both in CMU CL, to see whether it's really a
485 ;; previously-unexercised bug or whether I've
486 ;; misunderstood something (and, presumably,
487 ;; patched it wrong).
488 (slot-boundp-symbol instance slot-name-form)
490 (declare (ignore slot-boundp-symbol instance))
491 (let ((slot-name (eval slot-name-form)))
492 (optimize-instance-access slots
497 ;; See OPTIMIZE-SLOT-VALUE
498 `(optimized-slot-boundp ,form ,(car sparameter) ,optimized-form))
499 `(accessor-slot-boundp ,@(cdr form))))
501 (defmacro optimized-slot-boundp (form parameter-name optimized-form
503 (if (parameter-modified-p parameter-name env)
504 `(accessor-slot-boundp ,@(cdr form))
507 (defun optimize-reader (slots sparameter gf-name form)
509 (optimize-accessor-call slots :read sparameter gf-name nil)
512 (defun optimize-writer (slots sparameter gf-name form)
514 (destructuring-bind (ignore1 ignore2 new-value) form
515 (declare (ignore ignore1 ignore2))
516 (optimize-accessor-call slots :write sparameter gf-name new-value))
519 ;;; The SLOTS argument is an alist, the CAR of each entry is the name
520 ;;; of a required parameter to the function. The alist is in order, so
521 ;;; the position of an entry in the alist corresponds to the
522 ;;; argument's position in the lambda list.
523 (defun optimize-instance-access (slots
528 (let ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
529 (parameter (if (consp sparameter) (car sparameter) sparameter)))
530 (if (and (eq *boot-state* 'complete)
532 (memq *the-class-structure-object* (class-precedence-list class)))
533 (let ((slotd (find-slot-definition class slot-name)))
536 `(,(slot-definition-defstruct-accessor-symbol slotd) ,parameter))
538 `(setf (,(slot-definition-defstruct-accessor-symbol slotd)
543 (let* ((parameter-entry (assq parameter slots))
544 (slot-entry (assq slot-name (cdr parameter-entry)))
545 (position (posq parameter-entry slots))
546 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
547 (unless parameter-entry
548 (bug "slot optimization bewilderment: O-I-A"))
550 (setq slot-entry (list slot-name))
551 (push slot-entry (cdr parameter-entry)))
552 (push pv-offset-form (cdr slot-entry))
555 `(instance-read ,pv-offset-form ,parameter ,position
556 ',slot-name ',class))
558 `(let ((.new-value. ,new-value))
559 (instance-write ,pv-offset-form ,parameter ,position
560 ',slot-name ',class .new-value.)))
562 `(instance-boundp ,pv-offset-form ,parameter ,position
563 ',slot-name ',class)))))))
565 (defun optimize-accessor-call (slots read/write sparameter gf-name new-value)
566 (let* ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
567 (parameter (if (consp sparameter) (car sparameter) sparameter))
568 (parameter-entry (assq parameter slots))
569 (name (case read/write
570 (:read `(reader ,gf-name))
571 (:write `(writer ,gf-name))))
572 (slot-entry (assoc name (cdr parameter-entry) :test #'equal))
573 (position (posq parameter-entry slots))
574 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
575 (unless parameter-entry
576 (error "slot optimization bewilderment: O-A-C"))
578 (setq slot-entry (list name))
579 (push slot-entry (cdr parameter-entry)))
580 (push pv-offset-form (cdr slot-entry))
583 `(instance-reader ,pv-offset-form ,parameter ,position ,gf-name ',class))
585 `(let ((.new-value. ,new-value))
586 (instance-writer ,pv-offset-form ,parameter ,position ,gf-name ',class
589 (defvar *unspecific-arg* '..unspecific-arg..)
591 (defun optimize-gf-call-internal (form slots env)
592 (when (and (consp form)
593 (eq (car form) 'the))
594 (setq form (caddr form)))
595 (or (and (symbolp form)
596 (let* ((rebound? (caddr (var-declaration '%variable-rebinding
599 (parameter-or-nil (car (assq (or rebound? form) slots))))
600 (when parameter-or-nil
601 (let* ((class-name (caddr (var-declaration 'class
604 (when (and class-name (not (eq class-name t)))
605 (position parameter-or-nil slots :key #'car))))))
607 (let ((form (constant-form-value form)))
613 (defun optimize-gf-call (slots calls gf-call-form nreq restp env)
614 (unless (eq (car gf-call-form) 'make-instance) ; XXX needs more work
615 (let* ((args (cdr gf-call-form))
616 (all-args-p (eq (car gf-call-form) 'make-instance))
617 (non-required-args (nthcdr nreq args))
618 (required-args (ldiff args non-required-args))
619 (call-spec (list (car gf-call-form) nreq restp
620 (mapcar (lambda (form)
621 (optimize-gf-call-internal form slots env))
625 (call-entry (assoc call-spec calls :test #'equal))
626 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
627 (unless (some #'integerp
628 (let ((spec-args (cdr call-spec)))
630 (ldiff spec-args (nthcdr nreq spec-args))
632 (return-from optimize-gf-call nil))
634 (setq call-entry (list call-spec))
635 (push call-entry (cdr calls)))
636 (push pv-offset-form (cdr call-entry))
637 (if (eq (car call-spec) 'make-instance)
638 `(funcall (pv-ref .pv. ,pv-offset-form) ,@(cdr gf-call-form))
639 `(let ((.emf. (pv-ref .pv. ,pv-offset-form)))
640 (invoke-effective-method-function .emf. ,restp
641 ,@required-args ,@(when restp `((list ,@non-required-args)))))))))
643 (define-walker-template pv-offset) ; These forms get munged by mutate slots.
644 (defmacro pv-offset (arg) arg)
645 (define-walker-template instance-accessor-parameter)
646 (defmacro instance-accessor-parameter (x) x)
648 ;;; It is safe for these two functions to be wrong. They just try to
649 ;;; guess what the most likely case will be.
650 (defun generate-fast-class-slot-access-p (class-form slot-name-form)
651 (let ((class (and (constantp class-form) (constant-form-value class-form)))
652 (slot-name (and (constantp slot-name-form)
653 (constant-form-value slot-name-form))))
654 (and (eq *boot-state* 'complete)
655 (standard-class-p class)
656 (not (eq class *the-class-t*)) ; shouldn't happen, though.
657 (let ((slotd (find-slot-definition class slot-name)))
658 (and slotd (eq :class (slot-definition-allocation slotd)))))))
660 (defun skip-fast-slot-access-p (class-form slot-name-form type)
661 (let ((class (and (constantp class-form) (constant-form-value class-form)))
662 (slot-name (and (constantp slot-name-form)
663 (constant-form-value slot-name-form))))
664 (and (eq *boot-state* 'complete)
665 (standard-class-p class)
666 (not (eq class *the-class-t*)) ; shouldn't happen, though.
667 (let ((slotd (find-slot-definition class slot-name)))
668 (and slotd (skip-optimize-slot-value-by-class-p class
672 (defun skip-optimize-slot-value-by-class-p (class slot-name type)
673 (let ((slotd (find-slot-definition class slot-name)))
675 (eq *boot-state* 'complete)
676 (not (slot-accessor-std-p slotd type)))))
678 (defmacro instance-read-internal (pv slots pv-offset default &optional type)
679 (unless (member type '(nil :instance :class :default))
680 (error "illegal type argument to ~S: ~S" 'instance-read-internal type))
681 (if (eq type :default)
683 (let* ((index (gensym))
685 `(locally (declare #.*optimize-speed*)
686 (let ((,index (pvref ,pv ,pv-offset)))
687 (setq ,value (typecase ,index
688 ;; FIXME: the line marked by KLUDGE below
689 ;; (and the analogous spot in
690 ;; INSTANCE-WRITE-INTERNAL) is there purely
691 ;; to suppress a type mismatch warning that
692 ;; propagates through to user code.
693 ;; Presumably SLOTS at this point can never
694 ;; actually be NIL, but the compiler seems
695 ;; to think it could, so we put this here
696 ;; to shut it up. (see also mail Rudi
697 ;; Schlatte sbcl-devel 2003-09-21) -- CSR,
699 ,@(when (or (null type) (eq type :instance))
702 (clos-slots-ref ,slots ,index)))))
703 ,@(when (or (null type) (eq type :class))
704 `((cons (cdr ,index))))
706 (if (eq ,value +slot-unbound+)
710 (defmacro instance-read (pv-offset parameter position slot-name class)
711 (if (skip-fast-slot-access-p class slot-name 'reader)
712 `(accessor-slot-value ,parameter ,slot-name)
713 `(instance-read-internal .pv. ,(slot-vector-symbol position)
714 ,pv-offset (accessor-slot-value ,parameter ,slot-name)
715 ,(if (generate-fast-class-slot-access-p class slot-name)
718 (defmacro instance-reader (pv-offset parameter position gf-name class)
719 (declare (ignore class))
720 `(instance-read-internal .pv. ,(slot-vector-symbol position)
722 (,gf-name (instance-accessor-parameter ,parameter))
725 (defmacro instance-write-internal (pv slots pv-offset new-value default
727 (unless (member type '(nil :instance :class :default))
728 (error "illegal type argument to ~S: ~S" 'instance-write-internal type))
729 (if (eq type :default)
731 (let* ((index (gensym)))
732 `(locally (declare #.*optimize-speed*)
733 (let ((,index (pvref ,pv ,pv-offset)))
735 ,@(when (or (null type) (eq type :instance))
736 `((fixnum (and ,slots
737 (setf (clos-slots-ref ,slots ,index)
739 ,@(when (or (null type) (eq type :class))
740 `((cons (setf (cdr ,index) ,new-value))))
743 (defmacro instance-write (pv-offset
749 (if (skip-fast-slot-access-p class slot-name 'writer)
750 `(accessor-set-slot-value ,parameter ,slot-name ,new-value)
751 `(instance-write-internal .pv. ,(slot-vector-symbol position)
752 ,pv-offset ,new-value
753 (accessor-set-slot-value ,parameter ,slot-name ,new-value)
754 ,(if (generate-fast-class-slot-access-p class slot-name)
757 (defmacro instance-writer (pv-offset
763 (declare (ignore class))
764 `(instance-write-internal .pv. ,(slot-vector-symbol position)
765 ,pv-offset ,new-value
766 (,(if (consp gf-name)
767 (get-setf-fun-name gf-name)
769 (instance-accessor-parameter ,parameter)
773 (defmacro instance-boundp-internal (pv slots pv-offset default
775 (unless (member type '(nil :instance :class :default))
776 (error "illegal type argument to ~S: ~S" 'instance-boundp-internal type))
777 (if (eq type :default)
779 (let* ((index (gensym)))
780 `(locally (declare #.*optimize-speed*)
781 (let ((,index (pvref ,pv ,pv-offset)))
783 ,@(when (or (null type) (eq type :instance))
784 `((fixnum (not (and ,slots
785 (eq (clos-slots-ref ,slots ,index)
787 ,@(when (or (null type) (eq type :class))
788 `((cons (not (eq (cdr ,index) +slot-unbound+)))))
791 (defmacro instance-boundp (pv-offset parameter position slot-name class)
792 (if (skip-fast-slot-access-p class slot-name 'boundp)
793 `(accessor-slot-boundp ,parameter ,slot-name)
794 `(instance-boundp-internal .pv. ,(slot-vector-symbol position)
795 ,pv-offset (accessor-slot-boundp ,parameter ,slot-name)
796 ,(if (generate-fast-class-slot-access-p class slot-name)
799 ;;; This magic function has quite a job to do indeed.
801 ;;; The careful reader will recall that <slots> contains all of the
802 ;;; optimized slot access forms produced by OPTIMIZE-INSTANCE-ACCESS.
803 ;;; Each of these is a call to either INSTANCE-READ or INSTANCE-WRITE.
805 ;;; At the time these calls were produced, the first argument was
806 ;;; specified as the symbol .PV-OFFSET.; what we have to do now is
807 ;;; convert those pv-offset arguments into the actual number that is
808 ;;; the correct offset into the pv.
810 ;;; But first, oh but first, we sort <slots> a bit so that for each
811 ;;; argument we have the slots in alphabetical order. This
812 ;;; canonicalizes the PV-TABLE's a bit and will hopefully lead to
813 ;;; having fewer PV's floating around. Even if the gain is only
814 ;;; modest, it costs nothing.
815 (defun slot-name-lists-from-slots (slots calls)
816 (multiple-value-bind (slots calls) (mutate-slots-and-calls slots calls)
817 (let* ((slot-name-lists
818 (mapcar (lambda (parameter-entry)
819 (cons nil (mapcar #'car (cdr parameter-entry))))
822 (mapcar #'car calls)))
823 (dolist (call call-list)
824 (dolist (arg (cdr call))
826 (setf (car (nth arg slot-name-lists)) t))))
827 (setq slot-name-lists (mapcar (lambda (r+snl)
828 (when (or (car r+snl) (cdr r+snl))
831 (let ((cvt (apply #'vector
833 (mapcar (lambda (r+snl)
834 (when r+snl (incf i)))
836 (setq call-list (mapcar (lambda (call)
838 (mapcar (lambda (arg)
844 (values slot-name-lists call-list))))
846 (defun mutate-slots-and-calls (slots calls)
847 (let ((sorted-slots (sort-slots slots))
848 (sorted-calls (sort-calls (cdr calls)))
850 (dolist (parameter-entry sorted-slots)
851 (dolist (slot-entry (cdr parameter-entry))
853 (dolist (form (cdr slot-entry))
854 (setf (cadr form) pv-offset))))
855 (dolist (call-entry sorted-calls)
857 (dolist (form (cdr call-entry))
858 (setf (cadr form) pv-offset)))
859 (values sorted-slots sorted-calls)))
861 (defun symbol-pkg-name (sym)
862 (let ((pkg (symbol-package sym)))
863 (if pkg (package-name pkg) "")))
865 ;;; FIXME: Because of the existence of UNINTERN and RENAME-PACKAGE,
866 ;;; the part of this ordering which is based on SYMBOL-PKG-NAME is not
867 ;;; stable. This ordering is only used in to
868 ;;; SLOT-NAME-LISTS-FROM-SLOTS, where it serves to "canonicalize the
869 ;;; PV-TABLE's a bit and will hopefully lead to having fewer PV's
870 ;;; floating around", so it sounds as though the instability won't
871 ;;; actually lead to bugs, just small inefficiency. But still, it
872 ;;; would be better to reimplement this function as a comparison based
874 ;;; * stable comparison
875 ;;; * smaller code (here, and in being able to discard SYMBOL-PKG-NAME)
877 (defun symbol-lessp (a b)
878 (if (eq (symbol-package a)
880 (string-lessp (symbol-name a)
882 (string-lessp (symbol-pkg-name a)
883 (symbol-pkg-name b))))
885 (defun symbol-or-cons-lessp (a b)
888 (symbol (symbol-lessp a b))
892 (cons (if (eq (car a) (car b))
893 (symbol-or-cons-lessp (cdr a) (cdr b))
894 (symbol-or-cons-lessp (car a) (car b))))))))
896 (defun sort-slots (slots)
897 (mapcar (lambda (parameter-entry)
898 (cons (car parameter-entry)
899 (sort (cdr parameter-entry) ;slot entries
900 #'symbol-or-cons-lessp
904 (defun sort-calls (calls)
905 (sort calls #'symbol-or-cons-lessp :key #'car))
907 ;;;; This needs to work in terms of metatypes and also needs to work
908 ;;;; for automatically generated reader and writer functions.
909 ;;;; Automatically generated reader and writer functions use this
912 (defmacro pv-binding ((required-parameters slot-name-lists pv-table-form)
914 (let (slot-vars pv-parameters)
915 (loop for slots in slot-name-lists
916 for required-parameter in required-parameters
919 (push required-parameter pv-parameters)
920 (push (slot-vector-symbol i) slot-vars)))
921 `(pv-binding1 (.pv. .calls. ,pv-table-form
922 ,(nreverse pv-parameters) ,(nreverse slot-vars))
925 (defmacro pv-binding1 ((pv calls pv-table-form pv-parameters slot-vars)
927 `(pv-env (,pv ,calls ,pv-table-form ,pv-parameters)
928 (let (,@(mapcar (lambda (slot-var p) `(,slot-var (get-slots-or-nil ,p)))
929 slot-vars pv-parameters))
930 (declare (ignorable ,@(mapcar #'identity slot-vars)))
933 ;;; This will only be visible in PV-ENV when the default MAKE-METHOD-LAMBDA is
935 (define-symbol-macro pv-env-environment overridden)
937 (defmacro pv-env (&environment env
938 (pv calls pv-table-form pv-parameters)
940 ;; Decide which expansion to use based on the state of the PV-ENV-ENVIRONMENT
942 (if (eq (macroexpand 'pv-env-environment env) 'default)
943 `(let ((,pv (car .pv-cell.))
944 (,calls (cdr .pv-cell.)))
945 (declare ,(make-pv-type-declaration pv)
946 ,(make-calls-type-declaration calls))
949 `(let* ((.pv-table. ,pv-table-form)
950 (.pv-cell. (pv-table-lookup-pv-args .pv-table. ,@pv-parameters))
951 (,pv (car .pv-cell.))
952 (,calls (cdr .pv-cell.)))
953 (declare ,(make-pv-type-declaration pv))
954 (declare ,(make-calls-type-declaration calls))
958 (defvar *non-var-declarations*
959 ;; FIXME: VALUES was in this list, conditionalized with #+CMU, but I
960 ;; don't *think* CMU CL had, or SBCL has, VALUES declarations. If
961 ;; SBCL doesn't have 'em, VALUES should probably be removed from
971 (defvar *var-declarations-with-arg*
975 (defvar *var-declarations-without-arg*
977 ignorable special dynamic-extent
978 ;; FIXME: Possibly this entire list and variable could go away.
979 ;; If not, certainly we should remove all these built-in typenames
980 ;; from the list, and replace them with a test for "is it a type
981 ;; name?" (CLTL1 allowed only built-in type names as declarations,
982 ;; but ANSI CL allows any type name as a declaration.)
983 array atom base-char bignum bit bit-vector character compiled-function
984 complex cons double-float extended-char
985 fixnum float function hash-table integer
986 keyword list long-float nil null number package pathname random-state ratio
987 rational readtable sequence short-float signed-byte simple-array
988 simple-bit-vector simple-string simple-vector single-float standard-char
989 stream string symbol t unsigned-byte vector))
991 (defun split-declarations (body args maybe-reads-params-p)
992 (let ((inner-decls nil)
995 (loop (when (null body) (return nil))
996 (setq decl (car body))
997 (unless (and (consp decl)
998 (eq (car decl) 'declare))
1000 (dolist (form (cdr decl))
1002 (let ((declaration-name (car form)))
1003 (if (member declaration-name *non-var-declarations*)
1004 (push `(declare ,form) outer-decls)
1006 (member declaration-name
1007 *var-declarations-with-arg*))
1009 (member declaration-name
1010 *var-declarations-without-arg*))
1011 (dname (list (pop form)))
1012 (inners nil) (outers nil))
1013 (unless (or arg-p non-arg-p)
1014 ;; FIXME: This warning, and perhaps the
1015 ;; various *VAR-DECLARATIONS-FOO* and/or
1016 ;; *NON-VAR-DECLARATIONS* variables,
1017 ;; could probably go away now that we're not
1018 ;; trying to be portable between different
1019 ;; CLTL1 hosts the way PCL was. (Note that to
1020 ;; do this right, we need to be able to handle
1021 ;; user-defined (DECLAIM (DECLARATION FOO))
1023 (warn "The declaration ~S is not understood by ~S.~@
1024 Please put ~S on one of the lists ~S,~%~S, or~%~S.~@
1025 (Assuming it is a variable declaration without argument)."
1026 declaration-name 'split-declarations
1028 '*non-var-declarations*
1029 '*var-declarations-with-arg*
1030 '*var-declarations-without-arg*)
1031 (push declaration-name *var-declarations-without-arg*))
1033 (setq dname (append dname (list (pop form)))))
1035 (%class (push `(declare (,@dname ,@form)) inner-decls))
1038 (if (member var args)
1039 ;; Quietly remove IGNORE declarations
1040 ;; on args when a next-method is
1041 ;; involved, to prevent compiler
1042 ;; warnings about ignored args being
1044 (unless (and maybe-reads-params-p
1045 (eq (car dname) 'ignore))
1049 (push `(declare (,@dname ,@outers)) outer-decls))
1052 `(declare (,@dname ,@inners))
1053 inner-decls)))))))))
1054 (setq body (cdr body)))
1055 (values outer-decls inner-decls body)))
1057 ;;; Pull a name out of the %METHOD-NAME declaration in the function
1058 ;;; body given, or return NIL if no %METHOD-NAME declaration is found.
1059 (defun body-method-name (body)
1060 (multiple-value-bind (real-body declarations documentation)
1062 (declare (ignore real-body documentation))
1063 (let ((name-decl (get-declaration '%method-name declarations)))
1065 (destructuring-bind (name) name-decl
1068 ;;; Convert a lambda expression containing a SB-PCL::%METHOD-NAME
1069 ;;; declaration (which is a naming style internal to PCL) into an
1070 ;;; SB-INT:NAMED-LAMBDA expression (which is a naming style used
1071 ;;; throughout SBCL, understood by the main compiler); or if there's
1072 ;;; no SB-PCL::%METHOD-NAME declaration, then just return the original
1073 ;;; lambda expression.
1074 (defun name-method-lambda (method-lambda)
1075 (let ((method-name (body-method-name (cddr method-lambda))))
1077 `(named-lambda (slow-method ,method-name) ,(rest method-lambda))
1080 (defun make-method-initargs-form-internal (method-lambda initargs env)
1081 (declare (ignore env))
1082 (let (method-lambda-args
1083 lmf ; becomes body of function
1085 (if (not (and (= 3 (length method-lambda))
1086 (= 2 (length (setq method-lambda-args (cadr method-lambda))))
1087 (consp (setq lmf (third method-lambda)))
1088 (eq 'simple-lexical-method-functions (car lmf))
1089 (eq (car method-lambda-args)
1090 (cadr (setq lmf-params (cadr lmf))))
1091 (eq (cadr method-lambda-args)
1092 (caddr lmf-params))))
1093 `(list* :function ,(name-method-lambda method-lambda)
1095 (let* ((lambda-list (car lmf-params))
1099 (dolist (arg lambda-list)
1100 (when (member arg '(&optional &rest &key))
1103 (when (eq arg '&aux)
1107 (setq args (nreverse args))
1108 (setf (getf (getf initargs 'plist) :arg-info) (cons nreq restp))
1109 (make-method-initargs-form-internal1
1110 initargs (cddr lmf) args lmf-params restp)))))
1112 (defun make-method-initargs-form-internal1
1113 (initargs body req-args lmf-params restp)
1114 (multiple-value-bind (outer-decls inner-decls body-sans-decls)
1116 body req-args (or (getf (cdr lmf-params) :call-next-method-p)
1117 (getf (cdr lmf-params) :setq-p)))
1118 (let* ((rest-arg (when restp '.rest-arg.))
1119 (args+rest-arg (if restp
1120 (append req-args (list rest-arg))
1124 (let* ((fmf (,(if (body-method-name body) 'named-lambda 'lambda)
1125 ,@(when (body-method-name body)
1127 (list (cons 'fast-method (body-method-name body))))
1128 (.pv-cell. .next-method-call. ,@args+rest-arg) ; function args
1129 ;; body of the function
1130 (declare (ignorable .pv-cell. .next-method-call.)
1131 (disable-package-locks pv-env-environment))
1133 (symbol-macrolet ((pv-env-environment default))
1134 (fast-lexical-method-functions
1135 (,(car lmf-params) .next-method-call. ,req-args ,rest-arg
1136 ,@(cdddr lmf-params))
1138 ,@body-sans-decls))))
1139 (mf (%make-method-function fmf nil)))
1140 (set-funcallable-instance-function
1141 mf (method-function-from-fast-function fmf ',(getf initargs 'plist)))
1145 ;;; Use arrays and hash tables and the fngen stuff to make this much
1146 ;;; better. It doesn't really matter, though, because a function
1147 ;;; returned by this will get called only when the user explicitly
1148 ;;; funcalls a result of method-function. BUT, this is needed to make
1149 ;;; early methods work.
1150 (defun method-function-from-fast-function (fmf plist)
1151 (declare (type function fmf))
1152 (let* ((method-function nil)
1153 (calls (getf plist :call-list))
1154 (snl (getf plist :slot-name-lists))
1155 (pv-table (when (or calls snl)
1156 (intern-pv-table :call-list calls :slot-name-lists snl)))
1157 (arg-info (getf plist :arg-info))
1158 (nreq (car arg-info))
1159 (restp (cdr arg-info)))
1160 (setq method-function
1161 (lambda (method-args next-methods)
1162 (let* ((pv-cell (when pv-table
1163 (get-pv-cell method-args pv-table)))
1164 (nm (car next-methods))
1165 (nms (cdr next-methods))
1168 :function (if (std-instance-p nm)
1169 (method-function nm)
1171 :call-method-args (list nms)))))
1173 (let* ((rest (nthcdr nreq method-args))
1174 (args (ldiff method-args rest)))
1175 (apply fmf pv-cell nmc (nconc args (list rest))))
1176 (apply fmf pv-cell nmc method-args)))))
1177 ;; FIXME: this looks dangerous.
1178 (let* ((fname (%fun-name fmf)))
1179 (when (and fname (eq (car fname) 'fast-method))
1180 (set-fun-name method-function (cons 'slow-method (cdr fname)))))
1183 ;;; this is similar to the above, only not quite. Only called when
1184 ;;; the MOP is heavily involved. Not quite parallel to
1185 ;;; METHOD-FUNCTION-FROM-FAST-METHOD-FUNCTION, because we can close
1186 ;;; over the actual PV-CELL in this case.
1187 (defun method-function-from-fast-method-call (fmc)
1188 (let* ((fmf (fast-method-call-function fmc))
1189 (pv-cell (fast-method-call-pv-cell fmc))
1190 (arg-info (fast-method-call-arg-info fmc))
1191 (nreq (car arg-info))
1192 (restp (cdr arg-info)))
1193 (lambda (method-args next-methods)
1194 (let* ((nm (car next-methods))
1195 (nms (cdr next-methods))
1198 :function (if (std-instance-p nm)
1199 (method-function nm)
1201 :call-method-args (list nms)))))
1203 (let* ((rest (nthcdr nreq method-args))
1204 (args (ldiff method-args rest)))
1205 (apply fmf pv-cell nmc (nconc args (list rest))))
1206 (apply fmf pv-cell nmc method-args))))))
1208 (defun get-pv-cell (method-args pv-table)
1209 (let ((pv-wrappers (pv-wrappers-from-all-args pv-table method-args)))
1211 (pv-table-lookup pv-table pv-wrappers))))
1213 (defun pv-table-lookup-pv-args (pv-table &rest pv-parameters)
1214 (pv-table-lookup pv-table (pv-wrappers-from-pv-args pv-parameters)))
1216 (defun pv-wrappers-from-pv-args (&rest args)
1218 (dolist (arg args (if (cdr wrappers) (nreverse wrappers) (car wrappers)))
1219 (let ((wrapper (wrapper-of arg)))
1220 (push (if (invalid-wrapper-p wrapper)
1221 (check-wrapper-validity wrapper)
1225 (defun pv-wrappers-from-all-args (pv-table args)
1226 (loop for snl in (pv-table-slot-name-lists pv-table) and arg in args
1228 collect (wrapper-of arg) into wrappers
1229 finally (return (if (cdr wrappers) wrappers (car wrappers)))))
1231 ;;; Return the subset of WRAPPERS which is used in the cache
1233 (defun pv-wrappers-from-all-wrappers (pv-table wrappers)
1234 (loop for snl in (pv-table-slot-name-lists pv-table) and w in wrappers
1236 collect w into result
1237 finally (return (if (cdr result) result (car result)))))