3 ;;; Method combination implementation
5 ;;; (c) 2009 Straylight/Edgeware
8 ;;;----- Licensing notice ---------------------------------------------------
10 ;;; This file is part of the Sensble Object Design, an object system for C.
12 ;;; SOD is free software; you can redistribute it and/or modify
13 ;;; it under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 2 of the License, or
15 ;;; (at your option) any later version.
17 ;;; SOD is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with SOD; if not, write to the Free Software Foundation,
24 ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 ;;;--------------------------------------------------------------------------
31 (export 'basic-message)
32 (defclass basic-message (sod-message)
33 ((argument-tail :type list :reader sod-message-argument-tail)
34 (no-varargs-tail :type list :reader sod-message-no-varargs-tail))
36 "Base class for built-in message classes.
38 Provides the basic functionality for the built-in method combinations.
39 This is a separate class so that `special effect' messages can avoid
40 inheriting its default behaviour.
42 The function type protocol is implemented on `basic-message' using slot
43 reader methods. The actual values are computed on demand in methods
44 defined on `slot-unbound'."))
46 (defmethod slot-unbound (class
47 (message basic-message)
48 (slot-name (eql 'argument-tail)))
50 (setf (slot-value message 'argument-tail)
52 (if (or (eq arg :ellipsis) (argument-name arg)) arg
53 (make-argument (make-instance 'temporary-argument
56 (argument-type arg))))
57 (c-function-arguments (sod-message-type message))))))
59 (defmethod slot-unbound (class
60 (message basic-message)
61 (slot-name (eql 'no-varargs-tail)))
62 (setf (slot-value message 'no-varargs-tail)
64 (if (eq arg :ellipsis)
65 (make-argument *sod-ap* (c-type va-list))
67 (sod-message-argument-tail message))))
69 (defmethod sod-message-method-class
70 ((message basic-message) (class sod-class) pset)
71 (let ((role (get-property pset :role :keyword nil)))
73 ((:before :after) 'daemon-direct-method)
74 (:around 'delegating-direct-method)
75 ((nil) (error "How odd: a primary method slipped through the net"))
76 (t (error "Unknown method role ~A" role)))))
78 (export 'simple-message)
79 (defclass simple-message (basic-message)
82 "Base class for messages with `simple' method combinations.
84 A simple method combination is one which has only one method role other
85 than the `before', `after' and `around' methods provided by BASIC-MESSAGE.
86 We call these `primary' methods, and the programmer designates them by not
87 specifying an explicit role.
89 If the programmer doesn't define any primary methods then the effective
90 method is null -- i.e., the method entry pointer shows up as a null
93 (defmethod sod-message-method-class
94 ((message simple-message) (class sod-class) pset)
95 (if (get-property pset :role :keyword nil)
97 (primary-method-class message)))
99 ;;;--------------------------------------------------------------------------
100 ;;; Direct method classes.
102 (export 'basic-direct-method)
103 (defclass basic-direct-method (sod-method)
104 ((role :initarg :role :type symbol :reader sod-method-role)
105 (function-type :type c-function-type :reader sod-method-function-type))
107 "Base class for built-in direct method classes.
109 Provides the basic functionality for the built-in direct-method classes.
110 This is a separate class so that `special effect' methods can avoid
111 inheriting its default behaviour and slots.
113 A basic method can be assigned a `role', which may be set either as an
114 initarg or using the `:role' property. Roles are used for method
117 The function type protocol is implemented on `basic-direct-method' using
118 slot reader methods. The actual values are computed on demand in methods
119 defined on `slot-unbound'."))
121 (defmethod shared-initialize :after
122 ((method basic-direct-method) slot-names &key pset)
123 (declare (ignore slot-names))
124 (default-slot (method 'role) (get-property pset :role :keyword nil)))
126 (defmethod slot-unbound
127 (class (method basic-direct-method) (slot-name (eql 'function-type)))
128 (let ((type (sod-method-type method)))
129 (setf (slot-value method 'function-type)
130 (c-type (fun (lisp (c-type-subtype type))
131 ("me" (* (class (sod-method-class method))))
132 . (c-function-arguments type))))))
134 (defmethod sod-method-function-name ((method basic-direct-method))
135 (with-slots (class role message) method
136 (format nil "~A__~@[~(~A~)_~]method_~A__~A" class role
137 (sod-class-nickname (sod-message-class message))
138 (sod-message-name message))))
140 (export 'daemon-direct-method)
141 (defclass daemon-direct-method (basic-direct-method)
144 "A daemon direct method is invoked for side effects and cannot override.
146 This is the direct method class for `before' and `after' methods, which
147 cannot choose to override the remaining methods and are not involved in
148 the computation of the final result.
150 In C terms, a daemon method must return `void', and is not passed a
151 `next_method' pointer."))
153 (defmethod check-method-type ((method daemon-direct-method)
154 (message sod-message)
155 (type c-function-type))
156 (with-slots ((msgtype type)) message
157 (unless (c-type-equal-p (c-type-subtype type) (c-type void))
158 (error "Method return type ~A must be `void'" (c-type-subtype type)))
159 (unless (argument-lists-compatible-p (c-function-arguments msgtype)
160 (c-function-arguments type))
161 (error "Method arguments ~A don't match message ~A" type msgtype))))
163 (export 'delegating-direct-method)
164 (defclass delegating-direct-method (basic-direct-method)
165 ((next-method-type :type c-function-type
166 :reader sod-method-next-method-type))
168 "A delegating direct method can choose to override other methods.
170 This is the direct method class for `around' and standard-method-
171 combination primary methods, which are given the choice of computing the
172 entire method's result or delegating to (usually) less-specific methods.
174 In C terms, a delegating method is passed a `next_method' pointer so that
175 it can delegate part of its behaviour. (A delegating direct method for a
176 varargs message is also given an additional `va_list' argument,
177 conventionally named `sod__ap_master', which it is expected to pass on to
178 its `next_method' function if necessary.)
180 The function type protocol is implemented on DELEGATING-DIRECT-METHOD
181 using slot reader methods. The actual values are computed on demand in
182 methods defined on SLOT-UNBOUND."))
184 (defmethod slot-unbound (class
185 (method delegating-direct-method)
186 (slot-name (eql 'next-method-type)))
187 (let* ((message (sod-method-message method))
188 (type (sod-message-type message)))
189 (setf (slot-value method 'next-method-type)
190 (c-type (fun (lisp (c-type-subtype type))
191 ("me" (* (class (sod-method-class method))))
192 . (c-function-arguments type))))))
194 (defmethod slot-unbound (class
195 (method delegating-direct-method)
196 (slot-name (eql 'function-type)))
197 (let* ((message (sod-method-message method))
198 (type (sod-method-type method))
199 (method-args (c-function-arguments type)))
200 (setf (slot-value method 'function-type)
201 (c-type (fun (lisp (c-type-subtype type))
202 ("me" (* (class (sod-method-class method))))
203 ("next_method" (* (lisp (commentify-function-type
204 (sod-method-next-method-type
206 . (if (varargs-message-p message)
207 (cons (make-argument *sod-master-ap*
212 ;;;--------------------------------------------------------------------------
213 ;;; Effective method classes.
215 (export 'basic-effective-method)
216 (defclass basic-effective-method (effective-method)
217 ((around-methods :initarg :around-methods :initform nil
218 :type list :reader effective-method-around-methods)
219 (before-methods :initarg :before-methods :initform nil
220 :type list :reader effective-method-before-methods)
221 (after-methods :initarg :after-methods :initform nil
222 :type list :reader effective-method-after-methods)
223 (basic-argument-names :type list
224 :reader effective-method-basic-argument-names)
225 (functions :type list :reader effective-method-functions))
227 "Base class for built-in effective method classes.
229 This class maintains lists of the applicable `before', `after' and
230 `around' methods and provides behaviour for invoking these methods
233 The argument names protocol is implemented on `basic-effective-method'
234 using a slot reader method. The actual values are computed on demand in
235 methods defined on `slot-unbound'."))
237 (defmethod slot-unbound (class
238 (method basic-effective-method)
239 (slot-name (eql 'basic-argument-names)))
240 (let ((message (effective-method-message method)))
241 (setf (slot-value method 'basic-argument-names)
242 (subst *sod-master-ap* *sod-ap*
243 (mapcar #'argument-name
244 (sod-message-no-varargs-tail message))))))
246 (defmethod effective-method-function-name ((method effective-method))
247 (let* ((class (effective-method-class method))
248 (message (effective-method-message method))
249 (message-class (sod-message-class message)))
250 (format nil "~A__emethod_~A__~A"
252 (sod-class-nickname message-class)
253 (sod-message-name message))))
255 (defmethod slot-unbound
256 (class (method basic-effective-method) (slot-name (eql 'functions)))
257 (setf (slot-value method 'functions)
258 (compute-method-entry-functions method)))
260 (export 'simple-effective-method)
261 (defclass simple-effective-method (basic-effective-method)
262 ((primary-methods :initarg :primary-methods :initform nil
263 :type list :reader effective-method-primary-methods))
265 "Effective method counterpart to `simple-message'."))
267 (defmethod shared-initialize :after
268 ((method simple-effective-method) slot-names &key direct-methods)
269 (declare (ignore slot-names))
270 (categorize (method direct-methods :bind ((role (sod-method-role method))))
271 ((primary (null role))
272 (before (eq role :before))
273 (after (eq role :after))
274 (around (eq role :around)))
275 (with-slots (primary-methods before-methods after-methods around-methods)
277 (setf primary-methods primary
278 before-methods before
279 after-methods (reverse after)
280 around-methods around))))
282 ;;;--------------------------------------------------------------------------
285 (defmethod shared-initialize :after
286 ((codegen method-codegen) slot-names &key)
287 (with-slots (message target) codegen
289 (if (eq (c-type-subtype (sod-message-type message)) (c-type void))
293 ;;;--------------------------------------------------------------------------
294 ;;; Invoking direct methods.
296 (export 'basic-effective-method-body)
297 (defun basic-effective-method-body (codegen target method body)
298 "Build the common method-invocation structure.
300 Writes to CODEGEN some basic method-invocation instructions. It invokes
301 the `around' methods, from most- to least-specific. If they all delegate,
302 then the `before' methods are run, most-specific first; next, the
303 instructions generated by BODY (invoked with a target argument); then, the
304 `after' methods are run, least-specific first; and, finally, the value
305 delivered by the BODY is returned to the `around' methods. The result
306 returned by the outermost `around' method -- or, if there are none,
307 delivered by the BODY -- is finally delivered to the TARGET."
309 (with-slots (message class before-methods after-methods around-methods)
311 (let* ((message-type (sod-message-type message))
312 (return-type (c-type-subtype message-type))
313 (voidp (eq return-type (c-type void)))
314 (basic-tail (effective-method-basic-argument-names method)))
315 (flet ((method-kernel (target)
316 (dolist (before before-methods)
317 (invoke-method codegen :void basic-tail before))
318 (if (or voidp (null after-methods))
319 (funcall body target)
320 (convert-stmts codegen target return-type
322 (funcall body target)
323 (dolist (after (reverse after-methods))
324 (invoke-method codegen :void
325 after basic-tail)))))))
326 (invoke-delegation-chain codegen target basic-tail
327 around-methods #'method-kernel)))))
329 ;;;--------------------------------------------------------------------------
330 ;;; Method entry points.
332 (defparameter *method-entry-inline-threshold* 200
333 "Threshold below which effective method bodies are inlined into entries.
335 After the effective method body has been computed, we calculate its
336 metric, multiply by the number of entries we need to generate, and compare
337 it with this threshold. If the metric is below the threshold then we
338 fold the method body into the entry functions; otherwise we split the
339 effective method out into its own function.")
341 (defmethod method-entry-function-name
342 ((method effective-method) (chain-head sod-class))
343 (let* ((class (effective-method-class method))
344 (message (effective-method-message method))
345 (message-class (sod-message-class message)))
346 (if (or (not (slot-boundp method 'functions))
347 (slot-value method 'functions))
348 (format nil "~A__mentry_~A__~A__chain_~A"
350 (sod-class-nickname message-class)
351 (sod-message-name message)
352 (sod-class-nickname chain-head))
355 (defmethod method-entry-function-type ((entry method-entry))
356 (let* ((method (method-entry-effective-method entry))
357 (message (effective-method-message method))
358 (type (sod-message-type message)))
359 (c-type (fun (lisp (c-type-subtype type))
360 ("me" (* (class (method-entry-chain-tail entry))))
361 . (sod-message-argument-tail message)))))
363 (defmethod make-method-entry ((method basic-effective-method)
364 (chain-head sod-class) (chain-tail sod-class))
365 (make-instance 'method-entry
367 :chain-head chain-head
368 :chain-tail chain-tail))
370 (defmethod compute-method-entry-functions ((method basic-effective-method))
372 ;; OK, there's quite a lot of this, so hold tight.
374 ;; The first thing we need to do is find all of the related objects. This
375 ;; is a bit verbose but fairly straightforward.
377 ;; Next, we generate the effective method body -- using COMPUTE-EFFECTIVE-
378 ;; METHOD-BODY of all things. This gives us the declarations and body for
379 ;; an effective method function, but we don't have an actual function yet.
381 ;; Now we look at the chains which are actually going to need a method
382 ;; entry: only those chains whose tail (most specific) class is a
383 ;; superclass of the class which defined the message need an entry. We
384 ;; build a list of these tail classes.
386 ;; Having done this, we decide whether it's better to generate a standalone
387 ;; effective-method function and call it from each of the method entries,
388 ;; or to inline the effective method body into each of the entries.
390 ;; Most of the complexity here comes from (a) dealing with the two
391 ;; different strategies for constructing method entry functions and (b)
392 ;; (unsurprisingly) the mess involved with dealing with varargs messages.
394 (let* ((message (effective-method-message method))
395 (class (effective-method-class method))
396 (message-class (sod-message-class message))
397 (return-type (c-type-subtype (sod-message-type message)))
398 (codegen (make-instance 'method-codegen
403 ;; Effective method function details.
404 (emf-name (effective-method-function-name method))
405 (ilayout-type (c-type (* (struct (ilayout-struct-tag class)))))
406 (emf-arg-tail (mapcar (lambda (arg)
407 (if (eq (argument-name arg) *sod-ap*)
408 (make-argument *sod-master-ap*
411 (sod-message-no-varargs-tail message)))
412 (emf-type (c-type (fun (lisp return-type)
413 ("sod__obj" (lisp ilayout-type))
414 . (sod-message-no-varargs-tail message))))
415 (result (if (eq return-type (c-type void)) nil
416 (temporary-var codegen return-type)))
417 (emf-target (or result :void))
419 ;; Method entry details.
420 (chain-tails (remove-if-not (lambda (super)
421 (sod-subclass-p super message-class))
423 (sod-class-chains class))))
424 (n-entries (length chain-tails))
425 (entry-args (sod-message-argument-tail message))
426 (parm-n (do ((prev "me" (car args))
427 (args entry-args (cdr args)))
429 (when (eq (car args) :ellipsis)
431 (entry-target (codegen-target codegen)))
433 (flet ((setup-entry (tail)
434 (let ((head (sod-class-chain-head tail)))
435 (codegen-push codegen)
436 (ensure-var codegen "sod__obj" ilayout-type
437 (make-convert-to-ilayout-inst class
440 (ensure-var codegen *sod-master-ap* (c-type va-list))
442 (make-va-start-inst *sod-master-ap* parm-n)))
444 (emit-inst codegen (make-va-end-inst *sod-master-ap*)))
446 (let* ((head (sod-class-chain-head tail))
447 (name (method-entry-function-name method head))
448 (type (c-type (fun (lisp return-type)
449 ("me" (* (class tail)))
451 (codegen-pop-function codegen name type))))
453 ;; Generate the method body. We'll work out what to do with it later.
454 (codegen-push codegen)
455 (compute-effective-method-body method codegen emf-target)
456 (multiple-value-bind (vars insts) (codegen-pop codegen)
457 (cond ((or (= n-entries 1)
458 (<= (* n-entries (reduce #'+ insts :key #'inst-metric))
459 *method-entry-inline-threshold*))
461 ;; The effective method body is simple -- or there's only one
462 ;; of them. We'll inline the method body into the entry
464 (dolist (tail chain-tails)
467 (ensure-var codegen (inst-name var)
468 (inst-type var) (inst-init var)))
469 (when parm-n (varargs-prologue))
470 (emit-insts codegen insts)
471 (when parm-n (varargs-epilogue))
472 (deliver-expr codegen entry-target result)
473 (finish-entry tail)))
477 ;; The effective method body is complicated and we'd need more
478 ;; than one copy. We'll generate an effective method function
479 ;; and call it a lot.
480 (codegen-build-function codegen emf-name emf-type vars
481 (nconc insts (and result (list (make-return-inst result)))))
483 (let ((call (make-call-inst emf-name
484 (cons "sod__obj" (mapcar #'argument-name
486 (dolist (tail chain-tails)
490 (convert-stmts codegen entry-target return-type
492 (deliver-expr codegen target call)
493 (varargs-epilogue))))
495 (deliver-expr codegen entry-target call)))
496 (finish-entry tail))))))
498 (codegen-functions codegen))))
500 (defmethod compute-method-entry-functions
501 ((method simple-effective-method))
502 (if (effective-method-primary-methods method)
506 (defmethod compute-effective-method-body
507 ((method simple-effective-method) codegen target)
508 (with-slots (message basic-argument-names primary-methods) method
509 (basic-effective-method-body codegen target method
511 (simple-method-body method
515 ;;;--------------------------------------------------------------------------
516 ;;; Standard method combination.
518 (export 'standard-message)
519 (defclass standard-message (simple-message)
522 "Message class for standard method combination.
524 Standard method combination is a simple method combination where the
525 primary methods are invoked as a delegation chain, from most- to
528 (export 'standard-effective-method)
529 (defclass standard-effective-method (simple-effective-method) ()
530 (:documentation "Effective method counterpart to `standard-message'."))
532 (defmethod primary-method-class ((message standard-message))
533 'delegating-direct-method)
535 (defmethod message-effective-method-class ((message standard-message))
536 'standard-effective-method)
538 (defmethod simple-method-body
539 ((method standard-effective-method) codegen target)
540 (invoke-delegation-chain codegen
542 (effective-method-basic-argument-names method)
543 (effective-method-primary-methods method)
546 ;;;----- That's all, folks --------------------------------------------------