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