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