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.
21 #include <sys/systeminfo.h>
28 #include <mono/metadata/appdomain.h>
29 #include <mono/metadata/debug-helpers.h>
30 #include <mono/metadata/tokentype.h>
31 #include <mono/utils/mono-math.h>
33 #include "mini-sparc.h"
36 #include "cpu-sparc.h"
39 * Sparc V9 means two things:
40 * - the instruction set
43 * V9 instructions are only usable if the underlying processor is 64 bit. Most Sparc
44 * processors in use are 64 bit processors. The V9 ABI is only usable if the
45 * mono executable is a 64 bit executable. So it would make sense to use the 64 bit
46 * instructions without using the 64 bit ABI.
51 * - %i0..%i<n> hold the incoming arguments, these are never written by JITted
52 * code. Unused input registers are used for global register allocation.
53 * - %o0..%o5 and %l7 is used for local register allocation and passing arguments
54 * - %l0..%l6 is used for global register allocation
55 * - %o7 and %g1 is used as scratch registers in opcodes
56 * - all floating point registers are used for local register allocation except %f0.
57 * Only double precision registers are used.
59 * - fp registers %d0..%d30 are used for parameter passing, and %d32..%d62 are
60 * used for local allocation.
65 * - doubles and longs must be stored in dword aligned locations
69 * The following things are not implemented or do not work:
70 * - some fp arithmetic corner cases
71 * The following tests in mono/mini are expected to fail:
72 * - test_0_simple_double_casts
73 * This test casts (guint64)-1 to double and then back to guint64 again.
74 * Under x86, it returns 0, while under sparc it returns -1.
76 * In addition to this, the runtime requires the trunc function, or its
77 * solaris counterpart, aintl, to do some double->int conversions. If this
78 * function is not available, it is emulated somewhat, but the results can be
84 * - optimize sparc_set according to the memory model
85 * - when non-AOT compiling, compute patch targets immediately so we don't
86 * have to emit the 6 byte template.
88 * - struct arguments/returns
93 * - sparc_call_simple can't be used in a lot of places since the displacement
94 * might not fit into an imm30.
95 * - g1 can't be used in a lot of places since it is used as a scratch reg in
97 * - sparc_f0 can't be used as a scratch register on V9
98 * - the %d34..%d62 fp registers are encoded as: %dx = %f(x - 32 + 1), ie.
100 * - ldind.i4/u4 needs to sign extend/clear out upper word -> slows things down
101 * - ins->dreg can't be used as a scatch register in r4 opcodes since it might
102 * be a double precision register which has no single precision part.
103 * - passing/returning structs is hard to implement, because:
104 * - the spec is very hard to understand
105 * - it requires knowledge about the fields of structure, needs to handle
106 * nested structures etc.
110 * Possible optimizations:
111 * - delay slot scheduling
112 * - allocate large constants to registers
113 * - add more mul/div/rem optimizations
117 #define MONO_SPARC_THR_TLS 1
121 * There was a 64 bit bug in glib-2.2: g_bit_nth_msf (0, -1) would return 32,
122 * causing infinite loops in dominator computation. So glib-2.4 is required.
125 #if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 4
126 #error "glib 2.4 or later is required for 64 bit mode."
130 #define NOT_IMPLEMENTED do { g_assert_not_reached (); } while (0)
132 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
134 #define SIGNAL_STACK_SIZE (64 * 1024)
136 #define STACK_BIAS MONO_SPARC_STACK_BIAS
140 /* %g1 is used by sparc_set */
141 #define GP_SCRATCH_REG sparc_g4
142 /* %f0 is used for parameter passing */
143 #define FP_SCRATCH_REG sparc_f30
144 #define ARGS_OFFSET (STACK_BIAS + 128)
148 #define FP_SCRATCH_REG sparc_f0
149 #define ARGS_OFFSET 68
150 #define GP_SCRATCH_REG sparc_g1
154 /* Whenever the CPU supports v9 instructions */
155 static gboolean sparcv9 = FALSE;
157 /* Whenever this is a 64bit executable */
159 static gboolean v64 = TRUE;
161 static gboolean v64 = FALSE;
164 static gpointer mono_arch_get_lmf_addr (void);
167 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar);
170 mono_arch_regname (int reg) {
171 static const char * rnames[] = {
172 "sparc_g0", "sparc_g1", "sparc_g2", "sparc_g3", "sparc_g4",
173 "sparc_g5", "sparc_g6", "sparc_g7", "sparc_o0", "sparc_o1",
174 "sparc_o2", "sparc_o3", "sparc_o4", "sparc_o5", "sparc_sp",
175 "sparc_call", "sparc_l0", "sparc_l1", "sparc_l2", "sparc_l3",
176 "sparc_l4", "sparc_l5", "sparc_l6", "sparc_l7", "sparc_i0",
177 "sparc_i1", "sparc_i2", "sparc_i3", "sparc_i4", "sparc_i5",
178 "sparc_fp", "sparc_retadr"
180 if (reg >= 0 && reg < 32)
186 mono_arch_fregname (int reg) {
187 static const char *rnames [] = {
188 "sparc_f0", "sparc_f1", "sparc_f2", "sparc_f3", "sparc_f4",
189 "sparc_f5", "sparc_f6", "sparc_f7", "sparc_f8", "sparc_f9",
190 "sparc_f10", "sparc_f11", "sparc_f12", "sparc_f13", "sparc_f14",
191 "sparc_f15", "sparc_f16", "sparc_f17", "sparc_f18", "sparc_f19",
192 "sparc_f20", "sparc_f21", "sparc_f22", "sparc_f23", "sparc_f24",
193 "sparc_f25", "sparc_f26", "sparc_f27", "sparc_f28", "sparc_f29",
194 "sparc_f30", "sparc_f31"
197 if (reg >= 0 && reg < 32)
204 * Initialize the cpu to execute managed code.
207 mono_arch_cpu_init (void)
210 /* make sure sparcv9 is initialized for embedded use */
211 mono_arch_cpu_optimizazions(&dummy);
215 * This function returns the optimizations supported on this cpu.
218 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
226 if (!sysinfo (SI_ISALIST, buf, 1024))
227 g_assert_not_reached ();
229 /* From glibc. If the getpagesize is 8192, we're on sparc64, which
230 * (in)directly implies that we're a v9 or better.
231 * Improvements to this are greatly accepted...
232 * Also, we don't differentiate between v7 and v8. I sense SIGILL
233 * sniffing in my future.
235 if (getpagesize() == 8192)
236 strcpy (buf, "sparcv9");
238 strcpy (buf, "sparcv8");
242 * On some processors, the cmov instructions are even slower than the
245 if (strstr (buf, "sparcv9")) {
246 opts |= MONO_OPT_CMOV | MONO_OPT_FCMOV;
250 *exclude_mask |= MONO_OPT_CMOV | MONO_OPT_FCMOV;
256 mono_arch_break (void)
261 #define flushi(addr) __asm__ __volatile__ ("iflush %0"::"r"(addr):"memory")
262 #else /* assume Sun's compiler */
263 static void flushi(void *addr)
270 void sync_instruction_memory(caddr_t addr, int len);
274 mono_arch_flush_icache (guint8 *code, gint size)
277 /* Hopefully this is optimized based on the actual CPU */
278 sync_instruction_memory (code, size);
280 guint64 *p = (guint64*)code;
281 guint64 *end = (guint64*)(code + ((size + 8) /8));
284 * FIXME: Flushing code in dword chunks in _slow_.
288 __asm__ __volatile__ ("iflush %0"::"r"(p++));
298 * Flush all register windows to memory. Every register window is saved to
299 * a 16 word area on the stack pointed to by its %sp register.
302 mono_sparc_flushw (void)
304 static guint32 start [64];
305 static int inited = 0;
307 static void (*flushw) (void);
312 sparc_save_imm (code, sparc_sp, -160, sparc_sp);
315 sparc_restore_simple (code);
317 g_assert ((code - start) < 64);
319 flushw = (gpointer)start;
328 mono_arch_flush_register_windows (void)
330 mono_sparc_flushw ();
334 mono_arch_is_inst_imm (gint64 imm)
336 return sparc_is_imm13 (imm);
340 mono_sparc_is_v9 (void) {
345 mono_sparc_is_sparc64 (void) {
357 ArgInFloatReg, /* V9 only */
358 ArgInDoubleReg /* V9 only */
363 /* This needs to be offset by %i0 or %o0 depending on caller/callee */
366 guint32 vt_offset; /* for valuetypes */
384 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean pair)
386 ainfo->offset = *stack_size;
389 if (*gr >= PARAM_REGS) {
390 ainfo->storage = ArgOnStack;
393 ainfo->storage = ArgInIReg;
398 /* Allways reserve stack space for parameters passed in registers */
399 (*stack_size) += sizeof (gpointer);
402 if (*gr < PARAM_REGS - 1) {
403 /* A pair of registers */
404 ainfo->storage = ArgInIRegPair;
408 else if (*gr >= PARAM_REGS) {
409 /* A pair of stack locations */
410 ainfo->storage = ArgOnStackPair;
413 ainfo->storage = ArgInSplitRegStack;
418 (*stack_size) += 2 * sizeof (gpointer);
424 #define FLOAT_PARAM_REGS 32
427 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean single)
429 ainfo->offset = *stack_size;
432 if (*gr >= FLOAT_PARAM_REGS) {
433 ainfo->storage = ArgOnStack;
436 /* A single is passed in an even numbered fp register */
437 ainfo->storage = ArgInFloatReg;
438 ainfo->reg = *gr + 1;
443 if (*gr < FLOAT_PARAM_REGS) {
444 /* A double register */
445 ainfo->storage = ArgInDoubleReg;
450 ainfo->storage = ArgOnStack;
454 (*stack_size) += sizeof (gpointer);
462 * Obtain information about a call according to the calling convention.
463 * For V8, see the "System V ABI, Sparc Processor Supplement" Sparc V8 version
464 * document for more information.
465 * For V9, see the "Low Level System Information (64-bit psABI)" chapter in
466 * the 'Sparc Compliance Definition 2.4' document.
469 get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
472 int n = sig->hasthis + sig->param_count;
473 guint32 stack_size = 0;
476 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
482 if (MONO_TYPE_ISSTRUCT ((sig->ret))) {
483 /* The address of the return value is passed in %o0 */
484 add_general (&gr, &stack_size, &cinfo->ret, FALSE);
485 cinfo->ret.reg += sparc_i0;
491 add_general (&gr, &stack_size, cinfo->args + 0, FALSE);
493 if ((sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
496 /* Emit the signature cookie just before the implicit arguments */
497 add_general (&gr, &stack_size, &cinfo->sig_cookie, FALSE);
500 for (i = 0; i < sig->param_count; ++i) {
501 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
503 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
506 /* Emit the signature cookie just before the implicit arguments */
507 add_general (&gr, &stack_size, &cinfo->sig_cookie, FALSE);
510 DEBUG(printf("param %d: ", i));
511 if (sig->params [i]->byref) {
512 DEBUG(printf("byref\n"));
514 add_general (&gr, &stack_size, ainfo, FALSE);
517 switch (mono_type_get_underlying_type (sig->params [i])->type) {
518 case MONO_TYPE_BOOLEAN:
521 add_general (&gr, &stack_size, ainfo, FALSE);
522 /* the value is in the ls byte */
523 ainfo->offset += sizeof (gpointer) - 1;
528 add_general (&gr, &stack_size, ainfo, FALSE);
529 /* the value is in the ls word */
530 ainfo->offset += sizeof (gpointer) - 2;
534 add_general (&gr, &stack_size, ainfo, FALSE);
535 /* the value is in the ls dword */
536 ainfo->offset += sizeof (gpointer) - 4;
541 case MONO_TYPE_FNPTR:
542 case MONO_TYPE_CLASS:
543 case MONO_TYPE_OBJECT:
544 case MONO_TYPE_STRING:
545 case MONO_TYPE_SZARRAY:
546 case MONO_TYPE_ARRAY:
547 add_general (&gr, &stack_size, ainfo, FALSE);
549 case MONO_TYPE_GENERICINST:
550 if (!mono_type_generic_inst_is_valuetype (sig->params [i])) {
551 add_general (&gr, &stack_size, ainfo, FALSE);
555 case MONO_TYPE_VALUETYPE:
560 add_general (&gr, &stack_size, ainfo, FALSE);
562 case MONO_TYPE_TYPEDBYREF:
563 add_general (&gr, &stack_size, ainfo, FALSE);
568 add_general (&gr, &stack_size, ainfo, FALSE);
570 add_general (&gr, &stack_size, ainfo, TRUE);
575 add_float (&fr, &stack_size, ainfo, TRUE);
578 /* single precision values are passed in integer registers */
579 add_general (&gr, &stack_size, ainfo, FALSE);
584 add_float (&fr, &stack_size, ainfo, FALSE);
587 /* double precision values are passed in a pair of registers */
588 add_general (&gr, &stack_size, ainfo, TRUE);
592 g_assert_not_reached ();
596 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
599 /* Emit the signature cookie just before the implicit arguments */
600 add_general (&gr, &stack_size, &cinfo->sig_cookie, FALSE);
605 switch (mono_type_get_underlying_type (sig->ret)->type) {
606 case MONO_TYPE_BOOLEAN:
617 case MONO_TYPE_FNPTR:
618 case MONO_TYPE_CLASS:
619 case MONO_TYPE_OBJECT:
620 case MONO_TYPE_SZARRAY:
621 case MONO_TYPE_ARRAY:
622 case MONO_TYPE_STRING:
623 cinfo->ret.storage = ArgInIReg;
624 cinfo->ret.reg = sparc_i0;
631 cinfo->ret.storage = ArgInIReg;
632 cinfo->ret.reg = sparc_i0;
636 cinfo->ret.storage = ArgInIRegPair;
637 cinfo->ret.reg = sparc_i0;
644 cinfo->ret.storage = ArgInFReg;
645 cinfo->ret.reg = sparc_f0;
647 case MONO_TYPE_GENERICINST:
648 if (!mono_type_generic_inst_is_valuetype (sig->ret)) {
649 cinfo->ret.storage = ArgInIReg;
650 cinfo->ret.reg = sparc_i0;
656 case MONO_TYPE_VALUETYPE:
665 cinfo->ret.storage = ArgOnStack;
667 case MONO_TYPE_TYPEDBYREF:
670 /* Same as a valuetype with size 24 */
677 cinfo->ret.storage = ArgOnStack;
682 g_error ("Can't handle as return value 0x%x", sig->ret->type);
686 cinfo->stack_usage = stack_size;
687 cinfo->reg_usage = gr;
692 is_regsize_var (MonoType *t) {
695 switch (mono_type_get_underlying_type (t)->type) {
696 case MONO_TYPE_BOOLEAN:
707 case MONO_TYPE_OBJECT:
708 case MONO_TYPE_STRING:
709 case MONO_TYPE_CLASS:
710 case MONO_TYPE_SZARRAY:
711 case MONO_TYPE_ARRAY:
713 case MONO_TYPE_VALUETYPE:
725 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
731 * FIXME: If an argument is allocated to a register, then load it from the
732 * stack in the prolog.
735 for (i = 0; i < cfg->num_varinfo; i++) {
736 MonoInst *ins = cfg->varinfo [i];
737 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
740 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
743 /* FIXME: Make arguments on stack allocateable to registers */
744 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode == OP_REGVAR) || (ins->opcode == OP_ARG))
747 if (is_regsize_var (ins->inst_vtype)) {
748 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
749 g_assert (i == vmv->idx);
751 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
759 mono_arch_get_global_int_regs (MonoCompile *cfg)
763 MonoMethodSignature *sig;
766 sig = mono_method_signature (cfg->method);
768 cinfo = get_call_info (sig, FALSE);
770 /* Use unused input registers */
771 for (i = cinfo->reg_usage; i < 6; ++i)
772 regs = g_list_prepend (regs, GUINT_TO_POINTER (sparc_i0 + i));
774 /* Use %l0..%l6 as global registers */
775 for (i = sparc_l0; i < sparc_l7; ++i)
776 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
784 * mono_arch_regalloc_cost:
786 * Return the cost, in number of memory references, of the action of
787 * allocating the variable VMV into a register during global register
791 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
797 * Set var information according to the calling convention. sparc version.
798 * The locals var stuff should most likely be split in another method.
801 mono_arch_allocate_vars (MonoCompile *m)
803 MonoMethodSignature *sig;
804 MonoMethodHeader *header;
806 int i, offset, size, align, curinst;
809 header = mono_method_get_header (m->method);
811 sig = mono_method_signature (m->method);
813 cinfo = get_call_info (sig, FALSE);
815 if (sig->ret->type != MONO_TYPE_VOID) {
816 switch (cinfo->ret.storage) {
820 m->ret->opcode = OP_REGVAR;
821 m->ret->inst_c0 = cinfo->ret.reg;
825 g_assert_not_reached ();
828 m->ret->opcode = OP_REGOFFSET;
829 m->ret->inst_basereg = sparc_fp;
830 m->ret->inst_offset = 64;
836 m->ret->dreg = m->ret->inst_c0;
840 * We use the ABI calling conventions for managed code as well.
841 * Exception: valuetypes are never returned in registers on V9.
842 * FIXME: Use something more optimized.
845 /* Locals are allocated backwards from %fp */
846 m->frame_reg = sparc_fp;
850 * Reserve a stack slot for holding information used during exception
853 if (header->num_clauses)
854 offset += sizeof (gpointer) * 2;
856 if (m->method->save_lmf) {
857 offset += sizeof (MonoLMF);
858 m->arch.lmf_offset = offset;
861 curinst = m->locals_start;
862 for (i = curinst; i < m->num_varinfo; ++i) {
863 inst = m->varinfo [i];
865 if (inst->opcode == OP_REGVAR) {
866 //g_print ("allocating local %d to %s\n", i, mono_arch_regname (inst->dreg));
870 if (inst->flags & MONO_INST_IS_DEAD)
873 /* inst->unused indicates native sized value types, this is used by the
874 * pinvoke wrappers when they call functions returning structure */
875 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
876 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
878 size = mono_type_stack_size (inst->inst_vtype, &align);
881 * This is needed since structures containing doubles must be doubleword
883 * FIXME: Do this only if needed.
885 if (MONO_TYPE_ISSTRUCT (inst->inst_vtype))
889 * variables are accessed as negative offsets from %fp, so increase
890 * the offset before assigning it to a variable
895 offset &= ~(align - 1);
896 inst->opcode = OP_REGOFFSET;
897 inst->inst_basereg = sparc_fp;
898 inst->inst_offset = STACK_BIAS + -offset;
900 //g_print ("allocating local %d to [%s - %d]\n", i, mono_arch_regname (inst->inst_basereg), - inst->inst_offset);
903 if (sig->call_convention == MONO_CALL_VARARG) {
904 m->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
907 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
908 inst = m->varinfo [i];
909 if (inst->opcode != OP_REGVAR) {
910 ArgInfo *ainfo = &cinfo->args [i];
911 gboolean inreg = TRUE;
915 if (sig->hasthis && (i == 0))
916 arg_type = &mono_defaults.object_class->byval_arg;
918 arg_type = sig->params [i - sig->hasthis];
921 if (!arg_type->byref && ((arg_type->type == MONO_TYPE_R4)
922 || (arg_type->type == MONO_TYPE_R8)))
924 * Since float arguments are passed in integer registers, we need to
925 * save them to the stack in the prolog.
930 /* FIXME: Allocate volatile arguments to registers */
931 if (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
934 if (MONO_TYPE_ISSTRUCT (arg_type))
935 /* FIXME: this isn't needed */
938 inst->opcode = OP_REGOFFSET;
941 storage = ArgOnStack;
943 storage = ainfo->storage;
948 inst->opcode = OP_REGVAR;
949 inst->dreg = sparc_i0 + ainfo->reg;
954 * Since float regs are volatile, we save the arguments to
955 * the stack in the prolog.
956 * FIXME: Avoid this if the method contains no calls.
960 case ArgInSplitRegStack:
961 /* Split arguments are saved to the stack in the prolog */
962 inst->opcode = OP_REGOFFSET;
963 /* in parent frame */
964 inst->inst_basereg = sparc_fp;
965 inst->inst_offset = ainfo->offset + ARGS_OFFSET;
967 if (!arg_type->byref && (arg_type->type == MONO_TYPE_R8)) {
969 * It is very hard to load doubles from non-doubleword aligned
970 * memory locations. So if the offset is misaligned, we copy the
971 * argument to a stack location in the prolog.
973 if ((inst->inst_offset - STACK_BIAS) % 8) {
974 inst->inst_basereg = sparc_fp;
978 offset &= ~(align - 1);
979 inst->inst_offset = STACK_BIAS + -offset;
988 if (MONO_TYPE_ISSTRUCT (arg_type)) {
989 /* Add a level of indirection */
991 * It would be easier to add OP_LDIND_I here, but ldind_i instructions
992 * are destructively modified in a lot of places in inssel.brg.
995 MONO_INST_NEW (m, indir, 0);
997 inst->opcode = OP_SPARC_INARG_VT;
998 inst->inst_left = indir;
1004 * spillvars are stored between the normal locals and the storage reserved
1008 m->stack_offset = offset;
1010 /* Add a properly aligned dword for use by int<->float conversion opcodes */
1012 mono_spillvar_offset_float (m, 0);
1018 make_group (MonoCompile *cfg, MonoInst *left, int basereg, int offset)
1022 MONO_INST_NEW (cfg, group, OP_GROUP);
1023 group->inst_left = left;
1024 group->inst_basereg = basereg;
1025 group->inst_imm = offset;
1031 * take the arguments and generate the arch-specific
1032 * instructions to properly call the function in call.
1033 * This includes pushing, moving arguments to the right register
1037 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
1039 MonoMethodSignature *sig;
1043 guint32 extra_space = 0;
1045 sig = call->signature;
1046 n = sig->param_count + sig->hasthis;
1048 cinfo = get_call_info (sig, sig->pinvoke);
1050 for (i = 0; i < n; ++i) {
1051 ainfo = cinfo->args + i;
1053 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1054 /* Emit the signature cookie just before the first implicit argument */
1056 MonoMethodSignature *tmp_sig;
1059 * mono_ArgIterator_Setup assumes the signature cookie is
1060 * passed first and all the arguments which were before it are
1061 * passed on the stack after the signature. So compensate by
1062 * passing a different signature.
1064 tmp_sig = mono_metadata_signature_dup (call->signature);
1065 tmp_sig->param_count -= call->signature->sentinelpos;
1066 tmp_sig->sentinelpos = 0;
1067 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1069 /* FIXME: Add support for signature tokens to AOT */
1070 cfg->disable_aot = TRUE;
1071 /* We allways pass the signature on the stack for simplicity */
1072 MONO_INST_NEW (cfg, arg, OP_SPARC_OUTARG_MEM);
1073 arg->inst_right = make_group (cfg, (MonoInst*)call, sparc_sp, ARGS_OFFSET + cinfo->sig_cookie.offset);
1074 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
1075 sig_arg->inst_p0 = tmp_sig;
1076 arg->inst_left = sig_arg;
1077 arg->type = STACK_PTR;
1078 /* prepend, so they get reversed */
1079 arg->next = call->out_args;
1080 call->out_args = arg;
1083 if (is_virtual && i == 0) {
1084 /* the argument will be attached to the call instruction */
1085 in = call->args [i];
1087 MONO_INST_NEW (cfg, arg, OP_OUTARG);
1088 in = call->args [i];
1089 arg->cil_code = in->cil_code;
1090 arg->inst_left = in;
1091 arg->type = in->type;
1092 /* prepend, we'll need to reverse them later */
1093 arg->next = call->out_args;
1094 call->out_args = arg;
1096 if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis]))) {
1099 guint32 offset, pad;
1107 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_TYPEDBYREF) {
1108 size = sizeof (MonoTypedRef);
1109 align = sizeof (gpointer);
1113 size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
1116 * Can't use mono_type_stack_size (), but that
1117 * aligns the size to sizeof (gpointer), which is larger
1118 * than the size of the source, leading to reads of invalid
1119 * memory if the source is at the end of address space or
1122 size = mono_class_value_size (in->klass, &align);
1126 * We use OP_OUTARG_VT to copy the valuetype to a stack location, then
1127 * use the normal OUTARG opcodes to pass the address of the location to
1130 MONO_INST_NEW (cfg, inst, OP_OUTARG_VT);
1131 inst->inst_left = in;
1133 /* The first 6 argument locations are reserved */
1134 if (cinfo->stack_usage < 6 * sizeof (gpointer))
1135 cinfo->stack_usage = 6 * sizeof (gpointer);
1137 offset = ALIGN_TO ((ARGS_OFFSET - STACK_BIAS) + cinfo->stack_usage, align);
1138 pad = offset - ((ARGS_OFFSET - STACK_BIAS) + cinfo->stack_usage);
1140 inst->inst_c1 = STACK_BIAS + offset;
1141 inst->unused = size;
1142 arg->inst_left = inst;
1144 cinfo->stack_usage += size;
1145 cinfo->stack_usage += pad;
1148 arg->inst_right = make_group (cfg, (MonoInst*)call, sparc_sp, ARGS_OFFSET + ainfo->offset);
1150 switch (ainfo->storage) {
1154 if (ainfo->storage == ArgInIRegPair)
1155 arg->opcode = OP_SPARC_OUTARG_REGPAIR;
1156 arg->unused = sparc_o0 + ainfo->reg;
1157 call->used_iregs |= 1 << ainfo->reg;
1159 if ((i >= sig->hasthis) && !sig->params [i - sig->hasthis]->byref && ((sig->params [i - sig->hasthis]->type == MONO_TYPE_R8) || (sig->params [i - sig->hasthis]->type == MONO_TYPE_R4))) {
1160 /* An fp value is passed in an ireg */
1162 if (arg->opcode == OP_SPARC_OUTARG_REGPAIR)
1163 arg->opcode = OP_SPARC_OUTARG_REGPAIR_FLOAT;
1165 arg->opcode = OP_SPARC_OUTARG_FLOAT;
1168 * The OUTARG (freg) implementation needs an extra dword to store
1169 * the temporary value.
1175 arg->opcode = OP_SPARC_OUTARG_MEM;
1177 case ArgOnStackPair:
1178 arg->opcode = OP_SPARC_OUTARG_MEMPAIR;
1180 case ArgInSplitRegStack:
1181 arg->opcode = OP_SPARC_OUTARG_SPLIT_REG_STACK;
1182 arg->unused = sparc_o0 + ainfo->reg;
1183 call->used_iregs |= 1 << ainfo->reg;
1186 arg->opcode = OP_SPARC_OUTARG_FLOAT_REG;
1187 arg->unused = sparc_f0 + ainfo->reg;
1189 case ArgInDoubleReg:
1190 arg->opcode = OP_SPARC_OUTARG_DOUBLE_REG;
1191 arg->unused = sparc_f0 + ainfo->reg;
1200 * Reverse the call->out_args list.
1203 MonoInst *prev = NULL, *list = call->out_args, *next;
1210 call->out_args = prev;
1212 call->stack_usage = cinfo->stack_usage + extra_space;
1213 call->out_ireg_args = NULL;
1214 call->out_freg_args = NULL;
1215 cfg->param_area = MAX (cfg->param_area, call->stack_usage);
1216 cfg->flags |= MONO_CFG_HAS_CALLS;
1222 /* Map opcode to the sparc condition codes */
1223 static inline SparcCond
1224 opcode_to_sparc_cond (int opcode)
1246 case OP_COND_EXC_EQ:
1249 case OP_COND_EXC_NE_UN:
1256 case OP_COND_EXC_LT:
1262 case OP_COND_EXC_LT_UN:
1268 case OP_COND_EXC_GT:
1274 case OP_COND_EXC_GT_UN:
1278 case OP_COND_EXC_GE:
1282 case OP_COND_EXC_GE_UN:
1286 case OP_COND_EXC_LE:
1290 case OP_COND_EXC_LE_UN:
1292 case OP_COND_EXC_OV:
1293 case OP_COND_EXC_IOV:
1296 case OP_COND_EXC_IC:
1298 case OP_COND_EXC_NO:
1299 case OP_COND_EXC_NC:
1302 g_assert_not_reached ();
1307 #define COMPUTE_DISP(ins) \
1308 if (ins->flags & MONO_INST_BRLABEL) { \
1309 if (ins->inst_i0->inst_c0) \
1310 disp = (ins->inst_i0->inst_c0 - ((guint8*)code - cfg->native_code)) >> 2; \
1313 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1316 if (ins->inst_true_bb->native_offset) \
1317 disp = (ins->inst_true_bb->native_offset - ((guint8*)code - cfg->native_code)) >> 2; \
1320 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1325 #define DEFAULT_ICC sparc_xcc_short
1327 #define DEFAULT_ICC sparc_icc_short
1331 #define EMIT_COND_BRANCH_ICC(ins,cond,annul,filldelay,icc) \
1335 COMPUTE_DISP(ins); \
1336 predict = (disp != 0) ? 1 : 0; \
1337 g_assert (sparc_is_imm19 (disp)); \
1338 sparc_branchp (code, (annul), cond, icc, (predict), disp); \
1339 if (filldelay) sparc_nop (code); \
1341 #define EMIT_COND_BRANCH(ins,cond,annul,filldelay) EMIT_COND_BRANCH_ICC ((ins), (cond), (annul), (filldelay), (sparc_xcc_short))
1342 #define EMIT_FLOAT_COND_BRANCH(ins,cond,annul,filldelay) \
1346 COMPUTE_DISP(ins); \
1347 predict = (disp != 0) ? 1 : 0; \
1348 g_assert (sparc_is_imm19 (disp)); \
1349 sparc_fbranch (code, (annul), cond, disp); \
1350 if (filldelay) sparc_nop (code); \
1353 #define EMIT_COND_BRANCH_ICC(ins,cond,annul,filldelay,icc) g_assert_not_reached ()
1354 #define EMIT_COND_BRANCH_GENERAL(ins,bop,cond,annul,filldelay) \
1357 COMPUTE_DISP(ins); \
1358 g_assert (sparc_is_imm22 (disp)); \
1359 sparc_ ## bop (code, (annul), cond, disp); \
1360 if (filldelay) sparc_nop (code); \
1362 #define EMIT_COND_BRANCH(ins,cond,annul,filldelay) EMIT_COND_BRANCH_GENERAL((ins),branch,(cond),annul,filldelay)
1363 #define EMIT_FLOAT_COND_BRANCH(ins,cond,annul,filldelay) EMIT_COND_BRANCH_GENERAL((ins),fbranch,(cond),annul,filldelay)
1366 #define EMIT_COND_BRANCH_PREDICTED(ins,cond,annul,filldelay) \
1370 COMPUTE_DISP(ins); \
1371 predict = (disp != 0) ? 1 : 0; \
1372 g_assert (sparc_is_imm19 (disp)); \
1373 sparc_branchp (code, (annul), (cond), DEFAULT_ICC, (predict), disp); \
1374 if (filldelay) sparc_nop (code); \
1377 #define EMIT_COND_BRANCH_BPR(ins,bop,predict,annul,filldelay) \
1380 COMPUTE_DISP(ins); \
1381 g_assert (sparc_is_imm22 (disp)); \
1382 sparc_ ## bop (code, (annul), (predict), ins->sreg1, disp); \
1383 if (filldelay) sparc_nop (code); \
1386 /* emit an exception if condition is fail */
1388 * We put the exception throwing code out-of-line, at the end of the method
1390 #define EMIT_COND_SYSTEM_EXCEPTION_GENERAL(ins,cond,sexc_name,filldelay,icc) do { \
1391 mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code, \
1392 MONO_PATCH_INFO_EXC, sexc_name); \
1394 sparc_branchp (code, 0, (cond), (icc), 0, 0); \
1397 sparc_branch (code, 0, cond, 0); \
1399 if (filldelay) sparc_nop (code); \
1402 #define EMIT_COND_SYSTEM_EXCEPTION(ins,cond,sexc_name) EMIT_COND_SYSTEM_EXCEPTION_GENERAL(ins,cond,sexc_name,TRUE,DEFAULT_ICC)
1404 #define EMIT_COND_SYSTEM_EXCEPTION_BPR(ins,bop,sexc_name) do { \
1405 mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code, \
1406 MONO_PATCH_INFO_EXC, sexc_name); \
1407 sparc_ ## bop (code, FALSE, FALSE, ins->sreg1, 0); \
1411 #define EMIT_ALU_IMM(ins,op,setcc) do { \
1412 if (sparc_is_imm13 ((ins)->inst_imm)) \
1413 sparc_ ## op ## _imm (code, (setcc), (ins)->sreg1, ins->inst_imm, (ins)->dreg); \
1415 sparc_set (code, ins->inst_imm, sparc_o7); \
1416 sparc_ ## op (code, (setcc), (ins)->sreg1, sparc_o7, (ins)->dreg); \
1420 #define EMIT_LOAD_MEMBASE(ins,op) do { \
1421 if (sparc_is_imm13 (ins->inst_offset)) \
1422 sparc_ ## op ## _imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg); \
1424 sparc_set (code, ins->inst_offset, sparc_o7); \
1425 sparc_ ## op (code, ins->inst_basereg, sparc_o7, ins->dreg); \
1430 #define EMIT_STORE_MEMBASE_IMM(ins,op) do { \
1432 if (ins->inst_imm == 0) \
1435 sparc_set (code, ins->inst_imm, sparc_o7); \
1438 if (!sparc_is_imm13 (ins->inst_offset)) { \
1439 sparc_set (code, ins->inst_offset, GP_SCRATCH_REG); \
1440 sparc_ ## op (code, sreg, ins->inst_destbasereg, GP_SCRATCH_REG); \
1443 sparc_ ## op ## _imm (code, sreg, ins->inst_destbasereg, ins->inst_offset); \
1446 #define EMIT_STORE_MEMBASE_REG(ins,op) do { \
1447 if (!sparc_is_imm13 (ins->inst_offset)) { \
1448 sparc_set (code, ins->inst_offset, sparc_o7); \
1449 sparc_ ## op (code, ins->sreg1, ins->inst_destbasereg, sparc_o7); \
1452 sparc_ ## op ## _imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset); \
1455 #define EMIT_CALL() do { \
1457 sparc_set_template (code, sparc_o7); \
1458 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_o7); \
1461 sparc_call_simple (code, 0); \
1467 * A call template is 7 instructions long, so we want to avoid it if possible.
1470 emit_call (MonoCompile *cfg, guint32 *code, guint32 patch_type, gconstpointer data)
1474 /* FIXME: This only works if the target method is already compiled */
1475 if (0 && v64 && !cfg->compile_aot) {
1476 MonoJumpInfo patch_info;
1478 patch_info.type = patch_type;
1479 patch_info.data.target = data;
1481 target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, &patch_info, FALSE);
1483 /* FIXME: Add optimizations if the target is close enough */
1484 sparc_set (code, target, sparc_o7);
1485 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_o7);
1489 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, patch_type, data);
1497 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1499 MonoInst *ins, *last_ins = NULL;
1504 switch (ins->opcode) {
1506 /* remove unnecessary multiplication with 1 */
1507 if (ins->inst_imm == 1) {
1508 if (ins->dreg != ins->sreg1) {
1509 ins->opcode = OP_MOVE;
1511 last_ins->next = ins->next;
1518 case OP_LOAD_MEMBASE:
1519 case OP_LOADI4_MEMBASE:
1521 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1522 * OP_LOAD_MEMBASE offset(basereg), reg
1524 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1525 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1526 ins->inst_basereg == last_ins->inst_destbasereg &&
1527 ins->inst_offset == last_ins->inst_offset) {
1528 if (ins->dreg == last_ins->sreg1) {
1529 last_ins->next = ins->next;
1533 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1534 ins->opcode = OP_MOVE;
1535 ins->sreg1 = last_ins->sreg1;
1539 * Note: reg1 must be different from the basereg in the second load
1540 * OP_LOAD_MEMBASE offset(basereg), reg1
1541 * OP_LOAD_MEMBASE offset(basereg), reg2
1543 * OP_LOAD_MEMBASE offset(basereg), reg1
1544 * OP_MOVE reg1, reg2
1546 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1547 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1548 ins->inst_basereg != last_ins->dreg &&
1549 ins->inst_basereg == last_ins->inst_basereg &&
1550 ins->inst_offset == last_ins->inst_offset) {
1552 if (ins->dreg == last_ins->dreg) {
1553 last_ins->next = ins->next;
1557 ins->opcode = OP_MOVE;
1558 ins->sreg1 = last_ins->dreg;
1561 //g_assert_not_reached ();
1565 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1566 * OP_LOAD_MEMBASE offset(basereg), reg
1568 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1569 * OP_ICONST reg, imm
1571 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1572 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1573 ins->inst_basereg == last_ins->inst_destbasereg &&
1574 ins->inst_offset == last_ins->inst_offset) {
1575 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1576 ins->opcode = OP_ICONST;
1577 ins->inst_c0 = last_ins->inst_imm;
1578 g_assert_not_reached (); // check this rule
1583 case OP_LOADU1_MEMBASE:
1584 case OP_LOADI1_MEMBASE:
1585 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1586 ins->inst_basereg == last_ins->inst_destbasereg &&
1587 ins->inst_offset == last_ins->inst_offset) {
1588 if (ins->dreg == last_ins->sreg1) {
1589 last_ins->next = ins->next;
1593 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1594 ins->opcode = OP_MOVE;
1595 ins->sreg1 = last_ins->sreg1;
1599 case OP_LOADU2_MEMBASE:
1600 case OP_LOADI2_MEMBASE:
1601 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1602 ins->inst_basereg == last_ins->inst_destbasereg &&
1603 ins->inst_offset == last_ins->inst_offset) {
1604 if (ins->dreg == last_ins->sreg1) {
1605 last_ins->next = ins->next;
1609 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1610 ins->opcode = OP_MOVE;
1611 ins->sreg1 = last_ins->sreg1;
1615 case OP_STOREI4_MEMBASE_IMM:
1616 /* Convert pairs of 0 stores to a dword 0 store */
1617 /* Used when initializing temporaries */
1618 /* We know sparc_fp is dword aligned */
1619 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM) &&
1620 (ins->inst_destbasereg == last_ins->inst_destbasereg) &&
1621 (ins->inst_destbasereg == sparc_fp) &&
1622 (ins->inst_offset < 0) &&
1623 ((ins->inst_offset % 8) == 0) &&
1624 ((ins->inst_offset == last_ins->inst_offset - 4)) &&
1625 (ins->inst_imm == 0) &&
1626 (last_ins->inst_imm == 0)) {
1628 last_ins->opcode = OP_STOREI8_MEMBASE_IMM;
1629 last_ins->inst_offset = ins->inst_offset;
1630 last_ins->next = ins->next;
1642 case OP_COND_EXC_EQ:
1643 case OP_COND_EXC_GE:
1644 case OP_COND_EXC_GT:
1645 case OP_COND_EXC_LE:
1646 case OP_COND_EXC_LT:
1647 case OP_COND_EXC_NE_UN:
1649 * Convert compare with zero+branch to BRcc
1652 * This only works in 64 bit mode, since it examines all 64
1653 * bits of the register.
1654 * Only do this if the method is small since BPr only has a 16bit
1657 if (v64 && (mono_method_get_header (cfg->method)->code_size < 10000) && last_ins &&
1658 (last_ins->opcode == OP_COMPARE_IMM) &&
1659 (last_ins->inst_imm == 0)) {
1660 MonoInst *next = ins->next;
1661 switch (ins->opcode) {
1663 ins->opcode = OP_SPARC_BRZ;
1666 ins->opcode = OP_SPARC_BRNZ;
1669 ins->opcode = OP_SPARC_BRLZ;
1672 ins->opcode = OP_SPARC_BRGZ;
1675 ins->opcode = OP_SPARC_BRGEZ;
1678 ins->opcode = OP_SPARC_BRLEZ;
1680 case OP_COND_EXC_EQ:
1681 ins->opcode = OP_SPARC_COND_EXC_EQZ;
1683 case OP_COND_EXC_GE:
1684 ins->opcode = OP_SPARC_COND_EXC_GEZ;
1686 case OP_COND_EXC_GT:
1687 ins->opcode = OP_SPARC_COND_EXC_GTZ;
1689 case OP_COND_EXC_LE:
1690 ins->opcode = OP_SPARC_COND_EXC_LEZ;
1692 case OP_COND_EXC_LT:
1693 ins->opcode = OP_SPARC_COND_EXC_LTZ;
1695 case OP_COND_EXC_NE_UN:
1696 ins->opcode = OP_SPARC_COND_EXC_NEZ;
1699 g_assert_not_reached ();
1701 ins->sreg1 = last_ins->sreg1;
1703 last_ins->next = next;
1714 if (ins->dreg == ins->sreg1) {
1716 last_ins->next = ins->next;
1721 * OP_MOVE sreg, dreg
1722 * OP_MOVE dreg, sreg
1724 if (last_ins && last_ins->opcode == OP_MOVE &&
1725 ins->sreg1 == last_ins->dreg &&
1726 ins->dreg == last_ins->sreg1) {
1727 last_ins->next = ins->next;
1736 bb->last_ins = last_ins;
1739 static const char*const * ins_spec = sparc_desc;
1741 static inline const char*
1742 get_ins_spec (int opcode)
1744 if (ins_spec [opcode])
1745 return ins_spec [opcode];
1747 return ins_spec [CEE_ADD];
1751 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
1753 MonoSpillInfo **si, *info;
1756 si = &cfg->spill_info_float;
1758 while (i <= spillvar) {
1761 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1763 cfg->stack_offset += sizeof (double);
1764 cfg->stack_offset = ALIGN_TO (cfg->stack_offset, 8);
1765 info->offset = - cfg->stack_offset;
1769 return MONO_SPARC_STACK_BIAS + (*si)->offset;
1775 g_assert_not_reached ();
1779 /* FIXME: Strange loads from the stack in basic-float.cs:test_2_rem */
1782 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1784 mono_local_regalloc (cfg, bb);
1788 sparc_patch (guint32 *code, const gpointer target)
1791 guint32 ins = *code;
1792 guint32 op = ins >> 30;
1793 guint32 op2 = (ins >> 22) & 0x7;
1794 guint32 rd = (ins >> 25) & 0x1f;
1795 guint8* target8 = (guint8*)target;
1796 gint64 disp = (target8 - (guint8*)code) >> 2;
1799 // g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
1801 if ((op == 0) && (op2 == 2)) {
1802 if (!sparc_is_imm22 (disp))
1805 *code = ((ins >> 22) << 22) | (disp & 0x3fffff);
1807 else if ((op == 0) && (op2 == 1)) {
1808 if (!sparc_is_imm19 (disp))
1811 *code = ((ins >> 19) << 19) | (disp & 0x7ffff);
1813 else if ((op == 0) && (op2 == 3)) {
1814 if (!sparc_is_imm16 (disp))
1817 *code &= ~(0x180000 | 0x3fff);
1818 *code |= ((disp << 21) & (0x180000)) | (disp & 0x3fff);
1820 else if ((op == 0) && (op2 == 6)) {
1821 if (!sparc_is_imm22 (disp))
1824 *code = ((ins >> 22) << 22) | (disp & 0x3fffff);
1826 else if ((op == 0) && (op2 == 4)) {
1827 guint32 ins2 = code [1];
1829 if (((ins2 >> 30) == 2) && (((ins2 >> 19) & 0x3f) == 2)) {
1830 /* sethi followed by or */
1832 sparc_set (p, target8, rd);
1833 while (p <= (code + 1))
1836 else if (ins2 == 0x01000000) {
1837 /* sethi followed by nop */
1839 sparc_set (p, target8, rd);
1840 while (p <= (code + 1))
1843 else if ((sparc_inst_op (ins2) == 3) && (sparc_inst_imm (ins2))) {
1844 /* sethi followed by load/store */
1846 guint32 t = (guint32)target8;
1847 *code &= ~(0x3fffff);
1849 *(code + 1) &= ~(0x3ff);
1850 *(code + 1) |= (t & 0x3ff);
1854 (sparc_inst_rd (ins) == sparc_g1) &&
1855 (sparc_inst_op (c [1]) == 0) && (sparc_inst_op2 (c [1]) == 4) &&
1856 (sparc_inst_op (c [2]) == 2) && (sparc_inst_op3 (c [2]) == 2) &&
1857 (sparc_inst_op (c [3]) == 2) && (sparc_inst_op3 (c [3]) == 2))
1861 reg = sparc_inst_rd (c [1]);
1862 sparc_set (p, target8, reg);
1866 else if ((sparc_inst_op (ins2) == 2) && (sparc_inst_op3 (ins2) == 0x38) &&
1867 (sparc_inst_imm (ins2))) {
1868 /* sethi followed by jmpl */
1870 guint32 t = (guint32)target8;
1871 *code &= ~(0x3fffff);
1873 *(code + 1) &= ~(0x3ff);
1874 *(code + 1) |= (t & 0x3ff);
1880 else if (op == 01) {
1881 gint64 disp = (target8 - (guint8*)code) >> 2;
1883 if (!sparc_is_imm30 (disp))
1885 sparc_call_simple (code, target8 - (guint8*)code);
1887 else if ((op == 2) && (sparc_inst_op3 (ins) == 0x2) && sparc_inst_imm (ins)) {
1889 g_assert (sparc_is_imm13 (target8));
1891 *code |= (guint32)target8;
1893 else if ((sparc_inst_op (ins) == 2) && (sparc_inst_op3 (ins) == 0x7)) {
1894 /* sparc_set case 5. */
1898 reg = sparc_inst_rd (c [3]);
1899 sparc_set (p, target, reg);
1906 // g_print ("patched with 0x%08x\n", ins);
1910 * mono_sparc_emit_save_lmf:
1912 * Emit the code neccesary to push a new entry onto the lmf stack. Used by
1913 * trampolines as well.
1916 mono_sparc_emit_save_lmf (guint32 *code, guint32 lmf_offset)
1919 sparc_sti_imm (code, sparc_o0, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr));
1920 /* Save previous_lmf */
1921 sparc_ldi (code, sparc_o0, sparc_g0, sparc_o7);
1922 sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf));
1924 sparc_add_imm (code, FALSE, sparc_fp, lmf_offset, sparc_o7);
1925 sparc_sti (code, sparc_o7, sparc_o0, sparc_g0);
1931 mono_sparc_emit_restore_lmf (guint32 *code, guint32 lmf_offset)
1933 /* Load previous_lmf */
1934 sparc_ldi_imm (code, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sparc_l0);
1936 sparc_ldi_imm (code, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sparc_l1);
1937 /* *(lmf) = previous_lmf */
1938 sparc_sti (code, sparc_l0, sparc_l1, sparc_g0);
1943 emit_save_sp_to_lmf (MonoCompile *cfg, guint32 *code)
1946 * Since register windows are saved to the current value of %sp, we need to
1947 * set the sp field in the lmf before the call, not in the prolog.
1949 if (cfg->method->save_lmf) {
1950 gint32 lmf_offset = MONO_SPARC_STACK_BIAS - cfg->arch.lmf_offset;
1953 sparc_sti_imm (code, sparc_sp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, sp));
1960 emit_vret_token (MonoInst *ins, guint32 *code)
1962 MonoCallInst *call = (MonoCallInst*)ins;
1966 * The sparc ABI requires that calls to functions which return a structure
1967 * contain an additional unimpl instruction which is checked by the callee.
1969 if (call->signature->pinvoke && MONO_TYPE_ISSTRUCT(call->signature->ret)) {
1970 if (call->signature->ret->type == MONO_TYPE_TYPEDBYREF)
1971 size = mono_type_stack_size (call->signature->ret, NULL);
1973 size = mono_class_native_size (call->signature->ret->data.klass, NULL);
1974 sparc_unimp (code, size & 0xfff);
1981 emit_move_return_value (MonoInst *ins, guint32 *code)
1983 /* Move return value to the target register */
1984 /* FIXME: do more things in the local reg allocator */
1985 switch (ins->opcode) {
1987 case OP_VOIDCALL_REG:
1988 case OP_VOIDCALL_MEMBASE:
1992 case OP_CALL_MEMBASE:
1993 g_assert (ins->dreg == sparc_o0);
1997 case OP_LCALL_MEMBASE:
1999 * ins->dreg is the least significant reg due to the lreg: LCALL rule
2000 * in inssel-long32.brg.
2003 sparc_mov_reg_reg (code, sparc_o0, ins->dreg);
2005 g_assert (ins->dreg == sparc_o1);
2010 case OP_FCALL_MEMBASE:
2012 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4) {
2013 sparc_fmovs (code, sparc_f0, ins->dreg);
2014 sparc_fstod (code, ins->dreg, ins->dreg);
2017 sparc_fmovd (code, sparc_f0, ins->dreg);
2019 sparc_fmovs (code, sparc_f0, ins->dreg);
2020 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4)
2021 sparc_fstod (code, ins->dreg, ins->dreg);
2023 sparc_fmovs (code, sparc_f1, ins->dreg + 1);
2028 case OP_VCALL_MEMBASE:
2038 * emit_load_volatile_arguments:
2040 * Load volatile arguments from the stack to the original input registers.
2041 * Required before a tail call.
2044 emit_load_volatile_arguments (MonoCompile *cfg, guint32 *code)
2046 MonoMethod *method = cfg->method;
2047 MonoMethodSignature *sig;
2052 /* FIXME: Generate intermediate code instead */
2054 sig = mono_method_signature (method);
2056 cinfo = get_call_info (sig, FALSE);
2058 /* This is the opposite of the code in emit_prolog */
2060 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2061 ArgInfo *ainfo = cinfo->args + i;
2062 gint32 stack_offset;
2064 inst = cfg->varinfo [i];
2066 if (sig->hasthis && (i == 0))
2067 arg_type = &mono_defaults.object_class->byval_arg;
2069 arg_type = sig->params [i - sig->hasthis];
2071 stack_offset = ainfo->offset + ARGS_OFFSET;
2072 ireg = sparc_i0 + ainfo->reg;
2074 if (ainfo->storage == ArgInSplitRegStack) {
2075 g_assert (inst->opcode == OP_REGOFFSET);
2077 if (!sparc_is_imm13 (stack_offset))
2079 sparc_st_imm (code, inst->inst_basereg, stack_offset, sparc_i5);
2082 if (!v64 && !arg_type->byref && (arg_type->type == MONO_TYPE_R8)) {
2083 if (ainfo->storage == ArgInIRegPair) {
2084 if (!sparc_is_imm13 (inst->inst_offset + 4))
2086 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, ireg);
2087 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset + 4, ireg + 1);
2090 if (ainfo->storage == ArgInSplitRegStack) {
2091 if (stack_offset != inst->inst_offset) {
2092 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, sparc_i5);
2093 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset + 4, sparc_o7);
2094 sparc_st_imm (code, sparc_o7, sparc_fp, stack_offset + 4);
2099 if (ainfo->storage == ArgOnStackPair) {
2100 if (stack_offset != inst->inst_offset) {
2101 /* stack_offset is not dword aligned, so we need to make a copy */
2102 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, sparc_o7);
2103 sparc_st_imm (code, sparc_o7, sparc_fp, stack_offset);
2105 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset + 4, sparc_o7);
2106 sparc_st_imm (code, sparc_o7, sparc_fp, stack_offset + 4);
2111 g_assert_not_reached ();
2114 if ((ainfo->storage == ArgInIReg) && (inst->opcode != OP_REGVAR)) {
2115 /* Argument in register, but need to be saved to stack */
2116 if (!sparc_is_imm13 (stack_offset))
2118 if ((stack_offset - ARGS_OFFSET) & 0x1)
2119 /* FIXME: Is this ldsb or ldub ? */
2120 sparc_ldsb_imm (code, inst->inst_basereg, stack_offset, ireg);
2122 if ((stack_offset - ARGS_OFFSET) & 0x2)
2123 sparc_ldsh_imm (code, inst->inst_basereg, stack_offset, ireg);
2125 if ((stack_offset - ARGS_OFFSET) & 0x4)
2126 sparc_ld_imm (code, inst->inst_basereg, stack_offset, ireg);
2129 sparc_ldx_imm (code, inst->inst_basereg, stack_offset, ireg);
2131 sparc_ld_imm (code, inst->inst_basereg, stack_offset, ireg);
2134 else if ((ainfo->storage == ArgInIRegPair) && (inst->opcode != OP_REGVAR)) {
2135 /* Argument in regpair, but need to be saved to stack */
2136 if (!sparc_is_imm13 (inst->inst_offset + 4))
2138 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, ireg);
2139 sparc_st_imm (code, inst->inst_basereg, inst->inst_offset + 4, ireg + 1);
2141 else if ((ainfo->storage == ArgInFloatReg) && (inst->opcode != OP_REGVAR)) {
2144 else if ((ainfo->storage == ArgInDoubleReg) && (inst->opcode != OP_REGVAR)) {
2148 if ((ainfo->storage == ArgInSplitRegStack) || (ainfo->storage == ArgOnStack))
2149 if (inst->opcode == OP_REGVAR)
2150 /* FIXME: Load the argument into memory */
2160 * mono_sparc_is_virtual_call:
2162 * Determine whenever the instruction at CODE is a virtual call.
2165 mono_sparc_is_virtual_call (guint32 *code)
2172 if ((sparc_inst_op (*code) == 0x2) && (sparc_inst_op3 (*code) == 0x38)) {
2174 * Register indirect call. If it is a virtual call, then the
2175 * instruction in the delay slot is a special kind of nop.
2178 /* Construct special nop */
2179 sparc_or_imm (p, FALSE, sparc_g0, 0xca, sparc_g0);
2182 if (code [1] == p [0])
2190 * mono_arch_get_vcall_slot_addr:
2192 * Determine the vtable slot used by a virtual call.
2195 mono_arch_get_vcall_slot_addr (guint8 *code8, gpointer *regs)
2197 guint32 *code = (guint32*)(gpointer)code8;
2198 guint32 ins = code [0];
2199 guint32 prev_ins = code [-1];
2201 mono_sparc_flushw ();
2203 if (!mono_sparc_is_virtual_call (code))
2206 if ((sparc_inst_op (ins) == 0x2) && (sparc_inst_op3 (ins) == 0x38)) {
2207 if ((sparc_inst_op (prev_ins) == 0x3) && (sparc_inst_i (prev_ins) == 1) && (sparc_inst_op3 (prev_ins) == 0 || sparc_inst_op3 (prev_ins) == 0xb)) {
2208 /* ld [r1 + CONST ], r2; call r2 */
2209 guint32 base = sparc_inst_rs1 (prev_ins);
2210 guint32 disp = sparc_inst_imm13 (prev_ins);
2213 g_assert (sparc_inst_rd (prev_ins) == sparc_inst_rs1 (ins));
2215 g_assert ((base >= sparc_o0) && (base <= sparc_i7));
2217 base_val = regs [base - sparc_o0];
2219 return (gpointer)((guint8*)base_val + disp);
2221 else if ((sparc_inst_op (prev_ins) == 0x3) && (sparc_inst_i (prev_ins) == 0) && (sparc_inst_op3 (prev_ins) == 0)) {
2222 /* set r1, ICONST; ld [r1 + r2], r2; call r2 */
2223 /* Decode a sparc_set32 */
2224 guint32 base = sparc_inst_rs1 (prev_ins);
2227 guint32 s1 = code [-3];
2228 guint32 s2 = code [-2];
2235 g_assert (sparc_inst_op (s1) == 0);
2236 g_assert (sparc_inst_op2 (s1) == 4);
2239 g_assert (sparc_inst_op (s2) == 2);
2240 g_assert (sparc_inst_op3 (s2) == 2);
2241 g_assert (sparc_inst_i (s2) == 1);
2242 g_assert (sparc_inst_rs1 (s2) == sparc_inst_rd (s2));
2243 g_assert (sparc_inst_rd (s1) == sparc_inst_rs1 (s2));
2245 disp = ((s1 & 0x3fffff) << 10) | sparc_inst_imm13 (s2);
2247 g_assert ((base >= sparc_o0) && (base <= sparc_i7));
2249 base_val = regs [base - sparc_o0];
2251 return (gpointer)((guint8*)base_val + disp);
2253 g_assert_not_reached ();
2256 g_assert_not_reached ();
2262 * Some conventions used in the following code.
2263 * 2) The only scratch registers we have are o7 and g1. We try to
2264 * stick to o7 when we can, and use g1 when necessary.
2268 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2273 guint32 *code = (guint32*)(cfg->native_code + cfg->code_len);
2274 MonoInst *last_ins = NULL;
2278 if (cfg->opt & MONO_OPT_PEEPHOLE)
2279 peephole_pass (cfg, bb);
2281 if (cfg->verbose_level > 2)
2282 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2284 cpos = bb->max_offset;
2286 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2294 offset = (guint8*)code - cfg->native_code;
2296 spec = ins_spec [ins->opcode];
2298 spec = ins_spec [CEE_ADD];
2300 max_len = ((guint8 *)spec)[MONO_INST_LEN];
2302 if (offset > (cfg->code_size - max_len - 16)) {
2303 cfg->code_size *= 2;
2304 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2305 code = (guint32*)(cfg->native_code + offset);
2307 code_start = (guint8*)code;
2308 // if (ins->cil_code)
2309 // g_print ("cil code\n");
2310 mono_debug_record_line_number (cfg, ins, offset);
2312 switch (ins->opcode) {
2313 case OP_STOREI1_MEMBASE_IMM:
2314 EMIT_STORE_MEMBASE_IMM (ins, stb);
2316 case OP_STOREI2_MEMBASE_IMM:
2317 EMIT_STORE_MEMBASE_IMM (ins, sth);
2319 case OP_STORE_MEMBASE_IMM:
2320 EMIT_STORE_MEMBASE_IMM (ins, sti);
2322 case OP_STOREI4_MEMBASE_IMM:
2323 EMIT_STORE_MEMBASE_IMM (ins, st);
2325 case OP_STOREI8_MEMBASE_IMM:
2327 EMIT_STORE_MEMBASE_IMM (ins, stx);
2329 /* Only generated by peephole opts */
2330 g_assert ((ins->inst_offset % 8) == 0);
2331 g_assert (ins->inst_imm == 0);
2332 EMIT_STORE_MEMBASE_IMM (ins, stx);
2335 case OP_STOREI1_MEMBASE_REG:
2336 EMIT_STORE_MEMBASE_REG (ins, stb);
2338 case OP_STOREI2_MEMBASE_REG:
2339 EMIT_STORE_MEMBASE_REG (ins, sth);
2341 case OP_STOREI4_MEMBASE_REG:
2342 EMIT_STORE_MEMBASE_REG (ins, st);
2344 case OP_STOREI8_MEMBASE_REG:
2346 EMIT_STORE_MEMBASE_REG (ins, stx);
2348 /* Only used by OP_MEMSET */
2349 EMIT_STORE_MEMBASE_REG (ins, std);
2352 case OP_STORE_MEMBASE_REG:
2353 EMIT_STORE_MEMBASE_REG (ins, sti);
2357 sparc_ldx (code, ins->inst_c0, sparc_g0, ins->dreg);
2359 sparc_ld (code, ins->inst_c0, sparc_g0, ins->dreg);
2364 sparc_ldsw (code, ins->inst_c0, sparc_g0, ins->dreg);
2366 sparc_ld (code, ins->inst_c0, sparc_g0, ins->dreg);
2370 sparc_ld (code, ins->inst_c0, sparc_g0, ins->dreg);
2373 sparc_set (code, ins->inst_c0, ins->dreg);
2374 sparc_ld (code, ins->dreg, sparc_g0, ins->dreg);
2376 case OP_LOADI4_MEMBASE:
2378 EMIT_LOAD_MEMBASE (ins, ldsw);
2380 EMIT_LOAD_MEMBASE (ins, ld);
2383 case OP_LOADU4_MEMBASE:
2384 EMIT_LOAD_MEMBASE (ins, ld);
2386 case OP_LOADU1_MEMBASE:
2387 EMIT_LOAD_MEMBASE (ins, ldub);
2389 case OP_LOADI1_MEMBASE:
2390 EMIT_LOAD_MEMBASE (ins, ldsb);
2392 case OP_LOADU2_MEMBASE:
2393 EMIT_LOAD_MEMBASE (ins, lduh);
2395 case OP_LOADI2_MEMBASE:
2396 EMIT_LOAD_MEMBASE (ins, ldsh);
2398 case OP_LOAD_MEMBASE:
2400 EMIT_LOAD_MEMBASE (ins, ldx);
2402 EMIT_LOAD_MEMBASE (ins, ld);
2406 case OP_LOADI8_MEMBASE:
2407 EMIT_LOAD_MEMBASE (ins, ldx);
2411 sparc_sll_imm (code, ins->sreg1, 24, sparc_o7);
2412 sparc_sra_imm (code, sparc_o7, 24, ins->dreg);
2415 sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
2416 sparc_sra_imm (code, sparc_o7, 16, ins->dreg);
2419 sparc_and_imm (code, FALSE, ins->sreg1, 0xff, ins->dreg);
2422 sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
2423 sparc_srl_imm (code, sparc_o7, 16, ins->dreg);
2425 case CEE_CONV_OVF_U4:
2426 /* Only used on V9 */
2427 sparc_cmp_imm (code, ins->sreg1, 0);
2428 mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code,
2429 MONO_PATCH_INFO_EXC, "OverflowException");
2430 sparc_branchp (code, 0, sparc_bl, sparc_xcc_short, 0, 0);
2432 sparc_set (code, 1, sparc_o7);
2433 sparc_sllx_imm (code, sparc_o7, 32, sparc_o7);
2434 sparc_cmp (code, ins->sreg1, sparc_o7);
2435 mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code,
2436 MONO_PATCH_INFO_EXC, "OverflowException");
2437 sparc_branchp (code, 0, sparc_bge, sparc_xcc_short, 0, 0);
2439 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
2441 case CEE_CONV_OVF_I4_UN:
2442 /* Only used on V9 */
2447 /* Only used on V9 */
2448 sparc_srl_imm (code, ins->sreg1, 0, ins->dreg);
2452 /* Only used on V9 */
2453 sparc_sra_imm (code, ins->sreg1, 0, ins->dreg);
2458 sparc_cmp (code, ins->sreg1, ins->sreg2);
2460 case OP_COMPARE_IMM:
2461 case OP_ICOMPARE_IMM:
2462 if (sparc_is_imm13 (ins->inst_imm))
2463 sparc_cmp_imm (code, ins->sreg1, ins->inst_imm);
2465 sparc_set (code, ins->inst_imm, sparc_o7);
2466 sparc_cmp (code, ins->sreg1, sparc_o7);
2471 * gdb does not like encountering 'ta 1' in the debugged code. So
2472 * instead of emitting a trap, we emit a call a C function and place a
2475 //sparc_ta (code, 1);
2476 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, mono_arch_break);
2481 sparc_add (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2485 sparc_add (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2490 /* according to inssel-long32.brg, this should set cc */
2491 EMIT_ALU_IMM (ins, add, TRUE);
2495 /* according to inssel-long32.brg, this should set cc */
2496 sparc_addx (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2500 EMIT_ALU_IMM (ins, addx, TRUE);
2504 sparc_sub (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2508 sparc_sub (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2513 /* according to inssel-long32.brg, this should set cc */
2514 EMIT_ALU_IMM (ins, sub, TRUE);
2518 /* according to inssel-long32.brg, this should set cc */
2519 sparc_subx (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2523 EMIT_ALU_IMM (ins, subx, TRUE);
2527 sparc_and (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2531 EMIT_ALU_IMM (ins, and, FALSE);
2535 /* Sign extend sreg1 into %y */
2536 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2537 sparc_wry (code, sparc_o7, sparc_g0);
2538 sparc_sdiv (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2539 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2543 sparc_wry (code, sparc_g0, sparc_g0);
2544 sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2549 /* Transform division into a shift */
2550 for (i = 1; i < 30; ++i) {
2552 if (ins->inst_imm == imm)
2558 sparc_srl_imm (code, ins->sreg1, 31, sparc_o7);
2559 sparc_add (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2560 sparc_sra_imm (code, ins->dreg, 1, ins->dreg);
2563 /* http://compilers.iecc.com/comparch/article/93-04-079 */
2564 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2565 sparc_srl_imm (code, sparc_o7, 32 - i, sparc_o7);
2566 sparc_add (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2567 sparc_sra_imm (code, ins->dreg, i, ins->dreg);
2571 /* Sign extend sreg1 into %y */
2572 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2573 sparc_wry (code, sparc_o7, sparc_g0);
2574 EMIT_ALU_IMM (ins, sdiv, TRUE);
2575 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2581 /* Sign extend sreg1 into %y */
2582 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2583 sparc_wry (code, sparc_o7, sparc_g0);
2584 sparc_sdiv (code, TRUE, ins->sreg1, ins->sreg2, sparc_o7);
2585 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2586 sparc_smul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
2587 sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2591 sparc_wry (code, sparc_g0, sparc_g0);
2592 sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, sparc_o7);
2593 sparc_umul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
2594 sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2598 /* Sign extend sreg1 into %y */
2599 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2600 sparc_wry (code, sparc_o7, sparc_g0);
2601 if (!sparc_is_imm13 (ins->inst_imm)) {
2602 sparc_set (code, ins->inst_imm, GP_SCRATCH_REG);
2603 sparc_sdiv (code, TRUE, ins->sreg1, GP_SCRATCH_REG, sparc_o7);
2604 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2605 sparc_smul (code, FALSE, sparc_o7, GP_SCRATCH_REG, sparc_o7);
2608 sparc_sdiv_imm (code, TRUE, ins->sreg1, ins->inst_imm, sparc_o7);
2609 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2610 sparc_smul_imm (code, FALSE, sparc_o7, ins->inst_imm, sparc_o7);
2612 sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2616 sparc_or (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2620 EMIT_ALU_IMM (ins, or, FALSE);
2624 sparc_xor (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2628 EMIT_ALU_IMM (ins, xor, FALSE);
2632 sparc_sll (code, ins->sreg1, ins->sreg2, ins->dreg);
2636 if (ins->inst_imm < (1 << 5))
2637 sparc_sll_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2639 sparc_set (code, ins->inst_imm, sparc_o7);
2640 sparc_sll (code, ins->sreg1, sparc_o7, ins->dreg);
2645 sparc_sra (code, ins->sreg1, ins->sreg2, ins->dreg);
2649 if (ins->inst_imm < (1 << 5))
2650 sparc_sra_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2652 sparc_set (code, ins->inst_imm, sparc_o7);
2653 sparc_sra (code, ins->sreg1, sparc_o7, ins->dreg);
2657 case OP_ISHR_UN_IMM:
2658 if (ins->inst_imm < (1 << 5))
2659 sparc_srl_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2661 sparc_set (code, ins->inst_imm, sparc_o7);
2662 sparc_srl (code, ins->sreg1, sparc_o7, ins->dreg);
2667 sparc_srl (code, ins->sreg1, ins->sreg2, ins->dreg);
2670 sparc_sllx (code, ins->sreg1, ins->sreg2, ins->dreg);
2673 if (ins->inst_imm < (1 << 6))
2674 sparc_sllx_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2676 sparc_set (code, ins->inst_imm, sparc_o7);
2677 sparc_sllx (code, ins->sreg1, sparc_o7, ins->dreg);
2681 sparc_srax (code, ins->sreg1, ins->sreg2, ins->dreg);
2684 if (ins->inst_imm < (1 << 6))
2685 sparc_srax_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2687 sparc_set (code, ins->inst_imm, sparc_o7);
2688 sparc_srax (code, ins->sreg1, sparc_o7, ins->dreg);
2692 sparc_srlx (code, ins->sreg1, ins->sreg2, ins->dreg);
2694 case OP_LSHR_UN_IMM:
2695 if (ins->inst_imm < (1 << 6))
2696 sparc_srlx_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2698 sparc_set (code, ins->inst_imm, sparc_o7);
2699 sparc_srlx (code, ins->sreg1, sparc_o7, ins->dreg);
2704 /* can't use sparc_not */
2705 sparc_xnor (code, FALSE, ins->sreg1, sparc_g0, ins->dreg);
2709 /* can't use sparc_neg */
2710 sparc_sub (code, FALSE, sparc_g0, ins->sreg1, ins->dreg);
2714 sparc_smul (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2720 if ((ins->inst_imm == 1) && (ins->sreg1 == ins->dreg))
2723 /* Transform multiplication into a shift */
2724 for (i = 0; i < 30; ++i) {
2726 if (ins->inst_imm == imm)
2730 sparc_sll_imm (code, ins->sreg1, i, ins->dreg);
2732 EMIT_ALU_IMM (ins, smul, FALSE);
2737 sparc_smul (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2738 sparc_rdy (code, sparc_g1);
2739 sparc_sra_imm (code, ins->dreg, 31, sparc_o7);
2740 sparc_cmp (code, sparc_g1, sparc_o7);
2741 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (ins, sparc_bne, "OverflowException", TRUE, sparc_icc_short);
2743 case CEE_MUL_OVF_UN:
2744 case OP_IMUL_OVF_UN:
2745 sparc_umul (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2746 sparc_rdy (code, sparc_o7);
2747 sparc_cmp (code, sparc_o7, sparc_g0);
2748 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (ins, sparc_bne, "OverflowException", TRUE, sparc_icc_short);
2752 sparc_set (code, ins->inst_c0, ins->dreg);
2755 sparc_set (code, ins->inst_l, ins->dreg);
2758 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2759 sparc_set_template (code, ins->dreg);
2765 if (ins->sreg1 != ins->dreg)
2766 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
2769 /* Only used on V9 */
2770 if (ins->sreg1 != ins->dreg)
2771 sparc_fmovd (code, ins->sreg1, ins->dreg);
2773 case OP_SPARC_SETFREG_FLOAT:
2774 /* Only used on V9 */
2775 sparc_fdtos (code, ins->sreg1, ins->dreg);
2778 if (cfg->method->save_lmf)
2781 code = emit_load_volatile_arguments (cfg, code);
2782 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2783 sparc_set_template (code, sparc_o7);
2784 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_g0);
2785 /* Restore parent frame in delay slot */
2786 sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
2789 /* ensure ins->sreg1 is not NULL */
2790 sparc_ld_imm (code, ins->sreg1, 0, sparc_g0);
2793 sparc_add_imm (code, FALSE, sparc_fp, cfg->sig_cookie, sparc_o7);
2794 sparc_sti_imm (code, sparc_o7, ins->sreg1, 0);
2801 call = (MonoCallInst*)ins;
2802 g_assert (!call->virtual);
2803 code = emit_save_sp_to_lmf (cfg, code);
2804 if (ins->flags & MONO_INST_HAS_METHOD)
2805 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method);
2807 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
2809 code = emit_vret_token (ins, code);
2810 code = emit_move_return_value (ins, code);
2815 case OP_VOIDCALL_REG:
2817 call = (MonoCallInst*)ins;
2818 code = emit_save_sp_to_lmf (cfg, code);
2819 sparc_jmpl (code, ins->sreg1, sparc_g0, sparc_callsite);
2821 * We emit a special kind of nop in the delay slot to tell the
2822 * trampoline code that this is a virtual call, thus an unbox
2823 * trampoline might need to be called.
2826 sparc_or_imm (code, FALSE, sparc_g0, 0xca, sparc_g0);
2830 code = emit_vret_token (ins, code);
2831 code = emit_move_return_value (ins, code);
2833 case OP_FCALL_MEMBASE:
2834 case OP_LCALL_MEMBASE:
2835 case OP_VCALL_MEMBASE:
2836 case OP_VOIDCALL_MEMBASE:
2837 case OP_CALL_MEMBASE:
2838 call = (MonoCallInst*)ins;
2839 code = emit_save_sp_to_lmf (cfg, code);
2840 if (sparc_is_imm13 (ins->inst_offset)) {
2841 sparc_ldi_imm (code, ins->inst_basereg, ins->inst_offset, sparc_o7);
2843 sparc_set (code, ins->inst_offset, sparc_o7);
2844 sparc_ldi (code, ins->inst_basereg, sparc_o7, sparc_o7);
2846 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
2848 sparc_or_imm (code, FALSE, sparc_g0, 0xca, sparc_g0);
2852 code = emit_vret_token (ins, code);
2853 code = emit_move_return_value (ins, code);
2856 if (mono_method_signature (cfg->method)->ret->type == MONO_TYPE_R4)
2857 sparc_fdtos (code, ins->sreg1, sparc_f0);
2860 sparc_fmovd (code, ins->sreg1, ins->dreg);
2862 /* FIXME: Why not use fmovd ? */
2863 sparc_fmovs (code, ins->sreg1, ins->dreg);
2864 sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
2869 g_assert_not_reached ();
2874 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2875 /* Perform stack touching */
2879 /* Keep alignment */
2880 sparc_add_imm (code, FALSE, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1, ins->dreg);
2881 sparc_set (code, ~(MONO_ARCH_FRAME_ALIGNMENT - 1), sparc_o7);
2882 sparc_and (code, FALSE, ins->dreg, sparc_o7, ins->dreg);
2884 if ((ins->flags & MONO_INST_INIT) && (ins->sreg1 == ins->dreg)) {
2886 size_reg = sparc_g4;
2888 size_reg = sparc_g1;
2890 sparc_mov_reg_reg (code, ins->dreg, size_reg);
2893 size_reg = ins->sreg1;
2895 sparc_sub (code, FALSE, sparc_sp, ins->dreg, ins->dreg);
2896 /* Keep %sp valid at all times */
2897 sparc_mov_reg_reg (code, ins->dreg, sparc_sp);
2898 g_assert (sparc_is_imm13 (MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset));
2899 sparc_add_imm (code, FALSE, ins->dreg, MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset, ins->dreg);
2901 if (ins->flags & MONO_INST_INIT) {
2903 /* Initialize memory region */
2904 sparc_cmp_imm (code, size_reg, 0);
2906 sparc_branch (code, 0, sparc_be, 0);
2908 sparc_set (code, 0, sparc_o7);
2909 sparc_sub_imm (code, 0, size_reg, sparcv9 ? 8 : 4, size_reg);
2913 sparc_stx (code, sparc_g0, ins->dreg, sparc_o7);
2915 sparc_st (code, sparc_g0, ins->dreg, sparc_o7);
2916 sparc_cmp (code, sparc_o7, size_reg);
2918 sparc_branch (code, 0, sparc_bl, 0);
2919 sparc_patch (br [2], br [1]);
2921 sparc_add_imm (code, 0, sparc_o7, sparcv9 ? 8 : 4, sparc_o7);
2922 sparc_patch (br [0], code);
2926 case OP_SPARC_LOCALLOC_IMM: {
2927 gint32 offset = ins->inst_c0;
2929 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2930 /* Perform stack touching */
2934 offset = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
2935 if (sparc_is_imm13 (offset))
2936 sparc_sub_imm (code, FALSE, sparc_sp, offset, sparc_sp);
2938 sparc_set (code, offset, sparc_o7);
2939 sparc_sub (code, FALSE, sparc_sp, sparc_o7, sparc_sp);
2941 g_assert (sparc_is_imm13 (MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset));
2942 sparc_add_imm (code, FALSE, sparc_sp, MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset, ins->dreg);
2943 if ((ins->flags & MONO_INST_INIT) && (offset > 0)) {
2949 while (i < offset) {
2951 sparc_stx_imm (code, sparc_g0, ins->dreg, i);
2955 sparc_st_imm (code, sparc_g0, ins->dreg, i);
2961 sparc_set (code, offset, sparc_o7);
2962 sparc_sub_imm (code, 0, sparc_o7, sparcv9 ? 8 : 4, sparc_o7);
2963 /* beginning of loop */
2966 sparc_stx (code, sparc_g0, ins->dreg, sparc_o7);
2968 sparc_st (code, sparc_g0, ins->dreg, sparc_o7);
2969 sparc_cmp_imm (code, sparc_o7, 0);
2971 sparc_branch (code, 0, sparc_bne, 0);
2973 sparc_sub_imm (code, 0, sparc_o7, sparcv9 ? 8 : 4, sparc_o7);
2974 sparc_patch (br [1], br [0]);
2980 /* The return is done in the epilog */
2981 g_assert_not_reached ();
2984 sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
2985 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2986 (gpointer)"mono_arch_throw_exception");
2990 sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
2991 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2992 (gpointer)"mono_arch_rethrow_exception");
2995 case OP_START_HANDLER: {
2997 * The START_HANDLER instruction marks the beginning of a handler
2998 * block. It is called using a call instruction, so %o7 contains
2999 * the return address. Since the handler executes in the same stack
3000 * frame as the method itself, we can't use save/restore to save
3001 * the return address. Instead, we save it into a dedicated
3004 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3005 if (!sparc_is_imm13 (spvar->inst_offset)) {
3006 sparc_set (code, spvar->inst_offset, GP_SCRATCH_REG);
3007 sparc_sti (code, sparc_o7, spvar->inst_basereg, GP_SCRATCH_REG);
3010 sparc_sti_imm (code, sparc_o7, spvar->inst_basereg, spvar->inst_offset);
3013 case OP_ENDFILTER: {
3014 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3015 if (!sparc_is_imm13 (spvar->inst_offset)) {
3016 sparc_set (code, spvar->inst_offset, GP_SCRATCH_REG);
3017 sparc_ldi (code, spvar->inst_basereg, GP_SCRATCH_REG, sparc_o7);
3020 sparc_ldi_imm (code, spvar->inst_basereg, spvar->inst_offset, sparc_o7);
3021 sparc_jmpl_imm (code, sparc_o7, 8, sparc_g0);
3023 sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
3026 case CEE_ENDFINALLY: {
3027 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3028 if (!sparc_is_imm13 (spvar->inst_offset)) {
3029 sparc_set (code, spvar->inst_offset, GP_SCRATCH_REG);
3030 sparc_ldi (code, spvar->inst_basereg, GP_SCRATCH_REG, sparc_o7);
3033 sparc_ldi_imm (code, spvar->inst_basereg, spvar->inst_offset, sparc_o7);
3034 sparc_jmpl_imm (code, sparc_o7, 8, sparc_g0);
3038 case OP_CALL_HANDLER:
3039 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3040 /* This is a jump inside the method, so call_simple works even on V9 */
3041 sparc_call_simple (code, 0);
3045 ins->inst_c0 = (guint8*)code - cfg->native_code;
3048 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
3049 if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
3051 if (ins->flags & MONO_INST_BRLABEL) {
3052 if (ins->inst_i0->inst_c0) {
3053 gint32 disp = (ins->inst_i0->inst_c0 - ((guint8*)code - cfg->native_code)) >> 2;
3054 g_assert (sparc_is_imm22 (disp));
3055 sparc_branch (code, 1, sparc_ba, disp);
3057 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
3058 sparc_branch (code, 1, sparc_ba, 0);
3061 if (ins->inst_target_bb->native_offset) {
3062 gint32 disp = (ins->inst_target_bb->native_offset - ((guint8*)code - cfg->native_code)) >> 2;
3063 g_assert (sparc_is_imm22 (disp));
3064 sparc_branch (code, 1, sparc_ba, disp);
3066 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3067 sparc_branch (code, 1, sparc_ba, 0);
3073 sparc_jmp (code, ins->sreg1, sparc_g0);
3081 if (v64 && (cfg->opt & MONO_OPT_CMOV)) {
3082 sparc_clr_reg (code, ins->dreg);
3083 sparc_movcc_imm (code, sparc_xcc, opcode_to_sparc_cond (ins->opcode), 1, ins->dreg);
3086 sparc_clr_reg (code, ins->dreg);
3088 sparc_branchp (code, 1, opcode_to_sparc_cond (ins->opcode), DEFAULT_ICC, 0, 2);
3090 sparc_branch (code, 1, opcode_to_sparc_cond (ins->opcode), 2);
3093 sparc_set (code, 1, ins->dreg);
3101 if (v64 && (cfg->opt & MONO_OPT_CMOV)) {
3102 sparc_clr_reg (code, ins->dreg);
3103 sparc_movcc_imm (code, sparc_icc, opcode_to_sparc_cond (ins->opcode), 1, ins->dreg);
3106 sparc_clr_reg (code, ins->dreg);
3107 sparc_branchp (code, 1, opcode_to_sparc_cond (ins->opcode), sparc_icc_short, 0, 2);
3109 sparc_set (code, 1, ins->dreg);
3112 case OP_COND_EXC_EQ:
3113 case OP_COND_EXC_NE_UN:
3114 case OP_COND_EXC_LT:
3115 case OP_COND_EXC_LT_UN:
3116 case OP_COND_EXC_GT:
3117 case OP_COND_EXC_GT_UN:
3118 case OP_COND_EXC_GE:
3119 case OP_COND_EXC_GE_UN:
3120 case OP_COND_EXC_LE:
3121 case OP_COND_EXC_LE_UN:
3122 case OP_COND_EXC_OV:
3123 case OP_COND_EXC_NO:
3125 case OP_COND_EXC_NC:
3126 EMIT_COND_SYSTEM_EXCEPTION (ins, opcode_to_sparc_cond (ins->opcode), ins->inst_p1);
3128 case OP_SPARC_COND_EXC_EQZ:
3129 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brz, ins->inst_p1);
3131 case OP_SPARC_COND_EXC_GEZ:
3132 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brgez, ins->inst_p1);
3134 case OP_SPARC_COND_EXC_GTZ:
3135 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brgz, ins->inst_p1);
3137 case OP_SPARC_COND_EXC_LEZ:
3138 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brlez, ins->inst_p1);
3140 case OP_SPARC_COND_EXC_LTZ:
3141 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brlz, ins->inst_p1);
3143 case OP_SPARC_COND_EXC_NEZ:
3144 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brnz, ins->inst_p1);
3146 case OP_COND_EXC_IOV:
3147 case OP_COND_EXC_IC:
3148 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (ins, opcode_to_sparc_cond (ins->opcode), ins->inst_p1, TRUE, sparc_icc_short);
3161 EMIT_COND_BRANCH_PREDICTED (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
3163 EMIT_COND_BRANCH (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
3177 /* Only used on V9 */
3178 EMIT_COND_BRANCH_ICC (ins, opcode_to_sparc_cond (ins->opcode), 1, 1, sparc_icc_short);
3183 EMIT_COND_BRANCH_BPR (ins, brz, 1, 1, 1);
3185 case OP_SPARC_BRLEZ:
3186 EMIT_COND_BRANCH_BPR (ins, brlez, 1, 1, 1);
3189 EMIT_COND_BRANCH_BPR (ins, brlz, 1, 1, 1);
3192 EMIT_COND_BRANCH_BPR (ins, brnz, 1, 1, 1);
3195 EMIT_COND_BRANCH_BPR (ins, brgz, 1, 1, 1);
3197 case OP_SPARC_BRGEZ:
3198 EMIT_COND_BRANCH_BPR (ins, brgez, 1, 1, 1);
3201 /* floating point opcodes */
3203 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
3205 sparc_set_template (code, sparc_o7);
3207 sparc_sethi (code, 0, sparc_o7);
3209 sparc_lddf_imm (code, sparc_o7, 0, ins->dreg);
3212 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
3214 sparc_set_template (code, sparc_o7);
3216 sparc_sethi (code, 0, sparc_o7);
3218 sparc_ldf_imm (code, sparc_o7, 0, FP_SCRATCH_REG);
3220 /* Extend to double */
3221 sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
3223 case OP_STORER8_MEMBASE_REG:
3224 if (!sparc_is_imm13 (ins->inst_offset + 4)) {
3225 sparc_set (code, ins->inst_offset, sparc_o7);
3226 /* SPARCV9 handles misaligned fp loads/stores */
3227 if (!v64 && (ins->inst_offset % 8)) {
3229 sparc_add (code, FALSE, ins->inst_destbasereg, sparc_o7, sparc_o7);
3230 sparc_stf (code, ins->sreg1, sparc_o7, sparc_g0);
3231 sparc_stf_imm (code, ins->sreg1 + 1, sparc_o7, 4);
3233 sparc_stdf (code, ins->sreg1, ins->inst_destbasereg, sparc_o7);
3236 if (!v64 && (ins->inst_offset % 8)) {
3238 sparc_stf_imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3239 sparc_stf_imm (code, ins->sreg1 + 1, ins->inst_destbasereg, ins->inst_offset + 4);
3241 sparc_stdf_imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3244 case OP_LOADR8_MEMBASE:
3245 EMIT_LOAD_MEMBASE (ins, lddf);
3247 case OP_STORER4_MEMBASE_REG:
3248 /* This requires a double->single conversion */
3249 sparc_fdtos (code, ins->sreg1, FP_SCRATCH_REG);
3250 if (!sparc_is_imm13 (ins->inst_offset)) {
3251 sparc_set (code, ins->inst_offset, sparc_o7);
3252 sparc_stf (code, FP_SCRATCH_REG, ins->inst_destbasereg, sparc_o7);
3255 sparc_stf_imm (code, FP_SCRATCH_REG, ins->inst_destbasereg, ins->inst_offset);
3257 case OP_LOADR4_MEMBASE: {
3258 /* ldf needs a single precision register */
3259 int dreg = ins->dreg;
3260 ins->dreg = FP_SCRATCH_REG;
3261 EMIT_LOAD_MEMBASE (ins, ldf);
3263 /* Extend to double */
3264 sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
3269 sparc_fmovd (code, ins->sreg1, ins->dreg);
3271 sparc_fmovs (code, ins->sreg1, ins->dreg);
3272 sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
3276 gint32 offset = mono_spillvar_offset_float (cfg, 0);
3278 if (!sparc_is_imm13 (offset)) {
3279 sparc_set (code, offset, sparc_o7);
3280 sparc_stx (code, ins->sreg1, sparc_sp, offset);
3281 sparc_lddf (code, sparc_sp, offset, FP_SCRATCH_REG);
3283 sparc_stx_imm (code, ins->sreg1, sparc_sp, offset);
3284 sparc_lddf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3286 sparc_fxtos (code, FP_SCRATCH_REG, FP_SCRATCH_REG);
3288 if (!sparc_is_imm13 (offset)) {
3289 sparc_set (code, offset, sparc_o7);
3290 sparc_st (code, ins->sreg1, sparc_sp, sparc_o7);
3291 sparc_ldf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
3293 sparc_st_imm (code, ins->sreg1, sparc_sp, offset);
3294 sparc_ldf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3296 sparc_fitos (code, FP_SCRATCH_REG, FP_SCRATCH_REG);
3298 sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
3302 gint32 offset = mono_spillvar_offset_float (cfg, 0);
3304 if (!sparc_is_imm13 (offset)) {
3305 sparc_set (code, offset, sparc_o7);
3306 sparc_stx (code, ins->sreg1, sparc_sp, sparc_o7);
3307 sparc_lddf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
3309 sparc_stx_imm (code, ins->sreg1, sparc_sp, offset);
3310 sparc_lddf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3312 sparc_fxtod (code, FP_SCRATCH_REG, ins->dreg);
3314 if (!sparc_is_imm13 (offset)) {
3315 sparc_set (code, offset, sparc_o7);
3316 sparc_st (code, ins->sreg1, sparc_sp, sparc_o7);
3317 sparc_ldf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
3319 sparc_st_imm (code, ins->sreg1, sparc_sp, offset);
3320 sparc_ldf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3322 sparc_fitod (code, FP_SCRATCH_REG, ins->dreg);
3326 case OP_FCONV_TO_I1:
3327 case OP_FCONV_TO_U1:
3328 case OP_FCONV_TO_I2:
3329 case OP_FCONV_TO_U2:
3334 case OP_FCONV_TO_I4:
3335 case OP_FCONV_TO_U4: {
3336 gint32 offset = mono_spillvar_offset_float (cfg, 0);
3337 sparc_fdtoi (code, ins->sreg1, FP_SCRATCH_REG);
3338 if (!sparc_is_imm13 (offset)) {
3339 sparc_set (code, offset, sparc_o7);
3340 sparc_stdf (code, FP_SCRATCH_REG, sparc_sp, sparc_o7);
3341 sparc_ld (code, sparc_sp, sparc_o7, ins->dreg);
3343 sparc_stdf_imm (code, FP_SCRATCH_REG, sparc_sp, offset);
3344 sparc_ld_imm (code, sparc_sp, offset, ins->dreg);
3347 switch (ins->opcode) {
3348 case OP_FCONV_TO_I1:
3349 case OP_FCONV_TO_U1:
3350 sparc_and_imm (code, 0, ins->dreg, 0xff, ins->dreg);
3352 case OP_FCONV_TO_I2:
3353 case OP_FCONV_TO_U2:
3354 sparc_set (code, 0xffff, sparc_o7);
3355 sparc_and (code, 0, ins->dreg, sparc_o7, ins->dreg);
3362 case OP_FCONV_TO_I8:
3363 case OP_FCONV_TO_U8:
3365 g_assert_not_reached ();
3369 g_assert_not_reached ();
3371 case OP_LCONV_TO_R_UN: {
3373 g_assert_not_reached ();
3376 case OP_LCONV_TO_OVF_I: {
3377 guint32 *br [3], *label [1];
3380 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
3382 sparc_cmp_imm (code, ins->sreg1, 0);
3384 sparc_branch (code, 1, sparc_bneg, 0);
3388 /* ms word must be 0 */
3389 sparc_cmp_imm (code, ins->sreg2, 0);
3391 sparc_branch (code, 1, sparc_be, 0);
3396 EMIT_COND_SYSTEM_EXCEPTION (ins, sparc_ba, "OverflowException");
3399 sparc_patch (br [0], code);
3401 /* ms word must 0xfffffff */
3402 sparc_cmp_imm (code, ins->sreg2, -1);
3404 sparc_branch (code, 1, sparc_bne, 0);
3406 sparc_patch (br [2], label [0]);
3409 sparc_patch (br [1], code);
3410 if (ins->sreg1 != ins->dreg)
3411 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
3415 sparc_faddd (code, ins->sreg1, ins->sreg2, ins->dreg);
3418 sparc_fsubd (code, ins->sreg1, ins->sreg2, ins->dreg);
3421 sparc_fmuld (code, ins->sreg1, ins->sreg2, ins->dreg);
3424 sparc_fdivd (code, ins->sreg1, ins->sreg2, ins->dreg);
3428 sparc_fnegd (code, ins->sreg1, ins->dreg);
3430 /* FIXME: why don't use fnegd ? */
3431 sparc_fnegs (code, ins->sreg1, ins->dreg);
3435 sparc_fdivd (code, ins->sreg1, ins->sreg2, FP_SCRATCH_REG);
3436 sparc_fmuld (code, ins->sreg2, FP_SCRATCH_REG, FP_SCRATCH_REG);
3437 sparc_fsubd (code, ins->sreg1, FP_SCRATCH_REG, ins->dreg);
3440 sparc_fcmpd (code, ins->sreg1, ins->sreg2);
3447 sparc_fcmpd (code, ins->sreg1, ins->sreg2);
3448 sparc_clr_reg (code, ins->dreg);
3449 switch (ins->opcode) {
3452 sparc_fbranch (code, 1, opcode_to_sparc_cond (ins->opcode), 4);
3454 sparc_set (code, 1, ins->dreg);
3455 sparc_fbranch (code, 1, sparc_fbu, 2);
3457 sparc_set (code, 1, ins->dreg);
3460 sparc_fbranch (code, 1, opcode_to_sparc_cond (ins->opcode), 2);
3462 sparc_set (code, 1, ins->dreg);
3468 EMIT_FLOAT_COND_BRANCH (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
3471 /* clt.un + brfalse */
3473 sparc_fbranch (code, 1, sparc_fbul, 0);
3476 EMIT_FLOAT_COND_BRANCH (ins, sparc_fba, 1, 1);
3477 sparc_patch (p, (guint8*)code);
3481 /* cgt.un + brfalse */
3483 sparc_fbranch (code, 1, sparc_fbug, 0);
3486 EMIT_FLOAT_COND_BRANCH (ins, sparc_fba, 1, 1);
3487 sparc_patch (p, (guint8*)code);
3491 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbne, 1, 1);
3492 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3495 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbl, 1, 1);
3496 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3499 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbg, 1, 1);
3500 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3503 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbge, 1, 1);
3504 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3507 EMIT_FLOAT_COND_BRANCH (ins, sparc_fble, 1, 1);
3508 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3510 case CEE_CKFINITE: {
3511 gint32 offset = mono_spillvar_offset_float (cfg, 0);
3512 if (!sparc_is_imm13 (offset)) {
3513 sparc_set (code, offset, sparc_o7);
3514 sparc_stdf (code, ins->sreg1, sparc_sp, sparc_o7);
3515 sparc_lduh (code, sparc_sp, sparc_o7, sparc_o7);
3517 sparc_stdf_imm (code, ins->sreg1, sparc_sp, offset);
3518 sparc_lduh_imm (code, sparc_sp, offset, sparc_o7);
3520 sparc_srl_imm (code, sparc_o7, 4, sparc_o7);
3521 sparc_and_imm (code, FALSE, sparc_o7, 2047, sparc_o7);
3522 sparc_cmp_imm (code, sparc_o7, 2047);
3523 EMIT_COND_SYSTEM_EXCEPTION (ins, sparc_be, "ArithmeticException");
3525 sparc_fmovd (code, ins->sreg1, ins->dreg);
3527 sparc_fmovs (code, ins->sreg1, ins->dreg);
3528 sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
3533 case OP_MEMORY_BARRIER:
3534 sparc_membar (code, sparc_membar_all);
3539 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3541 g_warning ("%s:%d: unknown opcode %s\n", __FILE__, __LINE__, mono_inst_name (ins->opcode));
3543 g_assert_not_reached ();
3546 if ((((guint8*)code) - code_start) > max_len) {
3547 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3548 mono_inst_name (ins->opcode), max_len, ((guint8*)code) - code_start);
3549 g_assert_not_reached ();
3559 cfg->code_len = (guint8*)code - cfg->native_code;
3563 mono_arch_register_lowlevel_calls (void)
3565 mono_register_jit_icall (mono_arch_break, "mono_arch_break", NULL, TRUE);
3566 mono_register_jit_icall (mono_arch_get_lmf_addr, "mono_arch_get_lmf_addr", NULL, TRUE);
3570 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3572 MonoJumpInfo *patch_info;
3574 /* FIXME: Move part of this to arch independent code */
3575 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3576 unsigned char *ip = patch_info->ip.i + code;
3579 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
3581 switch (patch_info->type) {
3582 case MONO_PATCH_INFO_NONE:
3584 case MONO_PATCH_INFO_CLASS_INIT: {
3585 guint32 *ip2 = (guint32*)ip;
3586 /* Might already been changed to a nop */
3588 sparc_set_template (ip2, sparc_o7);
3589 sparc_jmpl (ip2, sparc_o7, sparc_g0, sparc_o7);
3591 sparc_call_simple (ip2, 0);
3595 case MONO_PATCH_INFO_METHOD_JUMP: {
3596 guint32 *ip2 = (guint32*)ip;
3597 /* Might already been patched */
3598 sparc_set_template (ip2, sparc_o7);
3604 sparc_patch ((guint32*)ip, target);
3609 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
3612 guint32 *code = (guint32*)p;
3613 MonoMethodSignature *sig = mono_method_signature (cfg->method);
3616 /* Save registers to stack */
3617 for (i = 0; i < 6; ++i)
3618 sparc_sti_imm (code, sparc_i0 + i, sparc_fp, ARGS_OFFSET + (i * sizeof (gpointer)));
3620 cinfo = get_call_info (sig, FALSE);
3622 /* Save float regs on V9, since they are caller saved */
3623 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3624 ArgInfo *ainfo = cinfo->args + i;
3625 gint32 stack_offset;
3627 stack_offset = ainfo->offset + ARGS_OFFSET;
3629 if (ainfo->storage == ArgInFloatReg) {
3630 if (!sparc_is_imm13 (stack_offset))
3632 sparc_stf_imm (code, ainfo->reg, sparc_fp, stack_offset);
3634 else if (ainfo->storage == ArgInDoubleReg) {
3635 /* The offset is guaranteed to be aligned by the ABI rules */
3636 sparc_stdf_imm (code, ainfo->reg, sparc_fp, stack_offset);
3640 sparc_set (code, cfg->method, sparc_o0);
3641 sparc_add_imm (code, FALSE, sparc_fp, MONO_SPARC_STACK_BIAS, sparc_o1);
3643 mono_add_patch_info (cfg, (guint8*)code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
3646 /* Restore float regs on V9 */
3647 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3648 ArgInfo *ainfo = cinfo->args + i;
3649 gint32 stack_offset;
3651 stack_offset = ainfo->offset + ARGS_OFFSET;
3653 if (ainfo->storage == ArgInFloatReg) {
3654 if (!sparc_is_imm13 (stack_offset))
3656 sparc_ldf_imm (code, sparc_fp, stack_offset, ainfo->reg);
3658 else if (ainfo->storage == ArgInDoubleReg) {
3659 /* The offset is guaranteed to be aligned by the ABI rules */
3660 sparc_lddf_imm (code, sparc_fp, stack_offset, ainfo->reg);
3678 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
3680 guint32 *code = (guint32*)p;
3681 int save_mode = SAVE_NONE;
3682 MonoMethod *method = cfg->method;
3684 switch (mono_type_get_underlying_type (mono_method_signature (method)->ret)->type) {
3685 case MONO_TYPE_VOID:
3686 /* special case string .ctor icall */
3687 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
3688 save_mode = SAVE_ONE;
3690 save_mode = SAVE_NONE;
3695 save_mode = SAVE_ONE;
3697 save_mode = SAVE_TWO;
3702 save_mode = SAVE_FP;
3704 case MONO_TYPE_VALUETYPE:
3705 save_mode = SAVE_STRUCT;
3708 save_mode = SAVE_ONE;
3712 /* Save the result to the stack and also put it into the output registers */
3714 switch (save_mode) {
3717 sparc_st_imm (code, sparc_i0, sparc_fp, 68);
3718 sparc_st_imm (code, sparc_i0, sparc_fp, 72);
3719 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
3720 sparc_mov_reg_reg (code, sparc_i1, sparc_o2);
3723 sparc_sti_imm (code, sparc_i0, sparc_fp, ARGS_OFFSET);
3724 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
3728 sparc_stdf_imm (code, sparc_f0, sparc_fp, ARGS_OFFSET);
3730 sparc_stdf_imm (code, sparc_f0, sparc_fp, 72);
3731 sparc_ld_imm (code, sparc_fp, 72, sparc_o1);
3732 sparc_ld_imm (code, sparc_fp, 72 + 4, sparc_o2);
3737 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
3739 sparc_ld_imm (code, sparc_fp, 64, sparc_o1);
3747 sparc_set (code, cfg->method, sparc_o0);
3749 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, func);
3752 /* Restore result */
3754 switch (save_mode) {
3756 sparc_ld_imm (code, sparc_fp, 68, sparc_i0);
3757 sparc_ld_imm (code, sparc_fp, 72, sparc_i0);
3760 sparc_ldi_imm (code, sparc_fp, ARGS_OFFSET, sparc_i0);
3763 sparc_lddf_imm (code, sparc_fp, ARGS_OFFSET, sparc_f0);
3774 mono_arch_emit_prolog (MonoCompile *cfg)
3776 MonoMethod *method = cfg->method;
3777 MonoMethodSignature *sig;
3783 cfg->code_size = 256;
3784 cfg->native_code = g_malloc (cfg->code_size);
3785 code = (guint32*)cfg->native_code;
3787 /* FIXME: Generate intermediate code instead */
3789 offset = cfg->stack_offset;
3790 offset += (16 * sizeof (gpointer)); /* register save area */
3792 offset += 4; /* struct/union return pointer */
3795 /* add parameter area size for called functions */
3796 if (cfg->param_area < (6 * sizeof (gpointer)))
3797 /* Reserve space for the first 6 arguments even if it is unused */
3798 offset += 6 * sizeof (gpointer);
3800 offset += cfg->param_area;
3802 /* align the stack size */
3803 offset = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
3806 * localloc'd memory is stored between the local variables (whose
3807 * size is given by cfg->stack_offset), and between the space reserved
3810 cfg->arch.localloc_offset = offset - cfg->stack_offset;
3812 cfg->stack_offset = offset;
3814 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
3815 /* Perform stack touching */
3819 if (!sparc_is_imm13 (- cfg->stack_offset)) {
3820 /* Can't use sparc_o7 here, since we're still in the caller's frame */
3821 sparc_set (code, (- cfg->stack_offset), GP_SCRATCH_REG);
3822 sparc_save (code, sparc_sp, GP_SCRATCH_REG, sparc_sp);
3825 sparc_save_imm (code, sparc_sp, - cfg->stack_offset, sparc_sp);
3828 if (strstr (cfg->method->name, "foo")) {
3829 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, mono_sparc_break);
3830 sparc_call_simple (code, 0);
3835 sig = mono_method_signature (method);
3837 cinfo = get_call_info (sig, FALSE);
3839 /* Keep in sync with emit_load_volatile_arguments */
3840 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3841 ArgInfo *ainfo = cinfo->args + i;
3842 gint32 stack_offset;
3844 inst = cfg->varinfo [i];
3846 if (sig->hasthis && (i == 0))
3847 arg_type = &mono_defaults.object_class->byval_arg;
3849 arg_type = sig->params [i - sig->hasthis];
3851 stack_offset = ainfo->offset + ARGS_OFFSET;
3853 /* Save the split arguments so they will reside entirely on the stack */
3854 if (ainfo->storage == ArgInSplitRegStack) {
3855 /* Save the register to the stack */
3856 g_assert (inst->opcode == OP_REGOFFSET);
3857 if (!sparc_is_imm13 (stack_offset))
3859 sparc_st_imm (code, sparc_i5, inst->inst_basereg, stack_offset);
3862 if (!v64 && !arg_type->byref && (arg_type->type == MONO_TYPE_R8)) {
3863 /* Save the argument to a dword aligned stack location */
3865 * stack_offset contains the offset of the argument on the stack.
3866 * inst->inst_offset contains the dword aligned offset where the value
3869 if (ainfo->storage == ArgInIRegPair) {
3870 if (!sparc_is_imm13 (inst->inst_offset + 4))
3872 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, inst->inst_offset);
3873 sparc_st_imm (code, sparc_i0 + ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
3876 if (ainfo->storage == ArgInSplitRegStack) {
3878 g_assert_not_reached ();
3880 if (stack_offset != inst->inst_offset) {
3881 /* stack_offset is not dword aligned, so we need to make a copy */
3882 sparc_st_imm (code, sparc_i5, inst->inst_basereg, inst->inst_offset);
3883 sparc_ld_imm (code, sparc_fp, stack_offset + 4, sparc_o7);
3884 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset + 4);
3888 if (ainfo->storage == ArgOnStackPair) {
3890 g_assert_not_reached ();
3892 if (stack_offset != inst->inst_offset) {
3893 /* stack_offset is not dword aligned, so we need to make a copy */
3894 sparc_ld_imm (code, sparc_fp, stack_offset, sparc_o7);
3895 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset);
3896 sparc_ld_imm (code, sparc_fp, stack_offset + 4, sparc_o7);
3897 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset + 4);
3901 g_assert_not_reached ();
3904 if ((ainfo->storage == ArgInIReg) && (inst->opcode != OP_REGVAR)) {
3905 /* Argument in register, but need to be saved to stack */
3906 if (!sparc_is_imm13 (stack_offset))
3908 if ((stack_offset - ARGS_OFFSET) & 0x1)
3909 sparc_stb_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
3911 if ((stack_offset - ARGS_OFFSET) & 0x2)
3912 sparc_sth_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
3914 if ((stack_offset - ARGS_OFFSET) & 0x4)
3915 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
3918 sparc_stx_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
3920 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
3924 if ((ainfo->storage == ArgInIRegPair) && (inst->opcode != OP_REGVAR)) {
3928 /* Argument in regpair, but need to be saved to stack */
3929 if (!sparc_is_imm13 (inst->inst_offset + 4))
3931 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, inst->inst_offset);
3932 sparc_st_imm (code, sparc_i0 + ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
3934 else if ((ainfo->storage == ArgInFloatReg) && (inst->opcode != OP_REGVAR)) {
3935 if (!sparc_is_imm13 (stack_offset))
3937 sparc_stf_imm (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
3939 else if ((ainfo->storage == ArgInDoubleReg) && (inst->opcode != OP_REGVAR)) {
3940 /* The offset is guaranteed to be aligned by the ABI rules */
3941 sparc_stdf_imm (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
3944 if ((ainfo->storage == ArgInFloatReg) && (inst->opcode == OP_REGVAR)) {
3945 /* Need to move into the a double precision register */
3946 sparc_fstod (code, ainfo->reg, ainfo->reg - 1);
3949 if ((ainfo->storage == ArgInSplitRegStack) || (ainfo->storage == ArgOnStack))
3950 if (inst->opcode == OP_REGVAR)
3951 /* FIXME: Load the argument into memory */
3957 if (cfg->method->save_lmf) {
3958 gint32 lmf_offset = STACK_BIAS - cfg->arch.lmf_offset;
3961 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3962 sparc_set_template (code, sparc_o7);
3963 sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ip));
3965 sparc_sti_imm (code, sparc_sp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, sp));
3967 sparc_sti_imm (code, sparc_fp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp));
3969 /* FIXME: add a relocation for this */
3970 sparc_set (code, cfg->method, sparc_o7);
3971 sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method));
3973 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3974 (gpointer)"mono_arch_get_lmf_addr");
3977 code = (guint32*)mono_sparc_emit_save_lmf (code, lmf_offset);
3980 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3981 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
3983 cfg->code_len = (guint8*)code - cfg->native_code;
3985 g_assert (cfg->code_len <= cfg->code_size);
3987 return (guint8*)code;
3991 mono_arch_emit_epilog (MonoCompile *cfg)
3993 MonoMethod *method = cfg->method;
3996 int max_epilog_size = 16 + 20 * 4;
3998 if (cfg->method->save_lmf)
3999 max_epilog_size += 128;
4001 if (mono_jit_trace_calls != NULL)
4002 max_epilog_size += 50;
4004 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
4005 max_epilog_size += 50;
4007 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
4008 cfg->code_size *= 2;
4009 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4010 mono_jit_stats.code_reallocs++;
4013 code = (guint32*)(cfg->native_code + cfg->code_len);
4015 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4016 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
4018 if (cfg->method->save_lmf) {
4019 gint32 lmf_offset = STACK_BIAS - cfg->arch.lmf_offset;
4021 code = mono_sparc_emit_restore_lmf (code, lmf_offset);
4025 * The V8 ABI requires that calls to functions which return a structure
4028 if (!v64 && mono_method_signature (cfg->method)->pinvoke && MONO_TYPE_ISSTRUCT(mono_method_signature (cfg->method)->ret))
4029 sparc_jmpl_imm (code, sparc_i7, 12, sparc_g0);
4033 /* Only fold last instruction into the restore if the exit block has an in count of 1
4034 and the previous block hasn't been optimized away since it may have an in count > 1 */
4035 if (cfg->bb_exit->in_count == 1 && cfg->bb_exit->in_bb[0]->native_offset != cfg->bb_exit->native_offset)
4038 /* Try folding last instruction into the restore */
4039 if (can_fold && (sparc_inst_op (code [-2]) == 0x2) && (sparc_inst_op3 (code [-2]) == 0x2) && sparc_inst_imm (code [-2]) && (sparc_inst_rd (code [-2]) == sparc_i0)) {
4040 /* or reg, imm, %i0 */
4041 int reg = sparc_inst_rs1 (code [-2]);
4042 int imm = sparc_inst_imm13 (code [-2]);
4043 code [-2] = code [-1];
4045 sparc_restore_imm (code, reg, imm, sparc_o0);
4048 if (can_fold && (sparc_inst_op (code [-2]) == 0x2) && (sparc_inst_op3 (code [-2]) == 0x2) && (!sparc_inst_imm (code [-2])) && (sparc_inst_rd (code [-2]) == sparc_i0)) {
4049 /* or reg, reg, %i0 */
4050 int reg1 = sparc_inst_rs1 (code [-2]);
4051 int reg2 = sparc_inst_rs2 (code [-2]);
4052 code [-2] = code [-1];
4054 sparc_restore (code, reg1, reg2, sparc_o0);
4057 sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
4059 cfg->code_len = (guint8*)code - cfg->native_code;
4061 g_assert (cfg->code_len < cfg->code_size);
4066 mono_arch_emit_exceptions (MonoCompile *cfg)
4068 MonoJumpInfo *patch_info;
4073 MonoClass *exc_classes [16];
4074 guint8 *exc_throw_start [16], *exc_throw_end [16];
4076 /* Compute needed space */
4077 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4078 if (patch_info->type == MONO_PATCH_INFO_EXC)
4083 * make sure we have enough space for exceptions
4086 code_size = exc_count * (20 * 4);
4088 code_size = exc_count * 24;
4091 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
4092 cfg->code_size *= 2;
4093 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4094 mono_jit_stats.code_reallocs++;
4097 code = (guint32*)(cfg->native_code + cfg->code_len);
4099 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4100 switch (patch_info->type) {
4101 case MONO_PATCH_INFO_EXC: {
4102 MonoClass *exc_class;
4103 guint32 *buf, *buf2;
4104 guint32 throw_ip, type_idx;
4107 sparc_patch ((guint32*)(cfg->native_code + patch_info->ip.i), code);
4109 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
4110 type_idx = exc_class->type_token - MONO_TOKEN_TYPE_DEF;
4111 g_assert (exc_class);
4112 throw_ip = patch_info->ip.i;
4114 /* Find a throw sequence for the same exception class */
4115 for (i = 0; i < nthrows; ++i)
4116 if (exc_classes [i] == exc_class)
4120 guint32 throw_offset = (((guint8*)exc_throw_end [i] - cfg->native_code) - throw_ip) >> 2;
4121 if (!sparc_is_imm13 (throw_offset))
4122 sparc_set32 (code, throw_offset, sparc_o1);
4124 disp = (exc_throw_start [i] - (guint8*)code) >> 2;
4125 g_assert (sparc_is_imm22 (disp));
4126 sparc_branch (code, 0, sparc_ba, disp);
4127 if (sparc_is_imm13 (throw_offset))
4128 sparc_set32 (code, throw_offset, sparc_o1);
4131 patch_info->type = MONO_PATCH_INFO_NONE;
4134 /* Emit the template for setting o1 */
4136 if (sparc_is_imm13 (((((guint8*)code - cfg->native_code) - throw_ip) >> 2) - 8))
4137 /* Can use a short form */
4140 sparc_set_template (code, sparc_o1);
4144 exc_classes [nthrows] = exc_class;
4145 exc_throw_start [nthrows] = (guint8*)code;
4149 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, mono_sparc_break);
4153 /* first arg = type token */
4154 /* Pass the type index to reduce the size of the sparc_set */
4155 if (!sparc_is_imm13 (type_idx))
4156 sparc_set32 (code, type_idx, sparc_o0);
4158 /* second arg = offset between the throw ip and the current ip */
4159 /* On sparc, the saved ip points to the call instruction */
4160 disp = (((guint8*)code - cfg->native_code) - throw_ip) >> 2;
4161 sparc_set32 (buf, disp, sparc_o1);
4166 exc_throw_end [nthrows] = (guint8*)code;
4170 patch_info->data.name = "mono_arch_throw_corlib_exception";
4171 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
4172 patch_info->ip.i = (guint8*)code - cfg->native_code;
4176 if (sparc_is_imm13 (type_idx)) {
4177 /* Put it into the delay slot */
4180 sparc_set32 (code, type_idx, sparc_o0);
4181 g_assert (code - buf == 1);
4192 cfg->code_len = (guint8*)code - cfg->native_code;
4194 g_assert (cfg->code_len < cfg->code_size);
4198 gboolean lmf_addr_key_inited = FALSE;
4200 #ifdef MONO_SPARC_THR_TLS
4201 thread_key_t lmf_addr_key;
4203 pthread_key_t lmf_addr_key;
4207 mono_arch_get_lmf_addr (void)
4209 /* This is perf critical so we bypass the IO layer */
4210 /* The thr_... functions seem to be somewhat faster */
4211 #ifdef MONO_SPARC_THR_TLS
4213 thr_getspecific (lmf_addr_key, &res);
4216 return pthread_getspecific (lmf_addr_key);
4220 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
4223 * There seems to be no way to determine stack boundaries under solaris,
4224 * so it's not possible to determine whenever a SIGSEGV is caused by stack
4227 #error "--with-sigaltstack=yes not supported on solaris"
4230 setup_stack (MonoJitTlsData *tls)
4233 struct sigaltstack sa;
4238 /* Setup an alternate signal stack */
4239 tls->signal_stack = mmap (0, SIGNAL_STACK_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
4240 tls->signal_stack_size = SIGNAL_STACK_SIZE;
4243 sa.ss_sp = tls->signal_stack;
4244 sa.ss_size = SIGNAL_STACK_SIZE;
4246 g_assert (sigaltstack (&sa, NULL) == 0);
4248 sigstk.ss_sp = tls->signal_stack;
4249 sigstk.ss_size = SIGNAL_STACK_SIZE;
4250 sigstk.ss_flags = 0;
4251 g_assert (sigaltstack (&sigstk, NULL) == 0);
4258 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
4260 if (!lmf_addr_key_inited) {
4263 lmf_addr_key_inited = TRUE;
4265 #ifdef MONO_SPARC_THR_TLS
4266 res = thr_keycreate (&lmf_addr_key, NULL);
4268 res = pthread_key_create (&lmf_addr_key, NULL);
4270 g_assert (res == 0);
4274 #ifdef MONO_SPARC_THR_TLS
4275 thr_setspecific (lmf_addr_key, &tls->lmf);
4277 pthread_setspecific (lmf_addr_key, &tls->lmf);
4280 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
4286 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
4291 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *call, int this_reg, int this_type, int vt_reg)
4293 int this_out_reg = sparc_o0;
4298 MONO_INST_NEW (cfg, ins, OP_SETREG);
4299 ins->sreg1 = vt_reg;
4300 ins->dreg = mono_regstate_next_int (cfg->rs);
4301 mono_bblock_add_inst (cfg->cbb, ins);
4303 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, sparc_o0, FALSE);
4305 this_out_reg = sparc_o1;
4307 /* Set the 'struct/union return pointer' location on the stack */
4308 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, sparc_sp, 64, vt_reg);
4312 /* add the this argument */
4313 if (this_reg != -1) {
4315 MONO_INST_NEW (cfg, this, OP_SETREG);
4316 this->type = this_type;
4317 this->sreg1 = this_reg;
4318 this->dreg = mono_regstate_next_int (cfg->rs);
4319 mono_bblock_add_inst (cfg->cbb, this);
4321 mono_call_inst_add_outarg_reg (cfg, call, this->dreg, this_out_reg, FALSE);
4327 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4329 MonoInst *ins = NULL;
4331 if (cmethod->klass == mono_defaults.thread_class &&
4332 strcmp (cmethod->name, "MemoryBarrier") == 0) {
4334 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
4341 * mono_arch_get_argument_info:
4342 * @csig: a method signature
4343 * @param_count: the number of parameters to consider
4344 * @arg_info: an array to store the result infos
4346 * Gathers information on parameters such as size, alignment and
4347 * padding. arg_info should be large enought to hold param_count + 1 entries.
4349 * Returns the size of the activation frame.
4352 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
4358 cinfo = get_call_info (csig, FALSE);
4360 if (csig->hasthis) {
4361 ainfo = &cinfo->args [0];
4362 arg_info [0].offset = ARGS_OFFSET - MONO_SPARC_STACK_BIAS + ainfo->offset;
4365 for (k = 0; k < param_count; k++) {
4366 ainfo = &cinfo->args [k + csig->hasthis];
4368 arg_info [k + 1].offset = ARGS_OFFSET - MONO_SPARC_STACK_BIAS + ainfo->offset;
4369 arg_info [k + 1].size = mono_type_size (csig->params [k], &align);
4378 mono_arch_print_tree (MonoInst *tree, int arity)
4383 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
4388 MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)