2 * mini-arm.c: ARM backend for the Mono code generator
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2003 Ximian, Inc.
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/debug-helpers.h>
20 #include "mono/arch/arm/arm-fpa-codegen.h"
24 * floating point support: on ARM it is a mess, there are at least 3
25 * different setups, each of which binary incompat with the other.
26 * 1) FPA: old and ugly, but unfortunately what current distros use
27 * the double binary format has the two words swapped. 8 double registers.
28 * Implemented usually by kernel emulation.
29 * 2) softfloat: the compiler emulates all the fp ops. Usually uses the
30 * ugly swapped double format (I guess a softfloat-vfp exists, too, though).
31 * 3) VFP: the new and actually sensible and useful FP support. Implemented
32 * in HW or kernel-emulated, requires new tools. I think this ios what symbian uses.
34 * The plan is to write the FPA support first. softfloat can be tested in a chroot.
36 int mono_exc_esp_offset = 0;
38 #define arm_is_imm12(v) ((v) > -4096 && (v) < 4096)
39 #define arm_is_imm8(v) ((v) > -256 && (v) < 256)
40 #define arm_is_fpimm8(v) ((v) >= -1020 && (v) <= 1020)
43 mono_arch_regname (int reg) {
44 static const char * rnames[] = {
45 "arm_r0", "arm_r1", "arm_r2", "arm_r3", "arm_v1",
46 "arm_v2", "arm_v3", "arm_v4", "arm_v5", "arm_v6",
47 "arm_v7", "arm_fp", "arm_ip", "arm_sp", "arm_lr",
50 if (reg >= 0 && reg < 16)
56 mono_arch_fregname (int reg) {
57 static const char * rnames[] = {
58 "arm_f0", "arm_f1", "arm_f2", "arm_f3", "arm_f4",
59 "arm_f5", "arm_f6", "arm_f7", "arm_f8", "arm_f9",
60 "arm_f10", "arm_f11", "arm_f12", "arm_f13", "arm_f14",
61 "arm_f15", "arm_f16", "arm_f17", "arm_f18", "arm_f19",
62 "arm_f20", "arm_f21", "arm_f22", "arm_f23", "arm_f24",
63 "arm_f25", "arm_f26", "arm_f27", "arm_f28", "arm_f29",
66 if (reg >= 0 && reg < 32)
72 emit_big_add (guint8 *code, int dreg, int sreg, int imm)
75 if ((imm8 = mono_arm_is_rotated_imm8 (imm, &rot_amount)) >= 0) {
76 ARM_ADD_REG_IMM (code, dreg, sreg, imm8, rot_amount);
79 g_assert (dreg != sreg);
80 code = mono_arm_emit_load_imm (code, dreg, imm);
81 ARM_ADD_REG_REG (code, dreg, dreg, sreg);
86 emit_memcpy (guint8 *code, int size, int dreg, int doffset, int sreg, int soffset)
88 /* we can use r0-r3, since this is called only for incoming args on the stack */
89 if (size > sizeof (gpointer) * 4) {
91 code = emit_big_add (code, ARMREG_R0, sreg, soffset);
92 code = emit_big_add (code, ARMREG_R1, dreg, doffset);
93 start_loop = code = mono_arm_emit_load_imm (code, ARMREG_R2, size);
94 ARM_LDR_IMM (code, ARMREG_R3, ARMREG_R0, 0);
95 ARM_STR_IMM (code, ARMREG_R3, ARMREG_R1, 0);
96 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, 4);
97 ARM_ADD_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, 4);
98 ARM_SUBS_REG_IMM8 (code, ARMREG_R2, ARMREG_R2, 4);
99 ARM_B_COND (code, ARMCOND_NE, 0);
100 arm_patch (code - 4, start_loop);
103 if (arm_is_imm12 (doffset) && arm_is_imm12 (doffset + size) &&
104 arm_is_imm12 (soffset) && arm_is_imm12 (soffset + size)) {
106 ARM_LDR_IMM (code, ARMREG_LR, sreg, soffset);
107 ARM_STR_IMM (code, ARMREG_LR, dreg, doffset);
113 code = emit_big_add (code, ARMREG_R0, sreg, soffset);
114 code = emit_big_add (code, ARMREG_R1, dreg, doffset);
115 doffset = soffset = 0;
117 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_R0, soffset);
118 ARM_STR_IMM (code, ARMREG_LR, ARMREG_R1, doffset);
124 g_assert (size == 0);
129 * mono_arch_get_argument_info:
130 * @csig: a method signature
131 * @param_count: the number of parameters to consider
132 * @arg_info: an array to store the result infos
134 * Gathers information on parameters such as size, alignment and
135 * padding. arg_info should be large enought to hold param_count + 1 entries.
137 * Returns the size of the activation frame.
140 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
142 int k, frame_size = 0;
143 int size, align, pad;
146 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
147 frame_size += sizeof (gpointer);
151 arg_info [0].offset = offset;
154 frame_size += sizeof (gpointer);
158 arg_info [0].size = frame_size;
160 for (k = 0; k < param_count; k++) {
163 size = mono_type_native_stack_size (csig->params [k], &align);
165 size = mono_type_stack_size (csig->params [k], &align);
167 /* ignore alignment for now */
170 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
171 arg_info [k].pad = pad;
173 arg_info [k + 1].pad = 0;
174 arg_info [k + 1].size = size;
176 arg_info [k + 1].offset = offset;
180 align = MONO_ARCH_FRAME_ALIGNMENT;
181 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
182 arg_info [k].pad = pad;
188 * Initialize the cpu to execute managed code.
191 mono_arch_cpu_init (void)
196 * This function returns the optimizations supported on this cpu.
199 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
203 /* no arm-specific optimizations yet */
209 is_regsize_var (MonoType *t) {
212 t = mono_type_get_underlying_type (t);
219 case MONO_TYPE_FNPTR:
221 case MONO_TYPE_OBJECT:
222 case MONO_TYPE_STRING:
223 case MONO_TYPE_CLASS:
224 case MONO_TYPE_SZARRAY:
225 case MONO_TYPE_ARRAY:
227 case MONO_TYPE_GENERICINST:
228 if (!mono_type_generic_inst_is_valuetype (t))
231 case MONO_TYPE_VALUETYPE:
238 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
243 for (i = 0; i < cfg->num_varinfo; i++) {
244 MonoInst *ins = cfg->varinfo [i];
245 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
248 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
251 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
254 /* we can only allocate 32 bit values */
255 if (is_regsize_var (ins->inst_vtype)) {
256 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
257 g_assert (i == vmv->idx);
258 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
265 #define USE_EXTRA_TEMPS 0
268 mono_arch_get_global_int_regs (MonoCompile *cfg)
271 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V1));
272 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V2));
273 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V3));
274 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V4));
275 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V5));
276 /*regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V6));*/
277 /*regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V7));*/
283 * mono_arch_regalloc_cost:
285 * Return the cost, in number of memory references, of the action of
286 * allocating the variable VMV into a register during global register
290 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
297 mono_arch_flush_icache (guint8 *code, gint size)
299 __asm __volatile ("mov r0, %0\n"
302 "swi 0x9f0002 @ sys_cacheflush"
304 : "r" (code), "r" (code + size), "r" (0)
305 : "r0", "r1", "r3" );
309 #define NOT_IMPLEMENTED(x) \
310 g_error ("FIXME: %s is not yet implemented. (trampoline)", x);
322 guint16 vtsize; /* in param area */
324 guint8 regtype : 4; /* 0 general, 1 basereg, 2 floating point register, see RegType* */
325 guint8 size : 4; /* 1, 2, 4, 8, or regs used by RegTypeStructByVal */
340 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
343 if (*gr > ARMREG_R3) {
344 ainfo->offset = *stack_size;
345 ainfo->reg = ARMREG_SP; /* in the caller */
346 ainfo->regtype = RegTypeBase;
352 if (*gr > ARMREG_R2) {
355 ainfo->offset = *stack_size;
356 ainfo->reg = ARMREG_SP; /* in the caller */
357 ainfo->regtype = RegTypeBase;
370 calculate_sizes (MonoMethodSignature *sig, gboolean is_pinvoke)
373 int n = sig->hasthis + sig->param_count;
375 guint32 stack_size = 0;
376 CallInfo *cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * n);
380 /* FIXME: handle returning a struct */
381 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
382 add_general (&gr, &stack_size, &cinfo->ret, TRUE);
383 cinfo->struct_ret = ARMREG_R0;
388 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
391 DEBUG(printf("params: %d\n", sig->param_count));
392 for (i = 0; i < sig->param_count; ++i) {
393 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
394 /* Prevent implicit arguments and sig_cookie from
395 being passed in registers */
397 /* Emit the signature cookie just before the implicit arguments */
398 add_general (&gr, &stack_size, &cinfo->sig_cookie, TRUE);
400 DEBUG(printf("param %d: ", i));
401 if (sig->params [i]->byref) {
402 DEBUG(printf("byref\n"));
403 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
407 simpletype = mono_type_get_underlying_type (sig->params [i])->type;
408 switch (simpletype) {
409 case MONO_TYPE_BOOLEAN:
412 cinfo->args [n].size = 1;
413 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
419 cinfo->args [n].size = 2;
420 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
425 cinfo->args [n].size = 4;
426 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
432 case MONO_TYPE_FNPTR:
433 case MONO_TYPE_CLASS:
434 case MONO_TYPE_OBJECT:
435 case MONO_TYPE_STRING:
436 case MONO_TYPE_SZARRAY:
437 case MONO_TYPE_ARRAY:
439 cinfo->args [n].size = sizeof (gpointer);
440 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
443 case MONO_TYPE_GENERICINST:
444 if (!mono_type_generic_inst_is_valuetype (sig->params [i])) {
445 cinfo->args [n].size = sizeof (gpointer);
446 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
451 case MONO_TYPE_TYPEDBYREF:
452 case MONO_TYPE_VALUETYPE: {
457 if (simpletype == MONO_TYPE_TYPEDBYREF) {
458 size = sizeof (MonoTypedRef);
460 MonoClass *klass = mono_class_from_mono_type (sig->params [i]);
462 size = mono_class_native_size (klass, NULL);
464 size = mono_class_value_size (klass, NULL);
466 DEBUG(printf ("load %d bytes struct\n",
467 mono_class_native_size (sig->params [i]->data.klass, NULL)));
470 align_size += (sizeof (gpointer) - 1);
471 align_size &= ~(sizeof (gpointer) - 1);
472 nwords = (align_size + sizeof (gpointer) -1 ) / sizeof (gpointer);
473 cinfo->args [n].regtype = RegTypeStructByVal;
474 /* FIXME: align gr and stack_size if needed */
475 if (gr > ARMREG_R3) {
476 cinfo->args [n].size = 0;
477 cinfo->args [n].vtsize = nwords;
479 int rest = ARMREG_R3 - gr + 1;
480 int n_in_regs = rest >= nwords? nwords: rest;
481 cinfo->args [n].size = n_in_regs;
482 cinfo->args [n].vtsize = nwords - n_in_regs;
483 cinfo->args [n].reg = gr;
486 cinfo->args [n].offset = stack_size;
487 /*g_print ("offset for arg %d at %d\n", n, stack_size);*/
488 stack_size += nwords * sizeof (gpointer);
495 cinfo->args [n].size = 8;
496 add_general (&gr, &stack_size, cinfo->args + n, FALSE);
500 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
505 simpletype = mono_type_get_underlying_type (sig->ret)->type;
506 switch (simpletype) {
507 case MONO_TYPE_BOOLEAN:
518 case MONO_TYPE_FNPTR:
519 case MONO_TYPE_CLASS:
520 case MONO_TYPE_OBJECT:
521 case MONO_TYPE_SZARRAY:
522 case MONO_TYPE_ARRAY:
523 case MONO_TYPE_STRING:
524 cinfo->ret.reg = ARMREG_R0;
528 cinfo->ret.reg = ARMREG_R0;
532 cinfo->ret.reg = ARMREG_R0;
533 /* FIXME: cinfo->ret.reg = ???;
534 cinfo->ret.regtype = RegTypeFP;*/
536 case MONO_TYPE_GENERICINST:
537 if (!mono_type_generic_inst_is_valuetype (sig->ret)) {
538 cinfo->ret.reg = ARMREG_R0;
542 case MONO_TYPE_VALUETYPE:
544 case MONO_TYPE_TYPEDBYREF:
548 g_error ("Can't handle as return value 0x%x", sig->ret->type);
552 /* align stack size to 8 */
553 DEBUG (printf (" stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
554 stack_size = (stack_size + 7) & ~7;
556 cinfo->stack_usage = stack_size;
562 * Set var information according to the calling convention. arm version.
563 * The locals var stuff should most likely be split in another method.
566 mono_arch_allocate_vars (MonoCompile *m)
568 MonoMethodSignature *sig;
569 MonoMethodHeader *header;
571 int i, offset, size, align, curinst;
572 int frame_reg = ARMREG_FP;
574 /* FIXME: this will change when we use FP as gcc does */
575 m->flags |= MONO_CFG_HAS_SPILLUP;
577 /* allow room for the vararg method args: void* and long/double */
578 if (mono_jit_trace_calls != NULL && mono_trace_eval (m->method))
579 m->param_area = MAX (m->param_area, sizeof (gpointer)*8);
581 header = mono_method_get_header (m->method);
584 * We use the frame register also for any method that has
585 * exception clauses. This way, when the handlers are called,
586 * the code will reference local variables using the frame reg instead of
587 * the stack pointer: if we had to restore the stack pointer, we'd
588 * corrupt the method frames that are already on the stack (since
589 * filters get called before stack unwinding happens) when the filter
590 * code would call any method (this also applies to finally etc.).
592 if ((m->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
593 frame_reg = ARMREG_FP;
594 m->frame_reg = frame_reg;
595 if (frame_reg != ARMREG_SP) {
596 m->used_int_regs |= 1 << frame_reg;
599 sig = mono_method_signature (m->method);
603 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
604 m->ret->opcode = OP_REGVAR;
605 m->ret->inst_c0 = ARMREG_R0;
607 /* FIXME: handle long and FP values */
608 switch (mono_type_get_underlying_type (sig->ret)->type) {
612 m->ret->opcode = OP_REGVAR;
613 m->ret->inst_c0 = ARMREG_R0;
617 /* local vars are at a positive offset from the stack pointer */
619 * also note that if the function uses alloca, we use FP
620 * to point at the local variables.
622 offset = 0; /* linkage area */
623 /* align the offset to 16 bytes: not sure this is needed here */
625 //offset &= ~(8 - 1);
627 /* add parameter area size for called functions */
628 offset += m->param_area;
631 if (m->flags & MONO_CFG_HAS_FPOUT)
634 /* allow room to save the return value */
635 if (mono_jit_trace_calls != NULL && mono_trace_eval (m->method))
638 /* the MonoLMF structure is stored just below the stack pointer */
640 if (sig->call_convention == MONO_CALL_VARARG) {
644 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
646 offset += sizeof(gpointer) - 1;
647 offset &= ~(sizeof(gpointer) - 1);
648 inst->inst_offset = offset;
649 inst->opcode = OP_REGOFFSET;
650 inst->inst_basereg = frame_reg;
651 offset += sizeof(gpointer);
652 if (sig->call_convention == MONO_CALL_VARARG)
653 m->sig_cookie += sizeof (gpointer);
656 curinst = m->locals_start;
657 for (i = curinst; i < m->num_varinfo; ++i) {
658 inst = m->varinfo [i];
659 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR)
662 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
663 * pinvoke wrappers when they call functions returning structure */
664 if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
665 size = mono_class_native_size (mono_class_from_mono_type (inst->inst_vtype), &align);
667 size = mono_type_size (inst->inst_vtype, &align);
669 /* FIXME: if a structure is misaligned, our memcpy doesn't work,
670 * since it loads/stores misaligned words, which don't do the right thing.
672 if (align < 4 && size >= 4)
675 offset &= ~(align - 1);
676 inst->inst_offset = offset;
677 inst->opcode = OP_REGOFFSET;
678 inst->inst_basereg = frame_reg;
680 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
685 inst = m->varinfo [curinst];
686 if (inst->opcode != OP_REGVAR) {
687 inst->opcode = OP_REGOFFSET;
688 inst->inst_basereg = frame_reg;
689 offset += sizeof (gpointer) - 1;
690 offset &= ~(sizeof (gpointer) - 1);
691 inst->inst_offset = offset;
692 offset += sizeof (gpointer);
693 if (sig->call_convention == MONO_CALL_VARARG)
694 m->sig_cookie += sizeof (gpointer);
699 for (i = 0; i < sig->param_count; ++i) {
700 inst = m->varinfo [curinst];
701 if (inst->opcode != OP_REGVAR) {
702 inst->opcode = OP_REGOFFSET;
703 inst->inst_basereg = frame_reg;
704 size = mono_type_size (sig->params [i], &align);
705 /* FIXME: if a structure is misaligned, our memcpy doesn't work,
706 * since it loads/stores misaligned words, which don't do the right thing.
708 if (align < 4 && size >= 4)
711 offset &= ~(align - 1);
712 inst->inst_offset = offset;
714 if ((sig->call_convention == MONO_CALL_VARARG) && (i < sig->sentinelpos))
715 m->sig_cookie += size;
720 /* align the offset to 8 bytes */
725 m->stack_offset = offset;
729 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
730 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
734 * take the arguments and generate the arch-specific
735 * instructions to properly call the function in call.
736 * This includes pushing, moving arguments to the right register
738 * Issue: who does the spilling if needed, and when?
741 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
743 MonoMethodSignature *sig;
748 sig = call->signature;
749 n = sig->param_count + sig->hasthis;
751 cinfo = calculate_sizes (sig, sig->pinvoke);
752 if (cinfo->struct_ret)
753 call->used_iregs |= 1 << cinfo->struct_ret;
755 for (i = 0; i < n; ++i) {
756 ainfo = cinfo->args + i;
757 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
759 cfg->disable_aot = TRUE;
761 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
762 sig_arg->inst_p0 = call->signature;
764 MONO_INST_NEW (cfg, arg, OP_OUTARG);
765 arg->inst_imm = cinfo->sig_cookie.offset;
766 arg->inst_left = sig_arg;
768 /* prepend, so they get reversed */
769 arg->next = call->out_args;
770 call->out_args = arg;
772 if (is_virtual && i == 0) {
773 /* the argument will be attached to the call instrucion */
775 call->used_iregs |= 1 << ainfo->reg;
777 MONO_INST_NEW (cfg, arg, OP_OUTARG);
779 arg->cil_code = in->cil_code;
781 arg->inst_right = (MonoInst*)call;
782 arg->type = in->type;
783 /* prepend, we'll need to reverse them later */
784 arg->next = call->out_args;
785 call->out_args = arg;
786 if (ainfo->regtype == RegTypeGeneral) {
787 arg->backend.reg3 = ainfo->reg;
788 call->used_iregs |= 1 << ainfo->reg;
789 if (arg->type == STACK_I8)
790 call->used_iregs |= 1 << (ainfo->reg + 1);
791 if (arg->type == STACK_R8) {
792 if (ainfo->size == 4) {
793 arg->opcode = OP_OUTARG_R4;
795 call->used_iregs |= 1 << (ainfo->reg + 1);
797 cfg->flags |= MONO_CFG_HAS_FPOUT;
799 } else if (ainfo->regtype == RegTypeStructByAddr) {
800 /* FIXME: where si the data allocated? */
801 arg->backend.reg3 = ainfo->reg;
802 call->used_iregs |= 1 << ainfo->reg;
803 g_assert_not_reached ();
804 } else if (ainfo->regtype == RegTypeStructByVal) {
806 /* mark the used regs */
807 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
808 call->used_iregs |= 1 << (ainfo->reg + cur_reg);
810 arg->opcode = OP_OUTARG_VT;
811 /* vtsize and offset have just 12 bits of encoding in number of words */
812 g_assert (((ainfo->vtsize | (ainfo->offset / 4)) & 0xfffff000) == 0);
813 arg->backend.arg_info = ainfo->reg | (ainfo->size << 4) | (ainfo->vtsize << 8) | ((ainfo->offset / 4) << 20);
814 } else if (ainfo->regtype == RegTypeBase) {
815 arg->opcode = OP_OUTARG_MEMBASE;
816 arg->backend.arg_info = (ainfo->offset << 8) | ainfo->size;
817 } else if (ainfo->regtype == RegTypeFP) {
818 arg->backend.reg3 = ainfo->reg;
819 /* FPA args are passed in int regs */
820 call->used_iregs |= 1 << ainfo->reg;
821 if (ainfo->size == 8) {
822 arg->opcode = OP_OUTARG_R8;
823 call->used_iregs |= 1 << (ainfo->reg + 1);
825 arg->opcode = OP_OUTARG_R4;
827 cfg->flags |= MONO_CFG_HAS_FPOUT;
829 g_assert_not_reached ();
834 * Reverse the call->out_args list.
837 MonoInst *prev = NULL, *list = call->out_args, *next;
844 call->out_args = prev;
846 call->stack_usage = cinfo->stack_usage;
847 cfg->param_area = MAX (cfg->param_area, cinfo->stack_usage);
848 cfg->flags |= MONO_CFG_HAS_CALLS;
850 * should set more info in call, such as the stack space
851 * used by the args that needs to be added back to esp
859 * Allow tracing to work with this interface (with an optional argument)
863 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
867 code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->method);
868 ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0); /* NULL ebp for now */
869 code = mono_arm_emit_load_imm (code, ARMREG_R2, (guint32)func);
870 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
871 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R2);
884 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
887 int save_mode = SAVE_NONE;
889 MonoMethod *method = cfg->method;
890 int rtype = mono_type_get_underlying_type (mono_method_signature (method)->ret)->type;
891 int save_offset = cfg->param_area;
895 offset = code - cfg->native_code;
896 /* we need about 16 instructions */
897 if (offset > (cfg->code_size - 16 * 4)) {
899 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
900 code = cfg->native_code + offset;
905 /* special case string .ctor icall */
906 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
907 save_mode = SAVE_ONE;
909 save_mode = SAVE_NONE;
913 save_mode = SAVE_TWO;
919 case MONO_TYPE_VALUETYPE:
920 save_mode = SAVE_STRUCT;
923 save_mode = SAVE_ONE;
929 ARM_STR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
930 ARM_STR_IMM (code, ARMREG_R1, cfg->frame_reg, save_offset + 4);
931 if (enable_arguments) {
932 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_R1);
933 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
937 ARM_STR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
938 if (enable_arguments) {
939 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
943 /* FIXME: what reg? */
944 if (enable_arguments) {
945 /* FIXME: what reg? */
949 if (enable_arguments) {
950 /* FIXME: get the actual address */
951 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
959 code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->method);
960 code = mono_arm_emit_load_imm (code, ARMREG_IP, (guint32)func);
961 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
962 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
966 ARM_LDR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
967 ARM_LDR_IMM (code, ARMREG_R1, cfg->frame_reg, save_offset + 4);
970 ARM_LDR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
984 * The immediate field for cond branches is big enough for all reasonable methods
986 #define EMIT_COND_BRANCH_FLAGS(ins,condcode) \
987 if (ins->flags & MONO_INST_BRLABEL) { \
988 if (0 && ins->inst_i0->inst_c0) { \
989 ARM_B_COND (code, (condcode), (code - cfg->native_code + ins->inst_i0->inst_c0) & 0xffffff); \
991 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
992 ARM_B_COND (code, (condcode), 0); \
995 if (0 && ins->inst_true_bb->native_offset) { \
996 ARM_B_COND (code, (condcode), (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffffff); \
998 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
999 ARM_B_COND (code, (condcode), 0); \
1003 #define EMIT_COND_BRANCH(ins,cond) EMIT_COND_BRANCH_FLAGS(ins, branch_cc_table [(cond)])
1005 /* emit an exception if condition is fail
1007 * We assign the extra code used to throw the implicit exceptions
1008 * to cfg->bb_exit as far as the big branch handling is concerned
1010 #define EMIT_COND_SYSTEM_EXCEPTION_FLAGS(condcode,exc_name) \
1012 mono_add_patch_info (cfg, code - cfg->native_code, \
1013 MONO_PATCH_INFO_EXC, exc_name); \
1014 ARM_BL_COND (code, (condcode), 0); \
1017 #define EMIT_COND_SYSTEM_EXCEPTION(cond,exc_name) EMIT_COND_SYSTEM_EXCEPTION_FLAGS(branch_cc_table [(cond)], (exc_name))
1020 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1022 MonoInst *ins, *last_ins = NULL;
1027 switch (ins->opcode) {
1029 /* remove unnecessary multiplication with 1 */
1030 if (ins->inst_imm == 1) {
1031 if (ins->dreg != ins->sreg1) {
1032 ins->opcode = OP_MOVE;
1034 last_ins->next = ins->next;
1039 int power2 = mono_is_power_of_two (ins->inst_imm);
1041 ins->opcode = OP_SHL_IMM;
1042 ins->inst_imm = power2;
1046 case OP_LOAD_MEMBASE:
1047 case OP_LOADI4_MEMBASE:
1049 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1050 * OP_LOAD_MEMBASE offset(basereg), reg
1052 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1053 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1054 ins->inst_basereg == last_ins->inst_destbasereg &&
1055 ins->inst_offset == last_ins->inst_offset) {
1056 if (ins->dreg == last_ins->sreg1) {
1057 last_ins->next = ins->next;
1061 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1062 ins->opcode = OP_MOVE;
1063 ins->sreg1 = last_ins->sreg1;
1067 * Note: reg1 must be different from the basereg in the second load
1068 * OP_LOAD_MEMBASE offset(basereg), reg1
1069 * OP_LOAD_MEMBASE offset(basereg), reg2
1071 * OP_LOAD_MEMBASE offset(basereg), reg1
1072 * OP_MOVE reg1, reg2
1074 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1075 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1076 ins->inst_basereg != last_ins->dreg &&
1077 ins->inst_basereg == last_ins->inst_basereg &&
1078 ins->inst_offset == last_ins->inst_offset) {
1080 if (ins->dreg == last_ins->dreg) {
1081 last_ins->next = ins->next;
1085 ins->opcode = OP_MOVE;
1086 ins->sreg1 = last_ins->dreg;
1089 //g_assert_not_reached ();
1093 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1094 * OP_LOAD_MEMBASE offset(basereg), reg
1096 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1097 * OP_ICONST reg, imm
1099 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1100 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1101 ins->inst_basereg == last_ins->inst_destbasereg &&
1102 ins->inst_offset == last_ins->inst_offset) {
1103 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1104 ins->opcode = OP_ICONST;
1105 ins->inst_c0 = last_ins->inst_imm;
1106 g_assert_not_reached (); // check this rule
1110 case OP_LOADU1_MEMBASE:
1111 case OP_LOADI1_MEMBASE:
1112 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1113 ins->inst_basereg == last_ins->inst_destbasereg &&
1114 ins->inst_offset == last_ins->inst_offset) {
1115 if (ins->dreg == last_ins->sreg1) {
1116 last_ins->next = ins->next;
1120 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1121 ins->opcode = OP_MOVE;
1122 ins->sreg1 = last_ins->sreg1;
1126 case OP_LOADU2_MEMBASE:
1127 case OP_LOADI2_MEMBASE:
1128 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1129 ins->inst_basereg == last_ins->inst_destbasereg &&
1130 ins->inst_offset == last_ins->inst_offset) {
1131 if (ins->dreg == last_ins->sreg1) {
1132 last_ins->next = ins->next;
1136 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1137 ins->opcode = OP_MOVE;
1138 ins->sreg1 = last_ins->sreg1;
1146 ins->opcode = OP_MOVE;
1150 if (ins->dreg == ins->sreg1) {
1152 last_ins->next = ins->next;
1157 * OP_MOVE sreg, dreg
1158 * OP_MOVE dreg, sreg
1160 if (last_ins && last_ins->opcode == OP_MOVE &&
1161 ins->sreg1 == last_ins->dreg &&
1162 ins->dreg == last_ins->sreg1) {
1163 last_ins->next = ins->next;
1172 bb->last_ins = last_ins;
1176 * the branch_cc_table should maintain the order of these
1190 branch_cc_table [] = {
1206 insert_after_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst *to_insert)
1210 bb->code = to_insert;
1211 to_insert->next = ins;
1213 to_insert->next = ins->next;
1214 ins->next = to_insert;
1218 #define NEW_INS(cfg,dest,op) do { \
1219 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
1220 (dest)->opcode = (op); \
1221 insert_after_ins (bb, last_ins, (dest)); \
1225 map_to_reg_reg_op (int op)
1234 case OP_COMPARE_IMM:
1248 case OP_LOAD_MEMBASE:
1249 return OP_LOAD_MEMINDEX;
1250 case OP_LOADI4_MEMBASE:
1251 return OP_LOADI4_MEMINDEX;
1252 case OP_LOADU4_MEMBASE:
1253 return OP_LOADU4_MEMINDEX;
1254 case OP_LOADU1_MEMBASE:
1255 return OP_LOADU1_MEMINDEX;
1256 case OP_LOADI2_MEMBASE:
1257 return OP_LOADI2_MEMINDEX;
1258 case OP_LOADU2_MEMBASE:
1259 return OP_LOADU2_MEMINDEX;
1260 case OP_LOADI1_MEMBASE:
1261 return OP_LOADI1_MEMINDEX;
1262 case OP_STOREI1_MEMBASE_REG:
1263 return OP_STOREI1_MEMINDEX;
1264 case OP_STOREI2_MEMBASE_REG:
1265 return OP_STOREI2_MEMINDEX;
1266 case OP_STOREI4_MEMBASE_REG:
1267 return OP_STOREI4_MEMINDEX;
1268 case OP_STORE_MEMBASE_REG:
1269 return OP_STORE_MEMINDEX;
1270 case OP_STORER4_MEMBASE_REG:
1271 return OP_STORER4_MEMINDEX;
1272 case OP_STORER8_MEMBASE_REG:
1273 return OP_STORER8_MEMINDEX;
1274 case OP_STORE_MEMBASE_IMM:
1275 return OP_STORE_MEMBASE_REG;
1276 case OP_STOREI1_MEMBASE_IMM:
1277 return OP_STOREI1_MEMBASE_REG;
1278 case OP_STOREI2_MEMBASE_IMM:
1279 return OP_STOREI2_MEMBASE_REG;
1280 case OP_STOREI4_MEMBASE_IMM:
1281 return OP_STOREI4_MEMBASE_REG;
1283 g_assert_not_reached ();
1287 * Remove from the instruction list the instructions that can't be
1288 * represented with very simple instructions with no register
1292 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1294 MonoInst *ins, *next, *temp, *last_ins = NULL;
1295 int rot_amount, imm8, low_imm;
1297 /* setup the virtual reg allocator */
1298 if (bb->max_ireg > cfg->rs->next_vireg)
1299 cfg->rs->next_vireg = bb->max_ireg;
1304 switch (ins->opcode) {
1308 case OP_COMPARE_IMM:
1315 if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount)) < 0) {
1316 NEW_INS (cfg, temp, OP_ICONST);
1317 temp->inst_c0 = ins->inst_imm;
1318 temp->dreg = mono_regstate_next_int (cfg->rs);
1319 ins->sreg2 = temp->dreg;
1320 ins->opcode = map_to_reg_reg_op (ins->opcode);
1324 if (ins->inst_imm == 1) {
1325 ins->opcode = OP_MOVE;
1328 if (ins->inst_imm == 0) {
1329 ins->opcode = OP_ICONST;
1333 imm8 = mono_is_power_of_two (ins->inst_imm);
1335 ins->opcode = OP_SHL_IMM;
1336 ins->inst_imm = imm8;
1339 NEW_INS (cfg, temp, OP_ICONST);
1340 temp->inst_c0 = ins->inst_imm;
1341 temp->dreg = mono_regstate_next_int (cfg->rs);
1342 ins->sreg2 = temp->dreg;
1343 ins->opcode = CEE_MUL;
1345 case OP_LOAD_MEMBASE:
1346 case OP_LOADI4_MEMBASE:
1347 case OP_LOADU4_MEMBASE:
1348 case OP_LOADU1_MEMBASE:
1349 /* we can do two things: load the immed in a register
1350 * and use an indexed load, or see if the immed can be
1351 * represented as an ad_imm + a load with a smaller offset
1352 * that fits. We just do the first for now, optimize later.
1354 if (arm_is_imm12 (ins->inst_offset))
1356 NEW_INS (cfg, temp, OP_ICONST);
1357 temp->inst_c0 = ins->inst_offset;
1358 temp->dreg = mono_regstate_next_int (cfg->rs);
1359 ins->sreg2 = temp->dreg;
1360 ins->opcode = map_to_reg_reg_op (ins->opcode);
1362 case OP_LOADI2_MEMBASE:
1363 case OP_LOADU2_MEMBASE:
1364 case OP_LOADI1_MEMBASE:
1365 if (arm_is_imm8 (ins->inst_offset))
1367 NEW_INS (cfg, temp, OP_ICONST);
1368 temp->inst_c0 = ins->inst_offset;
1369 temp->dreg = mono_regstate_next_int (cfg->rs);
1370 ins->sreg2 = temp->dreg;
1371 ins->opcode = map_to_reg_reg_op (ins->opcode);
1373 case OP_LOADR4_MEMBASE:
1374 case OP_LOADR8_MEMBASE:
1375 if (arm_is_fpimm8 (ins->inst_offset))
1377 low_imm = ins->inst_offset & 0x1ff;
1378 if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_offset & ~0x1ff, &rot_amount)) >= 0) {
1379 NEW_INS (cfg, temp, OP_ADD_IMM);
1380 temp->inst_imm = ins->inst_offset & ~0x1ff;
1381 temp->sreg1 = ins->inst_basereg;
1382 temp->dreg = mono_regstate_next_int (cfg->rs);
1383 ins->inst_basereg = temp->dreg;
1384 ins->inst_offset = low_imm;
1387 /* FPA doesn't have indexed load instructions */
1388 g_assert_not_reached ();
1390 case OP_STORE_MEMBASE_REG:
1391 case OP_STOREI4_MEMBASE_REG:
1392 case OP_STOREI1_MEMBASE_REG:
1393 if (arm_is_imm12 (ins->inst_offset))
1395 NEW_INS (cfg, temp, OP_ICONST);
1396 temp->inst_c0 = ins->inst_offset;
1397 temp->dreg = mono_regstate_next_int (cfg->rs);
1398 ins->sreg2 = temp->dreg;
1399 ins->opcode = map_to_reg_reg_op (ins->opcode);
1401 case OP_STOREI2_MEMBASE_REG:
1402 if (arm_is_imm8 (ins->inst_offset))
1404 NEW_INS (cfg, temp, OP_ICONST);
1405 temp->inst_c0 = ins->inst_offset;
1406 temp->dreg = mono_regstate_next_int (cfg->rs);
1407 ins->sreg2 = temp->dreg;
1408 ins->opcode = map_to_reg_reg_op (ins->opcode);
1410 case OP_STORER4_MEMBASE_REG:
1411 case OP_STORER8_MEMBASE_REG:
1412 if (arm_is_fpimm8 (ins->inst_offset))
1414 low_imm = ins->inst_offset & 0x1ff;
1415 if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_offset & ~ 0x1ff, &rot_amount)) >= 0 && arm_is_fpimm8 (low_imm)) {
1416 NEW_INS (cfg, temp, OP_ADD_IMM);
1417 temp->inst_imm = ins->inst_offset & ~0x1ff;
1418 temp->sreg1 = ins->inst_destbasereg;
1419 temp->dreg = mono_regstate_next_int (cfg->rs);
1420 ins->inst_destbasereg = temp->dreg;
1421 ins->inst_offset = low_imm;
1424 /*g_print ("fail with: %d (%d, %d)\n", ins->inst_offset, ins->inst_offset & ~0x1ff, low_imm);*/
1425 /* FPA doesn't have indexed store instructions */
1426 g_assert_not_reached ();
1428 case OP_STORE_MEMBASE_IMM:
1429 case OP_STOREI1_MEMBASE_IMM:
1430 case OP_STOREI2_MEMBASE_IMM:
1431 case OP_STOREI4_MEMBASE_IMM:
1432 NEW_INS (cfg, temp, OP_ICONST);
1433 temp->inst_c0 = ins->inst_imm;
1434 temp->dreg = mono_regstate_next_int (cfg->rs);
1435 ins->sreg1 = temp->dreg;
1436 ins->opcode = map_to_reg_reg_op (ins->opcode);
1438 goto loop_start; /* make it handle the possibly big ins->inst_offset */
1443 bb->last_ins = last_ins;
1444 bb->max_ireg = cfg->rs->next_vireg;
1449 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1453 mono_arch_lowering_pass (cfg, bb);
1454 mono_local_regalloc (cfg, bb);
1458 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
1460 /* sreg is a float, dreg is an integer reg */
1461 ARM_FIXZ (code, dreg, sreg);
1464 ARM_AND_REG_IMM8 (code, dreg, dreg, 0xff);
1465 else if (size == 2) {
1466 ARM_SHL_IMM (code, dreg, dreg, 16);
1467 ARM_SHR_IMM (code, dreg, dreg, 16);
1471 ARM_SHL_IMM (code, dreg, dreg, 24);
1472 ARM_SAR_IMM (code, dreg, dreg, 24);
1473 } else if (size == 2) {
1474 ARM_SHL_IMM (code, dreg, dreg, 16);
1475 ARM_SAR_IMM (code, dreg, dreg, 16);
1483 const guchar *target;
1488 #define is_call_imm(diff) ((gint)(diff) >= -33554432 && (gint)(diff) <= 33554431)
1491 search_thunk_slot (void *data, int csize, int bsize, void *user_data) {
1492 PatchData *pdata = (PatchData*)user_data;
1493 guchar *code = data;
1494 guint32 *thunks = data;
1495 guint32 *endthunks = (guint32*)(code + bsize);
1497 int difflow, diffhigh;
1499 /* always ensure a call from pdata->code can reach to the thunks without further thunks */
1500 difflow = (char*)pdata->code - (char*)thunks;
1501 diffhigh = (char*)pdata->code - (char*)endthunks;
1502 if (!((is_call_imm (thunks) && is_call_imm (endthunks)) || (is_call_imm (difflow) && is_call_imm (diffhigh))))
1506 * The thunk is composed of 3 words:
1507 * load constant from thunks [2] into ARM_IP
1510 * Note that the LR register is already setup
1512 //g_print ("thunk nentries: %d\n", ((char*)endthunks - (char*)thunks)/16);
1513 if ((pdata->found == 2) || (pdata->code >= code && pdata->code <= code + csize)) {
1514 while (thunks < endthunks) {
1515 //g_print ("looking for target: %p at %p (%08x-%08x)\n", pdata->target, thunks, thunks [0], thunks [1]);
1516 if (thunks [2] == (guint32)pdata->target) {
1517 arm_patch (pdata->code, (guchar*)thunks);
1518 mono_arch_flush_icache (pdata->code, 4);
1521 } else if ((thunks [0] == 0) && (thunks [1] == 0) && (thunks [2] == 0)) {
1522 /* found a free slot instead: emit thunk */
1523 code = (guchar*)thunks;
1524 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
1525 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
1526 thunks [2] = (guint32)pdata->target;
1527 mono_arch_flush_icache ((guchar*)thunks, 12);
1529 arm_patch (pdata->code, (guchar*)thunks);
1530 mono_arch_flush_icache (pdata->code, 4);
1534 /* skip 12 bytes, the size of the thunk */
1538 //g_print ("failed thunk lookup for %p from %p at %p (%d entries)\n", pdata->target, pdata->code, data, count);
1544 handle_thunk (int absolute, guchar *code, const guchar *target) {
1545 MonoDomain *domain = mono_domain_get ();
1549 pdata.target = target;
1550 pdata.absolute = absolute;
1553 mono_domain_lock (domain);
1554 mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
1557 /* this uses the first available slot */
1559 mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
1561 mono_domain_unlock (domain);
1563 if (pdata.found != 1)
1564 g_print ("thunk failed for %p from %p\n", target, code);
1565 g_assert (pdata.found == 1);
1569 arm_patch (guchar *code, const guchar *target)
1571 guint32 ins = *(guint32*)code;
1572 guint32 prim = (ins >> 25) & 7;
1575 //g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
1576 if (prim == 5) { /* 101b */
1577 /* the diff starts 8 bytes from the branch opcode */
1578 gint diff = target - code - 8;
1580 if (diff <= 33554431) {
1582 ins = (ins & 0xff000000) | diff;
1583 *(guint32*)code = ins;
1587 /* diff between 0 and -33554432 */
1588 if (diff >= -33554432) {
1590 ins = (ins & 0xff000000) | (diff & ~0xff000000);
1591 *(guint32*)code = ins;
1596 handle_thunk (TRUE, code, target);
1601 if ((ins & 0x0ffffff0) == 0x12fff10) {
1602 /* branch and exchange: the address is constructed in a reg */
1603 g_assert_not_reached ();
1606 guint32 *tmp = ccode;
1607 ARM_LDR_IMM (tmp, ARMREG_IP, ARMREG_PC, 0);
1608 ARM_MOV_REG_REG (tmp, ARMREG_LR, ARMREG_PC);
1609 ARM_MOV_REG_REG (tmp, ARMREG_PC, ARMREG_IP);
1610 if (ins == ccode [2]) {
1611 tmp = (guint32*)code;
1612 tmp [-1] = (guint32)target;
1615 if (ins == ccode [0]) {
1616 tmp = (guint32*)code;
1617 tmp [2] = (guint32)target;
1620 g_assert_not_reached ();
1622 // g_print ("patched with 0x%08x\n", ins);
1626 * Return the >= 0 uimm8 value if val can be represented with a byte + rotation
1627 * (with the rotation amount in *rot_amount. rot_amount is already adjusted
1628 * to be used with the emit macros.
1629 * Return -1 otherwise.
1632 mono_arm_is_rotated_imm8 (guint32 val, gint *rot_amount)
1635 for (i = 0; i < 31; i+= 2) {
1636 res = (val << (32 - i)) | (val >> i);
1639 *rot_amount = i? 32 - i: 0;
1646 * Emits in code a sequence of instructions that load the value 'val'
1647 * into the dreg register. Uses at most 4 instructions.
1650 mono_arm_emit_load_imm (guint8 *code, int dreg, guint32 val)
1652 int imm8, rot_amount;
1654 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
1655 /* skip the constant pool */
1661 if ((imm8 = mono_arm_is_rotated_imm8 (val, &rot_amount)) >= 0) {
1662 ARM_MOV_REG_IMM (code, dreg, imm8, rot_amount);
1663 } else if ((imm8 = mono_arm_is_rotated_imm8 (~val, &rot_amount)) >= 0) {
1664 ARM_MVN_REG_IMM (code, dreg, imm8, rot_amount);
1667 ARM_MOV_REG_IMM8 (code, dreg, (val & 0xFF));
1669 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF00) >> 8, 24);
1671 if (val & 0xFF0000) {
1672 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
1674 if (val & 0xFF000000) {
1675 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
1677 } else if (val & 0xFF00) {
1678 ARM_MOV_REG_IMM (code, dreg, (val & 0xFF00) >> 8, 24);
1679 if (val & 0xFF0000) {
1680 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
1682 if (val & 0xFF000000) {
1683 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
1685 } else if (val & 0xFF0000) {
1686 ARM_MOV_REG_IMM (code, dreg, (val & 0xFF0000) >> 16, 16);
1687 if (val & 0xFF000000) {
1688 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
1691 //g_assert_not_reached ();
1697 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
1702 guint8 *code = cfg->native_code + cfg->code_len;
1703 MonoInst *last_ins = NULL;
1704 guint last_offset = 0;
1706 int imm8, rot_amount;
1708 if (cfg->opt & MONO_OPT_PEEPHOLE)
1709 peephole_pass (cfg, bb);
1711 /* we don't align basic blocks of loops on arm */
1713 if (cfg->verbose_level > 2)
1714 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
1716 cpos = bb->max_offset;
1718 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
1719 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
1720 //g_assert (!mono_compile_aot);
1723 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
1724 /* this is not thread save, but good enough */
1725 /* fixme: howto handle overflows? */
1726 //x86_inc_mem (code, &cov->data [bb->dfn].count);
1731 offset = code - cfg->native_code;
1733 max_len = ((guint8 *)arm_cpu_desc [ins->opcode])[MONO_INST_LEN];
1735 if (offset > (cfg->code_size - max_len - 16)) {
1736 cfg->code_size *= 2;
1737 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
1738 code = cfg->native_code + offset;
1740 // if (ins->cil_code)
1741 // g_print ("cil code\n");
1742 mono_debug_record_line_number (cfg, ins, offset);
1744 switch (ins->opcode) {
1745 case OP_MEMORY_BARRIER:
1748 g_assert_not_reached ();
1751 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
1752 ppc_mulhw (code, ppc_r3, ins->sreg1, ins->sreg2);
1755 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
1756 ppc_mulhwu (code, ppc_r3, ins->sreg1, ins->sreg2);
1758 case OP_STOREI1_MEMBASE_IMM:
1759 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFF);
1760 g_assert (arm_is_imm12 (ins->inst_offset));
1761 ARM_STRB_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
1763 case OP_STOREI2_MEMBASE_IMM:
1764 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFFFF);
1765 g_assert (arm_is_imm8 (ins->inst_offset));
1766 ARM_STRH_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
1768 case OP_STORE_MEMBASE_IMM:
1769 case OP_STOREI4_MEMBASE_IMM:
1770 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm);
1771 g_assert (arm_is_imm12 (ins->inst_offset));
1772 ARM_STR_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
1774 case OP_STOREI1_MEMBASE_REG:
1775 g_assert (arm_is_imm12 (ins->inst_offset));
1776 ARM_STRB_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
1778 case OP_STOREI2_MEMBASE_REG:
1779 g_assert (arm_is_imm8 (ins->inst_offset));
1780 ARM_STRH_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
1782 case OP_STORE_MEMBASE_REG:
1783 case OP_STOREI4_MEMBASE_REG:
1784 /* this case is special, since it happens for spill code after lowering has been called */
1785 if (arm_is_imm12 (ins->inst_offset)) {
1786 ARM_STR_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
1788 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
1789 ARM_STR_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ARMREG_LR);
1792 case OP_STOREI1_MEMINDEX:
1793 ARM_STRB_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
1795 case OP_STOREI2_MEMINDEX:
1796 /* note: the args are reversed in the macro */
1797 ARM_STRH_REG_REG (code, ins->inst_destbasereg, ins->sreg1, ins->sreg2);
1799 case OP_STORE_MEMINDEX:
1800 case OP_STOREI4_MEMINDEX:
1801 ARM_STR_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
1806 g_assert_not_reached ();
1809 g_assert_not_reached ();
1811 case OP_LOAD_MEMINDEX:
1812 case OP_LOADI4_MEMINDEX:
1813 case OP_LOADU4_MEMINDEX:
1814 ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
1816 case OP_LOADI1_MEMINDEX:
1817 /* note: the args are reversed in the macro */
1818 ARM_LDRSB_REG_REG (code, ins->inst_basereg, ins->dreg, ins->sreg2);
1820 case OP_LOADU1_MEMINDEX:
1821 ARM_LDRB_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
1823 case OP_LOADI2_MEMINDEX:
1824 /* note: the args are reversed in the macro */
1825 ARM_LDRSH_REG_REG (code, ins->inst_basereg, ins->dreg, ins->sreg2);
1827 case OP_LOADU2_MEMINDEX:
1828 /* note: the args are reversed in the macro */
1829 ARM_LDRH_REG_REG (code, ins->inst_basereg, ins->dreg, ins->sreg2);
1831 case OP_LOAD_MEMBASE:
1832 case OP_LOADI4_MEMBASE:
1833 case OP_LOADU4_MEMBASE:
1834 /* this case is special, since it happens for spill code after lowering has been called */
1835 if (arm_is_imm12 (ins->inst_offset)) {
1836 ARM_LDR_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
1838 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
1839 ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ARMREG_LR);
1842 case OP_LOADI1_MEMBASE:
1843 g_assert (arm_is_imm8 (ins->inst_offset));
1844 ARM_LDRSB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
1846 case OP_LOADU1_MEMBASE:
1847 g_assert (arm_is_imm12 (ins->inst_offset));
1848 ARM_LDRB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
1850 case OP_LOADU2_MEMBASE:
1851 g_assert (arm_is_imm8 (ins->inst_offset));
1852 ARM_LDRH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
1854 case OP_LOADI2_MEMBASE:
1855 g_assert (arm_is_imm8 (ins->inst_offset));
1856 ARM_LDRSH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
1859 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 24);
1860 ARM_SAR_IMM (code, ins->dreg, ins->dreg, 24);
1863 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
1864 ARM_SAR_IMM (code, ins->dreg, ins->dreg, 16);
1867 ARM_AND_REG_IMM8 (code, ins->dreg, ins->sreg1, 0xff);
1870 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
1871 ARM_SHR_IMM (code, ins->dreg, ins->dreg, 16);
1874 ARM_CMP_REG_REG (code, ins->sreg1, ins->sreg2);
1876 case OP_COMPARE_IMM:
1877 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1878 g_assert (imm8 >= 0);
1879 ARM_CMP_REG_IMM (code, ins->sreg1, imm8, rot_amount);
1882 *(int*)code = 0xe7f001f0;
1883 *(int*)code = 0xef9f0001;
1888 ARM_ADDS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1891 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1894 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1897 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1898 g_assert (imm8 >= 0);
1899 ARM_ADDS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1902 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1903 g_assert (imm8 >= 0);
1904 ARM_ADD_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1907 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1908 g_assert (imm8 >= 0);
1909 ARM_ADCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1912 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1913 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
1915 case CEE_ADD_OVF_UN:
1916 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1917 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
1920 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1921 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
1923 case CEE_SUB_OVF_UN:
1924 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1925 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
1927 case OP_ADD_OVF_CARRY:
1928 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1929 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
1931 case OP_ADD_OVF_UN_CARRY:
1932 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1933 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
1935 case OP_SUB_OVF_CARRY:
1936 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1937 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
1939 case OP_SUB_OVF_UN_CARRY:
1940 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1941 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
1944 ARM_SUBS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1947 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1948 g_assert (imm8 >= 0);
1949 ARM_SUBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1952 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1955 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1958 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1959 g_assert (imm8 >= 0);
1960 ARM_SUB_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1963 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1964 g_assert (imm8 >= 0);
1965 ARM_SBCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1967 case OP_ARM_RSBS_IMM:
1968 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1969 g_assert (imm8 >= 0);
1970 ARM_RSBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1972 case OP_ARM_RSC_IMM:
1973 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1974 g_assert (imm8 >= 0);
1975 ARM_RSC_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1978 ARM_AND_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1981 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1982 g_assert (imm8 >= 0);
1983 ARM_AND_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1991 /* crappy ARM arch doesn't have a DIV instruction */
1992 g_assert_not_reached ();
1994 ARM_ORR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1997 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1998 g_assert (imm8 >= 0);
1999 ARM_ORR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
2002 ARM_EOR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
2005 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
2006 g_assert (imm8 >= 0);
2007 ARM_EOR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
2010 ARM_SHL_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
2014 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
2017 ARM_SAR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
2021 ARM_SAR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
2025 ARM_SHR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
2028 ARM_SHR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
2031 ARM_MVN_REG_REG (code, ins->dreg, ins->sreg1);
2034 ARM_RSB_REG_IMM8 (code, ins->dreg, ins->sreg1, 0);
2037 if (ins->dreg == ins->sreg2)
2038 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
2040 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg2, ins->sreg1);
2043 g_assert_not_reached ();
2046 /* FIXME: handle ovf/ sreg2 != dreg */
2047 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
2049 case CEE_MUL_OVF_UN:
2050 /* FIXME: handle ovf/ sreg2 != dreg */
2051 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
2055 code = mono_arm_emit_load_imm (code, ins->dreg, ins->inst_c0);
2058 g_assert_not_reached ();
2059 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2065 if (ins->dreg != ins->sreg1)
2066 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
2069 int saved = ins->sreg2;
2070 if (ins->sreg2 == ARM_LSW_REG) {
2071 ARM_MOV_REG_REG (code, ARMREG_LR, ins->sreg2);
2074 if (ins->sreg1 != ARM_LSW_REG)
2075 ARM_MOV_REG_REG (code, ARM_LSW_REG, ins->sreg1);
2076 if (saved != ARM_MSW_REG)
2077 ARM_MOV_REG_REG (code, ARM_MSW_REG, saved);
2082 ARM_MVFD (code, ins->dreg, ins->sreg1);
2084 case OP_FCONV_TO_R4:
2085 ARM_MVFS (code, ins->dreg, ins->sreg1);
2089 * Keep in sync with mono_arch_emit_epilog
2091 g_assert (!cfg->method->save_lmf);
2092 code = emit_big_add (code, ARMREG_SP, cfg->frame_reg, cfg->stack_usage);
2093 ARM_POP_NWB (code, cfg->used_int_regs | ((1 << ARMREG_SP)) | ((1 << ARMREG_LR)));
2094 mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2098 /* ensure ins->sreg1 is not NULL */
2099 ARM_LDR_IMM (code, ARMREG_LR, ins->sreg1, 0);
2103 if (ppc_is_imm16 (cfg->sig_cookie + cfg->stack_usage)) {
2104 ppc_addi (code, ppc_r11, cfg->frame_reg, cfg->sig_cookie + cfg->stack_usage);
2106 ppc_load (code, ppc_r11, cfg->sig_cookie + cfg->stack_usage);
2107 ppc_add (code, ppc_r11, cfg->frame_reg, ppc_r11);
2109 ppc_stw (code, ppc_r11, 0, ins->sreg1);
2118 call = (MonoCallInst*)ins;
2119 if (ins->flags & MONO_INST_HAS_METHOD)
2120 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2122 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2123 if (cfg->method->dynamic) {
2124 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
2126 *(gpointer*)code = NULL;
2128 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
2129 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
2137 case OP_VOIDCALL_REG:
2139 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
2140 ARM_MOV_REG_REG (code, ARMREG_PC, ins->sreg1);
2142 case OP_FCALL_MEMBASE:
2143 case OP_LCALL_MEMBASE:
2144 case OP_VCALL_MEMBASE:
2145 case OP_VOIDCALL_MEMBASE:
2146 case OP_CALL_MEMBASE:
2147 g_assert (arm_is_imm12 (ins->inst_offset));
2148 g_assert (ins->sreg1 != ARMREG_LR);
2149 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
2150 ARM_LDR_IMM (code, ARMREG_PC, ins->sreg1, ins->inst_offset);
2153 g_assert_not_reached ();
2156 /* keep alignment */
2157 int alloca_waste = cfg->param_area;
2160 /* round the size to 8 bytes */
2161 ARM_ADD_REG_IMM8 (code, ins->dreg, ins->sreg1, 7);
2162 ARM_BIC_REG_IMM8 (code, ins->dreg, ins->sreg1, 7);
2163 ARM_ADD_REG_IMM8 (code, ins->dreg, ins->dreg, alloca_waste);
2164 ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ins->dreg);
2165 /* memzero the area: dreg holds the size, sp is the pointer */
2166 if (ins->flags & MONO_INST_INIT) {
2167 guint8 *start_loop, *branch_to_cond;
2168 ARM_MOV_REG_IMM8 (code, ARMREG_LR, 0);
2169 branch_to_cond = code;
2172 ARM_STR_REG_REG (code, ARMREG_LR, ARMREG_SP, ins->dreg);
2173 arm_patch (branch_to_cond, code);
2174 /* decrement by 4 and set flags */
2175 ARM_SUBS_REG_IMM8 (code, ins->dreg, ins->dreg, 4);
2176 ARM_B_COND (code, ARMCOND_LT, 0);
2177 arm_patch (code - 4, start_loop);
2179 ARM_ADD_REG_IMM8 (code, ins->dreg, ARMREG_SP, alloca_waste);
2183 g_assert_not_reached ();
2184 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_LR);
2187 if (ins->sreg1 != ARMREG_R0)
2188 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
2189 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2190 (gpointer)"mono_arch_throw_exception");
2191 if (cfg->method->dynamic) {
2192 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
2194 *(gpointer*)code = NULL;
2196 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
2197 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
2204 if (ins->sreg1 != ARMREG_R0)
2205 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
2206 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2207 (gpointer)"mono_arch_rethrow_exception");
2208 if (cfg->method->dynamic) {
2209 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
2211 *(gpointer*)code = NULL;
2213 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
2214 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
2220 case OP_START_HANDLER:
2221 if (arm_is_imm12 (ins->inst_left->inst_offset)) {
2222 ARM_STR_IMM (code, ARMREG_LR, ins->inst_left->inst_basereg, ins->inst_left->inst_offset);
2224 code = mono_arm_emit_load_imm (code, ARMREG_IP, ins->inst_left->inst_offset);
2225 ARM_STR_REG_REG (code, ARMREG_LR, ins->inst_left->inst_basereg, ARMREG_IP);
2229 if (ins->sreg1 != ARMREG_R0)
2230 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
2231 if (arm_is_imm12 (ins->inst_left->inst_offset)) {
2232 ARM_LDR_IMM (code, ARMREG_IP, ins->inst_left->inst_basereg, ins->inst_left->inst_offset);
2234 g_assert (ARMREG_IP != ins->inst_left->inst_basereg);
2235 code = mono_arm_emit_load_imm (code, ARMREG_IP, ins->inst_left->inst_offset);
2236 ARM_LDR_REG_REG (code, ARMREG_IP, ins->inst_left->inst_basereg, ARMREG_IP);
2238 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
2240 case CEE_ENDFINALLY:
2241 if (arm_is_imm12 (ins->inst_left->inst_offset)) {
2242 ARM_LDR_IMM (code, ARMREG_IP, ins->inst_left->inst_basereg, ins->inst_left->inst_offset);
2244 g_assert (ARMREG_IP != ins->inst_left->inst_basereg);
2245 code = mono_arm_emit_load_imm (code, ARMREG_IP, ins->inst_left->inst_offset);
2246 ARM_LDR_REG_REG (code, ARMREG_IP, ins->inst_left->inst_basereg, ARMREG_IP);
2248 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
2250 case OP_CALL_HANDLER:
2251 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2255 ins->inst_c0 = code - cfg->native_code;
2258 if (ins->flags & MONO_INST_BRLABEL) {
2259 /*if (ins->inst_i0->inst_c0) {
2261 //x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2263 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2267 /*if (ins->inst_target_bb->native_offset) {
2269 //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
2271 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2277 ARM_MOV_REG_REG (code, ARMREG_PC, ins->sreg1);
2281 * In the normal case we have:
2282 * ldr pc, [pc, ins->sreg1 << 2]
2285 * ldr lr, [pc, ins->sreg1 << 2]
2287 * After follows the data.
2288 * FIXME: add aot support.
2290 max_len += 4 * GPOINTER_TO_INT (ins->klass);
2291 if (offset > (cfg->code_size - max_len - 16)) {
2292 cfg->code_size += max_len;
2293 cfg->code_size *= 2;
2294 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2295 code = cfg->native_code + offset;
2297 ARM_LDR_REG_REG_SHIFT (code, ARMREG_PC, ARMREG_PC, ins->sreg1, ARMSHIFT_LSL, 2);
2299 code += 4 * GPOINTER_TO_INT (ins->klass);
2302 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
2303 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
2306 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2307 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LT);
2310 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2311 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LO);
2314 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2315 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_GT);
2318 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2319 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_HI);
2321 case OP_COND_EXC_EQ:
2322 case OP_COND_EXC_NE_UN:
2323 case OP_COND_EXC_LT:
2324 case OP_COND_EXC_LT_UN:
2325 case OP_COND_EXC_GT:
2326 case OP_COND_EXC_GT_UN:
2327 case OP_COND_EXC_GE:
2328 case OP_COND_EXC_GE_UN:
2329 case OP_COND_EXC_LE:
2330 case OP_COND_EXC_LE_UN:
2331 EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_EQ, ins->inst_p1);
2334 case OP_COND_EXC_OV:
2335 case OP_COND_EXC_NC:
2336 case OP_COND_EXC_NO:
2337 g_assert_not_reached ();
2349 EMIT_COND_BRANCH (ins, ins->opcode - CEE_BEQ);
2352 /* floating point opcodes */
2354 /* FIXME: we can optimize the imm load by dealing with part of
2355 * the displacement in LDFD (aligning to 512).
2357 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
2358 ARM_LDFD (code, ins->dreg, ARMREG_LR, 0);
2361 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
2362 ARM_LDFS (code, ins->dreg, ARMREG_LR, 0);
2364 case OP_STORER8_MEMBASE_REG:
2365 g_assert (arm_is_fpimm8 (ins->inst_offset));
2366 ARM_STFD (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
2368 case OP_LOADR8_MEMBASE:
2369 g_assert (arm_is_fpimm8 (ins->inst_offset));
2370 ARM_LDFD (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
2372 case OP_STORER4_MEMBASE_REG:
2373 g_assert (arm_is_fpimm8 (ins->inst_offset));
2374 ARM_STFS (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
2376 case OP_LOADR4_MEMBASE:
2377 g_assert (arm_is_fpimm8 (ins->inst_offset));
2378 ARM_LDFS (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
2380 case CEE_CONV_R_UN: {
2382 tmpreg = ins->dreg == 0? 1: 0;
2383 ARM_CMP_REG_IMM8 (code, ins->sreg1, 0);
2384 ARM_FLTD (code, ins->dreg, ins->sreg1);
2385 ARM_B_COND (code, ARMCOND_GE, 8);
2386 /* save the temp register */
2387 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
2388 ARM_STFD (code, tmpreg, ARMREG_SP, 0);
2389 ARM_LDFD (code, tmpreg, ARMREG_PC, 12);
2390 ARM_FPA_ADFD (code, ins->dreg, ins->dreg, tmpreg);
2391 ARM_LDFD (code, tmpreg, ARMREG_SP, 0);
2392 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
2393 /* skip the constant pool */
2396 *(int*)code = 0x41f00000;
2401 * ldfltd ftemp, [pc, #8] 0x41f00000 0x00000000
2402 * adfltd fdest, fdest, ftemp
2407 ARM_FLTS (code, ins->dreg, ins->sreg1);
2410 ARM_FLTD (code, ins->dreg, ins->sreg1);
2412 case OP_FCONV_TO_I1:
2413 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
2415 case OP_FCONV_TO_U1:
2416 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
2418 case OP_FCONV_TO_I2:
2419 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
2421 case OP_FCONV_TO_U2:
2422 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
2424 case OP_FCONV_TO_I4:
2426 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
2428 case OP_FCONV_TO_U4:
2430 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
2432 case OP_FCONV_TO_I8:
2433 case OP_FCONV_TO_U8:
2434 g_assert_not_reached ();
2435 /* Implemented as helper calls */
2437 case OP_LCONV_TO_R_UN:
2438 g_assert_not_reached ();
2439 /* Implemented as helper calls */
2441 case OP_LCONV_TO_OVF_I: {
2443 guint32 *negative_branch, *msword_positive_branch, *msword_negative_branch, *ovf_ex_target;
2444 // Check if its negative
2445 ppc_cmpi (code, 0, 0, ins->sreg1, 0);
2446 negative_branch = code;
2447 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 0);
2448 // Its positive msword == 0
2449 ppc_cmpi (code, 0, 0, ins->sreg2, 0);
2450 msword_positive_branch = code;
2451 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
2453 ovf_ex_target = code;
2454 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_ALWAYS, 0, "OverflowException");
2456 ppc_patch (negative_branch, code);
2457 ppc_cmpi (code, 0, 0, ins->sreg2, -1);
2458 msword_negative_branch = code;
2459 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 0);
2460 ppc_patch (msword_negative_branch, ovf_ex_target);
2462 ppc_patch (msword_positive_branch, code);
2463 if (ins->dreg != ins->sreg1)
2464 ppc_mr (code, ins->dreg, ins->sreg1);
2466 if (ins->dreg != ins->sreg1)
2467 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
2471 ARM_FPA_ADFD (code, ins->dreg, ins->sreg1, ins->sreg2);
2474 ARM_FPA_SUFD (code, ins->dreg, ins->sreg1, ins->sreg2);
2477 ARM_FPA_MUFD (code, ins->dreg, ins->sreg1, ins->sreg2);
2480 ARM_FPA_DVFD (code, ins->dreg, ins->sreg1, ins->sreg2);
2483 ARM_MNFD (code, ins->dreg, ins->sreg1);
2487 g_assert_not_reached ();
2490 /* each fp compare op needs to do its own */
2491 g_assert_not_reached ();
2492 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2495 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2496 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
2497 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
2500 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2501 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2502 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
2505 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2506 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2507 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
2508 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
2512 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
2513 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2514 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
2518 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
2519 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2520 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
2521 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
2523 /* ARM FPA flags table:
2524 * N Less than ARMCOND_MI
2525 * Z Equal ARMCOND_EQ
2526 * C Greater Than or Equal ARMCOND_CS
2527 * V Unordered ARMCOND_VS
2530 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2531 EMIT_COND_BRANCH (ins, CEE_BEQ - CEE_BEQ);
2534 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2535 EMIT_COND_BRANCH (ins, CEE_BNE_UN - CEE_BEQ);
2538 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2539 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set */
2542 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2543 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
2544 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set */
2547 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
2548 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set, swapped args */
2551 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
2552 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
2553 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set, swapped args */
2556 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2557 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_CS);
2560 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2561 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
2562 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_GE);
2565 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
2566 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_CS); /* swapped */
2569 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
2570 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
2571 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_GE); /* swapped */
2573 case CEE_CKFINITE: {
2574 /*ppc_stfd (code, ins->sreg1, -8, ppc_sp);
2575 ppc_lwz (code, ppc_r11, -8, ppc_sp);
2576 ppc_rlwinm (code, ppc_r11, ppc_r11, 0, 1, 31);
2577 ppc_addis (code, ppc_r11, ppc_r11, -32752);
2578 ppc_rlwinmd (code, ppc_r11, ppc_r11, 1, 31, 31);
2579 EMIT_COND_SYSTEM_EXCEPTION (CEE_BEQ - CEE_BEQ, "ArithmeticException");*/
2580 g_assert_not_reached ();
2584 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
2585 g_assert_not_reached ();
2588 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
2589 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
2590 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
2591 g_assert_not_reached ();
2597 last_offset = offset;
2602 cfg->code_len = code - cfg->native_code;
2606 mono_arch_register_lowlevel_calls (void)
2610 #define patch_lis_ori(ip,val) do {\
2611 guint16 *__lis_ori = (guint16*)(ip); \
2612 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff; \
2613 __lis_ori [3] = ((guint32)(val)) & 0xffff; \
2617 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
2619 MonoJumpInfo *patch_info;
2621 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
2622 unsigned char *ip = patch_info->ip.i + code;
2623 const unsigned char *target;
2625 if (patch_info->type == MONO_PATCH_INFO_SWITCH) {
2626 gpointer *table = (gpointer *)patch_info->data.table->table;
2627 gpointer *jt = (gpointer*)(ip + 8);
2629 /* jt is the inlined jump table, 2 instructions after ip
2630 * In the normal case we store the absolute addresses,
2631 * otherwise the displacements.
2633 for (i = 0; i < patch_info->data.table->table_size; i++) {
2634 jt [i] = code + (int)patch_info->data.table->table [i];
2638 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
2640 switch (patch_info->type) {
2641 case MONO_PATCH_INFO_IP:
2642 g_assert_not_reached ();
2643 patch_lis_ori (ip, ip);
2645 case MONO_PATCH_INFO_METHOD_REL:
2646 g_assert_not_reached ();
2647 *((gpointer *)(ip)) = code + patch_info->data.offset;
2649 case MONO_PATCH_INFO_METHODCONST:
2650 case MONO_PATCH_INFO_CLASS:
2651 case MONO_PATCH_INFO_IMAGE:
2652 case MONO_PATCH_INFO_FIELD:
2653 case MONO_PATCH_INFO_VTABLE:
2654 case MONO_PATCH_INFO_IID:
2655 case MONO_PATCH_INFO_SFLDA:
2656 case MONO_PATCH_INFO_LDSTR:
2657 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
2658 case MONO_PATCH_INFO_LDTOKEN:
2659 g_assert_not_reached ();
2660 /* from OP_AOTCONST : lis + ori */
2661 patch_lis_ori (ip, target);
2663 case MONO_PATCH_INFO_R4:
2664 case MONO_PATCH_INFO_R8:
2665 g_assert_not_reached ();
2666 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
2668 case MONO_PATCH_INFO_EXC_NAME:
2669 g_assert_not_reached ();
2670 *((gconstpointer *)(ip + 1)) = patch_info->data.name;
2672 case MONO_PATCH_INFO_NONE:
2673 case MONO_PATCH_INFO_BB_OVF:
2674 case MONO_PATCH_INFO_EXC_OVF:
2675 /* everything is dealt with at epilog output time */
2680 arm_patch (ip, target);
2685 * Stack frame layout:
2687 * ------------------- fp
2688 * MonoLMF structure or saved registers
2689 * -------------------
2691 * -------------------
2693 * -------------------
2694 * optional 8 bytes for tracing
2695 * -------------------
2696 * param area size is cfg->param_area
2697 * ------------------- sp
2700 mono_arch_emit_prolog (MonoCompile *cfg)
2702 MonoMethod *method = cfg->method;
2704 MonoMethodSignature *sig;
2706 int alloc_size, pos, max_offset, i, rot_amount;
2713 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
2716 sig = mono_method_signature (method);
2717 cfg->code_size = 256 + sig->param_count * 20;
2718 code = cfg->native_code = g_malloc (cfg->code_size);
2720 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
2722 alloc_size = cfg->stack_offset;
2725 if (!method->save_lmf) {
2726 ARM_PUSH (code, (cfg->used_int_regs | (1 << ARMREG_IP) | (1 << ARMREG_LR)));
2727 prev_sp_offset = 8; /* ip and lr */
2728 for (i = 0; i < 16; ++i) {
2729 if (cfg->used_int_regs & (1 << i))
2730 prev_sp_offset += 4;
2733 ARM_PUSH (code, 0x5ff0);
2734 prev_sp_offset = 4 * 10; /* all but r0-r3, sp and pc */
2735 pos += sizeof (MonoLMF) - prev_sp_offset;
2739 // align to MONO_ARCH_FRAME_ALIGNMENT bytes
2740 if (alloc_size & (MONO_ARCH_FRAME_ALIGNMENT - 1)) {
2741 alloc_size += MONO_ARCH_FRAME_ALIGNMENT - 1;
2742 alloc_size &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
2745 /* the stack used in the pushed regs */
2746 if (prev_sp_offset & 4)
2748 cfg->stack_usage = alloc_size;
2750 if ((i = mono_arm_is_rotated_imm8 (alloc_size, &rot_amount)) >= 0) {
2751 ARM_SUB_REG_IMM (code, ARMREG_SP, ARMREG_SP, i, rot_amount);
2753 code = mono_arm_emit_load_imm (code, ARMREG_IP, alloc_size);
2754 ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
2757 if (cfg->frame_reg != ARMREG_SP)
2758 ARM_MOV_REG_REG (code, cfg->frame_reg, ARMREG_SP);
2759 //g_print ("prev_sp_offset: %d, alloc_size:%d\n", prev_sp_offset, alloc_size);
2760 prev_sp_offset += alloc_size;
2762 /* compute max_offset in order to use short forward jumps
2763 * we could skip do it on arm because the immediate displacement
2764 * for jumps is large enough, it may be useful later for constant pools
2767 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2768 MonoInst *ins = bb->code;
2769 bb->max_offset = max_offset;
2771 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
2775 max_offset += ((guint8 *)arm_cpu_desc [ins->opcode])[MONO_INST_LEN];
2780 /* load arguments allocated to register from the stack */
2783 cinfo = calculate_sizes (sig, sig->pinvoke);
2785 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
2786 ArgInfo *ainfo = &cinfo->ret;
2788 g_assert (arm_is_imm12 (inst->inst_offset));
2789 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
2791 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2792 ArgInfo *ainfo = cinfo->args + i;
2793 inst = cfg->varinfo [pos];
2795 if (cfg->verbose_level > 2)
2796 g_print ("Saving argument %d (type: %d)\n", i, ainfo->regtype);
2797 if (inst->opcode == OP_REGVAR) {
2798 if (ainfo->regtype == RegTypeGeneral)
2799 ARM_MOV_REG_REG (code, inst->dreg, ainfo->reg);
2800 else if (ainfo->regtype == RegTypeFP) {
2801 g_assert_not_reached ();
2802 } else if (ainfo->regtype == RegTypeBase) {
2803 g_assert (arm_is_imm12 (prev_sp_offset + ainfo->offset));
2804 ARM_LDR_IMM (code, inst->dreg, ARMREG_SP, (prev_sp_offset + ainfo->offset));
2806 g_assert_not_reached ();
2808 if (cfg->verbose_level > 2)
2809 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
2811 /* the argument should be put on the stack: FIXME handle size != word */
2812 if (ainfo->regtype == RegTypeGeneral) {
2813 switch (ainfo->size) {
2815 if (arm_is_imm12 (inst->inst_offset))
2816 ARM_STRB_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
2818 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
2819 ARM_STRB_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
2823 if (arm_is_imm8 (inst->inst_offset)) {
2824 ARM_STRH_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
2826 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
2827 ARM_ADD_REG_REG (code, ARMREG_IP, ARMREG_IP, inst->inst_basereg);
2828 ARM_STRH_IMM (code, ainfo->reg, ARMREG_IP, 0);
2832 g_assert (arm_is_imm12 (inst->inst_offset));
2833 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
2834 g_assert (arm_is_imm12 (inst->inst_offset + 4));
2835 ARM_STR_IMM (code, ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
2838 if (arm_is_imm12 (inst->inst_offset)) {
2839 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
2841 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
2842 ARM_STR_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
2846 } else if (ainfo->regtype == RegTypeBase) {
2847 g_assert (arm_is_imm12 (prev_sp_offset + ainfo->offset));
2848 switch (ainfo->size) {
2850 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
2851 g_assert (arm_is_imm12 (inst->inst_offset));
2852 ARM_STRB_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
2855 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
2856 if (arm_is_imm8 (inst->inst_offset)) {
2857 ARM_STRH_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
2859 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
2860 ARM_ADD_REG_REG (code, ARMREG_IP, ARMREG_IP, inst->inst_basereg);
2861 ARM_STRH_IMM (code, ARMREG_LR, ARMREG_IP, 0);
2865 g_assert (arm_is_imm12 (inst->inst_offset));
2866 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
2867 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
2868 g_assert (arm_is_imm12 (prev_sp_offset + ainfo->offset + 4));
2869 g_assert (arm_is_imm12 (inst->inst_offset + 4));
2870 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset + 4));
2871 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset + 4);
2874 g_assert (arm_is_imm12 (inst->inst_offset));
2875 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
2876 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
2879 } else if (ainfo->regtype == RegTypeFP) {
2880 g_assert_not_reached ();
2881 } else if (ainfo->regtype == RegTypeStructByVal) {
2882 int doffset = inst->inst_offset;
2886 if (mono_class_from_mono_type (inst->inst_vtype))
2887 size = mono_class_native_size (mono_class_from_mono_type (inst->inst_vtype), NULL);
2888 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
2889 g_assert (arm_is_imm12 (doffset));
2890 ARM_STR_IMM (code, ainfo->reg + cur_reg, inst->inst_basereg, doffset);
2891 soffset += sizeof (gpointer);
2892 doffset += sizeof (gpointer);
2894 if (ainfo->vtsize) {
2895 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
2896 //g_print ("emit_memcpy (prev_sp_ofs: %d, ainfo->offset: %d, soffset: %d)\n", prev_sp_offset, ainfo->offset, soffset);
2897 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ARMREG_SP, prev_sp_offset + ainfo->offset);
2899 } else if (ainfo->regtype == RegTypeStructByAddr) {
2900 g_assert_not_reached ();
2901 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
2902 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
2904 g_assert_not_reached ();
2909 if (method->save_lmf) {
2911 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2912 (gpointer)"mono_get_lmf_addr");
2913 if (cfg->method->dynamic) {
2914 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
2916 *(gpointer*)code = NULL;
2918 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
2919 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
2923 /* we build the MonoLMF structure on the stack - see mini-arm.h */
2924 /* lmf_offset is the offset from the previous stack pointer,
2925 * alloc_size is the total stack space allocated, so the offset
2926 * of MonoLMF from the current stack ptr is alloc_size - lmf_offset.
2927 * The pointer to the struct is put in r1 (new_lmf).
2928 * r2 is used as scratch
2929 * The callee-saved registers are already in the MonoLMF structure
2931 code = emit_big_add (code, ARMREG_R1, ARMREG_SP, alloc_size - lmf_offset);
2932 /* r0 is the result from mono_get_lmf_addr () */
2933 ARM_STR_IMM (code, ARMREG_R0, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
2934 /* new_lmf->previous_lmf = *lmf_addr */
2935 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
2936 ARM_STR_IMM (code, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
2937 /* *(lmf_addr) = r1 */
2938 ARM_STR_IMM (code, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
2939 /* save method info */
2940 code = mono_arm_emit_load_imm (code, ARMREG_R2, method);
2941 ARM_STR_IMM (code, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, method));
2942 ARM_STR_IMM (code, ARMREG_SP, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, ebp));
2943 /* save the current IP */
2944 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_PC);
2945 ARM_STR_IMM (code, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, eip));
2949 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
2951 cfg->code_len = code - cfg->native_code;
2952 g_assert (cfg->code_len < cfg->code_size);
2959 mono_arch_emit_epilog (MonoCompile *cfg)
2961 MonoJumpInfo *patch_info;
2962 MonoMethod *method = cfg->method;
2963 int pos, i, rot_amount;
2964 int max_epilog_size = 16 + 20*4;
2967 if (cfg->method->save_lmf)
2968 max_epilog_size += 128;
2970 if (mono_jit_trace_calls != NULL)
2971 max_epilog_size += 50;
2973 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2974 max_epilog_size += 50;
2976 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
2977 cfg->code_size *= 2;
2978 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2979 mono_jit_stats.code_reallocs++;
2983 * Keep in sync with CEE_JMP
2985 code = cfg->native_code + cfg->code_len;
2987 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
2988 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
2992 if (method->save_lmf) {
2994 /* all but r0-r3, sp and pc */
2995 pos += sizeof (MonoLMF) - (4 * 10);
2997 /* r2 contains the pointer to the current LMF */
2998 code = emit_big_add (code, ARMREG_R2, cfg->frame_reg, cfg->stack_usage - lmf_offset);
2999 /* ip = previous_lmf */
3000 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R2, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
3002 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_R2, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
3003 /* *(lmf_addr) = previous_lmf */
3004 ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
3005 /* FIXME: speedup: there is no actual need to restore the registers if
3006 * we didn't actually change them (idea from Zoltan).
3009 /* point sp at the registers to restore: 10 is 14 -4, because we skip r0-r3 */
3010 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_R2, (sizeof (MonoLMF) - 10 * sizeof (gulong)));
3011 ARM_POP_NWB (code, 0xaff0); /* restore ip to sp and lr to pc */
3013 if ((i = mono_arm_is_rotated_imm8 (cfg->stack_usage, &rot_amount)) >= 0) {
3014 ARM_ADD_REG_IMM (code, ARMREG_SP, cfg->frame_reg, i, rot_amount);
3016 code = mono_arm_emit_load_imm (code, ARMREG_IP, cfg->stack_usage);
3017 ARM_ADD_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
3019 ARM_POP_NWB (code, cfg->used_int_regs | ((1 << ARMREG_SP) | (1 << ARMREG_PC)));
3022 cfg->code_len = code - cfg->native_code;
3024 g_assert (cfg->code_len < cfg->code_size);
3028 /* remove once throw_exception_by_name is eliminated */
3030 exception_id_by_name (const char *name)
3032 if (strcmp (name, "IndexOutOfRangeException") == 0)
3033 return MONO_EXC_INDEX_OUT_OF_RANGE;
3034 if (strcmp (name, "OverflowException") == 0)
3035 return MONO_EXC_OVERFLOW;
3036 if (strcmp (name, "ArithmeticException") == 0)
3037 return MONO_EXC_ARITHMETIC;
3038 if (strcmp (name, "DivideByZeroException") == 0)
3039 return MONO_EXC_DIVIDE_BY_ZERO;
3040 if (strcmp (name, "InvalidCastException") == 0)
3041 return MONO_EXC_INVALID_CAST;
3042 if (strcmp (name, "NullReferenceException") == 0)
3043 return MONO_EXC_NULL_REF;
3044 if (strcmp (name, "ArrayTypeMismatchException") == 0)
3045 return MONO_EXC_ARRAY_TYPE_MISMATCH;
3046 g_error ("Unknown intrinsic exception %s\n", name);
3050 mono_arch_emit_exceptions (MonoCompile *cfg)
3052 MonoJumpInfo *patch_info;
3055 const guint8* exc_throw_pos [MONO_EXC_INTRINS_NUM] = {NULL};
3056 guint8 exc_throw_found [MONO_EXC_INTRINS_NUM] = {0};
3059 int max_epilog_size = 50;
3061 /* count the number of exception infos */
3064 * make sure we have enough space for exceptions
3065 * 12 is the simulated call to throw_exception_by_name
3067 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3068 if (patch_info->type == MONO_PATCH_INFO_EXC) {
3069 i = exception_id_by_name (patch_info->data.target);
3070 if (!exc_throw_found [i]) {
3071 max_epilog_size += 12;
3072 exc_throw_found [i] = TRUE;
3077 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
3078 cfg->code_size *= 2;
3079 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3080 mono_jit_stats.code_reallocs++;
3083 code = cfg->native_code + cfg->code_len;
3085 /* add code to raise exceptions */
3086 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3087 switch (patch_info->type) {
3088 case MONO_PATCH_INFO_EXC: {
3089 unsigned char *ip = patch_info->ip.i + cfg->native_code;
3090 const char *ex_name = patch_info->data.target;
3091 i = exception_id_by_name (patch_info->data.target);
3092 if (exc_throw_pos [i]) {
3093 arm_patch (ip, exc_throw_pos [i]);
3094 patch_info->type = MONO_PATCH_INFO_NONE;
3097 exc_throw_pos [i] = code;
3099 arm_patch (ip, code);
3100 //*(int*)code = 0xef9f0001;
3102 /*mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_NAME, patch_info->data.target);*/
3103 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
3104 /* we got here from a conditional call, so the calling ip is set in lr already */
3105 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3106 patch_info->data.name = "mono_arch_throw_exception_by_name";
3107 patch_info->ip.i = code - cfg->native_code;
3109 *(gpointer*)code = ex_name;
3119 cfg->code_len = code - cfg->native_code;
3121 g_assert (cfg->code_len < cfg->code_size);
3126 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
3131 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
3136 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
3139 int this_dreg = ARMREG_R0;
3142 this_dreg = ARMREG_R1;
3144 /* add the this argument */
3145 if (this_reg != -1) {
3147 MONO_INST_NEW (cfg, this, OP_SETREG);
3148 this->type = this_type;
3149 this->sreg1 = this_reg;
3150 this->dreg = mono_regstate_next_int (cfg->rs);
3151 mono_bblock_add_inst (cfg->cbb, this);
3152 mono_call_inst_add_outarg_reg (cfg, inst, this->dreg, this_dreg, FALSE);
3157 MONO_INST_NEW (cfg, vtarg, OP_SETREG);
3158 vtarg->type = STACK_MP;
3159 vtarg->sreg1 = vt_reg;
3160 vtarg->dreg = mono_regstate_next_int (cfg->rs);
3161 mono_bblock_add_inst (cfg->cbb, vtarg);
3162 mono_call_inst_add_outarg_reg (cfg, inst, vtarg->dreg, ARMREG_R0, FALSE);
3167 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3169 MonoInst *ins = NULL;
3170 if (cmethod->klass == mono_defaults.thread_class &&
3171 strcmp (cmethod->name, "MemoryBarrier") == 0) {
3172 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
3178 mono_arch_print_tree (MonoInst *tree, int arity)
3183 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
3189 mono_arch_get_thread_intrinsic (MonoCompile* cfg)
3195 mono_arch_flush_register_windows (void)
3200 mono_arch_fixup_jinfo (MonoCompile *cfg)
3202 /* max encoded stack usage is 64KB * 4 */
3203 g_assert ((cfg->stack_usage & ~(0xffff << 2)) == 0);
3204 cfg->jit_info->used_regs |= cfg->stack_usage << 14;