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 * Initialize architecture specific code.
218 mono_arch_init (void)
223 * Cleanup architecture specific code.
226 mono_arch_cleanup (void)
231 * This function returns the optimizations supported on this cpu.
234 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
242 if (!sysinfo (SI_ISALIST, buf, 1024))
243 g_assert_not_reached ();
245 /* From glibc. If the getpagesize is 8192, we're on sparc64, which
246 * (in)directly implies that we're a v9 or better.
247 * Improvements to this are greatly accepted...
248 * Also, we don't differentiate between v7 and v8. I sense SIGILL
249 * sniffing in my future.
251 if (getpagesize() == 8192)
252 strcpy (buf, "sparcv9");
254 strcpy (buf, "sparcv8");
258 * On some processors, the cmov instructions are even slower than the
261 if (strstr (buf, "sparcv9")) {
262 opts |= MONO_OPT_CMOV | MONO_OPT_FCMOV;
266 *exclude_mask |= MONO_OPT_CMOV | MONO_OPT_FCMOV;
272 mono_arch_break (void)
277 #define flushi(addr) __asm__ __volatile__ ("iflush %0"::"r"(addr):"memory")
278 #else /* assume Sun's compiler */
279 static void flushi(void *addr)
286 void sync_instruction_memory(caddr_t addr, int len);
290 mono_arch_flush_icache (guint8 *code, gint size)
293 /* Hopefully this is optimized based on the actual CPU */
294 sync_instruction_memory (code, size);
296 gulong start = (gulong) code;
297 gulong end = start + size;
300 /* Sparcv9 chips only need flushes on 32 byte
301 * cacheline boundaries.
303 * Sparcv8 needs a flush every 8 bytes.
305 align = (sparcv9 ? 32 : 8);
307 start &= ~(align - 1);
308 end = (end + (align - 1)) & ~(align - 1);
310 while (start < end) {
312 __asm__ __volatile__ ("iflush %0"::"r"(start));
324 * Flush all register windows to memory. Every register window is saved to
325 * a 16 word area on the stack pointed to by its %sp register.
328 mono_sparc_flushw (void)
330 static guint32 start [64];
331 static int inited = 0;
333 static void (*flushw) (void);
338 sparc_save_imm (code, sparc_sp, -160, sparc_sp);
341 sparc_restore_simple (code);
343 g_assert ((code - start) < 64);
345 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
347 flushw = (gpointer)start;
356 mono_arch_flush_register_windows (void)
358 mono_sparc_flushw ();
362 mono_arch_is_inst_imm (gint64 imm)
364 return sparc_is_imm13 (imm);
368 mono_sparc_is_v9 (void) {
373 mono_sparc_is_sparc64 (void) {
385 ArgInFloatReg, /* V9 only */
386 ArgInDoubleReg /* V9 only */
391 /* This needs to be offset by %i0 or %o0 depending on caller/callee */
394 guint32 vt_offset; /* for valuetypes */
412 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean pair)
414 ainfo->offset = *stack_size;
417 if (*gr >= PARAM_REGS) {
418 ainfo->storage = ArgOnStack;
421 ainfo->storage = ArgInIReg;
426 /* Allways reserve stack space for parameters passed in registers */
427 (*stack_size) += sizeof (gpointer);
430 if (*gr < PARAM_REGS - 1) {
431 /* A pair of registers */
432 ainfo->storage = ArgInIRegPair;
436 else if (*gr >= PARAM_REGS) {
437 /* A pair of stack locations */
438 ainfo->storage = ArgOnStackPair;
441 ainfo->storage = ArgInSplitRegStack;
446 (*stack_size) += 2 * sizeof (gpointer);
452 #define FLOAT_PARAM_REGS 32
455 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean single)
457 ainfo->offset = *stack_size;
460 if (*gr >= FLOAT_PARAM_REGS) {
461 ainfo->storage = ArgOnStack;
464 /* A single is passed in an even numbered fp register */
465 ainfo->storage = ArgInFloatReg;
466 ainfo->reg = *gr + 1;
471 if (*gr < FLOAT_PARAM_REGS) {
472 /* A double register */
473 ainfo->storage = ArgInDoubleReg;
478 ainfo->storage = ArgOnStack;
482 (*stack_size) += sizeof (gpointer);
490 * Obtain information about a call according to the calling convention.
491 * For V8, see the "System V ABI, Sparc Processor Supplement" Sparc V8 version
492 * document for more information.
493 * For V9, see the "Low Level System Information (64-bit psABI)" chapter in
494 * the 'Sparc Compliance Definition 2.4' document.
497 get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
500 int n = sig->hasthis + sig->param_count;
501 guint32 stack_size = 0;
504 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
510 if (MONO_TYPE_ISSTRUCT ((sig->ret))) {
511 /* The address of the return value is passed in %o0 */
512 add_general (&gr, &stack_size, &cinfo->ret, FALSE);
513 cinfo->ret.reg += sparc_i0;
519 add_general (&gr, &stack_size, cinfo->args + 0, FALSE);
521 if ((sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
524 /* Emit the signature cookie just before the implicit arguments */
525 add_general (&gr, &stack_size, &cinfo->sig_cookie, FALSE);
528 for (i = 0; i < sig->param_count; ++i) {
529 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
531 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
534 /* Emit the signature cookie just before the implicit arguments */
535 add_general (&gr, &stack_size, &cinfo->sig_cookie, FALSE);
538 DEBUG(printf("param %d: ", i));
539 if (sig->params [i]->byref) {
540 DEBUG(printf("byref\n"));
542 add_general (&gr, &stack_size, ainfo, FALSE);
545 switch (mono_type_get_underlying_type (sig->params [i])->type) {
546 case MONO_TYPE_BOOLEAN:
549 add_general (&gr, &stack_size, ainfo, FALSE);
550 /* the value is in the ls byte */
551 ainfo->offset += sizeof (gpointer) - 1;
556 add_general (&gr, &stack_size, ainfo, FALSE);
557 /* the value is in the ls word */
558 ainfo->offset += sizeof (gpointer) - 2;
562 add_general (&gr, &stack_size, ainfo, FALSE);
563 /* the value is in the ls dword */
564 ainfo->offset += sizeof (gpointer) - 4;
569 case MONO_TYPE_FNPTR:
570 case MONO_TYPE_CLASS:
571 case MONO_TYPE_OBJECT:
572 case MONO_TYPE_STRING:
573 case MONO_TYPE_SZARRAY:
574 case MONO_TYPE_ARRAY:
575 add_general (&gr, &stack_size, ainfo, FALSE);
577 case MONO_TYPE_GENERICINST:
578 if (!mono_type_generic_inst_is_valuetype (sig->params [i])) {
579 add_general (&gr, &stack_size, ainfo, FALSE);
583 case MONO_TYPE_VALUETYPE:
588 add_general (&gr, &stack_size, ainfo, FALSE);
590 case MONO_TYPE_TYPEDBYREF:
591 add_general (&gr, &stack_size, ainfo, FALSE);
596 add_general (&gr, &stack_size, ainfo, FALSE);
598 add_general (&gr, &stack_size, ainfo, TRUE);
603 add_float (&fr, &stack_size, ainfo, TRUE);
606 /* single precision values are passed in integer registers */
607 add_general (&gr, &stack_size, ainfo, FALSE);
612 add_float (&fr, &stack_size, ainfo, FALSE);
615 /* double precision values are passed in a pair of registers */
616 add_general (&gr, &stack_size, ainfo, TRUE);
620 g_assert_not_reached ();
624 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
627 /* Emit the signature cookie just before the implicit arguments */
628 add_general (&gr, &stack_size, &cinfo->sig_cookie, FALSE);
633 switch (mono_type_get_underlying_type (sig->ret)->type) {
634 case MONO_TYPE_BOOLEAN:
645 case MONO_TYPE_FNPTR:
646 case MONO_TYPE_CLASS:
647 case MONO_TYPE_OBJECT:
648 case MONO_TYPE_SZARRAY:
649 case MONO_TYPE_ARRAY:
650 case MONO_TYPE_STRING:
651 cinfo->ret.storage = ArgInIReg;
652 cinfo->ret.reg = sparc_i0;
659 cinfo->ret.storage = ArgInIReg;
660 cinfo->ret.reg = sparc_i0;
664 cinfo->ret.storage = ArgInIRegPair;
665 cinfo->ret.reg = sparc_i0;
672 cinfo->ret.storage = ArgInFReg;
673 cinfo->ret.reg = sparc_f0;
675 case MONO_TYPE_GENERICINST:
676 if (!mono_type_generic_inst_is_valuetype (sig->ret)) {
677 cinfo->ret.storage = ArgInIReg;
678 cinfo->ret.reg = sparc_i0;
684 case MONO_TYPE_VALUETYPE:
693 cinfo->ret.storage = ArgOnStack;
695 case MONO_TYPE_TYPEDBYREF:
698 /* Same as a valuetype with size 24 */
705 cinfo->ret.storage = ArgOnStack;
710 g_error ("Can't handle as return value 0x%x", sig->ret->type);
714 cinfo->stack_usage = stack_size;
715 cinfo->reg_usage = gr;
720 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
726 * FIXME: If an argument is allocated to a register, then load it from the
727 * stack in the prolog.
730 for (i = 0; i < cfg->num_varinfo; i++) {
731 MonoInst *ins = cfg->varinfo [i];
732 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
735 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
738 /* FIXME: Make arguments on stack allocateable to registers */
739 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode == OP_REGVAR) || (ins->opcode == OP_ARG))
742 if (mono_is_regsize_var (ins->inst_vtype)) {
743 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
744 g_assert (i == vmv->idx);
746 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
754 mono_arch_get_global_int_regs (MonoCompile *cfg)
758 MonoMethodSignature *sig;
761 sig = mono_method_signature (cfg->method);
763 cinfo = get_call_info (sig, FALSE);
765 /* Use unused input registers */
766 for (i = cinfo->reg_usage; i < 6; ++i)
767 regs = g_list_prepend (regs, GUINT_TO_POINTER (sparc_i0 + i));
769 /* Use %l0..%l6 as global registers */
770 for (i = sparc_l0; i < sparc_l7; ++i)
771 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
779 * mono_arch_regalloc_cost:
781 * Return the cost, in number of memory references, of the action of
782 * allocating the variable VMV into a register during global register
786 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
792 * Set var information according to the calling convention. sparc version.
793 * The locals var stuff should most likely be split in another method.
796 mono_arch_allocate_vars (MonoCompile *m)
798 MonoMethodSignature *sig;
799 MonoMethodHeader *header;
801 int i, offset, size, align, curinst;
804 header = mono_method_get_header (m->method);
806 sig = mono_method_signature (m->method);
808 cinfo = get_call_info (sig, FALSE);
810 if (sig->ret->type != MONO_TYPE_VOID) {
811 switch (cinfo->ret.storage) {
815 m->ret->opcode = OP_REGVAR;
816 m->ret->inst_c0 = cinfo->ret.reg;
820 g_assert_not_reached ();
823 m->ret->opcode = OP_REGOFFSET;
824 m->ret->inst_basereg = sparc_fp;
825 m->ret->inst_offset = 64;
831 m->ret->dreg = m->ret->inst_c0;
835 * We use the ABI calling conventions for managed code as well.
836 * Exception: valuetypes are never returned in registers on V9.
837 * FIXME: Use something more optimized.
840 /* Locals are allocated backwards from %fp */
841 m->frame_reg = sparc_fp;
845 * Reserve a stack slot for holding information used during exception
848 if (header->num_clauses)
849 offset += sizeof (gpointer) * 2;
851 if (m->method->save_lmf) {
852 offset += sizeof (MonoLMF);
853 m->arch.lmf_offset = offset;
856 curinst = m->locals_start;
857 for (i = curinst; i < m->num_varinfo; ++i) {
858 inst = m->varinfo [i];
860 if (inst->opcode == OP_REGVAR) {
861 //g_print ("allocating local %d to %s\n", i, mono_arch_regname (inst->dreg));
865 if (inst->flags & MONO_INST_IS_DEAD)
868 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
869 * pinvoke wrappers when they call functions returning structure */
870 if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
871 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
873 size = mini_type_stack_size (m->generic_sharing_context, inst->inst_vtype, &align);
876 * This is needed since structures containing doubles must be doubleword
878 * FIXME: Do this only if needed.
880 if (MONO_TYPE_ISSTRUCT (inst->inst_vtype))
884 * variables are accessed as negative offsets from %fp, so increase
885 * the offset before assigning it to a variable
890 offset &= ~(align - 1);
891 inst->opcode = OP_REGOFFSET;
892 inst->inst_basereg = sparc_fp;
893 inst->inst_offset = STACK_BIAS + -offset;
895 //g_print ("allocating local %d to [%s - %d]\n", i, mono_arch_regname (inst->inst_basereg), - inst->inst_offset);
898 if (sig->call_convention == MONO_CALL_VARARG) {
899 m->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
902 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
904 if (inst->opcode != OP_REGVAR) {
905 ArgInfo *ainfo = &cinfo->args [i];
906 gboolean inreg = TRUE;
910 if (sig->hasthis && (i == 0))
911 arg_type = &mono_defaults.object_class->byval_arg;
913 arg_type = sig->params [i - sig->hasthis];
916 if (!arg_type->byref && ((arg_type->type == MONO_TYPE_R4)
917 || (arg_type->type == MONO_TYPE_R8)))
919 * Since float arguments are passed in integer registers, we need to
920 * save them to the stack in the prolog.
925 /* FIXME: Allocate volatile arguments to registers */
926 if (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
929 if (MONO_TYPE_ISSTRUCT (arg_type))
930 /* FIXME: this isn't needed */
933 inst->opcode = OP_REGOFFSET;
936 storage = ArgOnStack;
938 storage = ainfo->storage;
943 inst->opcode = OP_REGVAR;
944 inst->dreg = sparc_i0 + ainfo->reg;
949 * Since float regs are volatile, we save the arguments to
950 * the stack in the prolog.
951 * FIXME: Avoid this if the method contains no calls.
955 case ArgInSplitRegStack:
956 /* Split arguments are saved to the stack in the prolog */
957 inst->opcode = OP_REGOFFSET;
958 /* in parent frame */
959 inst->inst_basereg = sparc_fp;
960 inst->inst_offset = ainfo->offset + ARGS_OFFSET;
962 if (!arg_type->byref && (arg_type->type == MONO_TYPE_R8)) {
964 * It is very hard to load doubles from non-doubleword aligned
965 * memory locations. So if the offset is misaligned, we copy the
966 * argument to a stack location in the prolog.
968 if ((inst->inst_offset - STACK_BIAS) % 8) {
969 inst->inst_basereg = sparc_fp;
973 offset &= ~(align - 1);
974 inst->inst_offset = STACK_BIAS + -offset;
983 if (MONO_TYPE_ISSTRUCT (arg_type)) {
984 /* Add a level of indirection */
986 * It would be easier to add OP_LDIND_I here, but ldind_i instructions
987 * are destructively modified in a lot of places in inssel.brg.
990 MONO_INST_NEW (m, indir, 0);
992 inst->opcode = OP_SPARC_INARG_VT;
993 inst->inst_left = indir;
999 * spillvars are stored between the normal locals and the storage reserved
1003 m->stack_offset = offset;
1005 /* Add a properly aligned dword for use by int<->float conversion opcodes */
1007 mono_spillvar_offset_float (m, 0);
1013 make_group (MonoCompile *cfg, MonoInst *left, int basereg, int offset)
1017 MONO_INST_NEW (cfg, group, OP_GROUP);
1018 group->inst_left = left;
1019 group->inst_basereg = basereg;
1020 group->inst_imm = offset;
1026 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1029 MonoMethodSignature *tmp_sig;
1033 * mono_ArgIterator_Setup assumes the signature cookie is
1034 * passed first and all the arguments which were before it are
1035 * passed on the stack after the signature. So compensate by
1036 * passing a different signature.
1038 tmp_sig = mono_metadata_signature_dup (call->signature);
1039 tmp_sig->param_count -= call->signature->sentinelpos;
1040 tmp_sig->sentinelpos = 0;
1041 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1043 /* FIXME: Add support for signature tokens to AOT */
1044 cfg->disable_aot = TRUE;
1045 /* We allways pass the signature on the stack for simplicity */
1046 MONO_INST_NEW (cfg, arg, OP_SPARC_OUTARG_MEM);
1047 arg->inst_right = make_group (cfg, (MonoInst*)call, sparc_sp, ARGS_OFFSET + cinfo->sig_cookie.offset);
1048 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
1049 sig_arg->inst_p0 = tmp_sig;
1050 arg->inst_left = sig_arg;
1051 arg->type = STACK_PTR;
1052 /* prepend, so they get reversed */
1053 arg->next = call->out_args;
1054 call->out_args = arg;
1058 * take the arguments and generate the arch-specific
1059 * instructions to properly call the function in call.
1060 * This includes pushing, moving arguments to the right register
1064 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
1066 MonoMethodSignature *sig;
1070 guint32 extra_space = 0;
1072 sig = call->signature;
1073 n = sig->param_count + sig->hasthis;
1075 cinfo = get_call_info (sig, sig->pinvoke);
1077 for (i = 0; i < n; ++i) {
1078 ainfo = cinfo->args + i;
1080 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1081 /* Emit the signature cookie just before the first implicit argument */
1082 emit_sig_cookie (cfg, call, cinfo);
1085 if (is_virtual && i == 0) {
1086 /* the argument will be attached to the call instruction */
1087 in = call->args [i];
1089 MONO_INST_NEW (cfg, arg, OP_OUTARG);
1090 in = call->args [i];
1091 arg->cil_code = in->cil_code;
1092 arg->inst_left = in;
1093 arg->type = in->type;
1094 /* prepend, we'll need to reverse them later */
1095 arg->next = call->out_args;
1096 call->out_args = arg;
1098 if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis]))) {
1101 guint32 offset, pad;
1109 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_TYPEDBYREF) {
1110 size = sizeof (MonoTypedRef);
1111 align = sizeof (gpointer);
1115 size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
1118 * Can't use mini_type_stack_size (), but that
1119 * aligns the size to sizeof (gpointer), which is larger
1120 * than the size of the source, leading to reads of invalid
1121 * memory if the source is at the end of address space or
1124 size = mono_class_value_size (in->klass, &align);
1128 * We use OP_OUTARG_VT to copy the valuetype to a stack location, then
1129 * use the normal OUTARG opcodes to pass the address of the location to
1132 MONO_INST_NEW (cfg, inst, OP_OUTARG_VT);
1133 inst->inst_left = in;
1135 /* The first 6 argument locations are reserved */
1136 if (cinfo->stack_usage < 6 * sizeof (gpointer))
1137 cinfo->stack_usage = 6 * sizeof (gpointer);
1139 offset = ALIGN_TO ((ARGS_OFFSET - STACK_BIAS) + cinfo->stack_usage, align);
1140 pad = offset - ((ARGS_OFFSET - STACK_BIAS) + cinfo->stack_usage);
1142 inst->inst_c1 = STACK_BIAS + offset;
1143 inst->backend.size = size;
1144 arg->inst_left = inst;
1146 cinfo->stack_usage += size;
1147 cinfo->stack_usage += pad;
1150 arg->inst_right = make_group (cfg, (MonoInst*)call, sparc_sp, ARGS_OFFSET + ainfo->offset);
1152 switch (ainfo->storage) {
1156 if (ainfo->storage == ArgInIRegPair)
1157 arg->opcode = OP_SPARC_OUTARG_REGPAIR;
1158 arg->backend.reg3 = sparc_o0 + ainfo->reg;
1159 call->used_iregs |= 1 << ainfo->reg;
1161 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))) {
1162 /* An fp value is passed in an ireg */
1164 if (arg->opcode == OP_SPARC_OUTARG_REGPAIR)
1165 arg->opcode = OP_SPARC_OUTARG_REGPAIR_FLOAT;
1167 arg->opcode = OP_SPARC_OUTARG_FLOAT;
1170 * The OUTARG (freg) implementation needs an extra dword to store
1171 * the temporary value.
1177 arg->opcode = OP_SPARC_OUTARG_MEM;
1179 case ArgOnStackPair:
1180 arg->opcode = OP_SPARC_OUTARG_MEMPAIR;
1182 case ArgInSplitRegStack:
1183 arg->opcode = OP_SPARC_OUTARG_SPLIT_REG_STACK;
1184 arg->backend.reg3 = sparc_o0 + ainfo->reg;
1185 call->used_iregs |= 1 << ainfo->reg;
1188 arg->opcode = OP_SPARC_OUTARG_FLOAT_REG;
1189 arg->backend.reg3 = sparc_f0 + ainfo->reg;
1191 case ArgInDoubleReg:
1192 arg->opcode = OP_SPARC_OUTARG_DOUBLE_REG;
1193 arg->backend.reg3 = sparc_f0 + ainfo->reg;
1201 /* Handle the case where there are no implicit arguments */
1202 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos)) {
1203 emit_sig_cookie (cfg, call, cinfo);
1207 * Reverse the call->out_args list.
1210 MonoInst *prev = NULL, *list = call->out_args, *next;
1217 call->out_args = prev;
1219 call->stack_usage = cinfo->stack_usage + extra_space;
1220 call->out_ireg_args = NULL;
1221 call->out_freg_args = NULL;
1222 cfg->param_area = MAX (cfg->param_area, call->stack_usage);
1223 cfg->flags |= MONO_CFG_HAS_CALLS;
1229 /* Map opcode to the sparc condition codes */
1230 static inline SparcCond
1231 opcode_to_sparc_cond (int opcode)
1253 case OP_COND_EXC_EQ:
1256 case OP_COND_EXC_NE_UN:
1263 case OP_COND_EXC_LT:
1269 case OP_COND_EXC_LT_UN:
1275 case OP_COND_EXC_GT:
1281 case OP_COND_EXC_GT_UN:
1285 case OP_COND_EXC_GE:
1289 case OP_COND_EXC_GE_UN:
1293 case OP_COND_EXC_LE:
1297 case OP_COND_EXC_LE_UN:
1299 case OP_COND_EXC_OV:
1300 case OP_COND_EXC_IOV:
1303 case OP_COND_EXC_IC:
1305 case OP_COND_EXC_NO:
1306 case OP_COND_EXC_NC:
1309 g_assert_not_reached ();
1314 #define COMPUTE_DISP(ins) \
1315 if (ins->flags & MONO_INST_BRLABEL) { \
1316 if (ins->inst_i0->inst_c0) \
1317 disp = (ins->inst_i0->inst_c0 - ((guint8*)code - cfg->native_code)) >> 2; \
1320 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1323 if (ins->inst_true_bb->native_offset) \
1324 disp = (ins->inst_true_bb->native_offset - ((guint8*)code - cfg->native_code)) >> 2; \
1327 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1332 #define DEFAULT_ICC sparc_xcc_short
1334 #define DEFAULT_ICC sparc_icc_short
1338 #define EMIT_COND_BRANCH_ICC(ins,cond,annul,filldelay,icc) \
1342 COMPUTE_DISP(ins); \
1343 predict = (disp != 0) ? 1 : 0; \
1344 g_assert (sparc_is_imm19 (disp)); \
1345 sparc_branchp (code, (annul), cond, icc, (predict), disp); \
1346 if (filldelay) sparc_nop (code); \
1348 #define EMIT_COND_BRANCH(ins,cond,annul,filldelay) EMIT_COND_BRANCH_ICC ((ins), (cond), (annul), (filldelay), (sparc_xcc_short))
1349 #define EMIT_FLOAT_COND_BRANCH(ins,cond,annul,filldelay) \
1353 COMPUTE_DISP(ins); \
1354 predict = (disp != 0) ? 1 : 0; \
1355 g_assert (sparc_is_imm19 (disp)); \
1356 sparc_fbranch (code, (annul), cond, disp); \
1357 if (filldelay) sparc_nop (code); \
1360 #define EMIT_COND_BRANCH_ICC(ins,cond,annul,filldelay,icc) g_assert_not_reached ()
1361 #define EMIT_COND_BRANCH_GENERAL(ins,bop,cond,annul,filldelay) \
1364 COMPUTE_DISP(ins); \
1365 g_assert (sparc_is_imm22 (disp)); \
1366 sparc_ ## bop (code, (annul), cond, disp); \
1367 if (filldelay) sparc_nop (code); \
1369 #define EMIT_COND_BRANCH(ins,cond,annul,filldelay) EMIT_COND_BRANCH_GENERAL((ins),branch,(cond),annul,filldelay)
1370 #define EMIT_FLOAT_COND_BRANCH(ins,cond,annul,filldelay) EMIT_COND_BRANCH_GENERAL((ins),fbranch,(cond),annul,filldelay)
1373 #define EMIT_COND_BRANCH_PREDICTED(ins,cond,annul,filldelay) \
1377 COMPUTE_DISP(ins); \
1378 predict = (disp != 0) ? 1 : 0; \
1379 g_assert (sparc_is_imm19 (disp)); \
1380 sparc_branchp (code, (annul), (cond), DEFAULT_ICC, (predict), disp); \
1381 if (filldelay) sparc_nop (code); \
1384 #define EMIT_COND_BRANCH_BPR(ins,bop,predict,annul,filldelay) \
1387 COMPUTE_DISP(ins); \
1388 g_assert (sparc_is_imm22 (disp)); \
1389 sparc_ ## bop (code, (annul), (predict), ins->sreg1, disp); \
1390 if (filldelay) sparc_nop (code); \
1393 /* emit an exception if condition is fail */
1395 * We put the exception throwing code out-of-line, at the end of the method
1397 #define EMIT_COND_SYSTEM_EXCEPTION_GENERAL(ins,cond,sexc_name,filldelay,icc) do { \
1398 mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code, \
1399 MONO_PATCH_INFO_EXC, sexc_name); \
1400 if (sparcv9 && ((icc) != sparc_icc_short)) { \
1401 sparc_branchp (code, 0, (cond), (icc), 0, 0); \
1404 sparc_branch (code, 0, cond, 0); \
1406 if (filldelay) sparc_nop (code); \
1409 #define EMIT_COND_SYSTEM_EXCEPTION(ins,cond,sexc_name) EMIT_COND_SYSTEM_EXCEPTION_GENERAL(ins,cond,sexc_name,TRUE,DEFAULT_ICC)
1411 #define EMIT_COND_SYSTEM_EXCEPTION_BPR(ins,bop,sexc_name) do { \
1412 mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code, \
1413 MONO_PATCH_INFO_EXC, sexc_name); \
1414 sparc_ ## bop (code, FALSE, FALSE, ins->sreg1, 0); \
1418 #define EMIT_ALU_IMM(ins,op,setcc) do { \
1419 if (sparc_is_imm13 ((ins)->inst_imm)) \
1420 sparc_ ## op ## _imm (code, (setcc), (ins)->sreg1, ins->inst_imm, (ins)->dreg); \
1422 sparc_set (code, ins->inst_imm, sparc_o7); \
1423 sparc_ ## op (code, (setcc), (ins)->sreg1, sparc_o7, (ins)->dreg); \
1427 #define EMIT_LOAD_MEMBASE(ins,op) do { \
1428 if (sparc_is_imm13 (ins->inst_offset)) \
1429 sparc_ ## op ## _imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg); \
1431 sparc_set (code, ins->inst_offset, sparc_o7); \
1432 sparc_ ## op (code, ins->inst_basereg, sparc_o7, ins->dreg); \
1437 #define EMIT_STORE_MEMBASE_IMM(ins,op) do { \
1439 if (ins->inst_imm == 0) \
1442 sparc_set (code, ins->inst_imm, sparc_o7); \
1445 if (!sparc_is_imm13 (ins->inst_offset)) { \
1446 sparc_set (code, ins->inst_offset, GP_SCRATCH_REG); \
1447 sparc_ ## op (code, sreg, ins->inst_destbasereg, GP_SCRATCH_REG); \
1450 sparc_ ## op ## _imm (code, sreg, ins->inst_destbasereg, ins->inst_offset); \
1453 #define EMIT_STORE_MEMBASE_REG(ins,op) do { \
1454 if (!sparc_is_imm13 (ins->inst_offset)) { \
1455 sparc_set (code, ins->inst_offset, sparc_o7); \
1456 sparc_ ## op (code, ins->sreg1, ins->inst_destbasereg, sparc_o7); \
1459 sparc_ ## op ## _imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset); \
1462 #define EMIT_CALL() do { \
1464 sparc_set_template (code, sparc_o7); \
1465 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_o7); \
1468 sparc_call_simple (code, 0); \
1474 * A call template is 7 instructions long, so we want to avoid it if possible.
1477 emit_call (MonoCompile *cfg, guint32 *code, guint32 patch_type, gconstpointer data)
1481 /* FIXME: This only works if the target method is already compiled */
1482 if (0 && v64 && !cfg->compile_aot) {
1483 MonoJumpInfo patch_info;
1485 patch_info.type = patch_type;
1486 patch_info.data.target = data;
1488 target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, &patch_info, FALSE);
1490 /* FIXME: Add optimizations if the target is close enough */
1491 sparc_set (code, target, sparc_o7);
1492 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_o7);
1496 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, patch_type, data);
1504 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1506 MonoInst *ins, *last_ins = NULL;
1511 switch (ins->opcode) {
1513 /* remove unnecessary multiplication with 1 */
1514 if (ins->inst_imm == 1) {
1515 if (ins->dreg != ins->sreg1) {
1516 ins->opcode = OP_MOVE;
1518 last_ins->next = ins->next;
1525 case OP_LOAD_MEMBASE:
1526 case OP_LOADI4_MEMBASE:
1528 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1529 * OP_LOAD_MEMBASE offset(basereg), reg
1531 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1532 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1533 ins->inst_basereg == last_ins->inst_destbasereg &&
1534 ins->inst_offset == last_ins->inst_offset) {
1535 if (ins->dreg == last_ins->sreg1) {
1536 last_ins->next = ins->next;
1540 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1541 ins->opcode = OP_MOVE;
1542 ins->sreg1 = last_ins->sreg1;
1546 * Note: reg1 must be different from the basereg in the second load
1547 * OP_LOAD_MEMBASE offset(basereg), reg1
1548 * OP_LOAD_MEMBASE offset(basereg), reg2
1550 * OP_LOAD_MEMBASE offset(basereg), reg1
1551 * OP_MOVE reg1, reg2
1553 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1554 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1555 ins->inst_basereg != last_ins->dreg &&
1556 ins->inst_basereg == last_ins->inst_basereg &&
1557 ins->inst_offset == last_ins->inst_offset) {
1559 if (ins->dreg == last_ins->dreg) {
1560 last_ins->next = ins->next;
1564 ins->opcode = OP_MOVE;
1565 ins->sreg1 = last_ins->dreg;
1568 //g_assert_not_reached ();
1572 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1573 * OP_LOAD_MEMBASE offset(basereg), reg
1575 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1576 * OP_ICONST reg, imm
1578 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1579 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1580 ins->inst_basereg == last_ins->inst_destbasereg &&
1581 ins->inst_offset == last_ins->inst_offset) {
1582 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1583 ins->opcode = OP_ICONST;
1584 ins->inst_c0 = last_ins->inst_imm;
1585 g_assert_not_reached (); // check this rule
1590 case OP_LOADI1_MEMBASE:
1591 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1592 ins->inst_basereg == last_ins->inst_destbasereg &&
1593 ins->inst_offset == last_ins->inst_offset) {
1594 if (ins->dreg == last_ins->sreg1) {
1595 last_ins->next = ins->next;
1599 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1600 ins->opcode = OP_MOVE;
1601 ins->sreg1 = last_ins->sreg1;
1605 case OP_LOADI2_MEMBASE:
1606 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1607 ins->inst_basereg == last_ins->inst_destbasereg &&
1608 ins->inst_offset == last_ins->inst_offset) {
1609 if (ins->dreg == last_ins->sreg1) {
1610 last_ins->next = ins->next;
1614 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1615 ins->opcode = OP_MOVE;
1616 ins->sreg1 = last_ins->sreg1;
1620 case OP_STOREI4_MEMBASE_IMM:
1621 /* Convert pairs of 0 stores to a dword 0 store */
1622 /* Used when initializing temporaries */
1623 /* We know sparc_fp is dword aligned */
1624 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM) &&
1625 (ins->inst_destbasereg == last_ins->inst_destbasereg) &&
1626 (ins->inst_destbasereg == sparc_fp) &&
1627 (ins->inst_offset < 0) &&
1628 ((ins->inst_offset % 8) == 0) &&
1629 ((ins->inst_offset == last_ins->inst_offset - 4)) &&
1630 (ins->inst_imm == 0) &&
1631 (last_ins->inst_imm == 0)) {
1633 last_ins->opcode = OP_STOREI8_MEMBASE_IMM;
1634 last_ins->inst_offset = ins->inst_offset;
1635 last_ins->next = ins->next;
1647 case OP_COND_EXC_EQ:
1648 case OP_COND_EXC_GE:
1649 case OP_COND_EXC_GT:
1650 case OP_COND_EXC_LE:
1651 case OP_COND_EXC_LT:
1652 case OP_COND_EXC_NE_UN:
1654 * Convert compare with zero+branch to BRcc
1657 * This only works in 64 bit mode, since it examines all 64
1658 * bits of the register.
1659 * Only do this if the method is small since BPr only has a 16bit
1662 if (v64 && (mono_method_get_header (cfg->method)->code_size < 10000) && last_ins &&
1663 (last_ins->opcode == OP_COMPARE_IMM) &&
1664 (last_ins->inst_imm == 0)) {
1665 MonoInst *next = ins->next;
1666 switch (ins->opcode) {
1668 ins->opcode = OP_SPARC_BRZ;
1671 ins->opcode = OP_SPARC_BRNZ;
1674 ins->opcode = OP_SPARC_BRLZ;
1677 ins->opcode = OP_SPARC_BRGZ;
1680 ins->opcode = OP_SPARC_BRGEZ;
1683 ins->opcode = OP_SPARC_BRLEZ;
1685 case OP_COND_EXC_EQ:
1686 ins->opcode = OP_SPARC_COND_EXC_EQZ;
1688 case OP_COND_EXC_GE:
1689 ins->opcode = OP_SPARC_COND_EXC_GEZ;
1691 case OP_COND_EXC_GT:
1692 ins->opcode = OP_SPARC_COND_EXC_GTZ;
1694 case OP_COND_EXC_LE:
1695 ins->opcode = OP_SPARC_COND_EXC_LEZ;
1697 case OP_COND_EXC_LT:
1698 ins->opcode = OP_SPARC_COND_EXC_LTZ;
1700 case OP_COND_EXC_NE_UN:
1701 ins->opcode = OP_SPARC_COND_EXC_NEZ;
1704 g_assert_not_reached ();
1706 ins->sreg1 = last_ins->sreg1;
1708 last_ins->next = next;
1719 if (ins->dreg == ins->sreg1) {
1721 last_ins->next = ins->next;
1726 * OP_MOVE sreg, dreg
1727 * OP_MOVE dreg, sreg
1729 if (last_ins && last_ins->opcode == OP_MOVE &&
1730 ins->sreg1 == last_ins->dreg &&
1731 ins->dreg == last_ins->sreg1) {
1732 last_ins->next = ins->next;
1741 bb->last_ins = last_ins;
1745 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
1747 MonoSpillInfo **si, *info;
1749 g_assert (spillvar == 0);
1751 si = &cfg->spill_info_float;
1754 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1755 cfg->stack_offset += sizeof (double);
1756 cfg->stack_offset = ALIGN_TO (cfg->stack_offset, 8);
1757 info->offset = - cfg->stack_offset;
1760 return MONO_SPARC_STACK_BIAS + (*si)->offset;
1763 /* FIXME: Strange loads from the stack in basic-float.cs:test_2_rem */
1766 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1768 mono_local_regalloc (cfg, bb);
1772 sparc_patch (guint32 *code, const gpointer target)
1775 guint32 ins = *code;
1776 guint32 op = ins >> 30;
1777 guint32 op2 = (ins >> 22) & 0x7;
1778 guint32 rd = (ins >> 25) & 0x1f;
1779 guint8* target8 = (guint8*)target;
1780 gint64 disp = (target8 - (guint8*)code) >> 2;
1783 // g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
1785 if ((op == 0) && (op2 == 2)) {
1786 if (!sparc_is_imm22 (disp))
1789 *code = ((ins >> 22) << 22) | (disp & 0x3fffff);
1791 else if ((op == 0) && (op2 == 1)) {
1792 if (!sparc_is_imm19 (disp))
1795 *code = ((ins >> 19) << 19) | (disp & 0x7ffff);
1797 else if ((op == 0) && (op2 == 3)) {
1798 if (!sparc_is_imm16 (disp))
1801 *code &= ~(0x180000 | 0x3fff);
1802 *code |= ((disp << 21) & (0x180000)) | (disp & 0x3fff);
1804 else if ((op == 0) && (op2 == 6)) {
1805 if (!sparc_is_imm22 (disp))
1808 *code = ((ins >> 22) << 22) | (disp & 0x3fffff);
1810 else if ((op == 0) && (op2 == 4)) {
1811 guint32 ins2 = code [1];
1813 if (((ins2 >> 30) == 2) && (((ins2 >> 19) & 0x3f) == 2)) {
1814 /* sethi followed by or */
1816 sparc_set (p, target8, rd);
1817 while (p <= (code + 1))
1820 else if (ins2 == 0x01000000) {
1821 /* sethi followed by nop */
1823 sparc_set (p, target8, rd);
1824 while (p <= (code + 1))
1827 else if ((sparc_inst_op (ins2) == 3) && (sparc_inst_imm (ins2))) {
1828 /* sethi followed by load/store */
1830 guint32 t = (guint32)target8;
1831 *code &= ~(0x3fffff);
1833 *(code + 1) &= ~(0x3ff);
1834 *(code + 1) |= (t & 0x3ff);
1838 (sparc_inst_rd (ins) == sparc_g1) &&
1839 (sparc_inst_op (c [1]) == 0) && (sparc_inst_op2 (c [1]) == 4) &&
1840 (sparc_inst_op (c [2]) == 2) && (sparc_inst_op3 (c [2]) == 2) &&
1841 (sparc_inst_op (c [3]) == 2) && (sparc_inst_op3 (c [3]) == 2))
1845 reg = sparc_inst_rd (c [1]);
1846 sparc_set (p, target8, reg);
1850 else if ((sparc_inst_op (ins2) == 2) && (sparc_inst_op3 (ins2) == 0x38) &&
1851 (sparc_inst_imm (ins2))) {
1852 /* sethi followed by jmpl */
1854 guint32 t = (guint32)target8;
1855 *code &= ~(0x3fffff);
1857 *(code + 1) &= ~(0x3ff);
1858 *(code + 1) |= (t & 0x3ff);
1864 else if (op == 01) {
1865 gint64 disp = (target8 - (guint8*)code) >> 2;
1867 if (!sparc_is_imm30 (disp))
1869 sparc_call_simple (code, target8 - (guint8*)code);
1871 else if ((op == 2) && (sparc_inst_op3 (ins) == 0x2) && sparc_inst_imm (ins)) {
1873 g_assert (sparc_is_imm13 (target8));
1875 *code |= (guint32)target8;
1877 else if ((sparc_inst_op (ins) == 2) && (sparc_inst_op3 (ins) == 0x7)) {
1878 /* sparc_set case 5. */
1882 reg = sparc_inst_rd (c [3]);
1883 sparc_set (p, target, reg);
1890 // g_print ("patched with 0x%08x\n", ins);
1894 * mono_sparc_emit_save_lmf:
1896 * Emit the code neccesary to push a new entry onto the lmf stack. Used by
1897 * trampolines as well.
1900 mono_sparc_emit_save_lmf (guint32 *code, guint32 lmf_offset)
1903 sparc_sti_imm (code, sparc_o0, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr));
1904 /* Save previous_lmf */
1905 sparc_ldi (code, sparc_o0, sparc_g0, sparc_o7);
1906 sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf));
1908 sparc_add_imm (code, FALSE, sparc_fp, lmf_offset, sparc_o7);
1909 sparc_sti (code, sparc_o7, sparc_o0, sparc_g0);
1915 mono_sparc_emit_restore_lmf (guint32 *code, guint32 lmf_offset)
1917 /* Load previous_lmf */
1918 sparc_ldi_imm (code, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sparc_l0);
1920 sparc_ldi_imm (code, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sparc_l1);
1921 /* *(lmf) = previous_lmf */
1922 sparc_sti (code, sparc_l0, sparc_l1, sparc_g0);
1927 emit_save_sp_to_lmf (MonoCompile *cfg, guint32 *code)
1930 * Since register windows are saved to the current value of %sp, we need to
1931 * set the sp field in the lmf before the call, not in the prolog.
1933 if (cfg->method->save_lmf) {
1934 gint32 lmf_offset = MONO_SPARC_STACK_BIAS - cfg->arch.lmf_offset;
1937 sparc_sti_imm (code, sparc_sp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, sp));
1944 emit_vret_token (MonoGenericSharingContext *gsctx, MonoInst *ins, guint32 *code)
1946 MonoCallInst *call = (MonoCallInst*)ins;
1950 * The sparc ABI requires that calls to functions which return a structure
1951 * contain an additional unimpl instruction which is checked by the callee.
1953 if (call->signature->pinvoke && MONO_TYPE_ISSTRUCT(call->signature->ret)) {
1954 if (call->signature->ret->type == MONO_TYPE_TYPEDBYREF)
1955 size = mini_type_stack_size (gsctx, call->signature->ret, NULL);
1957 size = mono_class_native_size (call->signature->ret->data.klass, NULL);
1958 sparc_unimp (code, size & 0xfff);
1965 emit_move_return_value (MonoInst *ins, guint32 *code)
1967 /* Move return value to the target register */
1968 /* FIXME: do more things in the local reg allocator */
1969 switch (ins->opcode) {
1971 case OP_VOIDCALL_REG:
1972 case OP_VOIDCALL_MEMBASE:
1976 case OP_CALL_MEMBASE:
1977 g_assert (ins->dreg == sparc_o0);
1981 case OP_LCALL_MEMBASE:
1983 * ins->dreg is the least significant reg due to the lreg: LCALL rule
1984 * in inssel-long32.brg.
1987 sparc_mov_reg_reg (code, sparc_o0, ins->dreg);
1989 g_assert (ins->dreg == sparc_o1);
1994 case OP_FCALL_MEMBASE:
1996 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4) {
1997 sparc_fmovs (code, sparc_f0, ins->dreg);
1998 sparc_fstod (code, ins->dreg, ins->dreg);
2001 sparc_fmovd (code, sparc_f0, ins->dreg);
2003 sparc_fmovs (code, sparc_f0, ins->dreg);
2004 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4)
2005 sparc_fstod (code, ins->dreg, ins->dreg);
2007 sparc_fmovs (code, sparc_f1, ins->dreg + 1);
2012 case OP_VCALL_MEMBASE:
2022 * emit_load_volatile_arguments:
2024 * Load volatile arguments from the stack to the original input registers.
2025 * Required before a tail call.
2028 emit_load_volatile_arguments (MonoCompile *cfg, guint32 *code)
2030 MonoMethod *method = cfg->method;
2031 MonoMethodSignature *sig;
2036 /* FIXME: Generate intermediate code instead */
2038 sig = mono_method_signature (method);
2040 cinfo = get_call_info (sig, FALSE);
2042 /* This is the opposite of the code in emit_prolog */
2044 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2045 ArgInfo *ainfo = cinfo->args + i;
2046 gint32 stack_offset;
2048 inst = cfg->args [i];
2050 if (sig->hasthis && (i == 0))
2051 arg_type = &mono_defaults.object_class->byval_arg;
2053 arg_type = sig->params [i - sig->hasthis];
2055 stack_offset = ainfo->offset + ARGS_OFFSET;
2056 ireg = sparc_i0 + ainfo->reg;
2058 if (ainfo->storage == ArgInSplitRegStack) {
2059 g_assert (inst->opcode == OP_REGOFFSET);
2061 if (!sparc_is_imm13 (stack_offset))
2063 sparc_st_imm (code, inst->inst_basereg, stack_offset, sparc_i5);
2066 if (!v64 && !arg_type->byref && (arg_type->type == MONO_TYPE_R8)) {
2067 if (ainfo->storage == ArgInIRegPair) {
2068 if (!sparc_is_imm13 (inst->inst_offset + 4))
2070 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, ireg);
2071 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset + 4, ireg + 1);
2074 if (ainfo->storage == ArgInSplitRegStack) {
2075 if (stack_offset != inst->inst_offset) {
2076 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, sparc_i5);
2077 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset + 4, sparc_o7);
2078 sparc_st_imm (code, sparc_o7, sparc_fp, stack_offset + 4);
2083 if (ainfo->storage == ArgOnStackPair) {
2084 if (stack_offset != inst->inst_offset) {
2085 /* stack_offset is not dword aligned, so we need to make a copy */
2086 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, sparc_o7);
2087 sparc_st_imm (code, sparc_o7, sparc_fp, stack_offset);
2089 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset + 4, sparc_o7);
2090 sparc_st_imm (code, sparc_o7, sparc_fp, stack_offset + 4);
2095 g_assert_not_reached ();
2098 if ((ainfo->storage == ArgInIReg) && (inst->opcode != OP_REGVAR)) {
2099 /* Argument in register, but need to be saved to stack */
2100 if (!sparc_is_imm13 (stack_offset))
2102 if ((stack_offset - ARGS_OFFSET) & 0x1)
2103 /* FIXME: Is this ldsb or ldub ? */
2104 sparc_ldsb_imm (code, inst->inst_basereg, stack_offset, ireg);
2106 if ((stack_offset - ARGS_OFFSET) & 0x2)
2107 sparc_ldsh_imm (code, inst->inst_basereg, stack_offset, ireg);
2109 if ((stack_offset - ARGS_OFFSET) & 0x4)
2110 sparc_ld_imm (code, inst->inst_basereg, stack_offset, ireg);
2113 sparc_ldx_imm (code, inst->inst_basereg, stack_offset, ireg);
2115 sparc_ld_imm (code, inst->inst_basereg, stack_offset, ireg);
2118 else if ((ainfo->storage == ArgInIRegPair) && (inst->opcode != OP_REGVAR)) {
2119 /* Argument in regpair, but need to be saved to stack */
2120 if (!sparc_is_imm13 (inst->inst_offset + 4))
2122 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, ireg);
2123 sparc_st_imm (code, inst->inst_basereg, inst->inst_offset + 4, ireg + 1);
2125 else if ((ainfo->storage == ArgInFloatReg) && (inst->opcode != OP_REGVAR)) {
2128 else if ((ainfo->storage == ArgInDoubleReg) && (inst->opcode != OP_REGVAR)) {
2132 if ((ainfo->storage == ArgInSplitRegStack) || (ainfo->storage == ArgOnStack))
2133 if (inst->opcode == OP_REGVAR)
2134 /* FIXME: Load the argument into memory */
2144 * mono_sparc_is_virtual_call:
2146 * Determine whenever the instruction at CODE is a virtual call.
2149 mono_sparc_is_virtual_call (guint32 *code)
2156 if ((sparc_inst_op (*code) == 0x2) && (sparc_inst_op3 (*code) == 0x38)) {
2158 * Register indirect call. If it is a virtual call, then the
2159 * instruction in the delay slot is a special kind of nop.
2162 /* Construct special nop */
2163 sparc_or_imm (p, FALSE, sparc_g0, 0xca, sparc_g0);
2166 if (code [1] == p [0])
2174 * mono_arch_get_vcall_slot:
2176 * Determine the vtable slot used by a virtual call.
2179 mono_arch_get_vcall_slot (guint8 *code8, gpointer *regs, int *displacement)
2181 guint32 *code = (guint32*)(gpointer)code8;
2182 guint32 ins = code [0];
2183 guint32 prev_ins = code [-1];
2185 mono_sparc_flushw ();
2189 if (!mono_sparc_is_virtual_call (code))
2192 if ((sparc_inst_op (ins) == 0x2) && (sparc_inst_op3 (ins) == 0x38)) {
2193 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)) {
2194 /* ld [r1 + CONST ], r2; call r2 */
2195 guint32 base = sparc_inst_rs1 (prev_ins);
2196 gint32 disp = (((gint32)(sparc_inst_imm13 (prev_ins))) << 19) >> 19;
2199 g_assert (sparc_inst_rd (prev_ins) == sparc_inst_rs1 (ins));
2201 g_assert ((base >= sparc_o0) && (base <= sparc_i7));
2203 base_val = regs [base];
2205 *displacement = disp;
2207 return (gpointer)base_val;
2209 else if ((sparc_inst_op (prev_ins) == 0x3) && (sparc_inst_i (prev_ins) == 0) && (sparc_inst_op3 (prev_ins) == 0)) {
2210 /* set r1, ICONST; ld [r1 + r2], r2; call r2 */
2211 /* Decode a sparc_set32 */
2212 guint32 base = sparc_inst_rs1 (prev_ins);
2215 guint32 s1 = code [-3];
2216 guint32 s2 = code [-2];
2223 g_assert (sparc_inst_op (s1) == 0);
2224 g_assert (sparc_inst_op2 (s1) == 4);
2227 g_assert (sparc_inst_op (s2) == 2);
2228 g_assert (sparc_inst_op3 (s2) == 2);
2229 g_assert (sparc_inst_i (s2) == 1);
2230 g_assert (sparc_inst_rs1 (s2) == sparc_inst_rd (s2));
2231 g_assert (sparc_inst_rd (s1) == sparc_inst_rs1 (s2));
2233 disp = ((s1 & 0x3fffff) << 10) | sparc_inst_imm13 (s2);
2235 g_assert ((base >= sparc_o0) && (base <= sparc_i7));
2237 base_val = regs [base];
2239 *displacement = disp;
2241 return (gpointer)base_val;
2243 g_assert_not_reached ();
2246 g_assert_not_reached ();
2252 mono_arch_get_vcall_slot_addr (guint8 *code, gpointer *regs)
2256 vt = mono_arch_get_vcall_slot (code, regs, &displacement);
2259 return (gpointer*)((char*)vt + displacement);
2263 #define BR_SMALL_SIZE 2
2264 #define BR_LARGE_SIZE 2
2265 #define JUMP_IMM_SIZE 5
2266 #define ENABLE_WRONG_METHOD_CHECK 0
2269 * LOCKING: called with the domain lock held
2272 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count)
2276 guint32 *code, *start;
2278 for (i = 0; i < count; ++i) {
2279 MonoIMTCheckItem *item = imt_entries [i];
2280 if (item->is_equals) {
2281 if (item->check_target_idx) {
2282 if (!item->compare_done)
2283 item->chunk_size += CMP_SIZE;
2284 item->chunk_size += BR_SMALL_SIZE + JUMP_IMM_SIZE;
2286 item->chunk_size += JUMP_IMM_SIZE;
2287 #if ENABLE_WRONG_METHOD_CHECK
2288 item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
2292 item->chunk_size += CMP_SIZE + BR_LARGE_SIZE;
2293 imt_entries [item->check_target_idx]->compare_done = TRUE;
2295 size += item->chunk_size;
2297 code = mono_code_manager_reserve (domain->code_mp, size * 4);
2300 for (i = 0; i < count; ++i) {
2301 MonoIMTCheckItem *item = imt_entries [i];
2302 item->code_target = (guint8*)code;
2303 if (item->is_equals) {
2304 if (item->check_target_idx) {
2305 if (!item->compare_done) {
2306 sparc_set (code, (guint32)item->method, sparc_g5);
2307 sparc_cmp (code, MONO_ARCH_IMT_REG, sparc_g5);
2309 item->jmp_code = (guint8*)code;
2310 sparc_branch (code, 0, sparc_bne, 0);
2312 sparc_set (code, ((guint32)(&(vtable->vtable [item->vtable_slot]))), sparc_g5);
2313 sparc_ld (code, sparc_g5, 0, sparc_g5);
2314 sparc_jmpl (code, sparc_g5, sparc_g0, sparc_g0);
2317 /* enable the commented code to assert on wrong method */
2318 #if ENABLE_WRONG_METHOD_CHECK
2319 g_assert_not_reached ();
2321 sparc_set (code, ((guint32)(&(vtable->vtable [item->vtable_slot]))), sparc_g5);
2322 sparc_ld (code, sparc_g5, 0, sparc_g5);
2323 sparc_jmpl (code, sparc_g5, sparc_g0, sparc_g0);
2325 #if ENABLE_WRONG_METHOD_CHECK
2326 g_assert_not_reached ();
2330 sparc_set (code, (guint32)item->method, sparc_g5);
2331 sparc_cmp (code, MONO_ARCH_IMT_REG, sparc_g5);
2332 item->jmp_code = (guint8*)code;
2333 sparc_branch (code, 0, sparc_beu, 0);
2337 /* patch the branches to get to the target items */
2338 for (i = 0; i < count; ++i) {
2339 MonoIMTCheckItem *item = imt_entries [i];
2340 if (item->jmp_code) {
2341 if (item->check_target_idx) {
2342 sparc_patch ((guint32*)item->jmp_code, imt_entries [item->check_target_idx]->code_target);
2347 mono_arch_flush_icache ((guint8*)start, (code - start) * 4);
2349 mono_stats.imt_thunks_size += (code - start) * 4;
2350 g_assert (code - start <= size);
2355 mono_arch_find_imt_method (gpointer *regs, guint8 *code)
2358 g_assert_not_reached ();
2361 return (MonoMethod*)regs [sparc_g1];
2365 mono_arch_find_this_argument (gpointer *regs, MonoMethod *method)
2367 mono_sparc_flushw ();
2369 return (gpointer)regs [sparc_o0];
2373 * Some conventions used in the following code.
2374 * 2) The only scratch registers we have are o7 and g1. We try to
2375 * stick to o7 when we can, and use g1 when necessary.
2379 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2384 guint32 *code = (guint32*)(cfg->native_code + cfg->code_len);
2385 MonoInst *last_ins = NULL;
2389 if (cfg->opt & MONO_OPT_PEEPHOLE)
2390 peephole_pass (cfg, bb);
2392 if (cfg->verbose_level > 2)
2393 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2395 cpos = bb->max_offset;
2397 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2405 offset = (guint8*)code - cfg->native_code;
2407 spec = ins_get_spec (ins->opcode);
2408 /* I kept this, but this looks a workaround for a bug */
2409 if (spec == MONO_ARCH_CPU_SPEC)
2410 spec = ins_get_spec (CEE_ADD);
2412 max_len = ((guint8 *)spec)[MONO_INST_LEN];
2414 if (offset > (cfg->code_size - max_len - 16)) {
2415 cfg->code_size *= 2;
2416 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2417 code = (guint32*)(cfg->native_code + offset);
2419 code_start = (guint8*)code;
2420 // if (ins->cil_code)
2421 // g_print ("cil code\n");
2422 mono_debug_record_line_number (cfg, ins, offset);
2424 switch (ins->opcode) {
2425 case OP_STOREI1_MEMBASE_IMM:
2426 EMIT_STORE_MEMBASE_IMM (ins, stb);
2428 case OP_STOREI2_MEMBASE_IMM:
2429 EMIT_STORE_MEMBASE_IMM (ins, sth);
2431 case OP_STORE_MEMBASE_IMM:
2432 EMIT_STORE_MEMBASE_IMM (ins, sti);
2434 case OP_STOREI4_MEMBASE_IMM:
2435 EMIT_STORE_MEMBASE_IMM (ins, st);
2437 case OP_STOREI8_MEMBASE_IMM:
2439 EMIT_STORE_MEMBASE_IMM (ins, stx);
2441 /* Only generated by peephole opts */
2442 g_assert ((ins->inst_offset % 8) == 0);
2443 g_assert (ins->inst_imm == 0);
2444 EMIT_STORE_MEMBASE_IMM (ins, stx);
2447 case OP_STOREI1_MEMBASE_REG:
2448 EMIT_STORE_MEMBASE_REG (ins, stb);
2450 case OP_STOREI2_MEMBASE_REG:
2451 EMIT_STORE_MEMBASE_REG (ins, sth);
2453 case OP_STOREI4_MEMBASE_REG:
2454 EMIT_STORE_MEMBASE_REG (ins, st);
2456 case OP_STOREI8_MEMBASE_REG:
2458 EMIT_STORE_MEMBASE_REG (ins, stx);
2460 /* Only used by OP_MEMSET */
2461 EMIT_STORE_MEMBASE_REG (ins, std);
2464 case OP_STORE_MEMBASE_REG:
2465 EMIT_STORE_MEMBASE_REG (ins, sti);
2469 sparc_ldx (code, ins->inst_c0, sparc_g0, ins->dreg);
2471 sparc_ld (code, ins->inst_c0, sparc_g0, ins->dreg);
2476 sparc_ldsw (code, ins->inst_c0, sparc_g0, ins->dreg);
2478 sparc_ld (code, ins->inst_c0, sparc_g0, ins->dreg);
2482 sparc_ld (code, ins->inst_c0, sparc_g0, ins->dreg);
2485 sparc_set (code, ins->inst_c0, ins->dreg);
2486 sparc_ld (code, ins->dreg, sparc_g0, ins->dreg);
2488 case OP_LOADI4_MEMBASE:
2490 EMIT_LOAD_MEMBASE (ins, ldsw);
2492 EMIT_LOAD_MEMBASE (ins, ld);
2495 case OP_LOADU4_MEMBASE:
2496 EMIT_LOAD_MEMBASE (ins, ld);
2498 case OP_LOADU1_MEMBASE:
2499 EMIT_LOAD_MEMBASE (ins, ldub);
2501 case OP_LOADI1_MEMBASE:
2502 EMIT_LOAD_MEMBASE (ins, ldsb);
2504 case OP_LOADU2_MEMBASE:
2505 EMIT_LOAD_MEMBASE (ins, lduh);
2507 case OP_LOADI2_MEMBASE:
2508 EMIT_LOAD_MEMBASE (ins, ldsh);
2510 case OP_LOAD_MEMBASE:
2512 EMIT_LOAD_MEMBASE (ins, ldx);
2514 EMIT_LOAD_MEMBASE (ins, ld);
2518 case OP_LOADI8_MEMBASE:
2519 EMIT_LOAD_MEMBASE (ins, ldx);
2523 sparc_sll_imm (code, ins->sreg1, 24, sparc_o7);
2524 sparc_sra_imm (code, sparc_o7, 24, ins->dreg);
2527 sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
2528 sparc_sra_imm (code, sparc_o7, 16, ins->dreg);
2531 sparc_and_imm (code, FALSE, ins->sreg1, 0xff, ins->dreg);
2534 sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
2535 sparc_srl_imm (code, sparc_o7, 16, ins->dreg);
2537 case CEE_CONV_OVF_U4:
2538 /* Only used on V9 */
2539 sparc_cmp_imm (code, ins->sreg1, 0);
2540 mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code,
2541 MONO_PATCH_INFO_EXC, "OverflowException");
2542 sparc_branchp (code, 0, sparc_bl, sparc_xcc_short, 0, 0);
2544 sparc_set (code, 1, sparc_o7);
2545 sparc_sllx_imm (code, sparc_o7, 32, sparc_o7);
2546 sparc_cmp (code, ins->sreg1, sparc_o7);
2547 mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code,
2548 MONO_PATCH_INFO_EXC, "OverflowException");
2549 sparc_branchp (code, 0, sparc_bge, sparc_xcc_short, 0, 0);
2551 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
2553 case CEE_CONV_OVF_I4_UN:
2554 /* Only used on V9 */
2559 /* Only used on V9 */
2560 sparc_srl_imm (code, ins->sreg1, 0, ins->dreg);
2564 /* Only used on V9 */
2565 sparc_sra_imm (code, ins->sreg1, 0, ins->dreg);
2570 sparc_cmp (code, ins->sreg1, ins->sreg2);
2572 case OP_COMPARE_IMM:
2573 case OP_ICOMPARE_IMM:
2574 if (sparc_is_imm13 (ins->inst_imm))
2575 sparc_cmp_imm (code, ins->sreg1, ins->inst_imm);
2577 sparc_set (code, ins->inst_imm, sparc_o7);
2578 sparc_cmp (code, ins->sreg1, sparc_o7);
2583 * gdb does not like encountering 'ta 1' in the debugged code. So
2584 * instead of emitting a trap, we emit a call a C function and place a
2587 //sparc_ta (code, 1);
2588 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, mono_arch_break);
2593 sparc_add (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2597 sparc_add (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2602 /* according to inssel-long32.brg, this should set cc */
2603 EMIT_ALU_IMM (ins, add, TRUE);
2607 /* according to inssel-long32.brg, this should set cc */
2608 sparc_addx (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2612 EMIT_ALU_IMM (ins, addx, TRUE);
2616 sparc_sub (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2620 sparc_sub (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2625 /* according to inssel-long32.brg, this should set cc */
2626 EMIT_ALU_IMM (ins, sub, TRUE);
2630 /* according to inssel-long32.brg, this should set cc */
2631 sparc_subx (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2635 EMIT_ALU_IMM (ins, subx, TRUE);
2639 sparc_and (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2643 EMIT_ALU_IMM (ins, and, FALSE);
2647 /* Sign extend sreg1 into %y */
2648 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2649 sparc_wry (code, sparc_o7, sparc_g0);
2650 sparc_sdiv (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2651 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2655 sparc_wry (code, sparc_g0, sparc_g0);
2656 sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2661 /* Transform division into a shift */
2662 for (i = 1; i < 30; ++i) {
2664 if (ins->inst_imm == imm)
2670 sparc_srl_imm (code, ins->sreg1, 31, sparc_o7);
2671 sparc_add (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2672 sparc_sra_imm (code, ins->dreg, 1, ins->dreg);
2675 /* http://compilers.iecc.com/comparch/article/93-04-079 */
2676 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2677 sparc_srl_imm (code, sparc_o7, 32 - i, sparc_o7);
2678 sparc_add (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2679 sparc_sra_imm (code, ins->dreg, i, ins->dreg);
2683 /* Sign extend sreg1 into %y */
2684 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2685 sparc_wry (code, sparc_o7, sparc_g0);
2686 EMIT_ALU_IMM (ins, sdiv, TRUE);
2687 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2693 /* Sign extend sreg1 into %y */
2694 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2695 sparc_wry (code, sparc_o7, sparc_g0);
2696 sparc_sdiv (code, TRUE, ins->sreg1, ins->sreg2, sparc_o7);
2697 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2698 sparc_smul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
2699 sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2703 sparc_wry (code, sparc_g0, sparc_g0);
2704 sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, sparc_o7);
2705 sparc_umul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
2706 sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2710 /* Sign extend sreg1 into %y */
2711 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2712 sparc_wry (code, sparc_o7, sparc_g0);
2713 if (!sparc_is_imm13 (ins->inst_imm)) {
2714 sparc_set (code, ins->inst_imm, GP_SCRATCH_REG);
2715 sparc_sdiv (code, TRUE, ins->sreg1, GP_SCRATCH_REG, sparc_o7);
2716 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2717 sparc_smul (code, FALSE, sparc_o7, GP_SCRATCH_REG, sparc_o7);
2720 sparc_sdiv_imm (code, TRUE, ins->sreg1, ins->inst_imm, sparc_o7);
2721 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2722 sparc_smul_imm (code, FALSE, sparc_o7, ins->inst_imm, sparc_o7);
2724 sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2728 sparc_or (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2732 EMIT_ALU_IMM (ins, or, FALSE);
2736 sparc_xor (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2740 EMIT_ALU_IMM (ins, xor, FALSE);
2744 sparc_sll (code, ins->sreg1, ins->sreg2, ins->dreg);
2748 if (ins->inst_imm < (1 << 5))
2749 sparc_sll_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2751 sparc_set (code, ins->inst_imm, sparc_o7);
2752 sparc_sll (code, ins->sreg1, sparc_o7, ins->dreg);
2757 sparc_sra (code, ins->sreg1, ins->sreg2, ins->dreg);
2761 if (ins->inst_imm < (1 << 5))
2762 sparc_sra_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2764 sparc_set (code, ins->inst_imm, sparc_o7);
2765 sparc_sra (code, ins->sreg1, sparc_o7, ins->dreg);
2769 case OP_ISHR_UN_IMM:
2770 if (ins->inst_imm < (1 << 5))
2771 sparc_srl_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2773 sparc_set (code, ins->inst_imm, sparc_o7);
2774 sparc_srl (code, ins->sreg1, sparc_o7, ins->dreg);
2779 sparc_srl (code, ins->sreg1, ins->sreg2, ins->dreg);
2782 sparc_sllx (code, ins->sreg1, ins->sreg2, ins->dreg);
2785 if (ins->inst_imm < (1 << 6))
2786 sparc_sllx_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2788 sparc_set (code, ins->inst_imm, sparc_o7);
2789 sparc_sllx (code, ins->sreg1, sparc_o7, ins->dreg);
2793 sparc_srax (code, ins->sreg1, ins->sreg2, ins->dreg);
2796 if (ins->inst_imm < (1 << 6))
2797 sparc_srax_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2799 sparc_set (code, ins->inst_imm, sparc_o7);
2800 sparc_srax (code, ins->sreg1, sparc_o7, ins->dreg);
2804 sparc_srlx (code, ins->sreg1, ins->sreg2, ins->dreg);
2806 case OP_LSHR_UN_IMM:
2807 if (ins->inst_imm < (1 << 6))
2808 sparc_srlx_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2810 sparc_set (code, ins->inst_imm, sparc_o7);
2811 sparc_srlx (code, ins->sreg1, sparc_o7, ins->dreg);
2816 /* can't use sparc_not */
2817 sparc_xnor (code, FALSE, ins->sreg1, sparc_g0, ins->dreg);
2821 /* can't use sparc_neg */
2822 sparc_sub (code, FALSE, sparc_g0, ins->sreg1, ins->dreg);
2826 sparc_smul (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2832 if ((ins->inst_imm == 1) && (ins->sreg1 == ins->dreg))
2835 /* Transform multiplication into a shift */
2836 for (i = 0; i < 30; ++i) {
2838 if (ins->inst_imm == imm)
2842 sparc_sll_imm (code, ins->sreg1, i, ins->dreg);
2844 EMIT_ALU_IMM (ins, smul, FALSE);
2849 sparc_smul (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2850 sparc_rdy (code, sparc_g1);
2851 sparc_sra_imm (code, ins->dreg, 31, sparc_o7);
2852 sparc_cmp (code, sparc_g1, sparc_o7);
2853 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (ins, sparc_bne, "OverflowException", TRUE, sparc_icc_short);
2855 case CEE_MUL_OVF_UN:
2856 case OP_IMUL_OVF_UN:
2857 sparc_umul (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2858 sparc_rdy (code, sparc_o7);
2859 sparc_cmp (code, sparc_o7, sparc_g0);
2860 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (ins, sparc_bne, "OverflowException", TRUE, sparc_icc_short);
2863 sparc_set (code, ins->inst_c0, ins->dreg);
2866 sparc_set (code, ins->inst_l, ins->dreg);
2869 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2870 sparc_set_template (code, ins->dreg);
2875 if (ins->sreg1 != ins->dreg)
2876 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
2879 /* Only used on V9 */
2880 if (ins->sreg1 != ins->dreg)
2881 sparc_fmovd (code, ins->sreg1, ins->dreg);
2883 case OP_SPARC_SETFREG_FLOAT:
2884 /* Only used on V9 */
2885 sparc_fdtos (code, ins->sreg1, ins->dreg);
2888 if (cfg->method->save_lmf)
2891 code = emit_load_volatile_arguments (cfg, code);
2892 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2893 sparc_set_template (code, sparc_o7);
2894 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_g0);
2895 /* Restore parent frame in delay slot */
2896 sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
2899 /* ensure ins->sreg1 is not NULL */
2900 sparc_ld_imm (code, ins->sreg1, 0, sparc_g0);
2903 sparc_add_imm (code, FALSE, sparc_fp, cfg->sig_cookie, sparc_o7);
2904 sparc_sti_imm (code, sparc_o7, ins->sreg1, 0);
2911 call = (MonoCallInst*)ins;
2912 g_assert (!call->virtual);
2913 code = emit_save_sp_to_lmf (cfg, code);
2914 if (ins->flags & MONO_INST_HAS_METHOD)
2915 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method);
2917 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
2919 code = emit_vret_token (cfg->generic_sharing_context, ins, code);
2920 code = emit_move_return_value (ins, code);
2925 case OP_VOIDCALL_REG:
2927 call = (MonoCallInst*)ins;
2928 code = emit_save_sp_to_lmf (cfg, code);
2929 sparc_jmpl (code, ins->sreg1, sparc_g0, sparc_callsite);
2931 * We emit a special kind of nop in the delay slot to tell the
2932 * trampoline code that this is a virtual call, thus an unbox
2933 * trampoline might need to be called.
2936 sparc_or_imm (code, FALSE, sparc_g0, 0xca, sparc_g0);
2940 code = emit_vret_token (cfg->generic_sharing_context, ins, code);
2941 code = emit_move_return_value (ins, code);
2943 case OP_FCALL_MEMBASE:
2944 case OP_LCALL_MEMBASE:
2945 case OP_VCALL_MEMBASE:
2946 case OP_VOIDCALL_MEMBASE:
2947 case OP_CALL_MEMBASE:
2948 call = (MonoCallInst*)ins;
2949 code = emit_save_sp_to_lmf (cfg, code);
2950 if (sparc_is_imm13 (ins->inst_offset)) {
2951 sparc_ldi_imm (code, ins->inst_basereg, ins->inst_offset, sparc_o7);
2953 sparc_set (code, ins->inst_offset, sparc_o7);
2954 sparc_ldi (code, ins->inst_basereg, sparc_o7, sparc_o7);
2956 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
2958 sparc_or_imm (code, FALSE, sparc_g0, 0xca, sparc_g0);
2962 code = emit_vret_token (cfg->generic_sharing_context, ins, code);
2963 code = emit_move_return_value (ins, code);
2966 if (mono_method_signature (cfg->method)->ret->type == MONO_TYPE_R4)
2967 sparc_fdtos (code, ins->sreg1, sparc_f0);
2970 sparc_fmovd (code, ins->sreg1, ins->dreg);
2972 /* FIXME: Why not use fmovd ? */
2973 sparc_fmovs (code, ins->sreg1, ins->dreg);
2974 sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
2979 g_assert_not_reached ();
2984 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2985 /* Perform stack touching */
2989 /* Keep alignment */
2990 sparc_add_imm (code, FALSE, ins->sreg1, MONO_ARCH_LOCALLOC_ALIGNMENT - 1, ins->dreg);
2991 sparc_set (code, ~(MONO_ARCH_LOCALLOC_ALIGNMENT - 1), sparc_o7);
2992 sparc_and (code, FALSE, ins->dreg, sparc_o7, ins->dreg);
2994 if ((ins->flags & MONO_INST_INIT) && (ins->sreg1 == ins->dreg)) {
2996 size_reg = sparc_g4;
2998 size_reg = sparc_g1;
3000 sparc_mov_reg_reg (code, ins->dreg, size_reg);
3003 size_reg = ins->sreg1;
3005 sparc_sub (code, FALSE, sparc_sp, ins->dreg, ins->dreg);
3006 /* Keep %sp valid at all times */
3007 sparc_mov_reg_reg (code, ins->dreg, sparc_sp);
3008 g_assert (sparc_is_imm13 (MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset));
3009 sparc_add_imm (code, FALSE, ins->dreg, MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset, ins->dreg);
3011 if (ins->flags & MONO_INST_INIT) {
3013 /* Initialize memory region */
3014 sparc_cmp_imm (code, size_reg, 0);
3016 sparc_branch (code, 0, sparc_be, 0);
3018 sparc_set (code, 0, sparc_o7);
3019 sparc_sub_imm (code, 0, size_reg, sparcv9 ? 8 : 4, size_reg);
3023 sparc_stx (code, sparc_g0, ins->dreg, sparc_o7);
3025 sparc_st (code, sparc_g0, ins->dreg, sparc_o7);
3026 sparc_cmp (code, sparc_o7, size_reg);
3028 sparc_branch (code, 0, sparc_bl, 0);
3029 sparc_patch (br [2], br [1]);
3031 sparc_add_imm (code, 0, sparc_o7, sparcv9 ? 8 : 4, sparc_o7);
3032 sparc_patch (br [0], code);
3036 case OP_SPARC_LOCALLOC_IMM: {
3037 gint32 offset = ins->inst_c0;
3039 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
3040 /* Perform stack touching */
3044 offset = ALIGN_TO (offset, MONO_ARCH_LOCALLOC_ALIGNMENT);
3045 if (sparc_is_imm13 (offset))
3046 sparc_sub_imm (code, FALSE, sparc_sp, offset, sparc_sp);
3048 sparc_set (code, offset, sparc_o7);
3049 sparc_sub (code, FALSE, sparc_sp, sparc_o7, sparc_sp);
3051 g_assert (sparc_is_imm13 (MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset));
3052 sparc_add_imm (code, FALSE, sparc_sp, MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset, ins->dreg);
3053 if ((ins->flags & MONO_INST_INIT) && (offset > 0)) {
3059 while (i < offset) {
3061 sparc_stx_imm (code, sparc_g0, ins->dreg, i);
3065 sparc_st_imm (code, sparc_g0, ins->dreg, i);
3071 sparc_set (code, offset, sparc_o7);
3072 sparc_sub_imm (code, 0, sparc_o7, sparcv9 ? 8 : 4, sparc_o7);
3073 /* beginning of loop */
3076 sparc_stx (code, sparc_g0, ins->dreg, sparc_o7);
3078 sparc_st (code, sparc_g0, ins->dreg, sparc_o7);
3079 sparc_cmp_imm (code, sparc_o7, 0);
3081 sparc_branch (code, 0, sparc_bne, 0);
3083 sparc_sub_imm (code, 0, sparc_o7, sparcv9 ? 8 : 4, sparc_o7);
3084 sparc_patch (br [1], br [0]);
3090 /* The return is done in the epilog */
3091 g_assert_not_reached ();
3094 sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
3095 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3096 (gpointer)"mono_arch_throw_exception");
3100 sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
3101 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3102 (gpointer)"mono_arch_rethrow_exception");
3105 case OP_START_HANDLER: {
3107 * The START_HANDLER instruction marks the beginning of a handler
3108 * block. It is called using a call instruction, so %o7 contains
3109 * the return address. Since the handler executes in the same stack
3110 * frame as the method itself, we can't use save/restore to save
3111 * the return address. Instead, we save it into a dedicated
3114 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3115 if (!sparc_is_imm13 (spvar->inst_offset)) {
3116 sparc_set (code, spvar->inst_offset, GP_SCRATCH_REG);
3117 sparc_sti (code, sparc_o7, spvar->inst_basereg, GP_SCRATCH_REG);
3120 sparc_sti_imm (code, sparc_o7, spvar->inst_basereg, spvar->inst_offset);
3123 case OP_ENDFILTER: {
3124 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3125 if (!sparc_is_imm13 (spvar->inst_offset)) {
3126 sparc_set (code, spvar->inst_offset, GP_SCRATCH_REG);
3127 sparc_ldi (code, spvar->inst_basereg, GP_SCRATCH_REG, sparc_o7);
3130 sparc_ldi_imm (code, spvar->inst_basereg, spvar->inst_offset, sparc_o7);
3131 sparc_jmpl_imm (code, sparc_o7, 8, sparc_g0);
3133 sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
3136 case OP_ENDFINALLY: {
3137 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3138 if (!sparc_is_imm13 (spvar->inst_offset)) {
3139 sparc_set (code, spvar->inst_offset, GP_SCRATCH_REG);
3140 sparc_ldi (code, spvar->inst_basereg, GP_SCRATCH_REG, sparc_o7);
3143 sparc_ldi_imm (code, spvar->inst_basereg, spvar->inst_offset, sparc_o7);
3144 sparc_jmpl_imm (code, sparc_o7, 8, sparc_g0);
3148 case OP_CALL_HANDLER:
3149 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3150 /* This is a jump inside the method, so call_simple works even on V9 */
3151 sparc_call_simple (code, 0);
3155 ins->inst_c0 = (guint8*)code - cfg->native_code;
3158 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
3159 if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
3161 if (ins->flags & MONO_INST_BRLABEL) {
3162 if (ins->inst_i0->inst_c0) {
3163 gint32 disp = (ins->inst_i0->inst_c0 - ((guint8*)code - cfg->native_code)) >> 2;
3164 g_assert (sparc_is_imm22 (disp));
3165 sparc_branch (code, 1, sparc_ba, disp);
3167 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
3168 sparc_branch (code, 1, sparc_ba, 0);
3171 if (ins->inst_target_bb->native_offset) {
3172 gint32 disp = (ins->inst_target_bb->native_offset - ((guint8*)code - cfg->native_code)) >> 2;
3173 g_assert (sparc_is_imm22 (disp));
3174 sparc_branch (code, 1, sparc_ba, disp);
3176 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3177 sparc_branch (code, 1, sparc_ba, 0);
3183 sparc_jmp (code, ins->sreg1, sparc_g0);
3191 if (v64 && (cfg->opt & MONO_OPT_CMOV)) {
3192 sparc_clr_reg (code, ins->dreg);
3193 sparc_movcc_imm (code, sparc_xcc, opcode_to_sparc_cond (ins->opcode), 1, ins->dreg);
3196 sparc_clr_reg (code, ins->dreg);
3198 sparc_branchp (code, 1, opcode_to_sparc_cond (ins->opcode), DEFAULT_ICC, 0, 2);
3200 sparc_branch (code, 1, opcode_to_sparc_cond (ins->opcode), 2);
3203 sparc_set (code, 1, ins->dreg);
3211 if (v64 && (cfg->opt & MONO_OPT_CMOV)) {
3212 sparc_clr_reg (code, ins->dreg);
3213 sparc_movcc_imm (code, sparc_icc, opcode_to_sparc_cond (ins->opcode), 1, ins->dreg);
3216 sparc_clr_reg (code, ins->dreg);
3217 sparc_branchp (code, 1, opcode_to_sparc_cond (ins->opcode), sparc_icc_short, 0, 2);
3219 sparc_set (code, 1, ins->dreg);
3222 case OP_COND_EXC_EQ:
3223 case OP_COND_EXC_NE_UN:
3224 case OP_COND_EXC_LT:
3225 case OP_COND_EXC_LT_UN:
3226 case OP_COND_EXC_GT:
3227 case OP_COND_EXC_GT_UN:
3228 case OP_COND_EXC_GE:
3229 case OP_COND_EXC_GE_UN:
3230 case OP_COND_EXC_LE:
3231 case OP_COND_EXC_LE_UN:
3232 case OP_COND_EXC_OV:
3233 case OP_COND_EXC_NO:
3235 case OP_COND_EXC_NC:
3236 EMIT_COND_SYSTEM_EXCEPTION (ins, opcode_to_sparc_cond (ins->opcode), ins->inst_p1);
3238 case OP_SPARC_COND_EXC_EQZ:
3239 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brz, ins->inst_p1);
3241 case OP_SPARC_COND_EXC_GEZ:
3242 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brgez, ins->inst_p1);
3244 case OP_SPARC_COND_EXC_GTZ:
3245 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brgz, ins->inst_p1);
3247 case OP_SPARC_COND_EXC_LEZ:
3248 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brlez, ins->inst_p1);
3250 case OP_SPARC_COND_EXC_LTZ:
3251 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brlz, ins->inst_p1);
3253 case OP_SPARC_COND_EXC_NEZ:
3254 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brnz, ins->inst_p1);
3256 case OP_COND_EXC_IOV:
3257 case OP_COND_EXC_IC:
3258 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (ins, opcode_to_sparc_cond (ins->opcode), ins->inst_p1, TRUE, sparc_icc_short);
3271 EMIT_COND_BRANCH_PREDICTED (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
3273 EMIT_COND_BRANCH (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
3287 /* Only used on V9 */
3288 EMIT_COND_BRANCH_ICC (ins, opcode_to_sparc_cond (ins->opcode), 1, 1, sparc_icc_short);
3293 EMIT_COND_BRANCH_BPR (ins, brz, 1, 1, 1);
3295 case OP_SPARC_BRLEZ:
3296 EMIT_COND_BRANCH_BPR (ins, brlez, 1, 1, 1);
3299 EMIT_COND_BRANCH_BPR (ins, brlz, 1, 1, 1);
3302 EMIT_COND_BRANCH_BPR (ins, brnz, 1, 1, 1);
3305 EMIT_COND_BRANCH_BPR (ins, brgz, 1, 1, 1);
3307 case OP_SPARC_BRGEZ:
3308 EMIT_COND_BRANCH_BPR (ins, brgez, 1, 1, 1);
3311 /* floating point opcodes */
3313 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
3315 sparc_set_template (code, sparc_o7);
3317 sparc_sethi (code, 0, sparc_o7);
3319 sparc_lddf_imm (code, sparc_o7, 0, ins->dreg);
3322 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
3324 sparc_set_template (code, sparc_o7);
3326 sparc_sethi (code, 0, sparc_o7);
3328 sparc_ldf_imm (code, sparc_o7, 0, FP_SCRATCH_REG);
3330 /* Extend to double */
3331 sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
3333 case OP_STORER8_MEMBASE_REG:
3334 if (!sparc_is_imm13 (ins->inst_offset + 4)) {
3335 sparc_set (code, ins->inst_offset, sparc_o7);
3336 /* SPARCV9 handles misaligned fp loads/stores */
3337 if (!v64 && (ins->inst_offset % 8)) {
3339 sparc_add (code, FALSE, ins->inst_destbasereg, sparc_o7, sparc_o7);
3340 sparc_stf (code, ins->sreg1, sparc_o7, sparc_g0);
3341 sparc_stf_imm (code, ins->sreg1 + 1, sparc_o7, 4);
3343 sparc_stdf (code, ins->sreg1, ins->inst_destbasereg, sparc_o7);
3346 if (!v64 && (ins->inst_offset % 8)) {
3348 sparc_stf_imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3349 sparc_stf_imm (code, ins->sreg1 + 1, ins->inst_destbasereg, ins->inst_offset + 4);
3351 sparc_stdf_imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3354 case OP_LOADR8_MEMBASE:
3355 EMIT_LOAD_MEMBASE (ins, lddf);
3357 case OP_STORER4_MEMBASE_REG:
3358 /* This requires a double->single conversion */
3359 sparc_fdtos (code, ins->sreg1, FP_SCRATCH_REG);
3360 if (!sparc_is_imm13 (ins->inst_offset)) {
3361 sparc_set (code, ins->inst_offset, sparc_o7);
3362 sparc_stf (code, FP_SCRATCH_REG, ins->inst_destbasereg, sparc_o7);
3365 sparc_stf_imm (code, FP_SCRATCH_REG, ins->inst_destbasereg, ins->inst_offset);
3367 case OP_LOADR4_MEMBASE: {
3368 /* ldf needs a single precision register */
3369 int dreg = ins->dreg;
3370 ins->dreg = FP_SCRATCH_REG;
3371 EMIT_LOAD_MEMBASE (ins, ldf);
3373 /* Extend to double */
3374 sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
3379 sparc_fmovd (code, ins->sreg1, ins->dreg);
3381 sparc_fmovs (code, ins->sreg1, ins->dreg);
3382 sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
3386 gint32 offset = mono_spillvar_offset_float (cfg, 0);
3388 if (!sparc_is_imm13 (offset)) {
3389 sparc_set (code, offset, sparc_o7);
3390 sparc_stx (code, ins->sreg1, sparc_sp, offset);
3391 sparc_lddf (code, sparc_sp, offset, FP_SCRATCH_REG);
3393 sparc_stx_imm (code, ins->sreg1, sparc_sp, offset);
3394 sparc_lddf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3396 sparc_fxtos (code, FP_SCRATCH_REG, FP_SCRATCH_REG);
3398 if (!sparc_is_imm13 (offset)) {
3399 sparc_set (code, offset, sparc_o7);
3400 sparc_st (code, ins->sreg1, sparc_sp, sparc_o7);
3401 sparc_ldf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
3403 sparc_st_imm (code, ins->sreg1, sparc_sp, offset);
3404 sparc_ldf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3406 sparc_fitos (code, FP_SCRATCH_REG, FP_SCRATCH_REG);
3408 sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
3412 gint32 offset = mono_spillvar_offset_float (cfg, 0);
3414 if (!sparc_is_imm13 (offset)) {
3415 sparc_set (code, offset, sparc_o7);
3416 sparc_stx (code, ins->sreg1, sparc_sp, sparc_o7);
3417 sparc_lddf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
3419 sparc_stx_imm (code, ins->sreg1, sparc_sp, offset);
3420 sparc_lddf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3422 sparc_fxtod (code, FP_SCRATCH_REG, ins->dreg);
3424 if (!sparc_is_imm13 (offset)) {
3425 sparc_set (code, offset, sparc_o7);
3426 sparc_st (code, ins->sreg1, sparc_sp, sparc_o7);
3427 sparc_ldf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
3429 sparc_st_imm (code, ins->sreg1, sparc_sp, offset);
3430 sparc_ldf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3432 sparc_fitod (code, FP_SCRATCH_REG, ins->dreg);
3436 case OP_FCONV_TO_I1:
3437 case OP_FCONV_TO_U1:
3438 case OP_FCONV_TO_I2:
3439 case OP_FCONV_TO_U2:
3444 case OP_FCONV_TO_I4:
3445 case OP_FCONV_TO_U4: {
3446 gint32 offset = mono_spillvar_offset_float (cfg, 0);
3447 sparc_fdtoi (code, ins->sreg1, FP_SCRATCH_REG);
3448 if (!sparc_is_imm13 (offset)) {
3449 sparc_set (code, offset, sparc_o7);
3450 sparc_stdf (code, FP_SCRATCH_REG, sparc_sp, sparc_o7);
3451 sparc_ld (code, sparc_sp, sparc_o7, ins->dreg);
3453 sparc_stdf_imm (code, FP_SCRATCH_REG, sparc_sp, offset);
3454 sparc_ld_imm (code, sparc_sp, offset, ins->dreg);
3457 switch (ins->opcode) {
3458 case OP_FCONV_TO_I1:
3459 case OP_FCONV_TO_U1:
3460 sparc_and_imm (code, 0, ins->dreg, 0xff, ins->dreg);
3462 case OP_FCONV_TO_I2:
3463 case OP_FCONV_TO_U2:
3464 sparc_set (code, 0xffff, sparc_o7);
3465 sparc_and (code, 0, ins->dreg, sparc_o7, ins->dreg);
3472 case OP_FCONV_TO_I8:
3473 case OP_FCONV_TO_U8:
3475 g_assert_not_reached ();
3479 g_assert_not_reached ();
3481 case OP_LCONV_TO_R_UN: {
3483 g_assert_not_reached ();
3486 case OP_LCONV_TO_OVF_I: {
3487 guint32 *br [3], *label [1];
3490 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
3492 sparc_cmp_imm (code, ins->sreg1, 0);
3494 sparc_branch (code, 1, sparc_bneg, 0);
3498 /* ms word must be 0 */
3499 sparc_cmp_imm (code, ins->sreg2, 0);
3501 sparc_branch (code, 1, sparc_be, 0);
3506 EMIT_COND_SYSTEM_EXCEPTION (ins, sparc_ba, "OverflowException");
3509 sparc_patch (br [0], code);
3511 /* ms word must 0xfffffff */
3512 sparc_cmp_imm (code, ins->sreg2, -1);
3514 sparc_branch (code, 1, sparc_bne, 0);
3516 sparc_patch (br [2], label [0]);
3519 sparc_patch (br [1], code);
3520 if (ins->sreg1 != ins->dreg)
3521 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
3525 sparc_faddd (code, ins->sreg1, ins->sreg2, ins->dreg);
3528 sparc_fsubd (code, ins->sreg1, ins->sreg2, ins->dreg);
3531 sparc_fmuld (code, ins->sreg1, ins->sreg2, ins->dreg);
3534 sparc_fdivd (code, ins->sreg1, ins->sreg2, ins->dreg);
3538 sparc_fnegd (code, ins->sreg1, ins->dreg);
3540 /* FIXME: why don't use fnegd ? */
3541 sparc_fnegs (code, ins->sreg1, ins->dreg);
3545 sparc_fdivd (code, ins->sreg1, ins->sreg2, FP_SCRATCH_REG);
3546 sparc_fmuld (code, ins->sreg2, FP_SCRATCH_REG, FP_SCRATCH_REG);
3547 sparc_fsubd (code, ins->sreg1, FP_SCRATCH_REG, ins->dreg);
3550 sparc_fcmpd (code, ins->sreg1, ins->sreg2);
3557 sparc_fcmpd (code, ins->sreg1, ins->sreg2);
3558 sparc_clr_reg (code, ins->dreg);
3559 switch (ins->opcode) {
3562 sparc_fbranch (code, 1, opcode_to_sparc_cond (ins->opcode), 4);
3564 sparc_set (code, 1, ins->dreg);
3565 sparc_fbranch (code, 1, sparc_fbu, 2);
3567 sparc_set (code, 1, ins->dreg);
3570 sparc_fbranch (code, 1, opcode_to_sparc_cond (ins->opcode), 2);
3572 sparc_set (code, 1, ins->dreg);
3578 EMIT_FLOAT_COND_BRANCH (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
3581 /* clt.un + brfalse */
3583 sparc_fbranch (code, 1, sparc_fbul, 0);
3586 EMIT_FLOAT_COND_BRANCH (ins, sparc_fba, 1, 1);
3587 sparc_patch (p, (guint8*)code);
3591 /* cgt.un + brfalse */
3593 sparc_fbranch (code, 1, sparc_fbug, 0);
3596 EMIT_FLOAT_COND_BRANCH (ins, sparc_fba, 1, 1);
3597 sparc_patch (p, (guint8*)code);
3601 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbne, 1, 1);
3602 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3605 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbl, 1, 1);
3606 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3609 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbg, 1, 1);
3610 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3613 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbge, 1, 1);
3614 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3617 EMIT_FLOAT_COND_BRANCH (ins, sparc_fble, 1, 1);
3618 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3621 gint32 offset = mono_spillvar_offset_float (cfg, 0);
3622 if (!sparc_is_imm13 (offset)) {
3623 sparc_set (code, offset, sparc_o7);
3624 sparc_stdf (code, ins->sreg1, sparc_sp, sparc_o7);
3625 sparc_lduh (code, sparc_sp, sparc_o7, sparc_o7);
3627 sparc_stdf_imm (code, ins->sreg1, sparc_sp, offset);
3628 sparc_lduh_imm (code, sparc_sp, offset, sparc_o7);
3630 sparc_srl_imm (code, sparc_o7, 4, sparc_o7);
3631 sparc_and_imm (code, FALSE, sparc_o7, 2047, sparc_o7);
3632 sparc_cmp_imm (code, sparc_o7, 2047);
3633 EMIT_COND_SYSTEM_EXCEPTION (ins, sparc_be, "ArithmeticException");
3635 sparc_fmovd (code, ins->sreg1, ins->dreg);
3637 sparc_fmovs (code, ins->sreg1, ins->dreg);
3638 sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
3643 case OP_MEMORY_BARRIER:
3644 sparc_membar (code, sparc_membar_all);
3649 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3651 g_warning ("%s:%d: unknown opcode %s\n", __FILE__, __LINE__, mono_inst_name (ins->opcode));
3653 g_assert_not_reached ();
3656 if ((((guint8*)code) - code_start) > max_len) {
3657 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3658 mono_inst_name (ins->opcode), max_len, ((guint8*)code) - code_start);
3659 g_assert_not_reached ();
3669 cfg->code_len = (guint8*)code - cfg->native_code;
3673 mono_arch_register_lowlevel_calls (void)
3675 mono_register_jit_icall (mono_arch_break, "mono_arch_break", NULL, TRUE);
3676 mono_register_jit_icall (mono_arch_get_lmf_addr, "mono_arch_get_lmf_addr", NULL, TRUE);
3680 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3682 MonoJumpInfo *patch_info;
3684 /* FIXME: Move part of this to arch independent code */
3685 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3686 unsigned char *ip = patch_info->ip.i + code;
3689 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
3691 switch (patch_info->type) {
3692 case MONO_PATCH_INFO_NONE:
3694 case MONO_PATCH_INFO_CLASS_INIT: {
3695 guint32 *ip2 = (guint32*)ip;
3696 /* Might already been changed to a nop */
3698 sparc_set_template (ip2, sparc_o7);
3699 sparc_jmpl (ip2, sparc_o7, sparc_g0, sparc_o7);
3701 sparc_call_simple (ip2, 0);
3705 case MONO_PATCH_INFO_METHOD_JUMP: {
3706 guint32 *ip2 = (guint32*)ip;
3707 /* Might already been patched */
3708 sparc_set_template (ip2, sparc_o7);
3714 sparc_patch ((guint32*)ip, target);
3719 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
3722 guint32 *code = (guint32*)p;
3723 MonoMethodSignature *sig = mono_method_signature (cfg->method);
3726 /* Save registers to stack */
3727 for (i = 0; i < 6; ++i)
3728 sparc_sti_imm (code, sparc_i0 + i, sparc_fp, ARGS_OFFSET + (i * sizeof (gpointer)));
3730 cinfo = get_call_info (sig, FALSE);
3732 /* Save float regs on V9, since they are caller saved */
3733 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3734 ArgInfo *ainfo = cinfo->args + i;
3735 gint32 stack_offset;
3737 stack_offset = ainfo->offset + ARGS_OFFSET;
3739 if (ainfo->storage == ArgInFloatReg) {
3740 if (!sparc_is_imm13 (stack_offset))
3742 sparc_stf_imm (code, ainfo->reg, sparc_fp, stack_offset);
3744 else if (ainfo->storage == ArgInDoubleReg) {
3745 /* The offset is guaranteed to be aligned by the ABI rules */
3746 sparc_stdf_imm (code, ainfo->reg, sparc_fp, stack_offset);
3750 sparc_set (code, cfg->method, sparc_o0);
3751 sparc_add_imm (code, FALSE, sparc_fp, MONO_SPARC_STACK_BIAS, sparc_o1);
3753 mono_add_patch_info (cfg, (guint8*)code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
3756 /* Restore float regs on V9 */
3757 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3758 ArgInfo *ainfo = cinfo->args + i;
3759 gint32 stack_offset;
3761 stack_offset = ainfo->offset + ARGS_OFFSET;
3763 if (ainfo->storage == ArgInFloatReg) {
3764 if (!sparc_is_imm13 (stack_offset))
3766 sparc_ldf_imm (code, sparc_fp, stack_offset, ainfo->reg);
3768 else if (ainfo->storage == ArgInDoubleReg) {
3769 /* The offset is guaranteed to be aligned by the ABI rules */
3770 sparc_lddf_imm (code, sparc_fp, stack_offset, ainfo->reg);
3788 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
3790 guint32 *code = (guint32*)p;
3791 int save_mode = SAVE_NONE;
3792 MonoMethod *method = cfg->method;
3794 switch (mono_type_get_underlying_type (mono_method_signature (method)->ret)->type) {
3795 case MONO_TYPE_VOID:
3796 /* special case string .ctor icall */
3797 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
3798 save_mode = SAVE_ONE;
3800 save_mode = SAVE_NONE;
3805 save_mode = SAVE_ONE;
3807 save_mode = SAVE_TWO;
3812 save_mode = SAVE_FP;
3814 case MONO_TYPE_VALUETYPE:
3815 save_mode = SAVE_STRUCT;
3818 save_mode = SAVE_ONE;
3822 /* Save the result to the stack and also put it into the output registers */
3824 switch (save_mode) {
3827 sparc_st_imm (code, sparc_i0, sparc_fp, 68);
3828 sparc_st_imm (code, sparc_i0, sparc_fp, 72);
3829 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
3830 sparc_mov_reg_reg (code, sparc_i1, sparc_o2);
3833 sparc_sti_imm (code, sparc_i0, sparc_fp, ARGS_OFFSET);
3834 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
3838 sparc_stdf_imm (code, sparc_f0, sparc_fp, ARGS_OFFSET);
3840 sparc_stdf_imm (code, sparc_f0, sparc_fp, 72);
3841 sparc_ld_imm (code, sparc_fp, 72, sparc_o1);
3842 sparc_ld_imm (code, sparc_fp, 72 + 4, sparc_o2);
3847 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
3849 sparc_ld_imm (code, sparc_fp, 64, sparc_o1);
3857 sparc_set (code, cfg->method, sparc_o0);
3859 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, func);
3862 /* Restore result */
3864 switch (save_mode) {
3866 sparc_ld_imm (code, sparc_fp, 68, sparc_i0);
3867 sparc_ld_imm (code, sparc_fp, 72, sparc_i0);
3870 sparc_ldi_imm (code, sparc_fp, ARGS_OFFSET, sparc_i0);
3873 sparc_lddf_imm (code, sparc_fp, ARGS_OFFSET, sparc_f0);
3884 mono_arch_emit_prolog (MonoCompile *cfg)
3886 MonoMethod *method = cfg->method;
3887 MonoMethodSignature *sig;
3893 cfg->code_size = 256;
3894 cfg->native_code = g_malloc (cfg->code_size);
3895 code = (guint32*)cfg->native_code;
3897 /* FIXME: Generate intermediate code instead */
3899 offset = cfg->stack_offset;
3900 offset += (16 * sizeof (gpointer)); /* register save area */
3902 offset += 4; /* struct/union return pointer */
3905 /* add parameter area size for called functions */
3906 if (cfg->param_area < (6 * sizeof (gpointer)))
3907 /* Reserve space for the first 6 arguments even if it is unused */
3908 offset += 6 * sizeof (gpointer);
3910 offset += cfg->param_area;
3912 /* align the stack size */
3913 offset = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
3916 * localloc'd memory is stored between the local variables (whose
3917 * size is given by cfg->stack_offset), and between the space reserved
3920 cfg->arch.localloc_offset = offset - cfg->stack_offset;
3922 cfg->stack_offset = offset;
3924 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
3925 /* Perform stack touching */
3929 if (!sparc_is_imm13 (- cfg->stack_offset)) {
3930 /* Can't use sparc_o7 here, since we're still in the caller's frame */
3931 sparc_set (code, (- cfg->stack_offset), GP_SCRATCH_REG);
3932 sparc_save (code, sparc_sp, GP_SCRATCH_REG, sparc_sp);
3935 sparc_save_imm (code, sparc_sp, - cfg->stack_offset, sparc_sp);
3938 if (strstr (cfg->method->name, "foo")) {
3939 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, mono_sparc_break);
3940 sparc_call_simple (code, 0);
3945 sig = mono_method_signature (method);
3947 cinfo = get_call_info (sig, FALSE);
3949 /* Keep in sync with emit_load_volatile_arguments */
3950 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3951 ArgInfo *ainfo = cinfo->args + i;
3952 gint32 stack_offset;
3954 inst = cfg->args [i];
3956 if (sig->hasthis && (i == 0))
3957 arg_type = &mono_defaults.object_class->byval_arg;
3959 arg_type = sig->params [i - sig->hasthis];
3961 stack_offset = ainfo->offset + ARGS_OFFSET;
3963 /* Save the split arguments so they will reside entirely on the stack */
3964 if (ainfo->storage == ArgInSplitRegStack) {
3965 /* Save the register to the stack */
3966 g_assert (inst->opcode == OP_REGOFFSET);
3967 if (!sparc_is_imm13 (stack_offset))
3969 sparc_st_imm (code, sparc_i5, inst->inst_basereg, stack_offset);
3972 if (!v64 && !arg_type->byref && (arg_type->type == MONO_TYPE_R8)) {
3973 /* Save the argument to a dword aligned stack location */
3975 * stack_offset contains the offset of the argument on the stack.
3976 * inst->inst_offset contains the dword aligned offset where the value
3979 if (ainfo->storage == ArgInIRegPair) {
3980 if (!sparc_is_imm13 (inst->inst_offset + 4))
3982 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, inst->inst_offset);
3983 sparc_st_imm (code, sparc_i0 + ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
3986 if (ainfo->storage == ArgInSplitRegStack) {
3988 g_assert_not_reached ();
3990 if (stack_offset != inst->inst_offset) {
3991 /* stack_offset is not dword aligned, so we need to make a copy */
3992 sparc_st_imm (code, sparc_i5, inst->inst_basereg, inst->inst_offset);
3993 sparc_ld_imm (code, sparc_fp, stack_offset + 4, sparc_o7);
3994 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset + 4);
3998 if (ainfo->storage == ArgOnStackPair) {
4000 g_assert_not_reached ();
4002 if (stack_offset != inst->inst_offset) {
4003 /* stack_offset is not dword aligned, so we need to make a copy */
4004 sparc_ld_imm (code, sparc_fp, stack_offset, sparc_o7);
4005 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset);
4006 sparc_ld_imm (code, sparc_fp, stack_offset + 4, sparc_o7);
4007 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset + 4);
4011 g_assert_not_reached ();
4014 if ((ainfo->storage == ArgInIReg) && (inst->opcode != OP_REGVAR)) {
4015 /* Argument in register, but need to be saved to stack */
4016 if (!sparc_is_imm13 (stack_offset))
4018 if ((stack_offset - ARGS_OFFSET) & 0x1)
4019 sparc_stb_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
4021 if ((stack_offset - ARGS_OFFSET) & 0x2)
4022 sparc_sth_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
4024 if ((stack_offset - ARGS_OFFSET) & 0x4)
4025 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
4028 sparc_stx_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
4030 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
4034 if ((ainfo->storage == ArgInIRegPair) && (inst->opcode != OP_REGVAR)) {
4038 /* Argument in regpair, but need to be saved to stack */
4039 if (!sparc_is_imm13 (inst->inst_offset + 4))
4041 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, inst->inst_offset);
4042 sparc_st_imm (code, sparc_i0 + ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
4044 else if ((ainfo->storage == ArgInFloatReg) && (inst->opcode != OP_REGVAR)) {
4045 if (!sparc_is_imm13 (stack_offset))
4047 sparc_stf_imm (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4049 else if ((ainfo->storage == ArgInDoubleReg) && (inst->opcode != OP_REGVAR)) {
4050 /* The offset is guaranteed to be aligned by the ABI rules */
4051 sparc_stdf_imm (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4054 if ((ainfo->storage == ArgInFloatReg) && (inst->opcode == OP_REGVAR)) {
4055 /* Need to move into the a double precision register */
4056 sparc_fstod (code, ainfo->reg, ainfo->reg - 1);
4059 if ((ainfo->storage == ArgInSplitRegStack) || (ainfo->storage == ArgOnStack))
4060 if (inst->opcode == OP_REGVAR)
4061 /* FIXME: Load the argument into memory */
4067 if (cfg->method->save_lmf) {
4068 gint32 lmf_offset = STACK_BIAS - cfg->arch.lmf_offset;
4071 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
4072 sparc_set_template (code, sparc_o7);
4073 sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ip));
4075 sparc_sti_imm (code, sparc_sp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, sp));
4077 sparc_sti_imm (code, sparc_fp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp));
4079 /* FIXME: add a relocation for this */
4080 sparc_set (code, cfg->method, sparc_o7);
4081 sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method));
4083 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
4084 (gpointer)"mono_arch_get_lmf_addr");
4087 code = (guint32*)mono_sparc_emit_save_lmf (code, lmf_offset);
4090 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4091 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
4093 cfg->code_len = (guint8*)code - cfg->native_code;
4095 g_assert (cfg->code_len <= cfg->code_size);
4097 return (guint8*)code;
4101 mono_arch_emit_epilog (MonoCompile *cfg)
4103 MonoMethod *method = cfg->method;
4106 int max_epilog_size = 16 + 20 * 4;
4108 if (cfg->method->save_lmf)
4109 max_epilog_size += 128;
4111 if (mono_jit_trace_calls != NULL)
4112 max_epilog_size += 50;
4114 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
4115 max_epilog_size += 50;
4117 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
4118 cfg->code_size *= 2;
4119 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4120 mono_jit_stats.code_reallocs++;
4123 code = (guint32*)(cfg->native_code + cfg->code_len);
4125 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4126 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
4128 if (cfg->method->save_lmf) {
4129 gint32 lmf_offset = STACK_BIAS - cfg->arch.lmf_offset;
4131 code = mono_sparc_emit_restore_lmf (code, lmf_offset);
4135 * The V8 ABI requires that calls to functions which return a structure
4138 if (!v64 && mono_method_signature (cfg->method)->pinvoke && MONO_TYPE_ISSTRUCT(mono_method_signature (cfg->method)->ret))
4139 sparc_jmpl_imm (code, sparc_i7, 12, sparc_g0);
4143 /* Only fold last instruction into the restore if the exit block has an in count of 1
4144 and the previous block hasn't been optimized away since it may have an in count > 1 */
4145 if (cfg->bb_exit->in_count == 1 && cfg->bb_exit->in_bb[0]->native_offset != cfg->bb_exit->native_offset)
4148 /* Try folding last instruction into the restore */
4149 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)) {
4150 /* or reg, imm, %i0 */
4151 int reg = sparc_inst_rs1 (code [-2]);
4152 int imm = sparc_inst_imm13 (code [-2]);
4153 code [-2] = code [-1];
4155 sparc_restore_imm (code, reg, imm, sparc_o0);
4158 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)) {
4159 /* or reg, reg, %i0 */
4160 int reg1 = sparc_inst_rs1 (code [-2]);
4161 int reg2 = sparc_inst_rs2 (code [-2]);
4162 code [-2] = code [-1];
4164 sparc_restore (code, reg1, reg2, sparc_o0);
4167 sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
4169 cfg->code_len = (guint8*)code - cfg->native_code;
4171 g_assert (cfg->code_len < cfg->code_size);
4176 mono_arch_emit_exceptions (MonoCompile *cfg)
4178 MonoJumpInfo *patch_info;
4183 MonoClass *exc_classes [16];
4184 guint8 *exc_throw_start [16], *exc_throw_end [16];
4186 /* Compute needed space */
4187 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4188 if (patch_info->type == MONO_PATCH_INFO_EXC)
4193 * make sure we have enough space for exceptions
4196 code_size = exc_count * (20 * 4);
4198 code_size = exc_count * 24;
4201 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
4202 cfg->code_size *= 2;
4203 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4204 mono_jit_stats.code_reallocs++;
4207 code = (guint32*)(cfg->native_code + cfg->code_len);
4209 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4210 switch (patch_info->type) {
4211 case MONO_PATCH_INFO_EXC: {
4212 MonoClass *exc_class;
4213 guint32 *buf, *buf2;
4214 guint32 throw_ip, type_idx;
4217 sparc_patch ((guint32*)(cfg->native_code + patch_info->ip.i), code);
4219 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
4220 g_assert (exc_class);
4221 type_idx = exc_class->type_token - MONO_TOKEN_TYPE_DEF;
4222 throw_ip = patch_info->ip.i;
4224 /* Find a throw sequence for the same exception class */
4225 for (i = 0; i < nthrows; ++i)
4226 if (exc_classes [i] == exc_class)
4230 guint32 throw_offset = (((guint8*)exc_throw_end [i] - cfg->native_code) - throw_ip) >> 2;
4231 if (!sparc_is_imm13 (throw_offset))
4232 sparc_set32 (code, throw_offset, sparc_o1);
4234 disp = (exc_throw_start [i] - (guint8*)code) >> 2;
4235 g_assert (sparc_is_imm22 (disp));
4236 sparc_branch (code, 0, sparc_ba, disp);
4237 if (sparc_is_imm13 (throw_offset))
4238 sparc_set32 (code, throw_offset, sparc_o1);
4241 patch_info->type = MONO_PATCH_INFO_NONE;
4244 /* Emit the template for setting o1 */
4246 if (sparc_is_imm13 (((((guint8*)code - cfg->native_code) - throw_ip) >> 2) - 8))
4247 /* Can use a short form */
4250 sparc_set_template (code, sparc_o1);
4254 exc_classes [nthrows] = exc_class;
4255 exc_throw_start [nthrows] = (guint8*)code;
4259 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, mono_sparc_break);
4263 /* first arg = type token */
4264 /* Pass the type index to reduce the size of the sparc_set */
4265 if (!sparc_is_imm13 (type_idx))
4266 sparc_set32 (code, type_idx, sparc_o0);
4268 /* second arg = offset between the throw ip and the current ip */
4269 /* On sparc, the saved ip points to the call instruction */
4270 disp = (((guint8*)code - cfg->native_code) - throw_ip) >> 2;
4271 sparc_set32 (buf, disp, sparc_o1);
4276 exc_throw_end [nthrows] = (guint8*)code;
4280 patch_info->data.name = "mono_arch_throw_corlib_exception";
4281 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
4282 patch_info->ip.i = (guint8*)code - cfg->native_code;
4286 if (sparc_is_imm13 (type_idx)) {
4287 /* Put it into the delay slot */
4290 sparc_set32 (code, type_idx, sparc_o0);
4291 g_assert (code - buf == 1);
4302 cfg->code_len = (guint8*)code - cfg->native_code;
4304 g_assert (cfg->code_len < cfg->code_size);
4308 gboolean lmf_addr_key_inited = FALSE;
4310 #ifdef MONO_SPARC_THR_TLS
4311 thread_key_t lmf_addr_key;
4313 pthread_key_t lmf_addr_key;
4317 mono_arch_get_lmf_addr (void)
4319 /* This is perf critical so we bypass the IO layer */
4320 /* The thr_... functions seem to be somewhat faster */
4321 #ifdef MONO_SPARC_THR_TLS
4323 thr_getspecific (lmf_addr_key, &res);
4326 return pthread_getspecific (lmf_addr_key);
4330 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
4333 * There seems to be no way to determine stack boundaries under solaris,
4334 * so it's not possible to determine whenever a SIGSEGV is caused by stack
4337 #error "--with-sigaltstack=yes not supported on solaris"
4342 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
4344 if (!lmf_addr_key_inited) {
4347 lmf_addr_key_inited = TRUE;
4349 #ifdef MONO_SPARC_THR_TLS
4350 res = thr_keycreate (&lmf_addr_key, NULL);
4352 res = pthread_key_create (&lmf_addr_key, NULL);
4354 g_assert (res == 0);
4358 #ifdef MONO_SPARC_THR_TLS
4359 thr_setspecific (lmf_addr_key, &tls->lmf);
4361 pthread_setspecific (lmf_addr_key, &tls->lmf);
4366 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
4371 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *call, int this_reg, int this_type, int vt_reg)
4373 int this_out_reg = sparc_o0;
4378 MONO_INST_NEW (cfg, ins, OP_MOVE);
4379 ins->sreg1 = vt_reg;
4380 ins->dreg = mono_regstate_next_int (cfg->rs);
4381 mono_bblock_add_inst (cfg->cbb, ins);
4383 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, sparc_o0, FALSE);
4385 this_out_reg = sparc_o1;
4387 /* Set the 'struct/union return pointer' location on the stack */
4388 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, sparc_sp, 64, vt_reg);
4392 /* add the this argument */
4393 if (this_reg != -1) {
4395 MONO_INST_NEW (cfg, this, OP_MOVE);
4396 this->type = this_type;
4397 this->sreg1 = this_reg;
4398 this->dreg = mono_regstate_next_int (cfg->rs);
4399 mono_bblock_add_inst (cfg->cbb, this);
4401 mono_call_inst_add_outarg_reg (cfg, call, this->dreg, this_out_reg, FALSE);
4407 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4409 MonoInst *ins = NULL;
4411 if (cmethod->klass == mono_defaults.thread_class &&
4412 strcmp (cmethod->name, "MemoryBarrier") == 0) {
4414 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
4421 * mono_arch_get_argument_info:
4422 * @csig: a method signature
4423 * @param_count: the number of parameters to consider
4424 * @arg_info: an array to store the result infos
4426 * Gathers information on parameters such as size, alignment and
4427 * padding. arg_info should be large enought to hold param_count + 1 entries.
4429 * Returns the size of the activation frame.
4432 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
4438 cinfo = get_call_info (csig, FALSE);
4440 if (csig->hasthis) {
4441 ainfo = &cinfo->args [0];
4442 arg_info [0].offset = ARGS_OFFSET - MONO_SPARC_STACK_BIAS + ainfo->offset;
4445 for (k = 0; k < param_count; k++) {
4446 ainfo = &cinfo->args [k + csig->hasthis];
4448 arg_info [k + 1].offset = ARGS_OFFSET - MONO_SPARC_STACK_BIAS + ainfo->offset;
4449 arg_info [k + 1].size = mono_type_size (csig->params [k], &align);
4458 mono_arch_print_tree (MonoInst *tree, int arity)
4463 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
4468 MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)