2 * mini-sparc.c: Sparc backend for the Mono code generator
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
9 * Christopher Taylor (ct@gentoo.org)
10 * Mark Crichton (crichton@gimp.org)
11 * Zoltan Varga (vargaz@freemail.hu)
13 * (C) 2003 Ximian, Inc.
18 #include <mono/metadata/appdomain.h>
19 #include <mono/metadata/debug-helpers.h>
20 #include <mono/utils/mono-math.h>
22 #include "mini-sparc.h"
24 #include "cpu-sparc.h"
27 * Sparc V9 means two things:
28 * - the instruction set
31 * V9 instructions are only usable if the underlying processor is 64 bit. Most Sparc
32 * processors in use are 64 bit processors. The V9 ABI is only usable if the
33 * mono executable is a 64 bit executable. So it would make sense to use the 64 bit
34 * instructions without using the 64 bit ABI.
39 * - %i0..%i7 hold the incoming arguments, these are never written by JITted code
40 * - %l0..%l7 is used for local register allocation
41 * - %o0..%o6 is used for outgoing arguments
42 * - %o7 and %g1 is used as scratch registers in opcodes
43 * - all floating point registers are used for local register allocation except %f0.
44 * Only double precision registers are used.
48 #error "Sparc V9 support not yet implemented."
51 int mono_exc_esp_offset = 0;
53 #define NOT_IMPLEMENTED g_assert_not_reached ();
55 static void enter_method (MonoMethod *method, char *ebp);
56 static void leave_method (MonoMethod *method, ...);
59 mono_arch_regname (int reg) {
60 static const char * rnames[] = {
61 "sparc_g0", "sparc_g1", "sparc_g2", "sparc_g3", "sparc_g4",
62 "sparc_g5", "sparc_g6", "sparc_g7", "sparc_o0", "sparc_o1",
63 "sparc_o2", "sparc_o3", "sparc_o4", "sparc_o5", "sparc_sp",
64 "sparc_call", "sparc_l0", "sparc_l1", "sparc_l2", "sparc_l3",
65 "sparc_l4", "sparc_l5", "sparc_l6", "sparc_l7", "sparc_i0",
66 "sparc_i1", "sparc_i2", "sparc_i3", "sparc_i4", "sparc_i5",
67 "sparc_fp", "sparc_retadr"
69 if (reg >= 0 && reg < 32)
75 * Initialize the cpu to execute managed code.
78 mono_arch_cpu_init (void)
83 * This function returns the optimizations supported on this cpu.
86 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
94 is_regsize_var (MonoType *t) {
103 case MONO_TYPE_OBJECT:
104 case MONO_TYPE_STRING:
105 case MONO_TYPE_CLASS:
106 case MONO_TYPE_SZARRAY:
107 case MONO_TYPE_ARRAY:
109 case MONO_TYPE_VALUETYPE:
110 if (t->data.klass->enumtype)
111 return is_regsize_var (t->data.klass->enum_basetype);
118 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
126 for (i = 0; i < cfg->num_varinfo; i++) {
127 MonoInst *ins = cfg->varinfo [i];
128 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
131 if (vmv->range.first_use.abs_pos > vmv->range.last_use.abs_pos)
134 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
138 /* we can only allocate 32 bit values */
139 if (is_regsize_var (ins->inst_vtype)) {
140 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
141 g_assert (i == vmv->idx);
142 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
150 mono_arch_get_global_int_regs (MonoCompile *cfg)
155 /* Use %l0..%l3 as global registers */
157 for (i = 16; i < 20; ++i)
158 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
164 #define flushi(addr) __asm__ __volatile__ ("flush %0"::"r"(addr):"memory")
165 #else /* assume Sun's compiler */
166 static void flushi(void *addr)
173 mono_arch_flush_icache (guint8 *code, gint size)
177 /* FIXME: Make this more efficient */
178 for (i = 0; i < (size/8); i++)
179 flushi(code + (i*8));
195 /* This needs to be offset by %i0 or %o0 depending on caller/callee */
198 guint32 vt_offset; /* for valuetypes */
214 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean pair)
216 ainfo->offset = *stack_size;
219 if (*gr >= PARAM_REGS) {
220 ainfo->storage = ArgOnStack;
223 ainfo->storage = ArgInIReg;
228 /* Allways reserve stack space for parameters passed in registers */
232 if (*gr < PARAM_REGS - 1) {
233 /* A pair of registers */
234 ainfo->storage = ArgInIRegPair;
238 else if (*gr >= PARAM_REGS) {
239 /* A pair of stack locations */
240 ainfo->storage = ArgOnStackPair;
241 ainfo->offset = *stack_size;
244 ainfo->storage = ArgInSplitRegStack;
246 ainfo->offset = *stack_size;
257 * Obtain information about a call according to the calling convention.
258 * See the "System V ABI, Sparc Processor Supplement" Sparc V8 version document for
262 get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
264 guint32 i, gr, simpletype;
265 int n = sig->hasthis + sig->param_count;
266 guint32 stack_size = 0;
269 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
275 add_general (&gr, &stack_size, cinfo->args + 0, FALSE);
277 for (i = 0; i < sig->param_count; ++i) {
278 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
280 DEBUG(printf("param %d: ", i));
281 if (sig->params [i]->byref) {
282 DEBUG(printf("byref\n"));
284 add_general (&gr, &stack_size, ainfo, FALSE);
287 simpletype = sig->params [i]->type;
289 switch (simpletype) {
290 case MONO_TYPE_BOOLEAN:
294 add_general (&gr, &stack_size, ainfo, FALSE);
295 /* the value is in the ls byte */
300 add_general (&gr, &stack_size, ainfo, FALSE);
301 /* the value is in the ls word */
309 case MONO_TYPE_CLASS:
310 case MONO_TYPE_OBJECT:
311 case MONO_TYPE_STRING:
312 case MONO_TYPE_SZARRAY:
313 case MONO_TYPE_ARRAY:
314 add_general (&gr, &stack_size, ainfo, FALSE);
316 case MONO_TYPE_VALUETYPE: {
317 if (sig->params [i]->data.klass->enumtype) {
318 simpletype = sig->params [i]->data.klass->enum_basetype->type;
322 add_general (&gr, &stack_size, ainfo, FALSE);
327 add_general (&gr, &stack_size, ainfo, TRUE);
330 /* single precision values are passed in integer registers */
331 add_general (&gr, &stack_size, ainfo, FALSE);
334 /* double precision values are passed in a pair of registers */
335 add_general (&gr, &stack_size, ainfo, TRUE);
338 g_assert_not_reached ();
344 simpletype = sig->ret->type;
346 switch (simpletype) {
347 case MONO_TYPE_BOOLEAN:
357 case MONO_TYPE_CLASS:
358 case MONO_TYPE_OBJECT:
359 case MONO_TYPE_SZARRAY:
360 case MONO_TYPE_ARRAY:
361 case MONO_TYPE_STRING:
362 cinfo->ret.storage = ArgInIReg;
363 cinfo->ret.reg = sparc_i0;
367 cinfo->ret.storage = ArgInIRegPair;
368 cinfo->ret.reg = sparc_i0;
372 cinfo->ret.storage = ArgInFReg;
373 cinfo->ret.reg = sparc_f0;
375 case MONO_TYPE_VALUETYPE:
376 if (sig->ret->data.klass->enumtype) {
377 simpletype = sig->ret->data.klass->enum_basetype->type;
380 cinfo->ret.storage = ArgOnStack;
385 g_error ("Can't handle as return value 0x%x", sig->ret->type);
389 cinfo->stack_usage = stack_size;
394 * Set var information according to the calling convention. sparc version.
395 * The locals var stuff should most likely be split in another method.
398 mono_arch_allocate_vars (MonoCompile *m)
400 MonoMethodSignature *sig;
401 MonoMethodHeader *header;
403 int i, offset, size, align, curinst;
404 int frame_reg = sparc_sp;
407 m->frame_reg = frame_reg;
409 header = ((MonoMethodNormal *)m->method)->header;
411 sig = m->method->signature;
413 cinfo = get_call_info (sig, FALSE);
415 if (sig->ret->type != MONO_TYPE_VOID) {
416 switch (cinfo->ret.storage) {
420 m->ret->opcode = OP_REGVAR;
421 m->ret->inst_c0 = cinfo->ret.reg;
425 m->ret->opcode = OP_REGOFFSET;
426 m->ret->inst_basereg = sparc_fp;
427 m->ret->inst_offset = 64;
435 * We use the Sparc V8 calling conventions for managed code as well.
436 * FIXME: Use something more optimized.
439 offset = 64; /* register save area */
440 offset += 4; /* struct/union return pointer */
442 /* add parameter area size for called functions */
443 if (m->param_area < 24)
444 /* Reserve space for the first 6 arguments even if it is unused */
447 offset += m->param_area;
449 curinst = m->locals_start;
450 for (i = curinst; i < m->num_varinfo; ++i) {
451 inst = m->varinfo [i];
453 if (inst->opcode == OP_REGVAR)
456 /* inst->unused indicates native sized value types, this is used by the
457 * pinvoke wrappers when they call functions returning structure */
458 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype))
459 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
461 size = mono_type_size (inst->inst_vtype, &align);
464 offset &= ~(align - 1);
465 inst->inst_offset = offset;
466 inst->opcode = OP_REGOFFSET;
467 inst->inst_basereg = frame_reg;
469 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
474 inst = m->varinfo [curinst];
475 if (inst->opcode != OP_REGVAR) {
476 ArgInfo *ainfo = &cinfo->args [0];
478 g_assert (ainfo->storage == ArgInIReg);
480 inst->opcode = OP_REGVAR;
481 inst->dreg = sparc_i0 + ainfo->reg;
486 for (i = 0; i < sig->param_count; ++i) {
487 inst = m->varinfo [curinst];
488 if (inst->opcode != OP_REGVAR) {
489 ArgInfo *ainfo = &cinfo->args [curinst];
490 gboolean inreg = TRUE;
492 if ((sig->params [i]->type == MONO_TYPE_R4)
493 || (sig->params [i]->type == MONO_TYPE_R8))
495 * Since float arguments are passed in integer registers, we need to
496 * save them to the stack in the prolog.
500 if (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
503 if (MONO_TYPE_ISSTRUCT (sig->params [i]))
504 /* FIXME: this isn't needed */
507 switch (ainfo->storage) {
511 inst->opcode = OP_REGVAR;
512 inst->dreg = sparc_i0 + ainfo->reg;
520 case ArgInSplitRegStack:
521 /* Split arguments are saved to the stack in the prolog */
522 inst->opcode = OP_REGOFFSET;
523 /* in parent frame */
524 inst->inst_basereg = sparc_fp;
525 inst->inst_offset = ainfo->offset + 68;
527 if (sig->params [i]->type == MONO_TYPE_R8) {
529 * It is very hard to load doubles from non-doubleword aligned
530 * memory locations. So if the offset is misaligned, we copy the
531 * argument to a stack location in the prolog.
533 if (inst->inst_offset % 8) {
534 inst->inst_basereg = sparc_sp;
537 offset &= ~(align - 1);
538 inst->inst_offset = offset;
547 if (MONO_TYPE_ISSTRUCT (sig->params [i])) {
548 /* Add a level of indirection */
550 * It would be easier to add OP_LDIND_I here, but ldind_i instructions
551 * are destructively modified in a lot of places in inssel.brg.
554 MONO_INST_NEW (m, indir, 0);
556 inst->opcode = OP_SPARC_INARG_VT;
557 inst->inst_left = indir;
561 g_assert_not_reached ();
565 /* align the stack size to 8 bytes */
569 /* Add a properly aligned dword for use by int<->float conversion opcodes */
572 m->stack_offset = offset;
578 * take the arguments and generate the arch-specific
579 * instructions to properly call the function in call.
580 * This includes pushing, moving arguments to the right register
584 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
586 MonoMethodSignature *sig;
590 guint32 extra_space = 0;
592 sig = call->signature;
593 n = sig->param_count + sig->hasthis;
595 cinfo = get_call_info (sig, sig->pinvoke);
597 for (i = 0; i < n; ++i) {
598 ainfo = cinfo->args + i;
599 if (is_virtual && i == 0) {
600 /* the argument will be attached to the call instruction */
603 MONO_INST_NEW (cfg, arg, OP_OUTARG);
605 arg->cil_code = in->cil_code;
607 arg->type = in->type;
608 /* prepend, we'll need to reverse them later */
609 arg->next = call->out_args;
610 call->out_args = arg;
612 if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis]))) {
614 guint32 size = mono_type_stack_size (&in->klass->byval_arg, NULL);
617 * We use OP_OUTARG_VT to copy the valuetype to a stack location, then
618 * use the normal OUTARG opcodes to pass the address of the location to
621 MONO_INST_NEW (cfg, inst, OP_OUTARG_VT);
622 inst->inst_left = in;
624 /* The first 6 argument locations are reserved */
625 if (cinfo->stack_usage < 24)
626 cinfo->stack_usage = 24;
628 inst->inst_c1 = 68 + cinfo->stack_usage;
630 arg->inst_left = inst;
632 cinfo->stack_usage += size;
635 switch (ainfo->storage) {
639 if (ainfo->storage == ArgInIRegPair)
640 arg->opcode = OP_SPARC_OUTARG_REGPAIR;
641 arg->unused = sparc_o0 + ainfo->reg;
642 /* outgoing arguments begin at sp+68 */
643 arg->inst_basereg = sparc_sp;
644 arg->inst_imm = 68 + ainfo->offset;
645 call->used_iregs |= 1 << ainfo->reg;
647 if ((i >= sig->hasthis) && (sig->params [i - sig->hasthis]->type == MONO_TYPE_R8)) {
649 * The OUTARG (freg) implementation needs an extra dword to store
650 * the temporary value.
656 arg->opcode = OP_SPARC_OUTARG_MEM;
657 arg->inst_basereg = sparc_sp;
658 arg->inst_imm = 68 + ainfo->offset;
661 arg->opcode = OP_SPARC_OUTARG_MEMPAIR;
662 arg->inst_basereg = sparc_sp;
663 arg->inst_imm = 68 + ainfo->offset;
665 case ArgInSplitRegStack:
666 arg->opcode = OP_SPARC_OUTARG_SPLIT_REG_STACK;
667 arg->unused = sparc_o0 + ainfo->reg;
668 arg->inst_basereg = sparc_sp;
669 arg->inst_imm = 68 + ainfo->offset;
670 call->used_iregs |= 1 << ainfo->reg;
679 * Reverse the call->out_args list.
682 MonoInst *prev = NULL, *list = call->out_args, *next;
689 call->out_args = prev;
691 call->stack_usage = cinfo->stack_usage + extra_space;
692 cfg->param_area = MAX (cfg->param_area, call->stack_usage);
693 cfg->flags |= MONO_CFG_HAS_CALLS;
699 /* Map opcode to the sparc condition codes */
700 static inline SparcCond
701 opcode_to_sparc_cond (int opcode)
746 g_assert_not_reached ();
751 #define EMIT_COND_BRANCH_GENERAL(ins,bop,cond) \
752 if (ins->flags & MONO_INST_BRLABEL) { \
753 if (ins->inst_i0->inst_c0) { \
754 gint32 disp = (ins->inst_i0->inst_c0 - ((guint8*)code - cfg->native_code)) >> 2; \
755 g_assert (sparc_is_imm22 (disp)); \
756 sparc_ ## bop (code, 1, cond, disp); \
758 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
759 sparc_ ## bop (code, 1, cond, 0); \
763 if (ins->inst_true_bb->native_offset) { \
764 gint32 disp = (ins->inst_true_bb->native_offset - ((guint8*)code - cfg->native_code)) >> 2; \
765 g_assert (sparc_is_imm22 (disp)); \
766 sparc_ ## bop (code, 1, cond, disp); \
768 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
769 sparc_ ## bop (code, 1, cond, 0); \
774 #define EMIT_COND_BRANCH(ins,cond) EMIT_COND_BRANCH_GENERAL((ins),branch,(cond))
776 #define EMIT_FLOAT_COND_BRANCH(ins,cond) EMIT_COND_BRANCH_GENERAL((ins),fbranch,(cond))
778 #define EMIT_ALU_IMM(ins,op,setcc) do { \
779 if (sparc_is_imm13 ((ins)->inst_imm)) \
780 sparc_ ## op ## _imm (code, (setcc), (ins)->sreg1, ins->inst_imm, (ins)->dreg); \
782 sparc_set (code, ins->inst_imm, sparc_o7); \
783 sparc_ ## op (code, (setcc), (ins)->sreg1, sparc_o7, (ins)->dreg); \
787 #define EMIT_LOAD_MEMBASE(ins,op) do { \
788 if (sparc_is_imm13 (ins->inst_offset)) \
789 sparc_ ## op ## _imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg); \
791 sparc_set (code, ins->inst_offset, sparc_o7); \
792 sparc_ ## op (code, ins->inst_basereg, sparc_o7, ins->dreg); \
796 /* emit an exception if condition is fail */
797 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
799 mono_add_patch_info (cfg, code - cfg->native_code, \
800 MONO_PATCH_INFO_EXC, exc_name); \
801 x86_branch32 (code, cond, 0, signed); \
804 #define EMIT_FPCOMPARE(code) do { \
807 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500); \
811 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
813 MonoInst *ins, *last_ins = NULL;
816 /* short circuit this for now */
821 switch (ins->opcode) {
823 /* remove unnecessary multiplication with 1 */
824 if (ins->inst_imm == 1) {
825 if (ins->dreg != ins->sreg1) {
826 ins->opcode = OP_MOVE;
828 last_ins->next = ins->next;
834 case OP_LOAD_MEMBASE:
835 case OP_LOADI4_MEMBASE:
837 * OP_STORE_MEMBASE_REG reg, offset(basereg)
838 * OP_LOAD_MEMBASE offset(basereg), reg
840 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
841 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
842 ins->inst_basereg == last_ins->inst_destbasereg &&
843 ins->inst_offset == last_ins->inst_offset) {
844 if (ins->dreg == last_ins->sreg1) {
845 last_ins->next = ins->next;
849 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
850 ins->opcode = OP_MOVE;
851 ins->sreg1 = last_ins->sreg1;
855 * Note: reg1 must be different from the basereg in the second load
856 * OP_LOAD_MEMBASE offset(basereg), reg1
857 * OP_LOAD_MEMBASE offset(basereg), reg2
859 * OP_LOAD_MEMBASE offset(basereg), reg1
862 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
863 || last_ins->opcode == OP_LOAD_MEMBASE) &&
864 ins->inst_basereg != last_ins->dreg &&
865 ins->inst_basereg == last_ins->inst_basereg &&
866 ins->inst_offset == last_ins->inst_offset) {
868 if (ins->dreg == last_ins->dreg) {
869 last_ins->next = ins->next;
873 ins->opcode = OP_MOVE;
874 ins->sreg1 = last_ins->dreg;
877 //g_assert_not_reached ();
881 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
882 * OP_LOAD_MEMBASE offset(basereg), reg
884 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
887 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
888 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
889 ins->inst_basereg == last_ins->inst_destbasereg &&
890 ins->inst_offset == last_ins->inst_offset) {
891 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
892 ins->opcode = OP_ICONST;
893 ins->inst_c0 = last_ins->inst_imm;
894 g_assert_not_reached (); // check this rule
898 case OP_LOADU1_MEMBASE:
899 case OP_LOADI1_MEMBASE:
900 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
901 ins->inst_basereg == last_ins->inst_destbasereg &&
902 ins->inst_offset == last_ins->inst_offset) {
903 if (ins->dreg == last_ins->sreg1) {
904 last_ins->next = ins->next;
908 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
909 ins->opcode = OP_MOVE;
910 ins->sreg1 = last_ins->sreg1;
914 case OP_LOADU2_MEMBASE:
915 case OP_LOADI2_MEMBASE:
916 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
917 ins->inst_basereg == last_ins->inst_destbasereg &&
918 ins->inst_offset == last_ins->inst_offset) {
919 if (ins->dreg == last_ins->sreg1) {
920 last_ins->next = ins->next;
924 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
925 ins->opcode = OP_MOVE;
926 ins->sreg1 = last_ins->sreg1;
936 if (ins->dreg == ins->sreg1) {
938 last_ins->next = ins->next;
946 if (last_ins && last_ins->opcode == OP_MOVE &&
947 ins->sreg1 == last_ins->dreg &&
948 ins->dreg == last_ins->sreg1) {
949 last_ins->next = ins->next;
958 bb->last_ins = last_ins;
962 #define DEBUG(a) if (cfg->verbose_level > 1) a
964 #define reg_is_freeable(r) (TRUE)
965 #define freg_is_freeable(r) (TRUE)
974 static const char*const * ins_spec = sparc_desc;
977 print_ins (int i, MonoInst *ins)
979 const char *spec = ins_spec [ins->opcode];
980 g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
981 if (spec [MONO_INST_DEST]) {
982 if (ins->dreg >= MONO_MAX_IREGS)
983 g_print (" R%d <-", ins->dreg);
985 g_print (" %s <-", mono_arch_regname (ins->dreg));
987 if (spec [MONO_INST_SRC1]) {
988 if (ins->sreg1 >= MONO_MAX_IREGS)
989 g_print (" R%d", ins->sreg1);
991 g_print (" %s", mono_arch_regname (ins->sreg1));
993 if (spec [MONO_INST_SRC2]) {
994 if (ins->sreg2 >= MONO_MAX_IREGS)
995 g_print (" R%d", ins->sreg2);
997 g_print (" %s", mono_arch_regname (ins->sreg2));
999 if (spec [MONO_INST_CLOB])
1000 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1005 print_regtrack (RegTrack *t, int num)
1011 for (i = 0; i < num; ++i) {
1014 if (i >= MONO_MAX_IREGS) {
1015 g_snprintf (buf, sizeof(buf), "R%d", i);
1018 r = mono_arch_regname (i);
1019 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1023 typedef struct InstList InstList;
1031 static inline InstList*
1032 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1034 InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1043 #define STACK_OFFSETS_POSITIVE
1046 * returns the offset used by spillvar. It allocates a new
1047 * spill variable if necessary. Likely incorrect for sparc.
1050 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
1052 MonoSpillInfo **si, *info;
1055 si = &cfg->spill_info;
1057 while (i <= spillvar) {
1060 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1062 #ifdef STACK_OFFSETS_POSITIVE
1063 cfg->stack_offset += sizeof (gpointer);
1065 cfg->stack_offset -= sizeof (gpointer);
1067 info->offset = cfg->stack_offset;
1071 return (*si)->offset;
1077 g_assert_not_reached ();
1082 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
1084 MonoSpillInfo **si, *info;
1087 si = &cfg->spill_info_float;
1089 while (i <= spillvar) {
1092 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1094 cfg->stack_offset += 7;
1095 cfg->stack_offset &= ~7;
1096 info->offset = cfg->stack_offset;
1097 cfg->stack_offset += sizeof (double);
1101 return (*si)->offset;
1107 g_assert_not_reached ();
1112 * Force the spilling of the variable in the symbolic register 'reg'.
1115 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1120 sel = cfg->rs->iassign [reg];
1121 /*i = cfg->rs->isymbolic [sel];
1122 g_assert (i == reg);*/
1124 spill = ++cfg->spill_count;
1125 cfg->rs->iassign [i] = -spill - 1;
1126 mono_regstate_free_int (cfg->rs, sel);
1127 /* we need to create a spill var and insert a load to sel after the current instruction */
1128 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1130 load->inst_basereg = cfg->frame_reg;
1131 load->inst_offset = mono_spillvar_offset (cfg, spill);
1133 while (ins->next != item->prev->data)
1136 load->next = ins->next;
1138 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1139 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1140 g_assert (i == sel);
1146 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1151 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));
1152 /* exclude the registers in the current instruction */
1153 if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1154 if (ins->sreg1 >= MONO_MAX_IREGS)
1155 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1157 regmask &= ~ (1 << ins->sreg1);
1158 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1160 if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1161 if (ins->sreg2 >= MONO_MAX_IREGS)
1162 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1164 regmask &= ~ (1 << ins->sreg2);
1165 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1167 if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1168 regmask &= ~ (1 << ins->dreg);
1169 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1172 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1173 g_assert (regmask); /* need at least a register we can free */
1175 /* we should track prev_use and spill the register that's farther */
1176 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1177 if (regmask & (1 << i)) {
1179 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1183 i = cfg->rs->isymbolic [sel];
1184 spill = ++cfg->spill_count;
1185 cfg->rs->iassign [i] = -spill - 1;
1186 mono_regstate_free_int (cfg->rs, sel);
1187 /* we need to create a spill var and insert a load to sel after the current instruction */
1188 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1190 load->inst_basereg = cfg->frame_reg;
1191 load->inst_offset = mono_spillvar_offset (cfg, spill);
1193 while (ins->next != item->prev->data)
1196 load->next = ins->next;
1198 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1199 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1200 g_assert (i == sel);
1206 get_float_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1211 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));
1212 /* exclude the registers in the current instruction */
1213 if (reg != ins->sreg1 && (freg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg1] >= 0))) {
1214 if (ins->sreg1 >= MONO_MAX_FREGS)
1215 regmask &= ~ (1 << cfg->rs->fassign [ins->sreg1]);
1217 regmask &= ~ (1 << ins->sreg1);
1218 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1220 if (reg != ins->sreg2 && (freg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg2] >= 0))) {
1221 if (ins->sreg2 >= MONO_MAX_FREGS)
1222 regmask &= ~ (1 << cfg->rs->fassign [ins->sreg2]);
1224 regmask &= ~ (1 << ins->sreg2);
1225 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1227 if (reg != ins->dreg && freg_is_freeable (ins->dreg)) {
1228 regmask &= ~ (1 << ins->dreg);
1229 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1232 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1233 g_assert (regmask); /* need at least a register we can free */
1235 /* we should track prev_use and spill the register that's farther */
1236 for (i = 0; i < MONO_MAX_FREGS; ++i) {
1237 if (regmask & (1 << i)) {
1239 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->fassign [sel]));
1243 i = cfg->rs->fsymbolic [sel];
1244 spill = ++cfg->spill_count;
1245 cfg->rs->fassign [i] = -spill - 1;
1246 mono_regstate_free_float(cfg->rs, sel);
1247 /* we need to create a spill var and insert a load to sel after the current instruction */
1248 MONO_INST_NEW (cfg, load, OP_LOADR8_MEMBASE);
1250 load->inst_basereg = cfg->frame_reg;
1251 load->inst_offset = mono_spillvar_offset_float (cfg, spill);
1253 while (ins->next != item->prev->data)
1256 load->next = ins->next;
1258 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1259 i = mono_regstate_alloc_float (cfg->rs, 1 << sel);
1260 g_assert (i == sel);
1266 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1269 MONO_INST_NEW (cfg, copy, OP_MOVE);
1273 copy->next = ins->next;
1276 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1281 create_copy_ins_float (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1284 MONO_INST_NEW (cfg, copy, OP_FMOVE);
1288 copy->next = ins->next;
1291 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1296 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1299 MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1301 store->inst_destbasereg = cfg->frame_reg;
1302 store->inst_offset = mono_spillvar_offset (cfg, spill);
1304 store->next = ins->next;
1307 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)));
1312 create_spilled_store_float (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1315 MONO_INST_NEW (cfg, store, OP_STORER8_MEMBASE_REG);
1317 store->inst_destbasereg = cfg->frame_reg;
1318 store->inst_offset = mono_spillvar_offset_float (cfg, spill);
1320 store->next = ins->next;
1323 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)));
1328 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1331 g_assert (item->next);
1332 prev = item->next->data;
1334 while (prev->next != ins)
1336 to_insert->next = ins;
1337 prev->next = to_insert;
1339 * needed otherwise in the next instruction we can add an ins to the
1340 * end and that would get past this instruction.
1342 item->data = to_insert;
1346 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1348 int val = cfg->rs->iassign [sym_reg];
1352 /* the register gets spilled after this inst */
1355 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1357 val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1358 cfg->rs->iassign [sym_reg] = val;
1359 /* add option to store before the instruction for src registers */
1361 create_spilled_store (cfg, spill, val, sym_reg, ins);
1363 cfg->rs->isymbolic [val] = sym_reg;
1367 /* Parameters used by the register allocator */
1369 /* Use %l4..%l7 as local registers */
1370 #define ARCH_CALLER_REGS (0xf0<<16)
1371 /* Use %f2..%f30 as the double precision floating point local registers */
1372 #define ARCH_CALLER_FREGS (0x55555554)
1374 /* FIXME: Strange loads from the stack in basic-float.cs:test_2_rem */
1377 * Local register allocation.
1378 * We first scan the list of instructions and we save the liveness info of
1379 * each register (when the register is first used, when it's value is set etc.).
1380 * We also reverse the list of instructions (in the InstList list) because assigning
1381 * registers backwards allows for more tricks to be used.
1384 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1387 MonoRegState *rs = cfg->rs;
1389 RegTrack *reginfo, *reginfof;
1390 RegTrack *reginfo1, *reginfo2, *reginfod;
1391 InstList *tmp, *reversed = NULL;
1393 guint32 src1_mask, src2_mask, dest_mask;
1394 guint32 cur_iregs, cur_fregs;
1396 /* FIXME: clobbering */
1400 rs->next_vireg = bb->max_ireg;
1401 rs->next_vfreg = bb->max_freg;
1402 mono_regstate_assign (rs);
1403 reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
1404 reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
1405 rs->ifree_mask = ARCH_CALLER_REGS;
1406 rs->ffree_mask = ARCH_CALLER_FREGS;
1410 DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1411 /* forward pass on the instructions to collect register liveness info */
1413 spec = ins_spec [ins->opcode];
1415 DEBUG (print_ins (i, ins));
1417 if (spec [MONO_INST_SRC1]) {
1418 if (spec [MONO_INST_SRC1] == 'f')
1419 reginfo1 = reginfof;
1422 reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1423 reginfo1 [ins->sreg1].last_use = i;
1427 if (spec [MONO_INST_SRC2]) {
1428 if (spec [MONO_INST_SRC2] == 'f')
1429 reginfo2 = reginfof;
1432 reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1433 reginfo2 [ins->sreg2].last_use = i;
1437 if (spec [MONO_INST_DEST]) {
1438 if (spec [MONO_INST_DEST] == 'f')
1439 reginfod = reginfof;
1442 if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1443 reginfod [ins->dreg].killed_in = i;
1444 reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1445 reginfod [ins->dreg].last_use = i;
1446 if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1447 reginfod [ins->dreg].born_in = i;
1448 if (spec [MONO_INST_DEST] == 'l') {
1449 /* result in eax:edx, the virtual register is allocated sequentially */
1450 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1451 reginfod [ins->dreg + 1].last_use = i;
1452 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1453 reginfod [ins->dreg + 1].born_in = i;
1458 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1463 cur_iregs = ARCH_CALLER_REGS;
1464 cur_fregs = ARCH_CALLER_FREGS;
1466 DEBUG (print_regtrack (reginfo, rs->next_vireg));
1467 DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1470 int prev_dreg, prev_sreg1, prev_sreg2;
1473 spec = ins_spec [ins->opcode];
1474 DEBUG (g_print ("processing:"));
1475 DEBUG (print_ins (i, ins));
1477 /* make the register available for allocation: FIXME add fp reg */
1478 if (ins->opcode == OP_SETREG || ins->opcode == OP_SETREGIMM) {
1479 cur_iregs |= 1 << ins->dreg;
1480 DEBUG (g_print ("adding %d to cur_iregs\n", ins->dreg));
1481 } else if (ins->opcode == OP_SETFREG) {
1482 cur_fregs |= 1 << ins->dreg;
1483 DEBUG (g_print ("adding %d to cur_fregs\n", ins->dreg));
1484 } else if (spec [MONO_INST_CLOB] == 'c') {
1485 MonoCallInst *cinst = (MonoCallInst*)ins;
1486 DEBUG (g_print ("excluding regs 0x%x from cur_iregs (0x%x)\n", cinst->used_iregs, cur_iregs));
1487 cur_iregs &= ~cinst->used_iregs;
1488 cur_fregs &= ~cinst->used_fregs;
1489 DEBUG (g_print ("available cur_iregs: 0x%x\n", cur_iregs));
1490 /* registers used by the calling convention are excluded from
1491 * allocation: they will be selectively enabled when they are
1492 * assigned by the special SETREG opcodes.
1495 dest_mask = src1_mask = src2_mask = cur_iregs;
1500 /* update for use with FP regs... */
1501 if (spec [MONO_INST_DEST] == 'f') {
1502 if (ins->dreg >= MONO_MAX_FREGS) {
1503 val = rs->fassign [ins->dreg];
1504 prev_dreg = ins->dreg;
1508 /* the register gets spilled after this inst */
1511 dest_mask = cur_fregs;
1512 val = mono_regstate_alloc_float (rs, dest_mask);
1514 val = get_float_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1515 rs->fassign [ins->dreg] = val;
1517 create_spilled_store_float (cfg, spill, val, prev_dreg, ins);
1519 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1520 rs->fsymbolic [val] = prev_dreg;
1525 if (freg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfo [prev_dreg].born_in >= i || !(cur_fregs & (1 << ins->dreg)))) {
1526 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1527 mono_regstate_free_float (rs, ins->dreg);
1529 } else if (ins->dreg >= MONO_MAX_IREGS) {
1530 val = rs->iassign [ins->dreg];
1531 prev_dreg = ins->dreg;
1535 /* the register gets spilled after this inst */
1538 val = mono_regstate_alloc_int (rs, dest_mask);
1540 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1541 rs->iassign [ins->dreg] = val;
1543 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1545 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1546 rs->isymbolic [val] = prev_dreg;
1548 if (spec [MONO_INST_DEST] == 'l') {
1549 int hreg = prev_dreg + 1;
1550 val = rs->iassign [hreg];
1554 /* the register gets spilled after this inst */
1557 /* The second register must be a pair of the first */
1558 dest_mask = 1 << (rs->iassign [prev_dreg] + 1);
1559 val = mono_regstate_alloc_int (rs, dest_mask);
1561 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1562 rs->iassign [hreg] = val;
1564 create_spilled_store (cfg, spill, val, hreg, ins);
1567 /* The second register must be a pair of the first */
1568 if (val != rs->iassign [prev_dreg] + 1) {
1569 dest_mask = 1 << (rs->iassign [prev_dreg] + 1);
1571 val = mono_regstate_alloc_int (rs, dest_mask);
1573 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1575 create_copy_ins (cfg, rs->iassign [hreg], val, ins);
1577 rs->iassign [hreg] = val;
1581 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
1582 rs->isymbolic [val] = hreg;
1584 if (reg_is_freeable (val) && hreg >= 0 && (reginfo [hreg].born_in >= i && !(cur_iregs & (1 << val)))) {
1585 DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
1586 mono_regstate_free_int (rs, val);
1592 if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfo [prev_dreg].born_in >= i)) {
1593 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1594 mono_regstate_free_int (rs, ins->dreg);
1600 if (spec [MONO_INST_SRC1] == 'f') {
1601 if (ins->sreg1 >= MONO_MAX_FREGS) {
1602 val = rs->fassign [ins->sreg1];
1603 prev_sreg1 = ins->sreg1;
1607 /* the register gets spilled after this inst */
1610 //g_assert (val == -1); /* source cannot be spilled */
1611 src1_mask = cur_fregs;
1612 val = mono_regstate_alloc_float (rs, src1_mask);
1614 val = get_float_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1615 rs->fassign [ins->sreg1] = val;
1616 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1618 MonoInst *store = create_spilled_store_float (cfg, spill, val, prev_sreg1, NULL);
1619 insert_before_ins (ins, tmp, store);
1622 rs->fsymbolic [val] = prev_sreg1;
1627 } else if (ins->sreg1 >= MONO_MAX_IREGS) {
1628 val = rs->iassign [ins->sreg1];
1629 prev_sreg1 = ins->sreg1;
1633 /* the register gets spilled after this inst */
1636 if (0 && ins->opcode == OP_MOVE) {
1638 * small optimization: the dest register is already allocated
1639 * but the src one is not: we can simply assign the same register
1640 * here and peephole will get rid of the instruction later.
1641 * This optimization may interfere with the clobbering handling:
1642 * it removes a mov operation that will be added again to handle clobbering.
1643 * There are also some other issues that should with make testjit.
1645 mono_regstate_alloc_int (rs, 1 << ins->dreg);
1646 val = rs->iassign [ins->sreg1] = ins->dreg;
1647 //g_assert (val >= 0);
1648 DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1650 //g_assert (val == -1); /* source cannot be spilled */
1651 val = mono_regstate_alloc_int (rs, src1_mask);
1653 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1654 rs->iassign [ins->sreg1] = val;
1655 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1658 MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
1659 insert_before_ins (ins, tmp, store);
1662 rs->isymbolic [val] = prev_sreg1;
1671 if (spec [MONO_INST_SRC2] == 'f') {
1672 if (ins->sreg2 >= MONO_MAX_FREGS) {
1673 val = rs->fassign [ins->sreg2];
1674 prev_sreg2 = ins->sreg2;
1678 /* the register gets spilled after this inst */
1681 src2_mask = cur_fregs;
1682 val = mono_regstate_alloc_float (rs, src2_mask);
1684 val = get_float_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1685 rs->fassign [ins->sreg2] = val;
1686 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1688 create_spilled_store_float (cfg, spill, val, prev_sreg2, ins);
1690 rs->fsymbolic [val] = prev_sreg2;
1695 } else if (ins->sreg2 >= MONO_MAX_IREGS) {
1696 val = rs->iassign [ins->sreg2];
1697 prev_sreg2 = ins->sreg2;
1701 /* the register gets spilled after this inst */
1704 val = mono_regstate_alloc_int (rs, src2_mask);
1706 val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1707 rs->iassign [ins->sreg2] = val;
1708 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1710 create_spilled_store (cfg, spill, val, prev_sreg2, ins);
1712 rs->isymbolic [val] = prev_sreg2;
1718 if (spec [MONO_INST_CLOB] == 'c') {
1720 guint32 clob_mask = ARCH_CALLER_REGS;
1721 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1723 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
1724 //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
1728 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
1729 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
1730 mono_regstate_free_int (rs, ins->sreg1);
1732 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
1733 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
1734 mono_regstate_free_int (rs, ins->sreg2);
1737 //DEBUG (print_ins (i, ins));
1744 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
1749 static unsigned char*
1750 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
1757 sparc_patch (guint8 *code, guint8 *target)
1759 guint32 ins = *(guint32*)code;
1760 guint32 op = ins >> 30;
1761 guint32 op2 = (ins >> 22) & 0x7;
1762 guint32 rd = (ins >> 25) & 0x1f;
1763 gint32 disp = (target - code) >> 2;
1765 // g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
1767 if ((op == 0) && (op2 == 2)) {
1768 if (!sparc_is_imm22 (disp))
1771 *(guint32*)code = ((ins >> 22) << 22) | disp;
1773 else if ((op == 0) && (op2 == 6)) {
1774 if (!sparc_is_imm22 (disp))
1777 *(guint32*)code = ((ins >> 22) << 22) | disp;
1779 else if ((op == 0) && (op2 == 4)) {
1780 guint32 ins2 = *(guint32*)(code + 4);
1782 if (((ins2 >> 30) == 2) && (((ins2 >> 19) & 0x3f) == 2)) {
1783 /* sethi followed by or */
1784 guint32 *p = (guint32*)code;
1785 sparc_set (p, target, rd);
1786 while (p < (code + 4))
1789 else if ((sparc_inst_op (ins2) == 3) && (sparc_inst_imm (ins2))) {
1790 /* sethi followed by load/store */
1791 guint32 t = (guint32)target;
1792 *(guint32*)code = ins | (t >> 10);
1793 *(guint32*)(code + 4) = ins2 | (t & 0x3ff);
1795 else if ((sparc_inst_op (ins2) == 2) && (sparc_inst_op3 (ins2) == 0x38) &&
1796 (sparc_inst_imm (ins2))) {
1797 /* sethi followed by jmpl */
1798 guint32 t = (guint32)target;
1799 *(guint32*)code = ins | (t >> 10);
1800 *(guint32*)(code + 4) = ins2 | (t & 0x3ff);
1805 else if (op == 01) {
1806 sparc_call_simple (code, target - code);
1811 // g_print ("patched with 0x%08x\n", ins);
1815 emit_move_return_value (MonoInst *ins, guint32 *code)
1817 /* Move return value to the target register */
1818 /* FIXME: do this in the local reg allocator */
1819 switch (ins->opcode) {
1821 case OP_VOIDCALL_REG:
1822 case OP_VOIDCALL_MEMBASE:
1826 case OP_CALL_MEMBASE:
1827 sparc_mov_reg_reg (code, sparc_o0, ins->dreg);
1831 case OP_LCALL_MEMBASE:
1833 * ins->dreg is the least significant reg due to the lreg: LCALL rule
1836 sparc_mov_reg_reg (code, sparc_o0, ins->dreg + 1);
1837 sparc_mov_reg_reg (code, sparc_o1, ins->dreg);
1841 case OP_FCALL_MEMBASE:
1842 sparc_fmovs (code, sparc_f0, ins->dreg);
1843 sparc_fmovs (code, sparc_f1, ins->dreg + 1);
1847 case OP_VCALL_MEMBASE:
1857 * Some conventions used in the following code.
1858 * 2) The only scratch registers we have are o7 and g1. We try to
1859 * stick to o7 when we can, and use g1 when necessary.
1863 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
1868 guint32 *code = (guint32*)(cfg->native_code + cfg->code_len);
1869 MonoInst *last_ins = NULL;
1870 guint last_offset = 0;
1875 if (cfg->opt & MONO_OPT_PEEPHOLE)
1876 peephole_pass (cfg, bb);
1878 if (cfg->verbose_level > 2)
1879 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
1881 cpos = bb->max_offset;
1883 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
1889 offset = (guint8*)code - cfg->native_code;
1891 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
1893 if (offset > (cfg->code_size - max_len - 16)) {
1894 cfg->code_size *= 2;
1895 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
1896 code = (guint32*)(cfg->native_code + offset);
1898 // if (ins->cil_code)
1899 // g_print ("cil code\n");
1901 switch (ins->opcode) {
1902 case OP_STOREI1_MEMBASE_IMM:
1903 if (!sparc_is_imm13 (ins->inst_offset))
1905 if (ins->inst_imm == 0)
1906 sparc_stb_imm (code, sparc_g0, ins->inst_destbasereg, ins->inst_offset);
1908 sparc_set (code, ins->inst_imm, sparc_o7);
1909 sparc_stb_imm (code, sparc_o7, ins->inst_destbasereg, ins->inst_offset);
1912 case OP_STOREI2_MEMBASE_IMM:
1913 if (!sparc_is_imm13 (ins->inst_offset))
1915 if (ins->inst_imm == 0)
1916 sparc_sth_imm (code, sparc_g0, ins->inst_destbasereg, ins->inst_offset);
1918 sparc_set (code, ins->inst_imm, sparc_o7);
1919 sparc_sth_imm (code, sparc_o7, ins->inst_destbasereg, ins->inst_offset);
1922 case OP_STORE_MEMBASE_IMM:
1923 case OP_STOREI4_MEMBASE_IMM:
1924 if (!sparc_is_imm13 (ins->inst_offset))
1926 if (ins->inst_imm == 0)
1927 sparc_st_imm (code, sparc_g0, ins->inst_destbasereg, ins->inst_offset);
1929 sparc_set (code, ins->inst_imm, sparc_o7);
1930 sparc_st_imm (code, sparc_o7, ins->inst_destbasereg, ins->inst_offset);
1933 case OP_STOREI1_MEMBASE_REG:
1934 if (!sparc_is_imm13 (ins->inst_offset))
1936 sparc_stb_imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
1938 case OP_STOREI2_MEMBASE_REG:
1939 if (!sparc_is_imm13 (ins->inst_offset))
1941 sparc_sth_imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
1943 case OP_STORE_MEMBASE_REG:
1944 case OP_STOREI4_MEMBASE_REG:
1945 if (!sparc_is_imm13 (ins->inst_offset))
1947 sparc_st_imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
1949 case OP_STOREI8_MEMBASE_REG:
1950 /* Only used by OP_MEMSET */
1951 if (!sparc_is_imm13 (ins->inst_offset))
1953 sparc_std_imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
1958 sparc_ld (code, ins->inst_p0, sparc_g0, ins->dreg);
1960 /* The cast IS BAD (maybe). But it needs to be done... */
1962 sparc_set (code, (guint)ins->inst_p0, ins->dreg);
1963 sparc_ld (code, ins->dreg, sparc_g0, ins->dreg);
1965 case OP_LOAD_MEMBASE:
1966 case OP_LOADI4_MEMBASE:
1967 case OP_LOADU4_MEMBASE:
1968 EMIT_LOAD_MEMBASE (ins, ld);
1970 case OP_LOADU1_MEMBASE:
1971 EMIT_LOAD_MEMBASE (ins, ldub);
1973 case OP_LOADI1_MEMBASE:
1974 EMIT_LOAD_MEMBASE (ins, ldsb);
1976 case OP_LOADU2_MEMBASE:
1977 EMIT_LOAD_MEMBASE (ins, lduh);
1979 case OP_LOADI2_MEMBASE:
1980 EMIT_LOAD_MEMBASE (ins, ldsh);
1983 sparc_sll_imm (code, ins->sreg1, 24, sparc_o7);
1984 sparc_sra_imm (code, sparc_o7, 24, ins->dreg);
1987 sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
1988 sparc_sra_imm (code, sparc_o7, 16, ins->dreg);
1990 /* GCC does this one differently. Don't ask me WHY. */
1992 sparc_and_imm (code, FALSE, ins->sreg1, 0xff, ins->dreg);
1995 sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
1996 sparc_srl_imm (code, sparc_o7, 16, ins->dreg);
1999 sparc_cmp (code, ins->sreg1, ins->sreg2);
2001 case OP_COMPARE_IMM:
2002 if (sparc_is_imm13 (ins->inst_imm))
2003 sparc_cmp_imm (code, ins->sreg1, ins->inst_imm);
2005 sparc_set (code, ins->inst_imm, sparc_o7);
2006 sparc_cmp (code, ins->sreg1, sparc_o7);
2009 case OP_X86_TEST_NULL:
2010 sparc_cmp_imm (code, ins->sreg1, 0);
2016 sparc_add (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2019 sparc_add (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2022 sparc_addx (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2025 EMIT_ALU_IMM (ins, add, FALSE);
2028 EMIT_ALU_IMM (ins, addx, FALSE);
2031 sparc_sub (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2034 sparc_sub (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2037 sparc_subx (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2040 // we add the negated value
2041 if (sparc_is_imm13 (- ins->inst_imm))
2042 sparc_add_imm (code, FALSE, ins->sreg1, -ins->inst_imm, ins->dreg);
2044 sparc_set (code, - ins->inst_imm, sparc_o7);
2045 sparc_add (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2049 EMIT_ALU_IMM (ins, subx, FALSE);
2052 sparc_and (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2055 EMIT_ALU_IMM (ins, and, FALSE);
2058 /* Sign extend sreg1 into %y */
2059 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2060 sparc_wry (code, sparc_o7, sparc_g0);
2061 sparc_sdiv (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2064 sparc_wry (code, sparc_g0, sparc_g0);
2065 sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2068 /* Sign extend sreg1 into %y */
2069 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2070 sparc_wry (code, sparc_o7, sparc_g0);
2071 EMIT_ALU_IMM (ins, sdiv, FALSE);
2074 /* Sign extend sreg1 into %y */
2075 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2076 sparc_wry (code, sparc_o7, sparc_g0);
2077 sparc_sdiv (code, FALSE, ins->sreg1, ins->sreg2, sparc_o7);
2078 sparc_smul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
2079 sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2082 sparc_wry (code, sparc_g0, sparc_g0);
2083 sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, sparc_o7);
2084 sparc_umul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
2085 sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2088 /* Sign extend sreg1 into %y */
2089 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2090 sparc_wry (code, sparc_o7, sparc_g0);
2091 if (!sparc_is_imm13 (ins->inst_imm))
2093 sparc_sdiv_imm (code, FALSE, ins->sreg1, ins->inst_imm, sparc_o7);
2094 sparc_smul_imm (code, FALSE, sparc_o7, ins->inst_imm, sparc_o7);
2095 sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2098 sparc_or (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2101 EMIT_ALU_IMM (ins, or, FALSE);
2104 sparc_xor (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2107 EMIT_ALU_IMM (ins, xor, FALSE);
2110 sparc_sll (code, ins->sreg1, ins->sreg2, ins->dreg);
2113 if (sparc_is_imm13 (ins->inst_imm))
2114 sparc_sll_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2116 sparc_set (code, ins->inst_imm, sparc_o7);
2117 sparc_sll (code, ins->sreg1, sparc_o7, ins->dreg);
2121 sparc_sra (code, ins->sreg1, ins->sreg2, ins->dreg);
2124 if (sparc_is_imm13 (ins->inst_imm))
2125 sparc_sra_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2127 sparc_set (code, ins->inst_imm, sparc_o7);
2128 sparc_sra (code, ins->sreg1, sparc_o7, ins->dreg);
2132 if (sparc_is_imm13 (ins->inst_imm))
2133 sparc_srl_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2135 sparc_set (code, ins->inst_imm, sparc_o7);
2136 sparc_srl (code, ins->sreg1, sparc_o7, ins->dreg);
2140 sparc_srl (code, ins->sreg1, ins->sreg2, ins->dreg);
2143 /* can't use sparc_not */
2144 sparc_xnor (code, FALSE, ins->sreg1, sparc_g0, ins->dreg);
2147 /* can't use sparc_neg */
2148 sparc_sub (code, FALSE, sparc_g0, ins->sreg1, ins->dreg);
2151 sparc_smul (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2154 EMIT_ALU_IMM (ins, smul, FALSE);
2158 sparc_smul (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2160 case CEE_MUL_OVF_UN:
2162 sparc_umul (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2166 sparc_set (code, ins->inst_c0, ins->dreg);
2172 if (ins->sreg1 != ins->dreg)
2173 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
2176 g_assert_not_reached ();
2179 /* ensure ins->sreg1 is not NULL */
2180 sparc_cmp_imm (code, ins->sreg1, 0);
2187 call = (MonoCallInst*)ins;
2188 if (ins->flags & MONO_INST_HAS_METHOD)
2189 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2191 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2192 sparc_call_simple (code, 0);
2195 code = emit_move_return_value (ins, code);
2200 case OP_VOIDCALL_REG:
2202 call = (MonoCallInst*)ins;
2203 sparc_jmpl (code, ins->sreg1, sparc_g0, sparc_callsite);
2206 code = emit_move_return_value (ins, code);
2208 case OP_FCALL_MEMBASE:
2209 case OP_LCALL_MEMBASE:
2210 case OP_VCALL_MEMBASE:
2211 case OP_VOIDCALL_MEMBASE:
2212 case OP_CALL_MEMBASE:
2213 call = (MonoCallInst*)ins;
2214 g_assert (sparc_is_imm13 (ins->inst_offset));
2216 sparc_ld_imm (code, ins->inst_basereg, ins->inst_offset, sparc_o7);
2217 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
2220 code = emit_move_return_value (ins, code);
2223 g_assert_not_reached ();
2229 /* The return is done in the epilog */
2230 g_assert_not_reached ();
2233 sparc_unimp (code, 0);
2240 case CEE_ENDFINALLY:
2243 case OP_CALL_HANDLER:
2247 ins->inst_c0 = (guint8*)code - cfg->native_code;
2250 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2251 if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2253 if (ins->flags & MONO_INST_BRLABEL) {
2254 if (ins->inst_i0->inst_c0) {
2255 gint32 disp = (ins->inst_i0->inst_c0 - ((guint8*)code - cfg->native_code)) >> 2;
2256 g_assert (sparc_is_imm22 (disp));
2257 sparc_branch (code, 1, sparc_ba, disp);
2259 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2260 sparc_branch (code, 1, sparc_ba, 0);
2263 if (ins->inst_target_bb->native_offset) {
2264 gint32 disp = (ins->inst_target_bb->native_offset - ((guint8*)code - cfg->native_code)) >> 2;
2265 g_assert (sparc_is_imm22 (disp));
2266 sparc_branch (code, 1, sparc_ba, disp);
2268 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2269 sparc_branch (code, 1, sparc_ba, 0);
2275 sparc_jmp (code, ins->sreg1, sparc_g0);
2283 sparc_clr_reg (code, ins->dreg);
2284 sparc_branch (code, 1, opcode_to_sparc_cond (ins->opcode), 2);
2286 sparc_set (code, 1, ins->dreg);
2288 case OP_COND_EXC_EQ:
2289 case OP_COND_EXC_NE_UN:
2290 case OP_COND_EXC_LT:
2291 case OP_COND_EXC_LT_UN:
2292 case OP_COND_EXC_GT:
2293 case OP_COND_EXC_GT_UN:
2294 case OP_COND_EXC_GE:
2295 case OP_COND_EXC_GE_UN:
2296 case OP_COND_EXC_LE:
2297 case OP_COND_EXC_LE_UN:
2298 case OP_COND_EXC_OV:
2299 case OP_COND_EXC_NO:
2301 case OP_COND_EXC_NC:
2303 //EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
2304 // (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
2316 EMIT_COND_BRANCH (ins, opcode_to_sparc_cond (ins->opcode));
2319 /* floating point opcodes */
2321 double d = *(double*)ins->inst_p0;
2323 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
2324 sparc_sethi (code, 0, sparc_o7);
2325 sparc_lddf_imm (code, sparc_o7, 0, ins->dreg);
2329 float f = *(float*)ins->inst_p0;
2331 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
2332 sparc_sethi (code, 0, sparc_o7);
2333 sparc_ldf_imm (code, sparc_o7, 0, ins->dreg);
2335 /* Extend to double */
2336 sparc_fstod (code, ins->dreg, ins->dreg);
2339 case OP_STORER8_MEMBASE_REG:
2340 if (!sparc_is_imm13 (ins->inst_offset + 4))
2342 if (ins->inst_offset % 8) {
2344 sparc_stf_imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
2345 sparc_stf_imm (code, ins->sreg1 + 1, ins->inst_destbasereg, ins->inst_offset + 4);
2347 sparc_stdf_imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
2349 case OP_LOADR8_MEMBASE:
2350 g_assert ((ins->inst_offset % 8) == 0);
2351 EMIT_LOAD_MEMBASE (ins, lddf);
2353 case OP_STORER4_MEMBASE_REG:
2354 /* This requires a double->single conversion */
2355 sparc_fdtos (code, ins->sreg1, sparc_f0);
2356 if (!sparc_is_imm13 (ins->inst_offset))
2358 sparc_stf_imm (code, sparc_f0, ins->inst_destbasereg, ins->inst_offset);
2360 case OP_LOADR4_MEMBASE:
2361 EMIT_LOAD_MEMBASE (ins, ldf);
2362 /* Extend to double */
2363 sparc_fstod (code, ins->dreg, ins->dreg);
2366 sparc_fmovs (code, ins->sreg1, ins->dreg);
2367 sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
2370 sparc_st_imm (code, ins->sreg1, sparc_sp, cfg->stack_offset - 8);
2371 sparc_ldf_imm (code, sparc_sp, cfg->stack_offset - 8, sparc_f0);
2372 sparc_fitos (code, sparc_f0, sparc_f0);
2373 sparc_fstod (code, sparc_f0, ins->dreg);
2378 case OP_FCONV_TO_I1:
2381 case OP_FCONV_TO_U1:
2384 case OP_FCONV_TO_I2:
2387 case OP_FCONV_TO_U2:
2390 case OP_FCONV_TO_I4:
2392 sparc_fdtoi (code, ins->sreg1, sparc_f0);
2393 sparc_stdf_imm (code, sparc_f0, sparc_sp, cfg->stack_offset - 8);
2394 sparc_ld_imm (code, sparc_sp, cfg->stack_offset - 8, ins->dreg);
2396 case OP_FCONV_TO_U4:
2400 case OP_FCONV_TO_I8:
2401 case OP_FCONV_TO_U8:
2404 case OP_LCONV_TO_R_UN: {
2408 case OP_LCONV_TO_OVF_I: {
2409 guint32 *br [3], *label [1];
2412 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
2414 sparc_cmp_imm (code, ins->sreg1, 0);
2416 sparc_branch (code, 1, sparc_bneg, 0);
2420 /* ms word must be 0 */
2421 sparc_cmp_imm (code, ins->sreg2, 0);
2423 sparc_branch (code, 1, sparc_be, 0);
2427 /* FIXME: throw exception */
2430 sparc_patch (br [0], code);
2432 /* ms word must 0xfffffff */
2433 sparc_cmp_imm (code, ins->sreg2, -1);
2434 sparc_branch (code, 1, sparc_bne, label [0]);
2437 sparc_patch (br [1], code);
2438 if (ins->sreg1 != ins->dreg)
2439 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
2443 sparc_faddd (code, ins->sreg1, ins->sreg2, ins->dreg);
2446 sparc_fsubd (code, ins->sreg1, ins->sreg2, ins->dreg);
2449 sparc_fmuld (code, ins->sreg1, ins->sreg2, ins->dreg);
2452 sparc_fdivd (code, ins->sreg1, ins->sreg2, ins->dreg);
2455 sparc_fnegs (code, ins->sreg1, ins->dreg);
2458 sparc_fdivd (code, ins->sreg1, ins->sreg2, sparc_f0);
2459 sparc_fmuld (code, ins->sreg2, sparc_f0, sparc_f0);
2460 sparc_fsubd (code, ins->sreg1, sparc_f0, ins->dreg);
2463 sparc_fcmpd (code, ins->sreg1, ins->sreg2);
2470 sparc_fcmpd (code, ins->sreg1, ins->sreg2);
2471 sparc_clr_reg (code, ins->dreg);
2472 switch (ins->opcode) {
2475 sparc_fbranch (code, 1, opcode_to_sparc_cond (ins->opcode), 4);
2477 sparc_set (code, 1, ins->dreg);
2478 sparc_fbranch (code, 1, sparc_fbu, 2);
2480 sparc_set (code, 1, ins->dreg);
2483 sparc_fbranch (code, 1, opcode_to_sparc_cond (ins->opcode), 2);
2485 sparc_set (code, 1, ins->dreg);
2491 EMIT_FLOAT_COND_BRANCH (ins, opcode_to_sparc_cond (ins->opcode));
2494 /* clt.un + brfalse */
2496 sparc_fbranch (code, 1, sparc_fbul, 0);
2499 EMIT_FLOAT_COND_BRANCH (ins, sparc_fba);
2500 sparc_patch ((guint8*)p, (guint8*)code);
2504 /* cgt.un + brfalse */
2506 sparc_fbranch (code, 1, sparc_fbug, 0);
2509 EMIT_FLOAT_COND_BRANCH (ins, sparc_fba);
2510 sparc_patch ((guint8*)p, (guint8*)code);
2514 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbne);
2515 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu);
2518 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbl);
2519 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu);
2522 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbg);
2523 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu);
2526 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbge);
2527 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu);
2530 EMIT_FLOAT_COND_BRANCH (ins, sparc_fble);
2531 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu);
2533 case CEE_CKFINITE: {
2539 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
2541 g_warning ("%s:%d: unknown opcode %s\n", __FILE__, __LINE__, mono_inst_name (ins->opcode));
2543 g_assert_not_reached ();
2546 if ((cfg->opt & MONO_OPT_BRANCH) && (((guint8*)code - cfg->native_code - offset) > max_len)) {
2547 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
2548 mono_inst_name (ins->opcode), max_len, (guint8*)code - cfg->native_code - offset);
2549 g_assert_not_reached ();
2555 last_offset = offset;
2560 cfg->code_len = (guint8*)code - cfg->native_code;
2564 mono_arch_register_lowlevel_calls (void)
2566 mono_register_jit_icall (enter_method, "mono_enter_method", NULL, TRUE);
2567 mono_register_jit_icall (leave_method, "mono_leave_method", NULL, TRUE);
2571 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
2573 MonoJumpInfo *patch_info;
2575 /* FIXME: Move part of this to arch independent code */
2576 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
2577 unsigned char *ip = patch_info->ip.i + code;
2578 const unsigned char *target = NULL;
2580 switch (patch_info->type) {
2581 case MONO_PATCH_INFO_BB:
2582 target = patch_info->data.bb->native_offset + code;
2584 case MONO_PATCH_INFO_ABS:
2585 target = patch_info->data.target;
2587 case MONO_PATCH_INFO_LABEL:
2588 target = patch_info->data.inst->inst_c0 + code;
2590 case MONO_PATCH_INFO_IP:
2591 *((gpointer *)(ip)) = ip;
2593 case MONO_PATCH_INFO_METHOD_REL:
2595 *((gpointer *)(ip)) = code + patch_info->data.offset;
2597 case MONO_PATCH_INFO_INTERNAL_METHOD: {
2598 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
2600 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
2601 g_assert_not_reached ();
2603 target = mono_icall_get_wrapper (mi);
2606 case MONO_PATCH_INFO_METHOD_JUMP: {
2609 /* get the trampoline to the method from the domain */
2610 target = mono_arch_create_jump_trampoline (patch_info->data.method);
2611 if (!domain->jump_target_hash)
2612 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
2613 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
2614 list = g_slist_prepend (list, ip);
2615 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
2618 case MONO_PATCH_INFO_METHOD:
2619 if (patch_info->data.method == method) {
2622 /* get the trampoline to the method from the domain */
2623 target = mono_arch_create_jit_trampoline (patch_info->data.method);
2625 case MONO_PATCH_INFO_SWITCH: {
2626 guint32 *p = (guint32*)ip;
2627 gpointer *jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->table_size);
2630 target = jump_table;
2632 for (i = 0; i < patch_info->table_size; i++) {
2633 jump_table [i] = code + (int)patch_info->data.table [i];
2637 case MONO_PATCH_INFO_METHODCONST:
2638 case MONO_PATCH_INFO_CLASS:
2639 case MONO_PATCH_INFO_IMAGE:
2640 case MONO_PATCH_INFO_FIELD:
2642 *((gconstpointer *)(ip + 1)) = patch_info->data.target;
2644 case MONO_PATCH_INFO_IID:
2646 mono_class_init (patch_info->data.klass);
2647 *((guint32 *)(ip + 1)) = patch_info->data.klass->interface_id;
2649 case MONO_PATCH_INFO_VTABLE:
2651 *((gconstpointer *)(ip + 1)) = mono_class_vtable (domain, patch_info->data.klass);
2653 case MONO_PATCH_INFO_CLASS_INIT: {
2654 /* Might already been changed to a nop */
2655 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
2658 case MONO_PATCH_INFO_SFLDA: {
2659 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
2660 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
2661 /* Done by the generated code */
2665 mono_runtime_class_init (vtable);
2668 *((gconstpointer *)(ip + 1)) =
2669 (char*)vtable->data + patch_info->data.field->offset;
2672 case MONO_PATCH_INFO_R4: {
2673 float *f = g_new0 (float, 1);
2674 *f = *(float*)patch_info->data.target;
2678 case MONO_PATCH_INFO_R8: {
2679 double *d = g_new0 (double, 1);
2680 *d = *(double*)patch_info->data.target;
2684 case MONO_PATCH_INFO_EXC_NAME:
2686 *((gconstpointer *)(ip + 1)) = patch_info->data.name;
2688 case MONO_PATCH_INFO_LDSTR:
2690 *((gconstpointer *)(ip + 1)) =
2691 mono_ldstr (domain, patch_info->data.token->image,
2692 mono_metadata_token_index (patch_info->data.token->token));
2694 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
2696 MonoClass *handle_class;
2698 handle = mono_ldtoken (patch_info->data.token->image,
2699 patch_info->data.token->token, &handle_class);
2700 mono_class_init (handle_class);
2701 mono_class_init (mono_class_from_mono_type (handle));
2704 *((gconstpointer *)(ip + 1)) =
2705 mono_type_get_object (domain, handle);
2708 case MONO_PATCH_INFO_LDTOKEN: {
2710 MonoClass *handle_class;
2712 handle = mono_ldtoken (patch_info->data.token->image,
2713 patch_info->data.token->token, &handle_class);
2714 mono_class_init (handle_class);
2717 *((gconstpointer *)(ip + 1)) = handle;
2721 g_assert_not_reached ();
2723 sparc_patch (ip, target);
2728 * Allow tracing to work with this interface (with an optional argument)
2732 * This may be needed on some archs or for debugging support.
2735 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
2737 /* no stack room needed now (may be needed for FASTCALL-trace support) */
2739 /* split prolog-epilog requirements? */
2740 *code = 256; /* max bytes needed: check this number */
2744 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
2746 int stack, code_size;
2747 guint32 *code = (guint32*)p;
2749 /* Save registers to stack */
2750 sparc_st_imm (code, sparc_i0, sparc_fp, 68);
2751 sparc_st_imm (code, sparc_i1, sparc_fp, 72);
2752 sparc_st_imm (code, sparc_i2, sparc_fp, 76);
2753 sparc_st_imm (code, sparc_i3, sparc_fp, 80);
2754 sparc_st_imm (code, sparc_i4, sparc_fp, 84);
2756 sparc_set (code, cfg->method, sparc_o0);
2757 sparc_mov_reg_reg (code, sparc_fp, sparc_o1);
2759 mono_add_patch_info (cfg, (guint8*)code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
2760 sparc_sethi (code, 0, sparc_o7);
2761 sparc_jmpl_imm (code, sparc_o7, 0, sparc_callsite);
2764 mono_arch_instrument_mem_needs (cfg->method, &stack, &code_size);
2766 g_assert ((code - (guint32*)p) <= (code_size * 4));
2780 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
2783 int arg_size = 0, save_mode = SAVE_NONE;
2784 MonoMethod *method = cfg->method;
2785 int rtype = method->signature->ret->type;
2789 case MONO_TYPE_VOID:
2790 /* special case string .ctor icall */
2791 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
2792 save_mode = SAVE_ONE;
2794 save_mode = SAVE_NONE;
2798 save_mode = SAVE_TWO;
2802 save_mode = SAVE_FP;
2804 case MONO_TYPE_VALUETYPE:
2805 if (method->signature->ret->data.klass->enumtype) {
2806 rtype = method->signature->ret->data.klass->enum_basetype->type;
2809 save_mode = SAVE_STRUCT;
2812 save_mode = SAVE_ONE;
2816 /* Save the result to the stack and also put it into the output registers */
2818 switch (save_mode) {
2820 sparc_st_imm (code, sparc_i0, sparc_fp, 68);
2821 sparc_st_imm (code, sparc_i0, sparc_fp, 72);
2822 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
2823 sparc_mov_reg_reg (code, sparc_i1, sparc_o2);
2826 sparc_st_imm (code, sparc_i0, sparc_fp, 68);
2827 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
2830 sparc_stdf (code, sparc_f0, sparc_fp, 72);
2831 sparc_ld_imm (code, sparc_fp, 72, sparc_o1);
2832 sparc_ld_imm (code, sparc_fp, 72, sparc_o2);
2835 sparc_ld_imm (code, sparc_fp, 64, sparc_o1);
2842 sparc_set (code, cfg->method, sparc_o0);
2844 mono_add_patch_info (cfg, (guint8*)code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
2845 sparc_sethi (code, 0, sparc_o7);
2846 sparc_jmpl_imm (code, sparc_o7, 0, sparc_callsite);
2849 /* Restore result */
2851 switch (save_mode) {
2853 sparc_ld_imm (code, sparc_fp, 68, sparc_i0);
2854 sparc_ld_imm (code, sparc_fp, 72, sparc_i0);
2857 sparc_ld_imm (code, sparc_fp, 68, sparc_i0);
2860 sparc_lddf_imm (code, sparc_fp, 72, sparc_f0);
2871 mono_arch_max_epilog_size (MonoCompile *cfg)
2873 int exc_count = 0, max_epilog_size = 16 + 20*4;
2874 MonoJumpInfo *patch_info;
2876 if (cfg->method->save_lmf)
2877 max_epilog_size += 128;
2879 if (mono_jit_trace_calls != NULL)
2880 max_epilog_size += 50;
2882 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2883 max_epilog_size += 50;
2885 /* count the number of exception infos */
2887 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2888 if (patch_info->type == MONO_PATCH_INFO_EXC)
2893 * make sure we have enough space for exceptions
2894 * 16 is the size of two push_imm instructions and a call
2896 max_epilog_size += exc_count*16;
2898 return max_epilog_size;
2902 mono_arch_emit_prolog (MonoCompile *cfg)
2904 MonoMethod *method = cfg->method;
2906 MonoMethodSignature *sig;
2908 int alloc_size, pos, max_offset, i;
2912 cfg->code_size = 256;
2913 code = cfg->native_code = g_malloc (cfg->code_size);
2915 sparc_save_imm (code, sparc_sp, - cfg->stack_offset, sparc_sp);
2917 sig = method->signature;
2922 cinfo = get_call_info (sig, FALSE);
2924 for (i = 0; i < sig->param_count; ++i) {
2925 ArgInfo *ainfo = cinfo->args + pos;
2926 guint32 stack_offset;
2927 inst = cfg->varinfo [pos];
2929 stack_offset = ainfo->offset + 68;
2931 /* Save the split arguments so they will reside entirely on the stack */
2932 if (ainfo->storage == ArgInSplitRegStack) {
2933 /* Save the register to the stack */
2934 g_assert (inst->opcode == OP_REGOFFSET);
2935 if (!sparc_is_imm13 (stack_offset))
2937 sparc_st_imm (code, sparc_i5, inst->inst_basereg, stack_offset);
2940 if (sig->params [i]->type == MONO_TYPE_R8) {
2941 /* Save the argument to a dword aligned stack location */
2943 * stack_offset contains the offset of the argument on the stack.
2944 * inst->inst_offset contains the dword aligned offset where the value
2947 if (ainfo->storage == ArgInIRegPair) {
2948 if (!sparc_is_imm13 (inst->inst_offset + 4))
2950 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, inst->inst_offset);
2951 sparc_st_imm (code, sparc_i0 + ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
2954 if (ainfo->storage == ArgInSplitRegStack) {
2955 if (stack_offset != inst->inst_offset) {
2956 /* stack_offset is not dword aligned, so we need to make a copy */
2957 sparc_st_imm (code, sparc_i5, inst->inst_basereg, inst->inst_offset);
2958 sparc_ld_imm (code, sparc_fp, stack_offset + 4, sparc_o7);
2959 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset + 4);
2963 if (ainfo->storage == ArgOnStackPair) {
2964 if (stack_offset != inst->inst_offset) {
2965 /* stack_offset is not dword aligned, so we need to make a copy */
2966 sparc_ld_imm (code, sparc_fp, stack_offset, sparc_o7);
2967 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset);
2968 sparc_ld_imm (code, sparc_fp, stack_offset + 4, sparc_o7);
2969 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset + 4);
2973 g_assert_not_reached ();
2976 if ((ainfo->storage == ArgInIReg) && (inst->opcode != OP_REGVAR)) {
2977 /* Argument in register, but need to be saved to stack */
2978 if (!sparc_is_imm13 (stack_offset))
2980 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
2983 if ((ainfo->storage == ArgInIRegPair) && (inst->opcode != OP_REGVAR))
2991 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
2992 code = mono_arch_instrument_prolog (cfg, enter_method, code, TRUE);
2994 cfg->code_len = code - cfg->native_code;
2996 g_assert (cfg->code_len <= cfg->code_size);
3002 mono_arch_emit_epilog (MonoCompile *cfg)
3004 MonoJumpInfo *patch_info;
3005 MonoMethod *method = cfg->method;
3009 code = cfg->native_code + cfg->code_len;
3011 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3012 code = mono_arch_instrument_epilog (cfg, leave_method, code, TRUE);
3015 sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
3022 if (method->save_lmf) {
3023 pos = -sizeof (MonoLMF);
3026 if (method->save_lmf) {
3028 /* ebx = previous_lmf */
3029 x86_pop_reg (code, X86_EBX);
3031 x86_pop_reg (code, X86_EDI);
3032 /* *(lmf) = previous_lmf */
3033 x86_mov_membase_reg (code, X86_EDI, 0, X86_EBX, 4);
3035 /* discard method info */
3036 x86_pop_reg (code, X86_ESI);
3038 /* restore caller saved regs */
3039 x86_pop_reg (code, X86_EBP);
3040 x86_pop_reg (code, X86_ESI);
3041 x86_pop_reg (code, X86_EDI);
3042 x86_pop_reg (code, X86_EBX);
3046 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3047 //ppc_lwz (code, sparc_l0, cfg->stack_usage + 8, cfg->frame_reg);
3048 //ppc_mtlr (code, sparc_l0);
3050 //ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
3051 for (i = 13; i < 32; ++i) {
3052 if (cfg->used_int_regs & (1 << i)) {
3054 //ppc_lwz (code, i, -pos, cfg->frame_reg);
3059 /* add code to raise exceptions */
3060 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3061 switch (patch_info->type) {
3062 case MONO_PATCH_INFO_EXC:
3063 /*x86_patch (patch_info->ip.i + cfg->native_code, code);
3064 x86_push_imm (code, patch_info->data.target);
3065 x86_push_imm (code, patch_info->ip.i + cfg->native_code);
3066 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3067 patch_info->data.name = "throw_exception_by_name";
3068 patch_info->ip.i = code - cfg->native_code;
3069 x86_jump_code (code, 0);*/
3078 cfg->code_len = code - cfg->native_code;
3080 g_assert (cfg->code_len < cfg->code_size);
3085 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
3090 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
3092 /* add the this argument */
3093 if (this_reg != -1) {
3095 MONO_INST_NEW (cfg, this, OP_SETREG);
3096 this->type = this_type;
3097 this->sreg1 = this_reg;
3098 this->dreg = sparc_o0;
3099 mono_bblock_add_inst (cfg->cbb, this);
3103 /* Set the 'struct/union return pointer' location on the stack */
3104 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, sparc_sp, 64, vt_reg);
3110 mono_arch_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3119 } MonoJitArgumentInfo;
3122 * arch_get_argument_info:
3123 * @csig: a method signature
3124 * @param_count: the number of parameters to consider
3125 * @arg_info: an array to store the result infos
3127 * Gathers information on parameters such as size, alignment and
3128 * padding. arg_info should be large enought to hold param_count + 1 entries.
3130 * Returns the size of the activation frame.
3133 arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
3135 int k, frame_size = 0;
3136 int size, align, pad;
3141 cinfo = get_call_info (csig, FALSE);
3143 if (csig->hasthis) {
3144 ainfo = &cinfo->args [0];
3145 arg_info [0].offset = 68 + ainfo->offset;
3148 for (k = 0; k < param_count; k++) {
3149 ainfo = &cinfo->args [k + csig->hasthis];
3151 arg_info [k + 1].offset = 68 + ainfo->offset;
3152 arg_info [k + 1].size = mono_type_size (csig->params [k], &align);
3161 static int indent_level = 0;
3163 static void indent (int diff) {
3166 indent_level += diff;
3172 indent_level += diff;
3175 static gboolean enable_trace = TRUE;
3178 enter_method (MonoMethod *method, char *ebp)
3183 MonoJitArgumentInfo *arg_info;
3184 MonoMethodSignature *sig;
3187 /* FIXME: move to arch independent code */
3192 fname = mono_method_full_name (method, TRUE);
3194 printf ("ENTER: %s(", fname);
3197 if (((int)ebp & (MONO_ARCH_FRAME_ALIGNMENT - 1)) != 0) {
3198 g_error ("unaligned stack detected (%p)", ebp);
3201 sig = method->signature;
3203 arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
3205 arch_get_argument_info (sig, sig->param_count, arg_info);
3207 if (MONO_TYPE_ISSTRUCT (method->signature->ret)) {
3208 g_assert (!method->signature->ret->byref);
3210 printf ("VALUERET:%p, ", *((gpointer *)(ebp + 8)));
3213 if (method->signature->hasthis) {
3214 gpointer *this = (gpointer *)(ebp + arg_info [0].offset);
3215 if (method->klass->valuetype) {
3216 printf ("value:%p, ", *this);
3218 o = *((MonoObject **)this);
3221 class = o->vtable->klass;
3223 if (class == mono_defaults.string_class) {
3224 printf ("this:[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
3226 printf ("this:%p[%s.%s %s], ", o, class->name_space, class->name, o->vtable->domain->friendly_name);
3229 printf ("this:NULL, ");
3233 for (i = 0; i < method->signature->param_count; ++i) {
3234 gpointer *cpos = (gpointer *)(ebp + arg_info [i + 1].offset);
3235 int size = arg_info [i + 1].size;
3237 MonoType *type = method->signature->params [i];
3240 printf ("[BYREF:%p], ", *cpos);
3241 } else switch (type->type) {
3245 printf ("%p, ", (gpointer)*((int *)(cpos)));
3247 case MONO_TYPE_BOOLEAN:
3248 case MONO_TYPE_CHAR:
3255 printf ("%d, ", *((int *)(cpos)));
3257 case MONO_TYPE_STRING: {
3258 MonoString *s = *((MonoString **)cpos);
3260 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
3261 printf ("[STRING:%p:%s], ", s, mono_string_to_utf8 (s));
3263 printf ("[STRING:null], ");
3266 case MONO_TYPE_CLASS:
3267 case MONO_TYPE_OBJECT: {
3268 o = *((MonoObject **)cpos);
3270 class = o->vtable->klass;
3272 if (class == mono_defaults.string_class) {
3273 printf ("[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
3274 } else if (class == mono_defaults.int32_class) {
3275 printf ("[INT32:%p:%d], ", o, *(gint32 *)((char *)o + sizeof (MonoObject)));
3277 printf ("[%s.%s:%p], ", class->name_space, class->name, o);
3279 printf ("%p, ", *((gpointer *)(cpos)));
3284 case MONO_TYPE_FNPTR:
3285 case MONO_TYPE_ARRAY:
3286 case MONO_TYPE_SZARRAY:
3287 printf ("%p, ", *((gpointer *)(cpos)));
3291 printf ("0x%016llx, ", *((gint64 *)(cpos)));
3294 printf ("%f, ", *((float *)(cpos)));
3297 printf ("%f, ", *((double *)(cpos)));
3299 case MONO_TYPE_VALUETYPE:
3301 for (j = 0; j < size; j++)
3302 printf ("%02x,", *((guint8*)cpos +j));
3314 leave_method (MonoMethod *method, ...)
3323 va_start(ap, method);
3325 fname = mono_method_full_name (method, TRUE);
3327 printf ("LEAVE: %s", fname);
3330 type = method->signature->ret;
3333 switch (type->type) {
3334 case MONO_TYPE_VOID:
3336 case MONO_TYPE_BOOLEAN: {
3337 int eax = va_arg (ap, int);
3339 printf ("TRUE:%d", eax);
3345 case MONO_TYPE_CHAR:
3354 int eax = va_arg (ap, int);
3355 printf ("RES=%d (0x%x)", eax, eax);
3358 case MONO_TYPE_STRING: {
3359 MonoString *s = va_arg (ap, MonoString *);
3362 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
3363 printf ("[STRING:%p:%s]", s, mono_string_to_utf8 (s));
3365 printf ("[STRING:null], ");
3368 case MONO_TYPE_CLASS:
3369 case MONO_TYPE_OBJECT: {
3370 MonoObject *o = va_arg (ap, MonoObject *);
3373 if (o->vtable->klass == mono_defaults.boolean_class) {
3374 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
3375 } else if (o->vtable->klass == mono_defaults.int32_class) {
3376 printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));
3377 } else if (o->vtable->klass == mono_defaults.int64_class) {
3378 printf ("[INT64:%p:%lld]", o, *((gint64 *)((char *)o + sizeof (MonoObject))));
3380 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
3382 printf ("[OBJECT:%p]", o);
3387 case MONO_TYPE_FNPTR:
3388 case MONO_TYPE_ARRAY:
3389 case MONO_TYPE_SZARRAY: {
3390 gpointer p = va_arg (ap, gpointer);
3391 printf ("EAX=%p", p);
3394 case MONO_TYPE_I8: {
3395 gint64 l = va_arg (ap, gint64);
3396 printf ("EAX/EDX=0x%16llx", l);
3399 case MONO_TYPE_U8: {
3400 gint64 l = va_arg (ap, gint64);
3401 printf ("EAX/EDX=0x%16llx", l);
3404 case MONO_TYPE_R8: {
3405 double f = va_arg (ap, double);
3406 printf ("FP=%f\n", f);
3409 case MONO_TYPE_VALUETYPE:
3410 if (type->data.klass->enumtype) {
3411 type = type->data.klass->enum_basetype;
3414 guint8 *p = va_arg (ap, gpointer);
3416 size = mono_type_size (type, &align);
3418 for (j = 0; p && j < size; j++)
3419 printf ("%02x,", p [j]);
3424 printf ("(unknown return type %x)", method->signature->ret->type);