2 * mini-ppc.c: PowerPC 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 int mono_exc_esp_offset = 0;
23 mono_arch_regname (int reg) {
24 static const char * rnames[] = {
25 "ppc_r0", "ppc_sp", "ppc_r2", "ppc_r3", "ppc_r4",
26 "ppc_r5", "ppc_r6", "ppc_r7", "ppc_r8", "ppc_r9",
27 "ppc_r10", "ppc_r11", "ppc_r12", "ppc_r13", "ppc_r14",
28 "ppc_r15", "ppc_r16", "ppc_r17", "ppc_r18", "ppc_r19",
29 "ppc_r20", "ppc_r21", "ppc_r22", "ppc_r23", "ppc_r24",
30 "ppc_r25", "ppc_r26", "ppc_r27", "ppc_r28", "ppc_r29",
33 if (reg >= 0 && reg < 32)
42 } MonoJitArgumentInfo;
45 * arch_get_argument_info:
46 * @csig: a method signature
47 * @param_count: the number of parameters to consider
48 * @arg_info: an array to store the result infos
50 * Gathers information on parameters such as size, alignment and
51 * padding. arg_info should be large enought to hold param_count + 1 entries.
53 * Returns the size of the activation frame.
56 arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
58 int k, frame_size = 0;
62 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
63 frame_size += sizeof (gpointer);
67 arg_info [0].offset = offset;
70 frame_size += sizeof (gpointer);
74 arg_info [0].size = frame_size;
76 for (k = 0; k < param_count; k++) {
79 size = mono_type_native_stack_size (csig->params [k], &align);
81 size = mono_type_stack_size (csig->params [k], &align);
83 /* ignore alignment for now */
86 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
87 arg_info [k].pad = pad;
89 arg_info [k + 1].pad = 0;
90 arg_info [k + 1].size = size;
92 arg_info [k + 1].offset = offset;
96 align = MONO_ARCH_FRAME_ALIGNMENT;
97 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
98 arg_info [k].pad = pad;
103 static int indent_level = 0;
105 static void indent (int diff) {
106 int v = indent_level;
110 indent_level += diff;
114 enter_method (MonoMethod *method, char *ebp)
119 MonoJitArgumentInfo *arg_info;
120 MonoMethodSignature *sig;
123 fname = mono_method_full_name (method, TRUE);
125 printf ("ENTER: %s(", fname);
128 if (((int)ebp & (MONO_ARCH_FRAME_ALIGNMENT - 1)) != 0) {
129 g_error ("unaligned stack detected (%p)", ebp);
132 sig = method->signature;
134 arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
136 arch_get_argument_info (sig, sig->param_count, arg_info);
138 if (MONO_TYPE_ISSTRUCT (method->signature->ret)) {
139 g_assert (!method->signature->ret->byref);
141 printf ("VALUERET:%p, ", *((gpointer *)(ebp + 8)));
144 if (method->signature->hasthis) {
145 gpointer *this = (gpointer *)(ebp + arg_info [0].offset);
146 if (method->klass->valuetype) {
147 printf ("value:%p, ", *this);
149 o = *((MonoObject **)this);
152 class = o->vtable->klass;
154 if (class == mono_defaults.string_class) {
155 printf ("this:[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
157 printf ("this:%p[%s.%s], ", o, class->name_space, class->name);
160 printf ("this:NULL, ");
164 for (i = 0; i < method->signature->param_count; ++i) {
165 gpointer *cpos = (gpointer *)(ebp + arg_info [i + 1].offset);
166 int size = arg_info [i + 1].size;
168 MonoType *type = method->signature->params [i];
171 printf ("[BYREF:%p], ", *cpos);
172 } else switch (type->type) {
176 printf ("%p, ", (gpointer)*((int *)(cpos)));
178 case MONO_TYPE_BOOLEAN:
186 printf ("%d, ", *((int *)(cpos)));
188 case MONO_TYPE_STRING: {
189 MonoString *s = *((MonoString **)cpos);
191 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
192 printf ("[STRING:%p:%s], ", s, mono_string_to_utf8 (s));
194 printf ("[STRING:null], ");
197 case MONO_TYPE_CLASS:
198 case MONO_TYPE_OBJECT: {
199 o = *((MonoObject **)cpos);
201 class = o->vtable->klass;
203 if (class == mono_defaults.string_class) {
204 printf ("[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
205 } else if (class == mono_defaults.int32_class) {
206 printf ("[INT32:%p:%d], ", o, *(gint32 *)((char *)o + sizeof (MonoObject)));
208 printf ("[%s.%s:%p], ", class->name_space, class->name, o);
210 printf ("%p, ", *((gpointer *)(cpos)));
215 case MONO_TYPE_FNPTR:
216 case MONO_TYPE_ARRAY:
217 case MONO_TYPE_SZARRAY:
218 printf ("%p, ", *((gpointer *)(cpos)));
221 printf ("%lld, ", *((gint64 *)(cpos)));
224 printf ("%f, ", *((float *)(cpos)));
227 printf ("%f, ", *((double *)(cpos)));
229 case MONO_TYPE_VALUETYPE:
231 for (j = 0; j < size; j++)
232 printf ("%02x,", *((guint8*)cpos +j));
244 leave_method (MonoMethod *method, ...)
250 va_start(ap, method);
252 fname = mono_method_full_name (method, TRUE);
254 printf ("LEAVE: %s", fname);
257 type = method->signature->ret;
260 switch (type->type) {
263 case MONO_TYPE_BOOLEAN: {
264 int eax = va_arg (ap, int);
266 printf ("TRUE:%d", eax);
281 int eax = va_arg (ap, int);
282 printf ("EAX=%d", eax);
285 case MONO_TYPE_STRING: {
286 MonoString *s = va_arg (ap, MonoString *);
289 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
290 printf ("[STRING:%p:%s]", s, mono_string_to_utf8 (s));
292 printf ("[STRING:null], ");
295 case MONO_TYPE_CLASS:
296 case MONO_TYPE_OBJECT: {
297 MonoObject *o = va_arg (ap, MonoObject *);
300 if (o->vtable->klass == mono_defaults.boolean_class) {
301 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
302 } else if (o->vtable->klass == mono_defaults.int32_class) {
303 printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));
304 } else if (o->vtable->klass == mono_defaults.int64_class) {
305 printf ("[INT64:%p:%lld]", o, *((gint64 *)((char *)o + sizeof (MonoObject))));
307 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
309 printf ("[OBJECT:%p]", o);
314 case MONO_TYPE_FNPTR:
315 case MONO_TYPE_ARRAY:
316 case MONO_TYPE_SZARRAY: {
317 gpointer p = va_arg (ap, gpointer);
318 printf ("result=%p", p);
322 gint64 l = va_arg (ap, gint64);
323 printf ("lresult=%lld", l);
327 double f = va_arg (ap, double);
328 printf ("FP=%f\n", f);
331 case MONO_TYPE_VALUETYPE:
332 if (type->data.klass->enumtype) {
333 type = type->data.klass->enum_basetype;
336 guint8 *p = va_arg (ap, gpointer);
338 size = mono_type_size (type, &align);
340 for (j = 0; p && j < size; j++)
341 printf ("%02x,", p [j]);
346 printf ("(unknown return type %x)", method->signature->ret->type);
353 * Initialize the cpu to execute managed code.
356 mono_arch_cpu_init (void)
361 * This function returns the optimizations supported on this cpu.
364 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
368 /* no ppc-specific optimizations yet */
374 is_regsize_var (MonoType *t) {
383 case MONO_TYPE_OBJECT:
384 case MONO_TYPE_STRING:
385 case MONO_TYPE_CLASS:
386 case MONO_TYPE_SZARRAY:
387 case MONO_TYPE_ARRAY:
389 case MONO_TYPE_VALUETYPE:
390 if (t->data.klass->enumtype)
391 return is_regsize_var (t->data.klass->enum_basetype);
398 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
403 for (i = 0; i < cfg->num_varinfo; i++) {
404 MonoInst *ins = cfg->varinfo [i];
405 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
408 if (vmv->range.first_use.abs_pos > vmv->range.last_use.abs_pos)
411 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
414 /* we can only allocate 32 bit values */
415 if (is_regsize_var (ins->inst_vtype)) {
416 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
417 g_assert (i == vmv->idx);
418 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
426 mono_arch_get_global_int_regs (MonoCompile *cfg)
430 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
432 for (i = 13; i < top; ++i)
433 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
438 // code from ppc/tramp.c, try to keep in sync
439 #define MIN_CACHE_LINE 8
442 mono_arch_flush_icache (guint8 *code, gint size)
448 for (i = 0; i < size; i += MIN_CACHE_LINE, p += MIN_CACHE_LINE) {
449 asm ("dcbst 0,%0;" : : "r"(p) : "memory");
453 for (i = 0; i < size; i += MIN_CACHE_LINE, p += MIN_CACHE_LINE) {
454 asm ("icbi 0,%0; sync;" : : "r"(p) : "memory");
460 #define NOT_IMPLEMENTED(x) \
461 g_error ("FIXME: %s is not yet implemented. (trampoline)", x);
467 #define GENERAL_REGS 8
469 #define MINIMAL_STACK_SIZE 10
470 #define ALWAYS_ON_STACK(s) s
471 #define FP_ALSO_IN_REG(s) s
472 #define RET_ADDR_OFFSET 8
473 #define STACK_PARAM_OFFSET 24
475 #define MINIMAL_STACK_SIZE 5
476 #define ALWAYS_ON_STACK(s)
477 #define FP_ALSO_IN_REG(s) s
478 #define ALIGN_DOUBLES
479 #define RET_ADDR_OFFSET 4
480 #define STACK_PARAM_OFFSET 8
486 gint8 regtype; /* 0 general, 1 basereg, 2 floating point register */
499 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
502 if (*gr >= 3 + GENERAL_REGS) {
503 ainfo->offset = *stack_size;
504 ainfo->reg = ppc_sp; /* in the caller */
508 ALWAYS_ON_STACK (*stack_size += 4);
512 if (*gr >= 3 + GENERAL_REGS - 1) {
513 ainfo->offset = *stack_size;
514 ainfo->reg = ppc_sp; /* in the caller */
518 *stack_size += (*stack_size % 8);
521 ALWAYS_ON_STACK (*stack_size += 8);
534 calculate_sizes (MonoMethodSignature *sig, gboolean is_pinvoke)
537 int n = sig->hasthis + sig->param_count;
539 guint32 stack_size = 0;
540 CallInfo *cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * n);
545 /* FIXME: handle returning a struct */
549 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
552 DEBUG(printf("params: %d\n", sig->param_count));
553 for (i = 0; i < sig->param_count; ++i) {
554 DEBUG(printf("param %d: ", i));
555 if (sig->params [i]->byref) {
556 DEBUG(printf("byref\n"));
557 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
561 simpletype = sig->params [i]->type;
563 switch (simpletype) {
564 case MONO_TYPE_BOOLEAN:
575 case MONO_TYPE_CLASS:
576 case MONO_TYPE_OBJECT:
577 case MONO_TYPE_STRING:
578 case MONO_TYPE_SZARRAY:
579 case MONO_TYPE_ARRAY:
580 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
583 case MONO_TYPE_VALUETYPE: {
585 if (sig->params [i]->data.klass->enumtype) {
586 simpletype = sig->params [i]->data.klass->enum_basetype->type;
590 size = mono_class_value_size (sig->params [i]->data.klass, NULL);
592 DEBUG(printf ("copy %d bytes struct on stack\n",
593 mono_class_value_size (sig->params [i]->data.klass, NULL)));
594 *stack_size += (size + 3) & (~3);
595 if (gr > 3 + GENERAL_REGS) {
599 DEBUG(printf ("load %d bytes struct\n",
600 mono_class_value_size (sig->params [i]->data.klass, NULL)));
601 add_general (&gr, stack_size, code_size, TRUE);
604 g_assert_not_reached ();
609 add_general (&gr, &stack_size, cinfo->args + n, FALSE);
615 FP_ALSO_IN_REG (gr ++);
616 ALWAYS_ON_STACK (stack_size += 4);
618 NOT_IMPLEMENTED ("R4 arg");
625 FP_ALSO_IN_REG (gr += 2);
626 ALWAYS_ON_STACK (stack_size += 8);
628 NOT_IMPLEMENTED ("R8 arg");
633 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
638 simpletype = sig->ret->type;
640 switch (simpletype) {
641 case MONO_TYPE_BOOLEAN:
651 case MONO_TYPE_CLASS:
652 case MONO_TYPE_OBJECT:
653 case MONO_TYPE_SZARRAY:
654 case MONO_TYPE_ARRAY:
655 case MONO_TYPE_STRING:
656 cinfo->ret.reg = ppc_r3;
660 cinfo->ret.reg = ppc_r3;
664 cinfo->ret.reg = ppc_f1;
665 cinfo->ret.regtype = 2;
667 case MONO_TYPE_VALUETYPE:
668 if (sig->ret->data.klass->enumtype) {
669 simpletype = sig->ret->data.klass->enum_basetype->type;
676 g_error ("Can't handle as return value 0x%x", sig->ret->type);
680 /* align stack size to 16 */
681 DEBUG (printf (" stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
682 stack_size = (stack_size + 15) & ~15;
684 cinfo->stack_usage = stack_size;
690 * Set var information according to the calling convention. ppc version.
691 * The locals var stuff should most likely be split in another method.
694 mono_arch_allocate_vars (MonoCompile *m)
696 MonoMethodSignature *sig;
697 MonoMethodHeader *header;
699 int i, offset, size, align, curinst;
700 int frame_reg = ppc_sp;
702 if (m->flags & MONO_CFG_HAS_ALLOCA)
704 m->frame_reg = frame_reg;
706 header = ((MonoMethodNormal *)m->method)->header;
708 sig = m->method->signature;
712 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
713 m->ret->opcode = OP_REGVAR;
714 m->ret->inst_c0 = ppc_r3;
716 /* FIXME: handle long and FP values */
717 switch (sig->ret->type) {
721 m->ret->opcode = OP_REGVAR;
722 m->ret->inst_c0 = ppc_r3;
726 /* local vars are at a positive offset from the stack pointer */
728 * also note that if the function uses alloca, we use ppc_r31
729 * to point at the local variables.
731 offset = 24; /* linkage area */
732 /* align the offset to 16 bytes: not sure this is needed here */
734 //offset &= ~(16 - 1);
736 /* add parameter area size for called functions */
737 offset += m->param_area;
741 /* FIXME: check how to handle this stuff... reserve space to save LMF and caller saved registers */
742 offset += sizeof (MonoLMF);
745 /* this stuff should not be needed on ppc and the new jit,
746 * because a call on ppc to the handlers doesn't change the
747 * stack pointer and the jist doesn't manipulate the stack pointer
748 * for operations involving valuetypes.
750 /* reserve space to store the esp */
751 offset += sizeof (gpointer);
753 /* this is a global constant */
754 mono_exc_esp_offset = offset;
757 curinst = m->locals_start;
758 for (i = curinst; i < m->num_varinfo; ++i) {
759 inst = m->varinfo [i];
760 if (inst->opcode == OP_REGVAR)
763 /* inst->unused indicates native sized value types, this is used by the
764 * pinvoke wrappers when they call functions returning structure */
765 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype))
766 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
768 size = mono_type_size (inst->inst_vtype, &align);
771 offset &= ~(align - 1);
772 inst->inst_offset = offset;
773 inst->opcode = OP_REGOFFSET;
774 inst->inst_basereg = frame_reg;
776 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
781 inst = m->varinfo [curinst];
782 if (inst->opcode != OP_REGVAR) {
783 inst->opcode = OP_REGOFFSET;
784 inst->inst_basereg = frame_reg;
785 offset += sizeof (gpointer) - 1;
786 offset &= ~(sizeof (gpointer) - 1);
787 inst->inst_offset = offset;
788 offset += sizeof (gpointer);
793 for (i = 0; i < sig->param_count; ++i) {
794 inst = m->varinfo [curinst];
795 if (inst->opcode != OP_REGVAR) {
796 inst->opcode = OP_REGOFFSET;
797 inst->inst_basereg = frame_reg;
798 size = mono_type_size (sig->params [i], &align);
800 offset &= ~(align - 1);
801 inst->inst_offset = offset;
807 /* align the offset to 16 bytes */
812 m->stack_offset = offset;
816 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
817 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
821 * take the arguments and generate the arch-specific
822 * instructions to properly call the function in call.
823 * This includes pushing, moving arguments to the right register
825 * Issue: who does the spilling if needed, and when?
828 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
830 MonoMethodSignature *sig;
836 sig = call->signature;
837 n = sig->param_count + sig->hasthis;
839 cinfo = calculate_sizes (sig, sig->pinvoke);
841 for (i = 0; i < n; ++i) {
842 ainfo = cinfo->args + i;
843 if (is_virtual && i == 0) {
844 /* the argument will be attached to the call instrucion */
847 MONO_INST_NEW (cfg, arg, OP_OUTARG);
849 arg->cil_code = in->cil_code;
851 arg->type = in->type;
852 /* prepend, we'll need to reverse them later */
853 arg->next = call->out_args;
854 call->out_args = arg;
855 if (ainfo->regtype == 0) {
856 arg->unused = ainfo->reg;
857 call->used_iregs |= 1 << ainfo->reg;
858 } else if (ainfo->regtype == 1) {
859 g_assert_not_reached ();
860 } else if (ainfo->regtype == 2) {
861 arg->opcode = OP_OUTARG_R8;
862 arg->unused = ainfo->reg;
863 call->used_fregs |= 1 << ainfo->reg;
865 g_assert_not_reached ();
870 * Reverse the call->out_args list.
873 MonoInst *prev = NULL, *list = call->out_args, *next;
881 call->stack_usage = cinfo->stack_usage;
882 cfg->param_area = MAX (cfg->param_area, cinfo->stack_usage);
883 cfg->flags |= MONO_CFG_HAS_CALLS;
885 * should set more info in call, such as the stack space
886 * used by the args that needs to be added back to esp
894 * Allow tracing to work with this interface (with an optional argument)
898 * This may be needed on some archs or for debugging support.
901 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
903 /* no stack room needed now (may be needed for FASTCALL-trace support) */
905 /* split prolog-epilog requirements? */
906 *code = 50; /* max bytes needed: check this number */
910 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
914 /* if some args are passed in registers, we need to save them here */
915 x86_push_reg (code, X86_EBP);
916 x86_push_imm (code, cfg->method);
917 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
918 x86_call_code (code, 0);
919 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
933 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
936 int arg_size = 0, save_mode = SAVE_NONE;
937 MonoMethod *method = cfg->method;
938 int rtype = method->signature->ret->type;
943 /* special case string .ctor icall */
944 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
945 save_mode = SAVE_ONE;
947 save_mode = SAVE_NONE;
951 save_mode = SAVE_TWO;
957 case MONO_TYPE_VALUETYPE:
958 if (method->signature->ret->data.klass->enumtype) {
959 rtype = method->signature->ret->data.klass->enum_basetype->type;
962 save_mode = SAVE_STRUCT;
965 save_mode = SAVE_ONE;
971 //x86_push_reg (code, X86_EDX);
972 //x86_push_reg (code, X86_EAX);
973 if (enable_arguments) {
974 //x86_push_reg (code, X86_EDX);
975 //x86_push_reg (code, X86_EAX);
980 //x86_push_reg (code, X86_EAX);
981 if (enable_arguments) {
982 //x86_push_reg (code, X86_EAX);
987 //x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
988 ///x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
989 if (enable_arguments) {
990 //x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
991 //x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
996 if (enable_arguments) {
997 //x86_push_membase (code, X86_EBP, 8);
1006 /*x86_push_imm (code, method);
1007 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1008 x86_call_code (code, 0);
1009 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
1012 switch (save_mode) {
1014 //x86_pop_reg (code, X86_EAX);
1015 //x86_pop_reg (code, X86_EDX);
1018 //x86_pop_reg (code, X86_EAX);
1021 //x86_fld_membase (code, X86_ESP, 0, TRUE);
1022 //x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1032 #define EMIT_COND_BRANCH(ins,cond) \
1033 if (ins->flags & MONO_INST_BRLABEL) { \
1034 if (ins->inst_i0->inst_c0) { \
1035 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], (code - cfg->native_code + ins->inst_i0->inst_c0) & 0xffff); \
1037 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1038 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], 0); \
1041 if (0 && ins->inst_true_bb->native_offset) { \
1042 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffff); \
1044 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1045 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], 0); \
1049 /* emit an exception if condition is fail */
1050 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
1052 mono_add_patch_info (cfg, code - cfg->native_code, \
1053 MONO_PATCH_INFO_EXC, exc_name); \
1054 x86_branch32 (code, cond, 0, signed); \
1057 #define EMIT_FPCOMPARE(code) do { \
1058 x86_fcompp (code); \
1059 x86_fnstsw (code); \
1060 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500); \
1064 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1066 MonoInst *ins, *last_ins = NULL;
1071 switch (ins->opcode) {
1073 /* remove unnecessary multiplication with 1 */
1074 if (ins->inst_imm == 1) {
1075 if (ins->dreg != ins->sreg1) {
1076 ins->opcode = OP_MOVE;
1078 last_ins->next = ins->next;
1084 case OP_LOAD_MEMBASE:
1085 case OP_LOADI4_MEMBASE:
1087 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1088 * OP_LOAD_MEMBASE offset(basereg), reg
1090 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1091 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1092 ins->inst_basereg == last_ins->inst_destbasereg &&
1093 ins->inst_offset == last_ins->inst_offset) {
1094 if (ins->dreg == last_ins->sreg1) {
1095 last_ins->next = ins->next;
1099 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1100 ins->opcode = OP_MOVE;
1101 ins->sreg1 = last_ins->sreg1;
1105 * Note: reg1 must be different from the basereg in the second load
1106 * OP_LOAD_MEMBASE offset(basereg), reg1
1107 * OP_LOAD_MEMBASE offset(basereg), reg2
1109 * OP_LOAD_MEMBASE offset(basereg), reg1
1110 * OP_MOVE reg1, reg2
1112 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1113 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1114 ins->inst_basereg != last_ins->dreg &&
1115 ins->inst_basereg == last_ins->inst_basereg &&
1116 ins->inst_offset == last_ins->inst_offset) {
1118 if (ins->dreg == last_ins->dreg) {
1119 last_ins->next = ins->next;
1123 ins->opcode = OP_MOVE;
1124 ins->sreg1 = last_ins->dreg;
1127 //g_assert_not_reached ();
1131 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1132 * OP_LOAD_MEMBASE offset(basereg), reg
1134 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1135 * OP_ICONST reg, imm
1137 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1138 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1139 ins->inst_basereg == last_ins->inst_destbasereg &&
1140 ins->inst_offset == last_ins->inst_offset) {
1141 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1142 ins->opcode = OP_ICONST;
1143 ins->inst_c0 = last_ins->inst_imm;
1144 g_assert_not_reached (); // check this rule
1148 case OP_LOADU1_MEMBASE:
1149 case OP_LOADI1_MEMBASE:
1150 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1151 ins->inst_basereg == last_ins->inst_destbasereg &&
1152 ins->inst_offset == last_ins->inst_offset) {
1153 if (ins->dreg == last_ins->sreg1) {
1154 last_ins->next = ins->next;
1158 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1159 ins->opcode = OP_MOVE;
1160 ins->sreg1 = last_ins->sreg1;
1164 case OP_LOADU2_MEMBASE:
1165 case OP_LOADI2_MEMBASE:
1166 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1167 ins->inst_basereg == last_ins->inst_destbasereg &&
1168 ins->inst_offset == last_ins->inst_offset) {
1169 if (ins->dreg == last_ins->sreg1) {
1170 last_ins->next = ins->next;
1174 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1175 ins->opcode = OP_MOVE;
1176 ins->sreg1 = last_ins->sreg1;
1186 if (ins->dreg == ins->sreg1) {
1188 last_ins->next = ins->next;
1193 * OP_MOVE sreg, dreg
1194 * OP_MOVE dreg, sreg
1196 if (last_ins && last_ins->opcode == OP_MOVE &&
1197 ins->sreg1 == last_ins->dreg &&
1198 ins->dreg == last_ins->sreg1) {
1199 last_ins->next = ins->next;
1208 bb->last_ins = last_ins;
1212 * the branch_b0_table should maintain the order of these
1226 branch_b0_table [] = {
1241 branch_b1_table [] = {
1256 #define DEBUG(a) if (cfg->verbose_level > 1) a
1258 #define reg_is_freeable(r) ((r) >= 3 && (r) <= 10)
1267 static const char*const * ins_spec = ppcg4;
1270 print_ins (int i, MonoInst *ins)
1272 const char *spec = ins_spec [ins->opcode];
1273 g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1274 if (spec [MONO_INST_DEST]) {
1275 if (ins->dreg >= MONO_MAX_IREGS)
1276 g_print (" R%d <-", ins->dreg);
1278 g_print (" %s <-", mono_arch_regname (ins->dreg));
1280 if (spec [MONO_INST_SRC1]) {
1281 if (ins->sreg1 >= MONO_MAX_IREGS)
1282 g_print (" R%d", ins->sreg1);
1284 g_print (" %s", mono_arch_regname (ins->sreg1));
1286 if (spec [MONO_INST_SRC2]) {
1287 if (ins->sreg2 >= MONO_MAX_IREGS)
1288 g_print (" R%d", ins->sreg2);
1290 g_print (" %s", mono_arch_regname (ins->sreg2));
1292 if (spec [MONO_INST_CLOB])
1293 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1298 print_regtrack (RegTrack *t, int num)
1304 for (i = 0; i < num; ++i) {
1307 if (i >= MONO_MAX_IREGS) {
1308 g_snprintf (buf, sizeof(buf), "R%d", i);
1311 r = mono_arch_regname (i);
1312 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1316 typedef struct InstList InstList;
1324 static inline InstList*
1325 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1327 InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1337 * Force the spilling of the variable in the symbolic register 'reg'.
1340 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1345 sel = cfg->rs->iassign [reg];
1346 /*i = cfg->rs->isymbolic [sel];
1347 g_assert (i == reg);*/
1349 spill = ++cfg->spill_count;
1350 cfg->rs->iassign [i] = -spill - 1;
1351 mono_regstate_free_int (cfg->rs, sel);
1352 /* we need to create a spill var and insert a load to sel after the current instruction */
1353 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1355 load->inst_basereg = cfg->frame_reg;
1356 load->inst_offset = mono_spillvar_offset (cfg, spill);
1358 while (ins->next != item->prev->data)
1361 load->next = ins->next;
1363 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1364 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1365 g_assert (i == sel);
1371 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1376 DEBUG (g_print ("start regmask to assign R%d: 0x%08x (R%d <- R%d R%d)\n", reg, regmask, ins->dreg, ins->sreg1, ins->sreg2));
1377 /* exclude the registers in the current instruction */
1378 if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1379 if (ins->sreg1 >= MONO_MAX_IREGS)
1380 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1382 regmask &= ~ (1 << ins->sreg1);
1383 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1385 if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1386 if (ins->sreg2 >= MONO_MAX_IREGS)
1387 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1389 regmask &= ~ (1 << ins->sreg2);
1390 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1392 if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1393 regmask &= ~ (1 << ins->dreg);
1394 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1397 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1398 g_assert (regmask); /* need at least a register we can free */
1400 /* we should track prev_use and spill the register that's farther */
1401 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1402 if (regmask & (1 << i)) {
1404 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1408 i = cfg->rs->isymbolic [sel];
1409 spill = ++cfg->spill_count;
1410 cfg->rs->iassign [i] = -spill - 1;
1411 mono_regstate_free_int (cfg->rs, sel);
1412 /* we need to create a spill var and insert a load to sel after the current instruction */
1413 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1415 load->inst_basereg = cfg->frame_reg;
1416 load->inst_offset = mono_spillvar_offset (cfg, spill);
1418 while (ins->next != item->prev->data)
1421 load->next = ins->next;
1423 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1424 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1425 g_assert (i == sel);
1431 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1434 MONO_INST_NEW (cfg, copy, OP_MOVE);
1438 copy->next = ins->next;
1441 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1446 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1449 MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1451 store->inst_destbasereg = ppc_r1;
1452 store->inst_offset = mono_spillvar_offset (cfg, spill);
1454 store->next = ins->next;
1457 DEBUG (g_print ("SPILLED STORE (%d at 0x%08x(%%sp)) R%d (from %s)\n", spill, store->inst_offset, prev_reg, mono_arch_regname (reg)));
1462 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1465 g_assert (item->next);
1466 prev = item->next->data;
1468 while (prev->next != ins)
1470 to_insert->next = ins;
1471 prev->next = to_insert;
1473 * needed otherwise in the next instruction we can add an ins to the
1474 * end and that would get past this instruction.
1476 item->data = to_insert;
1480 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1482 int val = cfg->rs->iassign [sym_reg];
1486 /* the register gets spilled after this inst */
1489 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1491 val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1492 cfg->rs->iassign [sym_reg] = val;
1493 /* add option to store before the instruction for src registers */
1495 create_spilled_store (cfg, spill, val, sym_reg, ins);
1497 cfg->rs->isymbolic [val] = sym_reg;
1501 /* use ppc_r3-ppc_310 as temp registers */
1502 #define PPC_CALLER_REGS (0xf<<3)
1505 * Local register allocation.
1506 * We first scan the list of instructions and we save the liveness info of
1507 * each register (when the register is first used, when it's value is set etc.).
1508 * We also reverse the list of instructions (in the InstList list) because assigning
1509 * registers backwards allows for more tricks to be used.
1512 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1515 MonoRegState *rs = cfg->rs;
1516 int i, val, fpcount;
1517 RegTrack *reginfo, *reginfof;
1518 RegTrack *reginfo1, *reginfo2, *reginfod;
1519 InstList *tmp, *reversed = NULL;
1521 guint32 src1_mask, src2_mask, dest_mask;
1525 rs->next_vireg = bb->max_ireg;
1526 rs->next_vfreg = bb->max_freg;
1527 mono_regstate_assign (rs);
1528 reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
1529 reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
1530 rs->ifree_mask = PPC_CALLER_REGS;
1534 fpcount = 0; /* FIXME: track fp stack utilization */
1535 DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1536 /* forward pass on the instructions to collect register liveness info */
1538 spec = ins_spec [ins->opcode];
1539 DEBUG (print_ins (i, ins));
1540 if (spec [MONO_INST_SRC1]) {
1541 if (spec [MONO_INST_SRC1] == 'f')
1542 reginfo1 = reginfof;
1545 reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1546 reginfo1 [ins->sreg1].last_use = i;
1550 if (spec [MONO_INST_SRC2]) {
1551 if (spec [MONO_INST_SRC2] == 'f')
1552 reginfo2 = reginfof;
1555 reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1556 reginfo2 [ins->sreg2].last_use = i;
1560 if (spec [MONO_INST_DEST]) {
1561 if (spec [MONO_INST_DEST] == 'f')
1562 reginfod = reginfof;
1565 if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1566 reginfod [ins->dreg].killed_in = i;
1567 reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1568 reginfod [ins->dreg].last_use = i;
1569 if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1570 reginfod [ins->dreg].born_in = i;
1571 if (spec [MONO_INST_DEST] == 'l') {
1572 /* result in eax:edx, the virtual register is allocated sequentially */
1573 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1574 reginfod [ins->dreg + 1].last_use = i;
1575 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1576 reginfod [ins->dreg + 1].born_in = i;
1581 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1586 DEBUG (print_regtrack (reginfo, rs->next_vireg));
1587 DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1590 int prev_dreg, prev_sreg1, prev_sreg2;
1591 dest_mask = src1_mask = src2_mask = PPC_CALLER_REGS;
1594 spec = ins_spec [ins->opcode];
1595 DEBUG (g_print ("processing:"));
1596 DEBUG (print_ins (i, ins));
1597 /* update for use with FP regs... */
1598 if (spec [MONO_INST_DEST] != 'f' && ins->dreg >= MONO_MAX_IREGS) {
1599 val = rs->iassign [ins->dreg];
1600 prev_dreg = ins->dreg;
1604 /* the register gets spilled after this inst */
1607 val = mono_regstate_alloc_int (rs, dest_mask);
1609 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1610 rs->iassign [ins->dreg] = val;
1612 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1614 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1615 rs->isymbolic [val] = prev_dreg;
1617 if (spec [MONO_INST_DEST] == 'l') {
1618 int hreg = prev_dreg + 1;
1619 val = rs->iassign [hreg];
1623 /* the register gets spilled after this inst */
1626 val = mono_regstate_alloc_int (rs, dest_mask);
1628 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1629 rs->iassign [hreg] = val;
1631 create_spilled_store (cfg, spill, val, hreg, ins);
1633 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
1634 rs->isymbolic [val] = hreg;
1635 /* FIXME:? ins->dreg = val; */
1636 if (ins->dreg == ppc_r4) {
1638 create_copy_ins (cfg, val, ppc_r3, ins);
1639 } else if (ins->dreg == ppc_r3) {
1640 if (val == ppc_r4) {
1642 create_copy_ins (cfg, ppc_r0, ppc_r3, ins);
1643 create_copy_ins (cfg, ppc_r3, ppc_r4, ins);
1644 create_copy_ins (cfg, ppc_r4, ppc_r0, ins);
1646 /* two forced copies */
1647 create_copy_ins (cfg, val, ppc_r3, ins);
1648 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1651 if (val == ppc_r3) {
1652 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1654 /* two forced copies */
1655 create_copy_ins (cfg, val, ppc_r3, ins);
1656 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1659 if (reg_is_freeable (val) && hreg >= 0 && reginfo [hreg].born_in >= i) {
1660 DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
1661 mono_regstate_free_int (rs, val);
1667 if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && reginfo [prev_dreg].born_in >= i) {
1668 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1669 mono_regstate_free_int (rs, ins->dreg);
1671 if (spec [MONO_INST_SRC1] != 'f' && ins->sreg1 >= MONO_MAX_IREGS) {
1672 val = rs->iassign [ins->sreg1];
1673 prev_sreg1 = ins->sreg1;
1677 /* the register gets spilled after this inst */
1680 if (0 && ins->opcode == OP_MOVE) {
1682 * small optimization: the dest register is already allocated
1683 * but the src one is not: we can simply assign the same register
1684 * here and peephole will get rid of the instruction later.
1685 * This optimization may interfere with the clobbering handling:
1686 * it removes a mov operation that will be added again to handle clobbering.
1687 * There are also some other issues that should with make testjit.
1689 mono_regstate_alloc_int (rs, 1 << ins->dreg);
1690 val = rs->iassign [ins->sreg1] = ins->dreg;
1691 //g_assert (val >= 0);
1692 DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1694 //g_assert (val == -1); /* source cannot be spilled */
1695 val = mono_regstate_alloc_int (rs, src1_mask);
1697 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1698 rs->iassign [ins->sreg1] = val;
1699 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1702 MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
1703 insert_before_ins (ins, tmp, store);
1706 rs->isymbolic [val] = prev_sreg1;
1711 if (spec [MONO_INST_SRC2] != 'f' && ins->sreg2 >= MONO_MAX_IREGS) {
1712 val = rs->iassign [ins->sreg2];
1713 prev_sreg2 = ins->sreg2;
1717 /* the register gets spilled after this inst */
1720 val = mono_regstate_alloc_int (rs, src2_mask);
1722 val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1723 rs->iassign [ins->sreg2] = val;
1724 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1726 create_spilled_store (cfg, spill, val, prev_sreg2, ins);
1728 rs->isymbolic [val] = prev_sreg2;
1734 if (spec [MONO_INST_CLOB] == 'c') {
1736 guint32 clob_mask = PPC_CALLER_REGS;
1737 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1739 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
1740 //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
1744 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
1745 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
1746 mono_regstate_free_int (rs, ins->sreg1);
1748 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
1749 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
1750 mono_regstate_free_int (rs, ins->sreg2);
1753 //DEBUG (print_ins (i, ins));
1759 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
1764 static unsigned char*
1765 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
1768 int sreg = tree->sreg1;
1769 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
1770 if (tree->flags & MONO_INST_INIT) {
1772 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
1773 x86_push_reg (code, X86_EAX);
1776 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
1777 x86_push_reg (code, X86_ECX);
1780 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
1781 x86_push_reg (code, X86_EDI);
1785 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
1786 if (sreg != X86_ECX)
1787 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
1788 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
1790 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
1792 x86_prefix (code, X86_REP_PREFIX);
1795 if (tree->dreg != X86_EDI && sreg != X86_EDI)
1796 x86_pop_reg (code, X86_EDI);
1797 if (tree->dreg != X86_ECX && sreg != X86_ECX)
1798 x86_pop_reg (code, X86_ECX);
1799 if (tree->dreg != X86_EAX && sreg != X86_EAX)
1800 x86_pop_reg (code, X86_EAX);
1807 ppc_patch (guchar *code, guchar *target)
1809 guint32 ins = *(guint32*)code;
1810 guint32 prim = ins >> 26;
1812 // g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
1816 guint32 li = (guint32)target;
1817 ins = prim << 26 | (ins & 3);
1819 // FIXME: assert the top bits of li are 0
1821 gint diff = target - code;
1822 ins = prim << 26 | (ins & 3);
1824 diff &= ~(63 << 26);
1827 *(guint32*)code = ins;
1828 } else if (prim == 16) {
1831 guint32 li = (guint32)target;
1832 ins = (ins & 0xffff0000) | (ins & 3);
1835 // FIXME: assert the top bits of li are 0
1837 gint diff = target - code;
1838 ins = (ins & 0xffff0000) | (ins & 3);
1842 *(guint32*)code = ins;
1844 g_assert_not_reached ();
1846 // g_print ("patched with 0x%08x\n", ins);
1850 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
1855 guint8 *code = cfg->native_code + cfg->code_len;
1856 MonoInst *last_ins = NULL;
1857 guint last_offset = 0;
1860 if (cfg->opt & MONO_OPT_PEEPHOLE)
1861 peephole_pass (cfg, bb);
1865 * various stratgies to align BBs. Using real loop detection or simply
1866 * aligning every block leads to more consistent benchmark results,
1867 * but usually slows down the code
1868 * we should do the alignment outside this function or we should adjust
1869 * bb->native offset as well or the code is effectively slowed down!
1871 /* align all blocks */
1872 // if ((pad = (cfg->code_len & (align - 1)))) {
1873 /* poor man loop start detection */
1874 // if (bb->code && bb->in_count && bb->in_bb [0]->cil_code > bb->cil_code && (pad = (cfg->code_len & (align - 1)))) {
1875 /* consider real loop detection and nesting level */
1876 // if (bb->loop_blocks && bb->nesting < 3 && (pad = (cfg->code_len & (align - 1)))) {
1877 /* consider real loop detection */
1878 if (/*bb->loop_blocks &&*/ (pad = (cfg->code_len & (align - 1)))) {
1880 x86_padding (code, pad);
1881 cfg->code_len += pad;
1882 bb->native_offset = cfg->code_len;
1886 if (cfg->verbose_level > 2)
1887 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
1889 cpos = bb->max_offset;
1891 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
1892 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
1893 //g_assert (!mono_compile_aot);
1896 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
1897 /* this is not thread save, but good enough */
1898 /* fixme: howto handle overflows? */
1899 //x86_inc_mem (code, &cov->data [bb->dfn].count);
1904 offset = code - cfg->native_code;
1906 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
1908 if (offset > (cfg->code_size - max_len - 16)) {
1909 cfg->code_size *= 2;
1910 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
1911 code = cfg->native_code + offset;
1913 // if (ins->cil_code)
1914 // g_print ("cil code\n");
1916 switch (ins->opcode) {
1917 case OP_STOREI1_MEMBASE_IMM:
1918 ppc_li (code, ppc_r11, ins->inst_imm);
1919 g_assert (ppc_is_imm16 (ins->inst_offset));
1920 ppc_stb (code, ppc_r11, ins->inst_offset, ins->inst_destbasereg);
1922 case OP_STOREI2_MEMBASE_IMM:
1923 ppc_li (code, ppc_r11, ins->inst_imm);
1924 g_assert (ppc_is_imm16 (ins->inst_offset));
1925 ppc_sth (code, ppc_r11, ins->inst_offset, ins->inst_destbasereg);
1927 case OP_STORE_MEMBASE_IMM:
1928 case OP_STOREI4_MEMBASE_IMM:
1929 ppc_load (code, ppc_r11, ins->inst_imm);
1930 g_assert (ppc_is_imm16 (ins->inst_offset));
1931 ppc_stw (code, ppc_r11, ins->inst_offset, ins->inst_destbasereg);
1933 case OP_STOREI1_MEMBASE_REG:
1934 g_assert (ppc_is_imm16 (ins->inst_offset));
1935 ppc_stb (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
1937 case OP_STOREI2_MEMBASE_REG:
1938 g_assert (ppc_is_imm16 (ins->inst_offset));
1939 ppc_sth (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
1941 case OP_STORE_MEMBASE_REG:
1942 case OP_STOREI4_MEMBASE_REG:
1943 g_assert (ppc_is_imm16 (ins->inst_offset));
1944 ppc_stw (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
1949 g_assert_not_reached ();
1950 //x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
1953 g_assert_not_reached ();
1954 //x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
1955 //x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
1957 case OP_LOAD_MEMBASE:
1958 case OP_LOADI4_MEMBASE:
1959 case OP_LOADU4_MEMBASE:
1960 if (ppc_is_imm16 (ins->inst_offset)) {
1961 ppc_lwz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
1963 ppc_load (code, ppc_r11, ins->inst_offset);
1964 ppc_lwzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
1967 case OP_LOADU1_MEMBASE:
1968 g_assert (ppc_is_imm16 (ins->inst_offset));
1969 ppc_lbz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
1971 case OP_LOADI1_MEMBASE:
1972 g_assert (ppc_is_imm16 (ins->inst_offset));
1973 // FIXME: sign extend
1974 ppc_lbz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
1976 case OP_LOADU2_MEMBASE:
1977 g_assert (ppc_is_imm16 (ins->inst_offset));
1978 ppc_lhz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
1980 case OP_LOADI2_MEMBASE:
1981 g_assert (ppc_is_imm16 (ins->inst_offset));
1982 ppc_lha (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
1985 ppc_extsb (code, ins->dreg, ins->sreg1);
1988 ppc_extsh (code, ins->dreg, ins->sreg1);
1991 ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 24, 31);
1994 ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 16, 31);
1997 if (ins->next && ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN)
1998 ppc_cmpl (code, 0, 0, ins->sreg1, ins->sreg2);
2000 ppc_cmp (code, 0, 0, ins->sreg1, ins->sreg2);
2002 case OP_COMPARE_IMM:
2003 if (ppc_is_imm16 (ins->inst_imm)) {
2004 if (ins->next && ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN)
2005 ppc_cmpli (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2007 ppc_cmpi (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2009 ppc_load (code, ppc_r11, ins->inst_imm);
2010 if (ins->next && ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN)
2011 ppc_cmp (code, 0, 0, ins->sreg1, ppc_r11);
2013 ppc_cmpl (code, 0, 0, ins->sreg1, ppc_r11);
2016 case OP_X86_TEST_NULL:
2017 ppc_cmpi (code, 0, 0, ins->sreg1, 0);
2023 ppc_addc (code, ins->dreg, ins->sreg1, ins->sreg2);
2026 ppc_add (code, ins->dreg, ins->sreg1, ins->sreg2);
2029 ppc_adde (code, ins->dreg, ins->sreg1, ins->sreg2);
2032 if (ppc_is_imm16 (ins->inst_imm)) {
2033 ppc_addi (code, ins->dreg, ins->sreg1, ins->inst_imm);
2035 ppc_load (code, ppc_r11, ins->inst_imm);
2036 ppc_add (code, ins->dreg, ins->sreg1, ppc_r11);
2040 ppc_load (code, ppc_r11, ins->inst_imm);
2041 ppc_adde (code, ins->dreg, ins->sreg1, ppc_r11);
2044 ppc_subfc (code, ins->dreg, ins->sreg2, ins->sreg1);
2047 ppc_subf (code, ins->dreg, ins->sreg2, ins->sreg1);
2050 ppc_subfe (code, ins->dreg, ins->sreg2, ins->sreg1);
2053 // we add the negated value
2054 g_assert (ppc_is_imm16 (-ins->inst_imm));
2055 ppc_addi (code, ins->dreg, ins->sreg1, -ins->inst_imm);
2058 ppc_load (code, ppc_r11, ins->inst_imm);
2059 ppc_subfe (code, ins->dreg, ins->sreg2, ppc_r11);
2062 g_assert (ppc_is_imm16 (ins->inst_imm));
2063 ppc_subfic (code, ins->dreg, ins->sreg1, ins->inst_imm);
2066 ppc_subfze (code, ins->dreg, ins->sreg1);
2069 /* FIXME: the ppc macros as inconsistent here: put dest as the first arg! */
2070 ppc_and (code, ins->sreg1, ins->dreg, ins->sreg2);
2073 if (!(ins->inst_imm & 0xffff0000)) {
2074 ppc_andid (code, ins->sreg1, ins->dreg, ins->inst_imm);
2075 } else if (!(ins->inst_imm & 0xffff)) {
2076 ppc_andisd (code, ins->sreg1, ins->dreg, ((guint32)ins->inst_imm >> 16));
2078 ppc_load (code, ppc_r11, ins->inst_imm);
2079 ppc_and (code, ins->sreg1, ins->dreg, ins->sreg2);
2083 ppc_divw (code, ins->dreg, ins->sreg1, ins->sreg2);
2086 ppc_divwu (code, ins->dreg, ins->sreg1, ins->sreg2);
2089 ppc_load (code, ppc_r11, ins->inst_imm);
2090 ppc_divw (code, ins->dreg, ins->sreg1, ppc_r11);
2093 ppc_divw (code, ppc_r11, ins->sreg1, ins->sreg2);
2094 ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2095 ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2098 ppc_divwu (code, ppc_r11, ins->sreg1, ins->sreg2);
2099 ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2100 ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2103 ppc_load (code, ppc_r11, ins->inst_imm);
2104 ppc_divw (code, ins->dreg, ins->sreg1, ppc_r11);
2105 ppc_mullw (code, ins->dreg, ins->dreg, ppc_r11);
2106 ppc_subf (code, ins->dreg, ins->dreg, ins->sreg1);
2109 ppc_or (code, ins->dreg, ins->sreg1, ins->sreg2);
2112 if (!(ins->inst_imm & 0xffff0000)) {
2113 ppc_ori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2114 } else if (!(ins->inst_imm & 0xffff)) {
2115 ppc_oris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2117 ppc_load (code, ppc_r11, ins->inst_imm);
2118 ppc_or (code, ins->sreg1, ins->dreg, ins->sreg2);
2122 ppc_xor (code, ins->dreg, ins->sreg1, ins->sreg2);
2125 if (!(ins->inst_imm & 0xffff0000)) {
2126 ppc_xori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2127 } else if (!(ins->inst_imm & 0xffff)) {
2128 ppc_xoris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2130 ppc_load (code, ppc_r11, ins->inst_imm);
2131 ppc_xor (code, ins->sreg1, ins->dreg, ins->sreg2);
2135 ppc_slw (code, ins->sreg1, ins->dreg, ins->sreg2);
2138 ppc_rlwinm (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0xf), 0, (31 - (ins->inst_imm & 0xf)));
2139 //ppc_load (code, ppc_r11, ins->inst_imm);
2140 //ppc_slw (code, ins->sreg1, ins->dreg, ppc_r11);
2143 ppc_sraw (code, ins->dreg, ins->sreg1, ins->sreg2);
2146 // there is also ppc_srawi
2147 //ppc_load (code, ppc_r11, ins->inst_imm);
2148 //ppc_sraw (code, ins->dreg, ins->sreg1, ppc_r11);
2149 ppc_srawi (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
2152 ppc_load (code, ppc_r11, ins->inst_imm);
2153 ppc_srw (code, ins->dreg, ins->sreg1, ppc_r11);
2154 //ppc_rlwinm (code, ins->dreg, ins->sreg1, (32 - (ins->inst_imm & 0xf)), (ins->inst_imm & 0xf), 31);
2157 ppc_srw (code, ins->dreg, ins->sreg1, ins->sreg2);
2160 ppc_not (code, ins->dreg, ins->sreg1);
2163 ppc_neg (code, ins->dreg, ins->sreg1);
2166 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2169 ppc_load (code, ppc_r11, ins->inst_imm);
2170 ppc_mullw (code, ins->dreg, ins->sreg1, ppc_r11);
2173 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2174 //g_assert_not_reached ();
2175 //x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2176 //EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2178 case CEE_MUL_OVF_UN:
2179 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2180 //FIXME: g_assert_not_reached ();
2184 ppc_load (code, ins->dreg, ins->inst_c0);
2187 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_CLASS, (gpointer)ins->inst_c0);
2188 ppc_load (code, ins->dreg, 0xff00ff00);
2191 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_IMAGE, (gpointer)ins->inst_c0);
2192 ppc_load (code, ins->dreg, 0xff00ff00);
2198 ppc_mr (code, ins->dreg, ins->sreg1);
2201 g_assert_not_reached ();
2204 /* ensure ins->sreg1 is not NULL */
2205 g_assert_not_reached ();
2206 //x86_alu_membase_imm (code, X86_CMP, ins->sreg1, 0, 0);
2213 call = (MonoCallInst*)ins;
2214 if (ins->flags & MONO_INST_HAS_METHOD)
2215 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2217 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2223 case OP_VOIDCALL_REG:
2225 ppc_mtlr (code, ins->sreg1);
2228 case OP_FCALL_MEMBASE:
2229 case OP_LCALL_MEMBASE:
2230 case OP_VCALL_MEMBASE:
2231 case OP_VOIDCALL_MEMBASE:
2232 case OP_CALL_MEMBASE:
2233 ppc_lwz (code, ppc_r0, ins->inst_offset, ins->sreg1);
2234 ppc_mtlr (code, ppc_r0);
2238 g_assert_not_reached ();
2241 /* keep alignment */
2242 #define MONO_FRAME_ALIGNMENT 32
2243 ppc_addi (code, ppc_r0, ins->sreg1, MONO_FRAME_ALIGNMENT-1);
2244 ppc_rlwinm (code, ppc_r0, ppc_r0, 0, 0, 27);
2245 ppc_lwz (code, ppc_r11, 0, ppc_sp);
2246 ppc_neg (code, ppc_r0, ppc_r0);
2247 ppc_stwux (code, ppc_sp, ppc_r0, ppc_sp);
2248 ppc_mr (code, ins->dreg, ppc_sp);
2249 g_assert_not_reached ();
2255 ppc_mr (code, ppc_r3, ins->sreg1);
2256 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2257 (gpointer)"throw_exception");
2262 if (ins->sreg1 != ppc_r3)
2263 ppc_mr (code, ppc_r3, ins->sreg1);
2266 case CEE_ENDFINALLY:
2269 case OP_CALL_HANDLER:
2270 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2274 ins->inst_c0 = code - cfg->native_code;
2277 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2278 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2280 if (ins->flags & MONO_INST_BRLABEL) {
2281 /*if (ins->inst_i0->inst_c0) {
2283 //x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2285 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2289 /*if (ins->inst_target_bb->native_offset) {
2291 //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
2293 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2299 ppc_mtctr (code, ins->sreg1);
2300 ppc_bcctr (code, 20, 0);
2303 ppc_li (code, ins->dreg, 0);
2304 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
2305 ppc_li (code, ins->dreg, 1);
2309 ppc_li (code, ins->dreg, 1);
2310 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2311 ppc_li (code, ins->dreg, 0);
2315 ppc_li (code, ins->dreg, 1);
2316 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2317 ppc_li (code, ins->dreg, 0);
2319 case OP_COND_EXC_EQ:
2320 case OP_COND_EXC_NE_UN:
2321 case OP_COND_EXC_LT:
2322 case OP_COND_EXC_LT_UN:
2323 case OP_COND_EXC_GT:
2324 case OP_COND_EXC_GT_UN:
2325 case OP_COND_EXC_GE:
2326 case OP_COND_EXC_GE_UN:
2327 case OP_COND_EXC_LE:
2328 case OP_COND_EXC_LE_UN:
2329 case OP_COND_EXC_OV:
2330 case OP_COND_EXC_NO:
2332 case OP_COND_EXC_NC:
2333 //EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
2334 // (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
2346 EMIT_COND_BRANCH (ins, ins->opcode - CEE_BEQ);
2349 /* floating point opcodes */
2351 ppc_load (code, ppc_r11, ins->inst_p0);
2352 ppc_lfd (code, ins->dreg, 0, ppc_r11);
2355 ppc_load (code, ppc_r11, ins->inst_p0);
2356 ppc_lfs (code, ins->dreg, 0, ppc_r11);
2358 case OP_STORER8_MEMBASE_REG:
2359 ppc_stfd (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2361 case OP_LOADR8_MEMBASE:
2362 ppc_lfd (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2364 case OP_STORER4_MEMBASE_REG:
2365 ppc_stfs (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2367 case OP_LOADR4_MEMBASE:
2368 ppc_lfs (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2370 case CEE_CONV_R4: /* FIXME: change precision */
2372 g_assert_not_reached ();
2373 x86_push_reg (code, ins->sreg1);
2374 x86_fild_membase (code, X86_ESP, 0, FALSE);
2375 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2377 case OP_X86_FP_LOAD_I8:
2378 g_assert_not_reached ();
2379 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2381 case OP_X86_FP_LOAD_I4:
2382 g_assert_not_reached ();
2383 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2385 case OP_FCONV_TO_I1:
2386 g_assert_not_reached ();
2387 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
2389 case OP_FCONV_TO_U1:
2390 g_assert_not_reached ();
2391 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
2393 case OP_FCONV_TO_I2:
2394 g_assert_not_reached ();
2395 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
2397 case OP_FCONV_TO_U2:
2398 g_assert_not_reached ();
2399 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
2401 case OP_FCONV_TO_I4:
2403 g_assert_not_reached ();
2404 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
2406 case OP_FCONV_TO_U4:
2408 g_assert_not_reached ();
2409 code = emit_float_to_int (cfg, code, ins->dreg, 4, FALSE);
2411 case OP_FCONV_TO_I8:
2412 case OP_FCONV_TO_U8:
2413 g_assert_not_reached ();
2414 /*x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2415 x86_fnstcw_membase(code, X86_ESP, 0);
2416 x86_mov_reg_membase (code, ins->inst_dreg_low, X86_ESP, 0, 2);
2417 x86_alu_reg_imm (code, X86_OR, ins->inst_dreg_low, 0xc00);
2418 x86_mov_membase_reg (code, X86_ESP, 2, ins->inst_dreg_low, 2);
2419 x86_fldcw_membase (code, X86_ESP, 2);
2420 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2421 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2422 x86_pop_reg (code, ins->inst_dreg_low);
2423 x86_pop_reg (code, ins->inst_dreg_high);
2424 x86_fldcw_membase (code, X86_ESP, 0);
2425 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);*/
2427 case OP_LCONV_TO_R_UN: {
2429 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
2432 /* load 64bit integer to FP stack */
2433 x86_push_imm (code, 0);
2434 x86_push_reg (code, ins->sreg2);
2435 x86_push_reg (code, ins->sreg1);
2436 x86_fild_membase (code, X86_ESP, 0, TRUE);
2437 /* store as 80bit FP value */
2438 x86_fst80_membase (code, X86_ESP, 0);
2440 /* test if lreg is negative */
2441 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2442 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
2444 /* add correction constant mn */
2445 x86_fld80_mem (code, mn);
2446 x86_fld80_membase (code, X86_ESP, 0);
2447 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2448 x86_fst80_membase (code, X86_ESP, 0);
2450 x86_patch (br, code);
2452 x86_fld80_membase (code, X86_ESP, 0);
2453 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
2455 g_assert_not_reached ();
2458 case OP_LCONV_TO_OVF_I: {
2460 guint8 *br [3], *label [1];
2463 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
2465 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2467 /* If the low word top bit is set, see if we are negative */
2468 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
2469 /* We are not negative (no top bit set, check for our top word to be zero */
2470 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2471 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2474 /* throw exception */
2475 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
2476 x86_jump32 (code, 0);
2478 x86_patch (br [0], code);
2479 /* our top bit is set, check that top word is 0xfffffff */
2480 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
2482 x86_patch (br [1], code);
2483 /* nope, emit exception */
2484 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
2485 x86_patch (br [2], label [0]);
2487 if (ins->dreg != ins->sreg1)
2488 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2490 g_assert_not_reached ();
2494 ppc_fadd (code, ins->dreg, ins->sreg1, ins->sreg2);
2497 ppc_fsub (code, ins->dreg, ins->sreg1, ins->sreg2);
2500 ppc_fmul (code, ins->dreg, ins->sreg1, ins->sreg2);
2503 ppc_fdiv (code, ins->dreg, ins->sreg1, ins->sreg2);
2506 ppc_fneg (code, ins->dreg, ins->sreg1);
2509 g_assert_not_reached ();
2512 g_assert_not_reached ();
2513 /* this overwrites EAX */
2514 EMIT_FPCOMPARE(code);
2517 g_assert_not_reached ();
2518 /*if (ins->dreg != X86_EAX)
2519 x86_push_reg (code, X86_EAX);
2521 EMIT_FPCOMPARE(code);
2522 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2523 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2524 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2526 if (ins->dreg != X86_EAX)
2527 x86_pop_reg (code, X86_EAX);*/
2531 g_assert_not_reached ();
2532 /*if (ins->dreg != X86_EAX)
2533 x86_push_reg (code, X86_EAX);
2535 EMIT_FPCOMPARE(code);
2536 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2537 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2539 if (ins->dreg != X86_EAX)
2540 x86_pop_reg (code, X86_EAX);*/
2544 g_assert_not_reached ();
2545 /*if (ins->dreg != X86_EAX)
2546 x86_push_reg (code, X86_EAX);
2548 EMIT_FPCOMPARE(code);
2549 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2550 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2551 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2553 if (ins->dreg != X86_EAX)
2554 x86_pop_reg (code, X86_EAX);*/
2557 g_assert_not_reached ();
2560 g_assert_not_reached ();
2564 g_assert_not_reached ();
2568 g_assert_not_reached ();
2572 g_assert_not_reached ();
2576 g_assert_not_reached ();
2578 case CEE_CKFINITE: {
2579 g_assert_not_reached ();
2580 x86_push_reg (code, X86_EAX);
2583 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
2584 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2585 x86_pop_reg (code, X86_EAX);
2586 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
2590 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
2591 g_assert_not_reached ();
2594 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
2595 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
2596 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
2597 g_assert_not_reached ();
2603 last_offset = offset;
2608 cfg->code_len = code - cfg->native_code;
2612 mono_arch_register_lowlevel_calls (void)
2614 mono_register_jit_icall (enter_method, "mono_enter_method", NULL, TRUE);
2615 mono_register_jit_icall (leave_method, "mono_leave_method", NULL, TRUE);
2619 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji)
2621 MonoJumpInfo *patch_info;
2623 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
2624 unsigned char *ip = patch_info->ip.i + code;
2625 const unsigned char *target = NULL;
2627 switch (patch_info->type) {
2628 case MONO_PATCH_INFO_BB:
2629 target = patch_info->data.bb->native_offset + code;
2631 case MONO_PATCH_INFO_ABS:
2632 target = patch_info->data.target;
2634 case MONO_PATCH_INFO_LABEL:
2635 target = patch_info->data.inst->inst_c0 + code;
2637 case MONO_PATCH_INFO_IP:
2638 *((gpointer *)(ip)) = ip;
2640 case MONO_PATCH_INFO_INTERNAL_METHOD: {
2641 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
2643 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
2644 g_assert_not_reached ();
2646 target = mi->wrapper;
2649 case MONO_PATCH_INFO_METHOD:
2650 if (patch_info->data.method == method) {
2653 /* get the trampoline to the method from the domain */
2654 target = mono_arch_create_jit_trampoline (patch_info->data.method);
2657 case MONO_PATCH_INFO_SWITCH: {
2658 gpointer *table = (gpointer *)patch_info->data.target;
2661 // FIXME: inspect code to get the register
2662 ppc_load (ip, ppc_r11, patch_info->data.target);
2663 //*((gconstpointer *)(ip + 2)) = patch_info->data.target;
2665 for (i = 0; i < patch_info->table_size; i++) {
2666 table [i] = (int)patch_info->data.table [i] + code;
2668 /* we put into the table the absolute address, no need for ppc_patch in this case */
2671 case MONO_PATCH_INFO_METHODCONST:
2672 case MONO_PATCH_INFO_CLASS:
2673 case MONO_PATCH_INFO_IMAGE:
2674 case MONO_PATCH_INFO_FIELD:
2675 g_assert_not_reached ();
2676 *((gconstpointer *)(ip + 1)) = patch_info->data.target;
2678 case MONO_PATCH_INFO_R4:
2679 case MONO_PATCH_INFO_R8:
2680 g_assert_not_reached ();
2681 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
2684 g_assert_not_reached ();
2686 ppc_patch (ip, target);
2691 mono_arch_max_epilog_size (MonoCompile *cfg)
2693 int exc_count = 0, max_epilog_size = 16 + 20*4;
2694 MonoJumpInfo *patch_info;
2696 if (cfg->method->save_lmf)
2697 max_epilog_size += 128;
2699 if (mono_jit_trace_calls)
2700 max_epilog_size += 50;
2702 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2703 max_epilog_size += 50;
2705 /* count the number of exception infos */
2707 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2708 if (patch_info->type == MONO_PATCH_INFO_EXC)
2713 * make sure we have enough space for exceptions
2714 * 16 is the size of two push_imm instructions and a call
2716 max_epilog_size += exc_count*16;
2718 return max_epilog_size;
2722 mono_arch_emit_prolog (MonoCompile *cfg)
2724 MonoMethod *method = cfg->method;
2726 MonoMethodSignature *sig;
2728 int alloc_size, pos, max_offset, i;
2732 cfg->code_size = 256;
2733 code = cfg->native_code = g_malloc (cfg->code_size);
2735 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2736 ppc_mflr (code, ppc_r0);
2737 ppc_stw (code, ppc_r0, 8, ppc_sp);
2739 if (cfg->flags & MONO_CFG_HAS_ALLOCA) {
2740 cfg->used_int_regs |= 1 << 31;
2743 alloc_size = cfg->stack_offset;
2746 if (method->save_lmf) {
2748 pos += sizeof (MonoLMF);
2750 /* save the current IP */
2751 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
2752 x86_push_imm (code, 0);
2754 /* save all caller saved regs */
2755 x86_push_reg (code, X86_EBX);
2756 x86_push_reg (code, X86_EDI);
2757 x86_push_reg (code, X86_ESI);
2758 x86_push_reg (code, X86_EBP);
2760 /* save method info */
2761 x86_push_imm (code, method);
2763 /* get the address of lmf for the current thread */
2764 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2765 (gpointer)"get_lmf_addr");
2766 x86_call_code (code, 0);
2769 x86_push_reg (code, X86_EAX);
2770 /* push *lfm (previous_lmf) */
2771 x86_push_membase (code, X86_EAX, 0);
2773 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
2777 for (i = 13; i < 32; ++i) {
2778 if (cfg->used_int_regs & (1 << i)) {
2780 ppc_stw (code, i, -pos, ppc_sp);
2786 // align to 16 bytes
2787 if (alloc_size & (16 - 1))
2788 alloc_size += 16 - (alloc_size & (16 - 1));
2790 cfg->stack_usage = alloc_size;
2792 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
2793 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
2794 ppc_mr (code, ppc_r31, ppc_sp);
2796 /* compute max_offset in order to use short forward jumps */
2798 if (cfg->opt & MONO_OPT_BRANCH) {
2799 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2800 MonoInst *ins = bb->code;
2801 bb->max_offset = max_offset;
2803 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
2807 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2813 if (mono_jit_trace_calls)
2814 code = mono_arch_instrument_prolog (cfg, enter_method, code, TRUE);
2816 /* load arguments allocated to register from the stack */
2817 sig = method->signature;
2820 cinfo = calculate_sizes (sig, sig->pinvoke);
2822 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2823 ArgInfo *ainfo = cinfo->args + i;
2824 inst = cfg->varinfo [pos];
2826 if (inst->opcode == OP_REGVAR) {
2827 g_assert (!ainfo->regtype); // fine for now
2828 ppc_mr (code, inst->dreg, ainfo->reg);
2829 if (cfg->verbose_level > 2)
2830 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
2832 /* the argument should be put on the stack: FIXME handle size != word */
2833 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
2838 cfg->code_len = code - cfg->native_code;
2844 mono_arch_emit_epilog (MonoCompile *cfg)
2846 MonoJumpInfo *patch_info;
2847 MonoMethod *method = cfg->method;
2851 code = cfg->native_code + cfg->code_len;
2853 if (mono_jit_trace_calls)
2854 code = mono_arch_instrument_epilog (cfg, leave_method, code, TRUE);
2859 if (method->save_lmf) {
2860 pos = -sizeof (MonoLMF);
2863 if (method->save_lmf) {
2865 /* ebx = previous_lmf */
2866 x86_pop_reg (code, X86_EBX);
2868 x86_pop_reg (code, X86_EDI);
2869 /* *(lmf) = previous_lmf */
2870 x86_mov_membase_reg (code, X86_EDI, 0, X86_EBX, 4);
2872 /* discard method info */
2873 x86_pop_reg (code, X86_ESI);
2875 /* restore caller saved regs */
2876 x86_pop_reg (code, X86_EBP);
2877 x86_pop_reg (code, X86_ESI);
2878 x86_pop_reg (code, X86_EDI);
2879 x86_pop_reg (code, X86_EBX);
2883 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2884 ppc_lwz (code, ppc_r0, cfg->stack_usage + 8, cfg->frame_reg);
2885 ppc_mtlr (code, ppc_r0);
2887 ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
2888 for (i = 13; i < 32; ++i) {
2889 if (cfg->used_int_regs & (1 << i)) {
2891 ppc_lwz (code, i, -pos, cfg->frame_reg);
2896 /* add code to raise exceptions */
2897 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2898 switch (patch_info->type) {
2899 case MONO_PATCH_INFO_EXC:
2900 /*x86_patch (patch_info->ip.i + cfg->native_code, code);
2901 x86_push_imm (code, patch_info->data.target);
2902 x86_push_imm (code, patch_info->ip.i + cfg->native_code);
2903 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
2904 patch_info->data.name = "throw_exception_by_name";
2905 patch_info->ip.i = code - cfg->native_code;
2906 x86_jump_code (code, 0);*/
2914 cfg->code_len = code - cfg->native_code;
2916 g_assert (cfg->code_len < cfg->code_size);