1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; This software is derived from software originally released by Xerox
5 ;;;; Corporation. Copyright and release statements follow. Later modifications
6 ;;;; to the software are in the public domain and are provided with
7 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
10 ;;;; copyright information from original PCL sources:
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
15 ;;;; Use and copying of this software and preparation of derivative works based
16 ;;;; upon this software are permitted. Any distribution of this software or
17 ;;;; derivative works must comply with all applicable United States export
20 ;;;; This software is made available AS IS, and Xerox Corporation makes no
21 ;;;; warranty about the software, its performance or its conformity to any
27 ;;;; some support stuff for getting a hold of symbols that we need when
28 ;;;; building the discriminator codes. It's OK for these to be interned
29 ;;;; symbols because we don't capture any user code in the scope in which
30 ;;;; these symbols are bound.
32 (declaim (list *dfun-arg-symbols*))
33 (defvar *dfun-arg-symbols* '(.ARG0. .ARG1. .ARG2. .ARG3.))
35 (defun dfun-arg-symbol (arg-number)
36 (or (nth arg-number *dfun-arg-symbols*)
37 (format-symbol *pcl-package* ".ARG~A." arg-number)))
39 (declaim (list *slot-vector-symbols*))
40 (defvar *slot-vector-symbols* '(.SLOTS0. .SLOTS1. .SLOTS2. .SLOTS3.))
42 (defun slot-vector-symbol (arg-number)
43 (or (nth arg-number *slot-vector-symbols*)
44 (format-symbol *pcl-package* ".SLOTS~A." arg-number)))
46 (declaim (inline make-dfun-required-args))
47 (defun make-dfun-required-args (count)
48 (declare (type index count))
50 (dotimes (i count (nreverse result))
51 (push (dfun-arg-symbol i) result))))
53 (defun make-dfun-lambda-list (nargs applyp)
54 (let ((required (make-dfun-required-args nargs)))
57 ;; Use &MORE arguments to avoid consing up an &REST list
58 ;; that we might not need at all. See MAKE-EMF-CALL and
59 ;; INVOKE-EFFECTIVE-METHOD-FUNCTION for the other
61 '(&more .dfun-more-context. .dfun-more-count.))
64 (defun make-dlap-lambda-list (nargs applyp)
65 (let* ((required (make-dfun-required-args nargs))
66 (lambda-list (if applyp
67 (append required '(&more .more-context. .more-count.))
69 ;; Return the full lambda list, the required arguments, a form
70 ;; that will generate a rest-list, and a list of the &MORE
75 '((sb-c::%listify-rest-args
77 (the (and unsigned-byte fixnum)
80 '(.more-context. .more-count.)))))
82 (defun make-emf-call (nargs applyp fn-variable &optional emf-type)
83 (let ((required (make-dfun-required-args nargs)))
84 `(,(if (eq emf-type 'fast-method-call)
85 'invoke-effective-method-function-fast
86 'invoke-effective-method-function)
89 :required-args ,required
90 ;; INVOKE-EFFECTIVE-METHOD-FUNCTION will decide whether to use
91 ;; the :REST-ARG version or the :MORE-ARG version depending on
92 ;; the type of the EMF.
94 ;; Creates a list from the &MORE arguments.
95 '((sb-c::%listify-rest-args
97 (the (and unsigned-byte fixnum)
100 :more-arg ,(when applyp
101 '(.dfun-more-context. .dfun-more-count.)))))
103 (defun make-fast-method-call-lambda-list (nargs applyp)
104 (list* '.pv-cell. '.next-method-call. (make-dfun-lambda-list nargs applyp)))
106 ;;; Emitting various accessors.
108 (defun emit-one-class-reader (class-slot-p)
109 (emit-reader/writer :reader 1 class-slot-p))
111 (defun emit-one-class-boundp (class-slot-p)
112 (emit-reader/writer :boundp 1 class-slot-p))
114 (defun emit-one-class-writer (class-slot-p)
115 (emit-reader/writer :writer 1 class-slot-p))
117 (defun emit-two-class-reader (class-slot-p)
118 (emit-reader/writer :reader 2 class-slot-p))
120 (defun emit-two-class-boundp (class-slot-p)
121 (emit-reader/writer :boundp 2 class-slot-p))
123 (defun emit-two-class-writer (class-slot-p)
124 (emit-reader/writer :writer 2 class-slot-p))
126 ;;; --------------------------------
128 (defun emit-one-index-readers (class-slot-p)
129 (emit-one-or-n-index-reader/writer :reader nil class-slot-p))
131 (defun emit-one-index-boundps (class-slot-p)
132 (emit-one-or-n-index-reader/writer :boundp nil class-slot-p))
134 (defun emit-one-index-writers (class-slot-p)
135 (emit-one-or-n-index-reader/writer :writer nil class-slot-p))
137 (defun emit-n-n-readers ()
138 (emit-one-or-n-index-reader/writer :reader t nil))
140 (defun emit-n-n-boundps ()
141 (emit-one-or-n-index-reader/writer :boundp t nil))
143 (defun emit-n-n-writers ()
144 (emit-one-or-n-index-reader/writer :writer t nil))
146 ;;; --------------------------------
148 (defun emit-checking (metatypes applyp)
149 (emit-checking-or-caching nil nil metatypes applyp))
151 (defun emit-caching (metatypes applyp)
152 (emit-checking-or-caching t nil metatypes applyp))
154 (defun emit-in-checking-cache-p (metatypes)
155 (emit-checking-or-caching nil t metatypes nil))
157 (defun emit-constant-value (metatypes)
158 (emit-checking-or-caching t t metatypes nil))
160 ;;; --------------------------------
162 ;;; FIXME: What do these variables mean?
163 (defvar *precompiling-lap* nil)
165 (defun emit-default-only (metatypes applyp)
166 (multiple-value-bind (lambda-list args rest-arg more-arg)
167 (make-dlap-lambda-list (length metatypes) applyp)
168 (generating-lisp '(emf)
170 `(invoke-effective-method-function emf
174 :rest-arg ,rest-arg))))
176 ;;; --------------------------------
178 (defun generating-lisp (closure-variables args form)
179 (let ((lambda `(lambda ,closure-variables
180 ,@(when (member 'miss-fn closure-variables)
181 `((declare (type function miss-fn))))
184 (declare #.*optimize-speed*)
186 (values (if *precompiling-lap*
188 (compile nil lambda))
191 ;;; note on implementation for CMU 17 and later (including SBCL):
192 ;;; Since STD-INSTANCE-P is weakened, that branch may run on non-PCL
193 ;;; instances (structures). The result will be the non-wrapper layout
194 ;;; for the structure, which will cause a miss. The "slots" will be
195 ;;; whatever the first slot is, but will be ignored. Similarly,
196 ;;; FSC-INSTANCE-P returns true on funcallable structures as well as
198 (defun emit-reader/writer (reader/writer 1-or-2-class class-slot-p)
201 (closure-variables ())
202 (read-form (emit-slot-read-form class-slot-p 'index 'slots)))
205 (setq instance (dfun-arg-symbol 0)
206 arglist (list instance)))
207 (:writer (setq instance (dfun-arg-symbol 1)
208 arglist (list (dfun-arg-symbol 0) instance))))
210 (1 (setq closure-variables '(wrapper-0 index miss-fn)))
211 (2 (setq closure-variables '(wrapper-0 wrapper-1 index miss-fn))))
215 `(let* (,@(unless class-slot-p `((slots nil)))
216 (wrapper (cond ((std-instance-p ,instance)
217 ,@(unless class-slot-p
219 (std-instance-slots ,instance))))
220 (std-instance-wrapper ,instance))
221 ((fsc-instance-p ,instance)
222 ,@(unless class-slot-p
224 (fsc-instance-slots ,instance))))
225 (fsc-instance-wrapper ,instance)))))
228 (not (zerop (layout-clos-hash wrapper)))
229 ,@(if (eql 1 1-or-2-class)
230 `((eq wrapper wrapper-0))
231 `((or (eq wrapper wrapper-0)
232 (eq wrapper wrapper-1)))))
233 ,@(ecase reader/writer
235 `((let ((value ,read-form))
236 (unless (eq value +slot-unbound+)
237 (return-from access value)))))
239 `((let ((value ,read-form))
240 (return-from access (not (eq value +slot-unbound+))))))
242 `((return-from access (setf ,read-form ,(car arglist)))))))
243 (funcall miss-fn ,@arglist))))))
245 (defun emit-slot-read-form (class-slot-p index slots)
248 `(clos-slots-ref ,slots ,index)))
250 (defun emit-boundp-check (value-form miss-fn arglist)
251 `(let ((value ,value-form))
252 (if (eq value +slot-unbound+)
253 (funcall ,miss-fn ,@arglist)
256 (defun emit-slot-access (reader/writer class-slot-p slots
257 index miss-fn arglist)
258 (let ((read-form (emit-slot-read-form class-slot-p index slots)))
260 (:reader (emit-boundp-check read-form miss-fn arglist))
261 (:boundp `(not (eq ,read-form +slot-unbound+)))
262 (:writer `(setf ,read-form ,(car arglist))))))
264 (defmacro emit-reader/writer-macro (reader/writer 1-or-2-class class-slot-p)
265 (let ((*precompiling-lap* t))
267 (emit-reader/writer reader/writer 1-or-2-class class-slot-p))))
269 (defun emit-one-or-n-index-reader/writer (reader/writer
272 (multiple-value-bind (arglist metatypes)
275 (values (list (dfun-arg-symbol 0))
276 '(standard-instance)))
277 (:writer (values (list (dfun-arg-symbol 0) (dfun-arg-symbol 1))
278 '(t standard-instance))))
280 `(cache ,@(unless cached-index-p '(index)) miss-fn)
282 `(let (,@(unless class-slot-p '(slots))
283 ,@(when cached-index-p '(index)))
284 ,(emit-dlap 'cache arglist metatypes
285 (emit-slot-access reader/writer class-slot-p
286 'slots 'index 'miss-fn arglist)
287 `(funcall miss-fn ,@arglist)
288 (when cached-index-p 'index)
289 (unless class-slot-p '(slots)))))))
291 (defmacro emit-one-or-n-index-reader/writer-macro
292 (reader/writer cached-index-p class-slot-p)
293 (let ((*precompiling-lap* t))
295 (emit-one-or-n-index-reader/writer reader/writer
299 (defun emit-miss (miss-fn args applyp)
301 `(multiple-value-call ,miss-fn ,@args
302 (sb-c::%more-arg-values .more-context.
305 `(funcall ,miss-fn ,@args)))
307 (defun emit-checking-or-caching (cached-emf-p return-value-p metatypes applyp)
308 (multiple-value-bind (lambda-list args rest-arg more-arg)
309 (make-dlap-lambda-list (length metatypes) applyp)
311 `(cache ,@(unless cached-emf-p '(emf)) miss-fn)
313 `(let (,@(when cached-emf-p '(emf)))
314 ,(emit-dlap 'cache args metatypes
316 (if cached-emf-p 'emf t)
317 `(invoke-effective-method-function
321 :rest-arg ,rest-arg))
322 (emit-miss 'miss-fn args applyp)
323 (when cached-emf-p 'emf))))))
325 (defmacro emit-checking-or-caching-macro (cached-emf-p
329 (let ((*precompiling-lap* t))
331 (emit-checking-or-caching cached-emf-p return-value-p metatypes applyp))))
333 (defun emit-dlap (cache-var args metatypes hit-form miss-form value-var
336 (miss-tag (gensym "MISSED"))
337 (wrapper-bindings (mapcan (lambda (arg mt)
340 `((,(format-symbol *pcl-package*
344 mt arg miss-tag (pop slot-vars))))))
346 (wrapper-vars (mapcar #'car wrapper-bindings)))
347 (declare (fixnum index))
349 (error "Every metatype is T."))
352 (let ,wrapper-bindings
353 ,(emit-cache-lookup cache-var wrapper-vars miss-tag value-var)
356 (return ,miss-form))))
358 (defun emit-fetch-wrapper (metatype argument miss-tag &optional slot)
361 ;; This branch may run on non-pcl instances (structures). The
362 ;; result will be the non-wrapper layout for the structure, which
363 ;; will cause a miss. Since refencing the structure is rather iffy
364 ;; if it should have no slots, or only raw slots, we use FOR-STD-CLASS-P
365 ;; to ensure that we have a wrapper.
367 ;; FIXME: If we unify layouts and wrappers we can use
368 ;; instance-slots-layout instead of for-std-class-p, as if there
369 ;; are no layouts there are no slots to worry about.
370 (with-unique-names (wrapper)
372 ((std-instance-p ,argument)
373 (let ((,wrapper (std-instance-wrapper ,argument)))
375 `((when (layout-for-std-class-p ,wrapper)
376 (setq ,slot (std-instance-slots ,argument)))))
378 ((fsc-instance-p ,argument)
379 (let ((,wrapper (fsc-instance-wrapper ,argument)))
381 `((when (layout-for-std-class-p ,wrapper)
382 (setq ,slot (fsc-instance-slots ,argument)))))
384 (t (go ,miss-tag)))))
385 ;; Sep92 PCL used to distinguish between some of these cases (and
386 ;; spuriously exclude others). Since in SBCL
387 ;; WRAPPER-OF/LAYOUT-OF/BUILT-IN-OR-STRUCTURE-WRAPPER are all
388 ;; equivalent and inlined to each other, we can collapse some
389 ;; spurious differences.
390 ((class built-in-instance structure-instance condition-instance)
392 (bug "SLOT requested for metatype ~S, but it isnt' going to happen."
394 `(wrapper-of ,argument))
395 ;; a metatype of NIL should never be seen here, as NIL is only in
396 ;; the metatypes before a generic function is fully initialized.
397 ;; T should never be seen because we never need to get a wrapper
398 ;; to do dispatch if all methods have T as the respective
401 (bug "~@<metatype ~S seen in ~S.~@:>" metatype 'emit-fetch-wrapper))))