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_memcpy (guint8 *code, int size, int dreg, int doffset, int sreg, int soffset)
78 * mono_arch_get_argument_info:
79 * @csig: a method signature
80 * @param_count: the number of parameters to consider
81 * @arg_info: an array to store the result infos
83 * Gathers information on parameters such as size, alignment and
84 * padding. arg_info should be large enought to hold param_count + 1 entries.
86 * Returns the size of the activation frame.
89 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
91 int k, frame_size = 0;
95 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
96 frame_size += sizeof (gpointer);
100 arg_info [0].offset = offset;
103 frame_size += sizeof (gpointer);
107 arg_info [0].size = frame_size;
109 for (k = 0; k < param_count; k++) {
112 size = mono_type_native_stack_size (csig->params [k], &align);
114 size = mono_type_stack_size (csig->params [k], &align);
116 /* ignore alignment for now */
119 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
120 arg_info [k].pad = pad;
122 arg_info [k + 1].pad = 0;
123 arg_info [k + 1].size = size;
125 arg_info [k + 1].offset = offset;
129 align = MONO_ARCH_FRAME_ALIGNMENT;
130 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
131 arg_info [k].pad = pad;
137 * Initialize the cpu to execute managed code.
140 mono_arch_cpu_init (void)
145 * This function returns the optimizations supported on this cpu.
148 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
152 /* no arm-specific optimizations yet */
158 is_regsize_var (MonoType *t) {
161 t = mono_type_get_underlying_type (t);
168 case MONO_TYPE_FNPTR:
170 case MONO_TYPE_OBJECT:
171 case MONO_TYPE_STRING:
172 case MONO_TYPE_CLASS:
173 case MONO_TYPE_SZARRAY:
174 case MONO_TYPE_ARRAY:
176 case MONO_TYPE_VALUETYPE:
183 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
188 for (i = 0; i < cfg->num_varinfo; i++) {
189 MonoInst *ins = cfg->varinfo [i];
190 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
193 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
196 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
199 /* we can only allocate 32 bit values */
200 if (is_regsize_var (ins->inst_vtype)) {
201 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
202 g_assert (i == vmv->idx);
203 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
210 #define USE_EXTRA_TEMPS 0
213 mono_arch_get_global_int_regs (MonoCompile *cfg)
216 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V1));
217 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V2));
218 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V3));
219 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V4));
220 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V5));
221 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V6));
222 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V7));
228 * mono_arch_regalloc_cost:
230 * Return the cost, in number of memory references, of the action of
231 * allocating the variable VMV into a register during global register
235 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
242 mono_arch_flush_icache (guint8 *code, gint size)
244 __asm __volatile ("mov r0, %0\n"
247 "swi 0x9f0002 @ sys_cacheflush"
249 : "r" (code), "r" (code + size), "r" (0)
250 : "r0", "r1", "r3" );
254 #define NOT_IMPLEMENTED(x) \
255 g_error ("FIXME: %s is not yet implemented. (trampoline)", x);
267 guint16 vtsize; /* in param area */
269 guint8 regtype : 4; /* 0 general, 1 basereg, 2 floating point register, see RegType* */
270 guint8 size : 4; /* 1, 2, 4, 8, or regs used by RegTypeStructByVal */
285 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
288 if (*gr > ARMREG_R3) {
289 ainfo->offset = *stack_size;
290 ainfo->reg = ARMREG_SP; /* in the caller */
291 ainfo->regtype = RegTypeBase;
297 if (*gr > ARMREG_R2) {
299 //*stack_size += (*stack_size % 8);
301 ainfo->offset = *stack_size;
302 ainfo->reg = ARMREG_SP; /* in the caller */
303 ainfo->regtype = RegTypeBase;
316 calculate_sizes (MonoMethodSignature *sig, gboolean is_pinvoke)
319 int n = sig->hasthis + sig->param_count;
321 guint32 stack_size = 0;
322 CallInfo *cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * n);
326 /* FIXME: handle returning a struct */
327 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
328 add_general (&gr, &stack_size, &cinfo->ret, TRUE);
329 cinfo->struct_ret = ARMREG_R0;
334 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
337 DEBUG(printf("params: %d\n", sig->param_count));
338 for (i = 0; i < sig->param_count; ++i) {
339 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
340 /* Prevent implicit arguments and sig_cookie from
341 being passed in registers */
343 /* Emit the signature cookie just before the implicit arguments */
344 add_general (&gr, &stack_size, &cinfo->sig_cookie, TRUE);
346 DEBUG(printf("param %d: ", i));
347 if (sig->params [i]->byref) {
348 DEBUG(printf("byref\n"));
349 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
353 simpletype = mono_type_get_underlying_type (sig->params [i])->type;
355 switch (simpletype) {
356 case MONO_TYPE_BOOLEAN:
359 cinfo->args [n].size = 1;
360 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
366 cinfo->args [n].size = 2;
367 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
372 cinfo->args [n].size = 4;
373 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
379 case MONO_TYPE_FNPTR:
380 case MONO_TYPE_CLASS:
381 case MONO_TYPE_OBJECT:
382 case MONO_TYPE_STRING:
383 case MONO_TYPE_SZARRAY:
384 case MONO_TYPE_ARRAY:
386 cinfo->args [n].size = sizeof (gpointer);
387 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
390 case MONO_TYPE_VALUETYPE: {
393 klass = mono_class_from_mono_type (sig->params [i]);
395 size = mono_class_native_size (klass, NULL);
397 size = mono_class_value_size (klass, NULL);
398 DEBUG(printf ("load %d bytes struct\n",
399 mono_class_native_size (sig->params [i]->data.klass, NULL)));
401 #if PPC_PASS_STRUCTS_BY_VALUE
403 int align_size = size;
405 align_size += (sizeof (gpointer) - 1);
406 align_size &= ~(sizeof (gpointer) - 1);
407 nwords = (align_size + sizeof (gpointer) -1 ) / sizeof (gpointer);
408 cinfo->args [n].regtype = RegTypeStructByVal;
409 if (gr > PPC_LAST_ARG_REG || (size >= 3 && size % 4 != 0)) {
410 cinfo->args [n].size = 0;
411 cinfo->args [n].vtsize = nwords;
413 int rest = PPC_LAST_ARG_REG - gr + 1;
414 int n_in_regs = rest >= nwords? nwords: rest;
415 cinfo->args [n].size = n_in_regs;
416 cinfo->args [n].vtsize = nwords - n_in_regs;
417 cinfo->args [n].reg = gr;
420 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
421 /*g_print ("offset for arg %d at %d\n", n, PPC_STACK_PARAM_OFFSET + stack_size);*/
422 stack_size += nwords * sizeof (gpointer);
425 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
426 cinfo->args [n].regtype = RegTypeStructByAddr;
432 case MONO_TYPE_TYPEDBYREF: {
433 int size = sizeof (MonoTypedRef);
434 /* keep in sync or merge with the valuetype case */
436 #if PPC_PASS_STRUCTS_BY_VALUE
438 int nwords = (size + sizeof (gpointer) -1 ) / sizeof (gpointer);
439 cinfo->args [n].regtype = RegTypeStructByVal;
440 if (gr <= PPC_LAST_ARG_REG) {
441 int rest = PPC_LAST_ARG_REG - gr + 1;
442 int n_in_regs = rest >= nwords? nwords: rest;
443 cinfo->args [n].size = n_in_regs;
444 cinfo->args [n].vtsize = nwords - n_in_regs;
445 cinfo->args [n].reg = gr;
448 cinfo->args [n].size = 0;
449 cinfo->args [n].vtsize = nwords;
451 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
452 /*g_print ("offset for arg %d at %d\n", n, PPC_STACK_PARAM_OFFSET + stack_size);*/
453 stack_size += nwords * sizeof (gpointer);
456 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
457 cinfo->args [n].regtype = RegTypeStructByAddr;
466 cinfo->args [n].size = 8;
467 add_general (&gr, &stack_size, cinfo->args + n, FALSE);
471 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
476 simpletype = mono_type_get_underlying_type (sig->ret)->type;
478 switch (simpletype) {
479 case MONO_TYPE_BOOLEAN:
490 case MONO_TYPE_FNPTR:
491 case MONO_TYPE_CLASS:
492 case MONO_TYPE_OBJECT:
493 case MONO_TYPE_SZARRAY:
494 case MONO_TYPE_ARRAY:
495 case MONO_TYPE_STRING:
496 cinfo->ret.reg = ARMREG_R0;
500 cinfo->ret.reg = ARMREG_R0;
504 cinfo->ret.reg = ARMREG_R0;
505 /* FIXME: cinfo->ret.reg = ???;
506 cinfo->ret.regtype = RegTypeFP;*/
508 case MONO_TYPE_VALUETYPE:
510 case MONO_TYPE_TYPEDBYREF:
514 g_error ("Can't handle as return value 0x%x", sig->ret->type);
518 /* align stack size to 8 */
519 DEBUG (printf (" stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
520 stack_size = (stack_size + 7) & ~7;
522 cinfo->stack_usage = stack_size;
528 * Set var information according to the calling convention. arm version.
529 * The locals var stuff should most likely be split in another method.
532 mono_arch_allocate_vars (MonoCompile *m)
534 MonoMethodSignature *sig;
535 MonoMethodHeader *header;
537 int i, offset, size, align, curinst;
538 int frame_reg = ARMREG_SP;
541 /* allow room for the vararg method args: void* and long/double */
542 if (mono_jit_trace_calls != NULL && mono_trace_eval (m->method))
543 m->param_area = MAX (m->param_area, sizeof (gpointer)*8);
544 /* this is bug #60332: remove when #59509 is fixed, so no weird vararg
545 * call convs needs to be handled this way.
547 if (m->flags & MONO_CFG_HAS_VARARGS)
548 m->param_area = MAX (m->param_area, sizeof (gpointer)*8);
549 /* gtk-sharp and other broken code will dllimport vararg functions even with
550 * non-varargs signatures. Since there is little hope people will get this right
551 * we assume they won't.
553 if (m->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
554 m->param_area = MAX (m->param_area, sizeof (gpointer)*8);
556 header = mono_method_get_header (m->method);
559 * We use the frame register also for any method that has
560 * exception clauses. This way, when the handlers are called,
561 * the code will reference local variables using the frame reg instead of
562 * the stack pointer: if we had to restore the stack pointer, we'd
563 * corrupt the method frames that are already on the stack (since
564 * filters get called before stack unwinding happens) when the filter
565 * code would call any method (this also applies to finally etc.).
567 if ((m->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
568 frame_reg = ARMREG_FP;
569 m->frame_reg = frame_reg;
570 if (frame_reg != ARMREG_SP) {
571 m->used_int_regs |= 1 << frame_reg;
574 sig = mono_method_signature (m->method);
578 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
579 m->ret->opcode = OP_REGVAR;
580 m->ret->inst_c0 = ARMREG_R0;
582 /* FIXME: handle long and FP values */
583 switch (mono_type_get_underlying_type (sig->ret)->type) {
587 m->ret->opcode = OP_REGVAR;
588 m->ret->inst_c0 = ARMREG_R0;
592 /* local vars are at a positive offset from the stack pointer */
594 * also note that if the function uses alloca, we use FP
595 * to point at the local variables.
597 offset = 0; /* linkage area */
598 /* align the offset to 16 bytes: not sure this is needed here */
600 //offset &= ~(8 - 1);
602 /* add parameter area size for called functions */
603 offset += m->param_area;
607 /* allow room to save the return value */
608 if (mono_jit_trace_calls != NULL && mono_trace_eval (m->method))
611 /* the MonoLMF structure is stored just below the stack pointer */
613 if (sig->call_convention == MONO_CALL_VARARG) {
617 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
619 offset += sizeof(gpointer) - 1;
620 offset &= ~(sizeof(gpointer) - 1);
621 inst->inst_offset = offset;
622 inst->opcode = OP_REGOFFSET;
623 inst->inst_basereg = frame_reg;
624 offset += sizeof(gpointer);
625 if (sig->call_convention == MONO_CALL_VARARG)
626 m->sig_cookie += sizeof (gpointer);
629 curinst = m->locals_start;
630 for (i = curinst; i < m->num_varinfo; ++i) {
631 inst = m->varinfo [i];
632 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR)
635 /* inst->unused indicates native sized value types, this is used by the
636 * pinvoke wrappers when they call functions returning structure */
637 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
638 size = mono_class_native_size (mono_class_from_mono_type (inst->inst_vtype), &align);
640 size = mono_type_size (inst->inst_vtype, &align);
643 offset &= ~(align - 1);
644 inst->inst_offset = offset;
645 inst->opcode = OP_REGOFFSET;
646 inst->inst_basereg = frame_reg;
648 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
653 inst = m->varinfo [curinst];
654 if (inst->opcode != OP_REGVAR) {
655 inst->opcode = OP_REGOFFSET;
656 inst->inst_basereg = frame_reg;
657 offset += sizeof (gpointer) - 1;
658 offset &= ~(sizeof (gpointer) - 1);
659 inst->inst_offset = offset;
660 offset += sizeof (gpointer);
661 if (sig->call_convention == MONO_CALL_VARARG)
662 m->sig_cookie += sizeof (gpointer);
667 for (i = 0; i < sig->param_count; ++i) {
668 inst = m->varinfo [curinst];
669 if (inst->opcode != OP_REGVAR) {
670 inst->opcode = OP_REGOFFSET;
671 inst->inst_basereg = frame_reg;
672 size = mono_type_size (sig->params [i], &align);
674 offset &= ~(align - 1);
675 inst->inst_offset = offset;
677 if ((sig->call_convention == MONO_CALL_VARARG) && (i < sig->sentinelpos))
678 m->sig_cookie += size;
683 /* align the offset to 8 bytes */
688 m->stack_offset = offset;
692 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
693 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
697 * take the arguments and generate the arch-specific
698 * instructions to properly call the function in call.
699 * This includes pushing, moving arguments to the right register
701 * Issue: who does the spilling if needed, and when?
704 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
706 MonoMethodSignature *sig;
711 sig = call->signature;
712 n = sig->param_count + sig->hasthis;
714 cinfo = calculate_sizes (sig, sig->pinvoke);
715 if (cinfo->struct_ret)
716 call->used_iregs |= 1 << cinfo->struct_ret;
718 for (i = 0; i < n; ++i) {
719 ainfo = cinfo->args + i;
720 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
722 cfg->disable_aot = TRUE;
724 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
725 sig_arg->inst_p0 = call->signature;
727 MONO_INST_NEW (cfg, arg, OP_OUTARG);
728 arg->inst_imm = cinfo->sig_cookie.offset;
729 arg->inst_left = sig_arg;
731 /* prepend, so they get reversed */
732 arg->next = call->out_args;
733 call->out_args = arg;
735 if (is_virtual && i == 0) {
736 /* the argument will be attached to the call instrucion */
738 call->used_iregs |= 1 << ainfo->reg;
740 MONO_INST_NEW (cfg, arg, OP_OUTARG);
742 arg->cil_code = in->cil_code;
744 arg->inst_right = (MonoInst*)call;
745 arg->type = in->type;
746 /* prepend, we'll need to reverse them later */
747 arg->next = call->out_args;
748 call->out_args = arg;
749 if (ainfo->regtype == RegTypeGeneral) {
750 arg->unused = ainfo->reg;
751 call->used_iregs |= 1 << ainfo->reg;
752 if (arg->type == STACK_I8)
753 call->used_iregs |= 1 << (ainfo->reg + 1);
754 } else if (ainfo->regtype == RegTypeStructByAddr) {
755 /* FIXME: where si the data allocated? */
756 arg->unused = ainfo->reg;
757 call->used_iregs |= 1 << ainfo->reg;
758 } else if (ainfo->regtype == RegTypeStructByVal) {
760 /* mark the used regs */
761 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
762 call->used_iregs |= 1 << (ainfo->reg + cur_reg);
764 arg->opcode = OP_OUTARG_VT;
765 arg->unused = ainfo->reg | (ainfo->size << 8) | (ainfo->vtsize << 16);
766 arg->inst_imm = ainfo->offset;
767 } else if (ainfo->regtype == RegTypeBase) {
768 arg->opcode = OP_OUTARG_MEMBASE;
769 arg->unused = ainfo->reg | (ainfo->size << 8);
770 arg->inst_imm = ainfo->offset;
771 } else if (ainfo->regtype == RegTypeFP) {
772 arg->opcode = OP_OUTARG_R8;
773 arg->unused = ainfo->reg;
774 call->used_fregs |= 1 << ainfo->reg;
775 if (ainfo->size == 4) {
776 arg->opcode = OP_OUTARG_R8;
777 /* we reduce the precision */
779 MONO_INST_NEW (cfg, conv, OP_FCONV_TO_R4);
780 conv->inst_left = arg->inst_left;
781 arg->inst_left = conv;*/
784 g_assert_not_reached ();
789 * Reverse the call->out_args list.
792 MonoInst *prev = NULL, *list = call->out_args, *next;
799 call->out_args = prev;
801 call->stack_usage = cinfo->stack_usage;
802 cfg->param_area = MAX (cfg->param_area, cinfo->stack_usage);
803 cfg->flags |= MONO_CFG_HAS_CALLS;
805 * should set more info in call, such as the stack space
806 * used by the args that needs to be added back to esp
814 * Allow tracing to work with this interface (with an optional argument)
818 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
822 code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->method);
823 ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0); /* NULL ebp for now */
824 code = mono_arm_emit_load_imm (code, ARMREG_R2, (guint32)func);
825 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
826 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R2);
839 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
842 int save_mode = SAVE_NONE;
844 MonoMethod *method = cfg->method;
845 int rtype = mono_type_get_underlying_type (mono_method_signature (method)->ret)->type;
846 int save_offset = cfg->param_area;
850 offset = code - cfg->native_code;
851 /* we need about 16 instructions */
852 if (offset > (cfg->code_size - 16 * 4)) {
854 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
855 code = cfg->native_code + offset;
860 /* special case string .ctor icall */
861 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
862 save_mode = SAVE_ONE;
864 save_mode = SAVE_NONE;
868 save_mode = SAVE_TWO;
874 case MONO_TYPE_VALUETYPE:
875 save_mode = SAVE_STRUCT;
878 save_mode = SAVE_ONE;
884 ARM_STR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
885 ARM_STR_IMM (code, ARMREG_R1, cfg->frame_reg, save_offset + 4);
886 if (enable_arguments) {
887 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_R1);
888 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
892 ARM_STR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
893 if (enable_arguments) {
894 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
898 /* FIXME: what reg? */
899 if (enable_arguments) {
900 /* FIXME: what reg? */
904 if (enable_arguments) {
905 /* FIXME: get the actual address */
906 ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
914 code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->method);
915 code = mono_arm_emit_load_imm (code, ARMREG_IP, (guint32)func);
916 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
917 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
921 ARM_LDR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
922 ARM_LDR_IMM (code, ARMREG_R1, cfg->frame_reg, save_offset + 4);
925 ARM_LDR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
939 * The immediate field for cond branches is big enough for all reasonable methods
941 #define EMIT_COND_BRANCH_FLAGS(ins,condcode) \
942 if (ins->flags & MONO_INST_BRLABEL) { \
943 if (0 && ins->inst_i0->inst_c0) { \
944 ARM_B_COND (code, (condcode), (code - cfg->native_code + ins->inst_i0->inst_c0) & 0xffffff); \
946 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
947 ARM_B_COND (code, (condcode), 0); \
950 if (0 && ins->inst_true_bb->native_offset) { \
951 ARM_B_COND (code, (condcode), (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffffff); \
953 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
954 ARM_B_COND (code, (condcode), 0); \
958 #define EMIT_COND_BRANCH(ins,cond) EMIT_COND_BRANCH_FLAGS(ins, branch_cc_table [(cond)])
960 /* emit an exception if condition is fail
962 * We assign the extra code used to throw the implicit exceptions
963 * to cfg->bb_exit as far as the big branch handling is concerned
965 #define EMIT_COND_SYSTEM_EXCEPTION_FLAGS(condcode,exc_name) \
967 mono_add_patch_info (cfg, code - cfg->native_code, \
968 MONO_PATCH_INFO_EXC, exc_name); \
969 ARM_BL_COND (code, (condcode), 0); \
972 #define EMIT_COND_SYSTEM_EXCEPTION(cond,exc_name) EMIT_COND_SYSTEM_EXCEPTION_FLAGS(branch_cc_table [(cond)], (exc_name))
975 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
977 MonoInst *ins, *last_ins = NULL;
982 switch (ins->opcode) {
984 /* remove unnecessary multiplication with 1 */
985 if (ins->inst_imm == 1) {
986 if (ins->dreg != ins->sreg1) {
987 ins->opcode = OP_MOVE;
989 last_ins->next = ins->next;
994 int power2 = mono_is_power_of_two (ins->inst_imm);
996 ins->opcode = OP_SHL_IMM;
997 ins->inst_imm = power2;
1001 case OP_LOAD_MEMBASE:
1002 case OP_LOADI4_MEMBASE:
1004 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1005 * OP_LOAD_MEMBASE offset(basereg), reg
1007 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1008 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1009 ins->inst_basereg == last_ins->inst_destbasereg &&
1010 ins->inst_offset == last_ins->inst_offset) {
1011 if (ins->dreg == last_ins->sreg1) {
1012 last_ins->next = ins->next;
1016 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1017 ins->opcode = OP_MOVE;
1018 ins->sreg1 = last_ins->sreg1;
1022 * Note: reg1 must be different from the basereg in the second load
1023 * OP_LOAD_MEMBASE offset(basereg), reg1
1024 * OP_LOAD_MEMBASE offset(basereg), reg2
1026 * OP_LOAD_MEMBASE offset(basereg), reg1
1027 * OP_MOVE reg1, reg2
1029 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1030 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1031 ins->inst_basereg != last_ins->dreg &&
1032 ins->inst_basereg == last_ins->inst_basereg &&
1033 ins->inst_offset == last_ins->inst_offset) {
1035 if (ins->dreg == last_ins->dreg) {
1036 last_ins->next = ins->next;
1040 ins->opcode = OP_MOVE;
1041 ins->sreg1 = last_ins->dreg;
1044 //g_assert_not_reached ();
1048 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1049 * OP_LOAD_MEMBASE offset(basereg), reg
1051 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1052 * OP_ICONST reg, imm
1054 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1055 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1056 ins->inst_basereg == last_ins->inst_destbasereg &&
1057 ins->inst_offset == last_ins->inst_offset) {
1058 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1059 ins->opcode = OP_ICONST;
1060 ins->inst_c0 = last_ins->inst_imm;
1061 g_assert_not_reached (); // check this rule
1065 case OP_LOADU1_MEMBASE:
1066 case OP_LOADI1_MEMBASE:
1067 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1068 ins->inst_basereg == last_ins->inst_destbasereg &&
1069 ins->inst_offset == last_ins->inst_offset) {
1070 if (ins->dreg == last_ins->sreg1) {
1071 last_ins->next = ins->next;
1075 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1076 ins->opcode = OP_MOVE;
1077 ins->sreg1 = last_ins->sreg1;
1081 case OP_LOADU2_MEMBASE:
1082 case OP_LOADI2_MEMBASE:
1083 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1084 ins->inst_basereg == last_ins->inst_destbasereg &&
1085 ins->inst_offset == last_ins->inst_offset) {
1086 if (ins->dreg == last_ins->sreg1) {
1087 last_ins->next = ins->next;
1091 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1092 ins->opcode = OP_MOVE;
1093 ins->sreg1 = last_ins->sreg1;
1101 ins->opcode = OP_MOVE;
1105 if (ins->dreg == ins->sreg1) {
1107 last_ins->next = ins->next;
1112 * OP_MOVE sreg, dreg
1113 * OP_MOVE dreg, sreg
1115 if (last_ins && last_ins->opcode == OP_MOVE &&
1116 ins->sreg1 == last_ins->dreg &&
1117 ins->dreg == last_ins->sreg1) {
1118 last_ins->next = ins->next;
1127 bb->last_ins = last_ins;
1131 * the branch_cc_table should maintain the order of these
1145 branch_cc_table [] = {
1161 insert_after_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst *to_insert)
1165 bb->code = to_insert;
1166 to_insert->next = ins;
1168 to_insert->next = ins->next;
1169 ins->next = to_insert;
1173 #define NEW_INS(cfg,dest,op) do { \
1174 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
1175 (dest)->opcode = (op); \
1176 insert_after_ins (bb, last_ins, (dest)); \
1180 map_to_reg_reg_op (int op)
1189 case OP_COMPARE_IMM:
1204 g_assert_not_reached ();
1208 * Remove from the instruction list the instructions that can't be
1209 * represented with very simple instructions with no register
1213 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1215 MonoInst *ins, *next, *temp, *last_ins = NULL;
1216 int rot_amount, imm8;
1218 /* setup the virtual reg allocator */
1219 if (bb->max_ireg > cfg->rs->next_vireg)
1220 cfg->rs->next_vireg = bb->max_ireg;
1224 switch (ins->opcode) {
1228 case OP_COMPARE_IMM:
1235 if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount)) < 0) {
1236 NEW_INS (cfg, temp, OP_ICONST);
1237 temp->inst_c0 = ins->inst_imm;
1238 temp->dreg = mono_regstate_next_int (cfg->rs);
1239 ins->sreg2 = temp->dreg;
1240 ins->opcode = map_to_reg_reg_op (ins->opcode);
1244 NEW_INS (cfg, temp, OP_ICONST);
1245 temp->inst_c0 = ins->inst_imm;
1246 temp->dreg = mono_regstate_next_int (cfg->rs);
1247 ins->sreg2 = temp->dreg;
1248 ins->opcode = CEE_MUL;
1254 bb->last_ins = last_ins;
1255 bb->max_ireg = cfg->rs->next_vireg;
1260 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1264 mono_arch_lowering_pass (cfg, bb);
1265 mono_local_regalloc (cfg, bb);
1269 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
1271 /* sreg is a float, dreg is an integer reg */
1272 ARM_FIXZ (code, dreg, sreg);
1275 ARM_AND_REG_IMM8 (code, dreg, dreg, 0xff);
1276 else if (size == 2) {
1277 ARM_SHL_IMM (code, dreg, dreg, 16);
1278 ARM_SHR_IMM (code, dreg, dreg, 16);
1282 ARM_SHL_IMM (code, dreg, dreg, 24);
1283 ARM_SAR_IMM (code, dreg, dreg, 24);
1284 } else if (size == 2) {
1285 ARM_SHL_IMM (code, dreg, dreg, 16);
1286 ARM_SAR_IMM (code, dreg, dreg, 16);
1299 #define is_call_imm(diff) ((gint)(diff) >= -33554432 && (gint)(diff) <= 33554431)
1302 search_thunk_slot (void *data, int csize, int bsize, void *user_data) {
1303 PatchData *pdata = (PatchData*)user_data;
1304 guchar *code = data;
1305 guint32 *thunks = data;
1306 guint32 *endthunks = (guint32*)(code + bsize);
1308 int difflow, diffhigh;
1310 /* always ensure a call from pdata->code can reach to the thunks without further thunks */
1311 difflow = (char*)pdata->code - (char*)thunks;
1312 diffhigh = (char*)pdata->code - (char*)endthunks;
1313 if (!((is_call_imm (thunks) && is_call_imm (endthunks)) || (is_call_imm (difflow) && is_call_imm (diffhigh))))
1317 * The thunk is composed of 3 words:
1318 * load constant from thunks [2] into ARM_IP
1321 * Note that the LR register is already setup
1323 //g_print ("thunk nentries: %d\n", ((char*)endthunks - (char*)thunks)/16);
1324 if ((pdata->found == 2) || (pdata->code >= code && pdata->code <= code + csize)) {
1325 while (thunks < endthunks) {
1326 //g_print ("looking for target: %p at %p (%08x-%08x)\n", pdata->target, thunks, thunks [0], thunks [1]);
1327 if (thunks [2] == (guint32)pdata->target) {
1328 arm_patch (pdata->code, (guchar*)thunks);
1329 mono_arch_flush_icache (pdata->code, 4);
1332 } else if ((thunks [0] == 0) && (thunks [1] == 0) && (thunks [2] == 0)) {
1333 /* found a free slot instead: emit thunk */
1334 code = (guchar*)thunks;
1335 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
1336 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
1337 thunks [2] = (guint32)pdata->target;
1338 mono_arch_flush_icache ((guchar*)thunks, 12);
1340 arm_patch (pdata->code, (guchar*)thunks);
1341 mono_arch_flush_icache (pdata->code, 4);
1345 /* skip 12 bytes, the size of the thunk */
1349 //g_print ("failed thunk lookup for %p from %p at %p (%d entries)\n", pdata->target, pdata->code, data, count);
1355 handle_thunk (int absolute, guchar *code, guchar *target) {
1356 MonoDomain *domain = mono_domain_get ();
1360 pdata.target = target;
1361 pdata.absolute = absolute;
1364 mono_domain_lock (domain);
1365 mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
1368 /* this uses the first available slot */
1370 mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
1372 mono_domain_unlock (domain);
1374 if (pdata.found != 1)
1375 g_print ("thunk failed for %p from %p\n", target, code);
1376 g_assert (pdata.found == 1);
1380 arm_patch (guchar *code, guchar *target)
1382 guint32 ins = *(guint32*)code;
1383 guint32 prim = (ins >> 25) & 7;
1386 //g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
1387 if (prim == 5) { /* 101b */
1388 /* the diff starts 8 bytes from the branch opcode */
1389 gint diff = target - code - 8;
1391 if (diff <= 33554431) {
1393 ins = (ins & 0xff000000) | diff;
1394 *(guint32*)code = ins;
1398 /* diff between 0 and -33554432 */
1399 if (diff >= -33554432) {
1401 ins = (ins & 0xff000000) | (diff & ~0xff000000);
1402 *(guint32*)code = ins;
1407 handle_thunk (TRUE, code, target);
1412 if ((ins & 0x0ffffff0) == 0x12fff10) {
1413 /* branch and exchange: the address is constructed in a reg */
1414 g_assert_not_reached ();
1416 g_assert_not_reached ();
1418 // g_print ("patched with 0x%08x\n", ins);
1422 * Return the >= 0 uimm8 value if val can be represented with a byte + rotation
1423 * (with the rotation amount in *rot_amount. rot_amount is already adjusted
1424 * to be used with the emit macros.
1425 * Return -1 otherwise.
1428 mono_arm_is_rotated_imm8 (guint32 val, gint *rot_amount)
1431 for (i = 0; i < 31; i+= 2) {
1432 res = (val << (32 - i)) | (val >> i);
1435 *rot_amount = i? 32 - i: 0;
1442 * Emits in code a sequence of instructions that load the value 'val'
1443 * into the dreg register. Uses at most 4 instructions.
1446 mono_arm_emit_load_imm (guint8 *code, int dreg, guint32 val)
1448 int imm8, rot_amount;
1449 if ((imm8 = mono_arm_is_rotated_imm8 (val, &rot_amount)) >= 0) {
1450 ARM_MOV_REG_IMM (code, dreg, imm8, rot_amount);
1451 } else if ((imm8 = mono_arm_is_rotated_imm8 (~val, &rot_amount)) >= 0) {
1452 ARM_MVN_REG_IMM (code, dreg, imm8, rot_amount);
1455 ARM_MOV_REG_IMM8 (code, dreg, (val & 0xFF));
1457 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF00) >> 8, 24);
1459 if (val & 0xFF0000) {
1460 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
1462 if (val & 0xFF000000) {
1463 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
1465 } else if (val & 0xFF00) {
1466 ARM_MOV_REG_IMM (code, dreg, (val & 0xFF00) >> 8, 24);
1467 if (val & 0xFF0000) {
1468 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
1470 if (val & 0xFF000000) {
1471 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
1473 } else if (val & 0xFF0000) {
1474 ARM_MOV_REG_IMM (code, dreg, (val & 0xFF0000) >> 16, 16);
1475 if (val & 0xFF000000) {
1476 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
1479 //g_assert_not_reached ();
1485 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
1490 guint8 *code = cfg->native_code + cfg->code_len;
1491 MonoInst *last_ins = NULL;
1492 guint last_offset = 0;
1494 int imm8, rot_amount;
1496 if (cfg->opt & MONO_OPT_PEEPHOLE)
1497 peephole_pass (cfg, bb);
1499 /* we don't align basic blocks of loops on arm */
1501 if (cfg->verbose_level > 2)
1502 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
1504 cpos = bb->max_offset;
1506 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
1507 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
1508 //g_assert (!mono_compile_aot);
1511 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
1512 /* this is not thread save, but good enough */
1513 /* fixme: howto handle overflows? */
1514 //x86_inc_mem (code, &cov->data [bb->dfn].count);
1519 offset = code - cfg->native_code;
1521 max_len = ((guint8 *)arm_cpu_desc [ins->opcode])[MONO_INST_LEN];
1523 if (offset > (cfg->code_size - max_len - 16)) {
1524 cfg->code_size *= 2;
1525 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
1526 code = cfg->native_code + offset;
1528 // if (ins->cil_code)
1529 // g_print ("cil code\n");
1530 mono_debug_record_line_number (cfg, ins, offset);
1532 switch (ins->opcode) {
1534 g_assert_not_reached ();
1537 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
1538 ppc_mulhw (code, ppc_r3, ins->sreg1, ins->sreg2);
1541 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
1542 ppc_mulhwu (code, ppc_r3, ins->sreg1, ins->sreg2);
1544 case OP_STOREI1_MEMBASE_IMM:
1545 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFF);
1546 g_assert (arm_is_imm12 (ins->inst_offset));
1547 ARM_STRB_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
1549 case OP_STOREI2_MEMBASE_IMM:
1550 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFFFF);
1551 g_assert (arm_is_imm8 (ins->inst_offset));
1552 ARM_STRH_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
1554 case OP_STORE_MEMBASE_IMM:
1555 case OP_STOREI4_MEMBASE_IMM:
1556 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm);
1557 g_assert (arm_is_imm12 (ins->inst_offset));
1558 ARM_STR_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
1560 case OP_STOREI1_MEMBASE_REG:
1561 g_assert (arm_is_imm12 (ins->inst_offset));
1562 ARM_STRB_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
1564 case OP_STOREI2_MEMBASE_REG:
1565 g_assert (arm_is_imm8 (ins->inst_offset));
1566 ARM_STRH_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
1568 case OP_STORE_MEMBASE_REG:
1569 case OP_STOREI4_MEMBASE_REG:
1570 g_assert (arm_is_imm12 (ins->inst_offset));
1571 ARM_STR_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
1576 g_assert_not_reached ();
1579 g_assert_not_reached ();
1581 case OP_LOAD_MEMBASE:
1582 case OP_LOADI4_MEMBASE:
1583 case OP_LOADU4_MEMBASE:
1584 g_assert (arm_is_imm12 (ins->inst_offset));
1585 ARM_LDR_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
1587 case OP_LOADI1_MEMBASE:
1588 g_assert (arm_is_imm8 (ins->inst_offset));
1589 ARM_LDRSB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
1591 case OP_LOADU1_MEMBASE:
1592 g_assert (arm_is_imm12 (ins->inst_offset));
1593 ARM_LDRB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
1595 case OP_LOADU2_MEMBASE:
1596 g_assert (arm_is_imm8 (ins->inst_offset));
1597 ARM_LDRH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
1599 case OP_LOADI2_MEMBASE:
1600 g_assert (arm_is_imm8 (ins->inst_offset));
1601 ARM_LDRSH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
1604 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 24);
1605 ARM_SAR_IMM (code, ins->dreg, ins->dreg, 24);
1608 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
1609 ARM_SAR_IMM (code, ins->dreg, ins->dreg, 16);
1612 ARM_AND_REG_IMM8 (code, ins->dreg, ins->sreg1, 0xff);
1615 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
1616 ARM_SHR_IMM (code, ins->dreg, ins->dreg, 16);
1619 ARM_CMP_REG_REG (code, ins->sreg1, ins->sreg2);
1621 case OP_COMPARE_IMM:
1622 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1623 g_assert (imm8 >= 0);
1624 ARM_CMP_REG_IMM (code, ins->sreg1, imm8, rot_amount);
1626 case OP_X86_TEST_NULL:
1627 g_assert_not_reached ();
1633 ARM_ADDS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1636 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1639 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1642 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1643 g_assert (imm8 >= 0);
1644 ARM_ADDS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1647 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1648 g_assert (imm8 >= 0);
1649 ARM_ADD_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1652 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1653 g_assert (imm8 >= 0);
1654 ARM_ADCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1657 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1658 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
1660 case CEE_ADD_OVF_UN:
1661 ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1662 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
1665 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1666 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
1668 case CEE_SUB_OVF_UN:
1669 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1670 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
1672 case OP_ADD_OVF_CARRY:
1673 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1674 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
1676 case OP_ADD_OVF_UN_CARRY:
1677 ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1678 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
1680 case OP_SUB_OVF_CARRY:
1681 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1682 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
1684 case OP_SUB_OVF_UN_CARRY:
1685 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1686 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
1689 ARM_SUBS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1692 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1693 g_assert (imm8 >= 0);
1694 ARM_SUBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1697 ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1700 ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1703 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1704 g_assert (imm8 >= 0);
1705 ARM_SUB_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1708 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1709 g_assert (imm8 >= 0);
1710 ARM_SBCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1712 case OP_ARM_RSBS_IMM:
1713 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1714 g_assert (imm8 >= 0);
1715 ARM_RSBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1717 case OP_ARM_RSC_IMM:
1718 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1719 g_assert (imm8 >= 0);
1720 ARM_RSC_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1723 ARM_AND_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1726 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1727 g_assert (imm8 >= 0);
1728 ARM_AND_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1736 /* crappy ARM arch doesn't have a DIV instruction */
1737 g_assert_not_reached ();
1739 ARM_ORR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1742 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1743 g_assert (imm8 >= 0);
1744 ARM_ORR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1747 ARM_EOR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1750 imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
1751 g_assert (imm8 >= 0);
1752 ARM_EOR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
1755 ARM_SHL_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1758 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
1761 ARM_SAR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1764 ARM_SAR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
1767 ARM_SHR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
1770 ARM_SHR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1773 ARM_MVN_REG_REG (code, ins->dreg, ins->sreg1);
1776 ARM_RSB_REG_IMM8 (code, ins->dreg, ins->sreg1, 0);
1779 if (ins->dreg == ins->sreg2)
1780 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1782 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg2, ins->sreg1);
1785 g_assert_not_reached ();
1788 /* FIXME: handle ovf/ sreg2 != dreg */
1789 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1791 case CEE_MUL_OVF_UN:
1792 /* FIXME: handle ovf/ sreg2 != dreg */
1793 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
1797 code = mono_arm_emit_load_imm (code, ins->dreg, ins->inst_c0);
1800 g_assert_not_reached ();
1801 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
1807 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
1810 int saved = ins->sreg2;
1811 if (ins->sreg2 == ARMREG_R0) {
1812 ARM_MOV_REG_REG (code, ARMREG_LR, ins->sreg2);
1815 if (ins->sreg1 != ARMREG_R0)
1816 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
1817 if (saved != ARMREG_R1)
1818 ARM_MOV_REG_REG (code, ARMREG_R1, saved);
1823 ARM_MVFD (code, ins->dreg, ins->sreg1);
1825 case OP_FCONV_TO_R4:
1826 ARM_MVFS (code, ins->dreg, ins->sreg1);
1833 * Keep in sync with mono_arch_emit_epilog
1835 g_assert (!cfg->method->save_lmf);
1836 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
1837 if (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET)) {
1838 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
1840 ppc_load (code, ppc_r11, cfg->stack_usage + PPC_RET_ADDR_OFFSET);
1841 ppc_lwzx (code, ppc_r0, cfg->frame_reg, ppc_r11);
1843 ppc_mtlr (code, ppc_r0);
1845 if (ppc_is_imm16 (cfg->stack_usage)) {
1846 ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
1848 ppc_load (code, ppc_r11, cfg->stack_usage);
1849 ppc_add (code, ppc_sp, cfg->frame_reg, ppc_r11);
1851 if (!cfg->method->save_lmf) {
1852 /*for (i = 31; i >= 14; --i) {
1853 if (cfg->used_float_regs & (1 << i)) {
1854 pos += sizeof (double);
1855 ppc_lfd (code, i, -pos, cfg->frame_reg);
1858 for (i = 31; i >= 13; --i) {
1859 if (cfg->used_int_regs & (1 << i)) {
1860 pos += sizeof (gulong);
1861 ppc_lwz (code, i, -pos, cfg->frame_reg);
1865 /* FIXME restore from MonoLMF: though this can't happen yet */
1867 mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
1873 /* ensure ins->sreg1 is not NULL */
1874 ARM_LDR_IMM (code, ARMREG_LR, ins->sreg1, 0);
1878 if (ppc_is_imm16 (cfg->sig_cookie + cfg->stack_usage)) {
1879 ppc_addi (code, ppc_r11, cfg->frame_reg, cfg->sig_cookie + cfg->stack_usage);
1881 ppc_load (code, ppc_r11, cfg->sig_cookie + cfg->stack_usage);
1882 ppc_add (code, ppc_r11, cfg->frame_reg, ppc_r11);
1884 ppc_stw (code, ppc_r11, 0, ins->sreg1);
1893 call = (MonoCallInst*)ins;
1894 if (ins->flags & MONO_INST_HAS_METHOD)
1895 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
1897 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
1898 if (cfg->method->dynamic) {
1899 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
1901 *(gpointer*)code = NULL;
1903 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
1904 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
1912 case OP_VOIDCALL_REG:
1914 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
1915 ARM_MOV_REG_REG (code, ARMREG_PC, ins->sreg1);
1917 case OP_FCALL_MEMBASE:
1918 case OP_LCALL_MEMBASE:
1919 case OP_VCALL_MEMBASE:
1920 case OP_VOIDCALL_MEMBASE:
1921 case OP_CALL_MEMBASE:
1922 g_assert (ins->inst_offset >= 0 && ins->inst_offset < 4096);
1923 ARM_LDR_IMM (code, ARMREG_IP, ins->sreg1, ins->inst_offset);
1924 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
1925 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
1928 g_assert_not_reached ();
1931 /* keep alignment */
1932 int alloca_waste = cfg->param_area;
1935 /* round the size to 8 bytes */
1936 ARM_ADD_REG_IMM8 (code, ins->dreg, ins->sreg1, 7);
1937 ARM_SHL_IMM (code, ins->dreg, ins->dreg, 3);
1938 ARM_SHR_IMM (code, ins->dreg, ins->dreg, 3);
1939 ARM_ADD_REG_IMM8 (code, ins->dreg, ins->dreg, alloca_waste);
1940 ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ins->dreg);
1941 /* memzero the area */
1942 if (ins->flags & MONO_INST_INIT) {
1943 guint8 *start_loop, *branch_to_cond;
1944 ARM_MOV_REG_IMM8 (code, ARMREG_LR, 0);
1945 branch_to_cond = code;
1948 ARM_STR_REG_REG (code, ARMREG_LR, ARMREG_SP, ins->dreg);
1949 arm_patch (branch_to_cond, code);
1950 /* decrement by 4 and set flags */
1951 ARM_SUBS_REG_IMM8 (code, ins->dreg, ins->dreg, 4);
1952 ARM_B_COND (code, ARMCOND_LT, 0);
1953 arm_patch (code - 4, start_loop);
1955 ARM_ADD_REG_IMM8 (code, ins->dreg, ARMREG_SP, alloca_waste);
1959 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_LR);
1962 if (ins->sreg1 != ARMREG_R0)
1963 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
1964 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
1965 (gpointer)"mono_arch_throw_exception");
1966 if (cfg->method->dynamic) {
1967 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
1969 *(gpointer*)code = NULL;
1971 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
1972 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
1979 if (ins->sreg1 != ARMREG_R0)
1980 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
1981 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
1982 (gpointer)"mono_arch_rethrow_exception");
1983 if (cfg->method->dynamic) {
1984 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
1986 *(gpointer*)code = NULL;
1988 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
1989 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
1995 case OP_START_HANDLER:
1997 ppc_mflr (code, ppc_r0);
1998 if (ppc_is_imm16 (ins->inst_left->inst_offset)) {
1999 ppc_stw (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2001 ppc_load (code, ppc_r11, ins->inst_left->inst_offset);
2002 ppc_stwx (code, ppc_r0, ppc_r11, ins->inst_left->inst_basereg);
2008 if (ins->sreg1 != ARMREG_R0)
2009 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
2010 if (ppc_is_imm16 (ins->inst_left->inst_offset)) {
2011 ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2013 ppc_load (code, ppc_r11, ins->inst_left->inst_offset);
2014 ppc_lwzx (code, ppc_r0, ins->inst_left->inst_basereg, ppc_r11);
2016 ppc_mtlr (code, ppc_r0);
2020 case CEE_ENDFINALLY:
2022 ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2023 ppc_mtlr (code, ppc_r0);
2027 case OP_CALL_HANDLER:
2028 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2032 ins->inst_c0 = code - cfg->native_code;
2035 if (ins->flags & MONO_INST_BRLABEL) {
2036 /*if (ins->inst_i0->inst_c0) {
2038 //x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2040 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2044 /*if (ins->inst_target_bb->native_offset) {
2046 //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
2048 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2054 ARM_MOV_REG_REG (code, ARMREG_PC, ins->sreg1);
2058 * In the normal case we have:
2059 * ldr pc, [pc, ins->sreg1 << 2]
2062 * ldr lr, [pc, ins->sreg1 << 2]
2064 * After follows the data.
2065 * FIXME: add aot support.
2067 max_len += 4 * GPOINTER_TO_INT (ins->klass);
2068 if (offset > (cfg->code_size - max_len - 16)) {
2069 cfg->code_size += max_len;
2070 cfg->code_size *= 2;
2071 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2072 code = cfg->native_code + offset;
2074 ARM_LDR_REG_REG_SHIFT (code, ARMREG_PC, ARMREG_PC, ins->sreg1, ARMSHIFT_LSL, 2);
2076 code += 4 * GPOINTER_TO_INT (ins->klass);
2079 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2080 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
2083 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2084 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LT);
2087 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2088 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LO);
2091 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2092 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_GT);
2095 ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
2096 ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_HI);
2098 case OP_COND_EXC_EQ:
2099 case OP_COND_EXC_NE_UN:
2100 case OP_COND_EXC_LT:
2101 case OP_COND_EXC_LT_UN:
2102 case OP_COND_EXC_GT:
2103 case OP_COND_EXC_GT_UN:
2104 case OP_COND_EXC_GE:
2105 case OP_COND_EXC_GE_UN:
2106 case OP_COND_EXC_LE:
2107 case OP_COND_EXC_LE_UN:
2108 EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_EQ, ins->inst_p1);
2111 case OP_COND_EXC_OV:
2112 case OP_COND_EXC_NC:
2113 case OP_COND_EXC_NO:
2114 g_assert_not_reached ();
2126 EMIT_COND_BRANCH (ins, ins->opcode - CEE_BEQ);
2129 /* floating point opcodes */
2131 /* FIXME: we can optimize the imm load by dealing with part of
2132 * the displacement in LDFD (aligning to 512).
2134 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
2135 ARM_LDFD (code, ins->dreg, ARMREG_LR, 0);
2138 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
2139 ARM_LDFS (code, ins->dreg, ARMREG_LR, 0);
2141 case OP_STORER8_MEMBASE_REG:
2142 g_assert (arm_is_fpimm8 (ins->inst_offset));
2143 ARM_STFD (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
2145 case OP_LOADR8_MEMBASE:
2146 g_assert (arm_is_fpimm8 (ins->inst_offset));
2147 ARM_LDFD (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
2149 case OP_STORER4_MEMBASE_REG:
2150 g_assert (arm_is_fpimm8 (ins->inst_offset));
2151 ARM_STFS (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
2153 case OP_LOADR4_MEMBASE:
2154 g_assert (arm_is_fpimm8 (ins->inst_offset));
2155 ARM_LDFS (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
2157 case CEE_CONV_R_UN: {
2158 /*static const guint64 adjust_val = 0x4330000000000000ULL;
2159 ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
2160 ppc_stw (code, ppc_r0, -8, ppc_sp);
2161 ppc_stw (code, ins->sreg1, -4, ppc_sp);
2162 ppc_load (code, ppc_r11, &adjust_val);
2163 ppc_lfd (code, ins->dreg, -8, ppc_sp);
2164 ppc_lfd (code, ppc_f0, 0, ppc_r11);
2165 ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);*/
2166 g_assert_not_reached ();
2170 ARM_FLTS (code, ins->dreg, ins->sreg1);
2173 ARM_FLTD (code, ins->dreg, ins->sreg1);
2175 case OP_X86_FP_LOAD_I8:
2176 g_assert_not_reached ();
2177 /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);*/
2179 case OP_X86_FP_LOAD_I4:
2180 g_assert_not_reached ();
2181 /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);*/
2183 case OP_FCONV_TO_I1:
2184 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
2186 case OP_FCONV_TO_U1:
2187 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
2189 case OP_FCONV_TO_I2:
2190 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
2192 case OP_FCONV_TO_U2:
2193 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
2195 case OP_FCONV_TO_I4:
2197 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
2199 case OP_FCONV_TO_U4:
2201 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
2203 case OP_FCONV_TO_I8:
2204 case OP_FCONV_TO_U8:
2205 g_assert_not_reached ();
2206 /* Implemented as helper calls */
2208 case OP_LCONV_TO_R_UN:
2209 g_assert_not_reached ();
2210 /* Implemented as helper calls */
2212 case OP_LCONV_TO_OVF_I: {
2214 guint32 *negative_branch, *msword_positive_branch, *msword_negative_branch, *ovf_ex_target;
2215 // Check if its negative
2216 ppc_cmpi (code, 0, 0, ins->sreg1, 0);
2217 negative_branch = code;
2218 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 0);
2219 // Its positive msword == 0
2220 ppc_cmpi (code, 0, 0, ins->sreg2, 0);
2221 msword_positive_branch = code;
2222 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
2224 ovf_ex_target = code;
2225 //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_ALWAYS, 0, "OverflowException");
2227 ppc_patch (negative_branch, code);
2228 ppc_cmpi (code, 0, 0, ins->sreg2, -1);
2229 msword_negative_branch = code;
2230 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 0);
2231 ppc_patch (msword_negative_branch, ovf_ex_target);
2233 ppc_patch (msword_positive_branch, code);
2234 if (ins->dreg != ins->sreg1)
2235 ppc_mr (code, ins->dreg, ins->sreg1);
2237 g_assert_not_reached ();
2241 ARM_FPA_ADFD (code, ins->dreg, ins->sreg1, ins->sreg2);
2244 ARM_FPA_SUFD (code, ins->dreg, ins->sreg1, ins->sreg2);
2247 ARM_FPA_MUFD (code, ins->dreg, ins->sreg1, ins->sreg2);
2250 ARM_FPA_DVFD (code, ins->dreg, ins->sreg1, ins->sreg2);
2253 ARM_MNFD (code, ins->dreg, ins->sreg1);
2257 g_assert_not_reached ();
2260 ARM_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
2263 g_assert_not_reached ();
2264 /*ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
2265 ppc_li (code, ins->dreg, 0);
2266 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
2267 ppc_li (code, ins->dreg, 1);*/
2270 g_assert_not_reached ();
2271 /*ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
2272 ppc_li (code, ins->dreg, 1);
2273 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2274 ppc_li (code, ins->dreg, 0);*/
2277 g_assert_not_reached ();
2278 /*ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
2279 ppc_li (code, ins->dreg, 1);
2280 ppc_bc (code, PPC_BR_TRUE, PPC_BR_SO, 3);
2281 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2282 ppc_li (code, ins->dreg, 0);*/
2285 g_assert_not_reached ();
2286 /*ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
2287 ppc_li (code, ins->dreg, 1);
2288 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
2289 ppc_li (code, ins->dreg, 0);*/
2292 g_assert_not_reached ();
2293 /*ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
2294 ppc_li (code, ins->dreg, 1);
2295 ppc_bc (code, PPC_BR_TRUE, PPC_BR_SO, 3);
2296 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
2297 ppc_li (code, ins->dreg, 0);*/
2300 EMIT_COND_BRANCH (ins, CEE_BEQ - CEE_BEQ);
2303 EMIT_COND_BRANCH (ins, CEE_BNE_UN - CEE_BEQ);
2306 EMIT_COND_BRANCH (ins, CEE_BLT - CEE_BEQ);
2309 //EMIT_COND_BRANCH_FLAGS (ins, PPC_BR_TRUE, PPC_BR_SO);
2310 EMIT_COND_BRANCH (ins, CEE_BLT_UN - CEE_BEQ);
2313 EMIT_COND_BRANCH (ins, CEE_BGT - CEE_BEQ);
2316 //EMIT_COND_BRANCH_FLAGS (ins, PPC_BR_TRUE, PPC_BR_SO);
2317 EMIT_COND_BRANCH (ins, CEE_BGT_UN - CEE_BEQ);
2320 EMIT_COND_BRANCH (ins, CEE_BGE - CEE_BEQ);
2323 EMIT_COND_BRANCH (ins, CEE_BGE_UN - CEE_BEQ);
2326 EMIT_COND_BRANCH (ins, CEE_BLE - CEE_BEQ);
2329 EMIT_COND_BRANCH (ins, CEE_BLE_UN - CEE_BEQ);
2331 case CEE_CKFINITE: {
2332 /*ppc_stfd (code, ins->sreg1, -8, ppc_sp);
2333 ppc_lwz (code, ppc_r11, -8, ppc_sp);
2334 ppc_rlwinm (code, ppc_r11, ppc_r11, 0, 1, 31);
2335 ppc_addis (code, ppc_r11, ppc_r11, -32752);
2336 ppc_rlwinmd (code, ppc_r11, ppc_r11, 1, 31, 31);
2337 EMIT_COND_SYSTEM_EXCEPTION (CEE_BEQ - CEE_BEQ, "ArithmeticException");*/
2338 g_assert_not_reached ();
2342 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
2343 g_assert_not_reached ();
2346 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
2347 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
2348 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
2349 g_assert_not_reached ();
2355 last_offset = offset;
2360 cfg->code_len = code - cfg->native_code;
2364 mono_arch_register_lowlevel_calls (void)
2368 #define patch_lis_ori(ip,val) do {\
2369 guint16 *__lis_ori = (guint16*)(ip); \
2370 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff; \
2371 __lis_ori [3] = ((guint32)(val)) & 0xffff; \
2375 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
2377 MonoJumpInfo *patch_info;
2379 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
2380 unsigned char *ip = patch_info->ip.i + code;
2381 const unsigned char *target;
2383 if (patch_info->type == MONO_PATCH_INFO_SWITCH) {
2384 gpointer *table = (gpointer *)patch_info->data.table->table;
2385 gpointer *jt = (gpointer*)(ip + 8);
2387 /* jt is the inlined jump table, 2 instructions after ip
2388 * In the normal case we store the absolute addresses,
2389 * otherwise the displacements.
2391 for (i = 0; i < patch_info->data.table->table_size; i++) {
2392 jt [i] = code + (int)patch_info->data.table->table [i];
2396 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
2398 switch (patch_info->type) {
2399 case MONO_PATCH_INFO_IP:
2400 patch_lis_ori (ip, ip);
2402 case MONO_PATCH_INFO_METHOD_REL:
2403 g_assert_not_reached ();
2404 *((gpointer *)(ip)) = code + patch_info->data.offset;
2406 case MONO_PATCH_INFO_METHODCONST:
2407 case MONO_PATCH_INFO_CLASS:
2408 case MONO_PATCH_INFO_IMAGE:
2409 case MONO_PATCH_INFO_FIELD:
2410 case MONO_PATCH_INFO_VTABLE:
2411 case MONO_PATCH_INFO_IID:
2412 case MONO_PATCH_INFO_SFLDA:
2413 case MONO_PATCH_INFO_LDSTR:
2414 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
2415 case MONO_PATCH_INFO_LDTOKEN:
2416 /* from OP_AOTCONST : lis + ori */
2417 patch_lis_ori (ip, target);
2419 case MONO_PATCH_INFO_R4:
2420 case MONO_PATCH_INFO_R8:
2421 g_assert_not_reached ();
2422 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
2424 case MONO_PATCH_INFO_EXC_NAME:
2425 g_assert_not_reached ();
2426 *((gconstpointer *)(ip + 1)) = patch_info->data.name;
2428 case MONO_PATCH_INFO_NONE:
2429 case MONO_PATCH_INFO_BB_OVF:
2430 case MONO_PATCH_INFO_EXC_OVF:
2431 /* everything is dealt with at epilog output time */
2436 arm_patch (ip, target);
2441 * Stack frame layout:
2443 * ------------------- fp
2444 * MonoLMF structure or saved registers
2445 * -------------------
2447 * -------------------
2449 * -------------------
2450 * optional 8 bytes for tracing
2451 * -------------------
2452 * param area size is cfg->param_area
2453 * ------------------- sp
2456 mono_arch_emit_prolog (MonoCompile *cfg)
2458 MonoMethod *method = cfg->method;
2460 MonoMethodSignature *sig;
2462 int alloc_size, pos, max_offset, i;
2468 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
2471 sig = mono_method_signature (method);
2472 cfg->code_size = 256 + sig->param_count * 20;
2473 code = cfg->native_code = g_malloc (cfg->code_size);
2475 if (cfg->max_ireg >= 29)
2476 cfg->used_int_regs |= USE_EXTRA_TEMPS;
2477 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
2478 ARM_PUSH (code, (cfg->used_int_regs | (1 << ARMREG_IP) | (1 << ARMREG_LR)));
2480 alloc_size = cfg->stack_offset;
2483 if (!method->save_lmf) {
2484 /*for (i = 31; i >= 14; --i) {
2485 if (cfg->used_float_regs & (1 << i)) {
2486 pos += sizeof (gdouble);
2487 ppc_stfd (code, i, -pos, ppc_sp);
2492 pos += sizeof (MonoLMF);
2494 ofs = -pos + G_STRUCT_OFFSET(MonoLMF, iregs);
2495 ppc_stmw (code, ppc_r13, ppc_r1, ofs);
2496 for (i = 14; i < 32; i++) {
2497 ppc_stfd (code, i, (-pos + G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble))), ppc_r1);
2501 // align to MONO_ARCH_FRAME_ALIGNMENT bytes
2502 if (alloc_size & (MONO_ARCH_FRAME_ALIGNMENT - 1)) {
2503 alloc_size += MONO_ARCH_FRAME_ALIGNMENT - 1;
2504 alloc_size &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
2507 cfg->stack_usage = alloc_size;
2508 g_assert ((alloc_size & (MONO_ARCH_FRAME_ALIGNMENT-1)) == 0);
2510 ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, alloc_size);
2512 if (cfg->frame_reg != ARMREG_SP)
2513 ARM_MOV_REG_REG (code, cfg->frame_reg, ARMREG_SP);
2515 /* compute max_offset in order to use short forward jumps
2516 * we always do it on ppc because the immediate displacement
2517 * for jumps is too small
2520 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2521 MonoInst *ins = bb->code;
2522 bb->max_offset = max_offset;
2524 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
2528 max_offset += ((guint8 *)arm_cpu_desc [ins->opcode])[MONO_INST_LEN];
2533 /* load arguments allocated to register from the stack */
2536 cinfo = calculate_sizes (sig, sig->pinvoke);
2538 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
2539 ArgInfo *ainfo = &cinfo->ret;
2541 g_assert (arm_is_imm12 (inst->inst_offset));
2542 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
2544 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2545 ArgInfo *ainfo = cinfo->args + i;
2546 inst = cfg->varinfo [pos];
2548 if (cfg->verbose_level > 2)
2549 g_print ("Saving argument %d (type: %d)\n", i, ainfo->regtype);
2550 if (inst->opcode == OP_REGVAR) {
2551 if (ainfo->regtype == RegTypeGeneral)
2552 ARM_MOV_REG_REG (code, inst->dreg, ainfo->reg);
2553 else if (ainfo->regtype == RegTypeFP) {
2554 g_assert_not_reached ();
2555 //ppc_fmr (code, inst->dreg, ainfo->reg);
2556 } else if (ainfo->regtype == RegTypeBase) {
2557 g_assert_not_reached ();
2558 //ppc_lwz (code, ppc_r11, 0, ppc_sp);
2559 //ppc_lwz (code, inst->dreg, ainfo->offset, ppc_r11);
2561 g_assert_not_reached ();
2563 if (cfg->verbose_level > 2)
2564 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
2567 /* the argument should be put on the stack: FIXME handle size != word */
2568 if (ainfo->regtype == RegTypeGeneral) {
2569 switch (ainfo->size) {
2571 if (ppc_is_imm16 (inst->inst_offset)) {
2572 ppc_stb (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
2574 ppc_load (code, ppc_r11, inst->inst_offset);
2575 ppc_stbx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
2579 if (ppc_is_imm16 (inst->inst_offset)) {
2580 ppc_sth (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
2582 ppc_load (code, ppc_r11, inst->inst_offset);
2583 ppc_sthx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
2587 if (ppc_is_imm16 (inst->inst_offset + 4)) {
2588 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
2589 ppc_stw (code, ainfo->reg + 1, inst->inst_offset + 4, inst->inst_basereg);
2591 ppc_load (code, ppc_r11, inst->inst_offset);
2592 ppc_add (code, ppc_r11, ppc_r11, inst->inst_basereg);
2593 ppc_stw (code, ainfo->reg, 0, ppc_r11);
2594 ppc_stw (code, ainfo->reg + 1, 4, ppc_r11);
2598 if (ppc_is_imm16 (inst->inst_offset)) {
2599 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
2601 ppc_load (code, ppc_r11, inst->inst_offset);
2602 ppc_stwx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
2606 } else if (ainfo->regtype == RegTypeBase) {
2607 /* load the previous stack pointer in r11 */
2608 ppc_lwz (code, ppc_r11, 0, ppc_sp);
2609 ppc_lwz (code, ppc_r0, ainfo->offset, ppc_r11);
2610 switch (ainfo->size) {
2612 if (ppc_is_imm16 (inst->inst_offset)) {
2613 ppc_stb (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
2615 ppc_load (code, ppc_r11, inst->inst_offset);
2616 ppc_stbx (code, ppc_r0, ppc_r11, inst->inst_basereg);
2620 if (ppc_is_imm16 (inst->inst_offset)) {
2621 ppc_sth (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
2623 ppc_load (code, ppc_r11, inst->inst_offset);
2624 ppc_sthx (code, ppc_r0, ppc_r11, inst->inst_basereg);
2628 if (ppc_is_imm16 (inst->inst_offset + 4)) {
2629 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
2630 ppc_lwz (code, ppc_r0, ainfo->offset + 4, ppc_r11);
2631 ppc_stw (code, ppc_r0, inst->inst_offset + 4, inst->inst_basereg);
2634 g_assert_not_reached ();
2638 if (ppc_is_imm16 (inst->inst_offset)) {
2639 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
2641 ppc_load (code, ppc_r11, inst->inst_offset);
2642 ppc_stwx (code, ppc_r0, ppc_r11, inst->inst_basereg);
2646 } else if (ainfo->regtype == RegTypeFP) {
2647 g_assert (ppc_is_imm16 (inst->inst_offset));
2648 if (ainfo->size == 8)
2649 ppc_stfd (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
2650 else if (ainfo->size == 4)
2651 ppc_stfs (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
2653 g_assert_not_reached ();
2654 } else if (ainfo->regtype == RegTypeStructByVal) {
2655 int doffset = inst->inst_offset;
2659 g_assert (ppc_is_imm16 (inst->inst_offset));
2660 g_assert (ppc_is_imm16 (inst->inst_offset + ainfo->size * sizeof (gpointer)));
2661 if (mono_class_from_mono_type (inst->inst_vtype))
2662 size = mono_class_native_size (mono_class_from_mono_type (inst->inst_vtype), NULL);
2663 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
2665 Darwin handles 1 and 2 byte structs specially by loading h/b into the arg
2666 register. Should this case include linux/ppc?
2670 ppc_sth (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
2672 ppc_stb (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
2675 ppc_stw (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
2676 soffset += sizeof (gpointer);
2677 doffset += sizeof (gpointer);
2679 if (ainfo->vtsize) {
2680 /* load the previous stack pointer in r11 (r0 gets overwritten by the memcpy) */
2681 ppc_lwz (code, ppc_r11, 0, ppc_sp);
2682 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
2683 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ppc_r11, ainfo->offset + soffset);
2685 } else if (ainfo->regtype == RegTypeStructByAddr) {
2686 g_assert (ppc_is_imm16 (inst->inst_offset));
2687 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
2688 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
2690 g_assert_not_reached ();
2696 if (method->save_lmf) {
2698 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2699 (gpointer)"mono_get_lmf_addr");
2700 if (cfg->method->dynamic) {
2701 /*ppc_lis (code, ppc_r0, 0);
2702 ppc_ori (code, ppc_r0, ppc_r0, 0);
2703 ppc_mtlr (code, ppc_r0);
2709 /* we build the MonoLMF structure on the stack - see mini-ppc.h */
2710 /* lmf_offset is the offset from the previous stack pointer,
2711 * alloc_size is the total stack space allocated, so the offset
2712 * of MonoLMF from the current stack ptr is alloc_size - lmf_offset.
2713 * The pointer to the struct is put in ppc_r11 (new_lmf).
2714 * The callee-saved registers are already in the MonoLMF structure
2716 ppc_addi (code, ppc_r11, ppc_sp, alloc_size - lmf_offset);
2717 /* ppc_r3 is the result from mono_get_lmf_addr () */
2718 ppc_stw (code, ppc_r3, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
2719 /* new_lmf->previous_lmf = *lmf_addr */
2720 ppc_lwz (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
2721 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
2722 /* *(lmf_addr) = r11 */
2723 ppc_stw (code, ppc_r11, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
2724 /* save method info */
2725 ppc_load (code, ppc_r0, method);
2726 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, method), ppc_r11);
2727 ppc_stw (code, ppc_sp, G_STRUCT_OFFSET(MonoLMF, ebp), ppc_r11);
2728 /* save the current IP */
2729 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
2730 ppc_load (code, ppc_r0, 0x01010101);
2731 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, eip), ppc_r11);
2736 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
2738 cfg->code_len = code - cfg->native_code;
2739 g_assert (cfg->code_len < cfg->code_size);
2746 mono_arch_emit_epilog (MonoCompile *cfg)
2748 MonoJumpInfo *patch_info;
2749 MonoMethod *method = cfg->method;
2751 int max_epilog_size = 16 + 20*4;
2754 if (cfg->method->save_lmf)
2755 max_epilog_size += 128;
2757 if (mono_jit_trace_calls != NULL)
2758 max_epilog_size += 50;
2760 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2761 max_epilog_size += 50;
2763 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
2764 cfg->code_size *= 2;
2765 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2766 mono_jit_stats.code_reallocs++;
2770 * Keep in sync with CEE_JMP
2772 code = cfg->native_code + cfg->code_len;
2774 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
2775 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
2779 if (method->save_lmf) {
2782 pos += sizeof (MonoLMF);
2784 /* save the frame reg in r8 */
2785 ppc_mr (code, ppc_r8, cfg->frame_reg);
2786 ppc_addi (code, ppc_r11, cfg->frame_reg, cfg->stack_usage - lmf_offset);
2787 /* r5 = previous_lmf */
2788 ppc_lwz (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
2790 ppc_lwz (code, ppc_r6, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
2791 /* *(lmf_addr) = previous_lmf */
2792 ppc_stw (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r6);
2793 /* FIXME: speedup: there is no actual need to restore the registers if
2794 * we didn't actually change them (idea from Zoltan).
2797 ppc_lmw (code, ppc_r13, ppc_r11, G_STRUCT_OFFSET(MonoLMF, iregs));
2799 /*for (i = 14; i < 32; i++) {
2800 ppc_lfd (code, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
2802 g_assert (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET));
2803 /* use the saved copy of the frame reg in r8 */
2804 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2805 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, ppc_r8);
2806 ppc_mtlr (code, ppc_r0);
2808 ppc_addic (code, ppc_sp, ppc_r8, cfg->stack_usage);
2810 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, cfg->stack_usage);
2811 ARM_POP_NWB (code, cfg->used_int_regs | ((1 << ARMREG_SP) | (1 << ARMREG_PC)));
2813 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, cfg->stack_usage);
2814 ARM_POP_NWB (code, cfg->used_int_regs | ((1 << ARMREG_SP) | (1 << ARMREG_PC)));
2817 cfg->code_len = code - cfg->native_code;
2819 g_assert (cfg->code_len < cfg->code_size);
2823 /* remove once throw_exception_by_name is eliminated */
2825 exception_id_by_name (const char *name)
2827 if (strcmp (name, "IndexOutOfRangeException") == 0)
2828 return MONO_EXC_INDEX_OUT_OF_RANGE;
2829 if (strcmp (name, "OverflowException") == 0)
2830 return MONO_EXC_OVERFLOW;
2831 if (strcmp (name, "ArithmeticException") == 0)
2832 return MONO_EXC_ARITHMETIC;
2833 if (strcmp (name, "DivideByZeroException") == 0)
2834 return MONO_EXC_DIVIDE_BY_ZERO;
2835 if (strcmp (name, "InvalidCastException") == 0)
2836 return MONO_EXC_INVALID_CAST;
2837 if (strcmp (name, "NullReferenceException") == 0)
2838 return MONO_EXC_NULL_REF;
2839 if (strcmp (name, "ArrayTypeMismatchException") == 0)
2840 return MONO_EXC_ARRAY_TYPE_MISMATCH;
2841 g_error ("Unknown intrinsic exception %s\n", name);
2845 mono_arch_emit_exceptions (MonoCompile *cfg)
2847 MonoJumpInfo *patch_info;
2850 const guint8* exc_throw_pos [MONO_EXC_INTRINS_NUM] = {NULL};
2851 guint8 exc_throw_found [MONO_EXC_INTRINS_NUM] = {0};
2854 int max_epilog_size = 50;
2856 /* count the number of exception infos */
2859 * make sure we have enough space for exceptions
2860 * 12 is the simulated call to throw_exception_by_name
2862 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2863 if (patch_info->type == MONO_PATCH_INFO_EXC) {
2864 i = exception_id_by_name (patch_info->data.target);
2865 if (!exc_throw_found [i]) {
2866 max_epilog_size += 12;
2867 exc_throw_found [i] = TRUE;
2872 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
2873 cfg->code_size *= 2;
2874 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2875 mono_jit_stats.code_reallocs++;
2878 code = cfg->native_code + cfg->code_len;
2880 /* add code to raise exceptions */
2881 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2882 switch (patch_info->type) {
2883 case MONO_PATCH_INFO_EXC: {
2884 unsigned char *ip = patch_info->ip.i + cfg->native_code;
2885 i = exception_id_by_name (patch_info->data.target);
2886 if (exc_throw_pos [i]) {
2887 arm_patch (ip, exc_throw_pos [i]);
2888 patch_info->type = MONO_PATCH_INFO_NONE;
2891 exc_throw_pos [i] = code;
2893 arm_patch (ip, code);
2894 /*mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_NAME, patch_info->data.target);*/
2895 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
2896 /* we got here from a conditional call, so the calling ip is set in lr already */
2897 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
2898 patch_info->data.name = "mono_arch_throw_exception_by_name";
2899 patch_info->ip.i = code - cfg->native_code;
2901 *(gpointer*)code = patch_info->data.target;
2911 cfg->code_len = code - cfg->native_code;
2913 g_assert (cfg->code_len < cfg->code_size);
2918 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
2923 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
2928 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
2931 int this_dreg = ARMREG_R0;
2934 this_dreg = ARMREG_R1;
2936 /* add the this argument */
2937 if (this_reg != -1) {
2939 MONO_INST_NEW (cfg, this, OP_SETREG);
2940 this->type = this_type;
2941 this->sreg1 = this_reg;
2942 this->dreg = mono_regstate_next_int (cfg->rs);
2943 mono_bblock_add_inst (cfg->cbb, this);
2944 mono_call_inst_add_outarg_reg (inst, this->dreg, this_dreg, FALSE);
2949 MONO_INST_NEW (cfg, vtarg, OP_SETREG);
2950 vtarg->type = STACK_MP;
2951 vtarg->sreg1 = vt_reg;
2952 vtarg->dreg = mono_regstate_next_int (cfg->rs);
2953 mono_bblock_add_inst (cfg->cbb, vtarg);
2954 mono_call_inst_add_outarg_reg (inst, vtarg->dreg, ARMREG_R0, FALSE);
2959 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2965 mono_arch_print_tree (MonoInst *tree, int arity)
2970 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
2976 mono_arch_get_thread_intrinsic (MonoCompile* cfg)
2982 mono_arch_flush_register_windows (void)