2008-07-10 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / docs / generic-sharing
1 -*- outline -*-
2
3 Generic code sharing
4 ====================
5
6 * Porting
7
8 ** Generic class init trampoline
9
10 The generic class init trampoline is very similar to the class init
11 trampoline, with the exception that the VTable of the class to be
12 inited is passed in a register, MONO_ARCH_VTABLE_REG.  That can be the
13 same as the IMT register.
14
15 The call to the generic class init trampoline is never patched because
16 the VTable is not constant - it depends on the type arguments of the
17 class/method doing the call to the trampoline.  For that reason, the
18 trampoline needs a fast path for the case that the class is already
19 inited.  See tramp-x86.c for how to portably figure out the number of
20 the bit in the bit-field that needs to be checked.
21
22 ** RGCTX register
23
24 Generic shared code needs access to type information.  This
25 information is contained in a RGCTX for non-generic methods and in an
26 MRGCTX for generic methods.  It is passed in one of several ways,
27 depending on the type of the called method:
28
29   1. Non-generic non-static methods of reference types have access to
30      the RGCTX via the "this" argument (this->vtable->rgctx).
31
32   2. Non-generic static methods of reference types and non-generic
33      methods of value types need to be passed a pointer to the
34      caller's class's VTable in the MONO_ARCH_RGCTX_REG register.
35
36   3. Generic methods need to be passed a pointer to the MRGCTX in the
37      MONO_ARCH_RGCTX_REG register.
38
39 The MONO_ARCH_RGCTX_REG must not be clobbered by trampolines.
40
41 MONO_ARCH_RGCTX_REG can be the same as the IMT register for now, but
42 this might change in the future when we implement virtual generic
43 method calls (more) efficiently.
44
45 ** Dealing with types
46
47 Types passed to arch functions might be type parameters
48 (MONO_TYPE_(M)VAR) if the MonoGenericSharingContext* argument is
49 non-NULL.  For example, an argument or return type in a method passed
50 to mono_arch_find_this_argument() could be a MONO_TYPE_VAR.  To guard
51 for that case use mini_get_basic_type_from_generic() on the type.  See
52 get_call_info() in mini-x86.c, for example.
53
54 ** (M)RGCTX lazy fetch trampoline
55
56 The purpose of the lazy fetch trampoline is to fetch a slot from an
57 (M)RGCTX which might not be inited, yet.  In the latter case, it needs
58 to go make a transition to unmanaged code to fill the slot.  This is
59 the layout of a RGCTX:
60
61      +---------------------------------+
62      | next | slot 0 | slot 1 | slot 2 |
63      +--|------------------------------+
64         |
65   +-----+
66   |  +---------------------------------
67   +->| next | slot 3 | slot 4 | slot 5 ....
68      +--|------------------------------
69         |
70   +-----+
71   |  +------------------------------------
72   +->| next | slot 10 | slot 11 | slot 12 ....
73      +--|---------------------------------
74         .
75         .
76         .
77
78 For fetching a slot from a RGCTX the trampoline is passed a pointer to
79 the VTable.  From there it has to fetch the pointer to the RGCTX,
80 which might be null.  Then it has to traverse the correct number of
81 "next" links, each of which might be NULL.  Arriving at the right
82 array it needs to fetch the slot, which might also be NULL.  If any of
83 the NULL cases, the trampoline must transition to unmanaged code to
84 potentially setup the RGCTX and fill the slot.  Here is pseudo-code
85 for fetching slot 11:
86
87     ; vtable ptr in r1
88     ; fetch RGCTX array 0
89     r2 = *(r1 + offsetof(MonoVTable, runtime_generic_context))
90     if r2 == NULL goto unmanaged
91     ; fetch RGCTX array 1
92     r2 = *r2
93     if r2 == NULL goto unmanaged
94     ; fetch RGCTX array 2
95     r2 = *r2
96     if r2 == NULL goto unmanaged
97     ; fetch slot 11
98     r2 = *(r2 + 2 * sizeof (gpointer))
99     if r2 == NULL goto unmanaged
100     return r2
101   unmanaged:
102     jump unmanaged_fetch_code
103
104 The number of slots in the arrays must be obtained from the function
105 mono_class_rgctx_get_array_size().
106
107 The MRGCTX case is different in two aspects.  First, the trampoline is
108 not passed a pointer to a VTable, but a pointer directly to the
109 MRGCTX, which is guaranteed not to be NULL (any of the next pointers
110 and any of the slots can be NULL, though).  Second, the layout of the
111 first array is slightly different, in that the first two slots are
112 occupied by a pointers to the class's VTable and to the method's
113 method_inst.  The next pointer is in the third slot and the first
114 actual slot, "slot 0", in the fourth:
115
116      +--------------------------------------------------------+
117      | vtable | method_inst | next | slot 0 | slot 1 | slot 2 |
118      +-------------------------|------------------------------+
119                                .
120                                .
121
122 All other arrays have the same layout as the RGCTX ones, except
123 possibly for their length.
124
125 The function to create the trampoline,
126 mono_arch_create_rgctx_lazy_fetch_trampoline(), gets passed an encoded
127 slot number.  Use the macro MONO_RGCTX_SLOT_IS_MRGCTX to query whether
128 a trampoline for an MRGCTX is needed, as opposed to one for a RGCTX.
129 Use MONO_RGCTX_SLOT_INDEX to get the index of the slot (like 2 for
130 "slot 2" as above).
131
132
133 * Getting generics information about a stack frame
134
135 If a method is compiled with generic sharing, its MonoJitInfo has
136 has_generic_jit_info set.  In that case, the MonoJitInfo is followed
137 (after the MonoJitExceptionInfo array) by a MonoGenericJitInfo.
138
139 The MonoGenericJitInfo contains information about the location of the
140 this/vtable/MRGCTX variable, if the has_this flag is set.  If that is
141 the case, there are two possibilities:
142
143   1. this_in_reg is set.  this_reg is the number of the register where
144      the variable is stored.
145
146   2. this_in_reg is not set.  The variable is stored at offset
147      this_offset from the address in the register with number
148      this_reg.
149
150 The variable can either point to the "this" object, to a vtable or to
151 an MRGCTX:
152
153   1. If the method is a non-generic non-static method of a reference
154      type, the variable points to the "this" object.
155
156   2. If the method is a non-generic static method or a non-generic
157      method of a value type, the variable points to the vtable of the
158      class.
159
160   3. If the method is a generic method, the variable points to the
161      MRGCTX of the method.
162
163
164 * Layout of the MRGCTX
165
166 The MRGCTX is a structure that starts with
167 MonoMethodRuntimeGenericContext, which contains a pointer to the
168 vtable of the class and a pointer to the MonoGenericInst with the type
169 arguments for the method.
170
171
172 * Blog posts about generic code sharing
173
174 http://schani.wordpress.com/2007/09/22/generics-sharing-in-mono/
175 http://schani.wordpress.com/2007/10/12/the-trouble-with-shared-generics/
176 http://schani.wordpress.com/2007/10/15/a-quick-generics-sharing-update/
177 http://schani.wordpress.com/2008/01/29/other-types/
178 http://schani.wordpress.com/2008/02/25/generic-types-are-lazy/
179 http://schani.wordpress.com/2008/03/10/sharing-static-methods/
180 http://schani.wordpress.com/2008/04/22/sharing-everything-and-saving-memory/
181 http://schani.wordpress.com/2008/06/02/sharing-generic-methods/
182 http://schani.wordpress.com/2008/06/27/another-generic-sharing-update/