More WIP.
[sod] / src / method-impl.lisp
CommitLineData
1f1d88f5
MW
1;;; -*-lisp-*-
2;;;
dea4d055 3;;; Method combination implementation
1f1d88f5
MW
4;;;
5;;; (c) 2009 Straylight/Edgeware
6;;;
7
8;;;----- Licensing notice ---------------------------------------------------
9;;;
dea4d055 10;;; This file is part of the Sensble Object Design, an object system for C.
1f1d88f5
MW
11;;;
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.
16;;;
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.
21;;;
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.
25
26(cl:in-package #:sod)
27
28;;;--------------------------------------------------------------------------
1f1d88f5
MW
29;;; Message classes.
30
dea4d055 31(export 'basic-message)
1f1d88f5
MW
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))
35 (:documentation
36 "Base class for built-in message classes.
37
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.
41
dea4d055 42 The function type protocol is implemented on `basic-message' using slot
1f1d88f5 43 reader methods. The actual values are computed on demand in methods
dea4d055 44 defined on `slot-unbound'."))
1f1d88f5
MW
45
46(defmethod slot-unbound (class
47 (message basic-message)
48 (slot-name (eql 'argument-tail)))
1d8cc67a 49 (declare (ignore class))
1f1d88f5 50 (let ((seq 0))
dea4d055
MW
51 (setf (slot-value message 'argument-tail)
52 (mapcar (lambda (arg)
53 (if (or (eq arg :ellipsis) (argument-name arg)) arg
54 (make-argument (make-instance 'temporary-argument
55 :tag (prog1 seq
56 (incf seq)))
57 (argument-type arg))))
58 (c-function-arguments (sod-message-type message))))))
1f1d88f5
MW
59
60(defmethod slot-unbound (class
61 (message basic-message)
62 (slot-name (eql 'no-varargs-tail)))
1d8cc67a 63 (declare (ignore class))
dea4d055
MW
64 (setf (slot-value message 'no-varargs-tail)
65 (mapcar (lambda (arg)
66 (if (eq arg :ellipsis)
67 (make-argument *sod-ap* (c-type va-list))
68 arg))
69 (sod-message-argument-tail message))))
1f1d88f5
MW
70
71(defmethod sod-message-method-class
72 ((message basic-message) (class sod-class) pset)
73 (let ((role (get-property pset :role :keyword nil)))
74 (case role
75 ((:before :after) 'daemon-direct-method)
76 (:around 'delegating-direct-method)
77 ((nil) (error "How odd: a primary method slipped through the net"))
78 (t (error "Unknown method role ~A" role)))))
79
dea4d055
MW
80(export 'simple-message)
81(defclass simple-message (basic-message)
82 ()
83 (:documentation
84 "Base class for messages with `simple' method combinations.
1f1d88f5 85
dea4d055 86 A simple method combination is one which has only one method role other
3109662a
MW
87 than the `before', `after' and `around' methods provided by
88 `basic-message'. We call these `primary' methods, and the programmer
89 designates them by not specifying an explicit role.
1f1d88f5 90
dea4d055
MW
91 If the programmer doesn't define any primary methods then the effective
92 method is null -- i.e., the method entry pointer shows up as a null
93 pointer."))
94
95(defmethod sod-message-method-class
96 ((message simple-message) (class sod-class) pset)
97 (if (get-property pset :role :keyword nil)
98 (call-next-method)
99 (primary-method-class message)))
1f1d88f5
MW
100
101;;;--------------------------------------------------------------------------
102;;; Direct method classes.
103
dea4d055 104(export 'basic-direct-method)
1f1d88f5 105(defclass basic-direct-method (sod-method)
77027cca
MW
106 ((role :initarg :role :type symbol :reader sod-method-role)
107 (function-type :type c-function-type :reader sod-method-function-type))
1f1d88f5
MW
108 (:documentation
109 "Base class for built-in direct method classes.
110
111 Provides the basic functionality for the built-in direct-method classes.
112 This is a separate class so that `special effect' methods can avoid
113 inheriting its default behaviour and slots.
114
115 A basic method can be assigned a `role', which may be set either as an
dea4d055 116 initarg or using the `:role' property. Roles are used for method
1f1d88f5
MW
117 categorization.
118
dea4d055 119 The function type protocol is implemented on `basic-direct-method' using
1f1d88f5 120 slot reader methods. The actual values are computed on demand in methods
dea4d055 121 defined on `slot-unbound'."))
1f1d88f5
MW
122
123(defmethod shared-initialize :after
124 ((method basic-direct-method) slot-names &key pset)
125 (declare (ignore slot-names))
126 (default-slot (method 'role) (get-property pset :role :keyword nil)))
127
128(defmethod slot-unbound
129 (class (method basic-direct-method) (slot-name (eql 'function-type)))
1d8cc67a 130 (declare (ignore class))
1f1d88f5
MW
131 (let ((type (sod-method-type method)))
132 (setf (slot-value method 'function-type)
133 (c-type (fun (lisp (c-type-subtype type))
134 ("me" (* (class (sod-method-class method))))
135 . (c-function-arguments type))))))
136
ddee4bb1 137(defmethod sod-method-function-name ((method basic-direct-method))
1f1d88f5
MW
138 (with-slots (class role message) method
139 (format nil "~A__~@[~(~A~)_~]method_~A__~A" class role
140 (sod-class-nickname (sod-message-class message))
141 (sod-message-name message))))
142
dea4d055 143(export 'daemon-direct-method)
1f1d88f5
MW
144(defclass daemon-direct-method (basic-direct-method)
145 ()
146 (:documentation
147 "A daemon direct method is invoked for side effects and cannot override.
148
149 This is the direct method class for `before' and `after' methods, which
150 cannot choose to override the remaining methods and are not involved in
151 the computation of the final result.
152
153 In C terms, a daemon method must return `void', and is not passed a
154 `next_method' pointer."))
155
dea4d055
MW
156(defmethod check-method-type ((method daemon-direct-method)
157 (message sod-message)
158 (type c-function-type))
1f1d88f5
MW
159 (with-slots ((msgtype type)) message
160 (unless (c-type-equal-p (c-type-subtype type) (c-type void))
161 (error "Method return type ~A must be `void'" (c-type-subtype type)))
162 (unless (argument-lists-compatible-p (c-function-arguments msgtype)
163 (c-function-arguments type))
164 (error "Method arguments ~A don't match message ~A" type msgtype))))
165
dea4d055 166(export 'delegating-direct-method)
1f1d88f5
MW
167(defclass delegating-direct-method (basic-direct-method)
168 ((next-method-type :type c-function-type
169 :reader sod-method-next-method-type))
170 (:documentation
171 "A delegating direct method can choose to override other methods.
172
173 This is the direct method class for `around' and standard-method-
174 combination primary methods, which are given the choice of computing the
175 entire method's result or delegating to (usually) less-specific methods.
176
177 In C terms, a delegating method is passed a `next_method' pointer so that
178 it can delegate part of its behaviour. (A delegating direct method for a
179 varargs message is also given an additional `va_list' argument,
180 conventionally named `sod__ap_master', which it is expected to pass on to
181 its `next_method' function if necessary.)
182
bf090e02 183 The function type protocol is implemented on `delegating-direct-method'
1f1d88f5 184 using slot reader methods. The actual values are computed on demand in
bf090e02 185 methods defined on `slot-unbound'."))
1f1d88f5
MW
186
187(defmethod slot-unbound (class
188 (method delegating-direct-method)
189 (slot-name (eql 'next-method-type)))
1d8cc67a 190 (declare (ignore class))
1f1d88f5
MW
191 (let* ((message (sod-method-message method))
192 (type (sod-message-type message)))
193 (setf (slot-value method 'next-method-type)
194 (c-type (fun (lisp (c-type-subtype type))
195 ("me" (* (class (sod-method-class method))))
196 . (c-function-arguments type))))))
197
198(defmethod slot-unbound (class
199 (method delegating-direct-method)
200 (slot-name (eql 'function-type)))
1d8cc67a 201 (declare (ignore class))
1f1d88f5
MW
202 (let* ((message (sod-method-message method))
203 (type (sod-method-type method))
204 (method-args (c-function-arguments type)))
205 (setf (slot-value method 'function-type)
206 (c-type (fun (lisp (c-type-subtype type))
207 ("me" (* (class (sod-method-class method))))
208 ("next_method" (* (lisp (commentify-function-type
209 (sod-method-next-method-type
210 method)))))
bf090e02
MW
211 .
212 (if (varargs-message-p message)
213 (cons (make-argument *sod-master-ap*
214 (c-type va-list))
215 method-args)
216 method-args))))))
1f1d88f5
MW
217
218;;;--------------------------------------------------------------------------
219;;; Effective method classes.
220
dea4d055 221(export 'basic-effective-method)
1f1d88f5 222(defclass basic-effective-method (effective-method)
77027cca
MW
223 ((around-methods :initarg :around-methods :initform nil
224 :type list :reader effective-method-around-methods)
225 (before-methods :initarg :before-methods :initform nil
226 :type list :reader effective-method-before-methods)
227 (after-methods :initarg :after-methods :initform nil
228 :type list :reader effective-method-after-methods)
1f1d88f5
MW
229 (basic-argument-names :type list
230 :reader effective-method-basic-argument-names)
231 (functions :type list :reader effective-method-functions))
232 (:documentation
233 "Base class for built-in effective method classes.
234
235 This class maintains lists of the applicable `before', `after' and
236 `around' methods and provides behaviour for invoking these methods
237 correctly.
238
dea4d055
MW
239 The argument names protocol is implemented on `basic-effective-method'
240 using a slot reader method. The actual values are computed on demand in
241 methods defined on `slot-unbound'."))
1f1d88f5
MW
242
243(defmethod slot-unbound (class
244 (method basic-effective-method)
245 (slot-name (eql 'basic-argument-names)))
1d8cc67a 246 (declare (ignore class))
1f1d88f5
MW
247 (let ((message (effective-method-message method)))
248 (setf (slot-value method 'basic-argument-names)
249 (subst *sod-master-ap* *sod-ap*
250 (mapcar #'argument-name
251 (sod-message-no-varargs-tail message))))))
252
dea4d055
MW
253(defmethod effective-method-function-name ((method effective-method))
254 (let* ((class (effective-method-class method))
255 (message (effective-method-message method))
256 (message-class (sod-message-class message)))
257 (format nil "~A__emethod_~A__~A"
258 class
259 (sod-class-nickname message-class)
260 (sod-message-name message))))
1f1d88f5 261
dea4d055
MW
262(defmethod slot-unbound
263 (class (method basic-effective-method) (slot-name (eql 'functions)))
1d8cc67a 264 (declare (ignore class))
dea4d055
MW
265 (setf (slot-value method 'functions)
266 (compute-method-entry-functions method)))
1f1d88f5 267
dea4d055
MW
268(export 'simple-effective-method)
269(defclass simple-effective-method (basic-effective-method)
270 ((primary-methods :initarg :primary-methods :initform nil
271 :type list :reader effective-method-primary-methods))
1f1d88f5 272 (:documentation
dea4d055 273 "Effective method counterpart to `simple-message'."))
1f1d88f5 274
dea4d055
MW
275(defmethod shared-initialize :after
276 ((method simple-effective-method) slot-names &key direct-methods)
277 (declare (ignore slot-names))
278 (categorize (method direct-methods :bind ((role (sod-method-role method))))
279 ((primary (null role))
280 (before (eq role :before))
281 (after (eq role :after))
282 (around (eq role :around)))
283 (with-slots (primary-methods before-methods after-methods around-methods)
284 method
285 (setf primary-methods primary
286 before-methods before
287 after-methods (reverse after)
288 around-methods around))))
289
290;;;--------------------------------------------------------------------------
291;;; Code generation.
1f1d88f5
MW
292
293(defmethod shared-initialize :after
294 ((codegen method-codegen) slot-names &key)
1d8cc67a 295 (declare (ignore slot-names))
1f1d88f5
MW
296 (with-slots (message target) codegen
297 (setf target
298 (if (eq (c-type-subtype (sod-message-type message)) (c-type void))
299 :void
300 :return))))
301
dea4d055
MW
302;;;--------------------------------------------------------------------------
303;;; Invoking direct methods.
1f1d88f5 304
dea4d055 305(export 'basic-effective-method-body)
1f1d88f5
MW
306(defun basic-effective-method-body (codegen target method body)
307 "Build the common method-invocation structure.
308
309 Writes to CODEGEN some basic method-invocation instructions. It invokes
310 the `around' methods, from most- to least-specific. If they all delegate,
311 then the `before' methods are run, most-specific first; next, the
312 instructions generated by BODY (invoked with a target argument); then, the
313 `after' methods are run, least-specific first; and, finally, the value
314 delivered by the BODY is returned to the `around' methods. The result
315 returned by the outermost `around' method -- or, if there are none,
316 delivered by the BODY -- is finally delivered to the TARGET."
317
318 (with-slots (message class before-methods after-methods around-methods)
319 method
320 (let* ((message-type (sod-message-type message))
321 (return-type (c-type-subtype message-type))
322 (voidp (eq return-type (c-type void)))
323 (basic-tail (effective-method-basic-argument-names method)))
324 (flet ((method-kernel (target)
325 (dolist (before before-methods)
326 (invoke-method codegen :void basic-tail before))
327 (if (or voidp (null after-methods))
328 (funcall body target)
329 (convert-stmts codegen target return-type
330 (lambda (target)
331 (funcall body target)
332 (dolist (after (reverse after-methods))
333 (invoke-method codegen :void
334 after basic-tail)))))))
335 (invoke-delegation-chain codegen target basic-tail
336 around-methods #'method-kernel)))))
337
338;;;--------------------------------------------------------------------------
dea4d055 339;;; Method entry points.
1f1d88f5 340
a07d8d00 341(defparameter *method-entry-inline-threshold* 200
1f1d88f5
MW
342 "Threshold below which effective method bodies are inlined into entries.
343
344 After the effective method body has been computed, we calculate its
345 metric, multiply by the number of entries we need to generate, and compare
346 it with this threshold. If the metric is below the threshold then we
347 fold the method body into the entry functions; otherwise we split the
348 effective method out into its own function.")
349
1f1d88f5
MW
350(defmethod method-entry-function-name
351 ((method effective-method) (chain-head sod-class))
352 (let* ((class (effective-method-class method))
353 (message (effective-method-message method))
354 (message-class (sod-message-class message)))
dea4d055
MW
355 (if (or (not (slot-boundp method 'functions))
356 (slot-value method 'functions))
357 (format nil "~A__mentry_~A__~A__chain_~A"
358 class
359 (sod-class-nickname message-class)
360 (sod-message-name message)
361 (sod-class-nickname chain-head))
362 0)))
363
364(defmethod method-entry-function-type ((entry method-entry))
365 (let* ((method (method-entry-effective-method entry))
366 (message (effective-method-message method))
367 (type (sod-message-type message)))
368 (c-type (fun (lisp (c-type-subtype type))
369 ("me" (* (class (method-entry-chain-tail entry))))
370 . (sod-message-argument-tail message)))))
371
372(defmethod make-method-entry ((method basic-effective-method)
373 (chain-head sod-class) (chain-tail sod-class))
374 (make-instance 'method-entry
375 :method method
376 :chain-head chain-head
377 :chain-tail chain-tail))
1f1d88f5
MW
378
379(defmethod compute-method-entry-functions ((method basic-effective-method))
380
381 ;; OK, there's quite a lot of this, so hold tight.
382 ;;
383 ;; The first thing we need to do is find all of the related objects. This
384 ;; is a bit verbose but fairly straightforward.
385 ;;
bf090e02
MW
386 ;; Next, we generate the effective method body -- using `compute-effective-
387 ;; method-body' of all things. This gives us the declarations and body for
1f1d88f5
MW
388 ;; an effective method function, but we don't have an actual function yet.
389 ;;
390 ;; Now we look at the chains which are actually going to need a method
391 ;; entry: only those chains whose tail (most specific) class is a
392 ;; superclass of the class which defined the message need an entry. We
393 ;; build a list of these tail classes.
394 ;;
395 ;; Having done this, we decide whether it's better to generate a standalone
396 ;; effective-method function and call it from each of the method entries,
397 ;; or to inline the effective method body into each of the entries.
398 ;;
399 ;; Most of the complexity here comes from (a) dealing with the two
400 ;; different strategies for constructing method entry functions and (b)
401 ;; (unsurprisingly) the mess involved with dealing with varargs messages.
402
403 (let* ((message (effective-method-message method))
404 (class (effective-method-class method))
405 (message-class (sod-message-class message))
406 (return-type (c-type-subtype (sod-message-type message)))
407 (codegen (make-instance 'method-codegen
408 :message message
409 :class class
410 :method method))
411
412 ;; Effective method function details.
413 (emf-name (effective-method-function-name method))
414 (ilayout-type (c-type (* (struct (ilayout-struct-tag class)))))
415 (emf-arg-tail (mapcar (lambda (arg)
416 (if (eq (argument-name arg) *sod-ap*)
417 (make-argument *sod-master-ap*
418 (c-type va-list))
419 arg))
420 (sod-message-no-varargs-tail message)))
421 (emf-type (c-type (fun (lisp return-type)
422 ("sod__obj" (lisp ilayout-type))
423 . (sod-message-no-varargs-tail message))))
424 (result (if (eq return-type (c-type void)) nil
425 (temporary-var codegen return-type)))
426 (emf-target (or result :void))
427
428 ;; Method entry details.
429 (chain-tails (remove-if-not (lambda (super)
430 (sod-subclass-p super message-class))
431 (mapcar #'car
432 (sod-class-chains class))))
433 (n-entries (length chain-tails))
434 (entry-args (sod-message-argument-tail message))
435 (parm-n (do ((prev "me" (car args))
436 (args entry-args (cdr args)))
437 ((endp args) nil)
438 (when (eq (car args) :ellipsis)
439 (return prev))))
440 (entry-target (codegen-target codegen)))
441
dea4d055
MW
442 (flet ((setup-entry (tail)
443 (let ((head (sod-class-chain-head tail)))
444 (codegen-push codegen)
445 (ensure-var codegen "sod__obj" ilayout-type
446 (make-convert-to-ilayout-inst class
447 head "me"))))
448 (varargs-prologue ()
449 (ensure-var codegen *sod-master-ap* (c-type va-list))
450 (emit-inst codegen
451 (make-va-start-inst *sod-master-ap* parm-n)))
452 (varargs-epilogue ()
453 (emit-inst codegen (make-va-end-inst *sod-master-ap*)))
454 (finish-entry (tail)
455 (let* ((head (sod-class-chain-head tail))
456 (name (method-entry-function-name method head))
457 (type (c-type (fun (lisp return-type)
458 ("me" (* (class tail)))
459 . entry-args))))
460 (codegen-pop-function codegen name type))))
1f1d88f5
MW
461
462 ;; Generate the method body. We'll work out what to do with it later.
463 (codegen-push codegen)
464 (compute-effective-method-body method codegen emf-target)
465 (multiple-value-bind (vars insts) (codegen-pop codegen)
466 (cond ((or (= n-entries 1)
467 (<= (* n-entries (reduce #'+ insts :key #'inst-metric))
468 *method-entry-inline-threshold*))
469
470 ;; The effective method body is simple -- or there's only one
471 ;; of them. We'll inline the method body into the entry
472 ;; functions.
473 (dolist (tail chain-tails)
474 (setup-entry tail)
475 (dolist (var vars)
476 (ensure-var codegen (inst-name var)
477 (inst-type var) (inst-init var)))
478 (when parm-n (varargs-prologue))
479 (emit-insts codegen insts)
480 (when parm-n (varargs-epilogue))
481 (deliver-expr codegen entry-target result)
482 (finish-entry tail)))
483
484 (t
485
486 ;; The effective method body is complicated and we'd need more
487 ;; than one copy. We'll generate an effective method function
488 ;; and call it a lot.
489 (codegen-build-function codegen emf-name emf-type vars
490 (nconc insts (and result (list (make-return-inst result)))))
491
492 (let ((call (make-call-inst emf-name
493 (cons "sod__obj" (mapcar #'argument-name
494 emf-arg-tail)))))
495 (dolist (tail chain-tails)
496 (setup-entry tail)
497 (cond (parm-n
498 (varargs-prologue)
499 (convert-stmts codegen entry-target return-type
500 (lambda (target)
501 (deliver-expr codegen target call)
502 (varargs-epilogue))))
503 (t
504 (deliver-expr codegen entry-target call)))
505 (finish-entry tail))))))
506
507 (codegen-functions codegen))))
508
dea4d055
MW
509(defmethod compute-method-entry-functions
510 ((method simple-effective-method))
511 (if (effective-method-primary-methods method)
512 (call-next-method)
513 nil))
514
515(defmethod compute-effective-method-body
516 ((method simple-effective-method) codegen target)
517 (with-slots (message basic-argument-names primary-methods) method
518 (basic-effective-method-body codegen target method
519 (lambda (target)
520 (simple-method-body method
521 codegen
522 target)))))
1f1d88f5 523
dea4d055
MW
524;;;--------------------------------------------------------------------------
525;;; Standard method combination.
ddee4bb1 526
dea4d055
MW
527(export 'standard-message)
528(defclass standard-message (simple-message)
529 ()
530 (:documentation
531 "Message class for standard method combination.
1f1d88f5 532
dea4d055
MW
533 Standard method combination is a simple method combination where the
534 primary methods are invoked as a delegation chain, from most- to
535 least-specific."))
536
537(export 'standard-effective-method)
538(defclass standard-effective-method (simple-effective-method) ()
539 (:documentation "Effective method counterpart to `standard-message'."))
540
541(defmethod primary-method-class ((message standard-message))
542 'delegating-direct-method)
543
544(defmethod message-effective-method-class ((message standard-message))
545 'standard-effective-method)
546
547(defmethod simple-method-body
548 ((method standard-effective-method) codegen target)
549 (invoke-delegation-chain codegen
550 target
551 (effective-method-basic-argument-names method)
552 (effective-method-primary-methods method)
553 nil))
a07d8d00 554
1f1d88f5 555;;;----- That's all, folks --------------------------------------------------