2010-01-18 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / tramp-arm.c
1 /*
2  * tramp-arm.c: JIT trampoline code for ARM
3  *
4  * Authors:
5  *   Paolo Molaro (lupus@ximian.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  */
9
10 #include <config.h>
11 #include <glib.h>
12
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/marshal.h>
15 #include <mono/metadata/tabledefs.h>
16 #include <mono/arch/arm/arm-codegen.h>
17
18 #include "mini.h"
19 #include "mini-arm.h"
20
21 static guint8* nullified_class_init_trampoline;
22
23 /*
24  * Return the instruction to jump from code to target, 0 if not
25  * reachable with a single instruction
26  */
27 static guint32
28 branch_for_target_reachable (guint8 *branch, guint8 *target)
29 {
30         gint diff = target - branch - 8;
31         g_assert ((diff & 3) == 0);
32         if (diff >= 0) {
33                 if (diff <= 33554431)
34                         return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | (diff >> 2);
35         } else {
36                 /* diff between 0 and -33554432 */
37                 if (diff >= -33554432)
38                         return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | ((diff >> 2) & ~0xff000000);
39         }
40         return 0;
41 }
42
43 static inline guint8*
44 emit_bx (guint8* code, int reg)
45 {
46         if (mono_arm_thumb_supported ())
47                 ARM_BX (code, reg);
48         else
49                 ARM_MOV_REG_REG (code, ARMREG_PC, reg);
50         return code;
51 }
52
53 /*
54  * mono_arch_get_unbox_trampoline:
55  * @gsctx: the generic sharing context
56  * @m: method pointer
57  * @addr: pointer to native code for @m
58  *
59  * when value type methods are called through the vtable we need to unbox the
60  * this argument. This method returns a pointer to a trampoline which does
61  * unboxing before calling the method
62  */
63 gpointer
64 mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
65 {
66         guint8 *code, *start;
67         int this_pos = 0;
68         MonoDomain *domain = mono_domain_get ();
69
70         if (MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
71                 this_pos = 1;
72
73         start = code = mono_domain_code_reserve (domain, 16);
74
75         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
76         ARM_ADD_REG_IMM8 (code, this_pos, this_pos, sizeof (MonoObject));
77         code = emit_bx (code, ARMREG_IP);
78         *(guint32*)code = (guint32)addr;
79         code += 4;
80         mono_arch_flush_icache (start, code - start);
81         g_assert ((code - start) <= 16);
82         /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
83         g_print ("unbox code is at %p for method at %p\n", start, addr);*/
84
85         return start;
86 }
87
88 gpointer
89 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
90 {
91         guint8 *code, *start;
92         int buf_len;
93
94         MonoDomain *domain = mono_domain_get ();
95
96         buf_len = 16;
97
98         start = code = mono_domain_code_reserve (domain, buf_len);
99
100         ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, 0);
101         ARM_LDR_IMM (code, ARMREG_PC, ARMREG_PC, 0);
102         *(guint32*)code = (guint32)mrgctx;
103         code += 4;
104         *(guint32*)code = (guint32)addr;
105         code += 4;
106
107         g_assert ((code - start) <= buf_len);
108
109         mono_arch_flush_icache (start, code - start);
110
111         return start;
112 }
113
114 void
115 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
116 {
117         guint32 *code = (guint32*)code_ptr;
118
119         /* This is the 'bl' or the 'mov pc' instruction */
120         --code;
121         
122         /*
123          * Note that methods are called also with the bl opcode.
124          */
125         if ((((*code) >> 25)  & 7) == 5) {
126                 /*g_print ("direct patching\n");*/
127                 arm_patch ((guint8*)code, addr);
128                 mono_arch_flush_icache ((guint8*)code, 4);
129                 return;
130         }
131
132         if ((((*code) >> 20) & 0xFF) == 0x12) {
133                 /*g_print ("patching bx\n");*/
134                 arm_patch ((guint8*)code, addr);
135                 mono_arch_flush_icache ((guint8*)(code - 2), 4);
136                 return;
137         }
138
139         g_assert_not_reached ();
140 }
141
142 void
143 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
144 {
145         guint8 *jump_entry;
146
147         /* Patch the jump table entry used by the plt entry */
148         if (*(guint32*)code == 0xe59fc000) {
149                 /* ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0); */
150                 guint32 offset = ((guint32*)code)[2];
151                 
152                 jump_entry = code + offset + 12;
153         } else {
154                 g_assert_not_reached ();
155         }
156
157         *(guint8**)jump_entry = addr;
158 }
159
160 void
161 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
162 {
163         mono_arch_patch_callsite (NULL, code, nullified_class_init_trampoline);
164 }
165
166 void
167 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
168 {
169         if (mono_aot_only && !nullified_class_init_trampoline)
170                 nullified_class_init_trampoline = mono_aot_get_named_code ("nullified_class_init_trampoline");
171
172         mono_arch_patch_plt_entry (code, NULL, regs, nullified_class_init_trampoline);
173 }
174
175 /* Stack size for trampoline function 
176  */
177 #define STACK (sizeof (MonoLMF))
178
179 /* Method-specific trampoline code fragment size */
180 #define METHOD_TRAMPOLINE_SIZE 64
181
182 /* Jump-specific trampoline code fragment size */
183 #define JUMP_TRAMPOLINE_SIZE   64
184
185 #define GEN_TRAMP_SIZE 196
186
187 /*
188  * Stack frame description when the generic trampoline is called.
189  * caller frame
190  * ------------------- old sp
191  *  MonoLMF
192  * ------------------- sp
193  */
194 guchar*
195 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
196 {
197         MonoJumpInfo *ji;
198         guint32 code_size;
199         guchar *code;
200         GSList *unwind_ops, *l;
201
202         code = mono_arch_create_trampoline_code_full (tramp_type, &code_size, &ji, &unwind_ops, FALSE);
203
204         mono_save_trampoline_xdebug_info ("<generic_trampoline>", code, code_size, unwind_ops);
205
206         for (l = unwind_ops; l; l = l->next)
207                 g_free (l->data);
208         g_slist_free (unwind_ops);
209
210         return code;
211 }
212         
213 guchar*
214 mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *code_size, MonoJumpInfo **ji, GSList **out_unwind_ops, gboolean aot)
215 {
216         guint8 *buf, *code = NULL;
217         guint8 *load_get_lmf_addr, *load_trampoline;
218         gpointer *constants;
219         GSList *unwind_ops = NULL;
220         int cfa_offset;
221
222         *ji = NULL;
223
224         /* Now we'll create in 'buf' the ARM trampoline code. This
225          is the trampoline code common to all methods  */
226         
227         code = buf = mono_global_codeman_reserve (GEN_TRAMP_SIZE);
228
229         /*
230          * At this point lr points to the specific arg and sp points to the saved
231          * regs on the stack (all but PC and SP). The original LR value has been
232          * saved as sp + LR_OFFSET by the push in the specific trampoline
233          */
234 #define LR_OFFSET (sizeof (gpointer) * 13)
235
236         // FIXME: Finish the unwind info, the current info allows us to unwind
237         // when the trampoline is not in the epilog
238
239         // CFA = SP + (num registers pushed) * 4
240         cfa_offset = 14 * sizeof (gpointer);
241         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, cfa_offset);
242         // PC saved at sp+LR_OFFSET
243         mono_add_unwind_op_offset (unwind_ops, code, buf, ARMREG_LR, -4);
244
245         ARM_MOV_REG_REG (code, ARMREG_V1, ARMREG_SP);
246         if (aot && tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
247                 /* 
248                  * The trampoline contains a pc-relative offset to the got slot 
249                  * preceeding the got slot where the value is stored. The offset can be
250                  * found at [lr + 0].
251                  */
252                 ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
253                 ARM_ADD_REG_IMM (code, ARMREG_V2, ARMREG_V2, 4, 0);
254                 ARM_LDR_REG_REG (code, ARMREG_V2, ARMREG_V2, ARMREG_LR);
255         } else {
256                 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
257                         ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
258                 else
259                         ARM_MOV_REG_REG (code, ARMREG_V2, MONO_ARCH_VTABLE_REG);
260         }
261         ARM_LDR_IMM (code, ARMREG_V3, ARMREG_SP, LR_OFFSET);
262
263         /* ok, now we can continue with the MonoLMF setup, mostly untouched 
264          * from emit_prolog in mini-arm.c
265          * This is a synthetized call to mono_get_lmf_addr ()
266          */
267         if (aot) {
268                 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
269                 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
270                 ARM_B (code, 0);
271                 *(gpointer*)code = NULL;
272                 code += 4;
273                 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
274         } else {
275                 load_get_lmf_addr = code;
276                 code += 4;
277         }
278         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
279         code = emit_bx (code, ARMREG_R0);
280
281         /* we build the MonoLMF structure on the stack - see mini-arm.h
282          * The pointer to the struct is put in r1.
283          * the iregs array is already allocated on the stack by push.
284          */
285         ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, sizeof (MonoLMF) - sizeof (guint) * 14);
286         cfa_offset += sizeof (MonoLMF) - sizeof (guint) * 14;
287         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
288         ARM_ADD_REG_IMM8 (code, ARMREG_R1, ARMREG_SP, STACK - sizeof (MonoLMF));
289         /* r0 is the result from mono_get_lmf_addr () */
290         ARM_STR_IMM (code, ARMREG_R0, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
291         /* new_lmf->previous_lmf = *lmf_addr */
292         ARM_LDR_IMM (code, ARMREG_R2, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
293         ARM_STR_IMM (code, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
294         /* *(lmf_addr) = r1 */
295         ARM_STR_IMM (code, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
296         /* save method info (it's in v2) */
297         if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
298                 ARM_STR_IMM (code, ARMREG_V2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, method));
299         else {
300                 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
301                 ARM_STR_IMM (code, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, method));
302         }
303         ARM_STR_IMM (code, ARMREG_SP, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, ebp));
304         /* save the IP (caller ip) */
305         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
306                 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
307         } else {
308                 /* assumes STACK == sizeof (MonoLMF) */
309                 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_SP, (G_STRUCT_OFFSET (MonoLMF, iregs) + 13*4));
310         }
311         ARM_STR_IMM (code, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, eip));
312
313         /*
314          * Now we're ready to call xxx_trampoline ().
315          */
316         /* Arg 1: the saved registers. It was put in v1 */
317         ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_V1);
318
319         /* Arg 2: code (next address to the instruction that called us) */
320         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
321                 ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0);
322         } else {
323                 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_V3);
324         }
325         
326         /* Arg 3: the specific argument, stored in v2
327          */
328         ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_V2);
329
330         if (aot) {
331                 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
332                 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
333                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
334                 ARM_B (code, 0);
335                 *(gpointer*)code = NULL;
336                 code += 4;
337                 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
338         } else {
339                 load_trampoline = code;
340                 code += 4;
341         }
342
343         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
344         code = emit_bx (code, ARMREG_IP);
345         
346         /* OK, code address is now on r0. Move it to the place on the stack
347          * where IP was saved (it is now no more useful to us and it can be
348          * clobbered). This way we can just restore all the regs in one inst
349          * and branch to IP.
350          */
351         ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, (ARMREG_R12 * 4));
352
353         /* Check for thread interruption */
354         /* This is not perf critical code so no need to check the interrupt flag */
355         /* 
356          * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
357          */
358         if (aot) {
359                 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
360                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
361                 ARM_B (code, 0);
362                 *(gpointer*)code = NULL;
363                 code += 4;
364                 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
365         } else {
366                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
367                 ARM_B (code, 0);
368                 *(gpointer*)code = mono_thread_force_interruption_checkpoint;
369                 code += 4;
370         }
371         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
372         code = emit_bx (code, ARMREG_IP);
373
374         /*
375          * Now we restore the MonoLMF (see emit_epilogue in mini-arm.c)
376          * and the rest of the registers, so the method called will see
377          * the same state as before we executed.
378          * The pointer to MonoLMF is in r2.
379          */
380         ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_SP);
381         /* ip = previous_lmf */
382         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R2, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
383         /* lr = lmf_addr */
384         ARM_LDR_IMM (code, ARMREG_LR, ARMREG_R2, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
385         /* *(lmf_addr) = previous_lmf */
386         ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
387
388         /* Non-standard function epilogue. Instead of doing a proper
389          * return, we just jump to the compiled code.
390          */
391         /* Restore the registers and jump to the code:
392          * Note that IP has been conveniently set to the method addr.
393          */
394         ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, sizeof (MonoLMF) - sizeof (guint) * 14);
395         ARM_POP_NWB (code, 0x5fff);
396         if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
397                 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_IP);
398         /* do we need to set sp? */
399         ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, (14 * 4));
400         if ((tramp_type == MONO_TRAMPOLINE_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH))
401                 code = emit_bx (code, ARMREG_LR);
402         else
403                 code = emit_bx (code, ARMREG_IP);
404
405         constants = (gpointer*)code;
406         constants [0] = mono_get_lmf_addr;
407         constants [1] = (gpointer)mono_get_trampoline_func (tramp_type);
408
409         if (!aot) {
410                 /* backpatch by emitting the missing instructions skipped above */
411                 ARM_LDR_IMM (load_get_lmf_addr, ARMREG_R0, ARMREG_PC, (code - load_get_lmf_addr - 8));
412                 ARM_LDR_IMM (load_trampoline, ARMREG_IP, ARMREG_PC, (code + 4 - load_trampoline - 8));
413         }
414
415         code += 8;
416
417         /* Flush instruction cache, since we've generated code */
418         mono_arch_flush_icache (buf, code - buf);
419
420         /* Sanity check */
421         g_assert ((code - buf) <= GEN_TRAMP_SIZE);
422
423         *code_size = code - buf;
424
425         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
426                 guint32 code_len;
427
428                 /* Initialize the nullified class init trampoline used in the AOT case */
429                 nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (&code_len);
430         }
431
432         *out_unwind_ops = unwind_ops;
433
434         return buf;
435 }
436
437 gpointer
438 mono_arch_get_nullified_class_init_trampoline (guint32 *code_len)
439 {
440         guint8 *buf, *code;
441
442         code = buf = mono_global_codeman_reserve (16);
443
444         code = emit_bx (code, ARMREG_LR);
445
446         mono_arch_flush_icache (buf, code - buf);
447
448         *code_len = code - buf;
449
450         return buf;
451 }
452
453 #define SPEC_TRAMP_SIZE 24
454
455 gpointer
456 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
457 {
458         guint8 *code, *buf, *tramp;
459         gpointer *constants;
460         guint32 short_branch, size = SPEC_TRAMP_SIZE;
461
462         tramp = mono_get_trampoline_code (tramp_type);
463
464         mono_domain_lock (domain);
465         code = buf = mono_domain_code_reserve_align (domain, size, 4);
466         if ((short_branch = branch_for_target_reachable (code + 8, tramp))) {
467                 size = 12;
468                 mono_domain_code_commit (domain, code, SPEC_TRAMP_SIZE, size);
469         }
470         mono_domain_unlock (domain);
471
472         /* we could reduce this to 12 bytes if tramp is within reach:
473          * ARM_PUSH ()
474          * ARM_BL ()
475          * method-literal
476          * The called code can access method using the lr register
477          * A 20 byte sequence could be:
478          * ARM_PUSH ()
479          * ARM_MOV_REG_REG (lr, pc)
480          * ARM_LDR_IMM (pc, pc, 0)
481          * method-literal
482          * tramp-literal
483          */
484         /* We save all the registers, except PC and SP */
485         ARM_PUSH (code, 0x5fff);
486         if (short_branch) {
487                 constants = (gpointer*)code;
488                 constants [0] = GUINT_TO_POINTER (short_branch | (1 << 24));
489                 constants [1] = arg1;
490                 code += 8;
491         } else {
492                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 8); /* temp reg */
493                 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
494                 code = emit_bx (code, ARMREG_R1);
495
496                 constants = (gpointer*)code;
497                 constants [0] = arg1;
498                 constants [1] = tramp;
499                 code += 8;
500         }
501
502         /* Flush instruction cache, since we've generated code */
503         mono_arch_flush_icache (buf, code - buf);
504
505         g_assert ((code - buf) <= size);
506
507         if (code_len)
508                 *code_len = code - buf;
509
510         return buf;
511 }
512
513 #define arm_is_imm12(v) ((int)(v) > -4096 && (int)(v) < 4096)
514
515 gpointer
516 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot)
517 {
518         guint32 code_size;
519         MonoJumpInfo *ji;
520
521         return mono_arch_create_rgctx_lazy_fetch_trampoline_full (slot, &code_size, &ji, FALSE);
522 }
523
524 gpointer
525 mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
526 {
527         guint8 *tramp;
528         guint8 *code, *buf;
529         int tramp_size;
530         guint32 code_len;
531         guint8 **rgctx_null_jumps;
532         int depth, index;
533         int i, njumps;
534         gboolean mrgctx;
535
536         *ji = NULL;
537
538         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
539         index = MONO_RGCTX_SLOT_INDEX (slot);
540         if (mrgctx)
541                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
542         for (depth = 0; ; ++depth) {
543                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
544
545                 if (index < size - 1)
546                         break;
547                 index -= size - 1;
548         }
549
550         tramp_size = 64 + 16 * depth;
551
552         code = buf = mono_global_codeman_reserve (tramp_size);
553
554         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
555         njumps = 0;
556
557         /* The vtable/mrgctx is in R0 */
558         g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
559
560         if (mrgctx) {
561                 /* get mrgctx ptr */
562                 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
563         } else {
564                 /* load rgctx ptr from vtable */
565                 g_assert (arm_is_imm12 (G_STRUCT_OFFSET (MonoVTable, runtime_generic_context)));
566                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
567                 /* is the rgctx ptr null? */
568                 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
569                 /* if yes, jump to actual trampoline */
570                 rgctx_null_jumps [njumps ++] = code;
571                 ARM_B_COND (code, ARMCOND_EQ, 0);
572         }
573
574         for (i = 0; i < depth; ++i) {
575                 /* load ptr to next array */
576                 if (mrgctx && i == 0) {
577                         g_assert (arm_is_imm12 (MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT));
578                         ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
579                 } else {
580                         ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, 0);
581                 }
582                 /* is the ptr null? */
583                 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
584                 /* if yes, jump to actual trampoline */
585                 rgctx_null_jumps [njumps ++] = code;
586                 ARM_B_COND (code, ARMCOND_EQ, 0);
587         }
588
589         /* fetch slot */
590         code = mono_arm_emit_load_imm (code, ARMREG_R2, sizeof (gpointer) * (index + 1));
591         ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_R1, ARMREG_R2);
592         /* is the slot null? */
593         ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
594         /* if yes, jump to actual trampoline */
595         rgctx_null_jumps [njumps ++] = code;
596         ARM_B_COND (code, ARMCOND_EQ, 0);
597         /* otherwise return, result is in R1 */
598         ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R1);
599         code = emit_bx (code, ARMREG_LR);
600
601         g_assert (njumps <= depth + 2);
602         for (i = 0; i < njumps; ++i)
603                 arm_patch (rgctx_null_jumps [i], code);
604
605         g_free (rgctx_null_jumps);
606
607         /* Slowpath */
608
609         /* The vtable/mrgctx is still in R0 */
610
611         if (aot) {
612                 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
613                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
614                 ARM_B (code, 0);
615                 *(gpointer*)code = NULL;
616                 code += 4;
617                 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
618         } else {
619                 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), &code_len);
620
621                 /* Jump to the actual trampoline */
622                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
623                 code = emit_bx (code, ARMREG_R1);
624                 *(gpointer*)code = tramp;
625                 code += 4;
626         }
627
628         mono_arch_flush_icache (buf, code - buf);
629
630         g_assert (code - buf <= tramp_size);
631
632         *code_size = code - buf;
633
634         return buf;
635 }
636
637 #define arm_is_imm8(v) ((v) > -256 && (v) < 256)
638
639 gpointer
640 mono_arch_create_generic_class_init_trampoline (void)
641 {
642         guint32 code_size;
643         MonoJumpInfo *ji;
644
645         return mono_arch_create_generic_class_init_trampoline_full (&code_size, &ji, FALSE);
646 }
647
648 gpointer
649 mono_arch_create_generic_class_init_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
650 {
651         guint8 *tramp;
652         guint8 *code, *buf;
653         static int byte_offset = -1;
654         static guint8 bitmask;
655         guint8 *jump;
656         int tramp_size;
657         guint32 code_len, imm8;
658         gint rot_amount;
659
660         *ji = NULL;
661
662         tramp_size = 64;
663
664         code = buf = mono_global_codeman_reserve (tramp_size);
665
666         if (byte_offset < 0)
667                 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
668
669         g_assert (arm_is_imm8 (byte_offset));
670         ARM_LDRSB_IMM (code, ARMREG_IP, MONO_ARCH_VTABLE_REG, byte_offset);
671         imm8 = mono_arm_is_rotated_imm8 (bitmask, &rot_amount);
672         g_assert (imm8 >= 0);
673         ARM_AND_REG_IMM (code, ARMREG_IP, ARMREG_IP, imm8, rot_amount);
674         ARM_CMP_REG_IMM (code, ARMREG_IP, 0, 0);
675         jump = code;
676         ARM_B_COND (code, ARMCOND_EQ, 0);
677
678         /* Initialized case */
679         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_LR);   
680
681         /* Uninitialized case */
682         arm_patch (jump, code);
683
684         if (aot) {
685                 *ji = mono_patch_info_list_prepend (*ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
686                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
687                 ARM_B (code, 0);
688                 *(gpointer*)code = NULL;
689                 code += 4;
690                 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
691         } else {
692                 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), &code_len);
693
694                 /* Jump to the actual trampoline */
695                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
696                 code = emit_bx (code, ARMREG_R1);
697                 *(gpointer*)code = tramp;
698                 code += 4;
699         }
700
701         mono_arch_flush_icache (buf, code - buf);
702
703         g_assert (code - buf <= tramp_size);
704
705         *code_size = code - buf;
706
707         return buf;
708 }