2 * mini-arm-gsharedvt.c: gsharedvt support code for arm
5 * Zoltan Varga <vargaz@gmail.com>
7 * Copyright 2013 Xamarin, Inc (http://www.xamarin.com)
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13 #include <mono/metadata/abi-details.h>
14 #include <mono/metadata/appdomain.h>
15 #include <mono/metadata/marshal.h>
16 #include <mono/metadata/tabledefs.h>
17 #include <mono/metadata/profiler-private.h>
18 #include <mono/arch/arm/arm-codegen.h>
19 #include <mono/arch/arm/arm-vfp-codegen.h>
24 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
26 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
33 mono_arch_gsharedvt_sig_supported (MonoMethodSignature *sig)
36 if (sig->ret && is_variable_size (sig->ret))
43 add_to_map (GPtrArray *map, int src, int dst)
45 g_ptr_array_add (map, GUINT_TO_POINTER (src));
46 g_ptr_array_add (map, GUINT_TO_POINTER (dst));
56 map_stack_slot (int slot)
62 get_arg_slots (ArgInfo *ainfo, int **out_slots)
64 int sreg = ainfo->reg;
65 int sslot = ainfo->offset / 4;
69 switch (ainfo->storage) {
72 src = g_malloc (nsrc * sizeof (int));
73 src [0] = map_reg (sreg);
77 src = g_malloc (nsrc * sizeof (int));
78 src [0] = map_reg (sreg);
79 src [1] = map_reg (sreg + 1);
81 case RegTypeStructByVal:
82 nsrc = ainfo->struct_size / 4;
83 src = g_malloc (nsrc * sizeof (int));
84 g_assert (ainfo->size <= nsrc);
85 for (i = 0; i < ainfo->size; ++i)
86 src [i] = map_reg (sreg + i);
87 for (i = ainfo->size; i < nsrc; ++i)
88 src [i] = map_stack_slot (sslot + (i - ainfo->size));
91 nsrc = ainfo->size / 4;
92 src = g_malloc (nsrc * sizeof (int));
93 for (i = 0; i < nsrc; ++i)
94 src [i] = map_stack_slot (sslot + i);
98 src = g_malloc (nsrc * sizeof (int));
99 src [0] = map_reg (ARMREG_R3);
100 src [1] = map_stack_slot (sslot);
103 g_assert_not_reached ();
112 * mono_arch_get_gsharedvt_call_info:
114 * See mini-x86.c for documentation.
117 mono_arch_get_gsharedvt_call_info (gpointer addr, MonoMethodSignature *normal_sig, MonoMethodSignature *gsharedvt_sig, gboolean gsharedvt_in, gint32 vcall_offset, gboolean calli)
119 GSharedVtCallInfo *info;
120 CallInfo *caller_cinfo, *callee_cinfo;
121 MonoMethodSignature *caller_sig, *callee_sig;
123 gboolean var_ret = FALSE;
124 CallInfo *cinfo, *gcinfo;
125 MonoMethodSignature *sig, *gsig;
129 caller_sig = normal_sig;
130 callee_sig = gsharedvt_sig;
131 caller_cinfo = mono_arch_get_call_info (NULL, caller_sig);
132 callee_cinfo = mono_arch_get_call_info (NULL, callee_sig);
134 callee_sig = normal_sig;
135 callee_cinfo = mono_arch_get_call_info (NULL, callee_sig);
136 caller_sig = gsharedvt_sig;
137 caller_cinfo = mono_arch_get_call_info (NULL, caller_sig);
141 * If GSHAREDVT_IN is true, this means we are transitioning from normal to gsharedvt code. The caller uses the
142 * normal call signature, while the callee uses the gsharedvt signature.
143 * If GSHAREDVT_IN is false, its the other way around.
146 /* sig/cinfo describes the normal call, while gsig/gcinfo describes the gsharedvt call */
150 cinfo = caller_cinfo;
151 gcinfo = callee_cinfo;
155 cinfo = callee_cinfo;
156 gcinfo = caller_cinfo;
159 if (gcinfo->ret.storage == RegTypeStructByAddr && gsig->ret && mini_is_gsharedvt_type (gsig->ret)) {
161 * The return type is gsharedvt
167 * The stack looks like this:
172 * We have to map the stack slots in <arguments> to the stack slots in <call area>.
173 * The argument registers are mapped to slot 0..3, stack slot 0 is mapped to slot 4, etc.
175 map = g_ptr_array_new ();
177 if (cinfo->ret.storage == RegTypeStructByAddr) {
180 * This handles the case when the method returns a normal vtype, and when it returns a type arg, and its instantiated
183 g_assert (caller_cinfo->ret.storage == RegTypeStructByAddr);
184 g_assert (callee_cinfo->ret.storage == RegTypeStructByAddr);
185 add_to_map (map, map_reg (caller_cinfo->ret.reg), map_reg (callee_cinfo->ret.reg));
188 for (aindex = 0; aindex < cinfo->nargs; ++aindex) {
189 ArgInfo *ainfo = &caller_cinfo->args [aindex];
190 ArgInfo *ainfo2 = &callee_cinfo->args [aindex];
191 int *src = NULL, *dst = NULL;
192 int nsrc, ndst, nslots, src_slot, arg_marshal;
195 * The src descriptor looks like this:
197 * - 12 bits number of slots
198 * - 8 bits marshal type (GSHAREDVT_ARG_...)
201 arg_marshal = GSHAREDVT_ARG_NONE;
203 if (ainfo->storage == RegTypeGSharedVtInReg || ainfo->storage == RegTypeGSharedVtOnStack) {
204 /* Pass the value whose address is received in a reg by value */
205 g_assert (ainfo2->storage != RegTypeGSharedVtInReg);
206 ndst = get_arg_slots (ainfo2, &dst);
208 src = g_new0 (int, 1);
209 if (ainfo->storage == RegTypeGSharedVtInReg)
210 src_slot = map_reg (ainfo->reg);
212 src_slot = map_stack_slot (ainfo->offset / 4);
213 g_assert (ndst < 256);
214 g_assert (src_slot < 16);
215 src [0] = (ndst << 4) | src_slot;
217 if (ainfo2->storage == RegTypeGeneral && ainfo2->size != 0 && ainfo2->size != 4) {
218 /* Have to load less than 4 bytes */
219 // FIXME: Signed types
220 switch (ainfo2->size) {
222 arg_marshal = GSHAREDVT_ARG_BYREF_TO_BYVAL_U1;
225 arg_marshal = GSHAREDVT_ARG_BYREF_TO_BYVAL_U2;
228 g_assert_not_reached ();
232 arg_marshal = GSHAREDVT_ARG_BYREF_TO_BYVAL;
235 nsrc = get_arg_slots (ainfo, &src);
237 if (ainfo2->storage == RegTypeGSharedVtInReg) {
238 /* Pass the address of the first src slot in a reg */
239 arg_marshal = GSHAREDVT_ARG_BYVAL_TO_BYREF;
241 dst = g_new0 (int, 1);
242 dst [0] = map_reg (ainfo2->reg);
243 } else if (ainfo2->storage == RegTypeGSharedVtOnStack) {
244 /* Pass the address of the first src slot in a stack slot */
245 arg_marshal = GSHAREDVT_ARG_BYVAL_TO_BYREF;
247 dst = g_new0 (int, 1);
248 dst [0] = map_stack_slot (ainfo2->offset / 4);
250 ndst = get_arg_slots (ainfo2, &dst);
253 src [0] |= (arg_marshal << 16);
254 nslots = MIN (nsrc, ndst);
256 for (i = 0; i < nslots; ++i)
257 add_to_map (map, src [i], dst [i]);
263 info = mono_domain_alloc0 (mono_domain_get (), sizeof (GSharedVtCallInfo) + (map->len * sizeof (int)));
265 info->stack_usage = callee_cinfo->stack_usage;
266 info->ret_marshal = GSHAREDVT_RET_NONE;
267 info->gsharedvt_in = gsharedvt_in ? 1 : 0;
268 info->vret_slot = -1;
271 g_assert (gcinfo->ret.storage == RegTypeStructByAddr);
272 info->vret_arg_reg = gcinfo->ret.reg;
274 info->vret_arg_reg = -1;
276 info->vcall_offset = vcall_offset;
277 info->map_count = map->len / 2;
278 for (i = 0; i < map->len; ++i)
279 info->map [i] = GPOINTER_TO_UINT (g_ptr_array_index (map, i));
280 g_ptr_array_free (map, TRUE);
282 /* Compute return value marshalling */
284 switch (cinfo->ret.storage) {
286 if (gsharedvt_in && !sig->ret->byref && sig->ret->type == MONO_TYPE_I1)
287 info->ret_marshal = GSHAREDVT_RET_I1;
288 else if (gsharedvt_in && !sig->ret->byref && (sig->ret->type == MONO_TYPE_U1 || sig->ret->type == MONO_TYPE_BOOLEAN))
289 info->ret_marshal = GSHAREDVT_RET_U1;
290 else if (gsharedvt_in && !sig->ret->byref && sig->ret->type == MONO_TYPE_I2)
291 info->ret_marshal = GSHAREDVT_RET_I2;
292 else if (gsharedvt_in && !sig->ret->byref && (sig->ret->type == MONO_TYPE_U2 || sig->ret->type == MONO_TYPE_CHAR))
293 info->ret_marshal = GSHAREDVT_RET_U2;
295 info->ret_marshal = GSHAREDVT_RET_IREG;
297 case RegTypeIRegPair:
298 info->ret_marshal = GSHAREDVT_RET_IREGS;
302 if (cinfo->ret.size == 4)
303 info->ret_marshal = GSHAREDVT_RET_IREG;
305 info->ret_marshal = GSHAREDVT_RET_IREGS;
307 case RegTypeStructByAddr:
308 info->ret_marshal = GSHAREDVT_RET_NONE;
311 g_assert_not_reached ();
315 if (gsharedvt_in && var_ret && caller_cinfo->ret.storage != RegTypeStructByAddr) {
316 /* Allocate stack space for the return value */
317 info->vret_slot = map_stack_slot (info->stack_usage / sizeof (gpointer));
318 info->stack_usage += mono_type_stack_size_internal (normal_sig->ret, NULL, FALSE) + sizeof (gpointer);
321 info->stack_usage = ALIGN_TO (info->stack_usage, MONO_ARCH_FRAME_ALIGNMENT);
323 g_free (caller_cinfo);
324 g_free (callee_cinfo);