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