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