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