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