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