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.
9 * Copyright 2003-2011 Novell, Inc (http://www.novell.com)
10 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
15 #include <mono/metadata/appdomain.h>
16 #include <mono/metadata/debug-helpers.h>
17 #include <mono/utils/mono-mmap.h>
23 #include "debugger-agent.h"
25 #include "mono/arch/arm/arm-fpa-codegen.h"
26 #include "mono/arch/arm/arm-vfp-codegen.h"
28 #if defined(__ARM_EABI__) && defined(__linux__) && !defined(PLATFORM_ANDROID)
29 #define HAVE_AEABI_READ_TP 1
32 #ifdef ARM_FPU_VFP_HARD
48 #ifdef MONO_ARCH_SOFT_FLOAT
49 #define IS_SOFT_FLOAT 1
51 #define IS_SOFT_FLOAT 0
54 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
56 static gint lmf_tls_offset = -1;
57 static gint lmf_addr_tls_offset = -1;
59 /* This mutex protects architecture specific caches */
60 #define mono_mini_arch_lock() EnterCriticalSection (&mini_arch_mutex)
61 #define mono_mini_arch_unlock() LeaveCriticalSection (&mini_arch_mutex)
62 static CRITICAL_SECTION mini_arch_mutex;
64 static int v5_supported = 0;
65 static int v6_supported = 0;
66 static int v7_supported = 0;
67 static int thumb_supported = 0;
69 * Whenever to use the ARM EABI
71 static int eabi_supported = 0;
74 * Whenever we are on arm/darwin aka the iphone.
76 static int darwin = 0;
78 * Whenever to use the iphone ABI extensions:
79 * http://developer.apple.com/library/ios/documentation/Xcode/Conceptual/iPhoneOSABIReference/index.html
80 * Basically, r7 is used as a frame pointer and it should point to the saved r7 + lr.
81 * This is required for debugging/profiling tools to work, but it has some overhead so it should
82 * only be turned on in debug builds.
84 static int iphone_abi = 0;
87 * The FPU we are generating code for. This is NOT runtime configurable right now,
88 * since some things like MONO_ARCH_CALLEE_FREGS still depend on defines.
90 static MonoArmFPU arm_fpu;
94 static volatile int ss_trigger_var = 0;
96 static gpointer single_step_func_wrapper;
97 static gpointer breakpoint_func_wrapper;
100 * The code generated for sequence points reads from this location, which is
101 * made read-only when single stepping is enabled.
103 static gpointer ss_trigger_page;
105 /* Enabled breakpoints read from this trigger page */
106 static gpointer bp_trigger_page;
108 /* Structure used by the sequence points in AOTed code */
110 gpointer ss_trigger_page;
111 gpointer bp_trigger_page;
112 guint8* bp_addrs [MONO_ZERO_LEN_ARRAY];
117 * floating point support: on ARM it is a mess, there are at least 3
118 * different setups, each of which binary incompat with the other.
119 * 1) FPA: old and ugly, but unfortunately what current distros use
120 * the double binary format has the two words swapped. 8 double registers.
121 * Implemented usually by kernel emulation.
122 * 2) softfloat: the compiler emulates all the fp ops. Usually uses the
123 * ugly swapped double format (I guess a softfloat-vfp exists, too, though).
124 * 3) VFP: the new and actually sensible and useful FP support. Implemented
125 * in HW or kernel-emulated, requires new tools. I think this is what symbian uses.
127 * The plan is to write the FPA support first. softfloat can be tested in a chroot.
129 int mono_exc_esp_offset = 0;
131 #define arm_is_imm12(v) ((v) > -4096 && (v) < 4096)
132 #define arm_is_imm8(v) ((v) > -256 && (v) < 256)
133 #define arm_is_fpimm8(v) ((v) >= -1020 && (v) <= 1020)
135 #define LDR_MASK ((0xf << ARMCOND_SHIFT) | (3 << 26) | (1 << 22) | (1 << 20) | (15 << 12))
136 #define LDR_PC_VAL ((ARMCOND_AL << ARMCOND_SHIFT) | (1 << 26) | (0 << 22) | (1 << 20) | (15 << 12))
137 #define IS_LDR_PC(val) (((val) & LDR_MASK) == LDR_PC_VAL)
139 #define ADD_LR_PC_4 ((ARMCOND_AL << ARMCOND_SHIFT) | (1 << 25) | (1 << 23) | (ARMREG_PC << 16) | (ARMREG_LR << 12) | 4)
140 #define MOV_LR_PC ((ARMCOND_AL << ARMCOND_SHIFT) | (1 << 24) | (0xa << 20) | (ARMREG_LR << 12) | ARMREG_PC)
143 /* A variant of ARM_LDR_IMM which can handle large offsets */
144 #define ARM_LDR_IMM_GENERAL(code, dreg, basereg, offset, scratch_reg) do { \
145 if (arm_is_imm12 ((offset))) { \
146 ARM_LDR_IMM (code, (dreg), (basereg), (offset)); \
148 g_assert ((scratch_reg) != (basereg)); \
149 code = mono_arm_emit_load_imm (code, (scratch_reg), (offset)); \
150 ARM_LDR_REG_REG (code, (dreg), (basereg), (scratch_reg)); \
154 #define ARM_STR_IMM_GENERAL(code, dreg, basereg, offset, scratch_reg) do { \
155 if (arm_is_imm12 ((offset))) { \
156 ARM_STR_IMM (code, (dreg), (basereg), (offset)); \
158 g_assert ((scratch_reg) != (basereg)); \
159 code = mono_arm_emit_load_imm (code, (scratch_reg), (offset)); \
160 ARM_STR_REG_REG (code, (dreg), (basereg), (scratch_reg)); \
164 static void mono_arch_compute_omit_fp (MonoCompile *cfg);
167 mono_arch_regname (int reg)
169 static const char * rnames[] = {
170 "arm_r0", "arm_r1", "arm_r2", "arm_r3", "arm_v1",
171 "arm_v2", "arm_v3", "arm_v4", "arm_v5", "arm_v6",
172 "arm_v7", "arm_fp", "arm_ip", "arm_sp", "arm_lr",
175 if (reg >= 0 && reg < 16)
181 mono_arch_fregname (int reg)
183 static const char * rnames[] = {
184 "arm_f0", "arm_f1", "arm_f2", "arm_f3", "arm_f4",
185 "arm_f5", "arm_f6", "arm_f7", "arm_f8", "arm_f9",
186 "arm_f10", "arm_f11", "arm_f12", "arm_f13", "arm_f14",
187 "arm_f15", "arm_f16", "arm_f17", "arm_f18", "arm_f19",
188 "arm_f20", "arm_f21", "arm_f22", "arm_f23", "arm_f24",
189 "arm_f25", "arm_f26", "arm_f27", "arm_f28", "arm_f29",
192 if (reg >= 0 && reg < 32)
200 emit_big_add (guint8 *code, int dreg, int sreg, int imm)
202 int imm8, rot_amount;
203 if ((imm8 = mono_arm_is_rotated_imm8 (imm, &rot_amount)) >= 0) {
204 ARM_ADD_REG_IMM (code, dreg, sreg, imm8, rot_amount);
207 g_assert (dreg != sreg);
208 code = mono_arm_emit_load_imm (code, dreg, imm);
209 ARM_ADD_REG_REG (code, dreg, dreg, sreg);
214 emit_memcpy (guint8 *code, int size, int dreg, int doffset, int sreg, int soffset)
216 /* we can use r0-r3, since this is called only for incoming args on the stack */
217 if (size > sizeof (gpointer) * 4) {
219 code = emit_big_add (code, ARMREG_R0, sreg, soffset);
220 code = emit_big_add (code, ARMREG_R1, dreg, doffset);
221 start_loop = code = mono_arm_emit_load_imm (code, ARMREG_R2, size);
222 ARM_LDR_IMM (code, ARMREG_R3, ARMREG_R0, 0);
223 ARM_STR_IMM (code, ARMREG_R3, ARMREG_R1, 0);
224 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, 4);
225 ARM_ADD_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, 4);
226 ARM_SUBS_REG_IMM8 (code, ARMREG_R2, ARMREG_R2, 4);
227 ARM_B_COND (code, ARMCOND_NE, 0);
228 arm_patch (code - 4, start_loop);
231 if (arm_is_imm12 (doffset) && arm_is_imm12 (doffset + size) &&
232 arm_is_imm12 (soffset) && arm_is_imm12 (soffset + size)) {
234 ARM_LDR_IMM (code, ARMREG_LR, sreg, soffset);
235 ARM_STR_IMM (code, ARMREG_LR, dreg, doffset);
241 code = emit_big_add (code, ARMREG_R0, sreg, soffset);
242 code = emit_big_add (code, ARMREG_R1, dreg, doffset);
243 doffset = soffset = 0;
245 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_R0, soffset);
246 ARM_STR_IMM (code, ARMREG_LR, ARMREG_R1, doffset);
252 g_assert (size == 0);
257 emit_call_reg (guint8 *code, int reg)
260 ARM_BLX_REG (code, reg);
262 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
266 ARM_MOV_REG_REG (code, ARMREG_PC, reg);
272 emit_call_seq (MonoCompile *cfg, guint8 *code)
274 if (cfg->method->dynamic) {
275 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
277 *(gpointer*)code = NULL;
279 code = emit_call_reg (code, ARMREG_IP);
287 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
289 switch (ins->opcode) {
292 case OP_FCALL_MEMBASE:
294 if (ins->dreg != ARM_FPA_F0)
295 ARM_FPA_MVFD (code, ins->dreg, ARM_FPA_F0);
297 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4) {
298 ARM_FMSR (code, ins->dreg, ARMREG_R0);
299 ARM_CVTS (code, ins->dreg, ins->dreg);
301 ARM_FMDRR (code, ARMREG_R0, ARMREG_R1, ins->dreg);
313 * Emit code to push an LMF structure on the LMF stack.
314 * On arm, this is intermixed with the initialization of other fields of the structure.
317 emit_save_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset)
319 gboolean get_lmf_fast = FALSE;
322 #ifdef HAVE_AEABI_READ_TP
323 gint32 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
325 if (lmf_addr_tls_offset != -1) {
328 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
329 (gpointer)"__aeabi_read_tp");
330 code = emit_call_seq (cfg, code);
332 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, lmf_addr_tls_offset);
337 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
338 (gpointer)"mono_get_lmf_addr");
339 code = emit_call_seq (cfg, code);
341 /* we build the MonoLMF structure on the stack - see mini-arm.h */
342 /* lmf_offset is the offset from the previous stack pointer,
343 * alloc_size is the total stack space allocated, so the offset
344 * of MonoLMF from the current stack ptr is alloc_size - lmf_offset.
345 * The pointer to the struct is put in r1 (new_lmf).
346 * ip is used as scratch
347 * The callee-saved registers are already in the MonoLMF structure
349 code = emit_big_add (code, ARMREG_R1, ARMREG_SP, lmf_offset);
350 /* r0 is the result from mono_get_lmf_addr () */
351 ARM_STR_IMM (code, ARMREG_R0, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
352 /* new_lmf->previous_lmf = *lmf_addr */
353 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
354 ARM_STR_IMM (code, ARMREG_IP, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
355 /* *(lmf_addr) = r1 */
356 ARM_STR_IMM (code, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
357 /* Skip method (only needed for trampoline LMF frames) */
358 ARM_STR_IMM (code, ARMREG_SP, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, sp));
359 ARM_STR_IMM (code, ARMREG_FP, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, fp));
360 /* save the current IP */
361 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_PC);
362 ARM_STR_IMM (code, ARMREG_IP, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, ip));
364 for (i = 0; i < sizeof (MonoLMF); i += sizeof (mgreg_t))
365 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + i, SLOT_NOREF);
373 * Emit code to pop an LMF structure from the LMF stack.
376 emit_restore_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset)
380 if (lmf_offset < 32) {
381 basereg = cfg->frame_reg;
386 code = emit_big_add (code, ARMREG_R2, cfg->frame_reg, lmf_offset);
389 /* ip = previous_lmf */
390 ARM_LDR_IMM (code, ARMREG_IP, basereg, offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf));
392 ARM_LDR_IMM (code, ARMREG_LR, basereg, offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr));
393 /* *(lmf_addr) = previous_lmf */
394 ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
399 #endif /* #ifndef DISABLE_JIT */
402 * mono_arch_get_argument_info:
403 * @csig: a method signature
404 * @param_count: the number of parameters to consider
405 * @arg_info: an array to store the result infos
407 * Gathers information on parameters such as size, alignment and
408 * padding. arg_info should be large enought to hold param_count + 1 entries.
410 * Returns the size of the activation frame.
413 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
415 int k, frame_size = 0;
416 guint32 size, align, pad;
419 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
420 frame_size += sizeof (gpointer);
424 arg_info [0].offset = offset;
427 frame_size += sizeof (gpointer);
431 arg_info [0].size = frame_size;
433 for (k = 0; k < param_count; k++) {
434 size = mini_type_stack_size_full (NULL, csig->params [k], &align, csig->pinvoke);
436 /* ignore alignment for now */
439 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
440 arg_info [k].pad = pad;
442 arg_info [k + 1].pad = 0;
443 arg_info [k + 1].size = size;
445 arg_info [k + 1].offset = offset;
449 align = MONO_ARCH_FRAME_ALIGNMENT;
450 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
451 arg_info [k].pad = pad;
456 #define MAX_ARCH_DELEGATE_PARAMS 3
459 get_delegate_invoke_impl (gboolean has_target, gboolean param_count, guint32 *code_size)
461 guint8 *code, *start;
464 start = code = mono_global_codeman_reserve (12);
466 /* Replace the this argument with the target */
467 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R0, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
468 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, G_STRUCT_OFFSET (MonoDelegate, target));
469 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
471 g_assert ((code - start) <= 12);
473 mono_arch_flush_icache (start, 12);
477 size = 8 + param_count * 4;
478 start = code = mono_global_codeman_reserve (size);
480 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R0, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
481 /* slide down the arguments */
482 for (i = 0; i < param_count; ++i) {
483 ARM_MOV_REG_REG (code, (ARMREG_R0 + i), (ARMREG_R0 + i + 1));
485 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
487 g_assert ((code - start) <= size);
489 mono_arch_flush_icache (start, size);
493 *code_size = code - start;
499 * mono_arch_get_delegate_invoke_impls:
501 * Return a list of MonoAotTrampInfo structures for the delegate invoke impl
505 mono_arch_get_delegate_invoke_impls (void)
512 code = get_delegate_invoke_impl (TRUE, 0, &code_len);
513 res = g_slist_prepend (res, mono_tramp_info_create (g_strdup ("delegate_invoke_impl_has_target"), code, code_len, NULL, NULL));
515 for (i = 0; i <= MAX_ARCH_DELEGATE_PARAMS; ++i) {
516 code = get_delegate_invoke_impl (FALSE, i, &code_len);
517 res = g_slist_prepend (res, mono_tramp_info_create (g_strdup_printf ("delegate_invoke_impl_target_%d", i), code, code_len, NULL, NULL));
524 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
526 guint8 *code, *start;
528 /* FIXME: Support more cases */
529 if (MONO_TYPE_ISSTRUCT (sig->ret))
533 static guint8* cached = NULL;
534 mono_mini_arch_lock ();
536 mono_mini_arch_unlock ();
541 start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
543 start = get_delegate_invoke_impl (TRUE, 0, NULL);
545 mono_mini_arch_unlock ();
548 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
551 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
553 for (i = 0; i < sig->param_count; ++i)
554 if (!mono_is_regsize_var (sig->params [i]))
557 mono_mini_arch_lock ();
558 code = cache [sig->param_count];
560 mono_mini_arch_unlock ();
565 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
566 start = mono_aot_get_trampoline (name);
569 start = get_delegate_invoke_impl (FALSE, sig->param_count, NULL);
571 cache [sig->param_count] = start;
572 mono_mini_arch_unlock ();
580 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
582 return (gpointer)regs [ARMREG_R0];
586 * Initialize the cpu to execute managed code.
589 mono_arch_cpu_init (void)
591 #if defined(__ARM_EABI__)
592 eabi_supported = TRUE;
594 #if defined(__APPLE__) && defined(MONO_CROSS_COMPILE)
597 i8_align = __alignof__ (gint64);
602 create_function_wrapper (gpointer function)
604 guint8 *start, *code;
606 start = code = mono_global_codeman_reserve (96);
609 * Construct the MonoContext structure on the stack.
612 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, sizeof (MonoContext));
614 /* save ip, lr and pc into their correspodings ctx.regs slots. */
615 ARM_STR_IMM (code, ARMREG_IP, ARMREG_SP, G_STRUCT_OFFSET (MonoContext, regs) + sizeof (mgreg_t) * ARMREG_IP);
616 ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, G_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_LR);
617 ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, G_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_PC);
619 /* save r0..r10 and fp */
620 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ARMREG_SP, G_STRUCT_OFFSET (MonoContext, regs));
621 ARM_STM (code, ARMREG_IP, 0x0fff);
623 /* now we can update fp. */
624 ARM_MOV_REG_REG (code, ARMREG_FP, ARMREG_SP);
626 /* make ctx.esp hold the actual value of sp at the beginning of this method. */
627 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_FP, sizeof (MonoContext));
628 ARM_STR_IMM (code, ARMREG_R0, ARMREG_IP, 4 * ARMREG_SP);
629 ARM_STR_IMM (code, ARMREG_R0, ARMREG_FP, G_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_SP);
631 /* make ctx.eip hold the address of the call. */
632 ARM_SUB_REG_IMM8 (code, ARMREG_LR, ARMREG_LR, 4);
633 ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, G_STRUCT_OFFSET (MonoContext, pc));
635 /* r0 now points to the MonoContext */
636 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_FP);
639 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
641 *(gpointer*)code = function;
643 ARM_BLX_REG (code, ARMREG_IP);
645 /* we're back; save ctx.eip and ctx.esp into the corresponding regs slots. */
646 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_FP, G_STRUCT_OFFSET (MonoContext, pc));
647 ARM_STR_IMM (code, ARMREG_R0, ARMREG_FP, G_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_LR);
648 ARM_STR_IMM (code, ARMREG_R0, ARMREG_FP, G_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_PC);
650 /* make ip point to the regs array, then restore everything, including pc. */
651 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ARMREG_FP, G_STRUCT_OFFSET (MonoContext, regs));
652 ARM_LDM (code, ARMREG_IP, 0xffff);
654 mono_arch_flush_icache (start, code - start);
660 * Initialize architecture specific code.
663 mono_arch_init (void)
665 InitializeCriticalSection (&mini_arch_mutex);
667 if (mini_get_debug_options ()->soft_breakpoints) {
668 single_step_func_wrapper = create_function_wrapper (debugger_agent_single_step_from_context);
669 breakpoint_func_wrapper = create_function_wrapper (debugger_agent_breakpoint_from_context);
671 ss_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT);
672 bp_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT);
673 mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
676 mono_aot_register_jit_icall ("mono_arm_throw_exception", mono_arm_throw_exception);
677 mono_aot_register_jit_icall ("mono_arm_throw_exception_by_token", mono_arm_throw_exception_by_token);
678 mono_aot_register_jit_icall ("mono_arm_resume_unwind", mono_arm_resume_unwind);
681 arm_fpu = MONO_ARM_FPU_FPA;
682 #elif defined(ARM_FPU_VFP_HARD)
683 arm_fpu = MONO_ARM_FPU_VFP_HARD;
684 #elif defined(ARM_FPU_VFP)
685 arm_fpu = MONO_ARM_FPU_VFP;
687 arm_fpu = MONO_ARM_FPU_NONE;
692 * Cleanup architecture specific code.
695 mono_arch_cleanup (void)
700 * This function returns the optimizations supported on this cpu.
703 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
706 const char *cpu_arch = getenv ("MONO_CPU_ARCH");
707 if (cpu_arch != NULL) {
708 thumb_supported = strstr (cpu_arch, "thumb") != NULL;
709 if (strncmp (cpu_arch, "armv", 4) == 0) {
710 v5_supported = cpu_arch [4] >= '5';
711 v6_supported = cpu_arch [4] >= '6';
712 v7_supported = cpu_arch [4] >= '7';
716 thumb_supported = TRUE;
723 FILE *file = fopen ("/proc/cpuinfo", "r");
725 while ((line = fgets (buf, 512, file))) {
726 if (strncmp (line, "Processor", 9) == 0) {
727 char *ver = strstr (line, "(v");
728 if (ver && (ver [2] == '5' || ver [2] == '6' || ver [2] == '7'))
730 if (ver && (ver [2] == '6' || ver [2] == '7'))
732 if (ver && (ver [2] == '7'))
736 if (strncmp (line, "Features", 8) == 0) {
737 char *th = strstr (line, "thumb");
739 thumb_supported = TRUE;
747 /*printf ("features: v5: %d, thumb: %d\n", v5_supported, thumb_supported);*/
752 /* no arm-specific optimizations yet */
760 is_regsize_var (MonoType *t) {
763 t = mini_type_get_underlying_type (NULL, t);
770 case MONO_TYPE_FNPTR:
772 case MONO_TYPE_OBJECT:
773 case MONO_TYPE_STRING:
774 case MONO_TYPE_CLASS:
775 case MONO_TYPE_SZARRAY:
776 case MONO_TYPE_ARRAY:
778 case MONO_TYPE_GENERICINST:
779 if (!mono_type_generic_inst_is_valuetype (t))
782 case MONO_TYPE_VALUETYPE:
789 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
794 for (i = 0; i < cfg->num_varinfo; i++) {
795 MonoInst *ins = cfg->varinfo [i];
796 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
799 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
802 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
805 /* we can only allocate 32 bit values */
806 if (is_regsize_var (ins->inst_vtype)) {
807 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
808 g_assert (i == vmv->idx);
809 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
816 #define USE_EXTRA_TEMPS 0
819 mono_arch_get_global_int_regs (MonoCompile *cfg)
823 mono_arch_compute_omit_fp (cfg);
826 * FIXME: Interface calls might go through a static rgctx trampoline which
827 * sets V5, but it doesn't save it, so we need to save it ourselves, and
830 if (cfg->flags & MONO_CFG_HAS_CALLS)
831 cfg->uses_rgctx_reg = TRUE;
833 if (cfg->arch.omit_fp)
834 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_FP));
835 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V1));
836 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V2));
837 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V3));
839 /* V4=R7 is used as a frame pointer, but V7=R10 is preserved */
840 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V7));
842 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V4));
843 if (!(cfg->compile_aot || cfg->uses_rgctx_reg || COMPILE_LLVM (cfg)))
844 /* V5 is reserved for passing the vtable/rgctx/IMT method */
845 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V5));
846 /*regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V6));*/
847 /*regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V7));*/
853 * mono_arch_regalloc_cost:
855 * Return the cost, in number of memory references, of the action of
856 * allocating the variable VMV into a register during global register
860 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
866 #endif /* #ifndef DISABLE_JIT */
868 #ifndef __GNUC_PREREQ
869 #define __GNUC_PREREQ(maj, min) (0)
873 mono_arch_flush_icache (guint8 *code, gint size)
876 sys_icache_invalidate (code, size);
877 #elif __GNUC_PREREQ(4, 1)
878 __clear_cache (code, code + size);
879 #elif defined(PLATFORM_ANDROID)
880 const int syscall = 0xf0002;
888 : "r" (code), "r" (code + size), "r" (syscall)
889 : "r0", "r1", "r7", "r2"
892 __asm __volatile ("mov r0, %0\n"
895 "swi 0x9f0002 @ sys_cacheflush"
897 : "r" (code), "r" (code + size), "r" (0)
898 : "r0", "r1", "r3" );
915 guint16 vtsize; /* in param area */
919 guint8 size : 4; /* 1, 2, 4, 8, or regs used by RegTypeStructByVal */
925 gboolean vtype_retaddr;
926 /* The index of the vret arg in the argument list */
936 /*#define __alignof__(a) sizeof(a)*/
937 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
943 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
946 if (*gr > ARMREG_R3) {
947 ainfo->offset = *stack_size;
948 ainfo->reg = ARMREG_SP; /* in the caller */
949 ainfo->storage = RegTypeBase;
952 ainfo->storage = RegTypeGeneral;
959 split = i8_align == 4;
963 if (*gr == ARMREG_R3 && split) {
964 /* first word in r3 and the second on the stack */
965 ainfo->offset = *stack_size;
966 ainfo->reg = ARMREG_SP; /* in the caller */
967 ainfo->storage = RegTypeBaseGen;
969 } else if (*gr >= ARMREG_R3) {
970 if (eabi_supported) {
971 /* darwin aligns longs to 4 byte only */
977 ainfo->offset = *stack_size;
978 ainfo->reg = ARMREG_SP; /* in the caller */
979 ainfo->storage = RegTypeBase;
982 if (eabi_supported) {
983 if (i8_align == 8 && ((*gr) & 1))
986 ainfo->storage = RegTypeIRegPair;
995 get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSignature *sig)
998 int n = sig->hasthis + sig->param_count;
999 MonoType *simpletype;
1000 guint32 stack_size = 0;
1002 gboolean is_pinvoke = sig->pinvoke;
1005 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1007 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1012 /* FIXME: handle returning a struct */
1013 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
1016 if (is_pinvoke && mono_class_native_size (mono_class_from_mono_type (sig->ret), &align) <= sizeof (gpointer)) {
1017 cinfo->ret.storage = RegTypeStructByVal;
1019 cinfo->vtype_retaddr = TRUE;
1026 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
1027 * the first argument, allowing 'this' to be always passed in the first arg reg.
1028 * Also do this if the first argument is a reference type, since virtual calls
1029 * are sometimes made using calli without sig->hasthis set, like in the delegate
1032 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]))))) {
1034 add_general (&gr, &stack_size, cinfo->args + 0, TRUE);
1036 add_general (&gr, &stack_size, &cinfo->args [sig->hasthis + 0], TRUE);
1040 add_general (&gr, &stack_size, &cinfo->ret, TRUE);
1041 cinfo->vret_arg_index = 1;
1045 add_general (&gr, &stack_size, cinfo->args + 0, TRUE);
1049 if (cinfo->vtype_retaddr)
1050 add_general (&gr, &stack_size, &cinfo->ret, TRUE);
1053 DEBUG(printf("params: %d\n", sig->param_count));
1054 for (i = pstart; i < sig->param_count; ++i) {
1055 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1056 /* Prevent implicit arguments and sig_cookie from
1057 being passed in registers */
1059 /* Emit the signature cookie just before the implicit arguments */
1060 add_general (&gr, &stack_size, &cinfo->sig_cookie, TRUE);
1062 DEBUG(printf("param %d: ", i));
1063 if (sig->params [i]->byref) {
1064 DEBUG(printf("byref\n"));
1065 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
1069 simpletype = mini_type_get_underlying_type (NULL, sig->params [i]);
1070 switch (simpletype->type) {
1071 case MONO_TYPE_BOOLEAN:
1074 cinfo->args [n].size = 1;
1075 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
1078 case MONO_TYPE_CHAR:
1081 cinfo->args [n].size = 2;
1082 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
1087 cinfo->args [n].size = 4;
1088 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
1094 case MONO_TYPE_FNPTR:
1095 case MONO_TYPE_CLASS:
1096 case MONO_TYPE_OBJECT:
1097 case MONO_TYPE_STRING:
1098 case MONO_TYPE_SZARRAY:
1099 case MONO_TYPE_ARRAY:
1101 cinfo->args [n].size = sizeof (gpointer);
1102 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
1105 case MONO_TYPE_GENERICINST:
1106 if (!mono_type_generic_inst_is_valuetype (simpletype)) {
1107 cinfo->args [n].size = sizeof (gpointer);
1108 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
1113 case MONO_TYPE_TYPEDBYREF:
1114 case MONO_TYPE_VALUETYPE: {
1120 if (simpletype->type == MONO_TYPE_TYPEDBYREF) {
1121 size = sizeof (MonoTypedRef);
1122 align = sizeof (gpointer);
1124 MonoClass *klass = mono_class_from_mono_type (sig->params [i]);
1126 size = mono_class_native_size (klass, &align);
1128 size = mono_class_value_size (klass, &align);
1130 DEBUG(printf ("load %d bytes struct\n",
1131 mono_class_native_size (sig->params [i]->data.klass, NULL)));
1134 align_size += (sizeof (gpointer) - 1);
1135 align_size &= ~(sizeof (gpointer) - 1);
1136 nwords = (align_size + sizeof (gpointer) -1 ) / sizeof (gpointer);
1137 cinfo->args [n].storage = RegTypeStructByVal;
1138 cinfo->args [n].struct_size = size;
1139 /* FIXME: align stack_size if needed */
1140 if (eabi_supported) {
1141 if (align >= 8 && (gr & 1))
1144 if (gr > ARMREG_R3) {
1145 cinfo->args [n].size = 0;
1146 cinfo->args [n].vtsize = nwords;
1148 int rest = ARMREG_R3 - gr + 1;
1149 int n_in_regs = rest >= nwords? nwords: rest;
1151 cinfo->args [n].size = n_in_regs;
1152 cinfo->args [n].vtsize = nwords - n_in_regs;
1153 cinfo->args [n].reg = gr;
1155 nwords -= n_in_regs;
1157 cinfo->args [n].offset = stack_size;
1158 /*g_print ("offset for arg %d at %d\n", n, stack_size);*/
1159 stack_size += nwords * sizeof (gpointer);
1166 cinfo->args [n].size = 8;
1167 add_general (&gr, &stack_size, cinfo->args + n, FALSE);
1171 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
1175 /* Handle the case where there are no implicit arguments */
1176 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1177 /* Prevent implicit arguments and sig_cookie from
1178 being passed in registers */
1180 /* Emit the signature cookie just before the implicit arguments */
1181 add_general (&gr, &stack_size, &cinfo->sig_cookie, TRUE);
1185 simpletype = mini_type_get_underlying_type (NULL, sig->ret);
1186 switch (simpletype->type) {
1187 case MONO_TYPE_BOOLEAN:
1192 case MONO_TYPE_CHAR:
1198 case MONO_TYPE_FNPTR:
1199 case MONO_TYPE_CLASS:
1200 case MONO_TYPE_OBJECT:
1201 case MONO_TYPE_SZARRAY:
1202 case MONO_TYPE_ARRAY:
1203 case MONO_TYPE_STRING:
1204 cinfo->ret.storage = RegTypeGeneral;
1205 cinfo->ret.reg = ARMREG_R0;
1209 cinfo->ret.storage = RegTypeIRegPair;
1210 cinfo->ret.reg = ARMREG_R0;
1214 cinfo->ret.storage = RegTypeFP;
1215 cinfo->ret.reg = ARMREG_R0;
1216 /* FIXME: cinfo->ret.reg = ???;
1217 cinfo->ret.storage = RegTypeFP;*/
1219 case MONO_TYPE_GENERICINST:
1220 if (!mono_type_generic_inst_is_valuetype (simpletype)) {
1221 cinfo->ret.storage = RegTypeGeneral;
1222 cinfo->ret.reg = ARMREG_R0;
1226 case MONO_TYPE_VALUETYPE:
1227 case MONO_TYPE_TYPEDBYREF:
1228 if (cinfo->ret.storage != RegTypeStructByVal)
1229 cinfo->ret.storage = RegTypeStructByAddr;
1231 case MONO_TYPE_VOID:
1234 g_error ("Can't handle as return value 0x%x", sig->ret->type);
1238 /* align stack size to 8 */
1239 DEBUG (printf (" stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
1240 stack_size = (stack_size + 7) & ~7;
1242 cinfo->stack_usage = stack_size;
1248 G_GNUC_UNUSED static void
1253 G_GNUC_UNUSED static gboolean
1256 static int count = 0;
1259 if (!getenv ("COUNT"))
1262 if (count == atoi (getenv ("COUNT"))) {
1266 if (count > atoi (getenv ("COUNT"))) {
1274 debug_omit_fp (void)
1277 return debug_count ();
1284 * mono_arch_compute_omit_fp:
1286 * Determine whenever the frame pointer can be eliminated.
1289 mono_arch_compute_omit_fp (MonoCompile *cfg)
1291 MonoMethodSignature *sig;
1292 MonoMethodHeader *header;
1296 if (cfg->arch.omit_fp_computed)
1299 header = cfg->header;
1301 sig = mono_method_signature (cfg->method);
1303 if (!cfg->arch.cinfo)
1304 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1305 cinfo = cfg->arch.cinfo;
1308 * FIXME: Remove some of the restrictions.
1310 cfg->arch.omit_fp = TRUE;
1311 cfg->arch.omit_fp_computed = TRUE;
1313 if (cfg->disable_omit_fp)
1314 cfg->arch.omit_fp = FALSE;
1315 if (!debug_omit_fp ())
1316 cfg->arch.omit_fp = FALSE;
1318 if (cfg->method->save_lmf)
1319 cfg->arch.omit_fp = FALSE;
1321 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1322 cfg->arch.omit_fp = FALSE;
1323 if (header->num_clauses)
1324 cfg->arch.omit_fp = FALSE;
1325 if (cfg->param_area)
1326 cfg->arch.omit_fp = FALSE;
1327 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1328 cfg->arch.omit_fp = FALSE;
1329 if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
1330 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
1331 cfg->arch.omit_fp = FALSE;
1332 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1333 ArgInfo *ainfo = &cinfo->args [i];
1335 if (ainfo->storage == RegTypeBase || ainfo->storage == RegTypeBaseGen || ainfo->storage == RegTypeStructByVal) {
1337 * The stack offset can only be determined when the frame
1340 cfg->arch.omit_fp = FALSE;
1345 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1346 MonoInst *ins = cfg->varinfo [i];
1349 locals_size += mono_type_size (ins->inst_vtype, &ialign);
1354 * Set var information according to the calling convention. arm version.
1355 * The locals var stuff should most likely be split in another method.
1358 mono_arch_allocate_vars (MonoCompile *cfg)
1360 MonoMethodSignature *sig;
1361 MonoMethodHeader *header;
1363 int i, offset, size, align, curinst;
1367 sig = mono_method_signature (cfg->method);
1369 if (!cfg->arch.cinfo)
1370 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1371 cinfo = cfg->arch.cinfo;
1373 mono_arch_compute_omit_fp (cfg);
1375 if (cfg->arch.omit_fp)
1376 cfg->frame_reg = ARMREG_SP;
1378 cfg->frame_reg = ARMREG_FP;
1380 cfg->flags |= MONO_CFG_HAS_SPILLUP;
1382 /* allow room for the vararg method args: void* and long/double */
1383 if (mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method))
1384 cfg->param_area = MAX (cfg->param_area, sizeof (gpointer)*8);
1386 header = cfg->header;
1388 /* See mono_arch_get_global_int_regs () */
1389 if (cfg->flags & MONO_CFG_HAS_CALLS)
1390 cfg->uses_rgctx_reg = TRUE;
1392 if (cfg->frame_reg != ARMREG_SP)
1393 cfg->used_int_regs |= 1 << cfg->frame_reg;
1395 if (cfg->compile_aot || cfg->uses_rgctx_reg || COMPILE_LLVM (cfg))
1396 /* V5 is reserved for passing the vtable/rgctx/IMT method */
1397 cfg->used_int_regs |= (1 << ARMREG_V5);
1401 if (!MONO_TYPE_ISSTRUCT (sig->ret)) {
1402 switch (mini_type_get_underlying_type (NULL, sig->ret)->type) {
1403 case MONO_TYPE_VOID:
1406 cfg->ret->opcode = OP_REGVAR;
1407 cfg->ret->inst_c0 = ARMREG_R0;
1411 /* local vars are at a positive offset from the stack pointer */
1413 * also note that if the function uses alloca, we use FP
1414 * to point at the local variables.
1416 offset = 0; /* linkage area */
1417 /* align the offset to 16 bytes: not sure this is needed here */
1419 //offset &= ~(8 - 1);
1421 /* add parameter area size for called functions */
1422 offset += cfg->param_area;
1425 if (cfg->flags & MONO_CFG_HAS_FPOUT)
1428 /* allow room to save the return value */
1429 if (mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method))
1432 /* the MonoLMF structure is stored just below the stack pointer */
1433 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
1434 if (cinfo->ret.storage == RegTypeStructByVal) {
1435 cfg->ret->opcode = OP_REGOFFSET;
1436 cfg->ret->inst_basereg = cfg->frame_reg;
1437 offset += sizeof (gpointer) - 1;
1438 offset &= ~(sizeof (gpointer) - 1);
1439 cfg->ret->inst_offset = - offset;
1441 ins = cfg->vret_addr;
1442 offset += sizeof(gpointer) - 1;
1443 offset &= ~(sizeof(gpointer) - 1);
1444 ins->inst_offset = offset;
1445 ins->opcode = OP_REGOFFSET;
1446 ins->inst_basereg = cfg->frame_reg;
1447 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1448 printf ("vret_addr =");
1449 mono_print_ins (cfg->vret_addr);
1452 offset += sizeof(gpointer);
1455 /* Allocate these first so they have a small offset, OP_SEQ_POINT depends on this */
1456 if (cfg->arch.seq_point_info_var) {
1459 ins = cfg->arch.seq_point_info_var;
1463 offset += align - 1;
1464 offset &= ~(align - 1);
1465 ins->opcode = OP_REGOFFSET;
1466 ins->inst_basereg = cfg->frame_reg;
1467 ins->inst_offset = offset;
1470 ins = cfg->arch.ss_trigger_page_var;
1473 offset += align - 1;
1474 offset &= ~(align - 1);
1475 ins->opcode = OP_REGOFFSET;
1476 ins->inst_basereg = cfg->frame_reg;
1477 ins->inst_offset = offset;
1481 if (cfg->arch.seq_point_read_var) {
1484 ins = cfg->arch.seq_point_read_var;
1488 offset += align - 1;
1489 offset &= ~(align - 1);
1490 ins->opcode = OP_REGOFFSET;
1491 ins->inst_basereg = cfg->frame_reg;
1492 ins->inst_offset = offset;
1495 ins = cfg->arch.seq_point_ss_method_var;
1498 offset += align - 1;
1499 offset &= ~(align - 1);
1500 ins->opcode = OP_REGOFFSET;
1501 ins->inst_basereg = cfg->frame_reg;
1502 ins->inst_offset = offset;
1505 ins = cfg->arch.seq_point_bp_method_var;
1508 offset += align - 1;
1509 offset &= ~(align - 1);
1510 ins->opcode = OP_REGOFFSET;
1511 ins->inst_basereg = cfg->frame_reg;
1512 ins->inst_offset = offset;
1516 cfg->locals_min_stack_offset = offset;
1518 curinst = cfg->locals_start;
1519 for (i = curinst; i < cfg->num_varinfo; ++i) {
1520 ins = cfg->varinfo [i];
1521 if ((ins->flags & MONO_INST_IS_DEAD) || ins->opcode == OP_REGVAR || ins->opcode == OP_REGOFFSET)
1524 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
1525 * pinvoke wrappers when they call functions returning structure */
1526 if (ins->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (ins->inst_vtype) && ins->inst_vtype->type != MONO_TYPE_TYPEDBYREF) {
1527 size = mono_class_native_size (mono_class_from_mono_type (ins->inst_vtype), &ualign);
1531 size = mono_type_size (ins->inst_vtype, &align);
1533 /* FIXME: if a structure is misaligned, our memcpy doesn't work,
1534 * since it loads/stores misaligned words, which don't do the right thing.
1536 if (align < 4 && size >= 4)
1538 if (ALIGN_TO (offset, align) > ALIGN_TO (offset, 4))
1539 mini_gc_set_slot_type_from_fp (cfg, ALIGN_TO (offset, 4), SLOT_NOREF);
1540 offset += align - 1;
1541 offset &= ~(align - 1);
1542 ins->opcode = OP_REGOFFSET;
1543 ins->inst_offset = offset;
1544 ins->inst_basereg = cfg->frame_reg;
1546 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
1549 cfg->locals_max_stack_offset = offset;
1553 ins = cfg->args [curinst];
1554 if (ins->opcode != OP_REGVAR) {
1555 ins->opcode = OP_REGOFFSET;
1556 ins->inst_basereg = cfg->frame_reg;
1557 offset += sizeof (gpointer) - 1;
1558 offset &= ~(sizeof (gpointer) - 1);
1559 ins->inst_offset = offset;
1560 offset += sizeof (gpointer);
1565 if (sig->call_convention == MONO_CALL_VARARG) {
1569 /* Allocate a local slot to hold the sig cookie address */
1570 offset += align - 1;
1571 offset &= ~(align - 1);
1572 cfg->sig_cookie = offset;
1576 for (i = 0; i < sig->param_count; ++i) {
1577 ins = cfg->args [curinst];
1579 if (ins->opcode != OP_REGVAR) {
1580 ins->opcode = OP_REGOFFSET;
1581 ins->inst_basereg = cfg->frame_reg;
1582 size = mini_type_stack_size_full (NULL, sig->params [i], &ualign, sig->pinvoke);
1584 /* FIXME: if a structure is misaligned, our memcpy doesn't work,
1585 * since it loads/stores misaligned words, which don't do the right thing.
1587 if (align < 4 && size >= 4)
1589 /* The code in the prolog () stores words when storing vtypes received in a register */
1590 if (MONO_TYPE_ISSTRUCT (sig->params [i]))
1592 if (ALIGN_TO (offset, align) > ALIGN_TO (offset, 4))
1593 mini_gc_set_slot_type_from_fp (cfg, ALIGN_TO (offset, 4), SLOT_NOREF);
1594 offset += align - 1;
1595 offset &= ~(align - 1);
1596 ins->inst_offset = offset;
1602 /* align the offset to 8 bytes */
1603 if (ALIGN_TO (offset, 8) > ALIGN_TO (offset, 4))
1604 mini_gc_set_slot_type_from_fp (cfg, ALIGN_TO (offset, 4), SLOT_NOREF);
1609 cfg->stack_offset = offset;
1613 mono_arch_create_vars (MonoCompile *cfg)
1615 MonoMethodSignature *sig;
1618 sig = mono_method_signature (cfg->method);
1620 if (!cfg->arch.cinfo)
1621 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1622 cinfo = cfg->arch.cinfo;
1624 if (cinfo->ret.storage == RegTypeStructByVal)
1625 cfg->ret_var_is_local = TRUE;
1627 if (MONO_TYPE_ISSTRUCT (sig->ret) && cinfo->ret.storage != RegTypeStructByVal) {
1628 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
1629 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1630 printf ("vret_addr = ");
1631 mono_print_ins (cfg->vret_addr);
1635 if (cfg->gen_seq_points) {
1636 if (cfg->soft_breakpoints) {
1637 MonoInst *ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1638 ins->flags |= MONO_INST_VOLATILE;
1639 cfg->arch.seq_point_read_var = ins;
1641 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1642 ins->flags |= MONO_INST_VOLATILE;
1643 cfg->arch.seq_point_ss_method_var = ins;
1645 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1646 ins->flags |= MONO_INST_VOLATILE;
1647 cfg->arch.seq_point_bp_method_var = ins;
1649 g_assert (!cfg->compile_aot);
1650 } else if (cfg->compile_aot) {
1651 MonoInst *ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1652 ins->flags |= MONO_INST_VOLATILE;
1653 cfg->arch.seq_point_info_var = ins;
1655 /* Allocate a separate variable for this to save 1 load per seq point */
1656 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1657 ins->flags |= MONO_INST_VOLATILE;
1658 cfg->arch.ss_trigger_page_var = ins;
1664 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1666 MonoMethodSignature *tmp_sig;
1669 if (call->tail_call)
1672 g_assert (cinfo->sig_cookie.storage == RegTypeBase);
1675 * mono_ArgIterator_Setup assumes the signature cookie is
1676 * passed first and all the arguments which were before it are
1677 * passed on the stack after the signature. So compensate by
1678 * passing a different signature.
1680 tmp_sig = mono_metadata_signature_dup (call->signature);
1681 tmp_sig->param_count -= call->signature->sentinelpos;
1682 tmp_sig->sentinelpos = 0;
1683 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1685 sig_reg = mono_alloc_ireg (cfg);
1686 MONO_EMIT_NEW_SIGNATURECONST (cfg, sig_reg, tmp_sig);
1688 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, cinfo->sig_cookie.offset, sig_reg);
1693 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
1698 LLVMCallInfo *linfo;
1700 n = sig->param_count + sig->hasthis;
1702 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1704 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
1707 * LLVM always uses the native ABI while we use our own ABI, the
1708 * only difference is the handling of vtypes:
1709 * - we only pass/receive them in registers in some cases, and only
1710 * in 1 or 2 integer registers.
1712 if (cinfo->vtype_retaddr) {
1713 /* Vtype returned using a hidden argument */
1714 linfo->ret.storage = LLVMArgVtypeRetAddr;
1715 linfo->vret_arg_index = cinfo->vret_arg_index;
1716 } else if (cinfo->ret.storage != RegTypeGeneral && cinfo->ret.storage != RegTypeNone && cinfo->ret.storage != RegTypeFP && cinfo->ret.storage != RegTypeIRegPair) {
1717 cfg->exception_message = g_strdup ("unknown ret conv");
1718 cfg->disable_llvm = TRUE;
1722 for (i = 0; i < n; ++i) {
1723 ainfo = cinfo->args + i;
1725 linfo->args [i].storage = LLVMArgNone;
1727 switch (ainfo->storage) {
1728 case RegTypeGeneral:
1729 case RegTypeIRegPair:
1731 linfo->args [i].storage = LLVMArgInIReg;
1733 case RegTypeStructByVal:
1734 // FIXME: Passing entirely on the stack or split reg/stack
1735 if (ainfo->vtsize == 0 && ainfo->size <= 2) {
1736 linfo->args [i].storage = LLVMArgVtypeInReg;
1737 linfo->args [i].pair_storage [0] = LLVMArgInIReg;
1738 if (ainfo->size == 2)
1739 linfo->args [i].pair_storage [1] = LLVMArgInIReg;
1741 linfo->args [i].pair_storage [1] = LLVMArgNone;
1743 cfg->exception_message = g_strdup_printf ("vtype-by-val on stack");
1744 cfg->disable_llvm = TRUE;
1748 cfg->exception_message = g_strdup_printf ("ainfo->storage (%d)", ainfo->storage);
1749 cfg->disable_llvm = TRUE;
1759 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
1762 MonoMethodSignature *sig;
1766 sig = call->signature;
1767 n = sig->param_count + sig->hasthis;
1769 cinfo = get_call_info (cfg->generic_sharing_context, NULL, sig);
1771 for (i = 0; i < n; ++i) {
1772 ArgInfo *ainfo = cinfo->args + i;
1775 if (i >= sig->hasthis)
1776 t = sig->params [i - sig->hasthis];
1778 t = &mono_defaults.int_class->byval_arg;
1779 t = mini_type_get_underlying_type (NULL, t);
1781 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1782 /* Emit the signature cookie just before the implicit arguments */
1783 emit_sig_cookie (cfg, call, cinfo);
1786 in = call->args [i];
1788 switch (ainfo->storage) {
1789 case RegTypeGeneral:
1790 case RegTypeIRegPair:
1791 if (!t->byref && ((t->type == MONO_TYPE_I8) || (t->type == MONO_TYPE_U8))) {
1792 MONO_INST_NEW (cfg, ins, OP_MOVE);
1793 ins->dreg = mono_alloc_ireg (cfg);
1794 ins->sreg1 = in->dreg + 1;
1795 MONO_ADD_INS (cfg->cbb, ins);
1796 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1798 MONO_INST_NEW (cfg, ins, OP_MOVE);
1799 ins->dreg = mono_alloc_ireg (cfg);
1800 ins->sreg1 = in->dreg + 2;
1801 MONO_ADD_INS (cfg->cbb, ins);
1802 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg + 1, FALSE);
1803 } else if (!t->byref && ((t->type == MONO_TYPE_R8) || (t->type == MONO_TYPE_R4))) {
1804 if (ainfo->size == 4) {
1805 if (IS_SOFT_FLOAT) {
1806 /* mono_emit_call_args () have already done the r8->r4 conversion */
1807 /* The converted value is in an int vreg */
1808 MONO_INST_NEW (cfg, ins, OP_MOVE);
1809 ins->dreg = mono_alloc_ireg (cfg);
1810 ins->sreg1 = in->dreg;
1811 MONO_ADD_INS (cfg->cbb, ins);
1812 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1816 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, ARMREG_SP, (cfg->param_area - 8), in->dreg);
1817 creg = mono_alloc_ireg (cfg);
1818 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8));
1819 mono_call_inst_add_outarg_reg (cfg, call, creg, ainfo->reg, FALSE);
1822 if (IS_SOFT_FLOAT) {
1823 MONO_INST_NEW (cfg, ins, OP_FGETLOW32);
1824 ins->dreg = mono_alloc_ireg (cfg);
1825 ins->sreg1 = in->dreg;
1826 MONO_ADD_INS (cfg->cbb, ins);
1827 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1829 MONO_INST_NEW (cfg, ins, OP_FGETHIGH32);
1830 ins->dreg = mono_alloc_ireg (cfg);
1831 ins->sreg1 = in->dreg;
1832 MONO_ADD_INS (cfg->cbb, ins);
1833 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg + 1, FALSE);
1837 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, ARMREG_SP, (cfg->param_area - 8), in->dreg);
1838 creg = mono_alloc_ireg (cfg);
1839 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8));
1840 mono_call_inst_add_outarg_reg (cfg, call, creg, ainfo->reg, FALSE);
1841 creg = mono_alloc_ireg (cfg);
1842 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8 + 4));
1843 mono_call_inst_add_outarg_reg (cfg, call, creg, ainfo->reg + 1, FALSE);
1846 cfg->flags |= MONO_CFG_HAS_FPOUT;
1848 MONO_INST_NEW (cfg, ins, OP_MOVE);
1849 ins->dreg = mono_alloc_ireg (cfg);
1850 ins->sreg1 = in->dreg;
1851 MONO_ADD_INS (cfg->cbb, ins);
1853 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1856 case RegTypeStructByAddr:
1859 /* FIXME: where si the data allocated? */
1860 arg->backend.reg3 = ainfo->reg;
1861 call->used_iregs |= 1 << ainfo->reg;
1862 g_assert_not_reached ();
1865 case RegTypeStructByVal:
1866 MONO_INST_NEW (cfg, ins, OP_OUTARG_VT);
1867 ins->opcode = OP_OUTARG_VT;
1868 ins->sreg1 = in->dreg;
1869 ins->klass = in->klass;
1870 ins->inst_p0 = call;
1871 ins->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
1872 memcpy (ins->inst_p1, ainfo, sizeof (ArgInfo));
1873 mono_call_inst_add_outarg_vt (cfg, call, ins);
1874 MONO_ADD_INS (cfg->cbb, ins);
1877 if (!t->byref && ((t->type == MONO_TYPE_I8) || (t->type == MONO_TYPE_U8))) {
1878 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1879 } else if (!t->byref && ((t->type == MONO_TYPE_R4) || (t->type == MONO_TYPE_R8))) {
1880 if (t->type == MONO_TYPE_R8) {
1881 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1884 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1886 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1889 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1892 case RegTypeBaseGen:
1893 if (!t->byref && ((t->type == MONO_TYPE_I8) || (t->type == MONO_TYPE_U8))) {
1894 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);
1895 MONO_INST_NEW (cfg, ins, OP_MOVE);
1896 ins->dreg = mono_alloc_ireg (cfg);
1897 ins->sreg1 = G_BYTE_ORDER == G_BIG_ENDIAN ? in->dreg + 2 : in->dreg + 1;
1898 MONO_ADD_INS (cfg->cbb, ins);
1899 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ARMREG_R3, FALSE);
1900 } else if (!t->byref && (t->type == MONO_TYPE_R8)) {
1903 /* This should work for soft-float as well */
1905 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, ARMREG_SP, (cfg->param_area - 8), in->dreg);
1906 creg = mono_alloc_ireg (cfg);
1907 mono_call_inst_add_outarg_reg (cfg, call, creg, ARMREG_R3, FALSE);
1908 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8));
1909 creg = mono_alloc_ireg (cfg);
1910 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 4));
1911 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, ainfo->offset, creg);
1912 cfg->flags |= MONO_CFG_HAS_FPOUT;
1914 g_assert_not_reached ();
1921 arg->backend.reg3 = ainfo->reg;
1922 /* FP args are passed in int regs */
1923 call->used_iregs |= 1 << ainfo->reg;
1924 if (ainfo->size == 8) {
1925 arg->opcode = OP_OUTARG_R8;
1926 call->used_iregs |= 1 << (ainfo->reg + 1);
1928 arg->opcode = OP_OUTARG_R4;
1931 cfg->flags |= MONO_CFG_HAS_FPOUT;
1935 g_assert_not_reached ();
1939 /* Handle the case where there are no implicit arguments */
1940 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos))
1941 emit_sig_cookie (cfg, call, cinfo);
1943 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
1946 if (cinfo->ret.storage == RegTypeStructByVal) {
1947 /* The JIT will transform this into a normal call */
1948 call->vret_in_reg = TRUE;
1950 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
1951 vtarg->sreg1 = call->vret_var->dreg;
1952 vtarg->dreg = mono_alloc_preg (cfg);
1953 MONO_ADD_INS (cfg->cbb, vtarg);
1955 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
1959 call->stack_usage = cinfo->stack_usage;
1965 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
1967 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
1968 ArgInfo *ainfo = ins->inst_p1;
1969 int ovf_size = ainfo->vtsize;
1970 int doffset = ainfo->offset;
1971 int struct_size = ainfo->struct_size;
1972 int i, soffset, dreg, tmpreg;
1975 for (i = 0; i < ainfo->size; ++i) {
1976 dreg = mono_alloc_ireg (cfg);
1977 switch (struct_size) {
1979 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, dreg, src->dreg, soffset);
1982 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, dreg, src->dreg, soffset);
1985 tmpreg = mono_alloc_ireg (cfg);
1986 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, dreg, src->dreg, soffset);
1987 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, tmpreg, src->dreg, soffset + 1);
1988 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, tmpreg, tmpreg, 8);
1989 MONO_EMIT_NEW_BIALU (cfg, OP_IOR, dreg, dreg, tmpreg);
1990 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, tmpreg, src->dreg, soffset + 2);
1991 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, tmpreg, tmpreg, 16);
1992 MONO_EMIT_NEW_BIALU (cfg, OP_IOR, dreg, dreg, tmpreg);
1995 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, soffset);
1998 mono_call_inst_add_outarg_reg (cfg, call, dreg, ainfo->reg + i, FALSE);
1999 soffset += sizeof (gpointer);
2000 struct_size -= sizeof (gpointer);
2002 //g_print ("vt size: %d at R%d + %d\n", doffset, vt->inst_basereg, vt->inst_offset);
2004 mini_emit_memcpy (cfg, ARMREG_SP, doffset, src->dreg, soffset, MIN (ovf_size * sizeof (gpointer), struct_size), struct_size < 4 ? 1 : 4);
2008 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
2010 MonoType *ret = mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret);
2013 if (ret->type == MONO_TYPE_I8 || ret->type == MONO_TYPE_U8) {
2016 if (COMPILE_LLVM (cfg)) {
2017 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2019 MONO_INST_NEW (cfg, ins, OP_SETLRET);
2020 ins->sreg1 = val->dreg + 1;
2021 ins->sreg2 = val->dreg + 2;
2022 MONO_ADD_INS (cfg->cbb, ins);
2027 case MONO_ARM_FPU_NONE:
2028 if (ret->type == MONO_TYPE_R8) {
2031 MONO_INST_NEW (cfg, ins, OP_SETFRET);
2032 ins->dreg = cfg->ret->dreg;
2033 ins->sreg1 = val->dreg;
2034 MONO_ADD_INS (cfg->cbb, ins);
2037 if (ret->type == MONO_TYPE_R4) {
2038 /* Already converted to an int in method_to_ir () */
2039 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2043 case MONO_ARM_FPU_VFP:
2044 if (ret->type == MONO_TYPE_R8 || ret->type == MONO_TYPE_R4) {
2047 MONO_INST_NEW (cfg, ins, OP_SETFRET);
2048 ins->dreg = cfg->ret->dreg;
2049 ins->sreg1 = val->dreg;
2050 MONO_ADD_INS (cfg->cbb, ins);
2054 case MONO_ARM_FPU_FPA:
2055 if (ret->type == MONO_TYPE_R4 || ret->type == MONO_TYPE_R8) {
2056 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2061 g_assert_not_reached ();
2065 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2068 #endif /* #ifndef DISABLE_JIT */
2071 mono_arch_is_inst_imm (gint64 imm)
2076 #define DYN_CALL_STACK_ARGS 6
2079 MonoMethodSignature *sig;
2084 mgreg_t regs [PARAM_REGS + DYN_CALL_STACK_ARGS];
2090 dyn_call_supported (CallInfo *cinfo, MonoMethodSignature *sig)
2094 if (sig->hasthis + sig->param_count > PARAM_REGS + DYN_CALL_STACK_ARGS)
2097 switch (cinfo->ret.storage) {
2099 case RegTypeGeneral:
2100 case RegTypeIRegPair:
2101 case RegTypeStructByAddr:
2114 for (i = 0; i < cinfo->nargs; ++i) {
2115 switch (cinfo->args [i].storage) {
2116 case RegTypeGeneral:
2118 case RegTypeIRegPair:
2121 if (cinfo->args [i].offset >= (DYN_CALL_STACK_ARGS * sizeof (gpointer)))
2124 case RegTypeStructByVal:
2125 if (cinfo->args [i].reg + cinfo->args [i].vtsize >= PARAM_REGS + DYN_CALL_STACK_ARGS)
2133 // FIXME: Can't use cinfo only as it doesn't contain info about I8/float */
2134 for (i = 0; i < sig->param_count; ++i) {
2135 MonoType *t = sig->params [i];
2161 mono_arch_dyn_call_prepare (MonoMethodSignature *sig)
2163 ArchDynCallInfo *info;
2166 cinfo = get_call_info (NULL, NULL, sig);
2168 if (!dyn_call_supported (cinfo, sig)) {
2173 info = g_new0 (ArchDynCallInfo, 1);
2174 // FIXME: Preprocess the info to speed up start_dyn_call ()
2176 info->cinfo = cinfo;
2178 return (MonoDynCallInfo*)info;
2182 mono_arch_dyn_call_free (MonoDynCallInfo *info)
2184 ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
2186 g_free (ainfo->cinfo);
2191 mono_arch_start_dyn_call (MonoDynCallInfo *info, gpointer **args, guint8 *ret, guint8 *buf, int buf_len)
2193 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2194 DynCallArgs *p = (DynCallArgs*)buf;
2195 int arg_index, greg, i, j, pindex;
2196 MonoMethodSignature *sig = dinfo->sig;
2198 g_assert (buf_len >= sizeof (DynCallArgs));
2207 if (sig->hasthis || dinfo->cinfo->vret_arg_index == 1) {
2208 p->regs [greg ++] = (mgreg_t)*(args [arg_index ++]);
2213 if (dinfo->cinfo->vtype_retaddr)
2214 p->regs [greg ++] = (mgreg_t)ret;
2216 for (i = pindex; i < sig->param_count; i++) {
2217 MonoType *t = mono_type_get_underlying_type (sig->params [i]);
2218 gpointer *arg = args [arg_index ++];
2219 ArgInfo *ainfo = &dinfo->cinfo->args [i + sig->hasthis];
2222 if (ainfo->storage == RegTypeGeneral || ainfo->storage == RegTypeIRegPair || ainfo->storage == RegTypeStructByVal)
2224 else if (ainfo->storage == RegTypeBase)
2225 slot = PARAM_REGS + (ainfo->offset / 4);
2227 g_assert_not_reached ();
2230 p->regs [slot] = (mgreg_t)*arg;
2235 case MONO_TYPE_STRING:
2236 case MONO_TYPE_CLASS:
2237 case MONO_TYPE_ARRAY:
2238 case MONO_TYPE_SZARRAY:
2239 case MONO_TYPE_OBJECT:
2243 p->regs [slot] = (mgreg_t)*arg;
2245 case MONO_TYPE_BOOLEAN:
2247 p->regs [slot] = *(guint8*)arg;
2250 p->regs [slot] = *(gint8*)arg;
2253 p->regs [slot] = *(gint16*)arg;
2256 case MONO_TYPE_CHAR:
2257 p->regs [slot] = *(guint16*)arg;
2260 p->regs [slot] = *(gint32*)arg;
2263 p->regs [slot] = *(guint32*)arg;
2267 p->regs [slot ++] = (mgreg_t)arg [0];
2268 p->regs [slot] = (mgreg_t)arg [1];
2271 p->regs [slot] = *(mgreg_t*)arg;
2274 p->regs [slot ++] = (mgreg_t)arg [0];
2275 p->regs [slot] = (mgreg_t)arg [1];
2277 case MONO_TYPE_GENERICINST:
2278 if (MONO_TYPE_IS_REFERENCE (t)) {
2279 p->regs [slot] = (mgreg_t)*arg;
2284 case MONO_TYPE_VALUETYPE:
2285 g_assert (ainfo->storage == RegTypeStructByVal);
2287 if (ainfo->size == 0)
2288 slot = PARAM_REGS + (ainfo->offset / 4);
2292 for (j = 0; j < ainfo->size + ainfo->vtsize; ++j)
2293 p->regs [slot ++] = ((mgreg_t*)arg) [j];
2296 g_assert_not_reached ();
2302 mono_arch_finish_dyn_call (MonoDynCallInfo *info, guint8 *buf)
2304 ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
2305 MonoMethodSignature *sig = ((ArchDynCallInfo*)info)->sig;
2306 guint8 *ret = ((DynCallArgs*)buf)->ret;
2307 mgreg_t res = ((DynCallArgs*)buf)->res;
2308 mgreg_t res2 = ((DynCallArgs*)buf)->res2;
2310 switch (mono_type_get_underlying_type (sig->ret)->type) {
2311 case MONO_TYPE_VOID:
2312 *(gpointer*)ret = NULL;
2314 case MONO_TYPE_STRING:
2315 case MONO_TYPE_CLASS:
2316 case MONO_TYPE_ARRAY:
2317 case MONO_TYPE_SZARRAY:
2318 case MONO_TYPE_OBJECT:
2322 *(gpointer*)ret = (gpointer)res;
2328 case MONO_TYPE_BOOLEAN:
2329 *(guint8*)ret = res;
2332 *(gint16*)ret = res;
2335 case MONO_TYPE_CHAR:
2336 *(guint16*)ret = res;
2339 *(gint32*)ret = res;
2342 *(guint32*)ret = res;
2346 /* This handles endianness as well */
2347 ((gint32*)ret) [0] = res;
2348 ((gint32*)ret) [1] = res2;
2350 case MONO_TYPE_GENERICINST:
2351 if (MONO_TYPE_IS_REFERENCE (sig->ret)) {
2352 *(gpointer*)ret = (gpointer)res;
2357 case MONO_TYPE_VALUETYPE:
2358 g_assert (ainfo->cinfo->vtype_retaddr);
2363 *(float*)ret = *(float*)&res;
2365 case MONO_TYPE_R8: {
2372 *(double*)ret = *(double*)®s;
2376 g_assert_not_reached ();
2383 * Allow tracing to work with this interface (with an optional argument)
2387 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
2391 code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->method);
2392 ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0); /* NULL ebp for now */
2393 code = mono_arm_emit_load_imm (code, ARMREG_R2, (guint32)func);
2394 code = emit_call_reg (code, ARMREG_R2);
2407 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
2410 int save_mode = SAVE_NONE;
2412 MonoMethod *method = cfg->method;
2413 int rtype = mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret)->type;
2414 int save_offset = cfg->param_area;
2418 offset = code - cfg->native_code;
2419 /* we need about 16 instructions */
2420 if (offset > (cfg->code_size - 16 * 4)) {
2421 cfg->code_size *= 2;
2422 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2423 code = cfg->native_code + offset;
2426 case MONO_TYPE_VOID:
2427 /* special case string .ctor icall */
2428 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
2429 save_mode = SAVE_ONE;
2431 save_mode = SAVE_NONE;
2435 save_mode = SAVE_TWO;
2439 save_mode = SAVE_FP;
2441 case MONO_TYPE_VALUETYPE:
2442 save_mode = SAVE_STRUCT;
2445 save_mode = SAVE_ONE;
2449 switch (save_mode) {
2451 ARM_STR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2452 ARM_STR_IMM (code, ARMREG_R1, cfg->frame_reg, save_offset + 4);
2453 if (enable_arguments) {
2454 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_R1);
2455 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
2459 ARM_STR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2460 if (enable_arguments) {
2461 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
2465 /* FIXME: what reg? */
2466 if (enable_arguments) {
2467 /* FIXME: what reg? */
2471 if (enable_arguments) {
2472 /* FIXME: get the actual address */
2473 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
2481 code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->method);
2482 code = mono_arm_emit_load_imm (code, ARMREG_IP, (guint32)func);
2483 code = emit_call_reg (code, ARMREG_IP);
2485 switch (save_mode) {
2487 ARM_LDR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2488 ARM_LDR_IMM (code, ARMREG_R1, cfg->frame_reg, save_offset + 4);
2491 ARM_LDR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2505 * The immediate field for cond branches is big enough for all reasonable methods
2507 #define EMIT_COND_BRANCH_FLAGS(ins,condcode) \
2508 if (0 && ins->inst_true_bb->native_offset) { \
2509 ARM_B_COND (code, (condcode), (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffffff); \
2511 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
2512 ARM_B_COND (code, (condcode), 0); \
2515 #define EMIT_COND_BRANCH(ins,cond) EMIT_COND_BRANCH_FLAGS(ins, branch_cc_table [(cond)])
2517 /* emit an exception if condition is fail
2519 * We assign the extra code used to throw the implicit exceptions
2520 * to cfg->bb_exit as far as the big branch handling is concerned
2522 #define EMIT_COND_SYSTEM_EXCEPTION_FLAGS(condcode,exc_name) \
2524 mono_add_patch_info (cfg, code - cfg->native_code, \
2525 MONO_PATCH_INFO_EXC, exc_name); \
2526 ARM_BL_COND (code, (condcode), 0); \
2529 #define EMIT_COND_SYSTEM_EXCEPTION(cond,exc_name) EMIT_COND_SYSTEM_EXCEPTION_FLAGS(branch_cc_table [(cond)], (exc_name))
2532 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
2537 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
2539 MonoInst *ins, *n, *last_ins = NULL;
2541 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2542 switch (ins->opcode) {
2545 /* Already done by an arch-independent pass */
2547 case OP_LOAD_MEMBASE:
2548 case OP_LOADI4_MEMBASE:
2550 * OP_STORE_MEMBASE_REG reg, offset(basereg)
2551 * OP_LOAD_MEMBASE offset(basereg), reg
2553 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
2554 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
2555 ins->inst_basereg == last_ins->inst_destbasereg &&
2556 ins->inst_offset == last_ins->inst_offset) {
2557 if (ins->dreg == last_ins->sreg1) {
2558 MONO_DELETE_INS (bb, ins);
2561 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
2562 ins->opcode = OP_MOVE;
2563 ins->sreg1 = last_ins->sreg1;
2567 * Note: reg1 must be different from the basereg in the second load
2568 * OP_LOAD_MEMBASE offset(basereg), reg1
2569 * OP_LOAD_MEMBASE offset(basereg), reg2
2571 * OP_LOAD_MEMBASE offset(basereg), reg1
2572 * OP_MOVE reg1, reg2
2574 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
2575 || last_ins->opcode == OP_LOAD_MEMBASE) &&
2576 ins->inst_basereg != last_ins->dreg &&
2577 ins->inst_basereg == last_ins->inst_basereg &&
2578 ins->inst_offset == last_ins->inst_offset) {
2580 if (ins->dreg == last_ins->dreg) {
2581 MONO_DELETE_INS (bb, ins);
2584 ins->opcode = OP_MOVE;
2585 ins->sreg1 = last_ins->dreg;
2588 //g_assert_not_reached ();
2592 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2593 * OP_LOAD_MEMBASE offset(basereg), reg
2595 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2596 * OP_ICONST reg, imm
2598 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
2599 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
2600 ins->inst_basereg == last_ins->inst_destbasereg &&
2601 ins->inst_offset == last_ins->inst_offset) {
2602 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
2603 ins->opcode = OP_ICONST;
2604 ins->inst_c0 = last_ins->inst_imm;
2605 g_assert_not_reached (); // check this rule
2609 case OP_LOADU1_MEMBASE:
2610 case OP_LOADI1_MEMBASE:
2611 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
2612 ins->inst_basereg == last_ins->inst_destbasereg &&
2613 ins->inst_offset == last_ins->inst_offset) {
2614 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? OP_ICONV_TO_I1 : OP_ICONV_TO_U1;
2615 ins->sreg1 = last_ins->sreg1;
2618 case OP_LOADU2_MEMBASE:
2619 case OP_LOADI2_MEMBASE:
2620 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
2621 ins->inst_basereg == last_ins->inst_destbasereg &&
2622 ins->inst_offset == last_ins->inst_offset) {
2623 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_ICONV_TO_I2 : OP_ICONV_TO_U2;
2624 ins->sreg1 = last_ins->sreg1;
2628 ins->opcode = OP_MOVE;
2632 if (ins->dreg == ins->sreg1) {
2633 MONO_DELETE_INS (bb, ins);
2637 * OP_MOVE sreg, dreg
2638 * OP_MOVE dreg, sreg
2640 if (last_ins && last_ins->opcode == OP_MOVE &&
2641 ins->sreg1 == last_ins->dreg &&
2642 ins->dreg == last_ins->sreg1) {
2643 MONO_DELETE_INS (bb, ins);
2651 bb->last_ins = last_ins;
2655 * the branch_cc_table should maintain the order of these
2669 branch_cc_table [] = {
2683 #define NEW_INS(cfg,dest,op) do { \
2684 MONO_INST_NEW ((cfg), (dest), (op)); \
2685 mono_bblock_insert_before_ins (bb, ins, (dest)); \
2689 map_to_reg_reg_op (int op)
2698 case OP_COMPARE_IMM:
2700 case OP_ICOMPARE_IMM:
2714 case OP_LOAD_MEMBASE:
2715 return OP_LOAD_MEMINDEX;
2716 case OP_LOADI4_MEMBASE:
2717 return OP_LOADI4_MEMINDEX;
2718 case OP_LOADU4_MEMBASE:
2719 return OP_LOADU4_MEMINDEX;
2720 case OP_LOADU1_MEMBASE:
2721 return OP_LOADU1_MEMINDEX;
2722 case OP_LOADI2_MEMBASE:
2723 return OP_LOADI2_MEMINDEX;
2724 case OP_LOADU2_MEMBASE:
2725 return OP_LOADU2_MEMINDEX;
2726 case OP_LOADI1_MEMBASE:
2727 return OP_LOADI1_MEMINDEX;
2728 case OP_STOREI1_MEMBASE_REG:
2729 return OP_STOREI1_MEMINDEX;
2730 case OP_STOREI2_MEMBASE_REG:
2731 return OP_STOREI2_MEMINDEX;
2732 case OP_STOREI4_MEMBASE_REG:
2733 return OP_STOREI4_MEMINDEX;
2734 case OP_STORE_MEMBASE_REG:
2735 return OP_STORE_MEMINDEX;
2736 case OP_STORER4_MEMBASE_REG:
2737 return OP_STORER4_MEMINDEX;
2738 case OP_STORER8_MEMBASE_REG:
2739 return OP_STORER8_MEMINDEX;
2740 case OP_STORE_MEMBASE_IMM:
2741 return OP_STORE_MEMBASE_REG;
2742 case OP_STOREI1_MEMBASE_IMM:
2743 return OP_STOREI1_MEMBASE_REG;
2744 case OP_STOREI2_MEMBASE_IMM:
2745 return OP_STOREI2_MEMBASE_REG;
2746 case OP_STOREI4_MEMBASE_IMM:
2747 return OP_STOREI4_MEMBASE_REG;
2749 g_assert_not_reached ();
2753 * Remove from the instruction list the instructions that can't be
2754 * represented with very simple instructions with no register
2758 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
2760 MonoInst *ins, *temp, *last_ins = NULL;
2761 int rot_amount, imm8, low_imm;
2763 MONO_BB_FOR_EACH_INS (bb, ins) {
2765 switch (ins->opcode) {
2769 case OP_COMPARE_IMM:
2770 case OP_ICOMPARE_IMM:
2784 if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount)) < 0) {
2785 NEW_INS (cfg, temp, OP_ICONST);
2786 temp->inst_c0 = ins->inst_imm;
2787 temp->dreg = mono_alloc_ireg (cfg);
2788 ins->sreg2 = temp->dreg;
2789 ins->opcode = mono_op_imm_to_op (ins->opcode);
2791 if (ins->opcode == OP_SBB || ins->opcode == OP_ISBB || ins->opcode == OP_SUBCC)
2797 if (ins->inst_imm == 1) {
2798 ins->opcode = OP_MOVE;
2801 if (ins->inst_imm == 0) {
2802 ins->opcode = OP_ICONST;
2806 imm8 = mono_is_power_of_two (ins->inst_imm);
2808 ins->opcode = OP_SHL_IMM;
2809 ins->inst_imm = imm8;
2812 NEW_INS (cfg, temp, OP_ICONST);
2813 temp->inst_c0 = ins->inst_imm;
2814 temp->dreg = mono_alloc_ireg (cfg);
2815 ins->sreg2 = temp->dreg;
2816 ins->opcode = OP_IMUL;
2822 if (ins->next && (ins->next->opcode == OP_COND_EXC_C || ins->next->opcode == OP_COND_EXC_IC))
2823 /* ARM sets the C flag to 1 if there was _no_ overflow */
2824 ins->next->opcode = OP_COND_EXC_NC;
2826 case OP_LOCALLOC_IMM:
2827 NEW_INS (cfg, temp, OP_ICONST);
2828 temp->inst_c0 = ins->inst_imm;
2829 temp->dreg = mono_alloc_ireg (cfg);
2830 ins->sreg1 = temp->dreg;
2831 ins->opcode = OP_LOCALLOC;
2833 case OP_LOAD_MEMBASE:
2834 case OP_LOADI4_MEMBASE:
2835 case OP_LOADU4_MEMBASE:
2836 case OP_LOADU1_MEMBASE:
2837 /* we can do two things: load the immed in a register
2838 * and use an indexed load, or see if the immed can be
2839 * represented as an ad_imm + a load with a smaller offset
2840 * that fits. We just do the first for now, optimize later.
2842 if (arm_is_imm12 (ins->inst_offset))
2844 NEW_INS (cfg, temp, OP_ICONST);
2845 temp->inst_c0 = ins->inst_offset;
2846 temp->dreg = mono_alloc_ireg (cfg);
2847 ins->sreg2 = temp->dreg;
2848 ins->opcode = map_to_reg_reg_op (ins->opcode);
2850 case OP_LOADI2_MEMBASE:
2851 case OP_LOADU2_MEMBASE:
2852 case OP_LOADI1_MEMBASE:
2853 if (arm_is_imm8 (ins->inst_offset))
2855 NEW_INS (cfg, temp, OP_ICONST);
2856 temp->inst_c0 = ins->inst_offset;
2857 temp->dreg = mono_alloc_ireg (cfg);
2858 ins->sreg2 = temp->dreg;
2859 ins->opcode = map_to_reg_reg_op (ins->opcode);
2861 case OP_LOADR4_MEMBASE:
2862 case OP_LOADR8_MEMBASE:
2863 if (arm_is_fpimm8 (ins->inst_offset))
2865 low_imm = ins->inst_offset & 0x1ff;
2866 if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_offset & ~0x1ff, &rot_amount)) >= 0) {
2867 NEW_INS (cfg, temp, OP_ADD_IMM);
2868 temp->inst_imm = ins->inst_offset & ~0x1ff;
2869 temp->sreg1 = ins->inst_basereg;
2870 temp->dreg = mono_alloc_ireg (cfg);
2871 ins->inst_basereg = temp->dreg;
2872 ins->inst_offset = low_imm;
2875 /* VFP/FPA doesn't have indexed load instructions */
2876 g_assert_not_reached ();
2878 case OP_STORE_MEMBASE_REG:
2879 case OP_STOREI4_MEMBASE_REG:
2880 case OP_STOREI1_MEMBASE_REG:
2881 if (arm_is_imm12 (ins->inst_offset))
2883 NEW_INS (cfg, temp, OP_ICONST);
2884 temp->inst_c0 = ins->inst_offset;
2885 temp->dreg = mono_alloc_ireg (cfg);
2886 ins->sreg2 = temp->dreg;
2887 ins->opcode = map_to_reg_reg_op (ins->opcode);
2889 case OP_STOREI2_MEMBASE_REG:
2890 if (arm_is_imm8 (ins->inst_offset))
2892 NEW_INS (cfg, temp, OP_ICONST);
2893 temp->inst_c0 = ins->inst_offset;
2894 temp->dreg = mono_alloc_ireg (cfg);
2895 ins->sreg2 = temp->dreg;
2896 ins->opcode = map_to_reg_reg_op (ins->opcode);
2898 case OP_STORER4_MEMBASE_REG:
2899 case OP_STORER8_MEMBASE_REG:
2900 if (arm_is_fpimm8 (ins->inst_offset))
2902 low_imm = ins->inst_offset & 0x1ff;
2903 if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_offset & ~ 0x1ff, &rot_amount)) >= 0 && arm_is_fpimm8 (low_imm)) {
2904 NEW_INS (cfg, temp, OP_ADD_IMM);
2905 temp->inst_imm = ins->inst_offset & ~0x1ff;
2906 temp->sreg1 = ins->inst_destbasereg;
2907 temp->dreg = mono_alloc_ireg (cfg);
2908 ins->inst_destbasereg = temp->dreg;
2909 ins->inst_offset = low_imm;
2912 /*g_print ("fail with: %d (%d, %d)\n", ins->inst_offset, ins->inst_offset & ~0x1ff, low_imm);*/
2913 /* VFP/FPA doesn't have indexed store instructions */
2914 g_assert_not_reached ();
2916 case OP_STORE_MEMBASE_IMM:
2917 case OP_STOREI1_MEMBASE_IMM:
2918 case OP_STOREI2_MEMBASE_IMM:
2919 case OP_STOREI4_MEMBASE_IMM:
2920 NEW_INS (cfg, temp, OP_ICONST);
2921 temp->inst_c0 = ins->inst_imm;
2922 temp->dreg = mono_alloc_ireg (cfg);
2923 ins->sreg1 = temp->dreg;
2924 ins->opcode = map_to_reg_reg_op (ins->opcode);
2926 goto loop_start; /* make it handle the possibly big ins->inst_offset */
2928 gboolean swap = FALSE;
2932 /* Optimized away */
2937 /* Some fp compares require swapped operands */
2938 switch (ins->next->opcode) {
2940 ins->next->opcode = OP_FBLT;
2944 ins->next->opcode = OP_FBLT_UN;
2948 ins->next->opcode = OP_FBGE;
2952 ins->next->opcode = OP_FBGE_UN;
2960 ins->sreg1 = ins->sreg2;
2969 bb->last_ins = last_ins;
2970 bb->max_vreg = cfg->next_vreg;
2974 mono_arch_decompose_long_opts (MonoCompile *cfg, MonoInst *long_ins)
2978 if (long_ins->opcode == OP_LNEG) {
2980 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ARM_RSBS_IMM, ins->dreg + 1, ins->sreg1 + 1, 0);
2981 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ARM_RSC_IMM, ins->dreg + 2, ins->sreg1 + 2, 0);
2987 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
2989 /* sreg is a float, dreg is an integer reg */
2991 ARM_FPA_FIXZ (code, dreg, sreg);
2994 ARM_TOSIZD (code, ARM_VFP_F0, sreg);
2996 ARM_TOUIZD (code, ARM_VFP_F0, sreg);
2997 ARM_FMRS (code, dreg, ARM_VFP_F0);
3001 ARM_AND_REG_IMM8 (code, dreg, dreg, 0xff);
3002 else if (size == 2) {
3003 ARM_SHL_IMM (code, dreg, dreg, 16);
3004 ARM_SHR_IMM (code, dreg, dreg, 16);
3008 ARM_SHL_IMM (code, dreg, dreg, 24);
3009 ARM_SAR_IMM (code, dreg, dreg, 24);
3010 } else if (size == 2) {
3011 ARM_SHL_IMM (code, dreg, dreg, 16);
3012 ARM_SAR_IMM (code, dreg, dreg, 16);
3018 #endif /* #ifndef DISABLE_JIT */
3022 const guchar *target;
3027 #define is_call_imm(diff) ((gint)(diff) >= -33554432 && (gint)(diff) <= 33554431)
3030 search_thunk_slot (void *data, int csize, int bsize, void *user_data) {
3031 PatchData *pdata = (PatchData*)user_data;
3032 guchar *code = data;
3033 guint32 *thunks = data;
3034 guint32 *endthunks = (guint32*)(code + bsize);
3036 int difflow, diffhigh;
3038 /* always ensure a call from pdata->code can reach to the thunks without further thunks */
3039 difflow = (char*)pdata->code - (char*)thunks;
3040 diffhigh = (char*)pdata->code - (char*)endthunks;
3041 if (!((is_call_imm (thunks) && is_call_imm (endthunks)) || (is_call_imm (difflow) && is_call_imm (diffhigh))))
3045 * The thunk is composed of 3 words:
3046 * load constant from thunks [2] into ARM_IP
3049 * Note that the LR register is already setup
3051 //g_print ("thunk nentries: %d\n", ((char*)endthunks - (char*)thunks)/16);
3052 if ((pdata->found == 2) || (pdata->code >= code && pdata->code <= code + csize)) {
3053 while (thunks < endthunks) {
3054 //g_print ("looking for target: %p at %p (%08x-%08x)\n", pdata->target, thunks, thunks [0], thunks [1]);
3055 if (thunks [2] == (guint32)pdata->target) {
3056 arm_patch (pdata->code, (guchar*)thunks);
3057 mono_arch_flush_icache (pdata->code, 4);
3060 } else if ((thunks [0] == 0) && (thunks [1] == 0) && (thunks [2] == 0)) {
3061 /* found a free slot instead: emit thunk */
3062 /* ARMREG_IP is fine to use since this can't be an IMT call
3065 code = (guchar*)thunks;
3066 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
3067 if (thumb_supported)
3068 ARM_BX (code, ARMREG_IP);
3070 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
3071 thunks [2] = (guint32)pdata->target;
3072 mono_arch_flush_icache ((guchar*)thunks, 12);
3074 arm_patch (pdata->code, (guchar*)thunks);
3075 mono_arch_flush_icache (pdata->code, 4);
3079 /* skip 12 bytes, the size of the thunk */
3083 //g_print ("failed thunk lookup for %p from %p at %p (%d entries)\n", pdata->target, pdata->code, data, count);
3089 handle_thunk (MonoDomain *domain, int absolute, guchar *code, const guchar *target, MonoCodeManager *dyn_code_mp)
3094 domain = mono_domain_get ();
3097 pdata.target = target;
3098 pdata.absolute = absolute;
3102 mono_code_manager_foreach (dyn_code_mp, search_thunk_slot, &pdata);
3105 if (pdata.found != 1) {
3106 mono_domain_lock (domain);
3107 mono_domain_code_foreach (domain, search_thunk_slot, &pdata);
3110 /* this uses the first available slot */
3112 mono_domain_code_foreach (domain, search_thunk_slot, &pdata);
3114 mono_domain_unlock (domain);
3117 if (pdata.found != 1) {
3119 GHashTableIter iter;
3120 MonoJitDynamicMethodInfo *ji;
3123 * This might be a dynamic method, search its code manager. We can only
3124 * use the dynamic method containing CODE, since the others might be freed later.
3128 mono_domain_lock (domain);
3129 hash = domain_jit_info (domain)->dynamic_code_hash;
3131 /* FIXME: Speed this up */
3132 g_hash_table_iter_init (&iter, hash);
3133 while (g_hash_table_iter_next (&iter, NULL, (gpointer*)&ji)) {
3134 mono_code_manager_foreach (ji->code_mp, search_thunk_slot, &pdata);
3135 if (pdata.found == 1)
3139 mono_domain_unlock (domain);
3141 if (pdata.found != 1)
3142 g_print ("thunk failed for %p from %p\n", target, code);
3143 g_assert (pdata.found == 1);
3147 arm_patch_general (MonoDomain *domain, guchar *code, const guchar *target, MonoCodeManager *dyn_code_mp)
3149 guint32 *code32 = (void*)code;
3150 guint32 ins = *code32;
3151 guint32 prim = (ins >> 25) & 7;
3152 guint32 tval = GPOINTER_TO_UINT (target);
3154 //g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
3155 if (prim == 5) { /* 101b */
3156 /* the diff starts 8 bytes from the branch opcode */
3157 gint diff = target - code - 8;
3159 gint tmask = 0xffffffff;
3160 if (tval & 1) { /* entering thumb mode */
3161 diff = target - 1 - code - 8;
3162 g_assert (thumb_supported);
3163 tbits = 0xf << 28; /* bl->blx bit pattern */
3164 g_assert ((ins & (1 << 24))); /* it must be a bl, not b instruction */
3165 /* this low bit of the displacement is moved to bit 24 in the instruction encoding */
3169 tmask = ~(1 << 24); /* clear the link bit */
3170 /*g_print ("blx to thumb: target: %p, code: %p, diff: %d, mask: %x\n", target, code, diff, tmask);*/
3175 if (diff <= 33554431) {
3177 ins = (ins & 0xff000000) | diff;
3179 *code32 = ins | tbits;
3183 /* diff between 0 and -33554432 */
3184 if (diff >= -33554432) {
3186 ins = (ins & 0xff000000) | (diff & ~0xff000000);
3188 *code32 = ins | tbits;
3193 handle_thunk (domain, TRUE, code, target, dyn_code_mp);
3198 * The alternative call sequences looks like this:
3200 * ldr ip, [pc] // loads the address constant
3201 * b 1f // jumps around the constant
3202 * address constant embedded in the code
3207 * There are two cases for patching:
3208 * a) at the end of method emission: in this case code points to the start
3209 * of the call sequence
3210 * b) during runtime patching of the call site: in this case code points
3211 * to the mov pc, ip instruction
3213 * We have to handle also the thunk jump code sequence:
3217 * address constant // execution never reaches here
3219 if ((ins & 0x0ffffff0) == 0x12fff10) {
3220 /* Branch and exchange: the address is constructed in a reg
3221 * We can patch BX when the code sequence is the following:
3222 * ldr ip, [pc, #0] ; 0x8
3229 guint8 *emit = (guint8*)ccode;
3230 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
3232 ARM_MOV_REG_REG (emit, ARMREG_LR, ARMREG_PC);
3233 ARM_BX (emit, ARMREG_IP);
3235 /*patching from magic trampoline*/
3236 if (ins == ccode [3]) {
3237 g_assert (code32 [-4] == ccode [0]);
3238 g_assert (code32 [-3] == ccode [1]);
3239 g_assert (code32 [-1] == ccode [2]);
3240 code32 [-2] = (guint32)target;
3243 /*patching from JIT*/
3244 if (ins == ccode [0]) {
3245 g_assert (code32 [1] == ccode [1]);
3246 g_assert (code32 [3] == ccode [2]);
3247 g_assert (code32 [4] == ccode [3]);
3248 code32 [2] = (guint32)target;
3251 g_assert_not_reached ();
3252 } else if ((ins & 0x0ffffff0) == 0x12fff30) {
3260 guint8 *emit = (guint8*)ccode;
3261 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
3263 ARM_BLX_REG (emit, ARMREG_IP);
3265 g_assert (code32 [-3] == ccode [0]);
3266 g_assert (code32 [-2] == ccode [1]);
3267 g_assert (code32 [0] == ccode [2]);
3269 code32 [-1] = (guint32)target;
3272 guint32 *tmp = ccode;
3273 guint8 *emit = (guint8*)tmp;
3274 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
3275 ARM_MOV_REG_REG (emit, ARMREG_LR, ARMREG_PC);
3276 ARM_MOV_REG_REG (emit, ARMREG_PC, ARMREG_IP);
3277 ARM_BX (emit, ARMREG_IP);
3278 if (ins == ccode [2]) {
3279 g_assert_not_reached (); // should be -2 ...
3280 code32 [-1] = (guint32)target;
3283 if (ins == ccode [0]) {
3284 /* handles both thunk jump code and the far call sequence */
3285 code32 [2] = (guint32)target;
3288 g_assert_not_reached ();
3290 // g_print ("patched with 0x%08x\n", ins);
3294 arm_patch (guchar *code, const guchar *target)
3296 arm_patch_general (NULL, code, target, NULL);
3300 * Return the >= 0 uimm8 value if val can be represented with a byte + rotation
3301 * (with the rotation amount in *rot_amount. rot_amount is already adjusted
3302 * to be used with the emit macros.
3303 * Return -1 otherwise.
3306 mono_arm_is_rotated_imm8 (guint32 val, gint *rot_amount)
3309 for (i = 0; i < 31; i+= 2) {
3310 res = (val << (32 - i)) | (val >> i);
3313 *rot_amount = i? 32 - i: 0;
3320 * Emits in code a sequence of instructions that load the value 'val'
3321 * into the dreg register. Uses at most 4 instructions.
3324 mono_arm_emit_load_imm (guint8 *code, int dreg, guint32 val)
3326 int imm8, rot_amount;
3328 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
3329 /* skip the constant pool */
3335 if ((imm8 = mono_arm_is_rotated_imm8 (val, &rot_amount)) >= 0) {
3336 ARM_MOV_REG_IMM (code, dreg, imm8, rot_amount);
3337 } else if ((imm8 = mono_arm_is_rotated_imm8 (~val, &rot_amount)) >= 0) {
3338 ARM_MVN_REG_IMM (code, dreg, imm8, rot_amount);
3341 ARM_MOVW_REG_IMM (code, dreg, val & 0xffff);
3343 ARM_MOVT_REG_IMM (code, dreg, (val >> 16) & 0xffff);
3347 ARM_MOV_REG_IMM8 (code, dreg, (val & 0xFF));
3349 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF00) >> 8, 24);
3351 if (val & 0xFF0000) {
3352 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
3354 if (val & 0xFF000000) {
3355 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
3357 } else if (val & 0xFF00) {
3358 ARM_MOV_REG_IMM (code, dreg, (val & 0xFF00) >> 8, 24);
3359 if (val & 0xFF0000) {
3360 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
3362 if (val & 0xFF000000) {
3363 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
3365 } else if (val & 0xFF0000) {
3366 ARM_MOV_REG_IMM (code, dreg, (val & 0xFF0000) >> 16, 16);
3367 if (val & 0xFF000000) {
3368 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
3371 //g_assert_not_reached ();
3377 mono_arm_thumb_supported (void)
3379 return thumb_supported;
3385 * emit_load_volatile_arguments:
3387 * Load volatile arguments from the stack to the original input registers.
3388 * Required before a tail call.
3391 emit_load_volatile_arguments (MonoCompile *cfg, guint8 *code)
3393 MonoMethod *method = cfg->method;
3394 MonoMethodSignature *sig;
3399 /* FIXME: Generate intermediate code instead */
3401 sig = mono_method_signature (method);
3403 /* This is the opposite of the code in emit_prolog */
3407 cinfo = get_call_info (cfg->generic_sharing_context, NULL, sig);
3409 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
3410 ArgInfo *ainfo = &cinfo->ret;
3411 inst = cfg->vret_addr;
3412 g_assert (arm_is_imm12 (inst->inst_offset));
3413 ARM_LDR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
3415 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3416 ArgInfo *ainfo = cinfo->args + i;
3417 inst = cfg->args [pos];
3419 if (cfg->verbose_level > 2)
3420 g_print ("Loading argument %d (type: %d)\n", i, ainfo->storage);
3421 if (inst->opcode == OP_REGVAR) {
3422 if (ainfo->storage == RegTypeGeneral)
3423 ARM_MOV_REG_REG (code, inst->dreg, ainfo->reg);
3424 else if (ainfo->storage == RegTypeFP) {
3425 g_assert_not_reached ();
3426 } else if (ainfo->storage == RegTypeBase) {
3430 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
3431 ARM_LDR_IMM (code, inst->dreg, ARMREG_SP, (prev_sp_offset + ainfo->offset));
3433 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
3434 ARM_LDR_REG_REG (code, inst->dreg, ARMREG_SP, ARMREG_IP);
3438 g_assert_not_reached ();
3440 if (ainfo->storage == RegTypeGeneral || ainfo->storage == RegTypeIRegPair) {
3441 switch (ainfo->size) {
3448 g_assert (arm_is_imm12 (inst->inst_offset));
3449 ARM_LDR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
3450 g_assert (arm_is_imm12 (inst->inst_offset + 4));
3451 ARM_LDR_IMM (code, ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
3454 if (arm_is_imm12 (inst->inst_offset)) {
3455 ARM_LDR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
3457 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
3458 ARM_LDR_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
3462 } else if (ainfo->storage == RegTypeBaseGen) {
3465 } else if (ainfo->storage == RegTypeBase) {
3467 } else if (ainfo->storage == RegTypeFP) {
3468 g_assert_not_reached ();
3469 } else if (ainfo->storage == RegTypeStructByVal) {
3470 int doffset = inst->inst_offset;
3474 if (mono_class_from_mono_type (inst->inst_vtype))
3475 size = mono_class_native_size (mono_class_from_mono_type (inst->inst_vtype), NULL);
3476 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
3477 if (arm_is_imm12 (doffset)) {
3478 ARM_LDR_IMM (code, ainfo->reg + cur_reg, inst->inst_basereg, doffset);
3480 code = mono_arm_emit_load_imm (code, ARMREG_IP, doffset);
3481 ARM_LDR_REG_REG (code, ainfo->reg + cur_reg, inst->inst_basereg, ARMREG_IP);
3483 soffset += sizeof (gpointer);
3484 doffset += sizeof (gpointer);
3489 } else if (ainfo->storage == RegTypeStructByAddr) {
3504 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
3509 guint8 *code = cfg->native_code + cfg->code_len;
3510 MonoInst *last_ins = NULL;
3511 guint last_offset = 0;
3513 int imm8, rot_amount;
3515 /* we don't align basic blocks of loops on arm */
3517 if (cfg->verbose_level > 2)
3518 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
3520 cpos = bb->max_offset;
3522 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
3523 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
3524 //g_assert (!mono_compile_aot);
3527 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
3528 /* this is not thread save, but good enough */
3529 /* fixme: howto handle overflows? */
3530 //x86_inc_mem (code, &cov->data [bb->dfn].count);
3533 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) {
3534 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3535 (gpointer)"mono_break");
3536 code = emit_call_seq (cfg, code);
3539 MONO_BB_FOR_EACH_INS (bb, ins) {
3540 offset = code - cfg->native_code;
3542 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
3544 if (offset > (cfg->code_size - max_len - 16)) {
3545 cfg->code_size *= 2;
3546 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3547 code = cfg->native_code + offset;
3549 // if (ins->cil_code)
3550 // g_print ("cil code\n");
3551 mono_debug_record_line_number (cfg, ins, offset);
3553 switch (ins->opcode) {
3554 case OP_MEMORY_BARRIER:
3556 ARM_MOV_REG_IMM8 (code, ARMREG_R0, 0);
3557 ARM_MCR (code, 15, 0, ARMREG_R0, 7, 10, 5);
3561 #ifdef HAVE_AEABI_READ_TP
3562 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3563 (gpointer)"__aeabi_read_tp");
3564 code = emit_call_seq (cfg, code);
3566 ARM_LDR_IMM (code, ins->dreg, ARMREG_R0, ins->inst_offset);
3568 g_assert_not_reached ();
3572 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
3573 ppc_mulhw (code, ppc_r3, ins->sreg1, ins->sreg2);
3576 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
3577 ppc_mulhwu (code, ppc_r3, ins->sreg1, ins->sreg2);
3579 case OP_STOREI1_MEMBASE_IMM:
3580 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFF);
3581 g_assert (arm_is_imm12 (ins->inst_offset));
3582 ARM_STRB_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
3584 case OP_STOREI2_MEMBASE_IMM:
3585 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFFFF);
3586 g_assert (arm_is_imm8 (ins->inst_offset));
3587 ARM_STRH_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
3589 case OP_STORE_MEMBASE_IMM:
3590 case OP_STOREI4_MEMBASE_IMM:
3591 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm);
3592 g_assert (arm_is_imm12 (ins->inst_offset));
3593 ARM_STR_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
3595 case OP_STOREI1_MEMBASE_REG:
3596 g_assert (arm_is_imm12 (ins->inst_offset));
3597 ARM_STRB_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3599 case OP_STOREI2_MEMBASE_REG:
3600 g_assert (arm_is_imm8 (ins->inst_offset));
3601 ARM_STRH_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3603 case OP_STORE_MEMBASE_REG:
3604 case OP_STOREI4_MEMBASE_REG:
3605 /* this case is special, since it happens for spill code after lowering has been called */
3606 if (arm_is_imm12 (ins->inst_offset)) {
3607 ARM_STR_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3609 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
3610 ARM_STR_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ARMREG_LR);
3613 case OP_STOREI1_MEMINDEX:
3614 ARM_STRB_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
3616 case OP_STOREI2_MEMINDEX:
3617 ARM_STRH_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
3619 case OP_STORE_MEMINDEX:
3620 case OP_STOREI4_MEMINDEX:
3621 ARM_STR_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
3624 g_assert_not_reached ();
3626 case OP_LOAD_MEMINDEX:
3627 case OP_LOADI4_MEMINDEX:
3628 case OP_LOADU4_MEMINDEX:
3629 ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3631 case OP_LOADI1_MEMINDEX:
3632 ARM_LDRSB_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3634 case OP_LOADU1_MEMINDEX:
3635 ARM_LDRB_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3637 case OP_LOADI2_MEMINDEX:
3638 ARM_LDRSH_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3640 case OP_LOADU2_MEMINDEX:
3641 ARM_LDRH_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3643 case OP_LOAD_MEMBASE:
3644 case OP_LOADI4_MEMBASE:
3645 case OP_LOADU4_MEMBASE:
3646 /* this case is special, since it happens for spill code after lowering has been called */
3647 if (arm_is_imm12 (ins->inst_offset)) {
3648 ARM_LDR_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3650 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
3651 ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ARMREG_LR);
3654 case OP_LOADI1_MEMBASE:
3655 g_assert (arm_is_imm8 (ins->inst_offset));
3656 ARM_LDRSB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3658 case OP_LOADU1_MEMBASE:
3659 g_assert (arm_is_imm12 (ins->inst_offset));
3660 ARM_LDRB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3662 case OP_LOADU2_MEMBASE:
3663 g_assert (arm_is_imm8 (ins->inst_offset));
3664 ARM_LDRH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3666 case OP_LOADI2_MEMBASE:
3667 g_assert (arm_is_imm8 (ins->inst_offset));
3668 ARM_LDRSH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3670 case OP_ICONV_TO_I1:
3671 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 24);
3672 ARM_SAR_IMM (code, ins->dreg, ins->dreg, 24);
3674 case OP_ICONV_TO_I2:
3675 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
3676 ARM_SAR_IMM (code, ins->dreg, ins->dreg, 16);
3678 case OP_ICONV_TO_U1:
3679 ARM_AND_REG_IMM8 (code, ins->dreg, ins->sreg1, 0xff);
3681 case OP_ICONV_TO_U2:
3682 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
3683 ARM_SHR_IMM (code, ins->dreg, ins->dreg, 16);
3687 ARM_CMP_REG_REG (code, ins->sreg1, ins->sreg2);
3689 case OP_COMPARE_IMM:
3690 case OP_ICOMPARE_IMM:
3691 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3692 g_assert (imm8 >= 0);
3693 ARM_CMP_REG_IMM (code, ins->sreg1, imm8, rot_amount);
3697 * gdb does not like encountering the hw breakpoint ins in the debugged code.
3698 * So instead of emitting a trap, we emit a call a C function and place a
3701 //*(int*)code = 0xef9f0001;
3704 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3705 (gpointer)"mono_break");
3706 code = emit_call_seq (cfg, code);
3708 case OP_RELAXED_NOP:
3713 case OP_DUMMY_STORE:
3714 case OP_NOT_REACHED:
3717 case OP_SEQ_POINT: {
3719 MonoInst *info_var = cfg->arch.seq_point_info_var;
3720 MonoInst *ss_trigger_page_var = cfg->arch.ss_trigger_page_var;
3721 MonoInst *ss_read_var = cfg->arch.seq_point_read_var;
3722 MonoInst *ss_method_var = cfg->arch.seq_point_ss_method_var;
3723 MonoInst *bp_method_var = cfg->arch.seq_point_bp_method_var;
3725 int dreg = ARMREG_LR;
3727 if (cfg->soft_breakpoints) {
3728 g_assert (!cfg->compile_aot);
3732 * For AOT, we use one got slot per method, which will point to a
3733 * SeqPointInfo structure, containing all the information required
3734 * by the code below.
3736 if (cfg->compile_aot) {
3737 g_assert (info_var);
3738 g_assert (info_var->opcode == OP_REGOFFSET);
3739 g_assert (arm_is_imm12 (info_var->inst_offset));
3742 if (!cfg->soft_breakpoints) {
3744 * Read from the single stepping trigger page. This will cause a
3745 * SIGSEGV when single stepping is enabled.
3746 * We do this _before_ the breakpoint, so single stepping after
3747 * a breakpoint is hit will step to the next IL offset.
3749 g_assert (((guint64)(gsize)ss_trigger_page >> 32) == 0);
3752 if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
3753 if (cfg->soft_breakpoints) {
3754 /* Load the address of the sequence point trigger variable. */
3757 g_assert (var->opcode == OP_REGOFFSET);
3758 g_assert (arm_is_imm12 (var->inst_offset));
3759 ARM_LDR_IMM (code, dreg, var->inst_basereg, var->inst_offset);
3761 /* Read the value and check whether it is non-zero. */
3762 ARM_LDR_IMM (code, dreg, dreg, 0);
3763 ARM_CMP_REG_IMM (code, dreg, 0, 0);
3765 /* Load the address of the sequence point method. */
3766 var = ss_method_var;
3768 g_assert (var->opcode == OP_REGOFFSET);
3769 g_assert (arm_is_imm12 (var->inst_offset));
3770 ARM_LDR_IMM (code, dreg, var->inst_basereg, var->inst_offset);
3772 /* Call it conditionally. */
3773 ARM_BLX_REG_COND (code, ARMCOND_NE, dreg);
3775 if (cfg->compile_aot) {
3776 /* Load the trigger page addr from the variable initialized in the prolog */
3777 var = ss_trigger_page_var;
3779 g_assert (var->opcode == OP_REGOFFSET);
3780 g_assert (arm_is_imm12 (var->inst_offset));
3781 ARM_LDR_IMM (code, dreg, var->inst_basereg, var->inst_offset);
3783 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
3785 *(int*)code = (int)ss_trigger_page;
3788 ARM_LDR_IMM (code, dreg, dreg, 0);
3792 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
3794 if (cfg->soft_breakpoints) {
3795 /* Load the address of the breakpoint method into ip. */
3796 var = bp_method_var;
3798 g_assert (var->opcode == OP_REGOFFSET);
3799 g_assert (arm_is_imm12 (var->inst_offset));
3800 ARM_LDR_IMM (code, dreg, var->inst_basereg, var->inst_offset);
3803 * A placeholder for a possible breakpoint inserted by
3804 * mono_arch_set_breakpoint ().
3807 } else if (cfg->compile_aot) {
3808 guint32 offset = code - cfg->native_code;
3811 ARM_LDR_IMM (code, dreg, info_var->inst_basereg, info_var->inst_offset);
3812 /* Add the offset */
3813 val = ((offset / 4) * sizeof (guint8*)) + G_STRUCT_OFFSET (SeqPointInfo, bp_addrs);
3814 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF), 0);
3816 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF00) >> 8, 24);
3818 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
3819 g_assert (!(val & 0xFF000000));
3820 /* Load the info->bp_addrs [offset], which is either 0 or the address of a trigger page */
3821 ARM_LDR_IMM (code, dreg, dreg, 0);
3823 /* What is faster, a branch or a load ? */
3824 ARM_CMP_REG_IMM (code, dreg, 0, 0);
3825 /* The breakpoint instruction */
3826 ARM_LDR_IMM_COND (code, dreg, dreg, 0, ARMCOND_NE);
3829 * A placeholder for a possible breakpoint inserted by
3830 * mono_arch_set_breakpoint ().
3832 for (i = 0; i < 4; ++i)
3839 ARM_ADDS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3842 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3846 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3849 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3850 g_assert (imm8 >= 0);
3851 ARM_ADDS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3855 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3856 g_assert (imm8 >= 0);
3857 ARM_ADD_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3861 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3862 g_assert (imm8 >= 0);
3863 ARM_ADCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3866 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3867 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3869 case OP_IADD_OVF_UN:
3870 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3871 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3874 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3875 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3877 case OP_ISUB_OVF_UN:
3878 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3879 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
3881 case OP_ADD_OVF_CARRY:
3882 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3883 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3885 case OP_ADD_OVF_UN_CARRY:
3886 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3887 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3889 case OP_SUB_OVF_CARRY:
3890 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3891 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3893 case OP_SUB_OVF_UN_CARRY:
3894 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3895 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
3899 ARM_SUBS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3902 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3903 g_assert (imm8 >= 0);
3904 ARM_SUBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3907 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3911 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3915 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3916 g_assert (imm8 >= 0);
3917 ARM_SUB_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3921 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3922 g_assert (imm8 >= 0);
3923 ARM_SBCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3925 case OP_ARM_RSBS_IMM:
3926 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3927 g_assert (imm8 >= 0);
3928 ARM_RSBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3930 case OP_ARM_RSC_IMM:
3931 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3932 g_assert (imm8 >= 0);
3933 ARM_RSC_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3936 ARM_AND_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3940 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3941 g_assert (imm8 >= 0);
3942 ARM_AND_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3950 /* crappy ARM arch doesn't have a DIV instruction */
3951 g_assert_not_reached ();
3953 ARM_ORR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3957 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3958 g_assert (imm8 >= 0);
3959 ARM_ORR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3962 ARM_EOR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3966 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3967 g_assert (imm8 >= 0);
3968 ARM_EOR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3971 ARM_SHL_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3976 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
3977 else if (ins->dreg != ins->sreg1)
3978 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
3981 ARM_SAR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3986 ARM_SAR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
3987 else if (ins->dreg != ins->sreg1)
3988 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
3991 case OP_ISHR_UN_IMM:
3993 ARM_SHR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
3994 else if (ins->dreg != ins->sreg1)
3995 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
3998 ARM_SHR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4001 ARM_MVN_REG_REG (code, ins->dreg, ins->sreg1);
4004 ARM_RSB_REG_IMM8 (code, ins->dreg, ins->sreg1, 0);
4007 if (ins->dreg == ins->sreg2)
4008 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4010 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg2, ins->sreg1);
4013 g_assert_not_reached ();
4016 /* FIXME: handle ovf/ sreg2 != dreg */
4017 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4018 /* FIXME: MUL doesn't set the C/O flags on ARM */
4020 case OP_IMUL_OVF_UN:
4021 /* FIXME: handle ovf/ sreg2 != dreg */
4022 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4023 /* FIXME: MUL doesn't set the C/O flags on ARM */
4026 code = mono_arm_emit_load_imm (code, ins->dreg, ins->inst_c0);
4029 /* Load the GOT offset */
4030 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4031 ARM_LDR_IMM (code, ins->dreg, ARMREG_PC, 0);
4033 *(gpointer*)code = NULL;
4035 /* Load the value from the GOT */
4036 ARM_LDR_REG_REG (code, ins->dreg, ARMREG_PC, ins->dreg);
4038 case OP_ICONV_TO_I4:
4039 case OP_ICONV_TO_U4:
4041 if (ins->dreg != ins->sreg1)
4042 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
4045 int saved = ins->sreg2;
4046 if (ins->sreg2 == ARM_LSW_REG) {
4047 ARM_MOV_REG_REG (code, ARMREG_LR, ins->sreg2);
4050 if (ins->sreg1 != ARM_LSW_REG)
4051 ARM_MOV_REG_REG (code, ARM_LSW_REG, ins->sreg1);
4052 if (saved != ARM_MSW_REG)
4053 ARM_MOV_REG_REG (code, ARM_MSW_REG, saved);
4058 ARM_FPA_MVFD (code, ins->dreg, ins->sreg1);
4060 ARM_CPYD (code, ins->dreg, ins->sreg1);
4062 case OP_FCONV_TO_R4:
4064 ARM_FPA_MVFS (code, ins->dreg, ins->sreg1);
4066 ARM_CVTD (code, ins->dreg, ins->sreg1);
4067 ARM_CVTS (code, ins->dreg, ins->dreg);
4072 * Keep in sync with mono_arch_emit_epilog
4074 g_assert (!cfg->method->save_lmf);
4076 code = emit_load_volatile_arguments (cfg, code);
4078 code = emit_big_add (code, ARMREG_SP, cfg->frame_reg, cfg->stack_usage);
4080 if (cfg->used_int_regs)
4081 ARM_POP (code, cfg->used_int_regs);
4082 ARM_POP (code, (1 << ARMREG_R7) | (1 << ARMREG_LR));
4084 ARM_POP (code, cfg->used_int_regs | (1 << ARMREG_LR));
4086 mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
4087 if (cfg->compile_aot) {
4088 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
4090 *(gpointer*)code = NULL;
4092 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
4098 /* ensure ins->sreg1 is not NULL */
4099 ARM_LDRB_IMM (code, ARMREG_LR, ins->sreg1, 0);
4102 g_assert (cfg->sig_cookie < 128);
4103 ARM_LDR_IMM (code, ARMREG_IP, cfg->frame_reg, cfg->sig_cookie);
4104 ARM_STR_IMM (code, ARMREG_IP, ins->sreg1, 0);
4113 call = (MonoCallInst*)ins;
4114 if (ins->flags & MONO_INST_HAS_METHOD)
4115 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
4117 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
4118 code = emit_call_seq (cfg, code);
4119 ins->flags |= MONO_INST_GC_CALLSITE;
4120 ins->backend.pc_offset = code - cfg->native_code;
4121 code = emit_move_return_value (cfg, ins, code);
4127 case OP_VOIDCALL_REG:
4129 code = emit_call_reg (code, ins->sreg1);
4130 ins->flags |= MONO_INST_GC_CALLSITE;
4131 ins->backend.pc_offset = code - cfg->native_code;
4132 code = emit_move_return_value (cfg, ins, code);
4134 case OP_FCALL_MEMBASE:
4135 case OP_LCALL_MEMBASE:
4136 case OP_VCALL_MEMBASE:
4137 case OP_VCALL2_MEMBASE:
4138 case OP_VOIDCALL_MEMBASE:
4139 case OP_CALL_MEMBASE:
4140 g_assert (arm_is_imm12 (ins->inst_offset));
4141 g_assert (ins->sreg1 != ARMREG_LR);
4142 call = (MonoCallInst*)ins;
4143 if (call->dynamic_imt_arg || call->method->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4144 ARM_ADD_REG_IMM8 (code, ARMREG_LR, ARMREG_PC, 4);
4145 ARM_LDR_IMM (code, ARMREG_PC, ins->sreg1, ins->inst_offset);
4147 * We can't embed the method in the code stream in PIC code, or
4149 * Instead, we put it in V5 in code emitted by
4150 * mono_arch_emit_imt_argument (), and embed NULL here to
4151 * signal the IMT thunk that the value is in V5.
4153 if (call->dynamic_imt_arg)
4154 *((gpointer*)code) = NULL;
4156 *((gpointer*)code) = (gpointer)call->method;
4159 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
4160 ARM_LDR_IMM (code, ARMREG_PC, ins->sreg1, ins->inst_offset);
4162 ins->flags |= MONO_INST_GC_CALLSITE;
4163 ins->backend.pc_offset = code - cfg->native_code;
4164 code = emit_move_return_value (cfg, ins, code);
4167 /* keep alignment */
4168 int alloca_waste = cfg->param_area;
4171 /* round the size to 8 bytes */
4172 ARM_ADD_REG_IMM8 (code, ins->dreg, ins->sreg1, 7);
4173 ARM_BIC_REG_IMM8 (code, ins->dreg, ins->dreg, 7);
4175 ARM_ADD_REG_IMM8 (code, ins->dreg, ins->dreg, alloca_waste);
4176 ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ins->dreg);
4177 /* memzero the area: dreg holds the size, sp is the pointer */
4178 if (ins->flags & MONO_INST_INIT) {
4179 guint8 *start_loop, *branch_to_cond;
4180 ARM_MOV_REG_IMM8 (code, ARMREG_LR, 0);
4181 branch_to_cond = code;
4184 ARM_STR_REG_REG (code, ARMREG_LR, ARMREG_SP, ins->dreg);
4185 arm_patch (branch_to_cond, code);
4186 /* decrement by 4 and set flags */
4187 ARM_SUBS_REG_IMM8 (code, ins->dreg, ins->dreg, sizeof (mgreg_t));
4188 ARM_B_COND (code, ARMCOND_GE, 0);
4189 arm_patch (code - 4, start_loop);
4191 ARM_ADD_REG_IMM8 (code, ins->dreg, ARMREG_SP, alloca_waste);
4196 MonoInst *var = cfg->dyn_call_var;
4198 g_assert (var->opcode == OP_REGOFFSET);
4199 g_assert (arm_is_imm12 (var->inst_offset));
4201 /* lr = args buffer filled by mono_arch_get_dyn_call_args () */
4202 ARM_MOV_REG_REG( code, ARMREG_LR, ins->sreg1);
4204 ARM_MOV_REG_REG( code, ARMREG_IP, ins->sreg2);
4206 /* Save args buffer */
4207 ARM_STR_IMM (code, ARMREG_LR, var->inst_basereg, var->inst_offset);
4209 /* Set stack slots using R0 as scratch reg */
4210 /* MONO_ARCH_DYN_CALL_PARAM_AREA gives the size of stack space available */
4211 for (i = 0; i < DYN_CALL_STACK_ARGS; ++i) {
4212 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_LR, (PARAM_REGS + i) * sizeof (mgreg_t));
4213 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, i * sizeof (mgreg_t));
4216 /* Set argument registers */
4217 for (i = 0; i < PARAM_REGS; ++i)
4218 ARM_LDR_IMM (code, i, ARMREG_LR, i * sizeof (mgreg_t));
4221 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
4222 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
4225 ARM_LDR_IMM (code, ARMREG_IP, var->inst_basereg, var->inst_offset);
4226 ARM_STR_IMM (code, ARMREG_R0, ARMREG_IP, G_STRUCT_OFFSET (DynCallArgs, res));
4227 ARM_STR_IMM (code, ARMREG_R1, ARMREG_IP, G_STRUCT_OFFSET (DynCallArgs, res2));
4231 if (ins->sreg1 != ARMREG_R0)
4232 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
4233 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
4234 (gpointer)"mono_arch_throw_exception");
4235 code = emit_call_seq (cfg, code);
4239 if (ins->sreg1 != ARMREG_R0)
4240 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
4241 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
4242 (gpointer)"mono_arch_rethrow_exception");
4243 code = emit_call_seq (cfg, code);
4246 case OP_START_HANDLER: {
4247 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4250 /* Reserve a param area, see filter-stack.exe */
4251 if (cfg->param_area) {
4252 if ((i = mono_arm_is_rotated_imm8 (cfg->param_area, &rot_amount)) >= 0) {
4253 ARM_SUB_REG_IMM (code, ARMREG_SP, ARMREG_SP, i, rot_amount);
4255 code = mono_arm_emit_load_imm (code, ARMREG_IP, cfg->param_area);
4256 ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
4260 if (arm_is_imm12 (spvar->inst_offset)) {
4261 ARM_STR_IMM (code, ARMREG_LR, spvar->inst_basereg, spvar->inst_offset);
4263 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
4264 ARM_STR_REG_REG (code, ARMREG_LR, spvar->inst_basereg, ARMREG_IP);
4268 case OP_ENDFILTER: {
4269 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4272 /* Free the param area */
4273 if (cfg->param_area) {
4274 if ((i = mono_arm_is_rotated_imm8 (cfg->param_area, &rot_amount)) >= 0) {
4275 ARM_ADD_REG_IMM (code, ARMREG_SP, ARMREG_SP, i, rot_amount);
4277 code = mono_arm_emit_load_imm (code, ARMREG_IP, cfg->param_area);
4278 ARM_ADD_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
4282 if (ins->sreg1 != ARMREG_R0)
4283 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
4284 if (arm_is_imm12 (spvar->inst_offset)) {
4285 ARM_LDR_IMM (code, ARMREG_IP, spvar->inst_basereg, spvar->inst_offset);
4287 g_assert (ARMREG_IP != spvar->inst_basereg);
4288 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
4289 ARM_LDR_REG_REG (code, ARMREG_IP, spvar->inst_basereg, ARMREG_IP);
4291 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
4294 case OP_ENDFINALLY: {
4295 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4298 /* Free the param area */
4299 if (cfg->param_area) {
4300 if ((i = mono_arm_is_rotated_imm8 (cfg->param_area, &rot_amount)) >= 0) {
4301 ARM_ADD_REG_IMM (code, ARMREG_SP, ARMREG_SP, i, rot_amount);
4303 code = mono_arm_emit_load_imm (code, ARMREG_IP, cfg->param_area);
4304 ARM_ADD_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
4308 if (arm_is_imm12 (spvar->inst_offset)) {
4309 ARM_LDR_IMM (code, ARMREG_IP, spvar->inst_basereg, spvar->inst_offset);
4311 g_assert (ARMREG_IP != spvar->inst_basereg);
4312 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
4313 ARM_LDR_REG_REG (code, ARMREG_IP, spvar->inst_basereg, ARMREG_IP);
4315 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
4318 case OP_CALL_HANDLER:
4319 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4321 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
4324 ins->inst_c0 = code - cfg->native_code;
4327 /*if (ins->inst_target_bb->native_offset) {
4329 //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
4331 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4336 ARM_MOV_REG_REG (code, ARMREG_PC, ins->sreg1);
4340 * In the normal case we have:
4341 * ldr pc, [pc, ins->sreg1 << 2]
4344 * ldr lr, [pc, ins->sreg1 << 2]
4346 * After follows the data.
4347 * FIXME: add aot support.
4349 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_SWITCH, ins->inst_p0);
4350 max_len += 4 * GPOINTER_TO_INT (ins->klass);
4351 if (offset + max_len > (cfg->code_size - 16)) {
4352 cfg->code_size += max_len;
4353 cfg->code_size *= 2;
4354 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4355 code = cfg->native_code + offset;
4357 ARM_LDR_REG_REG_SHIFT (code, ARMREG_PC, ARMREG_PC, ins->sreg1, ARMSHIFT_LSL, 2);
4359 code += 4 * GPOINTER_TO_INT (ins->klass);
4363 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
4364 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
4368 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4369 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LT);
4373 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4374 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LO);
4378 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4379 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_GT);
4383 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4384 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_HI);
4386 case OP_COND_EXC_EQ:
4387 case OP_COND_EXC_NE_UN:
4388 case OP_COND_EXC_LT:
4389 case OP_COND_EXC_LT_UN:
4390 case OP_COND_EXC_GT:
4391 case OP_COND_EXC_GT_UN:
4392 case OP_COND_EXC_GE:
4393 case OP_COND_EXC_GE_UN:
4394 case OP_COND_EXC_LE:
4395 case OP_COND_EXC_LE_UN:
4396 EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_EQ, ins->inst_p1);
4398 case OP_COND_EXC_IEQ:
4399 case OP_COND_EXC_INE_UN:
4400 case OP_COND_EXC_ILT:
4401 case OP_COND_EXC_ILT_UN:
4402 case OP_COND_EXC_IGT:
4403 case OP_COND_EXC_IGT_UN:
4404 case OP_COND_EXC_IGE:
4405 case OP_COND_EXC_IGE_UN:
4406 case OP_COND_EXC_ILE:
4407 case OP_COND_EXC_ILE_UN:
4408 EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_IEQ, ins->inst_p1);
4411 case OP_COND_EXC_IC:
4412 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_CS, ins->inst_p1);
4414 case OP_COND_EXC_OV:
4415 case OP_COND_EXC_IOV:
4416 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VS, ins->inst_p1);
4418 case OP_COND_EXC_NC:
4419 case OP_COND_EXC_INC:
4420 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_CC, ins->inst_p1);
4422 case OP_COND_EXC_NO:
4423 case OP_COND_EXC_INO:
4424 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VC, ins->inst_p1);
4436 EMIT_COND_BRANCH (ins, ins->opcode - OP_IBEQ);
4439 /* floating point opcodes */
4442 if (cfg->compile_aot) {
4443 ARM_FPA_LDFD (code, ins->dreg, ARMREG_PC, 0);
4445 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
4447 *(guint32*)code = ((guint32*)(ins->inst_p0))[1];
4450 /* FIXME: we can optimize the imm load by dealing with part of
4451 * the displacement in LDFD (aligning to 512).
4453 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
4454 ARM_FPA_LDFD (code, ins->dreg, ARMREG_LR, 0);
4458 if (cfg->compile_aot) {
4459 ARM_FPA_LDFS (code, ins->dreg, ARMREG_PC, 0);
4461 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
4464 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
4465 ARM_FPA_LDFS (code, ins->dreg, ARMREG_LR, 0);
4468 case OP_STORER8_MEMBASE_REG:
4469 /* This is generated by the local regalloc pass which runs after the lowering pass */
4470 if (!arm_is_fpimm8 (ins->inst_offset)) {
4471 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4472 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_destbasereg);
4473 ARM_FPA_STFD (code, ins->sreg1, ARMREG_LR, 0);
4475 ARM_FPA_STFD (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
4478 case OP_LOADR8_MEMBASE:
4479 /* This is generated by the local regalloc pass which runs after the lowering pass */
4480 if (!arm_is_fpimm8 (ins->inst_offset)) {
4481 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4482 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_basereg);
4483 ARM_FPA_LDFD (code, ins->dreg, ARMREG_LR, 0);
4485 ARM_FPA_LDFD (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4488 case OP_STORER4_MEMBASE_REG:
4489 g_assert (arm_is_fpimm8 (ins->inst_offset));
4490 ARM_FPA_STFS (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
4492 case OP_LOADR4_MEMBASE:
4493 g_assert (arm_is_fpimm8 (ins->inst_offset));
4494 ARM_FPA_LDFS (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4496 case OP_ICONV_TO_R_UN: {
4498 tmpreg = ins->dreg == 0? 1: 0;
4499 ARM_CMP_REG_IMM8 (code, ins->sreg1, 0);
4500 ARM_FPA_FLTD (code, ins->dreg, ins->sreg1);
4501 ARM_B_COND (code, ARMCOND_GE, 8);
4502 /* save the temp register */
4503 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
4504 ARM_FPA_STFD (code, tmpreg, ARMREG_SP, 0);
4505 ARM_FPA_LDFD (code, tmpreg, ARMREG_PC, 12);
4506 ARM_FPA_ADFD (code, ins->dreg, ins->dreg, tmpreg);
4507 ARM_FPA_LDFD (code, tmpreg, ARMREG_SP, 0);
4508 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
4509 /* skip the constant pool */
4512 *(int*)code = 0x41f00000;
4517 * ldfltd ftemp, [pc, #8] 0x41f00000 0x00000000
4518 * adfltd fdest, fdest, ftemp
4522 case OP_ICONV_TO_R4:
4523 ARM_FPA_FLTS (code, ins->dreg, ins->sreg1);
4525 case OP_ICONV_TO_R8:
4526 ARM_FPA_FLTD (code, ins->dreg, ins->sreg1);
4529 #elif defined(ARM_FPU_VFP)
4532 if (cfg->compile_aot) {
4533 ARM_FLDD (code, ins->dreg, ARMREG_PC, 0);
4535 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
4537 *(guint32*)code = ((guint32*)(ins->inst_p0))[1];
4540 /* FIXME: we can optimize the imm load by dealing with part of
4541 * the displacement in LDFD (aligning to 512).
4543 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
4544 ARM_FLDD (code, ins->dreg, ARMREG_LR, 0);
4548 if (cfg->compile_aot) {
4549 ARM_FLDS (code, ins->dreg, ARMREG_PC, 0);
4551 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
4553 ARM_CVTS (code, ins->dreg, ins->dreg);
4555 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
4556 ARM_FLDS (code, ins->dreg, ARMREG_LR, 0);
4557 ARM_CVTS (code, ins->dreg, ins->dreg);
4560 case OP_STORER8_MEMBASE_REG:
4561 /* This is generated by the local regalloc pass which runs after the lowering pass */
4562 if (!arm_is_fpimm8 (ins->inst_offset)) {
4563 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4564 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_destbasereg);
4565 ARM_FSTD (code, ins->sreg1, ARMREG_LR, 0);
4567 ARM_FSTD (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
4570 case OP_LOADR8_MEMBASE:
4571 /* This is generated by the local regalloc pass which runs after the lowering pass */
4572 if (!arm_is_fpimm8 (ins->inst_offset)) {
4573 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4574 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_basereg);
4575 ARM_FLDD (code, ins->dreg, ARMREG_LR, 0);
4577 ARM_FLDD (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4580 case OP_STORER4_MEMBASE_REG:
4581 g_assert (arm_is_fpimm8 (ins->inst_offset));
4582 ARM_CVTD (code, ARM_VFP_F0, ins->sreg1);
4583 ARM_FSTS (code, ARM_VFP_F0, ins->inst_destbasereg, ins->inst_offset);
4585 case OP_LOADR4_MEMBASE:
4586 g_assert (arm_is_fpimm8 (ins->inst_offset));
4587 ARM_FLDS (code, ARM_VFP_F0, ins->inst_basereg, ins->inst_offset);
4588 ARM_CVTS (code, ins->dreg, ARM_VFP_F0);
4590 case OP_ICONV_TO_R_UN: {
4591 g_assert_not_reached ();
4594 case OP_ICONV_TO_R4:
4595 ARM_FMSR (code, ARM_VFP_F0, ins->sreg1);
4596 ARM_FSITOS (code, ARM_VFP_F0, ARM_VFP_F0);
4597 ARM_CVTS (code, ins->dreg, ARM_VFP_F0);
4599 case OP_ICONV_TO_R8:
4600 ARM_FMSR (code, ARM_VFP_F0, ins->sreg1);
4601 ARM_FSITOD (code, ins->dreg, ARM_VFP_F0);
4605 if (mono_method_signature (cfg->method)->ret->type == MONO_TYPE_R4) {
4606 ARM_CVTD (code, ARM_VFP_F0, ins->sreg1);
4607 ARM_FMRS (code, ARMREG_R0, ARM_VFP_F0);
4609 ARM_FMRRD (code, ARMREG_R0, ARMREG_R1, ins->sreg1);
4615 case OP_FCONV_TO_I1:
4616 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
4618 case OP_FCONV_TO_U1:
4619 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
4621 case OP_FCONV_TO_I2:
4622 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
4624 case OP_FCONV_TO_U2:
4625 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
4627 case OP_FCONV_TO_I4:
4629 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
4631 case OP_FCONV_TO_U4:
4633 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
4635 case OP_FCONV_TO_I8:
4636 case OP_FCONV_TO_U8:
4637 g_assert_not_reached ();
4638 /* Implemented as helper calls */
4640 case OP_LCONV_TO_R_UN:
4641 g_assert_not_reached ();
4642 /* Implemented as helper calls */
4644 case OP_LCONV_TO_OVF_I4_2: {
4645 guint8 *high_bit_not_set, *valid_negative, *invalid_negative, *valid_positive;
4647 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
4650 ARM_CMP_REG_IMM8 (code, ins->sreg1, 0);
4651 high_bit_not_set = code;
4652 ARM_B_COND (code, ARMCOND_GE, 0); /*branch if bit 31 of the lower part is not set*/
4654 ARM_CMN_REG_IMM8 (code, ins->sreg2, 1); /*This have the same effect as CMP reg, 0xFFFFFFFF */
4655 valid_negative = code;
4656 ARM_B_COND (code, ARMCOND_EQ, 0); /*branch if upper part == 0xFFFFFFFF (lower part has bit 31 set) */
4657 invalid_negative = code;
4658 ARM_B_COND (code, ARMCOND_AL, 0);
4660 arm_patch (high_bit_not_set, code);
4662 ARM_CMP_REG_IMM8 (code, ins->sreg2, 0);
4663 valid_positive = code;
4664 ARM_B_COND (code, ARMCOND_EQ, 0); /*branch if upper part == 0 (lower part has bit 31 clear)*/
4666 arm_patch (invalid_negative, code);
4667 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_AL, "OverflowException");
4669 arm_patch (valid_negative, code);
4670 arm_patch (valid_positive, code);
4672 if (ins->dreg != ins->sreg1)
4673 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
4678 ARM_FPA_ADFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4681 ARM_FPA_SUFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4684 ARM_FPA_MUFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4687 ARM_FPA_DVFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4690 ARM_FPA_MNFD (code, ins->dreg, ins->sreg1);
4692 #elif defined(ARM_FPU_VFP)
4694 ARM_VFP_ADDD (code, ins->dreg, ins->sreg1, ins->sreg2);
4697 ARM_VFP_SUBD (code, ins->dreg, ins->sreg1, ins->sreg2);
4700 ARM_VFP_MULD (code, ins->dreg, ins->sreg1, ins->sreg2);
4703 ARM_VFP_DIVD (code, ins->dreg, ins->sreg1, ins->sreg2);
4706 ARM_NEGD (code, ins->dreg, ins->sreg1);
4711 g_assert_not_reached ();
4715 ARM_FPA_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4716 } else if (IS_VFP) {
4717 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4723 ARM_FPA_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4724 } else if (IS_VFP) {
4725 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4728 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
4729 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
4733 ARM_FPA_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4735 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4738 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4739 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4743 ARM_FPA_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4744 } else if (IS_VFP) {
4745 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4748 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4749 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4750 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
4755 ARM_FPA_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
4756 } else if (IS_VFP) {
4757 ARM_CMPD (code, ins->sreg2, ins->sreg1);
4760 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4761 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4766 ARM_FPA_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
4767 } else if (IS_VFP) {
4768 ARM_CMPD (code, ins->sreg2, ins->sreg1);
4771 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4772 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4773 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
4775 /* ARM FPA flags table:
4776 * N Less than ARMCOND_MI
4777 * Z Equal ARMCOND_EQ
4778 * C Greater Than or Equal ARMCOND_CS
4779 * V Unordered ARMCOND_VS
4782 EMIT_COND_BRANCH (ins, OP_IBEQ - OP_IBEQ);
4785 EMIT_COND_BRANCH (ins, OP_IBNE_UN - OP_IBEQ);
4788 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set */
4791 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
4792 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set */
4798 g_assert_not_reached ();
4802 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_GE);
4804 /* FPA requires EQ even thou the docs suggests that just CS is enough */
4805 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_EQ);
4806 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_CS);
4810 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
4811 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_GE);
4816 if (ins->dreg != ins->sreg1)
4817 ARM_FPA_MVFD (code, ins->dreg, ins->sreg1);
4818 } else if (IS_VFP) {
4819 ARM_ABSD (code, ARM_VFP_D1, ins->sreg1);
4820 ARM_FLDD (code, ARM_VFP_D0, ARMREG_PC, 0);
4822 *(guint32*)code = 0xffffffff;
4824 *(guint32*)code = 0x7fefffff;
4826 ARM_CMPD (code, ARM_VFP_D1, ARM_VFP_D0);
4828 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_GT, "ArithmeticException");
4829 ARM_CMPD (code, ins->sreg1, ins->sreg1);
4831 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VS, "ArithmeticException");
4832 ARM_CPYD (code, ins->dreg, ins->sreg1);
4837 case OP_GC_LIVENESS_DEF:
4838 case OP_GC_LIVENESS_USE:
4839 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
4840 ins->backend.pc_offset = code - cfg->native_code;
4842 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
4843 ins->backend.pc_offset = code - cfg->native_code;
4844 bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
4848 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
4849 g_assert_not_reached ();
4852 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
4853 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
4854 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
4855 g_assert_not_reached ();
4861 last_offset = offset;
4864 cfg->code_len = code - cfg->native_code;
4867 #endif /* DISABLE_JIT */
4869 #ifdef HAVE_AEABI_READ_TP
4870 void __aeabi_read_tp (void);
4874 mono_arch_register_lowlevel_calls (void)
4876 /* The signature doesn't matter */
4877 mono_register_jit_icall (mono_arm_throw_exception, "mono_arm_throw_exception", mono_create_icall_signature ("void"), TRUE);
4878 mono_register_jit_icall (mono_arm_throw_exception_by_token, "mono_arm_throw_exception_by_token", mono_create_icall_signature ("void"), TRUE);
4880 #ifndef MONO_CROSS_COMPILE
4881 #ifdef HAVE_AEABI_READ_TP
4882 mono_register_jit_icall (__aeabi_read_tp, "__aeabi_read_tp", mono_create_icall_signature ("void"), TRUE);
4887 #define patch_lis_ori(ip,val) do {\
4888 guint16 *__lis_ori = (guint16*)(ip); \
4889 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff; \
4890 __lis_ori [3] = ((guint32)(val)) & 0xffff; \
4894 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, MonoCodeManager *dyn_code_mp, gboolean run_cctors)
4896 MonoJumpInfo *patch_info;
4897 gboolean compile_aot = !run_cctors;
4899 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
4900 unsigned char *ip = patch_info->ip.i + code;
4901 const unsigned char *target;
4903 if (patch_info->type == MONO_PATCH_INFO_SWITCH && !compile_aot) {
4904 gpointer *jt = (gpointer*)(ip + 8);
4906 /* jt is the inlined jump table, 2 instructions after ip
4907 * In the normal case we store the absolute addresses,
4908 * otherwise the displacements.
4910 for (i = 0; i < patch_info->data.table->table_size; i++)
4911 jt [i] = code + (int)patch_info->data.table->table [i];
4914 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
4917 switch (patch_info->type) {
4918 case MONO_PATCH_INFO_BB:
4919 case MONO_PATCH_INFO_LABEL:
4922 /* No need to patch these */
4927 switch (patch_info->type) {
4928 case MONO_PATCH_INFO_IP:
4929 g_assert_not_reached ();
4930 patch_lis_ori (ip, ip);
4932 case MONO_PATCH_INFO_METHOD_REL:
4933 g_assert_not_reached ();
4934 *((gpointer *)(ip)) = code + patch_info->data.offset;
4936 case MONO_PATCH_INFO_METHODCONST:
4937 case MONO_PATCH_INFO_CLASS:
4938 case MONO_PATCH_INFO_IMAGE:
4939 case MONO_PATCH_INFO_FIELD:
4940 case MONO_PATCH_INFO_VTABLE:
4941 case MONO_PATCH_INFO_IID:
4942 case MONO_PATCH_INFO_SFLDA:
4943 case MONO_PATCH_INFO_LDSTR:
4944 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
4945 case MONO_PATCH_INFO_LDTOKEN:
4946 g_assert_not_reached ();
4947 /* from OP_AOTCONST : lis + ori */
4948 patch_lis_ori (ip, target);
4950 case MONO_PATCH_INFO_R4:
4951 case MONO_PATCH_INFO_R8:
4952 g_assert_not_reached ();
4953 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
4955 case MONO_PATCH_INFO_EXC_NAME:
4956 g_assert_not_reached ();
4957 *((gconstpointer *)(ip + 1)) = patch_info->data.name;
4959 case MONO_PATCH_INFO_NONE:
4960 case MONO_PATCH_INFO_BB_OVF:
4961 case MONO_PATCH_INFO_EXC_OVF:
4962 /* everything is dealt with at epilog output time */
4967 arm_patch_general (domain, ip, target, dyn_code_mp);
4974 * Stack frame layout:
4976 * ------------------- fp
4977 * MonoLMF structure or saved registers
4978 * -------------------
4980 * -------------------
4982 * -------------------
4983 * optional 8 bytes for tracing
4984 * -------------------
4985 * param area size is cfg->param_area
4986 * ------------------- sp
4989 mono_arch_emit_prolog (MonoCompile *cfg)
4991 MonoMethod *method = cfg->method;
4993 MonoMethodSignature *sig;
4995 int alloc_size, orig_alloc_size, pos, max_offset, i, rot_amount;
5000 int prev_sp_offset, reg_offset;
5002 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
5005 sig = mono_method_signature (method);
5006 cfg->code_size = 256 + sig->param_count * 64;
5007 code = cfg->native_code = g_malloc (cfg->code_size);
5009 mono_emit_unwind_op_def_cfa (cfg, code, ARMREG_SP, 0);
5011 alloc_size = cfg->stack_offset;
5015 if (!method->save_lmf) {
5018 * The iphone uses R7 as the frame pointer, and it points at the saved
5023 * We can't use r7 as a frame pointer since it points into the middle of
5024 * the frame, so we keep using our own frame pointer.
5025 * FIXME: Optimize this.
5028 ARM_PUSH (code, (1 << ARMREG_R7) | (1 << ARMREG_LR));
5029 ARM_MOV_REG_REG (code, ARMREG_R7, ARMREG_SP);
5030 prev_sp_offset += 8; /* r7 and lr */
5031 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
5032 mono_emit_unwind_op_offset (cfg, code, ARMREG_R7, (- prev_sp_offset) + 0);
5034 /* No need to push LR again */
5035 if (cfg->used_int_regs)
5036 ARM_PUSH (code, cfg->used_int_regs);
5038 ARM_PUSH (code, cfg->used_int_regs | (1 << ARMREG_LR));
5039 prev_sp_offset += 4;
5041 for (i = 0; i < 16; ++i) {
5042 if (cfg->used_int_regs & (1 << i))
5043 prev_sp_offset += 4;
5045 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
5047 for (i = 0; i < 16; ++i) {
5048 if ((cfg->used_int_regs & (1 << i))) {
5049 mono_emit_unwind_op_offset (cfg, code, i, (- prev_sp_offset) + reg_offset);
5050 mini_gc_set_slot_type_from_cfa (cfg, (- prev_sp_offset) + reg_offset, SLOT_NOREF);
5055 mono_emit_unwind_op_offset (cfg, code, ARMREG_LR, -4);
5056 mini_gc_set_slot_type_from_cfa (cfg, -4, SLOT_NOREF);
5058 mono_emit_unwind_op_offset (cfg, code, ARMREG_LR, -4);
5059 mini_gc_set_slot_type_from_cfa (cfg, -4, SLOT_NOREF);
5062 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
5063 ARM_PUSH (code, 0x5ff0);
5064 prev_sp_offset += 4 * 10; /* all but r0-r3, sp and pc */
5065 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
5067 for (i = 0; i < 16; ++i) {
5068 if ((i > ARMREG_R3) && (i != ARMREG_SP) && (i != ARMREG_PC)) {
5069 mono_emit_unwind_op_offset (cfg, code, i, (- prev_sp_offset) + reg_offset);
5073 pos += sizeof (MonoLMF) - prev_sp_offset;
5077 orig_alloc_size = alloc_size;
5078 // align to MONO_ARCH_FRAME_ALIGNMENT bytes
5079 if (alloc_size & (MONO_ARCH_FRAME_ALIGNMENT - 1)) {
5080 alloc_size += MONO_ARCH_FRAME_ALIGNMENT - 1;
5081 alloc_size &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
5084 /* the stack used in the pushed regs */
5085 if (prev_sp_offset & 4)
5087 cfg->stack_usage = alloc_size;
5089 if ((i = mono_arm_is_rotated_imm8 (alloc_size, &rot_amount)) >= 0) {
5090 ARM_SUB_REG_IMM (code, ARMREG_SP, ARMREG_SP, i, rot_amount);
5092 code = mono_arm_emit_load_imm (code, ARMREG_IP, alloc_size);
5093 ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
5095 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset + alloc_size);
5097 if (cfg->frame_reg != ARMREG_SP) {
5098 ARM_MOV_REG_REG (code, cfg->frame_reg, ARMREG_SP);
5099 mono_emit_unwind_op_def_cfa_reg (cfg, code, cfg->frame_reg);
5101 //g_print ("prev_sp_offset: %d, alloc_size:%d\n", prev_sp_offset, alloc_size);
5102 prev_sp_offset += alloc_size;
5104 for (i = 0; i < alloc_size - orig_alloc_size; i += 4)
5105 mini_gc_set_slot_type_from_cfa (cfg, (- prev_sp_offset) + orig_alloc_size + i, SLOT_NOREF);
5107 /* compute max_offset in order to use short forward jumps
5108 * we could skip do it on arm because the immediate displacement
5109 * for jumps is large enough, it may be useful later for constant pools
5112 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5113 MonoInst *ins = bb->code;
5114 bb->max_offset = max_offset;
5116 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
5119 MONO_BB_FOR_EACH_INS (bb, ins)
5120 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
5123 /* store runtime generic context */
5124 if (cfg->rgctx_var) {
5125 MonoInst *ins = cfg->rgctx_var;
5127 g_assert (ins->opcode == OP_REGOFFSET);
5129 if (arm_is_imm12 (ins->inst_offset)) {
5130 ARM_STR_IMM (code, MONO_ARCH_RGCTX_REG, ins->inst_basereg, ins->inst_offset);
5132 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
5133 ARM_STR_REG_REG (code, MONO_ARCH_RGCTX_REG, ins->inst_basereg, ARMREG_LR);
5137 /* load arguments allocated to register from the stack */
5140 cinfo = get_call_info (cfg->generic_sharing_context, NULL, sig);
5142 if (MONO_TYPE_ISSTRUCT (sig->ret) && cinfo->ret.storage != RegTypeStructByVal) {
5143 ArgInfo *ainfo = &cinfo->ret;
5144 inst = cfg->vret_addr;
5145 g_assert (arm_is_imm12 (inst->inst_offset));
5146 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
5149 if (sig->call_convention == MONO_CALL_VARARG) {
5150 ArgInfo *cookie = &cinfo->sig_cookie;
5152 /* Save the sig cookie address */
5153 g_assert (cookie->storage == RegTypeBase);
5155 g_assert (arm_is_imm12 (prev_sp_offset + cookie->offset));
5156 g_assert (arm_is_imm12 (cfg->sig_cookie));
5157 ARM_ADD_REG_IMM8 (code, ARMREG_IP, cfg->frame_reg, prev_sp_offset + cookie->offset);
5158 ARM_STR_IMM (code, ARMREG_IP, cfg->frame_reg, cfg->sig_cookie);
5161 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
5162 ArgInfo *ainfo = cinfo->args + i;
5163 inst = cfg->args [pos];
5165 if (cfg->verbose_level > 2)
5166 g_print ("Saving argument %d (type: %d)\n", i, ainfo->storage);
5167 if (inst->opcode == OP_REGVAR) {
5168 if (ainfo->storage == RegTypeGeneral)
5169 ARM_MOV_REG_REG (code, inst->dreg, ainfo->reg);
5170 else if (ainfo->storage == RegTypeFP) {
5171 g_assert_not_reached ();
5172 } else if (ainfo->storage == RegTypeBase) {
5173 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
5174 ARM_LDR_IMM (code, inst->dreg, ARMREG_SP, (prev_sp_offset + ainfo->offset));
5176 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
5177 ARM_LDR_REG_REG (code, inst->dreg, ARMREG_SP, ARMREG_IP);
5180 g_assert_not_reached ();
5182 if (cfg->verbose_level > 2)
5183 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
5185 /* the argument should be put on the stack: FIXME handle size != word */
5186 if (ainfo->storage == RegTypeGeneral || ainfo->storage == RegTypeIRegPair) {
5187 switch (ainfo->size) {
5189 if (arm_is_imm12 (inst->inst_offset))
5190 ARM_STRB_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
5192 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
5193 ARM_STRB_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
5197 if (arm_is_imm8 (inst->inst_offset)) {
5198 ARM_STRH_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
5200 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
5201 ARM_STRH_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
5205 g_assert (arm_is_imm12 (inst->inst_offset));
5206 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
5207 g_assert (arm_is_imm12 (inst->inst_offset + 4));
5208 ARM_STR_IMM (code, ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
5211 if (arm_is_imm12 (inst->inst_offset)) {
5212 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
5214 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
5215 ARM_STR_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
5219 } else if (ainfo->storage == RegTypeBaseGen) {
5220 g_assert (arm_is_imm12 (prev_sp_offset + ainfo->offset));
5221 g_assert (arm_is_imm12 (inst->inst_offset));
5222 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
5223 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset + 4);
5224 ARM_STR_IMM (code, ARMREG_R3, inst->inst_basereg, inst->inst_offset);
5225 } else if (ainfo->storage == RegTypeBase) {
5226 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
5227 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
5229 code = mono_arm_emit_load_imm (code, ARMREG_IP, prev_sp_offset + ainfo->offset);
5230 ARM_LDR_REG_REG (code, ARMREG_LR, ARMREG_SP, ARMREG_IP);
5233 switch (ainfo->size) {
5235 if (arm_is_imm8 (inst->inst_offset)) {
5236 ARM_STRB_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
5238 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
5239 ARM_STRB_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
5243 if (arm_is_imm8 (inst->inst_offset)) {
5244 ARM_STRH_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
5246 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
5247 ARM_STRH_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
5251 if (arm_is_imm12 (inst->inst_offset)) {
5252 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
5254 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
5255 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
5257 if (arm_is_imm12 (prev_sp_offset + ainfo->offset + 4)) {
5258 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset + 4));
5260 code = mono_arm_emit_load_imm (code, ARMREG_IP, prev_sp_offset + ainfo->offset + 4);
5261 ARM_LDR_REG_REG (code, ARMREG_LR, ARMREG_SP, ARMREG_IP);
5263 if (arm_is_imm12 (inst->inst_offset + 4)) {
5264 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset + 4);
5266 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset + 4);
5267 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
5271 if (arm_is_imm12 (inst->inst_offset)) {
5272 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
5274 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
5275 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
5279 } else if (ainfo->storage == RegTypeFP) {
5280 g_assert_not_reached ();
5281 } else if (ainfo->storage == RegTypeStructByVal) {
5282 int doffset = inst->inst_offset;
5286 size = mini_type_stack_size_full (cfg->generic_sharing_context, inst->inst_vtype, NULL, sig->pinvoke);
5287 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
5288 if (arm_is_imm12 (doffset)) {
5289 ARM_STR_IMM (code, ainfo->reg + cur_reg, inst->inst_basereg, doffset);
5291 code = mono_arm_emit_load_imm (code, ARMREG_IP, doffset);
5292 ARM_STR_REG_REG (code, ainfo->reg + cur_reg, inst->inst_basereg, ARMREG_IP);
5294 soffset += sizeof (gpointer);
5295 doffset += sizeof (gpointer);
5297 if (ainfo->vtsize) {
5298 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
5299 //g_print ("emit_memcpy (prev_sp_ofs: %d, ainfo->offset: %d, soffset: %d)\n", prev_sp_offset, ainfo->offset, soffset);
5300 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ARMREG_SP, prev_sp_offset + ainfo->offset);
5302 } else if (ainfo->storage == RegTypeStructByAddr) {
5303 g_assert_not_reached ();
5304 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
5305 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
5307 g_assert_not_reached ();
5312 if (method->save_lmf)
5313 code = emit_save_lmf (cfg, code, alloc_size - lmf_offset);
5316 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
5318 if (cfg->arch.seq_point_info_var) {
5319 MonoInst *ins = cfg->arch.seq_point_info_var;
5321 /* Initialize the variable from a GOT slot */
5322 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_SEQ_POINT_INFO, cfg->method);
5323 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
5325 *(gpointer*)code = NULL;
5327 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
5329 g_assert (ins->opcode == OP_REGOFFSET);
5331 if (arm_is_imm12 (ins->inst_offset)) {
5332 ARM_STR_IMM (code, ARMREG_R0, ins->inst_basereg, ins->inst_offset);
5334 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
5335 ARM_STR_REG_REG (code, ARMREG_R0, ins->inst_basereg, ARMREG_LR);
5339 /* Initialize ss_trigger_page_var */
5340 if (!cfg->soft_breakpoints) {
5341 MonoInst *info_var = cfg->arch.seq_point_info_var;
5342 MonoInst *ss_trigger_page_var = cfg->arch.ss_trigger_page_var;
5343 int dreg = ARMREG_LR;
5346 g_assert (info_var->opcode == OP_REGOFFSET);
5347 g_assert (arm_is_imm12 (info_var->inst_offset));
5349 ARM_LDR_IMM (code, dreg, info_var->inst_basereg, info_var->inst_offset);
5350 /* Load the trigger page addr */
5351 ARM_LDR_IMM (code, dreg, dreg, G_STRUCT_OFFSET (SeqPointInfo, ss_trigger_page));
5352 ARM_STR_IMM (code, dreg, ss_trigger_page_var->inst_basereg, ss_trigger_page_var->inst_offset);
5356 if (cfg->arch.seq_point_read_var) {
5357 MonoInst *read_ins = cfg->arch.seq_point_read_var;
5358 MonoInst *ss_method_ins = cfg->arch.seq_point_ss_method_var;
5359 MonoInst *bp_method_ins = cfg->arch.seq_point_bp_method_var;
5361 g_assert (read_ins->opcode == OP_REGOFFSET);
5362 g_assert (arm_is_imm12 (read_ins->inst_offset));
5363 g_assert (ss_method_ins->opcode == OP_REGOFFSET);
5364 g_assert (arm_is_imm12 (ss_method_ins->inst_offset));
5365 g_assert (bp_method_ins->opcode == OP_REGOFFSET);
5366 g_assert (arm_is_imm12 (bp_method_ins->inst_offset));
5368 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
5370 *(volatile int **)code = &ss_trigger_var;
5372 *(gpointer*)code = single_step_func_wrapper;
5374 *(gpointer*)code = breakpoint_func_wrapper;
5377 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_LR, 0);
5378 ARM_STR_IMM (code, ARMREG_IP, read_ins->inst_basereg, read_ins->inst_offset);
5379 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_LR, 4);
5380 ARM_STR_IMM (code, ARMREG_IP, ss_method_ins->inst_basereg, ss_method_ins->inst_offset);
5381 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_LR, 8);
5382 ARM_STR_IMM (code, ARMREG_IP, bp_method_ins->inst_basereg, bp_method_ins->inst_offset);
5385 cfg->code_len = code - cfg->native_code;
5386 g_assert (cfg->code_len < cfg->code_size);
5393 mono_arch_emit_epilog (MonoCompile *cfg)
5395 MonoMethod *method = cfg->method;
5396 int pos, i, rot_amount;
5397 int max_epilog_size = 16 + 20*4;
5401 if (cfg->method->save_lmf)
5402 max_epilog_size += 128;
5404 if (mono_jit_trace_calls != NULL)
5405 max_epilog_size += 50;
5407 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
5408 max_epilog_size += 50;
5410 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
5411 cfg->code_size *= 2;
5412 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5413 cfg->stat_code_reallocs++;
5417 * Keep in sync with OP_JMP
5419 code = cfg->native_code + cfg->code_len;
5421 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
5422 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
5426 /* Load returned vtypes into registers if needed */
5427 cinfo = cfg->arch.cinfo;
5428 if (cinfo->ret.storage == RegTypeStructByVal) {
5429 MonoInst *ins = cfg->ret;
5431 if (arm_is_imm12 (ins->inst_offset)) {
5432 ARM_LDR_IMM (code, ARMREG_R0, ins->inst_basereg, ins->inst_offset);
5434 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
5435 ARM_LDR_REG_REG (code, ARMREG_R0, ins->inst_basereg, ARMREG_LR);
5439 if (method->save_lmf) {
5440 int lmf_offset, reg, sp_adj, regmask;
5441 /* all but r0-r3, sp and pc */
5442 pos += sizeof (MonoLMF) - (MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t));
5445 code = emit_restore_lmf (cfg, code, cfg->stack_usage - lmf_offset);
5447 /* This points to r4 inside MonoLMF->iregs */
5448 sp_adj = (sizeof (MonoLMF) - MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t));
5450 regmask = 0x9ff0; /* restore lr to pc */
5451 /* Skip caller saved registers not used by the method */
5452 while (!(cfg->used_int_regs & (1 << reg)) && reg < ARMREG_FP) {
5453 regmask &= ~(1 << reg);
5457 /* point sp at the registers to restore: 10 is 14 -4, because we skip r0-r3 */
5458 code = emit_big_add (code, ARMREG_SP, cfg->frame_reg, cfg->stack_usage - lmf_offset + sp_adj);
5460 ARM_POP (code, regmask);
5462 if ((i = mono_arm_is_rotated_imm8 (cfg->stack_usage, &rot_amount)) >= 0) {
5463 ARM_ADD_REG_IMM (code, ARMREG_SP, cfg->frame_reg, i, rot_amount);
5465 code = mono_arm_emit_load_imm (code, ARMREG_IP, cfg->stack_usage);
5466 ARM_ADD_REG_REG (code, ARMREG_SP, cfg->frame_reg, ARMREG_IP);
5470 /* Restore saved gregs */
5471 if (cfg->used_int_regs)
5472 ARM_POP (code, cfg->used_int_regs);
5473 /* Restore saved r7, restore LR to PC */
5474 ARM_POP (code, (1 << ARMREG_R7) | (1 << ARMREG_PC));
5476 ARM_POP (code, cfg->used_int_regs | (1 << ARMREG_PC));
5480 cfg->code_len = code - cfg->native_code;
5482 g_assert (cfg->code_len < cfg->code_size);
5486 /* remove once throw_exception_by_name is eliminated */
5488 exception_id_by_name (const char *name)
5490 if (strcmp (name, "IndexOutOfRangeException") == 0)
5491 return MONO_EXC_INDEX_OUT_OF_RANGE;
5492 if (strcmp (name, "OverflowException") == 0)
5493 return MONO_EXC_OVERFLOW;
5494 if (strcmp (name, "ArithmeticException") == 0)
5495 return MONO_EXC_ARITHMETIC;
5496 if (strcmp (name, "DivideByZeroException") == 0)
5497 return MONO_EXC_DIVIDE_BY_ZERO;
5498 if (strcmp (name, "InvalidCastException") == 0)
5499 return MONO_EXC_INVALID_CAST;
5500 if (strcmp (name, "NullReferenceException") == 0)
5501 return MONO_EXC_NULL_REF;
5502 if (strcmp (name, "ArrayTypeMismatchException") == 0)
5503 return MONO_EXC_ARRAY_TYPE_MISMATCH;
5504 if (strcmp (name, "ArgumentException") == 0)
5505 return MONO_EXC_ARGUMENT;
5506 g_error ("Unknown intrinsic exception %s\n", name);
5511 mono_arch_emit_exceptions (MonoCompile *cfg)
5513 MonoJumpInfo *patch_info;
5516 guint8* exc_throw_pos [MONO_EXC_INTRINS_NUM];
5517 guint8 exc_throw_found [MONO_EXC_INTRINS_NUM];
5518 int max_epilog_size = 50;
5520 for (i = 0; i < MONO_EXC_INTRINS_NUM; i++) {
5521 exc_throw_pos [i] = NULL;
5522 exc_throw_found [i] = 0;
5525 /* count the number of exception infos */
5528 * make sure we have enough space for exceptions
5530 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5531 if (patch_info->type == MONO_PATCH_INFO_EXC) {
5532 i = exception_id_by_name (patch_info->data.target);
5533 if (!exc_throw_found [i]) {
5534 max_epilog_size += 32;
5535 exc_throw_found [i] = TRUE;
5540 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
5541 cfg->code_size *= 2;
5542 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5543 cfg->stat_code_reallocs++;
5546 code = cfg->native_code + cfg->code_len;
5548 /* add code to raise exceptions */
5549 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5550 switch (patch_info->type) {
5551 case MONO_PATCH_INFO_EXC: {
5552 MonoClass *exc_class;
5553 unsigned char *ip = patch_info->ip.i + cfg->native_code;
5555 i = exception_id_by_name (patch_info->data.target);
5556 if (exc_throw_pos [i]) {
5557 arm_patch (ip, exc_throw_pos [i]);
5558 patch_info->type = MONO_PATCH_INFO_NONE;
5561 exc_throw_pos [i] = code;
5563 arm_patch (ip, code);
5565 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
5566 g_assert (exc_class);
5568 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR);
5569 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
5570 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5571 patch_info->data.name = "mono_arch_throw_corlib_exception";
5572 patch_info->ip.i = code - cfg->native_code;
5574 *(guint32*)(gpointer)code = exc_class->type_token;
5584 cfg->code_len = code - cfg->native_code;
5586 g_assert (cfg->code_len < cfg->code_size);
5590 #endif /* #ifndef DISABLE_JIT */
5593 mono_arch_finish_init (void)
5595 lmf_tls_offset = mono_get_lmf_tls_offset ();
5596 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
5600 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
5605 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5612 mono_arch_print_tree (MonoInst *tree, int arity)
5618 mono_arch_get_domain_intrinsic (MonoCompile* cfg)
5620 return mono_get_domain_intrinsic (cfg);
5624 mono_arch_get_patch_offset (guint8 *code)
5631 mono_arch_flush_register_windows (void)
5635 #ifdef MONO_ARCH_HAVE_IMT
5640 mono_arch_emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoInst *imt_arg)
5642 if (cfg->compile_aot) {
5643 int method_reg = mono_alloc_ireg (cfg);
5646 call->dynamic_imt_arg = TRUE;
5649 mono_call_inst_add_outarg_reg (cfg, call, imt_arg->dreg, ARMREG_V5, FALSE);
5651 MONO_INST_NEW (cfg, ins, OP_AOTCONST);
5652 ins->dreg = method_reg;
5653 ins->inst_p0 = call->method;
5654 ins->inst_c1 = MONO_PATCH_INFO_METHODCONST;
5655 MONO_ADD_INS (cfg->cbb, ins);
5657 mono_call_inst_add_outarg_reg (cfg, call, method_reg, ARMREG_V5, FALSE);
5659 } else if (cfg->generic_context || imt_arg || mono_use_llvm) {
5661 /* Always pass in a register for simplicity */
5662 call->dynamic_imt_arg = TRUE;
5664 cfg->uses_rgctx_reg = TRUE;
5667 mono_call_inst_add_outarg_reg (cfg, call, imt_arg->dreg, ARMREG_V5, FALSE);
5670 int method_reg = mono_alloc_preg (cfg);
5672 MONO_INST_NEW (cfg, ins, OP_PCONST);
5673 ins->inst_p0 = call->method;
5674 ins->dreg = method_reg;
5675 MONO_ADD_INS (cfg->cbb, ins);
5677 mono_call_inst_add_outarg_reg (cfg, call, method_reg, ARMREG_V5, FALSE);
5682 #endif /* DISABLE_JIT */
5685 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
5687 guint32 *code_ptr = (guint32*)code;
5692 return (MonoMethod*)regs [ARMREG_V5];
5694 /* The IMT value is stored in the code stream right after the LDC instruction. */
5695 if (!IS_LDR_PC (code_ptr [0])) {
5696 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]);
5697 g_assert (IS_LDR_PC (code_ptr [0]));
5699 if (code_ptr [1] == 0)
5700 /* This is AOTed code, the IMT method is in V5 */
5701 return (MonoMethod*)regs [ARMREG_V5];
5703 return (MonoMethod*) code_ptr [1];
5707 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
5709 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
5712 #define ENABLE_WRONG_METHOD_CHECK 0
5713 #define BASE_SIZE (6 * 4)
5714 #define BSEARCH_ENTRY_SIZE (4 * 4)
5715 #define CMP_SIZE (3 * 4)
5716 #define BRANCH_SIZE (1 * 4)
5717 #define CALL_SIZE (2 * 4)
5718 #define WMC_SIZE (5 * 4)
5719 #define DISTANCE(A, B) (((gint32)(B)) - ((gint32)(A)))
5722 arm_emit_value_and_patch_ldr (arminstr_t *code, arminstr_t *target, guint32 value)
5724 guint32 delta = DISTANCE (target, code);
5726 g_assert (delta >= 0 && delta <= 0xFFF);
5727 *target = *target | delta;
5733 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
5734 gpointer fail_tramp)
5736 int size, i, extra_space = 0;
5737 arminstr_t *code, *start, *vtable_target = NULL;
5738 gboolean large_offsets = FALSE;
5739 guint32 **constant_pool_starts;
5742 constant_pool_starts = g_new0 (guint32*, count);
5744 for (i = 0; i < count; ++i) {
5745 MonoIMTCheckItem *item = imt_entries [i];
5746 if (item->is_equals) {
5747 gboolean fail_case = !item->check_target_idx && fail_tramp;
5749 if (item->has_target_code || !arm_is_imm12 (DISTANCE (vtable, &vtable->vtable[item->value.vtable_slot]))) {
5750 item->chunk_size += 32;
5751 large_offsets = TRUE;
5754 if (item->check_target_idx || fail_case) {
5755 if (!item->compare_done || fail_case)
5756 item->chunk_size += CMP_SIZE;
5757 item->chunk_size += BRANCH_SIZE;
5759 #if ENABLE_WRONG_METHOD_CHECK
5760 item->chunk_size += WMC_SIZE;
5764 item->chunk_size += 16;
5765 large_offsets = TRUE;
5767 item->chunk_size += CALL_SIZE;
5769 item->chunk_size += BSEARCH_ENTRY_SIZE;
5770 imt_entries [item->check_target_idx]->compare_done = TRUE;
5772 size += item->chunk_size;
5776 size += 4 * count; /* The ARM_ADD_REG_IMM to pop the stack */
5779 code = mono_method_alloc_generic_virtual_thunk (domain, size);
5781 code = mono_domain_code_reserve (domain, size);
5785 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);
5786 for (i = 0; i < count; ++i) {
5787 MonoIMTCheckItem *item = imt_entries [i];
5788 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);
5793 ARM_PUSH4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5795 ARM_PUSH2 (code, ARMREG_R0, ARMREG_R1);
5796 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_LR, -4);
5797 vtable_target = code;
5798 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
5800 if (mono_use_llvm) {
5801 /* LLVM always passes the IMT method in R5 */
5802 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_V5);
5804 /* R0 == 0 means we are called from AOT code. In this case, V5 contains the IMT method */
5805 ARM_CMP_REG_IMM8 (code, ARMREG_R0, 0);
5806 ARM_MOV_REG_REG_COND (code, ARMREG_R0, ARMREG_V5, ARMCOND_EQ);
5809 for (i = 0; i < count; ++i) {
5810 MonoIMTCheckItem *item = imt_entries [i];
5811 arminstr_t *imt_method = NULL, *vtable_offset_ins = NULL, *target_code_ins = NULL;
5812 gint32 vtable_offset;
5814 item->code_target = (guint8*)code;
5816 if (item->is_equals) {
5817 gboolean fail_case = !item->check_target_idx && fail_tramp;
5819 if (item->check_target_idx || fail_case) {
5820 if (!item->compare_done || fail_case) {
5822 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5823 ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
5825 item->jmp_code = (guint8*)code;
5826 ARM_B_COND (code, ARMCOND_NE, 0);
5828 /*Enable the commented code to assert on wrong method*/
5829 #if ENABLE_WRONG_METHOD_CHECK
5831 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5832 ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
5833 ARM_B_COND (code, ARMCOND_NE, 1);
5839 if (item->has_target_code) {
5840 target_code_ins = code;
5841 /* Load target address */
5842 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5843 /* Save it to the fourth slot */
5844 ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
5845 /* Restore registers and branch */
5846 ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5848 code = arm_emit_value_and_patch_ldr (code, target_code_ins, (gsize)item->value.target_code);
5850 vtable_offset = DISTANCE (vtable, &vtable->vtable[item->value.vtable_slot]);
5851 if (!arm_is_imm12 (vtable_offset)) {
5853 * We need to branch to a computed address but we don't have
5854 * a free register to store it, since IP must contain the
5855 * vtable address. So we push the two values to the stack, and
5856 * load them both using LDM.
5858 /* Compute target address */
5859 vtable_offset_ins = code;
5860 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5861 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_IP, ARMREG_R1);
5862 /* Save it to the fourth slot */
5863 ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
5864 /* Restore registers and branch */
5865 ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5867 code = arm_emit_value_and_patch_ldr (code, vtable_offset_ins, vtable_offset);
5869 ARM_POP2 (code, ARMREG_R0, ARMREG_R1);
5871 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 2 * sizeof (gpointer));
5872 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, vtable_offset);
5877 arm_patch (item->jmp_code, (guchar*)code);
5879 target_code_ins = code;
5880 /* Load target address */
5881 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5882 /* Save it to the fourth slot */
5883 ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
5884 /* Restore registers and branch */
5885 ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5887 code = arm_emit_value_and_patch_ldr (code, target_code_ins, (gsize)fail_tramp);
5888 item->jmp_code = NULL;
5892 code = arm_emit_value_and_patch_ldr (code, imt_method, (guint32)item->key);
5894 /*must emit after unconditional branch*/
5895 if (vtable_target) {
5896 code = arm_emit_value_and_patch_ldr (code, vtable_target, (guint32)vtable);
5897 item->chunk_size += 4;
5898 vtable_target = NULL;
5901 /*We reserve the space for bsearch IMT values after the first entry with an absolute jump*/
5902 constant_pool_starts [i] = code;
5904 code += extra_space;
5908 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5909 ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
5911 item->jmp_code = (guint8*)code;
5912 ARM_B_COND (code, ARMCOND_GE, 0);
5917 for (i = 0; i < count; ++i) {
5918 MonoIMTCheckItem *item = imt_entries [i];
5919 if (item->jmp_code) {
5920 if (item->check_target_idx)
5921 arm_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
5923 if (i > 0 && item->is_equals) {
5925 arminstr_t *space_start = constant_pool_starts [i];
5926 for (j = i - 1; j >= 0 && !imt_entries [j]->is_equals; --j) {
5927 space_start = arm_emit_value_and_patch_ldr (space_start, (arminstr_t*)imt_entries [j]->code_target, (guint32)imt_entries [j]->key);
5934 char *buff = g_strdup_printf ("thunk_for_class_%s_%s_entries_%d", vtable->klass->name_space, vtable->klass->name, count);
5935 mono_disassemble_code (NULL, (guint8*)start, size, buff);
5940 g_free (constant_pool_starts);
5942 mono_arch_flush_icache ((guint8*)start, size);
5943 mono_stats.imt_thunks_size += code - start;
5945 g_assert (DISTANCE (start, code) <= size);
5952 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
5954 return ctx->regs [reg];
5958 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
5960 ctx->regs [reg] = val;
5964 * mono_arch_get_trampolines:
5966 * Return a list of MonoTrampInfo structures describing arch specific trampolines
5970 mono_arch_get_trampolines (gboolean aot)
5972 return mono_arm_get_exception_trampolines (aot);
5976 * mono_arch_set_breakpoint:
5978 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
5979 * The location should contain code emitted by OP_SEQ_POINT.
5982 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
5985 guint32 native_offset = ip - (guint8*)ji->code_start;
5986 MonoDebugOptions *opt = mini_get_debug_options ();
5988 if (opt->soft_breakpoints) {
5989 g_assert (!ji->from_aot);
5991 ARM_BLX_REG (code, ARMREG_LR);
5992 mono_arch_flush_icache (code - 4, 4);
5993 } else if (ji->from_aot) {
5994 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
5996 g_assert (native_offset % 4 == 0);
5997 g_assert (info->bp_addrs [native_offset / 4] == 0);
5998 info->bp_addrs [native_offset / 4] = bp_trigger_page;
6000 int dreg = ARMREG_LR;
6002 /* Read from another trigger page */
6003 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
6005 *(int*)code = (int)bp_trigger_page;
6007 ARM_LDR_IMM (code, dreg, dreg, 0);
6009 mono_arch_flush_icache (code - 16, 16);
6012 /* This is currently implemented by emitting an SWI instruction, which
6013 * qemu/linux seems to convert to a SIGILL.
6015 *(int*)code = (0xef << 24) | 8;
6017 mono_arch_flush_icache (code - 4, 4);
6023 * mono_arch_clear_breakpoint:
6025 * Clear the breakpoint at IP.
6028 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
6030 MonoDebugOptions *opt = mini_get_debug_options ();
6034 if (opt->soft_breakpoints) {
6035 g_assert (!ji->from_aot);
6038 mono_arch_flush_icache (code - 4, 4);
6039 } else if (ji->from_aot) {
6040 guint32 native_offset = ip - (guint8*)ji->code_start;
6041 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
6043 g_assert (native_offset % 4 == 0);
6044 g_assert (info->bp_addrs [native_offset / 4] == bp_trigger_page);
6045 info->bp_addrs [native_offset / 4] = 0;
6047 for (i = 0; i < 4; ++i)
6050 mono_arch_flush_icache (ip, code - ip);
6055 * mono_arch_start_single_stepping:
6057 * Start single stepping.
6060 mono_arch_start_single_stepping (void)
6062 if (ss_trigger_page)
6063 mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
6069 * mono_arch_stop_single_stepping:
6071 * Stop single stepping.
6074 mono_arch_stop_single_stepping (void)
6076 if (ss_trigger_page)
6077 mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
6083 #define DBG_SIGNAL SIGBUS
6085 #define DBG_SIGNAL SIGSEGV
6089 * mono_arch_is_single_step_event:
6091 * Return whenever the machine state in SIGCTX corresponds to a single
6095 mono_arch_is_single_step_event (void *info, void *sigctx)
6097 siginfo_t *sinfo = info;
6099 if (!ss_trigger_page)
6102 /* Sometimes the address is off by 4 */
6103 if (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128)
6110 * mono_arch_is_breakpoint_event:
6112 * Return whenever the machine state in SIGCTX corresponds to a breakpoint event.
6115 mono_arch_is_breakpoint_event (void *info, void *sigctx)
6117 siginfo_t *sinfo = info;
6119 if (!ss_trigger_page)
6122 if (sinfo->si_signo == DBG_SIGNAL) {
6123 /* Sometimes the address is off by 4 */
6124 if (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128)
6134 * mono_arch_skip_breakpoint:
6136 * See mini-amd64.c for docs.
6139 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
6141 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 4);
6145 * mono_arch_skip_single_step:
6147 * See mini-amd64.c for docs.
6150 mono_arch_skip_single_step (MonoContext *ctx)
6152 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 4);
6156 * mono_arch_get_seq_point_info:
6158 * See mini-amd64.c for docs.
6161 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
6166 // FIXME: Add a free function
6168 mono_domain_lock (domain);
6169 info = g_hash_table_lookup (domain_jit_info (domain)->arch_seq_points,
6171 mono_domain_unlock (domain);
6174 ji = mono_jit_info_table_find (domain, (char*)code);
6177 info = g_malloc0 (sizeof (SeqPointInfo) + ji->code_size);
6179 info->ss_trigger_page = ss_trigger_page;
6180 info->bp_trigger_page = bp_trigger_page;
6182 mono_domain_lock (domain);
6183 g_hash_table_insert (domain_jit_info (domain)->arch_seq_points,
6185 mono_domain_unlock (domain);
6192 * mono_arch_set_target:
6194 * Set the target architecture the JIT backend should generate code for, in the form
6195 * of a GNU target triplet. Only used in AOT mode.
6198 mono_arch_set_target (char *mtriple)
6200 /* The GNU target triple format is not very well documented */
6201 if (strstr (mtriple, "armv7")) {
6202 v6_supported = TRUE;
6203 v7_supported = TRUE;
6205 if (strstr (mtriple, "armv6")) {
6206 v6_supported = TRUE;
6208 if (strstr (mtriple, "darwin")) {
6209 v5_supported = TRUE;
6210 thumb_supported = TRUE;
6214 if (strstr (mtriple, "gnueabi"))
6215 eabi_supported = TRUE;