2 * mini-arm.c: ARM backend for the Mono code generator
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2003 Ximian, Inc.
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/debug-helpers.h>
15 #include <mono/utils/mono-mmap.h>
22 #include "mono/arch/arm/arm-fpa-codegen.h"
23 #elif defined(ARM_FPU_VFP)
24 #include "mono/arch/arm/arm-vfp-codegen.h"
27 #if defined(__ARM_EABI__) && defined(__linux__) && !defined(PLATFORM_ANDROID)
28 #define HAVE_AEABI_READ_TP 1
31 static gint lmf_tls_offset = -1;
32 static gint lmf_addr_tls_offset = -1;
34 /* This mutex protects architecture specific caches */
35 #define mono_mini_arch_lock() EnterCriticalSection (&mini_arch_mutex)
36 #define mono_mini_arch_unlock() LeaveCriticalSection (&mini_arch_mutex)
37 static CRITICAL_SECTION mini_arch_mutex;
39 static int v5_supported = 0;
40 static int v7_supported = 0;
41 static int thumb_supported = 0;
44 * The code generated for sequence points reads from this location, which is
45 * made read-only when single stepping is enabled.
47 static gpointer ss_trigger_page;
49 /* Enabled breakpoints read from this trigger page */
50 static gpointer bp_trigger_page;
52 /* Structure used by the sequence points in AOTed code */
54 gpointer ss_trigger_page;
55 gpointer bp_trigger_page;
56 guint8* bp_addrs [MONO_ZERO_LEN_ARRAY];
61 * floating point support: on ARM it is a mess, there are at least 3
62 * different setups, each of which binary incompat with the other.
63 * 1) FPA: old and ugly, but unfortunately what current distros use
64 * the double binary format has the two words swapped. 8 double registers.
65 * Implemented usually by kernel emulation.
66 * 2) softfloat: the compiler emulates all the fp ops. Usually uses the
67 * ugly swapped double format (I guess a softfloat-vfp exists, too, though).
68 * 3) VFP: the new and actually sensible and useful FP support. Implemented
69 * in HW or kernel-emulated, requires new tools. I think this is what symbian uses.
71 * The plan is to write the FPA support first. softfloat can be tested in a chroot.
73 int mono_exc_esp_offset = 0;
75 #define arm_is_imm12(v) ((v) > -4096 && (v) < 4096)
76 #define arm_is_imm8(v) ((v) > -256 && (v) < 256)
77 #define arm_is_fpimm8(v) ((v) >= -1020 && (v) <= 1020)
79 #define LDR_MASK ((0xf << ARMCOND_SHIFT) | (3 << 26) | (1 << 22) | (1 << 20) | (15 << 12))
80 #define LDR_PC_VAL ((ARMCOND_AL << ARMCOND_SHIFT) | (1 << 26) | (0 << 22) | (1 << 20) | (15 << 12))
81 #define IS_LDR_PC(val) (((val) & LDR_MASK) == LDR_PC_VAL)
83 #define ADD_LR_PC_4 ((ARMCOND_AL << ARMCOND_SHIFT) | (1 << 25) | (1 << 23) | (ARMREG_PC << 16) | (ARMREG_LR << 12) | 4)
84 #define MOV_LR_PC ((ARMCOND_AL << ARMCOND_SHIFT) | (1 << 24) | (0xa << 20) | (ARMREG_LR << 12) | ARMREG_PC)
87 /* A variant of ARM_LDR_IMM which can handle large offsets */
88 #define ARM_LDR_IMM_GENERAL(code, dreg, basereg, offset, scratch_reg) do { \
89 if (arm_is_imm12 ((offset))) { \
90 ARM_LDR_IMM (code, (dreg), (basereg), (offset)); \
92 g_assert ((scratch_reg) != (basereg)); \
93 code = mono_arm_emit_load_imm (code, (scratch_reg), (offset)); \
94 ARM_LDR_REG_REG (code, (dreg), (basereg), (scratch_reg)); \
98 #define ARM_STR_IMM_GENERAL(code, dreg, basereg, offset, scratch_reg) do { \
99 if (arm_is_imm12 ((offset))) { \
100 ARM_STR_IMM (code, (dreg), (basereg), (offset)); \
102 g_assert ((scratch_reg) != (basereg)); \
103 code = mono_arm_emit_load_imm (code, (scratch_reg), (offset)); \
104 ARM_STR_REG_REG (code, (dreg), (basereg), (scratch_reg)); \
109 mono_arch_regname (int reg)
111 static const char * rnames[] = {
112 "arm_r0", "arm_r1", "arm_r2", "arm_r3", "arm_v1",
113 "arm_v2", "arm_v3", "arm_v4", "arm_v5", "arm_v6",
114 "arm_v7", "arm_fp", "arm_ip", "arm_sp", "arm_lr",
117 if (reg >= 0 && reg < 16)
123 mono_arch_fregname (int reg)
125 static const char * rnames[] = {
126 "arm_f0", "arm_f1", "arm_f2", "arm_f3", "arm_f4",
127 "arm_f5", "arm_f6", "arm_f7", "arm_f8", "arm_f9",
128 "arm_f10", "arm_f11", "arm_f12", "arm_f13", "arm_f14",
129 "arm_f15", "arm_f16", "arm_f17", "arm_f18", "arm_f19",
130 "arm_f20", "arm_f21", "arm_f22", "arm_f23", "arm_f24",
131 "arm_f25", "arm_f26", "arm_f27", "arm_f28", "arm_f29",
134 if (reg >= 0 && reg < 32)
142 emit_big_add (guint8 *code, int dreg, int sreg, int imm)
144 int imm8, rot_amount;
145 if ((imm8 = mono_arm_is_rotated_imm8 (imm, &rot_amount)) >= 0) {
146 ARM_ADD_REG_IMM (code, dreg, sreg, imm8, rot_amount);
149 g_assert (dreg != sreg);
150 code = mono_arm_emit_load_imm (code, dreg, imm);
151 ARM_ADD_REG_REG (code, dreg, dreg, sreg);
156 emit_memcpy (guint8 *code, int size, int dreg, int doffset, int sreg, int soffset)
158 /* we can use r0-r3, since this is called only for incoming args on the stack */
159 if (size > sizeof (gpointer) * 4) {
161 code = emit_big_add (code, ARMREG_R0, sreg, soffset);
162 code = emit_big_add (code, ARMREG_R1, dreg, doffset);
163 start_loop = code = mono_arm_emit_load_imm (code, ARMREG_R2, size);
164 ARM_LDR_IMM (code, ARMREG_R3, ARMREG_R0, 0);
165 ARM_STR_IMM (code, ARMREG_R3, ARMREG_R1, 0);
166 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, 4);
167 ARM_ADD_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, 4);
168 ARM_SUBS_REG_IMM8 (code, ARMREG_R2, ARMREG_R2, 4);
169 ARM_B_COND (code, ARMCOND_NE, 0);
170 arm_patch (code - 4, start_loop);
173 if (arm_is_imm12 (doffset) && arm_is_imm12 (doffset + size) &&
174 arm_is_imm12 (soffset) && arm_is_imm12 (soffset + size)) {
176 ARM_LDR_IMM (code, ARMREG_LR, sreg, soffset);
177 ARM_STR_IMM (code, ARMREG_LR, dreg, doffset);
183 code = emit_big_add (code, ARMREG_R0, sreg, soffset);
184 code = emit_big_add (code, ARMREG_R1, dreg, doffset);
185 doffset = soffset = 0;
187 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_R0, soffset);
188 ARM_STR_IMM (code, ARMREG_LR, ARMREG_R1, doffset);
194 g_assert (size == 0);
199 emit_call_reg (guint8 *code, int reg)
202 ARM_BLX_REG (code, reg);
204 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
208 ARM_MOV_REG_REG (code, ARMREG_PC, reg);
214 emit_call_seq (MonoCompile *cfg, guint8 *code)
216 if (cfg->method->dynamic) {
217 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
219 *(gpointer*)code = NULL;
221 code = emit_call_reg (code, ARMREG_IP);
229 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
231 switch (ins->opcode) {
234 case OP_FCALL_MEMBASE:
236 if (ins->dreg != ARM_FPA_F0)
237 ARM_MVFD (code, ins->dreg, ARM_FPA_F0);
238 #elif defined(ARM_FPU_VFP)
239 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4) {
240 ARM_FMSR (code, ins->dreg, ARMREG_R0);
241 ARM_CVTS (code, ins->dreg, ins->dreg);
243 ARM_FMDRR (code, ARMREG_R0, ARMREG_R1, ins->dreg);
252 #endif /* #ifndef DISABLE_JIT */
255 * mono_arch_get_argument_info:
256 * @csig: a method signature
257 * @param_count: the number of parameters to consider
258 * @arg_info: an array to store the result infos
260 * Gathers information on parameters such as size, alignment and
261 * padding. arg_info should be large enought to hold param_count + 1 entries.
263 * Returns the size of the activation frame.
266 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
268 int k, frame_size = 0;
269 guint32 size, align, pad;
272 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
273 frame_size += sizeof (gpointer);
277 arg_info [0].offset = offset;
280 frame_size += sizeof (gpointer);
284 arg_info [0].size = frame_size;
286 for (k = 0; k < param_count; k++) {
287 size = mini_type_stack_size_full (NULL, csig->params [k], &align, csig->pinvoke);
289 /* ignore alignment for now */
292 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
293 arg_info [k].pad = pad;
295 arg_info [k + 1].pad = 0;
296 arg_info [k + 1].size = size;
298 arg_info [k + 1].offset = offset;
302 align = MONO_ARCH_FRAME_ALIGNMENT;
303 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
304 arg_info [k].pad = pad;
309 #define MAX_ARCH_DELEGATE_PARAMS 3
312 get_delegate_invoke_impl (gboolean has_target, gboolean param_count, guint32 *code_size)
314 guint8 *code, *start;
317 start = code = mono_global_codeman_reserve (12);
319 /* Replace the this argument with the target */
320 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R0, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
321 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, G_STRUCT_OFFSET (MonoDelegate, target));
322 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
324 g_assert ((code - start) <= 12);
326 mono_arch_flush_icache (start, 12);
330 size = 8 + param_count * 4;
331 start = code = mono_global_codeman_reserve (size);
333 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R0, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
334 /* slide down the arguments */
335 for (i = 0; i < param_count; ++i) {
336 ARM_MOV_REG_REG (code, (ARMREG_R0 + i), (ARMREG_R0 + i + 1));
338 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
340 g_assert ((code - start) <= size);
342 mono_arch_flush_icache (start, size);
346 *code_size = code - start;
352 * mono_arch_get_delegate_invoke_impls:
354 * Return a list of MonoAotTrampInfo structures for the delegate invoke impl
358 mono_arch_get_delegate_invoke_impls (void)
365 code = get_delegate_invoke_impl (TRUE, 0, &code_len);
366 res = g_slist_prepend (res, mono_tramp_info_create (g_strdup ("delegate_invoke_impl_has_target"), code, code_len, NULL, NULL));
368 for (i = 0; i <= MAX_ARCH_DELEGATE_PARAMS; ++i) {
369 code = get_delegate_invoke_impl (FALSE, i, &code_len);
370 res = g_slist_prepend (res, mono_tramp_info_create (g_strdup_printf ("delegate_invoke_impl_target_%d", i), code, code_len, NULL, NULL));
377 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
379 guint8 *code, *start;
381 /* FIXME: Support more cases */
382 if (MONO_TYPE_ISSTRUCT (sig->ret))
386 static guint8* cached = NULL;
387 mono_mini_arch_lock ();
389 mono_mini_arch_unlock ();
394 start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
396 start = get_delegate_invoke_impl (TRUE, 0, NULL);
398 mono_mini_arch_unlock ();
401 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
404 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
406 for (i = 0; i < sig->param_count; ++i)
407 if (!mono_is_regsize_var (sig->params [i]))
410 mono_mini_arch_lock ();
411 code = cache [sig->param_count];
413 mono_mini_arch_unlock ();
418 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
419 start = mono_aot_get_trampoline (name);
422 start = get_delegate_invoke_impl (FALSE, sig->param_count, NULL);
424 cache [sig->param_count] = start;
425 mono_mini_arch_unlock ();
433 mono_arch_get_this_arg_from_call (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, mgreg_t *regs, guint8 *code)
435 return (gpointer)regs [ARMREG_R0];
439 * Initialize the cpu to execute managed code.
442 mono_arch_cpu_init (void)
447 * Initialize architecture specific code.
450 mono_arch_init (void)
452 InitializeCriticalSection (&mini_arch_mutex);
454 ss_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT);
455 bp_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT);
456 mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
458 mono_aot_register_jit_icall ("mono_arm_throw_exception", mono_arm_throw_exception);
459 mono_aot_register_jit_icall ("mono_arm_throw_exception_by_token", mono_arm_throw_exception_by_token);
463 * Cleanup architecture specific code.
466 mono_arch_cleanup (void)
471 * This function returns the optimizations supported on this cpu.
474 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
477 const char *cpu_arch = getenv ("MONO_CPU_ARCH");
478 if (cpu_arch != NULL) {
479 thumb_supported = strstr (cpu_arch, "thumb") != NULL;
480 if (strncmp (cpu_arch, "armv", 4) == 0) {
481 v5_supported = cpu_arch [4] >= '5';
482 v7_supported = cpu_arch [4] >= '7';
486 thumb_supported = TRUE;
491 FILE *file = fopen ("/proc/cpuinfo", "r");
493 while ((line = fgets (buf, 512, file))) {
494 if (strncmp (line, "Processor", 9) == 0) {
495 char *ver = strstr (line, "(v");
496 if (ver && (ver [2] == '5' || ver [2] == '6' || ver [2] == '7'))
498 if (ver && (ver [2] == '7'))
502 if (strncmp (line, "Features", 8) == 0) {
503 char *th = strstr (line, "thumb");
505 thumb_supported = TRUE;
513 /*printf ("features: v5: %d, thumb: %d\n", v5_supported, thumb_supported);*/
518 /* no arm-specific optimizations yet */
526 is_regsize_var (MonoType *t) {
529 t = mini_type_get_underlying_type (NULL, t);
536 case MONO_TYPE_FNPTR:
538 case MONO_TYPE_OBJECT:
539 case MONO_TYPE_STRING:
540 case MONO_TYPE_CLASS:
541 case MONO_TYPE_SZARRAY:
542 case MONO_TYPE_ARRAY:
544 case MONO_TYPE_GENERICINST:
545 if (!mono_type_generic_inst_is_valuetype (t))
548 case MONO_TYPE_VALUETYPE:
555 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
560 for (i = 0; i < cfg->num_varinfo; i++) {
561 MonoInst *ins = cfg->varinfo [i];
562 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
565 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
568 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
571 /* we can only allocate 32 bit values */
572 if (is_regsize_var (ins->inst_vtype)) {
573 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
574 g_assert (i == vmv->idx);
575 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
582 #define USE_EXTRA_TEMPS 0
585 mono_arch_get_global_int_regs (MonoCompile *cfg)
590 * FIXME: Interface calls might go through a static rgctx trampoline which
591 * sets V5, but it doesn't save it, so we need to save it ourselves, and
594 if (cfg->flags & MONO_CFG_HAS_CALLS)
595 cfg->uses_rgctx_reg = TRUE;
597 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V1));
598 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V2));
599 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V3));
600 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V4));
601 if (!(cfg->compile_aot || cfg->uses_rgctx_reg || COMPILE_LLVM (cfg)))
602 /* V5 is reserved for passing the vtable/rgctx/IMT method */
603 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V5));
604 /*regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V6));*/
605 /*regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V7));*/
611 * mono_arch_regalloc_cost:
613 * Return the cost, in number of memory references, of the action of
614 * allocating the variable VMV into a register during global register
618 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
624 #endif /* #ifndef DISABLE_JIT */
626 #ifndef __GNUC_PREREQ
627 #define __GNUC_PREREQ(maj, min) (0)
631 mono_arch_flush_icache (guint8 *code, gint size)
634 sys_icache_invalidate (code, size);
635 #elif __GNUC_PREREQ(4, 1)
636 __clear_cache (code, code + size);
637 #elif defined(PLATFORM_ANDROID)
638 const int syscall = 0xf0002;
646 : "r" (code), "r" (code + size), "r" (syscall)
647 : "r0", "r1", "r7", "r2"
650 __asm __volatile ("mov r0, %0\n"
653 "swi 0x9f0002 @ sys_cacheflush"
655 : "r" (code), "r" (code + size), "r" (0)
656 : "r0", "r1", "r3" );
673 guint16 vtsize; /* in param area */
676 guint8 size : 4; /* 1, 2, 4, 8, or regs used by RegTypeStructByVal */
682 gboolean vtype_retaddr;
683 /* The index of the vret arg in the argument list */
693 /*#define __alignof__(a) sizeof(a)*/
694 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
700 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
703 if (*gr > ARMREG_R3) {
704 ainfo->offset = *stack_size;
705 ainfo->reg = ARMREG_SP; /* in the caller */
706 ainfo->storage = RegTypeBase;
709 ainfo->storage = RegTypeGeneral;
713 #if defined(__APPLE__) && defined(MONO_CROSS_COMPILE)
716 int i8_align = __alignof__ (gint64);
720 gboolean split = i8_align == 4;
722 gboolean split = TRUE;
725 if (*gr == ARMREG_R3 && split) {
726 /* first word in r3 and the second on the stack */
727 ainfo->offset = *stack_size;
728 ainfo->reg = ARMREG_SP; /* in the caller */
729 ainfo->storage = RegTypeBaseGen;
731 } else if (*gr >= ARMREG_R3) {
733 /* darwin aligns longs to 4 byte only */
739 ainfo->offset = *stack_size;
740 ainfo->reg = ARMREG_SP; /* in the caller */
741 ainfo->storage = RegTypeBase;
745 if (i8_align == 8 && ((*gr) & 1))
748 ainfo->storage = RegTypeIRegPair;
757 get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSignature *sig, gboolean is_pinvoke)
760 int n = sig->hasthis + sig->param_count;
761 MonoType *simpletype;
762 guint32 stack_size = 0;
766 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
768 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
773 /* FIXME: handle returning a struct */
774 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
777 if (is_pinvoke && mono_class_native_size (mono_class_from_mono_type (sig->ret), &align) <= sizeof (gpointer)) {
778 cinfo->ret.storage = RegTypeStructByVal;
780 cinfo->vtype_retaddr = TRUE;
787 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
788 * the first argument, allowing 'this' to be always passed in the first arg reg.
789 * Also do this if the first argument is a reference type, since virtual calls
790 * are sometimes made using calli without sig->hasthis set, like in the delegate
793 if (cinfo->vtype_retaddr && !is_pinvoke && (sig->hasthis || (sig->param_count > 0 && MONO_TYPE_IS_REFERENCE (mini_type_get_underlying_type (gsctx, sig->params [0]))))) {
795 add_general (&gr, &stack_size, cinfo->args + 0, TRUE);
797 add_general (&gr, &stack_size, &cinfo->args [sig->hasthis + 0], TRUE);
801 add_general (&gr, &stack_size, &cinfo->ret, TRUE);
802 cinfo->vret_arg_index = 1;
806 add_general (&gr, &stack_size, cinfo->args + 0, TRUE);
810 if (cinfo->vtype_retaddr)
811 add_general (&gr, &stack_size, &cinfo->ret, TRUE);
814 DEBUG(printf("params: %d\n", sig->param_count));
815 for (i = pstart; i < sig->param_count; ++i) {
816 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
817 /* Prevent implicit arguments and sig_cookie from
818 being passed in registers */
820 /* Emit the signature cookie just before the implicit arguments */
821 add_general (&gr, &stack_size, &cinfo->sig_cookie, TRUE);
823 DEBUG(printf("param %d: ", i));
824 if (sig->params [i]->byref) {
825 DEBUG(printf("byref\n"));
826 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
830 simpletype = mini_type_get_underlying_type (NULL, sig->params [i]);
831 switch (simpletype->type) {
832 case MONO_TYPE_BOOLEAN:
835 cinfo->args [n].size = 1;
836 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
842 cinfo->args [n].size = 2;
843 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
848 cinfo->args [n].size = 4;
849 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
855 case MONO_TYPE_FNPTR:
856 case MONO_TYPE_CLASS:
857 case MONO_TYPE_OBJECT:
858 case MONO_TYPE_STRING:
859 case MONO_TYPE_SZARRAY:
860 case MONO_TYPE_ARRAY:
862 cinfo->args [n].size = sizeof (gpointer);
863 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
866 case MONO_TYPE_GENERICINST:
867 if (!mono_type_generic_inst_is_valuetype (simpletype)) {
868 cinfo->args [n].size = sizeof (gpointer);
869 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
874 case MONO_TYPE_TYPEDBYREF:
875 case MONO_TYPE_VALUETYPE: {
881 if (simpletype->type == MONO_TYPE_TYPEDBYREF) {
882 size = sizeof (MonoTypedRef);
883 align = sizeof (gpointer);
885 MonoClass *klass = mono_class_from_mono_type (sig->params [i]);
887 size = mono_class_native_size (klass, &align);
889 size = mono_class_value_size (klass, &align);
891 DEBUG(printf ("load %d bytes struct\n",
892 mono_class_native_size (sig->params [i]->data.klass, NULL)));
895 align_size += (sizeof (gpointer) - 1);
896 align_size &= ~(sizeof (gpointer) - 1);
897 nwords = (align_size + sizeof (gpointer) -1 ) / sizeof (gpointer);
898 cinfo->args [n].storage = RegTypeStructByVal;
899 /* FIXME: align stack_size if needed */
901 if (align >= 8 && (gr & 1))
904 if (gr > ARMREG_R3) {
905 cinfo->args [n].size = 0;
906 cinfo->args [n].vtsize = nwords;
908 int rest = ARMREG_R3 - gr + 1;
909 int n_in_regs = rest >= nwords? nwords: rest;
911 cinfo->args [n].size = n_in_regs;
912 cinfo->args [n].vtsize = nwords - n_in_regs;
913 cinfo->args [n].reg = gr;
917 cinfo->args [n].offset = stack_size;
918 /*g_print ("offset for arg %d at %d\n", n, stack_size);*/
919 stack_size += nwords * sizeof (gpointer);
926 cinfo->args [n].size = 8;
927 add_general (&gr, &stack_size, cinfo->args + n, FALSE);
931 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
935 /* Handle the case where there are no implicit arguments */
936 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
937 /* Prevent implicit arguments and sig_cookie from
938 being passed in registers */
940 /* Emit the signature cookie just before the implicit arguments */
941 add_general (&gr, &stack_size, &cinfo->sig_cookie, TRUE);
945 simpletype = mini_type_get_underlying_type (NULL, sig->ret);
946 switch (simpletype->type) {
947 case MONO_TYPE_BOOLEAN:
958 case MONO_TYPE_FNPTR:
959 case MONO_TYPE_CLASS:
960 case MONO_TYPE_OBJECT:
961 case MONO_TYPE_SZARRAY:
962 case MONO_TYPE_ARRAY:
963 case MONO_TYPE_STRING:
964 cinfo->ret.storage = RegTypeGeneral;
965 cinfo->ret.reg = ARMREG_R0;
969 cinfo->ret.storage = RegTypeIRegPair;
970 cinfo->ret.reg = ARMREG_R0;
974 cinfo->ret.storage = RegTypeFP;
975 cinfo->ret.reg = ARMREG_R0;
976 /* FIXME: cinfo->ret.reg = ???;
977 cinfo->ret.storage = RegTypeFP;*/
979 case MONO_TYPE_GENERICINST:
980 if (!mono_type_generic_inst_is_valuetype (simpletype)) {
981 cinfo->ret.storage = RegTypeGeneral;
982 cinfo->ret.reg = ARMREG_R0;
986 case MONO_TYPE_VALUETYPE:
987 case MONO_TYPE_TYPEDBYREF:
988 if (cinfo->ret.storage != RegTypeStructByVal)
989 cinfo->ret.storage = RegTypeStructByAddr;
994 g_error ("Can't handle as return value 0x%x", sig->ret->type);
998 /* align stack size to 8 */
999 DEBUG (printf (" stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
1000 stack_size = (stack_size + 7) & ~7;
1002 cinfo->stack_usage = stack_size;
1009 * Set var information according to the calling convention. arm version.
1010 * The locals var stuff should most likely be split in another method.
1013 mono_arch_allocate_vars (MonoCompile *cfg)
1015 MonoMethodSignature *sig;
1016 MonoMethodHeader *header;
1018 int i, offset, size, align, curinst;
1019 int frame_reg = ARMREG_FP;
1023 sig = mono_method_signature (cfg->method);
1025 if (!cfg->arch.cinfo)
1026 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, sig->pinvoke);
1027 cinfo = cfg->arch.cinfo;
1029 /* FIXME: this will change when we use FP as gcc does */
1030 cfg->flags |= MONO_CFG_HAS_SPILLUP;
1032 /* allow room for the vararg method args: void* and long/double */
1033 if (mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method))
1034 cfg->param_area = MAX (cfg->param_area, sizeof (gpointer)*8);
1036 header = cfg->header;
1039 * We use the frame register also for any method that has
1040 * exception clauses. This way, when the handlers are called,
1041 * the code will reference local variables using the frame reg instead of
1042 * the stack pointer: if we had to restore the stack pointer, we'd
1043 * corrupt the method frames that are already on the stack (since
1044 * filters get called before stack unwinding happens) when the filter
1045 * code would call any method (this also applies to finally etc.).
1047 if ((cfg->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
1048 frame_reg = ARMREG_FP;
1049 cfg->frame_reg = frame_reg;
1050 if (frame_reg != ARMREG_SP) {
1051 cfg->used_int_regs |= 1 << frame_reg;
1054 if (cfg->compile_aot || cfg->uses_rgctx_reg || COMPILE_LLVM (cfg))
1055 /* V5 is reserved for passing the vtable/rgctx/IMT method */
1056 cfg->used_int_regs |= (1 << ARMREG_V5);
1060 if (!MONO_TYPE_ISSTRUCT (sig->ret)) {
1061 switch (mini_type_get_underlying_type (NULL, sig->ret)->type) {
1062 case MONO_TYPE_VOID:
1065 cfg->ret->opcode = OP_REGVAR;
1066 cfg->ret->inst_c0 = ARMREG_R0;
1070 /* local vars are at a positive offset from the stack pointer */
1072 * also note that if the function uses alloca, we use FP
1073 * to point at the local variables.
1075 offset = 0; /* linkage area */
1076 /* align the offset to 16 bytes: not sure this is needed here */
1078 //offset &= ~(8 - 1);
1080 /* add parameter area size for called functions */
1081 offset += cfg->param_area;
1084 if (cfg->flags & MONO_CFG_HAS_FPOUT)
1087 /* allow room to save the return value */
1088 if (mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method))
1091 /* the MonoLMF structure is stored just below the stack pointer */
1092 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
1093 if (cinfo->ret.storage == RegTypeStructByVal) {
1094 cfg->ret->opcode = OP_REGOFFSET;
1095 cfg->ret->inst_basereg = cfg->frame_reg;
1096 offset += sizeof (gpointer) - 1;
1097 offset &= ~(sizeof (gpointer) - 1);
1098 cfg->ret->inst_offset = - offset;
1100 ins = cfg->vret_addr;
1101 offset += sizeof(gpointer) - 1;
1102 offset &= ~(sizeof(gpointer) - 1);
1103 ins->inst_offset = offset;
1104 ins->opcode = OP_REGOFFSET;
1105 ins->inst_basereg = frame_reg;
1106 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1107 printf ("vret_addr =");
1108 mono_print_ins (cfg->vret_addr);
1111 offset += sizeof(gpointer);
1114 /* Allocate these first so they have a small offset, OP_SEQ_POINT depends on this */
1115 if (cfg->arch.seq_point_info_var) {
1118 ins = cfg->arch.seq_point_info_var;
1122 offset += align - 1;
1123 offset &= ~(align - 1);
1124 ins->opcode = OP_REGOFFSET;
1125 ins->inst_basereg = frame_reg;
1126 ins->inst_offset = offset;
1129 ins = cfg->arch.ss_trigger_page_var;
1132 offset += align - 1;
1133 offset &= ~(align - 1);
1134 ins->opcode = OP_REGOFFSET;
1135 ins->inst_basereg = frame_reg;
1136 ins->inst_offset = offset;
1140 curinst = cfg->locals_start;
1141 for (i = curinst; i < cfg->num_varinfo; ++i) {
1142 ins = cfg->varinfo [i];
1143 if ((ins->flags & MONO_INST_IS_DEAD) || ins->opcode == OP_REGVAR || ins->opcode == OP_REGOFFSET)
1146 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
1147 * pinvoke wrappers when they call functions returning structure */
1148 if (ins->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (ins->inst_vtype) && ins->inst_vtype->type != MONO_TYPE_TYPEDBYREF) {
1149 size = mono_class_native_size (mono_class_from_mono_type (ins->inst_vtype), &ualign);
1153 size = mono_type_size (ins->inst_vtype, &align);
1155 /* FIXME: if a structure is misaligned, our memcpy doesn't work,
1156 * since it loads/stores misaligned words, which don't do the right thing.
1158 if (align < 4 && size >= 4)
1160 offset += align - 1;
1161 offset &= ~(align - 1);
1162 ins->opcode = OP_REGOFFSET;
1163 ins->inst_offset = offset;
1164 ins->inst_basereg = frame_reg;
1166 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
1171 ins = cfg->args [curinst];
1172 if (ins->opcode != OP_REGVAR) {
1173 ins->opcode = OP_REGOFFSET;
1174 ins->inst_basereg = frame_reg;
1175 offset += sizeof (gpointer) - 1;
1176 offset &= ~(sizeof (gpointer) - 1);
1177 ins->inst_offset = offset;
1178 offset += sizeof (gpointer);
1183 if (sig->call_convention == MONO_CALL_VARARG) {
1187 /* Allocate a local slot to hold the sig cookie address */
1188 offset += align - 1;
1189 offset &= ~(align - 1);
1190 cfg->sig_cookie = offset;
1194 for (i = 0; i < sig->param_count; ++i) {
1195 ins = cfg->args [curinst];
1197 if (ins->opcode != OP_REGVAR) {
1198 ins->opcode = OP_REGOFFSET;
1199 ins->inst_basereg = frame_reg;
1200 size = mini_type_stack_size_full (NULL, sig->params [i], &ualign, sig->pinvoke);
1202 /* FIXME: if a structure is misaligned, our memcpy doesn't work,
1203 * since it loads/stores misaligned words, which don't do the right thing.
1205 if (align < 4 && size >= 4)
1207 /* The code in the prolog () stores words when storing vtypes received in a register */
1208 if (MONO_TYPE_ISSTRUCT (sig->params [i]))
1210 offset += align - 1;
1211 offset &= ~(align - 1);
1212 ins->inst_offset = offset;
1218 /* align the offset to 8 bytes */
1223 cfg->stack_offset = offset;
1227 mono_arch_create_vars (MonoCompile *cfg)
1229 MonoMethodSignature *sig;
1232 sig = mono_method_signature (cfg->method);
1234 if (!cfg->arch.cinfo)
1235 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, sig->pinvoke);
1236 cinfo = cfg->arch.cinfo;
1238 if (cinfo->ret.storage == RegTypeStructByVal)
1239 cfg->ret_var_is_local = TRUE;
1241 if (MONO_TYPE_ISSTRUCT (sig->ret) && cinfo->ret.storage != RegTypeStructByVal) {
1242 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
1243 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1244 printf ("vret_addr = ");
1245 mono_print_ins (cfg->vret_addr);
1249 if (cfg->gen_seq_points && cfg->compile_aot) {
1250 MonoInst *ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1251 ins->flags |= MONO_INST_VOLATILE;
1252 cfg->arch.seq_point_info_var = ins;
1254 /* Allocate a separate variable for this to save 1 load per seq point */
1255 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1256 ins->flags |= MONO_INST_VOLATILE;
1257 cfg->arch.ss_trigger_page_var = ins;
1262 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1264 MonoMethodSignature *tmp_sig;
1267 if (call->tail_call)
1270 /* FIXME: Add support for signature tokens to AOT */
1271 cfg->disable_aot = TRUE;
1273 g_assert (cinfo->sig_cookie.storage == RegTypeBase);
1276 * mono_ArgIterator_Setup assumes the signature cookie is
1277 * passed first and all the arguments which were before it are
1278 * passed on the stack after the signature. So compensate by
1279 * passing a different signature.
1281 tmp_sig = mono_metadata_signature_dup (call->signature);
1282 tmp_sig->param_count -= call->signature->sentinelpos;
1283 tmp_sig->sentinelpos = 0;
1284 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1286 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
1287 sig_arg->dreg = mono_alloc_ireg (cfg);
1288 sig_arg->inst_p0 = tmp_sig;
1289 MONO_ADD_INS (cfg->cbb, sig_arg);
1291 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, cinfo->sig_cookie.offset, sig_arg->dreg);
1296 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
1301 LLVMCallInfo *linfo;
1303 n = sig->param_count + sig->hasthis;
1305 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, sig->pinvoke);
1307 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
1310 * LLVM always uses the native ABI while we use our own ABI, the
1311 * only difference is the handling of vtypes:
1312 * - we only pass/receive them in registers in some cases, and only
1313 * in 1 or 2 integer registers.
1315 if (cinfo->ret.storage != RegTypeGeneral && cinfo->ret.storage != RegTypeNone && cinfo->ret.storage != RegTypeFP && cinfo->ret.storage != RegTypeIRegPair) {
1316 cfg->exception_message = g_strdup ("unknown ret conv");
1317 cfg->disable_llvm = TRUE;
1321 for (i = 0; i < n; ++i) {
1322 ainfo = cinfo->args + i;
1324 linfo->args [i].storage = LLVMArgNone;
1326 switch (ainfo->storage) {
1327 case RegTypeGeneral:
1328 case RegTypeIRegPair:
1330 linfo->args [i].storage = LLVMArgInIReg;
1333 cfg->exception_message = g_strdup_printf ("ainfo->storage (%d)", ainfo->storage);
1334 cfg->disable_llvm = TRUE;
1344 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
1347 MonoMethodSignature *sig;
1351 sig = call->signature;
1352 n = sig->param_count + sig->hasthis;
1354 cinfo = get_call_info (cfg->generic_sharing_context, NULL, sig, sig->pinvoke);
1356 for (i = 0; i < n; ++i) {
1357 ArgInfo *ainfo = cinfo->args + i;
1360 if (i >= sig->hasthis)
1361 t = sig->params [i - sig->hasthis];
1363 t = &mono_defaults.int_class->byval_arg;
1364 t = mini_type_get_underlying_type (NULL, t);
1366 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1367 /* Emit the signature cookie just before the implicit arguments */
1368 emit_sig_cookie (cfg, call, cinfo);
1371 in = call->args [i];
1373 switch (ainfo->storage) {
1374 case RegTypeGeneral:
1375 case RegTypeIRegPair:
1376 if (!t->byref && ((t->type == MONO_TYPE_I8) || (t->type == MONO_TYPE_U8))) {
1377 MONO_INST_NEW (cfg, ins, OP_MOVE);
1378 ins->dreg = mono_alloc_ireg (cfg);
1379 ins->sreg1 = in->dreg + 1;
1380 MONO_ADD_INS (cfg->cbb, ins);
1381 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1383 MONO_INST_NEW (cfg, ins, OP_MOVE);
1384 ins->dreg = mono_alloc_ireg (cfg);
1385 ins->sreg1 = in->dreg + 2;
1386 MONO_ADD_INS (cfg->cbb, ins);
1387 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg + 1, FALSE);
1388 } else if (!t->byref && ((t->type == MONO_TYPE_R8) || (t->type == MONO_TYPE_R4))) {
1389 #ifndef MONO_ARCH_SOFT_FLOAT
1393 if (ainfo->size == 4) {
1394 #ifdef MONO_ARCH_SOFT_FLOAT
1395 /* mono_emit_call_args () have already done the r8->r4 conversion */
1396 /* The converted value is in an int vreg */
1397 MONO_INST_NEW (cfg, ins, OP_MOVE);
1398 ins->dreg = mono_alloc_ireg (cfg);
1399 ins->sreg1 = in->dreg;
1400 MONO_ADD_INS (cfg->cbb, ins);
1401 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1403 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, ARMREG_SP, (cfg->param_area - 8), in->dreg);
1404 creg = mono_alloc_ireg (cfg);
1405 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8));
1406 mono_call_inst_add_outarg_reg (cfg, call, creg, ainfo->reg, FALSE);
1409 #ifdef MONO_ARCH_SOFT_FLOAT
1410 MONO_INST_NEW (cfg, ins, OP_FGETLOW32);
1411 ins->dreg = mono_alloc_ireg (cfg);
1412 ins->sreg1 = in->dreg;
1413 MONO_ADD_INS (cfg->cbb, ins);
1414 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1416 MONO_INST_NEW (cfg, ins, OP_FGETHIGH32);
1417 ins->dreg = mono_alloc_ireg (cfg);
1418 ins->sreg1 = in->dreg;
1419 MONO_ADD_INS (cfg->cbb, ins);
1420 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg + 1, FALSE);
1422 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, ARMREG_SP, (cfg->param_area - 8), in->dreg);
1423 creg = mono_alloc_ireg (cfg);
1424 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8));
1425 mono_call_inst_add_outarg_reg (cfg, call, creg, ainfo->reg, FALSE);
1426 creg = mono_alloc_ireg (cfg);
1427 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8 + 4));
1428 mono_call_inst_add_outarg_reg (cfg, call, creg, ainfo->reg + 1, FALSE);
1431 cfg->flags |= MONO_CFG_HAS_FPOUT;
1433 MONO_INST_NEW (cfg, ins, OP_MOVE);
1434 ins->dreg = mono_alloc_ireg (cfg);
1435 ins->sreg1 = in->dreg;
1436 MONO_ADD_INS (cfg->cbb, ins);
1438 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1441 case RegTypeStructByAddr:
1444 /* FIXME: where si the data allocated? */
1445 arg->backend.reg3 = ainfo->reg;
1446 call->used_iregs |= 1 << ainfo->reg;
1447 g_assert_not_reached ();
1450 case RegTypeStructByVal:
1451 MONO_INST_NEW (cfg, ins, OP_OUTARG_VT);
1452 ins->opcode = OP_OUTARG_VT;
1453 ins->sreg1 = in->dreg;
1454 ins->klass = in->klass;
1455 ins->inst_p0 = call;
1456 ins->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
1457 memcpy (ins->inst_p1, ainfo, sizeof (ArgInfo));
1458 MONO_ADD_INS (cfg->cbb, ins);
1461 if (!t->byref && ((t->type == MONO_TYPE_I8) || (t->type == MONO_TYPE_U8))) {
1462 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1463 } else if (!t->byref && ((t->type == MONO_TYPE_R4) || (t->type == MONO_TYPE_R8))) {
1464 if (t->type == MONO_TYPE_R8) {
1465 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1467 #ifdef MONO_ARCH_SOFT_FLOAT
1468 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1470 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1474 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1477 case RegTypeBaseGen:
1478 if (!t->byref && ((t->type == MONO_TYPE_I8) || (t->type == MONO_TYPE_U8))) {
1479 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, ainfo->offset, (G_BYTE_ORDER == G_BIG_ENDIAN) ? in->dreg + 1 : in->dreg + 2);
1480 MONO_INST_NEW (cfg, ins, OP_MOVE);
1481 ins->dreg = mono_alloc_ireg (cfg);
1482 ins->sreg1 = G_BYTE_ORDER == G_BIG_ENDIAN ? in->dreg + 2 : in->dreg + 1;
1483 MONO_ADD_INS (cfg->cbb, ins);
1484 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ARMREG_R3, FALSE);
1485 } else if (!t->byref && (t->type == MONO_TYPE_R8)) {
1488 #ifdef MONO_ARCH_SOFT_FLOAT
1489 g_assert_not_reached ();
1492 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, ARMREG_SP, (cfg->param_area - 8), in->dreg);
1493 creg = mono_alloc_ireg (cfg);
1494 mono_call_inst_add_outarg_reg (cfg, call, creg, ARMREG_R3, FALSE);
1495 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8));
1496 creg = mono_alloc_ireg (cfg);
1497 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 4));
1498 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, ainfo->offset, creg);
1499 cfg->flags |= MONO_CFG_HAS_FPOUT;
1501 g_assert_not_reached ();
1508 arg->backend.reg3 = ainfo->reg;
1509 /* FP args are passed in int regs */
1510 call->used_iregs |= 1 << ainfo->reg;
1511 if (ainfo->size == 8) {
1512 arg->opcode = OP_OUTARG_R8;
1513 call->used_iregs |= 1 << (ainfo->reg + 1);
1515 arg->opcode = OP_OUTARG_R4;
1518 cfg->flags |= MONO_CFG_HAS_FPOUT;
1522 g_assert_not_reached ();
1526 /* Handle the case where there are no implicit arguments */
1527 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos))
1528 emit_sig_cookie (cfg, call, cinfo);
1530 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
1533 if (cinfo->ret.storage == RegTypeStructByVal) {
1534 /* The JIT will transform this into a normal call */
1535 call->vret_in_reg = TRUE;
1537 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
1538 vtarg->sreg1 = call->vret_var->dreg;
1539 vtarg->dreg = mono_alloc_preg (cfg);
1540 MONO_ADD_INS (cfg->cbb, vtarg);
1542 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
1546 call->stack_usage = cinfo->stack_usage;
1552 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
1554 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
1555 ArgInfo *ainfo = ins->inst_p1;
1556 int ovf_size = ainfo->vtsize;
1557 int doffset = ainfo->offset;
1558 int i, soffset, dreg;
1561 for (i = 0; i < ainfo->size; ++i) {
1562 dreg = mono_alloc_ireg (cfg);
1563 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, soffset);
1564 mono_call_inst_add_outarg_reg (cfg, call, dreg, ainfo->reg + i, FALSE);
1565 soffset += sizeof (gpointer);
1567 //g_print ("vt size: %d at R%d + %d\n", doffset, vt->inst_basereg, vt->inst_offset);
1569 mini_emit_memcpy (cfg, ARMREG_SP, doffset, src->dreg, soffset, ovf_size * sizeof (gpointer), 0);
1573 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
1575 MonoType *ret = mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret);
1578 if (ret->type == MONO_TYPE_I8 || ret->type == MONO_TYPE_U8) {
1581 if (COMPILE_LLVM (cfg)) {
1582 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
1584 MONO_INST_NEW (cfg, ins, OP_SETLRET);
1585 ins->sreg1 = val->dreg + 1;
1586 ins->sreg2 = val->dreg + 2;
1587 MONO_ADD_INS (cfg->cbb, ins);
1591 #ifdef MONO_ARCH_SOFT_FLOAT
1592 if (ret->type == MONO_TYPE_R8) {
1595 MONO_INST_NEW (cfg, ins, OP_SETFRET);
1596 ins->dreg = cfg->ret->dreg;
1597 ins->sreg1 = val->dreg;
1598 MONO_ADD_INS (cfg->cbb, ins);
1601 if (ret->type == MONO_TYPE_R4) {
1602 /* Already converted to an int in method_to_ir () */
1603 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
1606 #elif defined(ARM_FPU_VFP)
1607 if (ret->type == MONO_TYPE_R8 || ret->type == MONO_TYPE_R4) {
1610 MONO_INST_NEW (cfg, ins, OP_SETFRET);
1611 ins->dreg = cfg->ret->dreg;
1612 ins->sreg1 = val->dreg;
1613 MONO_ADD_INS (cfg->cbb, ins);
1617 if (ret->type == MONO_TYPE_R4 || ret->type == MONO_TYPE_R8) {
1618 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
1625 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
1628 #endif /* #ifndef DISABLE_JIT */
1631 mono_arch_is_inst_imm (gint64 imm)
1636 #define DYN_CALL_STACK_ARGS 6
1639 MonoMethodSignature *sig;
1644 mgreg_t regs [PARAM_REGS + DYN_CALL_STACK_ARGS];
1650 dyn_call_supported (CallInfo *cinfo, MonoMethodSignature *sig)
1654 if (sig->hasthis + sig->param_count > PARAM_REGS + DYN_CALL_STACK_ARGS)
1657 switch (cinfo->ret.storage) {
1659 case RegTypeGeneral:
1660 case RegTypeIRegPair:
1661 case RegTypeStructByAddr:
1666 #elif defined(ARM_FPU_VFP)
1675 for (i = 0; i < cinfo->nargs; ++i) {
1676 switch (cinfo->args [i].storage) {
1677 case RegTypeGeneral:
1679 case RegTypeIRegPair:
1682 if (cinfo->args [i].offset >= (DYN_CALL_STACK_ARGS * sizeof (gpointer)))
1685 case RegTypeStructByVal:
1686 if (cinfo->args [i].reg + cinfo->args [i].vtsize >= PARAM_REGS + DYN_CALL_STACK_ARGS)
1694 // FIXME: Can't use cinfo only as it doesn't contain info about I8/float */
1695 for (i = 0; i < sig->param_count; ++i) {
1696 MonoType *t = sig->params [i];
1704 #ifdef MONO_ARCH_SOFT_FLOAT
1723 mono_arch_dyn_call_prepare (MonoMethodSignature *sig)
1725 ArchDynCallInfo *info;
1728 cinfo = get_call_info (NULL, NULL, sig, FALSE);
1730 if (!dyn_call_supported (cinfo, sig)) {
1735 info = g_new0 (ArchDynCallInfo, 1);
1736 // FIXME: Preprocess the info to speed up start_dyn_call ()
1738 info->cinfo = cinfo;
1740 return (MonoDynCallInfo*)info;
1744 mono_arch_dyn_call_free (MonoDynCallInfo *info)
1746 ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
1748 g_free (ainfo->cinfo);
1753 mono_arch_start_dyn_call (MonoDynCallInfo *info, gpointer **args, guint8 *ret, guint8 *buf, int buf_len)
1755 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
1756 DynCallArgs *p = (DynCallArgs*)buf;
1757 int arg_index, greg, i, j;
1758 MonoMethodSignature *sig = dinfo->sig;
1760 g_assert (buf_len >= sizeof (DynCallArgs));
1768 if (dinfo->cinfo->vtype_retaddr)
1769 p->regs [greg ++] = (mgreg_t)ret;
1772 p->regs [greg ++] = (mgreg_t)*(args [arg_index ++]);
1774 for (i = 0; i < sig->param_count; i++) {
1775 MonoType *t = mono_type_get_underlying_type (sig->params [i]);
1776 gpointer *arg = args [arg_index ++];
1777 ArgInfo *ainfo = &dinfo->cinfo->args [i + sig->hasthis];
1780 if (ainfo->storage == RegTypeGeneral || ainfo->storage == RegTypeIRegPair || ainfo->storage == RegTypeStructByVal)
1782 else if (ainfo->storage == RegTypeBase)
1783 slot = PARAM_REGS + (ainfo->offset / 4);
1785 g_assert_not_reached ();
1788 p->regs [slot] = (mgreg_t)*arg;
1793 case MONO_TYPE_STRING:
1794 case MONO_TYPE_CLASS:
1795 case MONO_TYPE_ARRAY:
1796 case MONO_TYPE_SZARRAY:
1797 case MONO_TYPE_OBJECT:
1801 p->regs [slot] = (mgreg_t)*arg;
1803 case MONO_TYPE_BOOLEAN:
1805 p->regs [slot] = *(guint8*)arg;
1808 p->regs [slot] = *(gint8*)arg;
1811 p->regs [slot] = *(gint16*)arg;
1814 case MONO_TYPE_CHAR:
1815 p->regs [slot] = *(guint16*)arg;
1818 p->regs [slot] = *(gint32*)arg;
1821 p->regs [slot] = *(guint32*)arg;
1825 p->regs [slot ++] = (mgreg_t)arg [0];
1826 p->regs [slot] = (mgreg_t)arg [1];
1829 p->regs [slot] = *(mgreg_t*)arg;
1832 p->regs [slot ++] = (mgreg_t)arg [0];
1833 p->regs [slot] = (mgreg_t)arg [1];
1835 case MONO_TYPE_GENERICINST:
1836 if (MONO_TYPE_IS_REFERENCE (t)) {
1837 p->regs [slot] = (mgreg_t)*arg;
1842 case MONO_TYPE_VALUETYPE:
1843 g_assert (ainfo->storage == RegTypeStructByVal);
1845 if (ainfo->size == 0)
1846 slot = PARAM_REGS + (ainfo->offset / 4);
1850 for (j = 0; j < ainfo->size + ainfo->vtsize; ++j)
1851 p->regs [slot ++] = ((mgreg_t*)arg) [j];
1854 g_assert_not_reached ();
1860 mono_arch_finish_dyn_call (MonoDynCallInfo *info, guint8 *buf)
1862 ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
1863 MonoMethodSignature *sig = ((ArchDynCallInfo*)info)->sig;
1864 guint8 *ret = ((DynCallArgs*)buf)->ret;
1865 mgreg_t res = ((DynCallArgs*)buf)->res;
1866 mgreg_t res2 = ((DynCallArgs*)buf)->res2;
1868 switch (mono_type_get_underlying_type (sig->ret)->type) {
1869 case MONO_TYPE_VOID:
1870 *(gpointer*)ret = NULL;
1872 case MONO_TYPE_STRING:
1873 case MONO_TYPE_CLASS:
1874 case MONO_TYPE_ARRAY:
1875 case MONO_TYPE_SZARRAY:
1876 case MONO_TYPE_OBJECT:
1880 *(gpointer*)ret = (gpointer)res;
1886 case MONO_TYPE_BOOLEAN:
1887 *(guint8*)ret = res;
1890 *(gint16*)ret = res;
1893 case MONO_TYPE_CHAR:
1894 *(guint16*)ret = res;
1897 *(gint32*)ret = res;
1900 *(guint32*)ret = res;
1904 /* This handles endianness as well */
1905 ((gint32*)ret) [0] = res;
1906 ((gint32*)ret) [1] = res2;
1908 case MONO_TYPE_GENERICINST:
1909 if (MONO_TYPE_IS_REFERENCE (sig->ret)) {
1910 *(gpointer*)ret = (gpointer)res;
1915 case MONO_TYPE_VALUETYPE:
1916 g_assert (ainfo->cinfo->vtype_retaddr);
1919 #if defined(ARM_FPU_VFP)
1921 *(float*)ret = *(float*)&res;
1923 case MONO_TYPE_R8: {
1929 *(double*)ret = *(double*)®s;
1934 g_assert_not_reached ();
1941 * Allow tracing to work with this interface (with an optional argument)
1945 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1949 code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->method);
1950 ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0); /* NULL ebp for now */
1951 code = mono_arm_emit_load_imm (code, ARMREG_R2, (guint32)func);
1952 code = emit_call_reg (code, ARMREG_R2);
1965 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
1968 int save_mode = SAVE_NONE;
1970 MonoMethod *method = cfg->method;
1971 int rtype = mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret)->type;
1972 int save_offset = cfg->param_area;
1976 offset = code - cfg->native_code;
1977 /* we need about 16 instructions */
1978 if (offset > (cfg->code_size - 16 * 4)) {
1979 cfg->code_size *= 2;
1980 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
1981 code = cfg->native_code + offset;
1984 case MONO_TYPE_VOID:
1985 /* special case string .ctor icall */
1986 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
1987 save_mode = SAVE_ONE;
1989 save_mode = SAVE_NONE;
1993 save_mode = SAVE_TWO;
1997 save_mode = SAVE_FP;
1999 case MONO_TYPE_VALUETYPE:
2000 save_mode = SAVE_STRUCT;
2003 save_mode = SAVE_ONE;
2007 switch (save_mode) {
2009 ARM_STR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2010 ARM_STR_IMM (code, ARMREG_R1, cfg->frame_reg, save_offset + 4);
2011 if (enable_arguments) {
2012 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_R1);
2013 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
2017 ARM_STR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2018 if (enable_arguments) {
2019 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
2023 /* FIXME: what reg? */
2024 if (enable_arguments) {
2025 /* FIXME: what reg? */
2029 if (enable_arguments) {
2030 /* FIXME: get the actual address */
2031 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
2039 code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->method);
2040 code = mono_arm_emit_load_imm (code, ARMREG_IP, (guint32)func);
2041 code = emit_call_reg (code, ARMREG_IP);
2043 switch (save_mode) {
2045 ARM_LDR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2046 ARM_LDR_IMM (code, ARMREG_R1, cfg->frame_reg, save_offset + 4);
2049 ARM_LDR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2063 * The immediate field for cond branches is big enough for all reasonable methods
2065 #define EMIT_COND_BRANCH_FLAGS(ins,condcode) \
2066 if (0 && ins->inst_true_bb->native_offset) { \
2067 ARM_B_COND (code, (condcode), (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffffff); \
2069 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
2070 ARM_B_COND (code, (condcode), 0); \
2073 #define EMIT_COND_BRANCH(ins,cond) EMIT_COND_BRANCH_FLAGS(ins, branch_cc_table [(cond)])
2075 /* emit an exception if condition is fail
2077 * We assign the extra code used to throw the implicit exceptions
2078 * to cfg->bb_exit as far as the big branch handling is concerned
2080 #define EMIT_COND_SYSTEM_EXCEPTION_FLAGS(condcode,exc_name) \
2082 mono_add_patch_info (cfg, code - cfg->native_code, \
2083 MONO_PATCH_INFO_EXC, exc_name); \
2084 ARM_BL_COND (code, (condcode), 0); \
2087 #define EMIT_COND_SYSTEM_EXCEPTION(cond,exc_name) EMIT_COND_SYSTEM_EXCEPTION_FLAGS(branch_cc_table [(cond)], (exc_name))
2090 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
2095 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
2097 MonoInst *ins, *n, *last_ins = NULL;
2099 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2100 switch (ins->opcode) {
2103 /* Already done by an arch-independent pass */
2105 case OP_LOAD_MEMBASE:
2106 case OP_LOADI4_MEMBASE:
2108 * OP_STORE_MEMBASE_REG reg, offset(basereg)
2109 * OP_LOAD_MEMBASE offset(basereg), reg
2111 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
2112 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
2113 ins->inst_basereg == last_ins->inst_destbasereg &&
2114 ins->inst_offset == last_ins->inst_offset) {
2115 if (ins->dreg == last_ins->sreg1) {
2116 MONO_DELETE_INS (bb, ins);
2119 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
2120 ins->opcode = OP_MOVE;
2121 ins->sreg1 = last_ins->sreg1;
2125 * Note: reg1 must be different from the basereg in the second load
2126 * OP_LOAD_MEMBASE offset(basereg), reg1
2127 * OP_LOAD_MEMBASE offset(basereg), reg2
2129 * OP_LOAD_MEMBASE offset(basereg), reg1
2130 * OP_MOVE reg1, reg2
2132 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
2133 || last_ins->opcode == OP_LOAD_MEMBASE) &&
2134 ins->inst_basereg != last_ins->dreg &&
2135 ins->inst_basereg == last_ins->inst_basereg &&
2136 ins->inst_offset == last_ins->inst_offset) {
2138 if (ins->dreg == last_ins->dreg) {
2139 MONO_DELETE_INS (bb, ins);
2142 ins->opcode = OP_MOVE;
2143 ins->sreg1 = last_ins->dreg;
2146 //g_assert_not_reached ();
2150 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2151 * OP_LOAD_MEMBASE offset(basereg), reg
2153 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2154 * OP_ICONST reg, imm
2156 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
2157 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
2158 ins->inst_basereg == last_ins->inst_destbasereg &&
2159 ins->inst_offset == last_ins->inst_offset) {
2160 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
2161 ins->opcode = OP_ICONST;
2162 ins->inst_c0 = last_ins->inst_imm;
2163 g_assert_not_reached (); // check this rule
2167 case OP_LOADU1_MEMBASE:
2168 case OP_LOADI1_MEMBASE:
2169 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
2170 ins->inst_basereg == last_ins->inst_destbasereg &&
2171 ins->inst_offset == last_ins->inst_offset) {
2172 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? OP_ICONV_TO_I1 : OP_ICONV_TO_U1;
2173 ins->sreg1 = last_ins->sreg1;
2176 case OP_LOADU2_MEMBASE:
2177 case OP_LOADI2_MEMBASE:
2178 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
2179 ins->inst_basereg == last_ins->inst_destbasereg &&
2180 ins->inst_offset == last_ins->inst_offset) {
2181 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_ICONV_TO_I2 : OP_ICONV_TO_U2;
2182 ins->sreg1 = last_ins->sreg1;
2186 ins->opcode = OP_MOVE;
2190 if (ins->dreg == ins->sreg1) {
2191 MONO_DELETE_INS (bb, ins);
2195 * OP_MOVE sreg, dreg
2196 * OP_MOVE dreg, sreg
2198 if (last_ins && last_ins->opcode == OP_MOVE &&
2199 ins->sreg1 == last_ins->dreg &&
2200 ins->dreg == last_ins->sreg1) {
2201 MONO_DELETE_INS (bb, ins);
2209 bb->last_ins = last_ins;
2213 * the branch_cc_table should maintain the order of these
2227 branch_cc_table [] = {
2241 #define NEW_INS(cfg,dest,op) do { \
2242 MONO_INST_NEW ((cfg), (dest), (op)); \
2243 mono_bblock_insert_before_ins (bb, ins, (dest)); \
2247 map_to_reg_reg_op (int op)
2256 case OP_COMPARE_IMM:
2258 case OP_ICOMPARE_IMM:
2272 case OP_LOAD_MEMBASE:
2273 return OP_LOAD_MEMINDEX;
2274 case OP_LOADI4_MEMBASE:
2275 return OP_LOADI4_MEMINDEX;
2276 case OP_LOADU4_MEMBASE:
2277 return OP_LOADU4_MEMINDEX;
2278 case OP_LOADU1_MEMBASE:
2279 return OP_LOADU1_MEMINDEX;
2280 case OP_LOADI2_MEMBASE:
2281 return OP_LOADI2_MEMINDEX;
2282 case OP_LOADU2_MEMBASE:
2283 return OP_LOADU2_MEMINDEX;
2284 case OP_LOADI1_MEMBASE:
2285 return OP_LOADI1_MEMINDEX;
2286 case OP_STOREI1_MEMBASE_REG:
2287 return OP_STOREI1_MEMINDEX;
2288 case OP_STOREI2_MEMBASE_REG:
2289 return OP_STOREI2_MEMINDEX;
2290 case OP_STOREI4_MEMBASE_REG:
2291 return OP_STOREI4_MEMINDEX;
2292 case OP_STORE_MEMBASE_REG:
2293 return OP_STORE_MEMINDEX;
2294 case OP_STORER4_MEMBASE_REG:
2295 return OP_STORER4_MEMINDEX;
2296 case OP_STORER8_MEMBASE_REG:
2297 return OP_STORER8_MEMINDEX;
2298 case OP_STORE_MEMBASE_IMM:
2299 return OP_STORE_MEMBASE_REG;
2300 case OP_STOREI1_MEMBASE_IMM:
2301 return OP_STOREI1_MEMBASE_REG;
2302 case OP_STOREI2_MEMBASE_IMM:
2303 return OP_STOREI2_MEMBASE_REG;
2304 case OP_STOREI4_MEMBASE_IMM:
2305 return OP_STOREI4_MEMBASE_REG;
2307 g_assert_not_reached ();
2311 * Remove from the instruction list the instructions that can't be
2312 * represented with very simple instructions with no register
2316 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
2318 MonoInst *ins, *temp, *last_ins = NULL;
2319 int rot_amount, imm8, low_imm;
2321 MONO_BB_FOR_EACH_INS (bb, ins) {
2323 switch (ins->opcode) {
2327 case OP_COMPARE_IMM:
2328 case OP_ICOMPARE_IMM:
2342 if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount)) < 0) {
2343 NEW_INS (cfg, temp, OP_ICONST);
2344 temp->inst_c0 = ins->inst_imm;
2345 temp->dreg = mono_alloc_ireg (cfg);
2346 ins->sreg2 = temp->dreg;
2347 ins->opcode = mono_op_imm_to_op (ins->opcode);
2349 if (ins->opcode == OP_SBB || ins->opcode == OP_ISBB || ins->opcode == OP_SUBCC)
2355 if (ins->inst_imm == 1) {
2356 ins->opcode = OP_MOVE;
2359 if (ins->inst_imm == 0) {
2360 ins->opcode = OP_ICONST;
2364 imm8 = mono_is_power_of_two (ins->inst_imm);
2366 ins->opcode = OP_SHL_IMM;
2367 ins->inst_imm = imm8;
2370 NEW_INS (cfg, temp, OP_ICONST);
2371 temp->inst_c0 = ins->inst_imm;
2372 temp->dreg = mono_alloc_ireg (cfg);
2373 ins->sreg2 = temp->dreg;
2374 ins->opcode = OP_IMUL;
2380 if (ins->next && (ins->next->opcode == OP_COND_EXC_C || ins->next->opcode == OP_COND_EXC_IC))
2381 /* ARM sets the C flag to 1 if there was _no_ overflow */
2382 ins->next->opcode = OP_COND_EXC_NC;
2384 case OP_LOCALLOC_IMM:
2385 NEW_INS (cfg, temp, OP_ICONST);
2386 temp->inst_c0 = ins->inst_imm;
2387 temp->dreg = mono_alloc_ireg (cfg);
2388 ins->sreg1 = temp->dreg;
2389 ins->opcode = OP_LOCALLOC;
2391 case OP_LOAD_MEMBASE:
2392 case OP_LOADI4_MEMBASE:
2393 case OP_LOADU4_MEMBASE:
2394 case OP_LOADU1_MEMBASE:
2395 /* we can do two things: load the immed in a register
2396 * and use an indexed load, or see if the immed can be
2397 * represented as an ad_imm + a load with a smaller offset
2398 * that fits. We just do the first for now, optimize later.
2400 if (arm_is_imm12 (ins->inst_offset))
2402 NEW_INS (cfg, temp, OP_ICONST);
2403 temp->inst_c0 = ins->inst_offset;
2404 temp->dreg = mono_alloc_ireg (cfg);
2405 ins->sreg2 = temp->dreg;
2406 ins->opcode = map_to_reg_reg_op (ins->opcode);
2408 case OP_LOADI2_MEMBASE:
2409 case OP_LOADU2_MEMBASE:
2410 case OP_LOADI1_MEMBASE:
2411 if (arm_is_imm8 (ins->inst_offset))
2413 NEW_INS (cfg, temp, OP_ICONST);
2414 temp->inst_c0 = ins->inst_offset;
2415 temp->dreg = mono_alloc_ireg (cfg);
2416 ins->sreg2 = temp->dreg;
2417 ins->opcode = map_to_reg_reg_op (ins->opcode);
2419 case OP_LOADR4_MEMBASE:
2420 case OP_LOADR8_MEMBASE:
2421 if (arm_is_fpimm8 (ins->inst_offset))
2423 low_imm = ins->inst_offset & 0x1ff;
2424 if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_offset & ~0x1ff, &rot_amount)) >= 0) {
2425 NEW_INS (cfg, temp, OP_ADD_IMM);
2426 temp->inst_imm = ins->inst_offset & ~0x1ff;
2427 temp->sreg1 = ins->inst_basereg;
2428 temp->dreg = mono_alloc_ireg (cfg);
2429 ins->inst_basereg = temp->dreg;
2430 ins->inst_offset = low_imm;
2433 /* VFP/FPA doesn't have indexed load instructions */
2434 g_assert_not_reached ();
2436 case OP_STORE_MEMBASE_REG:
2437 case OP_STOREI4_MEMBASE_REG:
2438 case OP_STOREI1_MEMBASE_REG:
2439 if (arm_is_imm12 (ins->inst_offset))
2441 NEW_INS (cfg, temp, OP_ICONST);
2442 temp->inst_c0 = ins->inst_offset;
2443 temp->dreg = mono_alloc_ireg (cfg);
2444 ins->sreg2 = temp->dreg;
2445 ins->opcode = map_to_reg_reg_op (ins->opcode);
2447 case OP_STOREI2_MEMBASE_REG:
2448 if (arm_is_imm8 (ins->inst_offset))
2450 NEW_INS (cfg, temp, OP_ICONST);
2451 temp->inst_c0 = ins->inst_offset;
2452 temp->dreg = mono_alloc_ireg (cfg);
2453 ins->sreg2 = temp->dreg;
2454 ins->opcode = map_to_reg_reg_op (ins->opcode);
2456 case OP_STORER4_MEMBASE_REG:
2457 case OP_STORER8_MEMBASE_REG:
2458 if (arm_is_fpimm8 (ins->inst_offset))
2460 low_imm = ins->inst_offset & 0x1ff;
2461 if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_offset & ~ 0x1ff, &rot_amount)) >= 0 && arm_is_fpimm8 (low_imm)) {
2462 NEW_INS (cfg, temp, OP_ADD_IMM);
2463 temp->inst_imm = ins->inst_offset & ~0x1ff;
2464 temp->sreg1 = ins->inst_destbasereg;
2465 temp->dreg = mono_alloc_ireg (cfg);
2466 ins->inst_destbasereg = temp->dreg;
2467 ins->inst_offset = low_imm;
2470 /*g_print ("fail with: %d (%d, %d)\n", ins->inst_offset, ins->inst_offset & ~0x1ff, low_imm);*/
2471 /* VFP/FPA doesn't have indexed store instructions */
2472 g_assert_not_reached ();
2474 case OP_STORE_MEMBASE_IMM:
2475 case OP_STOREI1_MEMBASE_IMM:
2476 case OP_STOREI2_MEMBASE_IMM:
2477 case OP_STOREI4_MEMBASE_IMM:
2478 NEW_INS (cfg, temp, OP_ICONST);
2479 temp->inst_c0 = ins->inst_imm;
2480 temp->dreg = mono_alloc_ireg (cfg);
2481 ins->sreg1 = temp->dreg;
2482 ins->opcode = map_to_reg_reg_op (ins->opcode);
2484 goto loop_start; /* make it handle the possibly big ins->inst_offset */
2486 gboolean swap = FALSE;
2490 /* Optimized away */
2495 /* Some fp compares require swapped operands */
2496 switch (ins->next->opcode) {
2498 ins->next->opcode = OP_FBLT;
2502 ins->next->opcode = OP_FBLT_UN;
2506 ins->next->opcode = OP_FBGE;
2510 ins->next->opcode = OP_FBGE_UN;
2518 ins->sreg1 = ins->sreg2;
2527 bb->last_ins = last_ins;
2528 bb->max_vreg = cfg->next_vreg;
2532 mono_arch_decompose_long_opts (MonoCompile *cfg, MonoInst *long_ins)
2536 if (long_ins->opcode == OP_LNEG) {
2538 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ARM_RSBS_IMM, ins->dreg + 1, ins->sreg1 + 1, 0);
2539 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ARM_RSC_IMM, ins->dreg + 2, ins->sreg1 + 2, 0);
2545 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
2547 /* sreg is a float, dreg is an integer reg */
2549 ARM_FIXZ (code, dreg, sreg);
2550 #elif defined(ARM_FPU_VFP)
2552 ARM_TOSIZD (code, ARM_VFP_F0, sreg);
2554 ARM_TOUIZD (code, ARM_VFP_F0, sreg);
2555 ARM_FMRS (code, dreg, ARM_VFP_F0);
2559 ARM_AND_REG_IMM8 (code, dreg, dreg, 0xff);
2560 else if (size == 2) {
2561 ARM_SHL_IMM (code, dreg, dreg, 16);
2562 ARM_SHR_IMM (code, dreg, dreg, 16);
2566 ARM_SHL_IMM (code, dreg, dreg, 24);
2567 ARM_SAR_IMM (code, dreg, dreg, 24);
2568 } else if (size == 2) {
2569 ARM_SHL_IMM (code, dreg, dreg, 16);
2570 ARM_SAR_IMM (code, dreg, dreg, 16);
2576 #endif /* #ifndef DISABLE_JIT */
2580 const guchar *target;
2585 #define is_call_imm(diff) ((gint)(diff) >= -33554432 && (gint)(diff) <= 33554431)
2588 search_thunk_slot (void *data, int csize, int bsize, void *user_data) {
2589 PatchData *pdata = (PatchData*)user_data;
2590 guchar *code = data;
2591 guint32 *thunks = data;
2592 guint32 *endthunks = (guint32*)(code + bsize);
2594 int difflow, diffhigh;
2596 /* always ensure a call from pdata->code can reach to the thunks without further thunks */
2597 difflow = (char*)pdata->code - (char*)thunks;
2598 diffhigh = (char*)pdata->code - (char*)endthunks;
2599 if (!((is_call_imm (thunks) && is_call_imm (endthunks)) || (is_call_imm (difflow) && is_call_imm (diffhigh))))
2603 * The thunk is composed of 3 words:
2604 * load constant from thunks [2] into ARM_IP
2607 * Note that the LR register is already setup
2609 //g_print ("thunk nentries: %d\n", ((char*)endthunks - (char*)thunks)/16);
2610 if ((pdata->found == 2) || (pdata->code >= code && pdata->code <= code + csize)) {
2611 while (thunks < endthunks) {
2612 //g_print ("looking for target: %p at %p (%08x-%08x)\n", pdata->target, thunks, thunks [0], thunks [1]);
2613 if (thunks [2] == (guint32)pdata->target) {
2614 arm_patch (pdata->code, (guchar*)thunks);
2615 mono_arch_flush_icache (pdata->code, 4);
2618 } else if ((thunks [0] == 0) && (thunks [1] == 0) && (thunks [2] == 0)) {
2619 /* found a free slot instead: emit thunk */
2620 /* ARMREG_IP is fine to use since this can't be an IMT call
2623 code = (guchar*)thunks;
2624 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
2625 if (thumb_supported)
2626 ARM_BX (code, ARMREG_IP);
2628 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
2629 thunks [2] = (guint32)pdata->target;
2630 mono_arch_flush_icache ((guchar*)thunks, 12);
2632 arm_patch (pdata->code, (guchar*)thunks);
2633 mono_arch_flush_icache (pdata->code, 4);
2637 /* skip 12 bytes, the size of the thunk */
2641 //g_print ("failed thunk lookup for %p from %p at %p (%d entries)\n", pdata->target, pdata->code, data, count);
2647 handle_thunk (MonoDomain *domain, int absolute, guchar *code, const guchar *target)
2652 domain = mono_domain_get ();
2655 pdata.target = target;
2656 pdata.absolute = absolute;
2659 mono_domain_lock (domain);
2660 mono_domain_code_foreach (domain, search_thunk_slot, &pdata);
2663 /* this uses the first available slot */
2665 mono_domain_code_foreach (domain, search_thunk_slot, &pdata);
2667 mono_domain_unlock (domain);
2669 if (pdata.found != 1)
2670 g_print ("thunk failed for %p from %p\n", target, code);
2671 g_assert (pdata.found == 1);
2675 arm_patch_general (MonoDomain *domain, guchar *code, const guchar *target)
2677 guint32 *code32 = (void*)code;
2678 guint32 ins = *code32;
2679 guint32 prim = (ins >> 25) & 7;
2680 guint32 tval = GPOINTER_TO_UINT (target);
2682 //g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
2683 if (prim == 5) { /* 101b */
2684 /* the diff starts 8 bytes from the branch opcode */
2685 gint diff = target - code - 8;
2687 gint tmask = 0xffffffff;
2688 if (tval & 1) { /* entering thumb mode */
2689 diff = target - 1 - code - 8;
2690 g_assert (thumb_supported);
2691 tbits = 0xf << 28; /* bl->blx bit pattern */
2692 g_assert ((ins & (1 << 24))); /* it must be a bl, not b instruction */
2693 /* this low bit of the displacement is moved to bit 24 in the instruction encoding */
2697 tmask = ~(1 << 24); /* clear the link bit */
2698 /*g_print ("blx to thumb: target: %p, code: %p, diff: %d, mask: %x\n", target, code, diff, tmask);*/
2703 if (diff <= 33554431) {
2705 ins = (ins & 0xff000000) | diff;
2707 *code32 = ins | tbits;
2711 /* diff between 0 and -33554432 */
2712 if (diff >= -33554432) {
2714 ins = (ins & 0xff000000) | (diff & ~0xff000000);
2716 *code32 = ins | tbits;
2721 handle_thunk (domain, TRUE, code, target);
2726 * The alternative call sequences looks like this:
2728 * ldr ip, [pc] // loads the address constant
2729 * b 1f // jumps around the constant
2730 * address constant embedded in the code
2735 * There are two cases for patching:
2736 * a) at the end of method emission: in this case code points to the start
2737 * of the call sequence
2738 * b) during runtime patching of the call site: in this case code points
2739 * to the mov pc, ip instruction
2741 * We have to handle also the thunk jump code sequence:
2745 * address constant // execution never reaches here
2747 if ((ins & 0x0ffffff0) == 0x12fff10) {
2748 /* Branch and exchange: the address is constructed in a reg
2749 * We can patch BX when the code sequence is the following:
2750 * ldr ip, [pc, #0] ; 0x8
2757 guint8 *emit = (guint8*)ccode;
2758 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
2760 ARM_MOV_REG_REG (emit, ARMREG_LR, ARMREG_PC);
2761 ARM_BX (emit, ARMREG_IP);
2763 /*patching from magic trampoline*/
2764 if (ins == ccode [3]) {
2765 g_assert (code32 [-4] == ccode [0]);
2766 g_assert (code32 [-3] == ccode [1]);
2767 g_assert (code32 [-1] == ccode [2]);
2768 code32 [-2] = (guint32)target;
2771 /*patching from JIT*/
2772 if (ins == ccode [0]) {
2773 g_assert (code32 [1] == ccode [1]);
2774 g_assert (code32 [3] == ccode [2]);
2775 g_assert (code32 [4] == ccode [3]);
2776 code32 [2] = (guint32)target;
2779 g_assert_not_reached ();
2780 } else if ((ins & 0x0ffffff0) == 0x12fff30) {
2788 guint8 *emit = (guint8*)ccode;
2789 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
2791 ARM_BLX_REG (emit, ARMREG_IP);
2793 g_assert (code32 [-3] == ccode [0]);
2794 g_assert (code32 [-2] == ccode [1]);
2795 g_assert (code32 [0] == ccode [2]);
2797 code32 [-1] = (guint32)target;
2800 guint32 *tmp = ccode;
2801 guint8 *emit = (guint8*)tmp;
2802 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
2803 ARM_MOV_REG_REG (emit, ARMREG_LR, ARMREG_PC);
2804 ARM_MOV_REG_REG (emit, ARMREG_PC, ARMREG_IP);
2805 ARM_BX (emit, ARMREG_IP);
2806 if (ins == ccode [2]) {
2807 g_assert_not_reached (); // should be -2 ...
2808 code32 [-1] = (guint32)target;
2811 if (ins == ccode [0]) {
2812 /* handles both thunk jump code and the far call sequence */
2813 code32 [2] = (guint32)target;
2816 g_assert_not_reached ();
2818 // g_print ("patched with 0x%08x\n", ins);
2822 arm_patch (guchar *code, const guchar *target)
2824 arm_patch_general (NULL, code, target);
2828 * Return the >= 0 uimm8 value if val can be represented with a byte + rotation
2829 * (with the rotation amount in *rot_amount. rot_amount is already adjusted
2830 * to be used with the emit macros.
2831 * Return -1 otherwise.
2834 mono_arm_is_rotated_imm8 (guint32 val, gint *rot_amount)
2837 for (i = 0; i < 31; i+= 2) {
2838 res = (val << (32 - i)) | (val >> i);
2841 *rot_amount = i? 32 - i: 0;
2848 * Emits in code a sequence of instructions that load the value 'val'
2849 * into the dreg register. Uses at most 4 instructions.
2852 mono_arm_emit_load_imm (guint8 *code, int dreg, guint32 val)
2854 int imm8, rot_amount;
2856 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
2857 /* skip the constant pool */
2863 if ((imm8 = mono_arm_is_rotated_imm8 (val, &rot_amount)) >= 0) {
2864 ARM_MOV_REG_IMM (code, dreg, imm8, rot_amount);
2865 } else if ((imm8 = mono_arm_is_rotated_imm8 (~val, &rot_amount)) >= 0) {
2866 ARM_MVN_REG_IMM (code, dreg, imm8, rot_amount);
2869 ARM_MOVW_REG_IMM (code, dreg, val & 0xffff);
2871 ARM_MOVT_REG_IMM (code, dreg, (val >> 16) & 0xffff);
2875 ARM_MOV_REG_IMM8 (code, dreg, (val & 0xFF));
2877 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF00) >> 8, 24);
2879 if (val & 0xFF0000) {
2880 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
2882 if (val & 0xFF000000) {
2883 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
2885 } else if (val & 0xFF00) {
2886 ARM_MOV_REG_IMM (code, dreg, (val & 0xFF00) >> 8, 24);
2887 if (val & 0xFF0000) {
2888 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
2890 if (val & 0xFF000000) {
2891 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
2893 } else if (val & 0xFF0000) {
2894 ARM_MOV_REG_IMM (code, dreg, (val & 0xFF0000) >> 16, 16);
2895 if (val & 0xFF000000) {
2896 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
2899 //g_assert_not_reached ();
2905 mono_arm_thumb_supported (void)
2907 return thumb_supported;
2913 * emit_load_volatile_arguments:
2915 * Load volatile arguments from the stack to the original input registers.
2916 * Required before a tail call.
2919 emit_load_volatile_arguments (MonoCompile *cfg, guint8 *code)
2921 MonoMethod *method = cfg->method;
2922 MonoMethodSignature *sig;
2927 /* FIXME: Generate intermediate code instead */
2929 sig = mono_method_signature (method);
2931 /* This is the opposite of the code in emit_prolog */
2935 cinfo = get_call_info (cfg->generic_sharing_context, NULL, sig, sig->pinvoke);
2937 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
2938 ArgInfo *ainfo = &cinfo->ret;
2939 inst = cfg->vret_addr;
2940 g_assert (arm_is_imm12 (inst->inst_offset));
2941 ARM_LDR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
2943 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2944 ArgInfo *ainfo = cinfo->args + i;
2945 inst = cfg->args [pos];
2947 if (cfg->verbose_level > 2)
2948 g_print ("Loading argument %d (type: %d)\n", i, ainfo->storage);
2949 if (inst->opcode == OP_REGVAR) {
2950 if (ainfo->storage == RegTypeGeneral)
2951 ARM_MOV_REG_REG (code, inst->dreg, ainfo->reg);
2952 else if (ainfo->storage == RegTypeFP) {
2953 g_assert_not_reached ();
2954 } else if (ainfo->storage == RegTypeBase) {
2958 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
2959 ARM_LDR_IMM (code, inst->dreg, ARMREG_SP, (prev_sp_offset + ainfo->offset));
2961 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
2962 ARM_LDR_REG_REG (code, inst->dreg, ARMREG_SP, ARMREG_IP);
2966 g_assert_not_reached ();
2968 if (ainfo->storage == RegTypeGeneral || ainfo->storage == RegTypeIRegPair) {
2969 switch (ainfo->size) {
2976 g_assert (arm_is_imm12 (inst->inst_offset));
2977 ARM_LDR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
2978 g_assert (arm_is_imm12 (inst->inst_offset + 4));
2979 ARM_LDR_IMM (code, ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
2982 if (arm_is_imm12 (inst->inst_offset)) {
2983 ARM_LDR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
2985 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
2986 ARM_LDR_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
2990 } else if (ainfo->storage == RegTypeBaseGen) {
2993 } else if (ainfo->storage == RegTypeBase) {
2995 } else if (ainfo->storage == RegTypeFP) {
2996 g_assert_not_reached ();
2997 } else if (ainfo->storage == RegTypeStructByVal) {
2998 int doffset = inst->inst_offset;
3002 if (mono_class_from_mono_type (inst->inst_vtype))
3003 size = mono_class_native_size (mono_class_from_mono_type (inst->inst_vtype), NULL);
3004 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
3005 if (arm_is_imm12 (doffset)) {
3006 ARM_LDR_IMM (code, ainfo->reg + cur_reg, inst->inst_basereg, doffset);
3008 code = mono_arm_emit_load_imm (code, ARMREG_IP, doffset);
3009 ARM_LDR_REG_REG (code, ainfo->reg + cur_reg, inst->inst_basereg, ARMREG_IP);
3011 soffset += sizeof (gpointer);
3012 doffset += sizeof (gpointer);
3017 } else if (ainfo->storage == RegTypeStructByAddr) {
3032 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
3037 guint8 *code = cfg->native_code + cfg->code_len;
3038 MonoInst *last_ins = NULL;
3039 guint last_offset = 0;
3041 int imm8, rot_amount;
3043 /* we don't align basic blocks of loops on arm */
3045 if (cfg->verbose_level > 2)
3046 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
3048 cpos = bb->max_offset;
3050 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
3051 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
3052 //g_assert (!mono_compile_aot);
3055 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
3056 /* this is not thread save, but good enough */
3057 /* fixme: howto handle overflows? */
3058 //x86_inc_mem (code, &cov->data [bb->dfn].count);
3061 if (mono_break_at_bb_method && mono_method_desc_full_match (mono_break_at_bb_method, cfg->method) && bb->block_num == mono_break_at_bb_bb_num) {
3062 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3063 (gpointer)"mono_break");
3064 code = emit_call_seq (cfg, code);
3067 MONO_BB_FOR_EACH_INS (bb, ins) {
3068 offset = code - cfg->native_code;
3070 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
3072 if (offset > (cfg->code_size - max_len - 16)) {
3073 cfg->code_size *= 2;
3074 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3075 code = cfg->native_code + offset;
3077 // if (ins->cil_code)
3078 // g_print ("cil code\n");
3079 mono_debug_record_line_number (cfg, ins, offset);
3081 switch (ins->opcode) {
3082 case OP_MEMORY_BARRIER:
3085 #ifdef HAVE_AEABI_READ_TP
3086 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3087 (gpointer)"__aeabi_read_tp");
3088 code = emit_call_seq (cfg, code);
3090 ARM_LDR_IMM (code, ins->dreg, ARMREG_R0, ins->inst_offset);
3092 g_assert_not_reached ();
3096 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
3097 ppc_mulhw (code, ppc_r3, ins->sreg1, ins->sreg2);
3100 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
3101 ppc_mulhwu (code, ppc_r3, ins->sreg1, ins->sreg2);
3103 case OP_STOREI1_MEMBASE_IMM:
3104 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFF);
3105 g_assert (arm_is_imm12 (ins->inst_offset));
3106 ARM_STRB_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
3108 case OP_STOREI2_MEMBASE_IMM:
3109 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFFFF);
3110 g_assert (arm_is_imm8 (ins->inst_offset));
3111 ARM_STRH_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
3113 case OP_STORE_MEMBASE_IMM:
3114 case OP_STOREI4_MEMBASE_IMM:
3115 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm);
3116 g_assert (arm_is_imm12 (ins->inst_offset));
3117 ARM_STR_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
3119 case OP_STOREI1_MEMBASE_REG:
3120 g_assert (arm_is_imm12 (ins->inst_offset));
3121 ARM_STRB_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3123 case OP_STOREI2_MEMBASE_REG:
3124 g_assert (arm_is_imm8 (ins->inst_offset));
3125 ARM_STRH_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3127 case OP_STORE_MEMBASE_REG:
3128 case OP_STOREI4_MEMBASE_REG:
3129 /* this case is special, since it happens for spill code after lowering has been called */
3130 if (arm_is_imm12 (ins->inst_offset)) {
3131 ARM_STR_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3133 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
3134 ARM_STR_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ARMREG_LR);
3137 case OP_STOREI1_MEMINDEX:
3138 ARM_STRB_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
3140 case OP_STOREI2_MEMINDEX:
3141 ARM_STRH_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
3143 case OP_STORE_MEMINDEX:
3144 case OP_STOREI4_MEMINDEX:
3145 ARM_STR_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
3148 g_assert_not_reached ();
3150 case OP_LOAD_MEMINDEX:
3151 case OP_LOADI4_MEMINDEX:
3152 case OP_LOADU4_MEMINDEX:
3153 ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3155 case OP_LOADI1_MEMINDEX:
3156 ARM_LDRSB_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3158 case OP_LOADU1_MEMINDEX:
3159 ARM_LDRB_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3161 case OP_LOADI2_MEMINDEX:
3162 ARM_LDRSH_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3164 case OP_LOADU2_MEMINDEX:
3165 ARM_LDRH_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3167 case OP_LOAD_MEMBASE:
3168 case OP_LOADI4_MEMBASE:
3169 case OP_LOADU4_MEMBASE:
3170 /* this case is special, since it happens for spill code after lowering has been called */
3171 if (arm_is_imm12 (ins->inst_offset)) {
3172 ARM_LDR_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3174 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
3175 ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ARMREG_LR);
3178 case OP_LOADI1_MEMBASE:
3179 g_assert (arm_is_imm8 (ins->inst_offset));
3180 ARM_LDRSB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3182 case OP_LOADU1_MEMBASE:
3183 g_assert (arm_is_imm12 (ins->inst_offset));
3184 ARM_LDRB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3186 case OP_LOADU2_MEMBASE:
3187 g_assert (arm_is_imm8 (ins->inst_offset));
3188 ARM_LDRH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3190 case OP_LOADI2_MEMBASE:
3191 g_assert (arm_is_imm8 (ins->inst_offset));
3192 ARM_LDRSH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3194 case OP_ICONV_TO_I1:
3195 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 24);
3196 ARM_SAR_IMM (code, ins->dreg, ins->dreg, 24);
3198 case OP_ICONV_TO_I2:
3199 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
3200 ARM_SAR_IMM (code, ins->dreg, ins->dreg, 16);
3202 case OP_ICONV_TO_U1:
3203 ARM_AND_REG_IMM8 (code, ins->dreg, ins->sreg1, 0xff);
3205 case OP_ICONV_TO_U2:
3206 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
3207 ARM_SHR_IMM (code, ins->dreg, ins->dreg, 16);
3211 ARM_CMP_REG_REG (code, ins->sreg1, ins->sreg2);
3213 case OP_COMPARE_IMM:
3214 case OP_ICOMPARE_IMM:
3215 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3216 g_assert (imm8 >= 0);
3217 ARM_CMP_REG_IMM (code, ins->sreg1, imm8, rot_amount);
3221 * gdb does not like encountering the hw breakpoint ins in the debugged code.
3222 * So instead of emitting a trap, we emit a call a C function and place a
3225 //*(int*)code = 0xef9f0001;
3228 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3229 (gpointer)"mono_break");
3230 code = emit_call_seq (cfg, code);
3232 case OP_RELAXED_NOP:
3237 case OP_DUMMY_STORE:
3238 case OP_NOT_REACHED:
3241 case OP_SEQ_POINT: {
3243 MonoInst *info_var = cfg->arch.seq_point_info_var;
3244 MonoInst *ss_trigger_page_var = cfg->arch.ss_trigger_page_var;
3246 int dreg = ARMREG_LR;
3249 * For AOT, we use one got slot per method, which will point to a
3250 * SeqPointInfo structure, containing all the information required
3251 * by the code below.
3253 if (cfg->compile_aot) {
3254 g_assert (info_var);
3255 g_assert (info_var->opcode == OP_REGOFFSET);
3256 g_assert (arm_is_imm12 (info_var->inst_offset));
3260 * Read from the single stepping trigger page. This will cause a
3261 * SIGSEGV when single stepping is enabled.
3262 * We do this _before_ the breakpoint, so single stepping after
3263 * a breakpoint is hit will step to the next IL offset.
3265 g_assert (((guint64)(gsize)ss_trigger_page >> 32) == 0);
3267 if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
3268 if (cfg->compile_aot) {
3269 /* Load the trigger page addr from the variable initialized in the prolog */
3270 var = ss_trigger_page_var;
3272 g_assert (var->opcode == OP_REGOFFSET);
3273 g_assert (arm_is_imm12 (var->inst_offset));
3274 ARM_LDR_IMM (code, dreg, var->inst_basereg, var->inst_offset);
3276 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
3278 *(int*)code = (int)ss_trigger_page;
3281 ARM_LDR_IMM (code, dreg, dreg, 0);
3284 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
3286 if (cfg->compile_aot) {
3287 guint32 offset = code - cfg->native_code;
3290 ARM_LDR_IMM (code, dreg, info_var->inst_basereg, info_var->inst_offset);
3291 /* Add the offset */
3292 val = ((offset / 4) * sizeof (guint8*)) + G_STRUCT_OFFSET (SeqPointInfo, bp_addrs);
3293 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF), 0);
3295 * Have to emit nops to keep the difference between the offset
3296 * stored in seq_points and breakpoint instruction constant,
3297 * mono_arch_get_ip_for_breakpoint () depends on this.
3300 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF00) >> 8, 24);
3304 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
3307 g_assert (!(val & 0xFF000000));
3308 /* Load the info->bp_addrs [offset], which is either 0 or the address of a trigger page */
3309 ARM_LDR_IMM (code, dreg, dreg, 0);
3311 /* What is faster, a branch or a load ? */
3312 ARM_CMP_REG_IMM (code, dreg, 0, 0);
3313 /* The breakpoint instruction */
3314 ARM_LDR_IMM_COND (code, dreg, dreg, 0, ARMCOND_NE);
3317 * A placeholder for a possible breakpoint inserted by
3318 * mono_arch_set_breakpoint ().
3320 for (i = 0; i < 4; ++i)
3327 ARM_ADDS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3330 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3334 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3337 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3338 g_assert (imm8 >= 0);
3339 ARM_ADDS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3343 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3344 g_assert (imm8 >= 0);
3345 ARM_ADD_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3349 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3350 g_assert (imm8 >= 0);
3351 ARM_ADCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3354 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3355 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3357 case OP_IADD_OVF_UN:
3358 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3359 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3362 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3363 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3365 case OP_ISUB_OVF_UN:
3366 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3367 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
3369 case OP_ADD_OVF_CARRY:
3370 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3371 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3373 case OP_ADD_OVF_UN_CARRY:
3374 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3375 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3377 case OP_SUB_OVF_CARRY:
3378 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3379 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3381 case OP_SUB_OVF_UN_CARRY:
3382 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3383 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
3387 ARM_SUBS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3390 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3391 g_assert (imm8 >= 0);
3392 ARM_SUBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3395 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3399 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3403 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3404 g_assert (imm8 >= 0);
3405 ARM_SUB_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3409 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3410 g_assert (imm8 >= 0);
3411 ARM_SBCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3413 case OP_ARM_RSBS_IMM:
3414 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3415 g_assert (imm8 >= 0);
3416 ARM_RSBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3418 case OP_ARM_RSC_IMM:
3419 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3420 g_assert (imm8 >= 0);
3421 ARM_RSC_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3424 ARM_AND_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3428 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3429 g_assert (imm8 >= 0);
3430 ARM_AND_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3438 /* crappy ARM arch doesn't have a DIV instruction */
3439 g_assert_not_reached ();
3441 ARM_ORR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3445 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3446 g_assert (imm8 >= 0);
3447 ARM_ORR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3450 ARM_EOR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3454 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3455 g_assert (imm8 >= 0);
3456 ARM_EOR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3459 ARM_SHL_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3464 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
3465 else if (ins->dreg != ins->sreg1)
3466 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
3469 ARM_SAR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3474 ARM_SAR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
3475 else if (ins->dreg != ins->sreg1)
3476 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
3479 case OP_ISHR_UN_IMM:
3481 ARM_SHR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
3482 else if (ins->dreg != ins->sreg1)
3483 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
3486 ARM_SHR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3489 ARM_MVN_REG_REG (code, ins->dreg, ins->sreg1);
3492 ARM_RSB_REG_IMM8 (code, ins->dreg, ins->sreg1, 0);
3495 if (ins->dreg == ins->sreg2)
3496 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3498 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg2, ins->sreg1);
3501 g_assert_not_reached ();
3504 /* FIXME: handle ovf/ sreg2 != dreg */
3505 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3506 /* FIXME: MUL doesn't set the C/O flags on ARM */
3508 case OP_IMUL_OVF_UN:
3509 /* FIXME: handle ovf/ sreg2 != dreg */
3510 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3511 /* FIXME: MUL doesn't set the C/O flags on ARM */
3514 code = mono_arm_emit_load_imm (code, ins->dreg, ins->inst_c0);
3517 /* Load the GOT offset */
3518 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3519 ARM_LDR_IMM (code, ins->dreg, ARMREG_PC, 0);
3521 *(gpointer*)code = NULL;
3523 /* Load the value from the GOT */
3524 ARM_LDR_REG_REG (code, ins->dreg, ARMREG_PC, ins->dreg);
3526 case OP_ICONV_TO_I4:
3527 case OP_ICONV_TO_U4:
3529 if (ins->dreg != ins->sreg1)
3530 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
3533 int saved = ins->sreg2;
3534 if (ins->sreg2 == ARM_LSW_REG) {
3535 ARM_MOV_REG_REG (code, ARMREG_LR, ins->sreg2);
3538 if (ins->sreg1 != ARM_LSW_REG)
3539 ARM_MOV_REG_REG (code, ARM_LSW_REG, ins->sreg1);
3540 if (saved != ARM_MSW_REG)
3541 ARM_MOV_REG_REG (code, ARM_MSW_REG, saved);
3546 ARM_MVFD (code, ins->dreg, ins->sreg1);
3547 #elif defined(ARM_FPU_VFP)
3548 ARM_CPYD (code, ins->dreg, ins->sreg1);
3551 case OP_FCONV_TO_R4:
3553 ARM_MVFS (code, ins->dreg, ins->sreg1);
3554 #elif defined(ARM_FPU_VFP)
3555 ARM_CVTD (code, ins->dreg, ins->sreg1);
3556 ARM_CVTS (code, ins->dreg, ins->dreg);
3561 * Keep in sync with mono_arch_emit_epilog
3563 g_assert (!cfg->method->save_lmf);
3565 code = emit_load_volatile_arguments (cfg, code);
3567 code = emit_big_add (code, ARMREG_SP, cfg->frame_reg, cfg->stack_usage);
3568 ARM_POP_NWB (code, cfg->used_int_regs | ((1 << ARMREG_SP)) | ((1 << ARMREG_LR)));
3569 mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
3570 if (cfg->compile_aot) {
3571 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
3573 *(gpointer*)code = NULL;
3575 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
3581 /* ensure ins->sreg1 is not NULL */
3582 ARM_LDR_IMM (code, ARMREG_LR, ins->sreg1, 0);
3585 g_assert (cfg->sig_cookie < 128);
3586 ARM_LDR_IMM (code, ARMREG_IP, cfg->frame_reg, cfg->sig_cookie);
3587 ARM_STR_IMM (code, ARMREG_IP, ins->sreg1, 0);
3596 call = (MonoCallInst*)ins;
3597 if (ins->flags & MONO_INST_HAS_METHOD)
3598 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
3600 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
3601 code = emit_call_seq (cfg, code);
3602 code = emit_move_return_value (cfg, ins, code);
3608 case OP_VOIDCALL_REG:
3610 code = emit_call_reg (code, ins->sreg1);
3611 code = emit_move_return_value (cfg, ins, code);
3613 case OP_FCALL_MEMBASE:
3614 case OP_LCALL_MEMBASE:
3615 case OP_VCALL_MEMBASE:
3616 case OP_VCALL2_MEMBASE:
3617 case OP_VOIDCALL_MEMBASE:
3618 case OP_CALL_MEMBASE:
3619 g_assert (arm_is_imm12 (ins->inst_offset));
3620 g_assert (ins->sreg1 != ARMREG_LR);
3621 call = (MonoCallInst*)ins;
3622 if (call->dynamic_imt_arg || call->method->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
3623 ARM_ADD_REG_IMM8 (code, ARMREG_LR, ARMREG_PC, 4);
3624 ARM_LDR_IMM (code, ARMREG_PC, ins->sreg1, ins->inst_offset);
3626 * We can't embed the method in the code stream in PIC code, or
3628 * Instead, we put it in V5 in code emitted by
3629 * mono_arch_emit_imt_argument (), and embed NULL here to
3630 * signal the IMT thunk that the value is in V5.
3632 if (call->dynamic_imt_arg)
3633 *((gpointer*)code) = NULL;
3635 *((gpointer*)code) = (gpointer)call->method;
3638 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
3639 ARM_LDR_IMM (code, ARMREG_PC, ins->sreg1, ins->inst_offset);
3641 code = emit_move_return_value (cfg, ins, code);
3644 /* keep alignment */
3645 int alloca_waste = cfg->param_area;
3648 /* round the size to 8 bytes */
3649 ARM_ADD_REG_IMM8 (code, ins->dreg, ins->sreg1, 7);
3650 ARM_BIC_REG_IMM8 (code, ins->dreg, ins->dreg, 7);
3652 ARM_ADD_REG_IMM8 (code, ins->dreg, ins->dreg, alloca_waste);
3653 ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ins->dreg);
3654 /* memzero the area: dreg holds the size, sp is the pointer */
3655 if (ins->flags & MONO_INST_INIT) {
3656 guint8 *start_loop, *branch_to_cond;
3657 ARM_MOV_REG_IMM8 (code, ARMREG_LR, 0);
3658 branch_to_cond = code;
3661 ARM_STR_REG_REG (code, ARMREG_LR, ARMREG_SP, ins->dreg);
3662 arm_patch (branch_to_cond, code);
3663 /* decrement by 4 and set flags */
3664 ARM_SUBS_REG_IMM8 (code, ins->dreg, ins->dreg, 4);
3665 ARM_B_COND (code, ARMCOND_GE, 0);
3666 arm_patch (code - 4, start_loop);
3668 ARM_ADD_REG_IMM8 (code, ins->dreg, ARMREG_SP, alloca_waste);
3673 MonoInst *var = cfg->dyn_call_var;
3675 g_assert (var->opcode == OP_REGOFFSET);
3676 g_assert (arm_is_imm12 (var->inst_offset));
3678 /* lr = args buffer filled by mono_arch_get_dyn_call_args () */
3679 ARM_MOV_REG_REG( code, ARMREG_LR, ins->sreg1);
3681 ARM_MOV_REG_REG( code, ARMREG_IP, ins->sreg2);
3683 /* Save args buffer */
3684 ARM_STR_IMM (code, ARMREG_LR, var->inst_basereg, var->inst_offset);
3686 /* Set stack slots using R0 as scratch reg */
3687 /* MONO_ARCH_DYN_CALL_PARAM_AREA gives the size of stack space available */
3688 for (i = 0; i < DYN_CALL_STACK_ARGS; ++i) {
3689 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_LR, (PARAM_REGS + i) * sizeof (gpointer));
3690 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, i * sizeof (gpointer));
3693 /* Set argument registers */
3694 for (i = 0; i < PARAM_REGS; ++i)
3695 ARM_LDR_IMM (code, i, ARMREG_LR, i * sizeof (gpointer));
3698 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
3699 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
3702 ARM_LDR_IMM (code, ARMREG_IP, var->inst_basereg, var->inst_offset);
3703 ARM_STR_IMM (code, ARMREG_R0, ARMREG_IP, G_STRUCT_OFFSET (DynCallArgs, res));
3704 ARM_STR_IMM (code, ARMREG_R1, ARMREG_IP, G_STRUCT_OFFSET (DynCallArgs, res2));
3708 if (ins->sreg1 != ARMREG_R0)
3709 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
3710 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3711 (gpointer)"mono_arch_throw_exception");
3712 code = emit_call_seq (cfg, code);
3716 if (ins->sreg1 != ARMREG_R0)
3717 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
3718 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3719 (gpointer)"mono_arch_rethrow_exception");
3720 code = emit_call_seq (cfg, code);
3723 case OP_START_HANDLER: {
3724 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3726 if (arm_is_imm12 (spvar->inst_offset)) {
3727 ARM_STR_IMM (code, ARMREG_LR, spvar->inst_basereg, spvar->inst_offset);
3729 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
3730 ARM_STR_REG_REG (code, ARMREG_LR, spvar->inst_basereg, ARMREG_IP);
3734 case OP_ENDFILTER: {
3735 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3737 if (ins->sreg1 != ARMREG_R0)
3738 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
3739 if (arm_is_imm12 (spvar->inst_offset)) {
3740 ARM_LDR_IMM (code, ARMREG_IP, spvar->inst_basereg, spvar->inst_offset);
3742 g_assert (ARMREG_IP != spvar->inst_basereg);
3743 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
3744 ARM_LDR_REG_REG (code, ARMREG_IP, spvar->inst_basereg, ARMREG_IP);
3746 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
3749 case OP_ENDFINALLY: {
3750 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3752 if (arm_is_imm12 (spvar->inst_offset)) {
3753 ARM_LDR_IMM (code, ARMREG_IP, spvar->inst_basereg, spvar->inst_offset);
3755 g_assert (ARMREG_IP != spvar->inst_basereg);
3756 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
3757 ARM_LDR_REG_REG (code, ARMREG_IP, spvar->inst_basereg, ARMREG_IP);
3759 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
3762 case OP_CALL_HANDLER:
3763 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3765 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
3768 ins->inst_c0 = code - cfg->native_code;
3771 /*if (ins->inst_target_bb->native_offset) {
3773 //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
3775 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3780 ARM_MOV_REG_REG (code, ARMREG_PC, ins->sreg1);
3784 * In the normal case we have:
3785 * ldr pc, [pc, ins->sreg1 << 2]
3788 * ldr lr, [pc, ins->sreg1 << 2]
3790 * After follows the data.
3791 * FIXME: add aot support.
3793 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_SWITCH, ins->inst_p0);
3794 max_len += 4 * GPOINTER_TO_INT (ins->klass);
3795 if (offset + max_len > (cfg->code_size - 16)) {
3796 cfg->code_size += max_len;
3797 cfg->code_size *= 2;
3798 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3799 code = cfg->native_code + offset;
3801 ARM_LDR_REG_REG_SHIFT (code, ARMREG_PC, ARMREG_PC, ins->sreg1, ARMSHIFT_LSL, 2);
3803 code += 4 * GPOINTER_TO_INT (ins->klass);
3807 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
3808 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
3812 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
3813 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LT);
3817 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
3818 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LO);
3822 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
3823 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_GT);
3827 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
3828 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_HI);
3830 case OP_COND_EXC_EQ:
3831 case OP_COND_EXC_NE_UN:
3832 case OP_COND_EXC_LT:
3833 case OP_COND_EXC_LT_UN:
3834 case OP_COND_EXC_GT:
3835 case OP_COND_EXC_GT_UN:
3836 case OP_COND_EXC_GE:
3837 case OP_COND_EXC_GE_UN:
3838 case OP_COND_EXC_LE:
3839 case OP_COND_EXC_LE_UN:
3840 EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_EQ, ins->inst_p1);
3842 case OP_COND_EXC_IEQ:
3843 case OP_COND_EXC_INE_UN:
3844 case OP_COND_EXC_ILT:
3845 case OP_COND_EXC_ILT_UN:
3846 case OP_COND_EXC_IGT:
3847 case OP_COND_EXC_IGT_UN:
3848 case OP_COND_EXC_IGE:
3849 case OP_COND_EXC_IGE_UN:
3850 case OP_COND_EXC_ILE:
3851 case OP_COND_EXC_ILE_UN:
3852 EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_IEQ, ins->inst_p1);
3855 case OP_COND_EXC_IC:
3856 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_CS, ins->inst_p1);
3858 case OP_COND_EXC_OV:
3859 case OP_COND_EXC_IOV:
3860 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VS, ins->inst_p1);
3862 case OP_COND_EXC_NC:
3863 case OP_COND_EXC_INC:
3864 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_CC, ins->inst_p1);
3866 case OP_COND_EXC_NO:
3867 case OP_COND_EXC_INO:
3868 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VC, ins->inst_p1);
3880 EMIT_COND_BRANCH (ins, ins->opcode - OP_IBEQ);
3883 /* floating point opcodes */
3886 if (cfg->compile_aot) {
3887 ARM_LDFD (code, ins->dreg, ARMREG_PC, 0);
3889 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
3891 *(guint32*)code = ((guint32*)(ins->inst_p0))[1];
3894 /* FIXME: we can optimize the imm load by dealing with part of
3895 * the displacement in LDFD (aligning to 512).
3897 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
3898 ARM_LDFD (code, ins->dreg, ARMREG_LR, 0);
3902 if (cfg->compile_aot) {
3903 ARM_LDFS (code, ins->dreg, ARMREG_PC, 0);
3905 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
3908 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
3909 ARM_LDFS (code, ins->dreg, ARMREG_LR, 0);
3912 case OP_STORER8_MEMBASE_REG:
3913 /* This is generated by the local regalloc pass which runs after the lowering pass */
3914 if (!arm_is_fpimm8 (ins->inst_offset)) {
3915 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
3916 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_destbasereg);
3917 ARM_STFD (code, ins->sreg1, ARMREG_LR, 0);
3919 ARM_STFD (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3922 case OP_LOADR8_MEMBASE:
3923 /* This is generated by the local regalloc pass which runs after the lowering pass */
3924 if (!arm_is_fpimm8 (ins->inst_offset)) {
3925 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
3926 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_basereg);
3927 ARM_LDFD (code, ins->dreg, ARMREG_LR, 0);
3929 ARM_LDFD (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3932 case OP_STORER4_MEMBASE_REG:
3933 g_assert (arm_is_fpimm8 (ins->inst_offset));
3934 ARM_STFS (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3936 case OP_LOADR4_MEMBASE:
3937 g_assert (arm_is_fpimm8 (ins->inst_offset));
3938 ARM_LDFS (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3940 case OP_ICONV_TO_R_UN: {
3942 tmpreg = ins->dreg == 0? 1: 0;
3943 ARM_CMP_REG_IMM8 (code, ins->sreg1, 0);
3944 ARM_FLTD (code, ins->dreg, ins->sreg1);
3945 ARM_B_COND (code, ARMCOND_GE, 8);
3946 /* save the temp register */
3947 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
3948 ARM_STFD (code, tmpreg, ARMREG_SP, 0);
3949 ARM_LDFD (code, tmpreg, ARMREG_PC, 12);
3950 ARM_FPA_ADFD (code, ins->dreg, ins->dreg, tmpreg);
3951 ARM_LDFD (code, tmpreg, ARMREG_SP, 0);
3952 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
3953 /* skip the constant pool */
3956 *(int*)code = 0x41f00000;
3961 * ldfltd ftemp, [pc, #8] 0x41f00000 0x00000000
3962 * adfltd fdest, fdest, ftemp
3966 case OP_ICONV_TO_R4:
3967 ARM_FLTS (code, ins->dreg, ins->sreg1);
3969 case OP_ICONV_TO_R8:
3970 ARM_FLTD (code, ins->dreg, ins->sreg1);
3973 #elif defined(ARM_FPU_VFP)
3976 if (cfg->compile_aot) {
3977 ARM_FLDD (code, ins->dreg, ARMREG_PC, 0);
3979 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
3981 *(guint32*)code = ((guint32*)(ins->inst_p0))[1];
3984 /* FIXME: we can optimize the imm load by dealing with part of
3985 * the displacement in LDFD (aligning to 512).
3987 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
3988 ARM_FLDD (code, ins->dreg, ARMREG_LR, 0);
3992 if (cfg->compile_aot) {
3993 ARM_FLDS (code, ins->dreg, ARMREG_PC, 0);
3995 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
3997 ARM_CVTS (code, ins->dreg, ins->dreg);
3999 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
4000 ARM_FLDS (code, ins->dreg, ARMREG_LR, 0);
4001 ARM_CVTS (code, ins->dreg, ins->dreg);
4004 case OP_STORER8_MEMBASE_REG:
4005 /* This is generated by the local regalloc pass which runs after the lowering pass */
4006 if (!arm_is_fpimm8 (ins->inst_offset)) {
4007 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4008 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_destbasereg);
4009 ARM_FSTD (code, ins->sreg1, ARMREG_LR, 0);
4011 ARM_FSTD (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
4014 case OP_LOADR8_MEMBASE:
4015 /* This is generated by the local regalloc pass which runs after the lowering pass */
4016 if (!arm_is_fpimm8 (ins->inst_offset)) {
4017 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4018 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_basereg);
4019 ARM_FLDD (code, ins->dreg, ARMREG_LR, 0);
4021 ARM_FLDD (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4024 case OP_STORER4_MEMBASE_REG:
4025 g_assert (arm_is_fpimm8 (ins->inst_offset));
4026 ARM_CVTD (code, ARM_VFP_F0, ins->sreg1);
4027 ARM_FSTS (code, ARM_VFP_F0, ins->inst_destbasereg, ins->inst_offset);
4029 case OP_LOADR4_MEMBASE:
4030 g_assert (arm_is_fpimm8 (ins->inst_offset));
4031 ARM_FLDS (code, ARM_VFP_F0, ins->inst_basereg, ins->inst_offset);
4032 ARM_CVTS (code, ins->dreg, ARM_VFP_F0);
4034 case OP_ICONV_TO_R_UN: {
4035 g_assert_not_reached ();
4038 case OP_ICONV_TO_R4:
4039 ARM_FMSR (code, ARM_VFP_F0, ins->sreg1);
4040 ARM_FSITOS (code, ARM_VFP_F0, ARM_VFP_F0);
4041 ARM_CVTS (code, ins->dreg, ARM_VFP_F0);
4043 case OP_ICONV_TO_R8:
4044 ARM_FMSR (code, ARM_VFP_F0, ins->sreg1);
4045 ARM_FSITOD (code, ins->dreg, ARM_VFP_F0);
4049 if (mono_method_signature (cfg->method)->ret->type == MONO_TYPE_R4) {
4050 ARM_CVTD (code, ARM_VFP_F0, ins->sreg1);
4051 ARM_FMRS (code, ARMREG_R0, ARM_VFP_F0);
4053 ARM_FMRRD (code, ARMREG_R0, ARMREG_R1, ins->sreg1);
4059 case OP_FCONV_TO_I1:
4060 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
4062 case OP_FCONV_TO_U1:
4063 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
4065 case OP_FCONV_TO_I2:
4066 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
4068 case OP_FCONV_TO_U2:
4069 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
4071 case OP_FCONV_TO_I4:
4073 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
4075 case OP_FCONV_TO_U4:
4077 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
4079 case OP_FCONV_TO_I8:
4080 case OP_FCONV_TO_U8:
4081 g_assert_not_reached ();
4082 /* Implemented as helper calls */
4084 case OP_LCONV_TO_R_UN:
4085 g_assert_not_reached ();
4086 /* Implemented as helper calls */
4088 case OP_LCONV_TO_OVF_I4_2: {
4089 guint8 *high_bit_not_set, *valid_negative, *invalid_negative, *valid_positive;
4091 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
4094 ARM_CMP_REG_IMM8 (code, ins->sreg1, 0);
4095 high_bit_not_set = code;
4096 ARM_B_COND (code, ARMCOND_GE, 0); /*branch if bit 31 of the lower part is not set*/
4098 ARM_CMN_REG_IMM8 (code, ins->sreg2, 1); /*This have the same effect as CMP reg, 0xFFFFFFFF */
4099 valid_negative = code;
4100 ARM_B_COND (code, ARMCOND_EQ, 0); /*branch if upper part == 0xFFFFFFFF (lower part has bit 31 set) */
4101 invalid_negative = code;
4102 ARM_B_COND (code, ARMCOND_AL, 0);
4104 arm_patch (high_bit_not_set, code);
4106 ARM_CMP_REG_IMM8 (code, ins->sreg2, 0);
4107 valid_positive = code;
4108 ARM_B_COND (code, ARMCOND_EQ, 0); /*branch if upper part == 0 (lower part has bit 31 clear)*/
4110 arm_patch (invalid_negative, code);
4111 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_AL, "OverflowException");
4113 arm_patch (valid_negative, code);
4114 arm_patch (valid_positive, code);
4116 if (ins->dreg != ins->sreg1)
4117 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
4122 ARM_FPA_ADFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4125 ARM_FPA_SUFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4128 ARM_FPA_MUFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4131 ARM_FPA_DVFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4134 ARM_MNFD (code, ins->dreg, ins->sreg1);
4136 #elif defined(ARM_FPU_VFP)
4138 ARM_VFP_ADDD (code, ins->dreg, ins->sreg1, ins->sreg2);
4141 ARM_VFP_SUBD (code, ins->dreg, ins->sreg1, ins->sreg2);
4144 ARM_VFP_MULD (code, ins->dreg, ins->sreg1, ins->sreg2);
4147 ARM_VFP_DIVD (code, ins->dreg, ins->sreg1, ins->sreg2);
4150 ARM_NEGD (code, ins->dreg, ins->sreg1);
4155 g_assert_not_reached ();
4159 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4160 #elif defined(ARM_FPU_VFP)
4161 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4167 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4168 #elif defined(ARM_FPU_VFP)
4169 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4172 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
4173 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
4177 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4178 #elif defined(ARM_FPU_VFP)
4179 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4182 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4183 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4187 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4188 #elif defined(ARM_FPU_VFP)
4189 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4192 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4193 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4194 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
4199 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
4200 #elif defined(ARM_FPU_VFP)
4201 ARM_CMPD (code, ins->sreg2, ins->sreg1);
4204 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4205 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4210 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
4211 #elif defined(ARM_FPU_VFP)
4212 ARM_CMPD (code, ins->sreg2, ins->sreg1);
4215 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4216 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4217 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
4219 /* ARM FPA flags table:
4220 * N Less than ARMCOND_MI
4221 * Z Equal ARMCOND_EQ
4222 * C Greater Than or Equal ARMCOND_CS
4223 * V Unordered ARMCOND_VS
4226 EMIT_COND_BRANCH (ins, OP_IBEQ - OP_IBEQ);
4229 EMIT_COND_BRANCH (ins, OP_IBNE_UN - OP_IBEQ);
4232 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set */
4235 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
4236 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set */
4242 g_assert_not_reached ();
4246 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_GE);
4248 /* FPA requires EQ even thou the docs suggests that just CS is enough */
4249 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_EQ);
4250 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_CS);
4254 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
4255 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_GE);
4260 if (ins->dreg != ins->sreg1)
4261 ARM_MVFD (code, ins->dreg, ins->sreg1);
4262 #elif defined(ARM_FPU_VFP)
4263 ARM_ABSD (code, ARM_VFP_D1, ins->sreg1);
4264 ARM_FLDD (code, ARM_VFP_D0, ARMREG_PC, 0);
4266 *(guint32*)code = 0xffffffff;
4268 *(guint32*)code = 0x7fefffff;
4270 ARM_CMPD (code, ARM_VFP_D1, ARM_VFP_D0);
4272 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_GT, "ArithmeticException");
4273 ARM_CMPD (code, ins->sreg1, ins->sreg1);
4275 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VS, "ArithmeticException");
4277 ARM_CPYD (code, ins->dreg, ins->sreg1);
4282 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
4283 g_assert_not_reached ();
4286 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
4287 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
4288 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
4289 g_assert_not_reached ();
4295 last_offset = offset;
4298 cfg->code_len = code - cfg->native_code;
4301 #endif /* DISABLE_JIT */
4303 #ifdef HAVE_AEABI_READ_TP
4304 void __aeabi_read_tp (void);
4308 mono_arch_register_lowlevel_calls (void)
4310 /* The signature doesn't matter */
4311 mono_register_jit_icall (mono_arm_throw_exception, "mono_arm_throw_exception", mono_create_icall_signature ("void"), TRUE);
4312 mono_register_jit_icall (mono_arm_throw_exception_by_token, "mono_arm_throw_exception_by_token", mono_create_icall_signature ("void"), TRUE);
4314 #ifndef MONO_CROSS_COMPILE
4315 #ifdef HAVE_AEABI_READ_TP
4316 mono_register_jit_icall (__aeabi_read_tp, "__aeabi_read_tp", mono_create_icall_signature ("void"), TRUE);
4321 #define patch_lis_ori(ip,val) do {\
4322 guint16 *__lis_ori = (guint16*)(ip); \
4323 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff; \
4324 __lis_ori [3] = ((guint32)(val)) & 0xffff; \
4328 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
4330 MonoJumpInfo *patch_info;
4331 gboolean compile_aot = !run_cctors;
4333 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
4334 unsigned char *ip = patch_info->ip.i + code;
4335 const unsigned char *target;
4337 if (patch_info->type == MONO_PATCH_INFO_SWITCH && !compile_aot) {
4338 gpointer *jt = (gpointer*)(ip + 8);
4340 /* jt is the inlined jump table, 2 instructions after ip
4341 * In the normal case we store the absolute addresses,
4342 * otherwise the displacements.
4344 for (i = 0; i < patch_info->data.table->table_size; i++)
4345 jt [i] = code + (int)patch_info->data.table->table [i];
4348 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
4351 switch (patch_info->type) {
4352 case MONO_PATCH_INFO_BB:
4353 case MONO_PATCH_INFO_LABEL:
4356 /* No need to patch these */
4361 switch (patch_info->type) {
4362 case MONO_PATCH_INFO_IP:
4363 g_assert_not_reached ();
4364 patch_lis_ori (ip, ip);
4366 case MONO_PATCH_INFO_METHOD_REL:
4367 g_assert_not_reached ();
4368 *((gpointer *)(ip)) = code + patch_info->data.offset;
4370 case MONO_PATCH_INFO_METHODCONST:
4371 case MONO_PATCH_INFO_CLASS:
4372 case MONO_PATCH_INFO_IMAGE:
4373 case MONO_PATCH_INFO_FIELD:
4374 case MONO_PATCH_INFO_VTABLE:
4375 case MONO_PATCH_INFO_IID:
4376 case MONO_PATCH_INFO_SFLDA:
4377 case MONO_PATCH_INFO_LDSTR:
4378 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
4379 case MONO_PATCH_INFO_LDTOKEN:
4380 g_assert_not_reached ();
4381 /* from OP_AOTCONST : lis + ori */
4382 patch_lis_ori (ip, target);
4384 case MONO_PATCH_INFO_R4:
4385 case MONO_PATCH_INFO_R8:
4386 g_assert_not_reached ();
4387 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
4389 case MONO_PATCH_INFO_EXC_NAME:
4390 g_assert_not_reached ();
4391 *((gconstpointer *)(ip + 1)) = patch_info->data.name;
4393 case MONO_PATCH_INFO_NONE:
4394 case MONO_PATCH_INFO_BB_OVF:
4395 case MONO_PATCH_INFO_EXC_OVF:
4396 /* everything is dealt with at epilog output time */
4401 arm_patch_general (domain, ip, target);
4408 * Stack frame layout:
4410 * ------------------- fp
4411 * MonoLMF structure or saved registers
4412 * -------------------
4414 * -------------------
4416 * -------------------
4417 * optional 8 bytes for tracing
4418 * -------------------
4419 * param area size is cfg->param_area
4420 * ------------------- sp
4423 mono_arch_emit_prolog (MonoCompile *cfg)
4425 MonoMethod *method = cfg->method;
4427 MonoMethodSignature *sig;
4429 int alloc_size, pos, max_offset, i, rot_amount;
4434 int prev_sp_offset, reg_offset;
4436 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4439 sig = mono_method_signature (method);
4440 cfg->code_size = 256 + sig->param_count * 20;
4441 code = cfg->native_code = g_malloc (cfg->code_size);
4443 mono_emit_unwind_op_def_cfa (cfg, code, ARMREG_SP, 0);
4445 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
4447 alloc_size = cfg->stack_offset;
4450 if (!method->save_lmf) {
4451 /* We save SP by storing it into IP and saving IP */
4452 ARM_PUSH (code, (cfg->used_int_regs | (1 << ARMREG_IP) | (1 << ARMREG_LR)));
4453 prev_sp_offset = 8; /* ip and lr */
4454 for (i = 0; i < 16; ++i) {
4455 if (cfg->used_int_regs & (1 << i))
4456 prev_sp_offset += 4;
4458 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
4460 for (i = 0; i < 16; ++i) {
4461 if ((cfg->used_int_regs & (1 << i)) || (i == ARMREG_IP) || (i == ARMREG_LR)) {
4462 mono_emit_unwind_op_offset (cfg, code, i, (- prev_sp_offset) + reg_offset);
4467 ARM_PUSH (code, 0x5ff0);
4468 prev_sp_offset = 4 * 10; /* all but r0-r3, sp and pc */
4469 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
4471 for (i = 0; i < 16; ++i) {
4472 if ((i > ARMREG_R3) && (i != ARMREG_SP) && (i != ARMREG_PC)) {
4473 mono_emit_unwind_op_offset (cfg, code, i, (- prev_sp_offset) + reg_offset);
4477 pos += sizeof (MonoLMF) - prev_sp_offset;
4481 // align to MONO_ARCH_FRAME_ALIGNMENT bytes
4482 if (alloc_size & (MONO_ARCH_FRAME_ALIGNMENT - 1)) {
4483 alloc_size += MONO_ARCH_FRAME_ALIGNMENT - 1;
4484 alloc_size &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
4487 /* the stack used in the pushed regs */
4488 if (prev_sp_offset & 4)
4490 cfg->stack_usage = alloc_size;
4492 if ((i = mono_arm_is_rotated_imm8 (alloc_size, &rot_amount)) >= 0) {
4493 ARM_SUB_REG_IMM (code, ARMREG_SP, ARMREG_SP, i, rot_amount);
4495 code = mono_arm_emit_load_imm (code, ARMREG_IP, alloc_size);
4496 ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
4498 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset + alloc_size);
4500 if (cfg->frame_reg != ARMREG_SP) {
4501 ARM_MOV_REG_REG (code, cfg->frame_reg, ARMREG_SP);
4502 mono_emit_unwind_op_def_cfa_reg (cfg, code, cfg->frame_reg);
4504 //g_print ("prev_sp_offset: %d, alloc_size:%d\n", prev_sp_offset, alloc_size);
4505 prev_sp_offset += alloc_size;
4507 /* compute max_offset in order to use short forward jumps
4508 * we could skip do it on arm because the immediate displacement
4509 * for jumps is large enough, it may be useful later for constant pools
4512 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4513 MonoInst *ins = bb->code;
4514 bb->max_offset = max_offset;
4516 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
4519 MONO_BB_FOR_EACH_INS (bb, ins)
4520 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
4523 /* store runtime generic context */
4524 if (cfg->rgctx_var) {
4525 MonoInst *ins = cfg->rgctx_var;
4527 g_assert (ins->opcode == OP_REGOFFSET);
4529 if (arm_is_imm12 (ins->inst_offset)) {
4530 ARM_STR_IMM (code, MONO_ARCH_RGCTX_REG, ins->inst_basereg, ins->inst_offset);
4532 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4533 ARM_STR_REG_REG (code, MONO_ARCH_RGCTX_REG, ins->inst_basereg, ARMREG_LR);
4537 /* load arguments allocated to register from the stack */
4540 cinfo = get_call_info (cfg->generic_sharing_context, NULL, sig, sig->pinvoke);
4542 if (MONO_TYPE_ISSTRUCT (sig->ret) && cinfo->ret.storage != RegTypeStructByVal) {
4543 ArgInfo *ainfo = &cinfo->ret;
4544 inst = cfg->vret_addr;
4545 g_assert (arm_is_imm12 (inst->inst_offset));
4546 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4549 if (sig->call_convention == MONO_CALL_VARARG) {
4550 ArgInfo *cookie = &cinfo->sig_cookie;
4552 /* Save the sig cookie address */
4553 g_assert (cookie->storage == RegTypeBase);
4555 g_assert (arm_is_imm12 (prev_sp_offset + cookie->offset));
4556 g_assert (arm_is_imm12 (cfg->sig_cookie));
4557 ARM_ADD_REG_IMM8 (code, ARMREG_IP, cfg->frame_reg, prev_sp_offset + cookie->offset);
4558 ARM_STR_IMM (code, ARMREG_IP, cfg->frame_reg, cfg->sig_cookie);
4561 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
4562 ArgInfo *ainfo = cinfo->args + i;
4563 inst = cfg->args [pos];
4565 if (cfg->verbose_level > 2)
4566 g_print ("Saving argument %d (type: %d)\n", i, ainfo->storage);
4567 if (inst->opcode == OP_REGVAR) {
4568 if (ainfo->storage == RegTypeGeneral)
4569 ARM_MOV_REG_REG (code, inst->dreg, ainfo->reg);
4570 else if (ainfo->storage == RegTypeFP) {
4571 g_assert_not_reached ();
4572 } else if (ainfo->storage == RegTypeBase) {
4573 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
4574 ARM_LDR_IMM (code, inst->dreg, ARMREG_SP, (prev_sp_offset + ainfo->offset));
4576 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4577 ARM_LDR_REG_REG (code, inst->dreg, ARMREG_SP, ARMREG_IP);
4580 g_assert_not_reached ();
4582 if (cfg->verbose_level > 2)
4583 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
4585 /* the argument should be put on the stack: FIXME handle size != word */
4586 if (ainfo->storage == RegTypeGeneral || ainfo->storage == RegTypeIRegPair) {
4587 switch (ainfo->size) {
4589 if (arm_is_imm12 (inst->inst_offset))
4590 ARM_STRB_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4592 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4593 ARM_STRB_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
4597 if (arm_is_imm8 (inst->inst_offset)) {
4598 ARM_STRH_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4600 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4601 ARM_STRH_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
4605 g_assert (arm_is_imm12 (inst->inst_offset));
4606 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4607 g_assert (arm_is_imm12 (inst->inst_offset + 4));
4608 ARM_STR_IMM (code, ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
4611 if (arm_is_imm12 (inst->inst_offset)) {
4612 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4614 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4615 ARM_STR_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
4619 } else if (ainfo->storage == RegTypeBaseGen) {
4620 g_assert (arm_is_imm12 (prev_sp_offset + ainfo->offset));
4621 g_assert (arm_is_imm12 (inst->inst_offset));
4622 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
4623 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset + 4);
4624 ARM_STR_IMM (code, ARMREG_R3, inst->inst_basereg, inst->inst_offset);
4625 } else if (ainfo->storage == RegTypeBase) {
4626 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
4627 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
4629 code = mono_arm_emit_load_imm (code, ARMREG_IP, prev_sp_offset + ainfo->offset);
4630 ARM_LDR_REG_REG (code, ARMREG_LR, ARMREG_SP, ARMREG_IP);
4633 switch (ainfo->size) {
4635 if (arm_is_imm8 (inst->inst_offset)) {
4636 ARM_STRB_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
4638 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4639 ARM_STRB_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
4643 if (arm_is_imm8 (inst->inst_offset)) {
4644 ARM_STRH_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
4646 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4647 ARM_STRH_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
4651 if (arm_is_imm12 (inst->inst_offset)) {
4652 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
4654 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4655 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
4657 if (arm_is_imm12 (prev_sp_offset + ainfo->offset + 4)) {
4658 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset + 4));
4660 code = mono_arm_emit_load_imm (code, ARMREG_IP, prev_sp_offset + ainfo->offset + 4);
4661 ARM_LDR_REG_REG (code, ARMREG_LR, ARMREG_SP, ARMREG_IP);
4663 if (arm_is_imm12 (inst->inst_offset + 4)) {
4664 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset + 4);
4666 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset + 4);
4667 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
4671 if (arm_is_imm12 (inst->inst_offset)) {
4672 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
4674 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4675 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
4679 } else if (ainfo->storage == RegTypeFP) {
4680 g_assert_not_reached ();
4681 } else if (ainfo->storage == RegTypeStructByVal) {
4682 int doffset = inst->inst_offset;
4686 size = mini_type_stack_size_full (cfg->generic_sharing_context, inst->inst_vtype, NULL, sig->pinvoke);
4687 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
4688 if (arm_is_imm12 (doffset)) {
4689 ARM_STR_IMM (code, ainfo->reg + cur_reg, inst->inst_basereg, doffset);
4691 code = mono_arm_emit_load_imm (code, ARMREG_IP, doffset);
4692 ARM_STR_REG_REG (code, ainfo->reg + cur_reg, inst->inst_basereg, ARMREG_IP);
4694 soffset += sizeof (gpointer);
4695 doffset += sizeof (gpointer);
4697 if (ainfo->vtsize) {
4698 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
4699 //g_print ("emit_memcpy (prev_sp_ofs: %d, ainfo->offset: %d, soffset: %d)\n", prev_sp_offset, ainfo->offset, soffset);
4700 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ARMREG_SP, prev_sp_offset + ainfo->offset);
4702 } else if (ainfo->storage == RegTypeStructByAddr) {
4703 g_assert_not_reached ();
4704 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
4705 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
4707 g_assert_not_reached ();
4712 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
4713 if (cfg->compile_aot)
4714 /* AOT code is only used in the root domain */
4715 code = mono_arm_emit_load_imm (code, ARMREG_R0, 0);
4717 code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->domain);
4718 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
4719 (gpointer)"mono_jit_thread_attach");
4720 code = emit_call_seq (cfg, code);
4723 if (method->save_lmf) {
4724 gboolean get_lmf_fast = FALSE;
4726 #ifdef HAVE_AEABI_READ_TP
4727 gint32 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
4729 if (lmf_addr_tls_offset != -1) {
4730 get_lmf_fast = TRUE;
4732 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
4733 (gpointer)"__aeabi_read_tp");
4734 code = emit_call_seq (cfg, code);
4736 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, lmf_addr_tls_offset);
4737 get_lmf_fast = TRUE;
4740 if (!get_lmf_fast) {
4741 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
4742 (gpointer)"mono_get_lmf_addr");
4743 code = emit_call_seq (cfg, code);
4745 /* we build the MonoLMF structure on the stack - see mini-arm.h */
4746 /* lmf_offset is the offset from the previous stack pointer,
4747 * alloc_size is the total stack space allocated, so the offset
4748 * of MonoLMF from the current stack ptr is alloc_size - lmf_offset.
4749 * The pointer to the struct is put in r1 (new_lmf).
4750 * r2 is used as scratch
4751 * The callee-saved registers are already in the MonoLMF structure
4753 code = emit_big_add (code, ARMREG_R1, ARMREG_SP, alloc_size - lmf_offset);
4754 /* r0 is the result from mono_get_lmf_addr () */
4755 ARM_STR_IMM (code, ARMREG_R0, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
4756 /* new_lmf->previous_lmf = *lmf_addr */
4757 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
4758 ARM_STR_IMM (code, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
4759 /* *(lmf_addr) = r1 */
4760 ARM_STR_IMM (code, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
4761 /* Skip method (only needed for trampoline LMF frames) */
4762 ARM_STR_IMM (code, ARMREG_SP, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, esp));
4763 /* save the current IP */
4764 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_PC);
4765 ARM_STR_IMM (code, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, eip));
4769 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
4771 if (cfg->arch.seq_point_info_var) {
4772 MonoInst *ins = cfg->arch.seq_point_info_var;
4774 /* Initialize the variable from a GOT slot */
4775 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_SEQ_POINT_INFO, cfg->method);
4776 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
4778 *(gpointer*)code = NULL;
4780 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
4782 g_assert (ins->opcode == OP_REGOFFSET);
4784 if (arm_is_imm12 (ins->inst_offset)) {
4785 ARM_STR_IMM (code, ARMREG_R0, ins->inst_basereg, ins->inst_offset);
4787 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4788 ARM_STR_REG_REG (code, ARMREG_R0, ins->inst_basereg, ARMREG_LR);
4792 /* Initialize ss_trigger_page_var */
4794 MonoInst *info_var = cfg->arch.seq_point_info_var;
4795 MonoInst *ss_trigger_page_var = cfg->arch.ss_trigger_page_var;
4796 int dreg = ARMREG_LR;
4799 g_assert (info_var->opcode == OP_REGOFFSET);
4800 g_assert (arm_is_imm12 (info_var->inst_offset));
4802 ARM_LDR_IMM (code, dreg, info_var->inst_basereg, info_var->inst_offset);
4803 /* Load the trigger page addr */
4804 ARM_LDR_IMM (code, dreg, dreg, G_STRUCT_OFFSET (SeqPointInfo, ss_trigger_page));
4805 ARM_STR_IMM (code, dreg, ss_trigger_page_var->inst_basereg, ss_trigger_page_var->inst_offset);
4809 cfg->code_len = code - cfg->native_code;
4810 g_assert (cfg->code_len < cfg->code_size);
4817 mono_arch_emit_epilog (MonoCompile *cfg)
4819 MonoMethod *method = cfg->method;
4820 int pos, i, rot_amount;
4821 int max_epilog_size = 16 + 20*4;
4825 if (cfg->method->save_lmf)
4826 max_epilog_size += 128;
4828 if (mono_jit_trace_calls != NULL)
4829 max_epilog_size += 50;
4831 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
4832 max_epilog_size += 50;
4834 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
4835 cfg->code_size *= 2;
4836 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4837 mono_jit_stats.code_reallocs++;
4841 * Keep in sync with OP_JMP
4843 code = cfg->native_code + cfg->code_len;
4845 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
4846 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
4850 /* Load returned vtypes into registers if needed */
4851 cinfo = cfg->arch.cinfo;
4852 if (cinfo->ret.storage == RegTypeStructByVal) {
4853 MonoInst *ins = cfg->ret;
4855 if (arm_is_imm12 (ins->inst_offset)) {
4856 ARM_LDR_IMM (code, ARMREG_R0, ins->inst_basereg, ins->inst_offset);
4858 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4859 ARM_LDR_REG_REG (code, ARMREG_R0, ins->inst_basereg, ARMREG_LR);
4863 if (method->save_lmf) {
4865 /* all but r0-r3, sp and pc */
4866 pos += sizeof (MonoLMF) - (4 * 10);
4868 /* r2 contains the pointer to the current LMF */
4869 code = emit_big_add (code, ARMREG_R2, cfg->frame_reg, cfg->stack_usage - lmf_offset);
4870 /* ip = previous_lmf */
4871 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R2, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
4873 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_R2, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
4874 /* *(lmf_addr) = previous_lmf */
4875 ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
4876 /* FIXME: speedup: there is no actual need to restore the registers if
4877 * we didn't actually change them (idea from Zoltan).
4880 /* point sp at the registers to restore: 10 is 14 -4, because we skip r0-r3 */
4881 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_R2, (sizeof (MonoLMF) - 10 * sizeof (gulong)));
4882 ARM_POP_NWB (code, 0xaff0); /* restore ip to sp and lr to pc */
4884 if ((i = mono_arm_is_rotated_imm8 (cfg->stack_usage, &rot_amount)) >= 0) {
4885 ARM_ADD_REG_IMM (code, ARMREG_SP, cfg->frame_reg, i, rot_amount);
4887 code = mono_arm_emit_load_imm (code, ARMREG_IP, cfg->stack_usage);
4888 ARM_ADD_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
4890 /* FIXME: add v4 thumb interworking support */
4891 ARM_POP_NWB (code, cfg->used_int_regs | ((1 << ARMREG_SP) | (1 << ARMREG_PC)));
4894 cfg->code_len = code - cfg->native_code;
4896 g_assert (cfg->code_len < cfg->code_size);
4900 /* remove once throw_exception_by_name is eliminated */
4902 exception_id_by_name (const char *name)
4904 if (strcmp (name, "IndexOutOfRangeException") == 0)
4905 return MONO_EXC_INDEX_OUT_OF_RANGE;
4906 if (strcmp (name, "OverflowException") == 0)
4907 return MONO_EXC_OVERFLOW;
4908 if (strcmp (name, "ArithmeticException") == 0)
4909 return MONO_EXC_ARITHMETIC;
4910 if (strcmp (name, "DivideByZeroException") == 0)
4911 return MONO_EXC_DIVIDE_BY_ZERO;
4912 if (strcmp (name, "InvalidCastException") == 0)
4913 return MONO_EXC_INVALID_CAST;
4914 if (strcmp (name, "NullReferenceException") == 0)
4915 return MONO_EXC_NULL_REF;
4916 if (strcmp (name, "ArrayTypeMismatchException") == 0)
4917 return MONO_EXC_ARRAY_TYPE_MISMATCH;
4918 if (strcmp (name, "ArgumentException") == 0)
4919 return MONO_EXC_ARGUMENT;
4920 g_error ("Unknown intrinsic exception %s\n", name);
4925 mono_arch_emit_exceptions (MonoCompile *cfg)
4927 MonoJumpInfo *patch_info;
4930 guint8* exc_throw_pos [MONO_EXC_INTRINS_NUM];
4931 guint8 exc_throw_found [MONO_EXC_INTRINS_NUM];
4932 int max_epilog_size = 50;
4934 for (i = 0; i < MONO_EXC_INTRINS_NUM; i++) {
4935 exc_throw_pos [i] = NULL;
4936 exc_throw_found [i] = 0;
4939 /* count the number of exception infos */
4942 * make sure we have enough space for exceptions
4944 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4945 if (patch_info->type == MONO_PATCH_INFO_EXC) {
4946 i = exception_id_by_name (patch_info->data.target);
4947 if (!exc_throw_found [i]) {
4948 max_epilog_size += 32;
4949 exc_throw_found [i] = TRUE;
4954 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
4955 cfg->code_size *= 2;
4956 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4957 mono_jit_stats.code_reallocs++;
4960 code = cfg->native_code + cfg->code_len;
4962 /* add code to raise exceptions */
4963 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4964 switch (patch_info->type) {
4965 case MONO_PATCH_INFO_EXC: {
4966 MonoClass *exc_class;
4967 unsigned char *ip = patch_info->ip.i + cfg->native_code;
4969 i = exception_id_by_name (patch_info->data.target);
4970 if (exc_throw_pos [i]) {
4971 arm_patch (ip, exc_throw_pos [i]);
4972 patch_info->type = MONO_PATCH_INFO_NONE;
4975 exc_throw_pos [i] = code;
4977 arm_patch (ip, code);
4979 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
4980 g_assert (exc_class);
4982 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR);
4983 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
4984 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
4985 patch_info->data.name = "mono_arch_throw_corlib_exception";
4986 patch_info->ip.i = code - cfg->native_code;
4988 *(guint32*)(gpointer)code = exc_class->type_token;
4998 cfg->code_len = code - cfg->native_code;
5000 g_assert (cfg->code_len < cfg->code_size);
5004 #endif /* #ifndef DISABLE_JIT */
5006 static gboolean tls_offset_inited = FALSE;
5009 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
5011 if (!tls_offset_inited) {
5012 tls_offset_inited = TRUE;
5014 lmf_tls_offset = mono_get_lmf_tls_offset ();
5015 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
5020 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
5025 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5032 mono_arch_print_tree (MonoInst *tree, int arity)
5038 mono_arch_get_domain_intrinsic (MonoCompile* cfg)
5040 return mono_get_domain_intrinsic (cfg);
5044 mono_arch_get_patch_offset (guint8 *code)
5051 mono_arch_flush_register_windows (void)
5055 #ifdef MONO_ARCH_HAVE_IMT
5060 mono_arch_emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoInst *imt_arg)
5062 if (cfg->compile_aot) {
5063 int method_reg = mono_alloc_ireg (cfg);
5066 call->dynamic_imt_arg = TRUE;
5069 mono_call_inst_add_outarg_reg (cfg, call, imt_arg->dreg, ARMREG_V5, FALSE);
5071 MONO_INST_NEW (cfg, ins, OP_AOTCONST);
5072 ins->dreg = method_reg;
5073 ins->inst_p0 = call->method;
5074 ins->inst_c1 = MONO_PATCH_INFO_METHODCONST;
5075 MONO_ADD_INS (cfg->cbb, ins);
5077 mono_call_inst_add_outarg_reg (cfg, call, method_reg, ARMREG_V5, FALSE);
5079 } else if (cfg->generic_context || imt_arg || mono_use_llvm) {
5081 /* Always pass in a register for simplicity */
5082 call->dynamic_imt_arg = TRUE;
5084 cfg->uses_rgctx_reg = TRUE;
5087 mono_call_inst_add_outarg_reg (cfg, call, imt_arg->dreg, ARMREG_V5, FALSE);
5090 int method_reg = mono_alloc_preg (cfg);
5092 MONO_INST_NEW (cfg, ins, OP_PCONST);
5093 ins->inst_p0 = call->method;
5094 ins->dreg = method_reg;
5095 MONO_ADD_INS (cfg->cbb, ins);
5097 mono_call_inst_add_outarg_reg (cfg, call, method_reg, ARMREG_V5, FALSE);
5102 #endif /* DISABLE_JIT */
5105 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
5107 guint32 *code_ptr = (guint32*)code;
5112 return (MonoMethod*)regs [ARMREG_V5];
5114 /* The IMT value is stored in the code stream right after the LDC instruction. */
5115 if (!IS_LDR_PC (code_ptr [0])) {
5116 g_warning ("invalid code stream, instruction before IMT value is not a LDC in %s() (code %p value 0: 0x%x -1: 0x%x -2: 0x%x)", __FUNCTION__, code, code_ptr [2], code_ptr [1], code_ptr [0]);
5117 g_assert (IS_LDR_PC (code_ptr [0]));
5119 if (code_ptr [1] == 0)
5120 /* This is AOTed code, the IMT method is in V5 */
5121 return (MonoMethod*)regs [ARMREG_V5];
5123 return (MonoMethod*) code_ptr [1];
5127 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
5129 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
5132 #define ENABLE_WRONG_METHOD_CHECK 0
5133 #define BASE_SIZE (6 * 4)
5134 #define BSEARCH_ENTRY_SIZE (4 * 4)
5135 #define CMP_SIZE (3 * 4)
5136 #define BRANCH_SIZE (1 * 4)
5137 #define CALL_SIZE (2 * 4)
5138 #define WMC_SIZE (5 * 4)
5139 #define DISTANCE(A, B) (((gint32)(B)) - ((gint32)(A)))
5142 arm_emit_value_and_patch_ldr (arminstr_t *code, arminstr_t *target, guint32 value)
5144 guint32 delta = DISTANCE (target, code);
5146 g_assert (delta >= 0 && delta <= 0xFFF);
5147 *target = *target | delta;
5153 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
5154 gpointer fail_tramp)
5156 int size, i, extra_space = 0;
5157 arminstr_t *code, *start, *vtable_target = NULL;
5158 gboolean large_offsets = FALSE;
5159 guint32 **constant_pool_starts;
5162 constant_pool_starts = g_new0 (guint32*, count);
5164 for (i = 0; i < count; ++i) {
5165 MonoIMTCheckItem *item = imt_entries [i];
5166 if (item->is_equals) {
5167 gboolean fail_case = !item->check_target_idx && fail_tramp;
5169 if (item->has_target_code || !arm_is_imm12 (DISTANCE (vtable, &vtable->vtable[item->value.vtable_slot]))) {
5170 item->chunk_size += 32;
5171 large_offsets = TRUE;
5174 if (item->check_target_idx || fail_case) {
5175 if (!item->compare_done || fail_case)
5176 item->chunk_size += CMP_SIZE;
5177 item->chunk_size += BRANCH_SIZE;
5179 #if ENABLE_WRONG_METHOD_CHECK
5180 item->chunk_size += WMC_SIZE;
5184 item->chunk_size += 16;
5185 large_offsets = TRUE;
5187 item->chunk_size += CALL_SIZE;
5189 item->chunk_size += BSEARCH_ENTRY_SIZE;
5190 imt_entries [item->check_target_idx]->compare_done = TRUE;
5192 size += item->chunk_size;
5196 size += 4 * count; /* The ARM_ADD_REG_IMM to pop the stack */
5199 code = mono_method_alloc_generic_virtual_thunk (domain, size);
5201 code = mono_domain_code_reserve (domain, size);
5205 printf ("building IMT thunk for class %s %s entries %d code size %d code at %p end %p vtable %p\n", vtable->klass->name_space, vtable->klass->name, count, size, start, ((guint8*)start) + size, vtable);
5206 for (i = 0; i < count; ++i) {
5207 MonoIMTCheckItem *item = imt_entries [i];
5208 printf ("method %d (%p) %s vtable slot %p is_equals %d chunk size %d\n", i, item->key, item->key->name, &vtable->vtable [item->value.vtable_slot], item->is_equals, item->chunk_size);
5213 ARM_PUSH4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5215 ARM_PUSH2 (code, ARMREG_R0, ARMREG_R1);
5216 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_LR, -4);
5217 vtable_target = code;
5218 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
5220 if (mono_use_llvm) {
5221 /* LLVM always passes the IMT method in R5 */
5222 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_V5);
5224 /* R0 == 0 means we are called from AOT code. In this case, V5 contains the IMT method */
5225 ARM_CMP_REG_IMM8 (code, ARMREG_R0, 0);
5226 ARM_MOV_REG_REG_COND (code, ARMREG_R0, ARMREG_V5, ARMCOND_EQ);
5229 for (i = 0; i < count; ++i) {
5230 MonoIMTCheckItem *item = imt_entries [i];
5231 arminstr_t *imt_method = NULL, *vtable_offset_ins = NULL, *target_code_ins = NULL;
5232 gint32 vtable_offset;
5234 item->code_target = (guint8*)code;
5236 if (item->is_equals) {
5237 gboolean fail_case = !item->check_target_idx && fail_tramp;
5239 if (item->check_target_idx || fail_case) {
5240 if (!item->compare_done || fail_case) {
5242 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5243 ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
5245 item->jmp_code = (guint8*)code;
5246 ARM_B_COND (code, ARMCOND_NE, 0);
5248 /*Enable the commented code to assert on wrong method*/
5249 #if ENABLE_WRONG_METHOD_CHECK
5251 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5252 ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
5253 ARM_B_COND (code, ARMCOND_NE, 1);
5259 if (item->has_target_code) {
5260 target_code_ins = code;
5261 /* Load target address */
5262 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5263 /* Save it to the fourth slot */
5264 ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
5265 /* Restore registers and branch */
5266 ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5268 code = arm_emit_value_and_patch_ldr (code, target_code_ins, (gsize)item->value.target_code);
5270 vtable_offset = DISTANCE (vtable, &vtable->vtable[item->value.vtable_slot]);
5271 if (!arm_is_imm12 (vtable_offset)) {
5273 * We need to branch to a computed address but we don't have
5274 * a free register to store it, since IP must contain the
5275 * vtable address. So we push the two values to the stack, and
5276 * load them both using LDM.
5278 /* Compute target address */
5279 vtable_offset_ins = code;
5280 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5281 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_IP, ARMREG_R1);
5282 /* Save it to the fourth slot */
5283 ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
5284 /* Restore registers and branch */
5285 ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5287 code = arm_emit_value_and_patch_ldr (code, vtable_offset_ins, vtable_offset);
5289 ARM_POP2 (code, ARMREG_R0, ARMREG_R1);
5291 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 2 * sizeof (gpointer));
5292 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, vtable_offset);
5297 arm_patch (item->jmp_code, (guchar*)code);
5299 target_code_ins = code;
5300 /* Load target address */
5301 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5302 /* Save it to the fourth slot */
5303 ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
5304 /* Restore registers and branch */
5305 ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5307 code = arm_emit_value_and_patch_ldr (code, target_code_ins, (gsize)fail_tramp);
5308 item->jmp_code = NULL;
5312 code = arm_emit_value_and_patch_ldr (code, imt_method, (guint32)item->key);
5314 /*must emit after unconditional branch*/
5315 if (vtable_target) {
5316 code = arm_emit_value_and_patch_ldr (code, vtable_target, (guint32)vtable);
5317 item->chunk_size += 4;
5318 vtable_target = NULL;
5321 /*We reserve the space for bsearch IMT values after the first entry with an absolute jump*/
5322 constant_pool_starts [i] = code;
5324 code += extra_space;
5328 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5329 ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
5331 item->jmp_code = (guint8*)code;
5332 ARM_B_COND (code, ARMCOND_GE, 0);
5337 for (i = 0; i < count; ++i) {
5338 MonoIMTCheckItem *item = imt_entries [i];
5339 if (item->jmp_code) {
5340 if (item->check_target_idx)
5341 arm_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
5343 if (i > 0 && item->is_equals) {
5345 arminstr_t *space_start = constant_pool_starts [i];
5346 for (j = i - 1; j >= 0 && !imt_entries [j]->is_equals; --j) {
5347 space_start = arm_emit_value_and_patch_ldr (space_start, (arminstr_t*)imt_entries [j]->code_target, (guint32)imt_entries [j]->key);
5354 char *buff = g_strdup_printf ("thunk_for_class_%s_%s_entries_%d", vtable->klass->name_space, vtable->klass->name, count);
5355 mono_disassemble_code (NULL, (guint8*)start, size, buff);
5360 g_free (constant_pool_starts);
5362 mono_arch_flush_icache ((guint8*)start, size);
5363 mono_stats.imt_thunks_size += code - start;
5365 g_assert (DISTANCE (start, code) <= size);
5372 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
5374 if (reg == ARMREG_SP)
5375 return (gpointer)ctx->esp;
5377 return (gpointer)ctx->regs [reg];
5381 * mono_arch_set_breakpoint:
5383 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
5384 * The location should contain code emitted by OP_SEQ_POINT.
5387 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
5390 guint32 native_offset = ip - (guint8*)ji->code_start;
5393 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
5395 g_assert (native_offset % 4 == 0);
5396 g_assert (info->bp_addrs [native_offset / 4] == 0);
5397 info->bp_addrs [native_offset / 4] = bp_trigger_page;
5399 int dreg = ARMREG_LR;
5401 /* Read from another trigger page */
5402 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
5404 *(int*)code = (int)bp_trigger_page;
5406 ARM_LDR_IMM (code, dreg, dreg, 0);
5408 mono_arch_flush_icache (code - 16, 16);
5411 /* This is currently implemented by emitting an SWI instruction, which
5412 * qemu/linux seems to convert to a SIGILL.
5414 *(int*)code = (0xef << 24) | 8;
5416 mono_arch_flush_icache (code - 4, 4);
5422 * mono_arch_clear_breakpoint:
5424 * Clear the breakpoint at IP.
5427 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
5433 guint32 native_offset = ip - (guint8*)ji->code_start;
5434 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
5436 g_assert (native_offset % 4 == 0);
5437 g_assert (info->bp_addrs [native_offset / 4] == bp_trigger_page);
5438 info->bp_addrs [native_offset / 4] = 0;
5440 for (i = 0; i < 4; ++i)
5443 mono_arch_flush_icache (ip, code - ip);
5448 * mono_arch_start_single_stepping:
5450 * Start single stepping.
5453 mono_arch_start_single_stepping (void)
5455 mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
5459 * mono_arch_stop_single_stepping:
5461 * Stop single stepping.
5464 mono_arch_stop_single_stepping (void)
5466 mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
5470 #define DBG_SIGNAL SIGBUS
5472 #define DBG_SIGNAL SIGSEGV
5476 * mono_arch_is_single_step_event:
5478 * Return whenever the machine state in SIGCTX corresponds to a single
5482 mono_arch_is_single_step_event (void *info, void *sigctx)
5484 siginfo_t *sinfo = info;
5486 /* Sometimes the address is off by 4 */
5487 if (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128)
5494 * mono_arch_is_breakpoint_event:
5496 * Return whenever the machine state in SIGCTX corresponds to a breakpoint event.
5499 mono_arch_is_breakpoint_event (void *info, void *sigctx)
5501 siginfo_t *sinfo = info;
5503 if (sinfo->si_signo == DBG_SIGNAL) {
5504 /* Sometimes the address is off by 4 */
5505 if (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128)
5515 mono_arch_get_ip_for_breakpoint (MonoJitInfo *ji, MonoContext *ctx)
5517 guint8 *ip = MONO_CONTEXT_GET_IP (ctx);
5528 mono_arch_get_ip_for_single_step (MonoJitInfo *ji, MonoContext *ctx)
5530 guint8 *ip = MONO_CONTEXT_GET_IP (ctx);
5538 * mono_arch_skip_breakpoint:
5540 * See mini-amd64.c for docs.
5543 mono_arch_skip_breakpoint (MonoContext *ctx)
5545 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 4);
5549 * mono_arch_skip_single_step:
5551 * See mini-amd64.c for docs.
5554 mono_arch_skip_single_step (MonoContext *ctx)
5556 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 4);
5560 * mono_arch_get_seq_point_info:
5562 * See mini-amd64.c for docs.
5565 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
5570 // FIXME: Add a free function
5572 mono_domain_lock (domain);
5573 info = g_hash_table_lookup (domain_jit_info (domain)->arch_seq_points,
5575 mono_domain_unlock (domain);
5578 ji = mono_jit_info_table_find (domain, (char*)code);
5581 info = g_malloc0 (sizeof (SeqPointInfo) + ji->code_size);
5583 info->ss_trigger_page = ss_trigger_page;
5584 info->bp_trigger_page = bp_trigger_page;
5586 mono_domain_lock (domain);
5587 g_hash_table_insert (domain_jit_info (domain)->arch_seq_points,
5589 mono_domain_unlock (domain);