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