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