1.0.9.54: clean up old pv updating code
[sbcl/simd.git] / src / pcl / vector.lisp
blob63299b90275945756e6934f6592443ecc358fd74
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 ;;;; Up to 1.0.9.24 SBCL used to have a sketched out implementation
29 ;;;; for optimizing GF calls inside method bodies using a PV approach,
30 ;;;; inherited from the original PCL. This was never completed, and
31 ;;;; was removed at that point to make the code easier to understand
32 ;;;; -- but:
33 ;;;;
34 ;;;; FIXME: It would be possible to optimize GF calls inside method
35 ;;;; bodies using permutation vectors: if all the arguments to the
36 ;;;; GF are specializers parameters, we can assign a permutation index
37 ;;;; to each such (GF . ARGS) tuple inside a method body, and use this
38 ;;;; to cache effective method functions.
40 (declaim (inline make-pv-table))
41 (defstruct (pv-table (:predicate pv-tablep)
42 (:copier nil))
43 (cache nil :type (or cache null))
44 (pv-size 0 :type fixnum)
45 (slot-name-lists nil :type list))
47 (defun make-pv-table-type-declaration (var)
48 `(type pv-table ,var))
50 ;;; Used for interning parts of SLOT-NAME-LISTS, as part of
51 ;;; PV-TABLE interning -- just to save space.
52 (defvar *slot-name-lists* (make-hash-table :test 'equal))
54 ;;; Used for interning PV-TABLES, keyed by the SLOT-NAME-LISTS
55 ;;; used.
56 (defvar *pv-tables* (make-hash-table :test 'equal))
58 ;;; ...and one lock to rule them. Spinlock because for certain (rare)
59 ;;; cases this lock might be grabbed in the course of method dispatch
60 ;;; -- and mostly this is already under the *big-compiler-lock*.
61 (defvar *pv-lock*
62 (sb-thread::make-spinlock :name "pv table index lock"))
64 (defun intern-pv-table (&key slot-name-lists)
65 (flet ((intern-slot-names (slot-names)
66 ;; FIXME: NIL at the head of the list is a remnant from
67 ;; old purged code, that hasn't been quite cleaned up yet.
68 ;; ...but as long as we assume it is there, we may as well
69 ;; assert it.
70 (aver (not (car slot-names)))
71 (or (gethash slot-names *slot-name-lists*)
72 (setf (gethash slot-names *slot-name-lists*) slot-names)))
73 (%intern-pv-table (snl)
74 (or (gethash snl *pv-tables*)
75 (setf (gethash snl *pv-tables*)
76 (make-pv-table :slot-name-lists snl
77 :pv-size (reduce #'+ snl
78 :key (lambda (slots)
79 (length (cdr slots)))))))))
80 (sb-thread::with-spinlock (*pv-lock*)
81 (%intern-pv-table (mapcar #'intern-slot-names slot-name-lists)))))
83 (defun optimize-slot-value-by-class-p (class slot-name type)
84 (or (not (eq *boot-state* 'complete))
85 (let ((slotd (find-slot-definition class slot-name)))
86 (and slotd
87 (slot-accessor-std-p slotd type)))))
89 (defun compute-pv-slot (slot-name wrapper class)
90 (when (optimize-slot-value-by-class-p class slot-name 'all)
91 (car (find-slot-cell wrapper slot-name))))
93 (defun compute-pv (slot-name-lists wrappers)
94 (unless (listp wrappers)
95 (setq wrappers (list wrappers)))
96 (let (elements)
97 (dolist (slot-names slot-name-lists)
98 (when slot-names
99 (let* ((wrapper (pop wrappers))
100 (std-p (typep wrapper 'wrapper))
101 (class (wrapper-class* wrapper)))
102 (dolist (slot-name (cdr slot-names))
103 (push (if std-p
104 (compute-pv-slot slot-name wrapper class)
105 nil)
106 elements)))))
107 (let* ((n (length elements))
108 (pv (make-array n)))
109 (loop for i from (1- n) downto 0
110 do (setf (svref pv i) (pop elements)))
111 pv)))
113 (defun pv-table-lookup (pv-table pv-wrappers)
114 (let* ((slot-name-lists (pv-table-slot-name-lists pv-table))
115 (cache (or (pv-table-cache pv-table)
116 (setf (pv-table-cache pv-table)
117 (make-cache :key-count (- (length slot-name-lists)
118 (count nil slot-name-lists))
119 :value t
120 :size 2)))))
121 (multiple-value-bind (hitp value) (probe-cache cache pv-wrappers)
122 (if hitp
123 value
124 (let* ((pv (compute-pv slot-name-lists pv-wrappers))
125 (new-cache (fill-cache cache pv-wrappers pv)))
126 ;; This is safe: if another thread races us here the loser just
127 ;; misses the next time as well.
128 (unless (eq new-cache cache)
129 (setf (pv-table-cache pv-table) new-cache))
130 pv)))))
132 (defun make-pv-type-declaration (var)
133 `(type simple-vector ,var))
135 (defun can-optimize-access (form required-parameters env)
136 (destructuring-bind (op var-form slot-name-form &optional new-value) form
137 (let ((type (ecase op
138 (slot-value 'reader)
139 (set-slot-value 'writer)
140 (slot-boundp 'boundp)))
141 (var (extract-the var-form))
142 (slot-name (constant-form-value slot-name-form env)))
143 (when (symbolp var)
144 (let* ((rebound? (caddr (var-declaration '%variable-rebinding var env)))
145 (parameter-or-nil (car (memq (or rebound? var)
146 required-parameters))))
147 (when parameter-or-nil
148 (let* ((class-name (caddr (var-declaration '%class
149 parameter-or-nil
150 env)))
151 (class (find-class class-name nil)))
152 (when (or (not (eq *boot-state* 'complete))
153 (and class (not (class-finalized-p class))))
154 (setq class nil))
155 (when (and class-name (not (eq class-name t)))
156 (when (or (null type)
157 (not (and class
158 (memq *the-class-structure-object*
159 (class-precedence-list class))))
160 (optimize-slot-value-by-class-p class slot-name type))
161 (values (cons parameter-or-nil (or class class-name))
162 slot-name
163 new-value))))))))))
165 ;;; Check whether the binding of the named variable is modified in the
166 ;;; method body.
167 (defun parameter-modified-p (parameter-name env)
168 (let ((modified-variables (macroexpand '%parameter-binding-modified env)))
169 (memq parameter-name modified-variables)))
171 (defun optimize-slot-value (form slots required-parameters env)
172 (multiple-value-bind (sparameter slot-name)
173 (can-optimize-access form required-parameters env)
174 (if sparameter
175 (let ((optimized-form
176 (optimize-instance-access slots :read sparameter
177 slot-name nil)))
178 ;; We don't return the optimized form directly, since there's
179 ;; still a chance that we'll find out later on that the
180 ;; optimization should not have been done, for example due to
181 ;; the walker encountering a SETQ on SPARAMETER later on in
182 ;; the body [ see for example clos.impure.lisp test with :name
183 ;; ((:setq :method-parameter) slot-value)) ]. Instead we defer
184 ;; the decision until the compiler macroexpands
185 ;; OPTIMIZED-SLOT-VALUE.
187 ;; Note that we must still call OPTIMIZE-INSTANCE-ACCESS at
188 ;; this point (instead of when expanding
189 ;; OPTIMIZED-SLOT-VALUE), since it mutates the structure of
190 ;; SLOTS. If that mutation isn't done during the walking,
191 ;; MAKE-METHOD-LAMBDA-INTERNAL won't wrap a correct PV-BINDING
192 ;; form around the body, and compilation will fail. -- JES,
193 ;; 2006-09-18
194 `(optimized-slot-value ,form ,(car sparameter) ,optimized-form))
195 `(accessor-slot-value ,@(cdr form)))))
197 (defmacro optimized-slot-value (form parameter-name optimized-form
198 &environment env)
199 ;; Either use OPTIMIZED-FORM or fall back to the safe
200 ;; ACCESSOR-SLOT-VALUE.
201 (if (parameter-modified-p parameter-name env)
202 `(accessor-slot-value ,@(cdr form))
203 optimized-form))
205 (defun optimize-set-slot-value (form slots required-parameters env)
206 (multiple-value-bind (sparameter slot-name new-value)
207 (can-optimize-access form required-parameters env)
208 (if sparameter
209 (let ((optimized-form
210 (optimize-instance-access slots :write sparameter
211 slot-name new-value)))
212 ;; See OPTIMIZE-SLOT-VALUE
213 `(optimized-set-slot-value ,form ,(car sparameter) ,optimized-form))
214 `(accessor-set-slot-value ,@(cdr form)))))
216 (defmacro optimized-set-slot-value (form parameter-name optimized-form
217 &environment env)
218 (cond ((safe-code-p env)
219 ;; Don't optimize slot value setting in safe code, since the
220 ;; optimized version will fail to catch some type errors
221 ;; (for example when a subclass declares a tighter type for
222 ;; the slot than a superclass).
223 `(safe-set-slot-value ,@(cdr form)))
224 ((parameter-modified-p parameter-name env)
225 `(accessor-set-slot-value ,@(cdr form)))
227 optimized-form)))
229 (defun optimize-slot-boundp (form slots required-parameters env)
230 (multiple-value-bind (sparameter slot-name)
231 (can-optimize-access form required-parameters env)
232 (if sparameter
233 (let ((optimized-form
234 (optimize-instance-access slots :boundp sparameter
235 slot-name nil)))
236 ;; See OPTIMIZE-SLOT-VALUE
237 `(optimized-slot-boundp ,form ,(car sparameter) ,optimized-form))
238 `(accessor-slot-boundp ,@(cdr form)))))
240 (defmacro optimized-slot-boundp (form parameter-name optimized-form
241 &environment env)
242 (if (parameter-modified-p parameter-name env)
243 `(accessor-slot-boundp ,@(cdr form))
244 optimized-form))
246 ;;; The SLOTS argument is an alist, the CAR of each entry is the name
247 ;;; of a required parameter to the function. The alist is in order, so
248 ;;; the position of an entry in the alist corresponds to the
249 ;;; argument's position in the lambda list.
250 (defun optimize-instance-access (slots
251 read/write
252 sparameter
253 slot-name
254 new-value)
255 (let ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
256 (parameter (if (consp sparameter) (car sparameter) sparameter)))
257 (if (and (eq *boot-state* 'complete)
258 (classp class)
259 (memq *the-class-structure-object* (class-precedence-list class)))
260 (let ((slotd (find-slot-definition class slot-name)))
261 (ecase read/write
262 (:read
263 `(,(slot-definition-defstruct-accessor-symbol slotd) ,parameter))
264 (:write
265 `(setf (,(slot-definition-defstruct-accessor-symbol slotd)
266 ,parameter)
267 ,new-value))
268 (:boundp
269 t)))
270 (let* ((parameter-entry (assq parameter slots))
271 (slot-entry (assq slot-name (cdr parameter-entry)))
272 (position (posq parameter-entry slots))
273 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
274 (unless parameter-entry
275 (bug "slot optimization bewilderment: O-I-A"))
276 (unless slot-entry
277 (setq slot-entry (list slot-name))
278 (push slot-entry (cdr parameter-entry)))
279 (push pv-offset-form (cdr slot-entry))
280 (ecase read/write
281 (:read
282 `(instance-read ,pv-offset-form ,parameter ,position
283 ',slot-name ',class))
284 (:write
285 `(let ((.new-value. ,new-value))
286 (instance-write ,pv-offset-form ,parameter ,position
287 ',slot-name ',class .new-value.)))
288 (:boundp
289 `(instance-boundp ,pv-offset-form ,parameter ,position
290 ',slot-name ',class)))))))
292 (define-walker-template pv-offset) ; These forms get munged by mutate slots.
293 (defmacro pv-offset (arg) arg)
294 (define-walker-template instance-accessor-parameter)
295 (defmacro instance-accessor-parameter (x) x)
297 ;;; It is safe for these two functions to be wrong. They just try to
298 ;;; guess what the most likely case will be.
299 (defun generate-fast-class-slot-access-p (class-form slot-name-form)
300 (let ((class (and (constantp class-form) (constant-form-value class-form)))
301 (slot-name (and (constantp slot-name-form)
302 (constant-form-value slot-name-form))))
303 (and (eq *boot-state* 'complete)
304 (standard-class-p class)
305 (not (eq class *the-class-t*)) ; shouldn't happen, though.
306 (let ((slotd (find-slot-definition class slot-name)))
307 (and slotd (eq :class (slot-definition-allocation slotd)))))))
309 (defun skip-fast-slot-access-p (class-form slot-name-form type)
310 (let ((class (and (constantp class-form) (constant-form-value class-form)))
311 (slot-name (and (constantp slot-name-form)
312 (constant-form-value slot-name-form))))
313 (and (eq *boot-state* 'complete)
314 (standard-class-p class)
315 (not (eq class *the-class-t*)) ; shouldn't happen, though.
316 ;; FIXME: Is this really right? "Don't skip if there is
317 ;; no slot definition."
318 (let ((slotd (find-slot-definition class slot-name)))
319 (and slotd
320 (not (slot-accessor-std-p slotd type)))))))
322 (defmacro instance-read-internal (pv slots pv-offset default &optional kind)
323 (unless (member kind '(nil :instance :class :default))
324 (error "illegal kind argument to ~S: ~S" 'instance-read-internal kind))
325 (if (eq kind :default)
326 default
327 (let* ((index (gensym))
328 (value index))
329 `(locally (declare #.*optimize-speed*)
330 (let ((,index (svref ,pv ,pv-offset)))
331 (setq ,value (typecase ,index
332 ;; FIXME: the line marked by KLUDGE below
333 ;; (and the analogous spot in
334 ;; INSTANCE-WRITE-INTERNAL) is there purely
335 ;; to suppress a type mismatch warning that
336 ;; propagates through to user code.
337 ;; Presumably SLOTS at this point can never
338 ;; actually be NIL, but the compiler seems
339 ;; to think it could, so we put this here
340 ;; to shut it up. (see also mail Rudi
341 ;; Schlatte sbcl-devel 2003-09-21) -- CSR,
342 ;; 2003-11-30
343 ,@(when (or (null kind) (eq kind :instance))
344 `((fixnum
345 (and ,slots ; KLUDGE
346 (clos-slots-ref ,slots ,index)))))
347 ,@(when (or (null kind) (eq kind :class))
348 `((cons (cdr ,index))))
349 (t +slot-unbound+)))
350 (if (eq ,value +slot-unbound+)
351 ,default
352 ,value))))))
354 (defmacro instance-read (pv-offset parameter position slot-name class)
355 (if (skip-fast-slot-access-p class slot-name 'reader)
356 `(accessor-slot-value ,parameter ,slot-name)
357 `(instance-read-internal .pv. ,(slot-vector-symbol position)
358 ,pv-offset (accessor-slot-value ,parameter ,slot-name)
359 ,(if (generate-fast-class-slot-access-p class slot-name)
360 :class :instance))))
362 (defmacro instance-write-internal (pv slots pv-offset new-value default
363 &optional kind)
364 (unless (member kind '(nil :instance :class :default))
365 (error "illegal kind argument to ~S: ~S" 'instance-write-internal kind))
366 (if (eq kind :default)
367 default
368 (let* ((index (gensym)))
369 `(locally (declare #.*optimize-speed*)
370 (let ((,index (svref ,pv ,pv-offset)))
371 (typecase ,index
372 ,@(when (or (null kind) (eq kind :instance))
373 `((fixnum (and ,slots
374 (setf (clos-slots-ref ,slots ,index)
375 ,new-value)))))
376 ,@(when (or (null kind) (eq kind :class))
377 `((cons (setf (cdr ,index) ,new-value))))
378 (t ,default)))))))
380 (defmacro instance-write (pv-offset
381 parameter
382 position
383 slot-name
384 class
385 new-value)
386 (if (skip-fast-slot-access-p class slot-name 'writer)
387 `(accessor-set-slot-value ,parameter ,slot-name ,new-value)
388 `(instance-write-internal .pv. ,(slot-vector-symbol position)
389 ,pv-offset ,new-value
390 (accessor-set-slot-value ,parameter ,slot-name ,new-value)
391 ,(if (generate-fast-class-slot-access-p class slot-name)
392 :class :instance))))
394 (defmacro instance-boundp-internal (pv slots pv-offset default
395 &optional kind)
396 (unless (member kind '(nil :instance :class :default))
397 (error "illegal kind argument to ~S: ~S" 'instance-boundp-internal kind))
398 (if (eq kind :default)
399 default
400 (let* ((index (gensym)))
401 `(locally (declare #.*optimize-speed*)
402 (let ((,index (svref ,pv ,pv-offset)))
403 (typecase ,index
404 ,@(when (or (null kind) (eq kind :instance))
405 `((fixnum (not (and ,slots
406 (eq (clos-slots-ref ,slots ,index)
407 +slot-unbound+))))))
408 ,@(when (or (null kind) (eq kind :class))
409 `((cons (not (eq (cdr ,index) +slot-unbound+)))))
410 (t ,default)))))))
412 (defmacro instance-boundp (pv-offset parameter position slot-name class)
413 (if (skip-fast-slot-access-p class slot-name 'boundp)
414 `(accessor-slot-boundp ,parameter ,slot-name)
415 `(instance-boundp-internal .pv. ,(slot-vector-symbol position)
416 ,pv-offset (accessor-slot-boundp ,parameter ,slot-name)
417 ,(if (generate-fast-class-slot-access-p class slot-name)
418 :class :instance))))
420 ;;; This magic function has quite a job to do indeed.
422 ;;; The careful reader will recall that <slots> contains all of the
423 ;;; optimized slot access forms produced by OPTIMIZE-INSTANCE-ACCESS.
424 ;;; Each of these is a call to either INSTANCE-READ or INSTANCE-WRITE.
426 ;;; At the time these calls were produced, the first argument was
427 ;;; specified as the symbol .PV-OFFSET.; what we have to do now is
428 ;;; convert those pv-offset arguments into the actual number that is
429 ;;; the correct offset into the pv.
431 ;;; But first, oh but first, we sort <slots> a bit so that for each
432 ;;; argument we have the slots in alphabetical order. This
433 ;;; canonicalizes the PV-TABLE's a bit and will hopefully lead to
434 ;;; having fewer PV's floating around. Even if the gain is only
435 ;;; modest, it costs nothing.
436 (defun slot-name-lists-from-slots (slots)
437 (let ((slots (mutate-slots slots)))
438 (let* ((slot-name-lists
439 (mapcar (lambda (parameter-entry)
440 (cons nil (mapcar #'car (cdr parameter-entry))))
441 slots)))
442 (mapcar (lambda (r+snl)
443 (when (or (car r+snl) (cdr r+snl))
444 r+snl))
445 slot-name-lists))))
447 (defun mutate-slots (slots)
448 (let ((sorted-slots (sort-slots slots))
449 (pv-offset -1))
450 (dolist (parameter-entry sorted-slots)
451 (dolist (slot-entry (cdr parameter-entry))
452 (incf pv-offset)
453 (dolist (form (cdr slot-entry))
454 (setf (cadr form) pv-offset))))
455 sorted-slots))
457 (defun symbol-pkg-name (sym)
458 (let ((pkg (symbol-package sym)))
459 (if pkg (package-name pkg) "")))
461 ;;; FIXME: Because of the existence of UNINTERN and RENAME-PACKAGE,
462 ;;; the part of this ordering which is based on SYMBOL-PKG-NAME is not
463 ;;; stable. This ordering is only used in to
464 ;;; SLOT-NAME-LISTS-FROM-SLOTS, where it serves to "canonicalize the
465 ;;; PV-TABLE's a bit and will hopefully lead to having fewer PV's
466 ;;; floating around", so it sounds as though the instability won't
467 ;;; actually lead to bugs, just small inefficiency. But still, it
468 ;;; would be better to reimplement this function as a comparison based
469 ;;; on SYMBOL-HASH:
470 ;;; * stable comparison
471 ;;; * smaller code (here, and in being able to discard SYMBOL-PKG-NAME)
472 ;;; * faster code.
473 (defun symbol-lessp (a b)
474 (if (eq (symbol-package a)
475 (symbol-package b))
476 (string-lessp (symbol-name a)
477 (symbol-name b))
478 (string-lessp (symbol-pkg-name a)
479 (symbol-pkg-name b))))
481 (defun symbol-or-cons-lessp (a b)
482 (etypecase a
483 (symbol (etypecase b
484 (symbol (symbol-lessp a b))
485 (cons t)))
486 (cons (etypecase b
487 (symbol nil)
488 (cons (if (eq (car a) (car b))
489 (symbol-or-cons-lessp (cdr a) (cdr b))
490 (symbol-or-cons-lessp (car a) (car b))))))))
492 (defun sort-slots (slots)
493 (mapcar (lambda (parameter-entry)
494 (cons (car parameter-entry)
495 (sort (cdr parameter-entry) ;slot entries
496 #'symbol-or-cons-lessp
497 :key #'car)))
498 slots))
501 ;;;; This needs to work in terms of metatypes and also needs to work
502 ;;;; for automatically generated reader and writer functions.
503 ;;;; Automatically generated reader and writer functions use this
504 ;;;; stuff too.
506 (defmacro pv-binding ((required-parameters slot-name-lists pv-table-form)
507 &body body)
508 (let (slot-vars pv-parameters)
509 (loop for slots in slot-name-lists
510 for required-parameter in required-parameters
511 for i from 0
512 do (when slots
513 (push required-parameter pv-parameters)
514 (push (slot-vector-symbol i) slot-vars)))
515 `(pv-binding1 (,pv-table-form
516 ,(nreverse pv-parameters) ,(nreverse slot-vars))
517 ,@body)))
519 (defmacro pv-binding1 ((pv-table-form pv-parameters slot-vars)
520 &body body)
521 `(pv-env (,pv-table-form ,pv-parameters)
522 (let (,@(mapcar (lambda (slot-var p) `(,slot-var (get-slots-or-nil ,p)))
523 slot-vars pv-parameters))
524 (declare (ignorable ,@(mapcar #'identity slot-vars)))
525 ,@body)))
527 ;;; This will only be visible in PV-ENV when the default MAKE-METHOD-LAMBDA is
528 ;;; overridden.
529 (define-symbol-macro pv-env-environment overridden)
531 (defmacro pv-env (&environment env
532 (pv-table-form pv-parameters)
533 &rest forms)
534 ;; Decide which expansion to use based on the state of the PV-ENV-ENVIRONMENT
535 ;; symbol-macrolet.
536 (if (eq (macroexpand 'pv-env-environment env) 'default)
537 `(locally ,@forms)
538 `(let* ((.pv-table. ,pv-table-form)
539 (.pv. (pv-table-lookup-pv-args .pv-table. ,@pv-parameters)))
540 (declare ,(make-pv-type-declaration '.pv.))
541 ,@forms)))
543 (defvar *non-var-declarations*
544 ;; FIXME: VALUES was in this list, conditionalized with #+CMU, but I
545 ;; don't *think* CMU CL had, or SBCL has, VALUES declarations. If
546 ;; SBCL doesn't have 'em, VALUES should probably be removed from
547 ;; this list.
548 '(values
549 %method-name
550 %method-lambda-list
551 optimize
552 ftype
553 muffle-conditions
554 inline
555 notinline))
557 (defvar *var-declarations-with-arg*
558 '(%class
559 type))
561 (defvar *var-declarations-without-arg*
562 '(ignore
563 ignorable special dynamic-extent
564 ;; FIXME: Possibly this entire list and variable could go away.
565 ;; If not, certainly we should remove all these built-in typenames
566 ;; from the list, and replace them with a test for "is it a type
567 ;; name?" (CLTL1 allowed only built-in type names as declarations,
568 ;; but ANSI CL allows any type name as a declaration.)
569 array atom base-char bignum bit bit-vector character compiled-function
570 complex cons double-float extended-char
571 fixnum float function hash-table integer
572 keyword list long-float nil null number package pathname random-state ratio
573 rational readtable sequence short-float signed-byte simple-array
574 simple-bit-vector simple-string simple-vector single-float standard-char
575 stream string symbol t unsigned-byte vector))
577 (defun split-declarations (body args maybe-reads-params-p)
578 (let ((inner-decls nil)
579 (outer-decls nil)
580 decl)
581 (loop (when (null body) (return nil))
582 (setq decl (car body))
583 (unless (and (consp decl)
584 (eq (car decl) 'declare))
585 (return nil))
586 (dolist (form (cdr decl))
587 (when (consp form)
588 (let ((declaration-name (car form)))
589 (if (member declaration-name *non-var-declarations*)
590 (push `(declare ,form) outer-decls)
591 (let ((arg-p
592 (member declaration-name
593 *var-declarations-with-arg*))
594 (non-arg-p
595 (member declaration-name
596 *var-declarations-without-arg*))
597 (dname (list (pop form)))
598 (inners nil) (outers nil))
599 (unless (or arg-p non-arg-p)
600 ;; FIXME: This warning, and perhaps the
601 ;; various *VAR-DECLARATIONS-FOO* and/or
602 ;; *NON-VAR-DECLARATIONS* variables,
603 ;; could probably go away now that we're not
604 ;; trying to be portable between different
605 ;; CLTL1 hosts the way PCL was. (Note that to
606 ;; do this right, we need to be able to handle
607 ;; user-defined (DECLAIM (DECLARATION FOO))
608 ;; stuff.)
609 (warn "The declaration ~S is not understood by ~S.~@
610 Please put ~S on one of the lists ~S,~%~S, or~%~S.~@
611 (Assuming it is a variable declaration without argument)."
612 declaration-name 'split-declarations
613 declaration-name
614 '*non-var-declarations*
615 '*var-declarations-with-arg*
616 '*var-declarations-without-arg*)
617 (push declaration-name *var-declarations-without-arg*))
618 (when arg-p
619 (setq dname (append dname (list (pop form)))))
620 (case (car dname)
621 (%class (push `(declare (,@dname ,@form)) inner-decls))
623 (dolist (var form)
624 (if (member var args)
625 ;; Quietly remove IGNORE declarations
626 ;; on args when a next-method is
627 ;; involved, to prevent compiler
628 ;; warnings about ignored args being
629 ;; read.
630 (unless (and maybe-reads-params-p
631 (eq (car dname) 'ignore))
632 (push var outers))
633 (push var inners)))
634 (when outers
635 (push `(declare (,@dname ,@outers)) outer-decls))
636 (when inners
637 (push
638 `(declare (,@dname ,@inners))
639 inner-decls)))))))))
640 (setq body (cdr body)))
641 (values outer-decls inner-decls body)))
643 ;;; Pull a name out of the %METHOD-NAME declaration in the function
644 ;;; body given, or return NIL if no %METHOD-NAME declaration is found.
645 (defun body-method-name (body)
646 (multiple-value-bind (real-body declarations documentation)
647 (parse-body body)
648 (declare (ignore real-body documentation))
649 (let ((name-decl (get-declaration '%method-name declarations)))
650 (and name-decl
651 (destructuring-bind (name) name-decl
652 name)))))
654 ;;; Convert a lambda expression containing a SB-PCL::%METHOD-NAME
655 ;;; declaration (which is a naming style internal to PCL) into an
656 ;;; SB-INT:NAMED-LAMBDA expression (which is a naming style used
657 ;;; throughout SBCL, understood by the main compiler); or if there's
658 ;;; no SB-PCL::%METHOD-NAME declaration, then just return the original
659 ;;; lambda expression.
660 (defun name-method-lambda (method-lambda)
661 (let ((method-name (body-method-name (cddr method-lambda))))
662 (if method-name
663 `(named-lambda (slow-method ,method-name) ,(rest method-lambda))
664 method-lambda)))
666 (defun make-method-initargs-form-internal (method-lambda initargs env)
667 (declare (ignore env))
668 (let (method-lambda-args
669 lmf ; becomes body of function
670 lmf-params)
671 (if (not (and (= 3 (length method-lambda))
672 (= 2 (length (setq method-lambda-args (cadr method-lambda))))
673 (consp (setq lmf (third method-lambda)))
674 (eq 'simple-lexical-method-functions (car lmf))
675 (eq (car method-lambda-args)
676 (cadr (setq lmf-params (cadr lmf))))
677 (eq (cadr method-lambda-args)
678 (caddr lmf-params))))
679 `(list* :function ,(name-method-lambda method-lambda)
680 ',initargs)
681 (let* ((lambda-list (car lmf-params))
682 (nreq 0)
683 (restp nil)
684 (args nil))
685 (dolist (arg lambda-list)
686 (when (member arg '(&optional &rest &key))
687 (setq restp t)
688 (return nil))
689 (when (eq arg '&aux)
690 (return nil))
691 (incf nreq)
692 (push arg args))
693 (setq args (nreverse args))
694 (setf (getf (getf initargs 'plist) :arg-info) (cons nreq restp))
695 (make-method-initargs-form-internal1
696 initargs (cddr lmf) args lmf-params restp)))))
698 (defun lambda-list-parameter-names (lambda-list)
699 ;; Given a valid lambda list, extract the parameter names.
700 (loop for x in lambda-list
701 with res = nil
702 do (unless (member x lambda-list-keywords)
703 (if (consp x)
704 (let ((name (car x)))
705 (if (consp name)
706 ;; ... ((:BAR FOO) 1)
707 (push (second name) res)
708 ;; ... (FOO 1)
709 (push name res))
710 ;; ... (... 1 FOO-P)
711 (let ((name-p (cddr x)))
712 (when name-p
713 (push (car name-p) res))))
714 ;; ... FOO
715 (push x res)))
716 finally (return res)))
718 (defun make-method-initargs-form-internal1
719 (initargs body req-args lmf-params restp)
720 (let* (;; The lambda-list of the method, minus specifiers
721 (lambda-list (car lmf-params))
722 ;; Names of the parameters that will be in the outermost lambda-list
723 ;; (and whose bound declarations thus need to be in OUTER-DECLS).
724 (outer-parameters req-args)
725 ;; The lambda-list used by BIND-ARGS
726 (bind-list lambda-list)
727 (setq-p (getf (cdr lmf-params) :setq-p))
728 (auxp (member '&aux bind-list))
729 (call-next-method-p (getf (cdr lmf-params) :call-next-method-p)))
730 ;; Try to use the normal function call machinery instead of BIND-ARGS
731 ;; binding the arguments, unless:
732 (unless (or ;; If all arguments are required, BIND-ARGS will be a no-op
733 ;; in any case.
734 (and (not restp) (not auxp))
735 ;; CALL-NEXT-METHOD wants to use BIND-ARGS, and needs a
736 ;; list of all non-required arguments.
737 call-next-method-p)
738 (setf ;; We don't want a binding for .REST-ARG.
739 restp nil
740 ;; Get all the parameters for declaration parsing
741 outer-parameters (lambda-list-parameter-names lambda-list)
742 ;; Ensure that BIND-ARGS won't do anything (since
743 ;; BIND-LIST won't contain any non-required parameters,
744 ;; and REQ-ARGS will be of an equal length). We still want
745 ;; to pass BIND-LIST to FAST-LEXICAL-METHOD-FUNCTIONS so
746 ;; that BIND-FAST-LEXICAL-METHOD-FUNCTIONS can take care
747 ;; of rebinding SETQd required arguments around the method
748 ;; body.
749 bind-list req-args))
750 (multiple-value-bind (outer-decls inner-decls body-sans-decls)
751 (split-declarations
752 body outer-parameters (or call-next-method-p setq-p))
753 (let* ((rest-arg (when restp
754 '.rest-arg.))
755 (fmf-lambda-list (if rest-arg
756 (append req-args (list '&rest rest-arg))
757 (if call-next-method-p
758 req-args
759 lambda-list))))
760 `(list*
761 :function
762 (let* ((fmf (,(if (body-method-name body) 'named-lambda 'lambda)
763 ,@(when (body-method-name body)
764 ;; function name
765 (list (cons 'fast-method (body-method-name body))))
766 ;; The lambda-list of the FMF
767 (.pv. .next-method-call. ,@fmf-lambda-list)
768 ;; body of the function
769 (declare (ignorable .pv. .next-method-call.)
770 (disable-package-locks pv-env-environment))
771 ,@outer-decls
772 (symbol-macrolet ((pv-env-environment default))
773 (fast-lexical-method-functions
774 (,bind-list .next-method-call. ,req-args ,rest-arg
775 ,@(cdddr lmf-params))
776 ,@inner-decls
777 ,@body-sans-decls))))
778 (mf (%make-method-function fmf nil)))
779 (set-funcallable-instance-function
780 mf (method-function-from-fast-function fmf ',(getf initargs 'plist)))
782 ',initargs)))))
784 ;;; Use arrays and hash tables and the fngen stuff to make this much
785 ;;; better. It doesn't really matter, though, because a function
786 ;;; returned by this will get called only when the user explicitly
787 ;;; funcalls a result of method-function. BUT, this is needed to make
788 ;;; early methods work.
789 (defun method-function-from-fast-function (fmf plist)
790 (declare (type function fmf))
791 (let* ((method-function nil)
792 (snl (getf plist :slot-name-lists))
793 (pv-table (when snl
794 (intern-pv-table :slot-name-lists snl)))
795 (arg-info (getf plist :arg-info))
796 (nreq (car arg-info))
797 (restp (cdr arg-info)))
798 (setq method-function
799 (lambda (method-args next-methods)
800 (let* ((pv (when pv-table
801 (get-pv method-args pv-table)))
802 (nm (car next-methods))
803 (nms (cdr next-methods))
804 (nmc (when nm
805 (make-method-call
806 :function (if (std-instance-p nm)
807 (method-function nm)
809 :call-method-args (list nms)))))
810 (apply fmf pv nmc method-args))))
811 ;; FIXME: this looks dangerous.
812 (let* ((fname (%fun-name fmf)))
813 (when (and fname (eq (car fname) 'fast-method))
814 (set-fun-name method-function (cons 'slow-method (cdr fname)))))
815 method-function))
817 ;;; this is similar to the above, only not quite. Only called when
818 ;;; the MOP is heavily involved. Not quite parallel to
819 ;;; METHOD-FUNCTION-FROM-FAST-METHOD-FUNCTION, because we can close
820 ;;; over the actual PV-CELL in this case.
821 (defun method-function-from-fast-method-call (fmc)
822 (let* ((fmf (fast-method-call-function fmc))
823 (pv (fast-method-call-pv fmc))
824 (arg-info (fast-method-call-arg-info fmc))
825 (nreq (car arg-info))
826 (restp (cdr arg-info)))
827 (lambda (method-args next-methods)
828 (let* ((nm (car next-methods))
829 (nms (cdr next-methods))
830 (nmc (when nm
831 (make-method-call
832 :function (if (std-instance-p nm)
833 (method-function nm)
835 :call-method-args (list nms)))))
836 (apply fmf pv nmc method-args)))))
838 (defun get-pv (method-args pv-table)
839 (let ((pv-wrappers (pv-wrappers-from-all-args pv-table method-args)))
840 (when pv-wrappers
841 (pv-table-lookup pv-table pv-wrappers))))
843 (defun pv-table-lookup-pv-args (pv-table &rest pv-parameters)
844 (pv-table-lookup pv-table (pv-wrappers-from-pv-args pv-parameters)))
846 (defun pv-wrappers-from-pv-args (&rest args)
847 (loop for arg in args
848 collect (valid-wrapper-of arg)))
850 (defun pv-wrappers-from-all-args (pv-table args)
851 (loop for snl in (pv-table-slot-name-lists pv-table)
852 and arg in args
853 when snl
854 collect (valid-wrapper-of arg)))
856 ;;; Return the subset of WRAPPERS which is used in the cache
857 ;;; of PV-TABLE.
858 (defun pv-wrappers-from-all-wrappers (pv-table wrappers)
859 (loop for snl in (pv-table-slot-name-lists pv-table) and w in wrappers
860 when snl
861 collect w))