1.0.8.24: factor (THE TYPE FORM) => FORM transformations into a function
[sbcl/simd.git] / src / pcl / vector.lisp
blob9e4dcd3926c09848b91a03c404fc242ae68083b0
1 ;;;; permutation vectors
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
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
10 ;;;; information.
12 ;;;; copyright information from original PCL sources:
13 ;;;;
14 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
15 ;;;; All rights reserved.
16 ;;;;
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
20 ;;;; control laws.
21 ;;;;
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
24 ;;;; specification.
26 (in-package "SB-PCL")
28 (defmacro instance-slot-index (wrapper slot-name)
29 `(let ((pos 0))
30 (declare (fixnum pos))
31 (block loop
32 (dolist (sn (wrapper-instance-slots-layout ,wrapper))
33 (when (eq ,slot-name sn) (return-from loop pos))
34 (incf pos)))))
36 (defstruct (pv-table (:predicate pv-tablep)
37 (:constructor make-pv-table-internal
38 (slot-name-lists call-list))
39 (:copier nil))
40 (cache nil :type (or cache null))
41 (pv-size 0 :type fixnum)
42 (slot-name-lists nil :type list)
43 (call-list nil :type list))
45 #-sb-fluid (declaim (sb-ext:freeze-type pv-table))
47 ;;; FIXME: The comment below seem to indicate that this was intended
48 ;;; to be actually used, however, it isn't anymore, and was commented
49 ;;; out at 0.9.13.47. Also removed was code in MAKE-PV-TABLE that
50 ;;; pushed each new PV-TABLE onto this list. --NS 2006-06-18
51 ;;;
52 ;;; help new slot-value-using-class methods affect fast iv access
53 ;;;
54 ;;; (defvar *all-pv-table-list* nil)
56 (declaim (inline make-pv-table))
57 (defun make-pv-table (&key slot-name-lists call-list)
58 (make-pv-table-internal slot-name-lists call-list))
60 (defun make-pv-table-type-declaration (var)
61 `(type pv-table ,var))
63 (defvar *slot-name-lists-inner* (make-hash-table :test 'equal))
64 (defvar *slot-name-lists-outer* (make-hash-table :test 'equal))
66 ;;; Entries in this are lists of (table . pv-offset-list).
67 (defvar *pv-key-to-pv-table-table* (make-hash-table :test 'equal))
69 (defun intern-pv-table (&key slot-name-lists call-list)
70 (let ((new-p nil))
71 (flet ((inner (x)
72 (or (gethash x *slot-name-lists-inner*)
73 (setf (gethash x *slot-name-lists-inner*) (copy-list x))))
74 (outer (x)
75 (or (gethash x *slot-name-lists-outer*)
76 (setf (gethash x *slot-name-lists-outer*)
77 (let ((snl (copy-list (cdr x)))
78 (cl (car x)))
79 (setq new-p t)
80 (make-pv-table :slot-name-lists snl
81 :call-list cl))))))
82 (let ((pv-table
83 (outer (mapcar #'inner (cons call-list slot-name-lists)))))
84 (when new-p
85 (let ((pv-index 0))
86 (dolist (slot-name-list slot-name-lists)
87 (dolist (slot-name (cdr slot-name-list))
88 (note-pv-table-reference slot-name pv-index pv-table)
89 (incf pv-index)))
90 (dolist (gf-call call-list)
91 (note-pv-table-reference gf-call pv-index pv-table)
92 (incf pv-index))
93 (setf (pv-table-pv-size pv-table) pv-index)))
94 pv-table))))
96 (defun note-pv-table-reference (ref pv-offset pv-table)
97 (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
98 (when (listp entry)
99 (let ((table-entry (assq pv-table entry)))
100 (when (and (null table-entry)
101 (> (length entry) 8))
102 (let ((new-table-table (make-hash-table :size 16 :test 'eq)))
103 (dolist (table-entry entry)
104 (setf (gethash (car table-entry) new-table-table)
105 (cdr table-entry)))
106 (setf (gethash ref *pv-key-to-pv-table-table*) new-table-table)))
107 (when (listp entry)
108 (if (null table-entry)
109 (let ((new (cons pv-table pv-offset)))
110 (if (consp entry)
111 (push new (cdr entry))
112 (setf (gethash ref *pv-key-to-pv-table-table*)
113 (list new))))
114 (push pv-offset (cdr table-entry)))
115 (return-from note-pv-table-reference nil))))
116 (let ((list (gethash pv-table entry)))
117 (if (consp list)
118 (push pv-offset (cdr list))
119 (setf (gethash pv-table entry) (list pv-offset)))))
120 nil)
122 (defun map-pv-table-references-of (ref function)
123 (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
124 (if (listp entry)
125 (dolist (table+pv-offset-list entry)
126 (funcall function
127 (car table+pv-offset-list)
128 (cdr table+pv-offset-list)))
129 (maphash function entry)))
130 ref)
132 (defun optimize-slot-value-by-class-p (class slot-name type)
133 (or (not (eq *boot-state* 'complete))
134 (let ((slotd (find-slot-definition class slot-name)))
135 (and slotd
136 (slot-accessor-std-p slotd type)))))
138 (defun compute-pv-slot (slot-name wrapper class class-slots)
139 (if (symbolp slot-name)
140 (when (optimize-slot-value-by-class-p class slot-name 'all)
141 (or (instance-slot-index wrapper slot-name)
142 (assq slot-name class-slots)))
143 (when (consp slot-name)
144 (case (first slot-name)
145 ((reader writer)
146 (when (eq *boot-state* 'complete)
147 (let ((gf (gdefinition (second slot-name))))
148 (when (generic-function-p gf)
149 (accessor-values1 gf (first slot-name) class)))))
150 (t (bug "Don't know how to deal with ~S in ~S"
151 slot-name 'compute-pv-slots))))))
153 (defun compute-pv (slot-name-lists wrappers)
154 (unless (listp wrappers)
155 (setq wrappers (list wrappers)))
156 (let (elements)
157 (dolist (slot-names slot-name-lists
158 (make-permutation-vector (nreverse elements)))
159 (when slot-names
160 (let* ((wrapper (pop wrappers))
161 (std-p (typep wrapper 'wrapper))
162 (class (wrapper-class* wrapper))
163 (class-slots (and std-p (wrapper-class-slots wrapper))))
164 (dolist (slot-name (cdr slot-names))
165 (push (if std-p
166 (compute-pv-slot slot-name wrapper class class-slots)
167 nil)
168 elements)))))))
170 (defun compute-calls (call-list wrappers)
171 (declare (ignore call-list wrappers))
173 (map 'vector
174 (lambda (call)
175 (compute-emf-from-wrappers call wrappers))
176 call-list)
178 '#())
180 #|| ; Need to finish this, then write the maintenance functions.
181 (defun compute-emf-from-wrappers (call wrappers)
182 (when call
183 (destructuring-bind (gf-name nreq restp arg-info) call
184 (if (eq gf-name 'make-instance)
185 (error "should not get here") ; there is another mechanism for this.
186 (lambda (&rest args)
187 (if (not (eq *boot-state* 'complete))
188 (apply (gdefinition gf-name) args)
189 (let* ((gf (gdefinition gf-name))
190 (arg-info (arg-info-reader gf))
191 (classes '?)
192 (types '?)
193 (emf (cache-miss-values-internal gf arg-info
194 wrappers classes types
195 'caching)))
196 (update-all-pv-tables call wrappers emf)
197 (invoke-emf emf args))))))))
200 (defun make-permutation-vector (indexes)
201 (make-array (length indexes) :initial-contents indexes))
203 (defun pv-table-lookup (pv-table pv-wrappers)
204 (let* ((slot-name-lists (pv-table-slot-name-lists pv-table))
205 (call-list (pv-table-call-list pv-table))
206 (cache (or (pv-table-cache pv-table)
207 (setf (pv-table-cache pv-table)
208 (make-cache :key-count (- (length slot-name-lists)
209 (count nil slot-name-lists))
210 :value t
211 :size 2)))))
212 (multiple-value-bind (hitp value) (probe-cache cache pv-wrappers)
213 (if hitp
214 value
215 (let* ((pv (compute-pv slot-name-lists pv-wrappers))
216 (calls (compute-calls call-list pv-wrappers))
217 (pv-cell (cons pv calls))
218 (new-cache (fill-cache cache pv-wrappers pv-cell)))
219 ;; This is safe: if another thread races us here the loser just
220 ;; misses the next time as well.
221 (unless (eq new-cache cache)
222 (setf (pv-table-cache pv-table) new-cache))
223 pv-cell)))))
225 (defun make-pv-type-declaration (var)
226 `(type simple-vector ,var))
228 (defmacro copy-pv (pv)
229 `(copy-seq ,pv))
231 (defun make-calls-type-declaration (var)
232 `(type simple-vector ,var))
234 (defmacro callsref (calls index)
235 `(svref ,calls ,index))
237 (defvar *pv-table-cache-update-info* nil)
239 (defun update-pv-table-cache-info (class)
240 (let ((slot-names-for-pv-table-update nil)
241 (new-icui nil))
242 (dolist (icu *pv-table-cache-update-info*)
243 (if (eq (car icu) class)
244 (pushnew (cdr icu) slot-names-for-pv-table-update)
245 (push icu new-icui)))
246 (setq *pv-table-cache-update-info* new-icui)
247 (when slot-names-for-pv-table-update
248 (update-all-pv-table-caches class slot-names-for-pv-table-update))))
250 (defun update-all-pv-table-caches (class slot-names)
251 (let* ((cwrapper (class-wrapper class))
252 (std-p (typep cwrapper 'wrapper))
253 (class-slots (and std-p (wrapper-class-slots cwrapper)))
254 (new-values
255 (mapcar
256 (lambda (slot-name)
257 (cons slot-name
258 (if std-p
259 (compute-pv-slot slot-name cwrapper class class-slots)
260 nil)))
261 slot-names))
262 (pv-tables nil))
263 (dolist (slot-name slot-names)
264 (map-pv-table-references-of
265 slot-name
266 (lambda (pv-table pv-offset-list)
267 (declare (ignore pv-offset-list))
268 (pushnew pv-table pv-tables))))
269 (dolist (pv-table pv-tables)
270 (let* ((cache (pv-table-cache pv-table))
271 (slot-name-lists (pv-table-slot-name-lists pv-table))
272 (pv-size (pv-table-pv-size pv-table))
273 (pv-map (make-array pv-size :initial-element nil)))
274 (let ((map-index 0) (param-index 0))
275 (dolist (slot-name-list slot-name-lists)
276 (dolist (slot-name (cdr slot-name-list))
277 (let ((a (assoc slot-name new-values)))
278 (setf (svref pv-map map-index)
279 (and a (cons param-index (cdr a)))))
280 (incf map-index))
281 (incf param-index)))
282 (when cache
283 (map-cache (lambda (wrappers pv-cell)
284 (update-slots-in-pv wrappers (car pv-cell)
285 cwrapper pv-size pv-map))
286 cache))))))
288 (defun update-slots-in-pv (wrappers pv cwrapper pv-size pv-map)
289 (if (atom wrappers)
290 (when (eq cwrapper wrappers)
291 (dotimes-fixnum (i pv-size)
292 (let ((map (svref pv-map i)))
293 (when map
294 (aver (= (car map) 0))
295 (setf (svref pv i) (cdr map))))))
296 (when (memq cwrapper wrappers)
297 (let ((param 0))
298 (dolist (wrapper wrappers)
299 (when (eq wrapper cwrapper)
300 (dotimes-fixnum (i pv-size)
301 (let ((map (svref pv-map i)))
302 (when (and map (= (car map) param))
303 (setf (svref pv i) (cdr map))))))
304 (incf param))))))
306 (defun can-optimize-access (form required-parameters env)
307 (let ((type (ecase (car form)
308 (slot-value 'reader)
309 (set-slot-value 'writer)
310 (slot-boundp 'boundp)))
311 (var (extract-the (cadr form)))
312 (slot-name (eval (caddr form)))) ; known to be constant
313 (when (symbolp var)
314 (let* ((rebound? (caddr (var-declaration '%variable-rebinding var env)))
315 (parameter-or-nil (car (memq (or rebound? var)
316 required-parameters))))
317 (when parameter-or-nil
318 (let* ((class-name (caddr (var-declaration '%class
319 parameter-or-nil
320 env)))
321 (class (find-class class-name nil)))
322 (when (or (not (eq *boot-state* 'complete))
323 (and class (not (class-finalized-p class))))
324 (setq class nil))
325 (when (and class-name (not (eq class-name t)))
326 (when (or (null type)
327 (not (and class
328 (memq *the-class-structure-object*
329 (class-precedence-list class))))
330 (optimize-slot-value-by-class-p class slot-name type))
331 (cons parameter-or-nil (or class class-name))))))))))
333 ;;; Check whether the binding of the named variable is modified in the
334 ;;; method body.
335 (defun parameter-modified-p (parameter-name env)
336 (let ((modified-variables (macroexpand '%parameter-binding-modified env)))
337 (memq parameter-name modified-variables)))
339 (defun optimize-slot-value (slots sparameter form)
340 (if sparameter
341 (let ((optimized-form
342 (destructuring-bind (ignore1 ignore2 slot-name-form) form
343 (declare (ignore ignore1 ignore2))
344 (let ((slot-name (eval slot-name-form)))
345 (optimize-instance-access slots :read sparameter
346 slot-name nil)))))
347 ;; We don't return the optimized form directly, since there's
348 ;; still a chance that we'll find out later on that the
349 ;; optimization should not have been done, for example due to
350 ;; the walker encountering a SETQ on SPARAMETER later on in
351 ;; the body [ see for example clos.impure.lisp test with :name
352 ;; ((:setq :method-parameter) slot-value)) ]. Instead we defer
353 ;; the decision until the compiler macroexpands
354 ;; OPTIMIZED-SLOT-VALUE.
356 ;; Note that we must still call OPTIMIZE-INSTANCE-ACCESS at
357 ;; this point (instead of when expanding
358 ;; OPTIMIZED-SLOT-VALUE), since it mutates the structure of
359 ;; SLOTS. If that mutation isn't done during the walking,
360 ;; MAKE-METHOD-LAMBDA-INTERNAL won't wrap a correct PV-BINDING
361 ;; form around the body, and compilation will fail. -- JES,
362 ;; 2006-09-18
363 `(optimized-slot-value ,form ,(car sparameter) ,optimized-form))
364 `(accessor-slot-value ,@(cdr form))))
366 (defmacro optimized-slot-value (form parameter-name optimized-form
367 &environment env)
368 ;; Either use OPTIMIZED-FORM or fall back to the safe
369 ;; ACCESSOR-SLOT-VALUE.
370 (if (parameter-modified-p parameter-name env)
371 `(accessor-slot-value ,@(cdr form))
372 optimized-form))
374 (defun optimize-set-slot-value (slots sparameter form)
375 (if sparameter
376 (let ((optimized-form
377 (destructuring-bind (ignore1 ignore2 slot-name-form new-value) form
378 (declare (ignore ignore1 ignore2))
379 (let ((slot-name (eval slot-name-form)))
380 (optimize-instance-access slots
381 :write
382 sparameter
383 slot-name
384 new-value)))))
385 ;; See OPTIMIZE-SLOT-VALUE
386 `(optimized-set-slot-value ,form ,(car sparameter) ,optimized-form))
387 `(accessor-set-slot-value ,@(cdr form))))
389 (defmacro optimized-set-slot-value (form parameter-name optimized-form
390 &environment env)
391 (cond ((safe-code-p env)
392 ;; Don't optimize slot value setting in safe code, since the
393 ;; optimized version will fail to catch some type errors
394 ;; (for example when a subclass declares a tighter type for
395 ;; the slot than a superclass).
396 `(safe-set-slot-value ,@(cdr form)))
397 ((parameter-modified-p parameter-name env)
398 `(accessor-set-slot-value ,@(cdr form)))
400 optimized-form)))
402 (defun optimize-slot-boundp (slots sparameter form)
403 (if sparameter
404 (let ((optimized-form
405 (destructuring-bind
406 ;; FIXME: In CMU CL ca. 19991205, this binding list
407 ;; had a fourth element in it, NEW-VALUE. It's hard
408 ;; to see how that could possibly be right, since
409 ;; SLOT-BOUNDP has no NEW-VALUE. Since it was
410 ;; causing a failure in building PCL for SBCL, so I
411 ;; changed it to match the definition of
412 ;; SLOT-BOUNDP (and also to match the list used in
413 ;; the similar OPTIMIZE-SLOT-VALUE,
414 ;; above). However, I'm weirded out by this, since
415 ;; this is old code which has worked for ages to
416 ;; build PCL for CMU CL, so it's hard to see why it
417 ;; should need a patch like this in order to build
418 ;; PCL for SBCL. I'd like to return to this and
419 ;; find a test case which exercises this function
420 ;; both in CMU CL, to see whether it's really a
421 ;; previously-unexercised bug or whether I've
422 ;; misunderstood something (and, presumably,
423 ;; patched it wrong).
424 (slot-boundp-symbol instance slot-name-form)
425 form
426 (declare (ignore slot-boundp-symbol instance))
427 (let ((slot-name (eval slot-name-form)))
428 (optimize-instance-access slots
429 :boundp
430 sparameter
431 slot-name
432 nil)))))
433 ;; See OPTIMIZE-SLOT-VALUE
434 `(optimized-slot-boundp ,form ,(car sparameter) ,optimized-form))
435 `(accessor-slot-boundp ,@(cdr form))))
437 (defmacro optimized-slot-boundp (form parameter-name optimized-form
438 &environment env)
439 (if (parameter-modified-p parameter-name env)
440 `(accessor-slot-boundp ,@(cdr form))
441 optimized-form))
443 ;;; The SLOTS argument is an alist, the CAR of each entry is the name
444 ;;; of a required parameter to the function. The alist is in order, so
445 ;;; the position of an entry in the alist corresponds to the
446 ;;; argument's position in the lambda list.
447 (defun optimize-instance-access (slots
448 read/write
449 sparameter
450 slot-name
451 new-value)
452 (let ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
453 (parameter (if (consp sparameter) (car sparameter) sparameter)))
454 (if (and (eq *boot-state* 'complete)
455 (classp class)
456 (memq *the-class-structure-object* (class-precedence-list class)))
457 (let ((slotd (find-slot-definition class slot-name)))
458 (ecase read/write
459 (:read
460 `(,(slot-definition-defstruct-accessor-symbol slotd) ,parameter))
461 (:write
462 `(setf (,(slot-definition-defstruct-accessor-symbol slotd)
463 ,parameter)
464 ,new-value))
465 (:boundp
466 t)))
467 (let* ((parameter-entry (assq parameter slots))
468 (slot-entry (assq slot-name (cdr parameter-entry)))
469 (position (posq parameter-entry slots))
470 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
471 (unless parameter-entry
472 (bug "slot optimization bewilderment: O-I-A"))
473 (unless slot-entry
474 (setq slot-entry (list slot-name))
475 (push slot-entry (cdr parameter-entry)))
476 (push pv-offset-form (cdr slot-entry))
477 (ecase read/write
478 (:read
479 `(instance-read ,pv-offset-form ,parameter ,position
480 ',slot-name ',class))
481 (:write
482 `(let ((.new-value. ,new-value))
483 (instance-write ,pv-offset-form ,parameter ,position
484 ',slot-name ',class .new-value.)))
485 (:boundp
486 `(instance-boundp ,pv-offset-form ,parameter ,position
487 ',slot-name ',class)))))))
489 (define-walker-template pv-offset) ; These forms get munged by mutate slots.
490 (defmacro pv-offset (arg) arg)
491 (define-walker-template instance-accessor-parameter)
492 (defmacro instance-accessor-parameter (x) x)
494 ;;; It is safe for these two functions to be wrong. They just try to
495 ;;; guess what the most likely case will be.
496 (defun generate-fast-class-slot-access-p (class-form slot-name-form)
497 (let ((class (and (constantp class-form) (constant-form-value class-form)))
498 (slot-name (and (constantp slot-name-form)
499 (constant-form-value slot-name-form))))
500 (and (eq *boot-state* 'complete)
501 (standard-class-p class)
502 (not (eq class *the-class-t*)) ; shouldn't happen, though.
503 (let ((slotd (find-slot-definition class slot-name)))
504 (and slotd (eq :class (slot-definition-allocation slotd)))))))
506 (defun skip-fast-slot-access-p (class-form slot-name-form type)
507 (let ((class (and (constantp class-form) (constant-form-value class-form)))
508 (slot-name (and (constantp slot-name-form)
509 (constant-form-value slot-name-form))))
510 (and (eq *boot-state* 'complete)
511 (standard-class-p class)
512 (not (eq class *the-class-t*)) ; shouldn't happen, though.
513 (let ((slotd (find-slot-definition class slot-name)))
514 (and slotd (skip-optimize-slot-value-by-class-p class
515 slot-name
516 type))))))
518 (defun skip-optimize-slot-value-by-class-p (class slot-name type)
519 (let ((slotd (find-slot-definition class slot-name)))
520 (and slotd
521 (eq *boot-state* 'complete)
522 (not (slot-accessor-std-p slotd type)))))
524 (defmacro instance-read-internal (pv slots pv-offset default &optional kind)
525 (unless (member kind '(nil :instance :class :default))
526 (error "illegal kind argument to ~S: ~S" 'instance-read-internal kind))
527 (if (eq kind :default)
528 default
529 (let* ((index (gensym))
530 (value index))
531 `(locally (declare #.*optimize-speed*)
532 (let ((,index (svref ,pv ,pv-offset)))
533 (setq ,value (typecase ,index
534 ;; FIXME: the line marked by KLUDGE below
535 ;; (and the analogous spot in
536 ;; INSTANCE-WRITE-INTERNAL) is there purely
537 ;; to suppress a type mismatch warning that
538 ;; propagates through to user code.
539 ;; Presumably SLOTS at this point can never
540 ;; actually be NIL, but the compiler seems
541 ;; to think it could, so we put this here
542 ;; to shut it up. (see also mail Rudi
543 ;; Schlatte sbcl-devel 2003-09-21) -- CSR,
544 ;; 2003-11-30
545 ,@(when (or (null kind) (eq kind :instance))
546 `((fixnum
547 (and ,slots ; KLUDGE
548 (clos-slots-ref ,slots ,index)))))
549 ,@(when (or (null kind) (eq kind :class))
550 `((cons (cdr ,index))))
551 (t +slot-unbound+)))
552 (if (eq ,value +slot-unbound+)
553 ,default
554 ,value))))))
556 (defmacro instance-read (pv-offset parameter position slot-name class)
557 (if (skip-fast-slot-access-p class slot-name 'reader)
558 `(accessor-slot-value ,parameter ,slot-name)
559 `(instance-read-internal .pv. ,(slot-vector-symbol position)
560 ,pv-offset (accessor-slot-value ,parameter ,slot-name)
561 ,(if (generate-fast-class-slot-access-p class slot-name)
562 :class :instance))))
564 (defmacro instance-write-internal (pv slots pv-offset new-value default
565 &optional kind)
566 (unless (member kind '(nil :instance :class :default))
567 (error "illegal kind argument to ~S: ~S" 'instance-write-internal kind))
568 (if (eq kind :default)
569 default
570 (let* ((index (gensym)))
571 `(locally (declare #.*optimize-speed*)
572 (let ((,index (svref ,pv ,pv-offset)))
573 (typecase ,index
574 ,@(when (or (null kind) (eq kind :instance))
575 `((fixnum (and ,slots
576 (setf (clos-slots-ref ,slots ,index)
577 ,new-value)))))
578 ,@(when (or (null kind) (eq kind :class))
579 `((cons (setf (cdr ,index) ,new-value))))
580 (t ,default)))))))
582 (defmacro instance-write (pv-offset
583 parameter
584 position
585 slot-name
586 class
587 new-value)
588 (if (skip-fast-slot-access-p class slot-name 'writer)
589 `(accessor-set-slot-value ,parameter ,slot-name ,new-value)
590 `(instance-write-internal .pv. ,(slot-vector-symbol position)
591 ,pv-offset ,new-value
592 (accessor-set-slot-value ,parameter ,slot-name ,new-value)
593 ,(if (generate-fast-class-slot-access-p class slot-name)
594 :class :instance))))
596 (defmacro instance-boundp-internal (pv slots pv-offset default
597 &optional kind)
598 (unless (member kind '(nil :instance :class :default))
599 (error "illegal kind argument to ~S: ~S" 'instance-boundp-internal kind))
600 (if (eq kind :default)
601 default
602 (let* ((index (gensym)))
603 `(locally (declare #.*optimize-speed*)
604 (let ((,index (svref ,pv ,pv-offset)))
605 (typecase ,index
606 ,@(when (or (null kind) (eq kind :instance))
607 `((fixnum (not (and ,slots
608 (eq (clos-slots-ref ,slots ,index)
609 +slot-unbound+))))))
610 ,@(when (or (null kind) (eq kind :class))
611 `((cons (not (eq (cdr ,index) +slot-unbound+)))))
612 (t ,default)))))))
614 (defmacro instance-boundp (pv-offset parameter position slot-name class)
615 (if (skip-fast-slot-access-p class slot-name 'boundp)
616 `(accessor-slot-boundp ,parameter ,slot-name)
617 `(instance-boundp-internal .pv. ,(slot-vector-symbol position)
618 ,pv-offset (accessor-slot-boundp ,parameter ,slot-name)
619 ,(if (generate-fast-class-slot-access-p class slot-name)
620 :class :instance))))
622 ;;; This magic function has quite a job to do indeed.
624 ;;; The careful reader will recall that <slots> contains all of the
625 ;;; optimized slot access forms produced by OPTIMIZE-INSTANCE-ACCESS.
626 ;;; Each of these is a call to either INSTANCE-READ or INSTANCE-WRITE.
628 ;;; At the time these calls were produced, the first argument was
629 ;;; specified as the symbol .PV-OFFSET.; what we have to do now is
630 ;;; convert those pv-offset arguments into the actual number that is
631 ;;; the correct offset into the pv.
633 ;;; But first, oh but first, we sort <slots> a bit so that for each
634 ;;; argument we have the slots in alphabetical order. This
635 ;;; canonicalizes the PV-TABLE's a bit and will hopefully lead to
636 ;;; having fewer PV's floating around. Even if the gain is only
637 ;;; modest, it costs nothing.
638 (defun slot-name-lists-from-slots (slots calls)
639 (multiple-value-bind (slots calls) (mutate-slots-and-calls slots calls)
640 (let* ((slot-name-lists
641 (mapcar (lambda (parameter-entry)
642 (cons nil (mapcar #'car (cdr parameter-entry))))
643 slots))
644 (call-list
645 (mapcar #'car calls)))
646 (dolist (call call-list)
647 (dolist (arg (cdr call))
648 (when (integerp arg)
649 (setf (car (nth arg slot-name-lists)) t))))
650 (setq slot-name-lists (mapcar (lambda (r+snl)
651 (when (or (car r+snl) (cdr r+snl))
652 r+snl))
653 slot-name-lists))
654 (let ((cvt (apply #'vector
655 (let ((i -1))
656 (mapcar (lambda (r+snl)
657 (when r+snl (incf i)))
658 slot-name-lists)))))
659 (setq call-list (mapcar (lambda (call)
660 (cons (car call)
661 (mapcar (lambda (arg)
662 (if (integerp arg)
663 (svref cvt arg)
664 arg))
665 (cdr call))))
666 call-list)))
667 (values slot-name-lists call-list))))
669 (defun mutate-slots-and-calls (slots calls)
670 (let ((sorted-slots (sort-slots slots))
671 (sorted-calls (sort-calls (cdr calls)))
672 (pv-offset -1))
673 (dolist (parameter-entry sorted-slots)
674 (dolist (slot-entry (cdr parameter-entry))
675 (incf pv-offset)
676 (dolist (form (cdr slot-entry))
677 (setf (cadr form) pv-offset))))
678 (dolist (call-entry sorted-calls)
679 (incf pv-offset)
680 (dolist (form (cdr call-entry))
681 (setf (cadr form) pv-offset)))
682 (values sorted-slots sorted-calls)))
684 (defun symbol-pkg-name (sym)
685 (let ((pkg (symbol-package sym)))
686 (if pkg (package-name pkg) "")))
688 ;;; FIXME: Because of the existence of UNINTERN and RENAME-PACKAGE,
689 ;;; the part of this ordering which is based on SYMBOL-PKG-NAME is not
690 ;;; stable. This ordering is only used in to
691 ;;; SLOT-NAME-LISTS-FROM-SLOTS, where it serves to "canonicalize the
692 ;;; PV-TABLE's a bit and will hopefully lead to having fewer PV's
693 ;;; floating around", so it sounds as though the instability won't
694 ;;; actually lead to bugs, just small inefficiency. But still, it
695 ;;; would be better to reimplement this function as a comparison based
696 ;;; on SYMBOL-HASH:
697 ;;; * stable comparison
698 ;;; * smaller code (here, and in being able to discard SYMBOL-PKG-NAME)
699 ;;; * faster code.
700 (defun symbol-lessp (a b)
701 (if (eq (symbol-package a)
702 (symbol-package b))
703 (string-lessp (symbol-name a)
704 (symbol-name b))
705 (string-lessp (symbol-pkg-name a)
706 (symbol-pkg-name b))))
708 (defun symbol-or-cons-lessp (a b)
709 (etypecase a
710 (symbol (etypecase b
711 (symbol (symbol-lessp a b))
712 (cons t)))
713 (cons (etypecase b
714 (symbol nil)
715 (cons (if (eq (car a) (car b))
716 (symbol-or-cons-lessp (cdr a) (cdr b))
717 (symbol-or-cons-lessp (car a) (car b))))))))
719 (defun sort-slots (slots)
720 (mapcar (lambda (parameter-entry)
721 (cons (car parameter-entry)
722 (sort (cdr parameter-entry) ;slot entries
723 #'symbol-or-cons-lessp
724 :key #'car)))
725 slots))
727 (defun sort-calls (calls)
728 (sort calls #'symbol-or-cons-lessp :key #'car))
730 ;;;; This needs to work in terms of metatypes and also needs to work
731 ;;;; for automatically generated reader and writer functions.
732 ;;;; Automatically generated reader and writer functions use this
733 ;;;; stuff too.
735 (defmacro pv-binding ((required-parameters slot-name-lists pv-table-form)
736 &body body)
737 (let (slot-vars pv-parameters)
738 (loop for slots in slot-name-lists
739 for required-parameter in required-parameters
740 for i from 0
741 do (when slots
742 (push required-parameter pv-parameters)
743 (push (slot-vector-symbol i) slot-vars)))
744 `(pv-binding1 (.pv. .calls. ,pv-table-form
745 ,(nreverse pv-parameters) ,(nreverse slot-vars))
746 ,@body)))
748 (defmacro pv-binding1 ((pv calls pv-table-form pv-parameters slot-vars)
749 &body body)
750 `(pv-env (,pv ,calls ,pv-table-form ,pv-parameters)
751 (let (,@(mapcar (lambda (slot-var p) `(,slot-var (get-slots-or-nil ,p)))
752 slot-vars pv-parameters))
753 (declare (ignorable ,@(mapcar #'identity slot-vars)))
754 ,@body)))
756 ;;; This will only be visible in PV-ENV when the default MAKE-METHOD-LAMBDA is
757 ;;; overridden.
758 (define-symbol-macro pv-env-environment overridden)
760 (defmacro pv-env (&environment env
761 (pv calls pv-table-form pv-parameters)
762 &rest forms)
763 ;; Decide which expansion to use based on the state of the PV-ENV-ENVIRONMENT
764 ;; symbol-macrolet.
765 (if (eq (macroexpand 'pv-env-environment env) 'default)
766 `(let ((,pv (car .pv-cell.))
767 (,calls (cdr .pv-cell.)))
768 (declare ,(make-pv-type-declaration pv)
769 ,(make-calls-type-declaration calls))
770 ,pv ,calls
771 ,@forms)
772 `(let* ((.pv-table. ,pv-table-form)
773 (.pv-cell. (pv-table-lookup-pv-args .pv-table. ,@pv-parameters))
774 (,pv (car .pv-cell.))
775 (,calls (cdr .pv-cell.)))
776 (declare ,(make-pv-type-declaration pv))
777 (declare ,(make-calls-type-declaration calls))
778 ,pv ,calls
779 ,@forms)))
781 (defvar *non-var-declarations*
782 ;; FIXME: VALUES was in this list, conditionalized with #+CMU, but I
783 ;; don't *think* CMU CL had, or SBCL has, VALUES declarations. If
784 ;; SBCL doesn't have 'em, VALUES should probably be removed from
785 ;; this list.
786 '(values
787 %method-name
788 %method-lambda-list
789 optimize
790 ftype
791 muffle-conditions
792 inline
793 notinline))
795 (defvar *var-declarations-with-arg*
796 '(%class
797 type))
799 (defvar *var-declarations-without-arg*
800 '(ignore
801 ignorable special dynamic-extent
802 ;; FIXME: Possibly this entire list and variable could go away.
803 ;; If not, certainly we should remove all these built-in typenames
804 ;; from the list, and replace them with a test for "is it a type
805 ;; name?" (CLTL1 allowed only built-in type names as declarations,
806 ;; but ANSI CL allows any type name as a declaration.)
807 array atom base-char bignum bit bit-vector character compiled-function
808 complex cons double-float extended-char
809 fixnum float function hash-table integer
810 keyword list long-float nil null number package pathname random-state ratio
811 rational readtable sequence short-float signed-byte simple-array
812 simple-bit-vector simple-string simple-vector single-float standard-char
813 stream string symbol t unsigned-byte vector))
815 (defun split-declarations (body args maybe-reads-params-p)
816 (let ((inner-decls nil)
817 (outer-decls nil)
818 decl)
819 (loop (when (null body) (return nil))
820 (setq decl (car body))
821 (unless (and (consp decl)
822 (eq (car decl) 'declare))
823 (return nil))
824 (dolist (form (cdr decl))
825 (when (consp form)
826 (let ((declaration-name (car form)))
827 (if (member declaration-name *non-var-declarations*)
828 (push `(declare ,form) outer-decls)
829 (let ((arg-p
830 (member declaration-name
831 *var-declarations-with-arg*))
832 (non-arg-p
833 (member declaration-name
834 *var-declarations-without-arg*))
835 (dname (list (pop form)))
836 (inners nil) (outers nil))
837 (unless (or arg-p non-arg-p)
838 ;; FIXME: This warning, and perhaps the
839 ;; various *VAR-DECLARATIONS-FOO* and/or
840 ;; *NON-VAR-DECLARATIONS* variables,
841 ;; could probably go away now that we're not
842 ;; trying to be portable between different
843 ;; CLTL1 hosts the way PCL was. (Note that to
844 ;; do this right, we need to be able to handle
845 ;; user-defined (DECLAIM (DECLARATION FOO))
846 ;; stuff.)
847 (warn "The declaration ~S is not understood by ~S.~@
848 Please put ~S on one of the lists ~S,~%~S, or~%~S.~@
849 (Assuming it is a variable declaration without argument)."
850 declaration-name 'split-declarations
851 declaration-name
852 '*non-var-declarations*
853 '*var-declarations-with-arg*
854 '*var-declarations-without-arg*)
855 (push declaration-name *var-declarations-without-arg*))
856 (when arg-p
857 (setq dname (append dname (list (pop form)))))
858 (case (car dname)
859 (%class (push `(declare (,@dname ,@form)) inner-decls))
861 (dolist (var form)
862 (if (member var args)
863 ;; Quietly remove IGNORE declarations
864 ;; on args when a next-method is
865 ;; involved, to prevent compiler
866 ;; warnings about ignored args being
867 ;; read.
868 (unless (and maybe-reads-params-p
869 (eq (car dname) 'ignore))
870 (push var outers))
871 (push var inners)))
872 (when outers
873 (push `(declare (,@dname ,@outers)) outer-decls))
874 (when inners
875 (push
876 `(declare (,@dname ,@inners))
877 inner-decls)))))))))
878 (setq body (cdr body)))
879 (values outer-decls inner-decls body)))
881 ;;; Pull a name out of the %METHOD-NAME declaration in the function
882 ;;; body given, or return NIL if no %METHOD-NAME declaration is found.
883 (defun body-method-name (body)
884 (multiple-value-bind (real-body declarations documentation)
885 (parse-body body)
886 (declare (ignore real-body documentation))
887 (let ((name-decl (get-declaration '%method-name declarations)))
888 (and name-decl
889 (destructuring-bind (name) name-decl
890 name)))))
892 ;;; Convert a lambda expression containing a SB-PCL::%METHOD-NAME
893 ;;; declaration (which is a naming style internal to PCL) into an
894 ;;; SB-INT:NAMED-LAMBDA expression (which is a naming style used
895 ;;; throughout SBCL, understood by the main compiler); or if there's
896 ;;; no SB-PCL::%METHOD-NAME declaration, then just return the original
897 ;;; lambda expression.
898 (defun name-method-lambda (method-lambda)
899 (let ((method-name (body-method-name (cddr method-lambda))))
900 (if method-name
901 `(named-lambda (slow-method ,method-name) ,(rest method-lambda))
902 method-lambda)))
904 (defun make-method-initargs-form-internal (method-lambda initargs env)
905 (declare (ignore env))
906 (let (method-lambda-args
907 lmf ; becomes body of function
908 lmf-params)
909 (if (not (and (= 3 (length method-lambda))
910 (= 2 (length (setq method-lambda-args (cadr method-lambda))))
911 (consp (setq lmf (third method-lambda)))
912 (eq 'simple-lexical-method-functions (car lmf))
913 (eq (car method-lambda-args)
914 (cadr (setq lmf-params (cadr lmf))))
915 (eq (cadr method-lambda-args)
916 (caddr lmf-params))))
917 `(list* :function ,(name-method-lambda method-lambda)
918 ',initargs)
919 (let* ((lambda-list (car lmf-params))
920 (nreq 0)
921 (restp nil)
922 (args nil))
923 (dolist (arg lambda-list)
924 (when (member arg '(&optional &rest &key))
925 (setq restp t)
926 (return nil))
927 (when (eq arg '&aux)
928 (return nil))
929 (incf nreq)
930 (push arg args))
931 (setq args (nreverse args))
932 (setf (getf (getf initargs 'plist) :arg-info) (cons nreq restp))
933 (make-method-initargs-form-internal1
934 initargs (cddr lmf) args lmf-params restp)))))
936 (defun lambda-list-parameter-names (lambda-list)
937 ;; Given a valid lambda list, extract the parameter names.
938 (loop for x in lambda-list
939 with res = nil
940 do (unless (member x lambda-list-keywords)
941 (if (consp x)
942 (let ((name (car x)))
943 (if (consp name)
944 ;; ... ((:BAR FOO) 1)
945 (push (second name) res)
946 ;; ... (FOO 1)
947 (push name res))
948 ;; ... (... 1 FOO-P)
949 (let ((name-p (cddr x)))
950 (when name-p
951 (push (car name-p) res))))
952 ;; ... FOO
953 (push x res)))
954 finally (return res)))
956 (defun make-method-initargs-form-internal1
957 (initargs body req-args lmf-params restp)
958 (let* (;; The lambda-list of the method, minus specifiers
959 (lambda-list (car lmf-params))
960 ;; Names of the parameters that will be in the outermost lambda-list
961 ;; (and whose bound declarations thus need to be in OUTER-DECLS).
962 (outer-parameters req-args)
963 ;; The lambda-list used by BIND-ARGS
964 (bind-list lambda-list)
965 (setq-p (getf (cdr lmf-params) :setq-p))
966 (auxp (member '&aux bind-list))
967 (call-next-method-p (getf (cdr lmf-params) :call-next-method-p)))
968 ;; Try to use the normal function call machinery instead of BIND-ARGS
969 ;; binding the arguments, unless:
970 (unless (or ;; If all arguments are required, BIND-ARGS will be a no-op
971 ;; in any case.
972 (and (not restp) (not auxp))
973 ;; CALL-NEXT-METHOD wants to use BIND-ARGS, and needs a
974 ;; list of all non-required arguments.
975 call-next-method-p)
976 (setf ;; We don't want a binding for .REST-ARG.
977 restp nil
978 ;; Get all the parameters for declaration parsing
979 outer-parameters (lambda-list-parameter-names lambda-list)
980 ;; Ensure that BIND-ARGS won't do anything (since
981 ;; BIND-LIST won't contain any non-required parameters,
982 ;; and REQ-ARGS will be of an equal length). We still want
983 ;; to pass BIND-LIST to FAST-LEXICAL-METHOD-FUNCTIONS so
984 ;; that BIND-FAST-LEXICAL-METHOD-FUNCTIONS can take care
985 ;; of rebinding SETQd required arguments around the method
986 ;; body.
987 bind-list req-args))
988 (multiple-value-bind (outer-decls inner-decls body-sans-decls)
989 (split-declarations
990 body outer-parameters (or call-next-method-p setq-p))
991 (let* ((rest-arg (when restp
992 '.rest-arg.))
993 (fmf-lambda-list (if rest-arg
994 (append req-args (list '&rest rest-arg))
995 (if call-next-method-p
996 req-args
997 lambda-list))))
998 `(list*
999 :function
1000 (let* ((fmf (,(if (body-method-name body) 'named-lambda 'lambda)
1001 ,@(when (body-method-name body)
1002 ;; function name
1003 (list (cons 'fast-method (body-method-name body))))
1004 ;; The lambda-list of the FMF
1005 (.pv-cell. .next-method-call. ,@fmf-lambda-list)
1006 ;; body of the function
1007 (declare (ignorable .pv-cell. .next-method-call.)
1008 (disable-package-locks pv-env-environment))
1009 ,@outer-decls
1010 (symbol-macrolet ((pv-env-environment default))
1011 (fast-lexical-method-functions
1012 (,bind-list .next-method-call. ,req-args ,rest-arg
1013 ,@(cdddr lmf-params))
1014 ,@inner-decls
1015 ,@body-sans-decls))))
1016 (mf (%make-method-function fmf nil)))
1017 (set-funcallable-instance-function
1018 mf (method-function-from-fast-function fmf ',(getf initargs 'plist)))
1020 ',initargs)))))
1022 ;;; Use arrays and hash tables and the fngen stuff to make this much
1023 ;;; better. It doesn't really matter, though, because a function
1024 ;;; returned by this will get called only when the user explicitly
1025 ;;; funcalls a result of method-function. BUT, this is needed to make
1026 ;;; early methods work.
1027 (defun method-function-from-fast-function (fmf plist)
1028 (declare (type function fmf))
1029 (let* ((method-function nil)
1030 (calls (getf plist :call-list))
1031 (snl (getf plist :slot-name-lists))
1032 (pv-table (when (or calls snl)
1033 (intern-pv-table :call-list calls :slot-name-lists snl)))
1034 (arg-info (getf plist :arg-info))
1035 (nreq (car arg-info))
1036 (restp (cdr arg-info)))
1037 (setq method-function
1038 (lambda (method-args next-methods)
1039 (let* ((pv-cell (when pv-table
1040 (get-pv-cell method-args pv-table)))
1041 (nm (car next-methods))
1042 (nms (cdr next-methods))
1043 (nmc (when nm
1044 (make-method-call
1045 :function (if (std-instance-p nm)
1046 (method-function nm)
1048 :call-method-args (list nms)))))
1049 (apply fmf pv-cell nmc method-args))))
1050 ;; FIXME: this looks dangerous.
1051 (let* ((fname (%fun-name fmf)))
1052 (when (and fname (eq (car fname) 'fast-method))
1053 (set-fun-name method-function (cons 'slow-method (cdr fname)))))
1054 method-function))
1056 ;;; this is similar to the above, only not quite. Only called when
1057 ;;; the MOP is heavily involved. Not quite parallel to
1058 ;;; METHOD-FUNCTION-FROM-FAST-METHOD-FUNCTION, because we can close
1059 ;;; over the actual PV-CELL in this case.
1060 (defun method-function-from-fast-method-call (fmc)
1061 (let* ((fmf (fast-method-call-function fmc))
1062 (pv-cell (fast-method-call-pv-cell fmc))
1063 (arg-info (fast-method-call-arg-info fmc))
1064 (nreq (car arg-info))
1065 (restp (cdr arg-info)))
1066 (lambda (method-args next-methods)
1067 (let* ((nm (car next-methods))
1068 (nms (cdr next-methods))
1069 (nmc (when nm
1070 (make-method-call
1071 :function (if (std-instance-p nm)
1072 (method-function nm)
1074 :call-method-args (list nms)))))
1075 (apply fmf pv-cell nmc method-args)))))
1077 (defun get-pv-cell (method-args pv-table)
1078 (let ((pv-wrappers (pv-wrappers-from-all-args pv-table method-args)))
1079 (when pv-wrappers
1080 (pv-table-lookup pv-table pv-wrappers))))
1082 (defun pv-table-lookup-pv-args (pv-table &rest pv-parameters)
1083 (pv-table-lookup pv-table (pv-wrappers-from-pv-args pv-parameters)))
1085 (defun pv-wrappers-from-pv-args (&rest args)
1086 (let (wrappers)
1087 (dolist (arg args (if (cdr wrappers) (nreverse wrappers) (car wrappers)))
1088 (let ((wrapper (wrapper-of arg)))
1089 (push (if (invalid-wrapper-p wrapper)
1090 (check-wrapper-validity wrapper)
1091 wrapper)
1092 wrappers)))))
1094 (defun pv-wrappers-from-all-args (pv-table args)
1095 (loop for snl in (pv-table-slot-name-lists pv-table) and arg in args
1096 when snl
1097 collect (wrapper-of arg) into wrappers
1098 finally (return (if (cdr wrappers) wrappers (car wrappers)))))
1100 ;;; Return the subset of WRAPPERS which is used in the cache
1101 ;;; of PV-TABLE.
1102 (defun pv-wrappers-from-all-wrappers (pv-table wrappers)
1103 (loop for snl in (pv-table-slot-name-lists pv-table) and w in wrappers
1104 when snl
1105 collect w into result
1106 finally (return (if (cdr result) result (car result)))))