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);
276 * Emit code to push an LMF structure on the LMF stack.
277 * On arm, this is intermixed with the initialization of other fields of the structure.
280 emit_save_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset)
282 gboolean get_lmf_fast = FALSE;
284 #ifdef HAVE_AEABI_READ_TP
285 gint32 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
287 if (lmf_addr_tls_offset != -1) {
290 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
291 (gpointer)"__aeabi_read_tp");
292 code = emit_call_seq (cfg, code);
294 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, lmf_addr_tls_offset);
299 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
300 (gpointer)"mono_get_lmf_addr");
301 code = emit_call_seq (cfg, code);
303 /* we build the MonoLMF structure on the stack - see mini-arm.h */
304 /* lmf_offset is the offset from the previous stack pointer,
305 * alloc_size is the total stack space allocated, so the offset
306 * of MonoLMF from the current stack ptr is alloc_size - lmf_offset.
307 * The pointer to the struct is put in r1 (new_lmf).
308 * ip is used as scratch
309 * The callee-saved registers are already in the MonoLMF structure
311 code = emit_big_add (code, ARMREG_R1, ARMREG_SP, lmf_offset);
312 /* r0 is the result from mono_get_lmf_addr () */
313 ARM_STR_IMM (code, ARMREG_R0, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
314 /* new_lmf->previous_lmf = *lmf_addr */
315 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
316 ARM_STR_IMM (code, ARMREG_IP, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
317 /* *(lmf_addr) = r1 */
318 ARM_STR_IMM (code, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
319 /* Skip method (only needed for trampoline LMF frames) */
320 ARM_STR_IMM (code, ARMREG_SP, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, esp));
321 /* save the current IP */
322 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_PC);
323 ARM_STR_IMM (code, ARMREG_IP, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, eip));
331 * Emit code to pop an LMF structure from the LMF stack.
334 emit_restore_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset)
338 if (lmf_offset < 32) {
339 basereg = cfg->frame_reg;
344 code = emit_big_add (code, ARMREG_R2, cfg->frame_reg, lmf_offset);
347 /* ip = previous_lmf */
348 ARM_LDR_IMM (code, ARMREG_IP, basereg, offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf));
350 ARM_LDR_IMM (code, ARMREG_LR, basereg, offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr));
351 /* *(lmf_addr) = previous_lmf */
352 ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
357 #endif /* #ifndef DISABLE_JIT */
360 * mono_arch_get_argument_info:
361 * @csig: a method signature
362 * @param_count: the number of parameters to consider
363 * @arg_info: an array to store the result infos
365 * Gathers information on parameters such as size, alignment and
366 * padding. arg_info should be large enought to hold param_count + 1 entries.
368 * Returns the size of the activation frame.
371 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
373 int k, frame_size = 0;
374 guint32 size, align, pad;
377 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
378 frame_size += sizeof (gpointer);
382 arg_info [0].offset = offset;
385 frame_size += sizeof (gpointer);
389 arg_info [0].size = frame_size;
391 for (k = 0; k < param_count; k++) {
392 size = mini_type_stack_size_full (NULL, csig->params [k], &align, csig->pinvoke);
394 /* ignore alignment for now */
397 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
398 arg_info [k].pad = pad;
400 arg_info [k + 1].pad = 0;
401 arg_info [k + 1].size = size;
403 arg_info [k + 1].offset = offset;
407 align = MONO_ARCH_FRAME_ALIGNMENT;
408 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
409 arg_info [k].pad = pad;
414 #define MAX_ARCH_DELEGATE_PARAMS 3
417 get_delegate_invoke_impl (gboolean has_target, gboolean param_count, guint32 *code_size)
419 guint8 *code, *start;
422 start = code = mono_global_codeman_reserve (12);
424 /* Replace the this argument with the target */
425 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R0, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
426 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, G_STRUCT_OFFSET (MonoDelegate, target));
427 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
429 g_assert ((code - start) <= 12);
431 mono_arch_flush_icache (start, 12);
435 size = 8 + param_count * 4;
436 start = code = mono_global_codeman_reserve (size);
438 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R0, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
439 /* slide down the arguments */
440 for (i = 0; i < param_count; ++i) {
441 ARM_MOV_REG_REG (code, (ARMREG_R0 + i), (ARMREG_R0 + i + 1));
443 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
445 g_assert ((code - start) <= size);
447 mono_arch_flush_icache (start, size);
451 *code_size = code - start;
457 * mono_arch_get_delegate_invoke_impls:
459 * Return a list of MonoAotTrampInfo structures for the delegate invoke impl
463 mono_arch_get_delegate_invoke_impls (void)
470 code = get_delegate_invoke_impl (TRUE, 0, &code_len);
471 res = g_slist_prepend (res, mono_tramp_info_create (g_strdup ("delegate_invoke_impl_has_target"), code, code_len, NULL, NULL));
473 for (i = 0; i <= MAX_ARCH_DELEGATE_PARAMS; ++i) {
474 code = get_delegate_invoke_impl (FALSE, i, &code_len);
475 res = g_slist_prepend (res, mono_tramp_info_create (g_strdup_printf ("delegate_invoke_impl_target_%d", i), code, code_len, NULL, NULL));
482 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
484 guint8 *code, *start;
486 /* FIXME: Support more cases */
487 if (MONO_TYPE_ISSTRUCT (sig->ret))
491 static guint8* cached = NULL;
492 mono_mini_arch_lock ();
494 mono_mini_arch_unlock ();
499 start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
501 start = get_delegate_invoke_impl (TRUE, 0, NULL);
503 mono_mini_arch_unlock ();
506 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
509 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
511 for (i = 0; i < sig->param_count; ++i)
512 if (!mono_is_regsize_var (sig->params [i]))
515 mono_mini_arch_lock ();
516 code = cache [sig->param_count];
518 mono_mini_arch_unlock ();
523 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
524 start = mono_aot_get_trampoline (name);
527 start = get_delegate_invoke_impl (FALSE, sig->param_count, NULL);
529 cache [sig->param_count] = start;
530 mono_mini_arch_unlock ();
538 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
540 return (gpointer)regs [ARMREG_R0];
544 * Initialize the cpu to execute managed code.
547 mono_arch_cpu_init (void)
549 #if defined(__ARM_EABI__)
550 eabi_supported = TRUE;
552 #if defined(__APPLE__) && defined(MONO_CROSS_COMPILE)
555 i8_align = __alignof__ (gint64);
560 * Initialize architecture specific code.
563 mono_arch_init (void)
565 InitializeCriticalSection (&mini_arch_mutex);
567 ss_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT);
568 bp_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT);
569 mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
571 mono_aot_register_jit_icall ("mono_arm_throw_exception", mono_arm_throw_exception);
572 mono_aot_register_jit_icall ("mono_arm_throw_exception_by_token", mono_arm_throw_exception_by_token);
573 mono_aot_register_jit_icall ("mono_arm_resume_unwind", mono_arm_resume_unwind);
577 * Cleanup architecture specific code.
580 mono_arch_cleanup (void)
585 * This function returns the optimizations supported on this cpu.
588 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
591 const char *cpu_arch = getenv ("MONO_CPU_ARCH");
592 if (cpu_arch != NULL) {
593 thumb_supported = strstr (cpu_arch, "thumb") != NULL;
594 if (strncmp (cpu_arch, "armv", 4) == 0) {
595 v5_supported = cpu_arch [4] >= '5';
596 v6_supported = cpu_arch [4] >= '6';
597 v7_supported = cpu_arch [4] >= '7';
601 thumb_supported = TRUE;
608 FILE *file = fopen ("/proc/cpuinfo", "r");
610 while ((line = fgets (buf, 512, file))) {
611 if (strncmp (line, "Processor", 9) == 0) {
612 char *ver = strstr (line, "(v");
613 if (ver && (ver [2] == '5' || ver [2] == '6' || ver [2] == '7'))
615 if (ver && (ver [2] == '6' || ver [2] == '7'))
617 if (ver && (ver [2] == '7'))
621 if (strncmp (line, "Features", 8) == 0) {
622 char *th = strstr (line, "thumb");
624 thumb_supported = TRUE;
632 /*printf ("features: v5: %d, thumb: %d\n", v5_supported, thumb_supported);*/
637 /* no arm-specific optimizations yet */
645 is_regsize_var (MonoType *t) {
648 t = mini_type_get_underlying_type (NULL, t);
655 case MONO_TYPE_FNPTR:
657 case MONO_TYPE_OBJECT:
658 case MONO_TYPE_STRING:
659 case MONO_TYPE_CLASS:
660 case MONO_TYPE_SZARRAY:
661 case MONO_TYPE_ARRAY:
663 case MONO_TYPE_GENERICINST:
664 if (!mono_type_generic_inst_is_valuetype (t))
667 case MONO_TYPE_VALUETYPE:
674 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
679 for (i = 0; i < cfg->num_varinfo; i++) {
680 MonoInst *ins = cfg->varinfo [i];
681 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
684 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
687 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
690 /* we can only allocate 32 bit values */
691 if (is_regsize_var (ins->inst_vtype)) {
692 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
693 g_assert (i == vmv->idx);
694 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
701 #define USE_EXTRA_TEMPS 0
704 mono_arch_get_global_int_regs (MonoCompile *cfg)
708 mono_arch_compute_omit_fp (cfg);
711 * FIXME: Interface calls might go through a static rgctx trampoline which
712 * sets V5, but it doesn't save it, so we need to save it ourselves, and
715 if (cfg->flags & MONO_CFG_HAS_CALLS)
716 cfg->uses_rgctx_reg = TRUE;
718 if (cfg->arch.omit_fp)
719 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_FP));
720 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V1));
721 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V2));
722 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V3));
724 /* V4=R7 is used as a frame pointer, but V7=R10 is preserved */
725 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V7));
727 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V4));
728 if (!(cfg->compile_aot || cfg->uses_rgctx_reg || COMPILE_LLVM (cfg)))
729 /* V5 is reserved for passing the vtable/rgctx/IMT method */
730 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V5));
731 /*regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V6));*/
732 /*regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V7));*/
738 * mono_arch_regalloc_cost:
740 * Return the cost, in number of memory references, of the action of
741 * allocating the variable VMV into a register during global register
745 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
751 #endif /* #ifndef DISABLE_JIT */
753 #ifndef __GNUC_PREREQ
754 #define __GNUC_PREREQ(maj, min) (0)
758 mono_arch_flush_icache (guint8 *code, gint size)
761 sys_icache_invalidate (code, size);
762 #elif __GNUC_PREREQ(4, 1)
763 __clear_cache (code, code + size);
764 #elif defined(PLATFORM_ANDROID)
765 const int syscall = 0xf0002;
773 : "r" (code), "r" (code + size), "r" (syscall)
774 : "r0", "r1", "r7", "r2"
777 __asm __volatile ("mov r0, %0\n"
780 "swi 0x9f0002 @ sys_cacheflush"
782 : "r" (code), "r" (code + size), "r" (0)
783 : "r0", "r1", "r3" );
800 guint16 vtsize; /* in param area */
804 guint8 size : 4; /* 1, 2, 4, 8, or regs used by RegTypeStructByVal */
810 gboolean vtype_retaddr;
811 /* The index of the vret arg in the argument list */
821 /*#define __alignof__(a) sizeof(a)*/
822 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
828 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
831 if (*gr > ARMREG_R3) {
832 ainfo->offset = *stack_size;
833 ainfo->reg = ARMREG_SP; /* in the caller */
834 ainfo->storage = RegTypeBase;
837 ainfo->storage = RegTypeGeneral;
844 split = i8_align == 4;
848 if (*gr == ARMREG_R3 && split) {
849 /* first word in r3 and the second on the stack */
850 ainfo->offset = *stack_size;
851 ainfo->reg = ARMREG_SP; /* in the caller */
852 ainfo->storage = RegTypeBaseGen;
854 } else if (*gr >= ARMREG_R3) {
855 if (eabi_supported) {
856 /* darwin aligns longs to 4 byte only */
862 ainfo->offset = *stack_size;
863 ainfo->reg = ARMREG_SP; /* in the caller */
864 ainfo->storage = RegTypeBase;
867 if (eabi_supported) {
868 if (i8_align == 8 && ((*gr) & 1))
871 ainfo->storage = RegTypeIRegPair;
880 get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSignature *sig)
883 int n = sig->hasthis + sig->param_count;
884 MonoType *simpletype;
885 guint32 stack_size = 0;
887 gboolean is_pinvoke = sig->pinvoke;
890 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
892 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
897 /* FIXME: handle returning a struct */
898 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
901 if (is_pinvoke && mono_class_native_size (mono_class_from_mono_type (sig->ret), &align) <= sizeof (gpointer)) {
902 cinfo->ret.storage = RegTypeStructByVal;
904 cinfo->vtype_retaddr = TRUE;
911 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
912 * the first argument, allowing 'this' to be always passed in the first arg reg.
913 * Also do this if the first argument is a reference type, since virtual calls
914 * are sometimes made using calli without sig->hasthis set, like in the delegate
917 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]))))) {
919 add_general (&gr, &stack_size, cinfo->args + 0, TRUE);
921 add_general (&gr, &stack_size, &cinfo->args [sig->hasthis + 0], TRUE);
925 add_general (&gr, &stack_size, &cinfo->ret, TRUE);
926 cinfo->vret_arg_index = 1;
930 add_general (&gr, &stack_size, cinfo->args + 0, TRUE);
934 if (cinfo->vtype_retaddr)
935 add_general (&gr, &stack_size, &cinfo->ret, TRUE);
938 DEBUG(printf("params: %d\n", sig->param_count));
939 for (i = pstart; i < sig->param_count; ++i) {
940 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
941 /* Prevent implicit arguments and sig_cookie from
942 being passed in registers */
944 /* Emit the signature cookie just before the implicit arguments */
945 add_general (&gr, &stack_size, &cinfo->sig_cookie, TRUE);
947 DEBUG(printf("param %d: ", i));
948 if (sig->params [i]->byref) {
949 DEBUG(printf("byref\n"));
950 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
954 simpletype = mini_type_get_underlying_type (NULL, sig->params [i]);
955 switch (simpletype->type) {
956 case MONO_TYPE_BOOLEAN:
959 cinfo->args [n].size = 1;
960 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
966 cinfo->args [n].size = 2;
967 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
972 cinfo->args [n].size = 4;
973 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
979 case MONO_TYPE_FNPTR:
980 case MONO_TYPE_CLASS:
981 case MONO_TYPE_OBJECT:
982 case MONO_TYPE_STRING:
983 case MONO_TYPE_SZARRAY:
984 case MONO_TYPE_ARRAY:
986 cinfo->args [n].size = sizeof (gpointer);
987 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
990 case MONO_TYPE_GENERICINST:
991 if (!mono_type_generic_inst_is_valuetype (simpletype)) {
992 cinfo->args [n].size = sizeof (gpointer);
993 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
998 case MONO_TYPE_TYPEDBYREF:
999 case MONO_TYPE_VALUETYPE: {
1005 if (simpletype->type == MONO_TYPE_TYPEDBYREF) {
1006 size = sizeof (MonoTypedRef);
1007 align = sizeof (gpointer);
1009 MonoClass *klass = mono_class_from_mono_type (sig->params [i]);
1011 size = mono_class_native_size (klass, &align);
1013 size = mono_class_value_size (klass, &align);
1015 DEBUG(printf ("load %d bytes struct\n",
1016 mono_class_native_size (sig->params [i]->data.klass, NULL)));
1019 align_size += (sizeof (gpointer) - 1);
1020 align_size &= ~(sizeof (gpointer) - 1);
1021 nwords = (align_size + sizeof (gpointer) -1 ) / sizeof (gpointer);
1022 cinfo->args [n].storage = RegTypeStructByVal;
1023 cinfo->args [n].struct_size = size;
1024 /* FIXME: align stack_size if needed */
1025 if (eabi_supported) {
1026 if (align >= 8 && (gr & 1))
1029 if (gr > ARMREG_R3) {
1030 cinfo->args [n].size = 0;
1031 cinfo->args [n].vtsize = nwords;
1033 int rest = ARMREG_R3 - gr + 1;
1034 int n_in_regs = rest >= nwords? nwords: rest;
1036 cinfo->args [n].size = n_in_regs;
1037 cinfo->args [n].vtsize = nwords - n_in_regs;
1038 cinfo->args [n].reg = gr;
1040 nwords -= n_in_regs;
1042 cinfo->args [n].offset = stack_size;
1043 /*g_print ("offset for arg %d at %d\n", n, stack_size);*/
1044 stack_size += nwords * sizeof (gpointer);
1051 cinfo->args [n].size = 8;
1052 add_general (&gr, &stack_size, cinfo->args + n, FALSE);
1056 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
1060 /* Handle the case where there are no implicit arguments */
1061 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1062 /* Prevent implicit arguments and sig_cookie from
1063 being passed in registers */
1065 /* Emit the signature cookie just before the implicit arguments */
1066 add_general (&gr, &stack_size, &cinfo->sig_cookie, TRUE);
1070 simpletype = mini_type_get_underlying_type (NULL, sig->ret);
1071 switch (simpletype->type) {
1072 case MONO_TYPE_BOOLEAN:
1077 case MONO_TYPE_CHAR:
1083 case MONO_TYPE_FNPTR:
1084 case MONO_TYPE_CLASS:
1085 case MONO_TYPE_OBJECT:
1086 case MONO_TYPE_SZARRAY:
1087 case MONO_TYPE_ARRAY:
1088 case MONO_TYPE_STRING:
1089 cinfo->ret.storage = RegTypeGeneral;
1090 cinfo->ret.reg = ARMREG_R0;
1094 cinfo->ret.storage = RegTypeIRegPair;
1095 cinfo->ret.reg = ARMREG_R0;
1099 cinfo->ret.storage = RegTypeFP;
1100 cinfo->ret.reg = ARMREG_R0;
1101 /* FIXME: cinfo->ret.reg = ???;
1102 cinfo->ret.storage = RegTypeFP;*/
1104 case MONO_TYPE_GENERICINST:
1105 if (!mono_type_generic_inst_is_valuetype (simpletype)) {
1106 cinfo->ret.storage = RegTypeGeneral;
1107 cinfo->ret.reg = ARMREG_R0;
1111 case MONO_TYPE_VALUETYPE:
1112 case MONO_TYPE_TYPEDBYREF:
1113 if (cinfo->ret.storage != RegTypeStructByVal)
1114 cinfo->ret.storage = RegTypeStructByAddr;
1116 case MONO_TYPE_VOID:
1119 g_error ("Can't handle as return value 0x%x", sig->ret->type);
1123 /* align stack size to 8 */
1124 DEBUG (printf (" stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
1125 stack_size = (stack_size + 7) & ~7;
1127 cinfo->stack_usage = stack_size;
1133 G_GNUC_UNUSED static void
1138 G_GNUC_UNUSED static gboolean
1141 static int count = 0;
1144 if (!getenv ("COUNT"))
1147 if (count == atoi (getenv ("COUNT"))) {
1151 if (count > atoi (getenv ("COUNT"))) {
1159 debug_omit_fp (void)
1162 return debug_count ();
1169 * mono_arch_compute_omit_fp:
1171 * Determine whenever the frame pointer can be eliminated.
1174 mono_arch_compute_omit_fp (MonoCompile *cfg)
1176 MonoMethodSignature *sig;
1177 MonoMethodHeader *header;
1181 if (cfg->arch.omit_fp_computed)
1184 header = cfg->header;
1186 sig = mono_method_signature (cfg->method);
1188 if (!cfg->arch.cinfo)
1189 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1190 cinfo = cfg->arch.cinfo;
1193 * FIXME: Remove some of the restrictions.
1195 cfg->arch.omit_fp = TRUE;
1196 cfg->arch.omit_fp_computed = TRUE;
1198 if (cfg->disable_omit_fp)
1199 cfg->arch.omit_fp = FALSE;
1200 if (!debug_omit_fp ())
1201 cfg->arch.omit_fp = FALSE;
1203 if (cfg->method->save_lmf)
1204 cfg->arch.omit_fp = FALSE;
1206 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1207 cfg->arch.omit_fp = FALSE;
1208 if (header->num_clauses)
1209 cfg->arch.omit_fp = FALSE;
1210 if (cfg->param_area)
1211 cfg->arch.omit_fp = FALSE;
1212 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1213 cfg->arch.omit_fp = FALSE;
1214 if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
1215 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
1216 cfg->arch.omit_fp = FALSE;
1217 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1218 ArgInfo *ainfo = &cinfo->args [i];
1220 if (ainfo->storage == RegTypeBase || ainfo->storage == RegTypeBaseGen || ainfo->storage == RegTypeStructByVal) {
1222 * The stack offset can only be determined when the frame
1225 cfg->arch.omit_fp = FALSE;
1230 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1231 MonoInst *ins = cfg->varinfo [i];
1234 locals_size += mono_type_size (ins->inst_vtype, &ialign);
1239 * Set var information according to the calling convention. arm version.
1240 * The locals var stuff should most likely be split in another method.
1243 mono_arch_allocate_vars (MonoCompile *cfg)
1245 MonoMethodSignature *sig;
1246 MonoMethodHeader *header;
1248 int i, offset, size, align, curinst;
1252 sig = mono_method_signature (cfg->method);
1254 if (!cfg->arch.cinfo)
1255 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1256 cinfo = cfg->arch.cinfo;
1258 mono_arch_compute_omit_fp (cfg);
1260 if (cfg->arch.omit_fp)
1261 cfg->frame_reg = ARMREG_SP;
1263 cfg->frame_reg = ARMREG_FP;
1265 cfg->flags |= MONO_CFG_HAS_SPILLUP;
1267 /* allow room for the vararg method args: void* and long/double */
1268 if (mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method))
1269 cfg->param_area = MAX (cfg->param_area, sizeof (gpointer)*8);
1271 header = cfg->header;
1273 /* See mono_arch_get_global_int_regs () */
1274 if (cfg->flags & MONO_CFG_HAS_CALLS)
1275 cfg->uses_rgctx_reg = TRUE;
1277 if (cfg->frame_reg != ARMREG_SP)
1278 cfg->used_int_regs |= 1 << cfg->frame_reg;
1280 if (cfg->compile_aot || cfg->uses_rgctx_reg || COMPILE_LLVM (cfg))
1281 /* V5 is reserved for passing the vtable/rgctx/IMT method */
1282 cfg->used_int_regs |= (1 << ARMREG_V5);
1286 if (!MONO_TYPE_ISSTRUCT (sig->ret)) {
1287 switch (mini_type_get_underlying_type (NULL, sig->ret)->type) {
1288 case MONO_TYPE_VOID:
1291 cfg->ret->opcode = OP_REGVAR;
1292 cfg->ret->inst_c0 = ARMREG_R0;
1296 /* local vars are at a positive offset from the stack pointer */
1298 * also note that if the function uses alloca, we use FP
1299 * to point at the local variables.
1301 offset = 0; /* linkage area */
1302 /* align the offset to 16 bytes: not sure this is needed here */
1304 //offset &= ~(8 - 1);
1306 /* add parameter area size for called functions */
1307 offset += cfg->param_area;
1310 if (cfg->flags & MONO_CFG_HAS_FPOUT)
1313 /* allow room to save the return value */
1314 if (mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method))
1317 /* the MonoLMF structure is stored just below the stack pointer */
1318 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
1319 if (cinfo->ret.storage == RegTypeStructByVal) {
1320 cfg->ret->opcode = OP_REGOFFSET;
1321 cfg->ret->inst_basereg = cfg->frame_reg;
1322 offset += sizeof (gpointer) - 1;
1323 offset &= ~(sizeof (gpointer) - 1);
1324 cfg->ret->inst_offset = - offset;
1326 ins = cfg->vret_addr;
1327 offset += sizeof(gpointer) - 1;
1328 offset &= ~(sizeof(gpointer) - 1);
1329 ins->inst_offset = offset;
1330 ins->opcode = OP_REGOFFSET;
1331 ins->inst_basereg = cfg->frame_reg;
1332 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1333 printf ("vret_addr =");
1334 mono_print_ins (cfg->vret_addr);
1337 offset += sizeof(gpointer);
1340 /* Allocate these first so they have a small offset, OP_SEQ_POINT depends on this */
1341 if (cfg->arch.seq_point_info_var) {
1344 ins = cfg->arch.seq_point_info_var;
1348 offset += align - 1;
1349 offset &= ~(align - 1);
1350 ins->opcode = OP_REGOFFSET;
1351 ins->inst_basereg = cfg->frame_reg;
1352 ins->inst_offset = offset;
1355 ins = cfg->arch.ss_trigger_page_var;
1358 offset += align - 1;
1359 offset &= ~(align - 1);
1360 ins->opcode = OP_REGOFFSET;
1361 ins->inst_basereg = cfg->frame_reg;
1362 ins->inst_offset = offset;
1366 curinst = cfg->locals_start;
1367 for (i = curinst; i < cfg->num_varinfo; ++i) {
1368 ins = cfg->varinfo [i];
1369 if ((ins->flags & MONO_INST_IS_DEAD) || ins->opcode == OP_REGVAR || ins->opcode == OP_REGOFFSET)
1372 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
1373 * pinvoke wrappers when they call functions returning structure */
1374 if (ins->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (ins->inst_vtype) && ins->inst_vtype->type != MONO_TYPE_TYPEDBYREF) {
1375 size = mono_class_native_size (mono_class_from_mono_type (ins->inst_vtype), &ualign);
1379 size = mono_type_size (ins->inst_vtype, &align);
1381 /* FIXME: if a structure is misaligned, our memcpy doesn't work,
1382 * since it loads/stores misaligned words, which don't do the right thing.
1384 if (align < 4 && size >= 4)
1386 offset += align - 1;
1387 offset &= ~(align - 1);
1388 ins->opcode = OP_REGOFFSET;
1389 ins->inst_offset = offset;
1390 ins->inst_basereg = cfg->frame_reg;
1392 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
1397 ins = cfg->args [curinst];
1398 if (ins->opcode != OP_REGVAR) {
1399 ins->opcode = OP_REGOFFSET;
1400 ins->inst_basereg = cfg->frame_reg;
1401 offset += sizeof (gpointer) - 1;
1402 offset &= ~(sizeof (gpointer) - 1);
1403 ins->inst_offset = offset;
1404 offset += sizeof (gpointer);
1409 if (sig->call_convention == MONO_CALL_VARARG) {
1413 /* Allocate a local slot to hold the sig cookie address */
1414 offset += align - 1;
1415 offset &= ~(align - 1);
1416 cfg->sig_cookie = offset;
1420 for (i = 0; i < sig->param_count; ++i) {
1421 ins = cfg->args [curinst];
1423 if (ins->opcode != OP_REGVAR) {
1424 ins->opcode = OP_REGOFFSET;
1425 ins->inst_basereg = cfg->frame_reg;
1426 size = mini_type_stack_size_full (NULL, sig->params [i], &ualign, sig->pinvoke);
1428 /* FIXME: if a structure is misaligned, our memcpy doesn't work,
1429 * since it loads/stores misaligned words, which don't do the right thing.
1431 if (align < 4 && size >= 4)
1433 /* The code in the prolog () stores words when storing vtypes received in a register */
1434 if (MONO_TYPE_ISSTRUCT (sig->params [i]))
1436 offset += align - 1;
1437 offset &= ~(align - 1);
1438 ins->inst_offset = offset;
1444 /* align the offset to 8 bytes */
1449 cfg->stack_offset = offset;
1453 mono_arch_create_vars (MonoCompile *cfg)
1455 MonoMethodSignature *sig;
1458 sig = mono_method_signature (cfg->method);
1460 if (!cfg->arch.cinfo)
1461 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1462 cinfo = cfg->arch.cinfo;
1464 if (cinfo->ret.storage == RegTypeStructByVal)
1465 cfg->ret_var_is_local = TRUE;
1467 if (MONO_TYPE_ISSTRUCT (sig->ret) && cinfo->ret.storage != RegTypeStructByVal) {
1468 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
1469 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1470 printf ("vret_addr = ");
1471 mono_print_ins (cfg->vret_addr);
1475 if (cfg->gen_seq_points && cfg->compile_aot) {
1476 MonoInst *ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1477 ins->flags |= MONO_INST_VOLATILE;
1478 cfg->arch.seq_point_info_var = ins;
1480 /* Allocate a separate variable for this to save 1 load per seq point */
1481 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1482 ins->flags |= MONO_INST_VOLATILE;
1483 cfg->arch.ss_trigger_page_var = ins;
1488 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1490 MonoMethodSignature *tmp_sig;
1493 if (call->tail_call)
1496 /* FIXME: Add support for signature tokens to AOT */
1497 cfg->disable_aot = TRUE;
1499 g_assert (cinfo->sig_cookie.storage == RegTypeBase);
1502 * mono_ArgIterator_Setup assumes the signature cookie is
1503 * passed first and all the arguments which were before it are
1504 * passed on the stack after the signature. So compensate by
1505 * passing a different signature.
1507 tmp_sig = mono_metadata_signature_dup (call->signature);
1508 tmp_sig->param_count -= call->signature->sentinelpos;
1509 tmp_sig->sentinelpos = 0;
1510 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1512 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
1513 sig_arg->dreg = mono_alloc_ireg (cfg);
1514 sig_arg->inst_p0 = tmp_sig;
1515 MONO_ADD_INS (cfg->cbb, sig_arg);
1517 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, cinfo->sig_cookie.offset, sig_arg->dreg);
1522 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
1527 LLVMCallInfo *linfo;
1529 n = sig->param_count + sig->hasthis;
1531 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1533 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
1536 * LLVM always uses the native ABI while we use our own ABI, the
1537 * only difference is the handling of vtypes:
1538 * - we only pass/receive them in registers in some cases, and only
1539 * in 1 or 2 integer registers.
1541 if (cinfo->vtype_retaddr) {
1542 /* Vtype returned using a hidden argument */
1543 linfo->ret.storage = LLVMArgVtypeRetAddr;
1544 linfo->vret_arg_index = cinfo->vret_arg_index;
1545 } else if (cinfo->ret.storage != RegTypeGeneral && cinfo->ret.storage != RegTypeNone && cinfo->ret.storage != RegTypeFP && cinfo->ret.storage != RegTypeIRegPair) {
1546 cfg->exception_message = g_strdup ("unknown ret conv");
1547 cfg->disable_llvm = TRUE;
1551 for (i = 0; i < n; ++i) {
1552 ainfo = cinfo->args + i;
1554 linfo->args [i].storage = LLVMArgNone;
1556 switch (ainfo->storage) {
1557 case RegTypeGeneral:
1558 case RegTypeIRegPair:
1560 linfo->args [i].storage = LLVMArgInIReg;
1562 case RegTypeStructByVal:
1563 // FIXME: Passing entirely on the stack or split reg/stack
1564 if (ainfo->vtsize == 0 && ainfo->size <= 2) {
1565 linfo->args [i].storage = LLVMArgVtypeInReg;
1566 linfo->args [i].pair_storage [0] = LLVMArgInIReg;
1567 if (ainfo->size == 2)
1568 linfo->args [i].pair_storage [1] = LLVMArgInIReg;
1570 linfo->args [i].pair_storage [1] = LLVMArgNone;
1572 cfg->exception_message = g_strdup_printf ("vtype-by-val on stack");
1573 cfg->disable_llvm = TRUE;
1577 cfg->exception_message = g_strdup_printf ("ainfo->storage (%d)", ainfo->storage);
1578 cfg->disable_llvm = TRUE;
1588 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
1591 MonoMethodSignature *sig;
1595 sig = call->signature;
1596 n = sig->param_count + sig->hasthis;
1598 cinfo = get_call_info (cfg->generic_sharing_context, NULL, sig);
1600 for (i = 0; i < n; ++i) {
1601 ArgInfo *ainfo = cinfo->args + i;
1604 if (i >= sig->hasthis)
1605 t = sig->params [i - sig->hasthis];
1607 t = &mono_defaults.int_class->byval_arg;
1608 t = mini_type_get_underlying_type (NULL, t);
1610 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1611 /* Emit the signature cookie just before the implicit arguments */
1612 emit_sig_cookie (cfg, call, cinfo);
1615 in = call->args [i];
1617 switch (ainfo->storage) {
1618 case RegTypeGeneral:
1619 case RegTypeIRegPair:
1620 if (!t->byref && ((t->type == MONO_TYPE_I8) || (t->type == MONO_TYPE_U8))) {
1621 MONO_INST_NEW (cfg, ins, OP_MOVE);
1622 ins->dreg = mono_alloc_ireg (cfg);
1623 ins->sreg1 = in->dreg + 1;
1624 MONO_ADD_INS (cfg->cbb, ins);
1625 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1627 MONO_INST_NEW (cfg, ins, OP_MOVE);
1628 ins->dreg = mono_alloc_ireg (cfg);
1629 ins->sreg1 = in->dreg + 2;
1630 MONO_ADD_INS (cfg->cbb, ins);
1631 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg + 1, FALSE);
1632 } else if (!t->byref && ((t->type == MONO_TYPE_R8) || (t->type == MONO_TYPE_R4))) {
1633 #ifndef MONO_ARCH_SOFT_FLOAT
1637 if (ainfo->size == 4) {
1638 #ifdef MONO_ARCH_SOFT_FLOAT
1639 /* mono_emit_call_args () have already done the r8->r4 conversion */
1640 /* The converted value is in an int vreg */
1641 MONO_INST_NEW (cfg, ins, OP_MOVE);
1642 ins->dreg = mono_alloc_ireg (cfg);
1643 ins->sreg1 = in->dreg;
1644 MONO_ADD_INS (cfg->cbb, ins);
1645 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1647 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, ARMREG_SP, (cfg->param_area - 8), in->dreg);
1648 creg = mono_alloc_ireg (cfg);
1649 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8));
1650 mono_call_inst_add_outarg_reg (cfg, call, creg, ainfo->reg, FALSE);
1653 #ifdef MONO_ARCH_SOFT_FLOAT
1654 MONO_INST_NEW (cfg, ins, OP_FGETLOW32);
1655 ins->dreg = mono_alloc_ireg (cfg);
1656 ins->sreg1 = in->dreg;
1657 MONO_ADD_INS (cfg->cbb, ins);
1658 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1660 MONO_INST_NEW (cfg, ins, OP_FGETHIGH32);
1661 ins->dreg = mono_alloc_ireg (cfg);
1662 ins->sreg1 = in->dreg;
1663 MONO_ADD_INS (cfg->cbb, ins);
1664 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg + 1, FALSE);
1666 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, ARMREG_SP, (cfg->param_area - 8), in->dreg);
1667 creg = mono_alloc_ireg (cfg);
1668 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8));
1669 mono_call_inst_add_outarg_reg (cfg, call, creg, ainfo->reg, FALSE);
1670 creg = mono_alloc_ireg (cfg);
1671 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8 + 4));
1672 mono_call_inst_add_outarg_reg (cfg, call, creg, ainfo->reg + 1, FALSE);
1675 cfg->flags |= MONO_CFG_HAS_FPOUT;
1677 MONO_INST_NEW (cfg, ins, OP_MOVE);
1678 ins->dreg = mono_alloc_ireg (cfg);
1679 ins->sreg1 = in->dreg;
1680 MONO_ADD_INS (cfg->cbb, ins);
1682 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1685 case RegTypeStructByAddr:
1688 /* FIXME: where si the data allocated? */
1689 arg->backend.reg3 = ainfo->reg;
1690 call->used_iregs |= 1 << ainfo->reg;
1691 g_assert_not_reached ();
1694 case RegTypeStructByVal:
1695 MONO_INST_NEW (cfg, ins, OP_OUTARG_VT);
1696 ins->opcode = OP_OUTARG_VT;
1697 ins->sreg1 = in->dreg;
1698 ins->klass = in->klass;
1699 ins->inst_p0 = call;
1700 ins->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
1701 memcpy (ins->inst_p1, ainfo, sizeof (ArgInfo));
1702 mono_call_inst_add_outarg_vt (cfg, call, ins);
1703 MONO_ADD_INS (cfg->cbb, ins);
1706 if (!t->byref && ((t->type == MONO_TYPE_I8) || (t->type == MONO_TYPE_U8))) {
1707 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1708 } else if (!t->byref && ((t->type == MONO_TYPE_R4) || (t->type == MONO_TYPE_R8))) {
1709 if (t->type == MONO_TYPE_R8) {
1710 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1712 #ifdef MONO_ARCH_SOFT_FLOAT
1713 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1715 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1719 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1722 case RegTypeBaseGen:
1723 if (!t->byref && ((t->type == MONO_TYPE_I8) || (t->type == MONO_TYPE_U8))) {
1724 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);
1725 MONO_INST_NEW (cfg, ins, OP_MOVE);
1726 ins->dreg = mono_alloc_ireg (cfg);
1727 ins->sreg1 = G_BYTE_ORDER == G_BIG_ENDIAN ? in->dreg + 2 : in->dreg + 1;
1728 MONO_ADD_INS (cfg->cbb, ins);
1729 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ARMREG_R3, FALSE);
1730 } else if (!t->byref && (t->type == MONO_TYPE_R8)) {
1733 /* This should work for soft-float as well */
1735 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, ARMREG_SP, (cfg->param_area - 8), in->dreg);
1736 creg = mono_alloc_ireg (cfg);
1737 mono_call_inst_add_outarg_reg (cfg, call, creg, ARMREG_R3, FALSE);
1738 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8));
1739 creg = mono_alloc_ireg (cfg);
1740 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 4));
1741 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, ainfo->offset, creg);
1742 cfg->flags |= MONO_CFG_HAS_FPOUT;
1744 g_assert_not_reached ();
1751 arg->backend.reg3 = ainfo->reg;
1752 /* FP args are passed in int regs */
1753 call->used_iregs |= 1 << ainfo->reg;
1754 if (ainfo->size == 8) {
1755 arg->opcode = OP_OUTARG_R8;
1756 call->used_iregs |= 1 << (ainfo->reg + 1);
1758 arg->opcode = OP_OUTARG_R4;
1761 cfg->flags |= MONO_CFG_HAS_FPOUT;
1765 g_assert_not_reached ();
1769 /* Handle the case where there are no implicit arguments */
1770 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos))
1771 emit_sig_cookie (cfg, call, cinfo);
1773 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
1776 if (cinfo->ret.storage == RegTypeStructByVal) {
1777 /* The JIT will transform this into a normal call */
1778 call->vret_in_reg = TRUE;
1780 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
1781 vtarg->sreg1 = call->vret_var->dreg;
1782 vtarg->dreg = mono_alloc_preg (cfg);
1783 MONO_ADD_INS (cfg->cbb, vtarg);
1785 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
1789 call->stack_usage = cinfo->stack_usage;
1795 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
1797 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
1798 ArgInfo *ainfo = ins->inst_p1;
1799 int ovf_size = ainfo->vtsize;
1800 int doffset = ainfo->offset;
1801 int struct_size = ainfo->struct_size;
1802 int i, soffset, dreg, tmpreg;
1805 for (i = 0; i < ainfo->size; ++i) {
1806 dreg = mono_alloc_ireg (cfg);
1807 switch (struct_size) {
1809 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, dreg, src->dreg, soffset);
1812 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, dreg, src->dreg, soffset);
1815 tmpreg = mono_alloc_ireg (cfg);
1816 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, dreg, src->dreg, soffset);
1817 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, tmpreg, src->dreg, soffset + 1);
1818 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, tmpreg, tmpreg, 8);
1819 MONO_EMIT_NEW_BIALU (cfg, OP_IOR, dreg, dreg, tmpreg);
1820 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, tmpreg, src->dreg, soffset + 2);
1821 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, tmpreg, tmpreg, 16);
1822 MONO_EMIT_NEW_BIALU (cfg, OP_IOR, dreg, dreg, tmpreg);
1825 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, soffset);
1828 mono_call_inst_add_outarg_reg (cfg, call, dreg, ainfo->reg + i, FALSE);
1829 soffset += sizeof (gpointer);
1830 struct_size -= sizeof (gpointer);
1832 //g_print ("vt size: %d at R%d + %d\n", doffset, vt->inst_basereg, vt->inst_offset);
1834 mini_emit_memcpy (cfg, ARMREG_SP, doffset, src->dreg, soffset, MIN (ovf_size * sizeof (gpointer), struct_size), struct_size < 4 ? 1 : 4);
1838 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
1840 MonoType *ret = mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret);
1843 if (ret->type == MONO_TYPE_I8 || ret->type == MONO_TYPE_U8) {
1846 if (COMPILE_LLVM (cfg)) {
1847 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
1849 MONO_INST_NEW (cfg, ins, OP_SETLRET);
1850 ins->sreg1 = val->dreg + 1;
1851 ins->sreg2 = val->dreg + 2;
1852 MONO_ADD_INS (cfg->cbb, ins);
1856 #ifdef MONO_ARCH_SOFT_FLOAT
1857 if (ret->type == MONO_TYPE_R8) {
1860 MONO_INST_NEW (cfg, ins, OP_SETFRET);
1861 ins->dreg = cfg->ret->dreg;
1862 ins->sreg1 = val->dreg;
1863 MONO_ADD_INS (cfg->cbb, ins);
1866 if (ret->type == MONO_TYPE_R4) {
1867 /* Already converted to an int in method_to_ir () */
1868 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
1871 #elif defined(ARM_FPU_VFP)
1872 if (ret->type == MONO_TYPE_R8 || ret->type == MONO_TYPE_R4) {
1875 MONO_INST_NEW (cfg, ins, OP_SETFRET);
1876 ins->dreg = cfg->ret->dreg;
1877 ins->sreg1 = val->dreg;
1878 MONO_ADD_INS (cfg->cbb, ins);
1882 if (ret->type == MONO_TYPE_R4 || ret->type == MONO_TYPE_R8) {
1883 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
1890 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
1893 #endif /* #ifndef DISABLE_JIT */
1896 mono_arch_is_inst_imm (gint64 imm)
1901 #define DYN_CALL_STACK_ARGS 6
1904 MonoMethodSignature *sig;
1909 mgreg_t regs [PARAM_REGS + DYN_CALL_STACK_ARGS];
1915 dyn_call_supported (CallInfo *cinfo, MonoMethodSignature *sig)
1919 if (sig->hasthis + sig->param_count > PARAM_REGS + DYN_CALL_STACK_ARGS)
1922 switch (cinfo->ret.storage) {
1924 case RegTypeGeneral:
1925 case RegTypeIRegPair:
1926 case RegTypeStructByAddr:
1931 #elif defined(ARM_FPU_VFP)
1940 for (i = 0; i < cinfo->nargs; ++i) {
1941 switch (cinfo->args [i].storage) {
1942 case RegTypeGeneral:
1944 case RegTypeIRegPair:
1947 if (cinfo->args [i].offset >= (DYN_CALL_STACK_ARGS * sizeof (gpointer)))
1950 case RegTypeStructByVal:
1951 if (cinfo->args [i].reg + cinfo->args [i].vtsize >= PARAM_REGS + DYN_CALL_STACK_ARGS)
1959 // FIXME: Can't use cinfo only as it doesn't contain info about I8/float */
1960 for (i = 0; i < sig->param_count; ++i) {
1961 MonoType *t = sig->params [i];
1969 #ifdef MONO_ARCH_SOFT_FLOAT
1988 mono_arch_dyn_call_prepare (MonoMethodSignature *sig)
1990 ArchDynCallInfo *info;
1993 cinfo = get_call_info (NULL, NULL, sig);
1995 if (!dyn_call_supported (cinfo, sig)) {
2000 info = g_new0 (ArchDynCallInfo, 1);
2001 // FIXME: Preprocess the info to speed up start_dyn_call ()
2003 info->cinfo = cinfo;
2005 return (MonoDynCallInfo*)info;
2009 mono_arch_dyn_call_free (MonoDynCallInfo *info)
2011 ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
2013 g_free (ainfo->cinfo);
2018 mono_arch_start_dyn_call (MonoDynCallInfo *info, gpointer **args, guint8 *ret, guint8 *buf, int buf_len)
2020 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2021 DynCallArgs *p = (DynCallArgs*)buf;
2022 int arg_index, greg, i, j, pindex;
2023 MonoMethodSignature *sig = dinfo->sig;
2025 g_assert (buf_len >= sizeof (DynCallArgs));
2034 if (sig->hasthis || dinfo->cinfo->vret_arg_index == 1) {
2035 p->regs [greg ++] = (mgreg_t)*(args [arg_index ++]);
2040 if (dinfo->cinfo->vtype_retaddr)
2041 p->regs [greg ++] = (mgreg_t)ret;
2043 for (i = pindex; i < sig->param_count; i++) {
2044 MonoType *t = mono_type_get_underlying_type (sig->params [i]);
2045 gpointer *arg = args [arg_index ++];
2046 ArgInfo *ainfo = &dinfo->cinfo->args [i + sig->hasthis];
2049 if (ainfo->storage == RegTypeGeneral || ainfo->storage == RegTypeIRegPair || ainfo->storage == RegTypeStructByVal)
2051 else if (ainfo->storage == RegTypeBase)
2052 slot = PARAM_REGS + (ainfo->offset / 4);
2054 g_assert_not_reached ();
2057 p->regs [slot] = (mgreg_t)*arg;
2062 case MONO_TYPE_STRING:
2063 case MONO_TYPE_CLASS:
2064 case MONO_TYPE_ARRAY:
2065 case MONO_TYPE_SZARRAY:
2066 case MONO_TYPE_OBJECT:
2070 p->regs [slot] = (mgreg_t)*arg;
2072 case MONO_TYPE_BOOLEAN:
2074 p->regs [slot] = *(guint8*)arg;
2077 p->regs [slot] = *(gint8*)arg;
2080 p->regs [slot] = *(gint16*)arg;
2083 case MONO_TYPE_CHAR:
2084 p->regs [slot] = *(guint16*)arg;
2087 p->regs [slot] = *(gint32*)arg;
2090 p->regs [slot] = *(guint32*)arg;
2094 p->regs [slot ++] = (mgreg_t)arg [0];
2095 p->regs [slot] = (mgreg_t)arg [1];
2098 p->regs [slot] = *(mgreg_t*)arg;
2101 p->regs [slot ++] = (mgreg_t)arg [0];
2102 p->regs [slot] = (mgreg_t)arg [1];
2104 case MONO_TYPE_GENERICINST:
2105 if (MONO_TYPE_IS_REFERENCE (t)) {
2106 p->regs [slot] = (mgreg_t)*arg;
2111 case MONO_TYPE_VALUETYPE:
2112 g_assert (ainfo->storage == RegTypeStructByVal);
2114 if (ainfo->size == 0)
2115 slot = PARAM_REGS + (ainfo->offset / 4);
2119 for (j = 0; j < ainfo->size + ainfo->vtsize; ++j)
2120 p->regs [slot ++] = ((mgreg_t*)arg) [j];
2123 g_assert_not_reached ();
2129 mono_arch_finish_dyn_call (MonoDynCallInfo *info, guint8 *buf)
2131 ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
2132 MonoMethodSignature *sig = ((ArchDynCallInfo*)info)->sig;
2133 guint8 *ret = ((DynCallArgs*)buf)->ret;
2134 mgreg_t res = ((DynCallArgs*)buf)->res;
2135 mgreg_t res2 = ((DynCallArgs*)buf)->res2;
2137 switch (mono_type_get_underlying_type (sig->ret)->type) {
2138 case MONO_TYPE_VOID:
2139 *(gpointer*)ret = NULL;
2141 case MONO_TYPE_STRING:
2142 case MONO_TYPE_CLASS:
2143 case MONO_TYPE_ARRAY:
2144 case MONO_TYPE_SZARRAY:
2145 case MONO_TYPE_OBJECT:
2149 *(gpointer*)ret = (gpointer)res;
2155 case MONO_TYPE_BOOLEAN:
2156 *(guint8*)ret = res;
2159 *(gint16*)ret = res;
2162 case MONO_TYPE_CHAR:
2163 *(guint16*)ret = res;
2166 *(gint32*)ret = res;
2169 *(guint32*)ret = res;
2173 /* This handles endianness as well */
2174 ((gint32*)ret) [0] = res;
2175 ((gint32*)ret) [1] = res2;
2177 case MONO_TYPE_GENERICINST:
2178 if (MONO_TYPE_IS_REFERENCE (sig->ret)) {
2179 *(gpointer*)ret = (gpointer)res;
2184 case MONO_TYPE_VALUETYPE:
2185 g_assert (ainfo->cinfo->vtype_retaddr);
2188 #if defined(ARM_FPU_VFP)
2190 *(float*)ret = *(float*)&res;
2192 case MONO_TYPE_R8: {
2198 *(double*)ret = *(double*)®s;
2203 g_assert_not_reached ();
2210 * Allow tracing to work with this interface (with an optional argument)
2214 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
2218 code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->method);
2219 ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0); /* NULL ebp for now */
2220 code = mono_arm_emit_load_imm (code, ARMREG_R2, (guint32)func);
2221 code = emit_call_reg (code, ARMREG_R2);
2234 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
2237 int save_mode = SAVE_NONE;
2239 MonoMethod *method = cfg->method;
2240 int rtype = mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret)->type;
2241 int save_offset = cfg->param_area;
2245 offset = code - cfg->native_code;
2246 /* we need about 16 instructions */
2247 if (offset > (cfg->code_size - 16 * 4)) {
2248 cfg->code_size *= 2;
2249 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2250 code = cfg->native_code + offset;
2253 case MONO_TYPE_VOID:
2254 /* special case string .ctor icall */
2255 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
2256 save_mode = SAVE_ONE;
2258 save_mode = SAVE_NONE;
2262 save_mode = SAVE_TWO;
2266 save_mode = SAVE_FP;
2268 case MONO_TYPE_VALUETYPE:
2269 save_mode = SAVE_STRUCT;
2272 save_mode = SAVE_ONE;
2276 switch (save_mode) {
2278 ARM_STR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2279 ARM_STR_IMM (code, ARMREG_R1, cfg->frame_reg, save_offset + 4);
2280 if (enable_arguments) {
2281 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_R1);
2282 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
2286 ARM_STR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2287 if (enable_arguments) {
2288 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
2292 /* FIXME: what reg? */
2293 if (enable_arguments) {
2294 /* FIXME: what reg? */
2298 if (enable_arguments) {
2299 /* FIXME: get the actual address */
2300 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
2308 code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->method);
2309 code = mono_arm_emit_load_imm (code, ARMREG_IP, (guint32)func);
2310 code = emit_call_reg (code, ARMREG_IP);
2312 switch (save_mode) {
2314 ARM_LDR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2315 ARM_LDR_IMM (code, ARMREG_R1, cfg->frame_reg, save_offset + 4);
2318 ARM_LDR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2332 * The immediate field for cond branches is big enough for all reasonable methods
2334 #define EMIT_COND_BRANCH_FLAGS(ins,condcode) \
2335 if (0 && ins->inst_true_bb->native_offset) { \
2336 ARM_B_COND (code, (condcode), (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffffff); \
2338 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
2339 ARM_B_COND (code, (condcode), 0); \
2342 #define EMIT_COND_BRANCH(ins,cond) EMIT_COND_BRANCH_FLAGS(ins, branch_cc_table [(cond)])
2344 /* emit an exception if condition is fail
2346 * We assign the extra code used to throw the implicit exceptions
2347 * to cfg->bb_exit as far as the big branch handling is concerned
2349 #define EMIT_COND_SYSTEM_EXCEPTION_FLAGS(condcode,exc_name) \
2351 mono_add_patch_info (cfg, code - cfg->native_code, \
2352 MONO_PATCH_INFO_EXC, exc_name); \
2353 ARM_BL_COND (code, (condcode), 0); \
2356 #define EMIT_COND_SYSTEM_EXCEPTION(cond,exc_name) EMIT_COND_SYSTEM_EXCEPTION_FLAGS(branch_cc_table [(cond)], (exc_name))
2359 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
2364 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
2366 MonoInst *ins, *n, *last_ins = NULL;
2368 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2369 switch (ins->opcode) {
2372 /* Already done by an arch-independent pass */
2374 case OP_LOAD_MEMBASE:
2375 case OP_LOADI4_MEMBASE:
2377 * OP_STORE_MEMBASE_REG reg, offset(basereg)
2378 * OP_LOAD_MEMBASE offset(basereg), reg
2380 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
2381 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
2382 ins->inst_basereg == last_ins->inst_destbasereg &&
2383 ins->inst_offset == last_ins->inst_offset) {
2384 if (ins->dreg == last_ins->sreg1) {
2385 MONO_DELETE_INS (bb, ins);
2388 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
2389 ins->opcode = OP_MOVE;
2390 ins->sreg1 = last_ins->sreg1;
2394 * Note: reg1 must be different from the basereg in the second load
2395 * OP_LOAD_MEMBASE offset(basereg), reg1
2396 * OP_LOAD_MEMBASE offset(basereg), reg2
2398 * OP_LOAD_MEMBASE offset(basereg), reg1
2399 * OP_MOVE reg1, reg2
2401 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
2402 || last_ins->opcode == OP_LOAD_MEMBASE) &&
2403 ins->inst_basereg != last_ins->dreg &&
2404 ins->inst_basereg == last_ins->inst_basereg &&
2405 ins->inst_offset == last_ins->inst_offset) {
2407 if (ins->dreg == last_ins->dreg) {
2408 MONO_DELETE_INS (bb, ins);
2411 ins->opcode = OP_MOVE;
2412 ins->sreg1 = last_ins->dreg;
2415 //g_assert_not_reached ();
2419 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2420 * OP_LOAD_MEMBASE offset(basereg), reg
2422 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2423 * OP_ICONST reg, imm
2425 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
2426 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
2427 ins->inst_basereg == last_ins->inst_destbasereg &&
2428 ins->inst_offset == last_ins->inst_offset) {
2429 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
2430 ins->opcode = OP_ICONST;
2431 ins->inst_c0 = last_ins->inst_imm;
2432 g_assert_not_reached (); // check this rule
2436 case OP_LOADU1_MEMBASE:
2437 case OP_LOADI1_MEMBASE:
2438 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
2439 ins->inst_basereg == last_ins->inst_destbasereg &&
2440 ins->inst_offset == last_ins->inst_offset) {
2441 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? OP_ICONV_TO_I1 : OP_ICONV_TO_U1;
2442 ins->sreg1 = last_ins->sreg1;
2445 case OP_LOADU2_MEMBASE:
2446 case OP_LOADI2_MEMBASE:
2447 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
2448 ins->inst_basereg == last_ins->inst_destbasereg &&
2449 ins->inst_offset == last_ins->inst_offset) {
2450 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_ICONV_TO_I2 : OP_ICONV_TO_U2;
2451 ins->sreg1 = last_ins->sreg1;
2455 ins->opcode = OP_MOVE;
2459 if (ins->dreg == ins->sreg1) {
2460 MONO_DELETE_INS (bb, ins);
2464 * OP_MOVE sreg, dreg
2465 * OP_MOVE dreg, sreg
2467 if (last_ins && last_ins->opcode == OP_MOVE &&
2468 ins->sreg1 == last_ins->dreg &&
2469 ins->dreg == last_ins->sreg1) {
2470 MONO_DELETE_INS (bb, ins);
2478 bb->last_ins = last_ins;
2482 * the branch_cc_table should maintain the order of these
2496 branch_cc_table [] = {
2510 #define NEW_INS(cfg,dest,op) do { \
2511 MONO_INST_NEW ((cfg), (dest), (op)); \
2512 mono_bblock_insert_before_ins (bb, ins, (dest)); \
2516 map_to_reg_reg_op (int op)
2525 case OP_COMPARE_IMM:
2527 case OP_ICOMPARE_IMM:
2541 case OP_LOAD_MEMBASE:
2542 return OP_LOAD_MEMINDEX;
2543 case OP_LOADI4_MEMBASE:
2544 return OP_LOADI4_MEMINDEX;
2545 case OP_LOADU4_MEMBASE:
2546 return OP_LOADU4_MEMINDEX;
2547 case OP_LOADU1_MEMBASE:
2548 return OP_LOADU1_MEMINDEX;
2549 case OP_LOADI2_MEMBASE:
2550 return OP_LOADI2_MEMINDEX;
2551 case OP_LOADU2_MEMBASE:
2552 return OP_LOADU2_MEMINDEX;
2553 case OP_LOADI1_MEMBASE:
2554 return OP_LOADI1_MEMINDEX;
2555 case OP_STOREI1_MEMBASE_REG:
2556 return OP_STOREI1_MEMINDEX;
2557 case OP_STOREI2_MEMBASE_REG:
2558 return OP_STOREI2_MEMINDEX;
2559 case OP_STOREI4_MEMBASE_REG:
2560 return OP_STOREI4_MEMINDEX;
2561 case OP_STORE_MEMBASE_REG:
2562 return OP_STORE_MEMINDEX;
2563 case OP_STORER4_MEMBASE_REG:
2564 return OP_STORER4_MEMINDEX;
2565 case OP_STORER8_MEMBASE_REG:
2566 return OP_STORER8_MEMINDEX;
2567 case OP_STORE_MEMBASE_IMM:
2568 return OP_STORE_MEMBASE_REG;
2569 case OP_STOREI1_MEMBASE_IMM:
2570 return OP_STOREI1_MEMBASE_REG;
2571 case OP_STOREI2_MEMBASE_IMM:
2572 return OP_STOREI2_MEMBASE_REG;
2573 case OP_STOREI4_MEMBASE_IMM:
2574 return OP_STOREI4_MEMBASE_REG;
2576 g_assert_not_reached ();
2580 * Remove from the instruction list the instructions that can't be
2581 * represented with very simple instructions with no register
2585 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
2587 MonoInst *ins, *temp, *last_ins = NULL;
2588 int rot_amount, imm8, low_imm;
2590 MONO_BB_FOR_EACH_INS (bb, ins) {
2592 switch (ins->opcode) {
2596 case OP_COMPARE_IMM:
2597 case OP_ICOMPARE_IMM:
2611 if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount)) < 0) {
2612 NEW_INS (cfg, temp, OP_ICONST);
2613 temp->inst_c0 = ins->inst_imm;
2614 temp->dreg = mono_alloc_ireg (cfg);
2615 ins->sreg2 = temp->dreg;
2616 ins->opcode = mono_op_imm_to_op (ins->opcode);
2618 if (ins->opcode == OP_SBB || ins->opcode == OP_ISBB || ins->opcode == OP_SUBCC)
2624 if (ins->inst_imm == 1) {
2625 ins->opcode = OP_MOVE;
2628 if (ins->inst_imm == 0) {
2629 ins->opcode = OP_ICONST;
2633 imm8 = mono_is_power_of_two (ins->inst_imm);
2635 ins->opcode = OP_SHL_IMM;
2636 ins->inst_imm = imm8;
2639 NEW_INS (cfg, temp, OP_ICONST);
2640 temp->inst_c0 = ins->inst_imm;
2641 temp->dreg = mono_alloc_ireg (cfg);
2642 ins->sreg2 = temp->dreg;
2643 ins->opcode = OP_IMUL;
2649 if (ins->next && (ins->next->opcode == OP_COND_EXC_C || ins->next->opcode == OP_COND_EXC_IC))
2650 /* ARM sets the C flag to 1 if there was _no_ overflow */
2651 ins->next->opcode = OP_COND_EXC_NC;
2653 case OP_LOCALLOC_IMM:
2654 NEW_INS (cfg, temp, OP_ICONST);
2655 temp->inst_c0 = ins->inst_imm;
2656 temp->dreg = mono_alloc_ireg (cfg);
2657 ins->sreg1 = temp->dreg;
2658 ins->opcode = OP_LOCALLOC;
2660 case OP_LOAD_MEMBASE:
2661 case OP_LOADI4_MEMBASE:
2662 case OP_LOADU4_MEMBASE:
2663 case OP_LOADU1_MEMBASE:
2664 /* we can do two things: load the immed in a register
2665 * and use an indexed load, or see if the immed can be
2666 * represented as an ad_imm + a load with a smaller offset
2667 * that fits. We just do the first for now, optimize later.
2669 if (arm_is_imm12 (ins->inst_offset))
2671 NEW_INS (cfg, temp, OP_ICONST);
2672 temp->inst_c0 = ins->inst_offset;
2673 temp->dreg = mono_alloc_ireg (cfg);
2674 ins->sreg2 = temp->dreg;
2675 ins->opcode = map_to_reg_reg_op (ins->opcode);
2677 case OP_LOADI2_MEMBASE:
2678 case OP_LOADU2_MEMBASE:
2679 case OP_LOADI1_MEMBASE:
2680 if (arm_is_imm8 (ins->inst_offset))
2682 NEW_INS (cfg, temp, OP_ICONST);
2683 temp->inst_c0 = ins->inst_offset;
2684 temp->dreg = mono_alloc_ireg (cfg);
2685 ins->sreg2 = temp->dreg;
2686 ins->opcode = map_to_reg_reg_op (ins->opcode);
2688 case OP_LOADR4_MEMBASE:
2689 case OP_LOADR8_MEMBASE:
2690 if (arm_is_fpimm8 (ins->inst_offset))
2692 low_imm = ins->inst_offset & 0x1ff;
2693 if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_offset & ~0x1ff, &rot_amount)) >= 0) {
2694 NEW_INS (cfg, temp, OP_ADD_IMM);
2695 temp->inst_imm = ins->inst_offset & ~0x1ff;
2696 temp->sreg1 = ins->inst_basereg;
2697 temp->dreg = mono_alloc_ireg (cfg);
2698 ins->inst_basereg = temp->dreg;
2699 ins->inst_offset = low_imm;
2702 /* VFP/FPA doesn't have indexed load instructions */
2703 g_assert_not_reached ();
2705 case OP_STORE_MEMBASE_REG:
2706 case OP_STOREI4_MEMBASE_REG:
2707 case OP_STOREI1_MEMBASE_REG:
2708 if (arm_is_imm12 (ins->inst_offset))
2710 NEW_INS (cfg, temp, OP_ICONST);
2711 temp->inst_c0 = ins->inst_offset;
2712 temp->dreg = mono_alloc_ireg (cfg);
2713 ins->sreg2 = temp->dreg;
2714 ins->opcode = map_to_reg_reg_op (ins->opcode);
2716 case OP_STOREI2_MEMBASE_REG:
2717 if (arm_is_imm8 (ins->inst_offset))
2719 NEW_INS (cfg, temp, OP_ICONST);
2720 temp->inst_c0 = ins->inst_offset;
2721 temp->dreg = mono_alloc_ireg (cfg);
2722 ins->sreg2 = temp->dreg;
2723 ins->opcode = map_to_reg_reg_op (ins->opcode);
2725 case OP_STORER4_MEMBASE_REG:
2726 case OP_STORER8_MEMBASE_REG:
2727 if (arm_is_fpimm8 (ins->inst_offset))
2729 low_imm = ins->inst_offset & 0x1ff;
2730 if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_offset & ~ 0x1ff, &rot_amount)) >= 0 && arm_is_fpimm8 (low_imm)) {
2731 NEW_INS (cfg, temp, OP_ADD_IMM);
2732 temp->inst_imm = ins->inst_offset & ~0x1ff;
2733 temp->sreg1 = ins->inst_destbasereg;
2734 temp->dreg = mono_alloc_ireg (cfg);
2735 ins->inst_destbasereg = temp->dreg;
2736 ins->inst_offset = low_imm;
2739 /*g_print ("fail with: %d (%d, %d)\n", ins->inst_offset, ins->inst_offset & ~0x1ff, low_imm);*/
2740 /* VFP/FPA doesn't have indexed store instructions */
2741 g_assert_not_reached ();
2743 case OP_STORE_MEMBASE_IMM:
2744 case OP_STOREI1_MEMBASE_IMM:
2745 case OP_STOREI2_MEMBASE_IMM:
2746 case OP_STOREI4_MEMBASE_IMM:
2747 NEW_INS (cfg, temp, OP_ICONST);
2748 temp->inst_c0 = ins->inst_imm;
2749 temp->dreg = mono_alloc_ireg (cfg);
2750 ins->sreg1 = temp->dreg;
2751 ins->opcode = map_to_reg_reg_op (ins->opcode);
2753 goto loop_start; /* make it handle the possibly big ins->inst_offset */
2755 gboolean swap = FALSE;
2759 /* Optimized away */
2764 /* Some fp compares require swapped operands */
2765 switch (ins->next->opcode) {
2767 ins->next->opcode = OP_FBLT;
2771 ins->next->opcode = OP_FBLT_UN;
2775 ins->next->opcode = OP_FBGE;
2779 ins->next->opcode = OP_FBGE_UN;
2787 ins->sreg1 = ins->sreg2;
2796 bb->last_ins = last_ins;
2797 bb->max_vreg = cfg->next_vreg;
2801 mono_arch_decompose_long_opts (MonoCompile *cfg, MonoInst *long_ins)
2805 if (long_ins->opcode == OP_LNEG) {
2807 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ARM_RSBS_IMM, ins->dreg + 1, ins->sreg1 + 1, 0);
2808 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ARM_RSC_IMM, ins->dreg + 2, ins->sreg1 + 2, 0);
2814 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
2816 /* sreg is a float, dreg is an integer reg */
2818 ARM_FIXZ (code, dreg, sreg);
2819 #elif defined(ARM_FPU_VFP)
2821 ARM_TOSIZD (code, ARM_VFP_F0, sreg);
2823 ARM_TOUIZD (code, ARM_VFP_F0, sreg);
2824 ARM_FMRS (code, dreg, ARM_VFP_F0);
2828 ARM_AND_REG_IMM8 (code, dreg, dreg, 0xff);
2829 else if (size == 2) {
2830 ARM_SHL_IMM (code, dreg, dreg, 16);
2831 ARM_SHR_IMM (code, dreg, dreg, 16);
2835 ARM_SHL_IMM (code, dreg, dreg, 24);
2836 ARM_SAR_IMM (code, dreg, dreg, 24);
2837 } else if (size == 2) {
2838 ARM_SHL_IMM (code, dreg, dreg, 16);
2839 ARM_SAR_IMM (code, dreg, dreg, 16);
2845 #endif /* #ifndef DISABLE_JIT */
2849 const guchar *target;
2854 #define is_call_imm(diff) ((gint)(diff) >= -33554432 && (gint)(diff) <= 33554431)
2857 search_thunk_slot (void *data, int csize, int bsize, void *user_data) {
2858 PatchData *pdata = (PatchData*)user_data;
2859 guchar *code = data;
2860 guint32 *thunks = data;
2861 guint32 *endthunks = (guint32*)(code + bsize);
2863 int difflow, diffhigh;
2865 /* always ensure a call from pdata->code can reach to the thunks without further thunks */
2866 difflow = (char*)pdata->code - (char*)thunks;
2867 diffhigh = (char*)pdata->code - (char*)endthunks;
2868 if (!((is_call_imm (thunks) && is_call_imm (endthunks)) || (is_call_imm (difflow) && is_call_imm (diffhigh))))
2872 * The thunk is composed of 3 words:
2873 * load constant from thunks [2] into ARM_IP
2876 * Note that the LR register is already setup
2878 //g_print ("thunk nentries: %d\n", ((char*)endthunks - (char*)thunks)/16);
2879 if ((pdata->found == 2) || (pdata->code >= code && pdata->code <= code + csize)) {
2880 while (thunks < endthunks) {
2881 //g_print ("looking for target: %p at %p (%08x-%08x)\n", pdata->target, thunks, thunks [0], thunks [1]);
2882 if (thunks [2] == (guint32)pdata->target) {
2883 arm_patch (pdata->code, (guchar*)thunks);
2884 mono_arch_flush_icache (pdata->code, 4);
2887 } else if ((thunks [0] == 0) && (thunks [1] == 0) && (thunks [2] == 0)) {
2888 /* found a free slot instead: emit thunk */
2889 /* ARMREG_IP is fine to use since this can't be an IMT call
2892 code = (guchar*)thunks;
2893 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
2894 if (thumb_supported)
2895 ARM_BX (code, ARMREG_IP);
2897 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
2898 thunks [2] = (guint32)pdata->target;
2899 mono_arch_flush_icache ((guchar*)thunks, 12);
2901 arm_patch (pdata->code, (guchar*)thunks);
2902 mono_arch_flush_icache (pdata->code, 4);
2906 /* skip 12 bytes, the size of the thunk */
2910 //g_print ("failed thunk lookup for %p from %p at %p (%d entries)\n", pdata->target, pdata->code, data, count);
2916 handle_thunk (MonoDomain *domain, int absolute, guchar *code, const guchar *target, MonoCodeManager *dyn_code_mp)
2921 domain = mono_domain_get ();
2924 pdata.target = target;
2925 pdata.absolute = absolute;
2929 mono_code_manager_foreach (dyn_code_mp, search_thunk_slot, &pdata);
2932 if (pdata.found != 1) {
2933 mono_domain_lock (domain);
2934 mono_domain_code_foreach (domain, search_thunk_slot, &pdata);
2937 /* this uses the first available slot */
2939 mono_domain_code_foreach (domain, search_thunk_slot, &pdata);
2941 mono_domain_unlock (domain);
2944 if (pdata.found != 1) {
2946 GHashTableIter iter;
2947 MonoJitDynamicMethodInfo *ji;
2950 * This might be a dynamic method, search its code manager. We can only
2951 * use the dynamic method containing CODE, since the others might be freed later.
2955 mono_domain_lock (domain);
2956 hash = domain_jit_info (domain)->dynamic_code_hash;
2958 /* FIXME: Speed this up */
2959 g_hash_table_iter_init (&iter, hash);
2960 while (g_hash_table_iter_next (&iter, NULL, (gpointer*)&ji)) {
2961 mono_code_manager_foreach (ji->code_mp, search_thunk_slot, &pdata);
2962 if (pdata.found == 1)
2966 mono_domain_unlock (domain);
2968 if (pdata.found != 1)
2969 g_print ("thunk failed for %p from %p\n", target, code);
2970 g_assert (pdata.found == 1);
2974 arm_patch_general (MonoDomain *domain, guchar *code, const guchar *target, MonoCodeManager *dyn_code_mp)
2976 guint32 *code32 = (void*)code;
2977 guint32 ins = *code32;
2978 guint32 prim = (ins >> 25) & 7;
2979 guint32 tval = GPOINTER_TO_UINT (target);
2981 //g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
2982 if (prim == 5) { /* 101b */
2983 /* the diff starts 8 bytes from the branch opcode */
2984 gint diff = target - code - 8;
2986 gint tmask = 0xffffffff;
2987 if (tval & 1) { /* entering thumb mode */
2988 diff = target - 1 - code - 8;
2989 g_assert (thumb_supported);
2990 tbits = 0xf << 28; /* bl->blx bit pattern */
2991 g_assert ((ins & (1 << 24))); /* it must be a bl, not b instruction */
2992 /* this low bit of the displacement is moved to bit 24 in the instruction encoding */
2996 tmask = ~(1 << 24); /* clear the link bit */
2997 /*g_print ("blx to thumb: target: %p, code: %p, diff: %d, mask: %x\n", target, code, diff, tmask);*/
3002 if (diff <= 33554431) {
3004 ins = (ins & 0xff000000) | diff;
3006 *code32 = ins | tbits;
3010 /* diff between 0 and -33554432 */
3011 if (diff >= -33554432) {
3013 ins = (ins & 0xff000000) | (diff & ~0xff000000);
3015 *code32 = ins | tbits;
3020 handle_thunk (domain, TRUE, code, target, dyn_code_mp);
3025 * The alternative call sequences looks like this:
3027 * ldr ip, [pc] // loads the address constant
3028 * b 1f // jumps around the constant
3029 * address constant embedded in the code
3034 * There are two cases for patching:
3035 * a) at the end of method emission: in this case code points to the start
3036 * of the call sequence
3037 * b) during runtime patching of the call site: in this case code points
3038 * to the mov pc, ip instruction
3040 * We have to handle also the thunk jump code sequence:
3044 * address constant // execution never reaches here
3046 if ((ins & 0x0ffffff0) == 0x12fff10) {
3047 /* Branch and exchange: the address is constructed in a reg
3048 * We can patch BX when the code sequence is the following:
3049 * ldr ip, [pc, #0] ; 0x8
3056 guint8 *emit = (guint8*)ccode;
3057 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
3059 ARM_MOV_REG_REG (emit, ARMREG_LR, ARMREG_PC);
3060 ARM_BX (emit, ARMREG_IP);
3062 /*patching from magic trampoline*/
3063 if (ins == ccode [3]) {
3064 g_assert (code32 [-4] == ccode [0]);
3065 g_assert (code32 [-3] == ccode [1]);
3066 g_assert (code32 [-1] == ccode [2]);
3067 code32 [-2] = (guint32)target;
3070 /*patching from JIT*/
3071 if (ins == ccode [0]) {
3072 g_assert (code32 [1] == ccode [1]);
3073 g_assert (code32 [3] == ccode [2]);
3074 g_assert (code32 [4] == ccode [3]);
3075 code32 [2] = (guint32)target;
3078 g_assert_not_reached ();
3079 } else if ((ins & 0x0ffffff0) == 0x12fff30) {
3087 guint8 *emit = (guint8*)ccode;
3088 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
3090 ARM_BLX_REG (emit, ARMREG_IP);
3092 g_assert (code32 [-3] == ccode [0]);
3093 g_assert (code32 [-2] == ccode [1]);
3094 g_assert (code32 [0] == ccode [2]);
3096 code32 [-1] = (guint32)target;
3099 guint32 *tmp = ccode;
3100 guint8 *emit = (guint8*)tmp;
3101 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
3102 ARM_MOV_REG_REG (emit, ARMREG_LR, ARMREG_PC);
3103 ARM_MOV_REG_REG (emit, ARMREG_PC, ARMREG_IP);
3104 ARM_BX (emit, ARMREG_IP);
3105 if (ins == ccode [2]) {
3106 g_assert_not_reached (); // should be -2 ...
3107 code32 [-1] = (guint32)target;
3110 if (ins == ccode [0]) {
3111 /* handles both thunk jump code and the far call sequence */
3112 code32 [2] = (guint32)target;
3115 g_assert_not_reached ();
3117 // g_print ("patched with 0x%08x\n", ins);
3121 arm_patch (guchar *code, const guchar *target)
3123 arm_patch_general (NULL, code, target, NULL);
3127 * Return the >= 0 uimm8 value if val can be represented with a byte + rotation
3128 * (with the rotation amount in *rot_amount. rot_amount is already adjusted
3129 * to be used with the emit macros.
3130 * Return -1 otherwise.
3133 mono_arm_is_rotated_imm8 (guint32 val, gint *rot_amount)
3136 for (i = 0; i < 31; i+= 2) {
3137 res = (val << (32 - i)) | (val >> i);
3140 *rot_amount = i? 32 - i: 0;
3147 * Emits in code a sequence of instructions that load the value 'val'
3148 * into the dreg register. Uses at most 4 instructions.
3151 mono_arm_emit_load_imm (guint8 *code, int dreg, guint32 val)
3153 int imm8, rot_amount;
3155 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
3156 /* skip the constant pool */
3162 if ((imm8 = mono_arm_is_rotated_imm8 (val, &rot_amount)) >= 0) {
3163 ARM_MOV_REG_IMM (code, dreg, imm8, rot_amount);
3164 } else if ((imm8 = mono_arm_is_rotated_imm8 (~val, &rot_amount)) >= 0) {
3165 ARM_MVN_REG_IMM (code, dreg, imm8, rot_amount);
3168 ARM_MOVW_REG_IMM (code, dreg, val & 0xffff);
3170 ARM_MOVT_REG_IMM (code, dreg, (val >> 16) & 0xffff);
3174 ARM_MOV_REG_IMM8 (code, dreg, (val & 0xFF));
3176 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF00) >> 8, 24);
3178 if (val & 0xFF0000) {
3179 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
3181 if (val & 0xFF000000) {
3182 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
3184 } else if (val & 0xFF00) {
3185 ARM_MOV_REG_IMM (code, dreg, (val & 0xFF00) >> 8, 24);
3186 if (val & 0xFF0000) {
3187 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
3189 if (val & 0xFF000000) {
3190 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
3192 } else if (val & 0xFF0000) {
3193 ARM_MOV_REG_IMM (code, dreg, (val & 0xFF0000) >> 16, 16);
3194 if (val & 0xFF000000) {
3195 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
3198 //g_assert_not_reached ();
3204 mono_arm_thumb_supported (void)
3206 return thumb_supported;
3212 * emit_load_volatile_arguments:
3214 * Load volatile arguments from the stack to the original input registers.
3215 * Required before a tail call.
3218 emit_load_volatile_arguments (MonoCompile *cfg, guint8 *code)
3220 MonoMethod *method = cfg->method;
3221 MonoMethodSignature *sig;
3226 /* FIXME: Generate intermediate code instead */
3228 sig = mono_method_signature (method);
3230 /* This is the opposite of the code in emit_prolog */
3234 cinfo = get_call_info (cfg->generic_sharing_context, NULL, sig);
3236 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
3237 ArgInfo *ainfo = &cinfo->ret;
3238 inst = cfg->vret_addr;
3239 g_assert (arm_is_imm12 (inst->inst_offset));
3240 ARM_LDR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
3242 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3243 ArgInfo *ainfo = cinfo->args + i;
3244 inst = cfg->args [pos];
3246 if (cfg->verbose_level > 2)
3247 g_print ("Loading argument %d (type: %d)\n", i, ainfo->storage);
3248 if (inst->opcode == OP_REGVAR) {
3249 if (ainfo->storage == RegTypeGeneral)
3250 ARM_MOV_REG_REG (code, inst->dreg, ainfo->reg);
3251 else if (ainfo->storage == RegTypeFP) {
3252 g_assert_not_reached ();
3253 } else if (ainfo->storage == RegTypeBase) {
3257 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
3258 ARM_LDR_IMM (code, inst->dreg, ARMREG_SP, (prev_sp_offset + ainfo->offset));
3260 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
3261 ARM_LDR_REG_REG (code, inst->dreg, ARMREG_SP, ARMREG_IP);
3265 g_assert_not_reached ();
3267 if (ainfo->storage == RegTypeGeneral || ainfo->storage == RegTypeIRegPair) {
3268 switch (ainfo->size) {
3275 g_assert (arm_is_imm12 (inst->inst_offset));
3276 ARM_LDR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
3277 g_assert (arm_is_imm12 (inst->inst_offset + 4));
3278 ARM_LDR_IMM (code, ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
3281 if (arm_is_imm12 (inst->inst_offset)) {
3282 ARM_LDR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
3284 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
3285 ARM_LDR_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
3289 } else if (ainfo->storage == RegTypeBaseGen) {
3292 } else if (ainfo->storage == RegTypeBase) {
3294 } else if (ainfo->storage == RegTypeFP) {
3295 g_assert_not_reached ();
3296 } else if (ainfo->storage == RegTypeStructByVal) {
3297 int doffset = inst->inst_offset;
3301 if (mono_class_from_mono_type (inst->inst_vtype))
3302 size = mono_class_native_size (mono_class_from_mono_type (inst->inst_vtype), NULL);
3303 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
3304 if (arm_is_imm12 (doffset)) {
3305 ARM_LDR_IMM (code, ainfo->reg + cur_reg, inst->inst_basereg, doffset);
3307 code = mono_arm_emit_load_imm (code, ARMREG_IP, doffset);
3308 ARM_LDR_REG_REG (code, ainfo->reg + cur_reg, inst->inst_basereg, ARMREG_IP);
3310 soffset += sizeof (gpointer);
3311 doffset += sizeof (gpointer);
3316 } else if (ainfo->storage == RegTypeStructByAddr) {
3331 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
3336 guint8 *code = cfg->native_code + cfg->code_len;
3337 MonoInst *last_ins = NULL;
3338 guint last_offset = 0;
3340 int imm8, rot_amount;
3342 /* we don't align basic blocks of loops on arm */
3344 if (cfg->verbose_level > 2)
3345 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
3347 cpos = bb->max_offset;
3349 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
3350 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
3351 //g_assert (!mono_compile_aot);
3354 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
3355 /* this is not thread save, but good enough */
3356 /* fixme: howto handle overflows? */
3357 //x86_inc_mem (code, &cov->data [bb->dfn].count);
3360 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) {
3361 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3362 (gpointer)"mono_break");
3363 code = emit_call_seq (cfg, code);
3366 MONO_BB_FOR_EACH_INS (bb, ins) {
3367 offset = code - cfg->native_code;
3369 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
3371 if (offset > (cfg->code_size - max_len - 16)) {
3372 cfg->code_size *= 2;
3373 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3374 code = cfg->native_code + offset;
3376 // if (ins->cil_code)
3377 // g_print ("cil code\n");
3378 mono_debug_record_line_number (cfg, ins, offset);
3380 switch (ins->opcode) {
3381 case OP_MEMORY_BARRIER:
3383 ARM_MOV_REG_IMM8 (code, ARMREG_R0, 0);
3384 ARM_MCR (code, 15, 0, ARMREG_R0, 7, 10, 5);
3388 #ifdef HAVE_AEABI_READ_TP
3389 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3390 (gpointer)"__aeabi_read_tp");
3391 code = emit_call_seq (cfg, code);
3393 ARM_LDR_IMM (code, ins->dreg, ARMREG_R0, ins->inst_offset);
3395 g_assert_not_reached ();
3399 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
3400 ppc_mulhw (code, ppc_r3, ins->sreg1, ins->sreg2);
3403 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
3404 ppc_mulhwu (code, ppc_r3, ins->sreg1, ins->sreg2);
3406 case OP_STOREI1_MEMBASE_IMM:
3407 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFF);
3408 g_assert (arm_is_imm12 (ins->inst_offset));
3409 ARM_STRB_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
3411 case OP_STOREI2_MEMBASE_IMM:
3412 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFFFF);
3413 g_assert (arm_is_imm8 (ins->inst_offset));
3414 ARM_STRH_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
3416 case OP_STORE_MEMBASE_IMM:
3417 case OP_STOREI4_MEMBASE_IMM:
3418 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm);
3419 g_assert (arm_is_imm12 (ins->inst_offset));
3420 ARM_STR_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
3422 case OP_STOREI1_MEMBASE_REG:
3423 g_assert (arm_is_imm12 (ins->inst_offset));
3424 ARM_STRB_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3426 case OP_STOREI2_MEMBASE_REG:
3427 g_assert (arm_is_imm8 (ins->inst_offset));
3428 ARM_STRH_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3430 case OP_STORE_MEMBASE_REG:
3431 case OP_STOREI4_MEMBASE_REG:
3432 /* this case is special, since it happens for spill code after lowering has been called */
3433 if (arm_is_imm12 (ins->inst_offset)) {
3434 ARM_STR_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3436 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
3437 ARM_STR_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ARMREG_LR);
3440 case OP_STOREI1_MEMINDEX:
3441 ARM_STRB_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
3443 case OP_STOREI2_MEMINDEX:
3444 ARM_STRH_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
3446 case OP_STORE_MEMINDEX:
3447 case OP_STOREI4_MEMINDEX:
3448 ARM_STR_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
3451 g_assert_not_reached ();
3453 case OP_LOAD_MEMINDEX:
3454 case OP_LOADI4_MEMINDEX:
3455 case OP_LOADU4_MEMINDEX:
3456 ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3458 case OP_LOADI1_MEMINDEX:
3459 ARM_LDRSB_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3461 case OP_LOADU1_MEMINDEX:
3462 ARM_LDRB_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3464 case OP_LOADI2_MEMINDEX:
3465 ARM_LDRSH_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3467 case OP_LOADU2_MEMINDEX:
3468 ARM_LDRH_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3470 case OP_LOAD_MEMBASE:
3471 case OP_LOADI4_MEMBASE:
3472 case OP_LOADU4_MEMBASE:
3473 /* this case is special, since it happens for spill code after lowering has been called */
3474 if (arm_is_imm12 (ins->inst_offset)) {
3475 ARM_LDR_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3477 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
3478 ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ARMREG_LR);
3481 case OP_LOADI1_MEMBASE:
3482 g_assert (arm_is_imm8 (ins->inst_offset));
3483 ARM_LDRSB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3485 case OP_LOADU1_MEMBASE:
3486 g_assert (arm_is_imm12 (ins->inst_offset));
3487 ARM_LDRB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3489 case OP_LOADU2_MEMBASE:
3490 g_assert (arm_is_imm8 (ins->inst_offset));
3491 ARM_LDRH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3493 case OP_LOADI2_MEMBASE:
3494 g_assert (arm_is_imm8 (ins->inst_offset));
3495 ARM_LDRSH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3497 case OP_ICONV_TO_I1:
3498 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 24);
3499 ARM_SAR_IMM (code, ins->dreg, ins->dreg, 24);
3501 case OP_ICONV_TO_I2:
3502 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
3503 ARM_SAR_IMM (code, ins->dreg, ins->dreg, 16);
3505 case OP_ICONV_TO_U1:
3506 ARM_AND_REG_IMM8 (code, ins->dreg, ins->sreg1, 0xff);
3508 case OP_ICONV_TO_U2:
3509 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
3510 ARM_SHR_IMM (code, ins->dreg, ins->dreg, 16);
3514 ARM_CMP_REG_REG (code, ins->sreg1, ins->sreg2);
3516 case OP_COMPARE_IMM:
3517 case OP_ICOMPARE_IMM:
3518 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3519 g_assert (imm8 >= 0);
3520 ARM_CMP_REG_IMM (code, ins->sreg1, imm8, rot_amount);
3524 * gdb does not like encountering the hw breakpoint ins in the debugged code.
3525 * So instead of emitting a trap, we emit a call a C function and place a
3528 //*(int*)code = 0xef9f0001;
3531 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3532 (gpointer)"mono_break");
3533 code = emit_call_seq (cfg, code);
3535 case OP_RELAXED_NOP:
3540 case OP_DUMMY_STORE:
3541 case OP_NOT_REACHED:
3544 case OP_SEQ_POINT: {
3546 MonoInst *info_var = cfg->arch.seq_point_info_var;
3547 MonoInst *ss_trigger_page_var = cfg->arch.ss_trigger_page_var;
3549 int dreg = ARMREG_LR;
3552 * For AOT, we use one got slot per method, which will point to a
3553 * SeqPointInfo structure, containing all the information required
3554 * by the code below.
3556 if (cfg->compile_aot) {
3557 g_assert (info_var);
3558 g_assert (info_var->opcode == OP_REGOFFSET);
3559 g_assert (arm_is_imm12 (info_var->inst_offset));
3563 * Read from the single stepping trigger page. This will cause a
3564 * SIGSEGV when single stepping is enabled.
3565 * We do this _before_ the breakpoint, so single stepping after
3566 * a breakpoint is hit will step to the next IL offset.
3568 g_assert (((guint64)(gsize)ss_trigger_page >> 32) == 0);
3570 if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
3571 if (cfg->compile_aot) {
3572 /* Load the trigger page addr from the variable initialized in the prolog */
3573 var = ss_trigger_page_var;
3575 g_assert (var->opcode == OP_REGOFFSET);
3576 g_assert (arm_is_imm12 (var->inst_offset));
3577 ARM_LDR_IMM (code, dreg, var->inst_basereg, var->inst_offset);
3579 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
3581 *(int*)code = (int)ss_trigger_page;
3584 ARM_LDR_IMM (code, dreg, dreg, 0);
3587 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
3589 if (cfg->compile_aot) {
3590 guint32 offset = code - cfg->native_code;
3593 ARM_LDR_IMM (code, dreg, info_var->inst_basereg, info_var->inst_offset);
3594 /* Add the offset */
3595 val = ((offset / 4) * sizeof (guint8*)) + G_STRUCT_OFFSET (SeqPointInfo, bp_addrs);
3596 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF), 0);
3598 * Have to emit nops to keep the difference between the offset
3599 * stored in seq_points and breakpoint instruction constant,
3600 * mono_arch_get_ip_for_breakpoint () depends on this.
3603 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF00) >> 8, 24);
3607 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
3610 g_assert (!(val & 0xFF000000));
3611 /* Load the info->bp_addrs [offset], which is either 0 or the address of a trigger page */
3612 ARM_LDR_IMM (code, dreg, dreg, 0);
3614 /* What is faster, a branch or a load ? */
3615 ARM_CMP_REG_IMM (code, dreg, 0, 0);
3616 /* The breakpoint instruction */
3617 ARM_LDR_IMM_COND (code, dreg, dreg, 0, ARMCOND_NE);
3620 * A placeholder for a possible breakpoint inserted by
3621 * mono_arch_set_breakpoint ().
3623 for (i = 0; i < 4; ++i)
3630 ARM_ADDS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3633 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3637 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3640 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3641 g_assert (imm8 >= 0);
3642 ARM_ADDS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3646 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3647 g_assert (imm8 >= 0);
3648 ARM_ADD_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3652 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3653 g_assert (imm8 >= 0);
3654 ARM_ADCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3657 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3658 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3660 case OP_IADD_OVF_UN:
3661 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3662 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3665 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3666 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3668 case OP_ISUB_OVF_UN:
3669 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3670 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
3672 case OP_ADD_OVF_CARRY:
3673 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3674 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3676 case OP_ADD_OVF_UN_CARRY:
3677 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3678 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3680 case OP_SUB_OVF_CARRY:
3681 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3682 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3684 case OP_SUB_OVF_UN_CARRY:
3685 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3686 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
3690 ARM_SUBS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3693 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3694 g_assert (imm8 >= 0);
3695 ARM_SUBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3698 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3702 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3706 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3707 g_assert (imm8 >= 0);
3708 ARM_SUB_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3712 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3713 g_assert (imm8 >= 0);
3714 ARM_SBCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3716 case OP_ARM_RSBS_IMM:
3717 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3718 g_assert (imm8 >= 0);
3719 ARM_RSBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3721 case OP_ARM_RSC_IMM:
3722 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3723 g_assert (imm8 >= 0);
3724 ARM_RSC_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3727 ARM_AND_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3731 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3732 g_assert (imm8 >= 0);
3733 ARM_AND_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3741 /* crappy ARM arch doesn't have a DIV instruction */
3742 g_assert_not_reached ();
3744 ARM_ORR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3748 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3749 g_assert (imm8 >= 0);
3750 ARM_ORR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3753 ARM_EOR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3757 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3758 g_assert (imm8 >= 0);
3759 ARM_EOR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3762 ARM_SHL_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3767 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
3768 else if (ins->dreg != ins->sreg1)
3769 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
3772 ARM_SAR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3777 ARM_SAR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
3778 else if (ins->dreg != ins->sreg1)
3779 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
3782 case OP_ISHR_UN_IMM:
3784 ARM_SHR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
3785 else if (ins->dreg != ins->sreg1)
3786 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
3789 ARM_SHR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3792 ARM_MVN_REG_REG (code, ins->dreg, ins->sreg1);
3795 ARM_RSB_REG_IMM8 (code, ins->dreg, ins->sreg1, 0);
3798 if (ins->dreg == ins->sreg2)
3799 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3801 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg2, ins->sreg1);
3804 g_assert_not_reached ();
3807 /* FIXME: handle ovf/ sreg2 != dreg */
3808 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3809 /* FIXME: MUL doesn't set the C/O flags on ARM */
3811 case OP_IMUL_OVF_UN:
3812 /* FIXME: handle ovf/ sreg2 != dreg */
3813 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3814 /* FIXME: MUL doesn't set the C/O flags on ARM */
3817 code = mono_arm_emit_load_imm (code, ins->dreg, ins->inst_c0);
3820 /* Load the GOT offset */
3821 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3822 ARM_LDR_IMM (code, ins->dreg, ARMREG_PC, 0);
3824 *(gpointer*)code = NULL;
3826 /* Load the value from the GOT */
3827 ARM_LDR_REG_REG (code, ins->dreg, ARMREG_PC, ins->dreg);
3829 case OP_ICONV_TO_I4:
3830 case OP_ICONV_TO_U4:
3832 if (ins->dreg != ins->sreg1)
3833 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
3836 int saved = ins->sreg2;
3837 if (ins->sreg2 == ARM_LSW_REG) {
3838 ARM_MOV_REG_REG (code, ARMREG_LR, ins->sreg2);
3841 if (ins->sreg1 != ARM_LSW_REG)
3842 ARM_MOV_REG_REG (code, ARM_LSW_REG, ins->sreg1);
3843 if (saved != ARM_MSW_REG)
3844 ARM_MOV_REG_REG (code, ARM_MSW_REG, saved);
3849 ARM_MVFD (code, ins->dreg, ins->sreg1);
3850 #elif defined(ARM_FPU_VFP)
3851 ARM_CPYD (code, ins->dreg, ins->sreg1);
3854 case OP_FCONV_TO_R4:
3856 ARM_MVFS (code, ins->dreg, ins->sreg1);
3857 #elif defined(ARM_FPU_VFP)
3858 ARM_CVTD (code, ins->dreg, ins->sreg1);
3859 ARM_CVTS (code, ins->dreg, ins->dreg);
3864 * Keep in sync with mono_arch_emit_epilog
3866 g_assert (!cfg->method->save_lmf);
3868 code = emit_load_volatile_arguments (cfg, code);
3870 code = emit_big_add (code, ARMREG_SP, cfg->frame_reg, cfg->stack_usage);
3872 if (cfg->used_int_regs)
3873 ARM_POP (code, cfg->used_int_regs);
3874 ARM_POP (code, (1 << ARMREG_R7) | (1 << ARMREG_LR));
3876 ARM_POP (code, cfg->used_int_regs | (1 << ARMREG_LR));
3878 mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
3879 if (cfg->compile_aot) {
3880 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
3882 *(gpointer*)code = NULL;
3884 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
3890 /* ensure ins->sreg1 is not NULL */
3891 ARM_LDRB_IMM (code, ARMREG_LR, ins->sreg1, 0);
3894 g_assert (cfg->sig_cookie < 128);
3895 ARM_LDR_IMM (code, ARMREG_IP, cfg->frame_reg, cfg->sig_cookie);
3896 ARM_STR_IMM (code, ARMREG_IP, ins->sreg1, 0);
3905 call = (MonoCallInst*)ins;
3906 if (ins->flags & MONO_INST_HAS_METHOD)
3907 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
3909 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
3910 code = emit_call_seq (cfg, code);
3911 code = emit_move_return_value (cfg, ins, code);
3917 case OP_VOIDCALL_REG:
3919 code = emit_call_reg (code, ins->sreg1);
3920 code = emit_move_return_value (cfg, ins, code);
3922 case OP_FCALL_MEMBASE:
3923 case OP_LCALL_MEMBASE:
3924 case OP_VCALL_MEMBASE:
3925 case OP_VCALL2_MEMBASE:
3926 case OP_VOIDCALL_MEMBASE:
3927 case OP_CALL_MEMBASE:
3928 g_assert (arm_is_imm12 (ins->inst_offset));
3929 g_assert (ins->sreg1 != ARMREG_LR);
3930 call = (MonoCallInst*)ins;
3931 if (call->dynamic_imt_arg || call->method->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
3932 ARM_ADD_REG_IMM8 (code, ARMREG_LR, ARMREG_PC, 4);
3933 ARM_LDR_IMM (code, ARMREG_PC, ins->sreg1, ins->inst_offset);
3935 * We can't embed the method in the code stream in PIC code, or
3937 * Instead, we put it in V5 in code emitted by
3938 * mono_arch_emit_imt_argument (), and embed NULL here to
3939 * signal the IMT thunk that the value is in V5.
3941 if (call->dynamic_imt_arg)
3942 *((gpointer*)code) = NULL;
3944 *((gpointer*)code) = (gpointer)call->method;
3947 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
3948 ARM_LDR_IMM (code, ARMREG_PC, ins->sreg1, ins->inst_offset);
3950 code = emit_move_return_value (cfg, ins, code);
3953 /* keep alignment */
3954 int alloca_waste = cfg->param_area;
3957 /* round the size to 8 bytes */
3958 ARM_ADD_REG_IMM8 (code, ins->dreg, ins->sreg1, 7);
3959 ARM_BIC_REG_IMM8 (code, ins->dreg, ins->dreg, 7);
3961 ARM_ADD_REG_IMM8 (code, ins->dreg, ins->dreg, alloca_waste);
3962 ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ins->dreg);
3963 /* memzero the area: dreg holds the size, sp is the pointer */
3964 if (ins->flags & MONO_INST_INIT) {
3965 guint8 *start_loop, *branch_to_cond;
3966 ARM_MOV_REG_IMM8 (code, ARMREG_LR, 0);
3967 branch_to_cond = code;
3970 ARM_STR_REG_REG (code, ARMREG_LR, ARMREG_SP, ins->dreg);
3971 arm_patch (branch_to_cond, code);
3972 /* decrement by 4 and set flags */
3973 ARM_SUBS_REG_IMM8 (code, ins->dreg, ins->dreg, sizeof (mgreg_t));
3974 ARM_B_COND (code, ARMCOND_GE, 0);
3975 arm_patch (code - 4, start_loop);
3977 ARM_ADD_REG_IMM8 (code, ins->dreg, ARMREG_SP, alloca_waste);
3982 MonoInst *var = cfg->dyn_call_var;
3984 g_assert (var->opcode == OP_REGOFFSET);
3985 g_assert (arm_is_imm12 (var->inst_offset));
3987 /* lr = args buffer filled by mono_arch_get_dyn_call_args () */
3988 ARM_MOV_REG_REG( code, ARMREG_LR, ins->sreg1);
3990 ARM_MOV_REG_REG( code, ARMREG_IP, ins->sreg2);
3992 /* Save args buffer */
3993 ARM_STR_IMM (code, ARMREG_LR, var->inst_basereg, var->inst_offset);
3995 /* Set stack slots using R0 as scratch reg */
3996 /* MONO_ARCH_DYN_CALL_PARAM_AREA gives the size of stack space available */
3997 for (i = 0; i < DYN_CALL_STACK_ARGS; ++i) {
3998 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_LR, (PARAM_REGS + i) * sizeof (mgreg_t));
3999 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, i * sizeof (mgreg_t));
4002 /* Set argument registers */
4003 for (i = 0; i < PARAM_REGS; ++i)
4004 ARM_LDR_IMM (code, i, ARMREG_LR, i * sizeof (mgreg_t));
4007 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
4008 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
4011 ARM_LDR_IMM (code, ARMREG_IP, var->inst_basereg, var->inst_offset);
4012 ARM_STR_IMM (code, ARMREG_R0, ARMREG_IP, G_STRUCT_OFFSET (DynCallArgs, res));
4013 ARM_STR_IMM (code, ARMREG_R1, ARMREG_IP, G_STRUCT_OFFSET (DynCallArgs, res2));
4017 if (ins->sreg1 != ARMREG_R0)
4018 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
4019 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
4020 (gpointer)"mono_arch_throw_exception");
4021 code = emit_call_seq (cfg, code);
4025 if (ins->sreg1 != ARMREG_R0)
4026 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
4027 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
4028 (gpointer)"mono_arch_rethrow_exception");
4029 code = emit_call_seq (cfg, code);
4032 case OP_START_HANDLER: {
4033 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4035 if (arm_is_imm12 (spvar->inst_offset)) {
4036 ARM_STR_IMM (code, ARMREG_LR, spvar->inst_basereg, spvar->inst_offset);
4038 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
4039 ARM_STR_REG_REG (code, ARMREG_LR, spvar->inst_basereg, ARMREG_IP);
4043 case OP_ENDFILTER: {
4044 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4046 if (ins->sreg1 != ARMREG_R0)
4047 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
4048 if (arm_is_imm12 (spvar->inst_offset)) {
4049 ARM_LDR_IMM (code, ARMREG_IP, spvar->inst_basereg, spvar->inst_offset);
4051 g_assert (ARMREG_IP != spvar->inst_basereg);
4052 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
4053 ARM_LDR_REG_REG (code, ARMREG_IP, spvar->inst_basereg, ARMREG_IP);
4055 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
4058 case OP_ENDFINALLY: {
4059 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4061 if (arm_is_imm12 (spvar->inst_offset)) {
4062 ARM_LDR_IMM (code, ARMREG_IP, spvar->inst_basereg, spvar->inst_offset);
4064 g_assert (ARMREG_IP != spvar->inst_basereg);
4065 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
4066 ARM_LDR_REG_REG (code, ARMREG_IP, spvar->inst_basereg, ARMREG_IP);
4068 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
4071 case OP_CALL_HANDLER:
4072 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4074 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
4077 ins->inst_c0 = code - cfg->native_code;
4080 /*if (ins->inst_target_bb->native_offset) {
4082 //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
4084 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4089 ARM_MOV_REG_REG (code, ARMREG_PC, ins->sreg1);
4093 * In the normal case we have:
4094 * ldr pc, [pc, ins->sreg1 << 2]
4097 * ldr lr, [pc, ins->sreg1 << 2]
4099 * After follows the data.
4100 * FIXME: add aot support.
4102 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_SWITCH, ins->inst_p0);
4103 max_len += 4 * GPOINTER_TO_INT (ins->klass);
4104 if (offset + max_len > (cfg->code_size - 16)) {
4105 cfg->code_size += max_len;
4106 cfg->code_size *= 2;
4107 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4108 code = cfg->native_code + offset;
4110 ARM_LDR_REG_REG_SHIFT (code, ARMREG_PC, ARMREG_PC, ins->sreg1, ARMSHIFT_LSL, 2);
4112 code += 4 * GPOINTER_TO_INT (ins->klass);
4116 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
4117 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
4121 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4122 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LT);
4126 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4127 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LO);
4131 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4132 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_GT);
4136 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4137 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_HI);
4139 case OP_COND_EXC_EQ:
4140 case OP_COND_EXC_NE_UN:
4141 case OP_COND_EXC_LT:
4142 case OP_COND_EXC_LT_UN:
4143 case OP_COND_EXC_GT:
4144 case OP_COND_EXC_GT_UN:
4145 case OP_COND_EXC_GE:
4146 case OP_COND_EXC_GE_UN:
4147 case OP_COND_EXC_LE:
4148 case OP_COND_EXC_LE_UN:
4149 EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_EQ, ins->inst_p1);
4151 case OP_COND_EXC_IEQ:
4152 case OP_COND_EXC_INE_UN:
4153 case OP_COND_EXC_ILT:
4154 case OP_COND_EXC_ILT_UN:
4155 case OP_COND_EXC_IGT:
4156 case OP_COND_EXC_IGT_UN:
4157 case OP_COND_EXC_IGE:
4158 case OP_COND_EXC_IGE_UN:
4159 case OP_COND_EXC_ILE:
4160 case OP_COND_EXC_ILE_UN:
4161 EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_IEQ, ins->inst_p1);
4164 case OP_COND_EXC_IC:
4165 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_CS, ins->inst_p1);
4167 case OP_COND_EXC_OV:
4168 case OP_COND_EXC_IOV:
4169 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VS, ins->inst_p1);
4171 case OP_COND_EXC_NC:
4172 case OP_COND_EXC_INC:
4173 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_CC, ins->inst_p1);
4175 case OP_COND_EXC_NO:
4176 case OP_COND_EXC_INO:
4177 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VC, ins->inst_p1);
4189 EMIT_COND_BRANCH (ins, ins->opcode - OP_IBEQ);
4192 /* floating point opcodes */
4195 if (cfg->compile_aot) {
4196 ARM_LDFD (code, ins->dreg, ARMREG_PC, 0);
4198 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
4200 *(guint32*)code = ((guint32*)(ins->inst_p0))[1];
4203 /* FIXME: we can optimize the imm load by dealing with part of
4204 * the displacement in LDFD (aligning to 512).
4206 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
4207 ARM_LDFD (code, ins->dreg, ARMREG_LR, 0);
4211 if (cfg->compile_aot) {
4212 ARM_LDFS (code, ins->dreg, ARMREG_PC, 0);
4214 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
4217 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
4218 ARM_LDFS (code, ins->dreg, ARMREG_LR, 0);
4221 case OP_STORER8_MEMBASE_REG:
4222 /* This is generated by the local regalloc pass which runs after the lowering pass */
4223 if (!arm_is_fpimm8 (ins->inst_offset)) {
4224 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4225 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_destbasereg);
4226 ARM_STFD (code, ins->sreg1, ARMREG_LR, 0);
4228 ARM_STFD (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
4231 case OP_LOADR8_MEMBASE:
4232 /* This is generated by the local regalloc pass which runs after the lowering pass */
4233 if (!arm_is_fpimm8 (ins->inst_offset)) {
4234 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4235 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_basereg);
4236 ARM_LDFD (code, ins->dreg, ARMREG_LR, 0);
4238 ARM_LDFD (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4241 case OP_STORER4_MEMBASE_REG:
4242 g_assert (arm_is_fpimm8 (ins->inst_offset));
4243 ARM_STFS (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
4245 case OP_LOADR4_MEMBASE:
4246 g_assert (arm_is_fpimm8 (ins->inst_offset));
4247 ARM_LDFS (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4249 case OP_ICONV_TO_R_UN: {
4251 tmpreg = ins->dreg == 0? 1: 0;
4252 ARM_CMP_REG_IMM8 (code, ins->sreg1, 0);
4253 ARM_FLTD (code, ins->dreg, ins->sreg1);
4254 ARM_B_COND (code, ARMCOND_GE, 8);
4255 /* save the temp register */
4256 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
4257 ARM_STFD (code, tmpreg, ARMREG_SP, 0);
4258 ARM_LDFD (code, tmpreg, ARMREG_PC, 12);
4259 ARM_FPA_ADFD (code, ins->dreg, ins->dreg, tmpreg);
4260 ARM_LDFD (code, tmpreg, ARMREG_SP, 0);
4261 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
4262 /* skip the constant pool */
4265 *(int*)code = 0x41f00000;
4270 * ldfltd ftemp, [pc, #8] 0x41f00000 0x00000000
4271 * adfltd fdest, fdest, ftemp
4275 case OP_ICONV_TO_R4:
4276 ARM_FLTS (code, ins->dreg, ins->sreg1);
4278 case OP_ICONV_TO_R8:
4279 ARM_FLTD (code, ins->dreg, ins->sreg1);
4282 #elif defined(ARM_FPU_VFP)
4285 if (cfg->compile_aot) {
4286 ARM_FLDD (code, ins->dreg, ARMREG_PC, 0);
4288 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
4290 *(guint32*)code = ((guint32*)(ins->inst_p0))[1];
4293 /* FIXME: we can optimize the imm load by dealing with part of
4294 * the displacement in LDFD (aligning to 512).
4296 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
4297 ARM_FLDD (code, ins->dreg, ARMREG_LR, 0);
4301 if (cfg->compile_aot) {
4302 ARM_FLDS (code, ins->dreg, ARMREG_PC, 0);
4304 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
4306 ARM_CVTS (code, ins->dreg, ins->dreg);
4308 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
4309 ARM_FLDS (code, ins->dreg, ARMREG_LR, 0);
4310 ARM_CVTS (code, ins->dreg, ins->dreg);
4313 case OP_STORER8_MEMBASE_REG:
4314 /* This is generated by the local regalloc pass which runs after the lowering pass */
4315 if (!arm_is_fpimm8 (ins->inst_offset)) {
4316 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4317 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_destbasereg);
4318 ARM_FSTD (code, ins->sreg1, ARMREG_LR, 0);
4320 ARM_FSTD (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
4323 case OP_LOADR8_MEMBASE:
4324 /* This is generated by the local regalloc pass which runs after the lowering pass */
4325 if (!arm_is_fpimm8 (ins->inst_offset)) {
4326 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4327 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_basereg);
4328 ARM_FLDD (code, ins->dreg, ARMREG_LR, 0);
4330 ARM_FLDD (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4333 case OP_STORER4_MEMBASE_REG:
4334 g_assert (arm_is_fpimm8 (ins->inst_offset));
4335 ARM_CVTD (code, ARM_VFP_F0, ins->sreg1);
4336 ARM_FSTS (code, ARM_VFP_F0, ins->inst_destbasereg, ins->inst_offset);
4338 case OP_LOADR4_MEMBASE:
4339 g_assert (arm_is_fpimm8 (ins->inst_offset));
4340 ARM_FLDS (code, ARM_VFP_F0, ins->inst_basereg, ins->inst_offset);
4341 ARM_CVTS (code, ins->dreg, ARM_VFP_F0);
4343 case OP_ICONV_TO_R_UN: {
4344 g_assert_not_reached ();
4347 case OP_ICONV_TO_R4:
4348 ARM_FMSR (code, ARM_VFP_F0, ins->sreg1);
4349 ARM_FSITOS (code, ARM_VFP_F0, ARM_VFP_F0);
4350 ARM_CVTS (code, ins->dreg, ARM_VFP_F0);
4352 case OP_ICONV_TO_R8:
4353 ARM_FMSR (code, ARM_VFP_F0, ins->sreg1);
4354 ARM_FSITOD (code, ins->dreg, ARM_VFP_F0);
4358 if (mono_method_signature (cfg->method)->ret->type == MONO_TYPE_R4) {
4359 ARM_CVTD (code, ARM_VFP_F0, ins->sreg1);
4360 ARM_FMRS (code, ARMREG_R0, ARM_VFP_F0);
4362 ARM_FMRRD (code, ARMREG_R0, ARMREG_R1, ins->sreg1);
4368 case OP_FCONV_TO_I1:
4369 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
4371 case OP_FCONV_TO_U1:
4372 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
4374 case OP_FCONV_TO_I2:
4375 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
4377 case OP_FCONV_TO_U2:
4378 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
4380 case OP_FCONV_TO_I4:
4382 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
4384 case OP_FCONV_TO_U4:
4386 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
4388 case OP_FCONV_TO_I8:
4389 case OP_FCONV_TO_U8:
4390 g_assert_not_reached ();
4391 /* Implemented as helper calls */
4393 case OP_LCONV_TO_R_UN:
4394 g_assert_not_reached ();
4395 /* Implemented as helper calls */
4397 case OP_LCONV_TO_OVF_I4_2: {
4398 guint8 *high_bit_not_set, *valid_negative, *invalid_negative, *valid_positive;
4400 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
4403 ARM_CMP_REG_IMM8 (code, ins->sreg1, 0);
4404 high_bit_not_set = code;
4405 ARM_B_COND (code, ARMCOND_GE, 0); /*branch if bit 31 of the lower part is not set*/
4407 ARM_CMN_REG_IMM8 (code, ins->sreg2, 1); /*This have the same effect as CMP reg, 0xFFFFFFFF */
4408 valid_negative = code;
4409 ARM_B_COND (code, ARMCOND_EQ, 0); /*branch if upper part == 0xFFFFFFFF (lower part has bit 31 set) */
4410 invalid_negative = code;
4411 ARM_B_COND (code, ARMCOND_AL, 0);
4413 arm_patch (high_bit_not_set, code);
4415 ARM_CMP_REG_IMM8 (code, ins->sreg2, 0);
4416 valid_positive = code;
4417 ARM_B_COND (code, ARMCOND_EQ, 0); /*branch if upper part == 0 (lower part has bit 31 clear)*/
4419 arm_patch (invalid_negative, code);
4420 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_AL, "OverflowException");
4422 arm_patch (valid_negative, code);
4423 arm_patch (valid_positive, code);
4425 if (ins->dreg != ins->sreg1)
4426 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
4431 ARM_FPA_ADFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4434 ARM_FPA_SUFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4437 ARM_FPA_MUFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4440 ARM_FPA_DVFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4443 ARM_MNFD (code, ins->dreg, ins->sreg1);
4445 #elif defined(ARM_FPU_VFP)
4447 ARM_VFP_ADDD (code, ins->dreg, ins->sreg1, ins->sreg2);
4450 ARM_VFP_SUBD (code, ins->dreg, ins->sreg1, ins->sreg2);
4453 ARM_VFP_MULD (code, ins->dreg, ins->sreg1, ins->sreg2);
4456 ARM_VFP_DIVD (code, ins->dreg, ins->sreg1, ins->sreg2);
4459 ARM_NEGD (code, ins->dreg, ins->sreg1);
4464 g_assert_not_reached ();
4468 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4469 #elif defined(ARM_FPU_VFP)
4470 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4476 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4477 #elif defined(ARM_FPU_VFP)
4478 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4481 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
4482 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
4486 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4487 #elif defined(ARM_FPU_VFP)
4488 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4491 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4492 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4496 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4497 #elif defined(ARM_FPU_VFP)
4498 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4501 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4502 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4503 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
4508 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
4509 #elif defined(ARM_FPU_VFP)
4510 ARM_CMPD (code, ins->sreg2, ins->sreg1);
4513 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4514 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4519 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
4520 #elif defined(ARM_FPU_VFP)
4521 ARM_CMPD (code, ins->sreg2, ins->sreg1);
4524 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4525 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4526 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
4528 /* ARM FPA flags table:
4529 * N Less than ARMCOND_MI
4530 * Z Equal ARMCOND_EQ
4531 * C Greater Than or Equal ARMCOND_CS
4532 * V Unordered ARMCOND_VS
4535 EMIT_COND_BRANCH (ins, OP_IBEQ - OP_IBEQ);
4538 EMIT_COND_BRANCH (ins, OP_IBNE_UN - OP_IBEQ);
4541 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set */
4544 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
4545 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set */
4551 g_assert_not_reached ();
4555 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_GE);
4557 /* FPA requires EQ even thou the docs suggests that just CS is enough */
4558 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_EQ);
4559 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_CS);
4563 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
4564 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_GE);
4569 if (ins->dreg != ins->sreg1)
4570 ARM_MVFD (code, ins->dreg, ins->sreg1);
4571 #elif defined(ARM_FPU_VFP)
4572 ARM_ABSD (code, ARM_VFP_D1, ins->sreg1);
4573 ARM_FLDD (code, ARM_VFP_D0, ARMREG_PC, 0);
4575 *(guint32*)code = 0xffffffff;
4577 *(guint32*)code = 0x7fefffff;
4579 ARM_CMPD (code, ARM_VFP_D1, ARM_VFP_D0);
4581 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_GT, "ArithmeticException");
4582 ARM_CMPD (code, ins->sreg1, ins->sreg1);
4584 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VS, "ArithmeticException");
4586 ARM_CPYD (code, ins->dreg, ins->sreg1);
4591 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
4592 g_assert_not_reached ();
4595 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
4596 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
4597 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
4598 g_assert_not_reached ();
4604 last_offset = offset;
4607 cfg->code_len = code - cfg->native_code;
4610 #endif /* DISABLE_JIT */
4612 #ifdef HAVE_AEABI_READ_TP
4613 void __aeabi_read_tp (void);
4617 mono_arch_register_lowlevel_calls (void)
4619 /* The signature doesn't matter */
4620 mono_register_jit_icall (mono_arm_throw_exception, "mono_arm_throw_exception", mono_create_icall_signature ("void"), TRUE);
4621 mono_register_jit_icall (mono_arm_throw_exception_by_token, "mono_arm_throw_exception_by_token", mono_create_icall_signature ("void"), TRUE);
4623 #ifndef MONO_CROSS_COMPILE
4624 #ifdef HAVE_AEABI_READ_TP
4625 mono_register_jit_icall (__aeabi_read_tp, "__aeabi_read_tp", mono_create_icall_signature ("void"), TRUE);
4630 #define patch_lis_ori(ip,val) do {\
4631 guint16 *__lis_ori = (guint16*)(ip); \
4632 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff; \
4633 __lis_ori [3] = ((guint32)(val)) & 0xffff; \
4637 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, MonoCodeManager *dyn_code_mp, gboolean run_cctors)
4639 MonoJumpInfo *patch_info;
4640 gboolean compile_aot = !run_cctors;
4642 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
4643 unsigned char *ip = patch_info->ip.i + code;
4644 const unsigned char *target;
4646 if (patch_info->type == MONO_PATCH_INFO_SWITCH && !compile_aot) {
4647 gpointer *jt = (gpointer*)(ip + 8);
4649 /* jt is the inlined jump table, 2 instructions after ip
4650 * In the normal case we store the absolute addresses,
4651 * otherwise the displacements.
4653 for (i = 0; i < patch_info->data.table->table_size; i++)
4654 jt [i] = code + (int)patch_info->data.table->table [i];
4657 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
4660 switch (patch_info->type) {
4661 case MONO_PATCH_INFO_BB:
4662 case MONO_PATCH_INFO_LABEL:
4665 /* No need to patch these */
4670 switch (patch_info->type) {
4671 case MONO_PATCH_INFO_IP:
4672 g_assert_not_reached ();
4673 patch_lis_ori (ip, ip);
4675 case MONO_PATCH_INFO_METHOD_REL:
4676 g_assert_not_reached ();
4677 *((gpointer *)(ip)) = code + patch_info->data.offset;
4679 case MONO_PATCH_INFO_METHODCONST:
4680 case MONO_PATCH_INFO_CLASS:
4681 case MONO_PATCH_INFO_IMAGE:
4682 case MONO_PATCH_INFO_FIELD:
4683 case MONO_PATCH_INFO_VTABLE:
4684 case MONO_PATCH_INFO_IID:
4685 case MONO_PATCH_INFO_SFLDA:
4686 case MONO_PATCH_INFO_LDSTR:
4687 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
4688 case MONO_PATCH_INFO_LDTOKEN:
4689 g_assert_not_reached ();
4690 /* from OP_AOTCONST : lis + ori */
4691 patch_lis_ori (ip, target);
4693 case MONO_PATCH_INFO_R4:
4694 case MONO_PATCH_INFO_R8:
4695 g_assert_not_reached ();
4696 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
4698 case MONO_PATCH_INFO_EXC_NAME:
4699 g_assert_not_reached ();
4700 *((gconstpointer *)(ip + 1)) = patch_info->data.name;
4702 case MONO_PATCH_INFO_NONE:
4703 case MONO_PATCH_INFO_BB_OVF:
4704 case MONO_PATCH_INFO_EXC_OVF:
4705 /* everything is dealt with at epilog output time */
4710 arm_patch_general (domain, ip, target, dyn_code_mp);
4717 * Stack frame layout:
4719 * ------------------- fp
4720 * MonoLMF structure or saved registers
4721 * -------------------
4723 * -------------------
4725 * -------------------
4726 * optional 8 bytes for tracing
4727 * -------------------
4728 * param area size is cfg->param_area
4729 * ------------------- sp
4732 mono_arch_emit_prolog (MonoCompile *cfg)
4734 MonoMethod *method = cfg->method;
4736 MonoMethodSignature *sig;
4738 int alloc_size, pos, max_offset, i, rot_amount;
4743 int prev_sp_offset, reg_offset;
4745 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4748 sig = mono_method_signature (method);
4749 cfg->code_size = 256 + sig->param_count * 64;
4750 code = cfg->native_code = g_malloc (cfg->code_size);
4752 mono_emit_unwind_op_def_cfa (cfg, code, ARMREG_SP, 0);
4754 alloc_size = cfg->stack_offset;
4758 if (!method->save_lmf) {
4761 * The iphone uses R7 as the frame pointer, and it points at the saved
4766 * We can't use r7 as a frame pointer since it points into the middle of
4767 * the frame, so we keep using our own frame pointer.
4768 * FIXME: Optimize this.
4771 ARM_PUSH (code, (1 << ARMREG_R7) | (1 << ARMREG_LR));
4772 ARM_MOV_REG_REG (code, ARMREG_R7, ARMREG_SP);
4773 prev_sp_offset += 8; /* r7 and lr */
4774 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
4775 mono_emit_unwind_op_offset (cfg, code, ARMREG_R7, (- prev_sp_offset) + 0);
4777 /* No need to push LR again */
4778 if (cfg->used_int_regs)
4779 ARM_PUSH (code, cfg->used_int_regs);
4781 ARM_PUSH (code, cfg->used_int_regs | (1 << ARMREG_LR));
4782 prev_sp_offset += 4;
4784 for (i = 0; i < 16; ++i) {
4785 if (cfg->used_int_regs & (1 << i))
4786 prev_sp_offset += 4;
4788 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
4790 for (i = 0; i < 16; ++i) {
4791 if ((cfg->used_int_regs & (1 << i))) {
4792 mono_emit_unwind_op_offset (cfg, code, i, (- prev_sp_offset) + reg_offset);
4797 mono_emit_unwind_op_offset (cfg, code, ARMREG_LR, -4);
4799 mono_emit_unwind_op_offset (cfg, code, ARMREG_LR, -4);
4802 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
4803 ARM_PUSH (code, 0x5ff0);
4804 prev_sp_offset += 4 * 10; /* all but r0-r3, sp and pc */
4805 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
4807 for (i = 0; i < 16; ++i) {
4808 if ((i > ARMREG_R3) && (i != ARMREG_SP) && (i != ARMREG_PC)) {
4809 mono_emit_unwind_op_offset (cfg, code, i, (- prev_sp_offset) + reg_offset);
4813 pos += sizeof (MonoLMF) - prev_sp_offset;
4817 // align to MONO_ARCH_FRAME_ALIGNMENT bytes
4818 if (alloc_size & (MONO_ARCH_FRAME_ALIGNMENT - 1)) {
4819 alloc_size += MONO_ARCH_FRAME_ALIGNMENT - 1;
4820 alloc_size &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
4823 /* the stack used in the pushed regs */
4824 if (prev_sp_offset & 4)
4826 cfg->stack_usage = alloc_size;
4828 if ((i = mono_arm_is_rotated_imm8 (alloc_size, &rot_amount)) >= 0) {
4829 ARM_SUB_REG_IMM (code, ARMREG_SP, ARMREG_SP, i, rot_amount);
4831 code = mono_arm_emit_load_imm (code, ARMREG_IP, alloc_size);
4832 ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
4834 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset + alloc_size);
4836 if (cfg->frame_reg != ARMREG_SP) {
4837 ARM_MOV_REG_REG (code, cfg->frame_reg, ARMREG_SP);
4838 mono_emit_unwind_op_def_cfa_reg (cfg, code, cfg->frame_reg);
4840 //g_print ("prev_sp_offset: %d, alloc_size:%d\n", prev_sp_offset, alloc_size);
4841 prev_sp_offset += alloc_size;
4843 /* compute max_offset in order to use short forward jumps
4844 * we could skip do it on arm because the immediate displacement
4845 * for jumps is large enough, it may be useful later for constant pools
4848 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4849 MonoInst *ins = bb->code;
4850 bb->max_offset = max_offset;
4852 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
4855 MONO_BB_FOR_EACH_INS (bb, ins)
4856 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
4859 /* store runtime generic context */
4860 if (cfg->rgctx_var) {
4861 MonoInst *ins = cfg->rgctx_var;
4863 g_assert (ins->opcode == OP_REGOFFSET);
4865 if (arm_is_imm12 (ins->inst_offset)) {
4866 ARM_STR_IMM (code, MONO_ARCH_RGCTX_REG, ins->inst_basereg, ins->inst_offset);
4868 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4869 ARM_STR_REG_REG (code, MONO_ARCH_RGCTX_REG, ins->inst_basereg, ARMREG_LR);
4873 /* load arguments allocated to register from the stack */
4876 cinfo = get_call_info (cfg->generic_sharing_context, NULL, sig);
4878 if (MONO_TYPE_ISSTRUCT (sig->ret) && cinfo->ret.storage != RegTypeStructByVal) {
4879 ArgInfo *ainfo = &cinfo->ret;
4880 inst = cfg->vret_addr;
4881 g_assert (arm_is_imm12 (inst->inst_offset));
4882 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4885 if (sig->call_convention == MONO_CALL_VARARG) {
4886 ArgInfo *cookie = &cinfo->sig_cookie;
4888 /* Save the sig cookie address */
4889 g_assert (cookie->storage == RegTypeBase);
4891 g_assert (arm_is_imm12 (prev_sp_offset + cookie->offset));
4892 g_assert (arm_is_imm12 (cfg->sig_cookie));
4893 ARM_ADD_REG_IMM8 (code, ARMREG_IP, cfg->frame_reg, prev_sp_offset + cookie->offset);
4894 ARM_STR_IMM (code, ARMREG_IP, cfg->frame_reg, cfg->sig_cookie);
4897 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
4898 ArgInfo *ainfo = cinfo->args + i;
4899 inst = cfg->args [pos];
4901 if (cfg->verbose_level > 2)
4902 g_print ("Saving argument %d (type: %d)\n", i, ainfo->storage);
4903 if (inst->opcode == OP_REGVAR) {
4904 if (ainfo->storage == RegTypeGeneral)
4905 ARM_MOV_REG_REG (code, inst->dreg, ainfo->reg);
4906 else if (ainfo->storage == RegTypeFP) {
4907 g_assert_not_reached ();
4908 } else if (ainfo->storage == RegTypeBase) {
4909 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
4910 ARM_LDR_IMM (code, inst->dreg, ARMREG_SP, (prev_sp_offset + ainfo->offset));
4912 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4913 ARM_LDR_REG_REG (code, inst->dreg, ARMREG_SP, ARMREG_IP);
4916 g_assert_not_reached ();
4918 if (cfg->verbose_level > 2)
4919 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
4921 /* the argument should be put on the stack: FIXME handle size != word */
4922 if (ainfo->storage == RegTypeGeneral || ainfo->storage == RegTypeIRegPair) {
4923 switch (ainfo->size) {
4925 if (arm_is_imm12 (inst->inst_offset))
4926 ARM_STRB_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4928 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4929 ARM_STRB_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
4933 if (arm_is_imm8 (inst->inst_offset)) {
4934 ARM_STRH_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4936 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4937 ARM_STRH_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
4941 g_assert (arm_is_imm12 (inst->inst_offset));
4942 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4943 g_assert (arm_is_imm12 (inst->inst_offset + 4));
4944 ARM_STR_IMM (code, ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
4947 if (arm_is_imm12 (inst->inst_offset)) {
4948 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4950 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4951 ARM_STR_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
4955 } else if (ainfo->storage == RegTypeBaseGen) {
4956 g_assert (arm_is_imm12 (prev_sp_offset + ainfo->offset));
4957 g_assert (arm_is_imm12 (inst->inst_offset));
4958 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
4959 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset + 4);
4960 ARM_STR_IMM (code, ARMREG_R3, inst->inst_basereg, inst->inst_offset);
4961 } else if (ainfo->storage == RegTypeBase) {
4962 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
4963 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
4965 code = mono_arm_emit_load_imm (code, ARMREG_IP, prev_sp_offset + ainfo->offset);
4966 ARM_LDR_REG_REG (code, ARMREG_LR, ARMREG_SP, ARMREG_IP);
4969 switch (ainfo->size) {
4971 if (arm_is_imm8 (inst->inst_offset)) {
4972 ARM_STRB_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
4974 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4975 ARM_STRB_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
4979 if (arm_is_imm8 (inst->inst_offset)) {
4980 ARM_STRH_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
4982 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4983 ARM_STRH_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
4987 if (arm_is_imm12 (inst->inst_offset)) {
4988 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
4990 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4991 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
4993 if (arm_is_imm12 (prev_sp_offset + ainfo->offset + 4)) {
4994 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset + 4));
4996 code = mono_arm_emit_load_imm (code, ARMREG_IP, prev_sp_offset + ainfo->offset + 4);
4997 ARM_LDR_REG_REG (code, ARMREG_LR, ARMREG_SP, ARMREG_IP);
4999 if (arm_is_imm12 (inst->inst_offset + 4)) {
5000 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset + 4);
5002 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset + 4);
5003 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
5007 if (arm_is_imm12 (inst->inst_offset)) {
5008 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
5010 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
5011 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
5015 } else if (ainfo->storage == RegTypeFP) {
5016 g_assert_not_reached ();
5017 } else if (ainfo->storage == RegTypeStructByVal) {
5018 int doffset = inst->inst_offset;
5022 size = mini_type_stack_size_full (cfg->generic_sharing_context, inst->inst_vtype, NULL, sig->pinvoke);
5023 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
5024 if (arm_is_imm12 (doffset)) {
5025 ARM_STR_IMM (code, ainfo->reg + cur_reg, inst->inst_basereg, doffset);
5027 code = mono_arm_emit_load_imm (code, ARMREG_IP, doffset);
5028 ARM_STR_REG_REG (code, ainfo->reg + cur_reg, inst->inst_basereg, ARMREG_IP);
5030 soffset += sizeof (gpointer);
5031 doffset += sizeof (gpointer);
5033 if (ainfo->vtsize) {
5034 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
5035 //g_print ("emit_memcpy (prev_sp_ofs: %d, ainfo->offset: %d, soffset: %d)\n", prev_sp_offset, ainfo->offset, soffset);
5036 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ARMREG_SP, prev_sp_offset + ainfo->offset);
5038 } else if (ainfo->storage == RegTypeStructByAddr) {
5039 g_assert_not_reached ();
5040 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
5041 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
5043 g_assert_not_reached ();
5048 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
5049 if (cfg->compile_aot)
5050 /* AOT code is only used in the root domain */
5051 code = mono_arm_emit_load_imm (code, ARMREG_R0, 0);
5053 code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->domain);
5054 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
5055 (gpointer)"mono_jit_thread_attach");
5056 code = emit_call_seq (cfg, code);
5059 if (method->save_lmf)
5060 code = emit_save_lmf (cfg, code, alloc_size - lmf_offset);
5063 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
5065 if (cfg->arch.seq_point_info_var) {
5066 MonoInst *ins = cfg->arch.seq_point_info_var;
5068 /* Initialize the variable from a GOT slot */
5069 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_SEQ_POINT_INFO, cfg->method);
5070 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
5072 *(gpointer*)code = NULL;
5074 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
5076 g_assert (ins->opcode == OP_REGOFFSET);
5078 if (arm_is_imm12 (ins->inst_offset)) {
5079 ARM_STR_IMM (code, ARMREG_R0, ins->inst_basereg, ins->inst_offset);
5081 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
5082 ARM_STR_REG_REG (code, ARMREG_R0, ins->inst_basereg, ARMREG_LR);
5086 /* Initialize ss_trigger_page_var */
5088 MonoInst *info_var = cfg->arch.seq_point_info_var;
5089 MonoInst *ss_trigger_page_var = cfg->arch.ss_trigger_page_var;
5090 int dreg = ARMREG_LR;
5093 g_assert (info_var->opcode == OP_REGOFFSET);
5094 g_assert (arm_is_imm12 (info_var->inst_offset));
5096 ARM_LDR_IMM (code, dreg, info_var->inst_basereg, info_var->inst_offset);
5097 /* Load the trigger page addr */
5098 ARM_LDR_IMM (code, dreg, dreg, G_STRUCT_OFFSET (SeqPointInfo, ss_trigger_page));
5099 ARM_STR_IMM (code, dreg, ss_trigger_page_var->inst_basereg, ss_trigger_page_var->inst_offset);
5103 cfg->code_len = code - cfg->native_code;
5104 g_assert (cfg->code_len < cfg->code_size);
5111 mono_arch_emit_epilog (MonoCompile *cfg)
5113 MonoMethod *method = cfg->method;
5114 int pos, i, rot_amount;
5115 int max_epilog_size = 16 + 20*4;
5119 if (cfg->method->save_lmf)
5120 max_epilog_size += 128;
5122 if (mono_jit_trace_calls != NULL)
5123 max_epilog_size += 50;
5125 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
5126 max_epilog_size += 50;
5128 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
5129 cfg->code_size *= 2;
5130 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5131 mono_jit_stats.code_reallocs++;
5135 * Keep in sync with OP_JMP
5137 code = cfg->native_code + cfg->code_len;
5139 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
5140 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
5144 /* Load returned vtypes into registers if needed */
5145 cinfo = cfg->arch.cinfo;
5146 if (cinfo->ret.storage == RegTypeStructByVal) {
5147 MonoInst *ins = cfg->ret;
5149 if (arm_is_imm12 (ins->inst_offset)) {
5150 ARM_LDR_IMM (code, ARMREG_R0, ins->inst_basereg, ins->inst_offset);
5152 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
5153 ARM_LDR_REG_REG (code, ARMREG_R0, ins->inst_basereg, ARMREG_LR);
5157 if (method->save_lmf) {
5158 int lmf_offset, reg, sp_adj, regmask;
5159 /* all but r0-r3, sp and pc */
5160 pos += sizeof (MonoLMF) - (MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t));
5163 code = emit_restore_lmf (cfg, code, cfg->stack_usage - lmf_offset);
5165 /* This points to r4 inside MonoLMF->iregs */
5166 sp_adj = (sizeof (MonoLMF) - MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t));
5168 regmask = 0x9ff0; /* restore lr to pc */
5169 /* Skip caller saved registers not used by the method */
5170 while (!(cfg->used_int_regs & (1 << reg)) && reg < ARMREG_FP) {
5171 regmask &= ~(1 << reg);
5175 /* point sp at the registers to restore: 10 is 14 -4, because we skip r0-r3 */
5176 code = emit_big_add (code, ARMREG_SP, cfg->frame_reg, cfg->stack_usage - lmf_offset + sp_adj);
5178 ARM_POP (code, regmask);
5180 if ((i = mono_arm_is_rotated_imm8 (cfg->stack_usage, &rot_amount)) >= 0) {
5181 ARM_ADD_REG_IMM (code, ARMREG_SP, cfg->frame_reg, i, rot_amount);
5183 code = mono_arm_emit_load_imm (code, ARMREG_IP, cfg->stack_usage);
5184 ARM_ADD_REG_REG (code, ARMREG_SP, cfg->frame_reg, ARMREG_IP);
5188 /* Restore saved gregs */
5189 if (cfg->used_int_regs)
5190 ARM_POP (code, cfg->used_int_regs);
5191 /* Restore saved r7, restore LR to PC */
5192 ARM_POP (code, (1 << ARMREG_R7) | (1 << ARMREG_PC));
5194 ARM_POP (code, cfg->used_int_regs | (1 << ARMREG_PC));
5198 cfg->code_len = code - cfg->native_code;
5200 g_assert (cfg->code_len < cfg->code_size);
5204 /* remove once throw_exception_by_name is eliminated */
5206 exception_id_by_name (const char *name)
5208 if (strcmp (name, "IndexOutOfRangeException") == 0)
5209 return MONO_EXC_INDEX_OUT_OF_RANGE;
5210 if (strcmp (name, "OverflowException") == 0)
5211 return MONO_EXC_OVERFLOW;
5212 if (strcmp (name, "ArithmeticException") == 0)
5213 return MONO_EXC_ARITHMETIC;
5214 if (strcmp (name, "DivideByZeroException") == 0)
5215 return MONO_EXC_DIVIDE_BY_ZERO;
5216 if (strcmp (name, "InvalidCastException") == 0)
5217 return MONO_EXC_INVALID_CAST;
5218 if (strcmp (name, "NullReferenceException") == 0)
5219 return MONO_EXC_NULL_REF;
5220 if (strcmp (name, "ArrayTypeMismatchException") == 0)
5221 return MONO_EXC_ARRAY_TYPE_MISMATCH;
5222 if (strcmp (name, "ArgumentException") == 0)
5223 return MONO_EXC_ARGUMENT;
5224 g_error ("Unknown intrinsic exception %s\n", name);
5229 mono_arch_emit_exceptions (MonoCompile *cfg)
5231 MonoJumpInfo *patch_info;
5234 guint8* exc_throw_pos [MONO_EXC_INTRINS_NUM];
5235 guint8 exc_throw_found [MONO_EXC_INTRINS_NUM];
5236 int max_epilog_size = 50;
5238 for (i = 0; i < MONO_EXC_INTRINS_NUM; i++) {
5239 exc_throw_pos [i] = NULL;
5240 exc_throw_found [i] = 0;
5243 /* count the number of exception infos */
5246 * make sure we have enough space for exceptions
5248 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5249 if (patch_info->type == MONO_PATCH_INFO_EXC) {
5250 i = exception_id_by_name (patch_info->data.target);
5251 if (!exc_throw_found [i]) {
5252 max_epilog_size += 32;
5253 exc_throw_found [i] = TRUE;
5258 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
5259 cfg->code_size *= 2;
5260 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5261 mono_jit_stats.code_reallocs++;
5264 code = cfg->native_code + cfg->code_len;
5266 /* add code to raise exceptions */
5267 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5268 switch (patch_info->type) {
5269 case MONO_PATCH_INFO_EXC: {
5270 MonoClass *exc_class;
5271 unsigned char *ip = patch_info->ip.i + cfg->native_code;
5273 i = exception_id_by_name (patch_info->data.target);
5274 if (exc_throw_pos [i]) {
5275 arm_patch (ip, exc_throw_pos [i]);
5276 patch_info->type = MONO_PATCH_INFO_NONE;
5279 exc_throw_pos [i] = code;
5281 arm_patch (ip, code);
5283 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
5284 g_assert (exc_class);
5286 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR);
5287 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
5288 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5289 patch_info->data.name = "mono_arch_throw_corlib_exception";
5290 patch_info->ip.i = code - cfg->native_code;
5292 *(guint32*)(gpointer)code = exc_class->type_token;
5302 cfg->code_len = code - cfg->native_code;
5304 g_assert (cfg->code_len < cfg->code_size);
5308 #endif /* #ifndef DISABLE_JIT */
5310 static gboolean tls_offset_inited = FALSE;
5313 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
5315 if (!tls_offset_inited) {
5316 tls_offset_inited = TRUE;
5318 lmf_tls_offset = mono_get_lmf_tls_offset ();
5319 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
5324 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
5329 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5336 mono_arch_print_tree (MonoInst *tree, int arity)
5342 mono_arch_get_domain_intrinsic (MonoCompile* cfg)
5344 return mono_get_domain_intrinsic (cfg);
5348 mono_arch_get_patch_offset (guint8 *code)
5355 mono_arch_flush_register_windows (void)
5359 #ifdef MONO_ARCH_HAVE_IMT
5364 mono_arch_emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoInst *imt_arg)
5366 if (cfg->compile_aot) {
5367 int method_reg = mono_alloc_ireg (cfg);
5370 call->dynamic_imt_arg = TRUE;
5373 mono_call_inst_add_outarg_reg (cfg, call, imt_arg->dreg, ARMREG_V5, FALSE);
5375 MONO_INST_NEW (cfg, ins, OP_AOTCONST);
5376 ins->dreg = method_reg;
5377 ins->inst_p0 = call->method;
5378 ins->inst_c1 = MONO_PATCH_INFO_METHODCONST;
5379 MONO_ADD_INS (cfg->cbb, ins);
5381 mono_call_inst_add_outarg_reg (cfg, call, method_reg, ARMREG_V5, FALSE);
5383 } else if (cfg->generic_context || imt_arg || mono_use_llvm) {
5385 /* Always pass in a register for simplicity */
5386 call->dynamic_imt_arg = TRUE;
5388 cfg->uses_rgctx_reg = TRUE;
5391 mono_call_inst_add_outarg_reg (cfg, call, imt_arg->dreg, ARMREG_V5, FALSE);
5394 int method_reg = mono_alloc_preg (cfg);
5396 MONO_INST_NEW (cfg, ins, OP_PCONST);
5397 ins->inst_p0 = call->method;
5398 ins->dreg = method_reg;
5399 MONO_ADD_INS (cfg->cbb, ins);
5401 mono_call_inst_add_outarg_reg (cfg, call, method_reg, ARMREG_V5, FALSE);
5406 #endif /* DISABLE_JIT */
5409 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
5411 guint32 *code_ptr = (guint32*)code;
5416 return (MonoMethod*)regs [ARMREG_V5];
5418 /* The IMT value is stored in the code stream right after the LDC instruction. */
5419 if (!IS_LDR_PC (code_ptr [0])) {
5420 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]);
5421 g_assert (IS_LDR_PC (code_ptr [0]));
5423 if (code_ptr [1] == 0)
5424 /* This is AOTed code, the IMT method is in V5 */
5425 return (MonoMethod*)regs [ARMREG_V5];
5427 return (MonoMethod*) code_ptr [1];
5431 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
5433 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
5436 #define ENABLE_WRONG_METHOD_CHECK 0
5437 #define BASE_SIZE (6 * 4)
5438 #define BSEARCH_ENTRY_SIZE (4 * 4)
5439 #define CMP_SIZE (3 * 4)
5440 #define BRANCH_SIZE (1 * 4)
5441 #define CALL_SIZE (2 * 4)
5442 #define WMC_SIZE (5 * 4)
5443 #define DISTANCE(A, B) (((gint32)(B)) - ((gint32)(A)))
5446 arm_emit_value_and_patch_ldr (arminstr_t *code, arminstr_t *target, guint32 value)
5448 guint32 delta = DISTANCE (target, code);
5450 g_assert (delta >= 0 && delta <= 0xFFF);
5451 *target = *target | delta;
5457 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
5458 gpointer fail_tramp)
5460 int size, i, extra_space = 0;
5461 arminstr_t *code, *start, *vtable_target = NULL;
5462 gboolean large_offsets = FALSE;
5463 guint32 **constant_pool_starts;
5466 constant_pool_starts = g_new0 (guint32*, count);
5468 for (i = 0; i < count; ++i) {
5469 MonoIMTCheckItem *item = imt_entries [i];
5470 if (item->is_equals) {
5471 gboolean fail_case = !item->check_target_idx && fail_tramp;
5473 if (item->has_target_code || !arm_is_imm12 (DISTANCE (vtable, &vtable->vtable[item->value.vtable_slot]))) {
5474 item->chunk_size += 32;
5475 large_offsets = TRUE;
5478 if (item->check_target_idx || fail_case) {
5479 if (!item->compare_done || fail_case)
5480 item->chunk_size += CMP_SIZE;
5481 item->chunk_size += BRANCH_SIZE;
5483 #if ENABLE_WRONG_METHOD_CHECK
5484 item->chunk_size += WMC_SIZE;
5488 item->chunk_size += 16;
5489 large_offsets = TRUE;
5491 item->chunk_size += CALL_SIZE;
5493 item->chunk_size += BSEARCH_ENTRY_SIZE;
5494 imt_entries [item->check_target_idx]->compare_done = TRUE;
5496 size += item->chunk_size;
5500 size += 4 * count; /* The ARM_ADD_REG_IMM to pop the stack */
5503 code = mono_method_alloc_generic_virtual_thunk (domain, size);
5505 code = mono_domain_code_reserve (domain, size);
5509 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);
5510 for (i = 0; i < count; ++i) {
5511 MonoIMTCheckItem *item = imt_entries [i];
5512 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);
5517 ARM_PUSH4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5519 ARM_PUSH2 (code, ARMREG_R0, ARMREG_R1);
5520 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_LR, -4);
5521 vtable_target = code;
5522 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
5524 if (mono_use_llvm) {
5525 /* LLVM always passes the IMT method in R5 */
5526 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_V5);
5528 /* R0 == 0 means we are called from AOT code. In this case, V5 contains the IMT method */
5529 ARM_CMP_REG_IMM8 (code, ARMREG_R0, 0);
5530 ARM_MOV_REG_REG_COND (code, ARMREG_R0, ARMREG_V5, ARMCOND_EQ);
5533 for (i = 0; i < count; ++i) {
5534 MonoIMTCheckItem *item = imt_entries [i];
5535 arminstr_t *imt_method = NULL, *vtable_offset_ins = NULL, *target_code_ins = NULL;
5536 gint32 vtable_offset;
5538 item->code_target = (guint8*)code;
5540 if (item->is_equals) {
5541 gboolean fail_case = !item->check_target_idx && fail_tramp;
5543 if (item->check_target_idx || fail_case) {
5544 if (!item->compare_done || fail_case) {
5546 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5547 ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
5549 item->jmp_code = (guint8*)code;
5550 ARM_B_COND (code, ARMCOND_NE, 0);
5552 /*Enable the commented code to assert on wrong method*/
5553 #if ENABLE_WRONG_METHOD_CHECK
5555 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5556 ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
5557 ARM_B_COND (code, ARMCOND_NE, 1);
5563 if (item->has_target_code) {
5564 target_code_ins = code;
5565 /* Load target address */
5566 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5567 /* Save it to the fourth slot */
5568 ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
5569 /* Restore registers and branch */
5570 ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5572 code = arm_emit_value_and_patch_ldr (code, target_code_ins, (gsize)item->value.target_code);
5574 vtable_offset = DISTANCE (vtable, &vtable->vtable[item->value.vtable_slot]);
5575 if (!arm_is_imm12 (vtable_offset)) {
5577 * We need to branch to a computed address but we don't have
5578 * a free register to store it, since IP must contain the
5579 * vtable address. So we push the two values to the stack, and
5580 * load them both using LDM.
5582 /* Compute target address */
5583 vtable_offset_ins = code;
5584 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5585 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_IP, ARMREG_R1);
5586 /* Save it to the fourth slot */
5587 ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
5588 /* Restore registers and branch */
5589 ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5591 code = arm_emit_value_and_patch_ldr (code, vtable_offset_ins, vtable_offset);
5593 ARM_POP2 (code, ARMREG_R0, ARMREG_R1);
5595 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 2 * sizeof (gpointer));
5596 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, vtable_offset);
5601 arm_patch (item->jmp_code, (guchar*)code);
5603 target_code_ins = code;
5604 /* Load target address */
5605 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5606 /* Save it to the fourth slot */
5607 ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
5608 /* Restore registers and branch */
5609 ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5611 code = arm_emit_value_and_patch_ldr (code, target_code_ins, (gsize)fail_tramp);
5612 item->jmp_code = NULL;
5616 code = arm_emit_value_and_patch_ldr (code, imt_method, (guint32)item->key);
5618 /*must emit after unconditional branch*/
5619 if (vtable_target) {
5620 code = arm_emit_value_and_patch_ldr (code, vtable_target, (guint32)vtable);
5621 item->chunk_size += 4;
5622 vtable_target = NULL;
5625 /*We reserve the space for bsearch IMT values after the first entry with an absolute jump*/
5626 constant_pool_starts [i] = code;
5628 code += extra_space;
5632 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5633 ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
5635 item->jmp_code = (guint8*)code;
5636 ARM_B_COND (code, ARMCOND_GE, 0);
5641 for (i = 0; i < count; ++i) {
5642 MonoIMTCheckItem *item = imt_entries [i];
5643 if (item->jmp_code) {
5644 if (item->check_target_idx)
5645 arm_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
5647 if (i > 0 && item->is_equals) {
5649 arminstr_t *space_start = constant_pool_starts [i];
5650 for (j = i - 1; j >= 0 && !imt_entries [j]->is_equals; --j) {
5651 space_start = arm_emit_value_and_patch_ldr (space_start, (arminstr_t*)imt_entries [j]->code_target, (guint32)imt_entries [j]->key);
5658 char *buff = g_strdup_printf ("thunk_for_class_%s_%s_entries_%d", vtable->klass->name_space, vtable->klass->name, count);
5659 mono_disassemble_code (NULL, (guint8*)start, size, buff);
5664 g_free (constant_pool_starts);
5666 mono_arch_flush_icache ((guint8*)start, size);
5667 mono_stats.imt_thunks_size += code - start;
5669 g_assert (DISTANCE (start, code) <= size);
5676 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
5678 if (reg == ARMREG_SP)
5679 return (gpointer)ctx->esp;
5681 return (gpointer)ctx->regs [reg];
5685 * mono_arch_get_trampolines:
5687 * Return a list of MonoTrampInfo structures describing arch specific trampolines
5691 mono_arch_get_trampolines (gboolean aot)
5693 return mono_arm_get_exception_trampolines (aot);
5697 * mono_arch_set_breakpoint:
5699 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
5700 * The location should contain code emitted by OP_SEQ_POINT.
5703 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
5706 guint32 native_offset = ip - (guint8*)ji->code_start;
5709 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
5711 g_assert (native_offset % 4 == 0);
5712 g_assert (info->bp_addrs [native_offset / 4] == 0);
5713 info->bp_addrs [native_offset / 4] = bp_trigger_page;
5715 int dreg = ARMREG_LR;
5717 /* Read from another trigger page */
5718 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
5720 *(int*)code = (int)bp_trigger_page;
5722 ARM_LDR_IMM (code, dreg, dreg, 0);
5724 mono_arch_flush_icache (code - 16, 16);
5727 /* This is currently implemented by emitting an SWI instruction, which
5728 * qemu/linux seems to convert to a SIGILL.
5730 *(int*)code = (0xef << 24) | 8;
5732 mono_arch_flush_icache (code - 4, 4);
5738 * mono_arch_clear_breakpoint:
5740 * Clear the breakpoint at IP.
5743 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
5749 guint32 native_offset = ip - (guint8*)ji->code_start;
5750 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
5752 g_assert (native_offset % 4 == 0);
5753 g_assert (info->bp_addrs [native_offset / 4] == bp_trigger_page);
5754 info->bp_addrs [native_offset / 4] = 0;
5756 for (i = 0; i < 4; ++i)
5759 mono_arch_flush_icache (ip, code - ip);
5764 * mono_arch_start_single_stepping:
5766 * Start single stepping.
5769 mono_arch_start_single_stepping (void)
5771 mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
5775 * mono_arch_stop_single_stepping:
5777 * Stop single stepping.
5780 mono_arch_stop_single_stepping (void)
5782 mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
5786 #define DBG_SIGNAL SIGBUS
5788 #define DBG_SIGNAL SIGSEGV
5792 * mono_arch_is_single_step_event:
5794 * Return whenever the machine state in SIGCTX corresponds to a single
5798 mono_arch_is_single_step_event (void *info, void *sigctx)
5800 siginfo_t *sinfo = info;
5802 /* Sometimes the address is off by 4 */
5803 if (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128)
5810 * mono_arch_is_breakpoint_event:
5812 * Return whenever the machine state in SIGCTX corresponds to a breakpoint event.
5815 mono_arch_is_breakpoint_event (void *info, void *sigctx)
5817 siginfo_t *sinfo = info;
5819 if (sinfo->si_signo == DBG_SIGNAL) {
5820 /* Sometimes the address is off by 4 */
5821 if (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128)
5831 mono_arch_get_ip_for_breakpoint (MonoJitInfo *ji, MonoContext *ctx)
5833 guint8 *ip = MONO_CONTEXT_GET_IP (ctx);
5844 mono_arch_get_ip_for_single_step (MonoJitInfo *ji, MonoContext *ctx)
5846 guint8 *ip = MONO_CONTEXT_GET_IP (ctx);
5854 * mono_arch_skip_breakpoint:
5856 * See mini-amd64.c for docs.
5859 mono_arch_skip_breakpoint (MonoContext *ctx)
5861 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 4);
5865 * mono_arch_skip_single_step:
5867 * See mini-amd64.c for docs.
5870 mono_arch_skip_single_step (MonoContext *ctx)
5872 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 4);
5876 * mono_arch_get_seq_point_info:
5878 * See mini-amd64.c for docs.
5881 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
5886 // FIXME: Add a free function
5888 mono_domain_lock (domain);
5889 info = g_hash_table_lookup (domain_jit_info (domain)->arch_seq_points,
5891 mono_domain_unlock (domain);
5894 ji = mono_jit_info_table_find (domain, (char*)code);
5897 info = g_malloc0 (sizeof (SeqPointInfo) + ji->code_size);
5899 info->ss_trigger_page = ss_trigger_page;
5900 info->bp_trigger_page = bp_trigger_page;
5902 mono_domain_lock (domain);
5903 g_hash_table_insert (domain_jit_info (domain)->arch_seq_points,
5905 mono_domain_unlock (domain);
5912 * mono_arch_set_target:
5914 * Set the target architecture the JIT backend should generate code for, in the form
5915 * of a GNU target triplet. Only used in AOT mode.
5918 mono_arch_set_target (char *mtriple)
5920 /* The GNU target triple format is not very well documented */
5921 if (strstr (mtriple, "armv7")) {
5922 v6_supported = TRUE;
5923 v7_supported = TRUE;
5925 if (strstr (mtriple, "armv6")) {
5926 v6_supported = TRUE;
5928 if (strstr (mtriple, "darwin")) {
5929 v5_supported = TRUE;
5930 thumb_supported = TRUE;
5934 if (strstr (mtriple, "gnueabi"))
5935 eabi_supported = TRUE;