2 * mini-sparc.c: Sparc backend for the Mono code generator
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
9 * Christopher Taylor (ct@gentoo.org)
10 * Mark Crichton (crichton@gimp.org)
11 * Zoltan Varga (vargaz@freemail.hu)
13 * (C) 2003 Ximian, Inc.
21 #include <sys/systeminfo.h>
28 #include <mono/metadata/appdomain.h>
29 #include <mono/metadata/debug-helpers.h>
30 #include <mono/metadata/tokentype.h>
31 #include <mono/utils/mono-math.h>
33 #include "mini-sparc.h"
36 #include "cpu-sparc.h"
39 * Sparc V9 means two things:
40 * - the instruction set
43 * V9 instructions are only usable if the underlying processor is 64 bit. Most Sparc
44 * processors in use are 64 bit processors. The V9 ABI is only usable if the
45 * mono executable is a 64 bit executable. So it would make sense to use the 64 bit
46 * instructions without using the 64 bit ABI.
51 * - %i0..%i<n> hold the incoming arguments, these are never written by JITted
52 * code. Unused input registers are used for global register allocation.
53 * - %o0..%o5 and %l7 is used for local register allocation and passing arguments
54 * - %l0..%l6 is used for global register allocation
55 * - %o7 and %g1 is used as scratch registers in opcodes
56 * - all floating point registers are used for local register allocation except %f0.
57 * Only double precision registers are used.
59 * - fp registers %d0..%d30 are used for parameter passing, and %d32..%d62 are
60 * used for local allocation.
65 * - doubles and longs must be stored in dword aligned locations
69 * The following things are not implemented or do not work:
70 * - some fp arithmetic corner cases
71 * The following tests in mono/mini are expected to fail:
72 * - test_0_simple_double_casts
73 * This test casts (guint64)-1 to double and then back to guint64 again.
74 * Under x86, it returns 0, while under sparc it returns -1.
76 * In addition to this, the runtime requires the trunc function, or its
77 * solaris counterpart, aintl, to do some double->int conversions. If this
78 * function is not available, it is emulated somewhat, but the results can be
84 * - optimize sparc_set according to the memory model
85 * - when non-AOT compiling, compute patch targets immediately so we don't
86 * have to emit the 6 byte template.
88 * - struct arguments/returns
93 * - sparc_call_simple can't be used in a lot of places since the displacement
94 * might not fit into an imm30.
95 * - g1 can't be used in a lot of places since it is used as a scratch reg in
97 * - sparc_f0 can't be used as a scratch register on V9
98 * - the %d34..%d62 fp registers are encoded as: %dx = %f(x - 32 + 1), ie.
100 * - ldind.i4/u4 needs to sign extend/clear out upper word -> slows things down
101 * - ins->dreg can't be used as a scatch register in r4 opcodes since it might
102 * be a double precision register which has no single precision part.
103 * - passing/returning structs is hard to implement, because:
104 * - the spec is very hard to understand
105 * - it requires knowledge about the fields of structure, needs to handle
106 * nested structures etc.
110 * Possible optimizations:
111 * - delay slot scheduling
112 * - allocate large constants to registers
113 * - add more mul/div/rem optimizations
117 #define MONO_SPARC_THR_TLS 1
121 * There was a 64 bit bug in glib-2.2: g_bit_nth_msf (0, -1) would return 32,
122 * causing infinite loops in dominator computation. So glib-2.4 is required.
125 #if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 4
126 #error "glib 2.4 or later is required for 64 bit mode."
130 #define NOT_IMPLEMENTED do { g_assert_not_reached (); } while (0)
132 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
134 #define SIGNAL_STACK_SIZE (64 * 1024)
136 #define STACK_BIAS MONO_SPARC_STACK_BIAS
140 /* %g1 is used by sparc_set */
141 #define GP_SCRATCH_REG sparc_g4
142 /* %f0 is used for parameter passing */
143 #define FP_SCRATCH_REG sparc_f30
144 #define ARGS_OFFSET (STACK_BIAS + 128)
148 #define FP_SCRATCH_REG sparc_f0
149 #define ARGS_OFFSET 68
150 #define GP_SCRATCH_REG sparc_g1
154 /* Whenever the CPU supports v9 instructions */
155 static gboolean sparcv9 = FALSE;
157 /* Whenever this is a 64bit executable */
159 static gboolean v64 = TRUE;
161 static gboolean v64 = FALSE;
164 static gpointer mono_arch_get_lmf_addr (void);
167 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar);
170 mono_arch_regname (int reg) {
171 static const char * rnames[] = {
172 "sparc_g0", "sparc_g1", "sparc_g2", "sparc_g3", "sparc_g4",
173 "sparc_g5", "sparc_g6", "sparc_g7", "sparc_o0", "sparc_o1",
174 "sparc_o2", "sparc_o3", "sparc_o4", "sparc_o5", "sparc_sp",
175 "sparc_call", "sparc_l0", "sparc_l1", "sparc_l2", "sparc_l3",
176 "sparc_l4", "sparc_l5", "sparc_l6", "sparc_l7", "sparc_i0",
177 "sparc_i1", "sparc_i2", "sparc_i3", "sparc_i4", "sparc_i5",
178 "sparc_fp", "sparc_retadr"
180 if (reg >= 0 && reg < 32)
186 mono_arch_fregname (int reg) {
187 static const char *rnames [] = {
188 "sparc_f0", "sparc_f1", "sparc_f2", "sparc_f3", "sparc_f4",
189 "sparc_f5", "sparc_f6", "sparc_f7", "sparc_f8", "sparc_f9",
190 "sparc_f10", "sparc_f11", "sparc_f12", "sparc_f13", "sparc_f14",
191 "sparc_f15", "sparc_f16", "sparc_f17", "sparc_f18", "sparc_f19",
192 "sparc_f20", "sparc_f21", "sparc_f22", "sparc_f23", "sparc_f24",
193 "sparc_f25", "sparc_f26", "sparc_f27", "sparc_f28", "sparc_f29",
194 "sparc_f30", "sparc_f31"
197 if (reg >= 0 && reg < 32)
204 * Initialize the cpu to execute managed code.
207 mono_arch_cpu_init (void)
210 /* make sure sparcv9 is initialized for embedded use */
211 mono_arch_cpu_optimizazions(&dummy);
215 * This function returns the optimizations supported on this cpu.
218 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
226 if (!sysinfo (SI_ISALIST, buf, 1024))
227 g_assert_not_reached ();
229 /* From glibc. If the getpagesize is 8192, we're on sparc64, which
230 * (in)directly implies that we're a v9 or better.
231 * Improvements to this are greatly accepted...
232 * Also, we don't differentiate between v7 and v8. I sense SIGILL
233 * sniffing in my future.
235 if (getpagesize() == 8192)
236 strcpy (buf, "sparcv9");
238 strcpy (buf, "sparcv8");
242 * On some processors, the cmov instructions are even slower than the
245 if (strstr (buf, "sparcv9")) {
246 opts |= MONO_OPT_CMOV | MONO_OPT_FCMOV;
250 *exclude_mask |= MONO_OPT_CMOV | MONO_OPT_FCMOV;
256 mono_arch_break (void)
261 #define flushi(addr) __asm__ __volatile__ ("iflush %0"::"r"(addr):"memory")
262 #else /* assume Sun's compiler */
263 static void flushi(void *addr)
270 void sync_instruction_memory(caddr_t addr, int len);
274 mono_arch_flush_icache (guint8 *code, gint size)
277 /* Hopefully this is optimized based on the actual CPU */
278 sync_instruction_memory (code, size);
280 guint64 *p = (guint64*)code;
281 guint64 *end = (guint64*)(code + ((size + 8) /8));
284 * FIXME: Flushing code in dword chunks in _slow_.
288 __asm__ __volatile__ ("iflush %0"::"r"(p++));
298 * Flush all register windows to memory. Every register window is saved to
299 * a 16 word area on the stack pointed to by its %sp register.
302 mono_sparc_flushw (void)
304 static guint32 start [64];
305 static int inited = 0;
307 static void (*flushw) (void);
312 sparc_save_imm (code, sparc_sp, -160, sparc_sp);
315 sparc_restore_simple (code);
317 g_assert ((code - start) < 64);
319 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
321 flushw = (gpointer)start;
330 mono_arch_flush_register_windows (void)
332 mono_sparc_flushw ();
336 mono_arch_is_inst_imm (gint64 imm)
338 return sparc_is_imm13 (imm);
342 mono_sparc_is_v9 (void) {
347 mono_sparc_is_sparc64 (void) {
359 ArgInFloatReg, /* V9 only */
360 ArgInDoubleReg /* V9 only */
365 /* This needs to be offset by %i0 or %o0 depending on caller/callee */
368 guint32 vt_offset; /* for valuetypes */
386 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean pair)
388 ainfo->offset = *stack_size;
391 if (*gr >= PARAM_REGS) {
392 ainfo->storage = ArgOnStack;
395 ainfo->storage = ArgInIReg;
400 /* Allways reserve stack space for parameters passed in registers */
401 (*stack_size) += sizeof (gpointer);
404 if (*gr < PARAM_REGS - 1) {
405 /* A pair of registers */
406 ainfo->storage = ArgInIRegPair;
410 else if (*gr >= PARAM_REGS) {
411 /* A pair of stack locations */
412 ainfo->storage = ArgOnStackPair;
415 ainfo->storage = ArgInSplitRegStack;
420 (*stack_size) += 2 * sizeof (gpointer);
426 #define FLOAT_PARAM_REGS 32
429 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean single)
431 ainfo->offset = *stack_size;
434 if (*gr >= FLOAT_PARAM_REGS) {
435 ainfo->storage = ArgOnStack;
438 /* A single is passed in an even numbered fp register */
439 ainfo->storage = ArgInFloatReg;
440 ainfo->reg = *gr + 1;
445 if (*gr < FLOAT_PARAM_REGS) {
446 /* A double register */
447 ainfo->storage = ArgInDoubleReg;
452 ainfo->storage = ArgOnStack;
456 (*stack_size) += sizeof (gpointer);
464 * Obtain information about a call according to the calling convention.
465 * For V8, see the "System V ABI, Sparc Processor Supplement" Sparc V8 version
466 * document for more information.
467 * For V9, see the "Low Level System Information (64-bit psABI)" chapter in
468 * the 'Sparc Compliance Definition 2.4' document.
471 get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
474 int n = sig->hasthis + sig->param_count;
475 guint32 stack_size = 0;
478 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
484 if (MONO_TYPE_ISSTRUCT ((sig->ret))) {
485 /* The address of the return value is passed in %o0 */
486 add_general (&gr, &stack_size, &cinfo->ret, FALSE);
487 cinfo->ret.reg += sparc_i0;
493 add_general (&gr, &stack_size, cinfo->args + 0, FALSE);
495 if ((sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
498 /* Emit the signature cookie just before the implicit arguments */
499 add_general (&gr, &stack_size, &cinfo->sig_cookie, FALSE);
502 for (i = 0; i < sig->param_count; ++i) {
503 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
505 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
508 /* Emit the signature cookie just before the implicit arguments */
509 add_general (&gr, &stack_size, &cinfo->sig_cookie, FALSE);
512 DEBUG(printf("param %d: ", i));
513 if (sig->params [i]->byref) {
514 DEBUG(printf("byref\n"));
516 add_general (&gr, &stack_size, ainfo, FALSE);
519 switch (mono_type_get_underlying_type (sig->params [i])->type) {
520 case MONO_TYPE_BOOLEAN:
523 add_general (&gr, &stack_size, ainfo, FALSE);
524 /* the value is in the ls byte */
525 ainfo->offset += sizeof (gpointer) - 1;
530 add_general (&gr, &stack_size, ainfo, FALSE);
531 /* the value is in the ls word */
532 ainfo->offset += sizeof (gpointer) - 2;
536 add_general (&gr, &stack_size, ainfo, FALSE);
537 /* the value is in the ls dword */
538 ainfo->offset += sizeof (gpointer) - 4;
543 case MONO_TYPE_FNPTR:
544 case MONO_TYPE_CLASS:
545 case MONO_TYPE_OBJECT:
546 case MONO_TYPE_STRING:
547 case MONO_TYPE_SZARRAY:
548 case MONO_TYPE_ARRAY:
549 add_general (&gr, &stack_size, ainfo, FALSE);
551 case MONO_TYPE_GENERICINST:
552 if (!mono_type_generic_inst_is_valuetype (sig->params [i])) {
553 add_general (&gr, &stack_size, ainfo, FALSE);
557 case MONO_TYPE_VALUETYPE:
562 add_general (&gr, &stack_size, ainfo, FALSE);
564 case MONO_TYPE_TYPEDBYREF:
565 add_general (&gr, &stack_size, ainfo, FALSE);
570 add_general (&gr, &stack_size, ainfo, FALSE);
572 add_general (&gr, &stack_size, ainfo, TRUE);
577 add_float (&fr, &stack_size, ainfo, TRUE);
580 /* single precision values are passed in integer registers */
581 add_general (&gr, &stack_size, ainfo, FALSE);
586 add_float (&fr, &stack_size, ainfo, FALSE);
589 /* double precision values are passed in a pair of registers */
590 add_general (&gr, &stack_size, ainfo, TRUE);
594 g_assert_not_reached ();
598 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
601 /* Emit the signature cookie just before the implicit arguments */
602 add_general (&gr, &stack_size, &cinfo->sig_cookie, FALSE);
607 switch (mono_type_get_underlying_type (sig->ret)->type) {
608 case MONO_TYPE_BOOLEAN:
619 case MONO_TYPE_FNPTR:
620 case MONO_TYPE_CLASS:
621 case MONO_TYPE_OBJECT:
622 case MONO_TYPE_SZARRAY:
623 case MONO_TYPE_ARRAY:
624 case MONO_TYPE_STRING:
625 cinfo->ret.storage = ArgInIReg;
626 cinfo->ret.reg = sparc_i0;
633 cinfo->ret.storage = ArgInIReg;
634 cinfo->ret.reg = sparc_i0;
638 cinfo->ret.storage = ArgInIRegPair;
639 cinfo->ret.reg = sparc_i0;
646 cinfo->ret.storage = ArgInFReg;
647 cinfo->ret.reg = sparc_f0;
649 case MONO_TYPE_GENERICINST:
650 if (!mono_type_generic_inst_is_valuetype (sig->ret)) {
651 cinfo->ret.storage = ArgInIReg;
652 cinfo->ret.reg = sparc_i0;
658 case MONO_TYPE_VALUETYPE:
667 cinfo->ret.storage = ArgOnStack;
669 case MONO_TYPE_TYPEDBYREF:
672 /* Same as a valuetype with size 24 */
679 cinfo->ret.storage = ArgOnStack;
684 g_error ("Can't handle as return value 0x%x", sig->ret->type);
688 cinfo->stack_usage = stack_size;
689 cinfo->reg_usage = gr;
694 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
700 * FIXME: If an argument is allocated to a register, then load it from the
701 * stack in the prolog.
704 for (i = 0; i < cfg->num_varinfo; i++) {
705 MonoInst *ins = cfg->varinfo [i];
706 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
709 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
712 /* FIXME: Make arguments on stack allocateable to registers */
713 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode == OP_REGVAR) || (ins->opcode == OP_ARG))
716 if (mono_is_regsize_var (ins->inst_vtype)) {
717 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
718 g_assert (i == vmv->idx);
720 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
728 mono_arch_get_global_int_regs (MonoCompile *cfg)
732 MonoMethodSignature *sig;
735 sig = mono_method_signature (cfg->method);
737 cinfo = get_call_info (sig, FALSE);
739 /* Use unused input registers */
740 for (i = cinfo->reg_usage; i < 6; ++i)
741 regs = g_list_prepend (regs, GUINT_TO_POINTER (sparc_i0 + i));
743 /* Use %l0..%l6 as global registers */
744 for (i = sparc_l0; i < sparc_l7; ++i)
745 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
753 * mono_arch_regalloc_cost:
755 * Return the cost, in number of memory references, of the action of
756 * allocating the variable VMV into a register during global register
760 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
766 * Set var information according to the calling convention. sparc version.
767 * The locals var stuff should most likely be split in another method.
770 mono_arch_allocate_vars (MonoCompile *m)
772 MonoMethodSignature *sig;
773 MonoMethodHeader *header;
775 int i, offset, size, align, curinst;
778 header = mono_method_get_header (m->method);
780 sig = mono_method_signature (m->method);
782 cinfo = get_call_info (sig, FALSE);
784 if (sig->ret->type != MONO_TYPE_VOID) {
785 switch (cinfo->ret.storage) {
789 m->ret->opcode = OP_REGVAR;
790 m->ret->inst_c0 = cinfo->ret.reg;
794 g_assert_not_reached ();
797 m->ret->opcode = OP_REGOFFSET;
798 m->ret->inst_basereg = sparc_fp;
799 m->ret->inst_offset = 64;
805 m->ret->dreg = m->ret->inst_c0;
809 * We use the ABI calling conventions for managed code as well.
810 * Exception: valuetypes are never returned in registers on V9.
811 * FIXME: Use something more optimized.
814 /* Locals are allocated backwards from %fp */
815 m->frame_reg = sparc_fp;
819 * Reserve a stack slot for holding information used during exception
822 if (header->num_clauses)
823 offset += sizeof (gpointer) * 2;
825 if (m->method->save_lmf) {
826 offset += sizeof (MonoLMF);
827 m->arch.lmf_offset = offset;
830 curinst = m->locals_start;
831 for (i = curinst; i < m->num_varinfo; ++i) {
832 inst = m->varinfo [i];
834 if (inst->opcode == OP_REGVAR) {
835 //g_print ("allocating local %d to %s\n", i, mono_arch_regname (inst->dreg));
839 if (inst->flags & MONO_INST_IS_DEAD)
842 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
843 * pinvoke wrappers when they call functions returning structure */
844 if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
845 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
847 size = mono_type_stack_size (inst->inst_vtype, &align);
850 * This is needed since structures containing doubles must be doubleword
852 * FIXME: Do this only if needed.
854 if (MONO_TYPE_ISSTRUCT (inst->inst_vtype))
858 * variables are accessed as negative offsets from %fp, so increase
859 * the offset before assigning it to a variable
864 offset &= ~(align - 1);
865 inst->opcode = OP_REGOFFSET;
866 inst->inst_basereg = sparc_fp;
867 inst->inst_offset = STACK_BIAS + -offset;
869 //g_print ("allocating local %d to [%s - %d]\n", i, mono_arch_regname (inst->inst_basereg), - inst->inst_offset);
872 if (sig->call_convention == MONO_CALL_VARARG) {
873 m->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
876 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
878 if (inst->opcode != OP_REGVAR) {
879 ArgInfo *ainfo = &cinfo->args [i];
880 gboolean inreg = TRUE;
884 if (sig->hasthis && (i == 0))
885 arg_type = &mono_defaults.object_class->byval_arg;
887 arg_type = sig->params [i - sig->hasthis];
890 if (!arg_type->byref && ((arg_type->type == MONO_TYPE_R4)
891 || (arg_type->type == MONO_TYPE_R8)))
893 * Since float arguments are passed in integer registers, we need to
894 * save them to the stack in the prolog.
899 /* FIXME: Allocate volatile arguments to registers */
900 if (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
903 if (MONO_TYPE_ISSTRUCT (arg_type))
904 /* FIXME: this isn't needed */
907 inst->opcode = OP_REGOFFSET;
910 storage = ArgOnStack;
912 storage = ainfo->storage;
917 inst->opcode = OP_REGVAR;
918 inst->dreg = sparc_i0 + ainfo->reg;
923 * Since float regs are volatile, we save the arguments to
924 * the stack in the prolog.
925 * FIXME: Avoid this if the method contains no calls.
929 case ArgInSplitRegStack:
930 /* Split arguments are saved to the stack in the prolog */
931 inst->opcode = OP_REGOFFSET;
932 /* in parent frame */
933 inst->inst_basereg = sparc_fp;
934 inst->inst_offset = ainfo->offset + ARGS_OFFSET;
936 if (!arg_type->byref && (arg_type->type == MONO_TYPE_R8)) {
938 * It is very hard to load doubles from non-doubleword aligned
939 * memory locations. So if the offset is misaligned, we copy the
940 * argument to a stack location in the prolog.
942 if ((inst->inst_offset - STACK_BIAS) % 8) {
943 inst->inst_basereg = sparc_fp;
947 offset &= ~(align - 1);
948 inst->inst_offset = STACK_BIAS + -offset;
957 if (MONO_TYPE_ISSTRUCT (arg_type)) {
958 /* Add a level of indirection */
960 * It would be easier to add OP_LDIND_I here, but ldind_i instructions
961 * are destructively modified in a lot of places in inssel.brg.
964 MONO_INST_NEW (m, indir, 0);
966 inst->opcode = OP_SPARC_INARG_VT;
967 inst->inst_left = indir;
973 * spillvars are stored between the normal locals and the storage reserved
977 m->stack_offset = offset;
979 /* Add a properly aligned dword for use by int<->float conversion opcodes */
981 mono_spillvar_offset_float (m, 0);
987 make_group (MonoCompile *cfg, MonoInst *left, int basereg, int offset)
991 MONO_INST_NEW (cfg, group, OP_GROUP);
992 group->inst_left = left;
993 group->inst_basereg = basereg;
994 group->inst_imm = offset;
1000 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1003 MonoMethodSignature *tmp_sig;
1007 * mono_ArgIterator_Setup assumes the signature cookie is
1008 * passed first and all the arguments which were before it are
1009 * passed on the stack after the signature. So compensate by
1010 * passing a different signature.
1012 tmp_sig = mono_metadata_signature_dup (call->signature);
1013 tmp_sig->param_count -= call->signature->sentinelpos;
1014 tmp_sig->sentinelpos = 0;
1015 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1017 /* FIXME: Add support for signature tokens to AOT */
1018 cfg->disable_aot = TRUE;
1019 /* We allways pass the signature on the stack for simplicity */
1020 MONO_INST_NEW (cfg, arg, OP_SPARC_OUTARG_MEM);
1021 arg->inst_right = make_group (cfg, (MonoInst*)call, sparc_sp, ARGS_OFFSET + cinfo->sig_cookie.offset);
1022 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
1023 sig_arg->inst_p0 = tmp_sig;
1024 arg->inst_left = sig_arg;
1025 arg->type = STACK_PTR;
1026 /* prepend, so they get reversed */
1027 arg->next = call->out_args;
1028 call->out_args = arg;
1032 * take the arguments and generate the arch-specific
1033 * instructions to properly call the function in call.
1034 * This includes pushing, moving arguments to the right register
1038 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
1040 MonoMethodSignature *sig;
1044 guint32 extra_space = 0;
1046 sig = call->signature;
1047 n = sig->param_count + sig->hasthis;
1049 cinfo = get_call_info (sig, sig->pinvoke);
1051 for (i = 0; i < n; ++i) {
1052 ainfo = cinfo->args + i;
1054 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1055 /* Emit the signature cookie just before the first implicit argument */
1056 emit_sig_cookie (cfg, call, cinfo);
1059 if (is_virtual && i == 0) {
1060 /* the argument will be attached to the call instruction */
1061 in = call->args [i];
1063 MONO_INST_NEW (cfg, arg, OP_OUTARG);
1064 in = call->args [i];
1065 arg->cil_code = in->cil_code;
1066 arg->inst_left = in;
1067 arg->type = in->type;
1068 /* prepend, we'll need to reverse them later */
1069 arg->next = call->out_args;
1070 call->out_args = arg;
1072 if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis]))) {
1075 guint32 offset, pad;
1083 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_TYPEDBYREF) {
1084 size = sizeof (MonoTypedRef);
1085 align = sizeof (gpointer);
1089 size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
1092 * Can't use mono_type_stack_size (), but that
1093 * aligns the size to sizeof (gpointer), which is larger
1094 * than the size of the source, leading to reads of invalid
1095 * memory if the source is at the end of address space or
1098 size = mono_class_value_size (in->klass, &align);
1102 * We use OP_OUTARG_VT to copy the valuetype to a stack location, then
1103 * use the normal OUTARG opcodes to pass the address of the location to
1106 MONO_INST_NEW (cfg, inst, OP_OUTARG_VT);
1107 inst->inst_left = in;
1109 /* The first 6 argument locations are reserved */
1110 if (cinfo->stack_usage < 6 * sizeof (gpointer))
1111 cinfo->stack_usage = 6 * sizeof (gpointer);
1113 offset = ALIGN_TO ((ARGS_OFFSET - STACK_BIAS) + cinfo->stack_usage, align);
1114 pad = offset - ((ARGS_OFFSET - STACK_BIAS) + cinfo->stack_usage);
1116 inst->inst_c1 = STACK_BIAS + offset;
1117 inst->backend.size = size;
1118 arg->inst_left = inst;
1120 cinfo->stack_usage += size;
1121 cinfo->stack_usage += pad;
1124 arg->inst_right = make_group (cfg, (MonoInst*)call, sparc_sp, ARGS_OFFSET + ainfo->offset);
1126 switch (ainfo->storage) {
1130 if (ainfo->storage == ArgInIRegPair)
1131 arg->opcode = OP_SPARC_OUTARG_REGPAIR;
1132 arg->backend.reg3 = sparc_o0 + ainfo->reg;
1133 call->used_iregs |= 1 << ainfo->reg;
1135 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))) {
1136 /* An fp value is passed in an ireg */
1138 if (arg->opcode == OP_SPARC_OUTARG_REGPAIR)
1139 arg->opcode = OP_SPARC_OUTARG_REGPAIR_FLOAT;
1141 arg->opcode = OP_SPARC_OUTARG_FLOAT;
1144 * The OUTARG (freg) implementation needs an extra dword to store
1145 * the temporary value.
1151 arg->opcode = OP_SPARC_OUTARG_MEM;
1153 case ArgOnStackPair:
1154 arg->opcode = OP_SPARC_OUTARG_MEMPAIR;
1156 case ArgInSplitRegStack:
1157 arg->opcode = OP_SPARC_OUTARG_SPLIT_REG_STACK;
1158 arg->backend.reg3 = sparc_o0 + ainfo->reg;
1159 call->used_iregs |= 1 << ainfo->reg;
1162 arg->opcode = OP_SPARC_OUTARG_FLOAT_REG;
1163 arg->backend.reg3 = sparc_f0 + ainfo->reg;
1165 case ArgInDoubleReg:
1166 arg->opcode = OP_SPARC_OUTARG_DOUBLE_REG;
1167 arg->backend.reg3 = sparc_f0 + ainfo->reg;
1175 /* Handle the case where there are no implicit arguments */
1176 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos)) {
1177 emit_sig_cookie (cfg, call, cinfo);
1181 * Reverse the call->out_args list.
1184 MonoInst *prev = NULL, *list = call->out_args, *next;
1191 call->out_args = prev;
1193 call->stack_usage = cinfo->stack_usage + extra_space;
1194 call->out_ireg_args = NULL;
1195 call->out_freg_args = NULL;
1196 cfg->param_area = MAX (cfg->param_area, call->stack_usage);
1197 cfg->flags |= MONO_CFG_HAS_CALLS;
1203 /* Map opcode to the sparc condition codes */
1204 static inline SparcCond
1205 opcode_to_sparc_cond (int opcode)
1227 case OP_COND_EXC_EQ:
1230 case OP_COND_EXC_NE_UN:
1237 case OP_COND_EXC_LT:
1243 case OP_COND_EXC_LT_UN:
1249 case OP_COND_EXC_GT:
1255 case OP_COND_EXC_GT_UN:
1259 case OP_COND_EXC_GE:
1263 case OP_COND_EXC_GE_UN:
1267 case OP_COND_EXC_LE:
1271 case OP_COND_EXC_LE_UN:
1273 case OP_COND_EXC_OV:
1274 case OP_COND_EXC_IOV:
1277 case OP_COND_EXC_IC:
1279 case OP_COND_EXC_NO:
1280 case OP_COND_EXC_NC:
1283 g_assert_not_reached ();
1288 #define COMPUTE_DISP(ins) \
1289 if (ins->flags & MONO_INST_BRLABEL) { \
1290 if (ins->inst_i0->inst_c0) \
1291 disp = (ins->inst_i0->inst_c0 - ((guint8*)code - cfg->native_code)) >> 2; \
1294 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1297 if (ins->inst_true_bb->native_offset) \
1298 disp = (ins->inst_true_bb->native_offset - ((guint8*)code - cfg->native_code)) >> 2; \
1301 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1306 #define DEFAULT_ICC sparc_xcc_short
1308 #define DEFAULT_ICC sparc_icc_short
1312 #define EMIT_COND_BRANCH_ICC(ins,cond,annul,filldelay,icc) \
1316 COMPUTE_DISP(ins); \
1317 predict = (disp != 0) ? 1 : 0; \
1318 g_assert (sparc_is_imm19 (disp)); \
1319 sparc_branchp (code, (annul), cond, icc, (predict), disp); \
1320 if (filldelay) sparc_nop (code); \
1322 #define EMIT_COND_BRANCH(ins,cond,annul,filldelay) EMIT_COND_BRANCH_ICC ((ins), (cond), (annul), (filldelay), (sparc_xcc_short))
1323 #define EMIT_FLOAT_COND_BRANCH(ins,cond,annul,filldelay) \
1327 COMPUTE_DISP(ins); \
1328 predict = (disp != 0) ? 1 : 0; \
1329 g_assert (sparc_is_imm19 (disp)); \
1330 sparc_fbranch (code, (annul), cond, disp); \
1331 if (filldelay) sparc_nop (code); \
1334 #define EMIT_COND_BRANCH_ICC(ins,cond,annul,filldelay,icc) g_assert_not_reached ()
1335 #define EMIT_COND_BRANCH_GENERAL(ins,bop,cond,annul,filldelay) \
1338 COMPUTE_DISP(ins); \
1339 g_assert (sparc_is_imm22 (disp)); \
1340 sparc_ ## bop (code, (annul), cond, disp); \
1341 if (filldelay) sparc_nop (code); \
1343 #define EMIT_COND_BRANCH(ins,cond,annul,filldelay) EMIT_COND_BRANCH_GENERAL((ins),branch,(cond),annul,filldelay)
1344 #define EMIT_FLOAT_COND_BRANCH(ins,cond,annul,filldelay) EMIT_COND_BRANCH_GENERAL((ins),fbranch,(cond),annul,filldelay)
1347 #define EMIT_COND_BRANCH_PREDICTED(ins,cond,annul,filldelay) \
1351 COMPUTE_DISP(ins); \
1352 predict = (disp != 0) ? 1 : 0; \
1353 g_assert (sparc_is_imm19 (disp)); \
1354 sparc_branchp (code, (annul), (cond), DEFAULT_ICC, (predict), disp); \
1355 if (filldelay) sparc_nop (code); \
1358 #define EMIT_COND_BRANCH_BPR(ins,bop,predict,annul,filldelay) \
1361 COMPUTE_DISP(ins); \
1362 g_assert (sparc_is_imm22 (disp)); \
1363 sparc_ ## bop (code, (annul), (predict), ins->sreg1, disp); \
1364 if (filldelay) sparc_nop (code); \
1367 /* emit an exception if condition is fail */
1369 * We put the exception throwing code out-of-line, at the end of the method
1371 #define EMIT_COND_SYSTEM_EXCEPTION_GENERAL(ins,cond,sexc_name,filldelay,icc) do { \
1372 mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code, \
1373 MONO_PATCH_INFO_EXC, sexc_name); \
1375 sparc_branchp (code, 0, (cond), (icc), 0, 0); \
1378 sparc_branch (code, 0, cond, 0); \
1380 if (filldelay) sparc_nop (code); \
1383 #define EMIT_COND_SYSTEM_EXCEPTION(ins,cond,sexc_name) EMIT_COND_SYSTEM_EXCEPTION_GENERAL(ins,cond,sexc_name,TRUE,DEFAULT_ICC)
1385 #define EMIT_COND_SYSTEM_EXCEPTION_BPR(ins,bop,sexc_name) do { \
1386 mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code, \
1387 MONO_PATCH_INFO_EXC, sexc_name); \
1388 sparc_ ## bop (code, FALSE, FALSE, ins->sreg1, 0); \
1392 #define EMIT_ALU_IMM(ins,op,setcc) do { \
1393 if (sparc_is_imm13 ((ins)->inst_imm)) \
1394 sparc_ ## op ## _imm (code, (setcc), (ins)->sreg1, ins->inst_imm, (ins)->dreg); \
1396 sparc_set (code, ins->inst_imm, sparc_o7); \
1397 sparc_ ## op (code, (setcc), (ins)->sreg1, sparc_o7, (ins)->dreg); \
1401 #define EMIT_LOAD_MEMBASE(ins,op) do { \
1402 if (sparc_is_imm13 (ins->inst_offset)) \
1403 sparc_ ## op ## _imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg); \
1405 sparc_set (code, ins->inst_offset, sparc_o7); \
1406 sparc_ ## op (code, ins->inst_basereg, sparc_o7, ins->dreg); \
1411 #define EMIT_STORE_MEMBASE_IMM(ins,op) do { \
1413 if (ins->inst_imm == 0) \
1416 sparc_set (code, ins->inst_imm, sparc_o7); \
1419 if (!sparc_is_imm13 (ins->inst_offset)) { \
1420 sparc_set (code, ins->inst_offset, GP_SCRATCH_REG); \
1421 sparc_ ## op (code, sreg, ins->inst_destbasereg, GP_SCRATCH_REG); \
1424 sparc_ ## op ## _imm (code, sreg, ins->inst_destbasereg, ins->inst_offset); \
1427 #define EMIT_STORE_MEMBASE_REG(ins,op) do { \
1428 if (!sparc_is_imm13 (ins->inst_offset)) { \
1429 sparc_set (code, ins->inst_offset, sparc_o7); \
1430 sparc_ ## op (code, ins->sreg1, ins->inst_destbasereg, sparc_o7); \
1433 sparc_ ## op ## _imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset); \
1436 #define EMIT_CALL() do { \
1438 sparc_set_template (code, sparc_o7); \
1439 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_o7); \
1442 sparc_call_simple (code, 0); \
1448 * A call template is 7 instructions long, so we want to avoid it if possible.
1451 emit_call (MonoCompile *cfg, guint32 *code, guint32 patch_type, gconstpointer data)
1455 /* FIXME: This only works if the target method is already compiled */
1456 if (0 && v64 && !cfg->compile_aot) {
1457 MonoJumpInfo patch_info;
1459 patch_info.type = patch_type;
1460 patch_info.data.target = data;
1462 target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, &patch_info, FALSE);
1464 /* FIXME: Add optimizations if the target is close enough */
1465 sparc_set (code, target, sparc_o7);
1466 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_o7);
1470 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, patch_type, data);
1478 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1480 MonoInst *ins, *last_ins = NULL;
1485 switch (ins->opcode) {
1487 /* remove unnecessary multiplication with 1 */
1488 if (ins->inst_imm == 1) {
1489 if (ins->dreg != ins->sreg1) {
1490 ins->opcode = OP_MOVE;
1492 last_ins->next = ins->next;
1499 case OP_LOAD_MEMBASE:
1500 case OP_LOADI4_MEMBASE:
1502 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1503 * OP_LOAD_MEMBASE offset(basereg), reg
1505 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1506 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1507 ins->inst_basereg == last_ins->inst_destbasereg &&
1508 ins->inst_offset == last_ins->inst_offset) {
1509 if (ins->dreg == last_ins->sreg1) {
1510 last_ins->next = ins->next;
1514 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1515 ins->opcode = OP_MOVE;
1516 ins->sreg1 = last_ins->sreg1;
1520 * Note: reg1 must be different from the basereg in the second load
1521 * OP_LOAD_MEMBASE offset(basereg), reg1
1522 * OP_LOAD_MEMBASE offset(basereg), reg2
1524 * OP_LOAD_MEMBASE offset(basereg), reg1
1525 * OP_MOVE reg1, reg2
1527 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1528 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1529 ins->inst_basereg != last_ins->dreg &&
1530 ins->inst_basereg == last_ins->inst_basereg &&
1531 ins->inst_offset == last_ins->inst_offset) {
1533 if (ins->dreg == last_ins->dreg) {
1534 last_ins->next = ins->next;
1538 ins->opcode = OP_MOVE;
1539 ins->sreg1 = last_ins->dreg;
1542 //g_assert_not_reached ();
1546 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1547 * OP_LOAD_MEMBASE offset(basereg), reg
1549 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1550 * OP_ICONST reg, imm
1552 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1553 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1554 ins->inst_basereg == last_ins->inst_destbasereg &&
1555 ins->inst_offset == last_ins->inst_offset) {
1556 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1557 ins->opcode = OP_ICONST;
1558 ins->inst_c0 = last_ins->inst_imm;
1559 g_assert_not_reached (); // check this rule
1564 case OP_LOADI1_MEMBASE:
1565 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1566 ins->inst_basereg == last_ins->inst_destbasereg &&
1567 ins->inst_offset == last_ins->inst_offset) {
1568 if (ins->dreg == last_ins->sreg1) {
1569 last_ins->next = ins->next;
1573 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1574 ins->opcode = OP_MOVE;
1575 ins->sreg1 = last_ins->sreg1;
1579 case OP_LOADI2_MEMBASE:
1580 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1581 ins->inst_basereg == last_ins->inst_destbasereg &&
1582 ins->inst_offset == last_ins->inst_offset) {
1583 if (ins->dreg == last_ins->sreg1) {
1584 last_ins->next = ins->next;
1588 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1589 ins->opcode = OP_MOVE;
1590 ins->sreg1 = last_ins->sreg1;
1594 case OP_STOREI4_MEMBASE_IMM:
1595 /* Convert pairs of 0 stores to a dword 0 store */
1596 /* Used when initializing temporaries */
1597 /* We know sparc_fp is dword aligned */
1598 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM) &&
1599 (ins->inst_destbasereg == last_ins->inst_destbasereg) &&
1600 (ins->inst_destbasereg == sparc_fp) &&
1601 (ins->inst_offset < 0) &&
1602 ((ins->inst_offset % 8) == 0) &&
1603 ((ins->inst_offset == last_ins->inst_offset - 4)) &&
1604 (ins->inst_imm == 0) &&
1605 (last_ins->inst_imm == 0)) {
1607 last_ins->opcode = OP_STOREI8_MEMBASE_IMM;
1608 last_ins->inst_offset = ins->inst_offset;
1609 last_ins->next = ins->next;
1621 case OP_COND_EXC_EQ:
1622 case OP_COND_EXC_GE:
1623 case OP_COND_EXC_GT:
1624 case OP_COND_EXC_LE:
1625 case OP_COND_EXC_LT:
1626 case OP_COND_EXC_NE_UN:
1628 * Convert compare with zero+branch to BRcc
1631 * This only works in 64 bit mode, since it examines all 64
1632 * bits of the register.
1633 * Only do this if the method is small since BPr only has a 16bit
1636 if (v64 && (mono_method_get_header (cfg->method)->code_size < 10000) && last_ins &&
1637 (last_ins->opcode == OP_COMPARE_IMM) &&
1638 (last_ins->inst_imm == 0)) {
1639 MonoInst *next = ins->next;
1640 switch (ins->opcode) {
1642 ins->opcode = OP_SPARC_BRZ;
1645 ins->opcode = OP_SPARC_BRNZ;
1648 ins->opcode = OP_SPARC_BRLZ;
1651 ins->opcode = OP_SPARC_BRGZ;
1654 ins->opcode = OP_SPARC_BRGEZ;
1657 ins->opcode = OP_SPARC_BRLEZ;
1659 case OP_COND_EXC_EQ:
1660 ins->opcode = OP_SPARC_COND_EXC_EQZ;
1662 case OP_COND_EXC_GE:
1663 ins->opcode = OP_SPARC_COND_EXC_GEZ;
1665 case OP_COND_EXC_GT:
1666 ins->opcode = OP_SPARC_COND_EXC_GTZ;
1668 case OP_COND_EXC_LE:
1669 ins->opcode = OP_SPARC_COND_EXC_LEZ;
1671 case OP_COND_EXC_LT:
1672 ins->opcode = OP_SPARC_COND_EXC_LTZ;
1674 case OP_COND_EXC_NE_UN:
1675 ins->opcode = OP_SPARC_COND_EXC_NEZ;
1678 g_assert_not_reached ();
1680 ins->sreg1 = last_ins->sreg1;
1682 last_ins->next = next;
1693 if (ins->dreg == ins->sreg1) {
1695 last_ins->next = ins->next;
1700 * OP_MOVE sreg, dreg
1701 * OP_MOVE dreg, sreg
1703 if (last_ins && last_ins->opcode == OP_MOVE &&
1704 ins->sreg1 == last_ins->dreg &&
1705 ins->dreg == last_ins->sreg1) {
1706 last_ins->next = ins->next;
1715 bb->last_ins = last_ins;
1719 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
1721 MonoSpillInfo **si, *info;
1723 g_assert (spillvar == 0);
1725 si = &cfg->spill_info_float;
1728 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1729 cfg->stack_offset += sizeof (double);
1730 cfg->stack_offset = ALIGN_TO (cfg->stack_offset, 8);
1731 info->offset = - cfg->stack_offset;
1734 return MONO_SPARC_STACK_BIAS + (*si)->offset;
1737 /* FIXME: Strange loads from the stack in basic-float.cs:test_2_rem */
1740 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1742 mono_local_regalloc (cfg, bb);
1746 sparc_patch (guint32 *code, const gpointer target)
1749 guint32 ins = *code;
1750 guint32 op = ins >> 30;
1751 guint32 op2 = (ins >> 22) & 0x7;
1752 guint32 rd = (ins >> 25) & 0x1f;
1753 guint8* target8 = (guint8*)target;
1754 gint64 disp = (target8 - (guint8*)code) >> 2;
1757 // g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
1759 if ((op == 0) && (op2 == 2)) {
1760 if (!sparc_is_imm22 (disp))
1763 *code = ((ins >> 22) << 22) | (disp & 0x3fffff);
1765 else if ((op == 0) && (op2 == 1)) {
1766 if (!sparc_is_imm19 (disp))
1769 *code = ((ins >> 19) << 19) | (disp & 0x7ffff);
1771 else if ((op == 0) && (op2 == 3)) {
1772 if (!sparc_is_imm16 (disp))
1775 *code &= ~(0x180000 | 0x3fff);
1776 *code |= ((disp << 21) & (0x180000)) | (disp & 0x3fff);
1778 else if ((op == 0) && (op2 == 6)) {
1779 if (!sparc_is_imm22 (disp))
1782 *code = ((ins >> 22) << 22) | (disp & 0x3fffff);
1784 else if ((op == 0) && (op2 == 4)) {
1785 guint32 ins2 = code [1];
1787 if (((ins2 >> 30) == 2) && (((ins2 >> 19) & 0x3f) == 2)) {
1788 /* sethi followed by or */
1790 sparc_set (p, target8, rd);
1791 while (p <= (code + 1))
1794 else if (ins2 == 0x01000000) {
1795 /* sethi followed by nop */
1797 sparc_set (p, target8, rd);
1798 while (p <= (code + 1))
1801 else if ((sparc_inst_op (ins2) == 3) && (sparc_inst_imm (ins2))) {
1802 /* sethi followed by load/store */
1804 guint32 t = (guint32)target8;
1805 *code &= ~(0x3fffff);
1807 *(code + 1) &= ~(0x3ff);
1808 *(code + 1) |= (t & 0x3ff);
1812 (sparc_inst_rd (ins) == sparc_g1) &&
1813 (sparc_inst_op (c [1]) == 0) && (sparc_inst_op2 (c [1]) == 4) &&
1814 (sparc_inst_op (c [2]) == 2) && (sparc_inst_op3 (c [2]) == 2) &&
1815 (sparc_inst_op (c [3]) == 2) && (sparc_inst_op3 (c [3]) == 2))
1819 reg = sparc_inst_rd (c [1]);
1820 sparc_set (p, target8, reg);
1824 else if ((sparc_inst_op (ins2) == 2) && (sparc_inst_op3 (ins2) == 0x38) &&
1825 (sparc_inst_imm (ins2))) {
1826 /* sethi followed by jmpl */
1828 guint32 t = (guint32)target8;
1829 *code &= ~(0x3fffff);
1831 *(code + 1) &= ~(0x3ff);
1832 *(code + 1) |= (t & 0x3ff);
1838 else if (op == 01) {
1839 gint64 disp = (target8 - (guint8*)code) >> 2;
1841 if (!sparc_is_imm30 (disp))
1843 sparc_call_simple (code, target8 - (guint8*)code);
1845 else if ((op == 2) && (sparc_inst_op3 (ins) == 0x2) && sparc_inst_imm (ins)) {
1847 g_assert (sparc_is_imm13 (target8));
1849 *code |= (guint32)target8;
1851 else if ((sparc_inst_op (ins) == 2) && (sparc_inst_op3 (ins) == 0x7)) {
1852 /* sparc_set case 5. */
1856 reg = sparc_inst_rd (c [3]);
1857 sparc_set (p, target, reg);
1864 // g_print ("patched with 0x%08x\n", ins);
1868 * mono_sparc_emit_save_lmf:
1870 * Emit the code neccesary to push a new entry onto the lmf stack. Used by
1871 * trampolines as well.
1874 mono_sparc_emit_save_lmf (guint32 *code, guint32 lmf_offset)
1877 sparc_sti_imm (code, sparc_o0, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr));
1878 /* Save previous_lmf */
1879 sparc_ldi (code, sparc_o0, sparc_g0, sparc_o7);
1880 sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf));
1882 sparc_add_imm (code, FALSE, sparc_fp, lmf_offset, sparc_o7);
1883 sparc_sti (code, sparc_o7, sparc_o0, sparc_g0);
1889 mono_sparc_emit_restore_lmf (guint32 *code, guint32 lmf_offset)
1891 /* Load previous_lmf */
1892 sparc_ldi_imm (code, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sparc_l0);
1894 sparc_ldi_imm (code, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sparc_l1);
1895 /* *(lmf) = previous_lmf */
1896 sparc_sti (code, sparc_l0, sparc_l1, sparc_g0);
1901 emit_save_sp_to_lmf (MonoCompile *cfg, guint32 *code)
1904 * Since register windows are saved to the current value of %sp, we need to
1905 * set the sp field in the lmf before the call, not in the prolog.
1907 if (cfg->method->save_lmf) {
1908 gint32 lmf_offset = MONO_SPARC_STACK_BIAS - cfg->arch.lmf_offset;
1911 sparc_sti_imm (code, sparc_sp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, sp));
1918 emit_vret_token (MonoInst *ins, guint32 *code)
1920 MonoCallInst *call = (MonoCallInst*)ins;
1924 * The sparc ABI requires that calls to functions which return a structure
1925 * contain an additional unimpl instruction which is checked by the callee.
1927 if (call->signature->pinvoke && MONO_TYPE_ISSTRUCT(call->signature->ret)) {
1928 if (call->signature->ret->type == MONO_TYPE_TYPEDBYREF)
1929 size = mono_type_stack_size (call->signature->ret, NULL);
1931 size = mono_class_native_size (call->signature->ret->data.klass, NULL);
1932 sparc_unimp (code, size & 0xfff);
1939 emit_move_return_value (MonoInst *ins, guint32 *code)
1941 /* Move return value to the target register */
1942 /* FIXME: do more things in the local reg allocator */
1943 switch (ins->opcode) {
1945 case OP_VOIDCALL_REG:
1946 case OP_VOIDCALL_MEMBASE:
1950 case OP_CALL_MEMBASE:
1951 g_assert (ins->dreg == sparc_o0);
1955 case OP_LCALL_MEMBASE:
1957 * ins->dreg is the least significant reg due to the lreg: LCALL rule
1958 * in inssel-long32.brg.
1961 sparc_mov_reg_reg (code, sparc_o0, ins->dreg);
1963 g_assert (ins->dreg == sparc_o1);
1968 case OP_FCALL_MEMBASE:
1970 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4) {
1971 sparc_fmovs (code, sparc_f0, ins->dreg);
1972 sparc_fstod (code, ins->dreg, ins->dreg);
1975 sparc_fmovd (code, sparc_f0, ins->dreg);
1977 sparc_fmovs (code, sparc_f0, ins->dreg);
1978 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4)
1979 sparc_fstod (code, ins->dreg, ins->dreg);
1981 sparc_fmovs (code, sparc_f1, ins->dreg + 1);
1986 case OP_VCALL_MEMBASE:
1996 * emit_load_volatile_arguments:
1998 * Load volatile arguments from the stack to the original input registers.
1999 * Required before a tail call.
2002 emit_load_volatile_arguments (MonoCompile *cfg, guint32 *code)
2004 MonoMethod *method = cfg->method;
2005 MonoMethodSignature *sig;
2010 /* FIXME: Generate intermediate code instead */
2012 sig = mono_method_signature (method);
2014 cinfo = get_call_info (sig, FALSE);
2016 /* This is the opposite of the code in emit_prolog */
2018 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2019 ArgInfo *ainfo = cinfo->args + i;
2020 gint32 stack_offset;
2022 inst = cfg->args [i];
2024 if (sig->hasthis && (i == 0))
2025 arg_type = &mono_defaults.object_class->byval_arg;
2027 arg_type = sig->params [i - sig->hasthis];
2029 stack_offset = ainfo->offset + ARGS_OFFSET;
2030 ireg = sparc_i0 + ainfo->reg;
2032 if (ainfo->storage == ArgInSplitRegStack) {
2033 g_assert (inst->opcode == OP_REGOFFSET);
2035 if (!sparc_is_imm13 (stack_offset))
2037 sparc_st_imm (code, inst->inst_basereg, stack_offset, sparc_i5);
2040 if (!v64 && !arg_type->byref && (arg_type->type == MONO_TYPE_R8)) {
2041 if (ainfo->storage == ArgInIRegPair) {
2042 if (!sparc_is_imm13 (inst->inst_offset + 4))
2044 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, ireg);
2045 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset + 4, ireg + 1);
2048 if (ainfo->storage == ArgInSplitRegStack) {
2049 if (stack_offset != inst->inst_offset) {
2050 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, sparc_i5);
2051 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset + 4, sparc_o7);
2052 sparc_st_imm (code, sparc_o7, sparc_fp, stack_offset + 4);
2057 if (ainfo->storage == ArgOnStackPair) {
2058 if (stack_offset != inst->inst_offset) {
2059 /* stack_offset is not dword aligned, so we need to make a copy */
2060 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, sparc_o7);
2061 sparc_st_imm (code, sparc_o7, sparc_fp, stack_offset);
2063 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset + 4, sparc_o7);
2064 sparc_st_imm (code, sparc_o7, sparc_fp, stack_offset + 4);
2069 g_assert_not_reached ();
2072 if ((ainfo->storage == ArgInIReg) && (inst->opcode != OP_REGVAR)) {
2073 /* Argument in register, but need to be saved to stack */
2074 if (!sparc_is_imm13 (stack_offset))
2076 if ((stack_offset - ARGS_OFFSET) & 0x1)
2077 /* FIXME: Is this ldsb or ldub ? */
2078 sparc_ldsb_imm (code, inst->inst_basereg, stack_offset, ireg);
2080 if ((stack_offset - ARGS_OFFSET) & 0x2)
2081 sparc_ldsh_imm (code, inst->inst_basereg, stack_offset, ireg);
2083 if ((stack_offset - ARGS_OFFSET) & 0x4)
2084 sparc_ld_imm (code, inst->inst_basereg, stack_offset, ireg);
2087 sparc_ldx_imm (code, inst->inst_basereg, stack_offset, ireg);
2089 sparc_ld_imm (code, inst->inst_basereg, stack_offset, ireg);
2092 else if ((ainfo->storage == ArgInIRegPair) && (inst->opcode != OP_REGVAR)) {
2093 /* Argument in regpair, but need to be saved to stack */
2094 if (!sparc_is_imm13 (inst->inst_offset + 4))
2096 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, ireg);
2097 sparc_st_imm (code, inst->inst_basereg, inst->inst_offset + 4, ireg + 1);
2099 else if ((ainfo->storage == ArgInFloatReg) && (inst->opcode != OP_REGVAR)) {
2102 else if ((ainfo->storage == ArgInDoubleReg) && (inst->opcode != OP_REGVAR)) {
2106 if ((ainfo->storage == ArgInSplitRegStack) || (ainfo->storage == ArgOnStack))
2107 if (inst->opcode == OP_REGVAR)
2108 /* FIXME: Load the argument into memory */
2118 * mono_sparc_is_virtual_call:
2120 * Determine whenever the instruction at CODE is a virtual call.
2123 mono_sparc_is_virtual_call (guint32 *code)
2130 if ((sparc_inst_op (*code) == 0x2) && (sparc_inst_op3 (*code) == 0x38)) {
2132 * Register indirect call. If it is a virtual call, then the
2133 * instruction in the delay slot is a special kind of nop.
2136 /* Construct special nop */
2137 sparc_or_imm (p, FALSE, sparc_g0, 0xca, sparc_g0);
2140 if (code [1] == p [0])
2148 * mono_arch_get_vcall_slot_addr:
2150 * Determine the vtable slot used by a virtual call.
2153 mono_arch_get_vcall_slot_addr (guint8 *code8, gpointer *regs)
2155 guint32 *code = (guint32*)(gpointer)code8;
2156 guint32 ins = code [0];
2157 guint32 prev_ins = code [-1];
2159 mono_sparc_flushw ();
2161 if (!mono_sparc_is_virtual_call (code))
2164 if ((sparc_inst_op (ins) == 0x2) && (sparc_inst_op3 (ins) == 0x38)) {
2165 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)) {
2166 /* ld [r1 + CONST ], r2; call r2 */
2167 guint32 base = sparc_inst_rs1 (prev_ins);
2168 guint32 disp = sparc_inst_imm13 (prev_ins);
2171 g_assert (sparc_inst_rd (prev_ins) == sparc_inst_rs1 (ins));
2173 g_assert ((base >= sparc_o0) && (base <= sparc_i7));
2175 base_val = regs [base - sparc_o0];
2177 return (gpointer)((guint8*)base_val + disp);
2179 else if ((sparc_inst_op (prev_ins) == 0x3) && (sparc_inst_i (prev_ins) == 0) && (sparc_inst_op3 (prev_ins) == 0)) {
2180 /* set r1, ICONST; ld [r1 + r2], r2; call r2 */
2181 /* Decode a sparc_set32 */
2182 guint32 base = sparc_inst_rs1 (prev_ins);
2185 guint32 s1 = code [-3];
2186 guint32 s2 = code [-2];
2193 g_assert (sparc_inst_op (s1) == 0);
2194 g_assert (sparc_inst_op2 (s1) == 4);
2197 g_assert (sparc_inst_op (s2) == 2);
2198 g_assert (sparc_inst_op3 (s2) == 2);
2199 g_assert (sparc_inst_i (s2) == 1);
2200 g_assert (sparc_inst_rs1 (s2) == sparc_inst_rd (s2));
2201 g_assert (sparc_inst_rd (s1) == sparc_inst_rs1 (s2));
2203 disp = ((s1 & 0x3fffff) << 10) | sparc_inst_imm13 (s2);
2205 g_assert ((base >= sparc_o0) && (base <= sparc_i7));
2207 base_val = regs [base - sparc_o0];
2209 return (gpointer)((guint8*)base_val + disp);
2211 g_assert_not_reached ();
2214 g_assert_not_reached ();
2220 * Some conventions used in the following code.
2221 * 2) The only scratch registers we have are o7 and g1. We try to
2222 * stick to o7 when we can, and use g1 when necessary.
2226 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2231 guint32 *code = (guint32*)(cfg->native_code + cfg->code_len);
2232 MonoInst *last_ins = NULL;
2236 if (cfg->opt & MONO_OPT_PEEPHOLE)
2237 peephole_pass (cfg, bb);
2239 if (cfg->verbose_level > 2)
2240 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2242 cpos = bb->max_offset;
2244 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2252 offset = (guint8*)code - cfg->native_code;
2254 spec = ins_get_spec (ins->opcode);
2255 /* I kept this, but this looks a workaround for a bug */
2256 if (spec == MONO_ARCH_CPU_SPEC)
2257 spec = ins_get_spec (CEE_ADD);
2259 max_len = ((guint8 *)spec)[MONO_INST_LEN];
2261 if (offset > (cfg->code_size - max_len - 16)) {
2262 cfg->code_size *= 2;
2263 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2264 code = (guint32*)(cfg->native_code + offset);
2266 code_start = (guint8*)code;
2267 // if (ins->cil_code)
2268 // g_print ("cil code\n");
2269 mono_debug_record_line_number (cfg, ins, offset);
2271 switch (ins->opcode) {
2272 case OP_STOREI1_MEMBASE_IMM:
2273 EMIT_STORE_MEMBASE_IMM (ins, stb);
2275 case OP_STOREI2_MEMBASE_IMM:
2276 EMIT_STORE_MEMBASE_IMM (ins, sth);
2278 case OP_STORE_MEMBASE_IMM:
2279 EMIT_STORE_MEMBASE_IMM (ins, sti);
2281 case OP_STOREI4_MEMBASE_IMM:
2282 EMIT_STORE_MEMBASE_IMM (ins, st);
2284 case OP_STOREI8_MEMBASE_IMM:
2286 EMIT_STORE_MEMBASE_IMM (ins, stx);
2288 /* Only generated by peephole opts */
2289 g_assert ((ins->inst_offset % 8) == 0);
2290 g_assert (ins->inst_imm == 0);
2291 EMIT_STORE_MEMBASE_IMM (ins, stx);
2294 case OP_STOREI1_MEMBASE_REG:
2295 EMIT_STORE_MEMBASE_REG (ins, stb);
2297 case OP_STOREI2_MEMBASE_REG:
2298 EMIT_STORE_MEMBASE_REG (ins, sth);
2300 case OP_STOREI4_MEMBASE_REG:
2301 EMIT_STORE_MEMBASE_REG (ins, st);
2303 case OP_STOREI8_MEMBASE_REG:
2305 EMIT_STORE_MEMBASE_REG (ins, stx);
2307 /* Only used by OP_MEMSET */
2308 EMIT_STORE_MEMBASE_REG (ins, std);
2311 case OP_STORE_MEMBASE_REG:
2312 EMIT_STORE_MEMBASE_REG (ins, sti);
2316 sparc_ldx (code, ins->inst_c0, sparc_g0, ins->dreg);
2318 sparc_ld (code, ins->inst_c0, sparc_g0, ins->dreg);
2323 sparc_ldsw (code, ins->inst_c0, sparc_g0, ins->dreg);
2325 sparc_ld (code, ins->inst_c0, sparc_g0, ins->dreg);
2329 sparc_ld (code, ins->inst_c0, sparc_g0, ins->dreg);
2332 sparc_set (code, ins->inst_c0, ins->dreg);
2333 sparc_ld (code, ins->dreg, sparc_g0, ins->dreg);
2335 case OP_LOADI4_MEMBASE:
2337 EMIT_LOAD_MEMBASE (ins, ldsw);
2339 EMIT_LOAD_MEMBASE (ins, ld);
2342 case OP_LOADU4_MEMBASE:
2343 EMIT_LOAD_MEMBASE (ins, ld);
2345 case OP_LOADU1_MEMBASE:
2346 EMIT_LOAD_MEMBASE (ins, ldub);
2348 case OP_LOADI1_MEMBASE:
2349 EMIT_LOAD_MEMBASE (ins, ldsb);
2351 case OP_LOADU2_MEMBASE:
2352 EMIT_LOAD_MEMBASE (ins, lduh);
2354 case OP_LOADI2_MEMBASE:
2355 EMIT_LOAD_MEMBASE (ins, ldsh);
2357 case OP_LOAD_MEMBASE:
2359 EMIT_LOAD_MEMBASE (ins, ldx);
2361 EMIT_LOAD_MEMBASE (ins, ld);
2365 case OP_LOADI8_MEMBASE:
2366 EMIT_LOAD_MEMBASE (ins, ldx);
2370 sparc_sll_imm (code, ins->sreg1, 24, sparc_o7);
2371 sparc_sra_imm (code, sparc_o7, 24, ins->dreg);
2374 sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
2375 sparc_sra_imm (code, sparc_o7, 16, ins->dreg);
2378 sparc_and_imm (code, FALSE, ins->sreg1, 0xff, ins->dreg);
2381 sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
2382 sparc_srl_imm (code, sparc_o7, 16, ins->dreg);
2384 case CEE_CONV_OVF_U4:
2385 /* Only used on V9 */
2386 sparc_cmp_imm (code, ins->sreg1, 0);
2387 mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code,
2388 MONO_PATCH_INFO_EXC, "OverflowException");
2389 sparc_branchp (code, 0, sparc_bl, sparc_xcc_short, 0, 0);
2391 sparc_set (code, 1, sparc_o7);
2392 sparc_sllx_imm (code, sparc_o7, 32, sparc_o7);
2393 sparc_cmp (code, ins->sreg1, sparc_o7);
2394 mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code,
2395 MONO_PATCH_INFO_EXC, "OverflowException");
2396 sparc_branchp (code, 0, sparc_bge, sparc_xcc_short, 0, 0);
2398 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
2400 case CEE_CONV_OVF_I4_UN:
2401 /* Only used on V9 */
2406 /* Only used on V9 */
2407 sparc_srl_imm (code, ins->sreg1, 0, ins->dreg);
2411 /* Only used on V9 */
2412 sparc_sra_imm (code, ins->sreg1, 0, ins->dreg);
2417 sparc_cmp (code, ins->sreg1, ins->sreg2);
2419 case OP_COMPARE_IMM:
2420 case OP_ICOMPARE_IMM:
2421 if (sparc_is_imm13 (ins->inst_imm))
2422 sparc_cmp_imm (code, ins->sreg1, ins->inst_imm);
2424 sparc_set (code, ins->inst_imm, sparc_o7);
2425 sparc_cmp (code, ins->sreg1, sparc_o7);
2430 * gdb does not like encountering 'ta 1' in the debugged code. So
2431 * instead of emitting a trap, we emit a call a C function and place a
2434 //sparc_ta (code, 1);
2435 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, mono_arch_break);
2440 sparc_add (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2444 sparc_add (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2449 /* according to inssel-long32.brg, this should set cc */
2450 EMIT_ALU_IMM (ins, add, TRUE);
2454 /* according to inssel-long32.brg, this should set cc */
2455 sparc_addx (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2459 EMIT_ALU_IMM (ins, addx, TRUE);
2463 sparc_sub (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2467 sparc_sub (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2472 /* according to inssel-long32.brg, this should set cc */
2473 EMIT_ALU_IMM (ins, sub, TRUE);
2477 /* according to inssel-long32.brg, this should set cc */
2478 sparc_subx (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2482 EMIT_ALU_IMM (ins, subx, TRUE);
2486 sparc_and (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2490 EMIT_ALU_IMM (ins, and, FALSE);
2494 /* Sign extend sreg1 into %y */
2495 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2496 sparc_wry (code, sparc_o7, sparc_g0);
2497 sparc_sdiv (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2498 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2502 sparc_wry (code, sparc_g0, sparc_g0);
2503 sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2508 /* Transform division into a shift */
2509 for (i = 1; i < 30; ++i) {
2511 if (ins->inst_imm == imm)
2517 sparc_srl_imm (code, ins->sreg1, 31, sparc_o7);
2518 sparc_add (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2519 sparc_sra_imm (code, ins->dreg, 1, ins->dreg);
2522 /* http://compilers.iecc.com/comparch/article/93-04-079 */
2523 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2524 sparc_srl_imm (code, sparc_o7, 32 - i, sparc_o7);
2525 sparc_add (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2526 sparc_sra_imm (code, ins->dreg, i, ins->dreg);
2530 /* Sign extend sreg1 into %y */
2531 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2532 sparc_wry (code, sparc_o7, sparc_g0);
2533 EMIT_ALU_IMM (ins, sdiv, TRUE);
2534 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2540 /* Sign extend sreg1 into %y */
2541 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2542 sparc_wry (code, sparc_o7, sparc_g0);
2543 sparc_sdiv (code, TRUE, ins->sreg1, ins->sreg2, sparc_o7);
2544 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2545 sparc_smul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
2546 sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2550 sparc_wry (code, sparc_g0, sparc_g0);
2551 sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, sparc_o7);
2552 sparc_umul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
2553 sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2557 /* Sign extend sreg1 into %y */
2558 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2559 sparc_wry (code, sparc_o7, sparc_g0);
2560 if (!sparc_is_imm13 (ins->inst_imm)) {
2561 sparc_set (code, ins->inst_imm, GP_SCRATCH_REG);
2562 sparc_sdiv (code, TRUE, ins->sreg1, GP_SCRATCH_REG, sparc_o7);
2563 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2564 sparc_smul (code, FALSE, sparc_o7, GP_SCRATCH_REG, sparc_o7);
2567 sparc_sdiv_imm (code, TRUE, ins->sreg1, ins->inst_imm, sparc_o7);
2568 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2569 sparc_smul_imm (code, FALSE, sparc_o7, ins->inst_imm, sparc_o7);
2571 sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2575 sparc_or (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2579 EMIT_ALU_IMM (ins, or, FALSE);
2583 sparc_xor (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2587 EMIT_ALU_IMM (ins, xor, FALSE);
2591 sparc_sll (code, ins->sreg1, ins->sreg2, ins->dreg);
2595 if (ins->inst_imm < (1 << 5))
2596 sparc_sll_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2598 sparc_set (code, ins->inst_imm, sparc_o7);
2599 sparc_sll (code, ins->sreg1, sparc_o7, ins->dreg);
2604 sparc_sra (code, ins->sreg1, ins->sreg2, ins->dreg);
2608 if (ins->inst_imm < (1 << 5))
2609 sparc_sra_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2611 sparc_set (code, ins->inst_imm, sparc_o7);
2612 sparc_sra (code, ins->sreg1, sparc_o7, ins->dreg);
2616 case OP_ISHR_UN_IMM:
2617 if (ins->inst_imm < (1 << 5))
2618 sparc_srl_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2620 sparc_set (code, ins->inst_imm, sparc_o7);
2621 sparc_srl (code, ins->sreg1, sparc_o7, ins->dreg);
2626 sparc_srl (code, ins->sreg1, ins->sreg2, ins->dreg);
2629 sparc_sllx (code, ins->sreg1, ins->sreg2, ins->dreg);
2632 if (ins->inst_imm < (1 << 6))
2633 sparc_sllx_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2635 sparc_set (code, ins->inst_imm, sparc_o7);
2636 sparc_sllx (code, ins->sreg1, sparc_o7, ins->dreg);
2640 sparc_srax (code, ins->sreg1, ins->sreg2, ins->dreg);
2643 if (ins->inst_imm < (1 << 6))
2644 sparc_srax_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2646 sparc_set (code, ins->inst_imm, sparc_o7);
2647 sparc_srax (code, ins->sreg1, sparc_o7, ins->dreg);
2651 sparc_srlx (code, ins->sreg1, ins->sreg2, ins->dreg);
2653 case OP_LSHR_UN_IMM:
2654 if (ins->inst_imm < (1 << 6))
2655 sparc_srlx_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2657 sparc_set (code, ins->inst_imm, sparc_o7);
2658 sparc_srlx (code, ins->sreg1, sparc_o7, ins->dreg);
2663 /* can't use sparc_not */
2664 sparc_xnor (code, FALSE, ins->sreg1, sparc_g0, ins->dreg);
2668 /* can't use sparc_neg */
2669 sparc_sub (code, FALSE, sparc_g0, ins->sreg1, ins->dreg);
2673 sparc_smul (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2679 if ((ins->inst_imm == 1) && (ins->sreg1 == ins->dreg))
2682 /* Transform multiplication into a shift */
2683 for (i = 0; i < 30; ++i) {
2685 if (ins->inst_imm == imm)
2689 sparc_sll_imm (code, ins->sreg1, i, ins->dreg);
2691 EMIT_ALU_IMM (ins, smul, FALSE);
2696 sparc_smul (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2697 sparc_rdy (code, sparc_g1);
2698 sparc_sra_imm (code, ins->dreg, 31, sparc_o7);
2699 sparc_cmp (code, sparc_g1, sparc_o7);
2700 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (ins, sparc_bne, "OverflowException", TRUE, sparc_icc_short);
2702 case CEE_MUL_OVF_UN:
2703 case OP_IMUL_OVF_UN:
2704 sparc_umul (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2705 sparc_rdy (code, sparc_o7);
2706 sparc_cmp (code, sparc_o7, sparc_g0);
2707 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (ins, sparc_bne, "OverflowException", TRUE, sparc_icc_short);
2710 sparc_set (code, ins->inst_c0, ins->dreg);
2713 sparc_set (code, ins->inst_l, ins->dreg);
2716 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2717 sparc_set_template (code, ins->dreg);
2722 if (ins->sreg1 != ins->dreg)
2723 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
2726 /* Only used on V9 */
2727 if (ins->sreg1 != ins->dreg)
2728 sparc_fmovd (code, ins->sreg1, ins->dreg);
2730 case OP_SPARC_SETFREG_FLOAT:
2731 /* Only used on V9 */
2732 sparc_fdtos (code, ins->sreg1, ins->dreg);
2735 if (cfg->method->save_lmf)
2738 code = emit_load_volatile_arguments (cfg, code);
2739 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2740 sparc_set_template (code, sparc_o7);
2741 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_g0);
2742 /* Restore parent frame in delay slot */
2743 sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
2746 /* ensure ins->sreg1 is not NULL */
2747 sparc_ld_imm (code, ins->sreg1, 0, sparc_g0);
2750 sparc_add_imm (code, FALSE, sparc_fp, cfg->sig_cookie, sparc_o7);
2751 sparc_sti_imm (code, sparc_o7, ins->sreg1, 0);
2758 call = (MonoCallInst*)ins;
2759 g_assert (!call->virtual);
2760 code = emit_save_sp_to_lmf (cfg, code);
2761 if (ins->flags & MONO_INST_HAS_METHOD)
2762 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method);
2764 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
2766 code = emit_vret_token (ins, code);
2767 code = emit_move_return_value (ins, code);
2772 case OP_VOIDCALL_REG:
2774 call = (MonoCallInst*)ins;
2775 code = emit_save_sp_to_lmf (cfg, code);
2776 sparc_jmpl (code, ins->sreg1, sparc_g0, sparc_callsite);
2778 * We emit a special kind of nop in the delay slot to tell the
2779 * trampoline code that this is a virtual call, thus an unbox
2780 * trampoline might need to be called.
2783 sparc_or_imm (code, FALSE, sparc_g0, 0xca, sparc_g0);
2787 code = emit_vret_token (ins, code);
2788 code = emit_move_return_value (ins, code);
2790 case OP_FCALL_MEMBASE:
2791 case OP_LCALL_MEMBASE:
2792 case OP_VCALL_MEMBASE:
2793 case OP_VOIDCALL_MEMBASE:
2794 case OP_CALL_MEMBASE:
2795 call = (MonoCallInst*)ins;
2796 code = emit_save_sp_to_lmf (cfg, code);
2797 if (sparc_is_imm13 (ins->inst_offset)) {
2798 sparc_ldi_imm (code, ins->inst_basereg, ins->inst_offset, sparc_o7);
2800 sparc_set (code, ins->inst_offset, sparc_o7);
2801 sparc_ldi (code, ins->inst_basereg, sparc_o7, sparc_o7);
2803 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
2805 sparc_or_imm (code, FALSE, sparc_g0, 0xca, sparc_g0);
2809 code = emit_vret_token (ins, code);
2810 code = emit_move_return_value (ins, code);
2813 if (mono_method_signature (cfg->method)->ret->type == MONO_TYPE_R4)
2814 sparc_fdtos (code, ins->sreg1, sparc_f0);
2817 sparc_fmovd (code, ins->sreg1, ins->dreg);
2819 /* FIXME: Why not use fmovd ? */
2820 sparc_fmovs (code, ins->sreg1, ins->dreg);
2821 sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
2826 g_assert_not_reached ();
2831 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2832 /* Perform stack touching */
2836 /* Keep alignment */
2837 sparc_add_imm (code, FALSE, ins->sreg1, MONO_ARCH_LOCALLOC_ALIGNMENT - 1, ins->dreg);
2838 sparc_set (code, ~(MONO_ARCH_LOCALLOC_ALIGNMENT - 1), sparc_o7);
2839 sparc_and (code, FALSE, ins->dreg, sparc_o7, ins->dreg);
2841 if ((ins->flags & MONO_INST_INIT) && (ins->sreg1 == ins->dreg)) {
2843 size_reg = sparc_g4;
2845 size_reg = sparc_g1;
2847 sparc_mov_reg_reg (code, ins->dreg, size_reg);
2850 size_reg = ins->sreg1;
2852 sparc_sub (code, FALSE, sparc_sp, ins->dreg, ins->dreg);
2853 /* Keep %sp valid at all times */
2854 sparc_mov_reg_reg (code, ins->dreg, sparc_sp);
2855 g_assert (sparc_is_imm13 (MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset));
2856 sparc_add_imm (code, FALSE, ins->dreg, MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset, ins->dreg);
2858 if (ins->flags & MONO_INST_INIT) {
2860 /* Initialize memory region */
2861 sparc_cmp_imm (code, size_reg, 0);
2863 sparc_branch (code, 0, sparc_be, 0);
2865 sparc_set (code, 0, sparc_o7);
2866 sparc_sub_imm (code, 0, size_reg, sparcv9 ? 8 : 4, size_reg);
2870 sparc_stx (code, sparc_g0, ins->dreg, sparc_o7);
2872 sparc_st (code, sparc_g0, ins->dreg, sparc_o7);
2873 sparc_cmp (code, sparc_o7, size_reg);
2875 sparc_branch (code, 0, sparc_bl, 0);
2876 sparc_patch (br [2], br [1]);
2878 sparc_add_imm (code, 0, sparc_o7, sparcv9 ? 8 : 4, sparc_o7);
2879 sparc_patch (br [0], code);
2883 case OP_SPARC_LOCALLOC_IMM: {
2884 gint32 offset = ins->inst_c0;
2886 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2887 /* Perform stack touching */
2891 offset = ALIGN_TO (offset, MONO_ARCH_LOCALLOC_ALIGNMENT);
2892 if (sparc_is_imm13 (offset))
2893 sparc_sub_imm (code, FALSE, sparc_sp, offset, sparc_sp);
2895 sparc_set (code, offset, sparc_o7);
2896 sparc_sub (code, FALSE, sparc_sp, sparc_o7, sparc_sp);
2898 g_assert (sparc_is_imm13 (MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset));
2899 sparc_add_imm (code, FALSE, sparc_sp, MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset, ins->dreg);
2900 if ((ins->flags & MONO_INST_INIT) && (offset > 0)) {
2906 while (i < offset) {
2908 sparc_stx_imm (code, sparc_g0, ins->dreg, i);
2912 sparc_st_imm (code, sparc_g0, ins->dreg, i);
2918 sparc_set (code, offset, sparc_o7);
2919 sparc_sub_imm (code, 0, sparc_o7, sparcv9 ? 8 : 4, sparc_o7);
2920 /* beginning of loop */
2923 sparc_stx (code, sparc_g0, ins->dreg, sparc_o7);
2925 sparc_st (code, sparc_g0, ins->dreg, sparc_o7);
2926 sparc_cmp_imm (code, sparc_o7, 0);
2928 sparc_branch (code, 0, sparc_bne, 0);
2930 sparc_sub_imm (code, 0, sparc_o7, sparcv9 ? 8 : 4, sparc_o7);
2931 sparc_patch (br [1], br [0]);
2937 /* The return is done in the epilog */
2938 g_assert_not_reached ();
2941 sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
2942 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2943 (gpointer)"mono_arch_throw_exception");
2947 sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
2948 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2949 (gpointer)"mono_arch_rethrow_exception");
2952 case OP_START_HANDLER: {
2954 * The START_HANDLER instruction marks the beginning of a handler
2955 * block. It is called using a call instruction, so %o7 contains
2956 * the return address. Since the handler executes in the same stack
2957 * frame as the method itself, we can't use save/restore to save
2958 * the return address. Instead, we save it into a dedicated
2961 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
2962 if (!sparc_is_imm13 (spvar->inst_offset)) {
2963 sparc_set (code, spvar->inst_offset, GP_SCRATCH_REG);
2964 sparc_sti (code, sparc_o7, spvar->inst_basereg, GP_SCRATCH_REG);
2967 sparc_sti_imm (code, sparc_o7, spvar->inst_basereg, spvar->inst_offset);
2970 case OP_ENDFILTER: {
2971 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
2972 if (!sparc_is_imm13 (spvar->inst_offset)) {
2973 sparc_set (code, spvar->inst_offset, GP_SCRATCH_REG);
2974 sparc_ldi (code, spvar->inst_basereg, GP_SCRATCH_REG, sparc_o7);
2977 sparc_ldi_imm (code, spvar->inst_basereg, spvar->inst_offset, sparc_o7);
2978 sparc_jmpl_imm (code, sparc_o7, 8, sparc_g0);
2980 sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
2983 case OP_ENDFINALLY: {
2984 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
2985 if (!sparc_is_imm13 (spvar->inst_offset)) {
2986 sparc_set (code, spvar->inst_offset, GP_SCRATCH_REG);
2987 sparc_ldi (code, spvar->inst_basereg, GP_SCRATCH_REG, sparc_o7);
2990 sparc_ldi_imm (code, spvar->inst_basereg, spvar->inst_offset, sparc_o7);
2991 sparc_jmpl_imm (code, sparc_o7, 8, sparc_g0);
2995 case OP_CALL_HANDLER:
2996 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2997 /* This is a jump inside the method, so call_simple works even on V9 */
2998 sparc_call_simple (code, 0);
3002 ins->inst_c0 = (guint8*)code - cfg->native_code;
3005 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
3006 if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
3008 if (ins->flags & MONO_INST_BRLABEL) {
3009 if (ins->inst_i0->inst_c0) {
3010 gint32 disp = (ins->inst_i0->inst_c0 - ((guint8*)code - cfg->native_code)) >> 2;
3011 g_assert (sparc_is_imm22 (disp));
3012 sparc_branch (code, 1, sparc_ba, disp);
3014 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
3015 sparc_branch (code, 1, sparc_ba, 0);
3018 if (ins->inst_target_bb->native_offset) {
3019 gint32 disp = (ins->inst_target_bb->native_offset - ((guint8*)code - cfg->native_code)) >> 2;
3020 g_assert (sparc_is_imm22 (disp));
3021 sparc_branch (code, 1, sparc_ba, disp);
3023 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3024 sparc_branch (code, 1, sparc_ba, 0);
3030 sparc_jmp (code, ins->sreg1, sparc_g0);
3038 if (v64 && (cfg->opt & MONO_OPT_CMOV)) {
3039 sparc_clr_reg (code, ins->dreg);
3040 sparc_movcc_imm (code, sparc_xcc, opcode_to_sparc_cond (ins->opcode), 1, ins->dreg);
3043 sparc_clr_reg (code, ins->dreg);
3045 sparc_branchp (code, 1, opcode_to_sparc_cond (ins->opcode), DEFAULT_ICC, 0, 2);
3047 sparc_branch (code, 1, opcode_to_sparc_cond (ins->opcode), 2);
3050 sparc_set (code, 1, ins->dreg);
3058 if (v64 && (cfg->opt & MONO_OPT_CMOV)) {
3059 sparc_clr_reg (code, ins->dreg);
3060 sparc_movcc_imm (code, sparc_icc, opcode_to_sparc_cond (ins->opcode), 1, ins->dreg);
3063 sparc_clr_reg (code, ins->dreg);
3064 sparc_branchp (code, 1, opcode_to_sparc_cond (ins->opcode), sparc_icc_short, 0, 2);
3066 sparc_set (code, 1, ins->dreg);
3069 case OP_COND_EXC_EQ:
3070 case OP_COND_EXC_NE_UN:
3071 case OP_COND_EXC_LT:
3072 case OP_COND_EXC_LT_UN:
3073 case OP_COND_EXC_GT:
3074 case OP_COND_EXC_GT_UN:
3075 case OP_COND_EXC_GE:
3076 case OP_COND_EXC_GE_UN:
3077 case OP_COND_EXC_LE:
3078 case OP_COND_EXC_LE_UN:
3079 case OP_COND_EXC_OV:
3080 case OP_COND_EXC_NO:
3082 case OP_COND_EXC_NC:
3083 EMIT_COND_SYSTEM_EXCEPTION (ins, opcode_to_sparc_cond (ins->opcode), ins->inst_p1);
3085 case OP_SPARC_COND_EXC_EQZ:
3086 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brz, ins->inst_p1);
3088 case OP_SPARC_COND_EXC_GEZ:
3089 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brgez, ins->inst_p1);
3091 case OP_SPARC_COND_EXC_GTZ:
3092 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brgz, ins->inst_p1);
3094 case OP_SPARC_COND_EXC_LEZ:
3095 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brlez, ins->inst_p1);
3097 case OP_SPARC_COND_EXC_LTZ:
3098 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brlz, ins->inst_p1);
3100 case OP_SPARC_COND_EXC_NEZ:
3101 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brnz, ins->inst_p1);
3103 case OP_COND_EXC_IOV:
3104 case OP_COND_EXC_IC:
3105 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (ins, opcode_to_sparc_cond (ins->opcode), ins->inst_p1, TRUE, sparc_icc_short);
3118 EMIT_COND_BRANCH_PREDICTED (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
3120 EMIT_COND_BRANCH (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
3134 /* Only used on V9 */
3135 EMIT_COND_BRANCH_ICC (ins, opcode_to_sparc_cond (ins->opcode), 1, 1, sparc_icc_short);
3140 EMIT_COND_BRANCH_BPR (ins, brz, 1, 1, 1);
3142 case OP_SPARC_BRLEZ:
3143 EMIT_COND_BRANCH_BPR (ins, brlez, 1, 1, 1);
3146 EMIT_COND_BRANCH_BPR (ins, brlz, 1, 1, 1);
3149 EMIT_COND_BRANCH_BPR (ins, brnz, 1, 1, 1);
3152 EMIT_COND_BRANCH_BPR (ins, brgz, 1, 1, 1);
3154 case OP_SPARC_BRGEZ:
3155 EMIT_COND_BRANCH_BPR (ins, brgez, 1, 1, 1);
3158 /* floating point opcodes */
3160 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
3162 sparc_set_template (code, sparc_o7);
3164 sparc_sethi (code, 0, sparc_o7);
3166 sparc_lddf_imm (code, sparc_o7, 0, ins->dreg);
3169 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
3171 sparc_set_template (code, sparc_o7);
3173 sparc_sethi (code, 0, sparc_o7);
3175 sparc_ldf_imm (code, sparc_o7, 0, FP_SCRATCH_REG);
3177 /* Extend to double */
3178 sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
3180 case OP_STORER8_MEMBASE_REG:
3181 if (!sparc_is_imm13 (ins->inst_offset + 4)) {
3182 sparc_set (code, ins->inst_offset, sparc_o7);
3183 /* SPARCV9 handles misaligned fp loads/stores */
3184 if (!v64 && (ins->inst_offset % 8)) {
3186 sparc_add (code, FALSE, ins->inst_destbasereg, sparc_o7, sparc_o7);
3187 sparc_stf (code, ins->sreg1, sparc_o7, sparc_g0);
3188 sparc_stf_imm (code, ins->sreg1 + 1, sparc_o7, 4);
3190 sparc_stdf (code, ins->sreg1, ins->inst_destbasereg, sparc_o7);
3193 if (!v64 && (ins->inst_offset % 8)) {
3195 sparc_stf_imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3196 sparc_stf_imm (code, ins->sreg1 + 1, ins->inst_destbasereg, ins->inst_offset + 4);
3198 sparc_stdf_imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3201 case OP_LOADR8_MEMBASE:
3202 EMIT_LOAD_MEMBASE (ins, lddf);
3204 case OP_STORER4_MEMBASE_REG:
3205 /* This requires a double->single conversion */
3206 sparc_fdtos (code, ins->sreg1, FP_SCRATCH_REG);
3207 if (!sparc_is_imm13 (ins->inst_offset)) {
3208 sparc_set (code, ins->inst_offset, sparc_o7);
3209 sparc_stf (code, FP_SCRATCH_REG, ins->inst_destbasereg, sparc_o7);
3212 sparc_stf_imm (code, FP_SCRATCH_REG, ins->inst_destbasereg, ins->inst_offset);
3214 case OP_LOADR4_MEMBASE: {
3215 /* ldf needs a single precision register */
3216 int dreg = ins->dreg;
3217 ins->dreg = FP_SCRATCH_REG;
3218 EMIT_LOAD_MEMBASE (ins, ldf);
3220 /* Extend to double */
3221 sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
3226 sparc_fmovd (code, ins->sreg1, ins->dreg);
3228 sparc_fmovs (code, ins->sreg1, ins->dreg);
3229 sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
3233 gint32 offset = mono_spillvar_offset_float (cfg, 0);
3235 if (!sparc_is_imm13 (offset)) {
3236 sparc_set (code, offset, sparc_o7);
3237 sparc_stx (code, ins->sreg1, sparc_sp, offset);
3238 sparc_lddf (code, sparc_sp, offset, FP_SCRATCH_REG);
3240 sparc_stx_imm (code, ins->sreg1, sparc_sp, offset);
3241 sparc_lddf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3243 sparc_fxtos (code, FP_SCRATCH_REG, FP_SCRATCH_REG);
3245 if (!sparc_is_imm13 (offset)) {
3246 sparc_set (code, offset, sparc_o7);
3247 sparc_st (code, ins->sreg1, sparc_sp, sparc_o7);
3248 sparc_ldf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
3250 sparc_st_imm (code, ins->sreg1, sparc_sp, offset);
3251 sparc_ldf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3253 sparc_fitos (code, FP_SCRATCH_REG, FP_SCRATCH_REG);
3255 sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
3259 gint32 offset = mono_spillvar_offset_float (cfg, 0);
3261 if (!sparc_is_imm13 (offset)) {
3262 sparc_set (code, offset, sparc_o7);
3263 sparc_stx (code, ins->sreg1, sparc_sp, sparc_o7);
3264 sparc_lddf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
3266 sparc_stx_imm (code, ins->sreg1, sparc_sp, offset);
3267 sparc_lddf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3269 sparc_fxtod (code, FP_SCRATCH_REG, ins->dreg);
3271 if (!sparc_is_imm13 (offset)) {
3272 sparc_set (code, offset, sparc_o7);
3273 sparc_st (code, ins->sreg1, sparc_sp, sparc_o7);
3274 sparc_ldf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
3276 sparc_st_imm (code, ins->sreg1, sparc_sp, offset);
3277 sparc_ldf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3279 sparc_fitod (code, FP_SCRATCH_REG, ins->dreg);
3283 case OP_FCONV_TO_I1:
3284 case OP_FCONV_TO_U1:
3285 case OP_FCONV_TO_I2:
3286 case OP_FCONV_TO_U2:
3291 case OP_FCONV_TO_I4:
3292 case OP_FCONV_TO_U4: {
3293 gint32 offset = mono_spillvar_offset_float (cfg, 0);
3294 sparc_fdtoi (code, ins->sreg1, FP_SCRATCH_REG);
3295 if (!sparc_is_imm13 (offset)) {
3296 sparc_set (code, offset, sparc_o7);
3297 sparc_stdf (code, FP_SCRATCH_REG, sparc_sp, sparc_o7);
3298 sparc_ld (code, sparc_sp, sparc_o7, ins->dreg);
3300 sparc_stdf_imm (code, FP_SCRATCH_REG, sparc_sp, offset);
3301 sparc_ld_imm (code, sparc_sp, offset, ins->dreg);
3304 switch (ins->opcode) {
3305 case OP_FCONV_TO_I1:
3306 case OP_FCONV_TO_U1:
3307 sparc_and_imm (code, 0, ins->dreg, 0xff, ins->dreg);
3309 case OP_FCONV_TO_I2:
3310 case OP_FCONV_TO_U2:
3311 sparc_set (code, 0xffff, sparc_o7);
3312 sparc_and (code, 0, ins->dreg, sparc_o7, ins->dreg);
3319 case OP_FCONV_TO_I8:
3320 case OP_FCONV_TO_U8:
3322 g_assert_not_reached ();
3326 g_assert_not_reached ();
3328 case OP_LCONV_TO_R_UN: {
3330 g_assert_not_reached ();
3333 case OP_LCONV_TO_OVF_I: {
3334 guint32 *br [3], *label [1];
3337 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
3339 sparc_cmp_imm (code, ins->sreg1, 0);
3341 sparc_branch (code, 1, sparc_bneg, 0);
3345 /* ms word must be 0 */
3346 sparc_cmp_imm (code, ins->sreg2, 0);
3348 sparc_branch (code, 1, sparc_be, 0);
3353 EMIT_COND_SYSTEM_EXCEPTION (ins, sparc_ba, "OverflowException");
3356 sparc_patch (br [0], code);
3358 /* ms word must 0xfffffff */
3359 sparc_cmp_imm (code, ins->sreg2, -1);
3361 sparc_branch (code, 1, sparc_bne, 0);
3363 sparc_patch (br [2], label [0]);
3366 sparc_patch (br [1], code);
3367 if (ins->sreg1 != ins->dreg)
3368 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
3372 sparc_faddd (code, ins->sreg1, ins->sreg2, ins->dreg);
3375 sparc_fsubd (code, ins->sreg1, ins->sreg2, ins->dreg);
3378 sparc_fmuld (code, ins->sreg1, ins->sreg2, ins->dreg);
3381 sparc_fdivd (code, ins->sreg1, ins->sreg2, ins->dreg);
3385 sparc_fnegd (code, ins->sreg1, ins->dreg);
3387 /* FIXME: why don't use fnegd ? */
3388 sparc_fnegs (code, ins->sreg1, ins->dreg);
3392 sparc_fdivd (code, ins->sreg1, ins->sreg2, FP_SCRATCH_REG);
3393 sparc_fmuld (code, ins->sreg2, FP_SCRATCH_REG, FP_SCRATCH_REG);
3394 sparc_fsubd (code, ins->sreg1, FP_SCRATCH_REG, ins->dreg);
3397 sparc_fcmpd (code, ins->sreg1, ins->sreg2);
3404 sparc_fcmpd (code, ins->sreg1, ins->sreg2);
3405 sparc_clr_reg (code, ins->dreg);
3406 switch (ins->opcode) {
3409 sparc_fbranch (code, 1, opcode_to_sparc_cond (ins->opcode), 4);
3411 sparc_set (code, 1, ins->dreg);
3412 sparc_fbranch (code, 1, sparc_fbu, 2);
3414 sparc_set (code, 1, ins->dreg);
3417 sparc_fbranch (code, 1, opcode_to_sparc_cond (ins->opcode), 2);
3419 sparc_set (code, 1, ins->dreg);
3425 EMIT_FLOAT_COND_BRANCH (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
3428 /* clt.un + brfalse */
3430 sparc_fbranch (code, 1, sparc_fbul, 0);
3433 EMIT_FLOAT_COND_BRANCH (ins, sparc_fba, 1, 1);
3434 sparc_patch (p, (guint8*)code);
3438 /* cgt.un + brfalse */
3440 sparc_fbranch (code, 1, sparc_fbug, 0);
3443 EMIT_FLOAT_COND_BRANCH (ins, sparc_fba, 1, 1);
3444 sparc_patch (p, (guint8*)code);
3448 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbne, 1, 1);
3449 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3452 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbl, 1, 1);
3453 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3456 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbg, 1, 1);
3457 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3460 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbge, 1, 1);
3461 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3464 EMIT_FLOAT_COND_BRANCH (ins, sparc_fble, 1, 1);
3465 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3468 gint32 offset = mono_spillvar_offset_float (cfg, 0);
3469 if (!sparc_is_imm13 (offset)) {
3470 sparc_set (code, offset, sparc_o7);
3471 sparc_stdf (code, ins->sreg1, sparc_sp, sparc_o7);
3472 sparc_lduh (code, sparc_sp, sparc_o7, sparc_o7);
3474 sparc_stdf_imm (code, ins->sreg1, sparc_sp, offset);
3475 sparc_lduh_imm (code, sparc_sp, offset, sparc_o7);
3477 sparc_srl_imm (code, sparc_o7, 4, sparc_o7);
3478 sparc_and_imm (code, FALSE, sparc_o7, 2047, sparc_o7);
3479 sparc_cmp_imm (code, sparc_o7, 2047);
3480 EMIT_COND_SYSTEM_EXCEPTION (ins, sparc_be, "ArithmeticException");
3482 sparc_fmovd (code, ins->sreg1, ins->dreg);
3484 sparc_fmovs (code, ins->sreg1, ins->dreg);
3485 sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
3490 case OP_MEMORY_BARRIER:
3491 sparc_membar (code, sparc_membar_all);
3496 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3498 g_warning ("%s:%d: unknown opcode %s\n", __FILE__, __LINE__, mono_inst_name (ins->opcode));
3500 g_assert_not_reached ();
3503 if ((((guint8*)code) - code_start) > max_len) {
3504 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3505 mono_inst_name (ins->opcode), max_len, ((guint8*)code) - code_start);
3506 g_assert_not_reached ();
3516 cfg->code_len = (guint8*)code - cfg->native_code;
3520 mono_arch_register_lowlevel_calls (void)
3522 mono_register_jit_icall (mono_arch_break, "mono_arch_break", NULL, TRUE);
3523 mono_register_jit_icall (mono_arch_get_lmf_addr, "mono_arch_get_lmf_addr", NULL, TRUE);
3527 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3529 MonoJumpInfo *patch_info;
3531 /* FIXME: Move part of this to arch independent code */
3532 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3533 unsigned char *ip = patch_info->ip.i + code;
3536 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
3538 switch (patch_info->type) {
3539 case MONO_PATCH_INFO_NONE:
3541 case MONO_PATCH_INFO_CLASS_INIT: {
3542 guint32 *ip2 = (guint32*)ip;
3543 /* Might already been changed to a nop */
3545 sparc_set_template (ip2, sparc_o7);
3546 sparc_jmpl (ip2, sparc_o7, sparc_g0, sparc_o7);
3548 sparc_call_simple (ip2, 0);
3552 case MONO_PATCH_INFO_METHOD_JUMP: {
3553 guint32 *ip2 = (guint32*)ip;
3554 /* Might already been patched */
3555 sparc_set_template (ip2, sparc_o7);
3561 sparc_patch ((guint32*)ip, target);
3566 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
3569 guint32 *code = (guint32*)p;
3570 MonoMethodSignature *sig = mono_method_signature (cfg->method);
3573 /* Save registers to stack */
3574 for (i = 0; i < 6; ++i)
3575 sparc_sti_imm (code, sparc_i0 + i, sparc_fp, ARGS_OFFSET + (i * sizeof (gpointer)));
3577 cinfo = get_call_info (sig, FALSE);
3579 /* Save float regs on V9, since they are caller saved */
3580 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3581 ArgInfo *ainfo = cinfo->args + i;
3582 gint32 stack_offset;
3584 stack_offset = ainfo->offset + ARGS_OFFSET;
3586 if (ainfo->storage == ArgInFloatReg) {
3587 if (!sparc_is_imm13 (stack_offset))
3589 sparc_stf_imm (code, ainfo->reg, sparc_fp, stack_offset);
3591 else if (ainfo->storage == ArgInDoubleReg) {
3592 /* The offset is guaranteed to be aligned by the ABI rules */
3593 sparc_stdf_imm (code, ainfo->reg, sparc_fp, stack_offset);
3597 sparc_set (code, cfg->method, sparc_o0);
3598 sparc_add_imm (code, FALSE, sparc_fp, MONO_SPARC_STACK_BIAS, sparc_o1);
3600 mono_add_patch_info (cfg, (guint8*)code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
3603 /* Restore float regs on V9 */
3604 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3605 ArgInfo *ainfo = cinfo->args + i;
3606 gint32 stack_offset;
3608 stack_offset = ainfo->offset + ARGS_OFFSET;
3610 if (ainfo->storage == ArgInFloatReg) {
3611 if (!sparc_is_imm13 (stack_offset))
3613 sparc_ldf_imm (code, sparc_fp, stack_offset, ainfo->reg);
3615 else if (ainfo->storage == ArgInDoubleReg) {
3616 /* The offset is guaranteed to be aligned by the ABI rules */
3617 sparc_lddf_imm (code, sparc_fp, stack_offset, ainfo->reg);
3635 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
3637 guint32 *code = (guint32*)p;
3638 int save_mode = SAVE_NONE;
3639 MonoMethod *method = cfg->method;
3641 switch (mono_type_get_underlying_type (mono_method_signature (method)->ret)->type) {
3642 case MONO_TYPE_VOID:
3643 /* special case string .ctor icall */
3644 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
3645 save_mode = SAVE_ONE;
3647 save_mode = SAVE_NONE;
3652 save_mode = SAVE_ONE;
3654 save_mode = SAVE_TWO;
3659 save_mode = SAVE_FP;
3661 case MONO_TYPE_VALUETYPE:
3662 save_mode = SAVE_STRUCT;
3665 save_mode = SAVE_ONE;
3669 /* Save the result to the stack and also put it into the output registers */
3671 switch (save_mode) {
3674 sparc_st_imm (code, sparc_i0, sparc_fp, 68);
3675 sparc_st_imm (code, sparc_i0, sparc_fp, 72);
3676 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
3677 sparc_mov_reg_reg (code, sparc_i1, sparc_o2);
3680 sparc_sti_imm (code, sparc_i0, sparc_fp, ARGS_OFFSET);
3681 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
3685 sparc_stdf_imm (code, sparc_f0, sparc_fp, ARGS_OFFSET);
3687 sparc_stdf_imm (code, sparc_f0, sparc_fp, 72);
3688 sparc_ld_imm (code, sparc_fp, 72, sparc_o1);
3689 sparc_ld_imm (code, sparc_fp, 72 + 4, sparc_o2);
3694 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
3696 sparc_ld_imm (code, sparc_fp, 64, sparc_o1);
3704 sparc_set (code, cfg->method, sparc_o0);
3706 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, func);
3709 /* Restore result */
3711 switch (save_mode) {
3713 sparc_ld_imm (code, sparc_fp, 68, sparc_i0);
3714 sparc_ld_imm (code, sparc_fp, 72, sparc_i0);
3717 sparc_ldi_imm (code, sparc_fp, ARGS_OFFSET, sparc_i0);
3720 sparc_lddf_imm (code, sparc_fp, ARGS_OFFSET, sparc_f0);
3731 mono_arch_emit_prolog (MonoCompile *cfg)
3733 MonoMethod *method = cfg->method;
3734 MonoMethodSignature *sig;
3740 cfg->code_size = 256;
3741 cfg->native_code = g_malloc (cfg->code_size);
3742 code = (guint32*)cfg->native_code;
3744 /* FIXME: Generate intermediate code instead */
3746 offset = cfg->stack_offset;
3747 offset += (16 * sizeof (gpointer)); /* register save area */
3749 offset += 4; /* struct/union return pointer */
3752 /* add parameter area size for called functions */
3753 if (cfg->param_area < (6 * sizeof (gpointer)))
3754 /* Reserve space for the first 6 arguments even if it is unused */
3755 offset += 6 * sizeof (gpointer);
3757 offset += cfg->param_area;
3759 /* align the stack size */
3760 offset = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
3763 * localloc'd memory is stored between the local variables (whose
3764 * size is given by cfg->stack_offset), and between the space reserved
3767 cfg->arch.localloc_offset = offset - cfg->stack_offset;
3769 cfg->stack_offset = offset;
3771 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
3772 /* Perform stack touching */
3776 if (!sparc_is_imm13 (- cfg->stack_offset)) {
3777 /* Can't use sparc_o7 here, since we're still in the caller's frame */
3778 sparc_set (code, (- cfg->stack_offset), GP_SCRATCH_REG);
3779 sparc_save (code, sparc_sp, GP_SCRATCH_REG, sparc_sp);
3782 sparc_save_imm (code, sparc_sp, - cfg->stack_offset, sparc_sp);
3785 if (strstr (cfg->method->name, "foo")) {
3786 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, mono_sparc_break);
3787 sparc_call_simple (code, 0);
3792 sig = mono_method_signature (method);
3794 cinfo = get_call_info (sig, FALSE);
3796 /* Keep in sync with emit_load_volatile_arguments */
3797 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3798 ArgInfo *ainfo = cinfo->args + i;
3799 gint32 stack_offset;
3801 inst = cfg->args [i];
3803 if (sig->hasthis && (i == 0))
3804 arg_type = &mono_defaults.object_class->byval_arg;
3806 arg_type = sig->params [i - sig->hasthis];
3808 stack_offset = ainfo->offset + ARGS_OFFSET;
3810 /* Save the split arguments so they will reside entirely on the stack */
3811 if (ainfo->storage == ArgInSplitRegStack) {
3812 /* Save the register to the stack */
3813 g_assert (inst->opcode == OP_REGOFFSET);
3814 if (!sparc_is_imm13 (stack_offset))
3816 sparc_st_imm (code, sparc_i5, inst->inst_basereg, stack_offset);
3819 if (!v64 && !arg_type->byref && (arg_type->type == MONO_TYPE_R8)) {
3820 /* Save the argument to a dword aligned stack location */
3822 * stack_offset contains the offset of the argument on the stack.
3823 * inst->inst_offset contains the dword aligned offset where the value
3826 if (ainfo->storage == ArgInIRegPair) {
3827 if (!sparc_is_imm13 (inst->inst_offset + 4))
3829 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, inst->inst_offset);
3830 sparc_st_imm (code, sparc_i0 + ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
3833 if (ainfo->storage == ArgInSplitRegStack) {
3835 g_assert_not_reached ();
3837 if (stack_offset != inst->inst_offset) {
3838 /* stack_offset is not dword aligned, so we need to make a copy */
3839 sparc_st_imm (code, sparc_i5, inst->inst_basereg, inst->inst_offset);
3840 sparc_ld_imm (code, sparc_fp, stack_offset + 4, sparc_o7);
3841 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset + 4);
3845 if (ainfo->storage == ArgOnStackPair) {
3847 g_assert_not_reached ();
3849 if (stack_offset != inst->inst_offset) {
3850 /* stack_offset is not dword aligned, so we need to make a copy */
3851 sparc_ld_imm (code, sparc_fp, stack_offset, sparc_o7);
3852 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset);
3853 sparc_ld_imm (code, sparc_fp, stack_offset + 4, sparc_o7);
3854 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset + 4);
3858 g_assert_not_reached ();
3861 if ((ainfo->storage == ArgInIReg) && (inst->opcode != OP_REGVAR)) {
3862 /* Argument in register, but need to be saved to stack */
3863 if (!sparc_is_imm13 (stack_offset))
3865 if ((stack_offset - ARGS_OFFSET) & 0x1)
3866 sparc_stb_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
3868 if ((stack_offset - ARGS_OFFSET) & 0x2)
3869 sparc_sth_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
3871 if ((stack_offset - ARGS_OFFSET) & 0x4)
3872 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
3875 sparc_stx_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
3877 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
3881 if ((ainfo->storage == ArgInIRegPair) && (inst->opcode != OP_REGVAR)) {
3885 /* Argument in regpair, but need to be saved to stack */
3886 if (!sparc_is_imm13 (inst->inst_offset + 4))
3888 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, inst->inst_offset);
3889 sparc_st_imm (code, sparc_i0 + ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
3891 else if ((ainfo->storage == ArgInFloatReg) && (inst->opcode != OP_REGVAR)) {
3892 if (!sparc_is_imm13 (stack_offset))
3894 sparc_stf_imm (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
3896 else if ((ainfo->storage == ArgInDoubleReg) && (inst->opcode != OP_REGVAR)) {
3897 /* The offset is guaranteed to be aligned by the ABI rules */
3898 sparc_stdf_imm (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
3901 if ((ainfo->storage == ArgInFloatReg) && (inst->opcode == OP_REGVAR)) {
3902 /* Need to move into the a double precision register */
3903 sparc_fstod (code, ainfo->reg, ainfo->reg - 1);
3906 if ((ainfo->storage == ArgInSplitRegStack) || (ainfo->storage == ArgOnStack))
3907 if (inst->opcode == OP_REGVAR)
3908 /* FIXME: Load the argument into memory */
3914 if (cfg->method->save_lmf) {
3915 gint32 lmf_offset = STACK_BIAS - cfg->arch.lmf_offset;
3918 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3919 sparc_set_template (code, sparc_o7);
3920 sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ip));
3922 sparc_sti_imm (code, sparc_sp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, sp));
3924 sparc_sti_imm (code, sparc_fp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp));
3926 /* FIXME: add a relocation for this */
3927 sparc_set (code, cfg->method, sparc_o7);
3928 sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method));
3930 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3931 (gpointer)"mono_arch_get_lmf_addr");
3934 code = (guint32*)mono_sparc_emit_save_lmf (code, lmf_offset);
3937 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3938 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
3940 cfg->code_len = (guint8*)code - cfg->native_code;
3942 g_assert (cfg->code_len <= cfg->code_size);
3944 return (guint8*)code;
3948 mono_arch_emit_epilog (MonoCompile *cfg)
3950 MonoMethod *method = cfg->method;
3953 int max_epilog_size = 16 + 20 * 4;
3955 if (cfg->method->save_lmf)
3956 max_epilog_size += 128;
3958 if (mono_jit_trace_calls != NULL)
3959 max_epilog_size += 50;
3961 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3962 max_epilog_size += 50;
3964 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
3965 cfg->code_size *= 2;
3966 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3967 mono_jit_stats.code_reallocs++;
3970 code = (guint32*)(cfg->native_code + cfg->code_len);
3972 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3973 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
3975 if (cfg->method->save_lmf) {
3976 gint32 lmf_offset = STACK_BIAS - cfg->arch.lmf_offset;
3978 code = mono_sparc_emit_restore_lmf (code, lmf_offset);
3982 * The V8 ABI requires that calls to functions which return a structure
3985 if (!v64 && mono_method_signature (cfg->method)->pinvoke && MONO_TYPE_ISSTRUCT(mono_method_signature (cfg->method)->ret))
3986 sparc_jmpl_imm (code, sparc_i7, 12, sparc_g0);
3990 /* Only fold last instruction into the restore if the exit block has an in count of 1
3991 and the previous block hasn't been optimized away since it may have an in count > 1 */
3992 if (cfg->bb_exit->in_count == 1 && cfg->bb_exit->in_bb[0]->native_offset != cfg->bb_exit->native_offset)
3995 /* Try folding last instruction into the restore */
3996 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)) {
3997 /* or reg, imm, %i0 */
3998 int reg = sparc_inst_rs1 (code [-2]);
3999 int imm = sparc_inst_imm13 (code [-2]);
4000 code [-2] = code [-1];
4002 sparc_restore_imm (code, reg, imm, sparc_o0);
4005 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)) {
4006 /* or reg, reg, %i0 */
4007 int reg1 = sparc_inst_rs1 (code [-2]);
4008 int reg2 = sparc_inst_rs2 (code [-2]);
4009 code [-2] = code [-1];
4011 sparc_restore (code, reg1, reg2, sparc_o0);
4014 sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
4016 cfg->code_len = (guint8*)code - cfg->native_code;
4018 g_assert (cfg->code_len < cfg->code_size);
4023 mono_arch_emit_exceptions (MonoCompile *cfg)
4025 MonoJumpInfo *patch_info;
4030 MonoClass *exc_classes [16];
4031 guint8 *exc_throw_start [16], *exc_throw_end [16];
4033 /* Compute needed space */
4034 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4035 if (patch_info->type == MONO_PATCH_INFO_EXC)
4040 * make sure we have enough space for exceptions
4043 code_size = exc_count * (20 * 4);
4045 code_size = exc_count * 24;
4048 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
4049 cfg->code_size *= 2;
4050 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4051 mono_jit_stats.code_reallocs++;
4054 code = (guint32*)(cfg->native_code + cfg->code_len);
4056 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4057 switch (patch_info->type) {
4058 case MONO_PATCH_INFO_EXC: {
4059 MonoClass *exc_class;
4060 guint32 *buf, *buf2;
4061 guint32 throw_ip, type_idx;
4064 sparc_patch ((guint32*)(cfg->native_code + patch_info->ip.i), code);
4066 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
4067 type_idx = exc_class->type_token - MONO_TOKEN_TYPE_DEF;
4068 g_assert (exc_class);
4069 throw_ip = patch_info->ip.i;
4071 /* Find a throw sequence for the same exception class */
4072 for (i = 0; i < nthrows; ++i)
4073 if (exc_classes [i] == exc_class)
4077 guint32 throw_offset = (((guint8*)exc_throw_end [i] - cfg->native_code) - throw_ip) >> 2;
4078 if (!sparc_is_imm13 (throw_offset))
4079 sparc_set32 (code, throw_offset, sparc_o1);
4081 disp = (exc_throw_start [i] - (guint8*)code) >> 2;
4082 g_assert (sparc_is_imm22 (disp));
4083 sparc_branch (code, 0, sparc_ba, disp);
4084 if (sparc_is_imm13 (throw_offset))
4085 sparc_set32 (code, throw_offset, sparc_o1);
4088 patch_info->type = MONO_PATCH_INFO_NONE;
4091 /* Emit the template for setting o1 */
4093 if (sparc_is_imm13 (((((guint8*)code - cfg->native_code) - throw_ip) >> 2) - 8))
4094 /* Can use a short form */
4097 sparc_set_template (code, sparc_o1);
4101 exc_classes [nthrows] = exc_class;
4102 exc_throw_start [nthrows] = (guint8*)code;
4106 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, mono_sparc_break);
4110 /* first arg = type token */
4111 /* Pass the type index to reduce the size of the sparc_set */
4112 if (!sparc_is_imm13 (type_idx))
4113 sparc_set32 (code, type_idx, sparc_o0);
4115 /* second arg = offset between the throw ip and the current ip */
4116 /* On sparc, the saved ip points to the call instruction */
4117 disp = (((guint8*)code - cfg->native_code) - throw_ip) >> 2;
4118 sparc_set32 (buf, disp, sparc_o1);
4123 exc_throw_end [nthrows] = (guint8*)code;
4127 patch_info->data.name = "mono_arch_throw_corlib_exception";
4128 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
4129 patch_info->ip.i = (guint8*)code - cfg->native_code;
4133 if (sparc_is_imm13 (type_idx)) {
4134 /* Put it into the delay slot */
4137 sparc_set32 (code, type_idx, sparc_o0);
4138 g_assert (code - buf == 1);
4149 cfg->code_len = (guint8*)code - cfg->native_code;
4151 g_assert (cfg->code_len < cfg->code_size);
4155 gboolean lmf_addr_key_inited = FALSE;
4157 #ifdef MONO_SPARC_THR_TLS
4158 thread_key_t lmf_addr_key;
4160 pthread_key_t lmf_addr_key;
4164 mono_arch_get_lmf_addr (void)
4166 /* This is perf critical so we bypass the IO layer */
4167 /* The thr_... functions seem to be somewhat faster */
4168 #ifdef MONO_SPARC_THR_TLS
4170 thr_getspecific (lmf_addr_key, &res);
4173 return pthread_getspecific (lmf_addr_key);
4177 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
4180 * There seems to be no way to determine stack boundaries under solaris,
4181 * so it's not possible to determine whenever a SIGSEGV is caused by stack
4184 #error "--with-sigaltstack=yes not supported on solaris"
4189 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
4191 if (!lmf_addr_key_inited) {
4194 lmf_addr_key_inited = TRUE;
4196 #ifdef MONO_SPARC_THR_TLS
4197 res = thr_keycreate (&lmf_addr_key, NULL);
4199 res = pthread_key_create (&lmf_addr_key, NULL);
4201 g_assert (res == 0);
4205 #ifdef MONO_SPARC_THR_TLS
4206 thr_setspecific (lmf_addr_key, &tls->lmf);
4208 pthread_setspecific (lmf_addr_key, &tls->lmf);
4213 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
4218 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *call, int this_reg, int this_type, int vt_reg)
4220 int this_out_reg = sparc_o0;
4225 MONO_INST_NEW (cfg, ins, OP_MOVE);
4226 ins->sreg1 = vt_reg;
4227 ins->dreg = mono_regstate_next_int (cfg->rs);
4228 mono_bblock_add_inst (cfg->cbb, ins);
4230 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, sparc_o0, FALSE);
4232 this_out_reg = sparc_o1;
4234 /* Set the 'struct/union return pointer' location on the stack */
4235 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, sparc_sp, 64, vt_reg);
4239 /* add the this argument */
4240 if (this_reg != -1) {
4242 MONO_INST_NEW (cfg, this, OP_MOVE);
4243 this->type = this_type;
4244 this->sreg1 = this_reg;
4245 this->dreg = mono_regstate_next_int (cfg->rs);
4246 mono_bblock_add_inst (cfg->cbb, this);
4248 mono_call_inst_add_outarg_reg (cfg, call, this->dreg, this_out_reg, FALSE);
4254 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4256 MonoInst *ins = NULL;
4258 if (cmethod->klass == mono_defaults.thread_class &&
4259 strcmp (cmethod->name, "MemoryBarrier") == 0) {
4261 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
4268 * mono_arch_get_argument_info:
4269 * @csig: a method signature
4270 * @param_count: the number of parameters to consider
4271 * @arg_info: an array to store the result infos
4273 * Gathers information on parameters such as size, alignment and
4274 * padding. arg_info should be large enought to hold param_count + 1 entries.
4276 * Returns the size of the activation frame.
4279 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
4285 cinfo = get_call_info (csig, FALSE);
4287 if (csig->hasthis) {
4288 ainfo = &cinfo->args [0];
4289 arg_info [0].offset = ARGS_OFFSET - MONO_SPARC_STACK_BIAS + ainfo->offset;
4292 for (k = 0; k < param_count; k++) {
4293 ainfo = &cinfo->args [k + csig->hasthis];
4295 arg_info [k + 1].offset = ARGS_OFFSET - MONO_SPARC_STACK_BIAS + ainfo->offset;
4296 arg_info [k + 1].size = mono_type_size (csig->params [k], &align);
4305 mono_arch_print_tree (MonoInst *tree, int arity)
4310 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
4315 MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)