0fbb3f07e514ca7349f9422e1f30d5fa18137305
[sod] / methods.lisp
1 ;;; -*-lisp-*-
2 ;;;
3 ;;; Infrastructure for effective method generation
4 ;;;
5 ;;; (c) 2009 Straylight/Edgeware
6 ;;;
7
8 ;;;----- Licensing notice ---------------------------------------------------
9 ;;;
10 ;;; This file is part of the Simple Object Definition system.
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 ;;; Function type protocol.
30
31 (defgeneric sod-message-argument-tail (message)
32 (:documentation
33 "Return the argument tail for the message, with invented argument names.
34
35 No `me' argument is prepended; any :ELLIPSIS is left as it is."))
36
37 (defgeneric sod-message-no-varargs-tail (message)
38 (:documentation
39 "Return the argument tail for the message with :ELLIPSIS substituted.
40
41 As with SOD-MESSAGE-ARGUMENT-TAIL, no `me' argument is prepended.
42 However, an :ELLIPSIS is replaced by an argument of type `va_list', named
43 `sod__ap'."))
44
45 (defgeneric direct-method-function-type (method)
46 (:documentation
47 "Return the C function type for the direct method.
48
49 This is called during initialization of a direct method object, and the
50 result is cached.
51
52 A default method is provided (by BASIC-DIRECT-METHOD) which simply
53 prepends an appropriate `me' argument to the user-provided argument list.
54 Fancy method classes may need to override this behaviour."))
55
56 (defgeneric direct-method-next-method-type (method)
57 (:documentation
58 "Return the C function type for the next-method trampoline.
59
60 This is called during initialization of a direct method object, and the
61 result is cached. It should return a function type, not a pointer type.
62
63 A default method is provided (by DELEGATING-DIRECT-METHOD) which should do
64 the right job. Very fancy subclasses might need to do something
65 different."))
66
67 (defgeneric direct-method-function-name (method)
68 (:documentation
69 "Return the C function name for the direct method."))
70
71 ;;;--------------------------------------------------------------------------
72 ;;; Message classes.
73
74 (defclass basic-message (sod-message)
75 ((argument-tail :type list :reader sod-message-argument-tail)
76 (no-varargs-tail :type list :reader sod-message-no-varargs-tail))
77 (:documentation
78 "Base class for built-in message classes.
79
80 Provides the basic functionality for the built-in method combinations.
81 This is a separate class so that `special effect' messages can avoid
82 inheriting its default behaviour.
83
84 The function type protocol is implemented on BASIC-MESSAGE using slot
85 reader methods. The actual values are computed on demand in methods
86 defined on SLOT-UNBOUND."))
87
88 ;;; Function type protocol.
89
90 (defmethod slot-unbound (class
91 (message basic-message)
92 (slot-name (eql 'argument-tail)))
93 (let ((seq 0))
94 (mapcar (lambda (arg)
95 (if (or (eq arg :ellipsis) (argument-name arg))
96 arg
97 (make-argument (make-instance 'temporary-argument
98 :tag (prog1 seq (incf seq)))
99 (argument-type arg))))
100 (c-function-arguments (sod-message-type message)))))
101
102 (defmethod slot-unbound (class
103 (message basic-message)
104 (slot-name (eql 'no-varargs-tail)))
105 (mapcar (lambda (arg)
106 (if (eq arg :ellipsis)
107 (make-argument *sod-ap* (c-type va-list))
108 arg))
109 (sod-message-argument-tail message)))
110
111 ;;; Method class selection.
112
113 (defmethod sod-message-method-class
114 ((message basic-message) (class sod-class) pset)
115 (let ((role (get-property pset :role :keyword nil)))
116 (case role
117 ((:before :after) 'daemon-direct-method)
118 (:around 'delegating-direct-method)
119 ((nil) (error "How odd: a primary method slipped through the net"))
120 (t (error "Unknown method role ~A" role)))))
121
122 ;;; Utility functions.
123
124 (defun varargs-message-p (message)
125 "Answer whether the MESSAGE accepts a variable-length argument list.
126
127 We need to jump through some extra hoops in order to cope with varargs
128 messages, so this is useful to know."
129 (member :ellipsis (sod-message-argument-tail message)))
130
131 ;;;--------------------------------------------------------------------------
132 ;;; Direct method classes.
133
134 (defclass basic-direct-method (sod-method)
135 ((role :initarg :role :type symbol :reader sod-method-role)
136 (function-type :type c-function-type :reader sod-method-function-type))
137 (:documentation
138 "Base class for built-in direct method classes.
139
140 Provides the basic functionality for the built-in direct-method classes.
141 This is a separate class so that `special effect' methods can avoid
142 inheriting its default behaviour and slots.
143
144 A basic method can be assigned a `role', which may be set either as an
145 initarg or using the :ROLE property. Roles are used for method
146 categorization.
147
148 The function type protocol is implemented on BASIC-DIRECT-METHOD using
149 slot reader methods. The actual values are computed on demand in methods
150 defined on SLOT-UNBOUND."))
151
152 (defmethod shared-initialize :after
153 ((method basic-direct-method) slot-names &key pset)
154 (declare (ignore slot-names))
155 (default-slot (method 'role) (get-property pset :role :keyword nil)))
156
157 (defmethod slot-unbound
158 (class (method basic-direct-method) (slot-name (eql 'function-type)))
159 (let ((type (sod-method-type method)))
160 (setf (slot-value method 'function-type)
161 (c-type (fun (lisp (c-type-subtype type))
162 ("me" (* (class (sod-method-class method))))
163 . (c-function-arguments type))))))
164
165 (defmethod direct-method-function-name ((method basic-direct-method))
166 (with-slots (class role message) method
167 (format nil "~A__~@[~(~A~)_~]method_~A__~A" class role
168 (sod-class-nickname (sod-message-class message))
169 (sod-message-name message))))
170
171 (defclass daemon-direct-method (basic-direct-method)
172 ()
173 (:documentation
174 "A daemon direct method is invoked for side effects and cannot override.
175
176 This is the direct method class for `before' and `after' methods, which
177 cannot choose to override the remaining methods and are not involved in
178 the computation of the final result.
179
180 In C terms, a daemon method must return `void', and is not passed a
181 `next_method' pointer."))
182
183 (defmethod check-method-type
184 ((method daemon-direct-method)
185 (message sod-message)
186 (type c-function-type))
187 (with-slots ((msgtype type)) message
188 (unless (c-type-equal-p (c-type-subtype type) (c-type void))
189 (error "Method return type ~A must be `void'" (c-type-subtype type)))
190 (unless (argument-lists-compatible-p (c-function-arguments msgtype)
191 (c-function-arguments type))
192 (error "Method arguments ~A don't match message ~A" type msgtype))))
193
194 (defclass delegating-direct-method (basic-direct-method)
195 ((next-method-type :type c-function-type
196 :reader sod-method-next-method-type))
197 (:documentation
198 "A delegating direct method can choose to override other methods.
199
200 This is the direct method class for `around' and standard-method-
201 combination primary methods, which are given the choice of computing the
202 entire method's result or delegating to (usually) less-specific methods.
203
204 In C terms, a delegating method is passed a `next_method' pointer so that
205 it can delegate part of its behaviour. (A delegating direct method for a
206 varargs message is also given an additional `va_list' argument,
207 conventionally named `sod__ap_master', which it is expected to pass on to
208 its `next_method' function if necessary.)
209
210 The function type protocol is implemented on DELEGATING-DIRECT-METHOD
211 using slot reader methods. The actual values are computed on demand in
212 methods defined on SLOT-UNBOUND."))
213
214 (defmethod slot-unbound (class
215 (method delegating-direct-method)
216 (slot-name (eql 'next-method-type)))
217 (let* ((message (sod-method-message method))
218 (type (sod-message-type message)))
219 (setf (slot-value method 'next-method-type)
220 (c-type (fun (lisp (c-type-subtype type))
221 ("me" (* (class (sod-method-class method))))
222 . (c-function-arguments type))))))
223
224 (defmethod slot-unbound (class
225 (method delegating-direct-method)
226 (slot-name (eql 'function-type)))
227 (let* ((message (sod-method-message method))
228 (type (sod-method-type method))
229 (method-args (c-function-arguments type)))
230 (setf (slot-value method 'function-type)
231 (c-type (fun (lisp (c-type-subtype type))
232 ("me" (* (class (sod-method-class method))))
233 ("next_method" (* (lisp (commentify-function-type
234 (sod-method-next-method-type
235 method)))))
236 . (if (varargs-message-p message)
237 (cons (make-argument *sod-master-ap*
238 (c-type va-list))
239 method-args)
240 method-args))))))
241
242 ;;;--------------------------------------------------------------------------
243 ;;; Effective method classes.
244
245 (defgeneric effective-method-basic-argument-names (method)
246 (:documentation
247 "Return a list of argument names to be passed to direct methods.
248
249 The argument names are constructed from the message's arguments returned
250 by SOD-MESSAGE-NO-VARARGS-TAIL. The basic arguments are the ones
251 immediately derived from the programmer's explicitly stated arguments; the
252 `me' argument is not included, and neither are more exotic arguments added
253 as part of the method delegation protocol."))
254
255 (defclass basic-effective-method (effective-method)
256 ((around-methods :initarg :around-methods :initform nil
257 :type list :reader effective-method-around-methods)
258 (before-methods :initarg :before-methods :initform nil
259 :type list :reader effective-method-before-methods)
260 (after-methods :initarg :after-methods :initform nil
261 :type list :reader effective-method-after-methods)
262 (basic-argument-names :type list
263 :reader effective-method-basic-argument-names)
264 (functions :type list :reader effective-method-functions))
265 (:documentation
266 "Base class for built-in effective method classes.
267
268 This class maintains lists of the applicable `before', `after' and
269 `around' methods and provides behaviour for invoking these methods
270 correctly.
271
272 The argument names protocol is implemented on BASIC-EFFECTIVE-METHOD using
273 a slot reader method. The actual values are computed on demand in methods
274 defined on SLOT-UNBOUND."))
275
276 (defmethod slot-unbound (class
277 (method basic-effective-method)
278 (slot-name (eql 'basic-argument-names)))
279 (let ((message (effective-method-message method)))
280 (setf (slot-value method 'basic-argument-names)
281 (subst *sod-master-ap* *sod-ap*
282 (mapcar #'argument-name
283 (sod-message-no-varargs-tail message))))))
284
285 ;;;--------------------------------------------------------------------------
286 ;;; Method categorization.
287
288 (defmacro categorize ((itemvar items &key bind) categories &body body)
289 "Categorize ITEMS into lists and invoke BODY.
290
291 The ITEMVAR is a symbol; as the macro iterates over the ITEMS, ITEMVAR
292 will contain the current item. The BIND argument is a list of LET*-like
293 clauses. The CATEGORIES are a list of clauses of the form (SYMBOL
294 PREDICATE).
295
296 The behaviour of the macro is as follows. ITEMVAR is assigned (not
297 bound), in turn, each item in the list ITEMS. The PREDICATEs in the
298 CATEGORIES list are evaluated in turn, in an environment containing
299 ITEMVAR and the BINDings, until one of them evaluates to a non-nil value.
300 At this point, the item is assigned to the category named by the
301 corresponding SYMBOL. If none of the PREDICATEs returns non-nil then an
302 error is signalled; a PREDICATE consisting only of T will (of course)
303 match anything; it is detected specially so as to avoid compiler warnings.
304
305 Once all of the ITEMS have been categorized in this fashion, the BODY is
306 evaluated as an implicit PROGN. For each SYMBOL naming a category, a
307 variable named after that symbol will be bound in the BODY's environment
308 to a list of the items in that category, in the same order in which they
309 were found in the list ITEMS. The final values of the macro are the final
310 values of the BODY."
311
312 (let* ((cat-names (mapcar #'car categories))
313 (cat-match-forms (mapcar #'cadr categories))
314 (cat-vars (mapcar (lambda (name) (gensym (symbol-name name)))
315 cat-names))
316 (items-var (gensym "ITEMS")))
317 `(let ((,items-var ,items)
318 ,@(mapcar (lambda (cat-var) (list cat-var nil)) cat-vars))
319 (dolist (,itemvar ,items-var)
320 (let* ,bind
321 (cond ,@(mapcar (lambda (cat-match-form cat-var)
322 `(,cat-match-form
323 (push ,itemvar ,cat-var)))
324 cat-match-forms cat-vars)
325 ,@(and (not (member t cat-match-forms))
326 `((t (error "Failed to categorize ~A" ,itemvar)))))))
327 (let ,(mapcar (lambda (name var)
328 `(,name (nreverse ,var)))
329 cat-names cat-vars)
330 ,@body))))
331
332 ;;;--------------------------------------------------------------------------
333 ;;; Code generation.
334
335 (defclass method-codegen (codegen)
336 ((message :initarg :message :type sod-message :reader codegen-message)
337 (class :initarg :class :type sod-class :reader codegen-class)
338 (method :initarg :method :type effective-method :reader codegen-method)
339 (target :initarg :target :reader codegen-target))
340 (:documentation
341 "Augments CODEGEN with additional state regarding an effective method.
342
343 We store the effective method, and also its target class and owning
344 message, so that these values are readily available to the code-generating
345 functions."))
346
347 (defmethod shared-initialize :after
348 ((codegen method-codegen) slot-names &key)
349 (with-slots (message target) codegen
350 (setf target
351 (if (eq (c-type-subtype (sod-message-type message)) (c-type void))
352 :void
353 :return))))
354
355 (defgeneric compute-effective-method-body (method codegen target)
356 (:documentation
357 "Generates the body of an effective method.
358
359 Writes the function body to the code generator. It can (obviously)
360 generate auxiliary functions if it needs to.
361
362 The arguments are as specified by the SOD-MESSAGE-NO-VARARGS-TAIL, with an
363 additional argument `sod__obj' of type pointer-to-ilayout. The code
364 should deliver the result (if any) to the TARGET."))
365
366 (defun invoke-method (codegen target arguments-tail direct-method)
367 "Emit code to invoke DIRECT-METHOD, passing it ARGUMENTS-TAIL.
368
369 The code is generated in the context of CODEGEN, which can be any instance
370 of the CODEGEN class -- it needn't be an instance of METHOD-CODEGEN. The
371 DIRECT-METHOD is called with the given ARGUMENTS-TAIL (a list of argument
372 expressions), preceded by a `me' argument of type pointer-to-CLASS where
373 CLASS is the class on which the method was defined.
374
375 If the message accepts a variable-length argument list then a copy of the
376 prevailing master argument pointer is provided in place of the :ELLIPSIS."
377
378 (let* ((message (sod-method-message direct-method))
379 (class (sod-method-class direct-method))
380 (function (direct-method-function-name direct-method))
381 (arguments (cons (format nil "(~A *)&sod__obj.~A" class
382 (sod-class-nickname
383 (sod-class-chain-head class)))
384 arguments-tail)))
385 (if (varargs-message-p message)
386 (convert-stmts codegen target
387 (c-type-subtype (sod-method-type direct-method))
388 (lambda (var)
389 (ensure-var codegen *sod-ap* (c-type va-list))
390 (emit-inst codegen
391 (make-va-copy-inst *sod-ap*
392 *sod-master-ap*))
393 (deliver-expr codegen var
394 (make-call-inst function arguments))
395 (emit-inst codegen
396 (make-va-end-inst *sod-ap*))))
397 (deliver-expr codegen target (make-call-inst function arguments)))))
398
399 (definst convert-to-ilayout (stream) (class chain-head expr)
400 (format stream "SOD_ILAYOUT(~@<~A, ~_~A, ~_~A~:>)"
401 class (sod-class-nickname chain-head) expr))
402
403 (defun ensure-ilayout-var (codegen super)
404 "Define a variable `sod__obj' pointing to the class's ilayout structure.
405
406 CODEGEN is a METHOD-CODEGEN. The class in question is CODEGEN's class,
407 i.e., the target class for the effective method. SUPER is one of the
408 class's superclasses; it is assumed that `me' is a pointer to a SUPER
409 (i.e., to SUPER's ichain within the ilayout)."
410
411 (let* ((class (codegen-class codegen))
412 (super-head (sod-class-chain-head super)))
413 (ensure-var codegen "sod__obj"
414 (c-type (* (struct (ilayout-struct-tag class))))
415 (make-convert-to-ilayout-inst class super-head "me"))))
416
417 (defun make-trampoline (codegen super body)
418 "Construct a trampoline function and return its name.
419
420 CODEGEN is a METHOD-CODEGEN. SUPER is a superclass of the CODEGEN class.
421 We construct a new trampoline function (with an unimaginative name)
422 suitable for being passed to a direct method defined on SUPER as its
423 `next_method'. In particular, it will have a `me' argument whose type is
424 pointer-to-SUPER.
425
426 The code of the function is generated by BODY, which will be invoked with
427 a single argument which is the TARGET to which it should deliver its
428 result.
429
430 The return value is the name of the generated function."
431
432 (let* ((message (codegen-message codegen))
433 (message-type (sod-message-type message))
434 (return-type (c-type-subtype message-type))
435 (arguments (mapcar (lambda (arg)
436 (if (eq (argument-name arg) *sod-ap*)
437 (make-argument *sod-master-ap*
438 (c-type va-list))
439 arg))
440 (sod-message-no-varargs-tail message))))
441 (codegen-push codegen)
442 (ensure-ilayout-var codegen super)
443 (funcall body (codegen-target codegen))
444 (codegen-pop-function codegen (temporary-function)
445 (c-type (fun (lisp return-type)
446 ("me" (* (class super)))
447 . arguments))))))
448
449 (defun invoke-delegation-chain (codegen target basic-tail chain kernel)
450 "Invoke a chain of delegating methods.
451
452 CODEGEN is a METHOD-CODEGEN. BASIC-TAIL is a list of argument expressions
453 to provide to the methods. The result of the delegation chain will be
454 delivered to TARGET.
455
456 The CHAIN is a list of DELEGATING-DIRECT-METHOD objects. The behaviour is
457 as follows. The first method in the chain is invoked with the necessary
458 arguments (see below) including a `next_method' pointer. If KERNEL is nil
459 and there are no more methods in the chain then the `next_method' pointer
460 will be null; otherwise it will point to a `trampoline' function, whose
461 behaviour is to call the remaining methods on the chain as a delegation
462 chain. The method may choose to call this function with its arguments.
463 It will finally return a value, which will be delivered to the TARGET.
464
465 If the chain is empty, then the code generated by KERNEL (given a TARGET
466 argument) will be invoked. It is an error if both CHAIN and KERNEL are
467 nil."
468
469 (let* ((message (codegen-message codegen))
470 (argument-tail (if (varargs-message-p message)
471 (cons *sod-master-ap* basic-tail)
472 basic-tail)))
473 (labels ((next-trampoline (method chain)
474 (if (or kernel chain)
475 (make-trampoline codegen (sod-method-class method)
476 (lambda (target)
477 (invoke chain target)))
478 0))
479 (invoke (chain target)
480 (if (null chain)
481 (funcall kernel target)
482 (let* ((trampoline (next-trampoline (car chain)
483 (cdr chain))))
484 (invoke-method codegen target
485 (cons trampoline argument-tail)
486 (car chain))))))
487 (invoke chain target))))
488
489 (defun basic-effective-method-body (codegen target method body)
490 "Build the common method-invocation structure.
491
492 Writes to CODEGEN some basic method-invocation instructions. It invokes
493 the `around' methods, from most- to least-specific. If they all delegate,
494 then the `before' methods are run, most-specific first; next, the
495 instructions generated by BODY (invoked with a target argument); then, the
496 `after' methods are run, least-specific first; and, finally, the value
497 delivered by the BODY is returned to the `around' methods. The result
498 returned by the outermost `around' method -- or, if there are none,
499 delivered by the BODY -- is finally delivered to the TARGET."
500
501 (with-slots (message class before-methods after-methods around-methods)
502 method
503 (let* ((message-type (sod-message-type message))
504 (return-type (c-type-subtype message-type))
505 (voidp (eq return-type (c-type void)))
506 (basic-tail (effective-method-basic-argument-names method)))
507 (flet ((method-kernel (target)
508 (dolist (before before-methods)
509 (invoke-method codegen :void basic-tail before))
510 (if (or voidp (null after-methods))
511 (funcall body target)
512 (convert-stmts codegen target return-type
513 (lambda (target)
514 (funcall body target)
515 (dolist (after (reverse after-methods))
516 (invoke-method codegen :void
517 after basic-tail)))))))
518 (invoke-delegation-chain codegen target basic-tail
519 around-methods #'method-kernel)))))
520
521 ;;;--------------------------------------------------------------------------
522 ;;; Effective method entry points.
523
524 (defgeneric compute-method-entry-functions (method)
525 (:documentation
526 "Construct method entry functions.
527
528 Builds the effective method function (if there is one) and the necessary
529 method entries. Returns a list of functions (i.e., FUNCTION-INST objects)
530 which need to be defined in the generated source code."))
531
532 (defparameter *method-entry-inline-threshold* 20
533 "Threshold below which effective method bodies are inlined into entries.
534
535 After the effective method body has been computed, we calculate its
536 metric, multiply by the number of entries we need to generate, and compare
537 it with this threshold. If the metric is below the threshold then we
538 fold the method body into the entry functions; otherwise we split the
539 effective method out into its own function.")
540
541 (defgeneric effective-method-function-name (method)
542 (:documentation
543 "Returns the function name of an effective method."))
544
545 (defgeneric method-entry-function-name (method chain-head)
546 (:documentation
547 "Returns the function name of a method entry.
548
549 The method entry is given as an effective method/chain-head pair, rather
550 than as a method entry object because we want the function name before
551 we've made the entry object."))
552
553 (defmethod effective-method-function-name ((method effective-method))
554 (let* ((class (effective-method-class method))
555 (message (effective-method-message method))
556 (message-class (sod-message-class message)))
557 (format nil "~A__emethod_~A__~A"
558 class
559 (sod-class-nickname message-class)
560 (sod-message-name message))))
561
562 (defmethod method-entry-function-name
563 ((method effective-method) (chain-head sod-class))
564 (let* ((class (effective-method-class method))
565 (message (effective-method-message method))
566 (message-class (sod-message-class message)))
567 (format nil "~A__mentry_~A__~A__~A"
568 class
569 (sod-class-nickname message-class)
570 (sod-message-name message)
571 (sod-class-nickname chain-head))))
572
573 (defmethod compute-method-entry-functions ((method basic-effective-method))
574
575 ;; OK, there's quite a lot of this, so hold tight.
576 ;;
577 ;; The first thing we need to do is find all of the related objects. This
578 ;; is a bit verbose but fairly straightforward.
579 ;;
580 ;; Next, we generate the effective method body -- using COMPUTE-EFFECTIVE-
581 ;; METHOD-BODY of all things. This gives us the declarations and body for
582 ;; an effective method function, but we don't have an actual function yet.
583 ;;
584 ;; Now we look at the chains which are actually going to need a method
585 ;; entry: only those chains whose tail (most specific) class is a
586 ;; superclass of the class which defined the message need an entry. We
587 ;; build a list of these tail classes.
588 ;;
589 ;; Having done this, we decide whether it's better to generate a standalone
590 ;; effective-method function and call it from each of the method entries,
591 ;; or to inline the effective method body into each of the entries.
592 ;;
593 ;; Most of the complexity here comes from (a) dealing with the two
594 ;; different strategies for constructing method entry functions and (b)
595 ;; (unsurprisingly) the mess involved with dealing with varargs messages.
596
597 (let* ((message (effective-method-message method))
598 (class (effective-method-class method))
599 (message-class (sod-message-class message))
600 (return-type (c-type-subtype (sod-message-type message)))
601 (codegen (make-instance 'method-codegen
602 :message message
603 :class class
604 :method method))
605
606 ;; Effective method function details.
607 (emf-name (effective-method-function-name method))
608 (ilayout-type (c-type (* (struct (ilayout-struct-tag class)))))
609 (emf-arg-tail (mapcar (lambda (arg)
610 (if (eq (argument-name arg) *sod-ap*)
611 (make-argument *sod-master-ap*
612 (c-type va-list))
613 arg))
614 (sod-message-no-varargs-tail message)))
615 (emf-type (c-type (fun (lisp return-type)
616 ("sod__obj" (lisp ilayout-type))
617 . (sod-message-no-varargs-tail message))))
618 (result (if (eq return-type (c-type void)) nil
619 (temporary-var codegen return-type)))
620 (emf-target (or result :void))
621
622 ;; Method entry details.
623 (chain-tails (remove-if-not (lambda (super)
624 (sod-subclass-p super message-class))
625 (mapcar #'car
626 (sod-class-chains class))))
627 (n-entries (length chain-tails))
628 (entry-args (sod-message-argument-tail message))
629 (parm-n (do ((prev "me" (car args))
630 (args entry-args (cdr args)))
631 ((endp args) nil)
632 (when (eq (car args) :ellipsis)
633 (return prev))))
634 (entry-target (codegen-target codegen)))
635
636 (labels ((setup-entry (tail)
637 (let ((head (sod-class-chain-head tail)))
638 (codegen-push codegen)
639 (ensure-var codegen "sod__obj" ilayout-type
640 (make-convert-to-ilayout-inst class
641 head "me"))))
642 (varargs-prologue ()
643 (ensure-var codegen *sod-master-ap* (c-type va-list))
644 (emit-inst codegen
645 (make-va-start-inst *sod-master-ap* parm-n)))
646 (varargs-epilogue ()
647 (emit-inst codegen (make-va-end-inst *sod-master-ap*)))
648 (finish-entry (tail)
649 (let* ((head (sod-class-chain-head tail))
650 (name (method-entry-function-name method head))
651 (type (c-type (fun (lisp return-type)
652 ("me" (* (class tail)))
653 . entry-args))))
654 (codegen-pop-function codegen name type))))
655
656 ;; Generate the method body. We'll work out what to do with it later.
657 (codegen-push codegen)
658 (compute-effective-method-body method codegen emf-target)
659 (multiple-value-bind (vars insts) (codegen-pop codegen)
660 (cond ((or (= n-entries 1)
661 (<= (* n-entries (reduce #'+ insts :key #'inst-metric))
662 *method-entry-inline-threshold*))
663
664 ;; The effective method body is simple -- or there's only one
665 ;; of them. We'll inline the method body into the entry
666 ;; functions.
667 (dolist (tail chain-tails)
668 (setup-entry tail)
669 (dolist (var vars)
670 (ensure-var codegen (inst-name var)
671 (inst-type var) (inst-init var)))
672 (when parm-n (varargs-prologue))
673 (emit-insts codegen insts)
674 (when parm-n (varargs-epilogue))
675 (deliver-expr codegen entry-target result)
676 (finish-entry tail)))
677
678 (t
679
680 ;; The effective method body is complicated and we'd need more
681 ;; than one copy. We'll generate an effective method function
682 ;; and call it a lot.
683 (codegen-build-function codegen emf-name emf-type vars
684 (nconc insts (and result (list (make-return-inst result)))))
685
686 (let ((call (make-call-inst emf-name
687 (cons "sod__obj" (mapcar #'argument-name
688 emf-arg-tail)))))
689 (dolist (tail chain-tails)
690 (setup-entry tail)
691 (cond (parm-n
692 (varargs-prologue)
693 (convert-stmts codegen entry-target return-type
694 (lambda (target)
695 (deliver-expr codegen target call)
696 (varargs-epilogue))))
697 (t
698 (deliver-expr codegen entry-target call)))
699 (finish-entry tail))))))
700
701 (codegen-functions codegen))))
702
703 (defmethod slot-unbound
704 (class (method basic-effective-method) (slot-name (eql 'functions)))
705 (setf (slot-value method 'functions)
706 (compute-method-entry-functions method)))
707
708 (defmethod make-method-entry
709 ((method basic-effective-method) (chain-head sod-class))
710 (make-instance 'method-entry :method method :chain-head chain-head))
711
712 ;;;----- That's all, folks --------------------------------------------------