1
;;;-*-Mode:LISP; Package:(PCL LISP 1000); Base:10; Syntax:Common-lisp -*-
3
;;; *************************************************************************
4
;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
5
;;; All rights reserved.
7
;;; Use and copying of this software and preparation of derivative works
8
;;; based upon this software are permitted. Any distribution of this
9
;;; software or derivative works must comply with all applicable United
10
;;; States export control laws.
12
;;; This software is made available AS IS, and Xerox Corporation makes no
13
;;; warranty about the software, its performance or its conformity to any
16
;;; Any person obtaining a copy of this software is requested to send their
17
;;; name and post office or electronic mail address to:
18
;;; CommonLoops Coordinator
20
;;; 3333 Coyote Hill Rd.
21
;;; Palo Alto, CA 94304
22
;;; (or send Arpanet mail to CommonLoops-Coordinator.pa@Xerox.arpa)
24
;;; Suggestions, comments and requests for improvements are also welcome.
25
;;; *************************************************************************
27
;;; Permutation vectors.
32
(defmacro instance-slot-index (wrapper slot-name)
34
(declare (fixnum pos))
36
(dolist (sn (wrapper-instance-slots-layout ,wrapper))
37
(when (eq ,slot-name sn) (return-from loop pos))
44
(defun pv-cache-limit-fn (nlines)
45
(default-limit-fn nlines))
48
(:predicate pv-tablep)
49
(:constructor make-pv-table-internal
50
(slot-name-lists call-list)))
51
(cache nil :type (or cache null))
52
(pv-size 0 :type fixnum)
53
(slot-name-lists nil :type list)
54
(call-list nil :type list))
57
(declaim (ext:freeze-type pv-table))
59
(defvar *initial-pv-table* (make-pv-table-internal nil nil))
61
; help new slot-value-using-class methods affect fast iv access
62
(defvar *all-pv-table-list* nil)
64
(defun make-pv-table (&key slot-name-lists call-list)
65
(let ((pv-table (make-pv-table-internal slot-name-lists call-list)))
66
(push pv-table *all-pv-table-list*)
69
(defun make-pv-table-type-declaration (var)
70
`(type pv-table ,var))
72
(defvar *slot-name-lists-inner* (make-hash-table :test #'equal))
73
(defvar *slot-name-lists-outer* (make-hash-table :test #'equal))
75
;entries in this are lists of (table . pv-offset-list)
76
(defvar *pv-key-to-pv-table-table* (make-hash-table :test 'equal))
78
(defun intern-pv-table (&key slot-name-lists call-list)
81
(or (gethash x *slot-name-lists-inner*)
82
(setf (gethash x *slot-name-lists-inner*) (copy-list x))))
84
(or (gethash x *slot-name-lists-outer*)
85
(setf (gethash x *slot-name-lists-outer*)
86
(let ((snl (copy-list (cdr x)))
89
(make-pv-table :slot-name-lists snl
91
(let ((pv-table (outer (mapcar #'inner (cons call-list slot-name-lists)))))
94
(declare (fixnum pv-index))
95
(dolist (slot-name-list slot-name-lists)
96
(dolist (slot-name (cdr slot-name-list))
97
(note-pv-table-reference slot-name pv-index pv-table)
99
(dolist (gf-call call-list)
100
(note-pv-table-reference gf-call pv-index pv-table)
102
(setf (pv-table-pv-size pv-table) pv-index)))
105
(defun note-pv-table-reference (ref pv-offset pv-table)
106
(let ((entry (gethash ref *pv-key-to-pv-table-table*)))
108
(let ((table-entry (assq pv-table entry)))
109
(when (and (null table-entry)
110
(> (length entry) 8))
111
(let ((new-table-table (make-hash-table :size 16 :test 'eq)))
112
(dolist (table-entry entry)
113
(setf (gethash (car table-entry) new-table-table)
115
(setf (gethash ref *pv-key-to-pv-table-table*) new-table-table)))
117
(if (null table-entry)
118
(let ((new (cons pv-table pv-offset)))
120
(push new (cdr entry))
121
(setf (gethash ref *pv-key-to-pv-table-table*) (list new))))
122
(push pv-offset (cdr table-entry)))
123
(return-from note-pv-table-reference nil))))
124
(let ((list (gethash pv-table entry)))
126
(push pv-offset (cdr list))
127
(setf (gethash pv-table entry) (list pv-offset)))))
130
(defun map-pv-table-references-of (ref function)
131
(let ((entry (gethash ref *pv-key-to-pv-table-table*)))
133
(dolist (table+pv-offset-list entry)
135
(car table+pv-offset-list) (cdr table+pv-offset-list)))
136
(maphash function entry)))
140
(defvar *pvs* (make-hash-table :test #'equal))
142
(defun optimize-slot-value-by-class-p (class slot-name type)
143
(or (not (eq *boot-state* 'complete))
144
(let ((slotd (find-slot-definition class slot-name)))
146
(slot-accessor-std-p slotd type)))))
148
(defun compute-pv-slot (slot-name wrapper class class-slots class-slot-p-cell)
149
(if (symbolp slot-name)
150
(when (optimize-slot-value-by-class-p class slot-name 'all)
151
(or (instance-slot-index wrapper slot-name)
152
(let ((cell (assq slot-name class-slots)))
154
(setf (car class-slot-p-cell) t)
156
(when (consp slot-name)
157
(dolist (type '(reader writer) nil)
158
(when (eq (car slot-name) type)
160
(let* ((gf-name (cadr slot-name))
161
(gf (gdefinition gf-name))
163
(when (eq *boot-state* 'complete)
164
(accessor-values1 gf type class))))
165
(when (consp location)
166
(setf (car class-slot-p-cell) t))
169
(defun compute-pv (slot-name-lists wrappers)
170
(unless (listp wrappers) (setq wrappers (list wrappers)))
171
(let* ((not-simple-p-cell (list nil))
173
(gathering1 (collecting)
174
(iterate ((slot-names (list-elements slot-name-lists)))
176
(let* ((wrapper (pop wrappers))
177
(std-p #+cmu17 (typep wrapper 'wrapper)
179
(class (wrapper-class* wrapper))
180
(class-slots (and std-p (wrapper-class-slots wrapper))))
181
(dolist (slot-name (cdr slot-names))
184
(compute-pv-slot slot-name wrapper class
185
class-slots not-simple-p-cell))))))))))
186
(if (car not-simple-p-cell)
187
(make-permutation-vector (cons t elements))
188
(or (gethash elements *pvs*)
189
(setf (gethash elements *pvs*)
190
(make-permutation-vector (cons nil elements)))))))
192
(defun compute-calls (call-list wrappers)
193
(declare (ignore call-list wrappers))
197
(compute-emf-from-wrappers call wrappers))
202
#|| ; Need to finish this, then write the maintenance functions.
203
(defun compute-emf-from-wrappers (call wrappers)
205
; FIXME use regular destructuring-bind
206
(pcl-destructuring-bind (gf-name nreq restp arg-info) call
207
(if (eq gf-name 'make-instance)
208
(error "should not get here") ; there is another mechanism for this.
209
#'(lambda (&rest args)
210
(if (not (eq *boot-state* 'complete))
211
(apply (gdefinition gf-name) args)
212
(let* ((gf (gdefinition gf-name))
213
(arg-info (arg-info-reader gf))
216
(emf (cache-miss-values-internal gf arg-info
217
wrappers classes types
219
(update-all-pv-tables call wrappers emf)
220
#+copy-&rest-arg (setq args (copy-list args))
221
(invoke-emf emf args))))))))
224
(defun make-permutation-vector (indexes)
225
(make-array (length indexes) :initial-contents indexes))
227
(defun pv-table-lookup (pv-table pv-wrappers)
228
(let* ((slot-name-lists (pv-table-slot-name-lists pv-table))
229
(call-list (pv-table-call-list pv-table))
230
(cache (or (pv-table-cache pv-table)
231
(setf (pv-table-cache pv-table)
232
(get-cache (- (length slot-name-lists)
233
(count nil slot-name-lists))
237
(or (probe-cache cache pv-wrappers)
238
(let* ((pv (compute-pv slot-name-lists pv-wrappers))
239
(calls (compute-calls call-list pv-wrappers))
240
(pv-cell (cons pv calls))
241
(new-cache (fill-cache cache pv-wrappers pv-cell)))
242
(unless (eq new-cache cache)
243
(setf (pv-table-cache pv-table) new-cache)
247
(defun make-pv-type-declaration (var)
248
`(type simple-vector ,var))
250
(defvar *empty-pv* #())
252
(defmacro pvref (pv index)
255
(defmacro copy-pv (pv)
258
(defun make-calls-type-declaration (var)
259
`(type simple-vector ,var))
261
(defmacro callsref (calls index)
262
`(svref ,calls ,index))
264
(defvar *pv-table-cache-update-info* nil)
267
;(method shared-initialize :after (structure-class t))
269
(defun update-pv-table-cache-info (class)
270
(let ((slot-names-for-pv-table-update nil)
272
(dolist (icu *pv-table-cache-update-info*)
273
(if (eq (car icu) class)
274
(pushnew (cdr icu) slot-names-for-pv-table-update)
275
(push icu new-icui)))
276
(setq *pv-table-cache-update-info* new-icui)
277
(when slot-names-for-pv-table-update
278
(update-all-pv-table-caches class slot-names-for-pv-table-update))))
280
(defun update-all-pv-table-caches (class slot-names)
281
(let* ((cwrapper (class-wrapper class))
282
(std-p #+cmu17 (typep cwrapper 'wrapper) #-cmu17 t)
283
(class-slots (and std-p (wrapper-class-slots cwrapper)))
284
(class-slot-p-cell (list nil))
285
(new-values (mapcar #'(lambda (slot-name)
289
slot-name cwrapper class
290
class-slots class-slot-p-cell))))
293
(dolist (slot-name slot-names)
294
(map-pv-table-references-of
296
#'(lambda (pv-table pv-offset-list)
297
(declare (ignore pv-offset-list))
298
(pushnew pv-table pv-tables))))
299
(dolist (pv-table pv-tables)
300
(let* ((cache (pv-table-cache pv-table))
301
(slot-name-lists (pv-table-slot-name-lists pv-table))
302
(pv-size (pv-table-pv-size pv-table))
303
(pv-map (make-array pv-size :initial-element nil)))
304
(let ((map-index 1)(param-index 0))
305
(declare (fixnum map-index param-index))
306
(dolist (slot-name-list slot-name-lists)
307
(dolist (slot-name (cdr slot-name-list))
308
(let ((a (assoc slot-name new-values)))
309
(setf (svref pv-map map-index)
310
(and a (cons param-index (cdr a)))))
314
(map-cache #'(lambda (wrappers pv-cell)
316
(update-slots-in-pv wrappers (car pv-cell)
317
cwrapper pv-size pv-map)))
320
(defun update-slots-in-pv (wrappers pv cwrapper pv-size pv-map)
321
(if (not (if (atom wrappers)
322
(eq cwrapper wrappers)
323
(dolist (wrapper wrappers nil)
324
(when (eq wrapper cwrapper)
327
(let* ((old-intern-p (listp (pvref pv 0)))
328
(new-pv (if old-intern-p
334
(when (consp (let ((map (svref pv-map i)))
336
(setf (pvref new-pv i) (cdr map))
338
(setq new-intern-p nil)))
340
(declare (fixnum param))
341
(dolist (wrapper wrappers)
342
(when (eq wrapper cwrapper)
344
(when (consp (let ((map (svref pv-map i)))
345
(if (and map (= (car map) param))
346
(setf (pvref new-pv i) (cdr map))
348
(setq new-intern-p nil))))
351
(setq new-pv (let ((list-pv (coerce pv 'list)))
352
(or (gethash (cdr list-pv) *pvs*)
353
(setf (gethash (cdr list-pv) *pvs*)
356
(make-permutation-vector list-pv)))))))
360
(defun maybe-expand-accessor-form (form required-parameters slots env)
361
(let* ((fname (car form))
362
#||(len (length form))||#
363
(gf (if (symbolp fname)
364
(unencapsulated-fdefinition fname)
365
(gdefinition fname))))
366
(macrolet ((maybe-optimize-reader ()
368
(can-optimize-access1 (cadr form)
369
required-parameters env)))
371
(optimize-reader slots parameter gf-name form))))
372
(maybe-optimize-writer ()
374
(can-optimize-access1 (caddr form)
375
required-parameters env)))
377
(optimize-writer slots parameter gf-name form)))))
378
(unless (and (consp (cadr form))
379
(eq 'instance-accessor-parameter (caadr form)))
381
(cond ((and (= len 2) (symbolp fname))
382
(let ((gf-name (gethash fname *gf-declared-reader-table*)))
384
(maybe-optimize-reader))))
386
(let ((gf-name (gethash fname *gf-declared-writer-table*)))
388
(maybe-optimize-writer)))))
390
(when (and (eq *boot-state* 'complete)
391
(generic-function-p gf))
392
(let ((methods (generic-function-methods gf)))
394
(let* ((gf-name (generic-function-name gf))
395
(arg-info (gf-arg-info gf))
396
(metatypes (arg-info-metatypes arg-info))
397
(nreq (length metatypes))
398
(applyp (arg-info-applyp arg-info)))
401
(when (some #'standard-reader-method-p methods)
402
(maybe-optimize-reader)))
405
(eq (car gf-name) 'setf))
406
(when (some #'standard-writer-method-p methods)
407
(maybe-optimize-writer))))))))))))))
409
(defun optimize-generic-function-call (form required-parameters env slots calls)
410
(declare (ignore required-parameters env slots calls))
411
(or (and (eq (car form) 'make-instance)
412
(expand-make-instance-form form))
414
(maybe-expand-accessor-form form required-parameters slots env)
415
(let* ((fname (car form))
417
(gf (if (symbolp fname)
419
(unencapsulated-fdefinition fname))
421
(gdefinition fname))))
422
(gf-name (and (fsc-instance-p gf)
425
(generic-function-name gf)))))
427
(multiple-value-bind (nreq restp)
428
(get-generic-function-info gf)
429
(optimize-gf-call slots calls form nreq restp env))))
435
(defun can-optimize-access (form required-parameters env)
436
(let ((type (ecase (car form)
438
(set-slot-value 'writer)
439
(slot-boundp 'boundp)))
441
(slot-name (eval (caddr form)))) ; known to be constant
442
(can-optimize-access1 var required-parameters env type slot-name)))
444
(defun can-optimize-access1 (var required-parameters env &optional type slot-name)
445
(when (and (consp var) (eq 'the (car var)))
446
(setq var (caddr var)))
448
(let* ((rebound? (caddr (variable-declaration 'variable-rebinding var env)))
449
(parameter-or-nil (car (memq (or rebound? var) required-parameters))))
450
(when parameter-or-nil
451
(let* ((class-name (caddr (variable-declaration
452
'class parameter-or-nil env)))
453
(class (find-class class-name nil)))
454
(when (or (not (eq *boot-state* 'complete))
455
(and class (not (class-finalized-p class))))
457
(when (and class-name (not (eq class-name 't)))
458
(when (or (null type)
460
(memq *the-class-structure-object*
461
(class-precedence-list class))))
462
(optimize-slot-value-by-class-p class slot-name type))
463
(cons parameter-or-nil (or class class-name)))))))))
465
(defun optimize-slot-value (slots sparameter form)
467
; FIXME use regular destructuring-bind
468
(pcl-destructuring-bind (ignore ignore slot-name-form) form
469
(let ((slot-name (eval slot-name-form)))
470
(optimize-instance-access slots :read sparameter slot-name nil)))
471
`(accessor-slot-value ,@(cdr form))))
473
(defun optimize-set-slot-value (slots sparameter form)
475
; FIXME use regular destructuring-bind
476
(pcl-destructuring-bind (ignore ignore slot-name-form new-value) form
477
(let ((slot-name (eval slot-name-form)))
478
(optimize-instance-access slots :write sparameter slot-name new-value)))
479
`(accessor-set-slot-value ,@(cdr form))))
481
(defun optimize-slot-boundp (slots sparameter form)
483
; FIXME use regular destructuring-bind
484
(pcl-destructuring-bind (ignore ignore slot-name-form new-value) form
485
(let ((slot-name (eval slot-name-form)))
486
(optimize-instance-access slots :boundp sparameter slot-name new-value)))
487
`(accessor-slot-boundp ,@(cdr form))))
489
(defun optimize-reader (slots sparameter gf-name form)
491
(optimize-accessor-call slots :read sparameter gf-name nil)
494
(defun optimize-writer (slots sparameter gf-name form)
496
; FIXME use regular destructuring-bind
497
(pcl-destructuring-bind (ignore ignore new-value) form
498
(optimize-accessor-call slots :write sparameter gf-name new-value))
501
;;; The <slots> argument is an alist, the CAR of each entry is the name of
502
;;; a required parameter to the function. The alist is in order, so the
503
;;; position of an entry in the alist corresponds to the argument's position
504
;;; in the lambda list.
506
(defun optimize-instance-access (slots read/write sparameter slot-name new-value)
507
(let ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
508
(parameter (if (consp sparameter) (car sparameter) sparameter)))
509
(if (and (eq *boot-state* 'complete)
511
(memq *the-class-structure-object* (class-precedence-list class)))
512
(let ((slotd (find-slot-definition class slot-name)))
515
`(,(slot-definition-defstruct-accessor-symbol slotd) ,parameter))
517
`(setf (,(slot-definition-defstruct-accessor-symbol slotd) ,parameter)
521
(let* ((parameter-entry (assq parameter slots))
522
(slot-entry (assq slot-name (cdr parameter-entry)))
523
(position (posq parameter-entry slots))
524
(pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
525
(unless parameter-entry
526
(error "Internal error in slot optimization."))
528
(setq slot-entry (list slot-name))
529
(push slot-entry (cdr parameter-entry)))
530
(push pv-offset-form (cdr slot-entry))
533
`(instance-read ,pv-offset-form ,parameter ,position
534
',slot-name ',class))
536
`(let ((.new-value. ,new-value))
537
(instance-write ,pv-offset-form ,parameter ,position
538
',slot-name ',class .new-value.)))
540
`(instance-boundp ,pv-offset-form ,parameter ,position
541
',slot-name ',class)))))))
543
(defun optimize-accessor-call (slots read/write sparameter gf-name new-value)
544
(let* ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
545
(parameter (if (consp sparameter) (car sparameter) sparameter))
546
(parameter-entry (assq parameter slots))
547
(name (case read/write
548
(:read `(reader ,gf-name))
549
(:write `(writer ,gf-name))))
550
(slot-entry (assoc name (cdr parameter-entry) :test #'equal))
551
(position (posq parameter-entry slots))
552
(pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
553
(unless parameter-entry
554
(error "Internal error in slot optimization."))
556
(setq slot-entry (list name))
557
(push slot-entry (cdr parameter-entry)))
558
(push pv-offset-form (cdr slot-entry))
561
`(instance-reader ,pv-offset-form ,parameter ,position ,gf-name ',class))
563
`(let ((.new-value. ,new-value))
564
(instance-writer ,pv-offset-form ,parameter ,position ,gf-name ',class
567
(defvar *unspecific-arg* '..unspecific-arg..)
569
(defun optimize-gf-call-internal (form slots env)
570
(when (and (consp form)
571
(eq (car form) 'the))
572
(setq form (caddr form)))
573
(or (and (symbolp form)
574
(let* ((rebound? (caddr (variable-declaration 'variable-rebinding
576
(parameter-or-nil (car (assq (or rebound? form) slots))))
577
(when parameter-or-nil
578
(let* ((class-name (caddr (variable-declaration
579
'class parameter-or-nil env))))
580
(when (and class-name (not (eq class-name 't)))
581
(position parameter-or-nil slots :key #'car))))))
583
(let ((form (eval form)))
589
(defun optimize-gf-call (slots calls gf-call-form nreq restp env)
590
(unless (eq (car gf-call-form) 'make-instance) ; needs more work
591
(let* ((args (cdr gf-call-form))
592
(all-args-p (eq (car gf-call-form) 'make-instance))
593
(non-required-args (nthcdr nreq args))
594
(required-args (ldiff args non-required-args))
595
(call-spec (list (car gf-call-form) nreq restp
596
(mapcar #'(lambda (form)
597
(optimize-gf-call-internal form slots env))
601
(call-entry (assoc call-spec calls :test #'equal))
602
(pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
603
(unless (some #'integerp
604
(let ((spec-args (cdr call-spec)))
606
(ldiff spec-args (nthcdr nreq spec-args))
608
(return-from optimize-gf-call nil))
610
(setq call-entry (list call-spec))
611
(push call-entry (cdr calls)))
612
(push pv-offset-form (cdr call-entry))
613
(if (eq (car call-spec) 'make-instance)
614
`(funcall (pv-ref .pv. ,pv-offset-form) ,@(cdr gf-call-form))
615
`(let ((.emf. (pv-ref .pv. ,pv-offset-form)))
616
(invoke-effective-method-function .emf. ,restp
617
,@required-args ,@(when restp `((list ,@non-required-args)))))))))
620
(define-walker-template pv-offset) ; These forms get munged by mutate slots.
621
(defmacro pv-offset (arg) arg)
622
(define-walker-template instance-accessor-parameter)
623
(defmacro instance-accessor-parameter (x) x)
625
;; It is safe for these two functions to be wrong.
626
;; They just try to guess what the most likely case will be.
627
(defun generate-fast-class-slot-access-p (class-form slot-name-form)
628
(let ((class (and (constantp class-form) (eval class-form)))
629
(slot-name (and (constantp slot-name-form) (eval slot-name-form))))
630
(and (eq *boot-state* 'complete)
631
(standard-class-p class)
632
(not (eq class *the-class-t*)) ; shouldn't happen, though.
633
(let ((slotd (find-slot-definition class slot-name)))
634
(and slotd (classp (slot-definition-allocation slotd)))))))
636
(defun skip-fast-slot-access-p (class-form slot-name-form type)
637
(let ((class (and (constantp class-form) (eval class-form)))
638
(slot-name (and (constantp slot-name-form) (eval slot-name-form))))
639
(and (eq *boot-state* 'complete)
640
(standard-class-p class)
641
(not (eq class *the-class-t*)) ; shouldn't happen, though.
642
(let ((slotd (find-slot-definition class slot-name)))
643
(and slotd (skip-optimize-slot-value-by-class-p class slot-name type))))))
645
(defun skip-optimize-slot-value-by-class-p (class slot-name type)
646
(let ((slotd (find-slot-definition class slot-name)))
648
(eq *boot-state* 'complete)
649
(not (slot-accessor-std-p slotd type)))))
651
(defmacro instance-read-internal (pv slots pv-offset default &optional type)
652
(unless (member type '(nil :instance :class :default))
653
(error "Illegal type argument to ~S: ~S" 'instance-read-internal type))
654
(if (eq type ':default)
656
(let* ((index (gensym))
658
`(locally (declare #.*optimize-speed*)
659
(let ((,index (pvref ,pv ,pv-offset)))
660
(setq ,value (typecase ,index
661
,@(when (or (null type) (eq type ':instance))
662
`((fixnum (%instance-ref ,slots ,index))))
663
,@(when (or (null type) (eq type ':class))
664
`((cons (cdr ,index))))
665
(t ',*slot-unbound*)))
666
(if (eq ,value ',*slot-unbound*)
670
(defmacro instance-read (pv-offset parameter position slot-name class)
671
(if (skip-fast-slot-access-p class slot-name 'reader)
672
`(accessor-slot-value ,parameter ,slot-name)
673
`(instance-read-internal .pv. ,(slot-vector-symbol position)
674
,pv-offset (accessor-slot-value ,parameter ,slot-name)
675
,(if (generate-fast-class-slot-access-p class slot-name)
676
':class ':instance))))
678
(defmacro instance-reader (pv-offset parameter position gf-name class)
679
(declare (ignore class))
680
`(instance-read-internal .pv. ,(slot-vector-symbol position)
682
(,gf-name (instance-accessor-parameter ,parameter))
685
(defmacro instance-write-internal (pv slots pv-offset new-value default
687
(unless (member type '(nil :instance :class :default))
688
(error "Illegal type argument to ~S: ~S" 'instance-write-internal type))
689
(if (eq type ':default)
691
(let* ((index (gensym)))
692
`(locally (declare #.*optimize-speed*)
693
(let ((,index (pvref ,pv ,pv-offset)))
695
,@(when (or (null type) (eq type ':instance))
696
`((fixnum (setf (%instance-ref ,slots ,index) ,new-value))))
697
,@(when (or (null type) (eq type ':class))
698
`((cons (setf (cdr ,index) ,new-value))))
701
(defmacro instance-write (pv-offset parameter position slot-name class new-value)
702
(if (skip-fast-slot-access-p class slot-name 'writer)
703
`(accessor-set-slot-value ,parameter ,slot-name ,new-value)
704
`(instance-write-internal .pv. ,(slot-vector-symbol position)
705
,pv-offset ,new-value
706
(accessor-set-slot-value ,parameter ,slot-name ,new-value)
707
,(if (generate-fast-class-slot-access-p class slot-name)
708
':class ':instance))))
710
(defmacro instance-writer (pv-offset parameter position gf-name class new-value)
711
(declare (ignore class))
712
`(instance-write-internal .pv. ,(slot-vector-symbol position)
713
,pv-offset ,new-value
714
(,(if (consp gf-name)
715
(get-setf-function-name gf-name)
717
(instance-accessor-parameter ,parameter)
721
(defmacro instance-boundp-internal (pv slots pv-offset default
723
(unless (member type '(nil :instance :class :default))
724
(error "Illegal type argument to ~S: ~S" 'instance-boundp-internal type))
725
(if (eq type ':default)
727
(let* ((index (gensym)))
728
`(locally (declare #.*optimize-speed*)
729
(let ((,index (pvref ,pv ,pv-offset)))
731
,@(when (or (null type) (eq type ':instance))
732
`((fixnum (not (eq (%instance-ref ,slots ,index) ',*slot-unbound*)))))
733
,@(when (or (null type) (eq type ':class))
734
`((cons (not (eq (cdr ,index) ',*slot-unbound*)))))
737
(defmacro instance-boundp (pv-offset parameter position slot-name class)
738
(if (skip-fast-slot-access-p class slot-name 'boundp)
739
`(accessor-slot-boundp ,parameter ,slot-name)
740
`(instance-boundp-internal .pv. ,(slot-vector-symbol position)
741
,pv-offset (accessor-slot-boundp ,parameter ,slot-name)
742
,(if (generate-fast-class-slot-access-p class slot-name)
743
':class ':instance))))
746
;;; This magic function has quite a job to do indeed.
748
;;; The careful reader will recall that <slots> contains all of the optimized
749
;;; slot access forms produced by OPTIMIZE-INSTANCE-ACCESS. Each of these is
750
;;; a call to either INSTANCE-READ or INSTANCE-WRITE.
752
;;; At the time these calls were produced, the first argument was specified as
753
;;; the symbol .PV-OFFSET.; what we have to do now is convert those pv-offset
754
;;; arguments into the actual number that is the correct offset into the pv.
756
;;; But first, oh but first, we sort <slots> a bit so that for each argument
757
;;; we have the slots in alphabetical order. This canonicalizes the PV-TABLE's a
758
;;; bit and will hopefully lead to having fewer PV's floating around. Even
759
;;; if the gain is only modest, it costs nothing.
761
(defun slot-name-lists-from-slots (slots calls)
762
(multiple-value-bind (slots calls)
763
(mutate-slots-and-calls slots calls)
764
(let* ((slot-name-lists
765
(mapcar #'(lambda (parameter-entry)
766
(cons nil (mapcar #'car (cdr parameter-entry))))
769
(mapcar #'car calls)))
770
(dolist (call call-list)
771
(dolist (arg (cdr call))
773
(setf (car (nth arg slot-name-lists)) t))))
774
(setq slot-name-lists (mapcar #'(lambda (r+snl)
775
(when (or (car r+snl) (cdr r+snl))
778
(let ((cvt (apply #'vector
781
(mapcar #'(lambda (r+snl)
782
(when r+snl (incf i)))
784
(setq call-list (mapcar #'(lambda (call)
786
(mapcar #'(lambda (arg)
792
(values slot-name-lists call-list))))
794
(defun mutate-slots-and-calls (slots calls)
795
(let ((sorted-slots (sort-slots slots))
796
(sorted-calls (sort-calls (cdr calls)))
797
(pv-offset 0)) ; index 0 is for info
798
(declare (fixnum pv-offset))
799
(dolist (parameter-entry sorted-slots)
800
(dolist (slot-entry (cdr parameter-entry))
802
(dolist (form (cdr slot-entry))
803
(setf (cadr form) pv-offset))))
804
(dolist (call-entry sorted-calls)
806
(dolist (form (cdr call-entry))
807
(setf (cadr form) pv-offset)))
808
(values sorted-slots sorted-calls)))
810
(defun symbol-pkg-name (sym)
811
(let ((pkg (symbol-package sym)))
812
(if pkg (package-name pkg) "")))
814
(defun symbol-lessp (a b)
815
(if (eq (symbol-package a)
817
(string-lessp (symbol-name a)
819
(string-lessp (symbol-pkg-name a)
820
(symbol-pkg-name b))))
822
(defun symbol-or-cons-lessp (a b)
825
(symbol (symbol-lessp a b))
829
(cons (if (eq (car a) (car b))
830
(symbol-or-cons-lessp (cdr a) (cdr b))
831
(symbol-or-cons-lessp (car a) (car b))))))))
833
(defun sort-slots (slots)
834
(mapcar #'(lambda (parameter-entry)
835
(cons (car parameter-entry)
836
(sort (cdr parameter-entry) ;slot entries
837
#'symbol-or-cons-lessp
841
(defun sort-calls (calls)
842
(sort calls #'symbol-or-cons-lessp :key #'car))
846
;;; This needs to work in terms of metatypes and also needs to work for
847
;;; automatically generated reader and writer functions.
848
;;; -- Automatically generated reader and writer functions use this stuff too.
850
(defmacro pv-binding ((required-parameters slot-name-lists pv-table-symbol)
852
(with-gathering ((slot-vars (collecting))
853
(pv-parameters (collecting)))
854
(iterate ((slots (list-elements slot-name-lists))
855
(required-parameter (list-elements required-parameters))
856
(i (interval :from 0)))
858
(gather required-parameter pv-parameters)
859
(gather (slot-vector-symbol i) slot-vars)))
860
`(pv-binding1 (.pv. .calls. ,pv-table-symbol ,pv-parameters ,slot-vars)
863
(defmacro pv-binding1 ((pv calls pv-table-symbol pv-parameters slot-vars)
865
`(pv-env (,pv ,calls ,pv-table-symbol ,pv-parameters)
866
(let (,@(mapcar #'(lambda (slot-var p) `(,slot-var (get-slots-or-nil ,p)))
867
slot-vars pv-parameters))
870
;This gets used only when the default make-method-lambda is overriden.
871
(defmacro pv-env ((pv calls pv-table-symbol pv-parameters)
873
`(let* ((.pv-table. ,pv-table-symbol)
874
(.pv-cell. (pv-table-lookup-pv-args .pv-table. ,@pv-parameters))
875
(,pv (car .pv-cell.))
876
(,calls (cdr .pv-cell.)))
877
(declare ,(make-pv-type-declaration pv))
878
(declare ,(make-calls-type-declaration calls))
879
,@(when (symbolp pv-table-symbol)
880
`((declare (special ,pv-table-symbol))))
883
#+cmu `(declare (ignorable ,pv ,calls)))
886
(defvar *non-variable-declarations*
887
'(method-name method-lambda-list
888
optimize ftype inline notinline))
890
(defvar *variable-declarations-with-argument*
894
(defvar *variable-declarations-without-argument*
895
'(ignore ignorable special dynamic-extent
896
array atom base-char bignum bit bit-vector character common compiled-function
897
complex cons double-float extended-char fixnum float function hash-table integer
898
keyword list long-float nil null number package pathname random-state ratio
899
rational readtable sequence short-float signed-byte simple-array
900
simple-bit-vector simple-string simple-vector single-float standard-char
901
stream string-char symbol t unsigned-byte vector))
903
(defun split-declarations (body args)
904
(let ((inner-decls nil) (outer-decls nil) decl)
905
(loop (when (null body) (return nil))
906
(setq decl (car body))
907
(unless (and (consp decl)
908
(eq (car decl) 'declare))
910
(dolist (form (cdr decl))
912
(let ((declaration-name (car form)))
913
(if (member declaration-name *non-variable-declarations*)
914
(push `(declare ,form) outer-decls)
916
(member declaration-name
917
*variable-declarations-with-argument*))
919
(member declaration-name
920
*variable-declarations-without-argument*))
921
(dname (list (pop form)))
922
(inners nil) (outers nil))
923
(unless (or arg-p non-arg-p)
924
(warn "The declaration ~S is not understood by ~S.~@
925
Please put ~S on one of the lists ~S,~%~S, or~%~S.~@
926
(Assuming it is a variable declarations without argument)."
927
declaration-name 'split-declarations
929
'*non-variable-declarations*
930
'*variable-declarations-with-argument*
931
'*variable-declarations-without-argument*)
932
(push declaration-name
933
*variable-declarations-without-argument*))
935
(setq dname (append dname (list (pop form)))))
937
(if (member var args)
941
(push `(declare (,@dname ,@outers)) outer-decls))
943
(push `(declare (,@dname ,@inners)) inner-decls)))))))
944
(setq body (cdr body)))
945
(values outer-decls inner-decls body)))
947
(defun make-method-initargs-form-internal (method-lambda initargs env)
948
(declare (ignore env))
949
(let (method-lambda-args lmf lmf-params)
950
(if (not (and (= 3 (length method-lambda))
951
(= 2 (length (setq method-lambda-args (cadr method-lambda))))
952
(consp (setq lmf (third method-lambda)))
953
(eq 'simple-lexical-method-functions (car lmf))
954
(eq (car method-lambda-args) (cadr (setq lmf-params (cadr lmf))))
955
(eq (cadr method-lambda-args) (caddr lmf-params))))
956
`(list* :function #',method-lambda
958
(let* ((lambda-list (car lmf-params))
959
(nreq 0)(restp nil)(args nil))
960
(dolist (arg lambda-list)
961
(when (member arg '(&optional &rest &key))
962
(setq restp t)(return nil))
963
(when (eq arg '&aux) (return nil))
964
(incf nreq)(push arg args))
965
(setq args (nreverse args))
966
(setf (getf (getf initargs ':plist) ':arg-info) (cons nreq restp))
967
(make-method-initargs-form-internal1
968
initargs (cddr lmf) args lmf-params restp)))))
970
(defun make-method-initargs-form-internal1
971
(initargs body req-args lmf-params restp)
972
(multiple-value-bind (outer-decls inner-decls body)
973
(split-declarations body req-args)
974
(let* ((rest-arg (when restp '.rest-arg.))
975
(args+rest-arg (if restp (append req-args (list rest-arg)) req-args)))
976
`(list* :fast-function
977
#'(lambda (.pv-cell. .next-method-call. ,@args+rest-arg)
979
.pv-cell. .next-method-call.
980
(macrolet ((pv-env ((pv calls pv-table-symbol pv-parameters)
982
(declare (ignore pv-table-symbol pv-parameters))
983
`(let ((,pv (car .pv-cell.))
984
(,calls (cdr .pv-cell.)))
985
(declare ,(make-pv-type-declaration pv)
986
,(make-calls-type-declaration calls))
989
(fast-lexical-method-functions
990
(,(car lmf-params) .next-method-call. ,req-args ,rest-arg
991
,@(cdddr lmf-params))
996
;use arrays and hash tables and the fngen stuff to make this much better.
997
;It doesn't really matter, though, because a function returned by this
998
;will get called only when the user explicitly funcalls a result of method-function.
999
;BUT, this is needed to make early methods work.
1000
(defun method-function-from-fast-function (fmf)
1001
(declare (type function fmf))
1002
(let* ((method-function nil) (pv-table nil)
1003
(arg-info (method-function-get fmf ':arg-info))
1004
(nreq (car arg-info))
1005
(restp (cdr arg-info)))
1006
(setq method-function
1007
#'(lambda (method-args next-methods)
1009
(setq pv-table (method-function-pv-table fmf)))
1010
(let* ((pv-cell (when pv-table
1011
(get-method-function-pv-cell
1012
method-function method-args pv-table)))
1013
(nm (car next-methods))
1014
(nms (cdr next-methods))
1016
(make-method-call :function (if (std-instance-p nm)
1017
(method-function nm)
1019
:call-method-args (list nms)))))
1021
(let* ((rest (nthcdr nreq method-args))
1022
(args (ldiff method-args rest)))
1023
(apply fmf pv-cell nmc (nconc args (list rest))))
1024
(apply fmf pv-cell nmc method-args)))))
1025
(let* ((fname (method-function-get fmf :name))
1026
(name `(,(or (get (car fname) 'method-sym)
1027
(setf (get (car fname) 'method-sym)
1028
(let ((str (symbol-name (car fname))))
1029
(if (string= "FAST-" str :end2 5)
1030
(intern (subseq str 5) *the-pcl-package*)
1033
(set-function-name method-function name))
1034
(setf (method-function-get method-function :fast-function) fmf)
1037
(defun get-method-function-pv-cell (method-function method-args &optional pv-table)
1038
(let ((pv-table (or pv-table (method-function-pv-table method-function))))
1040
(let ((pv-wrappers (pv-wrappers-from-all-args pv-table method-args)))
1042
(pv-table-lookup pv-table pv-wrappers))))))
1044
(defun pv-table-lookup-pv-args (pv-table &rest pv-parameters)
1045
(pv-table-lookup pv-table (pv-wrappers-from-pv-args pv-parameters)))
1047
(defun pv-wrappers-from-pv-args (&rest args)
1048
(let* ((nkeys (length args))
1049
(pv-wrappers (make-list nkeys))
1050
w (w-t pv-wrappers))
1051
(declare (fixnum nkeys))
1054
#+cmu17 (wrapper-of arg)
1056
(cond ((std-instance-p arg)
1057
(std-instance-wrapper arg))
1058
((fsc-instance-p arg)
1059
(fsc-instance-wrapper arg))
1062
(built-in-wrapper-of arg)
1064
(built-in-or-structure-wrapper arg))))
1065
(unless (eq 't (wrapper-state w))
1066
(setq w (check-wrapper-validity arg)))
1068
(setq w-t (cdr w-t))
1069
(when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))
1072
(defun pv-wrappers-from-all-args (pv-table args)
1074
(slot-name-lists (pv-table-slot-name-lists pv-table)))
1075
(declare (fixnum nkeys))
1076
(dolist (sn slot-name-lists)
1077
(when sn (incf nkeys)))
1078
(let* ((pv-wrappers (make-list nkeys))
1079
(pv-w-t pv-wrappers))
1080
(dolist (sn slot-name-lists)
1082
(let* ((arg (car args))
1083
(w (wrapper-of arg)))
1084
(unless w ; can-optimize-access prevents this from happening.
1085
(error "error in pv-wrappers-from-all-args"))
1086
(setf (car pv-w-t) w)
1087
(setq pv-w-t (cdr pv-w-t))))
1088
(setq args (cdr args)))
1089
(when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))
1092
(defun pv-wrappers-from-all-wrappers (pv-table wrappers)
1094
(slot-name-lists (pv-table-slot-name-lists pv-table)))
1095
(declare (fixnum nkeys))
1096
(dolist (sn slot-name-lists)
1097
(when sn (incf nkeys)))
1098
(let* ((pv-wrappers (make-list nkeys))
1099
(pv-w-t pv-wrappers))
1100
(dolist (sn slot-name-lists)
1102
(let ((w (car wrappers)))
1103
(unless w ; can-optimize-access prevents this from happening.
1104
(error "error in pv-wrappers-from-all-wrappers"))
1105
(setf (car pv-w-t) w)
1106
(setq pv-w-t (cdr pv-w-t))))
1107
(setq wrappers (cdr wrappers)))
1108
(when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))