Merge pull request #649 from DavidS/feature/implement-additional-reference-path
[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-2003 Ximian, Inc.
8  * Copyright 2003-2011 Novell Inc
9  * Copyright 2011 Xamarin Inc
10  */
11
12 #include <config.h>
13 #include <glib.h>
14
15 #include <mono/metadata/appdomain.h>
16 #include <mono/metadata/marshal.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/arch/arm/arm-codegen.h>
19
20 #include "mini.h"
21 #include "mini-arm.h"
22
23 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
24
25 static guint8* nullified_class_init_trampoline;
26
27
28 #ifdef USE_JUMP_TABLES
29
30 static guint16
31 decode_imm16 (guint32 insn)
32 {
33         return (((insn >> 16) & 0xf) << 12) | (insn & 0xfff);
34 }
35
36 #define INSN_MASK 0xff00000
37 #define MOVW_MASK ((3 << 24) | (0 << 20))
38 #define MOVT_MASK ((3 << 24) | (4 << 20))
39
40 gpointer*
41 mono_arch_jumptable_entry_from_code (guint8 *code)
42 {
43         guint32 insn1 = ((guint32*)code) [0];
44         guint32 insn2 = ((guint32*)code) [1];
45
46         if (((insn1 & INSN_MASK) == MOVW_MASK) &&
47             ((insn2 & INSN_MASK) == MOVT_MASK) ) {
48                 guint32 imm_lo = decode_imm16 (insn1);
49                 guint32 imm_hi = decode_imm16 (insn2);
50                 return (gpointer*) GUINT_TO_POINTER (imm_lo | (imm_hi << 16));
51         } else {
52                 g_assert_not_reached ();
53                 return NULL;
54         }
55 }
56
57 #undef INSN_MASK
58 #undef MOVW_MASK
59 #undef MOVT_MASK
60
61 void
62 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
63 {
64         gpointer *jte;
65         /*
66          * code_ptr is 4 instructions after MOVW/MOVT used to address
67          * jumptable entry.
68          */
69         jte = mono_jumptable_get_entry (code_ptr - 16);
70         g_assert ( jte != NULL);
71         *jte = addr;
72 }
73 #else
74 void
75 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
76 {
77         guint32 *code = (guint32*)code_ptr;
78
79         /* This is the 'bl' or the 'mov pc' instruction */
80         --code;
81         
82         /*
83          * Note that methods are called also with the bl opcode.
84          */
85         if ((((*code) >> 25)  & 7) == 5) {
86                 /*g_print ("direct patching\n");*/
87                 arm_patch ((guint8*)code, addr);
88                 mono_arch_flush_icache ((guint8*)code, 4);
89                 return;
90         }
91
92         if ((((*code) >> 20) & 0xFF) == 0x12) {
93                 /*g_print ("patching bx\n");*/
94                 arm_patch ((guint8*)code, addr);
95                 mono_arch_flush_icache ((guint8*)(code - 2), 4);
96                 return;
97         }
98
99         g_assert_not_reached ();
100 }
101 #endif
102
103 void
104 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
105 {
106         guint8 *jump_entry;
107
108         /* Patch the jump table entry used by the plt entry */
109         if (*(guint32*)code == 0xe59fc000) {
110                 /* ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0); */
111                 guint32 offset = ((guint32*)code)[2];
112                 
113                 jump_entry = code + offset + 12;
114         } else if (*(guint16*)(code - 4) == 0xf8df) {
115                 /* 
116                  * Thumb PLT entry, begins with ldr.w ip, [pc, #8], code points to entry + 4, see
117                  * mono_arm_get_thumb_plt_entry ().
118                  */
119                 guint32 offset;
120
121                 code -= 4;
122                 offset = *(guint32*)(code + 12);
123                 jump_entry = code + offset + 8;
124         } else {
125                 g_assert_not_reached ();
126         }
127
128         *(guint8**)jump_entry = addr;
129 }
130
131 void
132 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
133 {
134         mono_arch_patch_callsite (NULL, code, nullified_class_init_trampoline);
135 }
136
137 void
138 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
139 {
140         if (mono_aot_only && !nullified_class_init_trampoline)
141                 nullified_class_init_trampoline = mono_aot_get_trampoline ("nullified_class_init_trampoline");
142
143         mono_arch_patch_plt_entry (code, NULL, regs, nullified_class_init_trampoline);
144 }
145
146 #ifndef DISABLE_JIT
147
148 #define arm_is_imm12(v) ((int)(v) > -4096 && (int)(v) < 4096)
149
150 #ifndef USE_JUMP_TABLES
151 /*
152  * Return the instruction to jump from code to target, 0 if not
153  * reachable with a single instruction
154  */
155 static guint32
156 branch_for_target_reachable (guint8 *branch, guint8 *target)
157 {
158         gint diff = target - branch - 8;
159         g_assert ((diff & 3) == 0);
160         if (diff >= 0) {
161                 if (diff <= 33554431)
162                         return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | (diff >> 2);
163         } else {
164                 /* diff between 0 and -33554432 */
165                 if (diff >= -33554432)
166                         return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | ((diff >> 2) & ~0xff000000);
167         }
168         return 0;
169 }
170 #endif
171
172 static inline guint8*
173 emit_bx (guint8* code, int reg)
174 {
175         if (mono_arm_thumb_supported ())
176                 ARM_BX (code, reg);
177         else
178                 ARM_MOV_REG_REG (code, ARMREG_PC, reg);
179         return code;
180 }
181
182 /* Stack size for trampoline function 
183  */
184 #define STACK ALIGN_TO (sizeof (MonoLMF), 8)
185
186 /* Method-specific trampoline code fragment size */
187 #define METHOD_TRAMPOLINE_SIZE 64
188
189 /* Jump-specific trampoline code fragment size */
190 #define JUMP_TRAMPOLINE_SIZE   64
191
192 guchar*
193 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
194 {
195         guint8 *buf, *code = NULL;
196 #ifdef USE_JUMP_TABLES
197         gpointer *load_get_lmf_addr = NULL, *load_trampoline = NULL;
198 #else
199         guint8 *load_get_lmf_addr  = NULL, *load_trampoline  = NULL;
200         gpointer *constants;
201 #endif
202
203         int cfa_offset, lmf_offset, regsave_size, lr_offset;
204         GSList *unwind_ops = NULL;
205         MonoJumpInfo *ji = NULL;
206         int buf_len;
207
208 #ifdef USE_JUMP_TABLES
209         g_assert (!aot);
210 #endif
211
212         /* Now we'll create in 'buf' the ARM trampoline code. This
213          is the trampoline code common to all methods  */
214
215         buf_len = 212;
216         code = buf = mono_global_codeman_reserve (buf_len);
217
218         /*
219          * At this point lr points to the specific arg and sp points to the saved
220          * regs on the stack (all but PC and SP). The original LR value has been
221          * saved as sp + LR_OFFSET by the push in the specific trampoline
222          */
223
224         /* The offset of lmf inside the stack frame */
225         lmf_offset = STACK - sizeof (MonoLMF);
226         /* The size of the area already allocated by the push in the specific trampoline */
227         regsave_size = 14 * sizeof (mgreg_t);
228         /* The offset where lr was saved inside the regsave area */
229         lr_offset = 13 * sizeof (mgreg_t);
230
231         // FIXME: Finish the unwind info, the current info allows us to unwind
232         // when the trampoline is not in the epilog
233
234         // CFA = SP + (num registers pushed) * 4
235         cfa_offset = 14 * sizeof (mgreg_t);
236         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, cfa_offset);
237         // PC saved at sp+LR_OFFSET
238         mono_add_unwind_op_offset (unwind_ops, code, buf, ARMREG_LR, -4);
239
240         if (aot && tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
241                 /* 
242                  * For page trampolines the data is in r1, so just move it, otherwise use the got slot as below.
243                  * The trampoline contains a pc-relative offset to the got slot 
244                  * preceeding the got slot where the value is stored. The offset can be
245                  * found at [lr + 0].
246                  */
247                 if (aot == 2) {
248                         ARM_MOV_REG_REG (code, ARMREG_V2, ARMREG_R1);
249                 } else {
250                         ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
251                         ARM_ADD_REG_IMM (code, ARMREG_V2, ARMREG_V2, 4, 0);
252                         ARM_LDR_REG_REG (code, ARMREG_V2, ARMREG_V2, ARMREG_LR);
253                 }
254         } else {
255                 if (tramp_type != MONO_TRAMPOLINE_GENERIC_CLASS_INIT) {
256                         ARM_LDR_IMM (code, ARMREG_V2, ARMREG_LR, 0);
257                 }
258                 else
259                         ARM_MOV_REG_REG (code, ARMREG_V2, MONO_ARCH_VTABLE_REG);
260         }
261         ARM_LDR_IMM (code, ARMREG_V3, ARMREG_SP, lr_offset);
262
263         /* ok, now we can continue with the MonoLMF setup, mostly untouched 
264          * from emit_prolog in mini-arm.c
265          * This is a synthetized call to mono_get_lmf_addr ()
266          */
267         if (aot) {
268                 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
269                 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
270                 ARM_B (code, 0);
271                 *(gpointer*)code = NULL;
272                 code += 4;
273                 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
274         } else {
275 #ifdef USE_JUMP_TABLES
276                 load_get_lmf_addr = mono_jumptable_add_entry ();
277                 code = mono_arm_load_jumptable_entry (code, load_get_lmf_addr, ARMREG_R0);
278 #else
279                 load_get_lmf_addr = code;
280                 code += 4;
281 #endif
282         }
283         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
284         code = emit_bx (code, ARMREG_R0);
285
286         /* we build the MonoLMF structure on the stack - see mini-arm.h
287          * The pointer to the struct is put in r1.
288          * the iregs array is already allocated on the stack by push.
289          */
290         ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
291         cfa_offset += STACK - regsave_size;
292         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, buf, cfa_offset);
293         /* V1 == lmf */
294         ARM_ADD_REG_IMM8 (code, ARMREG_V1, ARMREG_SP, STACK - sizeof (MonoLMF));
295
296         /*
297          * The stack now looks like:
298          *       <saved regs>
299          * v1 -> <rest of LMF>
300          * sp -> <alignment>
301          */
302
303         /* r0 is the result from mono_get_lmf_addr () */
304         ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
305         /* new_lmf->previous_lmf = *lmf_addr */
306         ARM_LDR_IMM (code, ARMREG_R2, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
307         ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
308         /* *(lmf_addr) = r1 */
309         ARM_STR_IMM (code, ARMREG_V1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
310         /* save method info (it's in v2) */
311         if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
312                 ARM_STR_IMM (code, ARMREG_V2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, method));
313         else {
314                 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
315                 ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, method));
316         }
317         /* save caller SP */
318         ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, cfa_offset);
319         ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, sp));
320         /* save caller FP */
321         ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (G_STRUCT_OFFSET (MonoLMF, iregs) + ARMREG_FP*4));
322         ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, fp));
323         /* save the IP (caller ip) */
324         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
325                 ARM_MOV_REG_IMM8 (code, ARMREG_R2, 0);
326         } else {
327                 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_V1, (G_STRUCT_OFFSET (MonoLMF, iregs) + 13*4));
328         }
329         ARM_STR_IMM (code, ARMREG_R2, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, ip));
330
331         /*
332          * Now we're ready to call xxx_trampoline ().
333          */
334         /* Arg 1: the saved registers */
335         ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, iregs));
336
337         /* Arg 2: code (next address to the instruction that called us) */
338         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
339                 ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0);
340         } else {
341                 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_V3);
342         }
343         
344         /* Arg 3: the specific argument, stored in v2
345          */
346         ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_V2);
347
348         if (aot) {
349                 char *icall_name = g_strdup_printf ("trampoline_func_%d", tramp_type);
350                 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
351                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
352                 ARM_B (code, 0);
353                 *(gpointer*)code = NULL;
354                 code += 4;
355                 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
356         } else {
357 #ifdef USE_JUMP_TABLES
358                 load_trampoline = mono_jumptable_add_entry ();
359                 code = mono_arm_load_jumptable_entry (code, load_trampoline, ARMREG_IP);
360 #else
361                 load_trampoline = code;
362                 code += 4;
363 #endif
364         }
365
366         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
367         code = emit_bx (code, ARMREG_IP);
368
369         /* OK, code address is now on r0. Move it to the place on the stack
370          * where IP was saved (it is now no more useful to us and it can be
371          * clobbered). This way we can just restore all the regs in one inst
372          * and branch to IP.
373          */
374         ARM_STR_IMM (code, ARMREG_R0, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, iregs) + (ARMREG_R12 * sizeof (mgreg_t)));
375
376         /* Check for thread interruption */
377         /* This is not perf critical code so no need to check the interrupt flag */
378         /* 
379          * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
380          */
381         if (aot) {
382                 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_force_interruption_checkpoint");
383                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
384                 ARM_B (code, 0);
385                 *(gpointer*)code = NULL;
386                 code += 4;
387                 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
388         } else {
389 #ifdef USE_JUMP_TABLES
390                 gpointer *jte = mono_jumptable_add_entry ();
391                 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_IP);
392                 jte [0] = mono_thread_force_interruption_checkpoint;
393 #else
394                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
395                 ARM_B (code, 0);
396                 *(gpointer*)code = mono_thread_force_interruption_checkpoint;
397                 code += 4;
398 #endif
399         }
400         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
401         code = emit_bx (code, ARMREG_IP);
402
403         /*
404          * Now we restore the MonoLMF (see emit_epilogue in mini-arm.c)
405          * and the rest of the registers, so the method called will see
406          * the same state as before we executed.
407          */
408         /* ip = previous_lmf */
409         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
410         /* lr = lmf_addr */
411         ARM_LDR_IMM (code, ARMREG_LR, ARMREG_V1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
412         /* *(lmf_addr) = previous_lmf */
413         ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
414
415         /* Non-standard function epilogue. Instead of doing a proper
416          * return, we just jump to the compiled code.
417          */
418         /* Restore the registers and jump to the code:
419          * Note that IP has been conveniently set to the method addr.
420          */
421         ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, STACK - regsave_size);
422         ARM_POP_NWB (code, 0x5fff);
423         if (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
424                 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_IP);
425         ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, regsave_size);
426         if ((tramp_type == MONO_TRAMPOLINE_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT) || (tramp_type == MONO_TRAMPOLINE_RGCTX_LAZY_FETCH))
427                 code = emit_bx (code, ARMREG_LR);
428         else
429                 code = emit_bx (code, ARMREG_IP);
430
431 #ifdef USE_JUMP_TABLES
432         load_get_lmf_addr [0] = mono_get_lmf_addr;
433         load_trampoline [0] = (gpointer)mono_get_trampoline_func (tramp_type);
434 #else
435         constants = (gpointer*)code;
436         constants [0] = mono_get_lmf_addr;
437         constants [1] = (gpointer)mono_get_trampoline_func (tramp_type);
438
439         if (!aot) {
440                 /* backpatch by emitting the missing instructions skipped above */
441                 ARM_LDR_IMM (load_get_lmf_addr, ARMREG_R0, ARMREG_PC, (code - load_get_lmf_addr - 8));
442                 ARM_LDR_IMM (load_trampoline, ARMREG_IP, ARMREG_PC, (code + 4 - load_trampoline - 8));
443         }
444
445         code += 8;
446 #endif
447
448         /* Flush instruction cache, since we've generated code */
449         mono_arch_flush_icache (buf, code - buf);
450
451         /* Sanity check */
452         g_assert ((code - buf) <= buf_len);
453
454         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
455                 /* Initialize the nullified class init trampoline used in the AOT case */
456                 nullified_class_init_trampoline = mono_arch_get_nullified_class_init_trampoline (NULL);
457
458         if (info)
459                 *info = mono_tramp_info_create (mono_get_generic_trampoline_name (tramp_type), buf, code - buf, ji, unwind_ops);
460
461         return buf;
462 }
463
464 gpointer
465 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
466 {
467         guint8 *buf, *code;
468
469         code = buf = mono_global_codeman_reserve (16);
470
471         code = emit_bx (code, ARMREG_LR);
472
473         mono_arch_flush_icache (buf, code - buf);
474
475         if (info)
476                 *info = mono_tramp_info_create ("nullified_class_init_trampoline", buf, code - buf, NULL, NULL);
477
478         return buf;
479 }
480
481 #define SPEC_TRAMP_SIZE 24
482
483 gpointer
484 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
485 {
486         guint8 *code, *buf, *tramp;
487         gpointer *constants;
488 #ifndef USE_JUMP_TABLES
489         guint32 short_branch;
490 #endif
491         guint32 size = SPEC_TRAMP_SIZE;
492
493         tramp = mono_get_trampoline_code (tramp_type);
494
495         mono_domain_lock (domain);
496 #ifdef USE_JUMP_TABLES
497         code = buf = mono_domain_code_reserve_align (domain, size, 4);
498 #else
499         code = buf = mono_domain_code_reserve_align (domain, size, 4);
500         if ((short_branch = branch_for_target_reachable (code + 4, tramp))) {
501                 size = 12;
502                 mono_domain_code_commit (domain, code, SPEC_TRAMP_SIZE, size);
503         }
504 #endif
505         mono_domain_unlock (domain);
506
507 #ifdef USE_JUMP_TABLES
508         /* For jumptables case we always generate the same code for trampolines,
509          * namely
510          *   push {r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, lr}
511          *   movw lr, lo(jte)
512          *   movt lr, hi(jte)
513          *   ldr r1, [lr + 4]
514          *   bx r1
515          */
516         ARM_PUSH (code, 0x5fff);
517         constants = mono_jumptable_add_entries (2);
518         code = mono_arm_load_jumptable_entry_addr (code, constants, ARMREG_LR);
519         ARM_LDR_IMM (code, ARMREG_R1, ARMREG_LR, 4);
520         code = emit_bx (code, ARMREG_R1);
521         constants [0] = arg1;
522         constants [1] = tramp;
523 #else
524         /* we could reduce this to 12 bytes if tramp is within reach:
525          * ARM_PUSH ()
526          * ARM_BL ()
527          * method-literal
528          * The called code can access method using the lr register
529          * A 20 byte sequence could be:
530          * ARM_PUSH ()
531          * ARM_MOV_REG_REG (lr, pc)
532          * ARM_LDR_IMM (pc, pc, 0)
533          * method-literal
534          * tramp-literal
535          */
536         /* We save all the registers, except PC and SP */
537         ARM_PUSH (code, 0x5fff);
538         if (short_branch) {
539                 constants = (gpointer*)code;
540                 constants [0] = GUINT_TO_POINTER (short_branch | (1 << 24));
541                 constants [1] = arg1;
542                 code += 8;
543         } else {
544                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 8); /* temp reg */
545                 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
546                 code = emit_bx (code, ARMREG_R1);
547
548                 constants = (gpointer*)code;
549                 constants [0] = arg1;
550                 constants [1] = tramp;
551                 code += 8;
552         }
553 #endif
554
555         /* Flush instruction cache, since we've generated code */
556         mono_arch_flush_icache (buf, code - buf);
557
558         g_assert ((code - buf) <= size);
559
560         if (code_len)
561                 *code_len = code - buf;
562
563         return buf;
564 }
565
566 /*
567  * mono_arch_get_unbox_trampoline:
568  * @m: method pointer
569  * @addr: pointer to native code for @m
570  *
571  * when value type methods are called through the vtable we need to unbox the
572  * this argument. This method returns a pointer to a trampoline which does
573  * unboxing before calling the method
574  */
575 gpointer
576 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
577 {
578         guint8 *code, *start;
579         MonoDomain *domain = mono_domain_get ();
580 #ifdef USE_JUMP_TABLES
581         gpointer *jte;
582         guint32 size = 20;
583 #else
584         guint32 size = 16;
585 #endif
586
587         start = code = mono_domain_code_reserve (domain, size);
588
589 #ifdef USE_JUMP_TABLES
590         jte = mono_jumptable_add_entry ();
591         code = mono_arm_load_jumptable_entry (code, jte, ARMREG_IP);
592         ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
593         code = emit_bx (code, ARMREG_IP);
594         jte [0] = addr;
595 #else
596         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
597         ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
598         code = emit_bx (code, ARMREG_IP);
599         *(guint32*)code = (guint32)addr;
600         code += 4;
601 #endif
602         mono_arch_flush_icache (start, code - start);
603         g_assert ((code - start) <= size);
604         /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
605         g_print ("unbox code is at %p for method at %p\n", start, addr);*/
606
607         return start;
608 }
609
610 gpointer
611 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
612 {
613         guint8 *code, *start;
614 #ifdef USE_JUMP_TABLES
615         int buf_len = 20;
616         gpointer *jte;
617 #else
618         int buf_len = 16;
619 #endif
620         MonoDomain *domain = mono_domain_get ();
621
622         start = code = mono_domain_code_reserve (domain, buf_len);
623
624 #ifdef USE_JUMP_TABLES
625         jte = mono_jumptable_add_entries (2);
626         code = mono_arm_load_jumptable_entry_addr (code, jte, ARMREG_IP);
627         ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_IP, 0);
628         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_IP, 4);
629         ARM_BX (code, ARMREG_IP);
630         jte [0] = mrgctx;
631         jte [1] = addr;
632 #else
633         ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, 0);
634         ARM_LDR_IMM (code, ARMREG_PC, ARMREG_PC, 0);
635         *(guint32*)code = (guint32)mrgctx;
636         code += 4;
637         *(guint32*)code = (guint32)addr;
638         code += 4;
639 #endif
640
641         g_assert ((code - start) <= buf_len);
642
643         mono_arch_flush_icache (start, code - start);
644
645         return start;
646 }
647
648 gpointer
649 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
650 {
651         guint8 *tramp;
652         guint8 *code, *buf;
653         int tramp_size;
654         guint32 code_len;
655         guint8 **rgctx_null_jumps;
656         int depth, index;
657         int i, njumps;
658         gboolean mrgctx;
659         MonoJumpInfo *ji = NULL;
660         GSList *unwind_ops = NULL;
661 #ifdef USE_JUMP_TABLES
662         gpointer *jte;
663 #endif
664
665         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
666         index = MONO_RGCTX_SLOT_INDEX (slot);
667         if (mrgctx)
668                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
669         for (depth = 0; ; ++depth) {
670                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
671
672                 if (index < size - 1)
673                         break;
674                 index -= size - 1;
675         }
676
677         tramp_size = 64 + 16 * depth;
678
679         code = buf = mono_global_codeman_reserve (tramp_size);
680
681         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
682
683         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
684         njumps = 0;
685
686         /* The vtable/mrgctx is in R0 */
687         g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
688
689         if (mrgctx) {
690                 /* get mrgctx ptr */
691                 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
692         } else {
693                 /* load rgctx ptr from vtable */
694                 g_assert (arm_is_imm12 (G_STRUCT_OFFSET (MonoVTable, runtime_generic_context)));
695                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
696                 /* is the rgctx ptr null? */
697                 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
698                 /* if yes, jump to actual trampoline */
699                 rgctx_null_jumps [njumps ++] = code;
700                 ARM_B_COND (code, ARMCOND_EQ, 0);
701         }
702
703         for (i = 0; i < depth; ++i) {
704                 /* load ptr to next array */
705                 if (mrgctx && i == 0) {
706                         g_assert (arm_is_imm12 (MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT));
707                         ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
708                 } else {
709                         ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R1, 0);
710                 }
711                 /* is the ptr null? */
712                 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
713                 /* if yes, jump to actual trampoline */
714                 rgctx_null_jumps [njumps ++] = code;
715                 ARM_B_COND (code, ARMCOND_EQ, 0);
716         }
717
718         /* fetch slot */
719         code = mono_arm_emit_load_imm (code, ARMREG_R2, sizeof (gpointer) * (index + 1));
720         ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_R1, ARMREG_R2);
721         /* is the slot null? */
722         ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
723         /* if yes, jump to actual trampoline */
724         rgctx_null_jumps [njumps ++] = code;
725         ARM_B_COND (code, ARMCOND_EQ, 0);
726         /* otherwise return, result is in R1 */
727         ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R1);
728         code = emit_bx (code, ARMREG_LR);
729
730         g_assert (njumps <= depth + 2);
731         for (i = 0; i < njumps; ++i)
732                 arm_patch (rgctx_null_jumps [i], code);
733
734         g_free (rgctx_null_jumps);
735
736         /* Slowpath */
737
738         /* The vtable/mrgctx is still in R0 */
739
740         if (aot) {
741                 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
742                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
743                 ARM_B (code, 0);
744                 *(gpointer*)code = NULL;
745                 code += 4;
746                 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
747         } else {
748                 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), &code_len);
749
750                 /* Jump to the actual trampoline */
751 #ifdef USE_JUMP_TABLES
752                 jte = mono_jumptable_add_entry ();
753                 jte [0] = tramp;
754                 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_R1);
755                 code = emit_bx (code, ARMREG_R1);
756 #else
757                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
758                 code = emit_bx (code, ARMREG_R1);
759                 *(gpointer*)code = tramp;
760                 code += 4;
761 #endif
762         }
763
764         mono_arch_flush_icache (buf, code - buf);
765
766         g_assert (code - buf <= tramp_size);
767
768         if (info) {
769                 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
770                 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
771                 g_free (name);
772         }
773
774         return buf;
775 }
776
777 gpointer
778 mono_arch_create_general_rgctx_lazy_fetch_trampoline (MonoTrampInfo **info, gboolean aot)
779 {
780         guint8 *code, *buf;
781         int tramp_size;
782         MonoJumpInfo *ji = NULL;
783         GSList *unwind_ops = NULL;
784
785         g_assert (aot);
786
787         tramp_size = 32;
788
789         code = buf = mono_global_codeman_reserve (tramp_size);
790
791         mono_add_unwind_op_def_cfa (unwind_ops, code, buf, ARMREG_SP, 0);
792
793         // FIXME: Currently, we always go to the slow path.
794         /* Load trampoline addr */
795         ARM_LDR_IMM (code, ARMREG_R1, MONO_ARCH_RGCTX_REG, 4);
796         /* The vtable/mrgctx is in R0 */
797         g_assert (MONO_ARCH_VTABLE_REG == ARMREG_R0);
798         code = emit_bx (code, ARMREG_R1);
799
800         mono_arch_flush_icache (buf, code - buf);
801
802         g_assert (code - buf <= tramp_size);
803
804         if (info)
805                 *info = mono_tramp_info_create ("rgctx_fetch_trampoline_general", buf, code - buf, ji, unwind_ops);
806
807         return buf;
808 }
809
810 #define arm_is_imm8(v) ((v) > -256 && (v) < 256)
811
812 gpointer
813 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
814 {
815         guint8 *tramp;
816         guint8 *code, *buf;
817         static int byte_offset = -1;
818         static guint8 bitmask;
819         guint8 *jump;
820         int tramp_size;
821         guint32 code_len, imm8;
822         gint rot_amount;
823         GSList *unwind_ops = NULL;
824         MonoJumpInfo *ji = NULL;
825
826         tramp_size = 64;
827
828         code = buf = mono_global_codeman_reserve (tramp_size);
829
830         if (byte_offset < 0)
831                 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
832
833         g_assert (arm_is_imm8 (byte_offset));
834         ARM_LDRSB_IMM (code, ARMREG_IP, MONO_ARCH_VTABLE_REG, byte_offset);
835         imm8 = mono_arm_is_rotated_imm8 (bitmask, &rot_amount);
836         g_assert (imm8 >= 0);
837         ARM_AND_REG_IMM (code, ARMREG_IP, ARMREG_IP, imm8, rot_amount);
838         ARM_CMP_REG_IMM (code, ARMREG_IP, 0, 0);
839         jump = code;
840         ARM_B_COND (code, ARMCOND_EQ, 0);
841
842         /* Initialized case */
843         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_LR);   
844
845         /* Uninitialized case */
846         arm_patch (jump, code);
847
848         if (aot) {
849                 ji = mono_patch_info_list_prepend (ji, code - buf, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
850                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
851                 ARM_B (code, 0);
852                 *(gpointer*)code = NULL;
853                 code += 4;
854                 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
855         } else {
856 #ifdef USE_JUMP_TABLES
857                 gpointer *jte = mono_jumptable_add_entry ();
858 #endif
859                 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), &code_len);
860
861                 /* Jump to the actual trampoline */
862 #ifdef USE_JUMP_TABLES
863                 code = mono_arm_load_jumptable_entry (code, jte, ARMREG_R1);
864                 jte [0] = tramp;
865                 code = emit_bx (code, ARMREG_R1);
866 #else
867                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0); /* temp reg */
868                 code = emit_bx (code, ARMREG_R1);
869                 *(gpointer*)code = tramp;
870                 code += 4;
871 #endif
872         }
873
874         mono_arch_flush_icache (buf, code - buf);
875
876         g_assert (code - buf <= tramp_size);
877
878         if (info)
879                 *info = mono_tramp_info_create ("generic_class_init_trampoline", buf, code - buf, ji, unwind_ops);
880
881         return buf;
882 }
883
884 #else
885
886 guchar*
887 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
888 {
889         g_assert_not_reached ();
890         return NULL;
891 }
892
893 gpointer
894 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
895 {
896         g_assert_not_reached ();
897         return NULL;
898 }
899
900 gpointer
901 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
902 {
903         g_assert_not_reached ();
904         return NULL;
905 }
906
907 gpointer
908 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
909 {
910         g_assert_not_reached ();
911         return NULL;
912 }
913
914 gpointer
915 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
916 {
917         g_assert_not_reached ();
918         return NULL;
919 }
920
921 gpointer
922 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
923 {
924         g_assert_not_reached ();
925         return NULL;
926 }
927         
928 #endif /* DISABLE_JIT */
929
930 guint8*
931 mono_arch_get_call_target (guint8 *code)
932 {
933         guint32 ins = ((guint32*)(gpointer)code) [-1];
934
935 #if MONOTOUCH
936         /* Should be a 'bl' or a 'b' */
937         if (((ins >> 25) & 0x7) == 0x5) {
938 #else
939         /* Should be a 'bl' */
940         if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
941 #endif
942                 gint32 disp = ((gint32)ins) & 0xffffff;
943                 guint8 *target = code - 4 + 8 + (disp * 4);
944
945                 return target;
946         } else {
947                 return NULL;
948         }
949 }
950
951 guint32
952 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
953 {
954         /* The offset is stored as the 4th word of the plt entry */
955         return ((guint32*)plt_entry) [3];
956 }
957
958 /*
959  * Return the address of the PLT entry called by the thumb code CODE.
960  */
961 guint8*
962 mono_arm_get_thumb_plt_entry (guint8 *code)
963 {
964         int s, j1, j2, imm10, imm11, i1, i2, imm32;
965         guint8 *bl, *base;
966         guint16 t1, t2;
967         guint8 *target;
968
969         /* code should be right after a BL */
970         code = (guint8*)((mgreg_t)code & ~1);
971         base = (guint8*)((mgreg_t)code & ~3);
972         bl = code - 4;
973         t1 = ((guint16*)bl) [0];
974         t2 = ((guint16*)bl) [1];
975
976         g_assert ((t1 >> 11) == 0x1e);
977
978         s = (t1 >> 10) & 0x1;
979         imm10 = (t1 >> 0) & 0x3ff;
980         j1 = (t2 >> 13) & 0x1;
981         j2 = (t2 >> 11) & 0x1;
982         imm11 = t2 & 0x7ff;
983
984         i1 = (s ^ j1) ? 0 : 1;
985         i2 = (s ^ j2) ? 0 : 1;
986
987         imm32 = (imm11 << 1) | (imm10 << 12) | (i2 << 22) | (i1 << 23);
988         // FIXME:
989         g_assert (s == 0);
990
991         target = code + imm32;
992
993         /* target now points to the thumb plt entry */
994         /* ldr.w r12, [pc, #8] */
995         g_assert (((guint16*)target) [0] == 0xf8df);
996         g_assert (((guint16*)target) [1] == 0xc008);
997
998         /* 
999          * The PLT info offset is at offset 16, but mono_arch_get_plt_entry_offset () returns
1000          * the 3rd word, so compensate by returning a different value.
1001          */
1002         target += 4;
1003
1004         return target;
1005 }
1006
1007 #ifndef DISABLE_JIT
1008
1009 /*
1010  * mono_arch_get_gsharedvt_arg_trampoline:
1011  *
1012  *   See tramp-x86.c for documentation.
1013  */
1014 gpointer
1015 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1016 {
1017         guint8 *code, *start;
1018         int buf_len;
1019         gpointer *constants;
1020
1021         buf_len = 24;
1022
1023         start = code = mono_domain_code_reserve (domain, buf_len);
1024
1025         /* Similar to the specialized trampoline code */
1026         ARM_PUSH (code, (1 << ARMREG_R0) | (1 << ARMREG_R1) | (1 << ARMREG_R2) | (1 << ARMREG_R3) | (1 << ARMREG_LR));
1027         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 8);
1028         /* arg is passed in LR */
1029         ARM_LDR_IMM (code, ARMREG_LR, ARMREG_PC, 0);
1030         code = emit_bx (code, ARMREG_IP);
1031         constants = (gpointer*)code;
1032         constants [0] = arg;
1033         constants [1] = addr;
1034         code += 8;
1035
1036         g_assert ((code - start) <= buf_len);
1037
1038         nacl_domain_code_validate (domain, &start, buf_len, &code);
1039         mono_arch_flush_icache (start, code - start);
1040
1041         return start;
1042 }
1043
1044 #else
1045
1046 gpointer
1047 mono_arch_get_gsharedvt_arg_trampoline (MonoDomain *domain, gpointer arg, gpointer addr)
1048 {
1049         g_assert_not_reached ();
1050         return NULL;
1051 }
1052
1053 #endif
1054
1055 #if defined(MONOTOUCH) || defined(MONO_EXTENSIONS)
1056
1057 #include "../../../mono-extensions/mono/mini/tramp-arm-gsharedvt.c"
1058
1059 #else
1060
1061 gpointer
1062 mono_arm_start_gsharedvt_call (GSharedVtCallInfo *info, gpointer *caller, gpointer *callee, gpointer mrgctx_reg)
1063 {
1064         g_assert_not_reached ();
1065         return NULL;
1066 }
1067
1068 gpointer
1069 mono_arch_get_gsharedvt_trampoline (MonoTrampInfo **info, gboolean aot)
1070 {
1071         if (info)
1072                 *info = NULL;
1073         return NULL;
1074 }
1075
1076 #endif /* !MONOTOUCH */