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) {
877 inst = m->varinfo [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;
1724 si = &cfg->spill_info_float;
1726 while (i <= spillvar) {
1729 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1731 cfg->stack_offset += sizeof (double);
1732 cfg->stack_offset = ALIGN_TO (cfg->stack_offset, 8);
1733 info->offset = - cfg->stack_offset;
1737 return MONO_SPARC_STACK_BIAS + (*si)->offset;
1743 g_assert_not_reached ();
1747 /* FIXME: Strange loads from the stack in basic-float.cs:test_2_rem */
1750 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1752 mono_local_regalloc (cfg, bb);
1756 sparc_patch (guint32 *code, const gpointer target)
1759 guint32 ins = *code;
1760 guint32 op = ins >> 30;
1761 guint32 op2 = (ins >> 22) & 0x7;
1762 guint32 rd = (ins >> 25) & 0x1f;
1763 guint8* target8 = (guint8*)target;
1764 gint64 disp = (target8 - (guint8*)code) >> 2;
1767 // g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
1769 if ((op == 0) && (op2 == 2)) {
1770 if (!sparc_is_imm22 (disp))
1773 *code = ((ins >> 22) << 22) | (disp & 0x3fffff);
1775 else if ((op == 0) && (op2 == 1)) {
1776 if (!sparc_is_imm19 (disp))
1779 *code = ((ins >> 19) << 19) | (disp & 0x7ffff);
1781 else if ((op == 0) && (op2 == 3)) {
1782 if (!sparc_is_imm16 (disp))
1785 *code &= ~(0x180000 | 0x3fff);
1786 *code |= ((disp << 21) & (0x180000)) | (disp & 0x3fff);
1788 else if ((op == 0) && (op2 == 6)) {
1789 if (!sparc_is_imm22 (disp))
1792 *code = ((ins >> 22) << 22) | (disp & 0x3fffff);
1794 else if ((op == 0) && (op2 == 4)) {
1795 guint32 ins2 = code [1];
1797 if (((ins2 >> 30) == 2) && (((ins2 >> 19) & 0x3f) == 2)) {
1798 /* sethi followed by or */
1800 sparc_set (p, target8, rd);
1801 while (p <= (code + 1))
1804 else if (ins2 == 0x01000000) {
1805 /* sethi followed by nop */
1807 sparc_set (p, target8, rd);
1808 while (p <= (code + 1))
1811 else if ((sparc_inst_op (ins2) == 3) && (sparc_inst_imm (ins2))) {
1812 /* sethi followed by load/store */
1814 guint32 t = (guint32)target8;
1815 *code &= ~(0x3fffff);
1817 *(code + 1) &= ~(0x3ff);
1818 *(code + 1) |= (t & 0x3ff);
1822 (sparc_inst_rd (ins) == sparc_g1) &&
1823 (sparc_inst_op (c [1]) == 0) && (sparc_inst_op2 (c [1]) == 4) &&
1824 (sparc_inst_op (c [2]) == 2) && (sparc_inst_op3 (c [2]) == 2) &&
1825 (sparc_inst_op (c [3]) == 2) && (sparc_inst_op3 (c [3]) == 2))
1829 reg = sparc_inst_rd (c [1]);
1830 sparc_set (p, target8, reg);
1834 else if ((sparc_inst_op (ins2) == 2) && (sparc_inst_op3 (ins2) == 0x38) &&
1835 (sparc_inst_imm (ins2))) {
1836 /* sethi followed by jmpl */
1838 guint32 t = (guint32)target8;
1839 *code &= ~(0x3fffff);
1841 *(code + 1) &= ~(0x3ff);
1842 *(code + 1) |= (t & 0x3ff);
1848 else if (op == 01) {
1849 gint64 disp = (target8 - (guint8*)code) >> 2;
1851 if (!sparc_is_imm30 (disp))
1853 sparc_call_simple (code, target8 - (guint8*)code);
1855 else if ((op == 2) && (sparc_inst_op3 (ins) == 0x2) && sparc_inst_imm (ins)) {
1857 g_assert (sparc_is_imm13 (target8));
1859 *code |= (guint32)target8;
1861 else if ((sparc_inst_op (ins) == 2) && (sparc_inst_op3 (ins) == 0x7)) {
1862 /* sparc_set case 5. */
1866 reg = sparc_inst_rd (c [3]);
1867 sparc_set (p, target, reg);
1874 // g_print ("patched with 0x%08x\n", ins);
1878 * mono_sparc_emit_save_lmf:
1880 * Emit the code neccesary to push a new entry onto the lmf stack. Used by
1881 * trampolines as well.
1884 mono_sparc_emit_save_lmf (guint32 *code, guint32 lmf_offset)
1887 sparc_sti_imm (code, sparc_o0, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr));
1888 /* Save previous_lmf */
1889 sparc_ldi (code, sparc_o0, sparc_g0, sparc_o7);
1890 sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf));
1892 sparc_add_imm (code, FALSE, sparc_fp, lmf_offset, sparc_o7);
1893 sparc_sti (code, sparc_o7, sparc_o0, sparc_g0);
1899 mono_sparc_emit_restore_lmf (guint32 *code, guint32 lmf_offset)
1901 /* Load previous_lmf */
1902 sparc_ldi_imm (code, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sparc_l0);
1904 sparc_ldi_imm (code, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sparc_l1);
1905 /* *(lmf) = previous_lmf */
1906 sparc_sti (code, sparc_l0, sparc_l1, sparc_g0);
1911 emit_save_sp_to_lmf (MonoCompile *cfg, guint32 *code)
1914 * Since register windows are saved to the current value of %sp, we need to
1915 * set the sp field in the lmf before the call, not in the prolog.
1917 if (cfg->method->save_lmf) {
1918 gint32 lmf_offset = MONO_SPARC_STACK_BIAS - cfg->arch.lmf_offset;
1921 sparc_sti_imm (code, sparc_sp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, sp));
1928 emit_vret_token (MonoInst *ins, guint32 *code)
1930 MonoCallInst *call = (MonoCallInst*)ins;
1934 * The sparc ABI requires that calls to functions which return a structure
1935 * contain an additional unimpl instruction which is checked by the callee.
1937 if (call->signature->pinvoke && MONO_TYPE_ISSTRUCT(call->signature->ret)) {
1938 if (call->signature->ret->type == MONO_TYPE_TYPEDBYREF)
1939 size = mono_type_stack_size (call->signature->ret, NULL);
1941 size = mono_class_native_size (call->signature->ret->data.klass, NULL);
1942 sparc_unimp (code, size & 0xfff);
1949 emit_move_return_value (MonoInst *ins, guint32 *code)
1951 /* Move return value to the target register */
1952 /* FIXME: do more things in the local reg allocator */
1953 switch (ins->opcode) {
1955 case OP_VOIDCALL_REG:
1956 case OP_VOIDCALL_MEMBASE:
1960 case OP_CALL_MEMBASE:
1961 g_assert (ins->dreg == sparc_o0);
1965 case OP_LCALL_MEMBASE:
1967 * ins->dreg is the least significant reg due to the lreg: LCALL rule
1968 * in inssel-long32.brg.
1971 sparc_mov_reg_reg (code, sparc_o0, ins->dreg);
1973 g_assert (ins->dreg == sparc_o1);
1978 case OP_FCALL_MEMBASE:
1980 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4) {
1981 sparc_fmovs (code, sparc_f0, ins->dreg);
1982 sparc_fstod (code, ins->dreg, ins->dreg);
1985 sparc_fmovd (code, sparc_f0, ins->dreg);
1987 sparc_fmovs (code, sparc_f0, ins->dreg);
1988 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4)
1989 sparc_fstod (code, ins->dreg, ins->dreg);
1991 sparc_fmovs (code, sparc_f1, ins->dreg + 1);
1996 case OP_VCALL_MEMBASE:
2006 * emit_load_volatile_arguments:
2008 * Load volatile arguments from the stack to the original input registers.
2009 * Required before a tail call.
2012 emit_load_volatile_arguments (MonoCompile *cfg, guint32 *code)
2014 MonoMethod *method = cfg->method;
2015 MonoMethodSignature *sig;
2020 /* FIXME: Generate intermediate code instead */
2022 sig = mono_method_signature (method);
2024 cinfo = get_call_info (sig, FALSE);
2026 /* This is the opposite of the code in emit_prolog */
2028 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2029 ArgInfo *ainfo = cinfo->args + i;
2030 gint32 stack_offset;
2032 inst = cfg->varinfo [i];
2034 if (sig->hasthis && (i == 0))
2035 arg_type = &mono_defaults.object_class->byval_arg;
2037 arg_type = sig->params [i - sig->hasthis];
2039 stack_offset = ainfo->offset + ARGS_OFFSET;
2040 ireg = sparc_i0 + ainfo->reg;
2042 if (ainfo->storage == ArgInSplitRegStack) {
2043 g_assert (inst->opcode == OP_REGOFFSET);
2045 if (!sparc_is_imm13 (stack_offset))
2047 sparc_st_imm (code, inst->inst_basereg, stack_offset, sparc_i5);
2050 if (!v64 && !arg_type->byref && (arg_type->type == MONO_TYPE_R8)) {
2051 if (ainfo->storage == ArgInIRegPair) {
2052 if (!sparc_is_imm13 (inst->inst_offset + 4))
2054 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, ireg);
2055 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset + 4, ireg + 1);
2058 if (ainfo->storage == ArgInSplitRegStack) {
2059 if (stack_offset != inst->inst_offset) {
2060 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, sparc_i5);
2061 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset + 4, sparc_o7);
2062 sparc_st_imm (code, sparc_o7, sparc_fp, stack_offset + 4);
2067 if (ainfo->storage == ArgOnStackPair) {
2068 if (stack_offset != inst->inst_offset) {
2069 /* stack_offset is not dword aligned, so we need to make a copy */
2070 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, sparc_o7);
2071 sparc_st_imm (code, sparc_o7, sparc_fp, stack_offset);
2073 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset + 4, sparc_o7);
2074 sparc_st_imm (code, sparc_o7, sparc_fp, stack_offset + 4);
2079 g_assert_not_reached ();
2082 if ((ainfo->storage == ArgInIReg) && (inst->opcode != OP_REGVAR)) {
2083 /* Argument in register, but need to be saved to stack */
2084 if (!sparc_is_imm13 (stack_offset))
2086 if ((stack_offset - ARGS_OFFSET) & 0x1)
2087 /* FIXME: Is this ldsb or ldub ? */
2088 sparc_ldsb_imm (code, inst->inst_basereg, stack_offset, ireg);
2090 if ((stack_offset - ARGS_OFFSET) & 0x2)
2091 sparc_ldsh_imm (code, inst->inst_basereg, stack_offset, ireg);
2093 if ((stack_offset - ARGS_OFFSET) & 0x4)
2094 sparc_ld_imm (code, inst->inst_basereg, stack_offset, ireg);
2097 sparc_ldx_imm (code, inst->inst_basereg, stack_offset, ireg);
2099 sparc_ld_imm (code, inst->inst_basereg, stack_offset, ireg);
2102 else if ((ainfo->storage == ArgInIRegPair) && (inst->opcode != OP_REGVAR)) {
2103 /* Argument in regpair, but need to be saved to stack */
2104 if (!sparc_is_imm13 (inst->inst_offset + 4))
2106 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, ireg);
2107 sparc_st_imm (code, inst->inst_basereg, inst->inst_offset + 4, ireg + 1);
2109 else if ((ainfo->storage == ArgInFloatReg) && (inst->opcode != OP_REGVAR)) {
2112 else if ((ainfo->storage == ArgInDoubleReg) && (inst->opcode != OP_REGVAR)) {
2116 if ((ainfo->storage == ArgInSplitRegStack) || (ainfo->storage == ArgOnStack))
2117 if (inst->opcode == OP_REGVAR)
2118 /* FIXME: Load the argument into memory */
2128 * mono_sparc_is_virtual_call:
2130 * Determine whenever the instruction at CODE is a virtual call.
2133 mono_sparc_is_virtual_call (guint32 *code)
2140 if ((sparc_inst_op (*code) == 0x2) && (sparc_inst_op3 (*code) == 0x38)) {
2142 * Register indirect call. If it is a virtual call, then the
2143 * instruction in the delay slot is a special kind of nop.
2146 /* Construct special nop */
2147 sparc_or_imm (p, FALSE, sparc_g0, 0xca, sparc_g0);
2150 if (code [1] == p [0])
2158 * mono_arch_get_vcall_slot_addr:
2160 * Determine the vtable slot used by a virtual call.
2163 mono_arch_get_vcall_slot_addr (guint8 *code8, gpointer *regs)
2165 guint32 *code = (guint32*)(gpointer)code8;
2166 guint32 ins = code [0];
2167 guint32 prev_ins = code [-1];
2169 mono_sparc_flushw ();
2171 if (!mono_sparc_is_virtual_call (code))
2174 if ((sparc_inst_op (ins) == 0x2) && (sparc_inst_op3 (ins) == 0x38)) {
2175 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)) {
2176 /* ld [r1 + CONST ], r2; call r2 */
2177 guint32 base = sparc_inst_rs1 (prev_ins);
2178 guint32 disp = sparc_inst_imm13 (prev_ins);
2181 g_assert (sparc_inst_rd (prev_ins) == sparc_inst_rs1 (ins));
2183 g_assert ((base >= sparc_o0) && (base <= sparc_i7));
2185 base_val = regs [base - sparc_o0];
2187 return (gpointer)((guint8*)base_val + disp);
2189 else if ((sparc_inst_op (prev_ins) == 0x3) && (sparc_inst_i (prev_ins) == 0) && (sparc_inst_op3 (prev_ins) == 0)) {
2190 /* set r1, ICONST; ld [r1 + r2], r2; call r2 */
2191 /* Decode a sparc_set32 */
2192 guint32 base = sparc_inst_rs1 (prev_ins);
2195 guint32 s1 = code [-3];
2196 guint32 s2 = code [-2];
2203 g_assert (sparc_inst_op (s1) == 0);
2204 g_assert (sparc_inst_op2 (s1) == 4);
2207 g_assert (sparc_inst_op (s2) == 2);
2208 g_assert (sparc_inst_op3 (s2) == 2);
2209 g_assert (sparc_inst_i (s2) == 1);
2210 g_assert (sparc_inst_rs1 (s2) == sparc_inst_rd (s2));
2211 g_assert (sparc_inst_rd (s1) == sparc_inst_rs1 (s2));
2213 disp = ((s1 & 0x3fffff) << 10) | sparc_inst_imm13 (s2);
2215 g_assert ((base >= sparc_o0) && (base <= sparc_i7));
2217 base_val = regs [base - sparc_o0];
2219 return (gpointer)((guint8*)base_val + disp);
2221 g_assert_not_reached ();
2224 g_assert_not_reached ();
2230 * Some conventions used in the following code.
2231 * 2) The only scratch registers we have are o7 and g1. We try to
2232 * stick to o7 when we can, and use g1 when necessary.
2236 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2241 guint32 *code = (guint32*)(cfg->native_code + cfg->code_len);
2242 MonoInst *last_ins = NULL;
2246 if (cfg->opt & MONO_OPT_PEEPHOLE)
2247 peephole_pass (cfg, bb);
2249 if (cfg->verbose_level > 2)
2250 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2252 cpos = bb->max_offset;
2254 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2262 offset = (guint8*)code - cfg->native_code;
2264 spec = ins_get_spec (ins->opcode);
2265 /* I kept this, but this looks a workaround for a bug */
2266 if (spec == MONO_ARCH_CPU_SPEC)
2267 spec = ins_get_spec (CEE_ADD);
2269 max_len = ((guint8 *)spec)[MONO_INST_LEN];
2271 if (offset > (cfg->code_size - max_len - 16)) {
2272 cfg->code_size *= 2;
2273 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2274 code = (guint32*)(cfg->native_code + offset);
2276 code_start = (guint8*)code;
2277 // if (ins->cil_code)
2278 // g_print ("cil code\n");
2279 mono_debug_record_line_number (cfg, ins, offset);
2281 switch (ins->opcode) {
2282 case OP_STOREI1_MEMBASE_IMM:
2283 EMIT_STORE_MEMBASE_IMM (ins, stb);
2285 case OP_STOREI2_MEMBASE_IMM:
2286 EMIT_STORE_MEMBASE_IMM (ins, sth);
2288 case OP_STORE_MEMBASE_IMM:
2289 EMIT_STORE_MEMBASE_IMM (ins, sti);
2291 case OP_STOREI4_MEMBASE_IMM:
2292 EMIT_STORE_MEMBASE_IMM (ins, st);
2294 case OP_STOREI8_MEMBASE_IMM:
2296 EMIT_STORE_MEMBASE_IMM (ins, stx);
2298 /* Only generated by peephole opts */
2299 g_assert ((ins->inst_offset % 8) == 0);
2300 g_assert (ins->inst_imm == 0);
2301 EMIT_STORE_MEMBASE_IMM (ins, stx);
2304 case OP_STOREI1_MEMBASE_REG:
2305 EMIT_STORE_MEMBASE_REG (ins, stb);
2307 case OP_STOREI2_MEMBASE_REG:
2308 EMIT_STORE_MEMBASE_REG (ins, sth);
2310 case OP_STOREI4_MEMBASE_REG:
2311 EMIT_STORE_MEMBASE_REG (ins, st);
2313 case OP_STOREI8_MEMBASE_REG:
2315 EMIT_STORE_MEMBASE_REG (ins, stx);
2317 /* Only used by OP_MEMSET */
2318 EMIT_STORE_MEMBASE_REG (ins, std);
2321 case OP_STORE_MEMBASE_REG:
2322 EMIT_STORE_MEMBASE_REG (ins, sti);
2326 sparc_ldx (code, ins->inst_c0, sparc_g0, ins->dreg);
2328 sparc_ld (code, ins->inst_c0, sparc_g0, ins->dreg);
2333 sparc_ldsw (code, ins->inst_c0, sparc_g0, ins->dreg);
2335 sparc_ld (code, ins->inst_c0, sparc_g0, ins->dreg);
2339 sparc_ld (code, ins->inst_c0, sparc_g0, ins->dreg);
2342 sparc_set (code, ins->inst_c0, ins->dreg);
2343 sparc_ld (code, ins->dreg, sparc_g0, ins->dreg);
2345 case OP_LOADI4_MEMBASE:
2347 EMIT_LOAD_MEMBASE (ins, ldsw);
2349 EMIT_LOAD_MEMBASE (ins, ld);
2352 case OP_LOADU4_MEMBASE:
2353 EMIT_LOAD_MEMBASE (ins, ld);
2355 case OP_LOADU1_MEMBASE:
2356 EMIT_LOAD_MEMBASE (ins, ldub);
2358 case OP_LOADI1_MEMBASE:
2359 EMIT_LOAD_MEMBASE (ins, ldsb);
2361 case OP_LOADU2_MEMBASE:
2362 EMIT_LOAD_MEMBASE (ins, lduh);
2364 case OP_LOADI2_MEMBASE:
2365 EMIT_LOAD_MEMBASE (ins, ldsh);
2367 case OP_LOAD_MEMBASE:
2369 EMIT_LOAD_MEMBASE (ins, ldx);
2371 EMIT_LOAD_MEMBASE (ins, ld);
2375 case OP_LOADI8_MEMBASE:
2376 EMIT_LOAD_MEMBASE (ins, ldx);
2380 sparc_sll_imm (code, ins->sreg1, 24, sparc_o7);
2381 sparc_sra_imm (code, sparc_o7, 24, ins->dreg);
2384 sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
2385 sparc_sra_imm (code, sparc_o7, 16, ins->dreg);
2388 sparc_and_imm (code, FALSE, ins->sreg1, 0xff, ins->dreg);
2391 sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
2392 sparc_srl_imm (code, sparc_o7, 16, ins->dreg);
2394 case CEE_CONV_OVF_U4:
2395 /* Only used on V9 */
2396 sparc_cmp_imm (code, ins->sreg1, 0);
2397 mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code,
2398 MONO_PATCH_INFO_EXC, "OverflowException");
2399 sparc_branchp (code, 0, sparc_bl, sparc_xcc_short, 0, 0);
2401 sparc_set (code, 1, sparc_o7);
2402 sparc_sllx_imm (code, sparc_o7, 32, sparc_o7);
2403 sparc_cmp (code, ins->sreg1, sparc_o7);
2404 mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code,
2405 MONO_PATCH_INFO_EXC, "OverflowException");
2406 sparc_branchp (code, 0, sparc_bge, sparc_xcc_short, 0, 0);
2408 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
2410 case CEE_CONV_OVF_I4_UN:
2411 /* Only used on V9 */
2416 /* Only used on V9 */
2417 sparc_srl_imm (code, ins->sreg1, 0, ins->dreg);
2421 /* Only used on V9 */
2422 sparc_sra_imm (code, ins->sreg1, 0, ins->dreg);
2427 sparc_cmp (code, ins->sreg1, ins->sreg2);
2429 case OP_COMPARE_IMM:
2430 case OP_ICOMPARE_IMM:
2431 if (sparc_is_imm13 (ins->inst_imm))
2432 sparc_cmp_imm (code, ins->sreg1, ins->inst_imm);
2434 sparc_set (code, ins->inst_imm, sparc_o7);
2435 sparc_cmp (code, ins->sreg1, sparc_o7);
2440 * gdb does not like encountering 'ta 1' in the debugged code. So
2441 * instead of emitting a trap, we emit a call a C function and place a
2444 //sparc_ta (code, 1);
2445 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, mono_arch_break);
2450 sparc_add (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2454 sparc_add (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2459 /* according to inssel-long32.brg, this should set cc */
2460 EMIT_ALU_IMM (ins, add, TRUE);
2464 /* according to inssel-long32.brg, this should set cc */
2465 sparc_addx (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2469 EMIT_ALU_IMM (ins, addx, TRUE);
2473 sparc_sub (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2477 sparc_sub (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2482 /* according to inssel-long32.brg, this should set cc */
2483 EMIT_ALU_IMM (ins, sub, TRUE);
2487 /* according to inssel-long32.brg, this should set cc */
2488 sparc_subx (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2492 EMIT_ALU_IMM (ins, subx, TRUE);
2496 sparc_and (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2500 EMIT_ALU_IMM (ins, and, FALSE);
2504 /* Sign extend sreg1 into %y */
2505 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2506 sparc_wry (code, sparc_o7, sparc_g0);
2507 sparc_sdiv (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2508 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2512 sparc_wry (code, sparc_g0, sparc_g0);
2513 sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2518 /* Transform division into a shift */
2519 for (i = 1; i < 30; ++i) {
2521 if (ins->inst_imm == imm)
2527 sparc_srl_imm (code, ins->sreg1, 31, sparc_o7);
2528 sparc_add (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2529 sparc_sra_imm (code, ins->dreg, 1, ins->dreg);
2532 /* http://compilers.iecc.com/comparch/article/93-04-079 */
2533 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2534 sparc_srl_imm (code, sparc_o7, 32 - i, sparc_o7);
2535 sparc_add (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2536 sparc_sra_imm (code, ins->dreg, i, ins->dreg);
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 EMIT_ALU_IMM (ins, sdiv, TRUE);
2544 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2550 /* Sign extend sreg1 into %y */
2551 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2552 sparc_wry (code, sparc_o7, sparc_g0);
2553 sparc_sdiv (code, TRUE, ins->sreg1, ins->sreg2, sparc_o7);
2554 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2555 sparc_smul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
2556 sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2560 sparc_wry (code, sparc_g0, sparc_g0);
2561 sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, sparc_o7);
2562 sparc_umul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
2563 sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2567 /* Sign extend sreg1 into %y */
2568 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2569 sparc_wry (code, sparc_o7, sparc_g0);
2570 if (!sparc_is_imm13 (ins->inst_imm)) {
2571 sparc_set (code, ins->inst_imm, GP_SCRATCH_REG);
2572 sparc_sdiv (code, TRUE, ins->sreg1, GP_SCRATCH_REG, sparc_o7);
2573 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2574 sparc_smul (code, FALSE, sparc_o7, GP_SCRATCH_REG, sparc_o7);
2577 sparc_sdiv_imm (code, TRUE, ins->sreg1, ins->inst_imm, sparc_o7);
2578 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2579 sparc_smul_imm (code, FALSE, sparc_o7, ins->inst_imm, sparc_o7);
2581 sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2585 sparc_or (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2589 EMIT_ALU_IMM (ins, or, FALSE);
2593 sparc_xor (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2597 EMIT_ALU_IMM (ins, xor, FALSE);
2601 sparc_sll (code, ins->sreg1, ins->sreg2, ins->dreg);
2605 if (ins->inst_imm < (1 << 5))
2606 sparc_sll_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2608 sparc_set (code, ins->inst_imm, sparc_o7);
2609 sparc_sll (code, ins->sreg1, sparc_o7, ins->dreg);
2614 sparc_sra (code, ins->sreg1, ins->sreg2, ins->dreg);
2618 if (ins->inst_imm < (1 << 5))
2619 sparc_sra_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2621 sparc_set (code, ins->inst_imm, sparc_o7);
2622 sparc_sra (code, ins->sreg1, sparc_o7, ins->dreg);
2626 case OP_ISHR_UN_IMM:
2627 if (ins->inst_imm < (1 << 5))
2628 sparc_srl_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2630 sparc_set (code, ins->inst_imm, sparc_o7);
2631 sparc_srl (code, ins->sreg1, sparc_o7, ins->dreg);
2636 sparc_srl (code, ins->sreg1, ins->sreg2, ins->dreg);
2639 sparc_sllx (code, ins->sreg1, ins->sreg2, ins->dreg);
2642 if (ins->inst_imm < (1 << 6))
2643 sparc_sllx_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2645 sparc_set (code, ins->inst_imm, sparc_o7);
2646 sparc_sllx (code, ins->sreg1, sparc_o7, ins->dreg);
2650 sparc_srax (code, ins->sreg1, ins->sreg2, ins->dreg);
2653 if (ins->inst_imm < (1 << 6))
2654 sparc_srax_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2656 sparc_set (code, ins->inst_imm, sparc_o7);
2657 sparc_srax (code, ins->sreg1, sparc_o7, ins->dreg);
2661 sparc_srlx (code, ins->sreg1, ins->sreg2, ins->dreg);
2663 case OP_LSHR_UN_IMM:
2664 if (ins->inst_imm < (1 << 6))
2665 sparc_srlx_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2667 sparc_set (code, ins->inst_imm, sparc_o7);
2668 sparc_srlx (code, ins->sreg1, sparc_o7, ins->dreg);
2673 /* can't use sparc_not */
2674 sparc_xnor (code, FALSE, ins->sreg1, sparc_g0, ins->dreg);
2678 /* can't use sparc_neg */
2679 sparc_sub (code, FALSE, sparc_g0, ins->sreg1, ins->dreg);
2683 sparc_smul (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2689 if ((ins->inst_imm == 1) && (ins->sreg1 == ins->dreg))
2692 /* Transform multiplication into a shift */
2693 for (i = 0; i < 30; ++i) {
2695 if (ins->inst_imm == imm)
2699 sparc_sll_imm (code, ins->sreg1, i, ins->dreg);
2701 EMIT_ALU_IMM (ins, smul, FALSE);
2706 sparc_smul (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2707 sparc_rdy (code, sparc_g1);
2708 sparc_sra_imm (code, ins->dreg, 31, sparc_o7);
2709 sparc_cmp (code, sparc_g1, sparc_o7);
2710 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (ins, sparc_bne, "OverflowException", TRUE, sparc_icc_short);
2712 case CEE_MUL_OVF_UN:
2713 case OP_IMUL_OVF_UN:
2714 sparc_umul (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2715 sparc_rdy (code, sparc_o7);
2716 sparc_cmp (code, sparc_o7, sparc_g0);
2717 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (ins, sparc_bne, "OverflowException", TRUE, sparc_icc_short);
2720 sparc_set (code, ins->inst_c0, ins->dreg);
2723 sparc_set (code, ins->inst_l, ins->dreg);
2726 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2727 sparc_set_template (code, ins->dreg);
2732 if (ins->sreg1 != ins->dreg)
2733 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
2736 /* Only used on V9 */
2737 if (ins->sreg1 != ins->dreg)
2738 sparc_fmovd (code, ins->sreg1, ins->dreg);
2740 case OP_SPARC_SETFREG_FLOAT:
2741 /* Only used on V9 */
2742 sparc_fdtos (code, ins->sreg1, ins->dreg);
2745 if (cfg->method->save_lmf)
2748 code = emit_load_volatile_arguments (cfg, code);
2749 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2750 sparc_set_template (code, sparc_o7);
2751 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_g0);
2752 /* Restore parent frame in delay slot */
2753 sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
2756 /* ensure ins->sreg1 is not NULL */
2757 sparc_ld_imm (code, ins->sreg1, 0, sparc_g0);
2760 sparc_add_imm (code, FALSE, sparc_fp, cfg->sig_cookie, sparc_o7);
2761 sparc_sti_imm (code, sparc_o7, ins->sreg1, 0);
2768 call = (MonoCallInst*)ins;
2769 g_assert (!call->virtual);
2770 code = emit_save_sp_to_lmf (cfg, code);
2771 if (ins->flags & MONO_INST_HAS_METHOD)
2772 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method);
2774 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
2776 code = emit_vret_token (ins, code);
2777 code = emit_move_return_value (ins, code);
2782 case OP_VOIDCALL_REG:
2784 call = (MonoCallInst*)ins;
2785 code = emit_save_sp_to_lmf (cfg, code);
2786 sparc_jmpl (code, ins->sreg1, sparc_g0, sparc_callsite);
2788 * We emit a special kind of nop in the delay slot to tell the
2789 * trampoline code that this is a virtual call, thus an unbox
2790 * trampoline might need to be called.
2793 sparc_or_imm (code, FALSE, sparc_g0, 0xca, sparc_g0);
2797 code = emit_vret_token (ins, code);
2798 code = emit_move_return_value (ins, code);
2800 case OP_FCALL_MEMBASE:
2801 case OP_LCALL_MEMBASE:
2802 case OP_VCALL_MEMBASE:
2803 case OP_VOIDCALL_MEMBASE:
2804 case OP_CALL_MEMBASE:
2805 call = (MonoCallInst*)ins;
2806 code = emit_save_sp_to_lmf (cfg, code);
2807 if (sparc_is_imm13 (ins->inst_offset)) {
2808 sparc_ldi_imm (code, ins->inst_basereg, ins->inst_offset, sparc_o7);
2810 sparc_set (code, ins->inst_offset, sparc_o7);
2811 sparc_ldi (code, ins->inst_basereg, sparc_o7, sparc_o7);
2813 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
2815 sparc_or_imm (code, FALSE, sparc_g0, 0xca, sparc_g0);
2819 code = emit_vret_token (ins, code);
2820 code = emit_move_return_value (ins, code);
2823 if (mono_method_signature (cfg->method)->ret->type == MONO_TYPE_R4)
2824 sparc_fdtos (code, ins->sreg1, sparc_f0);
2827 sparc_fmovd (code, ins->sreg1, ins->dreg);
2829 /* FIXME: Why not use fmovd ? */
2830 sparc_fmovs (code, ins->sreg1, ins->dreg);
2831 sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
2836 g_assert_not_reached ();
2841 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2842 /* Perform stack touching */
2846 /* Keep alignment */
2847 sparc_add_imm (code, FALSE, ins->sreg1, MONO_ARCH_LOCALLOC_ALIGNMENT - 1, ins->dreg);
2848 sparc_set (code, ~(MONO_ARCH_LOCALLOC_ALIGNMENT - 1), sparc_o7);
2849 sparc_and (code, FALSE, ins->dreg, sparc_o7, ins->dreg);
2851 if ((ins->flags & MONO_INST_INIT) && (ins->sreg1 == ins->dreg)) {
2853 size_reg = sparc_g4;
2855 size_reg = sparc_g1;
2857 sparc_mov_reg_reg (code, ins->dreg, size_reg);
2860 size_reg = ins->sreg1;
2862 sparc_sub (code, FALSE, sparc_sp, ins->dreg, ins->dreg);
2863 /* Keep %sp valid at all times */
2864 sparc_mov_reg_reg (code, ins->dreg, sparc_sp);
2865 g_assert (sparc_is_imm13 (MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset));
2866 sparc_add_imm (code, FALSE, ins->dreg, MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset, ins->dreg);
2868 if (ins->flags & MONO_INST_INIT) {
2870 /* Initialize memory region */
2871 sparc_cmp_imm (code, size_reg, 0);
2873 sparc_branch (code, 0, sparc_be, 0);
2875 sparc_set (code, 0, sparc_o7);
2876 sparc_sub_imm (code, 0, size_reg, sparcv9 ? 8 : 4, size_reg);
2880 sparc_stx (code, sparc_g0, ins->dreg, sparc_o7);
2882 sparc_st (code, sparc_g0, ins->dreg, sparc_o7);
2883 sparc_cmp (code, sparc_o7, size_reg);
2885 sparc_branch (code, 0, sparc_bl, 0);
2886 sparc_patch (br [2], br [1]);
2888 sparc_add_imm (code, 0, sparc_o7, sparcv9 ? 8 : 4, sparc_o7);
2889 sparc_patch (br [0], code);
2893 case OP_SPARC_LOCALLOC_IMM: {
2894 gint32 offset = ins->inst_c0;
2896 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2897 /* Perform stack touching */
2901 offset = ALIGN_TO (offset, MONO_ARCH_LOCALLOC_ALIGNMENT);
2902 if (sparc_is_imm13 (offset))
2903 sparc_sub_imm (code, FALSE, sparc_sp, offset, sparc_sp);
2905 sparc_set (code, offset, sparc_o7);
2906 sparc_sub (code, FALSE, sparc_sp, sparc_o7, sparc_sp);
2908 g_assert (sparc_is_imm13 (MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset));
2909 sparc_add_imm (code, FALSE, sparc_sp, MONO_SPARC_STACK_BIAS + cfg->arch.localloc_offset, ins->dreg);
2910 if ((ins->flags & MONO_INST_INIT) && (offset > 0)) {
2916 while (i < offset) {
2918 sparc_stx_imm (code, sparc_g0, ins->dreg, i);
2922 sparc_st_imm (code, sparc_g0, ins->dreg, i);
2928 sparc_set (code, offset, sparc_o7);
2929 sparc_sub_imm (code, 0, sparc_o7, sparcv9 ? 8 : 4, sparc_o7);
2930 /* beginning of loop */
2933 sparc_stx (code, sparc_g0, ins->dreg, sparc_o7);
2935 sparc_st (code, sparc_g0, ins->dreg, sparc_o7);
2936 sparc_cmp_imm (code, sparc_o7, 0);
2938 sparc_branch (code, 0, sparc_bne, 0);
2940 sparc_sub_imm (code, 0, sparc_o7, sparcv9 ? 8 : 4, sparc_o7);
2941 sparc_patch (br [1], br [0]);
2947 /* The return is done in the epilog */
2948 g_assert_not_reached ();
2951 sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
2952 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2953 (gpointer)"mono_arch_throw_exception");
2957 sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
2958 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2959 (gpointer)"mono_arch_rethrow_exception");
2962 case OP_START_HANDLER: {
2964 * The START_HANDLER instruction marks the beginning of a handler
2965 * block. It is called using a call instruction, so %o7 contains
2966 * the return address. Since the handler executes in the same stack
2967 * frame as the method itself, we can't use save/restore to save
2968 * the return address. Instead, we save it into a dedicated
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_sti (code, sparc_o7, spvar->inst_basereg, GP_SCRATCH_REG);
2977 sparc_sti_imm (code, sparc_o7, spvar->inst_basereg, spvar->inst_offset);
2980 case OP_ENDFILTER: {
2981 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
2982 if (!sparc_is_imm13 (spvar->inst_offset)) {
2983 sparc_set (code, spvar->inst_offset, GP_SCRATCH_REG);
2984 sparc_ldi (code, spvar->inst_basereg, GP_SCRATCH_REG, sparc_o7);
2987 sparc_ldi_imm (code, spvar->inst_basereg, spvar->inst_offset, sparc_o7);
2988 sparc_jmpl_imm (code, sparc_o7, 8, sparc_g0);
2990 sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
2993 case CEE_ENDFINALLY: {
2994 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
2995 if (!sparc_is_imm13 (spvar->inst_offset)) {
2996 sparc_set (code, spvar->inst_offset, GP_SCRATCH_REG);
2997 sparc_ldi (code, spvar->inst_basereg, GP_SCRATCH_REG, sparc_o7);
3000 sparc_ldi_imm (code, spvar->inst_basereg, spvar->inst_offset, sparc_o7);
3001 sparc_jmpl_imm (code, sparc_o7, 8, sparc_g0);
3005 case OP_CALL_HANDLER:
3006 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3007 /* This is a jump inside the method, so call_simple works even on V9 */
3008 sparc_call_simple (code, 0);
3012 ins->inst_c0 = (guint8*)code - cfg->native_code;
3015 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
3016 if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
3018 if (ins->flags & MONO_INST_BRLABEL) {
3019 if (ins->inst_i0->inst_c0) {
3020 gint32 disp = (ins->inst_i0->inst_c0 - ((guint8*)code - cfg->native_code)) >> 2;
3021 g_assert (sparc_is_imm22 (disp));
3022 sparc_branch (code, 1, sparc_ba, disp);
3024 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
3025 sparc_branch (code, 1, sparc_ba, 0);
3028 if (ins->inst_target_bb->native_offset) {
3029 gint32 disp = (ins->inst_target_bb->native_offset - ((guint8*)code - cfg->native_code)) >> 2;
3030 g_assert (sparc_is_imm22 (disp));
3031 sparc_branch (code, 1, sparc_ba, disp);
3033 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3034 sparc_branch (code, 1, sparc_ba, 0);
3040 sparc_jmp (code, ins->sreg1, sparc_g0);
3048 if (v64 && (cfg->opt & MONO_OPT_CMOV)) {
3049 sparc_clr_reg (code, ins->dreg);
3050 sparc_movcc_imm (code, sparc_xcc, opcode_to_sparc_cond (ins->opcode), 1, ins->dreg);
3053 sparc_clr_reg (code, ins->dreg);
3055 sparc_branchp (code, 1, opcode_to_sparc_cond (ins->opcode), DEFAULT_ICC, 0, 2);
3057 sparc_branch (code, 1, opcode_to_sparc_cond (ins->opcode), 2);
3060 sparc_set (code, 1, ins->dreg);
3068 if (v64 && (cfg->opt & MONO_OPT_CMOV)) {
3069 sparc_clr_reg (code, ins->dreg);
3070 sparc_movcc_imm (code, sparc_icc, opcode_to_sparc_cond (ins->opcode), 1, ins->dreg);
3073 sparc_clr_reg (code, ins->dreg);
3074 sparc_branchp (code, 1, opcode_to_sparc_cond (ins->opcode), sparc_icc_short, 0, 2);
3076 sparc_set (code, 1, ins->dreg);
3079 case OP_COND_EXC_EQ:
3080 case OP_COND_EXC_NE_UN:
3081 case OP_COND_EXC_LT:
3082 case OP_COND_EXC_LT_UN:
3083 case OP_COND_EXC_GT:
3084 case OP_COND_EXC_GT_UN:
3085 case OP_COND_EXC_GE:
3086 case OP_COND_EXC_GE_UN:
3087 case OP_COND_EXC_LE:
3088 case OP_COND_EXC_LE_UN:
3089 case OP_COND_EXC_OV:
3090 case OP_COND_EXC_NO:
3092 case OP_COND_EXC_NC:
3093 EMIT_COND_SYSTEM_EXCEPTION (ins, opcode_to_sparc_cond (ins->opcode), ins->inst_p1);
3095 case OP_SPARC_COND_EXC_EQZ:
3096 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brz, ins->inst_p1);
3098 case OP_SPARC_COND_EXC_GEZ:
3099 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brgez, ins->inst_p1);
3101 case OP_SPARC_COND_EXC_GTZ:
3102 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brgz, ins->inst_p1);
3104 case OP_SPARC_COND_EXC_LEZ:
3105 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brlez, ins->inst_p1);
3107 case OP_SPARC_COND_EXC_LTZ:
3108 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brlz, ins->inst_p1);
3110 case OP_SPARC_COND_EXC_NEZ:
3111 EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brnz, ins->inst_p1);
3113 case OP_COND_EXC_IOV:
3114 case OP_COND_EXC_IC:
3115 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (ins, opcode_to_sparc_cond (ins->opcode), ins->inst_p1, TRUE, sparc_icc_short);
3128 EMIT_COND_BRANCH_PREDICTED (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
3130 EMIT_COND_BRANCH (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
3144 /* Only used on V9 */
3145 EMIT_COND_BRANCH_ICC (ins, opcode_to_sparc_cond (ins->opcode), 1, 1, sparc_icc_short);
3150 EMIT_COND_BRANCH_BPR (ins, brz, 1, 1, 1);
3152 case OP_SPARC_BRLEZ:
3153 EMIT_COND_BRANCH_BPR (ins, brlez, 1, 1, 1);
3156 EMIT_COND_BRANCH_BPR (ins, brlz, 1, 1, 1);
3159 EMIT_COND_BRANCH_BPR (ins, brnz, 1, 1, 1);
3162 EMIT_COND_BRANCH_BPR (ins, brgz, 1, 1, 1);
3164 case OP_SPARC_BRGEZ:
3165 EMIT_COND_BRANCH_BPR (ins, brgez, 1, 1, 1);
3168 /* floating point opcodes */
3170 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
3172 sparc_set_template (code, sparc_o7);
3174 sparc_sethi (code, 0, sparc_o7);
3176 sparc_lddf_imm (code, sparc_o7, 0, ins->dreg);
3179 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
3181 sparc_set_template (code, sparc_o7);
3183 sparc_sethi (code, 0, sparc_o7);
3185 sparc_ldf_imm (code, sparc_o7, 0, FP_SCRATCH_REG);
3187 /* Extend to double */
3188 sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
3190 case OP_STORER8_MEMBASE_REG:
3191 if (!sparc_is_imm13 (ins->inst_offset + 4)) {
3192 sparc_set (code, ins->inst_offset, sparc_o7);
3193 /* SPARCV9 handles misaligned fp loads/stores */
3194 if (!v64 && (ins->inst_offset % 8)) {
3196 sparc_add (code, FALSE, ins->inst_destbasereg, sparc_o7, sparc_o7);
3197 sparc_stf (code, ins->sreg1, sparc_o7, sparc_g0);
3198 sparc_stf_imm (code, ins->sreg1 + 1, sparc_o7, 4);
3200 sparc_stdf (code, ins->sreg1, ins->inst_destbasereg, sparc_o7);
3203 if (!v64 && (ins->inst_offset % 8)) {
3205 sparc_stf_imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3206 sparc_stf_imm (code, ins->sreg1 + 1, ins->inst_destbasereg, ins->inst_offset + 4);
3208 sparc_stdf_imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3211 case OP_LOADR8_MEMBASE:
3212 EMIT_LOAD_MEMBASE (ins, lddf);
3214 case OP_STORER4_MEMBASE_REG:
3215 /* This requires a double->single conversion */
3216 sparc_fdtos (code, ins->sreg1, FP_SCRATCH_REG);
3217 if (!sparc_is_imm13 (ins->inst_offset)) {
3218 sparc_set (code, ins->inst_offset, sparc_o7);
3219 sparc_stf (code, FP_SCRATCH_REG, ins->inst_destbasereg, sparc_o7);
3222 sparc_stf_imm (code, FP_SCRATCH_REG, ins->inst_destbasereg, ins->inst_offset);
3224 case OP_LOADR4_MEMBASE: {
3225 /* ldf needs a single precision register */
3226 int dreg = ins->dreg;
3227 ins->dreg = FP_SCRATCH_REG;
3228 EMIT_LOAD_MEMBASE (ins, ldf);
3230 /* Extend to double */
3231 sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
3236 sparc_fmovd (code, ins->sreg1, ins->dreg);
3238 sparc_fmovs (code, ins->sreg1, ins->dreg);
3239 sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
3243 gint32 offset = mono_spillvar_offset_float (cfg, 0);
3245 if (!sparc_is_imm13 (offset)) {
3246 sparc_set (code, offset, sparc_o7);
3247 sparc_stx (code, ins->sreg1, sparc_sp, offset);
3248 sparc_lddf (code, sparc_sp, offset, FP_SCRATCH_REG);
3250 sparc_stx_imm (code, ins->sreg1, sparc_sp, offset);
3251 sparc_lddf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3253 sparc_fxtos (code, FP_SCRATCH_REG, FP_SCRATCH_REG);
3255 if (!sparc_is_imm13 (offset)) {
3256 sparc_set (code, offset, sparc_o7);
3257 sparc_st (code, ins->sreg1, sparc_sp, sparc_o7);
3258 sparc_ldf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
3260 sparc_st_imm (code, ins->sreg1, sparc_sp, offset);
3261 sparc_ldf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3263 sparc_fitos (code, FP_SCRATCH_REG, FP_SCRATCH_REG);
3265 sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
3269 gint32 offset = mono_spillvar_offset_float (cfg, 0);
3271 if (!sparc_is_imm13 (offset)) {
3272 sparc_set (code, offset, sparc_o7);
3273 sparc_stx (code, ins->sreg1, sparc_sp, sparc_o7);
3274 sparc_lddf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
3276 sparc_stx_imm (code, ins->sreg1, sparc_sp, offset);
3277 sparc_lddf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3279 sparc_fxtod (code, FP_SCRATCH_REG, ins->dreg);
3281 if (!sparc_is_imm13 (offset)) {
3282 sparc_set (code, offset, sparc_o7);
3283 sparc_st (code, ins->sreg1, sparc_sp, sparc_o7);
3284 sparc_ldf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
3286 sparc_st_imm (code, ins->sreg1, sparc_sp, offset);
3287 sparc_ldf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3289 sparc_fitod (code, FP_SCRATCH_REG, ins->dreg);
3293 case OP_FCONV_TO_I1:
3294 case OP_FCONV_TO_U1:
3295 case OP_FCONV_TO_I2:
3296 case OP_FCONV_TO_U2:
3301 case OP_FCONV_TO_I4:
3302 case OP_FCONV_TO_U4: {
3303 gint32 offset = mono_spillvar_offset_float (cfg, 0);
3304 sparc_fdtoi (code, ins->sreg1, FP_SCRATCH_REG);
3305 if (!sparc_is_imm13 (offset)) {
3306 sparc_set (code, offset, sparc_o7);
3307 sparc_stdf (code, FP_SCRATCH_REG, sparc_sp, sparc_o7);
3308 sparc_ld (code, sparc_sp, sparc_o7, ins->dreg);
3310 sparc_stdf_imm (code, FP_SCRATCH_REG, sparc_sp, offset);
3311 sparc_ld_imm (code, sparc_sp, offset, ins->dreg);
3314 switch (ins->opcode) {
3315 case OP_FCONV_TO_I1:
3316 case OP_FCONV_TO_U1:
3317 sparc_and_imm (code, 0, ins->dreg, 0xff, ins->dreg);
3319 case OP_FCONV_TO_I2:
3320 case OP_FCONV_TO_U2:
3321 sparc_set (code, 0xffff, sparc_o7);
3322 sparc_and (code, 0, ins->dreg, sparc_o7, ins->dreg);
3329 case OP_FCONV_TO_I8:
3330 case OP_FCONV_TO_U8:
3332 g_assert_not_reached ();
3336 g_assert_not_reached ();
3338 case OP_LCONV_TO_R_UN: {
3340 g_assert_not_reached ();
3343 case OP_LCONV_TO_OVF_I: {
3344 guint32 *br [3], *label [1];
3347 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
3349 sparc_cmp_imm (code, ins->sreg1, 0);
3351 sparc_branch (code, 1, sparc_bneg, 0);
3355 /* ms word must be 0 */
3356 sparc_cmp_imm (code, ins->sreg2, 0);
3358 sparc_branch (code, 1, sparc_be, 0);
3363 EMIT_COND_SYSTEM_EXCEPTION (ins, sparc_ba, "OverflowException");
3366 sparc_patch (br [0], code);
3368 /* ms word must 0xfffffff */
3369 sparc_cmp_imm (code, ins->sreg2, -1);
3371 sparc_branch (code, 1, sparc_bne, 0);
3373 sparc_patch (br [2], label [0]);
3376 sparc_patch (br [1], code);
3377 if (ins->sreg1 != ins->dreg)
3378 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
3382 sparc_faddd (code, ins->sreg1, ins->sreg2, ins->dreg);
3385 sparc_fsubd (code, ins->sreg1, ins->sreg2, ins->dreg);
3388 sparc_fmuld (code, ins->sreg1, ins->sreg2, ins->dreg);
3391 sparc_fdivd (code, ins->sreg1, ins->sreg2, ins->dreg);
3395 sparc_fnegd (code, ins->sreg1, ins->dreg);
3397 /* FIXME: why don't use fnegd ? */
3398 sparc_fnegs (code, ins->sreg1, ins->dreg);
3402 sparc_fdivd (code, ins->sreg1, ins->sreg2, FP_SCRATCH_REG);
3403 sparc_fmuld (code, ins->sreg2, FP_SCRATCH_REG, FP_SCRATCH_REG);
3404 sparc_fsubd (code, ins->sreg1, FP_SCRATCH_REG, ins->dreg);
3407 sparc_fcmpd (code, ins->sreg1, ins->sreg2);
3414 sparc_fcmpd (code, ins->sreg1, ins->sreg2);
3415 sparc_clr_reg (code, ins->dreg);
3416 switch (ins->opcode) {
3419 sparc_fbranch (code, 1, opcode_to_sparc_cond (ins->opcode), 4);
3421 sparc_set (code, 1, ins->dreg);
3422 sparc_fbranch (code, 1, sparc_fbu, 2);
3424 sparc_set (code, 1, ins->dreg);
3427 sparc_fbranch (code, 1, opcode_to_sparc_cond (ins->opcode), 2);
3429 sparc_set (code, 1, ins->dreg);
3435 EMIT_FLOAT_COND_BRANCH (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
3438 /* clt.un + brfalse */
3440 sparc_fbranch (code, 1, sparc_fbul, 0);
3443 EMIT_FLOAT_COND_BRANCH (ins, sparc_fba, 1, 1);
3444 sparc_patch (p, (guint8*)code);
3448 /* cgt.un + brfalse */
3450 sparc_fbranch (code, 1, sparc_fbug, 0);
3453 EMIT_FLOAT_COND_BRANCH (ins, sparc_fba, 1, 1);
3454 sparc_patch (p, (guint8*)code);
3458 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbne, 1, 1);
3459 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3462 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbl, 1, 1);
3463 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3466 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbg, 1, 1);
3467 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3470 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbge, 1, 1);
3471 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3474 EMIT_FLOAT_COND_BRANCH (ins, sparc_fble, 1, 1);
3475 EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3477 case CEE_CKFINITE: {
3478 gint32 offset = mono_spillvar_offset_float (cfg, 0);
3479 if (!sparc_is_imm13 (offset)) {
3480 sparc_set (code, offset, sparc_o7);
3481 sparc_stdf (code, ins->sreg1, sparc_sp, sparc_o7);
3482 sparc_lduh (code, sparc_sp, sparc_o7, sparc_o7);
3484 sparc_stdf_imm (code, ins->sreg1, sparc_sp, offset);
3485 sparc_lduh_imm (code, sparc_sp, offset, sparc_o7);
3487 sparc_srl_imm (code, sparc_o7, 4, sparc_o7);
3488 sparc_and_imm (code, FALSE, sparc_o7, 2047, sparc_o7);
3489 sparc_cmp_imm (code, sparc_o7, 2047);
3490 EMIT_COND_SYSTEM_EXCEPTION (ins, sparc_be, "ArithmeticException");
3492 sparc_fmovd (code, ins->sreg1, ins->dreg);
3494 sparc_fmovs (code, ins->sreg1, ins->dreg);
3495 sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
3500 case OP_MEMORY_BARRIER:
3501 sparc_membar (code, sparc_membar_all);
3506 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3508 g_warning ("%s:%d: unknown opcode %s\n", __FILE__, __LINE__, mono_inst_name (ins->opcode));
3510 g_assert_not_reached ();
3513 if ((((guint8*)code) - code_start) > max_len) {
3514 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3515 mono_inst_name (ins->opcode), max_len, ((guint8*)code) - code_start);
3516 g_assert_not_reached ();
3526 cfg->code_len = (guint8*)code - cfg->native_code;
3530 mono_arch_register_lowlevel_calls (void)
3532 mono_register_jit_icall (mono_arch_break, "mono_arch_break", NULL, TRUE);
3533 mono_register_jit_icall (mono_arch_get_lmf_addr, "mono_arch_get_lmf_addr", NULL, TRUE);
3537 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3539 MonoJumpInfo *patch_info;
3541 /* FIXME: Move part of this to arch independent code */
3542 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3543 unsigned char *ip = patch_info->ip.i + code;
3546 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
3548 switch (patch_info->type) {
3549 case MONO_PATCH_INFO_NONE:
3551 case MONO_PATCH_INFO_CLASS_INIT: {
3552 guint32 *ip2 = (guint32*)ip;
3553 /* Might already been changed to a nop */
3555 sparc_set_template (ip2, sparc_o7);
3556 sparc_jmpl (ip2, sparc_o7, sparc_g0, sparc_o7);
3558 sparc_call_simple (ip2, 0);
3562 case MONO_PATCH_INFO_METHOD_JUMP: {
3563 guint32 *ip2 = (guint32*)ip;
3564 /* Might already been patched */
3565 sparc_set_template (ip2, sparc_o7);
3571 sparc_patch ((guint32*)ip, target);
3576 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
3579 guint32 *code = (guint32*)p;
3580 MonoMethodSignature *sig = mono_method_signature (cfg->method);
3583 /* Save registers to stack */
3584 for (i = 0; i < 6; ++i)
3585 sparc_sti_imm (code, sparc_i0 + i, sparc_fp, ARGS_OFFSET + (i * sizeof (gpointer)));
3587 cinfo = get_call_info (sig, FALSE);
3589 /* Save float regs on V9, since they are caller saved */
3590 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3591 ArgInfo *ainfo = cinfo->args + i;
3592 gint32 stack_offset;
3594 stack_offset = ainfo->offset + ARGS_OFFSET;
3596 if (ainfo->storage == ArgInFloatReg) {
3597 if (!sparc_is_imm13 (stack_offset))
3599 sparc_stf_imm (code, ainfo->reg, sparc_fp, stack_offset);
3601 else if (ainfo->storage == ArgInDoubleReg) {
3602 /* The offset is guaranteed to be aligned by the ABI rules */
3603 sparc_stdf_imm (code, ainfo->reg, sparc_fp, stack_offset);
3607 sparc_set (code, cfg->method, sparc_o0);
3608 sparc_add_imm (code, FALSE, sparc_fp, MONO_SPARC_STACK_BIAS, sparc_o1);
3610 mono_add_patch_info (cfg, (guint8*)code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
3613 /* Restore float regs on V9 */
3614 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3615 ArgInfo *ainfo = cinfo->args + i;
3616 gint32 stack_offset;
3618 stack_offset = ainfo->offset + ARGS_OFFSET;
3620 if (ainfo->storage == ArgInFloatReg) {
3621 if (!sparc_is_imm13 (stack_offset))
3623 sparc_ldf_imm (code, sparc_fp, stack_offset, ainfo->reg);
3625 else if (ainfo->storage == ArgInDoubleReg) {
3626 /* The offset is guaranteed to be aligned by the ABI rules */
3627 sparc_lddf_imm (code, sparc_fp, stack_offset, ainfo->reg);
3645 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
3647 guint32 *code = (guint32*)p;
3648 int save_mode = SAVE_NONE;
3649 MonoMethod *method = cfg->method;
3651 switch (mono_type_get_underlying_type (mono_method_signature (method)->ret)->type) {
3652 case MONO_TYPE_VOID:
3653 /* special case string .ctor icall */
3654 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
3655 save_mode = SAVE_ONE;
3657 save_mode = SAVE_NONE;
3662 save_mode = SAVE_ONE;
3664 save_mode = SAVE_TWO;
3669 save_mode = SAVE_FP;
3671 case MONO_TYPE_VALUETYPE:
3672 save_mode = SAVE_STRUCT;
3675 save_mode = SAVE_ONE;
3679 /* Save the result to the stack and also put it into the output registers */
3681 switch (save_mode) {
3684 sparc_st_imm (code, sparc_i0, sparc_fp, 68);
3685 sparc_st_imm (code, sparc_i0, sparc_fp, 72);
3686 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
3687 sparc_mov_reg_reg (code, sparc_i1, sparc_o2);
3690 sparc_sti_imm (code, sparc_i0, sparc_fp, ARGS_OFFSET);
3691 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
3695 sparc_stdf_imm (code, sparc_f0, sparc_fp, ARGS_OFFSET);
3697 sparc_stdf_imm (code, sparc_f0, sparc_fp, 72);
3698 sparc_ld_imm (code, sparc_fp, 72, sparc_o1);
3699 sparc_ld_imm (code, sparc_fp, 72 + 4, sparc_o2);
3704 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
3706 sparc_ld_imm (code, sparc_fp, 64, sparc_o1);
3714 sparc_set (code, cfg->method, sparc_o0);
3716 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, func);
3719 /* Restore result */
3721 switch (save_mode) {
3723 sparc_ld_imm (code, sparc_fp, 68, sparc_i0);
3724 sparc_ld_imm (code, sparc_fp, 72, sparc_i0);
3727 sparc_ldi_imm (code, sparc_fp, ARGS_OFFSET, sparc_i0);
3730 sparc_lddf_imm (code, sparc_fp, ARGS_OFFSET, sparc_f0);
3741 mono_arch_emit_prolog (MonoCompile *cfg)
3743 MonoMethod *method = cfg->method;
3744 MonoMethodSignature *sig;
3750 cfg->code_size = 256;
3751 cfg->native_code = g_malloc (cfg->code_size);
3752 code = (guint32*)cfg->native_code;
3754 /* FIXME: Generate intermediate code instead */
3756 offset = cfg->stack_offset;
3757 offset += (16 * sizeof (gpointer)); /* register save area */
3759 offset += 4; /* struct/union return pointer */
3762 /* add parameter area size for called functions */
3763 if (cfg->param_area < (6 * sizeof (gpointer)))
3764 /* Reserve space for the first 6 arguments even if it is unused */
3765 offset += 6 * sizeof (gpointer);
3767 offset += cfg->param_area;
3769 /* align the stack size */
3770 offset = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
3773 * localloc'd memory is stored between the local variables (whose
3774 * size is given by cfg->stack_offset), and between the space reserved
3777 cfg->arch.localloc_offset = offset - cfg->stack_offset;
3779 cfg->stack_offset = offset;
3781 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
3782 /* Perform stack touching */
3786 if (!sparc_is_imm13 (- cfg->stack_offset)) {
3787 /* Can't use sparc_o7 here, since we're still in the caller's frame */
3788 sparc_set (code, (- cfg->stack_offset), GP_SCRATCH_REG);
3789 sparc_save (code, sparc_sp, GP_SCRATCH_REG, sparc_sp);
3792 sparc_save_imm (code, sparc_sp, - cfg->stack_offset, sparc_sp);
3795 if (strstr (cfg->method->name, "foo")) {
3796 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, mono_sparc_break);
3797 sparc_call_simple (code, 0);
3802 sig = mono_method_signature (method);
3804 cinfo = get_call_info (sig, FALSE);
3806 /* Keep in sync with emit_load_volatile_arguments */
3807 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3808 ArgInfo *ainfo = cinfo->args + i;
3809 gint32 stack_offset;
3811 inst = cfg->varinfo [i];
3813 if (sig->hasthis && (i == 0))
3814 arg_type = &mono_defaults.object_class->byval_arg;
3816 arg_type = sig->params [i - sig->hasthis];
3818 stack_offset = ainfo->offset + ARGS_OFFSET;
3820 /* Save the split arguments so they will reside entirely on the stack */
3821 if (ainfo->storage == ArgInSplitRegStack) {
3822 /* Save the register to the stack */
3823 g_assert (inst->opcode == OP_REGOFFSET);
3824 if (!sparc_is_imm13 (stack_offset))
3826 sparc_st_imm (code, sparc_i5, inst->inst_basereg, stack_offset);
3829 if (!v64 && !arg_type->byref && (arg_type->type == MONO_TYPE_R8)) {
3830 /* Save the argument to a dword aligned stack location */
3832 * stack_offset contains the offset of the argument on the stack.
3833 * inst->inst_offset contains the dword aligned offset where the value
3836 if (ainfo->storage == ArgInIRegPair) {
3837 if (!sparc_is_imm13 (inst->inst_offset + 4))
3839 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, inst->inst_offset);
3840 sparc_st_imm (code, sparc_i0 + ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
3843 if (ainfo->storage == ArgInSplitRegStack) {
3845 g_assert_not_reached ();
3847 if (stack_offset != inst->inst_offset) {
3848 /* stack_offset is not dword aligned, so we need to make a copy */
3849 sparc_st_imm (code, sparc_i5, inst->inst_basereg, inst->inst_offset);
3850 sparc_ld_imm (code, sparc_fp, stack_offset + 4, sparc_o7);
3851 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset + 4);
3855 if (ainfo->storage == ArgOnStackPair) {
3857 g_assert_not_reached ();
3859 if (stack_offset != inst->inst_offset) {
3860 /* stack_offset is not dword aligned, so we need to make a copy */
3861 sparc_ld_imm (code, sparc_fp, stack_offset, sparc_o7);
3862 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset);
3863 sparc_ld_imm (code, sparc_fp, stack_offset + 4, sparc_o7);
3864 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset + 4);
3868 g_assert_not_reached ();
3871 if ((ainfo->storage == ArgInIReg) && (inst->opcode != OP_REGVAR)) {
3872 /* Argument in register, but need to be saved to stack */
3873 if (!sparc_is_imm13 (stack_offset))
3875 if ((stack_offset - ARGS_OFFSET) & 0x1)
3876 sparc_stb_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
3878 if ((stack_offset - ARGS_OFFSET) & 0x2)
3879 sparc_sth_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
3881 if ((stack_offset - ARGS_OFFSET) & 0x4)
3882 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
3885 sparc_stx_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
3887 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
3891 if ((ainfo->storage == ArgInIRegPair) && (inst->opcode != OP_REGVAR)) {
3895 /* Argument in regpair, but need to be saved to stack */
3896 if (!sparc_is_imm13 (inst->inst_offset + 4))
3898 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, inst->inst_offset);
3899 sparc_st_imm (code, sparc_i0 + ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
3901 else if ((ainfo->storage == ArgInFloatReg) && (inst->opcode != OP_REGVAR)) {
3902 if (!sparc_is_imm13 (stack_offset))
3904 sparc_stf_imm (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
3906 else if ((ainfo->storage == ArgInDoubleReg) && (inst->opcode != OP_REGVAR)) {
3907 /* The offset is guaranteed to be aligned by the ABI rules */
3908 sparc_stdf_imm (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
3911 if ((ainfo->storage == ArgInFloatReg) && (inst->opcode == OP_REGVAR)) {
3912 /* Need to move into the a double precision register */
3913 sparc_fstod (code, ainfo->reg, ainfo->reg - 1);
3916 if ((ainfo->storage == ArgInSplitRegStack) || (ainfo->storage == ArgOnStack))
3917 if (inst->opcode == OP_REGVAR)
3918 /* FIXME: Load the argument into memory */
3924 if (cfg->method->save_lmf) {
3925 gint32 lmf_offset = STACK_BIAS - cfg->arch.lmf_offset;
3928 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3929 sparc_set_template (code, sparc_o7);
3930 sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ip));
3932 sparc_sti_imm (code, sparc_sp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, sp));
3934 sparc_sti_imm (code, sparc_fp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp));
3936 /* FIXME: add a relocation for this */
3937 sparc_set (code, cfg->method, sparc_o7);
3938 sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method));
3940 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3941 (gpointer)"mono_arch_get_lmf_addr");
3944 code = (guint32*)mono_sparc_emit_save_lmf (code, lmf_offset);
3947 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3948 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
3950 cfg->code_len = (guint8*)code - cfg->native_code;
3952 g_assert (cfg->code_len <= cfg->code_size);
3954 return (guint8*)code;
3958 mono_arch_emit_epilog (MonoCompile *cfg)
3960 MonoMethod *method = cfg->method;
3963 int max_epilog_size = 16 + 20 * 4;
3965 if (cfg->method->save_lmf)
3966 max_epilog_size += 128;
3968 if (mono_jit_trace_calls != NULL)
3969 max_epilog_size += 50;
3971 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3972 max_epilog_size += 50;
3974 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
3975 cfg->code_size *= 2;
3976 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3977 mono_jit_stats.code_reallocs++;
3980 code = (guint32*)(cfg->native_code + cfg->code_len);
3982 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3983 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
3985 if (cfg->method->save_lmf) {
3986 gint32 lmf_offset = STACK_BIAS - cfg->arch.lmf_offset;
3988 code = mono_sparc_emit_restore_lmf (code, lmf_offset);
3992 * The V8 ABI requires that calls to functions which return a structure
3995 if (!v64 && mono_method_signature (cfg->method)->pinvoke && MONO_TYPE_ISSTRUCT(mono_method_signature (cfg->method)->ret))
3996 sparc_jmpl_imm (code, sparc_i7, 12, sparc_g0);
4000 /* Only fold last instruction into the restore if the exit block has an in count of 1
4001 and the previous block hasn't been optimized away since it may have an in count > 1 */
4002 if (cfg->bb_exit->in_count == 1 && cfg->bb_exit->in_bb[0]->native_offset != cfg->bb_exit->native_offset)
4005 /* Try folding last instruction into the restore */
4006 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)) {
4007 /* or reg, imm, %i0 */
4008 int reg = sparc_inst_rs1 (code [-2]);
4009 int imm = sparc_inst_imm13 (code [-2]);
4010 code [-2] = code [-1];
4012 sparc_restore_imm (code, reg, imm, sparc_o0);
4015 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)) {
4016 /* or reg, reg, %i0 */
4017 int reg1 = sparc_inst_rs1 (code [-2]);
4018 int reg2 = sparc_inst_rs2 (code [-2]);
4019 code [-2] = code [-1];
4021 sparc_restore (code, reg1, reg2, sparc_o0);
4024 sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
4026 cfg->code_len = (guint8*)code - cfg->native_code;
4028 g_assert (cfg->code_len < cfg->code_size);
4033 mono_arch_emit_exceptions (MonoCompile *cfg)
4035 MonoJumpInfo *patch_info;
4040 MonoClass *exc_classes [16];
4041 guint8 *exc_throw_start [16], *exc_throw_end [16];
4043 /* Compute needed space */
4044 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4045 if (patch_info->type == MONO_PATCH_INFO_EXC)
4050 * make sure we have enough space for exceptions
4053 code_size = exc_count * (20 * 4);
4055 code_size = exc_count * 24;
4058 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
4059 cfg->code_size *= 2;
4060 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4061 mono_jit_stats.code_reallocs++;
4064 code = (guint32*)(cfg->native_code + cfg->code_len);
4066 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4067 switch (patch_info->type) {
4068 case MONO_PATCH_INFO_EXC: {
4069 MonoClass *exc_class;
4070 guint32 *buf, *buf2;
4071 guint32 throw_ip, type_idx;
4074 sparc_patch ((guint32*)(cfg->native_code + patch_info->ip.i), code);
4076 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
4077 type_idx = exc_class->type_token - MONO_TOKEN_TYPE_DEF;
4078 g_assert (exc_class);
4079 throw_ip = patch_info->ip.i;
4081 /* Find a throw sequence for the same exception class */
4082 for (i = 0; i < nthrows; ++i)
4083 if (exc_classes [i] == exc_class)
4087 guint32 throw_offset = (((guint8*)exc_throw_end [i] - cfg->native_code) - throw_ip) >> 2;
4088 if (!sparc_is_imm13 (throw_offset))
4089 sparc_set32 (code, throw_offset, sparc_o1);
4091 disp = (exc_throw_start [i] - (guint8*)code) >> 2;
4092 g_assert (sparc_is_imm22 (disp));
4093 sparc_branch (code, 0, sparc_ba, disp);
4094 if (sparc_is_imm13 (throw_offset))
4095 sparc_set32 (code, throw_offset, sparc_o1);
4098 patch_info->type = MONO_PATCH_INFO_NONE;
4101 /* Emit the template for setting o1 */
4103 if (sparc_is_imm13 (((((guint8*)code - cfg->native_code) - throw_ip) >> 2) - 8))
4104 /* Can use a short form */
4107 sparc_set_template (code, sparc_o1);
4111 exc_classes [nthrows] = exc_class;
4112 exc_throw_start [nthrows] = (guint8*)code;
4116 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, mono_sparc_break);
4120 /* first arg = type token */
4121 /* Pass the type index to reduce the size of the sparc_set */
4122 if (!sparc_is_imm13 (type_idx))
4123 sparc_set32 (code, type_idx, sparc_o0);
4125 /* second arg = offset between the throw ip and the current ip */
4126 /* On sparc, the saved ip points to the call instruction */
4127 disp = (((guint8*)code - cfg->native_code) - throw_ip) >> 2;
4128 sparc_set32 (buf, disp, sparc_o1);
4133 exc_throw_end [nthrows] = (guint8*)code;
4137 patch_info->data.name = "mono_arch_throw_corlib_exception";
4138 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
4139 patch_info->ip.i = (guint8*)code - cfg->native_code;
4143 if (sparc_is_imm13 (type_idx)) {
4144 /* Put it into the delay slot */
4147 sparc_set32 (code, type_idx, sparc_o0);
4148 g_assert (code - buf == 1);
4159 cfg->code_len = (guint8*)code - cfg->native_code;
4161 g_assert (cfg->code_len < cfg->code_size);
4165 gboolean lmf_addr_key_inited = FALSE;
4167 #ifdef MONO_SPARC_THR_TLS
4168 thread_key_t lmf_addr_key;
4170 pthread_key_t lmf_addr_key;
4174 mono_arch_get_lmf_addr (void)
4176 /* This is perf critical so we bypass the IO layer */
4177 /* The thr_... functions seem to be somewhat faster */
4178 #ifdef MONO_SPARC_THR_TLS
4180 thr_getspecific (lmf_addr_key, &res);
4183 return pthread_getspecific (lmf_addr_key);
4187 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
4190 * There seems to be no way to determine stack boundaries under solaris,
4191 * so it's not possible to determine whenever a SIGSEGV is caused by stack
4194 #error "--with-sigaltstack=yes not supported on solaris"
4199 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
4201 if (!lmf_addr_key_inited) {
4204 lmf_addr_key_inited = TRUE;
4206 #ifdef MONO_SPARC_THR_TLS
4207 res = thr_keycreate (&lmf_addr_key, NULL);
4209 res = pthread_key_create (&lmf_addr_key, NULL);
4211 g_assert (res == 0);
4215 #ifdef MONO_SPARC_THR_TLS
4216 thr_setspecific (lmf_addr_key, &tls->lmf);
4218 pthread_setspecific (lmf_addr_key, &tls->lmf);
4223 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
4228 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *call, int this_reg, int this_type, int vt_reg)
4230 int this_out_reg = sparc_o0;
4235 MONO_INST_NEW (cfg, ins, OP_MOVE);
4236 ins->sreg1 = vt_reg;
4237 ins->dreg = mono_regstate_next_int (cfg->rs);
4238 mono_bblock_add_inst (cfg->cbb, ins);
4240 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, sparc_o0, FALSE);
4242 this_out_reg = sparc_o1;
4244 /* Set the 'struct/union return pointer' location on the stack */
4245 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, sparc_sp, 64, vt_reg);
4249 /* add the this argument */
4250 if (this_reg != -1) {
4252 MONO_INST_NEW (cfg, this, OP_MOVE);
4253 this->type = this_type;
4254 this->sreg1 = this_reg;
4255 this->dreg = mono_regstate_next_int (cfg->rs);
4256 mono_bblock_add_inst (cfg->cbb, this);
4258 mono_call_inst_add_outarg_reg (cfg, call, this->dreg, this_out_reg, FALSE);
4264 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4266 MonoInst *ins = NULL;
4268 if (cmethod->klass == mono_defaults.thread_class &&
4269 strcmp (cmethod->name, "MemoryBarrier") == 0) {
4271 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
4278 * mono_arch_get_argument_info:
4279 * @csig: a method signature
4280 * @param_count: the number of parameters to consider
4281 * @arg_info: an array to store the result infos
4283 * Gathers information on parameters such as size, alignment and
4284 * padding. arg_info should be large enought to hold param_count + 1 entries.
4286 * Returns the size of the activation frame.
4289 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
4295 cinfo = get_call_info (csig, FALSE);
4297 if (csig->hasthis) {
4298 ainfo = &cinfo->args [0];
4299 arg_info [0].offset = ARGS_OFFSET - MONO_SPARC_STACK_BIAS + ainfo->offset;
4302 for (k = 0; k < param_count; k++) {
4303 ainfo = &cinfo->args [k + csig->hasthis];
4305 arg_info [k + 1].offset = ARGS_OFFSET - MONO_SPARC_STACK_BIAS + ainfo->offset;
4306 arg_info [k + 1].size = mono_type_size (csig->params [k], &align);
4315 mono_arch_print_tree (MonoInst *tree, int arity)
4320 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
4325 MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)