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