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 v6_supported = 0;
41 static int v7_supported = 0;
42 static int thumb_supported = 0;
44 * Whenever to use the ARM EABI
46 static int eabi_supported = 0;
49 * Whenever we are on arm/darwin aka the iphone.
51 static int darwin = 0;
53 * Whenever to use the iphone ABI extensions:
54 * http://developer.apple.com/library/ios/documentation/Xcode/Conceptual/iPhoneOSABIReference/index.html
55 * Basically, r7 is used as a frame pointer and it should point to the saved r7 + lr.
56 * This is required for debugging/profiling tools to work, but it has some overhead so it should
57 * only be turned on in debug builds.
59 static int iphone_abi = 0;
63 * The code generated for sequence points reads from this location, which is
64 * made read-only when single stepping is enabled.
66 static gpointer ss_trigger_page;
68 /* Enabled breakpoints read from this trigger page */
69 static gpointer bp_trigger_page;
71 /* Structure used by the sequence points in AOTed code */
73 gpointer ss_trigger_page;
74 gpointer bp_trigger_page;
75 guint8* bp_addrs [MONO_ZERO_LEN_ARRAY];
80 * floating point support: on ARM it is a mess, there are at least 3
81 * different setups, each of which binary incompat with the other.
82 * 1) FPA: old and ugly, but unfortunately what current distros use
83 * the double binary format has the two words swapped. 8 double registers.
84 * Implemented usually by kernel emulation.
85 * 2) softfloat: the compiler emulates all the fp ops. Usually uses the
86 * ugly swapped double format (I guess a softfloat-vfp exists, too, though).
87 * 3) VFP: the new and actually sensible and useful FP support. Implemented
88 * in HW or kernel-emulated, requires new tools. I think this is what symbian uses.
90 * The plan is to write the FPA support first. softfloat can be tested in a chroot.
92 int mono_exc_esp_offset = 0;
94 #define arm_is_imm12(v) ((v) > -4096 && (v) < 4096)
95 #define arm_is_imm8(v) ((v) > -256 && (v) < 256)
96 #define arm_is_fpimm8(v) ((v) >= -1020 && (v) <= 1020)
98 #define LDR_MASK ((0xf << ARMCOND_SHIFT) | (3 << 26) | (1 << 22) | (1 << 20) | (15 << 12))
99 #define LDR_PC_VAL ((ARMCOND_AL << ARMCOND_SHIFT) | (1 << 26) | (0 << 22) | (1 << 20) | (15 << 12))
100 #define IS_LDR_PC(val) (((val) & LDR_MASK) == LDR_PC_VAL)
102 #define ADD_LR_PC_4 ((ARMCOND_AL << ARMCOND_SHIFT) | (1 << 25) | (1 << 23) | (ARMREG_PC << 16) | (ARMREG_LR << 12) | 4)
103 #define MOV_LR_PC ((ARMCOND_AL << ARMCOND_SHIFT) | (1 << 24) | (0xa << 20) | (ARMREG_LR << 12) | ARMREG_PC)
106 /* A variant of ARM_LDR_IMM which can handle large offsets */
107 #define ARM_LDR_IMM_GENERAL(code, dreg, basereg, offset, scratch_reg) do { \
108 if (arm_is_imm12 ((offset))) { \
109 ARM_LDR_IMM (code, (dreg), (basereg), (offset)); \
111 g_assert ((scratch_reg) != (basereg)); \
112 code = mono_arm_emit_load_imm (code, (scratch_reg), (offset)); \
113 ARM_LDR_REG_REG (code, (dreg), (basereg), (scratch_reg)); \
117 #define ARM_STR_IMM_GENERAL(code, dreg, basereg, offset, scratch_reg) do { \
118 if (arm_is_imm12 ((offset))) { \
119 ARM_STR_IMM (code, (dreg), (basereg), (offset)); \
121 g_assert ((scratch_reg) != (basereg)); \
122 code = mono_arm_emit_load_imm (code, (scratch_reg), (offset)); \
123 ARM_STR_REG_REG (code, (dreg), (basereg), (scratch_reg)); \
127 static void mono_arch_compute_omit_fp (MonoCompile *cfg);
130 mono_arch_regname (int reg)
132 static const char * rnames[] = {
133 "arm_r0", "arm_r1", "arm_r2", "arm_r3", "arm_v1",
134 "arm_v2", "arm_v3", "arm_v4", "arm_v5", "arm_v6",
135 "arm_v7", "arm_fp", "arm_ip", "arm_sp", "arm_lr",
138 if (reg >= 0 && reg < 16)
144 mono_arch_fregname (int reg)
146 static const char * rnames[] = {
147 "arm_f0", "arm_f1", "arm_f2", "arm_f3", "arm_f4",
148 "arm_f5", "arm_f6", "arm_f7", "arm_f8", "arm_f9",
149 "arm_f10", "arm_f11", "arm_f12", "arm_f13", "arm_f14",
150 "arm_f15", "arm_f16", "arm_f17", "arm_f18", "arm_f19",
151 "arm_f20", "arm_f21", "arm_f22", "arm_f23", "arm_f24",
152 "arm_f25", "arm_f26", "arm_f27", "arm_f28", "arm_f29",
155 if (reg >= 0 && reg < 32)
163 emit_big_add (guint8 *code, int dreg, int sreg, int imm)
165 int imm8, rot_amount;
166 if ((imm8 = mono_arm_is_rotated_imm8 (imm, &rot_amount)) >= 0) {
167 ARM_ADD_REG_IMM (code, dreg, sreg, imm8, rot_amount);
170 g_assert (dreg != sreg);
171 code = mono_arm_emit_load_imm (code, dreg, imm);
172 ARM_ADD_REG_REG (code, dreg, dreg, sreg);
177 emit_memcpy (guint8 *code, int size, int dreg, int doffset, int sreg, int soffset)
179 /* we can use r0-r3, since this is called only for incoming args on the stack */
180 if (size > sizeof (gpointer) * 4) {
182 code = emit_big_add (code, ARMREG_R0, sreg, soffset);
183 code = emit_big_add (code, ARMREG_R1, dreg, doffset);
184 start_loop = code = mono_arm_emit_load_imm (code, ARMREG_R2, size);
185 ARM_LDR_IMM (code, ARMREG_R3, ARMREG_R0, 0);
186 ARM_STR_IMM (code, ARMREG_R3, ARMREG_R1, 0);
187 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, 4);
188 ARM_ADD_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, 4);
189 ARM_SUBS_REG_IMM8 (code, ARMREG_R2, ARMREG_R2, 4);
190 ARM_B_COND (code, ARMCOND_NE, 0);
191 arm_patch (code - 4, start_loop);
194 if (arm_is_imm12 (doffset) && arm_is_imm12 (doffset + size) &&
195 arm_is_imm12 (soffset) && arm_is_imm12 (soffset + size)) {
197 ARM_LDR_IMM (code, ARMREG_LR, sreg, soffset);
198 ARM_STR_IMM (code, ARMREG_LR, dreg, doffset);
204 code = emit_big_add (code, ARMREG_R0, sreg, soffset);
205 code = emit_big_add (code, ARMREG_R1, dreg, doffset);
206 doffset = soffset = 0;
208 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_R0, soffset);
209 ARM_STR_IMM (code, ARMREG_LR, ARMREG_R1, doffset);
215 g_assert (size == 0);
220 emit_call_reg (guint8 *code, int reg)
223 ARM_BLX_REG (code, reg);
225 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
229 ARM_MOV_REG_REG (code, ARMREG_PC, reg);
235 emit_call_seq (MonoCompile *cfg, guint8 *code)
237 if (cfg->method->dynamic) {
238 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
240 *(gpointer*)code = NULL;
242 code = emit_call_reg (code, ARMREG_IP);
250 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
252 switch (ins->opcode) {
255 case OP_FCALL_MEMBASE:
257 if (ins->dreg != ARM_FPA_F0)
258 ARM_MVFD (code, ins->dreg, ARM_FPA_F0);
259 #elif defined(ARM_FPU_VFP)
260 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4) {
261 ARM_FMSR (code, ins->dreg, ARMREG_R0);
262 ARM_CVTS (code, ins->dreg, ins->dreg);
264 ARM_FMDRR (code, ARMREG_R0, ARMREG_R1, ins->dreg);
273 #endif /* #ifndef DISABLE_JIT */
276 * mono_arch_get_argument_info:
277 * @csig: a method signature
278 * @param_count: the number of parameters to consider
279 * @arg_info: an array to store the result infos
281 * Gathers information on parameters such as size, alignment and
282 * padding. arg_info should be large enought to hold param_count + 1 entries.
284 * Returns the size of the activation frame.
287 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
289 int k, frame_size = 0;
290 guint32 size, align, pad;
293 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
294 frame_size += sizeof (gpointer);
298 arg_info [0].offset = offset;
301 frame_size += sizeof (gpointer);
305 arg_info [0].size = frame_size;
307 for (k = 0; k < param_count; k++) {
308 size = mini_type_stack_size_full (NULL, csig->params [k], &align, csig->pinvoke);
310 /* ignore alignment for now */
313 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
314 arg_info [k].pad = pad;
316 arg_info [k + 1].pad = 0;
317 arg_info [k + 1].size = size;
319 arg_info [k + 1].offset = offset;
323 align = MONO_ARCH_FRAME_ALIGNMENT;
324 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
325 arg_info [k].pad = pad;
330 #define MAX_ARCH_DELEGATE_PARAMS 3
333 get_delegate_invoke_impl (gboolean has_target, gboolean param_count, guint32 *code_size)
335 guint8 *code, *start;
338 start = code = mono_global_codeman_reserve (12);
340 /* Replace the this argument with the target */
341 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R0, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
342 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, G_STRUCT_OFFSET (MonoDelegate, target));
343 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
345 g_assert ((code - start) <= 12);
347 mono_arch_flush_icache (start, 12);
351 size = 8 + param_count * 4;
352 start = code = mono_global_codeman_reserve (size);
354 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R0, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
355 /* slide down the arguments */
356 for (i = 0; i < param_count; ++i) {
357 ARM_MOV_REG_REG (code, (ARMREG_R0 + i), (ARMREG_R0 + i + 1));
359 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
361 g_assert ((code - start) <= size);
363 mono_arch_flush_icache (start, size);
367 *code_size = code - start;
373 * mono_arch_get_delegate_invoke_impls:
375 * Return a list of MonoAotTrampInfo structures for the delegate invoke impl
379 mono_arch_get_delegate_invoke_impls (void)
386 code = get_delegate_invoke_impl (TRUE, 0, &code_len);
387 res = g_slist_prepend (res, mono_tramp_info_create (g_strdup ("delegate_invoke_impl_has_target"), code, code_len, NULL, NULL));
389 for (i = 0; i <= MAX_ARCH_DELEGATE_PARAMS; ++i) {
390 code = get_delegate_invoke_impl (FALSE, i, &code_len);
391 res = g_slist_prepend (res, mono_tramp_info_create (g_strdup_printf ("delegate_invoke_impl_target_%d", i), code, code_len, NULL, NULL));
398 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
400 guint8 *code, *start;
402 /* FIXME: Support more cases */
403 if (MONO_TYPE_ISSTRUCT (sig->ret))
407 static guint8* cached = NULL;
408 mono_mini_arch_lock ();
410 mono_mini_arch_unlock ();
415 start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
417 start = get_delegate_invoke_impl (TRUE, 0, NULL);
419 mono_mini_arch_unlock ();
422 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
425 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
427 for (i = 0; i < sig->param_count; ++i)
428 if (!mono_is_regsize_var (sig->params [i]))
431 mono_mini_arch_lock ();
432 code = cache [sig->param_count];
434 mono_mini_arch_unlock ();
439 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
440 start = mono_aot_get_trampoline (name);
443 start = get_delegate_invoke_impl (FALSE, sig->param_count, NULL);
445 cache [sig->param_count] = start;
446 mono_mini_arch_unlock ();
454 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
456 return (gpointer)regs [ARMREG_R0];
460 * Initialize the cpu to execute managed code.
463 mono_arch_cpu_init (void)
465 #if defined(__ARM_EABI__)
466 eabi_supported = TRUE;
468 #if defined(__APPLE__) && defined(MONO_CROSS_COMPILE)
471 i8_align = __alignof__ (gint64);
476 * Initialize architecture specific code.
479 mono_arch_init (void)
481 InitializeCriticalSection (&mini_arch_mutex);
483 ss_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT);
484 bp_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT);
485 mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
487 mono_aot_register_jit_icall ("mono_arm_throw_exception", mono_arm_throw_exception);
488 mono_aot_register_jit_icall ("mono_arm_throw_exception_by_token", mono_arm_throw_exception_by_token);
489 mono_aot_register_jit_icall ("mono_arm_resume_unwind", mono_arm_resume_unwind);
493 * Cleanup architecture specific code.
496 mono_arch_cleanup (void)
501 * This function returns the optimizations supported on this cpu.
504 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
507 const char *cpu_arch = getenv ("MONO_CPU_ARCH");
508 if (cpu_arch != NULL) {
509 thumb_supported = strstr (cpu_arch, "thumb") != NULL;
510 if (strncmp (cpu_arch, "armv", 4) == 0) {
511 v5_supported = cpu_arch [4] >= '5';
512 v6_supported = cpu_arch [4] >= '6';
513 v7_supported = cpu_arch [4] >= '7';
517 thumb_supported = TRUE;
524 FILE *file = fopen ("/proc/cpuinfo", "r");
526 while ((line = fgets (buf, 512, file))) {
527 if (strncmp (line, "Processor", 9) == 0) {
528 char *ver = strstr (line, "(v");
529 if (ver && (ver [2] == '5' || ver [2] == '6' || ver [2] == '7'))
531 if (ver && (ver [2] == '6' || ver [2] == '7'))
533 if (ver && (ver [2] == '7'))
537 if (strncmp (line, "Features", 8) == 0) {
538 char *th = strstr (line, "thumb");
540 thumb_supported = TRUE;
548 /*printf ("features: v5: %d, thumb: %d\n", v5_supported, thumb_supported);*/
553 /* no arm-specific optimizations yet */
561 is_regsize_var (MonoType *t) {
564 t = mini_type_get_underlying_type (NULL, t);
571 case MONO_TYPE_FNPTR:
573 case MONO_TYPE_OBJECT:
574 case MONO_TYPE_STRING:
575 case MONO_TYPE_CLASS:
576 case MONO_TYPE_SZARRAY:
577 case MONO_TYPE_ARRAY:
579 case MONO_TYPE_GENERICINST:
580 if (!mono_type_generic_inst_is_valuetype (t))
583 case MONO_TYPE_VALUETYPE:
590 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
595 for (i = 0; i < cfg->num_varinfo; i++) {
596 MonoInst *ins = cfg->varinfo [i];
597 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
600 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
603 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
606 /* we can only allocate 32 bit values */
607 if (is_regsize_var (ins->inst_vtype)) {
608 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
609 g_assert (i == vmv->idx);
610 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
617 #define USE_EXTRA_TEMPS 0
620 mono_arch_get_global_int_regs (MonoCompile *cfg)
624 mono_arch_compute_omit_fp (cfg);
627 * FIXME: Interface calls might go through a static rgctx trampoline which
628 * sets V5, but it doesn't save it, so we need to save it ourselves, and
631 if (cfg->flags & MONO_CFG_HAS_CALLS)
632 cfg->uses_rgctx_reg = TRUE;
634 if (cfg->arch.omit_fp)
635 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_FP));
636 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V1));
637 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V2));
638 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V3));
640 /* V4=R7 is used as a frame pointer, but V7=R10 is preserved */
641 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V7));
643 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V4));
644 if (!(cfg->compile_aot || cfg->uses_rgctx_reg || COMPILE_LLVM (cfg)))
645 /* V5 is reserved for passing the vtable/rgctx/IMT method */
646 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V5));
647 /*regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V6));*/
648 /*regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V7));*/
654 * mono_arch_regalloc_cost:
656 * Return the cost, in number of memory references, of the action of
657 * allocating the variable VMV into a register during global register
661 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
667 #endif /* #ifndef DISABLE_JIT */
669 #ifndef __GNUC_PREREQ
670 #define __GNUC_PREREQ(maj, min) (0)
674 mono_arch_flush_icache (guint8 *code, gint size)
677 sys_icache_invalidate (code, size);
678 #elif __GNUC_PREREQ(4, 1)
679 __clear_cache (code, code + size);
680 #elif defined(PLATFORM_ANDROID)
681 const int syscall = 0xf0002;
689 : "r" (code), "r" (code + size), "r" (syscall)
690 : "r0", "r1", "r7", "r2"
693 __asm __volatile ("mov r0, %0\n"
696 "swi 0x9f0002 @ sys_cacheflush"
698 : "r" (code), "r" (code + size), "r" (0)
699 : "r0", "r1", "r3" );
716 guint16 vtsize; /* in param area */
720 guint8 size : 4; /* 1, 2, 4, 8, or regs used by RegTypeStructByVal */
726 gboolean vtype_retaddr;
727 /* The index of the vret arg in the argument list */
737 /*#define __alignof__(a) sizeof(a)*/
738 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
744 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
747 if (*gr > ARMREG_R3) {
748 ainfo->offset = *stack_size;
749 ainfo->reg = ARMREG_SP; /* in the caller */
750 ainfo->storage = RegTypeBase;
753 ainfo->storage = RegTypeGeneral;
760 split = i8_align == 4;
764 if (*gr == ARMREG_R3 && split) {
765 /* first word in r3 and the second on the stack */
766 ainfo->offset = *stack_size;
767 ainfo->reg = ARMREG_SP; /* in the caller */
768 ainfo->storage = RegTypeBaseGen;
770 } else if (*gr >= ARMREG_R3) {
771 if (eabi_supported) {
772 /* darwin aligns longs to 4 byte only */
778 ainfo->offset = *stack_size;
779 ainfo->reg = ARMREG_SP; /* in the caller */
780 ainfo->storage = RegTypeBase;
783 if (eabi_supported) {
784 if (i8_align == 8 && ((*gr) & 1))
787 ainfo->storage = RegTypeIRegPair;
796 get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSignature *sig)
799 int n = sig->hasthis + sig->param_count;
800 MonoType *simpletype;
801 guint32 stack_size = 0;
803 gboolean is_pinvoke = sig->pinvoke;
806 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
808 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
813 /* FIXME: handle returning a struct */
814 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
817 if (is_pinvoke && mono_class_native_size (mono_class_from_mono_type (sig->ret), &align) <= sizeof (gpointer)) {
818 cinfo->ret.storage = RegTypeStructByVal;
820 cinfo->vtype_retaddr = TRUE;
827 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
828 * the first argument, allowing 'this' to be always passed in the first arg reg.
829 * Also do this if the first argument is a reference type, since virtual calls
830 * are sometimes made using calli without sig->hasthis set, like in the delegate
833 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]))))) {
835 add_general (&gr, &stack_size, cinfo->args + 0, TRUE);
837 add_general (&gr, &stack_size, &cinfo->args [sig->hasthis + 0], TRUE);
841 add_general (&gr, &stack_size, &cinfo->ret, TRUE);
842 cinfo->vret_arg_index = 1;
846 add_general (&gr, &stack_size, cinfo->args + 0, TRUE);
850 if (cinfo->vtype_retaddr)
851 add_general (&gr, &stack_size, &cinfo->ret, TRUE);
854 DEBUG(printf("params: %d\n", sig->param_count));
855 for (i = pstart; i < sig->param_count; ++i) {
856 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
857 /* Prevent implicit arguments and sig_cookie from
858 being passed in registers */
860 /* Emit the signature cookie just before the implicit arguments */
861 add_general (&gr, &stack_size, &cinfo->sig_cookie, TRUE);
863 DEBUG(printf("param %d: ", i));
864 if (sig->params [i]->byref) {
865 DEBUG(printf("byref\n"));
866 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
870 simpletype = mini_type_get_underlying_type (NULL, sig->params [i]);
871 switch (simpletype->type) {
872 case MONO_TYPE_BOOLEAN:
875 cinfo->args [n].size = 1;
876 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
882 cinfo->args [n].size = 2;
883 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
888 cinfo->args [n].size = 4;
889 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
895 case MONO_TYPE_FNPTR:
896 case MONO_TYPE_CLASS:
897 case MONO_TYPE_OBJECT:
898 case MONO_TYPE_STRING:
899 case MONO_TYPE_SZARRAY:
900 case MONO_TYPE_ARRAY:
902 cinfo->args [n].size = sizeof (gpointer);
903 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
906 case MONO_TYPE_GENERICINST:
907 if (!mono_type_generic_inst_is_valuetype (simpletype)) {
908 cinfo->args [n].size = sizeof (gpointer);
909 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
914 case MONO_TYPE_TYPEDBYREF:
915 case MONO_TYPE_VALUETYPE: {
921 if (simpletype->type == MONO_TYPE_TYPEDBYREF) {
922 size = sizeof (MonoTypedRef);
923 align = sizeof (gpointer);
925 MonoClass *klass = mono_class_from_mono_type (sig->params [i]);
927 size = mono_class_native_size (klass, &align);
929 size = mono_class_value_size (klass, &align);
931 DEBUG(printf ("load %d bytes struct\n",
932 mono_class_native_size (sig->params [i]->data.klass, NULL)));
935 align_size += (sizeof (gpointer) - 1);
936 align_size &= ~(sizeof (gpointer) - 1);
937 nwords = (align_size + sizeof (gpointer) -1 ) / sizeof (gpointer);
938 cinfo->args [n].storage = RegTypeStructByVal;
939 cinfo->args [n].struct_size = size;
940 /* FIXME: align stack_size if needed */
941 if (eabi_supported) {
942 if (align >= 8 && (gr & 1))
945 if (gr > ARMREG_R3) {
946 cinfo->args [n].size = 0;
947 cinfo->args [n].vtsize = nwords;
949 int rest = ARMREG_R3 - gr + 1;
950 int n_in_regs = rest >= nwords? nwords: rest;
952 cinfo->args [n].size = n_in_regs;
953 cinfo->args [n].vtsize = nwords - n_in_regs;
954 cinfo->args [n].reg = gr;
958 cinfo->args [n].offset = stack_size;
959 /*g_print ("offset for arg %d at %d\n", n, stack_size);*/
960 stack_size += nwords * sizeof (gpointer);
967 cinfo->args [n].size = 8;
968 add_general (&gr, &stack_size, cinfo->args + n, FALSE);
972 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
976 /* Handle the case where there are no implicit arguments */
977 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
978 /* Prevent implicit arguments and sig_cookie from
979 being passed in registers */
981 /* Emit the signature cookie just before the implicit arguments */
982 add_general (&gr, &stack_size, &cinfo->sig_cookie, TRUE);
986 simpletype = mini_type_get_underlying_type (NULL, sig->ret);
987 switch (simpletype->type) {
988 case MONO_TYPE_BOOLEAN:
999 case MONO_TYPE_FNPTR:
1000 case MONO_TYPE_CLASS:
1001 case MONO_TYPE_OBJECT:
1002 case MONO_TYPE_SZARRAY:
1003 case MONO_TYPE_ARRAY:
1004 case MONO_TYPE_STRING:
1005 cinfo->ret.storage = RegTypeGeneral;
1006 cinfo->ret.reg = ARMREG_R0;
1010 cinfo->ret.storage = RegTypeIRegPair;
1011 cinfo->ret.reg = ARMREG_R0;
1015 cinfo->ret.storage = RegTypeFP;
1016 cinfo->ret.reg = ARMREG_R0;
1017 /* FIXME: cinfo->ret.reg = ???;
1018 cinfo->ret.storage = RegTypeFP;*/
1020 case MONO_TYPE_GENERICINST:
1021 if (!mono_type_generic_inst_is_valuetype (simpletype)) {
1022 cinfo->ret.storage = RegTypeGeneral;
1023 cinfo->ret.reg = ARMREG_R0;
1027 case MONO_TYPE_VALUETYPE:
1028 case MONO_TYPE_TYPEDBYREF:
1029 if (cinfo->ret.storage != RegTypeStructByVal)
1030 cinfo->ret.storage = RegTypeStructByAddr;
1032 case MONO_TYPE_VOID:
1035 g_error ("Can't handle as return value 0x%x", sig->ret->type);
1039 /* align stack size to 8 */
1040 DEBUG (printf (" stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
1041 stack_size = (stack_size + 7) & ~7;
1043 cinfo->stack_usage = stack_size;
1049 G_GNUC_UNUSED static void
1054 G_GNUC_UNUSED static gboolean
1057 static int count = 0;
1060 if (!getenv ("COUNT"))
1063 if (count == atoi (getenv ("COUNT"))) {
1067 if (count > atoi (getenv ("COUNT"))) {
1075 debug_omit_fp (void)
1078 return debug_count ();
1085 * mono_arch_compute_omit_fp:
1087 * Determine whenever the frame pointer can be eliminated.
1090 mono_arch_compute_omit_fp (MonoCompile *cfg)
1092 MonoMethodSignature *sig;
1093 MonoMethodHeader *header;
1097 if (cfg->arch.omit_fp_computed)
1100 header = cfg->header;
1102 sig = mono_method_signature (cfg->method);
1104 if (!cfg->arch.cinfo)
1105 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1106 cinfo = cfg->arch.cinfo;
1109 * FIXME: Remove some of the restrictions.
1111 cfg->arch.omit_fp = TRUE;
1112 cfg->arch.omit_fp_computed = TRUE;
1114 if (cfg->disable_omit_fp)
1115 cfg->arch.omit_fp = FALSE;
1116 if (!debug_omit_fp ())
1117 cfg->arch.omit_fp = FALSE;
1119 if (cfg->method->save_lmf)
1120 cfg->arch.omit_fp = FALSE;
1122 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1123 cfg->arch.omit_fp = FALSE;
1124 if (header->num_clauses)
1125 cfg->arch.omit_fp = FALSE;
1126 if (cfg->param_area)
1127 cfg->arch.omit_fp = FALSE;
1128 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1129 cfg->arch.omit_fp = FALSE;
1130 if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
1131 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
1132 cfg->arch.omit_fp = FALSE;
1133 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1134 ArgInfo *ainfo = &cinfo->args [i];
1136 if (ainfo->storage == RegTypeBase || ainfo->storage == RegTypeBaseGen || ainfo->storage == RegTypeStructByVal) {
1138 * The stack offset can only be determined when the frame
1141 cfg->arch.omit_fp = FALSE;
1146 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1147 MonoInst *ins = cfg->varinfo [i];
1150 locals_size += mono_type_size (ins->inst_vtype, &ialign);
1155 * Set var information according to the calling convention. arm version.
1156 * The locals var stuff should most likely be split in another method.
1159 mono_arch_allocate_vars (MonoCompile *cfg)
1161 MonoMethodSignature *sig;
1162 MonoMethodHeader *header;
1164 int i, offset, size, align, curinst;
1168 sig = mono_method_signature (cfg->method);
1170 if (!cfg->arch.cinfo)
1171 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1172 cinfo = cfg->arch.cinfo;
1174 mono_arch_compute_omit_fp (cfg);
1176 if (cfg->arch.omit_fp)
1177 cfg->frame_reg = ARMREG_SP;
1179 cfg->frame_reg = ARMREG_FP;
1181 cfg->flags |= MONO_CFG_HAS_SPILLUP;
1183 /* allow room for the vararg method args: void* and long/double */
1184 if (mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method))
1185 cfg->param_area = MAX (cfg->param_area, sizeof (gpointer)*8);
1187 header = cfg->header;
1189 /* See mono_arch_get_global_int_regs () */
1190 if (cfg->flags & MONO_CFG_HAS_CALLS)
1191 cfg->uses_rgctx_reg = TRUE;
1193 if (cfg->frame_reg != ARMREG_SP)
1194 cfg->used_int_regs |= 1 << cfg->frame_reg;
1196 if (cfg->compile_aot || cfg->uses_rgctx_reg || COMPILE_LLVM (cfg))
1197 /* V5 is reserved for passing the vtable/rgctx/IMT method */
1198 cfg->used_int_regs |= (1 << ARMREG_V5);
1202 if (!MONO_TYPE_ISSTRUCT (sig->ret)) {
1203 switch (mini_type_get_underlying_type (NULL, sig->ret)->type) {
1204 case MONO_TYPE_VOID:
1207 cfg->ret->opcode = OP_REGVAR;
1208 cfg->ret->inst_c0 = ARMREG_R0;
1212 /* local vars are at a positive offset from the stack pointer */
1214 * also note that if the function uses alloca, we use FP
1215 * to point at the local variables.
1217 offset = 0; /* linkage area */
1218 /* align the offset to 16 bytes: not sure this is needed here */
1220 //offset &= ~(8 - 1);
1222 /* add parameter area size for called functions */
1223 offset += cfg->param_area;
1226 if (cfg->flags & MONO_CFG_HAS_FPOUT)
1229 /* allow room to save the return value */
1230 if (mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method))
1233 /* the MonoLMF structure is stored just below the stack pointer */
1234 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
1235 if (cinfo->ret.storage == RegTypeStructByVal) {
1236 cfg->ret->opcode = OP_REGOFFSET;
1237 cfg->ret->inst_basereg = cfg->frame_reg;
1238 offset += sizeof (gpointer) - 1;
1239 offset &= ~(sizeof (gpointer) - 1);
1240 cfg->ret->inst_offset = - offset;
1242 ins = cfg->vret_addr;
1243 offset += sizeof(gpointer) - 1;
1244 offset &= ~(sizeof(gpointer) - 1);
1245 ins->inst_offset = offset;
1246 ins->opcode = OP_REGOFFSET;
1247 ins->inst_basereg = cfg->frame_reg;
1248 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1249 printf ("vret_addr =");
1250 mono_print_ins (cfg->vret_addr);
1253 offset += sizeof(gpointer);
1256 /* Allocate these first so they have a small offset, OP_SEQ_POINT depends on this */
1257 if (cfg->arch.seq_point_info_var) {
1260 ins = cfg->arch.seq_point_info_var;
1264 offset += align - 1;
1265 offset &= ~(align - 1);
1266 ins->opcode = OP_REGOFFSET;
1267 ins->inst_basereg = cfg->frame_reg;
1268 ins->inst_offset = offset;
1271 ins = cfg->arch.ss_trigger_page_var;
1274 offset += align - 1;
1275 offset &= ~(align - 1);
1276 ins->opcode = OP_REGOFFSET;
1277 ins->inst_basereg = cfg->frame_reg;
1278 ins->inst_offset = offset;
1282 curinst = cfg->locals_start;
1283 for (i = curinst; i < cfg->num_varinfo; ++i) {
1284 ins = cfg->varinfo [i];
1285 if ((ins->flags & MONO_INST_IS_DEAD) || ins->opcode == OP_REGVAR || ins->opcode == OP_REGOFFSET)
1288 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
1289 * pinvoke wrappers when they call functions returning structure */
1290 if (ins->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (ins->inst_vtype) && ins->inst_vtype->type != MONO_TYPE_TYPEDBYREF) {
1291 size = mono_class_native_size (mono_class_from_mono_type (ins->inst_vtype), &ualign);
1295 size = mono_type_size (ins->inst_vtype, &align);
1297 /* FIXME: if a structure is misaligned, our memcpy doesn't work,
1298 * since it loads/stores misaligned words, which don't do the right thing.
1300 if (align < 4 && size >= 4)
1302 offset += align - 1;
1303 offset &= ~(align - 1);
1304 ins->opcode = OP_REGOFFSET;
1305 ins->inst_offset = offset;
1306 ins->inst_basereg = cfg->frame_reg;
1308 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
1313 ins = cfg->args [curinst];
1314 if (ins->opcode != OP_REGVAR) {
1315 ins->opcode = OP_REGOFFSET;
1316 ins->inst_basereg = cfg->frame_reg;
1317 offset += sizeof (gpointer) - 1;
1318 offset &= ~(sizeof (gpointer) - 1);
1319 ins->inst_offset = offset;
1320 offset += sizeof (gpointer);
1325 if (sig->call_convention == MONO_CALL_VARARG) {
1329 /* Allocate a local slot to hold the sig cookie address */
1330 offset += align - 1;
1331 offset &= ~(align - 1);
1332 cfg->sig_cookie = offset;
1336 for (i = 0; i < sig->param_count; ++i) {
1337 ins = cfg->args [curinst];
1339 if (ins->opcode != OP_REGVAR) {
1340 ins->opcode = OP_REGOFFSET;
1341 ins->inst_basereg = cfg->frame_reg;
1342 size = mini_type_stack_size_full (NULL, sig->params [i], &ualign, sig->pinvoke);
1344 /* FIXME: if a structure is misaligned, our memcpy doesn't work,
1345 * since it loads/stores misaligned words, which don't do the right thing.
1347 if (align < 4 && size >= 4)
1349 /* The code in the prolog () stores words when storing vtypes received in a register */
1350 if (MONO_TYPE_ISSTRUCT (sig->params [i]))
1352 offset += align - 1;
1353 offset &= ~(align - 1);
1354 ins->inst_offset = offset;
1360 /* align the offset to 8 bytes */
1365 cfg->stack_offset = offset;
1369 mono_arch_create_vars (MonoCompile *cfg)
1371 MonoMethodSignature *sig;
1374 sig = mono_method_signature (cfg->method);
1376 if (!cfg->arch.cinfo)
1377 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1378 cinfo = cfg->arch.cinfo;
1380 if (cinfo->ret.storage == RegTypeStructByVal)
1381 cfg->ret_var_is_local = TRUE;
1383 if (MONO_TYPE_ISSTRUCT (sig->ret) && cinfo->ret.storage != RegTypeStructByVal) {
1384 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
1385 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1386 printf ("vret_addr = ");
1387 mono_print_ins (cfg->vret_addr);
1391 if (cfg->gen_seq_points && cfg->compile_aot) {
1392 MonoInst *ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1393 ins->flags |= MONO_INST_VOLATILE;
1394 cfg->arch.seq_point_info_var = ins;
1396 /* Allocate a separate variable for this to save 1 load per seq point */
1397 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1398 ins->flags |= MONO_INST_VOLATILE;
1399 cfg->arch.ss_trigger_page_var = ins;
1404 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1406 MonoMethodSignature *tmp_sig;
1409 if (call->tail_call)
1412 /* FIXME: Add support for signature tokens to AOT */
1413 cfg->disable_aot = TRUE;
1415 g_assert (cinfo->sig_cookie.storage == RegTypeBase);
1418 * mono_ArgIterator_Setup assumes the signature cookie is
1419 * passed first and all the arguments which were before it are
1420 * passed on the stack after the signature. So compensate by
1421 * passing a different signature.
1423 tmp_sig = mono_metadata_signature_dup (call->signature);
1424 tmp_sig->param_count -= call->signature->sentinelpos;
1425 tmp_sig->sentinelpos = 0;
1426 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1428 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
1429 sig_arg->dreg = mono_alloc_ireg (cfg);
1430 sig_arg->inst_p0 = tmp_sig;
1431 MONO_ADD_INS (cfg->cbb, sig_arg);
1433 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, cinfo->sig_cookie.offset, sig_arg->dreg);
1438 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
1443 LLVMCallInfo *linfo;
1445 n = sig->param_count + sig->hasthis;
1447 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1449 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
1452 * LLVM always uses the native ABI while we use our own ABI, the
1453 * only difference is the handling of vtypes:
1454 * - we only pass/receive them in registers in some cases, and only
1455 * in 1 or 2 integer registers.
1457 if (cinfo->vtype_retaddr) {
1458 /* Vtype returned using a hidden argument */
1459 linfo->ret.storage = LLVMArgVtypeRetAddr;
1460 linfo->vret_arg_index = cinfo->vret_arg_index;
1461 } else if (cinfo->ret.storage != RegTypeGeneral && cinfo->ret.storage != RegTypeNone && cinfo->ret.storage != RegTypeFP && cinfo->ret.storage != RegTypeIRegPair) {
1462 cfg->exception_message = g_strdup ("unknown ret conv");
1463 cfg->disable_llvm = TRUE;
1467 for (i = 0; i < n; ++i) {
1468 ainfo = cinfo->args + i;
1470 linfo->args [i].storage = LLVMArgNone;
1472 switch (ainfo->storage) {
1473 case RegTypeGeneral:
1474 case RegTypeIRegPair:
1476 linfo->args [i].storage = LLVMArgInIReg;
1478 case RegTypeStructByVal:
1479 // FIXME: Passing entirely on the stack or split reg/stack
1480 if (ainfo->vtsize == 0 && ainfo->size <= 2) {
1481 linfo->args [i].storage = LLVMArgVtypeInReg;
1482 linfo->args [i].pair_storage [0] = LLVMArgInIReg;
1483 if (ainfo->size == 2)
1484 linfo->args [i].pair_storage [1] = LLVMArgInIReg;
1486 linfo->args [i].pair_storage [1] = LLVMArgNone;
1488 cfg->exception_message = g_strdup_printf ("vtype-by-val on stack");
1489 cfg->disable_llvm = TRUE;
1493 cfg->exception_message = g_strdup_printf ("ainfo->storage (%d)", ainfo->storage);
1494 cfg->disable_llvm = TRUE;
1504 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
1507 MonoMethodSignature *sig;
1511 sig = call->signature;
1512 n = sig->param_count + sig->hasthis;
1514 cinfo = get_call_info (cfg->generic_sharing_context, NULL, sig);
1516 for (i = 0; i < n; ++i) {
1517 ArgInfo *ainfo = cinfo->args + i;
1520 if (i >= sig->hasthis)
1521 t = sig->params [i - sig->hasthis];
1523 t = &mono_defaults.int_class->byval_arg;
1524 t = mini_type_get_underlying_type (NULL, t);
1526 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1527 /* Emit the signature cookie just before the implicit arguments */
1528 emit_sig_cookie (cfg, call, cinfo);
1531 in = call->args [i];
1533 switch (ainfo->storage) {
1534 case RegTypeGeneral:
1535 case RegTypeIRegPair:
1536 if (!t->byref && ((t->type == MONO_TYPE_I8) || (t->type == MONO_TYPE_U8))) {
1537 MONO_INST_NEW (cfg, ins, OP_MOVE);
1538 ins->dreg = mono_alloc_ireg (cfg);
1539 ins->sreg1 = in->dreg + 1;
1540 MONO_ADD_INS (cfg->cbb, ins);
1541 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1543 MONO_INST_NEW (cfg, ins, OP_MOVE);
1544 ins->dreg = mono_alloc_ireg (cfg);
1545 ins->sreg1 = in->dreg + 2;
1546 MONO_ADD_INS (cfg->cbb, ins);
1547 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg + 1, FALSE);
1548 } else if (!t->byref && ((t->type == MONO_TYPE_R8) || (t->type == MONO_TYPE_R4))) {
1549 #ifndef MONO_ARCH_SOFT_FLOAT
1553 if (ainfo->size == 4) {
1554 #ifdef MONO_ARCH_SOFT_FLOAT
1555 /* mono_emit_call_args () have already done the r8->r4 conversion */
1556 /* The converted value is in an int vreg */
1557 MONO_INST_NEW (cfg, ins, OP_MOVE);
1558 ins->dreg = mono_alloc_ireg (cfg);
1559 ins->sreg1 = in->dreg;
1560 MONO_ADD_INS (cfg->cbb, ins);
1561 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1563 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, ARMREG_SP, (cfg->param_area - 8), in->dreg);
1564 creg = mono_alloc_ireg (cfg);
1565 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8));
1566 mono_call_inst_add_outarg_reg (cfg, call, creg, ainfo->reg, FALSE);
1569 #ifdef MONO_ARCH_SOFT_FLOAT
1570 MONO_INST_NEW (cfg, ins, OP_FGETLOW32);
1571 ins->dreg = mono_alloc_ireg (cfg);
1572 ins->sreg1 = in->dreg;
1573 MONO_ADD_INS (cfg->cbb, ins);
1574 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1576 MONO_INST_NEW (cfg, ins, OP_FGETHIGH32);
1577 ins->dreg = mono_alloc_ireg (cfg);
1578 ins->sreg1 = in->dreg;
1579 MONO_ADD_INS (cfg->cbb, ins);
1580 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg + 1, FALSE);
1582 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, ARMREG_SP, (cfg->param_area - 8), in->dreg);
1583 creg = mono_alloc_ireg (cfg);
1584 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8));
1585 mono_call_inst_add_outarg_reg (cfg, call, creg, ainfo->reg, FALSE);
1586 creg = mono_alloc_ireg (cfg);
1587 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8 + 4));
1588 mono_call_inst_add_outarg_reg (cfg, call, creg, ainfo->reg + 1, FALSE);
1591 cfg->flags |= MONO_CFG_HAS_FPOUT;
1593 MONO_INST_NEW (cfg, ins, OP_MOVE);
1594 ins->dreg = mono_alloc_ireg (cfg);
1595 ins->sreg1 = in->dreg;
1596 MONO_ADD_INS (cfg->cbb, ins);
1598 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1601 case RegTypeStructByAddr:
1604 /* FIXME: where si the data allocated? */
1605 arg->backend.reg3 = ainfo->reg;
1606 call->used_iregs |= 1 << ainfo->reg;
1607 g_assert_not_reached ();
1610 case RegTypeStructByVal:
1611 MONO_INST_NEW (cfg, ins, OP_OUTARG_VT);
1612 ins->opcode = OP_OUTARG_VT;
1613 ins->sreg1 = in->dreg;
1614 ins->klass = in->klass;
1615 ins->inst_p0 = call;
1616 ins->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
1617 memcpy (ins->inst_p1, ainfo, sizeof (ArgInfo));
1618 mono_call_inst_add_outarg_vt (cfg, call, ins);
1619 MONO_ADD_INS (cfg->cbb, ins);
1622 if (!t->byref && ((t->type == MONO_TYPE_I8) || (t->type == MONO_TYPE_U8))) {
1623 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1624 } else if (!t->byref && ((t->type == MONO_TYPE_R4) || (t->type == MONO_TYPE_R8))) {
1625 if (t->type == MONO_TYPE_R8) {
1626 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1628 #ifdef MONO_ARCH_SOFT_FLOAT
1629 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1631 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1635 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1638 case RegTypeBaseGen:
1639 if (!t->byref && ((t->type == MONO_TYPE_I8) || (t->type == MONO_TYPE_U8))) {
1640 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);
1641 MONO_INST_NEW (cfg, ins, OP_MOVE);
1642 ins->dreg = mono_alloc_ireg (cfg);
1643 ins->sreg1 = G_BYTE_ORDER == G_BIG_ENDIAN ? in->dreg + 2 : in->dreg + 1;
1644 MONO_ADD_INS (cfg->cbb, ins);
1645 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ARMREG_R3, FALSE);
1646 } else if (!t->byref && (t->type == MONO_TYPE_R8)) {
1649 /* This should work for soft-float as well */
1651 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, ARMREG_SP, (cfg->param_area - 8), in->dreg);
1652 creg = mono_alloc_ireg (cfg);
1653 mono_call_inst_add_outarg_reg (cfg, call, creg, ARMREG_R3, FALSE);
1654 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8));
1655 creg = mono_alloc_ireg (cfg);
1656 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 4));
1657 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, ainfo->offset, creg);
1658 cfg->flags |= MONO_CFG_HAS_FPOUT;
1660 g_assert_not_reached ();
1667 arg->backend.reg3 = ainfo->reg;
1668 /* FP args are passed in int regs */
1669 call->used_iregs |= 1 << ainfo->reg;
1670 if (ainfo->size == 8) {
1671 arg->opcode = OP_OUTARG_R8;
1672 call->used_iregs |= 1 << (ainfo->reg + 1);
1674 arg->opcode = OP_OUTARG_R4;
1677 cfg->flags |= MONO_CFG_HAS_FPOUT;
1681 g_assert_not_reached ();
1685 /* Handle the case where there are no implicit arguments */
1686 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos))
1687 emit_sig_cookie (cfg, call, cinfo);
1689 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
1692 if (cinfo->ret.storage == RegTypeStructByVal) {
1693 /* The JIT will transform this into a normal call */
1694 call->vret_in_reg = TRUE;
1696 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
1697 vtarg->sreg1 = call->vret_var->dreg;
1698 vtarg->dreg = mono_alloc_preg (cfg);
1699 MONO_ADD_INS (cfg->cbb, vtarg);
1701 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
1705 call->stack_usage = cinfo->stack_usage;
1711 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
1713 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
1714 ArgInfo *ainfo = ins->inst_p1;
1715 int ovf_size = ainfo->vtsize;
1716 int doffset = ainfo->offset;
1717 int struct_size = ainfo->struct_size;
1718 int i, soffset, dreg, tmpreg;
1721 for (i = 0; i < ainfo->size; ++i) {
1722 dreg = mono_alloc_ireg (cfg);
1723 switch (struct_size) {
1725 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, dreg, src->dreg, soffset);
1728 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, dreg, src->dreg, soffset);
1731 tmpreg = mono_alloc_ireg (cfg);
1732 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, dreg, src->dreg, soffset);
1733 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, tmpreg, src->dreg, soffset + 1);
1734 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, tmpreg, tmpreg, 8);
1735 MONO_EMIT_NEW_BIALU (cfg, OP_IOR, dreg, dreg, tmpreg);
1736 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, tmpreg, src->dreg, soffset + 2);
1737 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, tmpreg, tmpreg, 16);
1738 MONO_EMIT_NEW_BIALU (cfg, OP_IOR, dreg, dreg, tmpreg);
1741 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, soffset);
1744 mono_call_inst_add_outarg_reg (cfg, call, dreg, ainfo->reg + i, FALSE);
1745 soffset += sizeof (gpointer);
1746 struct_size -= sizeof (gpointer);
1748 //g_print ("vt size: %d at R%d + %d\n", doffset, vt->inst_basereg, vt->inst_offset);
1750 mini_emit_memcpy (cfg, ARMREG_SP, doffset, src->dreg, soffset, MIN (ovf_size * sizeof (gpointer), struct_size), struct_size < 4 ? 1 : 4);
1754 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
1756 MonoType *ret = mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret);
1759 if (ret->type == MONO_TYPE_I8 || ret->type == MONO_TYPE_U8) {
1762 if (COMPILE_LLVM (cfg)) {
1763 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
1765 MONO_INST_NEW (cfg, ins, OP_SETLRET);
1766 ins->sreg1 = val->dreg + 1;
1767 ins->sreg2 = val->dreg + 2;
1768 MONO_ADD_INS (cfg->cbb, ins);
1772 #ifdef MONO_ARCH_SOFT_FLOAT
1773 if (ret->type == MONO_TYPE_R8) {
1776 MONO_INST_NEW (cfg, ins, OP_SETFRET);
1777 ins->dreg = cfg->ret->dreg;
1778 ins->sreg1 = val->dreg;
1779 MONO_ADD_INS (cfg->cbb, ins);
1782 if (ret->type == MONO_TYPE_R4) {
1783 /* Already converted to an int in method_to_ir () */
1784 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
1787 #elif defined(ARM_FPU_VFP)
1788 if (ret->type == MONO_TYPE_R8 || ret->type == MONO_TYPE_R4) {
1791 MONO_INST_NEW (cfg, ins, OP_SETFRET);
1792 ins->dreg = cfg->ret->dreg;
1793 ins->sreg1 = val->dreg;
1794 MONO_ADD_INS (cfg->cbb, ins);
1798 if (ret->type == MONO_TYPE_R4 || ret->type == MONO_TYPE_R8) {
1799 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
1806 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
1809 #endif /* #ifndef DISABLE_JIT */
1812 mono_arch_is_inst_imm (gint64 imm)
1817 #define DYN_CALL_STACK_ARGS 6
1820 MonoMethodSignature *sig;
1825 mgreg_t regs [PARAM_REGS + DYN_CALL_STACK_ARGS];
1831 dyn_call_supported (CallInfo *cinfo, MonoMethodSignature *sig)
1835 if (sig->hasthis + sig->param_count > PARAM_REGS + DYN_CALL_STACK_ARGS)
1838 switch (cinfo->ret.storage) {
1840 case RegTypeGeneral:
1841 case RegTypeIRegPair:
1842 case RegTypeStructByAddr:
1847 #elif defined(ARM_FPU_VFP)
1856 for (i = 0; i < cinfo->nargs; ++i) {
1857 switch (cinfo->args [i].storage) {
1858 case RegTypeGeneral:
1860 case RegTypeIRegPair:
1863 if (cinfo->args [i].offset >= (DYN_CALL_STACK_ARGS * sizeof (gpointer)))
1866 case RegTypeStructByVal:
1867 if (cinfo->args [i].reg + cinfo->args [i].vtsize >= PARAM_REGS + DYN_CALL_STACK_ARGS)
1875 // FIXME: Can't use cinfo only as it doesn't contain info about I8/float */
1876 for (i = 0; i < sig->param_count; ++i) {
1877 MonoType *t = sig->params [i];
1885 #ifdef MONO_ARCH_SOFT_FLOAT
1904 mono_arch_dyn_call_prepare (MonoMethodSignature *sig)
1906 ArchDynCallInfo *info;
1909 cinfo = get_call_info (NULL, NULL, sig);
1911 if (!dyn_call_supported (cinfo, sig)) {
1916 info = g_new0 (ArchDynCallInfo, 1);
1917 // FIXME: Preprocess the info to speed up start_dyn_call ()
1919 info->cinfo = cinfo;
1921 return (MonoDynCallInfo*)info;
1925 mono_arch_dyn_call_free (MonoDynCallInfo *info)
1927 ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
1929 g_free (ainfo->cinfo);
1934 mono_arch_start_dyn_call (MonoDynCallInfo *info, gpointer **args, guint8 *ret, guint8 *buf, int buf_len)
1936 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
1937 DynCallArgs *p = (DynCallArgs*)buf;
1938 int arg_index, greg, i, j, pindex;
1939 MonoMethodSignature *sig = dinfo->sig;
1941 g_assert (buf_len >= sizeof (DynCallArgs));
1950 if (sig->hasthis || dinfo->cinfo->vret_arg_index == 1) {
1951 p->regs [greg ++] = (mgreg_t)*(args [arg_index ++]);
1956 if (dinfo->cinfo->vtype_retaddr)
1957 p->regs [greg ++] = (mgreg_t)ret;
1959 for (i = pindex; i < sig->param_count; i++) {
1960 MonoType *t = mono_type_get_underlying_type (sig->params [i]);
1961 gpointer *arg = args [arg_index ++];
1962 ArgInfo *ainfo = &dinfo->cinfo->args [i + sig->hasthis];
1965 if (ainfo->storage == RegTypeGeneral || ainfo->storage == RegTypeIRegPair || ainfo->storage == RegTypeStructByVal)
1967 else if (ainfo->storage == RegTypeBase)
1968 slot = PARAM_REGS + (ainfo->offset / 4);
1970 g_assert_not_reached ();
1973 p->regs [slot] = (mgreg_t)*arg;
1978 case MONO_TYPE_STRING:
1979 case MONO_TYPE_CLASS:
1980 case MONO_TYPE_ARRAY:
1981 case MONO_TYPE_SZARRAY:
1982 case MONO_TYPE_OBJECT:
1986 p->regs [slot] = (mgreg_t)*arg;
1988 case MONO_TYPE_BOOLEAN:
1990 p->regs [slot] = *(guint8*)arg;
1993 p->regs [slot] = *(gint8*)arg;
1996 p->regs [slot] = *(gint16*)arg;
1999 case MONO_TYPE_CHAR:
2000 p->regs [slot] = *(guint16*)arg;
2003 p->regs [slot] = *(gint32*)arg;
2006 p->regs [slot] = *(guint32*)arg;
2010 p->regs [slot ++] = (mgreg_t)arg [0];
2011 p->regs [slot] = (mgreg_t)arg [1];
2014 p->regs [slot] = *(mgreg_t*)arg;
2017 p->regs [slot ++] = (mgreg_t)arg [0];
2018 p->regs [slot] = (mgreg_t)arg [1];
2020 case MONO_TYPE_GENERICINST:
2021 if (MONO_TYPE_IS_REFERENCE (t)) {
2022 p->regs [slot] = (mgreg_t)*arg;
2027 case MONO_TYPE_VALUETYPE:
2028 g_assert (ainfo->storage == RegTypeStructByVal);
2030 if (ainfo->size == 0)
2031 slot = PARAM_REGS + (ainfo->offset / 4);
2035 for (j = 0; j < ainfo->size + ainfo->vtsize; ++j)
2036 p->regs [slot ++] = ((mgreg_t*)arg) [j];
2039 g_assert_not_reached ();
2045 mono_arch_finish_dyn_call (MonoDynCallInfo *info, guint8 *buf)
2047 ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
2048 MonoMethodSignature *sig = ((ArchDynCallInfo*)info)->sig;
2049 guint8 *ret = ((DynCallArgs*)buf)->ret;
2050 mgreg_t res = ((DynCallArgs*)buf)->res;
2051 mgreg_t res2 = ((DynCallArgs*)buf)->res2;
2053 switch (mono_type_get_underlying_type (sig->ret)->type) {
2054 case MONO_TYPE_VOID:
2055 *(gpointer*)ret = NULL;
2057 case MONO_TYPE_STRING:
2058 case MONO_TYPE_CLASS:
2059 case MONO_TYPE_ARRAY:
2060 case MONO_TYPE_SZARRAY:
2061 case MONO_TYPE_OBJECT:
2065 *(gpointer*)ret = (gpointer)res;
2071 case MONO_TYPE_BOOLEAN:
2072 *(guint8*)ret = res;
2075 *(gint16*)ret = res;
2078 case MONO_TYPE_CHAR:
2079 *(guint16*)ret = res;
2082 *(gint32*)ret = res;
2085 *(guint32*)ret = res;
2089 /* This handles endianness as well */
2090 ((gint32*)ret) [0] = res;
2091 ((gint32*)ret) [1] = res2;
2093 case MONO_TYPE_GENERICINST:
2094 if (MONO_TYPE_IS_REFERENCE (sig->ret)) {
2095 *(gpointer*)ret = (gpointer)res;
2100 case MONO_TYPE_VALUETYPE:
2101 g_assert (ainfo->cinfo->vtype_retaddr);
2104 #if defined(ARM_FPU_VFP)
2106 *(float*)ret = *(float*)&res;
2108 case MONO_TYPE_R8: {
2114 *(double*)ret = *(double*)®s;
2119 g_assert_not_reached ();
2126 * Allow tracing to work with this interface (with an optional argument)
2130 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
2134 code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->method);
2135 ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0); /* NULL ebp for now */
2136 code = mono_arm_emit_load_imm (code, ARMREG_R2, (guint32)func);
2137 code = emit_call_reg (code, ARMREG_R2);
2150 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
2153 int save_mode = SAVE_NONE;
2155 MonoMethod *method = cfg->method;
2156 int rtype = mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret)->type;
2157 int save_offset = cfg->param_area;
2161 offset = code - cfg->native_code;
2162 /* we need about 16 instructions */
2163 if (offset > (cfg->code_size - 16 * 4)) {
2164 cfg->code_size *= 2;
2165 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2166 code = cfg->native_code + offset;
2169 case MONO_TYPE_VOID:
2170 /* special case string .ctor icall */
2171 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
2172 save_mode = SAVE_ONE;
2174 save_mode = SAVE_NONE;
2178 save_mode = SAVE_TWO;
2182 save_mode = SAVE_FP;
2184 case MONO_TYPE_VALUETYPE:
2185 save_mode = SAVE_STRUCT;
2188 save_mode = SAVE_ONE;
2192 switch (save_mode) {
2194 ARM_STR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2195 ARM_STR_IMM (code, ARMREG_R1, cfg->frame_reg, save_offset + 4);
2196 if (enable_arguments) {
2197 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_R1);
2198 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
2202 ARM_STR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2203 if (enable_arguments) {
2204 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
2208 /* FIXME: what reg? */
2209 if (enable_arguments) {
2210 /* FIXME: what reg? */
2214 if (enable_arguments) {
2215 /* FIXME: get the actual address */
2216 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
2224 code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->method);
2225 code = mono_arm_emit_load_imm (code, ARMREG_IP, (guint32)func);
2226 code = emit_call_reg (code, ARMREG_IP);
2228 switch (save_mode) {
2230 ARM_LDR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2231 ARM_LDR_IMM (code, ARMREG_R1, cfg->frame_reg, save_offset + 4);
2234 ARM_LDR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2248 * The immediate field for cond branches is big enough for all reasonable methods
2250 #define EMIT_COND_BRANCH_FLAGS(ins,condcode) \
2251 if (0 && ins->inst_true_bb->native_offset) { \
2252 ARM_B_COND (code, (condcode), (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffffff); \
2254 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
2255 ARM_B_COND (code, (condcode), 0); \
2258 #define EMIT_COND_BRANCH(ins,cond) EMIT_COND_BRANCH_FLAGS(ins, branch_cc_table [(cond)])
2260 /* emit an exception if condition is fail
2262 * We assign the extra code used to throw the implicit exceptions
2263 * to cfg->bb_exit as far as the big branch handling is concerned
2265 #define EMIT_COND_SYSTEM_EXCEPTION_FLAGS(condcode,exc_name) \
2267 mono_add_patch_info (cfg, code - cfg->native_code, \
2268 MONO_PATCH_INFO_EXC, exc_name); \
2269 ARM_BL_COND (code, (condcode), 0); \
2272 #define EMIT_COND_SYSTEM_EXCEPTION(cond,exc_name) EMIT_COND_SYSTEM_EXCEPTION_FLAGS(branch_cc_table [(cond)], (exc_name))
2275 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
2280 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
2282 MonoInst *ins, *n, *last_ins = NULL;
2284 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2285 switch (ins->opcode) {
2288 /* Already done by an arch-independent pass */
2290 case OP_LOAD_MEMBASE:
2291 case OP_LOADI4_MEMBASE:
2293 * OP_STORE_MEMBASE_REG reg, offset(basereg)
2294 * OP_LOAD_MEMBASE offset(basereg), reg
2296 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
2297 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
2298 ins->inst_basereg == last_ins->inst_destbasereg &&
2299 ins->inst_offset == last_ins->inst_offset) {
2300 if (ins->dreg == last_ins->sreg1) {
2301 MONO_DELETE_INS (bb, ins);
2304 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
2305 ins->opcode = OP_MOVE;
2306 ins->sreg1 = last_ins->sreg1;
2310 * Note: reg1 must be different from the basereg in the second load
2311 * OP_LOAD_MEMBASE offset(basereg), reg1
2312 * OP_LOAD_MEMBASE offset(basereg), reg2
2314 * OP_LOAD_MEMBASE offset(basereg), reg1
2315 * OP_MOVE reg1, reg2
2317 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
2318 || last_ins->opcode == OP_LOAD_MEMBASE) &&
2319 ins->inst_basereg != last_ins->dreg &&
2320 ins->inst_basereg == last_ins->inst_basereg &&
2321 ins->inst_offset == last_ins->inst_offset) {
2323 if (ins->dreg == last_ins->dreg) {
2324 MONO_DELETE_INS (bb, ins);
2327 ins->opcode = OP_MOVE;
2328 ins->sreg1 = last_ins->dreg;
2331 //g_assert_not_reached ();
2335 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2336 * OP_LOAD_MEMBASE offset(basereg), reg
2338 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2339 * OP_ICONST reg, imm
2341 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
2342 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
2343 ins->inst_basereg == last_ins->inst_destbasereg &&
2344 ins->inst_offset == last_ins->inst_offset) {
2345 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
2346 ins->opcode = OP_ICONST;
2347 ins->inst_c0 = last_ins->inst_imm;
2348 g_assert_not_reached (); // check this rule
2352 case OP_LOADU1_MEMBASE:
2353 case OP_LOADI1_MEMBASE:
2354 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
2355 ins->inst_basereg == last_ins->inst_destbasereg &&
2356 ins->inst_offset == last_ins->inst_offset) {
2357 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? OP_ICONV_TO_I1 : OP_ICONV_TO_U1;
2358 ins->sreg1 = last_ins->sreg1;
2361 case OP_LOADU2_MEMBASE:
2362 case OP_LOADI2_MEMBASE:
2363 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
2364 ins->inst_basereg == last_ins->inst_destbasereg &&
2365 ins->inst_offset == last_ins->inst_offset) {
2366 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_ICONV_TO_I2 : OP_ICONV_TO_U2;
2367 ins->sreg1 = last_ins->sreg1;
2371 ins->opcode = OP_MOVE;
2375 if (ins->dreg == ins->sreg1) {
2376 MONO_DELETE_INS (bb, ins);
2380 * OP_MOVE sreg, dreg
2381 * OP_MOVE dreg, sreg
2383 if (last_ins && last_ins->opcode == OP_MOVE &&
2384 ins->sreg1 == last_ins->dreg &&
2385 ins->dreg == last_ins->sreg1) {
2386 MONO_DELETE_INS (bb, ins);
2394 bb->last_ins = last_ins;
2398 * the branch_cc_table should maintain the order of these
2412 branch_cc_table [] = {
2426 #define NEW_INS(cfg,dest,op) do { \
2427 MONO_INST_NEW ((cfg), (dest), (op)); \
2428 mono_bblock_insert_before_ins (bb, ins, (dest)); \
2432 map_to_reg_reg_op (int op)
2441 case OP_COMPARE_IMM:
2443 case OP_ICOMPARE_IMM:
2457 case OP_LOAD_MEMBASE:
2458 return OP_LOAD_MEMINDEX;
2459 case OP_LOADI4_MEMBASE:
2460 return OP_LOADI4_MEMINDEX;
2461 case OP_LOADU4_MEMBASE:
2462 return OP_LOADU4_MEMINDEX;
2463 case OP_LOADU1_MEMBASE:
2464 return OP_LOADU1_MEMINDEX;
2465 case OP_LOADI2_MEMBASE:
2466 return OP_LOADI2_MEMINDEX;
2467 case OP_LOADU2_MEMBASE:
2468 return OP_LOADU2_MEMINDEX;
2469 case OP_LOADI1_MEMBASE:
2470 return OP_LOADI1_MEMINDEX;
2471 case OP_STOREI1_MEMBASE_REG:
2472 return OP_STOREI1_MEMINDEX;
2473 case OP_STOREI2_MEMBASE_REG:
2474 return OP_STOREI2_MEMINDEX;
2475 case OP_STOREI4_MEMBASE_REG:
2476 return OP_STOREI4_MEMINDEX;
2477 case OP_STORE_MEMBASE_REG:
2478 return OP_STORE_MEMINDEX;
2479 case OP_STORER4_MEMBASE_REG:
2480 return OP_STORER4_MEMINDEX;
2481 case OP_STORER8_MEMBASE_REG:
2482 return OP_STORER8_MEMINDEX;
2483 case OP_STORE_MEMBASE_IMM:
2484 return OP_STORE_MEMBASE_REG;
2485 case OP_STOREI1_MEMBASE_IMM:
2486 return OP_STOREI1_MEMBASE_REG;
2487 case OP_STOREI2_MEMBASE_IMM:
2488 return OP_STOREI2_MEMBASE_REG;
2489 case OP_STOREI4_MEMBASE_IMM:
2490 return OP_STOREI4_MEMBASE_REG;
2492 g_assert_not_reached ();
2496 * Remove from the instruction list the instructions that can't be
2497 * represented with very simple instructions with no register
2501 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
2503 MonoInst *ins, *temp, *last_ins = NULL;
2504 int rot_amount, imm8, low_imm;
2506 MONO_BB_FOR_EACH_INS (bb, ins) {
2508 switch (ins->opcode) {
2512 case OP_COMPARE_IMM:
2513 case OP_ICOMPARE_IMM:
2527 if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount)) < 0) {
2528 NEW_INS (cfg, temp, OP_ICONST);
2529 temp->inst_c0 = ins->inst_imm;
2530 temp->dreg = mono_alloc_ireg (cfg);
2531 ins->sreg2 = temp->dreg;
2532 ins->opcode = mono_op_imm_to_op (ins->opcode);
2534 if (ins->opcode == OP_SBB || ins->opcode == OP_ISBB || ins->opcode == OP_SUBCC)
2540 if (ins->inst_imm == 1) {
2541 ins->opcode = OP_MOVE;
2544 if (ins->inst_imm == 0) {
2545 ins->opcode = OP_ICONST;
2549 imm8 = mono_is_power_of_two (ins->inst_imm);
2551 ins->opcode = OP_SHL_IMM;
2552 ins->inst_imm = imm8;
2555 NEW_INS (cfg, temp, OP_ICONST);
2556 temp->inst_c0 = ins->inst_imm;
2557 temp->dreg = mono_alloc_ireg (cfg);
2558 ins->sreg2 = temp->dreg;
2559 ins->opcode = OP_IMUL;
2565 if (ins->next && (ins->next->opcode == OP_COND_EXC_C || ins->next->opcode == OP_COND_EXC_IC))
2566 /* ARM sets the C flag to 1 if there was _no_ overflow */
2567 ins->next->opcode = OP_COND_EXC_NC;
2569 case OP_LOCALLOC_IMM:
2570 NEW_INS (cfg, temp, OP_ICONST);
2571 temp->inst_c0 = ins->inst_imm;
2572 temp->dreg = mono_alloc_ireg (cfg);
2573 ins->sreg1 = temp->dreg;
2574 ins->opcode = OP_LOCALLOC;
2576 case OP_LOAD_MEMBASE:
2577 case OP_LOADI4_MEMBASE:
2578 case OP_LOADU4_MEMBASE:
2579 case OP_LOADU1_MEMBASE:
2580 /* we can do two things: load the immed in a register
2581 * and use an indexed load, or see if the immed can be
2582 * represented as an ad_imm + a load with a smaller offset
2583 * that fits. We just do the first for now, optimize later.
2585 if (arm_is_imm12 (ins->inst_offset))
2587 NEW_INS (cfg, temp, OP_ICONST);
2588 temp->inst_c0 = ins->inst_offset;
2589 temp->dreg = mono_alloc_ireg (cfg);
2590 ins->sreg2 = temp->dreg;
2591 ins->opcode = map_to_reg_reg_op (ins->opcode);
2593 case OP_LOADI2_MEMBASE:
2594 case OP_LOADU2_MEMBASE:
2595 case OP_LOADI1_MEMBASE:
2596 if (arm_is_imm8 (ins->inst_offset))
2598 NEW_INS (cfg, temp, OP_ICONST);
2599 temp->inst_c0 = ins->inst_offset;
2600 temp->dreg = mono_alloc_ireg (cfg);
2601 ins->sreg2 = temp->dreg;
2602 ins->opcode = map_to_reg_reg_op (ins->opcode);
2604 case OP_LOADR4_MEMBASE:
2605 case OP_LOADR8_MEMBASE:
2606 if (arm_is_fpimm8 (ins->inst_offset))
2608 low_imm = ins->inst_offset & 0x1ff;
2609 if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_offset & ~0x1ff, &rot_amount)) >= 0) {
2610 NEW_INS (cfg, temp, OP_ADD_IMM);
2611 temp->inst_imm = ins->inst_offset & ~0x1ff;
2612 temp->sreg1 = ins->inst_basereg;
2613 temp->dreg = mono_alloc_ireg (cfg);
2614 ins->inst_basereg = temp->dreg;
2615 ins->inst_offset = low_imm;
2618 /* VFP/FPA doesn't have indexed load instructions */
2619 g_assert_not_reached ();
2621 case OP_STORE_MEMBASE_REG:
2622 case OP_STOREI4_MEMBASE_REG:
2623 case OP_STOREI1_MEMBASE_REG:
2624 if (arm_is_imm12 (ins->inst_offset))
2626 NEW_INS (cfg, temp, OP_ICONST);
2627 temp->inst_c0 = ins->inst_offset;
2628 temp->dreg = mono_alloc_ireg (cfg);
2629 ins->sreg2 = temp->dreg;
2630 ins->opcode = map_to_reg_reg_op (ins->opcode);
2632 case OP_STOREI2_MEMBASE_REG:
2633 if (arm_is_imm8 (ins->inst_offset))
2635 NEW_INS (cfg, temp, OP_ICONST);
2636 temp->inst_c0 = ins->inst_offset;
2637 temp->dreg = mono_alloc_ireg (cfg);
2638 ins->sreg2 = temp->dreg;
2639 ins->opcode = map_to_reg_reg_op (ins->opcode);
2641 case OP_STORER4_MEMBASE_REG:
2642 case OP_STORER8_MEMBASE_REG:
2643 if (arm_is_fpimm8 (ins->inst_offset))
2645 low_imm = ins->inst_offset & 0x1ff;
2646 if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_offset & ~ 0x1ff, &rot_amount)) >= 0 && arm_is_fpimm8 (low_imm)) {
2647 NEW_INS (cfg, temp, OP_ADD_IMM);
2648 temp->inst_imm = ins->inst_offset & ~0x1ff;
2649 temp->sreg1 = ins->inst_destbasereg;
2650 temp->dreg = mono_alloc_ireg (cfg);
2651 ins->inst_destbasereg = temp->dreg;
2652 ins->inst_offset = low_imm;
2655 /*g_print ("fail with: %d (%d, %d)\n", ins->inst_offset, ins->inst_offset & ~0x1ff, low_imm);*/
2656 /* VFP/FPA doesn't have indexed store instructions */
2657 g_assert_not_reached ();
2659 case OP_STORE_MEMBASE_IMM:
2660 case OP_STOREI1_MEMBASE_IMM:
2661 case OP_STOREI2_MEMBASE_IMM:
2662 case OP_STOREI4_MEMBASE_IMM:
2663 NEW_INS (cfg, temp, OP_ICONST);
2664 temp->inst_c0 = ins->inst_imm;
2665 temp->dreg = mono_alloc_ireg (cfg);
2666 ins->sreg1 = temp->dreg;
2667 ins->opcode = map_to_reg_reg_op (ins->opcode);
2669 goto loop_start; /* make it handle the possibly big ins->inst_offset */
2671 gboolean swap = FALSE;
2675 /* Optimized away */
2680 /* Some fp compares require swapped operands */
2681 switch (ins->next->opcode) {
2683 ins->next->opcode = OP_FBLT;
2687 ins->next->opcode = OP_FBLT_UN;
2691 ins->next->opcode = OP_FBGE;
2695 ins->next->opcode = OP_FBGE_UN;
2703 ins->sreg1 = ins->sreg2;
2712 bb->last_ins = last_ins;
2713 bb->max_vreg = cfg->next_vreg;
2717 mono_arch_decompose_long_opts (MonoCompile *cfg, MonoInst *long_ins)
2721 if (long_ins->opcode == OP_LNEG) {
2723 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ARM_RSBS_IMM, ins->dreg + 1, ins->sreg1 + 1, 0);
2724 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ARM_RSC_IMM, ins->dreg + 2, ins->sreg1 + 2, 0);
2730 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
2732 /* sreg is a float, dreg is an integer reg */
2734 ARM_FIXZ (code, dreg, sreg);
2735 #elif defined(ARM_FPU_VFP)
2737 ARM_TOSIZD (code, ARM_VFP_F0, sreg);
2739 ARM_TOUIZD (code, ARM_VFP_F0, sreg);
2740 ARM_FMRS (code, dreg, ARM_VFP_F0);
2744 ARM_AND_REG_IMM8 (code, dreg, dreg, 0xff);
2745 else if (size == 2) {
2746 ARM_SHL_IMM (code, dreg, dreg, 16);
2747 ARM_SHR_IMM (code, dreg, dreg, 16);
2751 ARM_SHL_IMM (code, dreg, dreg, 24);
2752 ARM_SAR_IMM (code, dreg, dreg, 24);
2753 } else if (size == 2) {
2754 ARM_SHL_IMM (code, dreg, dreg, 16);
2755 ARM_SAR_IMM (code, dreg, dreg, 16);
2761 #endif /* #ifndef DISABLE_JIT */
2765 const guchar *target;
2770 #define is_call_imm(diff) ((gint)(diff) >= -33554432 && (gint)(diff) <= 33554431)
2773 search_thunk_slot (void *data, int csize, int bsize, void *user_data) {
2774 PatchData *pdata = (PatchData*)user_data;
2775 guchar *code = data;
2776 guint32 *thunks = data;
2777 guint32 *endthunks = (guint32*)(code + bsize);
2779 int difflow, diffhigh;
2781 /* always ensure a call from pdata->code can reach to the thunks without further thunks */
2782 difflow = (char*)pdata->code - (char*)thunks;
2783 diffhigh = (char*)pdata->code - (char*)endthunks;
2784 if (!((is_call_imm (thunks) && is_call_imm (endthunks)) || (is_call_imm (difflow) && is_call_imm (diffhigh))))
2788 * The thunk is composed of 3 words:
2789 * load constant from thunks [2] into ARM_IP
2792 * Note that the LR register is already setup
2794 //g_print ("thunk nentries: %d\n", ((char*)endthunks - (char*)thunks)/16);
2795 if ((pdata->found == 2) || (pdata->code >= code && pdata->code <= code + csize)) {
2796 while (thunks < endthunks) {
2797 //g_print ("looking for target: %p at %p (%08x-%08x)\n", pdata->target, thunks, thunks [0], thunks [1]);
2798 if (thunks [2] == (guint32)pdata->target) {
2799 arm_patch (pdata->code, (guchar*)thunks);
2800 mono_arch_flush_icache (pdata->code, 4);
2803 } else if ((thunks [0] == 0) && (thunks [1] == 0) && (thunks [2] == 0)) {
2804 /* found a free slot instead: emit thunk */
2805 /* ARMREG_IP is fine to use since this can't be an IMT call
2808 code = (guchar*)thunks;
2809 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
2810 if (thumb_supported)
2811 ARM_BX (code, ARMREG_IP);
2813 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
2814 thunks [2] = (guint32)pdata->target;
2815 mono_arch_flush_icache ((guchar*)thunks, 12);
2817 arm_patch (pdata->code, (guchar*)thunks);
2818 mono_arch_flush_icache (pdata->code, 4);
2822 /* skip 12 bytes, the size of the thunk */
2826 //g_print ("failed thunk lookup for %p from %p at %p (%d entries)\n", pdata->target, pdata->code, data, count);
2832 handle_thunk (MonoDomain *domain, int absolute, guchar *code, const guchar *target, MonoCodeManager *dyn_code_mp)
2837 domain = mono_domain_get ();
2840 pdata.target = target;
2841 pdata.absolute = absolute;
2845 mono_code_manager_foreach (dyn_code_mp, search_thunk_slot, &pdata);
2848 if (pdata.found != 1) {
2849 mono_domain_lock (domain);
2850 mono_domain_code_foreach (domain, search_thunk_slot, &pdata);
2853 /* this uses the first available slot */
2855 mono_domain_code_foreach (domain, search_thunk_slot, &pdata);
2857 mono_domain_unlock (domain);
2860 if (pdata.found != 1) {
2862 GHashTableIter iter;
2863 MonoJitDynamicMethodInfo *ji;
2866 * This might be a dynamic method, search its code manager. We can only
2867 * use the dynamic method containing CODE, since the others might be freed later.
2871 mono_domain_lock (domain);
2872 hash = domain_jit_info (domain)->dynamic_code_hash;
2874 /* FIXME: Speed this up */
2875 g_hash_table_iter_init (&iter, hash);
2876 while (g_hash_table_iter_next (&iter, NULL, (gpointer*)&ji)) {
2877 mono_code_manager_foreach (ji->code_mp, search_thunk_slot, &pdata);
2878 if (pdata.found == 1)
2882 mono_domain_unlock (domain);
2884 if (pdata.found != 1)
2885 g_print ("thunk failed for %p from %p\n", target, code);
2886 g_assert (pdata.found == 1);
2890 arm_patch_general (MonoDomain *domain, guchar *code, const guchar *target, MonoCodeManager *dyn_code_mp)
2892 guint32 *code32 = (void*)code;
2893 guint32 ins = *code32;
2894 guint32 prim = (ins >> 25) & 7;
2895 guint32 tval = GPOINTER_TO_UINT (target);
2897 //g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
2898 if (prim == 5) { /* 101b */
2899 /* the diff starts 8 bytes from the branch opcode */
2900 gint diff = target - code - 8;
2902 gint tmask = 0xffffffff;
2903 if (tval & 1) { /* entering thumb mode */
2904 diff = target - 1 - code - 8;
2905 g_assert (thumb_supported);
2906 tbits = 0xf << 28; /* bl->blx bit pattern */
2907 g_assert ((ins & (1 << 24))); /* it must be a bl, not b instruction */
2908 /* this low bit of the displacement is moved to bit 24 in the instruction encoding */
2912 tmask = ~(1 << 24); /* clear the link bit */
2913 /*g_print ("blx to thumb: target: %p, code: %p, diff: %d, mask: %x\n", target, code, diff, tmask);*/
2918 if (diff <= 33554431) {
2920 ins = (ins & 0xff000000) | diff;
2922 *code32 = ins | tbits;
2926 /* diff between 0 and -33554432 */
2927 if (diff >= -33554432) {
2929 ins = (ins & 0xff000000) | (diff & ~0xff000000);
2931 *code32 = ins | tbits;
2936 handle_thunk (domain, TRUE, code, target, dyn_code_mp);
2941 * The alternative call sequences looks like this:
2943 * ldr ip, [pc] // loads the address constant
2944 * b 1f // jumps around the constant
2945 * address constant embedded in the code
2950 * There are two cases for patching:
2951 * a) at the end of method emission: in this case code points to the start
2952 * of the call sequence
2953 * b) during runtime patching of the call site: in this case code points
2954 * to the mov pc, ip instruction
2956 * We have to handle also the thunk jump code sequence:
2960 * address constant // execution never reaches here
2962 if ((ins & 0x0ffffff0) == 0x12fff10) {
2963 /* Branch and exchange: the address is constructed in a reg
2964 * We can patch BX when the code sequence is the following:
2965 * ldr ip, [pc, #0] ; 0x8
2972 guint8 *emit = (guint8*)ccode;
2973 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
2975 ARM_MOV_REG_REG (emit, ARMREG_LR, ARMREG_PC);
2976 ARM_BX (emit, ARMREG_IP);
2978 /*patching from magic trampoline*/
2979 if (ins == ccode [3]) {
2980 g_assert (code32 [-4] == ccode [0]);
2981 g_assert (code32 [-3] == ccode [1]);
2982 g_assert (code32 [-1] == ccode [2]);
2983 code32 [-2] = (guint32)target;
2986 /*patching from JIT*/
2987 if (ins == ccode [0]) {
2988 g_assert (code32 [1] == ccode [1]);
2989 g_assert (code32 [3] == ccode [2]);
2990 g_assert (code32 [4] == ccode [3]);
2991 code32 [2] = (guint32)target;
2994 g_assert_not_reached ();
2995 } else if ((ins & 0x0ffffff0) == 0x12fff30) {
3003 guint8 *emit = (guint8*)ccode;
3004 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
3006 ARM_BLX_REG (emit, ARMREG_IP);
3008 g_assert (code32 [-3] == ccode [0]);
3009 g_assert (code32 [-2] == ccode [1]);
3010 g_assert (code32 [0] == ccode [2]);
3012 code32 [-1] = (guint32)target;
3015 guint32 *tmp = ccode;
3016 guint8 *emit = (guint8*)tmp;
3017 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
3018 ARM_MOV_REG_REG (emit, ARMREG_LR, ARMREG_PC);
3019 ARM_MOV_REG_REG (emit, ARMREG_PC, ARMREG_IP);
3020 ARM_BX (emit, ARMREG_IP);
3021 if (ins == ccode [2]) {
3022 g_assert_not_reached (); // should be -2 ...
3023 code32 [-1] = (guint32)target;
3026 if (ins == ccode [0]) {
3027 /* handles both thunk jump code and the far call sequence */
3028 code32 [2] = (guint32)target;
3031 g_assert_not_reached ();
3033 // g_print ("patched with 0x%08x\n", ins);
3037 arm_patch (guchar *code, const guchar *target)
3039 arm_patch_general (NULL, code, target, NULL);
3043 * Return the >= 0 uimm8 value if val can be represented with a byte + rotation
3044 * (with the rotation amount in *rot_amount. rot_amount is already adjusted
3045 * to be used with the emit macros.
3046 * Return -1 otherwise.
3049 mono_arm_is_rotated_imm8 (guint32 val, gint *rot_amount)
3052 for (i = 0; i < 31; i+= 2) {
3053 res = (val << (32 - i)) | (val >> i);
3056 *rot_amount = i? 32 - i: 0;
3063 * Emits in code a sequence of instructions that load the value 'val'
3064 * into the dreg register. Uses at most 4 instructions.
3067 mono_arm_emit_load_imm (guint8 *code, int dreg, guint32 val)
3069 int imm8, rot_amount;
3071 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
3072 /* skip the constant pool */
3078 if ((imm8 = mono_arm_is_rotated_imm8 (val, &rot_amount)) >= 0) {
3079 ARM_MOV_REG_IMM (code, dreg, imm8, rot_amount);
3080 } else if ((imm8 = mono_arm_is_rotated_imm8 (~val, &rot_amount)) >= 0) {
3081 ARM_MVN_REG_IMM (code, dreg, imm8, rot_amount);
3084 ARM_MOVW_REG_IMM (code, dreg, val & 0xffff);
3086 ARM_MOVT_REG_IMM (code, dreg, (val >> 16) & 0xffff);
3090 ARM_MOV_REG_IMM8 (code, dreg, (val & 0xFF));
3092 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF00) >> 8, 24);
3094 if (val & 0xFF0000) {
3095 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
3097 if (val & 0xFF000000) {
3098 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
3100 } else if (val & 0xFF00) {
3101 ARM_MOV_REG_IMM (code, dreg, (val & 0xFF00) >> 8, 24);
3102 if (val & 0xFF0000) {
3103 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
3105 if (val & 0xFF000000) {
3106 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
3108 } else if (val & 0xFF0000) {
3109 ARM_MOV_REG_IMM (code, dreg, (val & 0xFF0000) >> 16, 16);
3110 if (val & 0xFF000000) {
3111 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
3114 //g_assert_not_reached ();
3120 mono_arm_thumb_supported (void)
3122 return thumb_supported;
3128 * emit_load_volatile_arguments:
3130 * Load volatile arguments from the stack to the original input registers.
3131 * Required before a tail call.
3134 emit_load_volatile_arguments (MonoCompile *cfg, guint8 *code)
3136 MonoMethod *method = cfg->method;
3137 MonoMethodSignature *sig;
3142 /* FIXME: Generate intermediate code instead */
3144 sig = mono_method_signature (method);
3146 /* This is the opposite of the code in emit_prolog */
3150 cinfo = get_call_info (cfg->generic_sharing_context, NULL, sig);
3152 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
3153 ArgInfo *ainfo = &cinfo->ret;
3154 inst = cfg->vret_addr;
3155 g_assert (arm_is_imm12 (inst->inst_offset));
3156 ARM_LDR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
3158 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3159 ArgInfo *ainfo = cinfo->args + i;
3160 inst = cfg->args [pos];
3162 if (cfg->verbose_level > 2)
3163 g_print ("Loading argument %d (type: %d)\n", i, ainfo->storage);
3164 if (inst->opcode == OP_REGVAR) {
3165 if (ainfo->storage == RegTypeGeneral)
3166 ARM_MOV_REG_REG (code, inst->dreg, ainfo->reg);
3167 else if (ainfo->storage == RegTypeFP) {
3168 g_assert_not_reached ();
3169 } else if (ainfo->storage == RegTypeBase) {
3173 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
3174 ARM_LDR_IMM (code, inst->dreg, ARMREG_SP, (prev_sp_offset + ainfo->offset));
3176 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
3177 ARM_LDR_REG_REG (code, inst->dreg, ARMREG_SP, ARMREG_IP);
3181 g_assert_not_reached ();
3183 if (ainfo->storage == RegTypeGeneral || ainfo->storage == RegTypeIRegPair) {
3184 switch (ainfo->size) {
3191 g_assert (arm_is_imm12 (inst->inst_offset));
3192 ARM_LDR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
3193 g_assert (arm_is_imm12 (inst->inst_offset + 4));
3194 ARM_LDR_IMM (code, ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
3197 if (arm_is_imm12 (inst->inst_offset)) {
3198 ARM_LDR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
3200 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
3201 ARM_LDR_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
3205 } else if (ainfo->storage == RegTypeBaseGen) {
3208 } else if (ainfo->storage == RegTypeBase) {
3210 } else if (ainfo->storage == RegTypeFP) {
3211 g_assert_not_reached ();
3212 } else if (ainfo->storage == RegTypeStructByVal) {
3213 int doffset = inst->inst_offset;
3217 if (mono_class_from_mono_type (inst->inst_vtype))
3218 size = mono_class_native_size (mono_class_from_mono_type (inst->inst_vtype), NULL);
3219 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
3220 if (arm_is_imm12 (doffset)) {
3221 ARM_LDR_IMM (code, ainfo->reg + cur_reg, inst->inst_basereg, doffset);
3223 code = mono_arm_emit_load_imm (code, ARMREG_IP, doffset);
3224 ARM_LDR_REG_REG (code, ainfo->reg + cur_reg, inst->inst_basereg, ARMREG_IP);
3226 soffset += sizeof (gpointer);
3227 doffset += sizeof (gpointer);
3232 } else if (ainfo->storage == RegTypeStructByAddr) {
3247 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
3252 guint8 *code = cfg->native_code + cfg->code_len;
3253 MonoInst *last_ins = NULL;
3254 guint last_offset = 0;
3256 int imm8, rot_amount;
3258 /* we don't align basic blocks of loops on arm */
3260 if (cfg->verbose_level > 2)
3261 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
3263 cpos = bb->max_offset;
3265 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
3266 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
3267 //g_assert (!mono_compile_aot);
3270 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
3271 /* this is not thread save, but good enough */
3272 /* fixme: howto handle overflows? */
3273 //x86_inc_mem (code, &cov->data [bb->dfn].count);
3276 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) {
3277 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3278 (gpointer)"mono_break");
3279 code = emit_call_seq (cfg, code);
3282 MONO_BB_FOR_EACH_INS (bb, ins) {
3283 offset = code - cfg->native_code;
3285 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
3287 if (offset > (cfg->code_size - max_len - 16)) {
3288 cfg->code_size *= 2;
3289 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3290 code = cfg->native_code + offset;
3292 // if (ins->cil_code)
3293 // g_print ("cil code\n");
3294 mono_debug_record_line_number (cfg, ins, offset);
3296 switch (ins->opcode) {
3297 case OP_MEMORY_BARRIER:
3299 ARM_MOV_REG_IMM8 (code, ARMREG_R0, 0);
3300 ARM_MCR (code, 15, 0, ARMREG_R0, 7, 10, 5);
3304 #ifdef HAVE_AEABI_READ_TP
3305 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3306 (gpointer)"__aeabi_read_tp");
3307 code = emit_call_seq (cfg, code);
3309 ARM_LDR_IMM (code, ins->dreg, ARMREG_R0, ins->inst_offset);
3311 g_assert_not_reached ();
3315 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
3316 ppc_mulhw (code, ppc_r3, ins->sreg1, ins->sreg2);
3319 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
3320 ppc_mulhwu (code, ppc_r3, ins->sreg1, ins->sreg2);
3322 case OP_STOREI1_MEMBASE_IMM:
3323 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFF);
3324 g_assert (arm_is_imm12 (ins->inst_offset));
3325 ARM_STRB_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
3327 case OP_STOREI2_MEMBASE_IMM:
3328 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFFFF);
3329 g_assert (arm_is_imm8 (ins->inst_offset));
3330 ARM_STRH_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
3332 case OP_STORE_MEMBASE_IMM:
3333 case OP_STOREI4_MEMBASE_IMM:
3334 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm);
3335 g_assert (arm_is_imm12 (ins->inst_offset));
3336 ARM_STR_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
3338 case OP_STOREI1_MEMBASE_REG:
3339 g_assert (arm_is_imm12 (ins->inst_offset));
3340 ARM_STRB_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3342 case OP_STOREI2_MEMBASE_REG:
3343 g_assert (arm_is_imm8 (ins->inst_offset));
3344 ARM_STRH_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3346 case OP_STORE_MEMBASE_REG:
3347 case OP_STOREI4_MEMBASE_REG:
3348 /* this case is special, since it happens for spill code after lowering has been called */
3349 if (arm_is_imm12 (ins->inst_offset)) {
3350 ARM_STR_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3352 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
3353 ARM_STR_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ARMREG_LR);
3356 case OP_STOREI1_MEMINDEX:
3357 ARM_STRB_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
3359 case OP_STOREI2_MEMINDEX:
3360 ARM_STRH_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
3362 case OP_STORE_MEMINDEX:
3363 case OP_STOREI4_MEMINDEX:
3364 ARM_STR_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
3367 g_assert_not_reached ();
3369 case OP_LOAD_MEMINDEX:
3370 case OP_LOADI4_MEMINDEX:
3371 case OP_LOADU4_MEMINDEX:
3372 ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3374 case OP_LOADI1_MEMINDEX:
3375 ARM_LDRSB_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3377 case OP_LOADU1_MEMINDEX:
3378 ARM_LDRB_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3380 case OP_LOADI2_MEMINDEX:
3381 ARM_LDRSH_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3383 case OP_LOADU2_MEMINDEX:
3384 ARM_LDRH_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3386 case OP_LOAD_MEMBASE:
3387 case OP_LOADI4_MEMBASE:
3388 case OP_LOADU4_MEMBASE:
3389 /* this case is special, since it happens for spill code after lowering has been called */
3390 if (arm_is_imm12 (ins->inst_offset)) {
3391 ARM_LDR_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3393 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
3394 ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ARMREG_LR);
3397 case OP_LOADI1_MEMBASE:
3398 g_assert (arm_is_imm8 (ins->inst_offset));
3399 ARM_LDRSB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3401 case OP_LOADU1_MEMBASE:
3402 g_assert (arm_is_imm12 (ins->inst_offset));
3403 ARM_LDRB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3405 case OP_LOADU2_MEMBASE:
3406 g_assert (arm_is_imm8 (ins->inst_offset));
3407 ARM_LDRH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3409 case OP_LOADI2_MEMBASE:
3410 g_assert (arm_is_imm8 (ins->inst_offset));
3411 ARM_LDRSH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3413 case OP_ICONV_TO_I1:
3414 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 24);
3415 ARM_SAR_IMM (code, ins->dreg, ins->dreg, 24);
3417 case OP_ICONV_TO_I2:
3418 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
3419 ARM_SAR_IMM (code, ins->dreg, ins->dreg, 16);
3421 case OP_ICONV_TO_U1:
3422 ARM_AND_REG_IMM8 (code, ins->dreg, ins->sreg1, 0xff);
3424 case OP_ICONV_TO_U2:
3425 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
3426 ARM_SHR_IMM (code, ins->dreg, ins->dreg, 16);
3430 ARM_CMP_REG_REG (code, ins->sreg1, ins->sreg2);
3432 case OP_COMPARE_IMM:
3433 case OP_ICOMPARE_IMM:
3434 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3435 g_assert (imm8 >= 0);
3436 ARM_CMP_REG_IMM (code, ins->sreg1, imm8, rot_amount);
3440 * gdb does not like encountering the hw breakpoint ins in the debugged code.
3441 * So instead of emitting a trap, we emit a call a C function and place a
3444 //*(int*)code = 0xef9f0001;
3447 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3448 (gpointer)"mono_break");
3449 code = emit_call_seq (cfg, code);
3451 case OP_RELAXED_NOP:
3456 case OP_DUMMY_STORE:
3457 case OP_NOT_REACHED:
3460 case OP_SEQ_POINT: {
3462 MonoInst *info_var = cfg->arch.seq_point_info_var;
3463 MonoInst *ss_trigger_page_var = cfg->arch.ss_trigger_page_var;
3465 int dreg = ARMREG_LR;
3468 * For AOT, we use one got slot per method, which will point to a
3469 * SeqPointInfo structure, containing all the information required
3470 * by the code below.
3472 if (cfg->compile_aot) {
3473 g_assert (info_var);
3474 g_assert (info_var->opcode == OP_REGOFFSET);
3475 g_assert (arm_is_imm12 (info_var->inst_offset));
3479 * Read from the single stepping trigger page. This will cause a
3480 * SIGSEGV when single stepping is enabled.
3481 * We do this _before_ the breakpoint, so single stepping after
3482 * a breakpoint is hit will step to the next IL offset.
3484 g_assert (((guint64)(gsize)ss_trigger_page >> 32) == 0);
3486 if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
3487 if (cfg->compile_aot) {
3488 /* Load the trigger page addr from the variable initialized in the prolog */
3489 var = ss_trigger_page_var;
3491 g_assert (var->opcode == OP_REGOFFSET);
3492 g_assert (arm_is_imm12 (var->inst_offset));
3493 ARM_LDR_IMM (code, dreg, var->inst_basereg, var->inst_offset);
3495 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
3497 *(int*)code = (int)ss_trigger_page;
3500 ARM_LDR_IMM (code, dreg, dreg, 0);
3503 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
3505 if (cfg->compile_aot) {
3506 guint32 offset = code - cfg->native_code;
3509 ARM_LDR_IMM (code, dreg, info_var->inst_basereg, info_var->inst_offset);
3510 /* Add the offset */
3511 val = ((offset / 4) * sizeof (guint8*)) + G_STRUCT_OFFSET (SeqPointInfo, bp_addrs);
3512 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF), 0);
3514 * Have to emit nops to keep the difference between the offset
3515 * stored in seq_points and breakpoint instruction constant,
3516 * mono_arch_get_ip_for_breakpoint () depends on this.
3519 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF00) >> 8, 24);
3523 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
3526 g_assert (!(val & 0xFF000000));
3527 /* Load the info->bp_addrs [offset], which is either 0 or the address of a trigger page */
3528 ARM_LDR_IMM (code, dreg, dreg, 0);
3530 /* What is faster, a branch or a load ? */
3531 ARM_CMP_REG_IMM (code, dreg, 0, 0);
3532 /* The breakpoint instruction */
3533 ARM_LDR_IMM_COND (code, dreg, dreg, 0, ARMCOND_NE);
3536 * A placeholder for a possible breakpoint inserted by
3537 * mono_arch_set_breakpoint ().
3539 for (i = 0; i < 4; ++i)
3546 ARM_ADDS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3549 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3553 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3556 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3557 g_assert (imm8 >= 0);
3558 ARM_ADDS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3562 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3563 g_assert (imm8 >= 0);
3564 ARM_ADD_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3568 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3569 g_assert (imm8 >= 0);
3570 ARM_ADCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3573 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3574 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3576 case OP_IADD_OVF_UN:
3577 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3578 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3581 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3582 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3584 case OP_ISUB_OVF_UN:
3585 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3586 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
3588 case OP_ADD_OVF_CARRY:
3589 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3590 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3592 case OP_ADD_OVF_UN_CARRY:
3593 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3594 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3596 case OP_SUB_OVF_CARRY:
3597 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3598 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3600 case OP_SUB_OVF_UN_CARRY:
3601 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3602 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
3606 ARM_SUBS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3609 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3610 g_assert (imm8 >= 0);
3611 ARM_SUBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3614 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3618 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3622 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3623 g_assert (imm8 >= 0);
3624 ARM_SUB_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3628 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3629 g_assert (imm8 >= 0);
3630 ARM_SBCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3632 case OP_ARM_RSBS_IMM:
3633 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3634 g_assert (imm8 >= 0);
3635 ARM_RSBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3637 case OP_ARM_RSC_IMM:
3638 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3639 g_assert (imm8 >= 0);
3640 ARM_RSC_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3643 ARM_AND_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3647 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3648 g_assert (imm8 >= 0);
3649 ARM_AND_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3657 /* crappy ARM arch doesn't have a DIV instruction */
3658 g_assert_not_reached ();
3660 ARM_ORR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3664 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3665 g_assert (imm8 >= 0);
3666 ARM_ORR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3669 ARM_EOR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3673 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3674 g_assert (imm8 >= 0);
3675 ARM_EOR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3678 ARM_SHL_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3683 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
3684 else if (ins->dreg != ins->sreg1)
3685 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
3688 ARM_SAR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3693 ARM_SAR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
3694 else if (ins->dreg != ins->sreg1)
3695 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
3698 case OP_ISHR_UN_IMM:
3700 ARM_SHR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
3701 else if (ins->dreg != ins->sreg1)
3702 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
3705 ARM_SHR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3708 ARM_MVN_REG_REG (code, ins->dreg, ins->sreg1);
3711 ARM_RSB_REG_IMM8 (code, ins->dreg, ins->sreg1, 0);
3714 if (ins->dreg == ins->sreg2)
3715 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3717 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg2, ins->sreg1);
3720 g_assert_not_reached ();
3723 /* FIXME: handle ovf/ sreg2 != dreg */
3724 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3725 /* FIXME: MUL doesn't set the C/O flags on ARM */
3727 case OP_IMUL_OVF_UN:
3728 /* FIXME: handle ovf/ sreg2 != dreg */
3729 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3730 /* FIXME: MUL doesn't set the C/O flags on ARM */
3733 code = mono_arm_emit_load_imm (code, ins->dreg, ins->inst_c0);
3736 /* Load the GOT offset */
3737 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3738 ARM_LDR_IMM (code, ins->dreg, ARMREG_PC, 0);
3740 *(gpointer*)code = NULL;
3742 /* Load the value from the GOT */
3743 ARM_LDR_REG_REG (code, ins->dreg, ARMREG_PC, ins->dreg);
3745 case OP_ICONV_TO_I4:
3746 case OP_ICONV_TO_U4:
3748 if (ins->dreg != ins->sreg1)
3749 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
3752 int saved = ins->sreg2;
3753 if (ins->sreg2 == ARM_LSW_REG) {
3754 ARM_MOV_REG_REG (code, ARMREG_LR, ins->sreg2);
3757 if (ins->sreg1 != ARM_LSW_REG)
3758 ARM_MOV_REG_REG (code, ARM_LSW_REG, ins->sreg1);
3759 if (saved != ARM_MSW_REG)
3760 ARM_MOV_REG_REG (code, ARM_MSW_REG, saved);
3765 ARM_MVFD (code, ins->dreg, ins->sreg1);
3766 #elif defined(ARM_FPU_VFP)
3767 ARM_CPYD (code, ins->dreg, ins->sreg1);
3770 case OP_FCONV_TO_R4:
3772 ARM_MVFS (code, ins->dreg, ins->sreg1);
3773 #elif defined(ARM_FPU_VFP)
3774 ARM_CVTD (code, ins->dreg, ins->sreg1);
3775 ARM_CVTS (code, ins->dreg, ins->dreg);
3780 * Keep in sync with mono_arch_emit_epilog
3782 g_assert (!cfg->method->save_lmf);
3784 code = emit_load_volatile_arguments (cfg, code);
3786 code = emit_big_add (code, ARMREG_SP, cfg->frame_reg, cfg->stack_usage);
3788 if (cfg->used_int_regs)
3789 ARM_POP (code, cfg->used_int_regs);
3790 ARM_POP (code, (1 << ARMREG_R7) | (1 << ARMREG_LR));
3792 ARM_POP (code, cfg->used_int_regs | (1 << ARMREG_LR));
3794 mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
3795 if (cfg->compile_aot) {
3796 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
3798 *(gpointer*)code = NULL;
3800 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
3806 /* ensure ins->sreg1 is not NULL */
3807 ARM_LDRB_IMM (code, ARMREG_LR, ins->sreg1, 0);
3810 g_assert (cfg->sig_cookie < 128);
3811 ARM_LDR_IMM (code, ARMREG_IP, cfg->frame_reg, cfg->sig_cookie);
3812 ARM_STR_IMM (code, ARMREG_IP, ins->sreg1, 0);
3821 call = (MonoCallInst*)ins;
3822 if (ins->flags & MONO_INST_HAS_METHOD)
3823 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
3825 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
3826 code = emit_call_seq (cfg, code);
3827 code = emit_move_return_value (cfg, ins, code);
3833 case OP_VOIDCALL_REG:
3835 code = emit_call_reg (code, ins->sreg1);
3836 code = emit_move_return_value (cfg, ins, code);
3838 case OP_FCALL_MEMBASE:
3839 case OP_LCALL_MEMBASE:
3840 case OP_VCALL_MEMBASE:
3841 case OP_VCALL2_MEMBASE:
3842 case OP_VOIDCALL_MEMBASE:
3843 case OP_CALL_MEMBASE:
3844 g_assert (arm_is_imm12 (ins->inst_offset));
3845 g_assert (ins->sreg1 != ARMREG_LR);
3846 call = (MonoCallInst*)ins;
3847 if (call->dynamic_imt_arg || call->method->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
3848 ARM_ADD_REG_IMM8 (code, ARMREG_LR, ARMREG_PC, 4);
3849 ARM_LDR_IMM (code, ARMREG_PC, ins->sreg1, ins->inst_offset);
3851 * We can't embed the method in the code stream in PIC code, or
3853 * Instead, we put it in V5 in code emitted by
3854 * mono_arch_emit_imt_argument (), and embed NULL here to
3855 * signal the IMT thunk that the value is in V5.
3857 if (call->dynamic_imt_arg)
3858 *((gpointer*)code) = NULL;
3860 *((gpointer*)code) = (gpointer)call->method;
3863 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
3864 ARM_LDR_IMM (code, ARMREG_PC, ins->sreg1, ins->inst_offset);
3866 code = emit_move_return_value (cfg, ins, code);
3869 /* keep alignment */
3870 int alloca_waste = cfg->param_area;
3873 /* round the size to 8 bytes */
3874 ARM_ADD_REG_IMM8 (code, ins->dreg, ins->sreg1, 7);
3875 ARM_BIC_REG_IMM8 (code, ins->dreg, ins->dreg, 7);
3877 ARM_ADD_REG_IMM8 (code, ins->dreg, ins->dreg, alloca_waste);
3878 ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ins->dreg);
3879 /* memzero the area: dreg holds the size, sp is the pointer */
3880 if (ins->flags & MONO_INST_INIT) {
3881 guint8 *start_loop, *branch_to_cond;
3882 ARM_MOV_REG_IMM8 (code, ARMREG_LR, 0);
3883 branch_to_cond = code;
3886 ARM_STR_REG_REG (code, ARMREG_LR, ARMREG_SP, ins->dreg);
3887 arm_patch (branch_to_cond, code);
3888 /* decrement by 4 and set flags */
3889 ARM_SUBS_REG_IMM8 (code, ins->dreg, ins->dreg, sizeof (mgreg_t));
3890 ARM_B_COND (code, ARMCOND_GE, 0);
3891 arm_patch (code - 4, start_loop);
3893 ARM_ADD_REG_IMM8 (code, ins->dreg, ARMREG_SP, alloca_waste);
3898 MonoInst *var = cfg->dyn_call_var;
3900 g_assert (var->opcode == OP_REGOFFSET);
3901 g_assert (arm_is_imm12 (var->inst_offset));
3903 /* lr = args buffer filled by mono_arch_get_dyn_call_args () */
3904 ARM_MOV_REG_REG( code, ARMREG_LR, ins->sreg1);
3906 ARM_MOV_REG_REG( code, ARMREG_IP, ins->sreg2);
3908 /* Save args buffer */
3909 ARM_STR_IMM (code, ARMREG_LR, var->inst_basereg, var->inst_offset);
3911 /* Set stack slots using R0 as scratch reg */
3912 /* MONO_ARCH_DYN_CALL_PARAM_AREA gives the size of stack space available */
3913 for (i = 0; i < DYN_CALL_STACK_ARGS; ++i) {
3914 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_LR, (PARAM_REGS + i) * sizeof (mgreg_t));
3915 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, i * sizeof (mgreg_t));
3918 /* Set argument registers */
3919 for (i = 0; i < PARAM_REGS; ++i)
3920 ARM_LDR_IMM (code, i, ARMREG_LR, i * sizeof (mgreg_t));
3923 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
3924 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
3927 ARM_LDR_IMM (code, ARMREG_IP, var->inst_basereg, var->inst_offset);
3928 ARM_STR_IMM (code, ARMREG_R0, ARMREG_IP, G_STRUCT_OFFSET (DynCallArgs, res));
3929 ARM_STR_IMM (code, ARMREG_R1, ARMREG_IP, G_STRUCT_OFFSET (DynCallArgs, res2));
3933 if (ins->sreg1 != ARMREG_R0)
3934 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
3935 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3936 (gpointer)"mono_arch_throw_exception");
3937 code = emit_call_seq (cfg, code);
3941 if (ins->sreg1 != ARMREG_R0)
3942 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
3943 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3944 (gpointer)"mono_arch_rethrow_exception");
3945 code = emit_call_seq (cfg, code);
3948 case OP_START_HANDLER: {
3949 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3951 if (arm_is_imm12 (spvar->inst_offset)) {
3952 ARM_STR_IMM (code, ARMREG_LR, spvar->inst_basereg, spvar->inst_offset);
3954 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
3955 ARM_STR_REG_REG (code, ARMREG_LR, spvar->inst_basereg, ARMREG_IP);
3959 case OP_ENDFILTER: {
3960 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3962 if (ins->sreg1 != ARMREG_R0)
3963 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
3964 if (arm_is_imm12 (spvar->inst_offset)) {
3965 ARM_LDR_IMM (code, ARMREG_IP, spvar->inst_basereg, spvar->inst_offset);
3967 g_assert (ARMREG_IP != spvar->inst_basereg);
3968 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
3969 ARM_LDR_REG_REG (code, ARMREG_IP, spvar->inst_basereg, ARMREG_IP);
3971 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
3974 case OP_ENDFINALLY: {
3975 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3977 if (arm_is_imm12 (spvar->inst_offset)) {
3978 ARM_LDR_IMM (code, ARMREG_IP, spvar->inst_basereg, spvar->inst_offset);
3980 g_assert (ARMREG_IP != spvar->inst_basereg);
3981 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
3982 ARM_LDR_REG_REG (code, ARMREG_IP, spvar->inst_basereg, ARMREG_IP);
3984 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
3987 case OP_CALL_HANDLER:
3988 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3990 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
3993 ins->inst_c0 = code - cfg->native_code;
3996 /*if (ins->inst_target_bb->native_offset) {
3998 //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
4000 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4005 ARM_MOV_REG_REG (code, ARMREG_PC, ins->sreg1);
4009 * In the normal case we have:
4010 * ldr pc, [pc, ins->sreg1 << 2]
4013 * ldr lr, [pc, ins->sreg1 << 2]
4015 * After follows the data.
4016 * FIXME: add aot support.
4018 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_SWITCH, ins->inst_p0);
4019 max_len += 4 * GPOINTER_TO_INT (ins->klass);
4020 if (offset + max_len > (cfg->code_size - 16)) {
4021 cfg->code_size += max_len;
4022 cfg->code_size *= 2;
4023 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4024 code = cfg->native_code + offset;
4026 ARM_LDR_REG_REG_SHIFT (code, ARMREG_PC, ARMREG_PC, ins->sreg1, ARMSHIFT_LSL, 2);
4028 code += 4 * GPOINTER_TO_INT (ins->klass);
4032 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
4033 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
4037 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4038 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LT);
4042 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4043 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LO);
4047 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4048 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_GT);
4052 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4053 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_HI);
4055 case OP_COND_EXC_EQ:
4056 case OP_COND_EXC_NE_UN:
4057 case OP_COND_EXC_LT:
4058 case OP_COND_EXC_LT_UN:
4059 case OP_COND_EXC_GT:
4060 case OP_COND_EXC_GT_UN:
4061 case OP_COND_EXC_GE:
4062 case OP_COND_EXC_GE_UN:
4063 case OP_COND_EXC_LE:
4064 case OP_COND_EXC_LE_UN:
4065 EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_EQ, ins->inst_p1);
4067 case OP_COND_EXC_IEQ:
4068 case OP_COND_EXC_INE_UN:
4069 case OP_COND_EXC_ILT:
4070 case OP_COND_EXC_ILT_UN:
4071 case OP_COND_EXC_IGT:
4072 case OP_COND_EXC_IGT_UN:
4073 case OP_COND_EXC_IGE:
4074 case OP_COND_EXC_IGE_UN:
4075 case OP_COND_EXC_ILE:
4076 case OP_COND_EXC_ILE_UN:
4077 EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_IEQ, ins->inst_p1);
4080 case OP_COND_EXC_IC:
4081 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_CS, ins->inst_p1);
4083 case OP_COND_EXC_OV:
4084 case OP_COND_EXC_IOV:
4085 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VS, ins->inst_p1);
4087 case OP_COND_EXC_NC:
4088 case OP_COND_EXC_INC:
4089 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_CC, ins->inst_p1);
4091 case OP_COND_EXC_NO:
4092 case OP_COND_EXC_INO:
4093 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VC, ins->inst_p1);
4105 EMIT_COND_BRANCH (ins, ins->opcode - OP_IBEQ);
4108 /* floating point opcodes */
4111 if (cfg->compile_aot) {
4112 ARM_LDFD (code, ins->dreg, ARMREG_PC, 0);
4114 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
4116 *(guint32*)code = ((guint32*)(ins->inst_p0))[1];
4119 /* FIXME: we can optimize the imm load by dealing with part of
4120 * the displacement in LDFD (aligning to 512).
4122 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
4123 ARM_LDFD (code, ins->dreg, ARMREG_LR, 0);
4127 if (cfg->compile_aot) {
4128 ARM_LDFS (code, ins->dreg, ARMREG_PC, 0);
4130 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
4133 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
4134 ARM_LDFS (code, ins->dreg, ARMREG_LR, 0);
4137 case OP_STORER8_MEMBASE_REG:
4138 /* This is generated by the local regalloc pass which runs after the lowering pass */
4139 if (!arm_is_fpimm8 (ins->inst_offset)) {
4140 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4141 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_destbasereg);
4142 ARM_STFD (code, ins->sreg1, ARMREG_LR, 0);
4144 ARM_STFD (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
4147 case OP_LOADR8_MEMBASE:
4148 /* This is generated by the local regalloc pass which runs after the lowering pass */
4149 if (!arm_is_fpimm8 (ins->inst_offset)) {
4150 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4151 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_basereg);
4152 ARM_LDFD (code, ins->dreg, ARMREG_LR, 0);
4154 ARM_LDFD (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4157 case OP_STORER4_MEMBASE_REG:
4158 g_assert (arm_is_fpimm8 (ins->inst_offset));
4159 ARM_STFS (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
4161 case OP_LOADR4_MEMBASE:
4162 g_assert (arm_is_fpimm8 (ins->inst_offset));
4163 ARM_LDFS (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4165 case OP_ICONV_TO_R_UN: {
4167 tmpreg = ins->dreg == 0? 1: 0;
4168 ARM_CMP_REG_IMM8 (code, ins->sreg1, 0);
4169 ARM_FLTD (code, ins->dreg, ins->sreg1);
4170 ARM_B_COND (code, ARMCOND_GE, 8);
4171 /* save the temp register */
4172 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
4173 ARM_STFD (code, tmpreg, ARMREG_SP, 0);
4174 ARM_LDFD (code, tmpreg, ARMREG_PC, 12);
4175 ARM_FPA_ADFD (code, ins->dreg, ins->dreg, tmpreg);
4176 ARM_LDFD (code, tmpreg, ARMREG_SP, 0);
4177 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
4178 /* skip the constant pool */
4181 *(int*)code = 0x41f00000;
4186 * ldfltd ftemp, [pc, #8] 0x41f00000 0x00000000
4187 * adfltd fdest, fdest, ftemp
4191 case OP_ICONV_TO_R4:
4192 ARM_FLTS (code, ins->dreg, ins->sreg1);
4194 case OP_ICONV_TO_R8:
4195 ARM_FLTD (code, ins->dreg, ins->sreg1);
4198 #elif defined(ARM_FPU_VFP)
4201 if (cfg->compile_aot) {
4202 ARM_FLDD (code, ins->dreg, ARMREG_PC, 0);
4204 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
4206 *(guint32*)code = ((guint32*)(ins->inst_p0))[1];
4209 /* FIXME: we can optimize the imm load by dealing with part of
4210 * the displacement in LDFD (aligning to 512).
4212 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
4213 ARM_FLDD (code, ins->dreg, ARMREG_LR, 0);
4217 if (cfg->compile_aot) {
4218 ARM_FLDS (code, ins->dreg, ARMREG_PC, 0);
4220 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
4222 ARM_CVTS (code, ins->dreg, ins->dreg);
4224 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
4225 ARM_FLDS (code, ins->dreg, ARMREG_LR, 0);
4226 ARM_CVTS (code, ins->dreg, ins->dreg);
4229 case OP_STORER8_MEMBASE_REG:
4230 /* This is generated by the local regalloc pass which runs after the lowering pass */
4231 if (!arm_is_fpimm8 (ins->inst_offset)) {
4232 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4233 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_destbasereg);
4234 ARM_FSTD (code, ins->sreg1, ARMREG_LR, 0);
4236 ARM_FSTD (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
4239 case OP_LOADR8_MEMBASE:
4240 /* This is generated by the local regalloc pass which runs after the lowering pass */
4241 if (!arm_is_fpimm8 (ins->inst_offset)) {
4242 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4243 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_basereg);
4244 ARM_FLDD (code, ins->dreg, ARMREG_LR, 0);
4246 ARM_FLDD (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4249 case OP_STORER4_MEMBASE_REG:
4250 g_assert (arm_is_fpimm8 (ins->inst_offset));
4251 ARM_CVTD (code, ARM_VFP_F0, ins->sreg1);
4252 ARM_FSTS (code, ARM_VFP_F0, ins->inst_destbasereg, ins->inst_offset);
4254 case OP_LOADR4_MEMBASE:
4255 g_assert (arm_is_fpimm8 (ins->inst_offset));
4256 ARM_FLDS (code, ARM_VFP_F0, ins->inst_basereg, ins->inst_offset);
4257 ARM_CVTS (code, ins->dreg, ARM_VFP_F0);
4259 case OP_ICONV_TO_R_UN: {
4260 g_assert_not_reached ();
4263 case OP_ICONV_TO_R4:
4264 ARM_FMSR (code, ARM_VFP_F0, ins->sreg1);
4265 ARM_FSITOS (code, ARM_VFP_F0, ARM_VFP_F0);
4266 ARM_CVTS (code, ins->dreg, ARM_VFP_F0);
4268 case OP_ICONV_TO_R8:
4269 ARM_FMSR (code, ARM_VFP_F0, ins->sreg1);
4270 ARM_FSITOD (code, ins->dreg, ARM_VFP_F0);
4274 if (mono_method_signature (cfg->method)->ret->type == MONO_TYPE_R4) {
4275 ARM_CVTD (code, ARM_VFP_F0, ins->sreg1);
4276 ARM_FMRS (code, ARMREG_R0, ARM_VFP_F0);
4278 ARM_FMRRD (code, ARMREG_R0, ARMREG_R1, ins->sreg1);
4284 case OP_FCONV_TO_I1:
4285 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
4287 case OP_FCONV_TO_U1:
4288 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
4290 case OP_FCONV_TO_I2:
4291 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
4293 case OP_FCONV_TO_U2:
4294 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
4296 case OP_FCONV_TO_I4:
4298 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
4300 case OP_FCONV_TO_U4:
4302 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
4304 case OP_FCONV_TO_I8:
4305 case OP_FCONV_TO_U8:
4306 g_assert_not_reached ();
4307 /* Implemented as helper calls */
4309 case OP_LCONV_TO_R_UN:
4310 g_assert_not_reached ();
4311 /* Implemented as helper calls */
4313 case OP_LCONV_TO_OVF_I4_2: {
4314 guint8 *high_bit_not_set, *valid_negative, *invalid_negative, *valid_positive;
4316 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
4319 ARM_CMP_REG_IMM8 (code, ins->sreg1, 0);
4320 high_bit_not_set = code;
4321 ARM_B_COND (code, ARMCOND_GE, 0); /*branch if bit 31 of the lower part is not set*/
4323 ARM_CMN_REG_IMM8 (code, ins->sreg2, 1); /*This have the same effect as CMP reg, 0xFFFFFFFF */
4324 valid_negative = code;
4325 ARM_B_COND (code, ARMCOND_EQ, 0); /*branch if upper part == 0xFFFFFFFF (lower part has bit 31 set) */
4326 invalid_negative = code;
4327 ARM_B_COND (code, ARMCOND_AL, 0);
4329 arm_patch (high_bit_not_set, code);
4331 ARM_CMP_REG_IMM8 (code, ins->sreg2, 0);
4332 valid_positive = code;
4333 ARM_B_COND (code, ARMCOND_EQ, 0); /*branch if upper part == 0 (lower part has bit 31 clear)*/
4335 arm_patch (invalid_negative, code);
4336 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_AL, "OverflowException");
4338 arm_patch (valid_negative, code);
4339 arm_patch (valid_positive, code);
4341 if (ins->dreg != ins->sreg1)
4342 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
4347 ARM_FPA_ADFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4350 ARM_FPA_SUFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4353 ARM_FPA_MUFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4356 ARM_FPA_DVFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4359 ARM_MNFD (code, ins->dreg, ins->sreg1);
4361 #elif defined(ARM_FPU_VFP)
4363 ARM_VFP_ADDD (code, ins->dreg, ins->sreg1, ins->sreg2);
4366 ARM_VFP_SUBD (code, ins->dreg, ins->sreg1, ins->sreg2);
4369 ARM_VFP_MULD (code, ins->dreg, ins->sreg1, ins->sreg2);
4372 ARM_VFP_DIVD (code, ins->dreg, ins->sreg1, ins->sreg2);
4375 ARM_NEGD (code, ins->dreg, ins->sreg1);
4380 g_assert_not_reached ();
4384 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4385 #elif defined(ARM_FPU_VFP)
4386 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4392 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4393 #elif defined(ARM_FPU_VFP)
4394 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4397 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
4398 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
4402 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4403 #elif defined(ARM_FPU_VFP)
4404 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4407 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4408 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4412 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4413 #elif defined(ARM_FPU_VFP)
4414 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4417 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4418 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4419 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
4424 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
4425 #elif defined(ARM_FPU_VFP)
4426 ARM_CMPD (code, ins->sreg2, ins->sreg1);
4429 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4430 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4435 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
4436 #elif defined(ARM_FPU_VFP)
4437 ARM_CMPD (code, ins->sreg2, ins->sreg1);
4440 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4441 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4442 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
4444 /* ARM FPA flags table:
4445 * N Less than ARMCOND_MI
4446 * Z Equal ARMCOND_EQ
4447 * C Greater Than or Equal ARMCOND_CS
4448 * V Unordered ARMCOND_VS
4451 EMIT_COND_BRANCH (ins, OP_IBEQ - OP_IBEQ);
4454 EMIT_COND_BRANCH (ins, OP_IBNE_UN - OP_IBEQ);
4457 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set */
4460 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
4461 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set */
4467 g_assert_not_reached ();
4471 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_GE);
4473 /* FPA requires EQ even thou the docs suggests that just CS is enough */
4474 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_EQ);
4475 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_CS);
4479 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
4480 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_GE);
4485 if (ins->dreg != ins->sreg1)
4486 ARM_MVFD (code, ins->dreg, ins->sreg1);
4487 #elif defined(ARM_FPU_VFP)
4488 ARM_ABSD (code, ARM_VFP_D1, ins->sreg1);
4489 ARM_FLDD (code, ARM_VFP_D0, ARMREG_PC, 0);
4491 *(guint32*)code = 0xffffffff;
4493 *(guint32*)code = 0x7fefffff;
4495 ARM_CMPD (code, ARM_VFP_D1, ARM_VFP_D0);
4497 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_GT, "ArithmeticException");
4498 ARM_CMPD (code, ins->sreg1, ins->sreg1);
4500 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VS, "ArithmeticException");
4502 ARM_CPYD (code, ins->dreg, ins->sreg1);
4507 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
4508 g_assert_not_reached ();
4511 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
4512 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
4513 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
4514 g_assert_not_reached ();
4520 last_offset = offset;
4523 cfg->code_len = code - cfg->native_code;
4526 #endif /* DISABLE_JIT */
4528 #ifdef HAVE_AEABI_READ_TP
4529 void __aeabi_read_tp (void);
4533 mono_arch_register_lowlevel_calls (void)
4535 /* The signature doesn't matter */
4536 mono_register_jit_icall (mono_arm_throw_exception, "mono_arm_throw_exception", mono_create_icall_signature ("void"), TRUE);
4537 mono_register_jit_icall (mono_arm_throw_exception_by_token, "mono_arm_throw_exception_by_token", mono_create_icall_signature ("void"), TRUE);
4539 #ifndef MONO_CROSS_COMPILE
4540 #ifdef HAVE_AEABI_READ_TP
4541 mono_register_jit_icall (__aeabi_read_tp, "__aeabi_read_tp", mono_create_icall_signature ("void"), TRUE);
4546 #define patch_lis_ori(ip,val) do {\
4547 guint16 *__lis_ori = (guint16*)(ip); \
4548 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff; \
4549 __lis_ori [3] = ((guint32)(val)) & 0xffff; \
4553 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, MonoCodeManager *dyn_code_mp, gboolean run_cctors)
4555 MonoJumpInfo *patch_info;
4556 gboolean compile_aot = !run_cctors;
4558 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
4559 unsigned char *ip = patch_info->ip.i + code;
4560 const unsigned char *target;
4562 if (patch_info->type == MONO_PATCH_INFO_SWITCH && !compile_aot) {
4563 gpointer *jt = (gpointer*)(ip + 8);
4565 /* jt is the inlined jump table, 2 instructions after ip
4566 * In the normal case we store the absolute addresses,
4567 * otherwise the displacements.
4569 for (i = 0; i < patch_info->data.table->table_size; i++)
4570 jt [i] = code + (int)patch_info->data.table->table [i];
4573 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
4576 switch (patch_info->type) {
4577 case MONO_PATCH_INFO_BB:
4578 case MONO_PATCH_INFO_LABEL:
4581 /* No need to patch these */
4586 switch (patch_info->type) {
4587 case MONO_PATCH_INFO_IP:
4588 g_assert_not_reached ();
4589 patch_lis_ori (ip, ip);
4591 case MONO_PATCH_INFO_METHOD_REL:
4592 g_assert_not_reached ();
4593 *((gpointer *)(ip)) = code + patch_info->data.offset;
4595 case MONO_PATCH_INFO_METHODCONST:
4596 case MONO_PATCH_INFO_CLASS:
4597 case MONO_PATCH_INFO_IMAGE:
4598 case MONO_PATCH_INFO_FIELD:
4599 case MONO_PATCH_INFO_VTABLE:
4600 case MONO_PATCH_INFO_IID:
4601 case MONO_PATCH_INFO_SFLDA:
4602 case MONO_PATCH_INFO_LDSTR:
4603 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
4604 case MONO_PATCH_INFO_LDTOKEN:
4605 g_assert_not_reached ();
4606 /* from OP_AOTCONST : lis + ori */
4607 patch_lis_ori (ip, target);
4609 case MONO_PATCH_INFO_R4:
4610 case MONO_PATCH_INFO_R8:
4611 g_assert_not_reached ();
4612 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
4614 case MONO_PATCH_INFO_EXC_NAME:
4615 g_assert_not_reached ();
4616 *((gconstpointer *)(ip + 1)) = patch_info->data.name;
4618 case MONO_PATCH_INFO_NONE:
4619 case MONO_PATCH_INFO_BB_OVF:
4620 case MONO_PATCH_INFO_EXC_OVF:
4621 /* everything is dealt with at epilog output time */
4626 arm_patch_general (domain, ip, target, dyn_code_mp);
4633 * Stack frame layout:
4635 * ------------------- fp
4636 * MonoLMF structure or saved registers
4637 * -------------------
4639 * -------------------
4641 * -------------------
4642 * optional 8 bytes for tracing
4643 * -------------------
4644 * param area size is cfg->param_area
4645 * ------------------- sp
4648 mono_arch_emit_prolog (MonoCompile *cfg)
4650 MonoMethod *method = cfg->method;
4652 MonoMethodSignature *sig;
4654 int alloc_size, pos, max_offset, i, rot_amount;
4659 int prev_sp_offset, reg_offset;
4661 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4664 sig = mono_method_signature (method);
4665 cfg->code_size = 256 + sig->param_count * 64;
4666 code = cfg->native_code = g_malloc (cfg->code_size);
4668 mono_emit_unwind_op_def_cfa (cfg, code, ARMREG_SP, 0);
4670 alloc_size = cfg->stack_offset;
4674 if (!method->save_lmf) {
4677 * The iphone uses R7 as the frame pointer, and it points at the saved
4682 * We can't use r7 as a frame pointer since it points into the middle of
4683 * the frame, so we keep using our own frame pointer.
4684 * FIXME: Optimize this.
4687 ARM_PUSH (code, (1 << ARMREG_R7) | (1 << ARMREG_LR));
4688 ARM_MOV_REG_REG (code, ARMREG_R7, ARMREG_SP);
4689 prev_sp_offset += 8; /* r7 and lr */
4690 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
4691 mono_emit_unwind_op_offset (cfg, code, ARMREG_R7, (- prev_sp_offset) + 0);
4693 /* No need to push LR again */
4694 if (cfg->used_int_regs)
4695 ARM_PUSH (code, cfg->used_int_regs);
4697 ARM_PUSH (code, cfg->used_int_regs | (1 << ARMREG_LR));
4698 prev_sp_offset += 4;
4700 for (i = 0; i < 16; ++i) {
4701 if (cfg->used_int_regs & (1 << i))
4702 prev_sp_offset += 4;
4704 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
4706 for (i = 0; i < 16; ++i) {
4707 if ((cfg->used_int_regs & (1 << i))) {
4708 mono_emit_unwind_op_offset (cfg, code, i, (- prev_sp_offset) + reg_offset);
4713 mono_emit_unwind_op_offset (cfg, code, ARMREG_LR, -4);
4715 mono_emit_unwind_op_offset (cfg, code, ARMREG_LR, -4);
4718 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
4719 ARM_PUSH (code, 0x5ff0);
4720 prev_sp_offset += 4 * 10; /* all but r0-r3, sp and pc */
4721 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
4723 for (i = 0; i < 16; ++i) {
4724 if ((i > ARMREG_R3) && (i != ARMREG_SP) && (i != ARMREG_PC)) {
4725 mono_emit_unwind_op_offset (cfg, code, i, (- prev_sp_offset) + reg_offset);
4729 pos += sizeof (MonoLMF) - prev_sp_offset;
4733 // align to MONO_ARCH_FRAME_ALIGNMENT bytes
4734 if (alloc_size & (MONO_ARCH_FRAME_ALIGNMENT - 1)) {
4735 alloc_size += MONO_ARCH_FRAME_ALIGNMENT - 1;
4736 alloc_size &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
4739 /* the stack used in the pushed regs */
4740 if (prev_sp_offset & 4)
4742 cfg->stack_usage = alloc_size;
4744 if ((i = mono_arm_is_rotated_imm8 (alloc_size, &rot_amount)) >= 0) {
4745 ARM_SUB_REG_IMM (code, ARMREG_SP, ARMREG_SP, i, rot_amount);
4747 code = mono_arm_emit_load_imm (code, ARMREG_IP, alloc_size);
4748 ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
4750 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset + alloc_size);
4752 if (cfg->frame_reg != ARMREG_SP) {
4753 ARM_MOV_REG_REG (code, cfg->frame_reg, ARMREG_SP);
4754 mono_emit_unwind_op_def_cfa_reg (cfg, code, cfg->frame_reg);
4756 //g_print ("prev_sp_offset: %d, alloc_size:%d\n", prev_sp_offset, alloc_size);
4757 prev_sp_offset += alloc_size;
4759 /* compute max_offset in order to use short forward jumps
4760 * we could skip do it on arm because the immediate displacement
4761 * for jumps is large enough, it may be useful later for constant pools
4764 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4765 MonoInst *ins = bb->code;
4766 bb->max_offset = max_offset;
4768 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
4771 MONO_BB_FOR_EACH_INS (bb, ins)
4772 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
4775 /* store runtime generic context */
4776 if (cfg->rgctx_var) {
4777 MonoInst *ins = cfg->rgctx_var;
4779 g_assert (ins->opcode == OP_REGOFFSET);
4781 if (arm_is_imm12 (ins->inst_offset)) {
4782 ARM_STR_IMM (code, MONO_ARCH_RGCTX_REG, ins->inst_basereg, ins->inst_offset);
4784 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4785 ARM_STR_REG_REG (code, MONO_ARCH_RGCTX_REG, ins->inst_basereg, ARMREG_LR);
4789 /* load arguments allocated to register from the stack */
4792 cinfo = get_call_info (cfg->generic_sharing_context, NULL, sig);
4794 if (MONO_TYPE_ISSTRUCT (sig->ret) && cinfo->ret.storage != RegTypeStructByVal) {
4795 ArgInfo *ainfo = &cinfo->ret;
4796 inst = cfg->vret_addr;
4797 g_assert (arm_is_imm12 (inst->inst_offset));
4798 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4801 if (sig->call_convention == MONO_CALL_VARARG) {
4802 ArgInfo *cookie = &cinfo->sig_cookie;
4804 /* Save the sig cookie address */
4805 g_assert (cookie->storage == RegTypeBase);
4807 g_assert (arm_is_imm12 (prev_sp_offset + cookie->offset));
4808 g_assert (arm_is_imm12 (cfg->sig_cookie));
4809 ARM_ADD_REG_IMM8 (code, ARMREG_IP, cfg->frame_reg, prev_sp_offset + cookie->offset);
4810 ARM_STR_IMM (code, ARMREG_IP, cfg->frame_reg, cfg->sig_cookie);
4813 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
4814 ArgInfo *ainfo = cinfo->args + i;
4815 inst = cfg->args [pos];
4817 if (cfg->verbose_level > 2)
4818 g_print ("Saving argument %d (type: %d)\n", i, ainfo->storage);
4819 if (inst->opcode == OP_REGVAR) {
4820 if (ainfo->storage == RegTypeGeneral)
4821 ARM_MOV_REG_REG (code, inst->dreg, ainfo->reg);
4822 else if (ainfo->storage == RegTypeFP) {
4823 g_assert_not_reached ();
4824 } else if (ainfo->storage == RegTypeBase) {
4825 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
4826 ARM_LDR_IMM (code, inst->dreg, ARMREG_SP, (prev_sp_offset + ainfo->offset));
4828 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4829 ARM_LDR_REG_REG (code, inst->dreg, ARMREG_SP, ARMREG_IP);
4832 g_assert_not_reached ();
4834 if (cfg->verbose_level > 2)
4835 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
4837 /* the argument should be put on the stack: FIXME handle size != word */
4838 if (ainfo->storage == RegTypeGeneral || ainfo->storage == RegTypeIRegPair) {
4839 switch (ainfo->size) {
4841 if (arm_is_imm12 (inst->inst_offset))
4842 ARM_STRB_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4844 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4845 ARM_STRB_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
4849 if (arm_is_imm8 (inst->inst_offset)) {
4850 ARM_STRH_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4852 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4853 ARM_STRH_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
4857 g_assert (arm_is_imm12 (inst->inst_offset));
4858 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4859 g_assert (arm_is_imm12 (inst->inst_offset + 4));
4860 ARM_STR_IMM (code, ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
4863 if (arm_is_imm12 (inst->inst_offset)) {
4864 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4866 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4867 ARM_STR_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
4871 } else if (ainfo->storage == RegTypeBaseGen) {
4872 g_assert (arm_is_imm12 (prev_sp_offset + ainfo->offset));
4873 g_assert (arm_is_imm12 (inst->inst_offset));
4874 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
4875 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset + 4);
4876 ARM_STR_IMM (code, ARMREG_R3, inst->inst_basereg, inst->inst_offset);
4877 } else if (ainfo->storage == RegTypeBase) {
4878 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
4879 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
4881 code = mono_arm_emit_load_imm (code, ARMREG_IP, prev_sp_offset + ainfo->offset);
4882 ARM_LDR_REG_REG (code, ARMREG_LR, ARMREG_SP, ARMREG_IP);
4885 switch (ainfo->size) {
4887 if (arm_is_imm8 (inst->inst_offset)) {
4888 ARM_STRB_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
4890 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4891 ARM_STRB_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
4895 if (arm_is_imm8 (inst->inst_offset)) {
4896 ARM_STRH_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
4898 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4899 ARM_STRH_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
4903 if (arm_is_imm12 (inst->inst_offset)) {
4904 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
4906 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4907 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
4909 if (arm_is_imm12 (prev_sp_offset + ainfo->offset + 4)) {
4910 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset + 4));
4912 code = mono_arm_emit_load_imm (code, ARMREG_IP, prev_sp_offset + ainfo->offset + 4);
4913 ARM_LDR_REG_REG (code, ARMREG_LR, ARMREG_SP, ARMREG_IP);
4915 if (arm_is_imm12 (inst->inst_offset + 4)) {
4916 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset + 4);
4918 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset + 4);
4919 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
4923 if (arm_is_imm12 (inst->inst_offset)) {
4924 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
4926 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4927 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
4931 } else if (ainfo->storage == RegTypeFP) {
4932 g_assert_not_reached ();
4933 } else if (ainfo->storage == RegTypeStructByVal) {
4934 int doffset = inst->inst_offset;
4938 size = mini_type_stack_size_full (cfg->generic_sharing_context, inst->inst_vtype, NULL, sig->pinvoke);
4939 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
4940 if (arm_is_imm12 (doffset)) {
4941 ARM_STR_IMM (code, ainfo->reg + cur_reg, inst->inst_basereg, doffset);
4943 code = mono_arm_emit_load_imm (code, ARMREG_IP, doffset);
4944 ARM_STR_REG_REG (code, ainfo->reg + cur_reg, inst->inst_basereg, ARMREG_IP);
4946 soffset += sizeof (gpointer);
4947 doffset += sizeof (gpointer);
4949 if (ainfo->vtsize) {
4950 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
4951 //g_print ("emit_memcpy (prev_sp_ofs: %d, ainfo->offset: %d, soffset: %d)\n", prev_sp_offset, ainfo->offset, soffset);
4952 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ARMREG_SP, prev_sp_offset + ainfo->offset);
4954 } else if (ainfo->storage == RegTypeStructByAddr) {
4955 g_assert_not_reached ();
4956 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
4957 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
4959 g_assert_not_reached ();
4964 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
4965 if (cfg->compile_aot)
4966 /* AOT code is only used in the root domain */
4967 code = mono_arm_emit_load_imm (code, ARMREG_R0, 0);
4969 code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->domain);
4970 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
4971 (gpointer)"mono_jit_thread_attach");
4972 code = emit_call_seq (cfg, code);
4975 if (method->save_lmf) {
4976 gboolean get_lmf_fast = FALSE;
4978 #ifdef HAVE_AEABI_READ_TP
4979 gint32 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
4981 if (lmf_addr_tls_offset != -1) {
4982 get_lmf_fast = TRUE;
4984 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
4985 (gpointer)"__aeabi_read_tp");
4986 code = emit_call_seq (cfg, code);
4988 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, lmf_addr_tls_offset);
4989 get_lmf_fast = TRUE;
4992 if (!get_lmf_fast) {
4993 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
4994 (gpointer)"mono_get_lmf_addr");
4995 code = emit_call_seq (cfg, code);
4997 /* we build the MonoLMF structure on the stack - see mini-arm.h */
4998 /* lmf_offset is the offset from the previous stack pointer,
4999 * alloc_size is the total stack space allocated, so the offset
5000 * of MonoLMF from the current stack ptr is alloc_size - lmf_offset.
5001 * The pointer to the struct is put in r1 (new_lmf).
5002 * r2 is used as scratch
5003 * The callee-saved registers are already in the MonoLMF structure
5005 code = emit_big_add (code, ARMREG_R1, ARMREG_SP, alloc_size - lmf_offset);
5006 /* r0 is the result from mono_get_lmf_addr () */
5007 ARM_STR_IMM (code, ARMREG_R0, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
5008 /* new_lmf->previous_lmf = *lmf_addr */
5009 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
5010 ARM_STR_IMM (code, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
5011 /* *(lmf_addr) = r1 */
5012 ARM_STR_IMM (code, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
5013 /* Skip method (only needed for trampoline LMF frames) */
5014 ARM_STR_IMM (code, ARMREG_SP, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, esp));
5015 /* save the current IP */
5016 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_PC);
5017 ARM_STR_IMM (code, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, eip));
5021 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
5023 if (cfg->arch.seq_point_info_var) {
5024 MonoInst *ins = cfg->arch.seq_point_info_var;
5026 /* Initialize the variable from a GOT slot */
5027 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_SEQ_POINT_INFO, cfg->method);
5028 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
5030 *(gpointer*)code = NULL;
5032 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
5034 g_assert (ins->opcode == OP_REGOFFSET);
5036 if (arm_is_imm12 (ins->inst_offset)) {
5037 ARM_STR_IMM (code, ARMREG_R0, ins->inst_basereg, ins->inst_offset);
5039 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
5040 ARM_STR_REG_REG (code, ARMREG_R0, ins->inst_basereg, ARMREG_LR);
5044 /* Initialize ss_trigger_page_var */
5046 MonoInst *info_var = cfg->arch.seq_point_info_var;
5047 MonoInst *ss_trigger_page_var = cfg->arch.ss_trigger_page_var;
5048 int dreg = ARMREG_LR;
5051 g_assert (info_var->opcode == OP_REGOFFSET);
5052 g_assert (arm_is_imm12 (info_var->inst_offset));
5054 ARM_LDR_IMM (code, dreg, info_var->inst_basereg, info_var->inst_offset);
5055 /* Load the trigger page addr */
5056 ARM_LDR_IMM (code, dreg, dreg, G_STRUCT_OFFSET (SeqPointInfo, ss_trigger_page));
5057 ARM_STR_IMM (code, dreg, ss_trigger_page_var->inst_basereg, ss_trigger_page_var->inst_offset);
5061 cfg->code_len = code - cfg->native_code;
5062 g_assert (cfg->code_len < cfg->code_size);
5069 mono_arch_emit_epilog (MonoCompile *cfg)
5071 MonoMethod *method = cfg->method;
5072 int pos, i, rot_amount;
5073 int max_epilog_size = 16 + 20*4;
5077 if (cfg->method->save_lmf)
5078 max_epilog_size += 128;
5080 if (mono_jit_trace_calls != NULL)
5081 max_epilog_size += 50;
5083 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
5084 max_epilog_size += 50;
5086 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
5087 cfg->code_size *= 2;
5088 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5089 mono_jit_stats.code_reallocs++;
5093 * Keep in sync with OP_JMP
5095 code = cfg->native_code + cfg->code_len;
5097 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
5098 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
5102 /* Load returned vtypes into registers if needed */
5103 cinfo = cfg->arch.cinfo;
5104 if (cinfo->ret.storage == RegTypeStructByVal) {
5105 MonoInst *ins = cfg->ret;
5107 if (arm_is_imm12 (ins->inst_offset)) {
5108 ARM_LDR_IMM (code, ARMREG_R0, ins->inst_basereg, ins->inst_offset);
5110 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
5111 ARM_LDR_REG_REG (code, ARMREG_R0, ins->inst_basereg, ARMREG_LR);
5115 if (method->save_lmf) {
5116 int lmf_offset, reg, sp_adj, regmask;
5117 /* all but r0-r3, sp and pc */
5118 pos += sizeof (MonoLMF) - (MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t));
5120 /* r2 contains the pointer to the current LMF */
5121 code = emit_big_add (code, ARMREG_R2, cfg->frame_reg, cfg->stack_usage - lmf_offset);
5122 /* ip = previous_lmf */
5123 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R2, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
5125 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_R2, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
5126 /* *(lmf_addr) = previous_lmf */
5127 ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
5128 /* This points to r4 inside MonoLMF->iregs */
5129 sp_adj = (sizeof (MonoLMF) - MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t));
5131 regmask = 0x9ff0; /* restore lr to pc */
5132 /* Skip caller saved registers not used by the method */
5133 while (!(cfg->used_int_regs & (1 << reg)) && reg < ARMREG_FP) {
5134 regmask &= ~(1 << reg);
5138 /* point sp at the registers to restore: 10 is 14 -4, because we skip r0-r3 */
5139 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_R2, sp_adj);
5141 ARM_POP (code, regmask);
5143 if ((i = mono_arm_is_rotated_imm8 (cfg->stack_usage, &rot_amount)) >= 0) {
5144 ARM_ADD_REG_IMM (code, ARMREG_SP, cfg->frame_reg, i, rot_amount);
5146 code = mono_arm_emit_load_imm (code, ARMREG_IP, cfg->stack_usage);
5147 ARM_ADD_REG_REG (code, ARMREG_SP, cfg->frame_reg, ARMREG_IP);
5151 /* Restore saved gregs */
5152 if (cfg->used_int_regs)
5153 ARM_POP (code, cfg->used_int_regs);
5154 /* Restore saved r7, restore LR to PC */
5155 ARM_POP (code, (1 << ARMREG_R7) | (1 << ARMREG_PC));
5157 ARM_POP (code, cfg->used_int_regs | (1 << ARMREG_PC));
5161 cfg->code_len = code - cfg->native_code;
5163 g_assert (cfg->code_len < cfg->code_size);
5167 /* remove once throw_exception_by_name is eliminated */
5169 exception_id_by_name (const char *name)
5171 if (strcmp (name, "IndexOutOfRangeException") == 0)
5172 return MONO_EXC_INDEX_OUT_OF_RANGE;
5173 if (strcmp (name, "OverflowException") == 0)
5174 return MONO_EXC_OVERFLOW;
5175 if (strcmp (name, "ArithmeticException") == 0)
5176 return MONO_EXC_ARITHMETIC;
5177 if (strcmp (name, "DivideByZeroException") == 0)
5178 return MONO_EXC_DIVIDE_BY_ZERO;
5179 if (strcmp (name, "InvalidCastException") == 0)
5180 return MONO_EXC_INVALID_CAST;
5181 if (strcmp (name, "NullReferenceException") == 0)
5182 return MONO_EXC_NULL_REF;
5183 if (strcmp (name, "ArrayTypeMismatchException") == 0)
5184 return MONO_EXC_ARRAY_TYPE_MISMATCH;
5185 if (strcmp (name, "ArgumentException") == 0)
5186 return MONO_EXC_ARGUMENT;
5187 g_error ("Unknown intrinsic exception %s\n", name);
5192 mono_arch_emit_exceptions (MonoCompile *cfg)
5194 MonoJumpInfo *patch_info;
5197 guint8* exc_throw_pos [MONO_EXC_INTRINS_NUM];
5198 guint8 exc_throw_found [MONO_EXC_INTRINS_NUM];
5199 int max_epilog_size = 50;
5201 for (i = 0; i < MONO_EXC_INTRINS_NUM; i++) {
5202 exc_throw_pos [i] = NULL;
5203 exc_throw_found [i] = 0;
5206 /* count the number of exception infos */
5209 * make sure we have enough space for exceptions
5211 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5212 if (patch_info->type == MONO_PATCH_INFO_EXC) {
5213 i = exception_id_by_name (patch_info->data.target);
5214 if (!exc_throw_found [i]) {
5215 max_epilog_size += 32;
5216 exc_throw_found [i] = TRUE;
5221 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
5222 cfg->code_size *= 2;
5223 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5224 mono_jit_stats.code_reallocs++;
5227 code = cfg->native_code + cfg->code_len;
5229 /* add code to raise exceptions */
5230 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5231 switch (patch_info->type) {
5232 case MONO_PATCH_INFO_EXC: {
5233 MonoClass *exc_class;
5234 unsigned char *ip = patch_info->ip.i + cfg->native_code;
5236 i = exception_id_by_name (patch_info->data.target);
5237 if (exc_throw_pos [i]) {
5238 arm_patch (ip, exc_throw_pos [i]);
5239 patch_info->type = MONO_PATCH_INFO_NONE;
5242 exc_throw_pos [i] = code;
5244 arm_patch (ip, code);
5246 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
5247 g_assert (exc_class);
5249 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR);
5250 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
5251 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5252 patch_info->data.name = "mono_arch_throw_corlib_exception";
5253 patch_info->ip.i = code - cfg->native_code;
5255 *(guint32*)(gpointer)code = exc_class->type_token;
5265 cfg->code_len = code - cfg->native_code;
5267 g_assert (cfg->code_len < cfg->code_size);
5271 #endif /* #ifndef DISABLE_JIT */
5273 static gboolean tls_offset_inited = FALSE;
5276 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
5278 if (!tls_offset_inited) {
5279 tls_offset_inited = TRUE;
5281 lmf_tls_offset = mono_get_lmf_tls_offset ();
5282 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
5287 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
5292 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5299 mono_arch_print_tree (MonoInst *tree, int arity)
5305 mono_arch_get_domain_intrinsic (MonoCompile* cfg)
5307 return mono_get_domain_intrinsic (cfg);
5311 mono_arch_get_patch_offset (guint8 *code)
5318 mono_arch_flush_register_windows (void)
5322 #ifdef MONO_ARCH_HAVE_IMT
5327 mono_arch_emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoInst *imt_arg)
5329 if (cfg->compile_aot) {
5330 int method_reg = mono_alloc_ireg (cfg);
5333 call->dynamic_imt_arg = TRUE;
5336 mono_call_inst_add_outarg_reg (cfg, call, imt_arg->dreg, ARMREG_V5, FALSE);
5338 MONO_INST_NEW (cfg, ins, OP_AOTCONST);
5339 ins->dreg = method_reg;
5340 ins->inst_p0 = call->method;
5341 ins->inst_c1 = MONO_PATCH_INFO_METHODCONST;
5342 MONO_ADD_INS (cfg->cbb, ins);
5344 mono_call_inst_add_outarg_reg (cfg, call, method_reg, ARMREG_V5, FALSE);
5346 } else if (cfg->generic_context || imt_arg || mono_use_llvm) {
5348 /* Always pass in a register for simplicity */
5349 call->dynamic_imt_arg = TRUE;
5351 cfg->uses_rgctx_reg = TRUE;
5354 mono_call_inst_add_outarg_reg (cfg, call, imt_arg->dreg, ARMREG_V5, FALSE);
5357 int method_reg = mono_alloc_preg (cfg);
5359 MONO_INST_NEW (cfg, ins, OP_PCONST);
5360 ins->inst_p0 = call->method;
5361 ins->dreg = method_reg;
5362 MONO_ADD_INS (cfg->cbb, ins);
5364 mono_call_inst_add_outarg_reg (cfg, call, method_reg, ARMREG_V5, FALSE);
5369 #endif /* DISABLE_JIT */
5372 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
5374 guint32 *code_ptr = (guint32*)code;
5379 return (MonoMethod*)regs [ARMREG_V5];
5381 /* The IMT value is stored in the code stream right after the LDC instruction. */
5382 if (!IS_LDR_PC (code_ptr [0])) {
5383 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]);
5384 g_assert (IS_LDR_PC (code_ptr [0]));
5386 if (code_ptr [1] == 0)
5387 /* This is AOTed code, the IMT method is in V5 */
5388 return (MonoMethod*)regs [ARMREG_V5];
5390 return (MonoMethod*) code_ptr [1];
5394 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
5396 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
5399 #define ENABLE_WRONG_METHOD_CHECK 0
5400 #define BASE_SIZE (6 * 4)
5401 #define BSEARCH_ENTRY_SIZE (4 * 4)
5402 #define CMP_SIZE (3 * 4)
5403 #define BRANCH_SIZE (1 * 4)
5404 #define CALL_SIZE (2 * 4)
5405 #define WMC_SIZE (5 * 4)
5406 #define DISTANCE(A, B) (((gint32)(B)) - ((gint32)(A)))
5409 arm_emit_value_and_patch_ldr (arminstr_t *code, arminstr_t *target, guint32 value)
5411 guint32 delta = DISTANCE (target, code);
5413 g_assert (delta >= 0 && delta <= 0xFFF);
5414 *target = *target | delta;
5420 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
5421 gpointer fail_tramp)
5423 int size, i, extra_space = 0;
5424 arminstr_t *code, *start, *vtable_target = NULL;
5425 gboolean large_offsets = FALSE;
5426 guint32 **constant_pool_starts;
5429 constant_pool_starts = g_new0 (guint32*, count);
5431 for (i = 0; i < count; ++i) {
5432 MonoIMTCheckItem *item = imt_entries [i];
5433 if (item->is_equals) {
5434 gboolean fail_case = !item->check_target_idx && fail_tramp;
5436 if (item->has_target_code || !arm_is_imm12 (DISTANCE (vtable, &vtable->vtable[item->value.vtable_slot]))) {
5437 item->chunk_size += 32;
5438 large_offsets = TRUE;
5441 if (item->check_target_idx || fail_case) {
5442 if (!item->compare_done || fail_case)
5443 item->chunk_size += CMP_SIZE;
5444 item->chunk_size += BRANCH_SIZE;
5446 #if ENABLE_WRONG_METHOD_CHECK
5447 item->chunk_size += WMC_SIZE;
5451 item->chunk_size += 16;
5452 large_offsets = TRUE;
5454 item->chunk_size += CALL_SIZE;
5456 item->chunk_size += BSEARCH_ENTRY_SIZE;
5457 imt_entries [item->check_target_idx]->compare_done = TRUE;
5459 size += item->chunk_size;
5463 size += 4 * count; /* The ARM_ADD_REG_IMM to pop the stack */
5466 code = mono_method_alloc_generic_virtual_thunk (domain, size);
5468 code = mono_domain_code_reserve (domain, size);
5472 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);
5473 for (i = 0; i < count; ++i) {
5474 MonoIMTCheckItem *item = imt_entries [i];
5475 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);
5480 ARM_PUSH4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5482 ARM_PUSH2 (code, ARMREG_R0, ARMREG_R1);
5483 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_LR, -4);
5484 vtable_target = code;
5485 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
5487 if (mono_use_llvm) {
5488 /* LLVM always passes the IMT method in R5 */
5489 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_V5);
5491 /* R0 == 0 means we are called from AOT code. In this case, V5 contains the IMT method */
5492 ARM_CMP_REG_IMM8 (code, ARMREG_R0, 0);
5493 ARM_MOV_REG_REG_COND (code, ARMREG_R0, ARMREG_V5, ARMCOND_EQ);
5496 for (i = 0; i < count; ++i) {
5497 MonoIMTCheckItem *item = imt_entries [i];
5498 arminstr_t *imt_method = NULL, *vtable_offset_ins = NULL, *target_code_ins = NULL;
5499 gint32 vtable_offset;
5501 item->code_target = (guint8*)code;
5503 if (item->is_equals) {
5504 gboolean fail_case = !item->check_target_idx && fail_tramp;
5506 if (item->check_target_idx || fail_case) {
5507 if (!item->compare_done || fail_case) {
5509 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5510 ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
5512 item->jmp_code = (guint8*)code;
5513 ARM_B_COND (code, ARMCOND_NE, 0);
5515 /*Enable the commented code to assert on wrong method*/
5516 #if ENABLE_WRONG_METHOD_CHECK
5518 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5519 ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
5520 ARM_B_COND (code, ARMCOND_NE, 1);
5526 if (item->has_target_code) {
5527 target_code_ins = code;
5528 /* Load target address */
5529 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5530 /* Save it to the fourth slot */
5531 ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
5532 /* Restore registers and branch */
5533 ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5535 code = arm_emit_value_and_patch_ldr (code, target_code_ins, (gsize)item->value.target_code);
5537 vtable_offset = DISTANCE (vtable, &vtable->vtable[item->value.vtable_slot]);
5538 if (!arm_is_imm12 (vtable_offset)) {
5540 * We need to branch to a computed address but we don't have
5541 * a free register to store it, since IP must contain the
5542 * vtable address. So we push the two values to the stack, and
5543 * load them both using LDM.
5545 /* Compute target address */
5546 vtable_offset_ins = code;
5547 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5548 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_IP, ARMREG_R1);
5549 /* Save it to the fourth slot */
5550 ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
5551 /* Restore registers and branch */
5552 ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5554 code = arm_emit_value_and_patch_ldr (code, vtable_offset_ins, vtable_offset);
5556 ARM_POP2 (code, ARMREG_R0, ARMREG_R1);
5558 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 2 * sizeof (gpointer));
5559 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, vtable_offset);
5564 arm_patch (item->jmp_code, (guchar*)code);
5566 target_code_ins = code;
5567 /* Load target address */
5568 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5569 /* Save it to the fourth slot */
5570 ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
5571 /* Restore registers and branch */
5572 ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5574 code = arm_emit_value_and_patch_ldr (code, target_code_ins, (gsize)fail_tramp);
5575 item->jmp_code = NULL;
5579 code = arm_emit_value_and_patch_ldr (code, imt_method, (guint32)item->key);
5581 /*must emit after unconditional branch*/
5582 if (vtable_target) {
5583 code = arm_emit_value_and_patch_ldr (code, vtable_target, (guint32)vtable);
5584 item->chunk_size += 4;
5585 vtable_target = NULL;
5588 /*We reserve the space for bsearch IMT values after the first entry with an absolute jump*/
5589 constant_pool_starts [i] = code;
5591 code += extra_space;
5595 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5596 ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
5598 item->jmp_code = (guint8*)code;
5599 ARM_B_COND (code, ARMCOND_GE, 0);
5604 for (i = 0; i < count; ++i) {
5605 MonoIMTCheckItem *item = imt_entries [i];
5606 if (item->jmp_code) {
5607 if (item->check_target_idx)
5608 arm_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
5610 if (i > 0 && item->is_equals) {
5612 arminstr_t *space_start = constant_pool_starts [i];
5613 for (j = i - 1; j >= 0 && !imt_entries [j]->is_equals; --j) {
5614 space_start = arm_emit_value_and_patch_ldr (space_start, (arminstr_t*)imt_entries [j]->code_target, (guint32)imt_entries [j]->key);
5621 char *buff = g_strdup_printf ("thunk_for_class_%s_%s_entries_%d", vtable->klass->name_space, vtable->klass->name, count);
5622 mono_disassemble_code (NULL, (guint8*)start, size, buff);
5627 g_free (constant_pool_starts);
5629 mono_arch_flush_icache ((guint8*)start, size);
5630 mono_stats.imt_thunks_size += code - start;
5632 g_assert (DISTANCE (start, code) <= size);
5639 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
5641 if (reg == ARMREG_SP)
5642 return (gpointer)ctx->esp;
5644 return (gpointer)ctx->regs [reg];
5648 * mono_arch_get_trampolines:
5650 * Return a list of MonoTrampInfo structures describing arch specific trampolines
5654 mono_arch_get_trampolines (gboolean aot)
5656 return mono_arm_get_exception_trampolines (aot);
5660 * mono_arch_set_breakpoint:
5662 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
5663 * The location should contain code emitted by OP_SEQ_POINT.
5666 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
5669 guint32 native_offset = ip - (guint8*)ji->code_start;
5672 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
5674 g_assert (native_offset % 4 == 0);
5675 g_assert (info->bp_addrs [native_offset / 4] == 0);
5676 info->bp_addrs [native_offset / 4] = bp_trigger_page;
5678 int dreg = ARMREG_LR;
5680 /* Read from another trigger page */
5681 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
5683 *(int*)code = (int)bp_trigger_page;
5685 ARM_LDR_IMM (code, dreg, dreg, 0);
5687 mono_arch_flush_icache (code - 16, 16);
5690 /* This is currently implemented by emitting an SWI instruction, which
5691 * qemu/linux seems to convert to a SIGILL.
5693 *(int*)code = (0xef << 24) | 8;
5695 mono_arch_flush_icache (code - 4, 4);
5701 * mono_arch_clear_breakpoint:
5703 * Clear the breakpoint at IP.
5706 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
5712 guint32 native_offset = ip - (guint8*)ji->code_start;
5713 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
5715 g_assert (native_offset % 4 == 0);
5716 g_assert (info->bp_addrs [native_offset / 4] == bp_trigger_page);
5717 info->bp_addrs [native_offset / 4] = 0;
5719 for (i = 0; i < 4; ++i)
5722 mono_arch_flush_icache (ip, code - ip);
5727 * mono_arch_start_single_stepping:
5729 * Start single stepping.
5732 mono_arch_start_single_stepping (void)
5734 mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
5738 * mono_arch_stop_single_stepping:
5740 * Stop single stepping.
5743 mono_arch_stop_single_stepping (void)
5745 mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
5749 #define DBG_SIGNAL SIGBUS
5751 #define DBG_SIGNAL SIGSEGV
5755 * mono_arch_is_single_step_event:
5757 * Return whenever the machine state in SIGCTX corresponds to a single
5761 mono_arch_is_single_step_event (void *info, void *sigctx)
5763 siginfo_t *sinfo = info;
5765 /* Sometimes the address is off by 4 */
5766 if (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128)
5773 * mono_arch_is_breakpoint_event:
5775 * Return whenever the machine state in SIGCTX corresponds to a breakpoint event.
5778 mono_arch_is_breakpoint_event (void *info, void *sigctx)
5780 siginfo_t *sinfo = info;
5782 if (sinfo->si_signo == DBG_SIGNAL) {
5783 /* Sometimes the address is off by 4 */
5784 if (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128)
5794 mono_arch_get_ip_for_breakpoint (MonoJitInfo *ji, MonoContext *ctx)
5796 guint8 *ip = MONO_CONTEXT_GET_IP (ctx);
5807 mono_arch_get_ip_for_single_step (MonoJitInfo *ji, MonoContext *ctx)
5809 guint8 *ip = MONO_CONTEXT_GET_IP (ctx);
5817 * mono_arch_skip_breakpoint:
5819 * See mini-amd64.c for docs.
5822 mono_arch_skip_breakpoint (MonoContext *ctx)
5824 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 4);
5828 * mono_arch_skip_single_step:
5830 * See mini-amd64.c for docs.
5833 mono_arch_skip_single_step (MonoContext *ctx)
5835 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 4);
5839 * mono_arch_get_seq_point_info:
5841 * See mini-amd64.c for docs.
5844 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
5849 // FIXME: Add a free function
5851 mono_domain_lock (domain);
5852 info = g_hash_table_lookup (domain_jit_info (domain)->arch_seq_points,
5854 mono_domain_unlock (domain);
5857 ji = mono_jit_info_table_find (domain, (char*)code);
5860 info = g_malloc0 (sizeof (SeqPointInfo) + ji->code_size);
5862 info->ss_trigger_page = ss_trigger_page;
5863 info->bp_trigger_page = bp_trigger_page;
5865 mono_domain_lock (domain);
5866 g_hash_table_insert (domain_jit_info (domain)->arch_seq_points,
5868 mono_domain_unlock (domain);
5875 * mono_arch_set_target:
5877 * Set the target architecture the JIT backend should generate code for, in the form
5878 * of a GNU target triplet. Only used in AOT mode.
5881 mono_arch_set_target (char *mtriple)
5883 /* The GNU target triple format is not very well documented */
5884 if (strstr (mtriple, "armv7")) {
5885 v6_supported = TRUE;
5886 v7_supported = TRUE;
5888 if (strstr (mtriple, "armv6")) {
5889 v6_supported = TRUE;
5891 if (strstr (mtriple, "darwin")) {
5892 v5_supported = TRUE;
5893 thumb_supported = TRUE;
5897 if (strstr (mtriple, "gnueabi"))
5898 eabi_supported = TRUE;