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>
21 #include "mono/arch/arm/arm-fpa-codegen.h"
22 #elif defined(ARM_FPU_VFP)
23 #include "mono/arch/arm/arm-vfp-codegen.h"
26 static int mono_arm_is_rotated_imm8 (guint32 val, gint *rot_amount);
30 * floating point support: on ARM it is a mess, there are at least 3
31 * different setups, each of which binary incompat with the other.
32 * 1) FPA: old and ugly, but unfortunately what current distros use
33 * the double binary format has the two words swapped. 8 double registers.
34 * Implemented usually by kernel emulation.
35 * 2) softfloat: the compiler emulates all the fp ops. Usually uses the
36 * ugly swapped double format (I guess a softfloat-vfp exists, too, though).
37 * 3) VFP: the new and actually sensible and useful FP support. Implemented
38 * in HW or kernel-emulated, requires new tools. I think this ios what symbian uses.
40 * The plan is to write the FPA support first. softfloat can be tested in a chroot.
42 int mono_exc_esp_offset = 0;
44 #define arm_is_imm12(v) ((v) > -4096 && (v) < 4096)
45 #define arm_is_imm8(v) ((v) > -256 && (v) < 256)
46 #define arm_is_fpimm8(v) ((v) >= -1020 && (v) <= 1020)
49 mono_arch_regname (int reg) {
50 static const char * rnames[] = {
51 "arm_r0", "arm_r1", "arm_r2", "arm_r3", "arm_v1",
52 "arm_v2", "arm_v3", "arm_v4", "arm_v5", "arm_v6",
53 "arm_v7", "arm_fp", "arm_ip", "arm_sp", "arm_lr",
56 if (reg >= 0 && reg < 16)
62 mono_arch_fregname (int reg) {
63 static const char * rnames[] = {
64 "arm_f0", "arm_f1", "arm_f2", "arm_f3", "arm_f4",
65 "arm_f5", "arm_f6", "arm_f7", "arm_f8", "arm_f9",
66 "arm_f10", "arm_f11", "arm_f12", "arm_f13", "arm_f14",
67 "arm_f15", "arm_f16", "arm_f17", "arm_f18", "arm_f19",
68 "arm_f20", "arm_f21", "arm_f22", "arm_f23", "arm_f24",
69 "arm_f25", "arm_f26", "arm_f27", "arm_f28", "arm_f29",
72 if (reg >= 0 && reg < 32)
78 emit_big_add (guint8 *code, int dreg, int sreg, int imm)
81 if ((imm8 = mono_arm_is_rotated_imm8 (imm, &rot_amount)) >= 0) {
82 ARM_ADD_REG_IMM (code, dreg, sreg, imm8, rot_amount);
85 g_assert (dreg != sreg);
86 code = mono_arm_emit_load_imm (code, dreg, imm);
87 ARM_ADD_REG_REG (code, dreg, dreg, sreg);
92 emit_memcpy (guint8 *code, int size, int dreg, int doffset, int sreg, int soffset)
94 /* we can use r0-r3, since this is called only for incoming args on the stack */
95 if (size > sizeof (gpointer) * 4) {
97 code = emit_big_add (code, ARMREG_R0, sreg, soffset);
98 code = emit_big_add (code, ARMREG_R1, dreg, doffset);
99 start_loop = code = mono_arm_emit_load_imm (code, ARMREG_R2, size);
100 ARM_LDR_IMM (code, ARMREG_R3, ARMREG_R0, 0);
101 ARM_STR_IMM (code, ARMREG_R3, ARMREG_R1, 0);
102 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, 4);
103 ARM_ADD_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, 4);
104 ARM_SUBS_REG_IMM8 (code, ARMREG_R2, ARMREG_R2, 4);
105 ARM_B_COND (code, ARMCOND_NE, 0);
106 arm_patch (code - 4, start_loop);
109 if (arm_is_imm12 (doffset) && arm_is_imm12 (doffset + size) &&
110 arm_is_imm12 (soffset) && arm_is_imm12 (soffset + size)) {
112 ARM_LDR_IMM (code, ARMREG_LR, sreg, soffset);
113 ARM_STR_IMM (code, ARMREG_LR, dreg, doffset);
119 code = emit_big_add (code, ARMREG_R0, sreg, soffset);
120 code = emit_big_add (code, ARMREG_R1, dreg, doffset);
121 doffset = soffset = 0;
123 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_R0, soffset);
124 ARM_STR_IMM (code, ARMREG_LR, ARMREG_R1, doffset);
130 g_assert (size == 0);
135 * mono_arch_get_argument_info:
136 * @csig: a method signature
137 * @param_count: the number of parameters to consider
138 * @arg_info: an array to store the result infos
140 * Gathers information on parameters such as size, alignment and
141 * padding. arg_info should be large enought to hold param_count + 1 entries.
143 * Returns the size of the activation frame.
146 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
148 int k, frame_size = 0;
149 int size, align, pad;
152 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
153 frame_size += sizeof (gpointer);
157 arg_info [0].offset = offset;
160 frame_size += sizeof (gpointer);
164 arg_info [0].size = frame_size;
166 for (k = 0; k < param_count; k++) {
169 size = mono_type_native_stack_size (csig->params [k], &align);
171 size = mono_type_stack_size (csig->params [k], &align);
173 /* ignore alignment for now */
176 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
177 arg_info [k].pad = pad;
179 arg_info [k + 1].pad = 0;
180 arg_info [k + 1].size = size;
182 arg_info [k + 1].offset = offset;
186 align = MONO_ARCH_FRAME_ALIGNMENT;
187 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
188 arg_info [k].pad = pad;
194 * Initialize the cpu to execute managed code.
197 mono_arch_cpu_init (void)
202 * This function returns the optimizations supported on this cpu.
205 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
209 /* no arm-specific optimizations yet */
215 is_regsize_var (MonoType *t) {
218 t = mono_type_get_underlying_type (t);
225 case MONO_TYPE_FNPTR:
227 case MONO_TYPE_OBJECT:
228 case MONO_TYPE_STRING:
229 case MONO_TYPE_CLASS:
230 case MONO_TYPE_SZARRAY:
231 case MONO_TYPE_ARRAY:
233 case MONO_TYPE_GENERICINST:
234 if (!mono_type_generic_inst_is_valuetype (t))
237 case MONO_TYPE_VALUETYPE:
244 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
249 for (i = 0; i < cfg->num_varinfo; i++) {
250 MonoInst *ins = cfg->varinfo [i];
251 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
254 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
257 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
260 /* we can only allocate 32 bit values */
261 if (is_regsize_var (ins->inst_vtype)) {
262 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
263 g_assert (i == vmv->idx);
264 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
271 #define USE_EXTRA_TEMPS 0
274 mono_arch_get_global_int_regs (MonoCompile *cfg)
277 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V1));
278 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V2));
279 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V3));
280 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V4));
281 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V5));
282 /*regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V6));*/
283 /*regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V7));*/
289 * mono_arch_regalloc_cost:
291 * Return the cost, in number of memory references, of the action of
292 * allocating the variable VMV into a register during global register
296 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
303 mono_arch_flush_icache (guint8 *code, gint size)
305 __asm __volatile ("mov r0, %0\n"
308 "swi 0x9f0002 @ sys_cacheflush"
310 : "r" (code), "r" (code + size), "r" (0)
311 : "r0", "r1", "r3" );
315 #define NOT_IMPLEMENTED(x) \
316 g_error ("FIXME: %s is not yet implemented. (trampoline)", x);
329 guint16 vtsize; /* in param area */
331 guint8 regtype : 4; /* 0 general, 1 basereg, 2 floating point register, see RegType* */
332 guint8 size : 4; /* 1, 2, 4, 8, or regs used by RegTypeStructByVal */
347 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
350 if (*gr > ARMREG_R3) {
351 ainfo->offset = *stack_size;
352 ainfo->reg = ARMREG_SP; /* in the caller */
353 ainfo->regtype = RegTypeBase;
359 if (*gr == ARMREG_R3) {
360 /* first word in r3 and the second on the stack */
361 ainfo->offset = *stack_size;
362 ainfo->reg = ARMREG_SP; /* in the caller */
363 ainfo->regtype = RegTypeBaseGen;
365 } else if (*gr > ARMREG_R3) {
368 ainfo->offset = *stack_size;
369 ainfo->reg = ARMREG_SP; /* in the caller */
370 ainfo->regtype = RegTypeBase;
383 calculate_sizes (MonoMethodSignature *sig, gboolean is_pinvoke)
386 int n = sig->hasthis + sig->param_count;
388 guint32 stack_size = 0;
389 CallInfo *cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * n);
393 /* FIXME: handle returning a struct */
394 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
395 add_general (&gr, &stack_size, &cinfo->ret, TRUE);
396 cinfo->struct_ret = ARMREG_R0;
401 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
404 DEBUG(printf("params: %d\n", sig->param_count));
405 for (i = 0; i < sig->param_count; ++i) {
406 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
407 /* Prevent implicit arguments and sig_cookie from
408 being passed in registers */
410 /* Emit the signature cookie just before the implicit arguments */
411 add_general (&gr, &stack_size, &cinfo->sig_cookie, TRUE);
413 DEBUG(printf("param %d: ", i));
414 if (sig->params [i]->byref) {
415 DEBUG(printf("byref\n"));
416 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
420 simpletype = mono_type_get_underlying_type (sig->params [i])->type;
421 switch (simpletype) {
422 case MONO_TYPE_BOOLEAN:
425 cinfo->args [n].size = 1;
426 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
432 cinfo->args [n].size = 2;
433 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
438 cinfo->args [n].size = 4;
439 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
445 case MONO_TYPE_FNPTR:
446 case MONO_TYPE_CLASS:
447 case MONO_TYPE_OBJECT:
448 case MONO_TYPE_STRING:
449 case MONO_TYPE_SZARRAY:
450 case MONO_TYPE_ARRAY:
452 cinfo->args [n].size = sizeof (gpointer);
453 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
456 case MONO_TYPE_GENERICINST:
457 if (!mono_type_generic_inst_is_valuetype (sig->params [i])) {
458 cinfo->args [n].size = sizeof (gpointer);
459 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
464 case MONO_TYPE_TYPEDBYREF:
465 case MONO_TYPE_VALUETYPE: {
470 if (simpletype == MONO_TYPE_TYPEDBYREF) {
471 size = sizeof (MonoTypedRef);
473 MonoClass *klass = mono_class_from_mono_type (sig->params [i]);
475 size = mono_class_native_size (klass, NULL);
477 size = mono_class_value_size (klass, NULL);
479 DEBUG(printf ("load %d bytes struct\n",
480 mono_class_native_size (sig->params [i]->data.klass, NULL)));
483 align_size += (sizeof (gpointer) - 1);
484 align_size &= ~(sizeof (gpointer) - 1);
485 nwords = (align_size + sizeof (gpointer) -1 ) / sizeof (gpointer);
486 cinfo->args [n].regtype = RegTypeStructByVal;
487 /* FIXME: align gr and stack_size if needed */
488 if (gr > ARMREG_R3) {
489 cinfo->args [n].size = 0;
490 cinfo->args [n].vtsize = nwords;
492 int rest = ARMREG_R3 - gr + 1;
493 int n_in_regs = rest >= nwords? nwords: rest;
494 cinfo->args [n].size = n_in_regs;
495 cinfo->args [n].vtsize = nwords - n_in_regs;
496 cinfo->args [n].reg = gr;
499 cinfo->args [n].offset = stack_size;
500 /*g_print ("offset for arg %d at %d\n", n, stack_size);*/
501 stack_size += nwords * sizeof (gpointer);
508 cinfo->args [n].size = 8;
509 add_general (&gr, &stack_size, cinfo->args + n, FALSE);
513 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
518 simpletype = mono_type_get_underlying_type (sig->ret)->type;
519 switch (simpletype) {
520 case MONO_TYPE_BOOLEAN:
531 case MONO_TYPE_FNPTR:
532 case MONO_TYPE_CLASS:
533 case MONO_TYPE_OBJECT:
534 case MONO_TYPE_SZARRAY:
535 case MONO_TYPE_ARRAY:
536 case MONO_TYPE_STRING:
537 cinfo->ret.reg = ARMREG_R0;
541 cinfo->ret.reg = ARMREG_R0;
545 cinfo->ret.reg = ARMREG_R0;
546 /* FIXME: cinfo->ret.reg = ???;
547 cinfo->ret.regtype = RegTypeFP;*/
549 case MONO_TYPE_GENERICINST:
550 if (!mono_type_generic_inst_is_valuetype (sig->ret)) {
551 cinfo->ret.reg = ARMREG_R0;
555 case MONO_TYPE_VALUETYPE:
557 case MONO_TYPE_TYPEDBYREF:
561 g_error ("Can't handle as return value 0x%x", sig->ret->type);
565 /* align stack size to 8 */
566 DEBUG (printf (" stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
567 stack_size = (stack_size + 7) & ~7;
569 cinfo->stack_usage = stack_size;
575 * Set var information according to the calling convention. arm version.
576 * The locals var stuff should most likely be split in another method.
579 mono_arch_allocate_vars (MonoCompile *m)
581 MonoMethodSignature *sig;
582 MonoMethodHeader *header;
584 int i, offset, size, align, curinst;
585 int frame_reg = ARMREG_FP;
587 /* FIXME: this will change when we use FP as gcc does */
588 m->flags |= MONO_CFG_HAS_SPILLUP;
590 /* allow room for the vararg method args: void* and long/double */
591 if (mono_jit_trace_calls != NULL && mono_trace_eval (m->method))
592 m->param_area = MAX (m->param_area, sizeof (gpointer)*8);
594 header = mono_method_get_header (m->method);
597 * We use the frame register also for any method that has
598 * exception clauses. This way, when the handlers are called,
599 * the code will reference local variables using the frame reg instead of
600 * the stack pointer: if we had to restore the stack pointer, we'd
601 * corrupt the method frames that are already on the stack (since
602 * filters get called before stack unwinding happens) when the filter
603 * code would call any method (this also applies to finally etc.).
605 if ((m->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
606 frame_reg = ARMREG_FP;
607 m->frame_reg = frame_reg;
608 if (frame_reg != ARMREG_SP) {
609 m->used_int_regs |= 1 << frame_reg;
612 sig = mono_method_signature (m->method);
616 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
617 m->ret->opcode = OP_REGVAR;
618 m->ret->inst_c0 = ARMREG_R0;
620 /* FIXME: handle long and FP values */
621 switch (mono_type_get_underlying_type (sig->ret)->type) {
625 m->ret->opcode = OP_REGVAR;
626 m->ret->inst_c0 = ARMREG_R0;
630 /* local vars are at a positive offset from the stack pointer */
632 * also note that if the function uses alloca, we use FP
633 * to point at the local variables.
635 offset = 0; /* linkage area */
636 /* align the offset to 16 bytes: not sure this is needed here */
638 //offset &= ~(8 - 1);
640 /* add parameter area size for called functions */
641 offset += m->param_area;
644 if (m->flags & MONO_CFG_HAS_FPOUT)
647 /* allow room to save the return value */
648 if (mono_jit_trace_calls != NULL && mono_trace_eval (m->method))
651 /* the MonoLMF structure is stored just below the stack pointer */
653 if (sig->call_convention == MONO_CALL_VARARG) {
657 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
659 offset += sizeof(gpointer) - 1;
660 offset &= ~(sizeof(gpointer) - 1);
661 inst->inst_offset = offset;
662 inst->opcode = OP_REGOFFSET;
663 inst->inst_basereg = frame_reg;
664 offset += sizeof(gpointer);
665 if (sig->call_convention == MONO_CALL_VARARG)
666 m->sig_cookie += sizeof (gpointer);
669 curinst = m->locals_start;
670 for (i = curinst; i < m->num_varinfo; ++i) {
671 inst = m->varinfo [i];
672 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR)
675 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
676 * pinvoke wrappers when they call functions returning structure */
677 if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
678 size = mono_class_native_size (mono_class_from_mono_type (inst->inst_vtype), &align);
680 size = mono_type_size (inst->inst_vtype, &align);
682 /* FIXME: if a structure is misaligned, our memcpy doesn't work,
683 * since it loads/stores misaligned words, which don't do the right thing.
685 if (align < 4 && size >= 4)
688 offset &= ~(align - 1);
689 inst->inst_offset = offset;
690 inst->opcode = OP_REGOFFSET;
691 inst->inst_basereg = frame_reg;
693 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
698 inst = m->varinfo [curinst];
699 if (inst->opcode != OP_REGVAR) {
700 inst->opcode = OP_REGOFFSET;
701 inst->inst_basereg = frame_reg;
702 offset += sizeof (gpointer) - 1;
703 offset &= ~(sizeof (gpointer) - 1);
704 inst->inst_offset = offset;
705 offset += sizeof (gpointer);
706 if (sig->call_convention == MONO_CALL_VARARG)
707 m->sig_cookie += sizeof (gpointer);
712 for (i = 0; i < sig->param_count; ++i) {
713 inst = m->varinfo [curinst];
714 if (inst->opcode != OP_REGVAR) {
715 inst->opcode = OP_REGOFFSET;
716 inst->inst_basereg = frame_reg;
717 size = mono_type_size (sig->params [i], &align);
718 /* FIXME: if a structure is misaligned, our memcpy doesn't work,
719 * since it loads/stores misaligned words, which don't do the right thing.
721 if (align < 4 && size >= 4)
724 offset &= ~(align - 1);
725 inst->inst_offset = offset;
727 if ((sig->call_convention == MONO_CALL_VARARG) && (i < sig->sentinelpos))
728 m->sig_cookie += size;
733 /* align the offset to 8 bytes */
738 m->stack_offset = offset;
742 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
743 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
747 * take the arguments and generate the arch-specific
748 * instructions to properly call the function in call.
749 * This includes pushing, moving arguments to the right register
751 * Issue: who does the spilling if needed, and when?
754 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
756 MonoMethodSignature *sig;
761 sig = call->signature;
762 n = sig->param_count + sig->hasthis;
764 cinfo = calculate_sizes (sig, sig->pinvoke);
765 if (cinfo->struct_ret)
766 call->used_iregs |= 1 << cinfo->struct_ret;
768 for (i = 0; i < n; ++i) {
769 ainfo = cinfo->args + i;
770 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
772 cfg->disable_aot = TRUE;
774 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
775 sig_arg->inst_p0 = call->signature;
777 MONO_INST_NEW (cfg, arg, OP_OUTARG);
778 arg->inst_imm = cinfo->sig_cookie.offset;
779 arg->inst_left = sig_arg;
781 /* prepend, so they get reversed */
782 arg->next = call->out_args;
783 call->out_args = arg;
785 if (is_virtual && i == 0) {
786 /* the argument will be attached to the call instrucion */
788 call->used_iregs |= 1 << ainfo->reg;
790 MONO_INST_NEW (cfg, arg, OP_OUTARG);
792 arg->cil_code = in->cil_code;
794 arg->inst_right = (MonoInst*)call;
795 arg->type = in->type;
796 /* prepend, we'll need to reverse them later */
797 arg->next = call->out_args;
798 call->out_args = arg;
799 if (ainfo->regtype == RegTypeGeneral) {
800 arg->backend.reg3 = ainfo->reg;
801 call->used_iregs |= 1 << ainfo->reg;
802 if (arg->type == STACK_I8)
803 call->used_iregs |= 1 << (ainfo->reg + 1);
804 if (arg->type == STACK_R8) {
805 if (ainfo->size == 4) {
806 arg->opcode = OP_OUTARG_R4;
808 call->used_iregs |= 1 << (ainfo->reg + 1);
810 cfg->flags |= MONO_CFG_HAS_FPOUT;
812 } else if (ainfo->regtype == RegTypeStructByAddr) {
813 /* FIXME: where si the data allocated? */
814 arg->backend.reg3 = ainfo->reg;
815 call->used_iregs |= 1 << ainfo->reg;
816 g_assert_not_reached ();
817 } else if (ainfo->regtype == RegTypeStructByVal) {
819 /* mark the used regs */
820 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
821 call->used_iregs |= 1 << (ainfo->reg + cur_reg);
823 arg->opcode = OP_OUTARG_VT;
824 /* vtsize and offset have just 12 bits of encoding in number of words */
825 g_assert (((ainfo->vtsize | (ainfo->offset / 4)) & 0xfffff000) == 0);
826 arg->backend.arg_info = ainfo->reg | (ainfo->size << 4) | (ainfo->vtsize << 8) | ((ainfo->offset / 4) << 20);
827 } else if (ainfo->regtype == RegTypeBase) {
828 arg->opcode = OP_OUTARG_MEMBASE;
829 arg->backend.arg_info = (ainfo->offset << 8) | ainfo->size;
830 } else if (ainfo->regtype == RegTypeBaseGen) {
831 call->used_iregs |= 1 << ARMREG_R3;
832 arg->opcode = OP_OUTARG_MEMBASE;
833 arg->backend.arg_info = (ainfo->offset << 8) | 0xff;
834 if (arg->type == STACK_R8)
835 cfg->flags |= MONO_CFG_HAS_FPOUT;
836 } else if (ainfo->regtype == RegTypeFP) {
837 arg->backend.reg3 = ainfo->reg;
838 /* FP args are passed in int regs */
839 call->used_iregs |= 1 << ainfo->reg;
840 if (ainfo->size == 8) {
841 arg->opcode = OP_OUTARG_R8;
842 call->used_iregs |= 1 << (ainfo->reg + 1);
844 arg->opcode = OP_OUTARG_R4;
846 cfg->flags |= MONO_CFG_HAS_FPOUT;
848 g_assert_not_reached ();
853 * Reverse the call->out_args list.
856 MonoInst *prev = NULL, *list = call->out_args, *next;
863 call->out_args = prev;
865 call->stack_usage = cinfo->stack_usage;
866 cfg->param_area = MAX (cfg->param_area, cinfo->stack_usage);
867 cfg->flags |= MONO_CFG_HAS_CALLS;
869 * should set more info in call, such as the stack space
870 * used by the args that needs to be added back to esp
878 * Allow tracing to work with this interface (with an optional argument)
882 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
886 code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->method);
887 ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0); /* NULL ebp for now */
888 code = mono_arm_emit_load_imm (code, ARMREG_R2, (guint32)func);
889 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
890 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R2);
903 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
906 int save_mode = SAVE_NONE;
908 MonoMethod *method = cfg->method;
909 int rtype = mono_type_get_underlying_type (mono_method_signature (method)->ret)->type;
910 int save_offset = cfg->param_area;
914 offset = code - cfg->native_code;
915 /* we need about 16 instructions */
916 if (offset > (cfg->code_size - 16 * 4)) {
918 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
919 code = cfg->native_code + offset;
923 /* special case string .ctor icall */
924 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
925 save_mode = SAVE_ONE;
927 save_mode = SAVE_NONE;
931 save_mode = SAVE_TWO;
937 case MONO_TYPE_VALUETYPE:
938 save_mode = SAVE_STRUCT;
941 save_mode = SAVE_ONE;
947 ARM_STR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
948 ARM_STR_IMM (code, ARMREG_R1, cfg->frame_reg, save_offset + 4);
949 if (enable_arguments) {
950 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_R1);
951 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
955 ARM_STR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
956 if (enable_arguments) {
957 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
961 /* FIXME: what reg? */
962 if (enable_arguments) {
963 /* FIXME: what reg? */
967 if (enable_arguments) {
968 /* FIXME: get the actual address */
969 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
977 code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->method);
978 code = mono_arm_emit_load_imm (code, ARMREG_IP, (guint32)func);
979 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
980 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
984 ARM_LDR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
985 ARM_LDR_IMM (code, ARMREG_R1, cfg->frame_reg, save_offset + 4);
988 ARM_LDR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
1002 * The immediate field for cond branches is big enough for all reasonable methods
1004 #define EMIT_COND_BRANCH_FLAGS(ins,condcode) \
1005 if (ins->flags & MONO_INST_BRLABEL) { \
1006 if (0 && ins->inst_i0->inst_c0) { \
1007 ARM_B_COND (code, (condcode), (code - cfg->native_code + ins->inst_i0->inst_c0) & 0xffffff); \
1009 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1010 ARM_B_COND (code, (condcode), 0); \
1013 if (0 && ins->inst_true_bb->native_offset) { \
1014 ARM_B_COND (code, (condcode), (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffffff); \
1016 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1017 ARM_B_COND (code, (condcode), 0); \
1021 #define EMIT_COND_BRANCH(ins,cond) EMIT_COND_BRANCH_FLAGS(ins, branch_cc_table [(cond)])
1023 /* emit an exception if condition is fail
1025 * We assign the extra code used to throw the implicit exceptions
1026 * to cfg->bb_exit as far as the big branch handling is concerned
1028 #define EMIT_COND_SYSTEM_EXCEPTION_FLAGS(condcode,exc_name) \
1030 mono_add_patch_info (cfg, code - cfg->native_code, \
1031 MONO_PATCH_INFO_EXC, exc_name); \
1032 ARM_BL_COND (code, (condcode), 0); \
1035 #define EMIT_COND_SYSTEM_EXCEPTION(cond,exc_name) EMIT_COND_SYSTEM_EXCEPTION_FLAGS(branch_cc_table [(cond)], (exc_name))
1038 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1040 MonoInst *ins, *last_ins = NULL;
1045 switch (ins->opcode) {
1047 /* remove unnecessary multiplication with 1 */
1048 if (ins->inst_imm == 1) {
1049 if (ins->dreg != ins->sreg1) {
1050 ins->opcode = OP_MOVE;
1052 last_ins->next = ins->next;
1057 int power2 = mono_is_power_of_two (ins->inst_imm);
1059 ins->opcode = OP_SHL_IMM;
1060 ins->inst_imm = power2;
1064 case OP_LOAD_MEMBASE:
1065 case OP_LOADI4_MEMBASE:
1067 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1068 * OP_LOAD_MEMBASE offset(basereg), reg
1070 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1071 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1072 ins->inst_basereg == last_ins->inst_destbasereg &&
1073 ins->inst_offset == last_ins->inst_offset) {
1074 if (ins->dreg == last_ins->sreg1) {
1075 last_ins->next = ins->next;
1079 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1080 ins->opcode = OP_MOVE;
1081 ins->sreg1 = last_ins->sreg1;
1085 * Note: reg1 must be different from the basereg in the second load
1086 * OP_LOAD_MEMBASE offset(basereg), reg1
1087 * OP_LOAD_MEMBASE offset(basereg), reg2
1089 * OP_LOAD_MEMBASE offset(basereg), reg1
1090 * OP_MOVE reg1, reg2
1092 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1093 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1094 ins->inst_basereg != last_ins->dreg &&
1095 ins->inst_basereg == last_ins->inst_basereg &&
1096 ins->inst_offset == last_ins->inst_offset) {
1098 if (ins->dreg == last_ins->dreg) {
1099 last_ins->next = ins->next;
1103 ins->opcode = OP_MOVE;
1104 ins->sreg1 = last_ins->dreg;
1107 //g_assert_not_reached ();
1111 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1112 * OP_LOAD_MEMBASE offset(basereg), reg
1114 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1115 * OP_ICONST reg, imm
1117 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1118 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1119 ins->inst_basereg == last_ins->inst_destbasereg &&
1120 ins->inst_offset == last_ins->inst_offset) {
1121 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1122 ins->opcode = OP_ICONST;
1123 ins->inst_c0 = last_ins->inst_imm;
1124 g_assert_not_reached (); // check this rule
1128 case OP_LOADU1_MEMBASE:
1129 case OP_LOADI1_MEMBASE:
1130 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1131 ins->inst_basereg == last_ins->inst_destbasereg &&
1132 ins->inst_offset == last_ins->inst_offset) {
1133 if (ins->dreg == last_ins->sreg1) {
1134 last_ins->next = ins->next;
1138 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1139 ins->opcode = OP_MOVE;
1140 ins->sreg1 = last_ins->sreg1;
1144 case OP_LOADU2_MEMBASE:
1145 case OP_LOADI2_MEMBASE:
1146 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1147 ins->inst_basereg == last_ins->inst_destbasereg &&
1148 ins->inst_offset == last_ins->inst_offset) {
1149 if (ins->dreg == last_ins->sreg1) {
1150 last_ins->next = ins->next;
1154 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1155 ins->opcode = OP_MOVE;
1156 ins->sreg1 = last_ins->sreg1;
1164 ins->opcode = OP_MOVE;
1168 if (ins->dreg == ins->sreg1) {
1170 last_ins->next = ins->next;
1175 * OP_MOVE sreg, dreg
1176 * OP_MOVE dreg, sreg
1178 if (last_ins && last_ins->opcode == OP_MOVE &&
1179 ins->sreg1 == last_ins->dreg &&
1180 ins->dreg == last_ins->sreg1) {
1181 last_ins->next = ins->next;
1190 bb->last_ins = last_ins;
1194 * the branch_cc_table should maintain the order of these
1208 branch_cc_table [] = {
1224 insert_after_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst *to_insert)
1228 bb->code = to_insert;
1229 to_insert->next = ins;
1231 to_insert->next = ins->next;
1232 ins->next = to_insert;
1236 #define NEW_INS(cfg,dest,op) do { \
1237 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
1238 (dest)->opcode = (op); \
1239 insert_after_ins (bb, last_ins, (dest)); \
1243 map_to_reg_reg_op (int op)
1252 case OP_COMPARE_IMM:
1266 case OP_LOAD_MEMBASE:
1267 return OP_LOAD_MEMINDEX;
1268 case OP_LOADI4_MEMBASE:
1269 return OP_LOADI4_MEMINDEX;
1270 case OP_LOADU4_MEMBASE:
1271 return OP_LOADU4_MEMINDEX;
1272 case OP_LOADU1_MEMBASE:
1273 return OP_LOADU1_MEMINDEX;
1274 case OP_LOADI2_MEMBASE:
1275 return OP_LOADI2_MEMINDEX;
1276 case OP_LOADU2_MEMBASE:
1277 return OP_LOADU2_MEMINDEX;
1278 case OP_LOADI1_MEMBASE:
1279 return OP_LOADI1_MEMINDEX;
1280 case OP_STOREI1_MEMBASE_REG:
1281 return OP_STOREI1_MEMINDEX;
1282 case OP_STOREI2_MEMBASE_REG:
1283 return OP_STOREI2_MEMINDEX;
1284 case OP_STOREI4_MEMBASE_REG:
1285 return OP_STOREI4_MEMINDEX;
1286 case OP_STORE_MEMBASE_REG:
1287 return OP_STORE_MEMINDEX;
1288 case OP_STORER4_MEMBASE_REG:
1289 return OP_STORER4_MEMINDEX;
1290 case OP_STORER8_MEMBASE_REG:
1291 return OP_STORER8_MEMINDEX;
1292 case OP_STORE_MEMBASE_IMM:
1293 return OP_STORE_MEMBASE_REG;
1294 case OP_STOREI1_MEMBASE_IMM:
1295 return OP_STOREI1_MEMBASE_REG;
1296 case OP_STOREI2_MEMBASE_IMM:
1297 return OP_STOREI2_MEMBASE_REG;
1298 case OP_STOREI4_MEMBASE_IMM:
1299 return OP_STOREI4_MEMBASE_REG;
1301 g_assert_not_reached ();
1305 * Remove from the instruction list the instructions that can't be
1306 * represented with very simple instructions with no register
1310 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1312 MonoInst *ins, *temp, *last_ins = NULL;
1313 int rot_amount, imm8, low_imm;
1315 /* setup the virtual reg allocator */
1316 if (bb->max_ireg > cfg->rs->next_vireg)
1317 cfg->rs->next_vireg = bb->max_ireg;
1322 switch (ins->opcode) {
1326 case OP_COMPARE_IMM:
1333 if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount)) < 0) {
1334 NEW_INS (cfg, temp, OP_ICONST);
1335 temp->inst_c0 = ins->inst_imm;
1336 temp->dreg = mono_regstate_next_int (cfg->rs);
1337 ins->sreg2 = temp->dreg;
1338 ins->opcode = map_to_reg_reg_op (ins->opcode);
1342 if (ins->inst_imm == 1) {
1343 ins->opcode = OP_MOVE;
1346 if (ins->inst_imm == 0) {
1347 ins->opcode = OP_ICONST;
1351 imm8 = mono_is_power_of_two (ins->inst_imm);
1353 ins->opcode = OP_SHL_IMM;
1354 ins->inst_imm = imm8;
1357 NEW_INS (cfg, temp, OP_ICONST);
1358 temp->inst_c0 = ins->inst_imm;
1359 temp->dreg = mono_regstate_next_int (cfg->rs);
1360 ins->sreg2 = temp->dreg;
1361 ins->opcode = CEE_MUL;
1363 case OP_LOAD_MEMBASE:
1364 case OP_LOADI4_MEMBASE:
1365 case OP_LOADU4_MEMBASE:
1366 case OP_LOADU1_MEMBASE:
1367 /* we can do two things: load the immed in a register
1368 * and use an indexed load, or see if the immed can be
1369 * represented as an ad_imm + a load with a smaller offset
1370 * that fits. We just do the first for now, optimize later.
1372 if (arm_is_imm12 (ins->inst_offset))
1374 NEW_INS (cfg, temp, OP_ICONST);
1375 temp->inst_c0 = ins->inst_offset;
1376 temp->dreg = mono_regstate_next_int (cfg->rs);
1377 ins->sreg2 = temp->dreg;
1378 ins->opcode = map_to_reg_reg_op (ins->opcode);
1380 case OP_LOADI2_MEMBASE:
1381 case OP_LOADU2_MEMBASE:
1382 case OP_LOADI1_MEMBASE:
1383 if (arm_is_imm8 (ins->inst_offset))
1385 NEW_INS (cfg, temp, OP_ICONST);
1386 temp->inst_c0 = ins->inst_offset;
1387 temp->dreg = mono_regstate_next_int (cfg->rs);
1388 ins->sreg2 = temp->dreg;
1389 ins->opcode = map_to_reg_reg_op (ins->opcode);
1391 case OP_LOADR4_MEMBASE:
1392 case OP_LOADR8_MEMBASE:
1393 if (arm_is_fpimm8 (ins->inst_offset))
1395 low_imm = ins->inst_offset & 0x1ff;
1396 if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_offset & ~0x1ff, &rot_amount)) >= 0) {
1397 NEW_INS (cfg, temp, OP_ADD_IMM);
1398 temp->inst_imm = ins->inst_offset & ~0x1ff;
1399 temp->sreg1 = ins->inst_basereg;
1400 temp->dreg = mono_regstate_next_int (cfg->rs);
1401 ins->inst_basereg = temp->dreg;
1402 ins->inst_offset = low_imm;
1405 /* VFP/FPA doesn't have indexed load instructions */
1406 g_assert_not_reached ();
1408 case OP_STORE_MEMBASE_REG:
1409 case OP_STOREI4_MEMBASE_REG:
1410 case OP_STOREI1_MEMBASE_REG:
1411 if (arm_is_imm12 (ins->inst_offset))
1413 NEW_INS (cfg, temp, OP_ICONST);
1414 temp->inst_c0 = ins->inst_offset;
1415 temp->dreg = mono_regstate_next_int (cfg->rs);
1416 ins->sreg2 = temp->dreg;
1417 ins->opcode = map_to_reg_reg_op (ins->opcode);
1419 case OP_STOREI2_MEMBASE_REG:
1420 if (arm_is_imm8 (ins->inst_offset))
1422 NEW_INS (cfg, temp, OP_ICONST);
1423 temp->inst_c0 = ins->inst_offset;
1424 temp->dreg = mono_regstate_next_int (cfg->rs);
1425 ins->sreg2 = temp->dreg;
1426 ins->opcode = map_to_reg_reg_op (ins->opcode);
1428 case OP_STORER4_MEMBASE_REG:
1429 case OP_STORER8_MEMBASE_REG:
1430 if (arm_is_fpimm8 (ins->inst_offset))
1432 low_imm = ins->inst_offset & 0x1ff;
1433 if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_offset & ~ 0x1ff, &rot_amount)) >= 0 && arm_is_fpimm8 (low_imm)) {
1434 NEW_INS (cfg, temp, OP_ADD_IMM);
1435 temp->inst_imm = ins->inst_offset & ~0x1ff;
1436 temp->sreg1 = ins->inst_destbasereg;
1437 temp->dreg = mono_regstate_next_int (cfg->rs);
1438 ins->inst_destbasereg = temp->dreg;
1439 ins->inst_offset = low_imm;
1442 /*g_print ("fail with: %d (%d, %d)\n", ins->inst_offset, ins->inst_offset & ~0x1ff, low_imm);*/
1443 /* VFP/FPA doesn't have indexed store instructions */
1444 g_assert_not_reached ();
1446 case OP_STORE_MEMBASE_IMM:
1447 case OP_STOREI1_MEMBASE_IMM:
1448 case OP_STOREI2_MEMBASE_IMM:
1449 case OP_STOREI4_MEMBASE_IMM:
1450 NEW_INS (cfg, temp, OP_ICONST);
1451 temp->inst_c0 = ins->inst_imm;
1452 temp->dreg = mono_regstate_next_int (cfg->rs);
1453 ins->sreg1 = temp->dreg;
1454 ins->opcode = map_to_reg_reg_op (ins->opcode);
1456 goto loop_start; /* make it handle the possibly big ins->inst_offset */
1461 bb->last_ins = last_ins;
1462 bb->max_ireg = cfg->rs->next_vireg;
1467 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1471 mono_arch_lowering_pass (cfg, bb);
1472 mono_local_regalloc (cfg, bb);
1476 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
1478 /* sreg is a float, dreg is an integer reg */
1480 ARM_FIXZ (code, dreg, sreg);
1481 #elif defined(ARM_FPU_VFP)
1483 ARM_TOSIZD (code, ARM_VFP_F0, sreg);
1485 ARM_TOUIZD (code, ARM_VFP_F0, sreg);
1486 ARM_FMRS (code, dreg, ARM_VFP_F0);
1490 ARM_AND_REG_IMM8 (code, dreg, dreg, 0xff);
1491 else if (size == 2) {
1492 ARM_SHL_IMM (code, dreg, dreg, 16);
1493 ARM_SHR_IMM (code, dreg, dreg, 16);
1497 ARM_SHL_IMM (code, dreg, dreg, 24);
1498 ARM_SAR_IMM (code, dreg, dreg, 24);
1499 } else if (size == 2) {
1500 ARM_SHL_IMM (code, dreg, dreg, 16);
1501 ARM_SAR_IMM (code, dreg, dreg, 16);
1509 const guchar *target;
1514 #define is_call_imm(diff) ((gint)(diff) >= -33554432 && (gint)(diff) <= 33554431)
1517 search_thunk_slot (void *data, int csize, int bsize, void *user_data) {
1518 PatchData *pdata = (PatchData*)user_data;
1519 guchar *code = data;
1520 guint32 *thunks = data;
1521 guint32 *endthunks = (guint32*)(code + bsize);
1523 int difflow, diffhigh;
1525 /* always ensure a call from pdata->code can reach to the thunks without further thunks */
1526 difflow = (char*)pdata->code - (char*)thunks;
1527 diffhigh = (char*)pdata->code - (char*)endthunks;
1528 if (!((is_call_imm (thunks) && is_call_imm (endthunks)) || (is_call_imm (difflow) && is_call_imm (diffhigh))))
1532 * The thunk is composed of 3 words:
1533 * load constant from thunks [2] into ARM_IP
1536 * Note that the LR register is already setup
1538 //g_print ("thunk nentries: %d\n", ((char*)endthunks - (char*)thunks)/16);
1539 if ((pdata->found == 2) || (pdata->code >= code && pdata->code <= code + csize)) {
1540 while (thunks < endthunks) {
1541 //g_print ("looking for target: %p at %p (%08x-%08x)\n", pdata->target, thunks, thunks [0], thunks [1]);
1542 if (thunks [2] == (guint32)pdata->target) {
1543 arm_patch (pdata->code, (guchar*)thunks);
1544 mono_arch_flush_icache (pdata->code, 4);
1547 } else if ((thunks [0] == 0) && (thunks [1] == 0) && (thunks [2] == 0)) {
1548 /* found a free slot instead: emit thunk */
1549 code = (guchar*)thunks;
1550 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
1551 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
1552 thunks [2] = (guint32)pdata->target;
1553 mono_arch_flush_icache ((guchar*)thunks, 12);
1555 arm_patch (pdata->code, (guchar*)thunks);
1556 mono_arch_flush_icache (pdata->code, 4);
1560 /* skip 12 bytes, the size of the thunk */
1564 //g_print ("failed thunk lookup for %p from %p at %p (%d entries)\n", pdata->target, pdata->code, data, count);
1570 handle_thunk (int absolute, guchar *code, const guchar *target) {
1571 MonoDomain *domain = mono_domain_get ();
1575 pdata.target = target;
1576 pdata.absolute = absolute;
1579 mono_domain_lock (domain);
1580 mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
1583 /* this uses the first available slot */
1585 mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
1587 mono_domain_unlock (domain);
1589 if (pdata.found != 1)
1590 g_print ("thunk failed for %p from %p\n", target, code);
1591 g_assert (pdata.found == 1);
1595 arm_patch (guchar *code, const guchar *target)
1597 guint32 ins = *(guint32*)code;
1598 guint32 prim = (ins >> 25) & 7;
1600 //g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
1601 if (prim == 5) { /* 101b */
1602 /* the diff starts 8 bytes from the branch opcode */
1603 gint diff = target - code - 8;
1605 if (diff <= 33554431) {
1607 ins = (ins & 0xff000000) | diff;
1608 *(guint32*)code = ins;
1612 /* diff between 0 and -33554432 */
1613 if (diff >= -33554432) {
1615 ins = (ins & 0xff000000) | (diff & ~0xff000000);
1616 *(guint32*)code = ins;
1621 handle_thunk (TRUE, code, target);
1626 if ((ins & 0x0ffffff0) == 0x12fff10) {
1627 /* branch and exchange: the address is constructed in a reg */
1628 g_assert_not_reached ();
1631 guint32 *tmp = ccode;
1632 ARM_LDR_IMM (tmp, ARMREG_IP, ARMREG_PC, 0);
1633 ARM_MOV_REG_REG (tmp, ARMREG_LR, ARMREG_PC);
1634 ARM_MOV_REG_REG (tmp, ARMREG_PC, ARMREG_IP);
1635 if (ins == ccode [2]) {
1636 tmp = (guint32*)code;
1637 tmp [-1] = (guint32)target;
1640 if (ins == ccode [0]) {
1641 tmp = (guint32*)code;
1642 tmp [2] = (guint32)target;
1645 g_assert_not_reached ();
1647 // g_print ("patched with 0x%08x\n", ins);
1651 * Return the >= 0 uimm8 value if val can be represented with a byte + rotation
1652 * (with the rotation amount in *rot_amount. rot_amount is already adjusted
1653 * to be used with the emit macros.
1654 * Return -1 otherwise.
1657 mono_arm_is_rotated_imm8 (guint32 val, gint *rot_amount)
1660 for (i = 0; i < 31; i+= 2) {
1661 res = (val << (32 - i)) | (val >> i);
1664 *rot_amount = i? 32 - i: 0;
1671 * Emits in code a sequence of instructions that load the value 'val'
1672 * into the dreg register. Uses at most 4 instructions.
1675 mono_arm_emit_load_imm (guint8 *code, int dreg, guint32 val)
1677 int imm8, rot_amount;
1679 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
1680 /* skip the constant pool */
1686 if ((imm8 = mono_arm_is_rotated_imm8 (val, &rot_amount)) >= 0) {
1687 ARM_MOV_REG_IMM (code, dreg, imm8, rot_amount);
1688 } else if ((imm8 = mono_arm_is_rotated_imm8 (~val, &rot_amount)) >= 0) {
1689 ARM_MVN_REG_IMM (code, dreg, imm8, rot_amount);
1692 ARM_MOV_REG_IMM8 (code, dreg, (val & 0xFF));
1694 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF00) >> 8, 24);
1696 if (val & 0xFF0000) {
1697 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
1699 if (val & 0xFF000000) {
1700 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
1702 } else if (val & 0xFF00) {
1703 ARM_MOV_REG_IMM (code, dreg, (val & 0xFF00) >> 8, 24);
1704 if (val & 0xFF0000) {
1705 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
1707 if (val & 0xFF000000) {
1708 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
1710 } else if (val & 0xFF0000) {
1711 ARM_MOV_REG_IMM (code, dreg, (val & 0xFF0000) >> 16, 16);
1712 if (val & 0xFF000000) {
1713 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
1716 //g_assert_not_reached ();
1722 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
1727 guint8 *code = cfg->native_code + cfg->code_len;
1728 MonoInst *last_ins = NULL;
1729 guint last_offset = 0;
1731 int imm8, rot_amount;
1733 if (cfg->opt & MONO_OPT_PEEPHOLE)
1734 peephole_pass (cfg, bb);
1736 /* we don't align basic blocks of loops on arm */
1738 if (cfg->verbose_level > 2)
1739 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
1741 cpos = bb->max_offset;
1743 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
1744 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
1745 //g_assert (!mono_compile_aot);
1748 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
1749 /* this is not thread save, but good enough */
1750 /* fixme: howto handle overflows? */
1751 //x86_inc_mem (code, &cov->data [bb->dfn].count);
1756 offset = code - cfg->native_code;
1758 max_len = ((guint8 *)arm_cpu_desc [ins->opcode])[MONO_INST_LEN];
1760 if (offset > (cfg->code_size - max_len - 16)) {
1761 cfg->code_size *= 2;
1762 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
1763 code = cfg->native_code + offset;
1765 // if (ins->cil_code)
1766 // g_print ("cil code\n");
1767 mono_debug_record_line_number (cfg, ins, offset);
1769 switch (ins->opcode) {
1770 case OP_MEMORY_BARRIER:
1773 g_assert_not_reached ();
1776 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
1777 ppc_mulhw (code, ppc_r3, ins->sreg1, ins->sreg2);
1780 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
1781 ppc_mulhwu (code, ppc_r3, ins->sreg1, ins->sreg2);
1783 case OP_STOREI1_MEMBASE_IMM:
1784 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFF);
1785 g_assert (arm_is_imm12 (ins->inst_offset));
1786 ARM_STRB_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
1788 case OP_STOREI2_MEMBASE_IMM:
1789 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFFFF);
1790 g_assert (arm_is_imm8 (ins->inst_offset));
1791 ARM_STRH_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
1793 case OP_STORE_MEMBASE_IMM:
1794 case OP_STOREI4_MEMBASE_IMM:
1795 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm);
1796 g_assert (arm_is_imm12 (ins->inst_offset));
1797 ARM_STR_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
1799 case OP_STOREI1_MEMBASE_REG:
1800 g_assert (arm_is_imm12 (ins->inst_offset));
1801 ARM_STRB_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
1803 case OP_STOREI2_MEMBASE_REG:
1804 g_assert (arm_is_imm8 (ins->inst_offset));
1805 ARM_STRH_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
1807 case OP_STORE_MEMBASE_REG:
1808 case OP_STOREI4_MEMBASE_REG:
1809 /* this case is special, since it happens for spill code after lowering has been called */
1810 if (arm_is_imm12 (ins->inst_offset)) {
1811 ARM_STR_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
1813 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
1814 ARM_STR_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ARMREG_LR);
1817 case OP_STOREI1_MEMINDEX:
1818 ARM_STRB_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
1820 case OP_STOREI2_MEMINDEX:
1821 /* note: the args are reversed in the macro */
1822 ARM_STRH_REG_REG (code, ins->inst_destbasereg, ins->sreg1, ins->sreg2);
1824 case OP_STORE_MEMINDEX:
1825 case OP_STOREI4_MEMINDEX:
1826 ARM_STR_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
1831 g_assert_not_reached ();
1834 g_assert_not_reached ();
1836 case OP_LOAD_MEMINDEX:
1837 case OP_LOADI4_MEMINDEX:
1838 case OP_LOADU4_MEMINDEX:
1839 ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
1841 case OP_LOADI1_MEMINDEX:
1842 /* note: the args are reversed in the macro */
1843 ARM_LDRSB_REG_REG (code, ins->inst_basereg, ins->dreg, ins->sreg2);
1845 case OP_LOADU1_MEMINDEX:
1846 ARM_LDRB_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
1848 case OP_LOADI2_MEMINDEX:
1849 /* note: the args are reversed in the macro */
1850 ARM_LDRSH_REG_REG (code, ins->inst_basereg, ins->dreg, ins->sreg2);
1852 case OP_LOADU2_MEMINDEX:
1853 /* note: the args are reversed in the macro */
1854 ARM_LDRH_REG_REG (code, ins->inst_basereg, ins->dreg, ins->sreg2);
1856 case OP_LOAD_MEMBASE:
1857 case OP_LOADI4_MEMBASE:
1858 case OP_LOADU4_MEMBASE:
1859 /* this case is special, since it happens for spill code after lowering has been called */
1860 if (arm_is_imm12 (ins->inst_offset)) {
1861 ARM_LDR_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
1863 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
1864 ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ARMREG_LR);
1867 case OP_LOADI1_MEMBASE:
1868 g_assert (arm_is_imm8 (ins->inst_offset));
1869 ARM_LDRSB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
1871 case OP_LOADU1_MEMBASE:
1872 g_assert (arm_is_imm12 (ins->inst_offset));
1873 ARM_LDRB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
1875 case OP_LOADU2_MEMBASE:
1876 g_assert (arm_is_imm8 (ins->inst_offset));
1877 ARM_LDRH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
1879 case OP_LOADI2_MEMBASE:
1880 g_assert (arm_is_imm8 (ins->inst_offset));
1881 ARM_LDRSH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
1884 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 24);
1885 ARM_SAR_IMM (code, ins->dreg, ins->dreg, 24);
1888 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
1889 ARM_SAR_IMM (code, ins->dreg, ins->dreg, 16);
1892 ARM_AND_REG_IMM8 (code, ins->dreg, ins->sreg1, 0xff);
1895 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
1896 ARM_SHR_IMM (code, ins->dreg, ins->dreg, 16);
1899 ARM_CMP_REG_REG (code, ins->sreg1, ins->sreg2);
1901 case OP_COMPARE_IMM:
1902 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1903 g_assert (imm8 >= 0);
1904 ARM_CMP_REG_IMM (code, ins->sreg1, imm8, rot_amount);
1907 *(int*)code = 0xe7f001f0;
1908 *(int*)code = 0xef9f0001;
1913 ARM_ADDS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1916 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1919 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1922 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1923 g_assert (imm8 >= 0);
1924 ARM_ADDS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1927 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1928 g_assert (imm8 >= 0);
1929 ARM_ADD_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1932 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1933 g_assert (imm8 >= 0);
1934 ARM_ADCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1937 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1938 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
1940 case CEE_ADD_OVF_UN:
1941 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1942 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
1945 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1946 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
1948 case CEE_SUB_OVF_UN:
1949 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1950 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
1952 case OP_ADD_OVF_CARRY:
1953 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1954 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
1956 case OP_ADD_OVF_UN_CARRY:
1957 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1958 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
1960 case OP_SUB_OVF_CARRY:
1961 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1962 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
1964 case OP_SUB_OVF_UN_CARRY:
1965 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1966 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
1969 ARM_SUBS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1972 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1973 g_assert (imm8 >= 0);
1974 ARM_SUBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1977 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1980 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1983 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1984 g_assert (imm8 >= 0);
1985 ARM_SUB_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1988 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1989 g_assert (imm8 >= 0);
1990 ARM_SBCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1992 case OP_ARM_RSBS_IMM:
1993 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1994 g_assert (imm8 >= 0);
1995 ARM_RSBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1997 case OP_ARM_RSC_IMM:
1998 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1999 g_assert (imm8 >= 0);
2000 ARM_RSC_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
2003 ARM_AND_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
2006 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
2007 g_assert (imm8 >= 0);
2008 ARM_AND_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
2016 /* crappy ARM arch doesn't have a DIV instruction */
2017 g_assert_not_reached ();
2019 ARM_ORR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
2022 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
2023 g_assert (imm8 >= 0);
2024 ARM_ORR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
2027 ARM_EOR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
2030 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
2031 g_assert (imm8 >= 0);
2032 ARM_EOR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
2035 ARM_SHL_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
2039 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
2042 ARM_SAR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
2046 ARM_SAR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
2050 ARM_SHR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
2053 ARM_SHR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
2056 ARM_MVN_REG_REG (code, ins->dreg, ins->sreg1);
2059 ARM_RSB_REG_IMM8 (code, ins->dreg, ins->sreg1, 0);
2062 if (ins->dreg == ins->sreg2)
2063 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
2065 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg2, ins->sreg1);
2068 g_assert_not_reached ();
2071 /* FIXME: handle ovf/ sreg2 != dreg */
2072 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
2074 case CEE_MUL_OVF_UN:
2075 /* FIXME: handle ovf/ sreg2 != dreg */
2076 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
2080 code = mono_arm_emit_load_imm (code, ins->dreg, ins->inst_c0);
2083 g_assert_not_reached ();
2084 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2090 if (ins->dreg != ins->sreg1)
2091 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
2094 int saved = ins->sreg2;
2095 if (ins->sreg2 == ARM_LSW_REG) {
2096 ARM_MOV_REG_REG (code, ARMREG_LR, ins->sreg2);
2099 if (ins->sreg1 != ARM_LSW_REG)
2100 ARM_MOV_REG_REG (code, ARM_LSW_REG, ins->sreg1);
2101 if (saved != ARM_MSW_REG)
2102 ARM_MOV_REG_REG (code, ARM_MSW_REG, saved);
2108 ARM_MVFD (code, ins->dreg, ins->sreg1);
2109 #elif defined(ARM_FPU_VFP)
2110 ARM_CPYD (code, ins->dreg, ins->sreg1);
2113 case OP_FCONV_TO_R4:
2115 ARM_MVFS (code, ins->dreg, ins->sreg1);
2116 #elif defined(ARM_FPU_VFP)
2117 ARM_CVTD (code, ins->dreg, ins->sreg1);
2118 ARM_CVTS (code, ins->dreg, ins->dreg);
2123 * Keep in sync with mono_arch_emit_epilog
2125 g_assert (!cfg->method->save_lmf);
2126 code = emit_big_add (code, ARMREG_SP, cfg->frame_reg, cfg->stack_usage);
2127 ARM_POP_NWB (code, cfg->used_int_regs | ((1 << ARMREG_SP)) | ((1 << ARMREG_LR)));
2128 mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2132 /* ensure ins->sreg1 is not NULL */
2133 ARM_LDR_IMM (code, ARMREG_LR, ins->sreg1, 0);
2137 if (ppc_is_imm16 (cfg->sig_cookie + cfg->stack_usage)) {
2138 ppc_addi (code, ppc_r11, cfg->frame_reg, cfg->sig_cookie + cfg->stack_usage);
2140 ppc_load (code, ppc_r11, cfg->sig_cookie + cfg->stack_usage);
2141 ppc_add (code, ppc_r11, cfg->frame_reg, ppc_r11);
2143 ppc_stw (code, ppc_r11, 0, ins->sreg1);
2152 call = (MonoCallInst*)ins;
2153 if (ins->flags & MONO_INST_HAS_METHOD)
2154 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2156 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2157 if (cfg->method->dynamic) {
2158 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
2160 *(gpointer*)code = NULL;
2162 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
2163 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
2171 case OP_VOIDCALL_REG:
2173 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
2174 ARM_MOV_REG_REG (code, ARMREG_PC, ins->sreg1);
2176 case OP_FCALL_MEMBASE:
2177 case OP_LCALL_MEMBASE:
2178 case OP_VCALL_MEMBASE:
2179 case OP_VOIDCALL_MEMBASE:
2180 case OP_CALL_MEMBASE:
2181 g_assert (arm_is_imm12 (ins->inst_offset));
2182 g_assert (ins->sreg1 != ARMREG_LR);
2183 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
2184 ARM_LDR_IMM (code, ARMREG_PC, ins->sreg1, ins->inst_offset);
2187 g_assert_not_reached ();
2190 /* keep alignment */
2191 int alloca_waste = cfg->param_area;
2194 /* round the size to 8 bytes */
2195 ARM_ADD_REG_IMM8 (code, ins->dreg, ins->sreg1, 7);
2196 ARM_BIC_REG_IMM8 (code, ins->dreg, ins->sreg1, 7);
2197 ARM_ADD_REG_IMM8 (code, ins->dreg, ins->dreg, alloca_waste);
2198 ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ins->dreg);
2199 /* memzero the area: dreg holds the size, sp is the pointer */
2200 if (ins->flags & MONO_INST_INIT) {
2201 guint8 *start_loop, *branch_to_cond;
2202 ARM_MOV_REG_IMM8 (code, ARMREG_LR, 0);
2203 branch_to_cond = code;
2206 ARM_STR_REG_REG (code, ARMREG_LR, ARMREG_SP, ins->dreg);
2207 arm_patch (branch_to_cond, code);
2208 /* decrement by 4 and set flags */
2209 ARM_SUBS_REG_IMM8 (code, ins->dreg, ins->dreg, 4);
2210 ARM_B_COND (code, ARMCOND_LT, 0);
2211 arm_patch (code - 4, start_loop);
2213 ARM_ADD_REG_IMM8 (code, ins->dreg, ARMREG_SP, alloca_waste);
2217 g_assert_not_reached ();
2218 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_LR);
2221 if (ins->sreg1 != ARMREG_R0)
2222 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
2223 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2224 (gpointer)"mono_arch_throw_exception");
2225 if (cfg->method->dynamic) {
2226 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
2228 *(gpointer*)code = NULL;
2230 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
2231 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
2238 if (ins->sreg1 != ARMREG_R0)
2239 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
2240 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2241 (gpointer)"mono_arch_rethrow_exception");
2242 if (cfg->method->dynamic) {
2243 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
2245 *(gpointer*)code = NULL;
2247 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
2248 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
2254 case OP_START_HANDLER:
2255 if (arm_is_imm12 (ins->inst_left->inst_offset)) {
2256 ARM_STR_IMM (code, ARMREG_LR, ins->inst_left->inst_basereg, ins->inst_left->inst_offset);
2258 code = mono_arm_emit_load_imm (code, ARMREG_IP, ins->inst_left->inst_offset);
2259 ARM_STR_REG_REG (code, ARMREG_LR, ins->inst_left->inst_basereg, ARMREG_IP);
2263 if (ins->sreg1 != ARMREG_R0)
2264 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
2265 if (arm_is_imm12 (ins->inst_left->inst_offset)) {
2266 ARM_LDR_IMM (code, ARMREG_IP, ins->inst_left->inst_basereg, ins->inst_left->inst_offset);
2268 g_assert (ARMREG_IP != ins->inst_left->inst_basereg);
2269 code = mono_arm_emit_load_imm (code, ARMREG_IP, ins->inst_left->inst_offset);
2270 ARM_LDR_REG_REG (code, ARMREG_IP, ins->inst_left->inst_basereg, ARMREG_IP);
2272 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
2274 case CEE_ENDFINALLY:
2275 if (arm_is_imm12 (ins->inst_left->inst_offset)) {
2276 ARM_LDR_IMM (code, ARMREG_IP, ins->inst_left->inst_basereg, ins->inst_left->inst_offset);
2278 g_assert (ARMREG_IP != ins->inst_left->inst_basereg);
2279 code = mono_arm_emit_load_imm (code, ARMREG_IP, ins->inst_left->inst_offset);
2280 ARM_LDR_REG_REG (code, ARMREG_IP, ins->inst_left->inst_basereg, ARMREG_IP);
2282 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
2284 case OP_CALL_HANDLER:
2285 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2289 ins->inst_c0 = code - cfg->native_code;
2292 if (ins->flags & MONO_INST_BRLABEL) {
2293 /*if (ins->inst_i0->inst_c0) {
2295 //x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2297 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2301 /*if (ins->inst_target_bb->native_offset) {
2303 //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
2305 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2311 ARM_MOV_REG_REG (code, ARMREG_PC, ins->sreg1);
2315 * In the normal case we have:
2316 * ldr pc, [pc, ins->sreg1 << 2]
2319 * ldr lr, [pc, ins->sreg1 << 2]
2321 * After follows the data.
2322 * FIXME: add aot support.
2324 max_len += 4 * GPOINTER_TO_INT (ins->klass);
2325 if (offset > (cfg->code_size - max_len - 16)) {
2326 cfg->code_size += max_len;
2327 cfg->code_size *= 2;
2328 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2329 code = cfg->native_code + offset;
2331 ARM_LDR_REG_REG_SHIFT (code, ARMREG_PC, ARMREG_PC, ins->sreg1, ARMSHIFT_LSL, 2);
2333 code += 4 * GPOINTER_TO_INT (ins->klass);
2336 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
2337 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
2340 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2341 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LT);
2344 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2345 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LO);
2348 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2349 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_GT);
2352 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2353 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_HI);
2355 case OP_COND_EXC_EQ:
2356 case OP_COND_EXC_NE_UN:
2357 case OP_COND_EXC_LT:
2358 case OP_COND_EXC_LT_UN:
2359 case OP_COND_EXC_GT:
2360 case OP_COND_EXC_GT_UN:
2361 case OP_COND_EXC_GE:
2362 case OP_COND_EXC_GE_UN:
2363 case OP_COND_EXC_LE:
2364 case OP_COND_EXC_LE_UN:
2365 EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_EQ, ins->inst_p1);
2368 case OP_COND_EXC_OV:
2369 case OP_COND_EXC_NC:
2370 case OP_COND_EXC_NO:
2371 g_assert_not_reached ();
2383 EMIT_COND_BRANCH (ins, ins->opcode - CEE_BEQ);
2386 /* floating point opcodes */
2389 /* FIXME: we can optimize the imm load by dealing with part of
2390 * the displacement in LDFD (aligning to 512).
2392 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
2393 ARM_LDFD (code, ins->dreg, ARMREG_LR, 0);
2396 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
2397 ARM_LDFS (code, ins->dreg, ARMREG_LR, 0);
2399 case OP_STORER8_MEMBASE_REG:
2400 g_assert (arm_is_fpimm8 (ins->inst_offset));
2401 ARM_STFD (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
2403 case OP_LOADR8_MEMBASE:
2404 g_assert (arm_is_fpimm8 (ins->inst_offset));
2405 ARM_LDFD (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
2407 case OP_STORER4_MEMBASE_REG:
2408 g_assert (arm_is_fpimm8 (ins->inst_offset));
2409 ARM_STFS (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
2411 case OP_LOADR4_MEMBASE:
2412 g_assert (arm_is_fpimm8 (ins->inst_offset));
2413 ARM_LDFS (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
2415 case CEE_CONV_R_UN: {
2417 tmpreg = ins->dreg == 0? 1: 0;
2418 ARM_CMP_REG_IMM8 (code, ins->sreg1, 0);
2419 ARM_FLTD (code, ins->dreg, ins->sreg1);
2420 ARM_B_COND (code, ARMCOND_GE, 8);
2421 /* save the temp register */
2422 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
2423 ARM_STFD (code, tmpreg, ARMREG_SP, 0);
2424 ARM_LDFD (code, tmpreg, ARMREG_PC, 12);
2425 ARM_FPA_ADFD (code, ins->dreg, ins->dreg, tmpreg);
2426 ARM_LDFD (code, tmpreg, ARMREG_SP, 0);
2427 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
2428 /* skip the constant pool */
2431 *(int*)code = 0x41f00000;
2436 * ldfltd ftemp, [pc, #8] 0x41f00000 0x00000000
2437 * adfltd fdest, fdest, ftemp
2442 ARM_FLTS (code, ins->dreg, ins->sreg1);
2445 ARM_FLTD (code, ins->dreg, ins->sreg1);
2447 #elif defined(ARM_FPU_VFP)
2449 /* FIXME: we can optimize the imm load by dealing with part of
2450 * the displacement in LDFD (aligning to 512).
2452 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
2453 ARM_FLDD (code, ins->dreg, ARMREG_LR, 0);
2456 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
2457 ARM_FLDS (code, ins->dreg, ARMREG_LR, 0);
2458 ARM_CVTS (code, ins->dreg, ins->dreg);
2460 case OP_STORER8_MEMBASE_REG:
2461 g_assert (arm_is_fpimm8 (ins->inst_offset));
2462 ARM_FSTD (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
2464 case OP_LOADR8_MEMBASE:
2465 g_assert (arm_is_fpimm8 (ins->inst_offset));
2466 ARM_FLDD (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
2468 case OP_STORER4_MEMBASE_REG:
2469 g_assert (arm_is_fpimm8 (ins->inst_offset));
2470 ARM_FSTS (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
2472 case OP_LOADR4_MEMBASE:
2473 g_assert (arm_is_fpimm8 (ins->inst_offset));
2474 ARM_FLDS (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
2476 case CEE_CONV_R_UN: {
2477 g_assert_not_reached ();
2481 g_assert_not_reached ();
2482 //ARM_FLTS (code, ins->dreg, ins->sreg1);
2485 g_assert_not_reached ();
2486 //ARM_FLTD (code, ins->dreg, ins->sreg1);
2489 case OP_FCONV_TO_I1:
2490 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
2492 case OP_FCONV_TO_U1:
2493 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
2495 case OP_FCONV_TO_I2:
2496 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
2498 case OP_FCONV_TO_U2:
2499 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
2501 case OP_FCONV_TO_I4:
2503 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
2505 case OP_FCONV_TO_U4:
2507 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
2509 case OP_FCONV_TO_I8:
2510 case OP_FCONV_TO_U8:
2511 g_assert_not_reached ();
2512 /* Implemented as helper calls */
2514 case OP_LCONV_TO_R_UN:
2515 g_assert_not_reached ();
2516 /* Implemented as helper calls */
2518 case OP_LCONV_TO_OVF_I: {
2520 guint32 *negative_branch, *msword_positive_branch, *msword_negative_branch, *ovf_ex_target;
2521 // Check if its negative
2522 ppc_cmpi (code, 0, 0, ins->sreg1, 0);
2523 negative_branch = code;
2524 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 0);
2525 // Its positive msword == 0
2526 ppc_cmpi (code, 0, 0, ins->sreg2, 0);
2527 msword_positive_branch = code;
2528 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
2530 ovf_ex_target = code;
2531 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_ALWAYS, 0, "OverflowException");
2533 ppc_patch (negative_branch, code);
2534 ppc_cmpi (code, 0, 0, ins->sreg2, -1);
2535 msword_negative_branch = code;
2536 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 0);
2537 ppc_patch (msword_negative_branch, ovf_ex_target);
2539 ppc_patch (msword_positive_branch, code);
2540 if (ins->dreg != ins->sreg1)
2541 ppc_mr (code, ins->dreg, ins->sreg1);
2543 if (ins->dreg != ins->sreg1)
2544 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
2549 ARM_FPA_ADFD (code, ins->dreg, ins->sreg1, ins->sreg2);
2552 ARM_FPA_SUFD (code, ins->dreg, ins->sreg1, ins->sreg2);
2555 ARM_FPA_MUFD (code, ins->dreg, ins->sreg1, ins->sreg2);
2558 ARM_FPA_DVFD (code, ins->dreg, ins->sreg1, ins->sreg2);
2561 ARM_MNFD (code, ins->dreg, ins->sreg1);
2563 #elif defined(ARM_FPU_VFP)
2565 ARM_VFP_ADDD (code, ins->dreg, ins->sreg1, ins->sreg2);
2568 ARM_VFP_SUBD (code, ins->dreg, ins->sreg1, ins->sreg2);
2571 ARM_VFP_MULD (code, ins->dreg, ins->sreg1, ins->sreg2);
2574 ARM_VFP_DIVD (code, ins->dreg, ins->sreg1, ins->sreg2);
2577 ARM_NEGD (code, ins->dreg, ins->sreg1);
2582 g_assert_not_reached ();
2585 /* each fp compare op needs to do its own */
2586 g_assert_not_reached ();
2587 //ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2591 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2592 #elif defined(ARM_FPU_VFP)
2593 ARM_CMPD (code, ins->sreg1, ins->sreg2);
2595 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
2596 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
2600 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2601 #elif defined(ARM_FPU_VFP)
2602 ARM_CMPD (code, ins->sreg1, ins->sreg2);
2604 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2605 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
2609 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2610 #elif defined(ARM_FPU_VFP)
2611 ARM_CMPD (code, ins->sreg1, ins->sreg2);
2613 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2614 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
2615 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
2620 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
2621 #elif defined(ARM_FPU_VFP)
2622 ARM_CMPD (code, ins->sreg2, ins->sreg1);
2624 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2625 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
2630 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
2631 #elif defined(ARM_FPU_VFP)
2632 ARM_CMPD (code, ins->sreg2, ins->sreg1);
2634 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2635 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
2636 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
2638 /* ARM FPA flags table:
2639 * N Less than ARMCOND_MI
2640 * Z Equal ARMCOND_EQ
2641 * C Greater Than or Equal ARMCOND_CS
2642 * V Unordered ARMCOND_VS
2646 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2647 #elif defined(ARM_FPU_VFP)
2648 ARM_CMPD (code, ins->sreg1, ins->sreg2);
2650 EMIT_COND_BRANCH (ins, CEE_BEQ - CEE_BEQ);
2654 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2655 #elif defined(ARM_FPU_VFP)
2656 ARM_CMPD (code, ins->sreg1, ins->sreg2);
2658 EMIT_COND_BRANCH (ins, CEE_BNE_UN - CEE_BEQ);
2662 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2663 #elif defined(ARM_FPU_VFP)
2664 ARM_CMPD (code, ins->sreg1, ins->sreg2);
2666 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set */
2670 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2671 #elif defined(ARM_FPU_VFP)
2672 ARM_CMPD (code, ins->sreg1, ins->sreg2);
2674 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
2675 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set */
2679 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
2680 #elif defined(ARM_FPU_VFP)
2681 ARM_CMPD (code, ins->sreg2, ins->sreg1);
2683 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set, swapped args */
2687 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
2688 #elif defined(ARM_FPU_VFP)
2689 ARM_CMPD (code, ins->sreg2, ins->sreg1);
2691 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
2692 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set, swapped args */
2696 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2697 #elif defined(ARM_FPU_VFP)
2698 ARM_CMPD (code, ins->sreg1, ins->sreg2);
2700 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_CS);
2704 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2705 #elif defined(ARM_FPU_VFP)
2706 ARM_CMPD (code, ins->sreg1, ins->sreg2);
2708 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
2709 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_GE);
2713 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
2714 #elif defined(ARM_FPU_VFP)
2715 ARM_CMPD (code, ins->sreg2, ins->sreg1);
2717 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_CS); /* swapped */
2721 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
2722 #elif defined(ARM_FPU_VFP)
2723 ARM_CMPD (code, ins->sreg2, ins->sreg1);
2725 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
2726 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_GE); /* swapped */
2728 case CEE_CKFINITE: {
2729 /*ppc_stfd (code, ins->sreg1, -8, ppc_sp);
2730 ppc_lwz (code, ppc_r11, -8, ppc_sp);
2731 ppc_rlwinm (code, ppc_r11, ppc_r11, 0, 1, 31);
2732 ppc_addis (code, ppc_r11, ppc_r11, -32752);
2733 ppc_rlwinmd (code, ppc_r11, ppc_r11, 1, 31, 31);
2734 EMIT_COND_SYSTEM_EXCEPTION (CEE_BEQ - CEE_BEQ, "ArithmeticException");*/
2735 g_assert_not_reached ();
2739 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
2740 g_assert_not_reached ();
2743 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
2744 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
2745 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
2746 g_assert_not_reached ();
2752 last_offset = offset;
2757 cfg->code_len = code - cfg->native_code;
2761 mono_arch_register_lowlevel_calls (void)
2765 #define patch_lis_ori(ip,val) do {\
2766 guint16 *__lis_ori = (guint16*)(ip); \
2767 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff; \
2768 __lis_ori [3] = ((guint32)(val)) & 0xffff; \
2772 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
2774 MonoJumpInfo *patch_info;
2776 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
2777 unsigned char *ip = patch_info->ip.i + code;
2778 const unsigned char *target;
2780 if (patch_info->type == MONO_PATCH_INFO_SWITCH) {
2781 gpointer *jt = (gpointer*)(ip + 8);
2783 /* jt is the inlined jump table, 2 instructions after ip
2784 * In the normal case we store the absolute addresses,
2785 * otherwise the displacements.
2787 for (i = 0; i < patch_info->data.table->table_size; i++) {
2788 jt [i] = code + (int)patch_info->data.table->table [i];
2792 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
2794 switch (patch_info->type) {
2795 case MONO_PATCH_INFO_IP:
2796 g_assert_not_reached ();
2797 patch_lis_ori (ip, ip);
2799 case MONO_PATCH_INFO_METHOD_REL:
2800 g_assert_not_reached ();
2801 *((gpointer *)(ip)) = code + patch_info->data.offset;
2803 case MONO_PATCH_INFO_METHODCONST:
2804 case MONO_PATCH_INFO_CLASS:
2805 case MONO_PATCH_INFO_IMAGE:
2806 case MONO_PATCH_INFO_FIELD:
2807 case MONO_PATCH_INFO_VTABLE:
2808 case MONO_PATCH_INFO_IID:
2809 case MONO_PATCH_INFO_SFLDA:
2810 case MONO_PATCH_INFO_LDSTR:
2811 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
2812 case MONO_PATCH_INFO_LDTOKEN:
2813 g_assert_not_reached ();
2814 /* from OP_AOTCONST : lis + ori */
2815 patch_lis_ori (ip, target);
2817 case MONO_PATCH_INFO_R4:
2818 case MONO_PATCH_INFO_R8:
2819 g_assert_not_reached ();
2820 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
2822 case MONO_PATCH_INFO_EXC_NAME:
2823 g_assert_not_reached ();
2824 *((gconstpointer *)(ip + 1)) = patch_info->data.name;
2826 case MONO_PATCH_INFO_NONE:
2827 case MONO_PATCH_INFO_BB_OVF:
2828 case MONO_PATCH_INFO_EXC_OVF:
2829 /* everything is dealt with at epilog output time */
2834 arm_patch (ip, target);
2839 * Stack frame layout:
2841 * ------------------- fp
2842 * MonoLMF structure or saved registers
2843 * -------------------
2845 * -------------------
2847 * -------------------
2848 * optional 8 bytes for tracing
2849 * -------------------
2850 * param area size is cfg->param_area
2851 * ------------------- sp
2854 mono_arch_emit_prolog (MonoCompile *cfg)
2856 MonoMethod *method = cfg->method;
2858 MonoMethodSignature *sig;
2860 int alloc_size, pos, max_offset, i, rot_amount;
2867 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
2870 sig = mono_method_signature (method);
2871 cfg->code_size = 256 + sig->param_count * 20;
2872 code = cfg->native_code = g_malloc (cfg->code_size);
2874 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
2876 alloc_size = cfg->stack_offset;
2879 if (!method->save_lmf) {
2880 ARM_PUSH (code, (cfg->used_int_regs | (1 << ARMREG_IP) | (1 << ARMREG_LR)));
2881 prev_sp_offset = 8; /* ip and lr */
2882 for (i = 0; i < 16; ++i) {
2883 if (cfg->used_int_regs & (1 << i))
2884 prev_sp_offset += 4;
2887 ARM_PUSH (code, 0x5ff0);
2888 prev_sp_offset = 4 * 10; /* all but r0-r3, sp and pc */
2889 pos += sizeof (MonoLMF) - prev_sp_offset;
2893 // align to MONO_ARCH_FRAME_ALIGNMENT bytes
2894 if (alloc_size & (MONO_ARCH_FRAME_ALIGNMENT - 1)) {
2895 alloc_size += MONO_ARCH_FRAME_ALIGNMENT - 1;
2896 alloc_size &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
2899 /* the stack used in the pushed regs */
2900 if (prev_sp_offset & 4)
2902 cfg->stack_usage = alloc_size;
2904 if ((i = mono_arm_is_rotated_imm8 (alloc_size, &rot_amount)) >= 0) {
2905 ARM_SUB_REG_IMM (code, ARMREG_SP, ARMREG_SP, i, rot_amount);
2907 code = mono_arm_emit_load_imm (code, ARMREG_IP, alloc_size);
2908 ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
2911 if (cfg->frame_reg != ARMREG_SP)
2912 ARM_MOV_REG_REG (code, cfg->frame_reg, ARMREG_SP);
2913 //g_print ("prev_sp_offset: %d, alloc_size:%d\n", prev_sp_offset, alloc_size);
2914 prev_sp_offset += alloc_size;
2916 /* compute max_offset in order to use short forward jumps
2917 * we could skip do it on arm because the immediate displacement
2918 * for jumps is large enough, it may be useful later for constant pools
2921 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2922 MonoInst *ins = bb->code;
2923 bb->max_offset = max_offset;
2925 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
2929 max_offset += ((guint8 *)arm_cpu_desc [ins->opcode])[MONO_INST_LEN];
2934 /* load arguments allocated to register from the stack */
2937 cinfo = calculate_sizes (sig, sig->pinvoke);
2939 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
2940 ArgInfo *ainfo = &cinfo->ret;
2942 g_assert (arm_is_imm12 (inst->inst_offset));
2943 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
2945 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2946 ArgInfo *ainfo = cinfo->args + i;
2947 inst = cfg->varinfo [pos];
2949 if (cfg->verbose_level > 2)
2950 g_print ("Saving argument %d (type: %d)\n", i, ainfo->regtype);
2951 if (inst->opcode == OP_REGVAR) {
2952 if (ainfo->regtype == RegTypeGeneral)
2953 ARM_MOV_REG_REG (code, inst->dreg, ainfo->reg);
2954 else if (ainfo->regtype == RegTypeFP) {
2955 g_assert_not_reached ();
2956 } else if (ainfo->regtype == RegTypeBase) {
2957 g_assert (arm_is_imm12 (prev_sp_offset + ainfo->offset));
2958 ARM_LDR_IMM (code, inst->dreg, ARMREG_SP, (prev_sp_offset + ainfo->offset));
2960 g_assert_not_reached ();
2962 if (cfg->verbose_level > 2)
2963 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
2965 /* the argument should be put on the stack: FIXME handle size != word */
2966 if (ainfo->regtype == RegTypeGeneral) {
2967 switch (ainfo->size) {
2969 if (arm_is_imm12 (inst->inst_offset))
2970 ARM_STRB_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
2972 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
2973 ARM_STRB_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
2977 if (arm_is_imm8 (inst->inst_offset)) {
2978 ARM_STRH_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
2980 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
2981 ARM_ADD_REG_REG (code, ARMREG_IP, ARMREG_IP, inst->inst_basereg);
2982 ARM_STRH_IMM (code, ainfo->reg, ARMREG_IP, 0);
2986 g_assert (arm_is_imm12 (inst->inst_offset));
2987 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
2988 g_assert (arm_is_imm12 (inst->inst_offset + 4));
2989 ARM_STR_IMM (code, ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
2992 if (arm_is_imm12 (inst->inst_offset)) {
2993 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
2995 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
2996 ARM_STR_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
3000 } else if (ainfo->regtype == RegTypeBaseGen) {
3001 g_assert (arm_is_imm12 (prev_sp_offset + ainfo->offset));
3002 g_assert (arm_is_imm12 (inst->inst_offset));
3003 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
3004 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset + 4);
3005 ARM_STR_IMM (code, ARMREG_R3, inst->inst_basereg, inst->inst_offset);
3006 } else if (ainfo->regtype == RegTypeBase) {
3007 g_assert (arm_is_imm12 (prev_sp_offset + ainfo->offset));
3008 switch (ainfo->size) {
3010 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
3011 g_assert (arm_is_imm12 (inst->inst_offset));
3012 ARM_STRB_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
3015 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
3016 if (arm_is_imm8 (inst->inst_offset)) {
3017 ARM_STRH_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
3019 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
3020 ARM_ADD_REG_REG (code, ARMREG_IP, ARMREG_IP, inst->inst_basereg);
3021 ARM_STRH_IMM (code, ARMREG_LR, ARMREG_IP, 0);
3025 g_assert (arm_is_imm12 (inst->inst_offset));
3026 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
3027 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
3028 g_assert (arm_is_imm12 (prev_sp_offset + ainfo->offset + 4));
3029 g_assert (arm_is_imm12 (inst->inst_offset + 4));
3030 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset + 4));
3031 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset + 4);
3034 g_assert (arm_is_imm12 (inst->inst_offset));
3035 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
3036 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
3039 } else if (ainfo->regtype == RegTypeFP) {
3040 g_assert_not_reached ();
3041 } else if (ainfo->regtype == RegTypeStructByVal) {
3042 int doffset = inst->inst_offset;
3046 if (mono_class_from_mono_type (inst->inst_vtype))
3047 size = mono_class_native_size (mono_class_from_mono_type (inst->inst_vtype), NULL);
3048 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
3049 g_assert (arm_is_imm12 (doffset));
3050 ARM_STR_IMM (code, ainfo->reg + cur_reg, inst->inst_basereg, doffset);
3051 soffset += sizeof (gpointer);
3052 doffset += sizeof (gpointer);
3054 if (ainfo->vtsize) {
3055 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3056 //g_print ("emit_memcpy (prev_sp_ofs: %d, ainfo->offset: %d, soffset: %d)\n", prev_sp_offset, ainfo->offset, soffset);
3057 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ARMREG_SP, prev_sp_offset + ainfo->offset);
3059 } else if (ainfo->regtype == RegTypeStructByAddr) {
3060 g_assert_not_reached ();
3061 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3062 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
3064 g_assert_not_reached ();
3069 if (method->save_lmf) {
3071 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3072 (gpointer)"mono_get_lmf_addr");
3073 if (cfg->method->dynamic) {
3074 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
3076 *(gpointer*)code = NULL;
3078 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
3079 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
3083 /* we build the MonoLMF structure on the stack - see mini-arm.h */
3084 /* lmf_offset is the offset from the previous stack pointer,
3085 * alloc_size is the total stack space allocated, so the offset
3086 * of MonoLMF from the current stack ptr is alloc_size - lmf_offset.
3087 * The pointer to the struct is put in r1 (new_lmf).
3088 * r2 is used as scratch
3089 * The callee-saved registers are already in the MonoLMF structure
3091 code = emit_big_add (code, ARMREG_R1, ARMREG_SP, alloc_size - lmf_offset);
3092 /* r0 is the result from mono_get_lmf_addr () */
3093 ARM_STR_IMM (code, ARMREG_R0, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
3094 /* new_lmf->previous_lmf = *lmf_addr */
3095 ARM_LDR_IMM (code, ARMREG_R2, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
3096 ARM_STR_IMM (code, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
3097 /* *(lmf_addr) = r1 */
3098 ARM_STR_IMM (code, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
3099 /* save method info */
3100 code = mono_arm_emit_load_imm (code, ARMREG_R2, GPOINTER_TO_INT (method));
3101 ARM_STR_IMM (code, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, method));
3102 ARM_STR_IMM (code, ARMREG_SP, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, ebp));
3103 /* save the current IP */
3104 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_PC);
3105 ARM_STR_IMM (code, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, eip));
3109 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
3111 cfg->code_len = code - cfg->native_code;
3112 g_assert (cfg->code_len < cfg->code_size);
3119 mono_arch_emit_epilog (MonoCompile *cfg)
3121 MonoMethod *method = cfg->method;
3122 int pos, i, rot_amount;
3123 int max_epilog_size = 16 + 20*4;
3126 if (cfg->method->save_lmf)
3127 max_epilog_size += 128;
3129 if (mono_jit_trace_calls != NULL)
3130 max_epilog_size += 50;
3132 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3133 max_epilog_size += 50;
3135 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
3136 cfg->code_size *= 2;
3137 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3138 mono_jit_stats.code_reallocs++;
3142 * Keep in sync with CEE_JMP
3144 code = cfg->native_code + cfg->code_len;
3146 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
3147 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
3151 if (method->save_lmf) {
3153 /* all but r0-r3, sp and pc */
3154 pos += sizeof (MonoLMF) - (4 * 10);
3156 /* r2 contains the pointer to the current LMF */
3157 code = emit_big_add (code, ARMREG_R2, cfg->frame_reg, cfg->stack_usage - lmf_offset);
3158 /* ip = previous_lmf */
3159 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R2, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
3161 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_R2, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
3162 /* *(lmf_addr) = previous_lmf */
3163 ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
3164 /* FIXME: speedup: there is no actual need to restore the registers if
3165 * we didn't actually change them (idea from Zoltan).
3168 /* point sp at the registers to restore: 10 is 14 -4, because we skip r0-r3 */
3169 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_R2, (sizeof (MonoLMF) - 10 * sizeof (gulong)));
3170 ARM_POP_NWB (code, 0xaff0); /* restore ip to sp and lr to pc */
3172 if ((i = mono_arm_is_rotated_imm8 (cfg->stack_usage, &rot_amount)) >= 0) {
3173 ARM_ADD_REG_IMM (code, ARMREG_SP, cfg->frame_reg, i, rot_amount);
3175 code = mono_arm_emit_load_imm (code, ARMREG_IP, cfg->stack_usage);
3176 ARM_ADD_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
3178 ARM_POP_NWB (code, cfg->used_int_regs | ((1 << ARMREG_SP) | (1 << ARMREG_PC)));
3181 cfg->code_len = code - cfg->native_code;
3183 g_assert (cfg->code_len < cfg->code_size);
3187 /* remove once throw_exception_by_name is eliminated */
3189 exception_id_by_name (const char *name)
3191 if (strcmp (name, "IndexOutOfRangeException") == 0)
3192 return MONO_EXC_INDEX_OUT_OF_RANGE;
3193 if (strcmp (name, "OverflowException") == 0)
3194 return MONO_EXC_OVERFLOW;
3195 if (strcmp (name, "ArithmeticException") == 0)
3196 return MONO_EXC_ARITHMETIC;
3197 if (strcmp (name, "DivideByZeroException") == 0)
3198 return MONO_EXC_DIVIDE_BY_ZERO;
3199 if (strcmp (name, "InvalidCastException") == 0)
3200 return MONO_EXC_INVALID_CAST;
3201 if (strcmp (name, "NullReferenceException") == 0)
3202 return MONO_EXC_NULL_REF;
3203 if (strcmp (name, "ArrayTypeMismatchException") == 0)
3204 return MONO_EXC_ARRAY_TYPE_MISMATCH;
3205 g_error ("Unknown intrinsic exception %s\n", name);
3210 mono_arch_emit_exceptions (MonoCompile *cfg)
3212 MonoJumpInfo *patch_info;
3215 const guint8* exc_throw_pos [MONO_EXC_INTRINS_NUM] = {NULL};
3216 guint8 exc_throw_found [MONO_EXC_INTRINS_NUM] = {0};
3217 int max_epilog_size = 50;
3219 /* count the number of exception infos */
3222 * make sure we have enough space for exceptions
3223 * 12 is the simulated call to throw_exception_by_name
3225 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3226 if (patch_info->type == MONO_PATCH_INFO_EXC) {
3227 i = exception_id_by_name (patch_info->data.target);
3228 if (!exc_throw_found [i]) {
3229 max_epilog_size += 12;
3230 exc_throw_found [i] = TRUE;
3235 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
3236 cfg->code_size *= 2;
3237 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3238 mono_jit_stats.code_reallocs++;
3241 code = cfg->native_code + cfg->code_len;
3243 /* add code to raise exceptions */
3244 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3245 switch (patch_info->type) {
3246 case MONO_PATCH_INFO_EXC: {
3247 unsigned char *ip = patch_info->ip.i + cfg->native_code;
3248 const char *ex_name = patch_info->data.target;
3249 i = exception_id_by_name (patch_info->data.target);
3250 if (exc_throw_pos [i]) {
3251 arm_patch (ip, exc_throw_pos [i]);
3252 patch_info->type = MONO_PATCH_INFO_NONE;
3255 exc_throw_pos [i] = code;
3257 arm_patch (ip, code);
3258 //*(int*)code = 0xef9f0001;
3260 /*mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_NAME, patch_info->data.target);*/
3261 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
3262 /* we got here from a conditional call, so the calling ip is set in lr already */
3263 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3264 patch_info->data.name = "mono_arch_throw_exception_by_name";
3265 patch_info->ip.i = code - cfg->native_code;
3267 *(gconstpointer*)code = ex_name;
3277 cfg->code_len = code - cfg->native_code;
3279 g_assert (cfg->code_len < cfg->code_size);
3284 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
3289 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
3294 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
3297 int this_dreg = ARMREG_R0;
3300 this_dreg = ARMREG_R1;
3302 /* add the this argument */
3303 if (this_reg != -1) {
3305 MONO_INST_NEW (cfg, this, OP_SETREG);
3306 this->type = this_type;
3307 this->sreg1 = this_reg;
3308 this->dreg = mono_regstate_next_int (cfg->rs);
3309 mono_bblock_add_inst (cfg->cbb, this);
3310 mono_call_inst_add_outarg_reg (cfg, inst, this->dreg, this_dreg, FALSE);
3315 MONO_INST_NEW (cfg, vtarg, OP_SETREG);
3316 vtarg->type = STACK_MP;
3317 vtarg->sreg1 = vt_reg;
3318 vtarg->dreg = mono_regstate_next_int (cfg->rs);
3319 mono_bblock_add_inst (cfg->cbb, vtarg);
3320 mono_call_inst_add_outarg_reg (cfg, inst, vtarg->dreg, ARMREG_R0, FALSE);
3325 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3327 MonoInst *ins = NULL;
3328 if (cmethod->klass == mono_defaults.thread_class &&
3329 strcmp (cmethod->name, "MemoryBarrier") == 0) {
3330 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
3336 mono_arch_print_tree (MonoInst *tree, int arity)
3341 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
3347 mono_arch_get_thread_intrinsic (MonoCompile* cfg)
3353 mono_arch_flush_register_windows (void)
3358 mono_arch_fixup_jinfo (MonoCompile *cfg)
3360 /* max encoded stack usage is 64KB * 4 */
3361 g_assert ((cfg->stack_usage & ~(0xffff << 2)) == 0);
3362 cfg->jit_info->used_regs |= cfg->stack_usage << 14;