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