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