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